5 #include "packager/media/formats/mp2t/mp2t_media_parser.h"
8 #include "packager/base/bind.h"
9 #include "packager/media/base/media_sample.h"
10 #include "packager/media/base/stream_info.h"
11 #include "packager/media/formats/mp2t/es_parser.h"
12 #include "packager/media/formats/mp2t/es_parser_adts.h"
13 #include "packager/media/formats/mp2t/es_parser_h264.h"
14 #include "packager/media/formats/mp2t/es_parser_h265.h"
15 #include "packager/media/formats/mp2t/mp2t_common.h"
16 #include "packager/media/formats/mp2t/ts_packet.h"
17 #include "packager/media/formats/mp2t/ts_section.h"
18 #include "packager/media/formats/mp2t/ts_section_pat.h"
19 #include "packager/media/formats/mp2t/ts_section_pes.h"
20 #include "packager/media/formats/mp2t/ts_section_pmt.h"
28 kStreamTypeMpeg1Audio = 0x3,
30 kStreamTypeAVC = 0x1b,
31 kStreamTypeHEVC = 0x24,
45 std::unique_ptr<TsSection> section_parser);
49 bool PushTsPacket(
const TsPacket& ts_packet);
60 bool IsEnabled()
const;
62 PidType pid_type()
const {
return pid_type_; }
64 std::shared_ptr<StreamInfo>& config() {
return config_; }
65 void set_config(
const std::shared_ptr<StreamInfo>& config) {
69 SampleQueue& sample_queue() {
return sample_queue_; }
76 std::unique_ptr<TsSection> section_parser_;
79 int continuity_counter_;
80 std::shared_ptr<StreamInfo> config_;
81 SampleQueue sample_queue_;
84 PidState::PidState(
int pid,
86 std::unique_ptr<TsSection> section_parser)
89 section_parser_(std::move(section_parser)),
91 continuity_counter_(-1) {
92 DCHECK(section_parser_);
95 bool PidState::PushTsPacket(
const TsPacket& ts_packet) {
96 DCHECK_EQ(ts_packet.pid(), pid_);
103 int expected_continuity_counter = (continuity_counter_ + 1) % 16;
104 if (continuity_counter_ >= 0 &&
105 ts_packet.continuity_counter() != expected_continuity_counter) {
106 DVLOG(1) <<
"TS discontinuity detected for pid: " << pid_;
111 bool status = section_parser_->Parse(
112 ts_packet.payload_unit_start_indicator(),
114 ts_packet.payload_size());
119 DVLOG(1) <<
"Parsing failed for pid = " << pid_;
126 void PidState::Flush() {
127 section_parser_->Flush();
131 void PidState::Enable() {
135 void PidState::Disable() {
143 bool PidState::IsEnabled()
const {
147 void PidState::ResetState() {
148 section_parser_->Reset();
149 continuity_counter_ = -1;
152 Mp2tMediaParser::Mp2tMediaParser()
153 : sbr_in_mimetype_(false),
154 is_initialized_(false) {
157 Mp2tMediaParser::~Mp2tMediaParser() {}
160 const InitCB& init_cb,
161 const NewSampleCB& new_sample_cb,
163 DCHECK(!is_initialized_);
164 DCHECK(init_cb_.is_null());
165 DCHECK(!init_cb.is_null());
166 DCHECK(!new_sample_cb.is_null());
169 new_sample_cb_ = new_sample_cb;
173 DVLOG(1) <<
"Mp2tMediaParser::Flush";
176 for (
const auto& pair : pids_) {
177 DVLOG(1) <<
"Flushing PID: " << pair.first;
178 PidState* pid_state = pair.second.get();
181 bool result = EmitRemainingSamples();
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 std::unique_ptr<TsPacket> ts_packet(
214 TsPacket::Parse(ts_buffer, ts_buffer_size));
216 DVLOG(1) <<
"Error: invalid TS packet";
217 ts_byte_queue_.Pop(1);
221 <<
"Processing PID=" << ts_packet->pid()
222 <<
" start_unit=" << ts_packet->payload_unit_start_indicator();
225 std::map<int, std::unique_ptr<PidState>>::iterator it =
226 pids_.find(ts_packet->pid());
227 if (it == pids_.end() &&
228 ts_packet->pid() == TsSection::kPidPat) {
230 std::unique_ptr<TsSection> pat_section_parser(
new TsSectionPat(
231 base::Bind(&Mp2tMediaParser::RegisterPmt, base::Unretained(
this))));
232 std::unique_ptr<PidState> pat_pid_state(
new PidState(
233 ts_packet->pid(), PidState::kPidPat, std::move(pat_section_parser)));
234 pat_pid_state->Enable();
236 .insert(std::pair<
int, std::unique_ptr<PidState>>(
237 ts_packet->pid(), std::move(pat_pid_state)))
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 (
const auto& pair : pids_) {
264 if (pair.second->pid_type() == PidState::kPidPmt) {
265 DVLOG_IF(1, pmt_pid != pair.first) <<
"More than one program is defined";
271 DVLOG(1) <<
"Create a new PMT parser";
272 std::unique_ptr<TsSection> pmt_section_parser(
new TsSectionPmt(base::Bind(
273 &Mp2tMediaParser::RegisterPes, base::Unretained(
this), pmt_pid)));
274 std::unique_ptr<PidState> pmt_pid_state(
275 new PidState(pmt_pid, PidState::kPidPmt, std::move(pmt_section_parser)));
276 pmt_pid_state->Enable();
277 pids_.insert(std::pair<
int, std::unique_ptr<PidState>>(
278 pmt_pid, std::move(pmt_pid_state)));
281 void Mp2tMediaParser::RegisterPes(
int pmt_pid,
284 DVLOG(1) <<
"RegisterPes:"
285 <<
" pes_pid=" << pes_pid
286 <<
" stream_type=" << std::hex << stream_type << std::dec;
287 std::map<int, std::unique_ptr<PidState>>::iterator it = pids_.find(pes_pid);
288 if (it != pids_.end())
292 bool is_audio =
false;
293 std::unique_ptr<EsParser> es_parser;
294 if (stream_type == kStreamTypeAVC) {
298 base::Bind(&Mp2tMediaParser::OnNewStreamInfo,
299 base::Unretained(
this)),
300 base::Bind(&Mp2tMediaParser::OnEmitSample,
301 base::Unretained(
this))));
302 }
else if (stream_type == kStreamTypeHEVC) {
306 base::Bind(&Mp2tMediaParser::OnNewStreamInfo,
307 base::Unretained(
this)),
308 base::Bind(&Mp2tMediaParser::OnEmitSample,
309 base::Unretained(
this))));
310 }
else if (stream_type == kStreamTypeAAC) {
314 base::Bind(&Mp2tMediaParser::OnNewStreamInfo,
315 base::Unretained(
this)),
316 base::Bind(&Mp2tMediaParser::OnEmitSample,
317 base::Unretained(
this)),
325 DVLOG(1) <<
"Create a new PES state";
326 std::unique_ptr<TsSection> pes_section_parser(
327 new TsSectionPes(std::move(es_parser)));
328 PidState::PidType pid_type =
329 is_audio ? PidState::kPidAudioPes : PidState::kPidVideoPes;
330 std::unique_ptr<PidState> pes_pid_state(
331 new PidState(pes_pid, pid_type, std::move(pes_section_parser)));
332 pes_pid_state->Enable();
333 pids_.insert(std::pair<
int, std::unique_ptr<PidState>>(
334 pes_pid, std::move(pes_pid_state)));
337 void Mp2tMediaParser::OnNewStreamInfo(
338 const std::shared_ptr<StreamInfo>& new_stream_info) {
339 DCHECK(new_stream_info);
340 DVLOG(1) <<
"OnVideoConfigChanged for pid=" << new_stream_info->track_id();
342 PidMap::iterator pid_state = pids_.find(new_stream_info->track_id());
343 if (pid_state == pids_.end()) {
344 LOG(ERROR) <<
"PID State for new stream not found (pid = "
345 << new_stream_info->track_id() <<
").";
350 pid_state->second->set_config(new_stream_info);
353 FinishInitializationIfNeeded();
356 bool Mp2tMediaParser::FinishInitializationIfNeeded() {
365 std::vector<std::shared_ptr<StreamInfo>> all_stream_info;
367 for (PidMap::const_iterator iter = pids_.begin(); iter != pids_.end();
369 if (((iter->second->pid_type() == PidState::kPidAudioPes) ||
370 (iter->second->pid_type() == PidState::kPidVideoPes))) {
372 if (iter->second->config())
373 all_stream_info.push_back(iter->second->config());
376 if (num_es && (all_stream_info.size() == num_es)) {
379 init_cb_.Run(all_stream_info);
380 DVLOG(1) <<
"Mpeg2TS stream parser initialization done";
381 is_initialized_ =
true;
386 void Mp2tMediaParser::OnEmitSample(
388 const std::shared_ptr<MediaSample>& new_sample) {
395 << new_sample->data_size()
399 << new_sample->pts();
402 PidMap::iterator pid_state = pids_.find(pes_pid);
403 if (pid_state == pids_.end()) {
404 LOG(ERROR) <<
"PID State for new sample not found (pid = "
408 pid_state->second->sample_queue().push_back(new_sample);
411 bool Mp2tMediaParser::EmitRemainingSamples() {
412 DVLOG(LOG_LEVEL_ES) <<
"Mp2tMediaParser::EmitRemainingBuffers";
415 if (!is_initialized_)
419 for (PidMap::const_iterator pid_iter = pids_.begin(); pid_iter != pids_.end();
421 SampleQueue& sample_queue = pid_iter->second->sample_queue();
422 for (SampleQueue::iterator sample_iter = sample_queue.begin();
423 sample_iter != sample_queue.end();
425 if (!new_sample_cb_.Run(pid_iter->first, *sample_iter)) {
430 sample_queue.clear();