DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
segmenter.cc
1 // Copyright 2014 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/mp4/segmenter.h"
8 
9 #include <algorithm>
10 
11 #include "packager/base/logging.h"
12 #include "packager/base/stl_util.h"
13 #include "packager/media/base/aes_cryptor.h"
14 #include "packager/media/base/buffer_writer.h"
15 #include "packager/media/base/key_source.h"
16 #include "packager/media/base/media_sample.h"
17 #include "packager/media/base/media_stream.h"
18 #include "packager/media/base/muxer_options.h"
19 #include "packager/media/base/video_stream_info.h"
20 #include "packager/media/event/muxer_listener.h"
21 #include "packager/media/event/progress_listener.h"
22 #include "packager/media/formats/mp4/box_definitions.h"
23 #include "packager/media/formats/mp4/key_rotation_fragmenter.h"
24 
25 namespace edash_packager {
26 namespace media {
27 namespace mp4 {
28 
29 namespace {
30 // For pattern-based encryption.
31 const uint8_t kCryptByteBlock = 1u;
32 const uint8_t kSkipByteBlock = 9u;
33 
34 const size_t kCencKeyIdSize = 16u;
35 
36 // The version of cenc implemented here. CENC 4.
37 const int kCencSchemeVersion = 0x00010000;
38 
39 // The default KID for key rotation is all 0s.
40 const uint8_t kKeyRotationDefaultKeyId[] = {
41  0, 0, 0, 0, 0, 0, 0, 0,
42  0, 0, 0, 0, 0, 0, 0, 0
43 };
44 
45 COMPILE_ASSERT(arraysize(kKeyRotationDefaultKeyId) == kCencKeyIdSize,
46  cenc_key_id_must_be_size_16);
47 
48 uint64_t Rescale(uint64_t time_in_old_scale,
49  uint32_t old_scale,
50  uint32_t new_scale) {
51  return static_cast<double>(time_in_old_scale) / old_scale * new_scale;
52 }
53 
54 uint8_t GetCryptByteBlock(FourCC protection_scheme) {
55  return (protection_scheme == FOURCC_cbcs || protection_scheme == FOURCC_cens)
56  ? kCryptByteBlock
57  : 0;
58 }
59 
60 uint8_t GetSkipByteBlock(FourCC protection_scheme) {
61  return (protection_scheme == FOURCC_cbcs || protection_scheme == FOURCC_cens)
62  ? kSkipByteBlock
63  : 0;
64 }
65 
66 void GenerateSinf(const EncryptionKey& encryption_key,
67  FourCC old_type,
68  FourCC protection_scheme,
69  ProtectionSchemeInfo* sinf) {
70  sinf->format.format = old_type;
71 
72  DCHECK_NE(protection_scheme, FOURCC_NULL);
73  sinf->type.type = protection_scheme;
74  sinf->type.version = kCencSchemeVersion;
75 
76  auto& track_encryption = sinf->info.track_encryption;
77  track_encryption.default_is_protected = 1;
78  DCHECK(!encryption_key.iv.empty());
79  if (protection_scheme == FOURCC_cbcs) {
80  // ISO/IEC 23001-7:2016 10.4.1
81  // For 'cbcs' scheme, Constant IVs SHALL be used.
82  track_encryption.default_per_sample_iv_size = 0;
83  track_encryption.default_constant_iv = encryption_key.iv;
84  } else {
85  track_encryption.default_per_sample_iv_size = encryption_key.iv.size();
86  }
87  track_encryption.default_crypt_byte_block =
88  GetCryptByteBlock(protection_scheme);
89  track_encryption.default_skip_byte_block =
90  GetSkipByteBlock(protection_scheme);
91  track_encryption.default_kid = encryption_key.key_id;
92 }
93 
94 void GenerateEncryptedSampleEntry(const EncryptionKey& encryption_key,
95  double clear_lead_in_seconds,
96  FourCC protection_scheme,
97  SampleDescription* description) {
98  DCHECK(description);
99  if (description->type == kVideo) {
100  DCHECK_EQ(1u, description->video_entries.size());
101 
102  // Add a second entry for clear content if needed.
103  if (clear_lead_in_seconds > 0)
104  description->video_entries.push_back(description->video_entries[0]);
105 
106  // Convert the first entry to an encrypted entry.
107  VideoSampleEntry& entry = description->video_entries[0];
108  GenerateSinf(encryption_key, entry.format, protection_scheme, &entry.sinf);
109  entry.format = FOURCC_encv;
110  } else {
111  DCHECK_EQ(kAudio, description->type);
112  DCHECK_EQ(1u, description->audio_entries.size());
113 
114  // Add a second entry for clear content if needed.
115  if (clear_lead_in_seconds > 0)
116  description->audio_entries.push_back(description->audio_entries[0]);
117 
118  // Convert the first entry to an encrypted entry.
119  AudioSampleEntry& entry = description->audio_entries[0];
120  GenerateSinf(encryption_key, entry.format, protection_scheme, &entry.sinf);
121  entry.format = FOURCC_enca;
122  }
123 }
124 
125 KeySource::TrackType GetTrackTypeForEncryption(const StreamInfo& stream_info,
126  uint32_t max_sd_pixels) {
127  if (stream_info.stream_type() == kStreamAudio)
128  return KeySource::TRACK_TYPE_AUDIO;
129 
130  DCHECK_EQ(kStreamVideo, stream_info.stream_type());
131  const VideoStreamInfo& video_stream_info =
132  static_cast<const VideoStreamInfo&>(stream_info);
133  uint32_t pixels = video_stream_info.width() * video_stream_info.height();
134  return (pixels > max_sd_pixels) ? KeySource::TRACK_TYPE_HD
135  : KeySource::TRACK_TYPE_SD;
136 }
137 
138 } // namespace
139 
140 Segmenter::Segmenter(const MuxerOptions& options,
141  scoped_ptr<FileType> ftyp,
142  scoped_ptr<Movie> moov)
143  : options_(options),
144  ftyp_(ftyp.Pass()),
145  moov_(moov.Pass()),
146  moof_(new MovieFragment()),
147  fragment_buffer_(new BufferWriter()),
148  sidx_(new SegmentIndex()),
149  muxer_listener_(NULL),
150  progress_listener_(NULL),
151  progress_target_(0),
152  accumulated_progress_(0),
153  sample_duration_(0u) {}
154 
155 Segmenter::~Segmenter() { STLDeleteElements(&fragmenters_); }
156 
157 Status Segmenter::Initialize(const std::vector<MediaStream*>& streams,
158  MuxerListener* muxer_listener,
159  ProgressListener* progress_listener,
160  KeySource* encryption_key_source,
161  uint32_t max_sd_pixels,
162  double clear_lead_in_seconds,
163  double crypto_period_duration_in_seconds,
164  FourCC protection_scheme) {
165  DCHECK_LT(0u, streams.size());
166  muxer_listener_ = muxer_listener;
167  progress_listener_ = progress_listener;
168  moof_->header.sequence_number = 0;
169 
170  moof_->tracks.resize(streams.size());
171  segment_durations_.resize(streams.size());
172  fragmenters_.resize(streams.size());
173  const bool key_rotation_enabled = crypto_period_duration_in_seconds != 0;
174  const bool kInitialEncryptionInfo = true;
175 
176  for (uint32_t i = 0; i < streams.size(); ++i) {
177  stream_map_[streams[i]] = i;
178  moof_->tracks[i].header.track_id = i + 1;
179  if (streams[i]->info()->stream_type() == kStreamVideo) {
180  // Use the first video stream as the reference stream (which is 1-based).
181  if (sidx_->reference_id == 0)
182  sidx_->reference_id = i + 1;
183  }
184  if (!encryption_key_source) {
185  fragmenters_[i] = new Fragmenter(&moof_->tracks[i]);
186  continue;
187  }
188 
189  FourCC local_protection_scheme = protection_scheme;
190  if (streams[i]->info()->stream_type() != kStreamVideo) {
191  // Pattern encryption should only be used with video. Replaces with the
192  // corresponding non-pattern encryption scheme.
193  if (protection_scheme == FOURCC_cbcs)
194  local_protection_scheme = FOURCC_cbc1;
195  else if (protection_scheme == FOURCC_cens)
196  local_protection_scheme = FOURCC_cenc;
197  }
198 
199  KeySource::TrackType track_type =
200  GetTrackTypeForEncryption(*streams[i]->info(), max_sd_pixels);
201  SampleDescription& description =
202  moov_->tracks[i].media.information.sample_table.description;
203 
204  if (key_rotation_enabled) {
205  // Fill encrypted sample entry with default key.
206  EncryptionKey encryption_key;
207  encryption_key.key_id.assign(
208  kKeyRotationDefaultKeyId,
209  kKeyRotationDefaultKeyId + arraysize(kKeyRotationDefaultKeyId));
210  if (!AesCryptor::GenerateRandomIv(local_protection_scheme,
211  &encryption_key.iv)) {
212  return Status(error::INTERNAL_ERROR, "Failed to generate random iv.");
213  }
214  GenerateEncryptedSampleEntry(encryption_key, clear_lead_in_seconds,
215  local_protection_scheme, &description);
216  if (muxer_listener_) {
217  muxer_listener_->OnEncryptionInfoReady(
218  kInitialEncryptionInfo, encryption_key.key_id, encryption_key.iv,
219  encryption_key.key_system_info);
220  }
221 
222  fragmenters_[i] = new KeyRotationFragmenter(
223  moof_.get(), streams[i]->info(), &moof_->tracks[i],
224  encryption_key_source, track_type,
225  crypto_period_duration_in_seconds * streams[i]->info()->time_scale(),
226  clear_lead_in_seconds * streams[i]->info()->time_scale(),
227  local_protection_scheme, GetCryptByteBlock(local_protection_scheme),
228  GetSkipByteBlock(local_protection_scheme), muxer_listener_);
229  continue;
230  }
231 
232  scoped_ptr<EncryptionKey> encryption_key(new EncryptionKey());
233  Status status =
234  encryption_key_source->GetKey(track_type, encryption_key.get());
235  if (!status.ok())
236  return status;
237  if (encryption_key->iv.empty()) {
238  if (!AesCryptor::GenerateRandomIv(local_protection_scheme,
239  &encryption_key->iv)) {
240  return Status(error::INTERNAL_ERROR, "Failed to generate random iv.");
241  }
242  }
243 
244  GenerateEncryptedSampleEntry(*encryption_key, clear_lead_in_seconds,
245  local_protection_scheme, &description);
246 
247  if (moov_->pssh.empty()) {
248  moov_->pssh.resize(encryption_key->key_system_info.size());
249  for (size_t i = 0; i < encryption_key->key_system_info.size(); i++) {
250  moov_->pssh[i].raw_box = encryption_key->key_system_info[i].CreateBox();
251  }
252 
253  if (muxer_listener_) {
254  muxer_listener_->OnEncryptionInfoReady(kInitialEncryptionInfo,
255  encryption_key->key_id,
256  encryption_key->iv,
257  encryption_key->key_system_info);
258  }
259  }
260 
261  fragmenters_[i] = new EncryptingFragmenter(
262  streams[i]->info(), &moof_->tracks[i], encryption_key.Pass(),
263  clear_lead_in_seconds * streams[i]->info()->time_scale(),
264  local_protection_scheme, GetCryptByteBlock(local_protection_scheme),
265  GetSkipByteBlock(local_protection_scheme));
266  }
267 
268  // Choose the first stream if there is no VIDEO.
269  if (sidx_->reference_id == 0)
270  sidx_->reference_id = 1;
271  sidx_->timescale = streams[GetReferenceStreamId()]->info()->time_scale();
272 
273  // Use media duration as progress target.
274  progress_target_ = streams[GetReferenceStreamId()]->info()->duration();
275 
276  // Use the reference stream's time scale as movie time scale.
277  moov_->header.timescale = sidx_->timescale;
278  moof_->header.sequence_number = 1;
279 
280  // Fill in version information.
281  moov_->metadata.handler.handler_type = FOURCC_ID32;
282  moov_->metadata.id3v2.language.code = "eng";
283  moov_->metadata.id3v2.private_frame.owner =
284  "https://github.com/google/edash-packager";
285  moov_->metadata.id3v2.private_frame.value = options_.packager_version_string;
286  return DoInitialize();
287 }
288 
290  for (std::vector<Fragmenter*>::iterator it = fragmenters_.begin();
291  it != fragmenters_.end();
292  ++it) {
293  Status status = FinalizeFragment(true, *it);
294  if (!status.ok())
295  return status;
296  }
297 
298  // Set tracks and moov durations.
299  // Note that the updated moov box will be written to output file for VOD case
300  // only.
301  for (std::vector<Track>::iterator track = moov_->tracks.begin();
302  track != moov_->tracks.end();
303  ++track) {
304  track->header.duration = Rescale(track->media.header.duration,
305  track->media.header.timescale,
306  moov_->header.timescale);
307  if (track->header.duration > moov_->header.duration)
308  moov_->header.duration = track->header.duration;
309  }
310  moov_->extends.header.fragment_duration = moov_->header.duration;
311 
312  return DoFinalize();
313 }
314 
316  scoped_refptr<MediaSample> sample) {
317  // Find the fragmenter for this stream.
318  DCHECK(stream);
319  DCHECK(stream_map_.find(stream) != stream_map_.end());
320  uint32_t stream_id = stream_map_[stream];
321  Fragmenter* fragmenter = fragmenters_[stream_id];
322 
323  // Set default sample duration if it has not been set yet.
324  if (moov_->extends.tracks[stream_id].default_sample_duration == 0) {
325  moov_->extends.tracks[stream_id].default_sample_duration =
326  sample->duration();
327  }
328 
329  if (fragmenter->fragment_finalized()) {
330  return Status(error::FRAGMENT_FINALIZED,
331  "Current fragment is finalized already.");
332  }
333 
334  bool finalize_fragment = false;
335  if (fragmenter->fragment_duration() >=
336  options_.fragment_duration * stream->info()->time_scale()) {
337  if (sample->is_key_frame() || !options_.fragment_sap_aligned) {
338  finalize_fragment = true;
339  }
340  }
341  bool finalize_segment = false;
342  if (segment_durations_[stream_id] >=
343  options_.segment_duration * stream->info()->time_scale()) {
344  if (sample->is_key_frame() || !options_.segment_sap_aligned) {
345  finalize_segment = true;
346  finalize_fragment = true;
347  }
348  }
349 
350  Status status;
351  if (finalize_fragment) {
352  status = FinalizeFragment(finalize_segment, fragmenter);
353  if (!status.ok())
354  return status;
355  }
356 
357  status = fragmenter->AddSample(sample);
358  if (!status.ok())
359  return status;
360 
361  if (sample_duration_ == 0)
362  sample_duration_ = sample->duration();
363  moov_->tracks[stream_id].media.header.duration += sample->duration();
364  segment_durations_[stream_id] += sample->duration();
365  DCHECK_GE(segment_durations_[stream_id], fragmenter->fragment_duration());
366  return Status::OK;
367 }
368 
369 uint32_t Segmenter::GetReferenceTimeScale() const {
370  return moov_->header.timescale;
371 }
372 
373 double Segmenter::GetDuration() const {
374  if (moov_->header.timescale == 0) {
375  // Handling the case where this is not properly initialized.
376  return 0.0;
377  }
378 
379  return static_cast<double>(moov_->header.duration) / moov_->header.timescale;
380 }
381 
382 void Segmenter::UpdateProgress(uint64_t progress) {
383  accumulated_progress_ += progress;
384 
385  if (!progress_listener_) return;
386  if (progress_target_ == 0) return;
387  // It might happen that accumulated progress exceeds progress_target due to
388  // computation errors, e.g. rounding error. Cap it so it never reports > 100%
389  // progress.
390  if (accumulated_progress_ >= progress_target_) {
391  progress_listener_->OnProgress(1.0);
392  } else {
393  progress_listener_->OnProgress(static_cast<double>(accumulated_progress_) /
394  progress_target_);
395  }
396 }
397 
398 void Segmenter::SetComplete() {
399  if (!progress_listener_) return;
400  progress_listener_->OnProgress(1.0);
401 }
402 
403 Status Segmenter::FinalizeSegment() {
404  Status status = DoFinalizeSegment();
405 
406  // Reset segment information to initial state.
407  sidx_->references.clear();
408  std::vector<uint64_t>::iterator it = segment_durations_.begin();
409  for (; it != segment_durations_.end(); ++it)
410  *it = 0;
411 
412  return status;
413 }
414 
415 uint32_t Segmenter::GetReferenceStreamId() {
416  DCHECK(sidx_);
417  return sidx_->reference_id - 1;
418 }
419 
420 Status Segmenter::FinalizeFragment(bool finalize_segment,
421  Fragmenter* fragmenter) {
422  fragmenter->FinalizeFragment();
423 
424  // Check if all tracks are ready for fragmentation.
425  for (std::vector<Fragmenter*>::iterator it = fragmenters_.begin();
426  it != fragmenters_.end();
427  ++it) {
428  if (!(*it)->fragment_finalized())
429  return Status::OK;
430  }
431 
432  MediaData mdat;
433  // Data offset relative to 'moof': moof size + mdat header size.
434  // The code will also update box sizes for moof_ and its child boxes.
435  uint64_t data_offset = moof_->ComputeSize() + mdat.HeaderSize();
436  // 'traf' should follow 'mfhd' moof header box.
437  uint64_t next_traf_position = moof_->HeaderSize() + moof_->header.box_size();
438  for (size_t i = 0; i < moof_->tracks.size(); ++i) {
439  TrackFragment& traf = moof_->tracks[i];
440  if (traf.auxiliary_offset.offsets.size() > 0) {
441  DCHECK_EQ(traf.auxiliary_offset.offsets.size(), 1u);
442  DCHECK(!traf.sample_encryption.sample_encryption_entries.empty());
443 
444  next_traf_position += traf.box_size();
445  // SampleEncryption 'senc' box should be the last box in 'traf'.
446  // |auxiliary_offset| should point to the data of SampleEncryption.
447  traf.auxiliary_offset.offsets[0] =
448  next_traf_position - traf.sample_encryption.box_size() +
449  traf.sample_encryption.HeaderSize() +
450  sizeof(uint32_t); // for sample count field in 'senc'
451  }
452  traf.runs[0].data_offset = data_offset + mdat.data_size;
453  mdat.data_size += fragmenters_[i]->data()->Size();
454  }
455 
456  // Generate segment reference.
457  sidx_->references.resize(sidx_->references.size() + 1);
458  fragmenters_[GetReferenceStreamId()]->GenerateSegmentReference(
459  &sidx_->references[sidx_->references.size() - 1]);
460  sidx_->references[sidx_->references.size() - 1].referenced_size =
461  data_offset + mdat.data_size;
462 
463  // Write the fragment to buffer.
464  moof_->Write(fragment_buffer_.get());
465  mdat.WriteHeader(fragment_buffer_.get());
466  for (Fragmenter* fragmenter : fragmenters_)
467  fragment_buffer_->AppendBuffer(*fragmenter->data());
468 
469  // Increase sequence_number for next fragment.
470  ++moof_->header.sequence_number;
471 
472  if (finalize_segment)
473  return FinalizeSegment();
474 
475  return Status::OK;
476 }
477 
478 } // namespace mp4
479 } // namespace media
480 } // namespace edash_packager
virtual Status GetKey(TrackType track_type, EncryptionKey *key)=0
Status Initialize(scoped_ptr< MkvWriter > writer, StreamInfo *info, ProgressListener *progress_listener, MuxerListener *muxer_listener, KeySource *encryption_key_source, uint32_t max_sd_pixels, double clear_lead_in_seconds)
Definition: segmenter.cc:46
std::string packager_version_string
Specify the version string to be embedded in the output files.
Definition: muxer_options.h:82
This class listens to progress updates events.
EncryptingFragmenter generates MP4 fragments with sample encrypted.
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:109
virtual Status AddSample(scoped_refptr< MediaSample > sample)
Definition: fragmenter.cc:36
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:31
virtual void OnProgress(double progress)=0
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:247
Status AddSample(scoped_refptr< MediaSample > sample)
Definition: segmenter.cc:112