Shaka Packager SDK
Loading...
Searching...
No Matches
widevine_key_source.h
1// Copyright 2014 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#ifndef PACKAGER_MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
8#define PACKAGER_MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
9
10#include <cstdint>
11#include <map>
12#include <memory>
13#include <thread>
14
15#include <absl/synchronization/mutex.h>
16#include <absl/synchronization/notification.h>
17
18#include <packager/macros/classes.h>
19#include <packager/media/base/fourccs.h>
20#include <packager/media/base/key_source.h>
21
22namespace shaka {
23
24class CommonEncryptionRequest;
25
26namespace media {
27
28class KeyFetcher;
29class RequestSigner;
30template <class T> class ProducerConsumerQueue;
31
35 public:
42 WidevineKeySource(const std::string& server_url,
43 ProtectionSystem protection_systems,
44 FourCC protection_scheme);
45
46 ~WidevineKeySource() override;
47
50 Status FetchKeys(EmeInitDataType init_data_type,
51 const std::vector<uint8_t>& init_data) override;
52 Status GetKey(const std::string& stream_label, EncryptionKey* key) override;
53 Status GetKey(const std::vector<uint8_t>& key_id,
54 EncryptionKey* key) override;
55 Status GetCryptoPeriodKey(uint32_t crypto_period_index,
56 int32_t crypto_period_duration_in_seconds,
57 const std::string& stream_label,
58 EncryptionKey* key) override;
60
65 Status FetchKeys(const std::vector<uint8_t>& content_id,
66 const std::string& policy);
67
70 void set_signer(std::unique_ptr<RequestSigner> signer);
71
74 void set_key_fetcher(std::unique_ptr<KeyFetcher> key_fetcher);
75
77 void set_group_id(const std::vector<uint8_t>& group_id) {
78 group_id_ = group_id;
79 }
80
82 void set_enable_entitlement_license(bool enable_entitlement_license) {
83 enable_entitlement_license_ = enable_entitlement_license;
84 }
85
86 private:
88 EncryptionKeyQueue;
89
90 // Internal routine for getting keys.
91 Status GetKeyInternal(uint32_t crypto_period_index,
92 const std::string& stream_label,
93 EncryptionKey* key);
94
95 // The closure task to fetch keys repeatedly.
96 void FetchKeysTask();
97
98 // Fetch keys from server.
99 Status FetchKeysInternal(bool enable_key_rotation,
100 uint32_t first_crypto_period_index,
101 bool widevine_classic);
102
103 // Fill |request| with necessary fields for Widevine encryption request.
104 // |request| should not be NULL.
105 void FillRequest(bool enable_key_rotation,
106 uint32_t first_crypto_period_index,
107 CommonEncryptionRequest* request);
108 // Get request in JSON string. Optionally sign the request if a signer is
109 // provided. |message| should not be NULL. Return OK on success.
110 Status GenerateKeyMessage(const CommonEncryptionRequest& request,
111 std::string* message);
112 // Extract encryption key from |response|, which is expected to be properly
113 // formatted. |transient_error| will be set to true if it fails and the
114 // failure is because of a transient error from the server. |transient_error|
115 // should not be NULL.
116 bool ExtractEncryptionKey(bool enable_key_rotation,
117 bool widevine_classic,
118 const std::string& response,
119 bool* transient_error);
120 // Push the keys to the key pool.
121 bool PushToKeyPool(EncryptionKeyMap* encryption_key_map);
122
123 // Indicates whether Widevine protection system should be generated.
124 bool generate_widevine_protection_system_ = true;
125
126 // The fetcher object used to fetch keys from the license service.
127 // It is initialized to a default fetcher on class initialization.
128 // Can be overridden using set_key_fetcher for testing or other purposes.
129 std::unique_ptr<KeyFetcher> key_fetcher_;
130 std::string server_url_;
131 std::unique_ptr<RequestSigner> signer_;
132 std::unique_ptr<CommonEncryptionRequest> common_encryption_request_;
133
134 const int crypto_period_count_;
135 FourCC protection_scheme_ = FOURCC_NULL;
136 absl::Mutex mutex_;
137
138 bool key_production_started_ = false;
139 absl::Notification start_key_production_;
140 uint32_t first_crypto_period_index_ = 0;
141 int32_t crypto_period_duration_in_seconds_ = 0;
142 std::vector<uint8_t> group_id_;
143 bool enable_entitlement_license_ = false;
144 std::unique_ptr<EncryptionKeyQueue> key_pool_;
145
146 EncryptionKeyMap encryption_key_map_; // For non key rotation request.
147 Status common_encryption_request_status_;
148
149 std::thread key_production_thread_;
150
151 DISALLOW_COPY_AND_ASSIGN(WidevineKeySource);
152};
153
154} // namespace media
155} // namespace shaka
156
157#endif // PACKAGER_MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
KeySource is responsible for encryption key acquisition.
Definition key_source.h:53
Status GetCryptoPeriodKey(uint32_t crypto_period_index, int32_t crypto_period_duration_in_seconds, const std::string &stream_label, EncryptionKey *key) override
void set_enable_entitlement_license(bool enable_entitlement_license)
Not protected by Mutex. Must be called before FetchKeys().
void set_signer(std::unique_ptr< RequestSigner > signer)
void set_key_fetcher(std::unique_ptr< KeyFetcher > key_fetcher)
Status GetKey(const std::string &stream_label, EncryptionKey *key) override
Status FetchKeys(EmeInitDataType init_data_type, const std::vector< uint8_t > &init_data) override
void set_group_id(const std::vector< uint8_t > &group_id)
Not protected by Mutex. Must be called before FetchKeys().
All the methods that are virtual are virtual for mocking.