2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
|
|
|
|
#define PACKAGER_MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
#include <map>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2022-11-04 22:46:41 +00:00
|
|
|
#include <thread>
|
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/synchronization/mutex.h>
|
|
|
|
#include <absl/synchronization/notification.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/classes.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/fourccs.h>
|
|
|
|
#include <packager/media/base/key_source.h>
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2018-05-04 21:31:33 +00:00
|
|
|
|
|
|
|
class CommonEncryptionRequest;
|
|
|
|
|
2013-12-17 00:52:13 +00:00
|
|
|
namespace media {
|
2016-06-24 00:22:37 +00:00
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
class KeyFetcher;
|
2014-01-14 04:52:05 +00:00
|
|
|
class RequestSigner;
|
2014-06-30 15:40:02 +00:00
|
|
|
template <class T> class ProducerConsumerQueue;
|
2014-01-14 04:52:05 +00:00
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
/// WidevineKeySource talks to the Widevine encryption service to
|
2014-04-16 01:09:32 +00:00
|
|
|
/// acquire the encryption keys.
|
2014-08-20 23:51:15 +00:00
|
|
|
class WidevineKeySource : public KeySource {
|
2013-12-17 00:52:13 +00:00
|
|
|
public:
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @param server_url is the Widevine common encryption server url.
|
2020-05-29 18:45:57 +00:00
|
|
|
/// @param protection_systems is the enum indicating which PSSH should
|
2017-09-12 19:22:39 +00:00
|
|
|
/// be included.
|
2018-08-07 21:43:42 +00:00
|
|
|
/// @param protection_scheme is the Protection Scheme to be used for
|
|
|
|
/// encryption. It needs to be signalled in Widevine PSSH. This
|
|
|
|
/// argument can be ignored if Widevine PSSH is not generated.
|
2017-09-12 19:22:39 +00:00
|
|
|
WidevineKeySource(const std::string& server_url,
|
2020-05-29 18:45:57 +00:00
|
|
|
ProtectionSystem protection_systems,
|
2018-08-07 21:43:42 +00:00
|
|
|
FourCC protection_scheme);
|
2014-08-20 23:51:15 +00:00
|
|
|
|
2015-07-22 23:40:45 +00:00
|
|
|
~WidevineKeySource() override;
|
2014-08-20 23:51:15 +00:00
|
|
|
|
|
|
|
/// @name KeySource implementation overrides.
|
2014-04-24 16:59:07 +00:00
|
|
|
/// @{
|
2017-04-04 19:43:41 +00:00
|
|
|
Status FetchKeys(EmeInitDataType init_data_type,
|
|
|
|
const std::vector<uint8_t>& init_data) override;
|
2017-06-13 21:54:12 +00:00
|
|
|
Status GetKey(const std::string& stream_label, EncryptionKey* key) override;
|
2015-07-22 23:40:45 +00:00
|
|
|
Status GetKey(const std::vector<uint8_t>& key_id,
|
|
|
|
EncryptionKey* key) override;
|
|
|
|
Status GetCryptoPeriodKey(uint32_t crypto_period_index,
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t crypto_period_duration_in_seconds,
|
2017-06-13 21:54:12 +00:00
|
|
|
const std::string& stream_label,
|
2015-07-22 23:40:45 +00:00
|
|
|
EncryptionKey* key) override;
|
2014-04-24 16:59:07 +00:00
|
|
|
/// @}
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2016-02-19 20:23:37 +00:00
|
|
|
/// Fetch keys for CENC from the key server.
|
|
|
|
/// @param content_id the unique id identify the content.
|
|
|
|
/// @param policy specifies the DRM content rights.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
|
|
|
Status FetchKeys(const std::vector<uint8_t>& content_id,
|
|
|
|
const std::string& policy);
|
|
|
|
|
2014-10-15 00:47:25 +00:00
|
|
|
/// Set signer for the key source.
|
|
|
|
/// @param signer signs the request message.
|
2016-08-17 17:41:40 +00:00
|
|
|
void set_signer(std::unique_ptr<RequestSigner> signer);
|
2014-10-15 00:47:25 +00:00
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
/// Inject an @b KeyFetcher object, mainly used for testing.
|
|
|
|
/// @param key_fetcher points to the @b KeyFetcher object to be injected.
|
2016-08-17 17:41:40 +00:00
|
|
|
void set_key_fetcher(std::unique_ptr<KeyFetcher> key_fetcher);
|
2014-01-24 18:46:46 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
/// Not protected by Mutex. Must be called before FetchKeys().
|
2018-05-11 00:19:28 +00:00
|
|
|
void set_group_id(const std::vector<uint8_t>& group_id) {
|
|
|
|
group_id_ = group_id;
|
|
|
|
}
|
2022-11-04 22:46:41 +00:00
|
|
|
|
|
|
|
/// Not protected by Mutex. Must be called before FetchKeys().
|
2018-05-11 00:19:28 +00:00
|
|
|
void set_enable_entitlement_license(bool enable_entitlement_license) {
|
|
|
|
enable_entitlement_license_ = enable_entitlement_license;
|
|
|
|
}
|
2017-07-18 21:30:02 +00:00
|
|
|
|
2013-12-17 00:52:13 +00:00
|
|
|
private:
|
2017-01-24 00:55:02 +00:00
|
|
|
typedef ProducerConsumerQueue<std::shared_ptr<EncryptionKeyMap>>
|
2014-06-30 15:40:02 +00:00
|
|
|
EncryptionKeyQueue;
|
2014-04-24 16:59:07 +00:00
|
|
|
|
|
|
|
// Internal routine for getting keys.
|
2014-09-30 21:52:21 +00:00
|
|
|
Status GetKeyInternal(uint32_t crypto_period_index,
|
2017-06-13 21:54:12 +00:00
|
|
|
const std::string& stream_label,
|
2014-04-24 16:59:07 +00:00
|
|
|
EncryptionKey* key);
|
|
|
|
|
|
|
|
// The closure task to fetch keys repeatedly.
|
|
|
|
void FetchKeysTask();
|
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
// Fetch keys from server.
|
2014-08-20 23:51:15 +00:00
|
|
|
Status FetchKeysInternal(bool enable_key_rotation,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t first_crypto_period_index,
|
2014-08-20 23:51:15 +00:00
|
|
|
bool widevine_classic);
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2013-12-17 00:52:13 +00:00
|
|
|
// Fill |request| with necessary fields for Widevine encryption request.
|
|
|
|
// |request| should not be NULL.
|
2014-08-20 23:51:15 +00:00
|
|
|
void FillRequest(bool enable_key_rotation,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t first_crypto_period_index,
|
2018-05-04 21:31:33 +00:00
|
|
|
CommonEncryptionRequest* request);
|
|
|
|
// Get request in JSON string. Optionally sign the request if a signer is
|
|
|
|
// provided. |message| should not be NULL. Return OK on success.
|
|
|
|
Status GenerateKeyMessage(const CommonEncryptionRequest& request,
|
|
|
|
std::string* message);
|
2013-12-17 00:52:13 +00:00
|
|
|
// Extract encryption key from |response|, which is expected to be properly
|
2014-01-14 18:36:41 +00:00
|
|
|
// formatted. |transient_error| will be set to true if it fails and the
|
|
|
|
// failure is because of a transient error from the server. |transient_error|
|
|
|
|
// should not be NULL.
|
2014-06-30 15:40:02 +00:00
|
|
|
bool ExtractEncryptionKey(bool enable_key_rotation,
|
2014-08-20 23:51:15 +00:00
|
|
|
bool widevine_classic,
|
|
|
|
const std::string& response,
|
|
|
|
bool* transient_error);
|
2014-04-24 16:59:07 +00:00
|
|
|
// Push the keys to the key pool.
|
|
|
|
bool PushToKeyPool(EncryptionKeyMap* encryption_key_map);
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
// Indicates whether Widevine protection system should be generated.
|
|
|
|
bool generate_widevine_protection_system_ = true;
|
|
|
|
|
2014-10-07 21:33:08 +00:00
|
|
|
// The fetcher object used to fetch keys from the license service.
|
2014-02-20 22:38:28 +00:00
|
|
|
// It is initialized to a default fetcher on class initialization.
|
2014-10-07 21:33:08 +00:00
|
|
|
// Can be overridden using set_key_fetcher for testing or other purposes.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<KeyFetcher> key_fetcher_;
|
2013-12-17 00:52:13 +00:00
|
|
|
std::string server_url_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<RequestSigner> signer_;
|
2018-05-04 21:31:33 +00:00
|
|
|
std::unique_ptr<CommonEncryptionRequest> common_encryption_request_;
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2018-05-04 21:31:33 +00:00
|
|
|
const int crypto_period_count_;
|
2018-08-10 20:31:17 +00:00
|
|
|
FourCC protection_scheme_ = FOURCC_NULL;
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::Mutex mutex_;
|
|
|
|
|
2018-08-10 20:31:17 +00:00
|
|
|
bool key_production_started_ = false;
|
2022-11-04 22:46:41 +00:00
|
|
|
absl::Notification start_key_production_;
|
2018-08-10 20:31:17 +00:00
|
|
|
uint32_t first_crypto_period_index_ = 0;
|
2021-08-04 18:56:44 +00:00
|
|
|
int32_t crypto_period_duration_in_seconds_ = 0;
|
2017-07-18 21:30:02 +00:00
|
|
|
std::vector<uint8_t> group_id_;
|
2018-08-10 20:31:17 +00:00
|
|
|
bool enable_entitlement_license_ = false;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<EncryptionKeyQueue> key_pool_;
|
2022-11-04 22:46:41 +00:00
|
|
|
|
2014-06-30 15:40:02 +00:00
|
|
|
EncryptionKeyMap encryption_key_map_; // For non key rotation request.
|
2014-04-24 16:59:07 +00:00
|
|
|
Status common_encryption_request_status_;
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
std::thread key_production_thread_;
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WidevineKeySource);
|
2013-12-17 00:52:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
|