DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
key_rotation_fragmenter.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/key_rotation_fragmenter.h"
8 
9 #include "packager/media/base/aes_encryptor.h"
10 #include "packager/media/formats/mp4/box_definitions.h"
11 
12 namespace edash_packager {
13 namespace media {
14 namespace mp4 {
15 
16 namespace {
17 const bool kInitialEncryptionInfo = false;
18 } // namespace
19 
21  TrackFragment* traf,
22  KeySource* encryption_key_source,
23  KeySource::TrackType track_type,
24  int64_t crypto_period_duration,
25  int64_t clear_time,
26  VideoCodec video_codec,
27  uint8_t nalu_length_size,
28  MuxerListener* muxer_listener)
29  : EncryptingFragmenter(traf,
30  scoped_ptr<EncryptionKey>(new EncryptionKey()),
31  clear_time,
32  video_codec,
33  nalu_length_size),
34  moof_(moof),
35  encryption_key_source_(encryption_key_source),
36  track_type_(track_type),
37  crypto_period_duration_(crypto_period_duration),
38  prev_crypto_period_index_(-1),
39  muxer_listener_(muxer_listener) {
40  DCHECK(moof);
41  DCHECK(encryption_key_source);
42 }
43 
44 KeyRotationFragmenter::~KeyRotationFragmenter() {}
45 
47  bool enable_encryption) {
48  bool need_to_refresh_encryptor = !encryptor();
49 
50  size_t current_crypto_period_index =
51  traf()->decode_time.decode_time / crypto_period_duration_;
52  if (current_crypto_period_index != prev_crypto_period_index_) {
53  scoped_ptr<EncryptionKey> encryption_key(new EncryptionKey());
54  Status status = encryption_key_source_->GetCryptoPeriodKey(
55  current_crypto_period_index, track_type_, encryption_key.get());
56  if (!status.ok())
57  return status;
58  set_encryption_key(encryption_key.Pass());
59  prev_crypto_period_index_ = current_crypto_period_index;
60  need_to_refresh_encryptor = true;
61  }
62 
63  // One and only one 'pssh' box is needed.
64  if (moof_->pssh.empty())
65  moof_->pssh.resize(1);
66  DCHECK(encryption_key());
67  moof_->pssh[0].raw_box = encryption_key()->pssh;
68 
69  if (muxer_listener_) {
70  muxer_listener_->OnEncryptionInfoReady(
71  !kInitialEncryptionInfo,
72  encryption_key_source_->UUID(), encryption_key_source_->SystemName(),
73  encryption_key()->key_id, encryption_key()->pssh);
74  }
75 
76  // Skip the following steps if the current fragment is not going to be
77  // encrypted. 'pssh' box needs to be included in the fragment, which is
78  // performed above, regardless of whether the fragment is encrypted. This is
79  // necessary for two reasons: 1) Requesting keys before reaching encrypted
80  // content avoids playback delay due to license requests; 2) In Chrome, CDM
81  // must be initialized before starting the playback and CDM can only be
82  // initialized with a valid 'pssh'.
83  if (!enable_encryption) {
84  DCHECK(!encryptor());
85  return Status::OK;
86  }
87 
88  if (need_to_refresh_encryptor) {
89  Status status = CreateEncryptor();
90  if (!status.ok())
91  return status;
92  }
93  DCHECK(encryptor());
94 
95  // Key rotation happens in fragment boundary only in this implementation,
96  // i.e. there is at most one key for the fragment. So there should be only
97  // one entry in SampleGroupDescription box and one entry in SampleToGroup box.
98  // Fill in SampleGroupDescription box information.
99  traf()->sample_group_description.grouping_type = FOURCC_SEIG;
100  traf()->sample_group_description.entries.resize(1);
101  traf()->sample_group_description.entries[0].is_encrypted = true;
102  traf()->sample_group_description.entries[0].iv_size =
103  encryptor()->iv().size();
104  traf()->sample_group_description.entries[0].key_id = encryption_key()->key_id;
105 
106  // Fill in SampleToGroup box information.
107  traf()->sample_to_group.grouping_type = FOURCC_SEIG;
108  traf()->sample_to_group.entries.resize(1);
109  // sample_count is adjusted in |FinalizeFragment| later.
110  traf()->sample_to_group.entries[0].group_description_index =
111  SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
112 
113  return Status::OK;
114 }
115 
118  DCHECK_EQ(1u, traf()->sample_to_group.entries.size());
119  traf()->sample_to_group.entries[0].sample_count =
120  traf()->auxiliary_size.sample_count;
121 }
122 
123 } // namespace mp4
124 } // namespace media
125 } // namespace edash_packager
virtual Status GetCryptoPeriodKey(uint32_t crypto_period_index, TrackType track_type, EncryptionKey *key)
Definition: key_source.cc:66
void FinalizeFragmentForEncryption() override
Finalize current fragment for encryption.
EncryptingFragmenter generates MP4 fragments with sample encrypted.
Status PrepareFragmentForEncryption(bool enable_encryption) override
virtual std::string UUID()
Definition: key_source.cc:92
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:29
KeyRotationFragmenter(MovieFragment *moof, TrackFragment *traf, KeySource *encryption_key_source, KeySource::TrackType track_type, int64_t crypto_period_duration, int64_t clear_time, VideoCodec video_codec, uint8_t nalu_length_size, MuxerListener *muxer_listener)
virtual std::string SystemName()
Definition: key_source.cc:96
virtual void FinalizeFragmentForEncryption()
Finalize current fragment for encryption.