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 class Mp2tStreamParser::AudioBufferWithConfig { | |
|
damienv1
2013/09/17 16:11:52
Use a struct instead.
| |
| 142 public: | |
| 143 scoped_refptr<StreamParserBuffer> buffer; | |
|
damienv1
2013/09/17 16:11:52
AudioDecoderConfig config;
bool is_config_sent;
St
| |
| 144 AudioDecoderConfig config; | |
| 145 }; | |
| 146 | |
| 147 class Mp2tStreamParser::VideoBufferWithConfig { | |
| 148 public: | |
|
damienv1
2013/09/17 16:11:52
Ditto.
| |
| 149 scoped_refptr<StreamParserBuffer> buffer; | |
| 150 VideoDecoderConfig config; | |
| 151 }; | |
| 152 | |
| 153 | |
| 154 Mp2tStreamParser::Mp2tStreamParser() | |
| 155 : selected_audio_pid_(-1), | |
| 156 selected_video_pid_(-1), | |
| 157 is_initialized_(false), | |
| 158 segment_started_(false), | |
| 159 first_video_frame_in_segment_(true) { | |
| 160 } | |
| 161 | |
| 162 Mp2tStreamParser::~Mp2tStreamParser() { | |
| 163 STLDeleteValues(&pids_); | |
| 164 } | |
| 165 | |
| 166 void Mp2tStreamParser::Init( | |
| 167 const InitCB& init_cb, | |
| 168 const NewConfigCB& config_cb, | |
| 169 const NewBuffersCB& new_buffers_cb, | |
| 170 const NewTextBuffersCB& text_cb, | |
| 171 const NeedKeyCB& need_key_cb, | |
| 172 const AddTextTrackCB& add_text_track_cb, | |
| 173 const NewMediaSegmentCB& new_segment_cb, | |
| 174 const base::Closure& end_of_segment_cb, | |
| 175 const LogCB& log_cb) { | |
| 176 DCHECK(!is_initialized_); | |
| 177 DCHECK(init_cb_.is_null()); | |
| 178 DCHECK(!init_cb.is_null()); | |
| 179 DCHECK(!config_cb.is_null()); | |
| 180 DCHECK(!new_buffers_cb.is_null()); | |
| 181 DCHECK(!need_key_cb.is_null()); | |
| 182 DCHECK(!end_of_segment_cb.is_null()); | |
| 183 | |
| 184 init_cb_ = init_cb; | |
| 185 config_cb_ = config_cb; | |
| 186 new_buffers_cb_ = new_buffers_cb; | |
| 187 need_key_cb_ = need_key_cb; | |
| 188 new_segment_cb_ = new_segment_cb; | |
| 189 end_of_segment_cb_ = end_of_segment_cb; | |
| 190 log_cb_ = log_cb; | |
| 191 } | |
| 192 | |
| 193 void Mp2tStreamParser::Flush() { | |
| 194 DVLOG(1) << "Mp2tStreamParser::Flush"; | |
| 195 | |
| 196 // Flush the buffers and reset the pids. | |
| 197 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
| 198 it != pids_.end(); ++it) { | |
| 199 DVLOG(1) << "Flushing PID: " << it->first; | |
| 200 PidState* pid_state = it->second; | |
| 201 pid_state->Flush(); | |
| 202 delete pid_state; | |
| 203 } | |
| 204 pids_.clear(); | |
| 205 if (is_initialized_) { | |
| 206 EmitRemainingBuffers(); | |
| 207 DCHECK(audio_buffer_queue_.empty()); | |
| 208 DCHECK(video_buffer_queue_.empty()); | |
| 209 } else { | |
| 210 audio_buffer_queue_.clear(); | |
| 211 video_buffer_queue_.clear(); | |
| 212 } | |
| 213 | |
| 214 // End of the segment. | |
| 215 // Note: does not need to invoke |end_of_segment_cb_| since flushing the | |
| 216 // stream parser already involves the end of the current segment. | |
| 217 segment_started_ = false; | |
| 218 first_video_frame_in_segment_ = true; | |
| 219 | |
| 220 // Remove any bytes left in the TS buffer. | |
| 221 // (i.e. any partial TS packet => less than 188 bytes). | |
| 222 ts_byte_queue_.Reset(); | |
| 223 | |
| 224 // Reset the selected PIDs. | |
| 225 selected_audio_pid_ = -1; | |
| 226 selected_video_pid_ = -1; | |
| 227 | |
| 228 // Reset the audio and video configs. | |
| 229 audio_config_ = AudioDecoderConfig(); | |
| 230 video_config_ = VideoDecoderConfig(); | |
| 231 last_audio_config_ = AudioDecoderConfig(); | |
| 232 last_video_config_ = VideoDecoderConfig(); | |
| 233 } | |
| 234 | |
| 235 bool Mp2tStreamParser::Parse(const uint8* buf, int size) { | |
| 236 DVLOG(1) << "Mp2tStreamParser::Parse size=" << size; | |
| 237 | |
| 238 // Add the data to the parser state. | |
| 239 ts_byte_queue_.Push(buf, size); | |
| 240 | |
| 241 while (true) { | |
| 242 const uint8* ts_buffer; | |
| 243 int ts_buffer_size; | |
| 244 ts_byte_queue_.Peek(&ts_buffer, &ts_buffer_size); | |
| 245 if (ts_buffer_size < TsPacket::kPacketSize) | |
| 246 break; | |
| 247 | |
| 248 // Synchronization. | |
| 249 int skipped_bytes = TsPacket::Sync(ts_buffer, ts_buffer_size); | |
| 250 if (skipped_bytes > 0) { | |
| 251 DVLOG(1) << "Packet not aligned on a TS syncword:" | |
| 252 << " skipped_bytes=" << skipped_bytes; | |
| 253 ts_byte_queue_.Pop(skipped_bytes); | |
| 254 continue; | |
| 255 } | |
| 256 | |
| 257 // Parse the TS header, skipping 1 byte if the header is invalid. | |
| 258 scoped_ptr<TsPacket> ts_packet(TsPacket::Parse(ts_buffer, ts_buffer_size)); | |
| 259 if (!ts_packet) { | |
| 260 DVLOG(1) << "Error: invalid TS packet"; | |
| 261 ts_byte_queue_.Pop(1); | |
| 262 continue; | |
| 263 } | |
| 264 DVLOG(LOG_LEVEL_TS) | |
| 265 << "Processing PID=" << ts_packet->pid() | |
| 266 << " start_unit=" << ts_packet->payload_unit_start_indicator(); | |
| 267 | |
| 268 // Parse the section. | |
| 269 std::map<int, PidState*>::iterator it = pids_.find(ts_packet->pid()); | |
| 270 if (it == pids_.end() && | |
| 271 ts_packet->pid() == TsSection::kPidPat) { | |
| 272 // Create the PAT state here if needed. | |
| 273 scoped_ptr<TsSection> pat_section_parser( | |
| 274 new TsSectionPat( | |
| 275 base::Bind(&Mp2tStreamParser::RegisterPmt, | |
| 276 base::Unretained(this)))); | |
| 277 scoped_ptr<PidState> pat_pid_state( | |
| 278 new PidState(ts_packet->pid(), PidState::kPidPat, | |
| 279 pat_section_parser.Pass())); | |
| 280 pat_pid_state->Enable(); | |
| 281 it = pids_.insert( | |
| 282 std::pair<int, PidState*>(ts_packet->pid(), | |
| 283 pat_pid_state.release())).first; | |
| 284 } | |
| 285 | |
| 286 if (it != pids_.end()) { | |
| 287 if (!it->second->PushTsPacket(*ts_packet)) | |
| 288 return false; | |
| 289 } else { | |
| 290 DVLOG(LOG_LEVEL_TS) << "Ignoring TS packet for pid: " << ts_packet->pid(); | |
| 291 } | |
| 292 | |
| 293 // Go to the next packet. | |
| 294 ts_byte_queue_.Pop(TsPacket::kPacketSize); | |
| 295 } | |
| 296 | |
| 297 // Emit the A/V buffers that kept accumulating during TS parsing. | |
| 298 EmitRemainingBuffers(); | |
| 299 | |
| 300 return true; | |
| 301 } | |
| 302 | |
| 303 void Mp2tStreamParser::RegisterPmt(int program_number, int pmt_pid) { | |
| 304 DVLOG(1) << "RegisterPmt:" | |
| 305 << " program_number=" << program_number | |
| 306 << " pmt_pid=" << pmt_pid; | |
| 307 | |
| 308 // Only one TS program is allowed. Ignore the incoming program map table, | |
| 309 // if there is already one registered. | |
| 310 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
| 311 it != pids_.end(); ++it) { | |
| 312 PidState* pid_state = it->second; | |
| 313 if (pid_state->pid_type() == PidState::kPidPmt) { | |
| 314 int pid = it->first; | |
| 315 DVLOG_IF(1, pmt_pid != pid) << "More than one program is defined"; | |
| 316 return; | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 // Create the PMT state here if needed. | |
| 321 DVLOG(1) << "Create a new PMT parser"; | |
| 322 scoped_ptr<TsSection> pmt_section_parser( | |
| 323 new TsSectionPmt( | |
| 324 base::Bind(&Mp2tStreamParser::RegisterPes, | |
| 325 base::Unretained(this), pmt_pid))); | |
| 326 scoped_ptr<PidState> pmt_pid_state( | |
| 327 new PidState(pmt_pid, PidState::kPidPmt, pmt_section_parser.Pass())); | |
| 328 pmt_pid_state->Enable(); | |
| 329 pids_.insert(std::pair<int, PidState*>(pmt_pid, pmt_pid_state.release())); | |
| 330 } | |
| 331 | |
| 332 void Mp2tStreamParser::RegisterPes(int pmt_pid, | |
| 333 int pes_pid, | |
| 334 int stream_type) { | |
| 335 // TODO(damienv): check there is no mismatch if the entry already exists. | |
| 336 DVLOG(1) << "RegisterPes:" | |
| 337 << " pes_pid=" << pes_pid | |
| 338 << " stream_type=" << std::hex << stream_type << std::dec; | |
| 339 std::map<int, PidState*>::iterator it = pids_.find(pes_pid); | |
| 340 if (it != pids_.end()) | |
| 341 return; | |
| 342 | |
| 343 // Create a stream parser corresponding to the stream type. | |
| 344 bool is_audio = false; | |
| 345 scoped_ptr<EsParser> es_parser; | |
| 346 if (stream_type == kStreamTypeAVC) { | |
| 347 es_parser.reset( | |
| 348 new EsParserH264( | |
| 349 base::Bind(&Mp2tStreamParser::OnVideoConfigChanged, | |
| 350 base::Unretained(this), | |
| 351 pes_pid), | |
| 352 base::Bind(&Mp2tStreamParser::OnEmitVideoBuffer, | |
| 353 base::Unretained(this), | |
| 354 pes_pid))); | |
| 355 } else if (stream_type == kStreamTypeAAC) { | |
| 356 es_parser.reset( | |
| 357 new EsParserAdts( | |
| 358 base::Bind(&Mp2tStreamParser::OnAudioConfigChanged, | |
| 359 base::Unretained(this), | |
| 360 pes_pid), | |
| 361 base::Bind(&Mp2tStreamParser::OnEmitAudioBuffer, | |
| 362 base::Unretained(this), | |
| 363 pes_pid))); | |
| 364 is_audio = true; | |
| 365 } else { | |
| 366 return; | |
| 367 } | |
| 368 | |
| 369 // Create the PES state here. | |
| 370 DVLOG(1) << "Create a new PES state"; | |
| 371 scoped_ptr<TsSection> pes_section_parser( | |
| 372 new TsSectionPes(es_parser.Pass())); | |
| 373 PidState::PidType pid_type = | |
| 374 is_audio ? PidState::kPidAudioPes : PidState::kPidVideoPes; | |
| 375 scoped_ptr<PidState> pes_pid_state( | |
| 376 new PidState(pes_pid, pid_type, pes_section_parser.Pass())); | |
| 377 pids_.insert(std::pair<int, PidState*>(pes_pid, pes_pid_state.release())); | |
| 378 | |
| 379 // The pid filter must be updated. | |
| 380 UpdatePidFilter(); | |
| 381 } | |
| 382 | |
| 383 void Mp2tStreamParser::UpdatePidFilter() { | |
| 384 // Applies the HLS rule to select the default audio/video PIDs: | |
| 385 // select the audio/video streams with the lowest PID. | |
| 386 // TODO(damienv): this can be changed when the StreamParser interface | |
| 387 // supports multiple audio/video streams. | |
| 388 PidMap::iterator lowest_audio_pid = pids_.end(); | |
| 389 PidMap::iterator lowest_video_pid = pids_.end(); | |
| 390 for (PidMap::iterator it = pids_.begin(); it != pids_.end(); ++it) { | |
| 391 int pid = it->first; | |
| 392 PidState* pid_state = it->second; | |
| 393 if (pid_state->pid_type() == PidState::kPidAudioPes && | |
| 394 (lowest_audio_pid == pids_.end() || pid < lowest_audio_pid->first)) | |
| 395 lowest_audio_pid = it; | |
| 396 if (pid_state->pid_type() == PidState::kPidVideoPes && | |
| 397 (lowest_video_pid == pids_.end() || pid < lowest_video_pid->first)) | |
| 398 lowest_video_pid = it; | |
| 399 } | |
| 400 | |
| 401 // Enable both the lowest audio and video PIDs. | |
| 402 if (lowest_audio_pid != pids_.end()) { | |
| 403 DVLOG(1) << "Enable audio pid: " << lowest_audio_pid->first; | |
| 404 lowest_audio_pid->second->Enable(); | |
| 405 selected_audio_pid_ = lowest_audio_pid->first; | |
| 406 } | |
| 407 if (lowest_video_pid != pids_.end()) { | |
| 408 DVLOG(1) << "Enable video pid: " << lowest_audio_pid->first; | |
| 409 lowest_video_pid->second->Enable(); | |
| 410 selected_video_pid_ = lowest_video_pid->first; | |
| 411 } | |
| 412 | |
| 413 // Disable all the other audio and video PIDs. | |
| 414 for (PidMap::iterator it = pids_.begin(); it != pids_.end(); ++it) { | |
| 415 PidState* pid_state = it->second; | |
| 416 if (it != lowest_audio_pid && it != lowest_video_pid && | |
| 417 (pid_state->pid_type() == PidState::kPidAudioPes || | |
| 418 pid_state->pid_type() == PidState::kPidVideoPes)) | |
| 419 pid_state->Disable(); | |
| 420 } | |
| 421 } | |
| 422 | |
| 423 void Mp2tStreamParser::OnVideoConfigChanged( | |
| 424 int pes_pid, | |
| 425 const VideoDecoderConfig& video_decoder_config) { | |
| 426 DVLOG(1) << "OnVideoConfigChanged for pid=" << pes_pid; | |
| 427 DCHECK_EQ(pes_pid, selected_video_pid_); | |
| 428 | |
| 429 video_config_ = video_decoder_config; | |
| 430 FinishInitializationIfNeeded(); | |
| 431 } | |
| 432 | |
| 433 void Mp2tStreamParser::OnAudioConfigChanged( | |
| 434 int pes_pid, | |
| 435 const AudioDecoderConfig& audio_decoder_config) { | |
| 436 DVLOG(1) << "OnAudioConfigChanged for pid=" << pes_pid; | |
| 437 DCHECK_EQ(pes_pid, selected_audio_pid_); | |
| 438 | |
| 439 audio_config_ = audio_decoder_config; | |
| 440 FinishInitializationIfNeeded(); | |
| 441 } | |
| 442 | |
| 443 void Mp2tStreamParser::FinishInitializationIfNeeded() { | |
| 444 // Nothing to be done if already initialized. | |
| 445 if (is_initialized_) | |
| 446 return; | |
| 447 | |
| 448 // Initialization is done when both the audio decoder config | |
| 449 // and the video decoder config are known | |
| 450 // (for a stream with both audio and video). | |
| 451 if (selected_audio_pid_ > 0 && !audio_config_.IsValidConfig()) | |
| 452 return; | |
| 453 if (selected_video_pid_ > 0 && !video_config_.IsValidConfig()) | |
| 454 return; | |
| 455 | |
| 456 // The audio and video decoder configs passed in the callback | |
| 457 // are the latest audio and video decoder configs. | |
| 458 // This might be different from the configs of the first audio and video | |
| 459 // buffer if we have a sequence like this one in the Mpeg2 TS stream: | |
| 460 // VConfigA VBuffer0 VBuffer1 VConfigB VBuffer2 AConfigA ABuffer0 | |
| 461 // In this case, |audio_config_| corresponds to AConfigA | |
| 462 // and |video_config_| corresponds to VConfigB and not VConfigA. | |
| 463 // This does not matter since the callback will be invoked later before | |
| 464 // emitting any buffers and will thus overwrite the audio/video config. | |
| 465 config_cb_.Run(audio_config_, video_config_); | |
| 466 | |
| 467 // For Mpeg2 TS, the duration is not known. | |
| 468 DVLOG(1) << "Mpeg2TS stream parser initialization done"; | |
| 469 init_cb_.Run(true, kInfiniteDuration()); | |
| 470 is_initialized_ = true; | |
| 471 } | |
| 472 | |
| 473 void Mp2tStreamParser::OnEmitAudioBuffer( | |
| 474 int pes_pid, | |
| 475 scoped_refptr<StreamParserBuffer> stream_parser_buffer) { | |
| 476 DCHECK_EQ(pes_pid, selected_audio_pid_); | |
| 477 | |
| 478 DVLOG(LOG_LEVEL_ES) | |
| 479 << "OnEmitAudioBuffer: " | |
| 480 << " size=" | |
| 481 << stream_parser_buffer->data_size() | |
| 482 << " dts=" | |
| 483 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds() | |
| 484 << " pts=" | |
| 485 << stream_parser_buffer->timestamp().InMilliseconds(); | |
| 486 stream_parser_buffer->set_timestamp( | |
| 487 stream_parser_buffer->timestamp() - time_offset_); | |
| 488 stream_parser_buffer->SetDecodeTimestamp( | |
| 489 stream_parser_buffer->GetDecodeTimestamp() - time_offset_); | |
| 490 | |
| 491 AudioBufferWithConfig audio_buffer_with_config; | |
| 492 audio_buffer_with_config.buffer = stream_parser_buffer; | |
| 493 audio_buffer_with_config.config = audio_config_; | |
| 494 audio_buffer_queue_.push_back(audio_buffer_with_config); | |
| 495 } | |
| 496 | |
| 497 void Mp2tStreamParser::OnEmitVideoBuffer( | |
| 498 int pes_pid, | |
| 499 scoped_refptr<StreamParserBuffer> stream_parser_buffer) { | |
| 500 DCHECK_EQ(pes_pid, selected_video_pid_); | |
| 501 | |
| 502 DVLOG(LOG_LEVEL_ES) | |
| 503 << "OnEmitVideoBuffer" | |
| 504 << " size=" | |
| 505 << stream_parser_buffer->data_size() | |
| 506 << " dts=" | |
| 507 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds() | |
| 508 << " pts=" | |
| 509 << stream_parser_buffer->timestamp().InMilliseconds() | |
| 510 << " IsKeyframe=" | |
| 511 << stream_parser_buffer->IsKeyframe(); | |
| 512 stream_parser_buffer->set_timestamp( | |
| 513 stream_parser_buffer->timestamp() - time_offset_); | |
| 514 stream_parser_buffer->SetDecodeTimestamp( | |
| 515 stream_parser_buffer->GetDecodeTimestamp() - time_offset_); | |
| 516 | |
| 517 VideoBufferWithConfig video_buffer_with_config; | |
| 518 video_buffer_with_config.buffer = stream_parser_buffer; | |
| 519 video_buffer_with_config.config = video_config_; | |
| 520 video_buffer_queue_.push_back(video_buffer_with_config); | |
| 521 } | |
| 522 | |
| 523 void Mp2tStreamParser::EmitRemainingBuffers() { | |
| 524 DVLOG(LOG_LEVEL_ES) << "Mp2tStreamParser::EmitRemainingBuffers"; | |
| 525 if (!is_initialized_) | |
| 526 return; | |
| 527 | |
| 528 while (!audio_buffer_queue_.empty() || !video_buffer_queue_.empty()) { | |
| 529 StreamParser::BufferQueue audio_queue; | |
| 530 StreamParser::BufferQueue video_queue; | |
| 531 AudioDecoderConfig audio_config = GetAudioBuffers(&audio_queue); | |
| 532 VideoDecoderConfig video_config = GetVideoBuffers(&video_queue); | |
| 533 | |
| 534 if (audio_queue.empty() && video_queue.empty()) | |
| 535 break; | |
| 536 | |
| 537 // Start a segment if needed. | |
| 538 if (!segment_started_) { | |
| 539 DVLOG(1) << "Starting a new segment"; | |
| 540 segment_started_ = true; | |
| 541 new_segment_cb_.Run(); | |
| 542 } | |
| 543 | |
| 544 // Update the audio and video config if needed. | |
| 545 bool is_new_config = false; | |
| 546 if (!audio_queue.empty() && !audio_config.Matches(last_audio_config_)) { | |
| 547 last_audio_config_ = audio_config; | |
| 548 is_new_config = true; | |
| 549 } | |
| 550 if (!video_queue.empty() && !video_config.Matches(last_video_config_)) { | |
| 551 last_video_config_ = video_config; | |
| 552 is_new_config = true; | |
| 553 } | |
| 554 if (is_new_config) | |
| 555 config_cb_.Run(last_audio_config_, last_video_config_); | |
| 556 | |
| 557 // Add the buffers. | |
| 558 new_buffers_cb_.Run(audio_queue, video_queue); | |
| 559 } | |
| 560 } | |
| 561 | |
| 562 AudioDecoderConfig Mp2tStreamParser::GetAudioBuffers( | |
| 563 StreamParser::BufferQueue* audio_queue) { | |
| 564 if (audio_buffer_queue_.empty()) | |
| 565 return AudioDecoderConfig(); | |
| 566 | |
| 567 AudioDecoderConfig audio_config = audio_buffer_queue_.front().config; | |
| 568 while (!audio_buffer_queue_.empty() && | |
| 569 audio_buffer_queue_.front().config.Matches(audio_config)) { | |
| 570 audio_queue->push_back(audio_buffer_queue_.front().buffer); | |
| 571 audio_buffer_queue_.pop_front(); | |
| 572 } | |
| 573 return audio_config; | |
| 574 } | |
| 575 | |
| 576 VideoDecoderConfig Mp2tStreamParser::GetVideoBuffers( | |
| 577 StreamParser::BufferQueue* video_queue) { | |
| 578 if (video_buffer_queue_.empty()) | |
| 579 return VideoDecoderConfig(); | |
| 580 | |
| 581 if (first_video_frame_in_segment_) { | |
| 582 // Remove all the leading non key frames. | |
| 583 while (!video_buffer_queue_.empty() && | |
| 584 !video_buffer_queue_.front().buffer->IsKeyframe()) | |
| 585 video_buffer_queue_.pop_front(); | |
| 586 | |
| 587 if (video_buffer_queue_.empty()) | |
| 588 return VideoDecoderConfig(); | |
| 589 } | |
| 590 | |
| 591 VideoDecoderConfig video_config = video_buffer_queue_.front().config; | |
| 592 while (!video_buffer_queue_.empty() && | |
| 593 video_buffer_queue_.front().config.Matches(video_config)) { | |
| 594 video_queue->push_back(video_buffer_queue_.front().buffer); | |
| 595 video_buffer_queue_.pop_front(); | |
| 596 } | |
| 597 first_video_frame_in_segment_ = false; | |
| 598 | |
| 599 return video_config; | |
| 600 } | |
| 601 | |
| 602 } // namespace mp2t | |
| 603 } // namespace media | |
| 604 | |
| OLD | NEW |