Updating trunk VERSION from 935.0 to 936.0
[chromium-blink-merge.git] / media / filters / bitstream_converter_unittest.cc
blob3330df7b45101bced995aee0f9f7872c9cd4e7d9
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.
5 #include <deque>
7 #include "media/ffmpeg/ffmpeg_common.h"
8 #include "media/filters/bitstream_converter.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace media {
13 static const char kTestFilterName[] = "test_filter";
14 static uint8_t kFailData[] = { 3, 2, 1 };
15 static uint8_t kNewBufferData[] = { 2, 1 };
16 static uint8_t kInPlaceData[] = { 1 };
17 static const int kFailSize = 3;
18 static const int kNewBufferSize = 2;
19 static const int kInPlaceSize = 1;
22 // Test filter function that looks for specific input data and changes it's
23 // behavior based on what is passed to |buf| & |buf_size|. The three behaviors
24 // simulated are the following:
25 // - Create a new output buffer. Triggered by |buf| == |kNewBufferData|.
26 // - Use the existing output buffer. Triggered by |buf| == |kInPlaceData|.
27 // - Signal an error. Triggered by |buf| == |kFailData|.
28 static int DoFilter(AVBitStreamFilterContext* bsfc,
29 AVCodecContext* avctx,
30 const char* args,
31 uint8_t** poutbuf,
32 int* poutbuf_size,
33 const uint8_t* buf,
34 int buf_size,
35 int keyframe) {
36 if (buf_size == kNewBufferSize &&
37 !memcmp(buf, kNewBufferData, kNewBufferSize)) {
38 *poutbuf_size = buf_size + 1;
39 *poutbuf = static_cast<uint8*>(av_malloc(*poutbuf_size));
40 *poutbuf[0] = 0;
41 memcpy((*poutbuf) + 1, buf, buf_size);
42 return 0;
43 } else if (buf_size == kInPlaceSize &&
44 !memcmp(buf, kInPlaceData, kInPlaceSize)) {
45 return 0;
48 return -1;
51 static void DoClose(AVBitStreamFilterContext* bsfc) {}
53 static AVBitStreamFilter g_stream_filter = {
54 kTestFilterName,
55 0, // Private Data Size
56 DoFilter,
57 DoClose,
58 0, // Next filter pointer.
61 class BitstreamConverterTest : public testing::Test {
62 protected:
63 BitstreamConverterTest() {
64 memset(&test_stream_context_, 0, sizeof(test_stream_context_));
65 memset(&test_packet_, 0, sizeof(test_packet_));
66 test_packet_.data = kFailData;
67 test_packet_.size = kFailSize;
70 virtual ~BitstreamConverterTest() {
71 av_free_packet(&test_packet_);
74 static void SetUpTestCase() {
75 // Register g_stream_filter if it isn't already registered.
76 if (!g_stream_filter.next)
77 av_register_bitstream_filter(&g_stream_filter);
80 AVCodecContext test_stream_context_;
81 AVPacket test_packet_;
83 private:
84 DISALLOW_COPY_AND_ASSIGN(BitstreamConverterTest);
87 TEST_F(BitstreamConverterTest, InitializeFailed) {
88 FFmpegBitstreamConverter converter("BAD_FILTER_NAME", &test_stream_context_);
90 EXPECT_FALSE(converter.Initialize());
93 TEST_F(BitstreamConverterTest, InitializeSuccess) {
94 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_);
95 EXPECT_TRUE(converter.Initialize());
98 TEST_F(BitstreamConverterTest, ConvertPacket_NotInitialized) {
99 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_);
101 EXPECT_FALSE(converter.ConvertPacket(&test_packet_));
104 TEST_F(BitstreamConverterTest, ConvertPacket_FailedFilter) {
105 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_);
107 EXPECT_TRUE(converter.Initialize());
109 EXPECT_FALSE(converter.ConvertPacket(&test_packet_));
112 TEST_F(BitstreamConverterTest, ConvertPacket_Success) {
113 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_);
115 EXPECT_TRUE(converter.Initialize());
117 // Ensure our packet doesn't already have a destructor.
118 ASSERT_TRUE(test_packet_.destruct == NULL);
120 test_packet_.data = kNewBufferData;
121 test_packet_.size = kNewBufferSize;
123 EXPECT_TRUE(converter.ConvertPacket(&test_packet_));
124 EXPECT_NE(kNewBufferData, test_packet_.data);
125 EXPECT_EQ(kNewBufferSize + 1, test_packet_.size);
126 EXPECT_TRUE(test_packet_.destruct != NULL);
129 TEST_F(BitstreamConverterTest, ConvertPacket_SuccessInPlace) {
130 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_);
132 EXPECT_TRUE(converter.Initialize());
134 // Ensure our packet is in a sane start state.
135 ASSERT_TRUE(test_packet_.destruct == NULL);
136 test_packet_.data = kInPlaceData;
137 test_packet_.size = kInPlaceSize;
139 EXPECT_TRUE(converter.ConvertPacket(&test_packet_));
140 EXPECT_EQ(kInPlaceData, test_packet_.data);
141 EXPECT_EQ(kInPlaceSize, test_packet_.size);
142 EXPECT_TRUE(test_packet_.destruct == NULL);
145 } // namespace media