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 !kInitialEncryptionInfo, protection_scheme(), encryption_key()->key_id,
84 encryption_key()->iv, encryption_key()->key_system_info);
94 if (!enable_encryption) {
99 if (need_to_refresh_encryptor) {
110 traf()->sample_group_descriptions.resize(
111 traf()->sample_group_descriptions.size() + 1);
113 traf()->sample_group_descriptions.back();
114 sample_group_description.grouping_type = FOURCC_seig;
116 sample_group_description.cenc_sample_encryption_info_entries.resize(1);
118 sample_group_description.cenc_sample_encryption_info_entries.back();
119 sample_group_entry.is_protected = 1;
120 if (protection_scheme() == FOURCC_cbcs) {
122 sample_group_entry.per_sample_iv_size = 0;
123 sample_group_entry.constant_iv = encryptor()->
iv();
125 sample_group_entry.per_sample_iv_size = encryptor()->
iv().size();
127 sample_group_entry.crypt_byte_block = crypt_byte_block();
128 sample_group_entry.skip_byte_block = skip_byte_block();
129 sample_group_entry.key_id = encryption_key()->key_id;