7 #include <packager/media/base/widevine_key_source.h>
12 #include <absl/base/internal/endian.h>
13 #include <absl/flags/flag.h>
14 #include <absl/log/check.h>
15 #include <absl/strings/escaping.h>
17 #include <packager/macros/logging.h>
18 #include <packager/media/base/http_key_fetcher.h>
19 #include <packager/media/base/producer_consumer_queue.h>
20 #include <packager/media/base/protection_system_ids.h>
21 #include <packager/media/base/protection_system_specific_info.h>
22 #include <packager/media/base/proto_json_util.h>
23 #include <packager/media/base/pssh_generator_util.h>
24 #include <packager/media/base/rcheck.h>
25 #include <packager/media/base/request_signer.h>
26 #include <packager/media/base/widevine_common_encryption.pb.h>
28 ABSL_FLAG(std::string,
31 "Specify the optional video feature, e.g. HDR.");
37 const bool kEnableKeyRotation =
true;
41 const int kNumTransientErrorRetries = 5;
42 const int kFirstRetryDelayMilliseconds = 1000;
46 const int kDefaultCryptoPeriodCount = 10;
47 const int kGetKeyTimeoutInSeconds = 5 * 60;
48 const int kKeyFetchTimeoutInSeconds = 60;
50 CommonEncryptionRequest::ProtectionScheme ToCommonEncryptionProtectionScheme(
51 FourCC protection_scheme) {
52 switch (protection_scheme) {
54 return CommonEncryptionRequest::CENC;
56 case kAppleSampleAesProtectionScheme:
58 return CommonEncryptionRequest::CBCS;
60 return CommonEncryptionRequest::CBC1;
62 return CommonEncryptionRequest::CENS;
64 LOG(WARNING) <<
"Ignore unrecognized protection scheme "
65 << FourCCToString(protection_scheme);
66 return CommonEncryptionRequest::UNSPECIFIED;
70 ProtectionSystemSpecificInfo ProtectionSystemInfoFromPsshProto(
71 const CommonEncryptionResponse::Track::Pssh& pssh_proto) {
72 PsshBoxBuilder pssh_builder;
73 pssh_builder.set_system_id(kWidevineSystemId, std::size(kWidevineSystemId));
75 if (pssh_proto.has_boxes()) {
76 return {pssh_builder.system_id(),
77 std::vector<uint8_t>(pssh_proto.boxes().begin(),
78 pssh_proto.boxes().end())};
80 pssh_builder.set_pssh_box_version(0);
81 const std::vector<uint8_t> pssh_data(pssh_proto.data().begin(),
82 pssh_proto.data().end());
83 pssh_builder.set_pssh_data(pssh_data);
84 return {pssh_builder.system_id(), pssh_builder.CreateBox()};
91 ProtectionSystem protection_systems,
92 FourCC protection_scheme)
94 : generate_widevine_protection_system_(
97 protection_systems == ProtectionSystem::kNone ||
98 has_flag(protection_systems, ProtectionSystem::kWidevine)),
100 server_url_(server_url),
101 crypto_period_count_(kDefaultCryptoPeriodCount),
102 protection_scheme_(protection_scheme),
103 key_production_thread_(
106 WidevineKeySource::~WidevineKeySource() {
111 if (!start_key_production_.HasBeenNotified())
112 start_key_production_.Notify();
113 key_production_thread_.join();
117 const std::string& policy) {
118 absl::MutexLock scoped_lock(&mutex_);
119 common_encryption_request_.reset(
new CommonEncryptionRequest);
120 common_encryption_request_->set_content_id(content_id.data(),
122 common_encryption_request_->set_policy(policy);
123 common_encryption_request_->set_protection_scheme(
124 ToCommonEncryptionProtectionScheme(protection_scheme_));
125 if (enable_entitlement_license_)
126 common_encryption_request_->set_enable_entitlement_license(
true);
128 return FetchKeysInternal(!kEnableKeyRotation, 0,
false);
132 const std::vector<uint8_t>& init_data) {
133 std::vector<uint8_t> pssh_data;
134 uint32_t asset_id = 0;
135 switch (init_data_type) {
136 case EmeInitDataType::CENC: {
137 const std::vector<uint8_t> widevine_system_id(
138 kWidevineSystemId, kWidevineSystemId + std::size(kWidevineSystemId));
139 std::vector<ProtectionSystemSpecificInfo> protection_systems_info;
141 init_data.data(), init_data.size(), &protection_systems_info)) {
142 return Status(error::PARSER_FAILURE,
"Error parsing the PSSH boxes.");
144 for (
const auto& info : protection_systems_info) {
145 std::unique_ptr<PsshBoxBuilder> pssh_builder =
148 return Status(error::PARSER_FAILURE,
"Error parsing the PSSH box.");
151 if (info.system_id == widevine_system_id) {
152 pssh_data = pssh_builder->pssh_data();
154 }
else if (pssh_data.empty() && !pssh_builder->key_ids().empty()) {
156 GenerateWidevinePsshDataFromKeyIds(pssh_builder->key_ids());
162 if (pssh_data.empty())
163 return Status(error::INVALID_ARGUMENT,
"No supported PSSHs found.");
166 case EmeInitDataType::WEBM: {
167 pssh_data = GenerateWidevinePsshDataFromKeyIds({init_data});
170 case EmeInitDataType::WIDEVINE_CLASSIC:
171 if (init_data.size() <
sizeof(asset_id))
172 return Status(error::INVALID_ARGUMENT,
"Invalid asset id.");
173 asset_id = absl::big_endian::Load32(init_data.data());
176 LOG(ERROR) <<
"Init data type " <<
static_cast<int>(init_data_type)
177 <<
" not supported.";
178 return Status(error::INVALID_ARGUMENT,
"Unsupported init data type.");
180 const bool widevine_classic =
181 init_data_type == EmeInitDataType::WIDEVINE_CLASSIC;
182 absl::MutexLock scoped_lock(&mutex_);
183 common_encryption_request_.reset(
new CommonEncryptionRequest);
184 if (widevine_classic) {
185 common_encryption_request_->set_asset_id(asset_id);
187 common_encryption_request_->set_pssh_data(pssh_data.data(),
190 return FetchKeysInternal(!kEnableKeyRotation, 0, widevine_classic);
196 if (encryption_key_map_.find(stream_label) == encryption_key_map_.end()) {
197 return Status(error::INTERNAL_ERROR,
198 "Cannot find key for '" + stream_label +
"'.");
200 *key = *encryption_key_map_[stream_label];
207 for (
const auto& pair : encryption_key_map_) {
208 if (pair.second->key_id == key_id) {
213 return Status(error::INTERNAL_ERROR,
214 "Cannot find key with specified key ID");
218 uint32_t crypto_period_index,
219 int32_t crypto_period_duration_in_seconds,
220 const std::string& stream_label,
224 absl::MutexLock scoped_lock(&mutex_);
225 if (!key_production_started_) {
226 crypto_period_duration_in_seconds_ = crypto_period_duration_in_seconds;
229 first_crypto_period_index_ =
230 crypto_period_index ? crypto_period_index - 1 : 0;
232 const size_t queue_size = crypto_period_count_ * 10;
235 start_key_production_.Notify();
236 key_production_started_ =
true;
237 }
else if (crypto_period_duration_in_seconds_ !=
238 crypto_period_duration_in_seconds) {
239 return Status(error::INVALID_ARGUMENT,
240 "Crypto period duration should not change.");
243 return GetKeyInternal(crypto_period_index, stream_label, key);
247 signer_ = std::move(signer);
251 std::unique_ptr<KeyFetcher> key_fetcher) {
252 key_fetcher_ = std::move(key_fetcher);
255 Status WidevineKeySource::GetKeyInternal(uint32_t crypto_period_index,
256 const std::string& stream_label,
261 std::shared_ptr<EncryptionKeyMap> encryption_key_map;
262 Status status = key_pool_->Peek(crypto_period_index, &encryption_key_map,
263 kGetKeyTimeoutInSeconds * 1000);
265 if (status.error_code() == error::STOPPED) {
266 CHECK(!common_encryption_request_status_.ok());
267 return common_encryption_request_status_;
272 if (encryption_key_map->find(stream_label) == encryption_key_map->end()) {
273 return Status(error::INTERNAL_ERROR,
274 "Cannot find key for '" + stream_label +
"'.");
276 *key = *encryption_key_map->at(stream_label);
280 void WidevineKeySource::FetchKeysTask() {
282 start_key_production_.WaitForNotification();
283 if (!key_pool_ || key_pool_->Stopped())
286 Status status = FetchKeysInternal(kEnableKeyRotation,
287 first_crypto_period_index_,
289 while (status.ok()) {
290 first_crypto_period_index_ += crypto_period_count_;
291 status = FetchKeysInternal(kEnableKeyRotation,
292 first_crypto_period_index_,
295 common_encryption_request_status_ = status;
299 Status WidevineKeySource::FetchKeysInternal(
bool enable_key_rotation,
300 uint32_t first_crypto_period_index,
301 bool widevine_classic) {
302 CommonEncryptionRequest request;
303 FillRequest(enable_key_rotation, first_crypto_period_index, &request);
306 Status status = GenerateKeyMessage(request, &message);
309 VLOG(1) <<
"Message: " << message;
311 std::string raw_response;
312 int64_t sleep_duration = kFirstRetryDelayMilliseconds;
316 for (
int i = 0; i < kNumTransientErrorRetries; ++i) {
317 status = key_fetcher_->FetchKeys(server_url_, message, &raw_response);
319 VLOG(1) <<
"Retry [" << i <<
"] Response:" << raw_response;
321 bool transient_error =
false;
322 if (ExtractEncryptionKey(enable_key_rotation, widevine_classic,
323 raw_response, &transient_error))
326 if (!transient_error) {
329 "Failed to extract encryption key from '" + raw_response +
"'.");
331 }
else if (status.error_code() != error::TIME_OUT) {
336 if (i != kNumTransientErrorRetries - 1) {
337 std::this_thread::sleep_for(std::chrono::milliseconds(sleep_duration));
341 return Status(error::SERVER_ERROR,
342 "Failed to recover from server internal error.");
345 void WidevineKeySource::FillRequest(
bool enable_key_rotation,
346 uint32_t first_crypto_period_index,
347 CommonEncryptionRequest* request) {
348 DCHECK(common_encryption_request_);
350 *request = *common_encryption_request_;
352 request->add_tracks()->set_type(
"SD");
353 request->add_tracks()->set_type(
"HD");
354 request->add_tracks()->set_type(
"UHD1");
355 request->add_tracks()->set_type(
"UHD2");
356 request->add_tracks()->set_type(
"AUDIO");
358 request->add_drm_types(ModularDrmType::WIDEVINE);
360 if (enable_key_rotation) {
361 request->set_first_crypto_period_index(first_crypto_period_index);
362 request->set_crypto_period_count(crypto_period_count_);
363 request->set_crypto_period_seconds(crypto_period_duration_in_seconds_);
366 if (!group_id_.empty())
367 request->set_group_id(group_id_.data(), group_id_.size());
369 std::string video_feature = absl::GetFlag(FLAGS_video_feature);
370 if (!video_feature.empty())
371 request->set_video_feature(video_feature);
374 Status WidevineKeySource::GenerateKeyMessage(
375 const CommonEncryptionRequest& request,
376 std::string* message) {
379 SignedModularDrmRequest signed_request;
380 signed_request.set_request(MessageToJsonString(request));
384 std::string signature;
385 if (!signer_->GenerateSignature(signed_request.request(), &signature))
386 return Status(error::INTERNAL_ERROR,
"Signature generation failed.");
388 signed_request.set_signature(signature);
389 signed_request.set_signer(signer_->signer_name());
392 *message = MessageToJsonString(signed_request);
396 bool WidevineKeySource::ExtractEncryptionKey(
397 bool enable_key_rotation,
398 bool widevine_classic,
399 const std::string& response,
400 bool* transient_error) {
401 DCHECK(transient_error);
402 *transient_error =
false;
404 SignedModularDrmResponse signed_response_proto;
405 if (!JsonStringToMessage(response, &signed_response_proto)) {
406 LOG(ERROR) <<
"Failed to convert JSON to proto: " << response;
410 CommonEncryptionResponse response_proto;
411 if (!JsonStringToMessage(signed_response_proto.response(), &response_proto)) {
412 LOG(ERROR) <<
"Failed to convert JSON to proto: "
413 << signed_response_proto.response();
417 if (response_proto.status() != CommonEncryptionResponse::OK) {
418 LOG(ERROR) <<
"Received non-OK license response: " << response;
422 (response_proto.status() == CommonEncryptionResponse::INTERNAL_ERROR);
426 RCHECK(enable_key_rotation
427 ? response_proto.tracks_size() >= crypto_period_count_
428 : response_proto.tracks_size() >= 1);
430 uint32_t current_crypto_period_index = first_crypto_period_index_;
432 std::vector<std::vector<uint8_t>> key_ids;
433 for (
const auto& track : response_proto.tracks()) {
434 if (!widevine_classic)
435 key_ids.emplace_back(track.key_id().begin(), track.key_id().end());
438 EncryptionKeyMap encryption_key_map;
439 for (
const auto& track : response_proto.tracks()) {
440 VLOG(2) <<
"track " << track.ShortDebugString();
442 if (enable_key_rotation) {
443 if (track.crypto_period_index() != current_crypto_period_index) {
444 if (track.crypto_period_index() != current_crypto_period_index + 1) {
445 LOG(ERROR) <<
"Expecting crypto period index "
446 << current_crypto_period_index <<
" or "
447 << current_crypto_period_index + 1 <<
"; Seen "
448 << track.crypto_period_index();
451 if (!PushToKeyPool(&encryption_key_map))
453 ++current_crypto_period_index;
457 const std::string& stream_label = track.type();
458 RCHECK(encryption_key_map.find(stream_label) == encryption_key_map.end());
460 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
461 encryption_key->key.assign(track.key().begin(), track.key().end());
464 if (!widevine_classic) {
465 encryption_key->key_id.assign(track.key_id().begin(),
466 track.key_id().end());
467 encryption_key->iv.assign(track.iv().begin(), track.iv().end());
468 encryption_key->key_ids = key_ids;
470 if (generate_widevine_protection_system_) {
471 if (track.pssh_size() != 1) {
472 LOG(ERROR) <<
"Expecting one and only one pssh, seeing "
473 << track.pssh_size();
476 encryption_key->key_system_info.push_back(
477 ProtectionSystemInfoFromPsshProto(track.pssh(0)));
480 encryption_key_map[stream_label] = std::move(encryption_key);
483 DCHECK(!encryption_key_map.empty());
484 if (!enable_key_rotation) {
486 for (
auto& pair : encryption_key_map)
487 encryption_key_map_[pair.first] = std::move(pair.second);
491 return PushToKeyPool(&encryption_key_map);
494 bool WidevineKeySource::PushToKeyPool(
495 EncryptionKeyMap* encryption_key_map) {
497 DCHECK(encryption_key_map);
498 auto encryption_key_map_shared = std::make_shared<EncryptionKeyMap>();
499 encryption_key_map_shared->swap(*encryption_key_map);
500 Status status = key_pool_->Push(encryption_key_map_shared, kInfiniteTimeout);
502 DCHECK_EQ(error::STOPPED, status.error_code());
All the methods that are virtual are virtual for mocking.