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