2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
#ifndef MEDIA_BASE_WIDEVINE_ENCRYPTION_KEY_SOURCE_H_
|
|
|
|
#define MEDIA_BASE_WIDEVINE_ENCRYPTION_KEY_SOURCE_H_
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
#include <map>
|
|
|
|
|
2014-01-14 04:52:05 +00:00
|
|
|
#include "base/basictypes.h"
|
2014-02-20 22:38:28 +00:00
|
|
|
#include "base/memory/scoped_ptr.h"
|
2014-04-24 16:59:07 +00:00
|
|
|
#include "media/base/closure_thread.h"
|
2014-04-16 01:09:32 +00:00
|
|
|
#include "media/base/encryption_key_source.h"
|
2014-04-24 16:59:07 +00:00
|
|
|
#include "media/base/producer_consumer_queue.h"
|
2013-12-17 00:52:13 +00:00
|
|
|
|
|
|
|
namespace media {
|
2014-04-24 16:59:07 +00:00
|
|
|
/// A negative crypto period index disables key rotation.
|
|
|
|
static const int kDisableKeyRotation = -1;
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
class HttpFetcher;
|
2014-01-14 04:52:05 +00:00
|
|
|
class RequestSigner;
|
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
/// WidevineEncryptionKeySource talks to the Widevine encryption service to
|
|
|
|
/// acquire the encryption keys.
|
|
|
|
class WidevineEncryptionKeySource : public EncryptionKeySource {
|
2013-12-17 00:52:13 +00:00
|
|
|
public:
|
2014-01-24 18:46:46 +00:00
|
|
|
/// @param server_url is the Widevine common encryption server url.
|
|
|
|
/// @param content_id the unique id identify the content to be encrypted.
|
2014-04-24 16:59:07 +00:00
|
|
|
/// @param signer signs the request message. It should not be NULL.
|
|
|
|
/// @param first_crypto_period_index indicates the starting crypto period
|
|
|
|
/// index. Set it to kDisableKeyRotation to disable key rotation.
|
2014-04-16 01:09:32 +00:00
|
|
|
WidevineEncryptionKeySource(const std::string& server_url,
|
|
|
|
const std::string& content_id,
|
2014-04-24 16:59:07 +00:00
|
|
|
scoped_ptr<RequestSigner> signer,
|
|
|
|
int first_crypto_period_index);
|
2014-04-16 01:09:32 +00:00
|
|
|
virtual ~WidevineEncryptionKeySource();
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-04-24 16:59:07 +00:00
|
|
|
/// @name EncryptionKeySource implementation overrides.
|
|
|
|
/// @{
|
2014-04-15 22:18:26 +00:00
|
|
|
virtual Status GetKey(TrackType track_type, EncryptionKey* key) OVERRIDE;
|
2014-04-24 16:59:07 +00:00
|
|
|
virtual Status GetCryptoPeriodKey(uint32 crypto_period_index,
|
|
|
|
TrackType track_type,
|
|
|
|
EncryptionKey* key) OVERRIDE;
|
|
|
|
/// @}
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-01-24 18:46:46 +00:00
|
|
|
/// Inject an @b HttpFetcher object, mainly used for testing.
|
|
|
|
/// @param http_fetcher points to the @b HttpFetcher object to be injected.
|
|
|
|
void set_http_fetcher(scoped_ptr<HttpFetcher> http_fetcher);
|
|
|
|
|
2013-12-17 00:52:13 +00:00
|
|
|
private:
|
2014-04-24 16:59:07 +00:00
|
|
|
typedef std::map<TrackType, EncryptionKey*> EncryptionKeyMap;
|
|
|
|
class RefCountedEncryptionKeyMap;
|
|
|
|
|
|
|
|
// Internal routine for getting keys.
|
|
|
|
Status GetKeyInternal(uint32 crypto_period_index,
|
|
|
|
TrackType track_type,
|
|
|
|
EncryptionKey* key);
|
|
|
|
|
|
|
|
// The closure task to fetch keys repeatedly.
|
|
|
|
void FetchKeysTask();
|
|
|
|
|
2014-04-15 22:18:26 +00:00
|
|
|
// Fetch keys from server.
|
2014-04-24 16:59:07 +00:00
|
|
|
Status FetchKeys(uint32 first_crypto_period_index);
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2013-12-17 00:52:13 +00:00
|
|
|
// Fill |request| with necessary fields for Widevine encryption request.
|
|
|
|
// |request| should not be NULL.
|
2014-04-24 16:59:07 +00:00
|
|
|
void FillRequest(const std::string& content_id,
|
|
|
|
uint32 first_crypto_period_index,
|
|
|
|
std::string* request);
|
2013-12-17 00:52:13 +00:00
|
|
|
// Sign and properly format |request|.
|
|
|
|
// |signed_request| should not be NULL. Return OK on success.
|
|
|
|
Status SignRequest(const std::string& request, std::string* signed_request);
|
|
|
|
// Decode |response| from JSON formatted |raw_response|.
|
|
|
|
// |response| should not be NULL.
|
|
|
|
bool DecodeResponse(const std::string& raw_response, std::string* response);
|
|
|
|
// Extract encryption key from |response|, which is expected to be properly
|
2014-01-14 18:36:41 +00:00
|
|
|
// formatted. |transient_error| will be set to true if it fails and the
|
|
|
|
// failure is because of a transient error from the server. |transient_error|
|
|
|
|
// should not be NULL.
|
2014-04-16 01:09:32 +00:00
|
|
|
bool ExtractEncryptionKey(const std::string& response, bool* transient_error);
|
2014-04-24 16:59:07 +00:00
|
|
|
// Push the keys to the key pool.
|
|
|
|
bool PushToKeyPool(EncryptionKeyMap* encryption_key_map);
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-02-20 22:38:28 +00:00
|
|
|
// The fetcher object used to fetch HTTP response from server.
|
|
|
|
// It is initialized to a default fetcher on class initialization.
|
|
|
|
// Can be overridden using set_http_fetcher for testing or other purposes.
|
|
|
|
scoped_ptr<HttpFetcher> http_fetcher_;
|
2013-12-17 00:52:13 +00:00
|
|
|
std::string server_url_;
|
|
|
|
std::string content_id_;
|
2014-01-14 04:52:05 +00:00
|
|
|
scoped_ptr<RequestSigner> signer_;
|
2013-12-17 00:52:13 +00:00
|
|
|
|
2014-04-24 16:59:07 +00:00
|
|
|
const bool key_rotation_enabled_;
|
|
|
|
const uint32 crypto_period_count_;
|
|
|
|
uint32 first_crypto_period_index_;
|
|
|
|
ClosureThread key_production_thread_;
|
|
|
|
ProducerConsumerQueue<scoped_refptr<RefCountedEncryptionKeyMap> > key_pool_;
|
|
|
|
Status common_encryption_request_status_;
|
2014-04-15 22:18:26 +00:00
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(WidevineEncryptionKeySource);
|
2013-12-17 00:52:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
|
2014-04-16 01:09:32 +00:00
|
|
|
#endif // MEDIA_BASE_WIDEVINE_ENCRYPTION_KEY_SOURCE_H_
|