7#include <packager/media/crypto/sample_aes_ec3_cryptor.h>
18#include <absl/log/check.h>
19#include <absl/log/log.h>
21#include <packager/media/base/aes_cryptor.h>
22#include <packager/media/base/buffer_reader.h>
28bool ExtractEac3SyncframeSizes(
const uint8_t* source,
30 std::vector<size_t>* syncframe_sizes) {
32 DCHECK(syncframe_sizes);
34 syncframe_sizes->clear();
35 BufferReader frame(source, source_size);
37 while (frame.HasBytes(1)) {
39 if (!frame.Read2(&syncword)) {
40 LOG(ERROR) <<
"Not enough bytes for syncword.";
43 if (syncword != 0x0B77) {
44 LOG(ERROR) <<
"Invalid E-AC3 frame. Seeing 0x" << std::hex << syncword
46 <<
". The sync frame does not start with "
47 "the valid syncword 0x0B77.";
50 uint16_t stream_type_and_syncframe_size;
51 if (!frame.Read2(&stream_type_and_syncframe_size)) {
52 LOG(ERROR) <<
"Not enough bytes for syncframe size.";
56 const size_t syncframe_size =
57 ((stream_type_and_syncframe_size & 0x7FF) + 1) * 2;
58 if (!frame.SkipBytes(syncframe_size -
sizeof(syncword) -
59 sizeof(stream_type_and_syncframe_size))) {
60 LOG(ERROR) <<
"Not enough bytes for syncframe. Expecting "
61 << syncframe_size <<
" bytes.";
64 syncframe_sizes->push_back(syncframe_size);
74 DCHECK(!cryptor_->use_constant_iv());
78 const std::vector<uint8_t>& iv) {
79 return SetIv(
iv) && cryptor_->InitializeWithIv(key,
iv);
82bool SampleAesEc3Cryptor::CryptInternal(
const uint8_t* text,
85 size_t* crypt_text_size) {
87 if (*crypt_text_size < text_size) {
88 LOG(ERROR) <<
"Expecting output size of at least " << text_size
92 *crypt_text_size = text_size;
94 std::vector<size_t> syncframe_sizes;
95 if (!ExtractEac3SyncframeSizes(text, text_size, &syncframe_sizes))
101 const size_t kLeadingClearBytesSize = 16u;
103 for (
size_t syncframe_size : syncframe_sizes) {
104 memcpy(crypt_text, text, std::min(syncframe_size, kLeadingClearBytesSize));
105 if (syncframe_size > kLeadingClearBytesSize) {
108 if (!cryptor_->Crypt(text + kLeadingClearBytesSize,
109 syncframe_size - kLeadingClearBytesSize,
110 crypt_text + kLeadingClearBytesSize)) {
114 text += syncframe_size;
115 crypt_text += syncframe_size;
120void SampleAesEc3Cryptor::SetIvInternal() {
121 CHECK(cryptor_->SetIv(
iv()));
All the methods that are virtual are virtual for mocking.