7 #include "packager/media/formats/mp2t/ts_writer.h"
11 #include "packager/base/logging.h"
12 #include "packager/media/base/audio_stream_info.h"
13 #include "packager/media/base/buffer_writer.h"
14 #include "packager/media/base/stream_info.h"
15 #include "packager/media/base/video_stream_info.h"
16 #include "packager/media/formats/mp2t/ts_packet_writer_util.h"
27 const uint8_t kProgramAssociationTableId = 0x00;
30 const uint8_t kPat[] = {
32 kProgramAssociationTableId,
42 ProgramMapTableWriter::kPmtPid,
44 0xF9, 0x62, 0xF5, 0x8B,
47 const bool kHasPcr =
true;
48 const bool kPayloadUnitStartIndicator =
true;
52 const int kTsPacketHeaderSize = 4;
53 const int kTsPacketSize = 188;
54 const int kTsPacketMaximumPayloadSize =
55 kTsPacketSize - kTsPacketHeaderSize;
57 const size_t kMaxPesPacketLengthValue = 0xFFFF;
59 void WritePatToBuffer(
const uint8_t* pat,
61 ContinuityCounter* continuity_counter,
62 BufferWriter* writer) {
63 const int kPatPid = 0;
64 WritePayloadToBufferWriter(pat, pat_size, kPayloadUnitStartIndicator, kPatPid,
65 !kHasPcr, 0, continuity_counter, writer);
69 void WritePtsOrDts(uint8_t leading_bits,
71 BufferWriter* writer) {
74 leading_bits << 4 | (((pts_or_dts >> 30) & 0x07) << 1) | 1;
76 uint8_t second_byte = (pts_or_dts >> 22) & 0xFF;
78 uint8_t third_byte = (((pts_or_dts >> 15) & 0x7F) << 1) | 1;
80 uint8_t fourth_byte = ((pts_or_dts >> 7) & 0xFF);
82 uint8_t fifth_byte = ((pts_or_dts & 0x7F) << 1) | 1;
83 writer->AppendInt(first_byte);
84 writer->AppendInt(second_byte);
85 writer->AppendInt(third_byte);
86 writer->AppendInt(fourth_byte);
87 writer->AppendInt(fifth_byte);
90 bool WritePesToFile(
const PesPacket& pes,
91 ContinuityCounter* continuity_counter,
94 const int kAdaptationFieldLengthSize = 1;
96 const int kAdaptationFieldHeaderSize = 1;
97 const int kPcrFieldSize = 6;
98 const int kTsPacketMaxPayloadWithPcr =
99 kTsPacketMaximumPayloadSize - kAdaptationFieldLengthSize -
100 kAdaptationFieldHeaderSize - kPcrFieldSize;
101 const uint64_t pcr_base = pes.has_dts() ? pes.dts() : pes.pts();
102 const int pid = ProgramMapTableWriter::kElementaryPid;
105 BufferWriter pes_header_writer;
108 pes_header_writer.AppendInt(static_cast<uint8_t>(0x80));
109 pes_header_writer.AppendInt(
110 static_cast<uint8_t>(static_cast<int>(pes.has_pts()) << 7 |
111 static_cast<int>(pes.has_dts()) << 6
114 uint8_t pes_header_data_length = 0;
116 pes_header_data_length += 5;
118 pes_header_data_length += 5;
119 pes_header_writer.AppendInt(pes_header_data_length);
121 if (pes.has_pts() && pes.has_dts()) {
122 WritePtsOrDts(0x03, pes.pts(), &pes_header_writer);
123 WritePtsOrDts(0x01, pes.dts(), &pes_header_writer);
124 }
else if (pes.has_pts()) {
125 WritePtsOrDts(0x02, pes.pts(), &pes_header_writer);
130 BufferWriter first_ts_packet_buffer(kTsPacketSize);
131 first_ts_packet_buffer.AppendNBytes(static_cast<uint64_t>(0x000001), 3);
132 first_ts_packet_buffer.AppendInt(pes.stream_id());
133 const size_t pes_packet_length = pes.data().size() + pes_header_writer.Size();
134 first_ts_packet_buffer.AppendInt(static_cast<uint16_t>(
135 pes_packet_length > kMaxPesPacketLengthValue ? 0 : pes_packet_length));
136 first_ts_packet_buffer.AppendBuffer(pes_header_writer);
138 const int available_payload =
139 kTsPacketMaxPayloadWithPcr - first_ts_packet_buffer.Size();
140 const int bytes_consumed =
141 std::min(static_cast<int>(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);
157 return output_writer.WriteToFile(file).ok();
162 TsWriter::TsWriter() {}
163 TsWriter::~TsWriter() {}
166 const StreamType stream_type = stream_info.stream_type();
167 if (stream_type != StreamType::kStreamVideo &&
168 stream_type != StreamType::kStreamAudio) {
169 LOG(ERROR) <<
"TsWriter cannot handle stream type " << stream_type
174 if (stream_info.stream_type() == StreamType::kStreamVideo) {
177 if (video_stream_info.codec() != Codec::kCodecH264) {
178 LOG(ERROR) <<
"TsWriter cannot handle video codec "
179 << video_stream_info.codec() <<
" yet.";
184 DCHECK_EQ(stream_type, StreamType::kStreamAudio);
187 if (audio_stream_info.codec() != Codec::kCodecAAC) {
188 LOG(ERROR) <<
"TsWriter cannot handle audio codec "
189 << audio_stream_info.codec() <<
" yet.";
193 audio_stream_info.codec_config(), &pmt_continuity_counter_));
201 LOG(ERROR) <<
"File " << current_file_->file_name() <<
" still open.";
204 current_file_.reset(
File::Open(file_name.c_str(),
"w"));
205 if (!current_file_) {
206 LOG(ERROR) <<
"Failed to open file " << file_name;
211 WritePatToBuffer(kPat, arraysize(kPat), &pat_continuity_counter_, &psi);
213 if (!pmt_writer_->EncryptedSegmentPmt(&psi)) {
217 if (!pmt_writer_->ClearSegmentPmt(&psi)) {
223 LOG(ERROR) <<
"Failed to write PSI to file.";
235 return current_file_.release()->Close();
239 DCHECK(current_file_);
240 if (!WritePesToFile(*pes_packet, &elementary_stream_continuity_counter_,
241 current_file_.get())) {
242 LOG(ERROR) <<
"Failed to write pes to file.";
251 scoped_ptr<ProgramMapTableWriter> table_writer) {
252 pmt_writer_ = table_writer.Pass();