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