7 #include "packager/media/formats/mp4/key_rotation_fragmenter.h"
9 #include "packager/media/base/aes_encryptor.h"
10 #include "packager/media/formats/mp4/box_definitions.h"
12 namespace edash_packager {
17 const bool kInitialEncryptionInfo =
true;
21 scoped_refptr<StreamInfo> info,
24 KeySource::TrackType track_type,
25 int64_t crypto_period_duration,
27 FourCC protection_scheme,
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) {
41 DCHECK(encryption_key_source);
44 KeyRotationFragmenter::~KeyRotationFragmenter() {}
47 bool enable_encryption) {
48 bool need_to_refresh_encryptor = !encryptor();
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());
55 current_crypto_period_index, track_type_, encryption_key.get());
58 if (encryption_key->iv.empty()) {
60 &encryption_key->iv)) {
61 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
64 set_encryption_key(encryption_key.Pass());
65 prev_crypto_period_index_ = current_crypto_period_index;
66 need_to_refresh_encryptor =
true;
69 DCHECK(encryption_key());
70 const std::vector<ProtectionSystemSpecificInfo>& system_info =
71 encryption_key()->key_system_info;
72 moof_->pssh.resize(system_info.size());
73 for (
size_t i = 0; i < system_info.size(); i++) {
74 moof_->pssh[i].raw_box = system_info[i].CreateBox();
77 if (muxer_listener_) {
78 muxer_listener_->OnEncryptionInfoReady(!kInitialEncryptionInfo,
79 encryption_key()->key_id,
80 encryption_key()->key_system_info);
90 if (!enable_encryption) {
95 if (need_to_refresh_encryptor) {
106 traf()->sample_group_description.grouping_type = FOURCC_seig;
107 traf()->sample_group_description.entries.resize(1);
108 traf()->sample_group_description.entries[0].is_protected = 1;
109 traf()->sample_group_description.entries[0].per_sample_iv_size =
110 encryptor()->
iv().size();
111 traf()->sample_group_description.entries[0].key_id = encryption_key()->key_id;
114 traf()->sample_to_group.grouping_type = FOURCC_seig;
115 traf()->sample_to_group.entries.resize(1);
117 traf()->sample_to_group.entries[0].group_description_index =
118 SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
125 DCHECK_EQ(1u, traf()->sample_to_group.entries.size());
126 traf()->sample_to_group.entries[0].sample_count =
127 traf()->auxiliary_size.sample_count;