5 #include <packager/media/formats/mp2t/ts_section_pmt.h>
9 #include <absl/log/log.h>
11 #include <packager/media/base/bit_reader.h>
12 #include <packager/media/formats/mp2t/mp2t_common.h>
13 #include <packager/media/formats/mp2t/ts_audio_type.h>
14 #include <packager/media/formats/mp2t/ts_stream_type.h>
22 const int kISO639LanguageDescriptor = 0x0A;
23 const int kMaximumBitrateDescriptor = 0x0E;
24 const int kTeletextDescriptor = 0x56;
25 const int kSubtitlingDescriptor = 0x59;
29 TsSectionPmt::TsSectionPmt(
const RegisterPesCb& register_pes_cb)
30 : register_pes_cb_(register_pes_cb) {
33 TsSectionPmt::~TsSectionPmt() {
36 bool TsSectionPmt::ParsePsiSection(BitReader* bit_reader) {
39 int section_syntax_indicator;
45 int current_next_indicator;
47 int last_section_number;
48 RCHECK(bit_reader->ReadBits(8, &table_id));
49 RCHECK(bit_reader->ReadBits(1, §ion_syntax_indicator));
50 RCHECK(bit_reader->ReadBits(1, &dummy_zero));
51 RCHECK(bit_reader->ReadBits(2, &reserved));
52 RCHECK(bit_reader->ReadBits(12, §ion_length));
53 int section_start_marker =
static_cast<int>(bit_reader->bits_available()) / 8;
55 RCHECK(bit_reader->ReadBits(16, &program_number));
56 RCHECK(bit_reader->ReadBits(2, &reserved));
57 RCHECK(bit_reader->ReadBits(5, &version_number));
58 RCHECK(bit_reader->ReadBits(1, ¤t_next_indicator));
59 RCHECK(bit_reader->ReadBits(8, §ion_number));
60 RCHECK(bit_reader->ReadBits(8, &last_section_number));
66 RCHECK(table_id == 0x2);
67 RCHECK(section_syntax_indicator);
69 RCHECK(section_length <= 1021);
70 RCHECK(section_number == 0);
71 RCHECK(last_section_number == 0);
75 int program_info_length;
76 RCHECK(bit_reader->ReadBits(3, &reserved));
77 RCHECK(bit_reader->ReadBits(13, &pcr_pid));
78 RCHECK(bit_reader->ReadBits(4, &reserved));
79 RCHECK(bit_reader->ReadBits(12, &program_info_length));
80 RCHECK(program_info_length < 1024);
84 RCHECK(bit_reader->SkipBits(8 * program_info_length));
89 int pid_map_end_marker = section_start_marker - section_length + 4;
92 TsStreamType stream_type;
93 const uint8_t* descriptor;
94 size_t descriptor_length;
97 TsAudioType audio_type;
99 std::vector<Info> pid_info;
100 while (
static_cast<int>(bit_reader->bits_available()) >
101 8 * pid_map_end_marker) {
102 TsStreamType stream_type;
104 size_t es_info_length;
105 RCHECK(bit_reader->ReadBits(8, &stream_type));
106 RCHECK(bit_reader->SkipBits(3));
107 RCHECK(bit_reader->ReadBits(13, &pid_es));
108 RCHECK(bit_reader->ReadBits(4, &reserved));
109 RCHECK(bit_reader->ReadBits(12, &es_info_length));
110 const uint8_t* descriptor = bit_reader->current_byte_ptr();
115 pid_info.push_back({pid_es, stream_type, descriptor, es_info_length,
"", 0,
116 TsAudioType::kUndefined});
120 uint8_t descriptor_tag;
121 uint8_t descriptor_length;
123 while (es_info_length) {
124 RCHECK(bit_reader->ReadBits(8, &descriptor_tag));
125 RCHECK(bit_reader->ReadBits(8, &descriptor_length));
129 if (stream_type == TsStreamType::kPesPrivateData) {
130 switch (descriptor_tag) {
131 case kTeletextDescriptor:
132 pid_info.back().stream_type = TsStreamType::kTeletextSubtitles;
134 case kSubtitlingDescriptor:
135 pid_info.back().stream_type = TsStreamType::kDvbSubtitles;
140 }
else if (descriptor_tag == kISO639LanguageDescriptor &&
141 descriptor_length >= 4) {
145 RCHECK(es_info_length >= 4);
148 RCHECK(bit_reader->ReadBits(8, &lang[0]));
149 RCHECK(bit_reader->ReadBits(8, &lang[1]));
150 RCHECK(bit_reader->ReadBits(8, &lang[2]));
151 RCHECK(bit_reader->ReadBits(8, &pid_info.back().audio_type));
152 pid_info.back().lang = std::string(lang, 3);
155 descriptor_length -= 4;
156 }
else if (descriptor_tag == kMaximumBitrateDescriptor &&
157 descriptor_length >= 3) {
159 RCHECK(es_info_length >= 3);
161 uint32_t max_bitrate;
162 RCHECK(bit_reader->SkipBits(2));
163 RCHECK(bit_reader->ReadBits(22, &max_bitrate));
165 pid_info.back().max_bitrate = 50 * 8 * max_bitrate;
168 descriptor_length -= 3;
171 RCHECK(bit_reader->SkipBits(8 * descriptor_length));
172 es_info_length -= descriptor_length;
175 RCHECK(bit_reader->SkipBytes(es_info_length));
180 RCHECK(bit_reader->ReadBits(32, &crc32));
183 for (
auto& info : pid_info) {
184 register_pes_cb_(info.pid_es, info.stream_type, info.max_bitrate, info.lang,
185 info.audio_type, info.descriptor, info.descriptor_length);
191 void TsSectionPmt::ResetPsiSection() {
All the methods that are virtual are virtual for mocking.