diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-16 18:10:42 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-16 18:10:42 +0000 |
commit | 92e4afc1fbcb3850b505e5caceabbbcca491974a (patch) | |
tree | 8068f2be68df618aade295db81e5be32aeb1b13f /media/base | |
parent | 3d143e9b4beec02c615bd47a6a267a0ba24992cd (diff) | |
download | chromium_src-92e4afc1fbcb3850b505e5caceabbbcca491974a.zip chromium_src-92e4afc1fbcb3850b505e5caceabbbcca491974a.tar.gz chromium_src-92e4afc1fbcb3850b505e5caceabbbcca491974a.tar.bz2 |
Revert 96974 - Remove mock_ffmpeg and update media unittests.
BUG=92429
TEST=BitstreamConverterTest.*, ChunkDemuxerTest.*, FFmpegDemuxerTest.*, FFmpegGlueTest.*, FFmpegVideoDecoderTest.*, FFmpegH264BitstreamConverterTest.*, FFmpegVideoDecodeEngineTest.*
Review URL: http://codereview.chromium.org/7587012
TBR=acolwell@chromium.org
Review URL: http://codereview.chromium.org/7658017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96977 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/base')
-rw-r--r-- | media/base/media.h | 7 | ||||
-rw-r--r-- | media/base/media_posix.cc | 6 | ||||
-rw-r--r-- | media/base/media_win.cc | 6 | ||||
-rw-r--r-- | media/base/mock_ffmpeg.cc | 224 | ||||
-rw-r--r-- | media/base/mock_ffmpeg.h | 162 | ||||
-rw-r--r-- | media/base/run_all_unittests.cc | 9 | ||||
-rw-r--r-- | media/base/test_data_util.cc | 44 | ||||
-rw-r--r-- | media/base/test_data_util.h | 35 |
8 files changed, 389 insertions, 104 deletions
diff --git a/media/base/media.h b/media/base/media.h index bd25cbb..6bbad4c 100644 --- a/media/base/media.h +++ b/media/base/media.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -25,11 +25,6 @@ namespace media { // Returns true if everything was successfully initialized, false otherwise. bool InitializeMediaLibrary(const FilePath& module_dir); -// Helper function for unit tests to avoid boiler plate code everywhere. This -// function will crash if it fails to load the media library. This ensures tests -// fail if the media library is not available. -void InitializeMediaLibraryForTesting(); - // Use this if you need to check whether the media library is initialized // for the this process, without actually trying to initialize it. bool IsMediaLibraryInitialized(); diff --git a/media/base/media_posix.cc b/media/base/media_posix.cc index 7be3255..adad88b 100644 --- a/media/base/media_posix.cc +++ b/media/base/media_posix.cc @@ -92,12 +92,6 @@ bool InitializeMediaLibrary(const FilePath& module_dir) { return g_media_library_is_initialized; } -void InitializeMediaLibraryForTesting() { - FilePath file_path; - CHECK(PathService::Get(base::DIR_EXE, &file_path)); - CHECK(InitializeMediaLibrary(file_path)); -} - bool IsMediaLibraryInitialized() { return g_media_library_is_initialized; } diff --git a/media/base/media_win.cc b/media/base/media_win.cc index 9462c02..a58d977 100644 --- a/media/base/media_win.cc +++ b/media/base/media_win.cc @@ -78,12 +78,6 @@ bool InitializeMediaLibrary(const FilePath& base_path) { return g_media_library_is_initialized; } -void InitializeMediaLibraryForTesting() { - FilePath file_path; - CHECK(PathService::Get(base::DIR_EXE, &file_path)); - CHECK(InitializeMediaLibrary(file_path)); -} - bool IsMediaLibraryInitialized() { return g_media_library_is_initialized; } diff --git a/media/base/mock_ffmpeg.cc b/media/base/mock_ffmpeg.cc new file mode 100644 index 0000000..ebbfbf0 --- /dev/null +++ b/media/base/mock_ffmpeg.cc @@ -0,0 +1,224 @@ +// Copyright (c) 2011 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/mock_ffmpeg.h" + +#include "base/logging.h" +#include "media/ffmpeg/ffmpeg_common.h" + +using ::testing::_; +using ::testing::AtMost; +using ::testing::DoAll; +using ::testing::Return; +using ::testing::SaveArg; + +namespace media { + +MockFFmpeg* MockFFmpeg::instance_ = NULL; +URLProtocol* MockFFmpeg::protocol_ = NULL; + +MockFFmpeg::MockFFmpeg() + : outstanding_packets_(0) { + CHECK(instance_ == NULL) << "Only a single MockFFmpeg instance can exist"; + instance_ = this; + + // If we haven't assigned our static copy of URLProtocol, set up expectations + // to catch the URLProtocol registered when the singleton instance of + // FFmpegGlue is created. + // + // TODO(scherkus): this feels gross and I need to think of a way to better + // inject/mock singletons. + if (!protocol_) { + EXPECT_CALL(*this, AVLogSetLevel(AV_LOG_QUIET)) + .Times(AtMost(1)) + .WillOnce(Return()); + EXPECT_CALL(*this, AVCodecInit()) + .Times(AtMost(1)) + .WillOnce(Return()); + EXPECT_CALL(*this, AVRegisterProtocol2(_,_)) + .Times(AtMost(1)) + .WillOnce(DoAll(SaveArg<0>(&protocol_), Return(0))); + EXPECT_CALL(*this, AVRegisterAll()) + .Times(AtMost(1)) + .WillOnce(Return()); + } +} + +MockFFmpeg::~MockFFmpeg() { + CHECK(!outstanding_packets_) + << "MockFFmpeg destroyed with outstanding packets"; + CHECK(instance_); + instance_ = NULL; +} + +void MockFFmpeg::inc_outstanding_packets() { + ++outstanding_packets_; +} + +void MockFFmpeg::dec_outstanding_packets() { + CHECK(outstanding_packets_ > 0); + --outstanding_packets_; +} + +// static +MockFFmpeg* MockFFmpeg::get() { + return instance_; +} + +// static +URLProtocol* MockFFmpeg::protocol() { + return protocol_; +} + +// static +void MockFFmpeg::DestructPacket(AVPacket* packet) { + delete [] packet->data; + packet->data = NULL; + packet->size = 0; +} + +// FFmpeg stubs that delegate to the FFmpegMock instance. +extern "C" { +void avcodec_init() { + MockFFmpeg::get()->AVCodecInit(); +} + +int av_register_protocol2(URLProtocol* protocol, int size) { + return MockFFmpeg::get()->AVRegisterProtocol2(protocol, size); +} + +void av_register_all() { + MockFFmpeg::get()->AVRegisterAll(); +} + +int av_lockmgr_register(int (*cb)(void**, enum AVLockOp)) { + // Here |mock| may be NULL when this function is called from ~FFmpegGlue(). + if (MockFFmpeg::get()) { + return MockFFmpeg::get()->AVRegisterLockManager(cb); + } + return 0; +} + +AVCodec* avcodec_find_decoder(enum CodecID id) { + return MockFFmpeg::get()->AVCodecFindDecoder(id); +} + +int avcodec_open(AVCodecContext* avctx, AVCodec* codec) { + return MockFFmpeg::get()->AVCodecOpen(avctx, codec); +} + +int avcodec_close(AVCodecContext* avctx) { + return MockFFmpeg::get()->AVCodecClose(avctx); +} + +void avcodec_flush_buffers(AVCodecContext* avctx) { + return MockFFmpeg::get()->AVCodecFlushBuffers(avctx); +} + +AVCodecContext* avcodec_alloc_context() { + return MockFFmpeg::get()->AVCodecAllocContext(); +} + +AVFrame* avcodec_alloc_frame() { + return MockFFmpeg::get()->AVCodecAllocFrame(); +} + +int avcodec_decode_video2(AVCodecContext* avctx, AVFrame* picture, + int* got_picture_ptr, AVPacket* avpkt) { + return MockFFmpeg::get()-> + AVCodecDecodeVideo2(avctx, picture, got_picture_ptr, avpkt); +} + +AVBitStreamFilterContext* av_bitstream_filter_init(const char* name) { + return MockFFmpeg::get()->AVBitstreamFilterInit(name); +} + +int av_bitstream_filter_filter(AVBitStreamFilterContext* bsfc, + AVCodecContext* avctx, + const char* args, + uint8_t** poutbuf, + int* poutbuf_size, + const uint8_t* buf, + int buf_size, + int keyframe) { + return MockFFmpeg::get()-> + AVBitstreamFilterFilter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, + buf_size, keyframe); +} + +void av_bitstream_filter_close(AVBitStreamFilterContext* bsf) { + return MockFFmpeg::get()->AVBitstreamFilterClose(bsf); +} + +int av_open_input_file(AVFormatContext** format, const char* filename, + AVInputFormat* input_format, int buffer_size, + AVFormatParameters* parameters) { + return MockFFmpeg::get()->AVOpenInputFile(format, filename, + input_format, buffer_size, + parameters); +} + +void av_close_input_file(AVFormatContext* format) { + MockFFmpeg::get()->AVCloseInputFile(format); +} + +int av_find_stream_info(AVFormatContext* format) { + return MockFFmpeg::get()->AVFindStreamInfo(format); +} + +int64 av_rescale_q(int64 a, AVRational bq, AVRational cq) { + // Because this is a math function there's little point in mocking it, so we + // implement a cheap version that's capable of overflowing. + int64 num = bq.num * cq.den; + int64 den = cq.num * bq.den; + + // Rescale a by num/den. The den / 2 is for rounding. + return (a * num + den / 2) / den; +} + +int av_read_frame(AVFormatContext* format, AVPacket* packet) { + return MockFFmpeg::get()->AVReadFrame(format, packet); +} + +int av_seek_frame(AVFormatContext *format, int stream_index, int64_t timestamp, + int flags) { + return MockFFmpeg::get()->AVSeekFrame(format, stream_index, timestamp, + flags); +} + +void av_init_packet(AVPacket* pkt) { + return MockFFmpeg::get()->AVInitPacket(pkt); +} + +int av_new_packet(AVPacket* packet, int size) { + return MockFFmpeg::get()->AVNewPacket(packet, size); +} + +void av_free_packet(AVPacket* packet) { + MockFFmpeg::get()->AVFreePacket(packet); +} + +void av_free(void* ptr) { + // Freeing NULL pointers are valid, but they aren't interesting from a mock + // perspective. + if (ptr) { + MockFFmpeg::get()->AVFree(ptr); + } +} + +int av_dup_packet(AVPacket* packet) { + return MockFFmpeg::get()->AVDupPacket(packet); +} + +void av_log_set_level(int level) { + MockFFmpeg::get()->AVLogSetLevel(level); +} + +void av_destruct_packet(AVPacket *pkt) { + MockFFmpeg::get()->AVDestructPacket(pkt); +} + +} // extern "C" + +} // namespace media diff --git a/media/base/mock_ffmpeg.h b/media/base/mock_ffmpeg.h new file mode 100644 index 0000000..df5e69b4 --- /dev/null +++ b/media/base/mock_ffmpeg.h @@ -0,0 +1,162 @@ +// Copyright (c) 2011 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_MOCK_FFMPEG_H_ +#define MEDIA_BASE_MOCK_FFMPEG_H_ + +// TODO(scherkus): See if we can remove ffmpeg_common from this file. +#include "media/ffmpeg/ffmpeg_common.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace media { + +class MockFFmpeg { + public: + MockFFmpeg(); + virtual ~MockFFmpeg(); + + // TODO(ajwong): Organize this class, and make sure that all mock entrypoints + // are still used. + MOCK_METHOD0(AVCodecInit, void()); + MOCK_METHOD2(AVRegisterProtocol2, int(URLProtocol* protocol, int size)); + MOCK_METHOD0(AVRegisterAll, void()); + MOCK_METHOD1(AVRegisterLockManager, int(int (*cb)(void**, enum AVLockOp))); + + MOCK_METHOD1(AVCodecFindDecoder, AVCodec*(enum CodecID id)); + MOCK_METHOD2(AVCodecOpen, int(AVCodecContext* avctx, AVCodec* codec)); + MOCK_METHOD1(AVCodecClose, int(AVCodecContext* avctx)); + MOCK_METHOD2(AVCodecThreadInit, int(AVCodecContext* avctx, int threads)); + MOCK_METHOD1(AVCodecFlushBuffers, void(AVCodecContext* avctx)); + MOCK_METHOD0(AVCodecAllocContext, AVCodecContext*()); + MOCK_METHOD0(AVCodecAllocFrame, AVFrame*()); + MOCK_METHOD4(AVCodecDecodeVideo2, + int(AVCodecContext* avctx, AVFrame* picture, + int* got_picture_ptr, AVPacket* avpkt)); + MOCK_METHOD1(AVBitstreamFilterInit, + AVBitStreamFilterContext*(const char *name)); + MOCK_METHOD8(AVBitstreamFilterFilter, + int(AVBitStreamFilterContext* bsfc, AVCodecContext* avctx, + const char* args, uint8_t** poutbuf, int* poutbuf_size, + const uint8_t* buf, int buf_size, int keyframe)); + MOCK_METHOD1(AVBitstreamFilterClose, void(AVBitStreamFilterContext* bsf)); + MOCK_METHOD1(AVDestructPacket, void(AVPacket* packet)); + + MOCK_METHOD5(AVOpenInputFile, int(AVFormatContext** format, + const char* filename, + AVInputFormat* input_format, + int buffer_size, + AVFormatParameters* parameters)); + MOCK_METHOD1(AVCloseInputFile, void(AVFormatContext* format)); + MOCK_METHOD1(AVFindStreamInfo, int(AVFormatContext* format)); + MOCK_METHOD2(AVReadFrame, int(AVFormatContext* format, AVPacket* packet)); + MOCK_METHOD4(AVSeekFrame, int(AVFormatContext *format, + int stream_index, + int64_t timestamp, + int flags)); + + MOCK_METHOD1(AVInitPacket, void(AVPacket* pkt)); + MOCK_METHOD2(AVNewPacket, int(AVPacket* packet, int size)); + MOCK_METHOD1(AVFreePacket, void(AVPacket* packet)); + MOCK_METHOD1(AVFree, void(void* ptr)); + MOCK_METHOD1(AVDupPacket, int(AVPacket* packet)); + + MOCK_METHOD1(AVLogSetLevel, void(int level)); + + // Used for verifying check points during tests. + MOCK_METHOD1(CheckPoint, void(int id)); + + // Returns the current MockFFmpeg instance. + static MockFFmpeg* get(); + + // Returns the URLProtocol registered by the FFmpegGlue singleton. + static URLProtocol* protocol(); + + // AVPacket destructor for packets allocated by av_new_packet(). + static void DestructPacket(AVPacket* packet); + + // Modifies the number of outstanding packets. + void inc_outstanding_packets(); + void dec_outstanding_packets(); + + private: + static MockFFmpeg* instance_; + static URLProtocol* protocol_; + + // Tracks the number of packets allocated by calls to av_read_frame() and + // av_free_packet(). We crash the unit test if this is not zero at time of + // destruction. + int outstanding_packets_; +}; + +// Used for simulating av_read_frame(). +ACTION_P3(CreatePacket, stream_index, data, size) { + // Confirm we're dealing with AVPacket so we can safely const_cast<>. + ::testing::StaticAssertTypeEq<AVPacket*, arg1_type>(); + memset(arg1, 0, sizeof(*arg1)); + arg1->stream_index = stream_index; + arg1->data = const_cast<uint8*>(data); + arg1->size = size; + + // Increment number of packets allocated. + MockFFmpeg::get()->inc_outstanding_packets(); + + return 0; +} + +// Used for simulating av_read_frame(). +ACTION_P3(CreatePacketNoCount, stream_index, data, size) { + // Confirm we're dealing with AVPacket so we can safely const_cast<>. + ::testing::StaticAssertTypeEq<AVPacket*, arg1_type>(); + memset(arg1, 0, sizeof(*arg1)); + arg1->stream_index = stream_index; + arg1->data = const_cast<uint8*>(data); + arg1->size = size; + + return 0; +} + +// Used for simulating av_read_frame(). +ACTION_P4(CreatePacketTimeNoCount, stream_index, data, size, pts) { + // Confirm we're dealing with AVPacket so we can safely const_cast<>. + ::testing::StaticAssertTypeEq<AVPacket*, arg1_type>(); + memset(arg1, 0, sizeof(*arg1)); + arg1->stream_index = stream_index; + arg1->data = const_cast<uint8*>(data); + arg1->size = size; + arg1->pts = pts; + + return 0; +} + +// Used for simulating av_new_packet(). +ACTION(NewPacket) { + ::testing::StaticAssertTypeEq<AVPacket*, arg0_type>(); + int size = arg1; + memset(arg0, 0, sizeof(*arg0)); + arg0->data = new uint8[size]; + arg0->size = size; + arg0->destruct = &MockFFmpeg::DestructPacket; + + // Increment number of packets allocated. + MockFFmpeg::get()->inc_outstanding_packets(); + + return 0; +} + +// Used for simulating av_free_packet(). +ACTION(FreePacket) { + ::testing::StaticAssertTypeEq<AVPacket*, arg0_type>(); + + // Call the destructor if present, such as the one assigned in NewPacket(). + if (arg0->destruct) { + arg0->destruct(arg0); + } + + // Decrement number of packets allocated. + MockFFmpeg::get()->dec_outstanding_packets(); +} + +} // namespace media + +#endif // MEDIA_BASE_MOCK_FFMPEG_H_ diff --git a/media/base/run_all_unittests.cc b/media/base/run_all_unittests.cc index a24a278..b715a328b 100644 --- a/media/base/run_all_unittests.cc +++ b/media/base/run_all_unittests.cc @@ -1,14 +1,9 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2006-2008 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 "base/test/test_suite.h" -#include "media/base/media.h" int main(int argc, char** argv) { - base::TestSuite suite(argc, argv); - - media::InitializeMediaLibraryForTesting(); - - return suite.Run(); + return base::TestSuite(argc, argv).Run(); } diff --git a/media/base/test_data_util.cc b/media/base/test_data_util.cc deleted file mode 100644 index fc30744..0000000 --- a/media/base/test_data_util.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) 2011 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/test_data_util.h" - -#include "base/file_util.h" -#include "base/logging.h" -#include "base/path_service.h" - -namespace media { - -void ReadTestDataFile(const std::string& name, scoped_array<uint8>* buffer, - int* size) { - FilePath file_path; - CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &file_path)); - - file_path = file_path.Append(FILE_PATH_LITERAL("media")) - .Append(FILE_PATH_LITERAL("test")) - .Append(FILE_PATH_LITERAL("data")) - .AppendASCII(name); - - int64 tmp = 0; - CHECK(file_util::GetFileSize(file_path, &tmp)) - << "Failed to get file size for '" << name << "'"; - - int file_size = static_cast<int>(tmp); - buffer->reset(new uint8[file_size]); - - CHECK(file_size == file_util::ReadFile(file_path, - reinterpret_cast<char*>(buffer->get()), - file_size)) - << "Failed to read '" << name << "'"; - *size = file_size; -} - -void ReadTestDataFile(const std::string& name, scoped_refptr<Buffer>* buffer) { - scoped_array<uint8> buf; - int buf_size; - ReadTestDataFile(name, &buf, &buf_size); - *buffer = new DataBuffer(buf.release(), buf_size); -} - -} // namespace media diff --git a/media/base/test_data_util.h b/media/base/test_data_util.h deleted file mode 100644 index 42878ae..0000000 --- a/media/base/test_data_util.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2011 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_TEST_DATA_UTIL_H_ -#define MEDIA_BASE_TEST_DATA_UTIL_H_ - -#include <string> - -#include "base/basictypes.h" -#include "base/scoped_ptr.h" -#include "media/base/data_buffer.h" - -namespace media { - -// Reads a test file from media/test/data directory and stores it in -// a scoped_array. -// -// |name| - The name of the file. -// |buffer| - The contents of the file. -// |size| - The size of the buffer. -void ReadTestDataFile(const std::string& name, - scoped_array<uint8>* buffer, - int* size); - -// Reads a test file from media/test/data directory and stored it in -// a Buffer. -// -// |name| - The name of the file. -// |buffer| - The contents of the file. -void ReadTestDataFile(const std::string& name, scoped_refptr<Buffer>* buffer); - -} // namespace media - -#endif // MEDIA_BASE_TEST_DATA_UTIL_H_ |