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"
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,
40 encryption_key_source_(encryption_key_source),
41 track_type_(track_type),
42 crypto_period_duration_(crypto_period_duration),
43 prev_crypto_period_index_(-1),
44 muxer_listener_(muxer_listener) {
46 DCHECK(encryption_key_source);
49 KeyRotationFragmenter::~KeyRotationFragmenter() {}
52 bool enable_encryption) {
53 bool need_to_refresh_encryptor = !encryptor();
55 int64_t current_crypto_period_index =
56 traf()->decode_time.decode_time / crypto_period_duration_;
57 if (current_crypto_period_index != prev_crypto_period_index_) {
58 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
60 current_crypto_period_index, track_type_, encryption_key.get());
63 if (encryption_key->iv.empty()) {
65 &encryption_key->iv)) {
66 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
69 set_encryption_key(std::move(encryption_key));
70 prev_crypto_period_index_ = current_crypto_period_index;
71 need_to_refresh_encryptor =
true;
74 DCHECK(encryption_key());
75 const std::vector<ProtectionSystemSpecificInfo>& system_info =
76 encryption_key()->key_system_info;
77 moof_->pssh.resize(system_info.size());
78 for (
size_t i = 0; i < system_info.size(); i++) {
79 moof_->pssh[i].raw_box = system_info[i].CreateBox();
82 if (muxer_listener_) {
84 !kInitialEncryptionInfo, protection_scheme(), encryption_key()->key_id,
85 encryption_key()->iv, encryption_key()->key_system_info);
95 if (!enable_encryption) {
100 if (need_to_refresh_encryptor) {
111 traf()->sample_group_descriptions.resize(
112 traf()->sample_group_descriptions.size() + 1);
114 traf()->sample_group_descriptions.back();
115 sample_group_description.grouping_type = FOURCC_seig;
117 sample_group_description.cenc_sample_encryption_info_entries.resize(1);
119 sample_group_description.cenc_sample_encryption_info_entries.back();
120 sample_group_entry.is_protected = 1;
121 if (protection_scheme() == FOURCC_cbcs) {
123 sample_group_entry.per_sample_iv_size = 0;
124 sample_group_entry.constant_iv = encryptor()->
iv();
126 sample_group_entry.per_sample_iv_size =
127 static_cast<uint8_t
>(encryptor()->
iv().size());
129 sample_group_entry.crypt_byte_block = crypt_byte_block();
130 sample_group_entry.skip_byte_block = skip_byte_block();
131 sample_group_entry.key_id = encryption_key()->key_id;