Move encryptor creation out of EncryptorSource::Initialize.

Use EncryptorSource::CreateEncryptor for encryptor creation.
EncryptorSource no longer owns the created encryptor.

Change-Id: I34d1f32262b7692bcb347d3b084fd06cbae7850f
This commit is contained in:
Kongqun Yang 2014-01-13 11:34:08 -08:00 committed by KongQun Yang
parent 0154654539
commit b6af6ca976
5 changed files with 48 additions and 50 deletions

View File

@ -4,19 +4,36 @@
#include "media/base/encryptor_source.h" #include "media/base/encryptor_source.h"
#include "media/base/aes_encryptor.h"
namespace { namespace {
const char kWidevineSystemId[] = {0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6, // Generate 64bit IV by default.
0x4a, 0xce, 0xa3, 0xc8, 0x27, 0xdc, const size_t kDefaultIvSize = 8u;
0xd5, 0x1d, 0x21, 0xed};
const uint8 kWidevineSystemId[] = {0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6,
0x4a, 0xce, 0xa3, 0xc8, 0x27, 0xdc,
0xd5, 0x1d, 0x21, 0xed};
} // namespace } // namespace
namespace media { namespace media {
EncryptorSource::EncryptorSource() EncryptorSource::EncryptorSource()
: key_system_id_(kWidevineSystemId, : iv_size_(kDefaultIvSize),
kWidevineSystemId + arraysize(kWidevineSystemId)), key_system_id_(kWidevineSystemId,
clear_milliseconds_(0) {} kWidevineSystemId + arraysize(kWidevineSystemId)) {}
EncryptorSource::~EncryptorSource() {} EncryptorSource::~EncryptorSource() {}
scoped_ptr<AesCtrEncryptor> EncryptorSource::CreateEncryptor() {
scoped_ptr<AesCtrEncryptor> encryptor(new AesCtrEncryptor());
const bool initialized =
iv_.empty() ? encryptor->InitializeWithRandomIv(key_, iv_size_)
: encryptor->InitializeWithIv(key_, iv_);
if (!initialized) {
LOG(ERROR) << "Failed to the initialize encryptor.";
return scoped_ptr<AesCtrEncryptor>();
}
return encryptor.Pass();
}
} // namespace media } // namespace media

View File

@ -15,45 +15,51 @@
namespace media { namespace media {
class AesCtrEncryptor;
class EncryptorSource { class EncryptorSource {
public: public:
EncryptorSource(); EncryptorSource();
virtual ~EncryptorSource(); virtual ~EncryptorSource();
// Initialize the encryptor source. Calling other public methods of this
// class without this method returning Status::OK, results in an undefined
// behavior.
virtual Status Initialize() = 0; virtual Status Initialize() = 0;
// Refreshes the encryptor. NOP except for key rotation encryptor source. // Refresh the encryptor. NOP except for key rotation encryptor source.
// TODO(kqyang): Do we need to pass in duration or fragment number? // TODO(kqyang): Do we need to pass in duration or fragment number?
virtual void RefreshEncryptor() {} virtual void RefreshEncryptor() {}
// EncryptorSource retains the ownership of |encryptor_|. // Create an encryptor from this encryptor source. The encryptor will be
AesCtrEncryptor* encryptor() { return encryptor_.get(); } // initialized with a random IV of the default size by default. The behavior
// can be adjusted using set_iv_size or set_iv (exclusive).
scoped_ptr<AesCtrEncryptor> CreateEncryptor();
const std::vector<uint8>& key_id() const { return key_id_; } const std::vector<uint8>& key_id() const { return key_id_; }
const std::vector<uint8>& key() const { return key_; } const std::vector<uint8>& key() const { return key_; }
const std::vector<uint8>& pssh() const { return pssh_; } const std::vector<uint8>& pssh() const { return pssh_; }
uint32 clear_milliseconds() const { return clear_milliseconds_; }
const std::vector<uint8>& key_system_id() const { return key_system_id_; } const std::vector<uint8>& key_system_id() const { return key_system_id_; }
size_t iv_size() const { return iv_.empty() ? iv_size_ : iv_.size(); }
// Set IV size. The encryptor will be initialized with a random IV of the
// specified size. Mutally exclusive with set_iv.
void set_iv_size(size_t iv_size) { iv_size_ = iv_size; }
// Set IV. The encryptor will be initialized with the specified IV.
// Mutally exclusive with set_iv_size.
void set_iv(std::vector<uint8>& iv) { iv_ = iv; }
protected: protected:
// EncryptorSource takes ownership of |encryptor|.
void set_encryptor(scoped_ptr<AesCtrEncryptor> encryptor) {
encryptor_ = encryptor.Pass();
}
void set_key_id(const std::vector<uint8>& key_id) { key_id_ = key_id; } void set_key_id(const std::vector<uint8>& key_id) { key_id_ = key_id; }
void set_key(const std::vector<uint8>& key) { key_ = key; } void set_key(const std::vector<uint8>& key) { key_ = key; }
void set_pssh(const std::vector<uint8>& pssh) { pssh_ = pssh; } void set_pssh(const std::vector<uint8>& pssh) { pssh_ = pssh; }
void set_clear_milliseconds(uint32 clear_milliseconds) {
clear_milliseconds_ = clear_milliseconds;
}
private: private:
scoped_ptr<AesCtrEncryptor> encryptor_;
std::vector<uint8> key_id_; std::vector<uint8> key_id_;
std::vector<uint8> key_; std::vector<uint8> key_;
std::vector<uint8> pssh_; std::vector<uint8> pssh_;
// The first |clear_milliseconds_| of the result media should be in the clear size_t iv_size_;
// text, i.e. should not be encrypted. std::vector<uint8> iv_;
uint32 clear_milliseconds_;
const std::vector<uint8> key_system_id_; const std::vector<uint8> key_system_id_;
DISALLOW_COPY_AND_ASSIGN(EncryptorSource); DISALLOW_COPY_AND_ASSIGN(EncryptorSource);

View File

@ -6,24 +6,13 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "media/base/aes_encryptor.h"
namespace {
// The size of generated IV for this encryptor source.
const uint8 kIvSize = 8;
} // namespace
namespace media { namespace media {
FixedEncryptorSource::FixedEncryptorSource(const std::string& key_id_hex, FixedEncryptorSource::FixedEncryptorSource(const std::string& key_id_hex,
const std::string& key_hex, const std::string& key_hex,
const std::string& pssh_hex, const std::string& pssh_hex)
uint32 clear_milliseconds) : key_id_hex_(key_id_hex), key_hex_(key_hex), pssh_hex_(pssh_hex) {}
: key_id_hex_(key_id_hex),
key_hex_(key_hex),
pssh_hex_(pssh_hex) {
set_clear_milliseconds(clear_milliseconds);
}
FixedEncryptorSource::~FixedEncryptorSource() {} FixedEncryptorSource::~FixedEncryptorSource() {}
@ -46,11 +35,6 @@ Status FixedEncryptorSource::Initialize() {
return Status(error::INVALID_ARGUMENT, "Cannot parse input pssh_hex."); return Status(error::INVALID_ARGUMENT, "Cannot parse input pssh_hex.");
} }
scoped_ptr<AesCtrEncryptor> encryptor(new AesCtrEncryptor());
if (!encryptor->InitializeWithRandomIv(key, kIvSize))
return Status(error::UNKNOWN, "Failed to initialize the encryptor.");
set_encryptor(encryptor.Pass());
set_key_id(key_id); set_key_id(key_id);
set_key(key); set_key(key);
set_pssh(pssh); set_pssh(pssh);

View File

@ -15,8 +15,7 @@ class FixedEncryptorSource : public EncryptorSource {
public: public:
FixedEncryptorSource(const std::string& key_id_hex, FixedEncryptorSource(const std::string& key_id_hex,
const std::string& key_hex, const std::string& key_hex,
const std::string& pssh_hex, const std::string& pssh_hex);
uint32 clear_milliseconds);
virtual ~FixedEncryptorSource(); virtual ~FixedEncryptorSource();
// EncryptorSource implementation. // EncryptorSource implementation.

View File

@ -25,9 +25,6 @@
namespace { namespace {
// The size of generated IV for this encryptor source.
const uint8 kIvSize = 8;
bool Base64StringToBytes(const std::string& base64_string, bool Base64StringToBytes(const std::string& base64_string,
std::vector<uint8>* bytes) { std::vector<uint8>* bytes) {
DCHECK(bytes); DCHECK(bytes);
@ -117,7 +114,7 @@ bool WidevineEncryptorSource::SetAesSigningKey(const std::string& signer,
} }
scoped_ptr<AesCbcEncryptor> encryptor(new AesCbcEncryptor()); scoped_ptr<AesCbcEncryptor> encryptor(new AesCbcEncryptor());
if (!encryptor->Initialize(aes_key, iv)) { if (!encryptor->InitializeWithIv(aes_key, iv)) {
LOG(ERROR) << "Failed to initialize encryptor with key: " << aes_key_hex LOG(ERROR) << "Failed to initialize encryptor with key: " << aes_key_hex
<< " iv:" << iv_hex; << " iv:" << iv_hex;
return false; return false;
@ -166,11 +163,6 @@ Status WidevineEncryptorSource::Initialize() {
"Failed to extract encryption key from '" + response + "'."); "Failed to extract encryption key from '" + response + "'.");
} }
// TODO(kqyang): Move the creation of encryptor to Muxer.
scoped_ptr<AesCtrEncryptor> encryptor(new AesCtrEncryptor());
CHECK(encryptor->InitializeWithRandomIv(key, kIvSize));
set_encryptor(encryptor.Pass());
set_key_id(key_id); set_key_id(key_id);
set_key(key); set_key(key);
set_pssh(pssh); set_pssh(pssh);