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 0x00, // pointer field
33 kProgramAssociationTableId,
34 0xB0, // The last 2 '00' assumes that this PAT is not very long.
35 0x0D, // Length of the rest of this array.
36 0x00, 0x00, // Transport stream ID is 0.
37 0xC1, // version number 0, current next indicator 1.
38 0x00, // section number
39 0x00, // last section number
40 // program number -> PMT PID mapping.
41 0x00, 0x01, // program number is 1.
42 0xE0, // first 3 bits is reserved.
43 ProgramMapTableWriter::kPmtPid,
44 // CRC32.
45 0xF9, 0x62, 0xF5, 0x8B,
46};
47
48const bool kHasPcr = true;
49const bool kPayloadUnitStartIndicator = true;
50
51// This is the size of the first few fields in a TS packet, i.e. TS packet size
52// without adaptation field or the payload.
53const int kTsPacketHeaderSize = 4;
54const int kTsPacketSize = 188;
55const int kTsPacketMaximumPayloadSize =
56 kTsPacketSize - kTsPacketHeaderSize;
57
58const size_t kMaxPesPacketLengthValue = 0xFFFF;
59
60void WritePatToBuffer(const uint8_t* pat,
61 int pat_size,
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);
67}
68
69// The only difference between writing PTS or DTS is the leading bits.
70void WritePtsOrDts(uint8_t leading_bits,
71 uint64_t pts_or_dts,
72 BufferWriter* writer) {
73 // First byte has 3 MSB of PTS.
74 uint8_t first_byte =
75 leading_bits << 4 | (((pts_or_dts >> 30) & 0x07) << 1) | 1;
76 // Second byte has the next 8 bits of pts.
77 uint8_t second_byte = (pts_or_dts >> 22) & 0xFF;
78 // Third byte has the next 7 bits of pts followed by a marker bit.
79 uint8_t third_byte = (((pts_or_dts >> 15) & 0x7F) << 1) | 1;
80 // Fourth byte has the next 8 bits of pts.
81 uint8_t fourth_byte = ((pts_or_dts >> 7) & 0xFF);
82 // Fifth byte has the last 7 bits of pts followed by a marker bit.
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);
89}
90
91bool WritePesToBuffer(const PesPacket& pes,
92 ContinuityCounter* continuity_counter,
93 BufferWriter* current_buffer) {
94 // The size of the length field.
95 const int kAdaptationFieldLengthSize = 1;
96 // The size of the flags field.
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;
104
105 // This writer will hold part of PES packet after PES_packet_length field.
106 BufferWriter pes_header_writer;
107 // The first bit must be '10' for PES with video or audio stream id. The other
108 // flags (bits) don't matter so they are 0.
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
113 // Other fields are all 0.
114 ));
115 uint8_t pes_header_data_length = 0;
116 if (pes.has_pts())
117 pes_header_data_length += 5;
118 if (pes.has_dts())
119 pes_header_data_length += 5;
120 pes_header_writer.AppendInt(pes_header_data_length);
121
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);
127 }
128
129 // Put the first TS packet's payload into a buffer. This contains the PES
130 // packet's header.
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);
138
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);
143
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);
149
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);
156 }
157
158 current_buffer->AppendBuffer(output_writer);
159 return true;
160}
161
162} // namespace
163
164TsWriter::TsWriter(std::unique_ptr<ProgramMapTableWriter> pmt_writer)
165 : pmt_writer_(std::move(pmt_writer)) {}
166
167TsWriter::~TsWriter() {}
168
169bool TsWriter::NewSegment(BufferWriter* buffer) {
170 BufferWriter psi;
171 WritePatToBuffer(kPat, std::size(kPat), &pat_continuity_counter_, &psi);
172 if (encrypted_) {
173 if (!pmt_writer_->EncryptedSegmentPmt(&psi)) {
174 return false;
175 }
176 } else {
177 if (!pmt_writer_->ClearSegmentPmt(&psi)) {
178 return false;
179 }
180 }
181 buffer->AppendBuffer(psi);
182
183 return true;
184}
185
186void TsWriter::SignalEncrypted() {
187 encrypted_ = true;
188}
189
190bool TsWriter::AddPesPacket(std::unique_ptr<PesPacket> pes_packet,
191 BufferWriter* buffer) {
192 if (!WritePesToBuffer(*pes_packet, &elementary_stream_continuity_counter_,
193 buffer)) {
194 LOG(ERROR) << "Failed to write pes to buffer.";
195 return false;
196 }
197
198 // No need to keep pes_packet around so not passing it anywhere.
199 return true;
200}
201
202} // namespace mp2t
203} // namespace media
204} // namespace shaka
All the methods that are virtual are virtual for mocking.