7 #include <packager/media/formats/webm/encryptor.h>
9 #include <absl/log/check.h>
11 #include <packager/media/base/buffer_writer.h>
12 #include <packager/media/base/media_sample.h>
13 #include <packager/media/formats/webm/webm_constants.h>
19 void WriteEncryptedFrameHeader(
const DecryptConfig* decrypt_config,
20 BufferWriter* header_buffer) {
22 const size_t iv_size = decrypt_config->iv().size();
23 DCHECK_EQ(iv_size, kWebMIvSize);
24 if (!decrypt_config->subsamples().empty()) {
25 const auto& subsamples = decrypt_config->subsamples();
28 DCHECK_LT(subsamples.size(), kWebMMaxSubsamples);
29 const size_t num_partitions =
30 2 * subsamples.size() - 1 -
31 (subsamples.back().cipher_bytes == 0 ? 1 : 0);
32 const size_t header_size = kWebMSignalByteSize + iv_size +
33 kWebMNumPartitionsSize +
34 (kWebMPartitionOffsetSize * num_partitions);
36 const uint8_t signal_byte = kWebMEncryptedSignal | kWebMPartitionedSignal;
37 header_buffer->AppendInt(signal_byte);
38 header_buffer->AppendVector(decrypt_config->iv());
39 header_buffer->AppendInt(
static_cast<uint8_t
>(num_partitions));
41 uint32_t partition_offset = 0;
42 for (
size_t i = 0; i < subsamples.size() - 1; ++i) {
43 partition_offset += subsamples[i].clear_bytes;
44 header_buffer->AppendInt(partition_offset);
45 partition_offset += subsamples[i].cipher_bytes;
46 header_buffer->AppendInt(partition_offset);
50 if (subsamples.back().cipher_bytes != 0) {
51 partition_offset += subsamples.back().clear_bytes;
52 header_buffer->AppendInt(partition_offset);
55 DCHECK_EQ(header_size, header_buffer->Size());
58 const uint8_t signal_byte = kWebMEncryptedSignal;
59 header_buffer->AppendInt(signal_byte);
60 header_buffer->AppendVector(decrypt_config->iv());
64 const uint8_t signal_byte = 0x00;
65 header_buffer->AppendInt(signal_byte);
70 Status UpdateTrackForEncryption(
const std::vector<uint8_t>& key_id,
71 mkvmuxer::Track* track) {
72 DCHECK_EQ(track->content_encoding_entries_size(), 0u);
74 if (!track->AddContentEncoding()) {
75 return Status(error::INTERNAL_ERROR,
76 "Could not add ContentEncoding to track.");
79 mkvmuxer::ContentEncoding*
const encoding =
80 track->GetContentEncodingByIndex(0);
82 return Status(error::INTERNAL_ERROR,
83 "Could not add ContentEncoding to track.");
86 mkvmuxer::ContentEncAESSettings*
const aes = encoding->enc_aes_settings();
88 return Status(error::INTERNAL_ERROR,
89 "Error getting ContentEncAESSettings.");
91 if (aes->cipher_mode() != mkvmuxer::ContentEncAESSettings::kCTR) {
92 return Status(error::INTERNAL_ERROR,
"Cipher Mode is not CTR.");
95 if (!encoding->SetEncryptionID(key_id.data(), key_id.size())) {
96 return Status(error::INTERNAL_ERROR,
"Error setting encryption ID.");
101 void UpdateFrameForEncryption(MediaSample* sample) {
103 BufferWriter header_buffer;
104 WriteEncryptedFrameHeader(sample->decrypt_config(), &header_buffer);
106 const size_t sample_size = header_buffer.Size() + sample->data_size();
107 std::shared_ptr<uint8_t> new_sample_data(
new uint8_t[sample_size],
108 std::default_delete<uint8_t[]>());
109 memcpy(new_sample_data.get(), header_buffer.Buffer(), header_buffer.Size());
110 memcpy(&new_sample_data.get()[header_buffer.Size()], sample->data(),
111 sample->data_size());
112 sample->TransferData(std::move(new_sample_data), sample_size);
All the methods that are virtual are virtual for mocking.