7 #include <packager/media/formats/mp2t/ts_segmenter.h>
11 #include <absl/log/check.h>
13 #include <packager/macros/status.h>
14 #include <packager/media/base/audio_stream_info.h>
15 #include <packager/media/base/muxer_util.h>
16 #include <packager/media/base/video_stream_info.h>
17 #include <packager/media/event/muxer_listener.h>
18 #include <packager/media/formats/mp2t/pes_packet.h>
19 #include <packager/media/formats/mp2t/program_map_table_writer.h>
20 #include <packager/status.h>
27 const double kTsTimescale = 90000;
29 bool IsAudioCodec(Codec codec) {
30 return codec >= kCodecAudio && codec < kCodecAudioMaxPlusOne;
33 bool IsVideoCodec(Codec codec) {
34 return codec >= kCodecVideo && codec < kCodecVideoMaxPlusOne;
40 : listener_(listener),
41 transport_stream_timestamp_offset_(
42 options.transport_stream_timestamp_offset_ms * kTsTimescale / 1000),
43 pes_packet_generator_(
46 TsSegmenter::~TsSegmenter() {}
49 if (!pes_packet_generator_->Initialize(stream_info)) {
50 return Status(error::MUXER_FAILURE,
51 "Failed to initialize PesPacketGenerator.");
54 const StreamType stream_type = stream_info.stream_type();
55 if (stream_type != StreamType::kStreamVideo &&
56 stream_type != StreamType::kStreamAudio) {
57 LOG(ERROR) <<
"TsWriter cannot handle stream type " << stream_type
59 return Status(error::MUXER_FAILURE,
"Unsupported stream type.");
62 codec_ = stream_info.codec();
63 if (stream_type == StreamType::kStreamAudio)
64 audio_codec_config_ = stream_info.codec_config();
66 timescale_scale_ = kTsTimescale / stream_info.time_scale();
76 std::unique_ptr<ProgramMapTableWriter> pmt_writer;
77 if (codec_ == kCodecAC3) {
84 const size_t kSetupDataSize = 10u;
85 if (sample.data_size() < kSetupDataSize) {
86 LOG(ERROR) <<
"Sample is too small for AC3: " << sample.data_size();
87 return Status(error::MUXER_FAILURE,
"Sample is too small for AC3.");
89 const std::vector<uint8_t> setup_data(sample.data(),
90 sample.data() + kSetupDataSize);
92 }
else if (IsAudioCodec(codec_)) {
96 DCHECK(IsVideoCodec(codec_));
99 ts_writer_.reset(
new TsWriter(std::move(pmt_writer)));
102 if (sample.is_encrypted())
103 ts_writer_->SignalEncrypted();
105 if (!segment_started_ && !sample.is_key_frame())
106 LOG(WARNING) <<
"A segment will start with a non key frame.";
108 if (!pes_packet_generator_->PushSample(sample)) {
109 return Status(error::MUXER_FAILURE,
110 "Failed to add sample to PesPacketGenerator.");
112 return WritePesPackets();
116 ts_writer_ = std::move(writer);
120 std::unique_ptr<PesPacketGenerator> generator) {
121 pes_packet_generator_ = std::move(generator);
125 segment_started_ = value;
128 Status TsSegmenter::StartSegmentIfNeeded(int64_t next_pts) {
129 if (segment_started_)
131 segment_start_timestamp_ = next_pts;
132 if (!ts_writer_->NewSegment(&segment_buffer_))
133 return Status(error::MUXER_FAILURE,
"Failed to initialize new segment.");
134 segment_started_ =
true;
138 Status TsSegmenter::WritePesPackets() {
139 while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
140 std::unique_ptr<PesPacket> pes_packet =
141 pes_packet_generator_->GetNextPesPacket();
143 Status status = StartSegmentIfNeeded(pes_packet->pts());
147 if (listener_ && IsVideoCodec(codec_) && pes_packet->is_key_frame()) {
149 uint64_t start_pos = segment_buffer_.Size();
150 const int64_t timestamp = pes_packet->pts();
151 if (!ts_writer_->AddPesPacket(std::move(pes_packet), &segment_buffer_))
152 return Status(error::MUXER_FAILURE,
"Failed to add PES packet.");
154 uint64_t end_pos = segment_buffer_.Size();
156 listener_->
OnKeyFrame(timestamp, start_pos, end_pos - start_pos);
158 if (!ts_writer_->AddPesPacket(std::move(pes_packet), &segment_buffer_))
159 return Status(error::MUXER_FAILURE,
"Failed to add PES packet.");
166 if (!pes_packet_generator_->Flush()) {
167 return Status(error::MUXER_FAILURE,
"Failed to flush PesPacketGenerator.");
169 Status status = WritePesPackets();
All the methods that are virtual are virtual for mocking.