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