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