7 #include "packager/media/formats/webm/encryptor.h"
9 #include "packager/media/base/aes_encryptor.h"
10 #include "packager/media/base/media_sample.h"
12 namespace edash_packager {
18 const size_t kDefaultIvSize = 8u;
20 Status CreateContentEncryption(mkvmuxer::Track* track, EncryptionKey* key) {
21 if (!track->AddContentEncoding()) {
22 return Status(error::INTERNAL_ERROR,
23 "Could not add ContentEncoding to track.");
26 mkvmuxer::ContentEncoding*
const encoding =
27 track->GetContentEncodingByIndex(0);
29 return Status(error::INTERNAL_ERROR,
30 "Could not add ContentEncoding to track.");
33 mkvmuxer::ContentEncAESSettings*
const aes = encoding->enc_aes_settings();
35 return Status(error::INTERNAL_ERROR,
36 "Error getting ContentEncAESSettings.");
38 if (aes->cipher_mode() != mkvmuxer::ContentEncAESSettings::kCTR) {
39 return Status(error::INTERNAL_ERROR,
"Cipher Mode is not CTR.");
42 if (!key->key_id.empty() &&
43 !encoding->SetEncryptionID(
44 reinterpret_cast<const uint8*>(key->key_id.data()),
45 key->key_id.size())) {
46 return Status(error::INTERNAL_ERROR,
"Error setting encryption ID.");
53 Encryptor::Encryptor() {}
55 Encryptor::~Encryptor() {}
58 KeySource::TrackType track_type,
61 return CreateEncryptor(muxer_listener, track_type, key_source);
66 return CreateContentEncryption(track, key_.get());
73 const size_t sample_size = sample->data_size();
76 const size_t iv_size = encryptor_->iv().size();
77 sample->resize_data(sample_size + iv_size + 1);
78 uint8_t* sample_data = sample->writable_data();
81 if (!encryptor_->Encrypt(sample_data, sample_size, sample_data)) {
82 return Status(error::MUXER_FAILURE,
"Failed to encrypt the frame.");
87 memmove(sample_data + iv_size + 1, sample_data, sample_size);
88 sample_data[0] = 0x01;
89 memcpy(sample_data + 1, encryptor_->iv().data(), iv_size);
91 encryptor_->UpdateIv();
94 sample->resize_data(sample_size + 1);
95 uint8_t* sample_data = sample->writable_data();
96 memmove(sample_data + 1, sample_data, sample_size);
97 sample_data[0] = 0x00;
104 KeySource::TrackType track_type,
106 scoped_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
107 Status status = key_source->
GetKey(track_type, encryption_key.get());
112 const bool initialized = encryption_key->iv.empty()
113 ? encryptor->InitializeWithRandomIv(
114 encryption_key->key, kDefaultIvSize)
115 : encryptor->InitializeWithIv(
116 encryption_key->key, encryption_key->iv);
118 return Status(error::INTERNAL_ERROR,
"Failed to create the encryptor.");
120 if (muxer_listener) {
121 const bool kInitialEncryptionInfo =
true;
122 muxer_listener->OnEncryptionInfoReady(
123 kInitialEncryptionInfo, key_source->
UUID(), key_source->
SystemName(),
124 encryption_key->key_id, encryption_key->pssh);
127 key_ = encryption_key.Pass();
128 encryptor_ = encryptor.Pass();