7 #include <packager/media/formats/mp2t/ts_writer.h>
11 #include <absl/log/log.h>
13 #include <packager/media/base/buffer_writer.h>
14 #include <packager/media/base/media_sample.h>
15 #include <packager/media/formats/mp2t/pes_packet.h>
16 #include <packager/media/formats/mp2t/program_map_table_writer.h>
17 #include <packager/media/formats/mp2t/ts_packet_writer_util.h>
28 const uint8_t kProgramAssociationTableId = 0x00;
31 const uint8_t kPat[] = {
33 kProgramAssociationTableId,
43 ProgramMapTableWriter::kPmtPid,
45 0xF9, 0x62, 0xF5, 0x8B,
48 const bool kHasPcr =
true;
49 const bool kPayloadUnitStartIndicator =
true;
53 const int kTsPacketHeaderSize = 4;
54 const int kTsPacketSize = 188;
55 const int kTsPacketMaximumPayloadSize =
56 kTsPacketSize - kTsPacketHeaderSize;
58 const size_t kMaxPesPacketLengthValue = 0xFFFF;
60 void WritePatToBuffer(
const uint8_t* pat,
62 ContinuityCounter* continuity_counter,
63 BufferWriter* writer) {
64 const int kPatPid = 0;
65 WritePayloadToBufferWriter(pat, pat_size, kPayloadUnitStartIndicator, kPatPid,
66 !kHasPcr, 0, continuity_counter, writer);
70 void WritePtsOrDts(uint8_t leading_bits,
72 BufferWriter* writer) {
75 leading_bits << 4 | (((pts_or_dts >> 30) & 0x07) << 1) | 1;
77 uint8_t second_byte = (pts_or_dts >> 22) & 0xFF;
79 uint8_t third_byte = (((pts_or_dts >> 15) & 0x7F) << 1) | 1;
81 uint8_t fourth_byte = ((pts_or_dts >> 7) & 0xFF);
83 uint8_t fifth_byte = ((pts_or_dts & 0x7F) << 1) | 1;
84 writer->AppendInt(first_byte);
85 writer->AppendInt(second_byte);
86 writer->AppendInt(third_byte);
87 writer->AppendInt(fourth_byte);
88 writer->AppendInt(fifth_byte);
91 bool WritePesToBuffer(
const PesPacket& pes,
92 ContinuityCounter* continuity_counter,
93 BufferWriter* current_buffer) {
95 const int kAdaptationFieldLengthSize = 1;
97 const int kAdaptationFieldHeaderSize = 1;
98 const int kPcrFieldSize = 6;
99 const int kTsPacketMaxPayloadWithPcr =
100 kTsPacketMaximumPayloadSize - kAdaptationFieldLengthSize -
101 kAdaptationFieldHeaderSize - kPcrFieldSize;
102 const uint64_t pcr_base = pes.has_dts() ? pes.dts() : pes.pts();
103 const int pid = ProgramMapTableWriter::kElementaryPid;
106 BufferWriter pes_header_writer;
109 pes_header_writer.AppendInt(
static_cast<uint8_t
>(0x80));
110 pes_header_writer.AppendInt(
111 static_cast<uint8_t
>(
static_cast<int>(pes.has_pts()) << 7 |
112 static_cast<int>(pes.has_dts()) << 6
115 uint8_t pes_header_data_length = 0;
117 pes_header_data_length += 5;
119 pes_header_data_length += 5;
120 pes_header_writer.AppendInt(pes_header_data_length);
122 if (pes.has_pts() && pes.has_dts()) {
123 WritePtsOrDts(0x03, pes.pts(), &pes_header_writer);
124 WritePtsOrDts(0x01, pes.dts(), &pes_header_writer);
125 }
else if (pes.has_pts()) {
126 WritePtsOrDts(0x02, pes.pts(), &pes_header_writer);
131 BufferWriter first_ts_packet_buffer(kTsPacketSize);
132 first_ts_packet_buffer.AppendNBytes(
static_cast<uint64_t
>(0x000001), 3);
133 first_ts_packet_buffer.AppendInt(pes.stream_id());
134 const size_t pes_packet_length = pes.data().size() + pes_header_writer.Size();
135 first_ts_packet_buffer.AppendInt(
static_cast<uint16_t
>(
136 pes_packet_length > kMaxPesPacketLengthValue ? 0 : pes_packet_length));
137 first_ts_packet_buffer.AppendBuffer(pes_header_writer);
139 const size_t available_payload =
140 kTsPacketMaxPayloadWithPcr - first_ts_packet_buffer.Size();
141 const size_t bytes_consumed = std::min(pes.data().size(), available_payload);
142 first_ts_packet_buffer.AppendArray(pes.data().data(), bytes_consumed);
144 BufferWriter output_writer;
145 WritePayloadToBufferWriter(first_ts_packet_buffer.Buffer(),
146 first_ts_packet_buffer.Size(),
147 kPayloadUnitStartIndicator, pid, kHasPcr, pcr_base,
148 continuity_counter, &output_writer);
150 const size_t remaining_pes_data_size = pes.data().size() - bytes_consumed;
151 if (remaining_pes_data_size > 0) {
152 WritePayloadToBufferWriter(pes.data().data() + bytes_consumed,
153 remaining_pes_data_size,
154 !kPayloadUnitStartIndicator, pid, !kHasPcr, 0,
155 continuity_counter, &output_writer);
158 current_buffer->AppendBuffer(output_writer);
164 TsWriter::TsWriter(std::unique_ptr<ProgramMapTableWriter> pmt_writer)
165 : pmt_writer_(std::move(pmt_writer)) {}
167 TsWriter::~TsWriter() {}
171 WritePatToBuffer(kPat, std::size(kPat), &pat_continuity_counter_, &psi);
173 if (!pmt_writer_->EncryptedSegmentPmt(&psi)) {
177 if (!pmt_writer_->ClearSegmentPmt(&psi)) {
181 buffer->AppendBuffer(psi);
186 void TsWriter::SignalEncrypted() {
190 bool TsWriter::AddPesPacket(std::unique_ptr<PesPacket> pes_packet,
192 if (!WritePesToBuffer(*pes_packet, &elementary_stream_continuity_counter_,
194 LOG(ERROR) <<
"Failed to write pes to buffer.";
All the methods that are virtual are virtual for mocking.