7 #include "packager/media/crypto/encryption_handler.h"
14 #include "packager/media/base/aes_encryptor.h"
15 #include "packager/media/base/aes_pattern_cryptor.h"
16 #include "packager/media/base/key_source.h"
17 #include "packager/media/base/media_sample.h"
18 #include "packager/media/base/video_stream_info.h"
19 #include "packager/media/codecs/video_slice_header_parser.h"
20 #include "packager/media/codecs/vp8_parser.h"
21 #include "packager/media/codecs/vp9_parser.h"
27 const size_t kCencBlockSize = 16u;
31 void AddSubsample(uint64_t clear_bytes,
32 uint64_t cipher_bytes,
33 DecryptConfig* decrypt_config) {
34 CHECK_LT(cipher_bytes, std::numeric_limits<uint32_t>::max());
35 const uint64_t kUInt16Max = std::numeric_limits<uint16_t>::max();
36 while (clear_bytes > kUInt16Max) {
37 decrypt_config->AddSubsample(kUInt16Max, 0);
38 clear_bytes -= kUInt16Max;
41 if (clear_bytes > 0 || cipher_bytes > 0)
42 decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
45 Codec GetVideoCodec(
const StreamInfo& stream_info) {
46 if (stream_info.stream_type() != kStreamVideo)
return kUnknownCodec;
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();
61 KeySource::TrackType GetTrackTypeForEncryption(
const StreamInfo& stream_info,
62 uint32_t max_sd_pixels,
63 uint32_t max_hd_pixels,
64 uint32_t max_uhd1_pixels) {
65 if (stream_info.stream_type() == kStreamAudio)
66 return KeySource::TRACK_TYPE_AUDIO;
68 if (stream_info.stream_type() != kStreamVideo)
69 return KeySource::TRACK_TYPE_UNKNOWN;
71 DCHECK_EQ(kStreamVideo, stream_info.stream_type());
72 const VideoStreamInfo& video_stream_info =
73 static_cast<const VideoStreamInfo&
>(stream_info);
74 uint32_t pixels = video_stream_info.width() * video_stream_info.height();
75 if (pixels <= max_sd_pixels) {
76 return KeySource::TRACK_TYPE_SD;
77 }
else if (pixels <= max_hd_pixels) {
78 return KeySource::TRACK_TYPE_HD;
79 }
else if (pixels <= max_uhd1_pixels) {
80 return KeySource::TRACK_TYPE_UHD1;
82 return KeySource::TRACK_TYPE_UHD2;
86 EncryptionHandler::EncryptionHandler(
87 const EncryptionOptions& encryption_options,
88 KeySource* key_source)
89 : encryption_options_(encryption_options), key_source_(key_source) {}
91 EncryptionHandler::~EncryptionHandler() {}
94 if (num_input_streams() != 1 || next_output_stream_index() != 1) {
95 return Status(error::INVALID_ARGUMENT,
96 "Expects exactly one input and output.");
103 switch (stream_data->stream_data_type) {
104 case StreamDataType::kStreamInfo:
105 status = ProcessStreamInfo(stream_data->stream_info.get());
107 case StreamDataType::kSegmentInfo:
108 if (!stream_data->segment_info->is_subsegment) {
110 if (remaining_clear_lead_ > 0)
111 remaining_clear_lead_ -= stream_data->segment_info->duration;
113 stream_data->segment_info->is_encrypted =
true;
116 case StreamDataType::kMediaSample:
117 status = ProcessMediaSample(stream_data->media_sample.get());
120 VLOG(3) <<
"Stream data type "
121 <<
static_cast<int>(stream_data->stream_data_type) <<
" ignored.";
124 return status.ok() ?
Dispatch(std::move(stream_data)) : status;
128 if (stream_info->is_encrypted()) {
129 return Status(error::INVALID_ARGUMENT,
130 "Input stream is already encrypted.");
133 remaining_clear_lead_ =
135 crypto_period_duration_ =
137 stream_info->time_scale();
138 nalu_length_size_ = GetNaluLengthSize(*stream_info);
139 video_codec_ = GetVideoCodec(*stream_info);
140 track_type_ = GetTrackTypeForEncryption(
143 switch (video_codec_) {
145 vpx_parser_.reset(
new VP9Parser);
148 header_parser_.reset(
new H264VideoSliceHeaderParser);
151 FALLTHROUGH_INTENDED;
153 header_parser_.reset(
new H265VideoSliceHeaderParser);
157 if (nalu_length_size_ > 0) {
158 LOG(WARNING) <<
"Unknown video codec '" << video_codec_ <<
"'";
159 return Status(error::ENCRYPTION_FAILURE,
"Unknown video codec.");
162 if (header_parser_ &&
163 !header_parser_->Initialize(stream_info->codec_config())) {
164 return Status(error::ENCRYPTION_FAILURE,
"Fail to read SPS and PPS data.");
170 if (stream_info->stream_type() == kStreamVideo) {
172 crypt_byte_block_ = 1u;
173 skip_byte_block_ = 9u;
183 crypt_byte_block_ = 1u;
184 skip_byte_block_ = 0u;
188 crypt_byte_block_ = 0u;
189 skip_byte_block_ = 0u;
192 stream_info->set_is_encrypted(
true);
196 Status EncryptionHandler::ProcessMediaSample(MediaSample* sample) {
200 std::vector<VPxFrameInfo> vpx_frames;
202 !vpx_parser_->Parse(sample->data(), sample->data_size(), &vpx_frames)) {
203 return Status(error::ENCRYPTION_FAILURE,
"Failed to parse vpx frame.");
205 if (remaining_clear_lead_ > 0)
210 EncryptionKey encryption_key;
211 bool create_encryptor =
false;
212 if (crypto_period_duration_ != 0) {
213 const int64_t current_crypto_period_index =
214 sample->dts() / crypto_period_duration_;
215 if (current_crypto_period_index != prev_crypto_period_index_) {
217 track_type_, &encryption_key);
220 create_encryptor =
true;
222 }
else if (!encryptor_) {
223 status = key_source_->
GetKey(track_type_, &encryption_key);
226 create_encryptor =
true;
228 if (create_encryptor && !CreateEncryptor(&encryption_key))
229 return Status(error::ENCRYPTION_FAILURE,
"Failed to create encryptor");
230 new_segment_ =
false;
233 std::unique_ptr<DecryptConfig> decrypt_config(
new DecryptConfig(
234 key_id_, encryptor_->iv(), std::vector<SubsampleEntry>(),
238 if (!EncryptVpxFrame(vpx_frames, sample, decrypt_config.get()))
239 return Status(error::ENCRYPTION_FAILURE,
"Failed to encrypt VPx frames.");
240 DCHECK_EQ(decrypt_config->GetTotalSizeOfSubsamples(), sample->data_size());
241 }
else if (nalu_length_size_ > 0) {
242 if (!EncryptNalFrame(sample, decrypt_config.get())) {
243 return Status(error::ENCRYPTION_FAILURE,
244 "Failed to encrypt video frames.");
246 DCHECK_EQ(decrypt_config->GetTotalSizeOfSubsamples(), sample->data_size());
248 DCHECK_LE(crypt_byte_block_, 1u);
249 DCHECK_EQ(skip_byte_block_, 0u);
250 EncryptBytes(sample->writable_data(), sample->data_size());
252 sample->set_decrypt_config(std::move(decrypt_config));
253 encryptor_->UpdateIv();
257 bool EncryptionHandler::CreateEncryptor(EncryptionKey* encryption_key) {
258 std::unique_ptr<AesCryptor> encryptor;
261 encryptor.reset(
new AesCtrEncryptor);
264 encryptor.reset(
new AesCbcEncryptor(kNoPadding));
267 encryptor.reset(
new AesPatternCryptor(
268 crypt_byte_block_, skip_byte_block_,
270 AesCryptor::kDontUseConstantIv,
271 std::unique_ptr<AesCryptor>(
new AesCtrEncryptor())));
274 encryptor.reset(
new AesPatternCryptor(
275 crypt_byte_block_, skip_byte_block_,
277 AesCryptor::kUseConstantIv,
278 std::unique_ptr<AesCryptor>(
new AesCbcEncryptor(kNoPadding))));
281 LOG(ERROR) <<
"Unsupported protection scheme.";
285 if (encryption_key->iv.empty()) {
287 &encryption_key->iv)) {
288 LOG(ERROR) <<
"Failed to generate random iv.";
292 const bool initialized =
293 encryptor->InitializeWithIv(encryption_key->key, encryption_key->iv);
294 encryptor_ = std::move(encryptor);
295 key_id_ = encryption_key->key_id;
299 bool EncryptionHandler::EncryptVpxFrame(
300 const std::vector<VPxFrameInfo>& vpx_frames,
302 DecryptConfig* decrypt_config) {
303 uint8_t* data = sample->writable_data();
304 const bool is_superframe = vpx_frames.size() > 1;
305 for (
const VPxFrameInfo& frame : vpx_frames) {
306 uint16_t clear_bytes =
307 static_cast<uint16_t
>(frame.uncompressed_header_size);
308 uint32_t cipher_bytes =
static_cast<uint32_t
>(
309 frame.frame_size - frame.uncompressed_header_size);
319 const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
320 clear_bytes += misalign_bytes;
321 cipher_bytes -= misalign_bytes;
323 decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
324 if (cipher_bytes > 0)
325 EncryptBytes(data + clear_bytes, cipher_bytes);
326 data += frame.frame_size;
330 size_t index_size = sample->data() + sample->data_size() - data;
331 DCHECK_LE(index_size, 2 + vpx_frames.size() * 4);
332 DCHECK_GE(index_size, 2 + vpx_frames.size() * 1);
333 uint16_t clear_bytes =
static_cast<uint16_t
>(index_size);
334 uint32_t cipher_bytes = 0;
335 decrypt_config->AddSubsample(clear_bytes, cipher_bytes);
340 bool EncryptionHandler::EncryptNalFrame(MediaSample* sample,
341 DecryptConfig* decrypt_config) {
342 const Nalu::CodecType nalu_type =
343 (video_codec_ == kCodecHVC1 || video_codec_ == kCodecHEV1) ? Nalu::kH265
345 NaluReader reader(nalu_type, nalu_length_size_, sample->writable_data(),
346 sample->data_size());
350 uint64_t accumulated_clear_bytes = 0;
353 NaluReader::Result result;
354 while ((result = reader.Advance(&nalu)) == NaluReader::kOk) {
355 if (nalu.is_video_slice()) {
359 const int64_t video_slice_header_size =
360 header_parser_ ? header_parser_->GetHeaderSize(nalu) : 0;
361 if (video_slice_header_size < 0) {
362 LOG(ERROR) <<
"Failed to read slice header.";
366 uint64_t current_clear_bytes =
367 nalu.header_size() + video_slice_header_size;
368 uint64_t cipher_bytes = nalu.payload_size() - video_slice_header_size;
375 const uint16_t misalign_bytes = cipher_bytes % kCencBlockSize;
376 current_clear_bytes += misalign_bytes;
377 cipher_bytes -= misalign_bytes;
380 const uint8_t* nalu_data = nalu.data() + current_clear_bytes;
381 EncryptBytes(const_cast<uint8_t*>(nalu_data), cipher_bytes);
384 accumulated_clear_bytes + nalu_length_size_ + current_clear_bytes,
385 cipher_bytes, decrypt_config);
386 accumulated_clear_bytes = 0;
389 accumulated_clear_bytes +=
390 nalu_length_size_ + nalu.header_size() + nalu.payload_size();
393 if (result != NaluReader::kEOStream) {
394 LOG(ERROR) <<
"Failed to parse NAL units.";
397 AddSubsample(accumulated_clear_bytes, 0, decrypt_config);
401 void EncryptionHandler::EncryptBytes(uint8_t* data,
size_t size) {
403 CHECK(encryptor_->Crypt(data, size, data));
406 void EncryptionHandler::InjectVpxParserForTesting(
407 std::unique_ptr<VPxParser> vpx_parser) {
408 vpx_parser_ = std::move(vpx_parser);
411 void EncryptionHandler::InjectVideoSliceHeaderParserForTesting(
412 std::unique_ptr<VideoSliceHeaderParser> header_parser) {
413 header_parser_ = std::move(header_parser);