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/mpeg2/mpeg2ts_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/mpeg2/es_parser.h" | |
15 #include "media/mpeg2/es_parser_adts.h" | |
16 #include "media/mpeg2/es_parser_h264.h" | |
17 #include "media/mpeg2/mpeg2ts_common.h" | |
18 #include "media/mpeg2/mpeg2ts_pat.h" | |
19 #include "media/mpeg2/mpeg2ts_pes.h" | |
20 #include "media/mpeg2/mpeg2ts_pmt.h" | |
21 #include "media/mpeg2/mpeg2ts_section_parser.h" | |
22 #include "media/mpeg2/ts_packet.h" | |
23 | |
24 namespace { | |
25 | |
26 enum StreamType { | |
27 // ISO-13818.1 / ITU H.222 Table 2.34 "Stream type assignments" | |
28 kStreamTypeMpeg1Audio = 0x3, | |
29 kStreamTypeAAC = 0xf, | |
30 kStreamTypeAVC = 0x1b, | |
31 }; | |
32 | |
33 } | |
34 | |
35 namespace media { | |
36 namespace mpeg2ts { | |
37 | |
38 class PidState { | |
39 public: | |
40 enum PidType { | |
41 kPidPat, | |
42 kPidPmt, | |
43 kPidAudioPes, | |
44 kPidVideoPes, | |
45 }; | |
46 | |
47 // Take ownership of |section_parser|. | |
48 PidState(int pid, PidType pid_tyoe, | |
49 Mpeg2TsSectionParser* section_parser); | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: Use scoped_ptr<Mpeg2TsSectionParser> here so
damienv1
2013/09/09 23:29:45
Done.
| |
50 | |
51 // Extract the content of the TS packet and parse it. | |
52 // Return true if successful. | |
53 bool PushTsPacket(TsPacket* ts_packet); | |
54 | |
55 // Flush the PID state (possibly emitting some pending frames) | |
56 // and reset its state. | |
57 void Flush(); | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: EmitPendingBuffersAndReset() seems like a mor
| |
58 | |
59 // Enable/disable the PID. | |
60 // Disabling a PID will reset its state and ignore any further incoming TS | |
61 // packets. | |
62 void Enable(); | |
63 void Disable(); | |
64 bool IsEnabled(); | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: add const
damienv1
2013/09/09 23:29:45
Done.
| |
65 | |
66 PidType pid_type() { return pid_type_; } | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: add const here and in any other similar acces
damienv1
2013/09/09 23:29:45
Done.
| |
67 | |
68 private: | |
69 void ResetState(); | |
70 | |
71 int pid_; | |
72 PidType pid_type_; | |
73 scoped_ptr<Mpeg2TsSectionParser> section_parser_; | |
74 | |
75 bool enable_; | |
76 | |
77 int continuity_counter_; | |
78 }; | |
79 | |
80 PidState::PidState(int pid, PidType pid_type, | |
81 Mpeg2TsSectionParser* section_parser) | |
82 : pid_(pid), | |
83 pid_type_(pid_type), | |
84 section_parser_(section_parser), | |
85 enable_(false), | |
86 continuity_counter_(-1) { | |
87 DCHECK(section_parser); | |
88 } | |
89 | |
90 bool PidState::PushTsPacket(TsPacket* ts_packet) { | |
91 DCHECK_EQ(ts_packet->pid(), pid_); | |
92 | |
93 // The current PID is not part of the PID filter, | |
94 // just discard the incoming TS packet. | |
95 if (!enable_) | |
96 return true; | |
97 | |
98 int expected_continuity_counter = (continuity_counter_ + 1) % 16; | |
99 if (continuity_counter_ >= 0 && | |
100 ts_packet->continuity_counter() != expected_continuity_counter) { | |
101 LOG(WARNING) << "TS discontinuity detected for pid: " << pid_; | |
102 return false; | |
103 } | |
104 | |
105 bool parse_result = section_parser_->Parse( | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: just return here since nothing else uses pars
damienv1
2013/09/09 23:29:45
Done.
| |
106 ts_packet->payload_unit_start_indicator(), | |
107 ts_packet->GetPayload(), | |
108 ts_packet->GetPayloadSize()); | |
109 return parse_result; | |
110 } | |
111 | |
112 void PidState::Flush() { | |
113 section_parser_->Flush(); | |
114 ResetState(); | |
115 } | |
116 | |
117 void PidState::Enable() { | |
118 enable_ = true; | |
119 } | |
120 | |
121 void PidState::Disable() { | |
122 if (!enable_) | |
123 return; | |
124 | |
125 ResetState(); | |
126 enable_ = false; | |
127 } | |
128 | |
129 bool PidState::IsEnabled() { | |
130 return enable_; | |
131 } | |
132 | |
133 void PidState::ResetState() { | |
134 // TODO(damienv) | |
135 //section_parser_->ResetState(); | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
Remove if this really isn't needed.
damienv1
2013/09/09 23:29:45
Done: implemented.
| |
136 continuity_counter_ = -1; | |
137 } | |
138 | |
139 class Mpeg2TsStreamParser::AudioBufferWithConfig { | |
140 public: | |
141 scoped_refptr<StreamParserBuffer> buffer; | |
142 AudioDecoderConfig config; | |
143 }; | |
144 | |
145 class Mpeg2TsStreamParser::VideoBufferWithConfig { | |
146 public: | |
147 scoped_refptr<StreamParserBuffer> buffer; | |
148 VideoDecoderConfig config; | |
149 }; | |
150 | |
151 | |
152 Mpeg2TsStreamParser::Mpeg2TsStreamParser() | |
153 : selected_audio_pid_(-1), | |
154 selected_video_pid_(-1), | |
155 is_initialized_(false), | |
156 segment_started_(false) { | |
157 } | |
158 | |
159 Mpeg2TsStreamParser::~Mpeg2TsStreamParser() { | |
160 STLDeleteValues(&pids_); | |
161 } | |
162 | |
163 void Mpeg2TsStreamParser::Init( | |
164 const InitCB& init_cb, | |
165 const NewConfigCB& config_cb, | |
166 const NewBuffersCB& new_buffers_cb, | |
167 const NewTextBuffersCB& text_cb, | |
168 const NeedKeyCB& need_key_cb, | |
169 const AddTextTrackCB& add_text_track_cb, | |
170 const NewMediaSegmentCB& new_segment_cb, | |
171 const base::Closure& end_of_segment_cb, | |
172 const LogCB& log_cb) { | |
173 DCHECK(!is_initialized_); | |
174 DCHECK(init_cb_.is_null()); | |
175 DCHECK(!init_cb.is_null()); | |
176 DCHECK(!config_cb.is_null()); | |
177 DCHECK(!new_buffers_cb.is_null()); | |
178 DCHECK(!need_key_cb.is_null()); | |
179 DCHECK(!end_of_segment_cb.is_null()); | |
180 | |
181 init_cb_ = init_cb; | |
182 config_cb_ = config_cb; | |
183 new_buffers_cb_ = new_buffers_cb; | |
184 need_key_cb_ = need_key_cb; | |
185 new_segment_cb_ = new_segment_cb; | |
186 end_of_segment_cb_ = end_of_segment_cb; | |
187 log_cb_ = log_cb; | |
188 } | |
189 | |
190 void Mpeg2TsStreamParser::Flush() { | |
191 DVLOG(1) << "Mpeg2TsStreamParser::Flush"; | |
192 | |
193 // Flush the buffers and reset the pids. | |
194 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
195 it != pids_.end(); ++it) { | |
196 DVLOG(1) << "Flushing PID: " << it->first; | |
197 PidState* pid_state = it->second; | |
198 pid_state->Flush(); | |
199 delete pid_state; | |
200 } | |
201 pids_.clear(); | |
202 EmitRemainingBuffers(); | |
203 DCHECK(audio_buffer_queue_.empty()); | |
204 DCHECK(video_buffer_queue_.empty()); | |
205 audio_buffer_queue_.clear(); | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: If we are DCHECKing that these queues are emp
damienv1
2013/09/09 23:29:45
Should now be clearer. Whether buffers are emitted
| |
206 video_buffer_queue_.clear(); | |
207 | |
208 // End of the segment. | |
209 // Note: does not need to invoke |end_of_segment_cb_| since flushing the | |
210 // stream parser already involves the end of the current segment. | |
211 segment_started_ = false; | |
212 | |
213 // Remove any bytes left in the TS buffer. | |
214 // (i.e. any partial TS packet => less than 188 bytes). | |
215 ts_byte_queue_.Reset(); | |
216 | |
217 // Reset the selected PIDs. | |
218 selected_audio_pid_ = -1; | |
219 selected_video_pid_ = -1; | |
220 | |
221 // Reset the audio and video configs. | |
222 audio_config_ = AudioDecoderConfig(); | |
223 video_config_ = VideoDecoderConfig(); | |
224 last_audio_config_ = AudioDecoderConfig(); | |
225 last_video_config_ = VideoDecoderConfig(); | |
226 } | |
227 | |
228 bool Mpeg2TsStreamParser::Parse(const uint8* buf, int size) { | |
229 DVLOG(1) << "Mpeg2TsStreamParser::Parse size=" << size; | |
230 | |
231 // Add the data to the parser state. | |
232 ts_byte_queue_.Push(buf, size); | |
233 const uint8* ts_buffer = NULL; | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
nit: init not needed
damienv1
2013/09/09 23:29:45
Done.
| |
234 int ts_buffer_size = 0; | |
235 ts_byte_queue_.Peek(&ts_buffer, &ts_buffer_size); | |
236 | |
237 int pos = 0; | |
238 int remaining_size = ts_buffer_size; | |
239 while (remaining_size >= TsPacket::kPacketSize) { | |
240 // Synchronization. | |
241 int skipped_bytes = TsPacket::Sync(&ts_buffer[pos], remaining_size); | |
242 if (skipped_bytes > 0) { | |
243 LOG(WARNING) << "Packet not aligned on a TS syncword:" | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
DVLOG here and everywhere else
damienv1
2013/09/09 23:29:45
Done.
| |
244 << " skipped_bytes=" << skipped_bytes; | |
245 pos += skipped_bytes; | |
246 remaining_size -= skipped_bytes; | |
247 continue; | |
248 } | |
249 | |
250 // Parse the TS header. | |
251 scoped_ptr<TsPacket> ts_packet( | |
252 TsPacket::Parse(&ts_buffer[pos], remaining_size)); | |
253 if (!ts_packet) { | |
254 LOG(WARNING) << "Error: invalid TS packet"; | |
255 pos += 1; | |
256 remaining_size -= 1; | |
257 continue; | |
258 } | |
259 | |
260 DVLOG(LOG_LEVEL_TS) | |
261 << "Processing PID=" << ts_packet->pid() | |
262 << " start_unit=" << ts_packet->payload_unit_start_indicator(); | |
263 | |
264 // Parse the section. | |
265 std::map<int, PidState*>::iterator it = pids_.find(ts_packet->pid()); | |
266 if (it == pids_.end() && | |
267 ts_packet->pid() == Mpeg2TsSectionParser::kPidPat) { | |
268 // Create the PAT state here if needed. | |
269 scoped_ptr<Mpeg2TsPatParser> pat_section_parser( | |
270 new Mpeg2TsPatParser( | |
271 base::Bind(&Mpeg2TsStreamParser::RegisterPmt, | |
272 base::Unretained(this)))); | |
273 scoped_ptr<PidState> pat_pid_state( | |
274 new PidState(ts_packet->pid(), PidState::kPidPat, | |
275 pat_section_parser.release())); | |
276 pat_pid_state->Enable(); | |
277 it = pids_.insert( | |
278 std::pair<int, PidState*>(ts_packet->pid(), | |
279 pat_pid_state.release())).first; | |
280 } | |
281 | |
282 if (it != pids_.end()) { | |
283 it->second->PushTsPacket(ts_packet.get()); | |
284 } else { | |
285 LOG(WARNING) << "Ignoring TS packet for pid: " << ts_packet->pid(); | |
286 } | |
287 | |
288 // Go to the next packet. | |
289 pos += TsPacket::kPacketSize; | |
290 remaining_size -= TsPacket::kPacketSize; | |
291 } | |
292 | |
293 // Keep only the possible incomplete trailing TS packet. | |
294 ts_byte_queue_.Pop(ts_buffer_size - remaining_size); | |
295 | |
296 // Emit the A/V buffers that kept accumulating during TS parsing. | |
297 EmitRemainingBuffers(); | |
298 | |
299 return true; | |
300 } | |
301 | |
302 void Mpeg2TsStreamParser::RegisterPmt(int program_number, int pmt_pid) { | |
303 DVLOG(1) << "RegisterPmt:" | |
304 << " program_number=" << program_number | |
305 << " pmt_pid=" << pmt_pid; | |
306 | |
307 // Only one TS program is allowed. Ignore the incoming program map table, | |
308 // if there is already one registered. | |
309 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
310 it != pids_.end(); ++it) { | |
311 PidState* pid_state = it->second; | |
312 if (pid_state->pid_type() == PidState::kPidPmt) { | |
313 int pid = it->first; | |
314 LOG_IF(WARNING, pmt_pid != pid) << "More than one program is defined"; | |
315 return; | |
316 } | |
317 } | |
318 | |
319 // Create the PMT state here if needed. | |
320 DVLOG(1) << "Create a new PMT parser"; | |
321 scoped_ptr<Mpeg2TsPmtParser> pmt_section_parser( | |
322 new Mpeg2TsPmtParser( | |
323 base::Bind(&Mpeg2TsStreamParser::RegisterPes, | |
324 base::Unretained(this), pmt_pid))); | |
325 scoped_ptr<PidState> pmt_pid_state( | |
326 new PidState(pmt_pid, PidState::kPidPmt, pmt_section_parser.release())); | |
327 pmt_pid_state->Enable(); | |
328 pids_.insert(std::pair<int, PidState*>(pmt_pid, pmt_pid_state.release())); | |
329 } | |
330 | |
331 void Mpeg2TsStreamParser::RegisterPes(int pmt_pid, | |
332 int pes_pid, | |
333 int stream_type) { | |
334 // TODO(damienv): check there is no mismatch if the entry already exists. | |
335 LOG(INFO) << "RegisterPes:" | |
336 << " pes_pid=" << pes_pid | |
337 << " stream_type=" << std::hex << stream_type << std::dec; | |
338 std::map<int, PidState*>::iterator it = pids_.find(pes_pid); | |
339 if (it != pids_.end()) | |
340 return; | |
341 | |
342 // Create a stream parser corresponding to the stream type. | |
343 bool is_audio = false; | |
344 scoped_ptr<EsParser> es_parser; | |
345 if (stream_type == kStreamTypeAVC) { | |
346 es_parser.reset( | |
347 new EsParserH264( | |
348 base::Bind(&Mpeg2TsStreamParser::OnVideoConfigChanged, | |
349 base::Unretained(this), | |
350 pes_pid), | |
351 base::Bind(&Mpeg2TsStreamParser::OnEmitVideoBuffer, | |
352 base::Unretained(this), | |
353 pes_pid))); | |
354 } else if (stream_type == kStreamTypeAAC) { | |
355 es_parser.reset( | |
356 new EsParserAdts( | |
357 base::Bind(&Mpeg2TsStreamParser::OnAudioConfigChanged, | |
358 base::Unretained(this), | |
359 pes_pid), | |
360 base::Bind(&Mpeg2TsStreamParser::OnEmitAudioBuffer, | |
361 base::Unretained(this), | |
362 pes_pid))); | |
363 is_audio = true; | |
364 } else { | |
365 return; | |
366 } | |
367 | |
368 // Create the PES state here. | |
369 DVLOG(1) << "Create a new PES state"; | |
370 scoped_ptr<Mpeg2TsPesParser> pes_section_parser( | |
371 new Mpeg2TsPesParser(es_parser.release())); | |
372 PidState::PidType pid_type = | |
373 is_audio ? PidState::kPidAudioPes : PidState::kPidVideoPes; | |
374 scoped_ptr<PidState> pes_pid_state( | |
375 new PidState(pes_pid, pid_type, pes_section_parser.release())); | |
376 pids_.insert(std::pair<int, PidState*>(pes_pid, pes_pid_state.release())); | |
377 | |
378 // The pid filter must be updated. | |
379 UpdatePidFilter(); | |
380 } | |
381 | |
382 void Mpeg2TsStreamParser::UpdatePidFilter() { | |
383 // Applies the HLS rule to select the default audio/video PIDs: | |
384 // select the pid with the lowest PID. | |
385 // TODO(damienv): this can be changed when the StreamParser interface | |
386 // supports multiple audio/video streams. | |
387 std::map<int, PidState*>::iterator lowest_audio_pid = pids_.end(); | |
388 std::map<int, PidState*>::iterator lowest_video_pid = pids_.end(); | |
389 for (std::map<int, PidState*>::iterator it = pids_.begin(); | |
390 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 (std::map<int, PidState*>::iterator it = pids_.begin(); | |
415 it != pids_.end(); ++it) { | |
416 PidState* pid_state = it->second; | |
417 if (it != lowest_audio_pid && it != lowest_video_pid && | |
418 (pid_state->pid_type() == PidState::kPidAudioPes || | |
419 pid_state->pid_type() == PidState::kPidVideoPes)) | |
420 pid_state->Disable(); | |
421 } | |
422 } | |
423 | |
424 void Mpeg2TsStreamParser::OnVideoConfigChanged( | |
425 int pes_pid, | |
426 const VideoDecoderConfig& video_decoder_config) { | |
427 DVLOG(1) << "OnVideoConfigChanged for pid=" << pes_pid; | |
428 DCHECK_EQ(pes_pid, selected_video_pid_); | |
429 | |
430 video_config_ = video_decoder_config; | |
431 FinishInitializationIfNeeded(); | |
432 } | |
433 | |
434 void Mpeg2TsStreamParser::OnAudioConfigChanged( | |
435 int pes_pid, | |
436 const AudioDecoderConfig& audio_decoder_config) { | |
437 DVLOG(1) << "OnAudioConfigChanged for pid=" << pes_pid; | |
438 DCHECK_EQ(pes_pid, selected_audio_pid_); | |
439 | |
440 audio_config_ = audio_decoder_config; | |
441 FinishInitializationIfNeeded(); | |
442 } | |
443 | |
444 void Mpeg2TsStreamParser::FinishInitializationIfNeeded() { | |
445 // Nothing to be done if already initialized. | |
446 if (is_initialized_) | |
447 return; | |
448 | |
449 // Initialization is done when both the audio decoder config | |
450 // and the video decoder config are known | |
451 // (for a stream with both audio and video). | |
452 if (selected_audio_pid_ > 0 && !audio_config_.IsValidConfig()) | |
453 return; | |
454 if (selected_video_pid_ > 0 && !video_config_.IsValidConfig()) | |
455 return; | |
456 | |
457 // The audio and video decoder configs passed in the callback | |
458 // are the latest audio and video decoder configs. | |
459 // This might be different from the configs of the first audio and video | |
460 // buffer if we have a sequence like this one in the Mpeg2 TS stream: | |
461 // VConfigA VBuffer0 VBuffer1 VConfigB VBuffer2 AConfigA ABuffer0 | |
462 // In this case, |audio_config_| corresponds to AConfigA | |
463 // and |video_config_| corresponds to VConfigB and not VConfigA. | |
464 // This does not matter since the callback will be invoked later before | |
465 // emitting any buffers and will thus overwrite the audio/video config. | |
466 config_cb_.Run(audio_config_, video_config_); | |
467 | |
468 // For Mpeg2 TS, the duration is not known. | |
469 DVLOG(1) << "Mpeg2TS stream parser initialization done"; | |
470 init_cb_.Run(true, kInfiniteDuration()); | |
471 is_initialized_ = true; | |
472 } | |
473 | |
474 void Mpeg2TsStreamParser::OnEmitAudioBuffer( | |
475 int pes_pid, | |
476 scoped_refptr<StreamParserBuffer> stream_parser_buffer) { | |
477 DCHECK_EQ(pes_pid, selected_audio_pid_); | |
478 | |
479 DVLOG(LOG_LEVEL_ES) | |
480 << "OnEmitAudioBuffer: " | |
481 << " size=" | |
482 << stream_parser_buffer->data_size() | |
483 << " dts=" | |
484 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds() | |
485 << " pts=" | |
486 << stream_parser_buffer->timestamp().InMilliseconds(); | |
487 stream_parser_buffer->set_timestamp( | |
488 stream_parser_buffer->timestamp() - time_offset_); | |
489 stream_parser_buffer->SetDecodeTimestamp( | |
490 stream_parser_buffer->GetDecodeTimestamp() - time_offset_); | |
491 | |
492 AudioBufferWithConfig audio_buffer_with_config; | |
493 audio_buffer_with_config.buffer = stream_parser_buffer; | |
494 audio_buffer_with_config.config = audio_config_; | |
acolwell GONE FROM CHROMIUM
2013/09/05 18:29:10
You shouldn't need to do this. Just emit the pendi
damienv1
2013/09/09 23:29:45
That was my initial implementation. However, I swi
| |
495 audio_buffer_queue_.push_back(audio_buffer_with_config); | |
496 } | |
497 | |
498 void Mpeg2TsStreamParser::OnEmitVideoBuffer( | |
499 int pes_pid, | |
500 scoped_refptr<StreamParserBuffer> stream_parser_buffer) { | |
501 DCHECK_EQ(pes_pid, selected_video_pid_); | |
502 | |
503 DVLOG(LOG_LEVEL_ES) | |
504 << "OnEmitVideoBuffer" | |
505 << " size=" | |
506 << stream_parser_buffer->data_size() | |
507 << " dts=" | |
508 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds() | |
509 << " pts=" | |
510 << stream_parser_buffer->timestamp().InMilliseconds() | |
511 << " IsKeyframe=" | |
512 << stream_parser_buffer->IsKeyframe(); | |
513 stream_parser_buffer->set_timestamp( | |
514 stream_parser_buffer->timestamp() - time_offset_); | |
515 stream_parser_buffer->SetDecodeTimestamp( | |
516 stream_parser_buffer->GetDecodeTimestamp() - time_offset_); | |
517 | |
518 VideoBufferWithConfig video_buffer_with_config; | |
519 video_buffer_with_config.buffer = stream_parser_buffer; | |
520 video_buffer_with_config.config = video_config_; | |
521 video_buffer_queue_.push_back(video_buffer_with_config); | |
522 } | |
523 | |
524 void Mpeg2TsStreamParser::EmitRemainingBuffers() { | |
525 DVLOG(LOG_LEVEL_ES) << "Mpeg2TsStreamParser::EmitRemainingBuffers"; | |
526 if (!is_initialized_) | |
527 return; | |
528 | |
529 while (!audio_buffer_queue_.empty()) | |
530 EmitAudioBuffers(); | |
531 | |
532 while (!video_buffer_queue_.empty()) | |
533 EmitVideoBuffers(); | |
534 } | |
535 | |
536 void Mpeg2TsStreamParser::EmitAudioBuffers() { | |
537 DCHECK(!audio_buffer_queue_.empty()); | |
538 | |
539 AudioDecoderConfig audio_config = audio_buffer_queue_.front().config; | |
540 | |
541 if (!segment_started_) { | |
542 DVLOG(1) << "Starting a new segment"; | |
543 segment_started_ = true; | |
544 new_segment_cb_.Run(); | |
545 } | |
546 | |
547 if (!audio_config.Matches(last_audio_config_)) { | |
548 last_audio_config_ = audio_config; | |
549 config_cb_.Run(last_audio_config_, last_video_config_); | |
550 } | |
551 | |
552 StreamParser::BufferQueue audio_queue; | |
553 StreamParser::BufferQueue video_queue; | |
554 while (!audio_buffer_queue_.empty() && | |
555 audio_buffer_queue_.front().config.Matches(last_audio_config_)) { | |
556 audio_queue.push_back(audio_buffer_queue_.front().buffer); | |
557 audio_buffer_queue_.pop_front(); | |
558 } | |
559 new_buffers_cb_.Run(audio_queue, video_queue); | |
560 } | |
561 | |
562 void Mpeg2TsStreamParser::EmitVideoBuffers() { | |
563 DCHECK(!video_buffer_queue_.empty()); | |
564 | |
565 VideoDecoderConfig video_config = video_buffer_queue_.front().config; | |
566 | |
567 if (!segment_started_) { | |
568 DVLOG(1) << "Starting a new segment"; | |
569 segment_started_ = true; | |
570 new_segment_cb_.Run(); | |
571 } | |
572 | |
573 if (!video_config.Matches(last_video_config_)) { | |
574 last_video_config_ = video_config; | |
575 config_cb_.Run(last_audio_config_, last_video_config_); | |
576 } | |
577 | |
578 StreamParser::BufferQueue audio_queue; | |
579 StreamParser::BufferQueue video_queue; | |
580 while (!video_buffer_queue_.empty() && | |
581 video_buffer_queue_.front().config.Matches(last_video_config_)) { | |
582 video_queue.push_back(video_buffer_queue_.front().buffer); | |
583 video_buffer_queue_.pop_front(); | |
584 } | |
585 new_buffers_cb_.Run(audio_queue, video_queue); | |
586 } | |
587 | |
588 } // namespace mpeg2ts | |
589 } // namespace media | |
OLD | NEW |