Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
8 #define MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
9 
10 #include <map>
11 #include <memory>
12 #include "packager/base/synchronization/waitable_event.h"
13 #include "packager/base/values.h"
14 #include "packager/media/base/closure_thread.h"
15 #include "packager/media/base/fourccs.h"
16 #include "packager/media/base/key_source.h"
17 
18 namespace shaka {
19 namespace media {
20 
21 const uint8_t kWidevineSystemId[] = {0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6,
22  0x4a, 0xce, 0xa3, 0xc8, 0x27, 0xdc,
23  0xd5, 0x1d, 0x21, 0xed};
24 
25 class KeyFetcher;
26 class RequestSigner;
27 template <class T> class ProducerConsumerQueue;
28 
31 class WidevineKeySource : public KeySource {
32  public:
34  WidevineKeySource(const std::string& server_url, bool add_common_pssh);
35 
36  ~WidevineKeySource() override;
37 
40  Status FetchKeys(EmeInitDataType init_data_type,
41  const std::vector<uint8_t>& init_data) override;
42  Status GetKey(const std::string& stream_label, EncryptionKey* key) override;
43  Status GetKey(const std::vector<uint8_t>& key_id,
44  EncryptionKey* key) override;
45  Status GetCryptoPeriodKey(uint32_t crypto_period_index,
46  const std::string& stream_label,
47  EncryptionKey* key) override;
49 
54  Status FetchKeys(const std::vector<uint8_t>& content_id,
55  const std::string& policy);
56 
58  void set_protection_scheme(FourCC protection_scheme) {
59  protection_scheme_ = protection_scheme;
60  }
61 
64  void set_signer(std::unique_ptr<RequestSigner> signer);
65 
68  void set_key_fetcher(std::unique_ptr<KeyFetcher> key_fetcher);
69 
70  // Set the group id for the key source
71  // @param group_id group identifier
72  void set_group_id(const std::vector<uint8_t>& group_id);
73 
74  private:
75  typedef std::map<std::string, std::unique_ptr<EncryptionKey>>
76  EncryptionKeyMap;
78  EncryptionKeyQueue;
79 
80  // Internal routine for getting keys.
81  Status GetKeyInternal(uint32_t crypto_period_index,
82  const std::string& stream_label,
83  EncryptionKey* key);
84 
85  // The closure task to fetch keys repeatedly.
86  void FetchKeysTask();
87 
88  // Fetch keys from server.
89  Status FetchKeysInternal(bool enable_key_rotation,
90  uint32_t first_crypto_period_index,
91  bool widevine_classic);
92 
93  // Fill |request| with necessary fields for Widevine encryption request.
94  // |request| should not be NULL.
95  void FillRequest(bool enable_key_rotation,
96  uint32_t first_crypto_period_index,
97  std::string* request);
98  // Base64 escape and format the request. Optionally sign the request if a
99  // signer is provided. |message| should not be NULL. Return OK on success.
100  Status GenerateKeyMessage(const std::string& request, std::string* message);
101  // Decode |response| from JSON formatted |raw_response|.
102  // |response| should not be NULL.
103  bool DecodeResponse(const std::string& raw_response, std::string* response);
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  base::DictionaryValue request_dict_;
123 
124  const uint32_t crypto_period_count_;
125  FourCC protection_scheme_;
126  base::Lock lock_;
127  bool add_common_pssh_;
128  bool key_production_started_;
129  base::WaitableEvent start_key_production_;
130  uint32_t first_crypto_period_index_;
131  std::vector<uint8_t> group_id_;
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 // MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
void set_protection_scheme(FourCC protection_scheme)
Set the protection scheme for the key source.
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, bool add_common_pssh)
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:45
Status FetchKeys(EmeInitDataType init_data_type, const std::vector< uint8_t > &init_data) override