7 #include <packager/media/formats/mp2t/es_parser_dvb.h>
9 #include <packager/media/base/bit_reader.h>
10 #include <packager/media/base/text_stream_info.h>
11 #include <packager/media/base/timestamp.h>
12 #include <packager/media/formats/mp2t/mp2t_common.h>
20 bool ParseSubtitlingDescriptor(
21 const uint8_t* descriptor,
23 std::unordered_map<uint16_t, std::string>* langs) {
25 BitReader reader(descriptor, size);
27 RCHECK(reader.SkipBits(8));
28 RCHECK(reader.ReadBits(8, &data_size));
29 RCHECK(data_size + 2 <= size);
30 for (
size_t i = 0; i < data_size; i += 8) {
33 RCHECK(reader.ReadBits(24, &lang_code));
34 RCHECK(reader.SkipBits(8));
35 RCHECK(reader.ReadBits(16, &page));
36 RCHECK(reader.SkipBits(16));
39 std::string lang(3,
'\0');
40 lang[0] = (lang_code >> 16) & 0xff;
41 lang[1] = (lang_code >> 8) & 0xff;
42 lang[2] = (lang_code >> 0) & 0xff;
43 langs->emplace(page, std::move(lang));
50 EsParserDvb::EsParserDvb(uint32_t pid,
51 const NewStreamInfoCB& new_stream_info_cb,
52 const EmitTextSampleCB& emit_sample_cb,
53 const uint8_t* descriptor,
54 size_t descriptor_length)
56 new_stream_info_cb_(new_stream_info_cb),
57 emit_sample_cb_(emit_sample_cb) {
58 if (!ParseSubtitlingDescriptor(descriptor, descriptor_length, &languages_)) {
59 LOG(WARNING) <<
"Error parsing subtitling descriptor";
63 EsParserDvb::~EsParserDvb() {}
65 bool EsParserDvb::Parse(
const uint8_t* buf,
71 std::shared_ptr<TextStreamInfo> info = std::make_shared<TextStreamInfo>(
72 pid(), kMpeg2Timescale, kInfiniteDuration, kCodecText,
75 for (
const auto& pair : languages_) {
76 info->AddSubStream(pair.first, {pair.second});
79 new_stream_info_cb_(info);
84 return ParseInternal(buf, size, pts);
87 bool EsParserDvb::Flush() {
88 for (
auto& pair : parsers_) {
89 std::vector<std::shared_ptr<TextSample>> samples;
90 RCHECK(pair.second.Flush(&samples));
92 for (
auto sample : samples) {
93 sample->set_sub_stream_index(pair.first);
94 emit_sample_cb_(sample);
100 void EsParserDvb::Reset() {
104 bool EsParserDvb::ParseInternal(
const uint8_t* data,
size_t size, int64_t pts) {
106 BitReader reader(data, size);
108 int subtitle_stream_id;
109 RCHECK(reader.ReadBits(8, &data_identifier));
110 RCHECK(reader.ReadBits(8, &subtitle_stream_id));
111 RCHECK(data_identifier == 0x20);
112 RCHECK(subtitle_stream_id == 0);
115 while (reader.ReadBits(8, &temp) && temp == 0xf) {
116 DvbSubSegmentType segment_type;
118 size_t segment_length;
119 RCHECK(reader.ReadBits(8, &segment_type));
120 RCHECK(reader.ReadBits(16, &page_id));
121 RCHECK(reader.ReadBits(16, &segment_length));
122 RCHECK(reader.bits_available() > segment_length * 8);
124 const uint8_t* payload = data + (size - reader.bits_available() / 8);
125 std::vector<std::shared_ptr<TextSample>> samples;
126 RCHECK(parsers_[page_id].Parse(segment_type, pts, payload, segment_length,
128 for (
auto sample : samples) {
129 sample->set_sub_stream_index(page_id);
130 emit_sample_cb_(sample);
133 RCHECK(reader.SkipBytes(segment_length));
All the methods that are virtual are virtual for mocking.