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.");
49 const bool passed_segment_duration =
51 if (sample->is_key_frame() && passed_segment_duration) {
57 if (!ts_writer_file_opened_ && !sample->is_key_frame())
58 LOG(WARNING) <<
"A segment will start with a non key frame.";
60 if (!pes_packet_generator_->PushSample(sample)) {
61 return Status(error::MUXER_FAILURE,
62 "Failed to add sample to PesPacketGenerator.");
65 current_segment_total_sample_duration_ += sample->duration() / kTsTimescale;
67 return WritePesPacketsToFile();
71 ts_writer_ = writer.Pass();
75 scoped_ptr<PesPacketGenerator> generator) {
76 pes_packet_generator_ = generator.Pass();
80 ts_writer_file_opened_ = value;
83 Status TsSegmenter::OpenNewSegmentIfClosed(uint32_t next_pts) {
84 if (ts_writer_file_opened_)
86 const std::string segment_name =
88 segment_number_++, muxer_options_.
bandwidth);
89 if (!ts_writer_->NewSegment(segment_name))
90 return Status(error::MUXER_FAILURE,
"Failed to initilize TsPacketWriter.");
91 ts_writer_file_opened_ =
true;
95 Status TsSegmenter::WritePesPacketsToFile() {
96 while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
97 scoped_ptr<PesPacket> pes_packet =
98 pes_packet_generator_->GetNextPesPacket();
100 Status status = OpenNewSegmentIfClosed(pes_packet->pts());
104 if (!ts_writer_->AddPesPacket(pes_packet.Pass()))
105 return Status(error::MUXER_FAILURE,
"Failed to add PES packet.");
110 Status TsSegmenter::Flush() {
111 if (!pes_packet_generator_->Flush()) {
112 return Status(error::MUXER_FAILURE,
113 "Failed to flush PesPacketGenerator.");
115 Status status = WritePesPacketsToFile();
121 if (ts_writer_file_opened_) {
122 if (!ts_writer_->FinalizeSegment()) {
123 return Status(error::MUXER_FAILURE,
"Failed to finalize TsWriter.");
125 ts_writer_file_opened_ =
false;
127 current_segment_total_sample_duration_ = 0.0;