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/filters/nalu_reader.h"
17 #include "packager/media/filters/vp8_parser.h"
18 #include "packager/media/filters/vp9_parser.h"
19 #include "packager/media/formats/mp4/box_definitions.h"
21 namespace edash_packager {
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 VideoCodec GetVideoCodec(
const StreamInfo& stream_info) {
45 if (stream_info.stream_type() != kStreamVideo)
46 return kUnknownVideoCodec;
47 const VideoStreamInfo& video_stream_info =
48 static_cast<const VideoStreamInfo&
>(stream_info);
49 return video_stream_info.codec();
52 uint8_t GetNaluLengthSize(
const StreamInfo& stream_info) {
53 if (stream_info.stream_type() != kStreamVideo)
56 const VideoStreamInfo& video_stream_info =
57 static_cast<const VideoStreamInfo&
>(stream_info);
58 return video_stream_info.nalu_length_size();
63 scoped_refptr<StreamInfo> info,
65 scoped_ptr<EncryptionKey> encryption_key,
67 FourCC protection_scheme,
68 uint8_t crypt_byte_block,
69 uint8_t skip_byte_block)
72 encryption_key_(encryption_key.Pass()),
73 nalu_length_size_(GetNaluLengthSize(*info)),
74 video_codec_(GetVideoCodec(*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) {
79 DCHECK(encryption_key_);
80 switch (video_codec_) {
96 if (nalu_length_size_ > 0) {
97 LOG(WARNING) <<
"Unknown video codec '" << video_codec_
98 <<
"', whole subsamples will be encrypted.";
103 EncryptingFragmenter::~EncryptingFragmenter() {}
107 if (!fragment_initialized()) {
113 Status status = EncryptSample(sample);
125 if (header_parser_ && !header_parser_->Initialize(info_->extra_data()))
126 return Status(error::MUXER_FAILURE,
"Fail to read SPS and PPS data.");
128 traf()->auxiliary_size.sample_info_sizes.clear();
129 traf()->auxiliary_offset.offsets.clear();
130 if (IsSubsampleEncryptionRequired()) {
131 traf()->sample_encryption.flags |=
132 SampleEncryption::kUseSubsampleEncryption;
134 traf()->sample_encryption.sample_encryption_entries.clear();
136 const bool enable_encryption = clear_time_ <= 0;
137 if (!enable_encryption) {
141 const uint32_t kClearSampleDescriptionIndex = 2;
143 traf()->header.flags |=
144 TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
145 traf()->header.sample_description_index = kClearSampleDescriptionIndex;
152 DCHECK_LE(clear_time_, 0);
155 DCHECK_GT(clear_time_, 0);
156 clear_time_ -= fragment_duration();
162 bool enable_encryption) {
163 return (!enable_encryption || encryptor_) ? Status::OK :
CreateEncryptor();
168 traf()->auxiliary_offset.offsets.push_back(0);
171 const size_t per_sample_iv_size =
172 (protection_scheme_ == FOURCC_cbcs) ? 0 : encryptor_->iv().size();
173 traf()->sample_encryption.iv_size = per_sample_iv_size;
177 saiz.sample_count = traf()->runs[0].sample_sizes.size();
178 if (!saiz.sample_info_sizes.empty()) {
180 &saiz.default_sample_info_size)) {
181 saiz.default_sample_info_size = 0;
186 DCHECK(!IsSubsampleEncryptionRequired());
187 saiz.default_sample_info_size = per_sample_iv_size;
191 CHECK(!saiz.sample_info_sizes.empty() || saiz.default_sample_info_size != 0);
195 DCHECK(encryption_key_);
196 scoped_ptr<AesCryptor> encryptor;
197 switch (protection_scheme_) {
206 crypt_byte_block(), skip_byte_block(), AesCryptor::kDontUseConstantIv,
211 crypt_byte_block(), skip_byte_block(), AesCryptor::kUseConstantIv,
215 return Status(error::MUXER_FAILURE,
"Unsupported protection scheme.");
218 DCHECK(!encryption_key_->iv.empty());
219 const bool initialized =
220 encryptor->InitializeWithIv(encryption_key_->key, encryption_key_->iv);
222 return Status(error::MUXER_FAILURE,
"Failed to create the encryptor.");
223 encryptor_ = encryptor.Pass();
227 void EncryptingFragmenter::EncryptBytes(uint8_t* data, uint32_t size) {
229 CHECK(encryptor_->Crypt(data, size, data));
232 Status EncryptingFragmenter::EncryptSample(scoped_refptr<MediaSample> sample) {
235 SampleEncryptionEntry sample_encryption_entry;
237 if (protection_scheme_ != FOURCC_cbcs)
238 sample_encryption_entry.initialization_vector = encryptor_->iv();
239 uint8_t* data = sample->writable_data();
240 if (IsSubsampleEncryptionRequired()) {
242 std::vector<VPxFrameInfo> vpx_frames;
243 if (!vpx_parser_->Parse(sample->data(), sample->data_size(),
245 return Status(error::MUXER_FAILURE,
"Failed to parse vpx frame.");
248 const bool is_superframe = vpx_frames.size() > 1;
249 for (
const VPxFrameInfo& frame : vpx_frames) {
250 SubsampleEntry subsample;
251 subsample.clear_bytes = frame.uncompressed_header_size;
252 subsample.cipher_bytes =
253 frame.frame_size - frame.uncompressed_header_size;
262 if (is_superframe || protection_scheme_ == FOURCC_cbc1 ||
263 protection_scheme_ == FOURCC_cens) {
264 const uint16_t misalign_bytes =
265 subsample.cipher_bytes % kCencBlockSize;
266 subsample.clear_bytes += misalign_bytes;
267 subsample.cipher_bytes -= misalign_bytes;
270 sample_encryption_entry.subsamples.push_back(subsample);
271 if (subsample.cipher_bytes > 0)
272 EncryptBytes(data + subsample.clear_bytes, subsample.cipher_bytes);
273 data += frame.frame_size;
276 const Nalu::CodecType nalu_type =
277 (video_codec_ == kCodecHVC1 || video_codec_ == kCodecHEV1)
280 NaluReader reader(nalu_type, nalu_length_size_, data,
281 sample->data_size());
285 uint64_t accumulated_clear_bytes = 0;
288 NaluReader::Result result;
289 while ((result = reader.Advance(&nalu)) == NaluReader::kOk) {
290 if (nalu.is_video_slice()) {
294 const int64_t video_slice_header_size =
295 header_parser_ ? header_parser_->GetHeaderSize(nalu) : 0;
296 if (video_slice_header_size < 0)
297 return Status(error::MUXER_FAILURE,
"Failed to read slice header.");
299 uint64_t current_clear_bytes =
300 nalu.header_size() + video_slice_header_size;
301 uint64_t cipher_bytes = nalu.payload_size() - video_slice_header_size;
306 if (protection_scheme_ == FOURCC_cbc1 ||
307 protection_scheme_ == FOURCC_cens) {
308 const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
309 current_clear_bytes += misalign_bytes;
310 cipher_bytes -= misalign_bytes;
313 const uint8_t* nalu_data = nalu.data() + current_clear_bytes;
314 EncryptBytes(const_cast<uint8_t*>(nalu_data), cipher_bytes);
317 accumulated_clear_bytes + nalu_length_size_ + current_clear_bytes,
318 cipher_bytes, &sample_encryption_entry.subsamples);
319 accumulated_clear_bytes = 0;
322 accumulated_clear_bytes +=
323 nalu_length_size_ + nalu.header_size() + nalu.payload_size();
326 if (result != NaluReader::kEOStream)
327 return Status(error::MUXER_FAILURE,
"Failed to parse NAL units.");
328 AddSubsamples(accumulated_clear_bytes, 0,
329 &sample_encryption_entry.subsamples);
333 traf()->auxiliary_size.sample_info_sizes.push_back(
334 sample_encryption_entry.ComputeSize());
336 EncryptBytes(data, sample->data_size());
339 traf()->sample_encryption.sample_encryption_entries.push_back(
340 sample_encryption_entry);
341 encryptor_->UpdateIv();
345 bool EncryptingFragmenter::IsSubsampleEncryptionRequired() {
346 return vpx_parser_ || nalu_length_size_ != 0;