Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/mp2t/mp2t_stream_parser.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/stl_util.h" | |
| 10 #include "media/base/audio_decoder_config.h" | |
| 11 #include "media/base/buffers.h" | |
| 12 #include "media/base/stream_parser_buffer.h" | |
| 13 #include "media/base/video_decoder_config.h" | |
| 14 #include "media/mp2t/es_parser.h" | |
| 15 #include "media/mp2t/es_parser_adts.h" | |
| 16 #include "media/mp2t/es_parser_h264.h" | |
| 17 #include "media/mp2t/mp2t_common.h" | |
| 18 #include "media/mp2t/ts_packet.h" | |
| 19 #include "media/mp2t/ts_section.h" | |
| 20 #include "media/mp2t/ts_section_pat.h" | |
| 21 #include "media/mp2t/ts_section_pes.h" | |
| 22 #include "media/mp2t/ts_section_pmt.h" | |
| 23 | |
| 24 namespace media { | |
| 25 namespace mp2t { | |
| 26 | |
| 27 enum StreamType { | |
| 28 // ISO-13818.1 / ITU H.222 Table 2.34 "Stream type assignments" | |
| 29 kStreamTypeMpeg1Audio = 0x3, | |
| 30 kStreamTypeAAC = 0xf, | |
| 31 kStreamTypeAVC = 0x1b, | |
| 32 }; | |
| 33 | |
| 34 class PidState { | |
| 35 public: | |
| 36 enum PidType { | |
| 37 kPidPat, | |
| 38 kPidPmt, | |
| 39 kPidAudioPes, | |
| 40 kPidVideoPes, | |
| 41 }; | |
| 42 | |
| 43 PidState(int pid, PidType pid_tyoe, | |
| 44 scoped_ptr<TsSection> section_parser); | |
| 45 | |
| 46 // Extract the content of the TS packet and parse it. | |
| 47 // Return true if successful. | |
| 48 bool PushTsPacket(const TsPacket& ts_packet); | |
| 49 | |
| 50 // Flush the PID state (possibly emitting some pending frames) | |
| 51 // and reset its state. | |
| 52 void Flush(); | |
| 53 | |
| 54 // Enable/disable the PID. | |
| 55 // Disabling a PID will reset its state and ignore any further incoming TS | |
| 56 // packets. | |
| 57 void Enable(); | |
| 58 void Disable(); | |
| 59 bool IsEnabled() const; | |
| 60 | |
| 61 PidType pid_type() const { return pid_type_; } | |
| 62 | |
| 63 private: | |
| 64 void ResetState(); | |
| 65 | |
| 66 int pid_; | |
| 67 PidType pid_type_; | |
| 68 scoped_ptr<TsSection> section_parser_; | |
| 69 | |
| 70 bool enable_; | |
| 71 | |
| 72 int continuity_counter_; | |
| 73 }; | |
| 74 | |
| 75 PidState::PidState(int pid, PidType pid_type, | |
| 76 scoped_ptr<TsSection> section_parser) | |
| 77 : pid_(pid), | |
| 78 pid_type_(pid_type), | |
| 79 section_parser_(section_parser.Pass()), | |
| 80 enable_(false), | |
| 81 continuity_counter_(-1) { | |
| 82 DCHECK(section_parser_); | |
| 83 } | |
| 84 | |
| 85 bool PidState::PushTsPacket(const TsPacket& ts_packet) { | |
| 86 DCHECK_EQ(ts_packet.pid(), pid_); | |
| 87 | |
| 88 // The current PID is not part of the PID filter, | |
| 89 // just discard the incoming TS packet. | |
| 90 if (!enable_) | |
| 91 return true; | |
| 92 | |
| 93 int expected_continuity_counter = (continuity_counter_ + 1) % 16; | |
| 94 if (continuity_counter_ >= 0 && | |
| 95 ts_packet.continuity_counter() != expected_continuity_counter) { | |
| 96 DVLOG(1) << "TS discontinuity detected for pid: " << pid_; | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 bool status = section_parser_->Parse( | |
| 101 ts_packet.payload_unit_start_indicator(), | |
| 102 ts_packet.payload(), | |
| 103 ts_packet.payload_size()); | |
| 104 | |
| 105 // At the minimum, when parsing failed, auto reset the section parser. | |
| 106 // Components that use the StreamParser can take further action if needed. | |
| 107 if (!status) { | |
| 108 DVLOG(1) << "Parsing failed for pid = " << pid_; | |
| 109 ResetState(); | |
| 110 } | |
| 111 | |
| 112 return status; | |
| 113 } | |
| 114 | |
| 115 void PidState::Flush() { | |
| 116 section_parser_->Flush(); | |
| 117 ResetState(); | |
| 118 } | |
| 119 | |
| 120 void PidState::Enable() { | |
| 121 enable_ = true; | |
| 122 } | |
| 123 | |
| 124 void PidState::Disable() { | |
| 125 if (!enable_) | |
| 126 return; | |
| 127 | |
| 128 ResetState(); | |
| 129 enable_ = false; | |
| 130 } | |
| 131 | |
| 132 bool PidState::IsEnabled() const { | |
| 133 return enable_; | |
| 134 } | |
| 135 | |
| 136 void PidState::ResetState() { | |
| 137 section_parser_->Reset(); | |
| 138 continuity_counter_ = -1; | |
| 139 } | |
| 140 | |
| 141 Mp2tStreamParser::Mp2tStreamParser() | |
| 142 : selected_audio_pid_(-1), | |
| 143 selected_video_pid_(-1), | |
| 144 is_initialized_(false), | |
| 145 segment_started_(false), | |
| 146 first_video_frame_in_segment_(true) { | |
| 147 } | |
| 148 | |
| 149 Mp2tStreamParser::~Mp2tStreamParser() { | |
| 150 STLDeleteValues(&pids_); | |
| 151 } | |
| 152 | |
| 153 void Mp2tStreamParser::Init( | |
| 154 const InitCB& init_cb, | |
| 155 const NewConfigCB& config_cb, | |
| 156 const NewBuffersCB& new_buffers_cb, | |
| 157 const NewTextBuffersCB& text_cb, | |
| 158 const NeedKeyCB& need_key_cb, | |
| 159 const AddTextTrackCB& add_text_track_cb, | |
| 160 const NewMediaSegmentCB& new_segment_cb, | |
| 161 const base::Closure& end_of_segment_cb, | |
| 162 const LogCB& log_cb) { | |
| 163 DCHECK(!is_initialized_); | |
| 164 DCHECK(init_cb_.is_null()); | |
| 165 DCHECK(!init_cb.is_null()); | |
| 166 DCHECK(!config_cb.is_null()); | |
| 167 DCHECK(!new_buffers_cb.is_null()); | |
| 168 DCHECK(!need_key_cb.is_null()); | |
| 169 DCHECK(!end_of_segment_cb.is_null()); | |
| 170 | |
| 171 init_cb_ = init_cb; | |
| 172 config_cb_ = config_cb; | |
| 173 new_buffers_cb_ = new_buffers_cb; | |
| 174 need_key_cb_ = need_key_cb; | |
| 175 new_segment_cb_ = new_segment_cb; | |
| 176 end_of_segment_cb_ = end_of_segment_cb; | |
| 177 log_cb_ = log_cb; | |
| 178 } | |
| 179 | |
| 180 void Mp2tStreamParser::Flush() { | |
| 181 DVLOG(1) << "Mp2tStreamParser::Flush"; | |
| 182 | |
| 183 // Flush the buffers and reset the pids. | |
| 184 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
| 185 it != pids_.end(); ++it) { | |
| 186 DVLOG(1) << "Flushing PID: " << it->first; | |
| 187 PidState* pid_state = it->second; | |
| 188 pid_state->Flush(); | |
| 189 delete pid_state; | |
| 190 } | |
| 191 pids_.clear(); | |
| 192 EmitRemainingBuffers(); | |
| 193 buffer_queue_chain_.clear(); | |
| 194 | |
| 195 // End of the segment. | |
| 196 // Note: does not need to invoke |end_of_segment_cb_| since flushing the | |
| 197 // stream parser already involves the end of the current segment. | |
| 198 segment_started_ = false; | |
| 199 first_video_frame_in_segment_ = true; | |
| 200 | |
| 201 // Remove any bytes left in the TS buffer. | |
| 202 // (i.e. any partial TS packet => less than 188 bytes). | |
| 203 ts_byte_queue_.Reset(); | |
| 204 | |
| 205 // Reset the selected PIDs. | |
| 206 selected_audio_pid_ = -1; | |
| 207 selected_video_pid_ = -1; | |
| 208 | |
| 209 // Reset the audio and video configs. | |
| 210 last_audio_config_ = AudioDecoderConfig(); | |
| 211 last_video_config_ = VideoDecoderConfig(); | |
| 212 } | |
| 213 | |
| 214 bool Mp2tStreamParser::Parse(const uint8* buf, int size) { | |
| 215 DVLOG(1) << "Mp2tStreamParser::Parse size=" << size; | |
| 216 | |
| 217 // Add the data to the parser state. | |
| 218 ts_byte_queue_.Push(buf, size); | |
| 219 | |
| 220 while (true) { | |
| 221 const uint8* ts_buffer; | |
| 222 int ts_buffer_size; | |
| 223 ts_byte_queue_.Peek(&ts_buffer, &ts_buffer_size); | |
| 224 if (ts_buffer_size < TsPacket::kPacketSize) | |
| 225 break; | |
| 226 | |
| 227 // Synchronization. | |
| 228 int skipped_bytes = TsPacket::Sync(ts_buffer, ts_buffer_size); | |
| 229 if (skipped_bytes > 0) { | |
| 230 DVLOG(1) << "Packet not aligned on a TS syncword:" | |
| 231 << " skipped_bytes=" << skipped_bytes; | |
| 232 ts_byte_queue_.Pop(skipped_bytes); | |
| 233 continue; | |
| 234 } | |
| 235 | |
| 236 // Parse the TS header, skipping 1 byte if the header is invalid. | |
| 237 scoped_ptr<TsPacket> ts_packet(TsPacket::Parse(ts_buffer, ts_buffer_size)); | |
| 238 if (!ts_packet) { | |
| 239 DVLOG(1) << "Error: invalid TS packet"; | |
| 240 ts_byte_queue_.Pop(1); | |
| 241 continue; | |
| 242 } | |
| 243 DVLOG(LOG_LEVEL_TS) | |
| 244 << "Processing PID=" << ts_packet->pid() | |
| 245 << " start_unit=" << ts_packet->payload_unit_start_indicator(); | |
| 246 | |
| 247 // Parse the section. | |
| 248 std::map<int, PidState*>::iterator it = pids_.find(ts_packet->pid()); | |
| 249 if (it == pids_.end() && | |
| 250 ts_packet->pid() == TsSection::kPidPat) { | |
| 251 // Create the PAT state here if needed. | |
| 252 scoped_ptr<TsSection> pat_section_parser( | |
| 253 new TsSectionPat( | |
| 254 base::Bind(&Mp2tStreamParser::RegisterPmt, | |
| 255 base::Unretained(this)))); | |
| 256 scoped_ptr<PidState> pat_pid_state( | |
| 257 new PidState(ts_packet->pid(), PidState::kPidPat, | |
| 258 pat_section_parser.Pass())); | |
| 259 pat_pid_state->Enable(); | |
| 260 it = pids_.insert( | |
| 261 std::pair<int, PidState*>(ts_packet->pid(), | |
| 262 pat_pid_state.release())).first; | |
| 263 } | |
| 264 | |
| 265 if (it != pids_.end()) { | |
| 266 if (!it->second->PushTsPacket(*ts_packet)) | |
| 267 return false; | |
| 268 } else { | |
| 269 DVLOG(LOG_LEVEL_TS) << "Ignoring TS packet for pid: " << ts_packet->pid(); | |
| 270 } | |
| 271 | |
| 272 // Go to the next packet. | |
| 273 ts_byte_queue_.Pop(TsPacket::kPacketSize); | |
| 274 } | |
| 275 | |
| 276 RCHECK(FinishInitializationIfNeeded()); | |
| 277 | |
| 278 // Emit the A/V buffers that kept accumulating during TS parsing. | |
| 279 RCHECK(EmitRemainingBuffers()); | |
|
acolwell GONE FROM CHROMIUM
2013/09/18 22:38:55
nit: Just return instead of RCHECK.
damienv1
2013/09/19 00:43:25
Done.
| |
| 280 | |
| 281 return true; | |
| 282 } | |
| 283 | |
| 284 void Mp2tStreamParser::RegisterPmt(int program_number, int pmt_pid) { | |
| 285 DVLOG(1) << "RegisterPmt:" | |
| 286 << " program_number=" << program_number | |
| 287 << " pmt_pid=" << pmt_pid; | |
| 288 | |
| 289 // Only one TS program is allowed. Ignore the incoming program map table, | |
| 290 // if there is already one registered. | |
| 291 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
| 292 it != pids_.end(); ++it) { | |
| 293 PidState* pid_state = it->second; | |
| 294 if (pid_state->pid_type() == PidState::kPidPmt) { | |
| 295 int pid = it->first; | |
| 296 DVLOG_IF(1, pmt_pid != pid) << "More than one program is defined"; | |
| 297 return; | |
| 298 } | |
| 299 } | |
| 300 | |
| 301 // Create the PMT state here if needed. | |
| 302 DVLOG(1) << "Create a new PMT parser"; | |
| 303 scoped_ptr<TsSection> pmt_section_parser( | |
| 304 new TsSectionPmt( | |
| 305 base::Bind(&Mp2tStreamParser::RegisterPes, | |
| 306 base::Unretained(this), pmt_pid))); | |
| 307 scoped_ptr<PidState> pmt_pid_state( | |
| 308 new PidState(pmt_pid, PidState::kPidPmt, pmt_section_parser.Pass())); | |
| 309 pmt_pid_state->Enable(); | |
| 310 pids_.insert(std::pair<int, PidState*>(pmt_pid, pmt_pid_state.release())); | |
| 311 } | |
| 312 | |
| 313 void Mp2tStreamParser::RegisterPes(int pmt_pid, | |
| 314 int pes_pid, | |
| 315 int stream_type) { | |
| 316 // TODO(damienv): check there is no mismatch if the entry already exists. | |
| 317 DVLOG(1) << "RegisterPes:" | |
| 318 << " pes_pid=" << pes_pid | |
| 319 << " stream_type=" << std::hex << stream_type << std::dec; | |
| 320 std::map<int, PidState*>::iterator it = pids_.find(pes_pid); | |
| 321 if (it != pids_.end()) | |
| 322 return; | |
| 323 | |
| 324 // Create a stream parser corresponding to the stream type. | |
| 325 bool is_audio = false; | |
| 326 scoped_ptr<EsParser> es_parser; | |
| 327 if (stream_type == kStreamTypeAVC) { | |
| 328 es_parser.reset( | |
| 329 new EsParserH264( | |
| 330 base::Bind(&Mp2tStreamParser::OnVideoConfigChanged, | |
| 331 base::Unretained(this), | |
| 332 pes_pid), | |
| 333 base::Bind(&Mp2tStreamParser::OnEmitVideoBuffer, | |
| 334 base::Unretained(this), | |
| 335 pes_pid))); | |
| 336 } else if (stream_type == kStreamTypeAAC) { | |
| 337 es_parser.reset( | |
| 338 new EsParserAdts( | |
| 339 base::Bind(&Mp2tStreamParser::OnAudioConfigChanged, | |
| 340 base::Unretained(this), | |
| 341 pes_pid), | |
| 342 base::Bind(&Mp2tStreamParser::OnEmitAudioBuffer, | |
| 343 base::Unretained(this), | |
| 344 pes_pid))); | |
| 345 is_audio = true; | |
| 346 } else { | |
| 347 return; | |
| 348 } | |
| 349 | |
| 350 // Create the PES state here. | |
| 351 DVLOG(1) << "Create a new PES state"; | |
| 352 scoped_ptr<TsSection> pes_section_parser( | |
| 353 new TsSectionPes(es_parser.Pass())); | |
| 354 PidState::PidType pid_type = | |
| 355 is_audio ? PidState::kPidAudioPes : PidState::kPidVideoPes; | |
| 356 scoped_ptr<PidState> pes_pid_state( | |
| 357 new PidState(pes_pid, pid_type, pes_section_parser.Pass())); | |
| 358 pids_.insert(std::pair<int, PidState*>(pes_pid, pes_pid_state.release())); | |
| 359 | |
| 360 // A new PES pid has been added, the PID filter might change. | |
| 361 UpdatePidFilter(); | |
| 362 } | |
| 363 | |
| 364 void Mp2tStreamParser::UpdatePidFilter() { | |
| 365 // Applies the HLS rule to select the default audio/video PIDs: | |
| 366 // select the audio/video streams with the lowest PID. | |
| 367 // TODO(damienv): this can be changed when the StreamParser interface | |
| 368 // supports multiple audio/video streams. | |
| 369 PidMap::iterator lowest_audio_pid = pids_.end(); | |
| 370 PidMap::iterator lowest_video_pid = pids_.end(); | |
| 371 for (PidMap::iterator it = pids_.begin(); it != pids_.end(); ++it) { | |
| 372 int pid = it->first; | |
| 373 PidState* pid_state = it->second; | |
| 374 if (pid_state->pid_type() == PidState::kPidAudioPes && | |
| 375 (lowest_audio_pid == pids_.end() || pid < lowest_audio_pid->first)) | |
| 376 lowest_audio_pid = it; | |
| 377 if (pid_state->pid_type() == PidState::kPidVideoPes && | |
| 378 (lowest_video_pid == pids_.end() || pid < lowest_video_pid->first)) | |
| 379 lowest_video_pid = it; | |
| 380 } | |
| 381 | |
| 382 // Enable both the lowest audio and video PIDs. | |
| 383 if (lowest_audio_pid != pids_.end()) { | |
| 384 DVLOG(1) << "Enable audio pid: " << lowest_audio_pid->first; | |
| 385 lowest_audio_pid->second->Enable(); | |
| 386 selected_audio_pid_ = lowest_audio_pid->first; | |
| 387 } | |
| 388 if (lowest_video_pid != pids_.end()) { | |
| 389 DVLOG(1) << "Enable video pid: " << lowest_audio_pid->first; | |
| 390 lowest_video_pid->second->Enable(); | |
| 391 selected_video_pid_ = lowest_video_pid->first; | |
| 392 } | |
| 393 | |
| 394 // Disable all the other audio and video PIDs. | |
| 395 for (PidMap::iterator it = pids_.begin(); it != pids_.end(); ++it) { | |
| 396 PidState* pid_state = it->second; | |
| 397 if (it != lowest_audio_pid && it != lowest_video_pid && | |
| 398 (pid_state->pid_type() == PidState::kPidAudioPes || | |
| 399 pid_state->pid_type() == PidState::kPidVideoPes)) | |
| 400 pid_state->Disable(); | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 void Mp2tStreamParser::OnVideoConfigChanged( | |
| 405 int pes_pid, | |
| 406 const VideoDecoderConfig& video_decoder_config) { | |
| 407 DVLOG(1) << "OnVideoConfigChanged for pid=" << pes_pid; | |
| 408 DCHECK_EQ(pes_pid, selected_video_pid_); | |
| 409 | |
|
acolwell GONE FROM CHROMIUM
2013/09/18 22:38:55
nit: DCHECK(video_decoder_config.IsValid()) since
damienv1
2013/09/19 00:43:25
Done.
| |
| 410 // Create a new entry in |buffer_queue_chain_| with the updated configs. | |
| 411 BufferQueueWithConfig buffer_queue_with_config; | |
| 412 buffer_queue_with_config.is_config_sent = false; | |
| 413 buffer_queue_with_config.video_config = video_decoder_config; | |
| 414 if (!buffer_queue_chain_.empty()) { | |
| 415 buffer_queue_with_config.audio_config = | |
| 416 buffer_queue_chain_.back().audio_config; | |
| 417 } | |
| 418 buffer_queue_chain_.push_back(buffer_queue_with_config); | |
| 419 | |
| 420 // Replace any non valid config with the 1st valid entry. | |
| 421 // This might happen if there was no available config before. | |
| 422 for (std::list<BufferQueueWithConfig>::iterator it = | |
| 423 buffer_queue_chain_.begin(); it != buffer_queue_chain_.end(); ++it) { | |
| 424 if (it->video_config.IsValidConfig()) | |
| 425 break; | |
| 426 it->video_config = video_decoder_config; | |
| 427 } | |
| 428 } | |
| 429 | |
| 430 void Mp2tStreamParser::OnAudioConfigChanged( | |
| 431 int pes_pid, | |
| 432 const AudioDecoderConfig& audio_decoder_config) { | |
| 433 DVLOG(1) << "OnAudioConfigChanged for pid=" << pes_pid; | |
| 434 DCHECK_EQ(pes_pid, selected_audio_pid_); | |
| 435 | |
|
acolwell GONE FROM CHROMIUM
2013/09/18 22:38:55
nit: DCHECK(audio_decoder_config.IsValid()) .
damienv1
2013/09/19 00:43:25
Done.
| |
| 436 // Create a new entry in |buffer_queue_chain_| with the updated configs. | |
| 437 BufferQueueWithConfig buffer_queue_with_config; | |
| 438 buffer_queue_with_config.is_config_sent = false; | |
| 439 buffer_queue_with_config.audio_config = audio_decoder_config; | |
| 440 if (!buffer_queue_chain_.empty()) { | |
| 441 buffer_queue_with_config.video_config = | |
| 442 buffer_queue_chain_.back().video_config; | |
| 443 } | |
| 444 buffer_queue_chain_.push_back(buffer_queue_with_config); | |
| 445 | |
| 446 // Replace any non valid config with the 1st valid entry. | |
| 447 // This might happen if there was no available config before. | |
| 448 for (std::list<BufferQueueWithConfig>::iterator it = | |
| 449 buffer_queue_chain_.begin(); it != buffer_queue_chain_.end(); ++it) { | |
| 450 if (it->audio_config.IsValidConfig()) | |
| 451 break; | |
| 452 it->audio_config = audio_decoder_config; | |
| 453 } | |
| 454 } | |
| 455 | |
| 456 bool Mp2tStreamParser::FinishInitializationIfNeeded() { | |
| 457 // Nothing to be done if already initialized. | |
| 458 if (is_initialized_) | |
| 459 return true; | |
| 460 | |
| 461 // Wait for more data to come to finish initialization. | |
| 462 if (buffer_queue_chain_.empty()) | |
| 463 return true; | |
| 464 | |
| 465 // Wait for more data to come if one of the config is not available. | |
| 466 BufferQueueWithConfig& queue_with_config = buffer_queue_chain_.front(); | |
| 467 if (selected_audio_pid_ > 0 && | |
| 468 !queue_with_config.audio_config.IsValidConfig()) | |
| 469 return true; | |
| 470 if (selected_video_pid_ > 0 && | |
| 471 !queue_with_config.video_config.IsValidConfig()) | |
| 472 return true; | |
| 473 | |
| 474 // Pass the config before invoking the initialization callback. | |
| 475 RCHECK(config_cb_.Run(queue_with_config.audio_config, | |
| 476 queue_with_config.video_config)); | |
| 477 queue_with_config.is_config_sent = true; | |
| 478 | |
| 479 // For Mpeg2 TS, the duration is not known. | |
| 480 DVLOG(1) << "Mpeg2TS stream parser initialization done"; | |
| 481 init_cb_.Run(true, kInfiniteDuration()); | |
| 482 is_initialized_ = true; | |
| 483 | |
| 484 return true; | |
| 485 } | |
| 486 | |
| 487 void Mp2tStreamParser::OnEmitAudioBuffer( | |
| 488 int pes_pid, | |
| 489 scoped_refptr<StreamParserBuffer> stream_parser_buffer) { | |
| 490 DCHECK_EQ(pes_pid, selected_audio_pid_); | |
| 491 | |
| 492 DVLOG(LOG_LEVEL_ES) | |
| 493 << "OnEmitAudioBuffer: " | |
| 494 << " size=" | |
| 495 << stream_parser_buffer->data_size() | |
| 496 << " dts=" | |
| 497 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds() | |
| 498 << " pts=" | |
| 499 << stream_parser_buffer->timestamp().InMilliseconds(); | |
| 500 stream_parser_buffer->set_timestamp( | |
| 501 stream_parser_buffer->timestamp() - time_offset_); | |
| 502 stream_parser_buffer->SetDecodeTimestamp( | |
| 503 stream_parser_buffer->GetDecodeTimestamp() - time_offset_); | |
| 504 | |
| 505 // Ignore the incoming buffer if it is not associated with any config. | |
| 506 if (buffer_queue_chain_.empty()) { | |
| 507 DVLOG(1) << "Ignoring audio buffer with no corresponding audio config"; | |
| 508 return; | |
| 509 } | |
| 510 | |
| 511 buffer_queue_chain_.back().audio_queue.push_back(stream_parser_buffer); | |
| 512 } | |
| 513 | |
| 514 void Mp2tStreamParser::OnEmitVideoBuffer( | |
| 515 int pes_pid, | |
| 516 scoped_refptr<StreamParserBuffer> stream_parser_buffer) { | |
| 517 DCHECK_EQ(pes_pid, selected_video_pid_); | |
| 518 | |
| 519 DVLOG(LOG_LEVEL_ES) | |
| 520 << "OnEmitVideoBuffer" | |
| 521 << " size=" | |
| 522 << stream_parser_buffer->data_size() | |
| 523 << " dts=" | |
| 524 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds() | |
| 525 << " pts=" | |
| 526 << stream_parser_buffer->timestamp().InMilliseconds() | |
| 527 << " IsKeyframe=" | |
| 528 << stream_parser_buffer->IsKeyframe(); | |
| 529 stream_parser_buffer->set_timestamp( | |
| 530 stream_parser_buffer->timestamp() - time_offset_); | |
| 531 stream_parser_buffer->SetDecodeTimestamp( | |
| 532 stream_parser_buffer->GetDecodeTimestamp() - time_offset_); | |
| 533 | |
| 534 // Ignore the incoming buffer if it is not associated with any config. | |
| 535 if (buffer_queue_chain_.empty()) { | |
| 536 DVLOG(1) << "Ignoring video buffer with no corresponding video config:" | |
| 537 << " keyframe=" << stream_parser_buffer->IsKeyframe() | |
| 538 << " dts=" | |
| 539 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds(); | |
| 540 return; | |
| 541 } | |
| 542 | |
| 543 // A segment cannot start with a non key frame. | |
| 544 // Ignore the frame if that's the case. | |
| 545 if (first_video_frame_in_segment_ && !stream_parser_buffer->IsKeyframe()) { | |
| 546 DVLOG(1) << "Ignoring non-key frame:" | |
| 547 << " dts=" | |
| 548 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds(); | |
| 549 return; | |
| 550 } | |
| 551 | |
| 552 first_video_frame_in_segment_ = false; | |
| 553 buffer_queue_chain_.back().video_queue.push_back(stream_parser_buffer); | |
| 554 } | |
| 555 | |
| 556 bool Mp2tStreamParser::EmitRemainingBuffers() { | |
| 557 DVLOG(LOG_LEVEL_ES) << "Mp2tStreamParser::EmitRemainingBuffers"; | |
| 558 | |
| 559 // No buffer should be sent until fully initialized. | |
| 560 if (!is_initialized_) | |
| 561 return true; | |
| 562 | |
| 563 // Buffer emission. | |
| 564 while (!buffer_queue_chain_.empty()) { | |
| 565 // Start a segment if needed. | |
| 566 if (!segment_started_) { | |
| 567 DVLOG(1) << "Starting a new segment"; | |
| 568 segment_started_ = true; | |
| 569 new_segment_cb_.Run(); | |
| 570 } | |
| 571 | |
| 572 // Update the audio and video config if needed. | |
| 573 BufferQueueWithConfig& queue_with_config = buffer_queue_chain_.front(); | |
| 574 if (!queue_with_config.is_config_sent) { | |
| 575 last_audio_config_ = queue_with_config.audio_config; | |
|
acolwell GONE FROM CHROMIUM
2013/09/18 22:38:55
nit: It looks like these don't need to be member v
damienv1
2013/09/19 00:43:25
Done.
| |
| 576 last_video_config_ = queue_with_config.video_config; | |
| 577 DCHECK(last_audio_config_.IsValidConfig()); | |
| 578 DCHECK(last_video_config_.IsValidConfig()); | |
| 579 if (!config_cb_.Run(last_audio_config_, last_video_config_)) | |
| 580 return false; | |
| 581 queue_with_config.is_config_sent = true; | |
| 582 } | |
| 583 | |
| 584 // Add buffers. | |
| 585 if (!queue_with_config.audio_queue.empty() || | |
| 586 !queue_with_config.video_queue.empty()) { | |
| 587 if (!new_buffers_cb_.Run(queue_with_config.audio_queue, | |
| 588 queue_with_config.video_queue)) { | |
| 589 return false; | |
| 590 } | |
| 591 } | |
| 592 | |
| 593 buffer_queue_chain_.pop_front(); | |
| 594 } | |
| 595 | |
| 596 // Push an empty queue with the last audio/video config | |
| 597 // so that buffers with the same config can be added later on. | |
| 598 BufferQueueWithConfig queue_with_config; | |
| 599 queue_with_config.is_config_sent = true; | |
| 600 queue_with_config.audio_config = last_audio_config_; | |
| 601 queue_with_config.video_config = last_video_config_; | |
| 602 buffer_queue_chain_.push_back(queue_with_config); | |
| 603 | |
| 604 return true; | |
| 605 } | |
| 606 | |
| 607 } // namespace mp2t | |
| 608 } // namespace media | |
| 609 | |
| OLD | NEW |