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,
28 uint8_t crypt_byte_block,
29 uint8_t skip_byte_block,
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) {
45 DCHECK(encryption_key_source);
48 KeyRotationFragmenter::~KeyRotationFragmenter() {}
51 bool enable_encryption) {
52 bool need_to_refresh_encryptor = !encryptor();
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());
59 current_crypto_period_index, track_type_, encryption_key.get());
62 if (encryption_key->iv.empty()) {
64 &encryption_key->iv)) {
65 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
68 set_encryption_key(encryption_key.Pass());
69 prev_crypto_period_index_ = current_crypto_period_index;
70 need_to_refresh_encryptor =
true;
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();
81 if (muxer_listener_) {
83 encryption_key()->key_id,
85 encryption_key()->key_system_info);
95 if (!enable_encryption) {
100 if (need_to_refresh_encryptor) {
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) {
117 sample_group_entry.per_sample_iv_size = 0;
118 sample_group_entry.constant_iv = encryptor()->
iv();
120 sample_group_entry.per_sample_iv_size = encryptor()->
iv().size();
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;
127 traf()->sample_to_group.grouping_type = FOURCC_seig;
128 traf()->sample_to_group.entries.resize(1);
130 traf()->sample_to_group.entries[0].group_description_index =
131 SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
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;