5 #include "packager/media/formats/mp2t/mp2t_media_parser.h"
7 #include "packager/base/bind.h"
8 #include "packager/base/memory/scoped_ptr.h"
9 #include "packager/base/stl_util.h"
10 #include "packager/media/base/media_sample.h"
11 #include "packager/media/base/stream_info.h"
12 #include "packager/media/formats/mp2t/es_parser.h"
13 #include "packager/media/formats/mp2t/es_parser_adts.h"
14 #include "packager/media/formats/mp2t/es_parser_h264.h"
15 #include "packager/media/formats/mp2t/es_parser_h265.h"
16 #include "packager/media/formats/mp2t/mp2t_common.h"
17 #include "packager/media/formats/mp2t/ts_packet.h"
18 #include "packager/media/formats/mp2t/ts_section.h"
19 #include "packager/media/formats/mp2t/ts_section_pat.h"
20 #include "packager/media/formats/mp2t/ts_section_pes.h"
21 #include "packager/media/formats/mp2t/ts_section_pmt.h"
23 namespace edash_packager {
29 kStreamTypeMpeg1Audio = 0x3,
31 kStreamTypeAVC = 0x1b,
32 kStreamTypeHEVC = 0x24,
44 PidState(
int pid, PidType pid_type,
45 scoped_ptr<TsSection> section_parser);
49 bool PushTsPacket(
const TsPacket& ts_packet);
60 bool IsEnabled()
const;
62 PidType pid_type()
const {
return pid_type_; }
64 scoped_refptr<StreamInfo>& config() {
return config_; }
65 void set_config(
const scoped_refptr<StreamInfo>& config) { config_ = config; }
67 SampleQueue& sample_queue() {
return sample_queue_; }
74 scoped_ptr<TsSection> section_parser_;
77 int continuity_counter_;
78 scoped_refptr<StreamInfo> config_;
79 SampleQueue sample_queue_;
82 PidState::PidState(
int pid, PidType pid_type,
83 scoped_ptr<TsSection> section_parser)
86 section_parser_(section_parser.Pass()),
88 continuity_counter_(-1) {
89 DCHECK(section_parser_);
92 bool PidState::PushTsPacket(
const TsPacket& ts_packet) {
93 DCHECK_EQ(ts_packet.pid(), pid_);
100 int expected_continuity_counter = (continuity_counter_ + 1) % 16;
101 if (continuity_counter_ >= 0 &&
102 ts_packet.continuity_counter() != expected_continuity_counter) {
103 DVLOG(1) <<
"TS discontinuity detected for pid: " << pid_;
108 bool status = section_parser_->Parse(
109 ts_packet.payload_unit_start_indicator(),
111 ts_packet.payload_size());
116 DVLOG(1) <<
"Parsing failed for pid = " << pid_;
123 void PidState::Flush() {
124 section_parser_->Flush();
128 void PidState::Enable() {
132 void PidState::Disable() {
140 bool PidState::IsEnabled()
const {
144 void PidState::ResetState() {
145 section_parser_->Reset();
146 continuity_counter_ = -1;
149 Mp2tMediaParser::Mp2tMediaParser()
150 : sbr_in_mimetype_(false),
151 is_initialized_(false) {
154 Mp2tMediaParser::~Mp2tMediaParser() {
155 STLDeleteValues(&pids_);
159 const InitCB& init_cb,
160 const NewSampleCB& new_sample_cb,
162 DCHECK(!is_initialized_);
163 DCHECK(init_cb_.is_null());
164 DCHECK(!init_cb.is_null());
165 DCHECK(!new_sample_cb.is_null());
168 new_sample_cb_ = new_sample_cb;
172 DVLOG(1) <<
"Mp2tMediaParser::Flush";
175 for (std::map<int, PidState*>::iterator it = pids_.begin();
176 it != pids_.end(); ++it) {
177 DVLOG(1) <<
"Flushing PID: " << it->first;
178 PidState* pid_state = it->second;
181 bool result = EmitRemainingSamples();
182 STLDeleteValues(&pids_);
186 ts_byte_queue_.
Reset();
191 DVLOG(1) <<
"Mp2tMediaParser::Parse size=" << size;
194 ts_byte_queue_.
Push(buf, size);
197 const uint8_t* ts_buffer;
199 ts_byte_queue_.
Peek(&ts_buffer, &ts_buffer_size);
200 if (ts_buffer_size < TsPacket::kPacketSize)
204 int skipped_bytes = TsPacket::Sync(ts_buffer, ts_buffer_size);
205 if (skipped_bytes > 0) {
206 DVLOG(1) <<
"Packet not aligned on a TS syncword:"
207 <<
" skipped_bytes=" << skipped_bytes;
208 ts_byte_queue_.
Pop(skipped_bytes);
213 scoped_ptr<TsPacket> ts_packet(TsPacket::Parse(ts_buffer, ts_buffer_size));
215 DVLOG(1) <<
"Error: invalid TS packet";
216 ts_byte_queue_.
Pop(1);
220 <<
"Processing PID=" << ts_packet->pid()
221 <<
" start_unit=" << ts_packet->payload_unit_start_indicator();
224 std::map<int, PidState*>::iterator it = pids_.find(ts_packet->pid());
225 if (it == pids_.end() &&
226 ts_packet->pid() == TsSection::kPidPat) {
228 scoped_ptr<TsSection> pat_section_parser(
230 base::Bind(&Mp2tMediaParser::RegisterPmt,
231 base::Unretained(
this))));
232 scoped_ptr<PidState> pat_pid_state(
233 new PidState(ts_packet->pid(), PidState::kPidPat,
234 pat_section_parser.Pass()));
235 pat_pid_state->Enable();
237 std::pair<int, PidState*>(ts_packet->pid(),
238 pat_pid_state.release())).first;
241 if (it != pids_.end()) {
242 if (!it->second->PushTsPacket(*ts_packet))
245 DVLOG(LOG_LEVEL_TS) <<
"Ignoring TS packet for pid: " << ts_packet->pid();
249 ts_byte_queue_.
Pop(TsPacket::kPacketSize);
253 return EmitRemainingSamples();
256 void Mp2tMediaParser::RegisterPmt(
int program_number,
int pmt_pid) {
257 DVLOG(1) <<
"RegisterPmt:"
258 <<
" program_number=" << program_number
259 <<
" pmt_pid=" << pmt_pid;
263 for (std::map<int, PidState*>::iterator it = pids_.begin();
264 it != pids_.end(); ++it) {
265 PidState* pid_state = it->second;
266 if (pid_state->pid_type() == PidState::kPidPmt) {
267 DVLOG_IF(1, pmt_pid != it->first) <<
"More than one program is defined";
273 DVLOG(1) <<
"Create a new PMT parser";
274 scoped_ptr<TsSection> pmt_section_parser(
276 base::Bind(&Mp2tMediaParser::RegisterPes,
277 base::Unretained(
this), pmt_pid)));
278 scoped_ptr<PidState> pmt_pid_state(
279 new PidState(pmt_pid, PidState::kPidPmt, pmt_section_parser.Pass()));
280 pmt_pid_state->Enable();
281 pids_.insert(std::pair<int, PidState*>(pmt_pid, pmt_pid_state.release()));
284 void Mp2tMediaParser::RegisterPes(
int pmt_pid,
287 DVLOG(1) <<
"RegisterPes:"
288 <<
" pes_pid=" << pes_pid
289 <<
" stream_type=" << std::hex << stream_type << std::dec;
290 std::map<int, PidState*>::iterator it = pids_.find(pes_pid);
291 if (it != pids_.end())
295 bool is_audio =
false;
296 scoped_ptr<EsParser> es_parser;
297 if (stream_type == kStreamTypeAVC) {
301 base::Bind(&Mp2tMediaParser::OnNewStreamInfo,
302 base::Unretained(
this)),
303 base::Bind(&Mp2tMediaParser::OnEmitSample,
304 base::Unretained(
this))));
305 }
else if (stream_type == kStreamTypeHEVC) {
309 base::Bind(&Mp2tMediaParser::OnNewStreamInfo,
310 base::Unretained(
this)),
311 base::Bind(&Mp2tMediaParser::OnEmitSample,
312 base::Unretained(
this))));
313 }
else if (stream_type == kStreamTypeAAC) {
317 base::Bind(&Mp2tMediaParser::OnNewStreamInfo,
318 base::Unretained(
this)),
319 base::Bind(&Mp2tMediaParser::OnEmitSample,
320 base::Unretained(
this)),
328 DVLOG(1) <<
"Create a new PES state";
329 scoped_ptr<TsSection> pes_section_parser(
330 new TsSectionPes(es_parser.Pass()));
331 PidState::PidType pid_type =
332 is_audio ? PidState::kPidAudioPes : PidState::kPidVideoPes;
333 scoped_ptr<PidState> pes_pid_state(
334 new PidState(pes_pid, pid_type, pes_section_parser.Pass()));
335 pes_pid_state->Enable();
336 pids_.insert(std::pair<int, PidState*>(pes_pid, pes_pid_state.release()));
339 void Mp2tMediaParser::OnNewStreamInfo(
340 const scoped_refptr<StreamInfo>& new_stream_info) {
341 DCHECK(new_stream_info);
342 DVLOG(1) <<
"OnVideoConfigChanged for pid=" << new_stream_info->track_id();
344 PidMap::iterator pid_state = pids_.find(new_stream_info->track_id());
345 if (pid_state == pids_.end()) {
346 LOG(ERROR) <<
"PID State for new stream not found (pid = "
347 << new_stream_info->track_id() <<
").";
352 pid_state->second->set_config(new_stream_info);
355 FinishInitializationIfNeeded();
358 bool Mp2tMediaParser::FinishInitializationIfNeeded() {
367 std::vector<scoped_refptr<StreamInfo> > all_stream_info;
369 for (PidMap::const_iterator iter = pids_.begin(); iter != pids_.end();
371 if (((iter->second->pid_type() == PidState::kPidAudioPes) ||
372 (iter->second->pid_type() == PidState::kPidVideoPes))) {
374 if (iter->second->config())
375 all_stream_info.push_back(iter->second->config());
378 if (num_es && (all_stream_info.size() == num_es)) {
381 init_cb_.Run(all_stream_info);
382 DVLOG(1) <<
"Mpeg2TS stream parser initialization done";
383 is_initialized_ =
true;
388 void Mp2tMediaParser::OnEmitSample(
390 const scoped_refptr<MediaSample>& new_sample) {
397 << new_sample->data_size()
401 << new_sample->pts();
404 PidMap::iterator pid_state = pids_.find(pes_pid);
405 if (pid_state == pids_.end()) {
406 LOG(ERROR) <<
"PID State for new sample not found (pid = "
410 pid_state->second->sample_queue().push_back(new_sample);
413 bool Mp2tMediaParser::EmitRemainingSamples() {
414 DVLOG(LOG_LEVEL_ES) <<
"Mp2tMediaParser::EmitRemainingBuffers";
417 if (!is_initialized_)
421 for (PidMap::const_iterator pid_iter = pids_.begin(); pid_iter != pids_.end();
423 SampleQueue& sample_queue = pid_iter->second->sample_queue();
424 for (SampleQueue::iterator sample_iter = sample_queue.begin();
425 sample_iter != sample_queue.end();
427 if (!new_sample_cb_.Run(pid_iter->first, *sample_iter)) {
432 sample_queue.clear();