Skip to content

Commit

Permalink
Separate MediaKeys interface from Decryptor interface.
Browse files Browse the repository at this point in the history
BUG=none
TEST=none

Review URL: https://chromiumcodereview.appspot.com/15772012

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203026 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
xhwang@chromium.org committed May 30, 2013
1 parent 2dcec1f commit 15b05a7
Show file tree
Hide file tree
Showing 22 changed files with 189 additions and 132 deletions.
80 changes: 11 additions & 69 deletions media/base/decryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define MEDIA_BASE_DECRYPTOR_H_

#include <list>
#include <string>

#include "base/basictypes.h"
#include "base/callback.h"
Expand All @@ -18,34 +17,18 @@ namespace media {
class AudioDecoderConfig;
class DataBuffer;
class DecoderBuffer;
class MediaKeys;
class VideoDecoderConfig;
class VideoFrame;

// Performs key operations and decrypts (and decodes) encrypted buffer.
// Decrypts (and decodes) encrypted buffer.
//
// Key operations (GenerateKeyRequest(), AddKey() and CancelKeyRequest())
// are called on the renderer thread. Therefore, these calls should be fast
// and nonblocking; key events should be fired asynchronously.
// All other methods are called on the (video/audio) decoder thread.
// Decryptor implementations must be thread safe when methods are called
// following the above model.
// All methods are called on the (video/audio) decoder thread. Decryptor
// implementations must be thread safe when methods are called this way.
// Depending on the implementation callbacks may be fired synchronously or
// asynchronously.
class MEDIA_EXPORT Decryptor {
public:
// Reported to UMA, so never reuse a value!
// Must be kept in sync with WebKit::WebMediaPlayerClient::MediaKeyErrorCode
// (enforced in webmediaplayer_impl.cc).
enum KeyError {
kUnknownError = 1,
kClientError,
kServiceError,
kOutputError,
kHardwareChangeError,
kDomainError,
kMaxKeyError // Must be last and greater than any legit value.
};

// TODO(xhwang): Replace kError with kDecryptError and kDecodeError.
// TODO(xhwang): Replace kNeedMoreData with kNotEnoughData.
enum Status {
Expand All @@ -64,32 +47,13 @@ class MEDIA_EXPORT Decryptor {
Decryptor();
virtual ~Decryptor();

// Generates a key request for the |key_system| with |type| and
// |init_data| provided.
// Returns true if generating key request succeeded, false otherwise.
// Note: AddKey() and CancelKeyRequest() should only be called after
// GenerateKeyRequest() returns true.
virtual bool GenerateKeyRequest(const std::string& key_system,
const std::string& type,
const uint8* init_data,
int init_data_length) = 0;

// Adds a |key| to the |key_system|. The |key| is not limited to a decryption
// key. It can be any data that the key system accepts, such as a license.
// If multiple calls of this function set different keys for the same
// key ID, the older key will be replaced by the newer key.
virtual void AddKey(const std::string& key_system,
const uint8* key,
int key_length,
const uint8* init_data,
int init_data_length,
const std::string& session_id) = 0;

// Cancels the key request specified by |session_id|.
virtual void CancelKeyRequest(const std::string& key_system,
const std::string& session_id) = 0;

// Indicates that a new key has been added to the Decryptor.
// Gets the MediaKey object associated with the Decryptor. Returns NULL if
// no MediaKey object is associated. The returned object is only guaranteed
// to be valid during the Decryptor's lifetime.
virtual MediaKeys* GetMediaKeys() = 0;

// Indicates that a new key has been added to the MediaKeys object associated
// with the Decryptor.
typedef base::Callback<void()> NewKeyCB;

// Registers a NewKeyCB which should be called when a new key is added to the
Expand Down Expand Up @@ -215,28 +179,6 @@ typedef base::Callback<void(Decryptor*)> DecryptorReadyCB;
// fired immediately with NULL.
typedef base::Callback<void(const DecryptorReadyCB&)> SetDecryptorReadyCB;


// Key event callbacks. See the spec for details:
// http://dvcs.w3.org/hg/html-media/raw-file/eme-v0.1b/encrypted-media/encrypted-media.html#event-summary
typedef base::Callback<void(const std::string& key_system,
const std::string& session_id)> KeyAddedCB;

typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
media::Decryptor::KeyError error_code,
int system_code)> KeyErrorCB;

typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
const std::string& message,
const std::string& default_url)> KeyMessageCB;

typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
const std::string& type,
scoped_ptr<uint8[]> init_data,
int init_data_size)> NeedKeyCB;

} // namespace media

#endif // MEDIA_BASE_DECRYPTOR_H_
13 changes: 13 additions & 0 deletions media/base/media_keys.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/media_keys.h"

namespace media {

MediaKeys::MediaKeys() {}

MediaKeys::~MediaKeys() {}

} // namespace media
89 changes: 89 additions & 0 deletions media/base/media_keys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_BASE_MEDIA_KEYS_H_
#define MEDIA_BASE_MEDIA_KEYS_H_

#include <string>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "media/base/media_export.h"

namespace media {

// Performs media key operations.
//
// All key operations are called on the renderer thread. Therefore, these calls
// should be fast and nonblocking; key events should be fired asynchronously.
class MEDIA_EXPORT MediaKeys {
public:
// Reported to UMA, so never reuse a value!
// Must be kept in sync with WebKit::WebMediaPlayerClient::MediaKeyErrorCode
// (enforced in webmediaplayer_impl.cc).
enum KeyError {
kUnknownError = 1,
kClientError,
kServiceError,
kOutputError,
kHardwareChangeError,
kDomainError,
kMaxKeyError // Must be last and greater than any legit value.
};

MediaKeys();
virtual ~MediaKeys();

// Generates a key request for the |key_system| with |type| and
// |init_data| provided.
// Returns true if generating key request succeeded, false otherwise.
// Note: AddKey() and CancelKeyRequest() should only be called after
// GenerateKeyRequest() returns true.
virtual bool GenerateKeyRequest(const std::string& key_system,
const std::string& type,
const uint8* init_data,
int init_data_length) = 0;

// Adds a |key| to the |key_system|. The |key| is not limited to a decryption
// key. It can be any data that the key system accepts, such as a license.
// If multiple calls of this function set different keys for the same
// key ID, the older key will be replaced by the newer key.
virtual void AddKey(const std::string& key_system,
const uint8* key, int key_length,
const uint8* init_data, int init_data_length,
const std::string& session_id) = 0;

// Cancels the key request specified by |session_id|.
virtual void CancelKeyRequest(const std::string& key_system,
const std::string& session_id) = 0;

private:
DISALLOW_COPY_AND_ASSIGN(MediaKeys);
};

// Key event callbacks. See the spec for details:
// http://dvcs.w3.org/hg/html-media/raw-file/eme-v0.1b/encrypted-media/encrypted-media.html#event-summary
typedef base::Callback<void(const std::string& key_system,
const std::string& session_id)> KeyAddedCB;

typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
media::MediaKeys::KeyError error_code,
int system_code)> KeyErrorCB;

typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
const std::string& message,
const std::string& default_url)> KeyMessageCB;

typedef base::Callback<void(const std::string& key_system,
const std::string& session_id,
const std::string& type,
scoped_ptr<uint8[]> init_data,
int init_data_size)> NeedKeyCB;

} // namespace media

#endif // MEDIA_BASE_MEDIA_KEYS_H_
14 changes: 2 additions & 12 deletions media/base/mock_filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,8 @@ class MockDecryptor : public Decryptor {
MockDecryptor();
virtual ~MockDecryptor();

MOCK_METHOD4(GenerateKeyRequest, bool(const std::string& key_system,
const std::string& type,
const uint8* init_data,
int init_data_length));
MOCK_METHOD6(AddKey, void(const std::string& key_system,
const uint8* key,
int key_length,
const uint8* init_data,
int init_data_length,
const std::string& session_id));
MOCK_METHOD2(CancelKeyRequest, void(const std::string& key_system,
const std::string& session_id));
MOCK_METHOD0(GetMediaKeys, MediaKeys*(void));

MOCK_METHOD2(RegisterNewKeyCB, void(StreamType stream_type,
const NewKeyCB& new_key_cb));
MOCK_METHOD3(Decrypt, void(StreamType stream_type,
Expand Down
10 changes: 7 additions & 3 deletions media/crypto/aes_decryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void AesDecryptor::AddKey(const std::string& key_system,
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550
if (key_length != DecryptConfig::kDecryptionKeySize) {
DVLOG(1) << "Invalid key length: " << key_length;
key_error_cb_.Run(key_system, session_id, Decryptor::kUnknownError, 0);
key_error_cb_.Run(key_system, session_id, MediaKeys::kUnknownError, 0);
return;
}

Expand All @@ -189,13 +189,13 @@ void AesDecryptor::AddKey(const std::string& key_system,
scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string));
if (!decryption_key) {
DVLOG(1) << "Could not create key.";
key_error_cb_.Run(key_system, session_id, Decryptor::kUnknownError, 0);
key_error_cb_.Run(key_system, session_id, MediaKeys::kUnknownError, 0);
return;
}

if (!decryption_key->Init()) {
DVLOG(1) << "Could not initialize decryption key.";
key_error_cb_.Run(key_system, session_id, Decryptor::kUnknownError, 0);
key_error_cb_.Run(key_system, session_id, MediaKeys::kUnknownError, 0);
return;
}

Expand All @@ -214,6 +214,10 @@ void AesDecryptor::CancelKeyRequest(const std::string& key_system,
const std::string& session_id) {
}

MediaKeys* AesDecryptor::GetMediaKeys() {
return this;
}

void AesDecryptor::RegisterNewKeyCB(StreamType stream_type,
const NewKeyCB& new_key_cb) {
switch (stream_type) {
Expand Down
14 changes: 8 additions & 6 deletions media/crypto/aes_decryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "base/synchronization/lock.h"
#include "media/base/decryptor.h"
#include "media/base/media_export.h"
#include "media/base/media_keys.h"

namespace crypto {
class SymmetricKey;
Expand All @@ -24,27 +25,28 @@ namespace media {

// Decrypts an AES encrypted buffer into an unencrypted buffer. The AES
// encryption must be CTR with a key size of 128bits.
class MEDIA_EXPORT AesDecryptor : public Decryptor {
class MEDIA_EXPORT AesDecryptor : public MediaKeys, public Decryptor {
public:
AesDecryptor(const KeyAddedCB& key_added_cb,
const KeyErrorCB& key_error_cb,
const KeyMessageCB& key_message_cb,
const NeedKeyCB& need_key_cb);
virtual ~AesDecryptor();

// Decryptor implementation.
// MediaKeys implementation.
virtual bool GenerateKeyRequest(const std::string& key_system,
const std::string& type,
const uint8* init_data,
int init_data_length) OVERRIDE;
virtual void AddKey(const std::string& key_system,
const uint8* key,
int key_length,
const uint8* init_data,
int init_data_length,
const uint8* key, int key_length,
const uint8* init_data, int init_data_length,
const std::string& session_id) OVERRIDE;
virtual void CancelKeyRequest(const std::string& key_system,
const std::string& session_id) OVERRIDE;

// Decryptor implementation.
virtual MediaKeys* GetMediaKeys() OVERRIDE;
virtual void RegisterNewKeyCB(StreamType stream_type,
const NewKeyCB& key_added_cb) OVERRIDE;
virtual void Decrypt(StreamType stream_type,
Expand Down
4 changes: 2 additions & 2 deletions media/crypto/aes_decryptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class AesDecryptorTest : public testing::Test {
void AddKeyAndExpectToFail(const uint8* key_id, int key_id_size,
const uint8* key, int key_size) {
EXPECT_CALL(*this, KeyError(kClearKeySystem, session_id_string_,
Decryptor::kUnknownError, 0));
MediaKeys::kUnknownError, 0));
decryptor_.AddKey(kClearKeySystem, key, key_size, key_id, key_id_size,
session_id_string_);
}
Expand Down Expand Up @@ -314,7 +314,7 @@ class AesDecryptorTest : public testing::Test {

MOCK_METHOD2(KeyAdded, void(const std::string&, const std::string&));
MOCK_METHOD4(KeyError, void(const std::string&, const std::string&,
Decryptor::KeyError, int));
MediaKeys::KeyError, int));
MOCK_METHOD4(KeyMessage, void(const std::string& key_system,
const std::string& session_id,
const std::string& message,
Expand Down
5 changes: 3 additions & 2 deletions media/filters/pipeline_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/string_util.h"
#include "build/build_config.h"
#include "media/base/decoder_buffer.h"
#include "media/base/media_keys.h"
#include "media/base/test_data_util.h"
#include "media/crypto/aes_decryptor.h"
#include "media/filters/chunk_demuxer.h"
Expand Down Expand Up @@ -71,7 +72,7 @@ class FakeEncryptedMedia {
// Errors are not expected unless overridden.
virtual void KeyError(const std::string& key_system,
const std::string& session_id,
AesDecryptor::KeyError error_code,
MediaKeys::KeyError error_code,
int system_code) {
FAIL() << "Unexpected Key Error";
}
Expand Down Expand Up @@ -111,7 +112,7 @@ class FakeEncryptedMedia {

void KeyError(const std::string& key_system,
const std::string& session_id,
AesDecryptor::KeyError error_code,
MediaKeys::KeyError error_code,
int system_code) {
app_->KeyError(key_system, session_id, error_code, system_code);
}
Expand Down
1 change: 1 addition & 0 deletions media/filters/pipeline_integration_test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/md5.h"
#include "media/audio/null_audio_sink.h"
#include "media/base/filter_collection.h"
#include "media/base/media_keys.h"
#include "media/base/pipeline.h"
#include "media/base/video_frame.h"
#include "media/filters/video_renderer_base.h"
Expand Down
2 changes: 2 additions & 0 deletions media/media.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@
'base/filter_collection.h',
'base/media.cc',
'base/media.h',
'base/media_keys.cc',
'base/media_keys.h',
'base/media_log.cc',
'base/media_log.h',
'base/media_log_event.h',
Expand Down
2 changes: 1 addition & 1 deletion webkit/media/android/media_source_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ void MediaSourceDelegate::OnDemuxerOpened() {

void MediaSourceDelegate::OnKeyError(const std::string& key_system,
const std::string& session_id,
media::Decryptor::KeyError error_code,
media::MediaKeys::KeyError error_code,
int system_code) {
if (!client_)
return;
Expand Down
Loading

0 comments on commit 15b05a7

Please sign in to comment.