This source file includes following definitions.
- ReadPacket
- Seek
- Initialize
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "media/base/mock_filters.h"
#include "media/base/test_data_util.h"
#include "media/ffmpeg/ffmpeg_common.h"
#include "media/filters/ffmpeg_glue.h"
#include "media/filters/in_memory_url_protocol.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::SetArgumentPointee;
using ::testing::StrictMock;
namespace media {
class MockProtocol : public FFmpegURLProtocol {
public:
MockProtocol() {}
MOCK_METHOD2(Read, int(int size, uint8* data));
MOCK_METHOD1(GetPosition, bool(int64* position_out));
MOCK_METHOD1(SetPosition, bool(int64 position));
MOCK_METHOD1(GetSize, bool(int64* size_out));
MOCK_METHOD0(IsStreaming, bool());
private:
DISALLOW_COPY_AND_ASSIGN(MockProtocol);
};
class FFmpegGlueTest : public ::testing::Test {
public:
FFmpegGlueTest()
: protocol_(new StrictMock<MockProtocol>()) {
EXPECT_CALL(*protocol_.get(), IsStreaming()).WillOnce(Return(true));
glue_.reset(new FFmpegGlue(protocol_.get()));
CHECK(glue_->format_context());
CHECK(glue_->format_context()->pb);
}
virtual ~FFmpegGlueTest() {
CHECK(glue_.get());
CHECK(protocol_.get());
glue_.reset();
}
int ReadPacket(int size, uint8* data) {
return glue_->format_context()->pb->read_packet(
protocol_.get(), data, size);
}
int64 Seek(int64 offset, int whence) {
return glue_->format_context()->pb->seek(protocol_.get(), offset, whence);
}
protected:
scoped_ptr<FFmpegGlue> glue_;
scoped_ptr< StrictMock<MockProtocol> > protocol_;
private:
DISALLOW_COPY_AND_ASSIGN(FFmpegGlueTest);
};
class FFmpegGlueDestructionTest : public ::testing::Test {
public:
FFmpegGlueDestructionTest() {}
void Initialize(const char* filename) {
data_ = ReadTestDataFile(filename);
protocol_.reset(new InMemoryUrlProtocol(
data_->data(), data_->data_size(), false));
glue_.reset(new FFmpegGlue(protocol_.get()));
CHECK(glue_->format_context());
CHECK(glue_->format_context()->pb);
}
virtual ~FFmpegGlueDestructionTest() {
CHECK(glue_.get());
CHECK(protocol_.get());
glue_.reset();
protocol_.reset();
data_ = NULL;
}
protected:
scoped_ptr<FFmpegGlue> glue_;
private:
scoped_ptr<InMemoryUrlProtocol> protocol_;
scoped_refptr<DecoderBuffer> data_;
DISALLOW_COPY_AND_ASSIGN(FFmpegGlueDestructionTest);
};
TEST_F(FFmpegGlueTest, Write) {
ASSERT_FALSE(glue_->format_context()->pb->write_packet);
ASSERT_FALSE(glue_->format_context()->pb->write_flag);
}
TEST_F(FFmpegGlueTest, Read) {
const int kBufferSize = 16;
uint8 buffer[kBufferSize];
InSequence s;
EXPECT_CALL(*protocol_, Read(0, buffer))
.WillOnce(Return(0));
EXPECT_CALL(*protocol_, Read(kBufferSize, buffer))
.WillOnce(Return(kBufferSize));
EXPECT_CALL(*protocol_, Read(kBufferSize, buffer))
.WillOnce(Return(DataSource::kReadError));
EXPECT_EQ(0, ReadPacket(0, buffer));
EXPECT_EQ(kBufferSize, ReadPacket(kBufferSize, buffer));
EXPECT_EQ(AVERROR(EIO), ReadPacket(kBufferSize, buffer));
}
TEST_F(FFmpegGlueTest, Seek) {
InSequence s;
EXPECT_CALL(*protocol_, SetPosition(-16))
.WillOnce(Return(false));
EXPECT_CALL(*protocol_, SetPosition(16))
.WillOnce(Return(true));
EXPECT_CALL(*protocol_, GetPosition(_))
.WillOnce(DoAll(SetArgumentPointee<0>(8), Return(true)));
EXPECT_EQ(AVERROR(EIO), Seek(-16, SEEK_SET));
EXPECT_EQ(8, Seek(16, SEEK_SET));
EXPECT_CALL(*protocol_, GetPosition(_))
.WillOnce(Return(false));
EXPECT_CALL(*protocol_, GetPosition(_))
.WillOnce(DoAll(SetArgumentPointee<0>(8), Return(true)));
EXPECT_CALL(*protocol_, SetPosition(16))
.WillOnce(Return(false));
EXPECT_CALL(*protocol_, GetPosition(_))
.WillOnce(DoAll(SetArgumentPointee<0>(8), Return(true)));
EXPECT_CALL(*protocol_, SetPosition(16))
.WillOnce(Return(true));
EXPECT_CALL(*protocol_, GetPosition(_))
.WillOnce(DoAll(SetArgumentPointee<0>(16), Return(true)));
EXPECT_EQ(AVERROR(EIO), Seek(8, SEEK_CUR));
EXPECT_EQ(AVERROR(EIO), Seek(8, SEEK_CUR));
EXPECT_EQ(16, Seek(8, SEEK_CUR));
EXPECT_CALL(*protocol_, GetSize(_))
.WillOnce(Return(false));
EXPECT_CALL(*protocol_, GetSize(_))
.WillOnce(DoAll(SetArgumentPointee<0>(16), Return(true)));
EXPECT_CALL(*protocol_, SetPosition(8))
.WillOnce(Return(false));
EXPECT_CALL(*protocol_, GetSize(_))
.WillOnce(DoAll(SetArgumentPointee<0>(16), Return(true)));
EXPECT_CALL(*protocol_, SetPosition(8))
.WillOnce(Return(true));
EXPECT_CALL(*protocol_, GetPosition(_))
.WillOnce(DoAll(SetArgumentPointee<0>(8), Return(true)));
EXPECT_EQ(AVERROR(EIO), Seek(-8, SEEK_END));
EXPECT_EQ(AVERROR(EIO), Seek(-8, SEEK_END));
EXPECT_EQ(8, Seek(-8, SEEK_END));
EXPECT_CALL(*protocol_, GetSize(_))
.WillOnce(Return(false));
EXPECT_CALL(*protocol_, GetSize(_))
.WillOnce(DoAll(SetArgumentPointee<0>(16), Return(true)));
EXPECT_EQ(AVERROR(EIO), Seek(0, AVSEEK_SIZE));
EXPECT_EQ(16, Seek(0, AVSEEK_SIZE));
}
TEST_F(FFmpegGlueDestructionTest, WithoutOpen) {
Initialize("ten_byte_file");
}
TEST_F(FFmpegGlueDestructionTest, WithOpenFailure) {
Initialize("ten_byte_file");
ASSERT_FALSE(glue_->OpenContext());
}
TEST_F(FFmpegGlueDestructionTest, WithOpenNoStreams) {
Initialize("no_streams.webm");
ASSERT_TRUE(glue_->OpenContext());
}
TEST_F(FFmpegGlueDestructionTest, WithOpenWithStreams) {
Initialize("bear-320x240.webm");
ASSERT_TRUE(glue_->OpenContext());
}
TEST_F(FFmpegGlueDestructionTest, WithOpenWithOpenStreams) {
Initialize("bear-320x240.webm");
ASSERT_TRUE(glue_->OpenContext());
ASSERT_GT(glue_->format_context()->nb_streams, 0u);
AVCodecContext* context = glue_->format_context()->streams[0]->codec;
ASSERT_EQ(avcodec_open2(
context, avcodec_find_decoder(context->codec_id), NULL), 0);
}
}