Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(488)

Side by Side Diff: media/mpeg2/mpeg2ts_stream_parser.cc

Issue 23566013: Mpeg2 TS stream parser for media source. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add basic unit tests + Cleanup Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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);
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();
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();
65
66 PidType pid_type() { return pid_type_; }
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(
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();
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();
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;
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:"
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 DVLOG(LOG_LEVEL_TS) << "Ignoring TS packet for pid: " << ts_packet->pid();
286
287 // Go to the next packet.
288 pos += TsPacket::kPacketSize;
289 remaining_size -= TsPacket::kPacketSize;
290 }
291
292 // Keep only the possible incomplete trailing TS packet.
293 ts_byte_queue_.Pop(ts_buffer_size - remaining_size);
294
295 // Emit the A/V buffers that kept accumulating during TS parsing.
296 EmitRemainingBuffers();
297
298 return true;
299 }
300
301 void Mpeg2TsStreamParser::RegisterPmt(int program_number, int pmt_pid) {
302 DVLOG(1) << "RegisterPmt:"
303 << " program_number=" << program_number
304 << " pmt_pid=" << pmt_pid;
305
306 // Only one TS program is allowed. Ignore the incoming program map table,
307 // if there is already one registered.
308 for (std::map<int, PidState*>::iterator it = pids_.begin();
309 it != pids_.end(); ++it) {
310 PidState* pid_state = it->second;
311 if (pid_state->pid_type() == PidState::kPidPmt) {
312 int pid = it->first;
313 LOG_IF(WARNING, pmt_pid != pid) << "More than one program is defined";
314 return;
315 }
316 }
317
318 // Create the PMT state here if needed.
319 DVLOG(1) << "Create a new PMT parser";
320 scoped_ptr<Mpeg2TsPmtParser> pmt_section_parser(
321 new Mpeg2TsPmtParser(
322 base::Bind(&Mpeg2TsStreamParser::RegisterPes,
323 base::Unretained(this), pmt_pid)));
324 scoped_ptr<PidState> pmt_pid_state(
325 new PidState(pmt_pid, PidState::kPidPmt, pmt_section_parser.release()));
326 pmt_pid_state->Enable();
327 pids_.insert(std::pair<int, PidState*>(pmt_pid, pmt_pid_state.release()));
328 }
329
330 void Mpeg2TsStreamParser::RegisterPes(int pmt_pid,
331 int pes_pid,
332 int stream_type) {
333 // TODO(damienv): check there is no mismatch if the entry already exists.
334 DVLOG(1) << "RegisterPes:"
335 << " pes_pid=" << pes_pid
336 << " stream_type=" << std::hex << stream_type << std::dec;
337 std::map<int, PidState*>::iterator it = pids_.find(pes_pid);
338 if (it != pids_.end())
339 return;
340
341 // Create a stream parser corresponding to the stream type.
342 bool is_audio = false;
343 scoped_ptr<EsParser> es_parser;
344 if (stream_type == kStreamTypeAVC) {
345 es_parser.reset(
346 new EsParserH264(
347 base::Bind(&Mpeg2TsStreamParser::OnVideoConfigChanged,
348 base::Unretained(this),
349 pes_pid),
350 base::Bind(&Mpeg2TsStreamParser::OnEmitVideoBuffer,
351 base::Unretained(this),
352 pes_pid)));
353 } else if (stream_type == kStreamTypeAAC) {
354 es_parser.reset(
355 new EsParserAdts(
356 base::Bind(&Mpeg2TsStreamParser::OnAudioConfigChanged,
357 base::Unretained(this),
358 pes_pid),
359 base::Bind(&Mpeg2TsStreamParser::OnEmitAudioBuffer,
360 base::Unretained(this),
361 pes_pid)));
362 is_audio = true;
363 } else {
364 return;
365 }
366
367 // Create the PES state here.
368 DVLOG(1) << "Create a new PES state";
369 scoped_ptr<Mpeg2TsPesParser> pes_section_parser(
370 new Mpeg2TsPesParser(es_parser.release()));
371 PidState::PidType pid_type =
372 is_audio ? PidState::kPidAudioPes : PidState::kPidVideoPes;
373 scoped_ptr<PidState> pes_pid_state(
374 new PidState(pes_pid, pid_type, pes_section_parser.release()));
375 pids_.insert(std::pair<int, PidState*>(pes_pid, pes_pid_state.release()));
376
377 // The pid filter must be updated.
378 UpdatePidFilter();
379 }
380
381 void Mpeg2TsStreamParser::UpdatePidFilter() {
382 // Applies the HLS rule to select the default audio/video PIDs:
383 // select the pid with the lowest PID.
384 // TODO(damienv): this can be changed when the StreamParser interface
385 // supports multiple audio/video streams.
386 std::map<int, PidState*>::iterator lowest_audio_pid = pids_.end();
387 std::map<int, PidState*>::iterator lowest_video_pid = pids_.end();
388 for (std::map<int, PidState*>::iterator it = pids_.begin();
389 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;
398 }
399
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;
405 }
406 if (lowest_video_pid != pids_.end()) {
407 DVLOG(1) << "Enable video pid: " << lowest_audio_pid->first;
408 lowest_video_pid->second->Enable();
409 selected_video_pid_ = lowest_video_pid->first;
410 }
411
412 // Disable all the other audio and video PIDs.
413 for (std::map<int, PidState*>::iterator it = pids_.begin();
414 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 Mpeg2TsStreamParser::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 Mpeg2TsStreamParser::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 Mpeg2TsStreamParser::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 Mpeg2TsStreamParser::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 Mpeg2TsStreamParser::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 Mpeg2TsStreamParser::EmitRemainingBuffers() {
524 DVLOG(LOG_LEVEL_ES) << "Mpeg2TsStreamParser::EmitRemainingBuffers";
525 if (!is_initialized_)
526 return;
527
528 while (!audio_buffer_queue_.empty())
529 EmitAudioBuffers();
530
531 while (!video_buffer_queue_.empty())
532 EmitVideoBuffers();
533 }
534
535 void Mpeg2TsStreamParser::EmitAudioBuffers() {
536 DCHECK(!audio_buffer_queue_.empty());
537
538 AudioDecoderConfig audio_config = audio_buffer_queue_.front().config;
539
540 if (!segment_started_) {
541 DVLOG(1) << "Starting a new segment";
542 segment_started_ = true;
543 new_segment_cb_.Run();
544 }
545
546 if (!audio_config.Matches(last_audio_config_)) {
547 last_audio_config_ = audio_config;
548 config_cb_.Run(last_audio_config_, last_video_config_);
549 }
550
551 StreamParser::BufferQueue audio_queue;
552 StreamParser::BufferQueue video_queue;
553 while (!audio_buffer_queue_.empty() &&
554 audio_buffer_queue_.front().config.Matches(last_audio_config_)) {
555 audio_queue.push_back(audio_buffer_queue_.front().buffer);
556 audio_buffer_queue_.pop_front();
557 }
558 new_buffers_cb_.Run(audio_queue, video_queue);
559 }
560
561 void Mpeg2TsStreamParser::EmitVideoBuffers() {
562 DCHECK(!video_buffer_queue_.empty());
563
564 VideoDecoderConfig video_config = video_buffer_queue_.front().config;
565
566 if (!segment_started_) {
567 DVLOG(1) << "Starting a new segment";
568 segment_started_ = true;
569 new_segment_cb_.Run();
570 }
571
572 if (!video_config.Matches(last_video_config_)) {
573 last_video_config_ = video_config;
574 config_cb_.Run(last_audio_config_, last_video_config_);
575 }
576
577 StreamParser::BufferQueue audio_queue;
578 StreamParser::BufferQueue video_queue;
579 while (!video_buffer_queue_.empty() &&
580 video_buffer_queue_.front().config.Matches(last_video_config_)) {
581 video_queue.push_back(video_buffer_queue_.front().buffer);
582 video_buffer_queue_.pop_front();
583 }
584 new_buffers_cb_.Run(audio_queue, video_queue);
585 }
586
587 } // namespace mpeg2ts
588 } // namespace media
589
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698