Removed unused VideoCaptureCapability parameters.
[chromium-blink-merge.git] / media / mp2t / mp2t_stream_parser.cc
blob68fca5cedd26374fcd70bebcdd52f92a7b460ec8
1 // Copyright 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.
5 #include "media/mp2t/mp2t_stream_parser.h"
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"
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()
154 : selected_audio_pid_(-1),
155 selected_video_pid_(-1),
156 is_initialized_(false),
157 segment_started_(false),
158 first_video_frame_in_segment_(true) {
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 const NewTextBuffersCB& text_cb,
170 const NeedKeyCB& need_key_cb,
171 const AddTextTrackCB& add_text_track_cb,
172 const NewMediaSegmentCB& new_segment_cb,
173 const base::Closure& end_of_segment_cb,
174 const LogCB& log_cb) {
175 DCHECK(!is_initialized_);
176 DCHECK(init_cb_.is_null());
177 DCHECK(!init_cb.is_null());
178 DCHECK(!config_cb.is_null());
179 DCHECK(!new_buffers_cb.is_null());
180 DCHECK(!need_key_cb.is_null());
181 DCHECK(!end_of_segment_cb.is_null());
183 init_cb_ = init_cb;
184 config_cb_ = config_cb;
185 new_buffers_cb_ = new_buffers_cb;
186 need_key_cb_ = need_key_cb;
187 new_segment_cb_ = new_segment_cb;
188 end_of_segment_cb_ = end_of_segment_cb;
189 log_cb_ = log_cb;
192 void Mp2tStreamParser::Flush() {
193 DVLOG(1) << "Mp2tStreamParser::Flush";
195 // Flush the buffers and reset the pids.
196 for (std::map<int, PidState*>::iterator it = pids_.begin();
197 it != pids_.end(); ++it) {
198 DVLOG(1) << "Flushing PID: " << it->first;
199 PidState* pid_state = it->second;
200 pid_state->Flush();
201 delete pid_state;
203 pids_.clear();
204 EmitRemainingBuffers();
205 buffer_queue_chain_.clear();
207 // End of the segment.
208 // Note: does not need to invoke |end_of_segment_cb_| since flushing the
209 // stream parser already involves the end of the current segment.
210 segment_started_ = false;
211 first_video_frame_in_segment_ = true;
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();
217 // Reset the selected PIDs.
218 selected_audio_pid_ = -1;
219 selected_video_pid_ = -1;
222 bool Mp2tStreamParser::Parse(const uint8* buf, int size) {
223 DVLOG(1) << "Mp2tStreamParser::Parse size=" << size;
225 // Add the data to the parser state.
226 ts_byte_queue_.Push(buf, size);
228 while (true) {
229 const uint8* ts_buffer;
230 int ts_buffer_size;
231 ts_byte_queue_.Peek(&ts_buffer, &ts_buffer_size);
232 if (ts_buffer_size < TsPacket::kPacketSize)
233 break;
235 // Synchronization.
236 int skipped_bytes = TsPacket::Sync(ts_buffer, ts_buffer_size);
237 if (skipped_bytes > 0) {
238 DVLOG(1) << "Packet not aligned on a TS syncword:"
239 << " skipped_bytes=" << skipped_bytes;
240 ts_byte_queue_.Pop(skipped_bytes);
241 continue;
244 // Parse the TS header, skipping 1 byte if the header is invalid.
245 scoped_ptr<TsPacket> ts_packet(TsPacket::Parse(ts_buffer, ts_buffer_size));
246 if (!ts_packet) {
247 DVLOG(1) << "Error: invalid TS packet";
248 ts_byte_queue_.Pop(1);
249 continue;
251 DVLOG(LOG_LEVEL_TS)
252 << "Processing PID=" << ts_packet->pid()
253 << " start_unit=" << ts_packet->payload_unit_start_indicator();
255 // Parse the section.
256 std::map<int, PidState*>::iterator it = pids_.find(ts_packet->pid());
257 if (it == pids_.end() &&
258 ts_packet->pid() == TsSection::kPidPat) {
259 // Create the PAT state here if needed.
260 scoped_ptr<TsSection> pat_section_parser(
261 new TsSectionPat(
262 base::Bind(&Mp2tStreamParser::RegisterPmt,
263 base::Unretained(this))));
264 scoped_ptr<PidState> pat_pid_state(
265 new PidState(ts_packet->pid(), PidState::kPidPat,
266 pat_section_parser.Pass()));
267 pat_pid_state->Enable();
268 it = pids_.insert(
269 std::pair<int, PidState*>(ts_packet->pid(),
270 pat_pid_state.release())).first;
273 if (it != pids_.end()) {
274 if (!it->second->PushTsPacket(*ts_packet))
275 return false;
276 } else {
277 DVLOG(LOG_LEVEL_TS) << "Ignoring TS packet for pid: " << ts_packet->pid();
280 // Go to the next packet.
281 ts_byte_queue_.Pop(TsPacket::kPacketSize);
284 RCHECK(FinishInitializationIfNeeded());
286 // Emit the A/V buffers that kept accumulating during TS parsing.
287 return EmitRemainingBuffers();
290 void Mp2tStreamParser::RegisterPmt(int program_number, int pmt_pid) {
291 DVLOG(1) << "RegisterPmt:"
292 << " program_number=" << program_number
293 << " pmt_pid=" << pmt_pid;
295 // Only one TS program is allowed. Ignore the incoming program map table,
296 // if there is already one registered.
297 for (std::map<int, PidState*>::iterator it = pids_.begin();
298 it != pids_.end(); ++it) {
299 PidState* pid_state = it->second;
300 if (pid_state->pid_type() == PidState::kPidPmt) {
301 DVLOG_IF(1, pmt_pid != it->first) << "More than one program is defined";
302 return;
306 // Create the PMT state here if needed.
307 DVLOG(1) << "Create a new PMT parser";
308 scoped_ptr<TsSection> pmt_section_parser(
309 new TsSectionPmt(
310 base::Bind(&Mp2tStreamParser::RegisterPes,
311 base::Unretained(this), pmt_pid)));
312 scoped_ptr<PidState> pmt_pid_state(
313 new PidState(pmt_pid, PidState::kPidPmt, pmt_section_parser.Pass()));
314 pmt_pid_state->Enable();
315 pids_.insert(std::pair<int, PidState*>(pmt_pid, pmt_pid_state.release()));
318 void Mp2tStreamParser::RegisterPes(int pmt_pid,
319 int pes_pid,
320 int stream_type) {
321 // TODO(damienv): check there is no mismatch if the entry already exists.
322 DVLOG(1) << "RegisterPes:"
323 << " pes_pid=" << pes_pid
324 << " stream_type=" << std::hex << stream_type << std::dec;
325 std::map<int, PidState*>::iterator it = pids_.find(pes_pid);
326 if (it != pids_.end())
327 return;
329 // Create a stream parser corresponding to the stream type.
330 bool is_audio = false;
331 scoped_ptr<EsParser> es_parser;
332 if (stream_type == kStreamTypeAVC) {
333 es_parser.reset(
334 new EsParserH264(
335 base::Bind(&Mp2tStreamParser::OnVideoConfigChanged,
336 base::Unretained(this),
337 pes_pid),
338 base::Bind(&Mp2tStreamParser::OnEmitVideoBuffer,
339 base::Unretained(this),
340 pes_pid)));
341 } else if (stream_type == kStreamTypeAAC) {
342 es_parser.reset(
343 new EsParserAdts(
344 base::Bind(&Mp2tStreamParser::OnAudioConfigChanged,
345 base::Unretained(this),
346 pes_pid),
347 base::Bind(&Mp2tStreamParser::OnEmitAudioBuffer,
348 base::Unretained(this),
349 pes_pid)));
350 is_audio = true;
351 } else {
352 return;
355 // Create the PES state here.
356 DVLOG(1) << "Create a new PES state";
357 scoped_ptr<TsSection> pes_section_parser(
358 new TsSectionPes(es_parser.Pass()));
359 PidState::PidType pid_type =
360 is_audio ? PidState::kPidAudioPes : PidState::kPidVideoPes;
361 scoped_ptr<PidState> pes_pid_state(
362 new PidState(pes_pid, pid_type, pes_section_parser.Pass()));
363 pids_.insert(std::pair<int, PidState*>(pes_pid, pes_pid_state.release()));
365 // A new PES pid has been added, the PID filter might change.
366 UpdatePidFilter();
369 void Mp2tStreamParser::UpdatePidFilter() {
370 // Applies the HLS rule to select the default audio/video PIDs:
371 // select the audio/video streams with the lowest PID.
372 // TODO(damienv): this can be changed when the StreamParser interface
373 // supports multiple audio/video streams.
374 PidMap::iterator lowest_audio_pid = pids_.end();
375 PidMap::iterator lowest_video_pid = pids_.end();
376 for (PidMap::iterator it = pids_.begin(); it != pids_.end(); ++it) {
377 int pid = it->first;
378 PidState* pid_state = it->second;
379 if (pid_state->pid_type() == PidState::kPidAudioPes &&
380 (lowest_audio_pid == pids_.end() || pid < lowest_audio_pid->first))
381 lowest_audio_pid = it;
382 if (pid_state->pid_type() == PidState::kPidVideoPes &&
383 (lowest_video_pid == pids_.end() || pid < lowest_video_pid->first))
384 lowest_video_pid = it;
387 // Enable both the lowest audio and video PIDs.
388 if (lowest_audio_pid != pids_.end()) {
389 DVLOG(1) << "Enable audio pid: " << lowest_audio_pid->first;
390 lowest_audio_pid->second->Enable();
391 selected_audio_pid_ = lowest_audio_pid->first;
393 if (lowest_video_pid != pids_.end()) {
394 DVLOG(1) << "Enable video pid: " << lowest_audio_pid->first;
395 lowest_video_pid->second->Enable();
396 selected_video_pid_ = lowest_video_pid->first;
399 // Disable all the other audio and video PIDs.
400 for (PidMap::iterator it = pids_.begin(); it != pids_.end(); ++it) {
401 PidState* pid_state = it->second;
402 if (it != lowest_audio_pid && it != lowest_video_pid &&
403 (pid_state->pid_type() == PidState::kPidAudioPes ||
404 pid_state->pid_type() == PidState::kPidVideoPes))
405 pid_state->Disable();
409 void Mp2tStreamParser::OnVideoConfigChanged(
410 int pes_pid,
411 const VideoDecoderConfig& video_decoder_config) {
412 DVLOG(1) << "OnVideoConfigChanged for pid=" << pes_pid;
413 DCHECK_EQ(pes_pid, selected_video_pid_);
414 DCHECK(video_decoder_config.IsValidConfig());
416 // Create a new entry in |buffer_queue_chain_| with the updated configs.
417 BufferQueueWithConfig buffer_queue_with_config(
418 false,
419 buffer_queue_chain_.empty()
420 ? AudioDecoderConfig() : buffer_queue_chain_.back().audio_config,
421 video_decoder_config);
422 buffer_queue_chain_.push_back(buffer_queue_with_config);
424 // Replace any non valid config with the 1st valid entry.
425 // This might happen if there was no available config before.
426 for (std::list<BufferQueueWithConfig>::iterator it =
427 buffer_queue_chain_.begin(); it != buffer_queue_chain_.end(); ++it) {
428 if (it->video_config.IsValidConfig())
429 break;
430 it->video_config = video_decoder_config;
434 void Mp2tStreamParser::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 DCHECK(audio_decoder_config.IsValidConfig());
441 // Create a new entry in |buffer_queue_chain_| with the updated configs.
442 BufferQueueWithConfig buffer_queue_with_config(
443 false,
444 audio_decoder_config,
445 buffer_queue_chain_.empty()
446 ? VideoDecoderConfig() : buffer_queue_chain_.back().video_config);
447 buffer_queue_chain_.push_back(buffer_queue_with_config);
449 // Replace any non valid config with the 1st valid entry.
450 // This might happen if there was no available config before.
451 for (std::list<BufferQueueWithConfig>::iterator it =
452 buffer_queue_chain_.begin(); it != buffer_queue_chain_.end(); ++it) {
453 if (it->audio_config.IsValidConfig())
454 break;
455 it->audio_config = audio_decoder_config;
459 bool Mp2tStreamParser::FinishInitializationIfNeeded() {
460 // Nothing to be done if already initialized.
461 if (is_initialized_)
462 return true;
464 // Wait for more data to come to finish initialization.
465 if (buffer_queue_chain_.empty())
466 return true;
468 // Wait for more data to come if one of the config is not available.
469 BufferQueueWithConfig& queue_with_config = buffer_queue_chain_.front();
470 if (selected_audio_pid_ > 0 &&
471 !queue_with_config.audio_config.IsValidConfig())
472 return true;
473 if (selected_video_pid_ > 0 &&
474 !queue_with_config.video_config.IsValidConfig())
475 return true;
477 // Pass the config before invoking the initialization callback.
478 RCHECK(config_cb_.Run(queue_with_config.audio_config,
479 queue_with_config.video_config));
480 queue_with_config.is_config_sent = true;
482 // For Mpeg2 TS, the duration is not known.
483 DVLOG(1) << "Mpeg2TS stream parser initialization done";
484 init_cb_.Run(true, kInfiniteDuration());
485 is_initialized_ = true;
487 return true;
490 void Mp2tStreamParser::OnEmitAudioBuffer(
491 int pes_pid,
492 scoped_refptr<StreamParserBuffer> stream_parser_buffer) {
493 DCHECK_EQ(pes_pid, selected_audio_pid_);
495 DVLOG(LOG_LEVEL_ES)
496 << "OnEmitAudioBuffer: "
497 << " size="
498 << stream_parser_buffer->data_size()
499 << " dts="
500 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds()
501 << " pts="
502 << stream_parser_buffer->timestamp().InMilliseconds();
503 stream_parser_buffer->set_timestamp(
504 stream_parser_buffer->timestamp() - time_offset_);
505 stream_parser_buffer->SetDecodeTimestamp(
506 stream_parser_buffer->GetDecodeTimestamp() - time_offset_);
508 // Ignore the incoming buffer if it is not associated with any config.
509 if (buffer_queue_chain_.empty()) {
510 DVLOG(1) << "Ignoring audio buffer with no corresponding audio config";
511 return;
514 buffer_queue_chain_.back().audio_queue.push_back(stream_parser_buffer);
517 void Mp2tStreamParser::OnEmitVideoBuffer(
518 int pes_pid,
519 scoped_refptr<StreamParserBuffer> stream_parser_buffer) {
520 DCHECK_EQ(pes_pid, selected_video_pid_);
522 DVLOG(LOG_LEVEL_ES)
523 << "OnEmitVideoBuffer"
524 << " size="
525 << stream_parser_buffer->data_size()
526 << " dts="
527 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds()
528 << " pts="
529 << stream_parser_buffer->timestamp().InMilliseconds()
530 << " IsKeyframe="
531 << stream_parser_buffer->IsKeyframe();
532 stream_parser_buffer->set_timestamp(
533 stream_parser_buffer->timestamp() - time_offset_);
534 stream_parser_buffer->SetDecodeTimestamp(
535 stream_parser_buffer->GetDecodeTimestamp() - time_offset_);
537 // Ignore the incoming buffer if it is not associated with any config.
538 if (buffer_queue_chain_.empty()) {
539 DVLOG(1) << "Ignoring video buffer with no corresponding video config:"
540 << " keyframe=" << stream_parser_buffer->IsKeyframe()
541 << " dts="
542 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds();
543 return;
546 // A segment cannot start with a non key frame.
547 // Ignore the frame if that's the case.
548 if (first_video_frame_in_segment_ && !stream_parser_buffer->IsKeyframe()) {
549 DVLOG(1) << "Ignoring non-key frame:"
550 << " dts="
551 << stream_parser_buffer->GetDecodeTimestamp().InMilliseconds();
552 return;
555 first_video_frame_in_segment_ = false;
556 buffer_queue_chain_.back().video_queue.push_back(stream_parser_buffer);
559 bool Mp2tStreamParser::EmitRemainingBuffers() {
560 DVLOG(LOG_LEVEL_ES) << "Mp2tStreamParser::EmitRemainingBuffers";
562 // No buffer should be sent until fully initialized.
563 if (!is_initialized_)
564 return true;
566 if (buffer_queue_chain_.empty())
567 return true;
569 // Keep track of the last audio and video config sent.
570 AudioDecoderConfig last_audio_config =
571 buffer_queue_chain_.back().audio_config;
572 VideoDecoderConfig last_video_config =
573 buffer_queue_chain_.back().video_config;
575 // Buffer emission.
576 while (!buffer_queue_chain_.empty()) {
577 // Start a segment if needed.
578 if (!segment_started_) {
579 DVLOG(1) << "Starting a new segment";
580 segment_started_ = true;
581 new_segment_cb_.Run();
584 // Update the audio and video config if needed.
585 BufferQueueWithConfig& queue_with_config = buffer_queue_chain_.front();
586 if (!queue_with_config.is_config_sent) {
587 if (!config_cb_.Run(queue_with_config.audio_config,
588 queue_with_config.video_config))
589 return false;
590 queue_with_config.is_config_sent = true;
593 // Add buffers.
594 if (!queue_with_config.audio_queue.empty() ||
595 !queue_with_config.video_queue.empty()) {
596 if (!new_buffers_cb_.Run(queue_with_config.audio_queue,
597 queue_with_config.video_queue)) {
598 return false;
602 buffer_queue_chain_.pop_front();
605 // Push an empty queue with the last audio/video config
606 // so that buffers with the same config can be added later on.
607 BufferQueueWithConfig queue_with_config(
608 true, last_audio_config, last_video_config);
609 buffer_queue_chain_.push_back(queue_with_config);
611 return true;
614 } // namespace mp2t
615 } // namespace media