7 #include <packager/media/crypto/sample_aes_ec3_cryptor.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
14 #include <packager/media/base/buffer_reader.h>
20 bool ExtractEac3SyncframeSizes(
const uint8_t* source,
22 std::vector<size_t>* syncframe_sizes) {
24 DCHECK(syncframe_sizes);
26 syncframe_sizes->clear();
27 BufferReader frame(source, source_size);
29 while (frame.HasBytes(1)) {
31 if (!frame.Read2(&syncword)) {
32 LOG(ERROR) <<
"Not enough bytes for syncword.";
35 if (syncword != 0x0B77) {
36 LOG(ERROR) <<
"Invalid E-AC3 frame. Seeing 0x" << std::hex << syncword
38 <<
". The sync frame does not start with "
39 "the valid syncword 0x0B77.";
42 uint16_t stream_type_and_syncframe_size;
43 if (!frame.Read2(&stream_type_and_syncframe_size)) {
44 LOG(ERROR) <<
"Not enough bytes for syncframe size.";
48 const size_t syncframe_size =
49 ((stream_type_and_syncframe_size & 0x7FF) + 1) * 2;
50 if (!frame.SkipBytes(syncframe_size -
sizeof(syncword) -
51 sizeof(stream_type_and_syncframe_size))) {
52 LOG(ERROR) <<
"Not enough bytes for syncframe. Expecting "
53 << syncframe_size <<
" bytes.";
56 syncframe_sizes->push_back(syncframe_size);
66 DCHECK(!cryptor_->use_constant_iv());
70 const std::vector<uint8_t>& iv) {
71 return SetIv(
iv) && cryptor_->InitializeWithIv(key,
iv);
74 bool SampleAesEc3Cryptor::CryptInternal(
const uint8_t* text,
77 size_t* crypt_text_size) {
79 if (*crypt_text_size < text_size) {
80 LOG(ERROR) <<
"Expecting output size of at least " << text_size
84 *crypt_text_size = text_size;
86 std::vector<size_t> syncframe_sizes;
87 if (!ExtractEac3SyncframeSizes(text, text_size, &syncframe_sizes))
93 const size_t kLeadingClearBytesSize = 16u;
95 for (
size_t syncframe_size : syncframe_sizes) {
96 memcpy(crypt_text, text, std::min(syncframe_size, kLeadingClearBytesSize));
97 if (syncframe_size > kLeadingClearBytesSize) {
100 if (!cryptor_->Crypt(text + kLeadingClearBytesSize,
101 syncframe_size - kLeadingClearBytesSize,
102 crypt_text + kLeadingClearBytesSize)) {
106 text += syncframe_size;
107 crypt_text += syncframe_size;
112 void SampleAesEc3Cryptor::SetIvInternal() {
113 CHECK(cryptor_->SetIv(
iv()));
All the methods that are virtual are virtual for mocking.