Shaka Packager SDK
Loading...
Searching...
No Matches
ts_writer.cc
1// Copyright 2016 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#include <packager/media/formats/mp2t/ts_writer.h>
8
9#include <algorithm>
10#include <cstddef>
11#include <cstdint>
12#include <iterator>
13#include <memory>
14#include <utility>
15
16#include <absl/log/log.h>
17
18#include <packager/media/base/buffer_writer.h>
19#include <packager/media/formats/mp2t/pes_packet.h>
20#include <packager/media/formats/mp2t/program_map_table_writer.h>
21#include <packager/media/formats/mp2t/ts_packet_writer_util.h>
22
23namespace shaka {
24namespace media {
25namespace mp2t {
26
27namespace {
28
29// For all the pointer fields in the following PAT and PMTs, they are not really
30// part of PAT or PMT but it's there so that TsPacket can point to a memory
31// location that starts from pointer field.
32const uint8_t kProgramAssociationTableId = 0x00;
33
34// This PAT can be used for both encrypted and clear.
35const uint8_t kPat[] = {
36 // clang-format off
37 0x00, // pointer field
38 kProgramAssociationTableId,
39 0xB0, // The last 2 '00' assumes that this PAT is not very long.
40 0x0D, // Length of the rest of this array.
41 0x00, 0x00, // Transport stream ID is 0.
42 0xC1, // version number 0, current next indicator 1.
43 0x00, // section number
44 0x00, // last section number
45 // program number -> PMT PID mapping.
46 0x00, 0x01, // program number is 1.
47 0xE0, // first 3 bits is reserved.
48 ProgramMapTableWriter::kPmtPid,
49 // CRC32.
50 0xF9, 0x62, 0xF5, 0x8B,
51 // clang-format on
52};
53
54const bool kHasPcr = true;
55const bool kPayloadUnitStartIndicator = true;
56
57// This is the size of the first few fields in a TS packet, i.e. TS packet size
58// without adaptation field or the payload.
59const int kTsPacketHeaderSize = 4;
60const int kTsPacketSize = 188;
61const int kTsPacketMaximumPayloadSize = kTsPacketSize - kTsPacketHeaderSize;
62
63const size_t kMaxPesPacketLengthValue = 0xFFFF;
64
65void WritePatToBuffer(const uint8_t* pat,
66 int pat_size,
67 ContinuityCounter* continuity_counter,
68 BufferWriter* writer) {
69 const int kPatPid = 0;
70 WritePayloadToBufferWriter(pat, pat_size, kPayloadUnitStartIndicator, kPatPid,
71 !kHasPcr, 0, continuity_counter, writer);
72}
73
74// The only difference between writing PTS or DTS is the leading bits.
75void WritePtsOrDts(uint8_t leading_bits,
76 uint64_t pts_or_dts,
77 BufferWriter* writer) {
78 // First byte has 3 MSB of PTS.
79 uint8_t first_byte =
80 leading_bits << 4 | (((pts_or_dts >> 30) & 0x07) << 1) | 1;
81 // Second byte has the next 8 bits of pts.
82 uint8_t second_byte = (pts_or_dts >> 22) & 0xFF;
83 // Third byte has the next 7 bits of pts followed by a marker bit.
84 uint8_t third_byte = (((pts_or_dts >> 15) & 0x7F) << 1) | 1;
85 // Fourth byte has the next 8 bits of pts.
86 uint8_t fourth_byte = ((pts_or_dts >> 7) & 0xFF);
87 // Fifth byte has the last 7 bits of pts followed by a marker bit.
88 uint8_t fifth_byte = ((pts_or_dts & 0x7F) << 1) | 1;
89 writer->AppendInt(first_byte);
90 writer->AppendInt(second_byte);
91 writer->AppendInt(third_byte);
92 writer->AppendInt(fourth_byte);
93 writer->AppendInt(fifth_byte);
94}
95
96bool WritePesToBuffer(const PesPacket& pes,
97 ContinuityCounter* continuity_counter,
98 BufferWriter* current_buffer) {
99 // The size of the length field.
100 const int kAdaptationFieldLengthSize = 1;
101 // The size of the flags field.
102 const int kAdaptationFieldHeaderSize = 1;
103 const int kPcrFieldSize = 6;
104 const int kTsPacketMaxPayloadWithPcr =
105 kTsPacketMaximumPayloadSize - kAdaptationFieldLengthSize -
106 kAdaptationFieldHeaderSize - kPcrFieldSize;
107 const uint64_t pcr_base = pes.has_dts() ? pes.dts() : pes.pts();
108 const int pid = ProgramMapTableWriter::kElementaryPid;
109
110 // This writer will hold part of PES packet after PES_packet_length field.
111 BufferWriter pes_header_writer;
112 // The first bit must be '10' for PES with video or audio stream id. The other
113 // flags (bits) don't matter so they are 0.
114 pes_header_writer.AppendInt(static_cast<uint8_t>(0x80));
115 pes_header_writer.AppendInt(
116 static_cast<uint8_t>(static_cast<int>(pes.has_pts()) << 7 |
117 static_cast<int>(pes.has_dts()) << 6
118 // Other fields are all 0.
119 ));
120 uint8_t pes_header_data_length = 0;
121 if (pes.has_pts())
122 pes_header_data_length += 5;
123 if (pes.has_dts())
124 pes_header_data_length += 5;
125 pes_header_writer.AppendInt(pes_header_data_length);
126
127 if (pes.has_pts() && pes.has_dts()) {
128 WritePtsOrDts(0x03, pes.pts(), &pes_header_writer);
129 WritePtsOrDts(0x01, pes.dts(), &pes_header_writer);
130 } else if (pes.has_pts()) {
131 WritePtsOrDts(0x02, pes.pts(), &pes_header_writer);
132 }
133
134 // Put the first TS packet's payload into a buffer. This contains the PES
135 // packet's header.
136 BufferWriter first_ts_packet_buffer(kTsPacketSize);
137 first_ts_packet_buffer.AppendNBytes(static_cast<uint64_t>(0x000001), 3);
138 first_ts_packet_buffer.AppendInt(pes.stream_id());
139 const size_t pes_packet_length = pes.data().size() + pes_header_writer.Size();
140 first_ts_packet_buffer.AppendInt(static_cast<uint16_t>(
141 pes_packet_length > kMaxPesPacketLengthValue ? 0 : pes_packet_length));
142 first_ts_packet_buffer.AppendBuffer(pes_header_writer);
143
144 const size_t available_payload =
145 kTsPacketMaxPayloadWithPcr - first_ts_packet_buffer.Size();
146 const size_t bytes_consumed = std::min(pes.data().size(), available_payload);
147 first_ts_packet_buffer.AppendArray(pes.data().data(), bytes_consumed);
148
149 BufferWriter output_writer;
150 WritePayloadToBufferWriter(first_ts_packet_buffer.Buffer(),
151 first_ts_packet_buffer.Size(),
152 kPayloadUnitStartIndicator, pid, kHasPcr, pcr_base,
153 continuity_counter, &output_writer);
154
155 const size_t remaining_pes_data_size = pes.data().size() - bytes_consumed;
156 if (remaining_pes_data_size > 0) {
157 WritePayloadToBufferWriter(pes.data().data() + bytes_consumed,
158 remaining_pes_data_size,
159 !kPayloadUnitStartIndicator, pid, !kHasPcr, 0,
160 continuity_counter, &output_writer);
161 }
162
163 current_buffer->AppendBuffer(output_writer);
164 return true;
165}
166
167} // namespace
168
169TsWriter::TsWriter(std::unique_ptr<ProgramMapTableWriter> pmt_writer)
170 : pmt_writer_(std::move(pmt_writer)) {}
171
172TsWriter::~TsWriter() {}
173
174bool TsWriter::NewSegment(BufferWriter* buffer) {
175 BufferWriter psi;
176 WritePatToBuffer(kPat, std::size(kPat), &pat_continuity_counter_, &psi);
177 if (encrypted_) {
178 if (!pmt_writer_->EncryptedSegmentPmt(&psi)) {
179 return false;
180 }
181 } else {
182 if (!pmt_writer_->ClearSegmentPmt(&psi)) {
183 return false;
184 }
185 }
186 buffer->AppendBuffer(psi);
187
188 return true;
189}
190
191void TsWriter::SignalEncrypted() {
192 encrypted_ = true;
193}
194
195bool TsWriter::AddPesPacket(std::unique_ptr<PesPacket> pes_packet,
196 BufferWriter* buffer) {
197 if (!WritePesToBuffer(*pes_packet, &elementary_stream_continuity_counter_,
198 buffer)) {
199 LOG(ERROR) << "Failed to write pes to buffer.";
200 return false;
201 }
202
203 // No need to keep pes_packet around so not passing it anywhere.
204 return true;
205}
206
207} // namespace mp2t
208} // namespace media
209} // namespace shaka
All the methods that are virtual are virtual for mocking.