DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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/key_source.h"
16 
17 namespace shaka {
18 namespace media {
19 
20 const uint8_t kWidevineSystemId[] = {0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6,
21  0x4a, 0xce, 0xa3, 0xc8, 0x27, 0xdc,
22  0xd5, 0x1d, 0x21, 0xed};
23 
24 class KeyFetcher;
25 class RequestSigner;
26 template <class T> class ProducerConsumerQueue;
27 
30 class WidevineKeySource : public KeySource {
31  public:
33  WidevineKeySource(const std::string& server_url, bool add_common_pssh);
34 
35  ~WidevineKeySource() override;
36 
39  Status FetchKeys(const std::vector<uint8_t>& pssh_box) override;
40  Status FetchKeys(const std::vector<std::vector<uint8_t>>& key_ids) override;
41  Status FetchKeys(uint32_t asset_id) override;
42 
43  Status GetKey(TrackType track_type, 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  TrackType track_type,
48  EncryptionKey* key) override;
50 
55  Status FetchKeys(const std::vector<uint8_t>& content_id,
56  const std::string& policy);
57 
60  void set_signer(std::unique_ptr<RequestSigner> signer);
61 
64  void set_key_fetcher(std::unique_ptr<KeyFetcher> key_fetcher);
65 
66  protected:
67  ClosureThread key_production_thread_;
68 
69  private:
70  typedef std::map<TrackType, std::unique_ptr<EncryptionKey>> EncryptionKeyMap;
73 
74  // Internal routine for getting keys.
75  Status GetKeyInternal(uint32_t crypto_period_index,
76  TrackType track_type,
77  EncryptionKey* key);
78 
79  // The closure task to fetch keys repeatedly.
80  void FetchKeysTask();
81 
82  // Fetch keys from server.
83  Status FetchKeysInternal(bool enable_key_rotation,
84  uint32_t first_crypto_period_index,
85  bool widevine_classic);
86 
87  // Fill |request| with necessary fields for Widevine encryption request.
88  // |request| should not be NULL.
89  void FillRequest(bool enable_key_rotation,
90  uint32_t first_crypto_period_index,
91  std::string* request);
92  // Base64 escape and format the request. Optionally sign the request if a
93  // signer is provided. |message| should not be NULL. Return OK on success.
94  Status GenerateKeyMessage(const std::string& request, std::string* message);
95  // Decode |response| from JSON formatted |raw_response|.
96  // |response| should not be NULL.
97  bool DecodeResponse(const std::string& raw_response, std::string* response);
98  // Extract encryption key from |response|, which is expected to be properly
99  // formatted. |transient_error| will be set to true if it fails and the
100  // failure is because of a transient error from the server. |transient_error|
101  // should not be NULL.
102  bool ExtractEncryptionKey(bool enable_key_rotation,
103  bool widevine_classic,
104  const std::string& response,
105  bool* transient_error);
106  // Push the keys to the key pool.
107  bool PushToKeyPool(EncryptionKeyMap* encryption_key_map);
108 
109  // The fetcher object used to fetch keys from the license service.
110  // It is initialized to a default fetcher on class initialization.
111  // Can be overridden using set_key_fetcher for testing or other purposes.
112  std::unique_ptr<KeyFetcher> key_fetcher_;
113  std::string server_url_;
114  std::unique_ptr<RequestSigner> signer_;
115  base::DictionaryValue request_dict_;
116 
117  const uint32_t crypto_period_count_;
118  base::Lock lock_;
119  bool add_common_pssh_;
120  bool key_production_started_;
121  base::WaitableEvent start_key_production_;
122  uint32_t first_crypto_period_index_;
123  std::unique_ptr<EncryptionKeyQueue> key_pool_;
124  EncryptionKeyMap encryption_key_map_; // For non key rotation request.
125  Status common_encryption_request_status_;
126 
127  DISALLOW_COPY_AND_ASSIGN(WidevineKeySource);
128 };
129 
130 } // namespace media
131 } // namespace shaka
132 
133 #endif // MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
Status FetchKeys(const std::vector< uint8_t > &pssh_box) override
Status GetCryptoPeriodKey(uint32_t crypto_period_index, TrackType track_type, EncryptionKey *key) override
Status GetKey(TrackType track_type, EncryptionKey *key) override
void set_key_fetcher(std::unique_ptr< KeyFetcher > key_fetcher)
WidevineKeySource(const std::string &server_url, bool add_common_pssh)
void set_signer(std::unique_ptr< RequestSigner > signer)
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30