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:
34  WidevineKeySource(const std::string& server_url,
35  int protection_systems_flags);
36 
37  ~WidevineKeySource() override;
38 
41  Status FetchKeys(EmeInitDataType init_data_type,
42  const std::vector<uint8_t>& init_data) override;
43  Status GetKey(const std::string& stream_label, EncryptionKey* key) override;
44  Status GetKey(const std::vector<uint8_t>& key_id,
45  EncryptionKey* key) override;
46  Status GetCryptoPeriodKey(uint32_t crypto_period_index,
47  const std::string& stream_label,
48  EncryptionKey* key) override;
50 
55  Status FetchKeys(const std::vector<uint8_t>& content_id,
56  const std::string& policy);
57 
59  void set_protection_scheme(FourCC protection_scheme) {
60  protection_scheme_ = protection_scheme;
61  }
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  ClosureThread key_production_thread_;
116  // The fetcher object used to fetch keys from the license service.
117  // It is initialized to a default fetcher on class initialization.
118  // Can be overridden using set_key_fetcher for testing or other purposes.
119  std::unique_ptr<KeyFetcher> key_fetcher_;
120  std::string server_url_;
121  std::unique_ptr<RequestSigner> signer_;
122  std::unique_ptr<CommonEncryptionRequest> common_encryption_request_;
123 
124  const int crypto_period_count_;
125  FourCC protection_scheme_;
126  base::Lock lock_;
127  bool key_production_started_;
128  base::WaitableEvent start_key_production_;
129  uint32_t first_crypto_period_index_;
130  std::vector<uint8_t> group_id_;
131  bool enable_entitlement_license_;
132  std::unique_ptr<EncryptionKeyQueue> key_pool_;
133  EncryptionKeyMap encryption_key_map_; // For non key rotation request.
134  Status common_encryption_request_status_;
135 
136  DISALLOW_COPY_AND_ASSIGN(WidevineKeySource);
137 };
138 
139 } // namespace media
140 } // namespace shaka
141 
142 #endif // PACKAGER_MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
All the methods that are virtual are virtual for mocking.
void set_protection_scheme(FourCC protection_scheme)
Set the protection scheme for the key source.
WidevineKeySource(const std::string &server_url, int protection_systems_flags)
Status GetKey(const std::string &stream_label, EncryptionKey *key) override
void set_key_fetcher(std::unique_ptr< KeyFetcher > key_fetcher)
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:50
Status FetchKeys(EmeInitDataType init_data_type, const std::vector< uint8_t > &init_data) override