7 #include "packager/media/formats/mp4/encrypting_fragmenter.h"
9 #include "packager/media/base/aes_encryptor.h"
10 #include "packager/media/base/buffer_reader.h"
11 #include "packager/media/base/key_source.h"
12 #include "packager/media/base/media_sample.h"
13 #include "packager/media/filters/vp8_parser.h"
14 #include "packager/media/filters/vp9_parser.h"
15 #include "packager/media/formats/mp4/box_definitions.h"
19 const size_t kDefaultIvSize = 8u;
22 namespace edash_packager {
28 scoped_ptr<EncryptionKey> encryption_key,
30 VideoCodec video_codec,
31 uint8_t nalu_length_size)
33 encryption_key_(encryption_key.Pass()),
34 video_codec_(video_codec),
35 nalu_length_size_(nalu_length_size),
36 clear_time_(clear_time) {
37 DCHECK(encryption_key_);
38 if (video_codec == kCodecVP8) {
40 }
else if (video_codec == kCodecVP9) {
45 EncryptingFragmenter::~EncryptingFragmenter() {}
49 if (!fragment_initialized()) {
55 Status status = EncryptSample(sample);
67 traf()->auxiliary_size.sample_info_sizes.clear();
68 traf()->auxiliary_offset.offsets.clear();
69 if (IsSubsampleEncryptionRequired()) {
70 traf()->sample_encryption.flags |=
71 SampleEncryption::kUseSubsampleEncryption;
73 traf()->sample_encryption.sample_encryption_entries.clear();
75 const bool enable_encryption = clear_time_ <= 0;
76 if (!enable_encryption) {
80 const uint32_t kClearSampleDescriptionIndex = 2;
82 traf()->header.flags |=
83 TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
84 traf()->header.sample_description_index = kClearSampleDescriptionIndex;
91 DCHECK_LE(clear_time_, 0);
94 DCHECK_GT(clear_time_, 0);
95 clear_time_ -= fragment_duration();
101 bool enable_encryption) {
102 return (!enable_encryption || encryptor_) ? Status::OK :
CreateEncryptor();
107 traf()->auxiliary_offset.offsets.push_back(0);
111 saiz.sample_count = traf()->runs[0].sample_sizes.size();
112 if (!saiz.sample_info_sizes.empty()) {
114 &saiz.default_sample_info_size)) {
115 saiz.default_sample_info_size = 0;
120 DCHECK(!IsSubsampleEncryptionRequired());
121 saiz.default_sample_info_size = encryptor_->iv().size();
123 traf()->sample_encryption.iv_size = encryptor_->iv().size();
127 DCHECK(encryption_key_);
130 const bool initialized = encryption_key_->iv.empty()
131 ? encryptor->InitializeWithRandomIv(
132 encryption_key_->key, kDefaultIvSize)
133 : encryptor->InitializeWithIv(
134 encryption_key_->key, encryption_key_->iv);
136 return Status(error::MUXER_FAILURE,
"Failed to create the encryptor.");
137 encryptor_ = encryptor.Pass();
141 void EncryptingFragmenter::EncryptBytes(uint8_t* data, uint32_t size) {
143 CHECK(encryptor_->Encrypt(data, size, data));
146 Status EncryptingFragmenter::EncryptSample(scoped_refptr<MediaSample> sample) {
149 SampleEncryptionEntry sample_encryption_entry;
150 sample_encryption_entry.initialization_vector = encryptor_->iv();
151 uint8_t* data = sample->writable_data();
152 if (IsSubsampleEncryptionRequired()) {
154 std::vector<VPxFrameInfo> vpx_frames;
155 if (!vpx_parser_->Parse(sample->data(), sample->data_size(),
157 return Status(error::MUXER_FAILURE,
"Failed to parse vpx frame.");
159 for (
const VPxFrameInfo& frame : vpx_frames) {
160 SubsampleEntry subsample;
161 subsample.clear_bytes = frame.uncompressed_header_size;
162 subsample.cipher_bytes =
163 frame.frame_size - frame.uncompressed_header_size;
164 sample_encryption_entry.subsamples.push_back(subsample);
165 if (subsample.cipher_bytes > 0)
166 EncryptBytes(data + subsample.clear_bytes, subsample.cipher_bytes);
167 data += frame.frame_size;
170 BufferReader reader(data, sample->data_size());
171 while (reader.HasBytes(1)) {
172 uint64_t nalu_length;
173 if (!reader.ReadNBytesInto8(&nalu_length, nalu_length_size_))
174 return Status(error::MUXER_FAILURE,
"Fail to read nalu_length.");
176 if (!reader.SkipBytes(nalu_length)) {
177 return Status(error::MUXER_FAILURE,
178 "Sample size does not match nalu_length.");
181 SubsampleEntry subsample;
182 subsample.clear_bytes = nalu_length_size_ + 1;
183 subsample.cipher_bytes = nalu_length - 1;
184 sample_encryption_entry.subsamples.push_back(subsample);
186 EncryptBytes(data + subsample.clear_bytes, subsample.cipher_bytes);
187 data += nalu_length_size_ + nalu_length;
192 traf()->auxiliary_size.sample_info_sizes.push_back(
193 sample_encryption_entry.ComputeSize());
195 EncryptBytes(data, sample->data_size());
198 traf()->sample_encryption.sample_encryption_entries.push_back(
199 sample_encryption_entry);
200 encryptor_->UpdateIv();