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()->iv,
85  encryption_key()->key_system_info);
86  }
87 
88  // Skip the following steps if the current fragment is not going to be
89  // encrypted. 'pssh' box needs to be included in the fragment, which is
90  // performed above, regardless of whether the fragment is encrypted. This is
91  // necessary for two reasons: 1) Requesting keys before reaching encrypted
92  // content avoids playback delay due to license requests; 2) In Chrome, CDM
93  // must be initialized before starting the playback and CDM can only be
94  // initialized with a valid 'pssh'.
95  if (!enable_encryption) {
96  DCHECK(!encryptor());
97  return Status::OK;
98  }
99 
100  if (need_to_refresh_encryptor) {
101  Status status = CreateEncryptor();
102  if (!status.ok())
103  return status;
104  }
105  DCHECK(encryptor());
106 
107  // Key rotation happens in fragment boundary only in this implementation,
108  // i.e. there is at most one key for the fragment. So there should be only
109  // one entry in SampleGroupDescription box and one entry in SampleToGroup box.
110  // Fill in SampleGroupDescription box information.
111  traf()->sample_group_description.grouping_type = FOURCC_seig;
112  traf()->sample_group_description.entries.resize(1);
113  auto& sample_group_entry = traf()->sample_group_description.entries[0];
114  sample_group_entry.is_protected = 1;
115  if (protection_scheme() == FOURCC_cbcs) {
116  // For 'cbcs' scheme, Constant IVs SHALL be used.
117  sample_group_entry.per_sample_iv_size = 0;
118  sample_group_entry.constant_iv = encryptor()->iv();
119  } else {
120  sample_group_entry.per_sample_iv_size = encryptor()->iv().size();
121  }
122  sample_group_entry.crypt_byte_block = crypt_byte_block();
123  sample_group_entry.skip_byte_block = skip_byte_block();
124  sample_group_entry.key_id = encryption_key()->key_id;
125 
126  // Fill in SampleToGroup box information.
127  traf()->sample_to_group.grouping_type = FOURCC_seig;
128  traf()->sample_to_group.entries.resize(1);
129  // sample_count is adjusted in |FinalizeFragment| later.
130  traf()->sample_to_group.entries[0].group_description_index =
131  SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
132 
133  return Status::OK;
134 }
135 
138  DCHECK_EQ(1u, traf()->sample_to_group.entries.size());
139  traf()->sample_to_group.entries[0].sample_count =
140  traf()->auxiliary_size.sample_count;
141 }
142 
143 } // namespace mp4
144 } // namespace media
145 } // 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.
virtual void OnEncryptionInfoReady(bool is_initial_encryption_info, const std::vector< uint8_t > &key_id, const std::vector< uint8_t > &iv, const std::vector< ProtectionSystemSpecificInfo > &key_system_info)=0
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