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