7 #include "packager/media/formats/webm/encryptor.h"
9 #include "packager/media/base/aes_encryptor.h"
10 #include "packager/media/base/fourccs.h"
11 #include "packager/media/base/media_sample.h"
13 namespace edash_packager {
18 Status CreateContentEncryption(mkvmuxer::Track* track, EncryptionKey* key) {
19 if (!track->AddContentEncoding()) {
20 return Status(error::INTERNAL_ERROR,
21 "Could not add ContentEncoding to track.");
24 mkvmuxer::ContentEncoding*
const encoding =
25 track->GetContentEncodingByIndex(0);
27 return Status(error::INTERNAL_ERROR,
28 "Could not add ContentEncoding to track.");
31 mkvmuxer::ContentEncAESSettings*
const aes = encoding->enc_aes_settings();
33 return Status(error::INTERNAL_ERROR,
34 "Error getting ContentEncAESSettings.");
36 if (aes->cipher_mode() != mkvmuxer::ContentEncAESSettings::kCTR) {
37 return Status(error::INTERNAL_ERROR,
"Cipher Mode is not CTR.");
40 if (!key->key_id.empty() &&
41 !encoding->SetEncryptionID(
42 reinterpret_cast<const uint8*>(key->key_id.data()),
43 key->key_id.size())) {
44 return Status(error::INTERNAL_ERROR,
"Error setting encryption ID.");
51 Encryptor::Encryptor() {}
53 Encryptor::~Encryptor() {}
56 KeySource::TrackType track_type,
59 return CreateEncryptor(muxer_listener, track_type, key_source);
64 return CreateContentEncryption(track, key_.get());
71 const size_t sample_size = sample->data_size();
74 const size_t iv_size = encryptor_->iv().size();
75 sample->resize_data(sample_size + iv_size + 1);
76 uint8_t* sample_data = sample->writable_data();
79 if (!encryptor_->Crypt(sample_data, sample_size, sample_data)) {
80 return Status(error::MUXER_FAILURE,
"Failed to encrypt the frame.");
85 memmove(sample_data + iv_size + 1, sample_data, sample_size);
86 sample_data[0] = 0x01;
87 memcpy(sample_data + 1, encryptor_->iv().data(), iv_size);
89 encryptor_->UpdateIv();
92 sample->resize_data(sample_size + 1);
93 uint8_t* sample_data = sample->writable_data();
94 memmove(sample_data + 1, sample_data, sample_size);
95 sample_data[0] = 0x00;
102 KeySource::TrackType track_type,
104 scoped_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
105 Status status = key_source->
GetKey(track_type, encryption_key.get());
108 if (encryption_key->iv.empty()) {
110 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
114 const bool initialized =
115 encryptor->InitializeWithIv(encryption_key->key, encryption_key->iv);
117 return Status(error::INTERNAL_ERROR,
"Failed to create the encryptor.");
119 if (muxer_listener) {
120 const bool kInitialEncryptionInfo =
true;
122 kInitialEncryptionInfo, FOURCC_cenc, encryption_key->key_id,
123 encryptor->iv(), encryption_key->key_system_info);
126 key_ = encryption_key.Pass();
127 encryptor_ = encryptor.Pass();