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 = true;
18 } // namespace
19 
21  scoped_refptr<StreamInfo> info,
22  TrackFragment* traf,
23  KeySource* encryption_key_source,
24  KeySource::TrackType track_type,
25  int64_t crypto_period_duration,
26  int64_t clear_time,
27  FourCC protection_scheme,
28  uint8_t crypt_byte_block,
29  uint8_t skip_byte_block,
30  MuxerListener* muxer_listener)
31  : EncryptingFragmenter(info,
32  traf,
33  scoped_ptr<EncryptionKey>(new EncryptionKey()),
34  clear_time,
35  protection_scheme,
36  crypt_byte_block,
37  skip_byte_block),
38  moof_(moof),
39  encryption_key_source_(encryption_key_source),
40  track_type_(track_type),
41  crypto_period_duration_(crypto_period_duration),
42  prev_crypto_period_index_(-1),
43  muxer_listener_(muxer_listener) {
44  DCHECK(moof);
45  DCHECK(encryption_key_source);
46 }
47 
48 KeyRotationFragmenter::~KeyRotationFragmenter() {}
49 
51  bool enable_encryption) {
52  bool need_to_refresh_encryptor = !encryptor();
53 
54  size_t current_crypto_period_index =
55  traf()->decode_time.decode_time / crypto_period_duration_;
56  if (current_crypto_period_index != prev_crypto_period_index_) {
57  scoped_ptr<EncryptionKey> encryption_key(new EncryptionKey());
58  Status status = encryption_key_source_->GetCryptoPeriodKey(
59  current_crypto_period_index, track_type_, encryption_key.get());
60  if (!status.ok())
61  return status;
62  if (encryption_key->iv.empty()) {
63  if (!AesCryptor::GenerateRandomIv(protection_scheme(),
64  &encryption_key->iv)) {
65  return Status(error::INTERNAL_ERROR, "Failed to generate random iv.");
66  }
67  }
68  set_encryption_key(encryption_key.Pass());
69  prev_crypto_period_index_ = current_crypto_period_index;
70  need_to_refresh_encryptor = true;
71  }
72 
73  DCHECK(encryption_key());
74  const std::vector<ProtectionSystemSpecificInfo>& system_info =
75  encryption_key()->key_system_info;
76  moof_->pssh.resize(system_info.size());
77  for (size_t i = 0; i < system_info.size(); i++) {
78  moof_->pssh[i].raw_box = system_info[i].CreateBox();
79  }
80 
81  if (muxer_listener_) {
82  muxer_listener_->OnEncryptionInfoReady(!kInitialEncryptionInfo,
83  encryption_key()->key_id,
84  encryption_key()->key_system_info);
85  }
86 
87  // Skip the following steps if the current fragment is not going to be
88  // encrypted. 'pssh' box needs to be included in the fragment, which is
89  // performed above, regardless of whether the fragment is encrypted. This is
90  // necessary for two reasons: 1) Requesting keys before reaching encrypted
91  // content avoids playback delay due to license requests; 2) In Chrome, CDM
92  // must be initialized before starting the playback and CDM can only be
93  // initialized with a valid 'pssh'.
94  if (!enable_encryption) {
95  DCHECK(!encryptor());
96  return Status::OK;
97  }
98 
99  if (need_to_refresh_encryptor) {
100  Status status = CreateEncryptor();
101  if (!status.ok())
102  return status;
103  }
104  DCHECK(encryptor());
105 
106  // Key rotation happens in fragment boundary only in this implementation,
107  // i.e. there is at most one key for the fragment. So there should be only
108  // one entry in SampleGroupDescription box and one entry in SampleToGroup box.
109  // Fill in SampleGroupDescription box information.
110  traf()->sample_group_description.grouping_type = FOURCC_seig;
111  traf()->sample_group_description.entries.resize(1);
112  auto& sample_group_entry = traf()->sample_group_description.entries[0];
113  sample_group_entry.is_protected = 1;
114  if (protection_scheme() == FOURCC_cbcs) {
115  // For 'cbcs' scheme, Constant IVs SHALL be used.
116  sample_group_entry.per_sample_iv_size = 0;
117  sample_group_entry.constant_iv = encryptor()->iv();
118  } else {
119  sample_group_entry.per_sample_iv_size = encryptor()->iv().size();
120  }
121  sample_group_entry.crypt_byte_block = crypt_byte_block();
122  sample_group_entry.skip_byte_block = skip_byte_block();
123  sample_group_entry.key_id = encryption_key()->key_id;
124 
125  // Fill in SampleToGroup box information.
126  traf()->sample_to_group.grouping_type = FOURCC_seig;
127  traf()->sample_to_group.entries.resize(1);
128  // sample_count is adjusted in |FinalizeFragment| later.
129  traf()->sample_to_group.entries[0].group_description_index =
130  SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
131 
132  return Status::OK;
133 }
134 
137  DCHECK_EQ(1u, traf()->sample_to_group.entries.size());
138  traf()->sample_to_group.entries[0].sample_count =
139  traf()->auxiliary_size.sample_count;
140 }
141 
142 } // namespace mp4
143 } // namespace media
144 } // namespace edash_packager
KeyRotationFragmenter(MovieFragment *moof, scoped_refptr< StreamInfo > info, TrackFragment *traf, KeySource *encryption_key_source, KeySource::TrackType track_type, int64_t crypto_period_duration, int64_t clear_time, FourCC protection_scheme, uint8_t crypt_byte_block, uint8_t skip_byte_block, MuxerListener *muxer_listener)
void FinalizeFragmentForEncryption() override
Finalize current fragment for encryption.
EncryptingFragmenter generates MP4 fragments with sample encrypted.
static bool GenerateRandomIv(FourCC protection_scheme, std::vector< uint8_t > *iv)
Definition: aes_cryptor.cc:58
Status PrepareFragmentForEncryption(bool enable_encryption) override
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:31
const std::vector< uint8_t > & iv() const
Definition: aes_cryptor.h:60
virtual void FinalizeFragmentForEncryption()
Finalize current fragment for encryption.
virtual Status GetCryptoPeriodKey(uint32_t crypto_period_index, TrackType track_type, EncryptionKey *key)=0