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 double 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.");
38 timescale_scale_ = kTsTimescale / stream_info.time_scale();
50 const bool passed_segment_duration =
52 if (sample->is_key_frame() && passed_segment_duration) {
58 if (!ts_writer_file_opened_ && !sample->is_key_frame())
59 LOG(WARNING) <<
"A segment will start with a non key frame.";
61 if (!pes_packet_generator_->PushSample(sample)) {
62 return Status(error::MUXER_FAILURE,
63 "Failed to add sample to PesPacketGenerator.");
66 const double scaled_sample_duration = sample->duration() * timescale_scale_;
67 current_segment_total_sample_duration_ +=
68 scaled_sample_duration / kTsTimescale;
70 return WritePesPacketsToFile();
74 ts_writer_ = writer.Pass();
78 scoped_ptr<PesPacketGenerator> generator) {
79 pes_packet_generator_ = generator.Pass();
83 ts_writer_file_opened_ = value;
86 Status TsSegmenter::OpenNewSegmentIfClosed(uint32_t next_pts) {
87 if (ts_writer_file_opened_)
89 const std::string segment_name =
91 segment_number_++, muxer_options_.
bandwidth);
92 if (!ts_writer_->NewSegment(segment_name))
93 return Status(error::MUXER_FAILURE,
"Failed to initilize TsPacketWriter.");
94 ts_writer_file_opened_ =
true;
98 Status TsSegmenter::WritePesPacketsToFile() {
99 while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
100 scoped_ptr<PesPacket> pes_packet =
101 pes_packet_generator_->GetNextPesPacket();
103 Status status = OpenNewSegmentIfClosed(pes_packet->pts());
107 if (!ts_writer_->AddPesPacket(pes_packet.Pass()))
108 return Status(error::MUXER_FAILURE,
"Failed to add PES packet.");
113 Status TsSegmenter::Flush() {
114 if (!pes_packet_generator_->Flush()) {
115 return Status(error::MUXER_FAILURE,
116 "Failed to flush PesPacketGenerator.");
118 Status status = WritePesPacketsToFile();
124 if (ts_writer_file_opened_) {
125 if (!ts_writer_->FinalizeSegment()) {
126 return Status(error::MUXER_FAILURE,
"Failed to finalize TsWriter.");
128 ts_writer_file_opened_ =
false;
130 current_segment_total_sample_duration_ = 0.0;