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