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