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  MuxerListener* muxer_listener,
28  EncryptionMode encryption_mode)
29  : EncryptingFragmenter(info,
30  traf,
31  scoped_ptr<EncryptionKey>(new EncryptionKey()),
32  clear_time,
33  encryption_mode),
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  DCHECK(encryption_key());
64  const std::vector<ProtectionSystemSpecificInfo>& system_info =
65  encryption_key()->key_system_info;
66  moof_->pssh.resize(system_info.size());
67  for (size_t i = 0; i < system_info.size(); i++) {
68  moof_->pssh[i].raw_box = system_info[i].CreateBox();
69  }
70 
71  if (muxer_listener_) {
72  muxer_listener_->OnEncryptionInfoReady(!kInitialEncryptionInfo,
73  encryption_key()->key_id,
74  encryption_key()->key_system_info);
75  }
76 
77  // Skip the following steps if the current fragment is not going to be
78  // encrypted. 'pssh' box needs to be included in the fragment, which is
79  // performed above, regardless of whether the fragment is encrypted. This is
80  // necessary for two reasons: 1) Requesting keys before reaching encrypted
81  // content avoids playback delay due to license requests; 2) In Chrome, CDM
82  // must be initialized before starting the playback and CDM can only be
83  // initialized with a valid 'pssh'.
84  if (!enable_encryption) {
85  DCHECK(!encryptor());
86  return Status::OK;
87  }
88 
89  if (need_to_refresh_encryptor) {
90  Status status = CreateEncryptor();
91  if (!status.ok())
92  return status;
93  }
94  DCHECK(encryptor());
95 
96  // Key rotation happens in fragment boundary only in this implementation,
97  // i.e. there is at most one key for the fragment. So there should be only
98  // one entry in SampleGroupDescription box and one entry in SampleToGroup box.
99  // Fill in SampleGroupDescription box information.
100  traf()->sample_group_description.grouping_type = FOURCC_SEIG;
101  traf()->sample_group_description.entries.resize(1);
102  traf()->sample_group_description.entries[0].is_encrypted = true;
103  traf()->sample_group_description.entries[0].iv_size =
104  encryptor()->iv().size();
105  traf()->sample_group_description.entries[0].key_id = encryption_key()->key_id;
106 
107  // Fill in SampleToGroup box information.
108  traf()->sample_to_group.grouping_type = FOURCC_SEIG;
109  traf()->sample_to_group.entries.resize(1);
110  // sample_count is adjusted in |FinalizeFragment| later.
111  traf()->sample_to_group.entries[0].group_description_index =
112  SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
113 
114  return Status::OK;
115 }
116 
119  DCHECK_EQ(1u, traf()->sample_to_group.entries.size());
120  traf()->sample_to_group.entries[0].sample_count =
121  traf()->auxiliary_size.sample_count;
122 }
123 
124 } // namespace mp4
125 } // namespace media
126 } // 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, MuxerListener *muxer_listener, EncryptionMode encryption_mode)
void FinalizeFragmentForEncryption() override
Finalize current fragment for encryption.
EncryptingFragmenter generates MP4 fragments with sample encrypted.
Status PrepareFragmentForEncryption(bool enable_encryption) override
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:31
virtual void FinalizeFragmentForEncryption()
Finalize current fragment for encryption.
virtual Status GetCryptoPeriodKey(uint32_t crypto_period_index, TrackType track_type, EncryptionKey *key)=0