7 #include "packager/media/formats/mp2t/ts_segmenter.h"
11 #include "packager/media/base/muxer_util.h"
12 #include "packager/media/event/muxer_listener.h"
13 #include "packager/status.h"
20 const double kTsTimescale = 90000;
24 : muxer_options_(options),
28 TsSegmenter::~TsSegmenter() {}
32 return Status(error::MUXER_FAILURE,
"Segment template not specified.");
33 if (!ts_writer_->Initialize(stream_info))
34 return Status(error::MUXER_FAILURE,
"Failed to initialize TsWriter.");
35 if (!pes_packet_generator_->Initialize(stream_info)) {
36 return Status(error::MUXER_FAILURE,
37 "Failed to initialize PesPacketGenerator.");
40 timescale_scale_ = kTsTimescale / stream_info.time_scale();
49 if (sample.is_encrypted())
50 ts_writer_->SignalEncrypted();
52 if (!ts_writer_file_opened_ && !sample.is_key_frame())
53 LOG(WARNING) <<
"A segment will start with a non key frame.";
55 if (!pes_packet_generator_->PushSample(sample)) {
56 return Status(error::MUXER_FAILURE,
57 "Failed to add sample to PesPacketGenerator.");
59 return WritePesPacketsToFile();
63 ts_writer_ = std::move(writer);
67 std::unique_ptr<PesPacketGenerator> generator) {
68 pes_packet_generator_ = std::move(generator);
72 ts_writer_file_opened_ = value;
75 Status TsSegmenter::OpenNewSegmentIfClosed(uint32_t next_pts) {
76 if (ts_writer_file_opened_)
78 const std::string segment_name =
80 segment_number_++, muxer_options_.
bandwidth);
81 if (!ts_writer_->NewSegment(segment_name))
82 return Status(error::MUXER_FAILURE,
"Failed to initilize TsPacketWriter.");
83 current_segment_path_ = segment_name;
84 ts_writer_file_opened_ =
true;
88 Status TsSegmenter::WritePesPacketsToFile() {
89 while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
90 std::unique_ptr<PesPacket> pes_packet =
91 pes_packet_generator_->GetNextPesPacket();
93 Status status = OpenNewSegmentIfClosed(pes_packet->pts());
97 if (!ts_writer_->AddPesPacket(std::move(pes_packet)))
98 return Status(error::MUXER_FAILURE,
"Failed to add PES packet.");
105 if (!pes_packet_generator_->Flush()) {
106 return Status(error::MUXER_FAILURE,
107 "Failed to flush PesPacketGenerator.");
109 Status status = WritePesPacketsToFile();
115 if (ts_writer_file_opened_) {
116 if (!ts_writer_->FinalizeSegment()) {
117 return Status(error::MUXER_FAILURE,
"Failed to finalize TsWriter.");
120 const int64_t file_size =
123 start_timestamp * timescale_scale_,
124 duration * timescale_scale_, file_size);
126 ts_writer_file_opened_ =
false;
128 current_segment_path_.clear();
static int64_t GetFileSize(const char *file_name)