DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
ts_segmenter.cc
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/formats/mp2t/ts_segmenter.h"
8 
9 #include <memory>
10 
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"
18 
19 namespace shaka {
20 namespace media {
21 namespace mp2t {
22 
23 namespace {
24 const double kTsTimescale = 90000;
25 } // namespace
26 
28  : muxer_options_(options),
29  listener_(listener),
30  ts_writer_(new TsWriter()),
31  pes_packet_generator_(new PesPacketGenerator()) {}
32 TsSegmenter::~TsSegmenter() {}
33 
35  KeySource* encryption_key_source,
36  uint32_t max_sd_pixels,
37  uint32_t max_hd_pixels,
38  uint32_t max_uhd1_pixels,
39  double clear_lead_in_seconds) {
40  if (muxer_options_.segment_template.empty())
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.");
47  }
48 
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());
55 
56  if (encryption_key->iv.empty()) {
57  if (!AesCryptor::GenerateRandomIv(FOURCC_cbcs, &encryption_key->iv)) {
58  return Status(error::INTERNAL_ERROR, "Failed to generate random iv.");
59  }
60  }
61  if (!status.ok())
62  return status;
63 
64  encryption_key_ = std::move(encryption_key);
65  clear_lead_in_seconds_ = clear_lead_in_seconds;
66 
67  if (listener_) {
68  // For now this only happens once, so send true.
69  const bool kIsInitialEncryptionInfo = true;
70  listener_->OnEncryptionInfoReady(
71  kIsInitialEncryptionInfo, FOURCC_cbcs, encryption_key_->key_id,
72  encryption_key_->iv, encryption_key_->key_system_info);
73  }
74 
75  status = NotifyEncrypted();
76  if (!status.ok())
77  return status;
78  }
79 
80  timescale_scale_ = kTsTimescale / stream_info.time_scale();
81  return Status::OK;
82 }
83 
85  return Flush();
86 }
87 
88 // First checks whether the sample is a key frame. If so and the segment has
89 // passed the segment duration, then flush the generator and write all the data
90 // to file.
91 Status TsSegmenter::AddSample(std::shared_ptr<MediaSample> sample) {
92  const bool passed_segment_duration =
93  current_segment_total_sample_duration_ > muxer_options_.segment_duration;
94  if (sample->is_key_frame() && passed_segment_duration) {
95  Status status = Flush();
96  if (!status.ok())
97  return status;
98  }
99 
100  if (!ts_writer_file_opened_ && !sample->is_key_frame())
101  LOG(WARNING) << "A segment will start with a non key frame.";
102 
103  if (!pes_packet_generator_->PushSample(sample)) {
104  return Status(error::MUXER_FAILURE,
105  "Failed to add sample to PesPacketGenerator.");
106  }
107 
108  const double scaled_sample_duration = sample->duration() * timescale_scale_;
109  current_segment_total_sample_duration_ +=
110  scaled_sample_duration / kTsTimescale;
111 
112  return WritePesPacketsToFile();
113 }
114 
115 void TsSegmenter::InjectTsWriterForTesting(std::unique_ptr<TsWriter> writer) {
116  ts_writer_ = std::move(writer);
117 }
118 
120  std::unique_ptr<PesPacketGenerator> generator) {
121  pes_packet_generator_ = std::move(generator);
122 }
123 
125  ts_writer_file_opened_ = value;
126 }
127 
128 Status TsSegmenter::OpenNewSegmentIfClosed(uint32_t next_pts) {
129  if (ts_writer_file_opened_)
130  return Status::OK;
131  const std::string segment_name =
132  GetSegmentName(muxer_options_.segment_template, next_pts,
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;
139  return Status::OK;
140 }
141 
142 Status TsSegmenter::WritePesPacketsToFile() {
143  while (pes_packet_generator_->NumberOfReadyPesPackets() > 0u) {
144  std::unique_ptr<PesPacket> pes_packet =
145  pes_packet_generator_->GetNextPesPacket();
146 
147  Status status = OpenNewSegmentIfClosed(pes_packet->pts());
148  if (!status.ok())
149  return status;
150 
151  if (!ts_writer_->AddPesPacket(std::move(pes_packet)))
152  return Status(error::MUXER_FAILURE, "Failed to add PES packet.");
153  }
154  return Status::OK;
155 }
156 
157 Status TsSegmenter::Flush() {
158  if (!pes_packet_generator_->Flush()) {
159  return Status(error::MUXER_FAILURE,
160  "Failed to flush PesPacketGenerator.");
161  }
162  Status status = WritePesPacketsToFile();
163  if (!status.ok())
164  return status;
165 
166  // This method may be called from Finalize() so ts_writer_file_opened_ could
167  // be false.
168  if (ts_writer_file_opened_) {
169  if (!ts_writer_->FinalizeSegment()) {
170  return Status(error::MUXER_FAILURE, "Failed to finalize TsWriter.");
171  }
172  if (listener_) {
173  const int64_t file_size =
174  File::GetFileSize(current_segment_path_.c_str());
175  listener_->OnNewSegment(
176  current_segment_path_, current_segment_start_time_,
177  current_segment_total_sample_duration_ * kTsTimescale, file_size);
178  }
179  ts_writer_file_opened_ = false;
180  total_duration_in_seconds_ += current_segment_total_sample_duration_;
181  }
182  current_segment_total_sample_duration_ = 0.0;
183  current_segment_start_time_ = 0;
184  current_segment_path_.clear();
185  return NotifyEncrypted();
186 }
187 
188 Status TsSegmenter::NotifyEncrypted() {
189  if (encryption_key_ && total_duration_in_seconds_ >= clear_lead_in_seconds_) {
190  if (listener_)
191  listener_->OnEncryptionStart();
192 
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();
196  }
197  return Status::OK;
198 }
199 
200 } // namespace mp2t
201 } // namespace media
202 } // namespace shaka
virtual void OnNewSegment(const std::string &segment_name, uint64_t start_time, uint64_t duration, uint64_t segment_file_size)=0
virtual void OnEncryptionInfoReady(bool is_initial_encryption_info, FourCC protection_scheme, const std::vector< uint8_t > &key_id, const std::vector< uint8_t > &iv, const std::vector< ProtectionSystemSpecificInfo > &key_system_info)=0
Abstract class holds stream information.
Definition: stream_info.h:57
virtual void OnEncryptionStart()=0
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:18
virtual Status GetKey(TrackType track_type, EncryptionKey *key)=0
Status AddSample(std::shared_ptr< MediaSample > sample)
Definition: ts_segmenter.cc:91
TsSegmenter(const MuxerOptions &options, MuxerListener *listener)
Definition: ts_segmenter.cc:27
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:107
void InjectPesPacketGeneratorForTesting(std::unique_ptr< PesPacketGenerator > generator)
Only for testing.
void SetTsWriterFileOpenedForTesting(bool value)
Only for testing.
void InjectTsWriterForTesting(std::unique_ptr< TsWriter > writer)
Only for testing.
Status Initialize(const StreamInfo &stream_info, KeySource *encryption_key_source, uint32_t max_sd_pixels, uint32_t max_hd_pixels, uint32_t max_uhd1_pixels, double clear_lead_in_seconds)
Definition: ts_segmenter.cc:34
static int64_t GetFileSize(const char *file_name)
Definition: file.cc:176
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30