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 uint32_t max_hd_pixels,
38 uint32_t max_uhd1_pixels,
39 double clear_lead_in_seconds) {
41 return Status(error::MUXER_FAILURE,
"Segment template not specified.");
42 if (!ts_writer_->Initialize(stream_info))
43 return Status(error::MUXER_FAILURE,
"Failed to initialize TsWriter.");
44 if (!pes_packet_generator_->Initialize(stream_info)) {
45 return Status(error::MUXER_FAILURE,
46 "Failed to initialize PesPacketGenerator.");
49 if (encryption_key_source) {
50 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
51 const KeySource::TrackType type =
52 GetTrackTypeForEncryption(stream_info, max_sd_pixels,
53 max_hd_pixels, max_uhd1_pixels);
54 Status status = encryption_key_source->
GetKey(type, encryption_key.get());
56 if (encryption_key->iv.empty()) {
58 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
64 encryption_key_ = std::move(encryption_key);
65 clear_lead_in_seconds_ = clear_lead_in_seconds;
69 const bool kIsInitialEncryptionInfo =
true;
71 kIsInitialEncryptionInfo, FOURCC_cbcs, encryption_key_->key_id,
72 encryption_key_->iv, encryption_key_->key_system_info);
75 status = NotifyEncrypted();
80 timescale_scale_ = kTsTimescale / stream_info.time_scale();
92 const bool passed_segment_duration =
94 if (sample->is_key_frame() && passed_segment_duration) {
100 if (!ts_writer_file_opened_ && !sample->is_key_frame())
101 LOG(WARNING) <<
"A segment will start with a non key frame.";
103 if (!pes_packet_generator_->PushSample(sample)) {
104 return Status(error::MUXER_FAILURE,
105 "Failed to add sample to PesPacketGenerator.");
108 const double scaled_sample_duration = sample->duration() * timescale_scale_;
109 current_segment_total_sample_duration_ +=
110 scaled_sample_duration / kTsTimescale;
112 return WritePesPacketsToFile();
116 ts_writer_ = std::move(writer);
120 std::unique_ptr<PesPacketGenerator> generator) {
121 pes_packet_generator_ = std::move(generator);
125 ts_writer_file_opened_ = value;
128 Status TsSegmenter::OpenNewSegmentIfClosed(uint32_t next_pts) {
129 if (ts_writer_file_opened_)
131 const std::string segment_name =
133 segment_number_++, muxer_options_.
bandwidth);
134 if (!ts_writer_->NewSegment(segment_name))
135 return Status(error::MUXER_FAILURE,
"Failed to initilize TsPacketWriter.");
136 current_segment_start_time_ = next_pts;
137 current_segment_path_ = segment_name;
138 ts_writer_file_opened_ =
true;
142 Status TsSegmenter::WritePesPacketsToFile() {
143 while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
144 std::unique_ptr<PesPacket> pes_packet =
145 pes_packet_generator_->GetNextPesPacket();
147 Status status = OpenNewSegmentIfClosed(pes_packet->pts());
151 if (!ts_writer_->AddPesPacket(std::move(pes_packet)))
152 return Status(error::MUXER_FAILURE,
"Failed to add PES packet.");
157 Status TsSegmenter::Flush() {
158 if (!pes_packet_generator_->Flush()) {
159 return Status(error::MUXER_FAILURE,
160 "Failed to flush PesPacketGenerator.");
162 Status status = WritePesPacketsToFile();
168 if (ts_writer_file_opened_) {
169 if (!ts_writer_->FinalizeSegment()) {
170 return Status(error::MUXER_FAILURE,
"Failed to finalize TsWriter.");
173 const int64_t file_size =
176 current_segment_path_, current_segment_start_time_,
177 current_segment_total_sample_duration_ * kTsTimescale, file_size);
179 ts_writer_file_opened_ =
false;
180 total_duration_in_seconds_ += current_segment_total_sample_duration_;
182 current_segment_total_sample_duration_ = 0.0;
183 current_segment_start_time_ = 0;
184 current_segment_path_.clear();
185 return NotifyEncrypted();
188 Status TsSegmenter::NotifyEncrypted() {
189 if (encryption_key_ && total_duration_in_seconds_ >= clear_lead_in_seconds_) {
193 if (!pes_packet_generator_->SetEncryptionKey(std::move(encryption_key_)))
194 return Status(error::INTERNAL_ERROR,
"Failed to set encryption key.");
195 ts_writer_->SignalEncrypted();