7 #include "packager/media/formats/mp4/encrypting_fragmenter.h"
11 #include "packager/media/base/aes_encryptor.h"
12 #include "packager/media/base/aes_pattern_cryptor.h"
13 #include "packager/media/base/buffer_reader.h"
14 #include "packager/media/base/key_source.h"
15 #include "packager/media/base/media_sample.h"
16 #include "packager/media/codecs/nalu_reader.h"
17 #include "packager/media/codecs/vp8_parser.h"
18 #include "packager/media/codecs/vp9_parser.h"
19 #include "packager/media/formats/mp4/box_definitions.h"
26 const size_t kCencBlockSize = 16u;
30 void AddSubsamples(uint64_t clear_bytes,
31 uint64_t cipher_bytes,
32 std::vector<SubsampleEntry>* subsamples) {
33 CHECK_LT(cipher_bytes, std::numeric_limits<uint32_t>::max());
34 const uint64_t kUInt16Max = std::numeric_limits<uint16_t>::max();
35 while (clear_bytes > kUInt16Max) {
36 subsamples->push_back(SubsampleEntry(kUInt16Max, 0));
37 clear_bytes -= kUInt16Max;
40 if (clear_bytes > 0 || cipher_bytes > 0)
41 subsamples->push_back(SubsampleEntry(clear_bytes, cipher_bytes));
44 Codec GetCodec(
const StreamInfo& stream_info) {
45 if (stream_info.stream_type() != kStreamVideo)
return kUnknownCodec;
46 const VideoStreamInfo& video_stream_info =
47 static_cast<const VideoStreamInfo&
>(stream_info);
48 return video_stream_info.codec();
51 uint8_t GetNaluLengthSize(
const StreamInfo& stream_info) {
52 if (stream_info.stream_type() != kStreamVideo)
55 const VideoStreamInfo& video_stream_info =
56 static_cast<const VideoStreamInfo&
>(stream_info);
57 return video_stream_info.nalu_length_size();
62 scoped_refptr<StreamInfo> info,
64 std::unique_ptr<EncryptionKey> encryption_key,
66 FourCC protection_scheme,
67 uint8_t crypt_byte_block,
68 uint8_t skip_byte_block,
72 encryption_key_(std::move(encryption_key)),
73 nalu_length_size_(GetNaluLengthSize(*info)),
74 video_codec_(GetCodec(*info)),
75 clear_time_(clear_time),
76 protection_scheme_(protection_scheme),
77 crypt_byte_block_(crypt_byte_block),
78 skip_byte_block_(skip_byte_block),
80 DCHECK(encryption_key_);
81 switch (video_codec_) {
97 if (nalu_length_size_ > 0) {
98 LOG(WARNING) <<
"Unknown video codec '" << video_codec_
99 <<
"', whole subsamples will be encrypted.";
104 EncryptingFragmenter::~EncryptingFragmenter() {}
108 if (!fragment_initialized()) {
114 Status status = EncryptSample(sample);
126 if (header_parser_ && !header_parser_->Initialize(info_->codec_config()))
127 return Status(error::MUXER_FAILURE,
"Fail to read SPS and PPS data.");
129 traf()->auxiliary_size.sample_info_sizes.clear();
130 traf()->auxiliary_offset.offsets.clear();
131 if (IsSubsampleEncryptionRequired()) {
132 traf()->sample_encryption.flags |=
133 SampleEncryption::kUseSubsampleEncryption;
135 traf()->sample_encryption.sample_encryption_entries.clear();
137 const bool enable_encryption = clear_time_ <= 0;
138 if (!enable_encryption) {
142 const uint32_t kClearSampleDescriptionIndex = 2;
144 traf()->header.flags |=
145 TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
146 traf()->header.sample_description_index = kClearSampleDescriptionIndex;
156 DCHECK_LE(clear_time_, 0);
159 DCHECK_GT(clear_time_, 0);
160 clear_time_ -= fragment_duration();
166 bool enable_encryption) {
167 return (!enable_encryption || encryptor_) ? Status::OK :
CreateEncryptor();
172 traf()->auxiliary_offset.offsets.push_back(0);
175 const uint8_t per_sample_iv_size =
176 (protection_scheme_ == FOURCC_cbcs) ? 0 :
177 static_cast<uint8_t>(encryptor_->iv().size());
178 traf()->sample_encryption.iv_size = per_sample_iv_size;
182 saiz.sample_count = traf()->runs[0].sample_sizes.size();
183 if (!saiz.sample_info_sizes.empty()) {
185 &saiz.default_sample_info_size)) {
186 saiz.default_sample_info_size = 0;
191 DCHECK(!IsSubsampleEncryptionRequired());
192 saiz.default_sample_info_size =
static_cast<uint8_t
>(per_sample_iv_size);
197 if (saiz.default_sample_info_size == 0 && saiz.sample_info_sizes.empty()) {
198 DCHECK_EQ(protection_scheme_, FOURCC_cbcs);
199 DCHECK(!IsSubsampleEncryptionRequired());
203 saiz.sample_count = 0;
204 traf()->auxiliary_offset.offsets.clear();
209 DCHECK(encryption_key_);
210 std::unique_ptr<AesCryptor> encryptor;
211 switch (protection_scheme_) {
220 crypt_byte_block(), skip_byte_block(),
222 AesCryptor::kDontUseConstantIv,
227 crypt_byte_block(), skip_byte_block(),
229 AesCryptor::kUseConstantIv,
233 return Status(error::MUXER_FAILURE,
"Unsupported protection scheme.");
236 DCHECK(!encryption_key_->iv.empty());
237 const bool initialized =
238 encryptor->InitializeWithIv(encryption_key_->key, encryption_key_->iv);
240 return Status(error::MUXER_FAILURE,
"Failed to create the encryptor.");
241 encryptor_ = std::move(encryptor);
245 void EncryptingFragmenter::EncryptBytes(uint8_t* data, uint32_t size) {
247 CHECK(encryptor_->Crypt(data, size, data));
250 Status EncryptingFragmenter::EncryptSample(scoped_refptr<MediaSample> sample) {
253 SampleEncryptionEntry sample_encryption_entry;
255 if (protection_scheme_ != FOURCC_cbcs)
256 sample_encryption_entry.initialization_vector = encryptor_->iv();
257 uint8_t* data = sample->writable_data();
258 if (IsSubsampleEncryptionRequired()) {
260 std::vector<VPxFrameInfo> vpx_frames;
261 if (!vpx_parser_->Parse(sample->data(), sample->data_size(),
263 return Status(error::MUXER_FAILURE,
"Failed to parse vpx frame.");
266 const bool is_superframe = vpx_frames.size() > 1;
267 for (
const VPxFrameInfo& frame : vpx_frames) {
268 SubsampleEntry subsample;
269 subsample.clear_bytes =
270 static_cast<uint16_t
>(frame.uncompressed_header_size);
271 subsample.cipher_bytes =
272 frame.frame_size - frame.uncompressed_header_size;
281 if (is_superframe || protection_scheme_ == FOURCC_cbc1 ||
282 protection_scheme_ == FOURCC_cens) {
283 const uint16_t misalign_bytes =
284 subsample.cipher_bytes % kCencBlockSize;
285 subsample.clear_bytes += misalign_bytes;
286 subsample.cipher_bytes -= misalign_bytes;
289 sample_encryption_entry.subsamples.push_back(subsample);
290 if (subsample.cipher_bytes > 0)
291 EncryptBytes(data + subsample.clear_bytes, subsample.cipher_bytes);
292 data += frame.frame_size;
296 size_t index_size = sample->data() + sample->data_size() - data;
297 DCHECK_LE(index_size, 2 + vpx_frames.size() * 4);
298 DCHECK_GE(index_size, 2 + vpx_frames.size() * 1);
299 SubsampleEntry subsample;
300 subsample.clear_bytes =
static_cast<uint16_t
>(index_size);
301 subsample.cipher_bytes = 0;
302 sample_encryption_entry.subsamples.push_back(subsample);
305 const Nalu::CodecType nalu_type =
306 (video_codec_ == kCodecHVC1 || video_codec_ == kCodecHEV1)
309 NaluReader reader(nalu_type, nalu_length_size_, data,
310 sample->data_size());
314 uint64_t accumulated_clear_bytes = 0;
317 NaluReader::Result result;
318 while ((result = reader.Advance(&nalu)) == NaluReader::kOk) {
319 if (nalu.is_video_slice()) {
323 const int64_t video_slice_header_size =
324 header_parser_ ? header_parser_->GetHeaderSize(nalu) : 0;
325 if (video_slice_header_size < 0)
326 return Status(error::MUXER_FAILURE,
"Failed to read slice header.");
328 uint64_t current_clear_bytes =
329 nalu.header_size() + video_slice_header_size;
330 uint64_t cipher_bytes = nalu.payload_size() - video_slice_header_size;
335 if (protection_scheme_ == FOURCC_cbc1 ||
336 protection_scheme_ == FOURCC_cens) {
337 const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
338 current_clear_bytes += misalign_bytes;
339 cipher_bytes -= misalign_bytes;
342 const uint8_t* nalu_data = nalu.data() + current_clear_bytes;
343 EncryptBytes(const_cast<uint8_t*>(nalu_data), cipher_bytes);
346 accumulated_clear_bytes + nalu_length_size_ + current_clear_bytes,
347 cipher_bytes, &sample_encryption_entry.subsamples);
348 accumulated_clear_bytes = 0;
351 accumulated_clear_bytes +=
352 nalu_length_size_ + nalu.header_size() + nalu.payload_size();
355 if (result != NaluReader::kEOStream)
356 return Status(error::MUXER_FAILURE,
"Failed to parse NAL units.");
357 AddSubsamples(accumulated_clear_bytes, 0,
358 &sample_encryption_entry.subsamples);
360 DCHECK_EQ(sample_encryption_entry.GetTotalSizeOfSubsamples(),
361 sample->data_size());
364 traf()->auxiliary_size.sample_info_sizes.push_back(
365 sample_encryption_entry.ComputeSize());
367 DCHECK_LE(crypt_byte_block(), 1u);
368 DCHECK_EQ(skip_byte_block(), 0u);
369 EncryptBytes(data, sample->data_size());
372 traf()->sample_encryption.sample_encryption_entries.push_back(
373 sample_encryption_entry);
374 encryptor_->UpdateIv();
378 bool EncryptingFragmenter::IsSubsampleEncryptionRequired() {
379 return vpx_parser_ || nalu_length_size_ != 0;