7 #include "packager/media/formats/mp2t/ts_segmenter.h"
11 #include "packager/media/base/muxer_util.h"
12 #include "packager/media/base/status.h"
14 namespace edash_packager {
19 const int kTsTimescale = 90000;
23 : muxer_options_(options),
26 TsSegmenter::~TsSegmenter() {}
30 return Status(error::MUXER_FAILURE,
"Segment template not specified.");
31 if (!ts_writer_->Initialize(stream_info))
32 return Status(error::MUXER_FAILURE,
"Failed to initialize TsWriter.");
33 if (!pes_packet_generator_->Initialize(stream_info)) {
34 return Status(error::MUXER_FAILURE,
35 "Failed to initialize PesPacketGenerator.");
42 if (!pes_packet_generator_->Flush()) {
43 return Status(error::MUXER_FAILURE,
44 "Failed to finalize PesPacketGenerator.");
47 Status status = WritePesPacketsToFiles();
51 if (!ts_writer_file_opened_)
54 if (!ts_writer_->FinalizeSegment())
55 return Status(error::MUXER_FAILURE,
"Failed to finalize TsPacketWriter.");
56 ts_writer_file_opened_ =
false;
61 if (!pes_packet_generator_->PushSample(sample)) {
62 return Status(error::MUXER_FAILURE,
63 "Failed to add sample to PesPacketGenerator.");
66 return WritePesPacketsToFiles();
70 ts_writer_ = writer.Pass();
74 scoped_ptr<PesPacketGenerator> generator) {
75 pes_packet_generator_ = generator.Pass();
79 ts_writer_file_opened_ = value;
82 Status TsSegmenter::FinalizeSegmentIfPastSegmentDuration() {
84 if (!ts_writer_->FinalizeSegment())
85 return Status(error::FILE_FAILURE,
"Failed to finalize segment.");
86 ts_writer_file_opened_ =
false;
87 current_segment_duration_ = 0.0;
92 Status TsSegmenter::OpenNewSegmentIfClosed(uint32_t next_pts) {
93 if (ts_writer_file_opened_)
95 const std::string segment_name =
97 segment_number_++, muxer_options_.
bandwidth);
98 if (!ts_writer_->NewSegment(segment_name))
99 return Status(error::MUXER_FAILURE,
"Failed to initilize TsPacketWriter.");
100 ts_writer_file_opened_ =
true;
104 Status TsSegmenter::WritePesPacketsToFiles() {
105 while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
106 scoped_ptr<PesPacket> pes_packet =
107 pes_packet_generator_->GetNextPesPacket();
109 Status status = OpenNewSegmentIfClosed(pes_packet->pts());
113 const double pes_packet_duration = pes_packet->duration();
115 if (!ts_writer_->AddPesPacket(pes_packet.Pass()))
116 return Status(error::MUXER_FAILURE,
"Failed to add PES packet.");
118 current_segment_duration_ += pes_packet_duration / kTsTimescale;
120 status = FinalizeSegmentIfPastSegmentDuration();