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  const std::string& stream_label,
52  EncryptionKey* key) override;
54 
59  Status FetchKeys(const std::vector<uint8_t>& content_id,
60  const std::string& policy);
61 
64  void set_signer(std::unique_ptr<RequestSigner> signer);
65 
68  void set_key_fetcher(std::unique_ptr<KeyFetcher> key_fetcher);
69 
70  void set_group_id(const std::vector<uint8_t>& group_id) {
71  group_id_ = group_id;
72  }
73  void set_enable_entitlement_license(bool enable_entitlement_license) {
74  enable_entitlement_license_ = enable_entitlement_license;
75  }
76 
77  private:
80 
81  // Internal routine for getting keys.
82  Status GetKeyInternal(uint32_t crypto_period_index,
83  const std::string& stream_label,
84  EncryptionKey* key);
85 
86  // The closure task to fetch keys repeatedly.
87  void FetchKeysTask();
88 
89  // Fetch keys from server.
90  Status FetchKeysInternal(bool enable_key_rotation,
91  uint32_t first_crypto_period_index,
92  bool widevine_classic);
93 
94  // Fill |request| with necessary fields for Widevine encryption request.
95  // |request| should not be NULL.
96  void FillRequest(bool enable_key_rotation,
97  uint32_t first_crypto_period_index,
98  CommonEncryptionRequest* request);
99  // Get request in JSON string. Optionally sign the request if a signer is
100  // provided. |message| should not be NULL. Return OK on success.
101  Status GenerateKeyMessage(const CommonEncryptionRequest& request,
102  std::string* message);
103  // Extract encryption key from |response|, which is expected to be properly
104  // formatted. |transient_error| will be set to true if it fails and the
105  // failure is because of a transient error from the server. |transient_error|
106  // should not be NULL.
107  bool ExtractEncryptionKey(bool enable_key_rotation,
108  bool widevine_classic,
109  const std::string& response,
110  bool* transient_error);
111  // Push the keys to the key pool.
112  bool PushToKeyPool(EncryptionKeyMap* encryption_key_map);
113 
114  // Indicates whether Widevine protection system should be generated.
115  bool generate_widevine_protection_system_ = true;
116 
117  ClosureThread key_production_thread_;
118  // The fetcher object used to fetch keys from the license service.
119  // It is initialized to a default fetcher on class initialization.
120  // Can be overridden using set_key_fetcher for testing or other purposes.
121  std::unique_ptr<KeyFetcher> key_fetcher_;
122  std::string server_url_;
123  std::unique_ptr<RequestSigner> signer_;
124  std::unique_ptr<CommonEncryptionRequest> common_encryption_request_;
125 
126  const int crypto_period_count_;
127  FourCC protection_scheme_ = FOURCC_NULL;
128  base::Lock lock_;
129  bool key_production_started_ = false;
130  base::WaitableEvent start_key_production_;
131  uint32_t first_crypto_period_index_ = 0;
132  std::vector<uint8_t> group_id_;
133  bool enable_entitlement_license_ = false;
134  std::unique_ptr<EncryptionKeyQueue> key_pool_;
135  EncryptionKeyMap encryption_key_map_; // For non key rotation request.
136  Status common_encryption_request_status_;
137 
138  DISALLOW_COPY_AND_ASSIGN(WidevineKeySource);
139 };
140 
141 } // namespace media
142 } // namespace shaka
143 
144 #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)
Status GetCryptoPeriodKey(uint32_t crypto_period_index, const std::string &stream_label, EncryptionKey *key) override
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