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 
12 #include "packager/base/memory/scoped_ptr.h"
13 #include "packager/base/synchronization/waitable_event.h"
14 #include "packager/base/values.h"
15 #include "packager/media/base/closure_thread.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(const std::vector<uint8_t>& pssh_box) override;
41  Status FetchKeys(const std::vector<std::vector<uint8_t>>& key_ids) override;
42  Status FetchKeys(uint32_t asset_id) override;
43 
44  Status GetKey(TrackType track_type, EncryptionKey* key) override;
45  Status GetKey(const std::vector<uint8_t>& key_id,
46  EncryptionKey* key) override;
47  Status GetCryptoPeriodKey(uint32_t crypto_period_index,
48  TrackType track_type,
49  EncryptionKey* key) override;
51 
56  Status FetchKeys(const std::vector<uint8_t>& content_id,
57  const std::string& policy);
58 
61  void set_signer(scoped_ptr<RequestSigner> signer);
62 
65  void set_key_fetcher(scoped_ptr<KeyFetcher> key_fetcher);
66 
67  protected:
68  ClosureThread key_production_thread_;
69 
70  private:
71  typedef std::map<TrackType, EncryptionKey*> EncryptionKeyMap;
72  class RefCountedEncryptionKeyMap;
75 
76  // Internal routine for getting keys.
77  Status GetKeyInternal(uint32_t crypto_period_index,
78  TrackType track_type,
79  EncryptionKey* key);
80 
81  // The closure task to fetch keys repeatedly.
82  void FetchKeysTask();
83 
84  // Fetch keys from server.
85  Status FetchKeysInternal(bool enable_key_rotation,
86  uint32_t first_crypto_period_index,
87  bool widevine_classic);
88 
89  // Fill |request| with necessary fields for Widevine encryption request.
90  // |request| should not be NULL.
91  void FillRequest(bool enable_key_rotation,
92  uint32_t first_crypto_period_index,
93  std::string* request);
94  // Base64 escape and format the request. Optionally sign the request if a
95  // signer is provided. |message| should not be NULL. Return OK on success.
96  Status GenerateKeyMessage(const std::string& request, std::string* message);
97  // Decode |response| from JSON formatted |raw_response|.
98  // |response| should not be NULL.
99  bool DecodeResponse(const std::string& raw_response, std::string* response);
100  // Extract encryption key from |response|, which is expected to be properly
101  // formatted. |transient_error| will be set to true if it fails and the
102  // failure is because of a transient error from the server. |transient_error|
103  // should not be NULL.
104  bool ExtractEncryptionKey(bool enable_key_rotation,
105  bool widevine_classic,
106  const std::string& response,
107  bool* transient_error);
108  // Push the keys to the key pool.
109  bool PushToKeyPool(EncryptionKeyMap* encryption_key_map);
110 
111  // The fetcher object used to fetch keys from the license service.
112  // It is initialized to a default fetcher on class initialization.
113  // Can be overridden using set_key_fetcher for testing or other purposes.
114  scoped_ptr<KeyFetcher> key_fetcher_;
115  std::string server_url_;
116  scoped_ptr<RequestSigner> signer_;
117  base::DictionaryValue request_dict_;
118 
119  const uint32_t crypto_period_count_;
120  base::Lock lock_;
121  bool add_common_pssh_;
122  bool key_production_started_;
123  base::WaitableEvent start_key_production_;
124  uint32_t first_crypto_period_index_;
125  scoped_ptr<EncryptionKeyQueue> key_pool_;
126  EncryptionKeyMap encryption_key_map_; // For non key rotation request.
127  Status common_encryption_request_status_;
128 
129  DISALLOW_COPY_AND_ASSIGN(WidevineKeySource);
130 };
131 
132 } // namespace media
133 } // namespace shaka
134 
135 #endif // MEDIA_BASE_WIDEVINE_KEY_SOURCE_H_
void set_signer(scoped_ptr< RequestSigner > signer)
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(scoped_ptr< KeyFetcher > key_fetcher)
WidevineKeySource(const std::string &server_url, bool add_common_pssh)
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:31