This source file includes following definitions.
- CreateFakeEncryptedBuffer
- ACTION_P
- ACTION_P
- ACTION_P2
- MATCHER
- decoded_frame_list_
- InitializeAndExpectStatus
- Initialize
- Reinitialize
- ReinitializeConfigChange
- ReadAndExpectFrameReadyWith
- EnterNormalDecodingState
- EnterEndOfStreamState
- EnterPendingDecodeState
- EnterWaitingForKeyState
- AbortPendingAudioDecodeCB
- AbortAllPendingCBs
- Reset
- Stop
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/message_loop/message_loop.h"
#include "media/base/audio_buffer.h"
#include "media/base/buffers.h"
#include "media/base/decoder_buffer.h"
#include "media/base/decrypt_config.h"
#include "media/base/gmock_callback_support.h"
#include "media/base/mock_filters.h"
#include "media/base/test_helpers.h"
#include "media/filters/decrypting_audio_decoder.h"
#include "testing/gmock/include/gmock/gmock.h"
using ::testing::_;
using ::testing::AtMost;
using ::testing::IsNull;
using ::testing::SaveArg;
using ::testing::StrictMock;
namespace media {
static const int kSampleRate = 44100;
static const int kFakeAudioFrameSize = 48;
static const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 };
static const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 };
static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() {
const int buffer_size = 16;
scoped_refptr<DecoderBuffer> buffer(new DecoderBuffer(buffer_size));
buffer->set_decrypt_config(scoped_ptr<DecryptConfig>(new DecryptConfig(
std::string(reinterpret_cast<const char*>(kFakeKeyId),
arraysize(kFakeKeyId)),
std::string(reinterpret_cast<const char*>(kFakeIv), arraysize(kFakeIv)),
std::vector<SubsampleEntry>())));
return buffer;
}
namespace {
ACTION_P(ReturnBuffer, buffer) {
return buffer;
}
ACTION_P(RunCallbackIfNotNull, param) {
if (!arg0.is_null())
arg0.Run(param);
}
ACTION_P2(CallExpectFrameReadyMoreData, test, buffer) {
test->ReadAndExpectFrameReadyWith(
CreateFakeEncryptedBuffer(), AudioDecoder::kNotEnoughData, buffer);
}
MATCHER(IsEndOfStream, "end of stream") {
return (arg->end_of_stream());
}
}
class DecryptingAudioDecoderTest : public testing::Test {
public:
DecryptingAudioDecoderTest()
: decoder_(new DecryptingAudioDecoder(
message_loop_.message_loop_proxy(),
base::Bind(
&DecryptingAudioDecoderTest::RequestDecryptorNotification,
base::Unretained(this)))),
decryptor_(new StrictMock<MockDecryptor>()),
encrypted_buffer_(CreateFakeEncryptedBuffer()),
decoded_frame_(NULL),
end_of_stream_frame_(AudioBuffer::CreateEOSBuffer()),
decoded_frame_list_() {}
virtual ~DecryptingAudioDecoderTest() {
EXPECT_CALL(*this, RequestDecryptorNotification(_))
.Times(testing::AnyNumber());
Stop();
}
void InitializeAndExpectStatus(const AudioDecoderConfig& config,
PipelineStatus status) {
int channels = ChannelLayoutToChannelCount(config.channel_layout());
if (channels < 0)
channels = 0;
decoded_frame_ = AudioBuffer::CreateEmptyBuffer(config.channel_layout(),
channels,
kSampleRate,
kFakeAudioFrameSize,
kNoTimestamp(),
kNoTimestamp());
decoded_frame_list_.push_back(decoded_frame_);
decoder_->Initialize(config, NewExpectedStatusCB(status));
message_loop_.RunUntilIdle();
}
void Initialize() {
EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
.Times(AtMost(1))
.WillOnce(RunCallback<1>(true));
EXPECT_CALL(*this, RequestDecryptorNotification(_))
.WillOnce(RunCallbackIfNotNull(decryptor_.get()));
EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
.WillOnce(SaveArg<1>(&key_added_cb_));
config_.Initialize(kCodecVorbis, kSampleFormatPlanarF32,
CHANNEL_LAYOUT_STEREO, kSampleRate, NULL, 0, true, true,
base::TimeDelta(), base::TimeDelta());
InitializeAndExpectStatus(config_, PIPELINE_OK);
}
void Reinitialize() {
ReinitializeConfigChange(config_);
}
void ReinitializeConfigChange(AudioDecoderConfig& new_config) {
EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio));
EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
.WillOnce(RunCallback<1>(true));
EXPECT_CALL(*decryptor_, RegisterNewKeyCB(Decryptor::kAudio, _))
.WillOnce(SaveArg<1>(&key_added_cb_));
decoder_->Initialize(new_config, NewExpectedStatusCB(PIPELINE_OK));
}
void ReadAndExpectFrameReadyWith(
scoped_refptr<DecoderBuffer> input,
AudioDecoder::Status status,
const scoped_refptr<AudioBuffer>& audio_frame) {
const scoped_refptr<AudioBuffer>& buffer = decoder_->GetDecodeOutput();
if (buffer) {
EXPECT_EQ(audio_frame, buffer);
EXPECT_EQ(status, AudioDecoder::kOk);
return;
}
if (status == AudioDecoder::kNotEnoughData)
EXPECT_CALL(*this, FrameReady(status, scoped_refptr<AudioBuffer>(NULL))).
WillRepeatedly(CallExpectFrameReadyMoreData(this, audio_frame));
else if (status != AudioDecoder::kOk)
EXPECT_CALL(*this, FrameReady(status, IsNull()));
else if (audio_frame->end_of_stream())
EXPECT_CALL(*this, FrameReady(status, IsEndOfStream()));
else
EXPECT_CALL(*this, FrameReady(status, audio_frame));
decoder_->Decode(input,
base::Bind(&DecryptingAudioDecoderTest::FrameReady,
base::Unretained(this)));
message_loop_.RunUntilIdle();
}
void EnterNormalDecodingState() {
Decryptor::AudioBuffers end_of_stream_frames_(1, end_of_stream_frame_);
EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
.WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_))
.WillRepeatedly(RunCallback<1>(Decryptor::kNeedMoreData,
Decryptor::AudioBuffers()));
ReadAndExpectFrameReadyWith(
encrypted_buffer_, AudioDecoder::kOk, decoded_frame_);
}
void EnterEndOfStreamState() {
ReadAndExpectFrameReadyWith(DecoderBuffer::CreateEOSBuffer(),
AudioDecoder::kOk,
end_of_stream_frame_);
}
void EnterPendingDecodeState() {
EXPECT_TRUE(pending_audio_decode_cb_.is_null());
EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
.WillOnce(SaveArg<1>(&pending_audio_decode_cb_));
decoder_->Decode(encrypted_buffer_,
base::Bind(&DecryptingAudioDecoderTest::FrameReady,
base::Unretained(this)));
message_loop_.RunUntilIdle();
EXPECT_FALSE(pending_audio_decode_cb_.is_null());
}
void EnterWaitingForKeyState() {
EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(encrypted_buffer_, _))
.WillRepeatedly(RunCallback<1>(Decryptor::kNoKey,
Decryptor::AudioBuffers()));
decoder_->Decode(encrypted_buffer_,
base::Bind(&DecryptingAudioDecoderTest::FrameReady,
base::Unretained(this)));
message_loop_.RunUntilIdle();
}
void AbortPendingAudioDecodeCB() {
if (!pending_audio_decode_cb_.is_null()) {
base::ResetAndReturn(&pending_audio_decode_cb_).Run(
Decryptor::kSuccess, Decryptor::AudioBuffers());
}
}
void AbortAllPendingCBs() {
if (!pending_init_cb_.is_null()) {
ASSERT_TRUE(pending_audio_decode_cb_.is_null());
base::ResetAndReturn(&pending_init_cb_).Run(false);
return;
}
AbortPendingAudioDecodeCB();
}
void Reset() {
EXPECT_CALL(*decryptor_, ResetDecoder(Decryptor::kAudio))
.WillRepeatedly(InvokeWithoutArgs(
this, &DecryptingAudioDecoderTest::AbortPendingAudioDecodeCB));
decoder_->Reset(NewExpectedClosure());
message_loop_.RunUntilIdle();
}
void Stop() {
EXPECT_CALL(*decryptor_, DeinitializeDecoder(Decryptor::kAudio))
.WillRepeatedly(InvokeWithoutArgs(
this, &DecryptingAudioDecoderTest::AbortAllPendingCBs));
decoder_->Stop();
message_loop_.RunUntilIdle();
}
MOCK_METHOD1(RequestDecryptorNotification, void(const DecryptorReadyCB&));
MOCK_METHOD2(FrameReady,
void(AudioDecoder::Status, const scoped_refptr<AudioBuffer>&));
base::MessageLoop message_loop_;
scoped_ptr<DecryptingAudioDecoder> decoder_;
scoped_ptr<StrictMock<MockDecryptor> > decryptor_;
AudioDecoderConfig config_;
Decryptor::DecoderInitCB pending_init_cb_;
Decryptor::NewKeyCB key_added_cb_;
Decryptor::AudioDecodeCB pending_audio_decode_cb_;
scoped_refptr<DecoderBuffer> encrypted_buffer_;
scoped_refptr<AudioBuffer> decoded_frame_;
scoped_refptr<AudioBuffer> end_of_stream_frame_;
Decryptor::AudioBuffers decoded_frame_list_;
private:
DISALLOW_COPY_AND_ASSIGN(DecryptingAudioDecoderTest);
};
TEST_F(DecryptingAudioDecoderTest, Initialize_Normal) {
Initialize();
}
TEST_F(DecryptingAudioDecoderTest, Initialize_UnencryptedAudioConfig) {
AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
CHANNEL_LAYOUT_STEREO, kSampleRate, NULL, 0, false);
InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
}
TEST_F(DecryptingAudioDecoderTest, Initialize_InvalidAudioConfig) {
AudioDecoderConfig config(kUnknownAudioCodec, kUnknownSampleFormat,
CHANNEL_LAYOUT_STEREO, 0, NULL, 0, true);
InitializeAndExpectStatus(config, PIPELINE_ERROR_DECODE);
}
TEST_F(DecryptingAudioDecoderTest, Initialize_UnsupportedAudioConfig) {
EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
.WillOnce(RunCallback<1>(false));
EXPECT_CALL(*this, RequestDecryptorNotification(_))
.WillOnce(RunCallbackIfNotNull(decryptor_.get()));
AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
CHANNEL_LAYOUT_STEREO, kSampleRate, NULL, 0, true);
InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
}
TEST_F(DecryptingAudioDecoderTest, Initialize_NullDecryptor) {
EXPECT_CALL(*this, RequestDecryptorNotification(_))
.WillRepeatedly(RunCallbackIfNotNull(static_cast<Decryptor*>(NULL)));
AudioDecoderConfig config(kCodecVorbis, kSampleFormatPlanarF32,
CHANNEL_LAYOUT_STEREO, kSampleRate, NULL, 0, true);
InitializeAndExpectStatus(config, DECODER_ERROR_NOT_SUPPORTED);
}
TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_Normal) {
Initialize();
EnterNormalDecodingState();
}
TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_DecodeError) {
Initialize();
EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
.WillRepeatedly(RunCallback<1>(Decryptor::kError,
Decryptor::AudioBuffers()));
ReadAndExpectFrameReadyWith(
encrypted_buffer_, AudioDecoder::kDecodeError, NULL);
}
TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_NeedMoreData) {
Initialize();
EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
.WillOnce(RunCallback<1>(Decryptor::kNeedMoreData,
Decryptor::AudioBuffers()))
.WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
EXPECT_CALL(*this, FrameReady(AudioDecoder::kOk, decoded_frame_));
ReadAndExpectFrameReadyWith(
encrypted_buffer_, AudioDecoder::kNotEnoughData, decoded_frame_);
}
TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_MultipleFrames) {
Initialize();
scoped_refptr<AudioBuffer> frame_a = AudioBuffer::CreateEmptyBuffer(
config_.channel_layout(),
ChannelLayoutToChannelCount(config_.channel_layout()),
kSampleRate,
kFakeAudioFrameSize,
kNoTimestamp(),
kNoTimestamp());
scoped_refptr<AudioBuffer> frame_b = AudioBuffer::CreateEmptyBuffer(
config_.channel_layout(),
ChannelLayoutToChannelCount(config_.channel_layout()),
kSampleRate,
kFakeAudioFrameSize,
kNoTimestamp(),
kNoTimestamp());
decoded_frame_list_.push_back(frame_a);
decoded_frame_list_.push_back(frame_b);
EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
.WillOnce(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
ReadAndExpectFrameReadyWith(
encrypted_buffer_, AudioDecoder::kOk, decoded_frame_);
ReadAndExpectFrameReadyWith(encrypted_buffer_, AudioDecoder::kOk, frame_a);
ReadAndExpectFrameReadyWith(encrypted_buffer_, AudioDecoder::kOk, frame_b);
}
TEST_F(DecryptingAudioDecoderTest, DecryptAndDecode_EndOfStream) {
Initialize();
EnterNormalDecodingState();
EnterEndOfStreamState();
}
TEST_F(DecryptingAudioDecoderTest, Reinitialize_ConfigChange) {
Initialize();
EXPECT_CALL(*decryptor_, InitializeAudioDecoder(_, _))
.Times(AtMost(1))
.WillOnce(RunCallback<1>(true));
AudioDecoderConfig new_config(kCodecVorbis, kSampleFormatPlanarS16,
CHANNEL_LAYOUT_5_1, 88200, NULL, 0, true);
EXPECT_NE(new_config.bits_per_channel(), config_.bits_per_channel());
EXPECT_NE(new_config.channel_layout(), config_.channel_layout());
EXPECT_NE(new_config.samples_per_second(), config_.samples_per_second());
ReinitializeConfigChange(new_config);
message_loop_.RunUntilIdle();
}
TEST_F(DecryptingAudioDecoderTest, KeyAdded_DuringWaitingForKey) {
Initialize();
EnterWaitingForKeyState();
EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
.WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
EXPECT_CALL(*this, FrameReady(AudioDecoder::kOk, decoded_frame_));
key_added_cb_.Run();
message_loop_.RunUntilIdle();
}
TEST_F(DecryptingAudioDecoderTest, KeyAdded_DruingPendingDecode) {
Initialize();
EnterPendingDecodeState();
EXPECT_CALL(*decryptor_, DecryptAndDecodeAudio(_, _))
.WillRepeatedly(RunCallback<1>(Decryptor::kSuccess, decoded_frame_list_));
EXPECT_CALL(*this, FrameReady(AudioDecoder::kOk, decoded_frame_));
key_added_cb_.Run();
base::ResetAndReturn(&pending_audio_decode_cb_).Run(
Decryptor::kNoKey, Decryptor::AudioBuffers());
message_loop_.RunUntilIdle();
}
TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterInitialization) {
Initialize();
Reset();
}
TEST_F(DecryptingAudioDecoderTest, Reset_DuringIdleAfterDecodedOneFrame) {
Initialize();
EnterNormalDecodingState();
Reset();
}
TEST_F(DecryptingAudioDecoderTest, Reset_DuringPendingDecode) {
Initialize();
EnterPendingDecodeState();
EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
Reset();
}
TEST_F(DecryptingAudioDecoderTest, Reset_DuringWaitingForKey) {
Initialize();
EnterWaitingForKeyState();
EXPECT_CALL(*this, FrameReady(AudioDecoder::kAborted, IsNull()));
Reset();
}
TEST_F(DecryptingAudioDecoderTest, Reset_AfterDecodeFinished) {
Initialize();
EnterNormalDecodingState();
EnterEndOfStreamState();
Reset();
}
TEST_F(DecryptingAudioDecoderTest, Reset_AfterReset) {
Initialize();
EnterNormalDecodingState();
Reset();
Reset();
}
}