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(TsPacket* ts_packet); | |
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: const TsPacket& since NULL shouldn't be allow
damienv1
2013/09/17 02:58:22
Done.
| |
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(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->GetPayload(), | |
103 ts_packet->GetPayloadSize()); | |
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 { | |
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
I am still not a fan of this. The decoder configs
damienv1
2013/09/17 02:58:22
I need to think a bit more about it. Will do it in
| |
142 public: | |
143 scoped_refptr<StreamParserBuffer> buffer; | |
144 AudioDecoderConfig config; | |
145 }; | |
146 | |
147 class Mp2tStreamParser::VideoBufferWithConfig { | |
148 public: | |
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 bool status = it->second->PushTsPacket(ts_packet.get()); | |
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: inline below.
damienv1
2013/09/17 02:58:22
Done.
| |
288 if (!status) | |
289 return false; | |
290 } else { | |
291 DVLOG(LOG_LEVEL_TS) << "Ignoring TS packet for pid: " << ts_packet->pid(); | |
292 } | |
293 | |
294 // Go to the next packet. | |
295 ts_byte_queue_.Pop(TsPacket::kPacketSize); | |
296 } | |
297 | |
298 // Emit the A/V buffers that kept accumulating during TS parsing. | |
299 EmitRemainingBuffers(); | |
300 | |
301 return true; | |
302 } | |
303 | |
304 void Mp2tStreamParser::RegisterPmt(int program_number, int pmt_pid) { | |
305 DVLOG(1) << "RegisterPmt:" | |
306 << " program_number=" << program_number | |
307 << " pmt_pid=" << pmt_pid; | |
308 | |
309 // Only one TS program is allowed. Ignore the incoming program map table, | |
310 // if there is already one registered. | |
311 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
312 it != pids_.end(); ++it) { | |
313 PidState* pid_state = it->second; | |
314 if (pid_state->pid_type() == PidState::kPidPmt) { | |
315 int pid = it->first; | |
316 LOG_IF(WARNING, pmt_pid != pid) << "More than one program is defined"; | |
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: convert to DVLOG
damienv1
2013/09/17 02:58:22
Done.
| |
317 return; | |
318 } | |
319 } | |
320 | |
321 // Create the PMT state here if needed. | |
322 DVLOG(1) << "Create a new PMT parser"; | |
323 scoped_ptr<TsSection> pmt_section_parser( | |
324 new TsSectionPmt( | |
325 base::Bind(&Mp2tStreamParser::RegisterPes, | |
326 base::Unretained(this), pmt_pid))); | |
327 scoped_ptr<PidState> pmt_pid_state( | |
328 new PidState(pmt_pid, PidState::kPidPmt, pmt_section_parser.Pass())); | |
329 pmt_pid_state->Enable(); | |
330 pids_.insert(std::pair<int, PidState*>(pmt_pid, pmt_pid_state.release())); | |
331 } | |
332 | |
333 void Mp2tStreamParser::RegisterPes(int pmt_pid, | |
334 int pes_pid, | |
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: fix indent
damienv1
2013/09/17 02:58:22
Done.
| |
335 int stream_type) { | |
336 // TODO(damienv): check there is no mismatch if the entry already exists. | |
337 DVLOG(1) << "RegisterPes:" | |
338 << " pes_pid=" << pes_pid | |
339 << " stream_type=" << std::hex << stream_type << std::dec; | |
340 std::map<int, PidState*>::iterator it = pids_.find(pes_pid); | |
341 if (it != pids_.end()) | |
342 return; | |
343 | |
344 // Create a stream parser corresponding to the stream type. | |
345 bool is_audio = false; | |
346 scoped_ptr<EsParser> es_parser; | |
347 if (stream_type == kStreamTypeAVC) { | |
348 es_parser.reset( | |
349 new EsParserH264( | |
350 base::Bind(&Mp2tStreamParser::OnVideoConfigChanged, | |
351 base::Unretained(this), | |
352 pes_pid), | |
353 base::Bind(&Mp2tStreamParser::OnEmitVideoBuffer, | |
354 base::Unretained(this), | |
355 pes_pid))); | |
356 } else if (stream_type == kStreamTypeAAC) { | |
357 es_parser.reset( | |
358 new EsParserAdts( | |
359 base::Bind(&Mp2tStreamParser::OnAudioConfigChanged, | |
360 base::Unretained(this), | |
361 pes_pid), | |
362 base::Bind(&Mp2tStreamParser::OnEmitAudioBuffer, | |
363 base::Unretained(this), | |
364 pes_pid))); | |
365 is_audio = true; | |
366 } else { | |
367 return; | |
368 } | |
369 | |
370 // Create the PES state here. | |
371 DVLOG(1) << "Create a new PES state"; | |
372 scoped_ptr<TsSection> pes_section_parser( | |
373 new TsSectionPes(es_parser.Pass())); | |
374 PidState::PidType pid_type = | |
375 is_audio ? PidState::kPidAudioPes : PidState::kPidVideoPes; | |
376 scoped_ptr<PidState> pes_pid_state( | |
377 new PidState(pes_pid, pid_type, pes_section_parser.Pass())); | |
378 pids_.insert(std::pair<int, PidState*>(pes_pid, pes_pid_state.release())); | |
379 | |
380 // The pid filter must be updated. | |
381 UpdatePidFilter(); | |
382 } | |
383 | |
384 void Mp2tStreamParser::UpdatePidFilter() { | |
385 // Applies the HLS rule to select the default audio/video PIDs: | |
386 // select the pid with the lowest PID. | |
387 // TODO(damienv): this can be changed when the StreamParser interface | |
388 // supports multiple audio/video streams. | |
389 std::map<int, PidState*>::iterator lowest_audio_pid = pids_.end(); | |
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: use PidMap here and elsewhere below.
damienv1
2013/09/17 02:58:22
Done.
| |
390 std::map<int, PidState*>::iterator lowest_video_pid = pids_.end(); | |
391 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
392 it != pids_.end(); ++it) { | |
393 int pid = it->first; | |
394 PidState* pid_state = it->second; | |
395 if (pid_state->pid_type() == PidState::kPidAudioPes && | |
396 ((lowest_audio_pid == pids_.end() || pid < lowest_audio_pid->first))) | |
397 lowest_audio_pid = it; | |
398 if (pid_state->pid_type() == PidState::kPidVideoPes && | |
399 ((lowest_video_pid == pids_.end() || pid < lowest_video_pid->first))) | |
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: It looks like you have an extra set of () her
damienv1
2013/09/17 02:58:22
Done.
| |
400 lowest_video_pid = it; | |
401 } | |
402 | |
403 // Enable both the lowest audio and video PIDs. | |
404 if (lowest_audio_pid != pids_.end()) { | |
405 DVLOG(1) << "Enable audio pid: " << lowest_audio_pid->first; | |
406 lowest_audio_pid->second->Enable(); | |
407 selected_audio_pid_ = lowest_audio_pid->first; | |
408 } | |
409 if (lowest_video_pid != pids_.end()) { | |
410 DVLOG(1) << "Enable video pid: " << lowest_audio_pid->first; | |
411 lowest_video_pid->second->Enable(); | |
412 selected_video_pid_ = lowest_video_pid->first; | |
413 } | |
414 | |
415 // Disable all the other audio and video PIDs. | |
416 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
417 it != pids_.end(); ++it) { | |
418 PidState* pid_state = it->second; | |
419 if (it != lowest_audio_pid && it != lowest_video_pid && | |
420 (pid_state->pid_type() == PidState::kPidAudioPes || | |
421 pid_state->pid_type() == PidState::kPidVideoPes)) | |
422 pid_state->Disable(); | |
423 } | |
424 } | |
425 | |
426 void Mp2tStreamParser::OnVideoConfigChanged( | |
427 int pes_pid, | |
428 const VideoDecoderConfig& video_decoder_config) { | |
429 DVLOG(1) << "OnVideoConfigChanged for pid=" << pes_pid; | |
430 DCHECK_EQ(pes_pid, selected_video_pid_); | |
431 | |
432 video_config_ = video_decoder_config; | |
433 FinishInitializationIfNeeded(); | |
434 } | |
435 | |
436 void Mp2tStreamParser::OnAudioConfigChanged( | |
437 int pes_pid, | |
438 const AudioDecoderConfig& audio_decoder_config) { | |
439 DVLOG(1) << "OnAudioConfigChanged for pid=" << pes_pid; | |
440 DCHECK_EQ(pes_pid, selected_audio_pid_); | |
441 | |
442 audio_config_ = audio_decoder_config; | |
443 FinishInitializationIfNeeded(); | |
444 } | |
445 | |
446 void Mp2tStreamParser::FinishInitializationIfNeeded() { | |
447 // Nothing to be done if already initialized. | |
448 if (is_initialized_) | |
449 return; | |
450 | |
451 // Initialization is done when both the audio decoder config | |
452 // and the video decoder config are known | |
453 // (for a stream with both audio and video). | |
454 if (selected_audio_pid_ > 0 && !audio_config_.IsValidConfig()) | |
455 return; | |
456 if (selected_video_pid_ > 0 && !video_config_.IsValidConfig()) | |
457 return; | |
458 | |
459 // The audio and video decoder configs passed in the callback | |
460 // are the latest audio and video decoder configs. | |
461 // This might be different from the configs of the first audio and video | |
462 // buffer if we have a sequence like this one in the Mpeg2 TS stream: | |
463 // VConfigA VBuffer0 VBuffer1 VConfigB VBuffer2 AConfigA ABuffer0 | |
464 // In this case, |audio_config_| corresponds to AConfigA | |
465 // and |video_config_| corresponds to VConfigB and not VConfigA. | |
466 // This does not matter since the callback will be invoked later before | |
467 // emitting any buffers and will thus overwrite the audio/video config. | |
468 config_cb_.Run(audio_config_, video_config_); | |
469 | |
470 // For Mpeg2 TS, the duration is not known. | |
471 DVLOG(1) << "Mpeg2TS stream parser initialization done"; | |
472 init_cb_.Run(true, kInfiniteDuration()); | |
473 is_initialized_ = true; | |
474 } | |
475 | |
476 void Mp2tStreamParser::OnEmitAudioBuffer( | |
477 int pes_pid, | |
478 scoped_refptr<StreamParserBuffer> stream_parser_buffer) { | |
479 DCHECK_EQ(pes_pid, selected_audio_pid_); | |
480 | |
481 DVLOG(LOG_LEVEL_ES) | |
482 << "OnEmitAudioBuffer: " | |
483 << " size=" | |
484 << stream_parser_buffer->data_size() | |
485 << " dts=" | |
486 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds() | |
487 << " pts=" | |
488 << stream_parser_buffer->timestamp().InMilliseconds(); | |
489 stream_parser_buffer->set_timestamp( | |
490 stream_parser_buffer->timestamp() - time_offset_); | |
491 stream_parser_buffer->SetDecodeTimestamp( | |
492 stream_parser_buffer->GetDecodeTimestamp() - time_offset_); | |
493 | |
494 AudioBufferWithConfig audio_buffer_with_config; | |
495 audio_buffer_with_config.buffer = stream_parser_buffer; | |
496 audio_buffer_with_config.config = audio_config_; | |
497 audio_buffer_queue_.push_back(audio_buffer_with_config); | |
498 } | |
499 | |
500 void Mp2tStreamParser::OnEmitVideoBuffer( | |
501 int pes_pid, | |
502 scoped_refptr<StreamParserBuffer> stream_parser_buffer) { | |
503 DCHECK_EQ(pes_pid, selected_video_pid_); | |
504 | |
505 DVLOG(LOG_LEVEL_ES) | |
506 << "OnEmitVideoBuffer" | |
507 << " size=" | |
508 << stream_parser_buffer->data_size() | |
509 << " dts=" | |
510 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds() | |
511 << " pts=" | |
512 << stream_parser_buffer->timestamp().InMilliseconds() | |
513 << " IsKeyframe=" | |
514 << stream_parser_buffer->IsKeyframe(); | |
515 stream_parser_buffer->set_timestamp( | |
516 stream_parser_buffer->timestamp() - time_offset_); | |
517 stream_parser_buffer->SetDecodeTimestamp( | |
518 stream_parser_buffer->GetDecodeTimestamp() - time_offset_); | |
519 | |
520 VideoBufferWithConfig video_buffer_with_config; | |
521 video_buffer_with_config.buffer = stream_parser_buffer; | |
522 video_buffer_with_config.config = video_config_; | |
523 video_buffer_queue_.push_back(video_buffer_with_config); | |
524 } | |
525 | |
526 void Mp2tStreamParser::EmitRemainingBuffers() { | |
527 DVLOG(LOG_LEVEL_ES) << "Mp2tStreamParser::EmitRemainingBuffers"; | |
528 if (!is_initialized_) | |
529 return; | |
530 | |
531 while (!audio_buffer_queue_.empty() || !video_buffer_queue_.empty()) { | |
532 StreamParser::BufferQueue audio_queue; | |
533 StreamParser::BufferQueue video_queue; | |
534 AudioDecoderConfig audio_config = GetAudioBuffers(&audio_queue); | |
535 VideoDecoderConfig video_config = GetVideoBuffers(&video_queue); | |
536 | |
537 if (audio_queue.empty() && video_queue.empty()) | |
538 break; | |
539 | |
540 // Start a segment if needed. | |
541 if (!segment_started_) { | |
542 DVLOG(1) << "Starting a new segment"; | |
543 segment_started_ = true; | |
544 new_segment_cb_.Run(); | |
545 } | |
546 | |
547 // Update the audio and video config if needed. | |
548 bool is_new_config = false; | |
549 if (!audio_queue.empty() && !audio_config.Matches(last_audio_config_)) { | |
550 last_audio_config_ = audio_config; | |
551 is_new_config = true; | |
552 } | |
553 if (!video_queue.empty() && !video_config.Matches(last_video_config_)) { | |
554 last_video_config_ = video_config; | |
555 is_new_config = true; | |
556 } | |
557 if (is_new_config) | |
558 config_cb_.Run(last_audio_config_, last_video_config_); | |
559 | |
560 // Add the buffers. | |
561 new_buffers_cb_.Run(audio_queue, video_queue); | |
562 } | |
563 } | |
564 | |
565 AudioDecoderConfig Mp2tStreamParser::GetAudioBuffers( | |
566 StreamParser::BufferQueue* audio_queue) { | |
567 if (audio_buffer_queue_.empty()) | |
568 return AudioDecoderConfig(); | |
569 | |
570 AudioDecoderConfig audio_config = audio_buffer_queue_.front().config; | |
571 while (!audio_buffer_queue_.empty() && | |
572 audio_buffer_queue_.front().config.Matches(audio_config)) { | |
573 audio_queue->push_back(audio_buffer_queue_.front().buffer); | |
574 audio_buffer_queue_.pop_front(); | |
575 } | |
576 return audio_config; | |
577 } | |
578 | |
579 VideoDecoderConfig Mp2tStreamParser::GetVideoBuffers( | |
580 StreamParser::BufferQueue* video_queue) { | |
581 if (video_buffer_queue_.empty()) | |
582 return VideoDecoderConfig(); | |
583 | |
584 if (first_video_frame_in_segment_) { | |
585 // Remove all the leading non key frames. | |
586 while (!video_buffer_queue_.empty() && | |
587 !video_buffer_queue_.front().buffer->IsKeyframe()) | |
588 video_buffer_queue_.pop_front(); | |
589 | |
590 if (video_buffer_queue_.empty()) | |
591 return VideoDecoderConfig(); | |
592 } | |
593 | |
594 VideoDecoderConfig video_config = video_buffer_queue_.front().config; | |
595 while (!video_buffer_queue_.empty() && | |
596 video_buffer_queue_.front().config.Matches(video_config)) { | |
597 video_queue->push_back(video_buffer_queue_.front().buffer); | |
598 video_buffer_queue_.pop_front(); | |
599 } | |
600 first_video_frame_in_segment_ = false; | |
601 | |
602 return video_config; | |
603 } | |
604 | |
605 } // namespace mp2t | |
606 } // namespace media | |
607 | |
OLD | NEW |