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