| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/base/mock_ffmpeg.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "media/ffmpeg/ffmpeg_common.h" | |
| 9 | |
| 10 using ::testing::_; | |
| 11 using ::testing::AtMost; | |
| 12 using ::testing::DoAll; | |
| 13 using ::testing::Return; | |
| 14 using ::testing::SaveArg; | |
| 15 | |
| 16 namespace media { | |
| 17 | |
| 18 MockFFmpeg* MockFFmpeg::instance_ = NULL; | |
| 19 URLProtocol* MockFFmpeg::protocol_ = NULL; | |
| 20 | |
| 21 MockFFmpeg::MockFFmpeg() | |
| 22 : outstanding_packets_(0) { | |
| 23 CHECK(instance_ == NULL) << "Only a single MockFFmpeg instance can exist"; | |
| 24 instance_ = this; | |
| 25 | |
| 26 // If we haven't assigned our static copy of URLProtocol, set up expectations | |
| 27 // to catch the URLProtocol registered when the singleton instance of | |
| 28 // FFmpegGlue is created. | |
| 29 // | |
| 30 // TODO(scherkus): this feels gross and I need to think of a way to better | |
| 31 // inject/mock singletons. | |
| 32 if (!protocol_) { | |
| 33 EXPECT_CALL(*this, AVLogSetLevel(AV_LOG_QUIET)) | |
| 34 .Times(AtMost(1)) | |
| 35 .WillOnce(Return()); | |
| 36 EXPECT_CALL(*this, AVCodecInit()) | |
| 37 .Times(AtMost(1)) | |
| 38 .WillOnce(Return()); | |
| 39 EXPECT_CALL(*this, AVRegisterProtocol2(_,_)) | |
| 40 .Times(AtMost(1)) | |
| 41 .WillOnce(DoAll(SaveArg<0>(&protocol_), Return(0))); | |
| 42 EXPECT_CALL(*this, AVRegisterAll()) | |
| 43 .Times(AtMost(1)) | |
| 44 .WillOnce(Return()); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 MockFFmpeg::~MockFFmpeg() { | |
| 49 CHECK(!outstanding_packets_) | |
| 50 << "MockFFmpeg destroyed with outstanding packets"; | |
| 51 CHECK(instance_); | |
| 52 instance_ = NULL; | |
| 53 } | |
| 54 | |
| 55 void MockFFmpeg::inc_outstanding_packets() { | |
| 56 ++outstanding_packets_; | |
| 57 } | |
| 58 | |
| 59 void MockFFmpeg::dec_outstanding_packets() { | |
| 60 CHECK(outstanding_packets_ > 0); | |
| 61 --outstanding_packets_; | |
| 62 } | |
| 63 | |
| 64 // static | |
| 65 MockFFmpeg* MockFFmpeg::get() { | |
| 66 return instance_; | |
| 67 } | |
| 68 | |
| 69 // static | |
| 70 URLProtocol* MockFFmpeg::protocol() { | |
| 71 return protocol_; | |
| 72 } | |
| 73 | |
| 74 // static | |
| 75 void MockFFmpeg::DestructPacket(AVPacket* packet) { | |
| 76 delete [] packet->data; | |
| 77 packet->data = NULL; | |
| 78 packet->size = 0; | |
| 79 } | |
| 80 | |
| 81 // FFmpeg stubs that delegate to the FFmpegMock instance. | |
| 82 extern "C" { | |
| 83 void avcodec_init() { | |
| 84 MockFFmpeg::get()->AVCodecInit(); | |
| 85 } | |
| 86 | |
| 87 int av_register_protocol2(URLProtocol* protocol, int size) { | |
| 88 return MockFFmpeg::get()->AVRegisterProtocol2(protocol, size); | |
| 89 } | |
| 90 | |
| 91 void av_register_all() { | |
| 92 MockFFmpeg::get()->AVRegisterAll(); | |
| 93 } | |
| 94 | |
| 95 int av_lockmgr_register(int (*cb)(void**, enum AVLockOp)) { | |
| 96 // Here |mock| may be NULL when this function is called from ~FFmpegGlue(). | |
| 97 if (MockFFmpeg::get()) { | |
| 98 return MockFFmpeg::get()->AVRegisterLockManager(cb); | |
| 99 } | |
| 100 return 0; | |
| 101 } | |
| 102 | |
| 103 AVCodec* avcodec_find_decoder(enum CodecID id) { | |
| 104 return MockFFmpeg::get()->AVCodecFindDecoder(id); | |
| 105 } | |
| 106 | |
| 107 int avcodec_open(AVCodecContext* avctx, AVCodec* codec) { | |
| 108 return MockFFmpeg::get()->AVCodecOpen(avctx, codec); | |
| 109 } | |
| 110 | |
| 111 int avcodec_close(AVCodecContext* avctx) { | |
| 112 return MockFFmpeg::get()->AVCodecClose(avctx); | |
| 113 } | |
| 114 | |
| 115 void avcodec_flush_buffers(AVCodecContext* avctx) { | |
| 116 return MockFFmpeg::get()->AVCodecFlushBuffers(avctx); | |
| 117 } | |
| 118 | |
| 119 AVCodecContext* avcodec_alloc_context() { | |
| 120 return MockFFmpeg::get()->AVCodecAllocContext(); | |
| 121 } | |
| 122 | |
| 123 AVFrame* avcodec_alloc_frame() { | |
| 124 return MockFFmpeg::get()->AVCodecAllocFrame(); | |
| 125 } | |
| 126 | |
| 127 int avcodec_decode_video2(AVCodecContext* avctx, AVFrame* picture, | |
| 128 int* got_picture_ptr, AVPacket* avpkt) { | |
| 129 return MockFFmpeg::get()-> | |
| 130 AVCodecDecodeVideo2(avctx, picture, got_picture_ptr, avpkt); | |
| 131 } | |
| 132 | |
| 133 AVBitStreamFilterContext* av_bitstream_filter_init(const char* name) { | |
| 134 return MockFFmpeg::get()->AVBitstreamFilterInit(name); | |
| 135 } | |
| 136 | |
| 137 int av_bitstream_filter_filter(AVBitStreamFilterContext* bsfc, | |
| 138 AVCodecContext* avctx, | |
| 139 const char* args, | |
| 140 uint8_t** poutbuf, | |
| 141 int* poutbuf_size, | |
| 142 const uint8_t* buf, | |
| 143 int buf_size, | |
| 144 int keyframe) { | |
| 145 return MockFFmpeg::get()-> | |
| 146 AVBitstreamFilterFilter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, | |
| 147 buf_size, keyframe); | |
| 148 } | |
| 149 | |
| 150 void av_bitstream_filter_close(AVBitStreamFilterContext* bsf) { | |
| 151 return MockFFmpeg::get()->AVBitstreamFilterClose(bsf); | |
| 152 } | |
| 153 | |
| 154 int av_open_input_file(AVFormatContext** format, const char* filename, | |
| 155 AVInputFormat* input_format, int buffer_size, | |
| 156 AVFormatParameters* parameters) { | |
| 157 return MockFFmpeg::get()->AVOpenInputFile(format, filename, | |
| 158 input_format, buffer_size, | |
| 159 parameters); | |
| 160 } | |
| 161 | |
| 162 void av_close_input_file(AVFormatContext* format) { | |
| 163 MockFFmpeg::get()->AVCloseInputFile(format); | |
| 164 } | |
| 165 | |
| 166 int av_find_stream_info(AVFormatContext* format) { | |
| 167 return MockFFmpeg::get()->AVFindStreamInfo(format); | |
| 168 } | |
| 169 | |
| 170 int64 av_rescale_q(int64 a, AVRational bq, AVRational cq) { | |
| 171 // Because this is a math function there's little point in mocking it, so we | |
| 172 // implement a cheap version that's capable of overflowing. | |
| 173 int64 num = bq.num * cq.den; | |
| 174 int64 den = cq.num * bq.den; | |
| 175 | |
| 176 // Rescale a by num/den. The den / 2 is for rounding. | |
| 177 return (a * num + den / 2) / den; | |
| 178 } | |
| 179 | |
| 180 int av_read_frame(AVFormatContext* format, AVPacket* packet) { | |
| 181 return MockFFmpeg::get()->AVReadFrame(format, packet); | |
| 182 } | |
| 183 | |
| 184 int av_seek_frame(AVFormatContext *format, int stream_index, int64_t timestamp, | |
| 185 int flags) { | |
| 186 return MockFFmpeg::get()->AVSeekFrame(format, stream_index, timestamp, | |
| 187 flags); | |
| 188 } | |
| 189 | |
| 190 void av_init_packet(AVPacket* pkt) { | |
| 191 return MockFFmpeg::get()->AVInitPacket(pkt); | |
| 192 } | |
| 193 | |
| 194 int av_new_packet(AVPacket* packet, int size) { | |
| 195 return MockFFmpeg::get()->AVNewPacket(packet, size); | |
| 196 } | |
| 197 | |
| 198 void av_free_packet(AVPacket* packet) { | |
| 199 MockFFmpeg::get()->AVFreePacket(packet); | |
| 200 } | |
| 201 | |
| 202 void av_free(void* ptr) { | |
| 203 // Freeing NULL pointers are valid, but they aren't interesting from a mock | |
| 204 // perspective. | |
| 205 if (ptr) { | |
| 206 MockFFmpeg::get()->AVFree(ptr); | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 int av_dup_packet(AVPacket* packet) { | |
| 211 return MockFFmpeg::get()->AVDupPacket(packet); | |
| 212 } | |
| 213 | |
| 214 void av_log_set_level(int level) { | |
| 215 MockFFmpeg::get()->AVLogSetLevel(level); | |
| 216 } | |
| 217 | |
| 218 void av_destruct_packet(AVPacket *pkt) { | |
| 219 MockFFmpeg::get()->AVDestructPacket(pkt); | |
| 220 } | |
| 221 | |
| 222 } // extern "C" | |
| 223 | |
| 224 } // namespace media | |
| OLD | NEW |