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 <memory>
12#include <string>
13#include <thread>
14#include <vector>
15
16#include <absl/synchronization/mutex.h>
17#include <absl/synchronization/notification.h>
18
19#include <packager/crypto_params.h>
20#include <packager/macros/classes.h>
21#include <packager/media/base/fourccs.h>
22#include <packager/media/base/key_source.h>
23#include <packager/status.h>
24
25namespace shaka {
26
27class CommonEncryptionRequest;
28
29namespace media {
30
31class KeyFetcher;
32class RequestSigner;
33template <class T>
34class ProducerConsumerQueue;
35
39 public:
46 WidevineKeySource(const std::string& server_url,
47 ProtectionSystem protection_systems,
48 FourCC protection_scheme);
49
50 ~WidevineKeySource() override;
51
54 Status FetchKeys(EmeInitDataType init_data_type,
55 const std::vector<uint8_t>& init_data) override;
56 Status GetKey(const std::string& stream_label, EncryptionKey* key) override;
57 Status GetKey(const std::vector<uint8_t>& key_id,
58 EncryptionKey* key) override;
59 Status GetCryptoPeriodKey(uint32_t crypto_period_index,
60 int32_t crypto_period_duration_in_seconds,
61 const std::string& stream_label,
62 EncryptionKey* key) override;
64
69 Status FetchKeys(const std::vector<uint8_t>& content_id,
70 const std::string& policy);
71
74 void set_signer(std::unique_ptr<RequestSigner> signer);
75
78 void set_key_fetcher(std::unique_ptr<KeyFetcher> key_fetcher);
79
81 void set_group_id(const std::vector<uint8_t>& group_id) {
82 group_id_ = group_id;
83 }
84
86 void set_enable_entitlement_license(bool enable_entitlement_license) {
87 enable_entitlement_license_ = enable_entitlement_license;
88 }
89
90 private:
92 EncryptionKeyQueue;
93
94 // Internal routine for getting keys.
95 Status GetKeyInternal(uint32_t crypto_period_index,
96 const std::string& stream_label,
97 EncryptionKey* key);
98
99 // The closure task to fetch keys repeatedly.
100 void FetchKeysTask();
101
102 // Fetch keys from server.
103 Status FetchKeysInternal(bool enable_key_rotation,
104 uint32_t first_crypto_period_index,
105 bool widevine_classic);
106
107 // Fill |request| with necessary fields for Widevine encryption request.
108 // |request| should not be NULL.
109 void FillRequest(bool enable_key_rotation,
110 uint32_t first_crypto_period_index,
111 CommonEncryptionRequest* request);
112 // Get request in JSON string. Optionally sign the request if a signer is
113 // provided. |message| should not be NULL. Return OK on success.
114 Status GenerateKeyMessage(const CommonEncryptionRequest& request,
115 std::string* message);
116 // Extract encryption key from |response|, which is expected to be properly
117 // formatted. |transient_error| will be set to true if it fails and the
118 // failure is because of a transient error from the server. |transient_error|
119 // should not be NULL.
120 bool ExtractEncryptionKey(bool enable_key_rotation,
121 bool widevine_classic,
122 const std::string& response,
123 bool* transient_error);
124 // Push the keys to the key pool.
125 bool PushToKeyPool(EncryptionKeyMap* encryption_key_map);
126
127 // Indicates whether Widevine protection system should be generated.
128 bool generate_widevine_protection_system_ = true;
129
130 // The fetcher object used to fetch keys from the license service.
131 // It is initialized to a default fetcher on class initialization.
132 // Can be overridden using set_key_fetcher for testing or other purposes.
133 std::unique_ptr<KeyFetcher> key_fetcher_;
134 std::string server_url_;
135 std::unique_ptr<RequestSigner> signer_;
136 std::unique_ptr<CommonEncryptionRequest> common_encryption_request_;
137
138 const int crypto_period_count_;
139 FourCC protection_scheme_ = FOURCC_NULL;
140 absl::Mutex mutex_;
141
142 bool key_production_started_ = false;
143 absl::Notification start_key_production_;
144 uint32_t first_crypto_period_index_ = 0;
145 int32_t crypto_period_duration_in_seconds_ = 0;
146 std::vector<uint8_t> group_id_;
147 bool enable_entitlement_license_ = false;
148 std::unique_ptr<EncryptionKeyQueue> key_pool_;
149
150 EncryptionKeyMap encryption_key_map_; // For non key rotation request.
151 Status common_encryption_request_status_;
152
153 std::thread key_production_thread_;
154
155 DISALLOW_COPY_AND_ASSIGN(WidevineKeySource);
156};
157
158} // namespace media
159} // namespace shaka
160
161#endif // PACKAGER_MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
KeySource is responsible for encryption key acquisition.
Definition key_source.h:56
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.