Roll ANGLE e754fb8..6ffeb74
[chromium-blink-merge.git] / media / formats / mp2t / mp2t_stream_parser.cc
blob4d2e2d4c1aa279f9d0a4d68286d8b808d10bf254
1 // Copyright 2014 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.
5 #include "media/formats/mp2t/mp2t_stream_parser.h"
7 #include "base/bind.h"
8 #include "base/callback_helpers.h"
9 #include "base/stl_util.h"
10 #include "media/base/buffers.h"
11 #include "media/base/stream_parser_buffer.h"
12 #include "media/base/text_track_config.h"
13 #include "media/formats/mp2t/es_parser.h"
14 #include "media/formats/mp2t/es_parser_adts.h"
15 #include "media/formats/mp2t/es_parser_h264.h"
16 #include "media/formats/mp2t/es_parser_mpeg1audio.h"
17 #include "media/formats/mp2t/mp2t_common.h"
18 #include "media/formats/mp2t/ts_packet.h"
19 #include "media/formats/mp2t/ts_section.h"
20 #include "media/formats/mp2t/ts_section_pat.h"
21 #include "media/formats/mp2t/ts_section_pes.h"
22 #include "media/formats/mp2t/ts_section_pmt.h"
24 namespace media {
25 namespace mp2t {
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,
34 class PidState {
35 public:
36 enum PidType {
37 kPidPat,
38 kPidPmt,
39 kPidAudioPes,
40 kPidVideoPes,
43 PidState(int pid, PidType pid_tyoe,
44 scoped_ptr<TsSection> section_parser);
46 // Extract the content of the TS packet and parse it.
47 // Return true if successful.
48 bool PushTsPacket(const TsPacket& ts_packet);
50 // Flush the PID state (possibly emitting some pending frames)
51 // and reset its state.
52 void Flush();
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;
61 PidType pid_type() const { return pid_type_; }
63 private:
64 void ResetState();
66 int pid_;
67 PidType pid_type_;
68 scoped_ptr<TsSection> section_parser_;
70 bool enable_;
72 int continuity_counter_;
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_);
85 bool PidState::PushTsPacket(const TsPacket& ts_packet) {
86 DCHECK_EQ(ts_packet.pid(), pid_);
88 // The current PID is not part of the PID filter,
89 // just discard the incoming TS packet.
90 if (!enable_)
91 return true;
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;
100 bool status = section_parser_->Parse(
101 ts_packet.payload_unit_start_indicator(),
102 ts_packet.payload(),
103 ts_packet.payload_size());
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();
112 return status;
115 void PidState::Flush() {
116 section_parser_->Flush();
117 ResetState();
120 void PidState::Enable() {
121 enable_ = true;
124 void PidState::Disable() {
125 if (!enable_)
126 return;
128 ResetState();
129 enable_ = false;
132 bool PidState::IsEnabled() const {
133 return enable_;
136 void PidState::ResetState() {
137 section_parser_->Reset();
138 continuity_counter_ = -1;
141 Mp2tStreamParser::BufferQueueWithConfig::BufferQueueWithConfig(
142 bool is_cfg_sent,
143 const AudioDecoderConfig& audio_cfg,
144 const VideoDecoderConfig& video_cfg)
145 : is_config_sent(is_cfg_sent),
146 audio_config(audio_cfg),
147 video_config(video_cfg) {
150 Mp2tStreamParser::BufferQueueWithConfig::~BufferQueueWithConfig() {
153 Mp2tStreamParser::Mp2tStreamParser(bool sbr_in_mimetype)
154 : sbr_in_mimetype_(sbr_in_mimetype),
155 selected_audio_pid_(-1),
156 selected_video_pid_(-1),
157 is_initialized_(false),
158 segment_started_(false) {
161 Mp2tStreamParser::~Mp2tStreamParser() {
162 STLDeleteValues(&pids_);
165 void Mp2tStreamParser::Init(
166 const InitCB& init_cb,
167 const NewConfigCB& config_cb,
168 const NewBuffersCB& new_buffers_cb,
169 bool /* ignore_text_tracks */,
170 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
171 const NewMediaSegmentCB& new_segment_cb,
172 const base::Closure& end_of_segment_cb,
173 const LogCB& log_cb) {
174 DCHECK(!is_initialized_);
175 DCHECK(init_cb_.is_null());
176 DCHECK(!init_cb.is_null());
177 DCHECK(!config_cb.is_null());
178 DCHECK(!new_buffers_cb.is_null());
179 DCHECK(!encrypted_media_init_data_cb.is_null());
180 DCHECK(!end_of_segment_cb.is_null());
182 init_cb_ = init_cb;
183 config_cb_ = config_cb;
184 new_buffers_cb_ = new_buffers_cb;
185 encrypted_media_init_data_cb_ = encrypted_media_init_data_cb;
186 new_segment_cb_ = new_segment_cb;
187 end_of_segment_cb_ = end_of_segment_cb;
188 log_cb_ = log_cb;
191 void Mp2tStreamParser::Flush() {
192 DVLOG(1) << "Mp2tStreamParser::Flush";
194 // Flush the buffers and reset the pids.
195 for (std::map<int, PidState*>::iterator it = pids_.begin();
196 it != pids_.end(); ++it) {
197 DVLOG(1) << "Flushing PID: " << it->first;
198 PidState* pid_state = it->second;
199 pid_state->Flush();
200 delete pid_state;
202 pids_.clear();
203 EmitRemainingBuffers();
204 buffer_queue_chain_.clear();
206 // End of the segment.
207 // Note: does not need to invoke |end_of_segment_cb_| since flushing the
208 // stream parser already involves the end of the current segment.
209 segment_started_ = false;
211 // Remove any bytes left in the TS buffer.
212 // (i.e. any partial TS packet => less than 188 bytes).
213 ts_byte_queue_.Reset();
215 // Reset the selected PIDs.
216 selected_audio_pid_ = -1;
217 selected_video_pid_ = -1;
219 // Reset the timestamp unroller.
220 timestamp_unroller_.Reset();
223 bool Mp2tStreamParser::Parse(const uint8* buf, int size) {
224 DVLOG(1) << "Mp2tStreamParser::Parse size=" << size;
226 // Add the data to the parser state.
227 ts_byte_queue_.Push(buf, size);
229 while (true) {
230 const uint8* ts_buffer;
231 int ts_buffer_size;
232 ts_byte_queue_.Peek(&ts_buffer, &ts_buffer_size);
233 if (ts_buffer_size < TsPacket::kPacketSize)
234 break;
236 // Synchronization.
237 int skipped_bytes = TsPacket::Sync(ts_buffer, ts_buffer_size);
238 if (skipped_bytes > 0) {
239 DVLOG(1) << "Packet not aligned on a TS syncword:"
240 << " skipped_bytes=" << skipped_bytes;
241 ts_byte_queue_.Pop(skipped_bytes);
242 continue;
245 // Parse the TS header, skipping 1 byte if the header is invalid.
246 scoped_ptr<TsPacket> ts_packet(TsPacket::Parse(ts_buffer, ts_buffer_size));
247 if (!ts_packet) {
248 DVLOG(1) << "Error: invalid TS packet";
249 ts_byte_queue_.Pop(1);
250 continue;
252 DVLOG(LOG_LEVEL_TS)
253 << "Processing PID=" << ts_packet->pid()
254 << " start_unit=" << ts_packet->payload_unit_start_indicator();
256 // Parse the section.
257 std::map<int, PidState*>::iterator it = pids_.find(ts_packet->pid());
258 if (it == pids_.end() &&
259 ts_packet->pid() == TsSection::kPidPat) {
260 // Create the PAT state here if needed.
261 scoped_ptr<TsSection> pat_section_parser(
262 new TsSectionPat(
263 base::Bind(&Mp2tStreamParser::RegisterPmt,
264 base::Unretained(this))));
265 scoped_ptr<PidState> pat_pid_state(
266 new PidState(ts_packet->pid(), PidState::kPidPat,
267 pat_section_parser.Pass()));
268 pat_pid_state->Enable();
269 it = pids_.insert(
270 std::pair<int, PidState*>(ts_packet->pid(),
271 pat_pid_state.release())).first;
274 if (it != pids_.end()) {
275 if (!it->second->PushTsPacket(*ts_packet))
276 return false;
277 } else {
278 DVLOG(LOG_LEVEL_TS) << "Ignoring TS packet for pid: " << ts_packet->pid();
281 // Go to the next packet.
282 ts_byte_queue_.Pop(TsPacket::kPacketSize);
285 RCHECK(FinishInitializationIfNeeded());
287 // Emit the A/V buffers that kept accumulating during TS parsing.
288 return EmitRemainingBuffers();
291 void Mp2tStreamParser::RegisterPmt(int program_number, int pmt_pid) {
292 DVLOG(1) << "RegisterPmt:"
293 << " program_number=" << program_number
294 << " pmt_pid=" << pmt_pid;
296 // Only one TS program is allowed. Ignore the incoming program map table,
297 // if there is already one registered.
298 for (std::map<int, PidState*>::iterator it = pids_.begin();
299 it != pids_.end(); ++it) {
300 PidState* pid_state = it->second;
301 if (pid_state->pid_type() == PidState::kPidPmt) {
302 DVLOG_IF(1, pmt_pid != it->first) << "More than one program is defined";
303 return;
307 // Create the PMT state here if needed.
308 DVLOG(1) << "Create a new PMT parser";
309 scoped_ptr<TsSection> pmt_section_parser(
310 new TsSectionPmt(
311 base::Bind(&Mp2tStreamParser::RegisterPes,
312 base::Unretained(this), pmt_pid)));
313 scoped_ptr<PidState> pmt_pid_state(
314 new PidState(pmt_pid, PidState::kPidPmt, pmt_section_parser.Pass()));
315 pmt_pid_state->Enable();
316 pids_.insert(std::pair<int, PidState*>(pmt_pid, pmt_pid_state.release()));
319 void Mp2tStreamParser::RegisterPes(int pmt_pid,
320 int pes_pid,
321 int stream_type) {
322 // TODO(damienv): check there is no mismatch if the entry already exists.
323 DVLOG(1) << "RegisterPes:"
324 << " pes_pid=" << pes_pid
325 << " stream_type=" << std::hex << stream_type << std::dec;
326 std::map<int, PidState*>::iterator it = pids_.find(pes_pid);
327 if (it != pids_.end())
328 return;
330 // Create a stream parser corresponding to the stream type.
331 bool is_audio = false;
332 scoped_ptr<EsParser> es_parser;
333 if (stream_type == kStreamTypeAVC) {
334 es_parser.reset(
335 new EsParserH264(
336 base::Bind(&Mp2tStreamParser::OnVideoConfigChanged,
337 base::Unretained(this),
338 pes_pid),
339 base::Bind(&Mp2tStreamParser::OnEmitVideoBuffer,
340 base::Unretained(this),
341 pes_pid)));
342 } else if (stream_type == kStreamTypeAAC) {
343 es_parser.reset(
344 new EsParserAdts(
345 base::Bind(&Mp2tStreamParser::OnAudioConfigChanged,
346 base::Unretained(this),
347 pes_pid),
348 base::Bind(&Mp2tStreamParser::OnEmitAudioBuffer,
349 base::Unretained(this),
350 pes_pid),
351 sbr_in_mimetype_));
352 is_audio = true;
353 } else if (stream_type == kStreamTypeMpeg1Audio) {
354 es_parser.reset(
355 new EsParserMpeg1Audio(
356 base::Bind(&Mp2tStreamParser::OnAudioConfigChanged,
357 base::Unretained(this),
358 pes_pid),
359 base::Bind(&Mp2tStreamParser::OnEmitAudioBuffer,
360 base::Unretained(this),
361 pes_pid),
362 log_cb_));
363 is_audio = true;
364 } else {
365 return;
368 // Create the PES state here.
369 DVLOG(1) << "Create a new PES state";
370 scoped_ptr<TsSection> pes_section_parser(
371 new TsSectionPes(es_parser.Pass(), &timestamp_unroller_));
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.Pass()));
376 pids_.insert(std::pair<int, PidState*>(pes_pid, pes_pid_state.release()));
378 // A new PES pid has been added, the PID filter might change.
379 UpdatePidFilter();
382 void Mp2tStreamParser::UpdatePidFilter() {
383 // Applies the HLS rule to select the default audio/video PIDs:
384 // select the audio/video streams with the lowest PID.
385 // TODO(damienv): this can be changed when the StreamParser interface
386 // supports multiple audio/video streams.
387 PidMap::iterator lowest_audio_pid = pids_.end();
388 PidMap::iterator lowest_video_pid = pids_.end();
389 for (PidMap::iterator it = pids_.begin(); it != pids_.end(); ++it) {
390 int pid = it->first;
391 PidState* pid_state = it->second;
392 if (pid_state->pid_type() == PidState::kPidAudioPes &&
393 (lowest_audio_pid == pids_.end() || pid < lowest_audio_pid->first))
394 lowest_audio_pid = it;
395 if (pid_state->pid_type() == PidState::kPidVideoPes &&
396 (lowest_video_pid == pids_.end() || pid < lowest_video_pid->first))
397 lowest_video_pid = it;
400 // Enable both the lowest audio and video PIDs.
401 if (lowest_audio_pid != pids_.end()) {
402 DVLOG(1) << "Enable audio pid: " << lowest_audio_pid->first;
403 lowest_audio_pid->second->Enable();
404 selected_audio_pid_ = lowest_audio_pid->first;
406 if (lowest_video_pid != pids_.end()) {
407 DVLOG(1) << "Enable video pid: " << lowest_video_pid->first;
408 lowest_video_pid->second->Enable();
409 selected_video_pid_ = lowest_video_pid->first;
412 // Disable all the other audio and video PIDs.
413 for (PidMap::iterator it = pids_.begin(); it != pids_.end(); ++it) {
414 PidState* pid_state = it->second;
415 if (it != lowest_audio_pid && it != lowest_video_pid &&
416 (pid_state->pid_type() == PidState::kPidAudioPes ||
417 pid_state->pid_type() == PidState::kPidVideoPes))
418 pid_state->Disable();
422 void Mp2tStreamParser::OnVideoConfigChanged(
423 int pes_pid,
424 const VideoDecoderConfig& video_decoder_config) {
425 DVLOG(1) << "OnVideoConfigChanged for pid=" << pes_pid;
426 DCHECK_EQ(pes_pid, selected_video_pid_);
427 DCHECK(video_decoder_config.IsValidConfig());
429 if (!buffer_queue_chain_.empty() &&
430 !buffer_queue_chain_.back().video_config.IsValidConfig()) {
431 // No video has been received so far, can reuse the existing video queue.
432 DCHECK(buffer_queue_chain_.back().video_queue.empty());
433 buffer_queue_chain_.back().video_config = video_decoder_config;
434 } else {
435 // Create a new entry in |buffer_queue_chain_| with the updated configs.
436 BufferQueueWithConfig buffer_queue_with_config(
437 false,
438 buffer_queue_chain_.empty()
439 ? AudioDecoderConfig() : buffer_queue_chain_.back().audio_config,
440 video_decoder_config);
441 buffer_queue_chain_.push_back(buffer_queue_with_config);
444 // Replace any non valid config with the 1st valid entry.
445 // This might happen if there was no available config before.
446 for (std::list<BufferQueueWithConfig>::iterator it =
447 buffer_queue_chain_.begin(); it != buffer_queue_chain_.end(); ++it) {
448 if (it->video_config.IsValidConfig())
449 break;
450 it->video_config = video_decoder_config;
454 void Mp2tStreamParser::OnAudioConfigChanged(
455 int pes_pid,
456 const AudioDecoderConfig& audio_decoder_config) {
457 DVLOG(1) << "OnAudioConfigChanged for pid=" << pes_pid;
458 DCHECK_EQ(pes_pid, selected_audio_pid_);
459 DCHECK(audio_decoder_config.IsValidConfig());
461 if (!buffer_queue_chain_.empty() &&
462 !buffer_queue_chain_.back().audio_config.IsValidConfig()) {
463 // No audio has been received so far, can reuse the existing audio queue.
464 DCHECK(buffer_queue_chain_.back().audio_queue.empty());
465 buffer_queue_chain_.back().audio_config = audio_decoder_config;
466 } else {
467 // Create a new entry in |buffer_queue_chain_| with the updated configs.
468 BufferQueueWithConfig buffer_queue_with_config(
469 false,
470 audio_decoder_config,
471 buffer_queue_chain_.empty()
472 ? VideoDecoderConfig() : buffer_queue_chain_.back().video_config);
473 buffer_queue_chain_.push_back(buffer_queue_with_config);
476 // Replace any non valid config with the 1st valid entry.
477 // This might happen if there was no available config before.
478 for (std::list<BufferQueueWithConfig>::iterator it =
479 buffer_queue_chain_.begin(); it != buffer_queue_chain_.end(); ++it) {
480 if (it->audio_config.IsValidConfig())
481 break;
482 it->audio_config = audio_decoder_config;
486 bool Mp2tStreamParser::FinishInitializationIfNeeded() {
487 // Nothing to be done if already initialized.
488 if (is_initialized_)
489 return true;
491 // Wait for more data to come to finish initialization.
492 if (buffer_queue_chain_.empty())
493 return true;
495 // Wait for more data to come if one of the config is not available.
496 BufferQueueWithConfig& queue_with_config = buffer_queue_chain_.front();
497 if (selected_audio_pid_ > 0 &&
498 !queue_with_config.audio_config.IsValidConfig())
499 return true;
500 if (selected_video_pid_ > 0 &&
501 !queue_with_config.video_config.IsValidConfig())
502 return true;
504 // Pass the config before invoking the initialization callback.
505 RCHECK(config_cb_.Run(queue_with_config.audio_config,
506 queue_with_config.video_config,
507 TextTrackConfigMap()));
508 queue_with_config.is_config_sent = true;
510 // For Mpeg2 TS, the duration is not known.
511 DVLOG(1) << "Mpeg2TS stream parser initialization done";
512 base::ResetAndReturn(&init_cb_).Run(InitParameters(kInfiniteDuration()));
513 is_initialized_ = true;
515 return true;
518 void Mp2tStreamParser::OnEmitAudioBuffer(
519 int pes_pid,
520 scoped_refptr<StreamParserBuffer> stream_parser_buffer) {
521 DCHECK_EQ(pes_pid, selected_audio_pid_);
523 DVLOG(LOG_LEVEL_ES)
524 << "OnEmitAudioBuffer: "
525 << " size="
526 << stream_parser_buffer->data_size()
527 << " dts="
528 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds()
529 << " pts="
530 << stream_parser_buffer->timestamp().InMilliseconds()
531 << " dur="
532 << stream_parser_buffer->duration().InMilliseconds();
534 // Ignore the incoming buffer if it is not associated with any config.
535 if (buffer_queue_chain_.empty()) {
536 NOTREACHED() << "Cannot provide buffers before configs";
537 return;
540 buffer_queue_chain_.back().audio_queue.push_back(stream_parser_buffer);
543 void Mp2tStreamParser::OnEmitVideoBuffer(
544 int pes_pid,
545 scoped_refptr<StreamParserBuffer> stream_parser_buffer) {
546 DCHECK_EQ(pes_pid, selected_video_pid_);
548 DVLOG(LOG_LEVEL_ES)
549 << "OnEmitVideoBuffer"
550 << " size="
551 << stream_parser_buffer->data_size()
552 << " dts="
553 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds()
554 << " pts="
555 << stream_parser_buffer->timestamp().InMilliseconds()
556 << " dur="
557 << stream_parser_buffer->duration().InMilliseconds()
558 << " is_key_frame="
559 << stream_parser_buffer->is_key_frame();
561 // Ignore the incoming buffer if it is not associated with any config.
562 if (buffer_queue_chain_.empty()) {
563 NOTREACHED() << "Cannot provide buffers before configs";
564 return;
567 buffer_queue_chain_.back().video_queue.push_back(stream_parser_buffer);
570 bool Mp2tStreamParser::EmitRemainingBuffers() {
571 DVLOG(LOG_LEVEL_ES) << "Mp2tStreamParser::EmitRemainingBuffers";
573 // No buffer should be sent until fully initialized.
574 if (!is_initialized_)
575 return true;
577 if (buffer_queue_chain_.empty())
578 return true;
580 // Keep track of the last audio and video config sent.
581 AudioDecoderConfig last_audio_config =
582 buffer_queue_chain_.back().audio_config;
583 VideoDecoderConfig last_video_config =
584 buffer_queue_chain_.back().video_config;
586 // Do not have all the configs, need more data.
587 if (selected_audio_pid_ >= 0 && !last_audio_config.IsValidConfig())
588 return true;
589 if (selected_video_pid_ >= 0 && !last_video_config.IsValidConfig())
590 return true;
592 // Buffer emission.
593 while (!buffer_queue_chain_.empty()) {
594 // Start a segment if needed.
595 if (!segment_started_) {
596 DVLOG(1) << "Starting a new segment";
597 segment_started_ = true;
598 new_segment_cb_.Run();
601 // Update the audio and video config if needed.
602 BufferQueueWithConfig& queue_with_config = buffer_queue_chain_.front();
603 if (!queue_with_config.is_config_sent) {
604 if (!config_cb_.Run(queue_with_config.audio_config,
605 queue_with_config.video_config,
606 TextTrackConfigMap()))
607 return false;
608 queue_with_config.is_config_sent = true;
611 // Add buffers.
612 TextBufferQueueMap empty_text_map;
613 if (!queue_with_config.audio_queue.empty() ||
614 !queue_with_config.video_queue.empty()) {
615 if (!new_buffers_cb_.Run(queue_with_config.audio_queue,
616 queue_with_config.video_queue,
617 empty_text_map)) {
618 return false;
622 buffer_queue_chain_.pop_front();
625 // Push an empty queue with the last audio/video config
626 // so that buffers with the same config can be added later on.
627 BufferQueueWithConfig queue_with_config(
628 true, last_audio_config, last_video_config);
629 buffer_queue_chain_.push_back(queue_with_config);
631 return true;
634 } // namespace mp2t
635 } // namespace media