7 #include "packager/media/base/widevine_key_source.h" 9 #include <gflags/gflags.h> 11 #include "packager/base/base64.h" 12 #include "packager/base/bind.h" 13 #include "packager/base/strings/string_number_conversions.h" 14 #include "packager/media/base/http_key_fetcher.h" 15 #include "packager/media/base/network_util.h" 16 #include "packager/media/base/producer_consumer_queue.h" 17 #include "packager/media/base/protection_system_ids.h" 18 #include "packager/media/base/protection_system_specific_info.h" 19 #include "packager/media/base/proto_json_util.h" 20 #include "packager/media/base/pssh_generator_util.h" 21 #include "packager/media/base/rcheck.h" 22 #include "packager/media/base/request_signer.h" 23 #include "packager/media/base/widevine_common_encryption.pb.h" 25 DEFINE_string(video_feature,
27 "Specify the optional video feature, e.g. HDR.");
33 const bool kEnableKeyRotation =
true;
37 const int kNumTransientErrorRetries = 5;
38 const int kFirstRetryDelayMilliseconds = 1000;
42 const int kDefaultCryptoPeriodCount = 10;
43 const int kGetKeyTimeoutInSeconds = 5 * 60;
44 const int kKeyFetchTimeoutInSeconds = 60;
46 CommonEncryptionRequest::ProtectionScheme ToCommonEncryptionProtectionScheme(
47 FourCC protection_scheme) {
48 switch (protection_scheme) {
50 return CommonEncryptionRequest::CENC;
52 case kAppleSampleAesProtectionScheme:
54 return CommonEncryptionRequest::CBCS;
56 return CommonEncryptionRequest::CBC1;
58 return CommonEncryptionRequest::CENS;
60 LOG(WARNING) <<
"Ignore unrecognized protection scheme " 61 << FourCCToString(protection_scheme);
62 return CommonEncryptionRequest::UNSPECIFIED;
66 ProtectionSystemSpecificInfo ProtectionSystemInfoFromPsshProto(
67 const CommonEncryptionResponse::Track::Pssh& pssh_proto) {
68 PsshBoxBuilder pssh_builder;
69 pssh_builder.set_system_id(kWidevineSystemId, arraysize(kWidevineSystemId));
71 if (pssh_proto.has_boxes()) {
72 return {pssh_builder.system_id(),
73 std::vector<uint8_t>(pssh_proto.boxes().begin(),
74 pssh_proto.boxes().end())};
76 pssh_builder.set_pssh_box_version(0);
77 const std::vector<uint8_t> pssh_data(pssh_proto.data().begin(),
78 pssh_proto.data().end());
79 pssh_builder.set_pssh_data(pssh_data);
80 return {pssh_builder.system_id(), pssh_builder.CreateBox()};
87 int protection_system_flags,
88 FourCC protection_scheme)
90 :
KeySource(protection_system_flags & ~WIDEVINE_PROTECTION_SYSTEM_FLAG,
92 generate_widevine_protection_system_(
95 protection_system_flags == NO_PROTECTION_SYSTEM_FLAG ||
96 protection_system_flags & WIDEVINE_PROTECTION_SYSTEM_FLAG),
97 key_production_thread_(
"KeyProductionThread",
99 base::Unretained(this))),
101 server_url_(server_url),
102 crypto_period_count_(kDefaultCryptoPeriodCount),
103 protection_scheme_(protection_scheme),
104 start_key_production_(
base::WaitableEvent::ResetPolicy::AUTOMATIC,
105 base::WaitableEvent::InitialState::NOT_SIGNALED) {
106 key_production_thread_.Start();
109 WidevineKeySource::~WidevineKeySource() {
112 if (key_production_thread_.HasBeenStarted()) {
115 start_key_production_.Signal();
116 key_production_thread_.Join();
121 const std::string& policy) {
122 base::AutoLock scoped_lock(lock_);
123 common_encryption_request_.reset(
new CommonEncryptionRequest);
124 common_encryption_request_->set_content_id(content_id.data(),
126 common_encryption_request_->set_policy(policy);
127 common_encryption_request_->set_protection_scheme(
128 ToCommonEncryptionProtectionScheme(protection_scheme_));
129 if (enable_entitlement_license_)
130 common_encryption_request_->set_enable_entitlement_license(
true);
132 return FetchKeysInternal(!kEnableKeyRotation, 0,
false);
136 const std::vector<uint8_t>& init_data) {
137 std::vector<uint8_t> pssh_data;
138 uint32_t asset_id = 0;
139 switch (init_data_type) {
140 case EmeInitDataType::CENC: {
141 const std::vector<uint8_t> widevine_system_id(
142 kWidevineSystemId, kWidevineSystemId + arraysize(kWidevineSystemId));
143 std::vector<ProtectionSystemSpecificInfo> protection_systems_info;
145 init_data.data(), init_data.size(), &protection_systems_info)) {
146 return Status(error::PARSER_FAILURE,
"Error parsing the PSSH boxes.");
148 for (
const auto& info : protection_systems_info) {
149 std::unique_ptr<PsshBoxBuilder> pssh_builder =
152 return Status(error::PARSER_FAILURE,
"Error parsing the PSSH box.");
155 if (info.system_id == widevine_system_id) {
156 pssh_data = pssh_builder->pssh_data();
158 }
else if (pssh_data.empty() && !pssh_builder->key_ids().empty()) {
160 GenerateWidevinePsshDataFromKeyIds(pssh_builder->key_ids());
166 if (pssh_data.empty())
167 return Status(error::INVALID_ARGUMENT,
"No supported PSSHs found.");
170 case EmeInitDataType::WEBM: {
171 pssh_data = GenerateWidevinePsshDataFromKeyIds({init_data});
174 case EmeInitDataType::WIDEVINE_CLASSIC:
175 if (init_data.size() <
sizeof(asset_id))
176 return Status(error::INVALID_ARGUMENT,
"Invalid asset id.");
177 asset_id = ntohlFromBuffer(init_data.data());
180 LOG(ERROR) <<
"Init data type " <<
static_cast<int>(init_data_type)
181 <<
" not supported.";
182 return Status(error::INVALID_ARGUMENT,
"Unsupported init data type.");
184 const bool widevine_classic =
185 init_data_type == EmeInitDataType::WIDEVINE_CLASSIC;
186 base::AutoLock scoped_lock(lock_);
187 common_encryption_request_.reset(
new CommonEncryptionRequest);
188 if (widevine_classic) {
189 common_encryption_request_->set_asset_id(asset_id);
191 common_encryption_request_->set_pssh_data(pssh_data.data(),
194 return FetchKeysInternal(!kEnableKeyRotation, 0, widevine_classic);
200 if (encryption_key_map_.find(stream_label) == encryption_key_map_.end()) {
201 return Status(error::INTERNAL_ERROR,
202 "Cannot find key for '" + stream_label +
"'.");
204 *key = *encryption_key_map_[stream_label];
211 for (
const auto& pair : encryption_key_map_) {
212 if (pair.second->key_id == key_id) {
217 return Status(error::INTERNAL_ERROR,
218 "Cannot find key with specified key ID");
222 uint32_t crypto_period_duration_in_seconds,
223 const std::string& stream_label,
225 DCHECK(key_production_thread_.HasBeenStarted());
228 base::AutoLock scoped_lock(lock_);
229 if (!key_production_started_) {
230 crypto_period_duration_in_seconds_ = crypto_period_duration_in_seconds;
233 first_crypto_period_index_ =
234 crypto_period_index ? crypto_period_index - 1 : 0;
236 const size_t queue_size = crypto_period_count_ * 10;
239 start_key_production_.Signal();
240 key_production_started_ =
true;
241 }
else if (crypto_period_duration_in_seconds_ !=
242 crypto_period_duration_in_seconds) {
243 return Status(error::INVALID_ARGUMENT,
244 "Crypto period duration should not change.");
247 return GetKeyInternal(crypto_period_index, stream_label, key);
251 signer_ = std::move(signer);
255 std::unique_ptr<KeyFetcher> key_fetcher) {
256 key_fetcher_ = std::move(key_fetcher);
259 Status WidevineKeySource::GetKeyInternal(uint32_t crypto_period_index,
260 const std::string& stream_label,
265 std::shared_ptr<EncryptionKeyMap> encryption_key_map;
266 Status status = key_pool_->Peek(crypto_period_index, &encryption_key_map,
267 kGetKeyTimeoutInSeconds * 1000);
269 if (status.error_code() == error::STOPPED) {
270 CHECK(!common_encryption_request_status_.ok());
271 return common_encryption_request_status_;
276 if (encryption_key_map->find(stream_label) == encryption_key_map->end()) {
277 return Status(error::INTERNAL_ERROR,
278 "Cannot find key for '" + stream_label +
"'.");
280 *key = *encryption_key_map->at(stream_label);
284 void WidevineKeySource::FetchKeysTask() {
286 start_key_production_.Wait();
287 if (!key_pool_ || key_pool_->Stopped())
290 Status status = FetchKeysInternal(kEnableKeyRotation,
291 first_crypto_period_index_,
293 while (status.ok()) {
294 first_crypto_period_index_ += crypto_period_count_;
295 status = FetchKeysInternal(kEnableKeyRotation,
296 first_crypto_period_index_,
299 common_encryption_request_status_ = status;
303 Status WidevineKeySource::FetchKeysInternal(
bool enable_key_rotation,
304 uint32_t first_crypto_period_index,
305 bool widevine_classic) {
306 CommonEncryptionRequest request;
307 FillRequest(enable_key_rotation, first_crypto_period_index, &request);
310 Status status = GenerateKeyMessage(request, &message);
313 VLOG(1) <<
"Message: " << message;
315 std::string raw_response;
316 int64_t sleep_duration = kFirstRetryDelayMilliseconds;
320 for (
int i = 0; i < kNumTransientErrorRetries; ++i) {
321 status = key_fetcher_->FetchKeys(server_url_, message, &raw_response);
323 VLOG(1) <<
"Retry [" << i <<
"] Response:" << raw_response;
325 bool transient_error =
false;
326 if (ExtractEncryptionKey(enable_key_rotation, widevine_classic,
327 raw_response, &transient_error))
330 if (!transient_error) {
333 "Failed to extract encryption key from '" + raw_response +
"'.");
335 }
else if (status.error_code() != error::TIME_OUT) {
340 if (i != kNumTransientErrorRetries - 1) {
341 base::PlatformThread::Sleep(
342 base::TimeDelta::FromMilliseconds(sleep_duration));
346 return Status(error::SERVER_ERROR,
347 "Failed to recover from server internal error.");
350 void WidevineKeySource::FillRequest(
bool enable_key_rotation,
351 uint32_t first_crypto_period_index,
352 CommonEncryptionRequest* request) {
353 DCHECK(common_encryption_request_);
355 *request = *common_encryption_request_;
357 request->add_tracks()->set_type(
"SD");
358 request->add_tracks()->set_type(
"HD");
359 request->add_tracks()->set_type(
"UHD1");
360 request->add_tracks()->set_type(
"UHD2");
361 request->add_tracks()->set_type(
"AUDIO");
363 request->add_drm_types(ModularDrmType::WIDEVINE);
365 if (enable_key_rotation) {
366 request->set_first_crypto_period_index(first_crypto_period_index);
367 request->set_crypto_period_count(crypto_period_count_);
368 request->set_crypto_period_seconds(crypto_period_duration_in_seconds_);
371 if (!group_id_.empty())
372 request->set_group_id(group_id_.data(), group_id_.size());
374 if (!FLAGS_video_feature.empty())
375 request->set_video_feature(FLAGS_video_feature);
378 Status WidevineKeySource::GenerateKeyMessage(
379 const CommonEncryptionRequest& request,
380 std::string* message) {
383 SignedModularDrmRequest signed_request;
384 signed_request.set_request(MessageToJsonString(request));
388 std::string signature;
389 if (!signer_->GenerateSignature(signed_request.request(), &signature))
390 return Status(error::INTERNAL_ERROR,
"Signature generation failed.");
392 signed_request.set_signature(signature);
393 signed_request.set_signer(signer_->signer_name());
396 *message = MessageToJsonString(signed_request);
400 bool WidevineKeySource::ExtractEncryptionKey(
401 bool enable_key_rotation,
402 bool widevine_classic,
403 const std::string& response,
404 bool* transient_error) {
405 DCHECK(transient_error);
406 *transient_error =
false;
408 SignedModularDrmResponse signed_response_proto;
409 if (!JsonStringToMessage(response, &signed_response_proto)) {
410 LOG(ERROR) <<
"Failed to convert JSON to proto: " << response;
414 CommonEncryptionResponse response_proto;
415 if (!JsonStringToMessage(signed_response_proto.response(), &response_proto)) {
416 LOG(ERROR) <<
"Failed to convert JSON to proto: " 417 << signed_response_proto.response();
421 if (response_proto.status() != CommonEncryptionResponse::OK) {
422 LOG(ERROR) <<
"Received non-OK license response: " << response;
426 (response_proto.status() == CommonEncryptionResponse::INTERNAL_ERROR);
430 RCHECK(enable_key_rotation
431 ? response_proto.tracks_size() >= crypto_period_count_
432 : response_proto.tracks_size() >= 1);
434 uint32_t current_crypto_period_index = first_crypto_period_index_;
436 EncryptionKeyMap encryption_key_map;
437 for (
const auto& track : response_proto.tracks()) {
438 VLOG(2) <<
"track " << track.ShortDebugString();
440 if (enable_key_rotation) {
441 if (track.crypto_period_index() != current_crypto_period_index) {
442 if (track.crypto_period_index() != current_crypto_period_index + 1) {
443 LOG(ERROR) <<
"Expecting crypto period index " 444 << current_crypto_period_index <<
" or " 445 << current_crypto_period_index + 1 <<
"; Seen " 446 << track.crypto_period_index();
449 if (!PushToKeyPool(&encryption_key_map))
451 ++current_crypto_period_index;
455 const std::string& stream_label = track.type();
456 RCHECK(encryption_key_map.find(stream_label) == encryption_key_map.end());
458 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
459 encryption_key->key.assign(track.key().begin(), track.key().end());
462 if (!widevine_classic) {
463 encryption_key->key_id.assign(track.key_id().begin(),
464 track.key_id().end());
465 encryption_key->iv.assign(track.iv().begin(), track.iv().end());
467 if (generate_widevine_protection_system_) {
468 if (track.pssh_size() != 1) {
469 LOG(ERROR) <<
"Expecting one and only one pssh, seeing " 470 << track.pssh_size();
473 encryption_key->key_system_info.push_back(
474 ProtectionSystemInfoFromPsshProto(track.pssh(0)));
477 encryption_key_map[stream_label] = std::move(encryption_key);
480 if (!widevine_classic) {
486 DCHECK(!encryption_key_map.empty());
487 if (!enable_key_rotation) {
489 for (
auto& pair : encryption_key_map)
490 encryption_key_map_[pair.first] = std::move(pair.second);
493 return PushToKeyPool(&encryption_key_map);
496 bool WidevineKeySource::PushToKeyPool(
497 EncryptionKeyMap* encryption_key_map) {
499 DCHECK(encryption_key_map);
500 auto encryption_key_map_shared = std::make_shared<EncryptionKeyMap>();
501 encryption_key_map_shared->swap(*encryption_key_map);
502 Status status = key_pool_->Push(encryption_key_map_shared, kInfiniteTimeout);
504 DCHECK_EQ(error::STOPPED, status.error_code());
All the methods that are virtual are virtual for mocking.