From 5bcda6b88bad2b9afcf2163cdffc51fbb8166a9c Mon Sep 17 00:00:00 2001 From: Jacob Trimble Date: Mon, 25 Jan 2021 12:16:32 -0800 Subject: [PATCH] Use TsStreamType for MP2T parser. This also changes some of the logs to error so the user can see why the parsing failed. Change-Id: Ib8b7a5076462bccc718e17ef9e0a57d172d1f7b4 --- .../media/formats/mp2t/mp2t_media_parser.cc | 32 ++++++++----------- .../media/formats/mp2t/mp2t_media_parser.h | 3 +- packager/media/formats/mp2t/ts_section_pat.cc | 2 +- packager/media/formats/mp2t/ts_section_pmt.cc | 5 +-- packager/media/formats/mp2t/ts_section_pmt.h | 3 +- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/packager/media/formats/mp2t/mp2t_media_parser.cc b/packager/media/formats/mp2t/mp2t_media_parser.cc index 894ecb31c9..15ac98bbbf 100644 --- a/packager/media/formats/mp2t/mp2t_media_parser.cc +++ b/packager/media/formats/mp2t/mp2t_media_parser.cc @@ -99,7 +99,7 @@ bool PidState::PushTsPacket(const TsPacket& ts_packet) { int expected_continuity_counter = (continuity_counter_ + 1) % 16; if (continuity_counter_ >= 0 && ts_packet.continuity_counter() != expected_continuity_counter) { - DVLOG(1) << "TS discontinuity detected for pid: " << pid_; + LOG(ERROR) << "TS discontinuity detected for pid: " << pid_; // TODO(tinskip): Handle discontinuity better. return false; } @@ -112,7 +112,7 @@ bool PidState::PushTsPacket(const TsPacket& ts_packet) { // At the minimum, when parsing failed, auto reset the section parser. // Components that use the Mp2tMediaParser can take further action if needed. if (!status) { - DVLOG(1) << "Parsing failed for pid = " << pid_; + LOG(ERROR) << "Parsing failed for pid = " << pid_ << ", type=" << pid_type_; ResetState(); } @@ -234,8 +234,7 @@ bool Mp2tMediaParser::Parse(const uint8_t* buf, int size) { } if (it != pids_.end()) { - if (!it->second->PushTsPacket(*ts_packet)) - return false; + RCHECK(it->second->PushTsPacket(*ts_packet)); } else { DVLOG(LOG_LEVEL_TS) << "Ignoring TS packet for pid: " << ts_packet->pid(); } @@ -274,12 +273,12 @@ void Mp2tMediaParser::RegisterPmt(int program_number, int pmt_pid) { void Mp2tMediaParser::RegisterPes(int pmt_pid, int pes_pid, - uint8_t stream_type) { + TsStreamType stream_type) { if (pids_.count(pes_pid) != 0) return; DVLOG(1) << "RegisterPes:" << " pes_pid=" << pes_pid << " stream_type=" << std::hex - << stream_type << std::dec; + << static_cast(stream_type) << std::dec; // Create a stream parser corresponding to the stream type. bool is_audio = false; @@ -288,7 +287,7 @@ void Mp2tMediaParser::RegisterPes(int pmt_pid, base::Unretained(this), pes_pid); auto on_emit_media = base::Bind(&Mp2tMediaParser::OnEmitMediaSample, base::Unretained(this), pes_pid); - switch (static_cast(stream_type)) { + switch (stream_type) { case TsStreamType::kAvc: es_parser.reset(new EsParserH264(pes_pid, on_new_stream, on_emit_media)); break; @@ -304,10 +303,11 @@ void Mp2tMediaParser::RegisterPes(int pmt_pid, is_audio = true; break; default: { - LOG_IF(ERROR, !stream_type_logged_once_[stream_type]) - << "Ignore unsupported MPEG2TS stream type 0x" << std::hex - << stream_type << std::dec; - stream_type_logged_once_[stream_type] = true; + auto type = static_cast(stream_type); + LOG_IF(ERROR, !stream_type_logged_once_[type]) + << "Ignore unsupported MPEG2TS stream type 0x" << std::hex << type + << std::dec; + stream_type_logged_once_[type] = true; return; } } @@ -427,18 +427,12 @@ bool Mp2tMediaParser::EmitRemainingSamples() { // Buffer emission. for (const auto& pid_pair : pids_) { for (auto sample : pid_pair.second->media_sample_queue_) { - if (!new_media_sample_cb_.Run(pid_pair.first, sample)) { - // Error processing sample. Propagate error condition. - return false; - } + RCHECK(new_media_sample_cb_.Run(pid_pair.first, sample)); } pid_pair.second->media_sample_queue_.clear(); for (auto sample : pid_pair.second->text_sample_queue_) { - if (!new_text_sample_cb_.Run(pid_pair.first, sample)) { - // Error processing sample. Propagate error condition. - return false; - } + RCHECK(new_text_sample_cb_.Run(pid_pair.first, sample)); } pid_pair.second->text_sample_queue_.clear(); } diff --git a/packager/media/formats/mp2t/mp2t_media_parser.h b/packager/media/formats/mp2t/mp2t_media_parser.h index d3708ae5b4..c33081ccce 100644 --- a/packager/media/formats/mp2t/mp2t_media_parser.h +++ b/packager/media/formats/mp2t/mp2t_media_parser.h @@ -13,6 +13,7 @@ #include "packager/media/base/byte_queue.h" #include "packager/media/base/media_parser.h" #include "packager/media/base/stream_info.h" +#include "packager/media/formats/mp2t/ts_stream_type.h" namespace shaka { namespace media { @@ -49,7 +50,7 @@ class Mp2tMediaParser : public MediaParser { // Possible values for |media_type| are defined in: // ISO-13818.1 / ITU H.222 Table 2.34 "Media type assignments". // |pes_pid| is part of the Program Map Table refered by |pmt_pid|. - void RegisterPes(int pmt_pid, int pes_pid, uint8_t media_type); + void RegisterPes(int pmt_pid, int pes_pid, TsStreamType media_type); // Callback invoked each time the audio/video decoder configuration is // changed. diff --git a/packager/media/formats/mp2t/ts_section_pat.cc b/packager/media/formats/mp2t/ts_section_pat.cc index 81e1d81f4a..f90f5b1c45 100644 --- a/packager/media/formats/mp2t/ts_section_pat.cc +++ b/packager/media/formats/mp2t/ts_section_pat.cc @@ -87,7 +87,7 @@ bool TsSectionPat::ParsePsiSection(BitReader* bit_reader) { // Both the MSE and the HLS spec specifies that TS streams should convey // exactly one program. if (pmt_pid_count > 1) { - DVLOG(1) << "Multiple programs detected in the Mpeg2 TS stream"; + LOG(ERROR) << "Multiple programs detected in the Mpeg2 TS stream"; return false; } diff --git a/packager/media/formats/mp2t/ts_section_pmt.cc b/packager/media/formats/mp2t/ts_section_pmt.cc index 7fbd22a528..a0c23c7d4a 100644 --- a/packager/media/formats/mp2t/ts_section_pmt.cc +++ b/packager/media/formats/mp2t/ts_section_pmt.cc @@ -9,6 +9,7 @@ #include "packager/base/logging.h" #include "packager/media/base/bit_reader.h" #include "packager/media/formats/mp2t/mp2t_common.h" +#include "packager/media/formats/mp2t/ts_stream_type.h" namespace shaka { namespace media { @@ -75,10 +76,10 @@ bool TsSectionPmt::ParsePsiSection(BitReader* bit_reader) { // The end of the PID map if 4 bytes away from the end of the section // (4 bytes = size of the CRC). int pid_map_end_marker = section_start_marker - section_length + 4; - std::map pid_map; + std::map pid_map; while (static_cast(bit_reader->bits_available()) > 8 * pid_map_end_marker) { - uint8_t stream_type; + TsStreamType stream_type; int pid_es; int es_info_length; RCHECK(bit_reader->ReadBits(8, &stream_type)); diff --git a/packager/media/formats/mp2t/ts_section_pmt.h b/packager/media/formats/mp2t/ts_section_pmt.h index 8e600c1570..4422ee679a 100644 --- a/packager/media/formats/mp2t/ts_section_pmt.h +++ b/packager/media/formats/mp2t/ts_section_pmt.h @@ -8,6 +8,7 @@ #include "packager/base/callback.h" #include "packager/base/compiler_specific.h" #include "packager/media/formats/mp2t/ts_section_psi.h" +#include "packager/media/formats/mp2t/ts_stream_type.h" namespace shaka { namespace media { @@ -18,7 +19,7 @@ class TsSectionPmt : public TsSectionPsi { // RegisterPesCb::Run(int pes_pid, int stream_type); // Stream type is defined in // "Table 2-34 – Stream type assignments" in H.222 - typedef base::Callback RegisterPesCb; + typedef base::Callback RegisterPesCb; explicit TsSectionPmt(const RegisterPesCb& register_pes_cb); ~TsSectionPmt() override;