7 #include "packager/media/formats/mp2t/ts_segmenter.h"
11 #include "packager/media/base/aes_encryptor.h"
12 #include "packager/media/base/key_source.h"
13 #include "packager/media/base/muxer_util.h"
14 #include "packager/media/base/status.h"
15 #include "packager/media/base/video_stream_info.h"
16 #include "packager/media/event/muxer_listener.h"
17 #include "packager/media/event/progress_listener.h"
24 const double kTsTimescale = 90000;
28 : muxer_options_(options),
32 TsSegmenter::~TsSegmenter() {}
36 uint32_t max_sd_pixels,
37 double clear_lead_in_seconds) {
39 return Status(error::MUXER_FAILURE,
"Segment template not specified.");
40 if (!ts_writer_->Initialize(stream_info))
41 return Status(error::MUXER_FAILURE,
"Failed to initialize TsWriter.");
42 if (!pes_packet_generator_->Initialize(stream_info)) {
43 return Status(error::MUXER_FAILURE,
44 "Failed to initialize PesPacketGenerator.");
47 if (encryption_key_source) {
48 scoped_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
49 const KeySource::TrackType type =
50 GetTrackTypeForEncryption(stream_info, max_sd_pixels);
51 Status status = encryption_key_source->
GetKey(type, encryption_key.get());
53 if (encryption_key->iv.empty()) {
55 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
61 encryption_key_ = encryption_key.Pass();
62 clear_lead_in_seconds_ = clear_lead_in_seconds;
66 const bool kIsInitialEncryptionInfo =
true;
68 kIsInitialEncryptionInfo, FOURCC_cbcs, encryption_key_->key_id,
69 encryption_key_->iv, encryption_key_->key_system_info);
72 status = NotifyEncrypted();
77 timescale_scale_ = kTsTimescale / stream_info.time_scale();
89 const bool passed_segment_duration =
91 if (sample->is_key_frame() && passed_segment_duration) {
97 if (!ts_writer_file_opened_ && !sample->is_key_frame())
98 LOG(WARNING) <<
"A segment will start with a non key frame.";
100 if (!pes_packet_generator_->PushSample(sample)) {
101 return Status(error::MUXER_FAILURE,
102 "Failed to add sample to PesPacketGenerator.");
105 const double scaled_sample_duration = sample->duration() * timescale_scale_;
106 current_segment_total_sample_duration_ +=
107 scaled_sample_duration / kTsTimescale;
109 return WritePesPacketsToFile();
113 ts_writer_ = writer.Pass();
117 scoped_ptr<PesPacketGenerator> generator) {
118 pes_packet_generator_ = generator.Pass();
122 ts_writer_file_opened_ = value;
125 Status TsSegmenter::OpenNewSegmentIfClosed(uint32_t next_pts) {
126 if (ts_writer_file_opened_)
128 const std::string segment_name =
130 segment_number_++, muxer_options_.
bandwidth);
131 if (!ts_writer_->NewSegment(segment_name))
132 return Status(error::MUXER_FAILURE,
"Failed to initilize TsPacketWriter.");
133 current_segment_start_time_ = next_pts;
134 current_segment_path_ = segment_name;
135 ts_writer_file_opened_ =
true;
139 Status TsSegmenter::WritePesPacketsToFile() {
140 while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
141 scoped_ptr<PesPacket> pes_packet =
142 pes_packet_generator_->GetNextPesPacket();
144 Status status = OpenNewSegmentIfClosed(pes_packet->pts());
148 if (!ts_writer_->AddPesPacket(pes_packet.Pass()))
149 return Status(error::MUXER_FAILURE,
"Failed to add PES packet.");
154 Status TsSegmenter::Flush() {
155 if (!pes_packet_generator_->Flush()) {
156 return Status(error::MUXER_FAILURE,
157 "Failed to flush PesPacketGenerator.");
159 Status status = WritePesPacketsToFile();
165 if (ts_writer_file_opened_) {
166 if (!ts_writer_->FinalizeSegment()) {
167 return Status(error::MUXER_FAILURE,
"Failed to finalize TsWriter.");
170 const int64_t file_size =
173 current_segment_path_, current_segment_start_time_,
174 current_segment_total_sample_duration_ * kTsTimescale, file_size);
176 ts_writer_file_opened_ =
false;
177 total_duration_in_seconds_ += current_segment_total_sample_duration_;
179 current_segment_total_sample_duration_ = 0.0;
180 current_segment_start_time_ = 0;
181 current_segment_path_.clear();
182 return NotifyEncrypted();
185 Status TsSegmenter::NotifyEncrypted() {
186 if (encryption_key_ && total_duration_in_seconds_ >= clear_lead_in_seconds_) {
190 if (!pes_packet_generator_->SetEncryptionKey(encryption_key_.Pass()))
191 return Status(error::INTERNAL_ERROR,
"Failed to set encryption key.");
192 ts_writer_->SignalEncrypted();