OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 <deque> | |
6 | |
7 #include "media/ffmpeg/ffmpeg_common.h" | |
8 #include "media/filters/bitstream_converter.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace media { | |
12 | |
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; | |
20 | |
21 | |
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; | |
46 } | |
47 | |
48 return -1; | |
49 } | |
50 | |
51 static void DoClose(AVBitStreamFilterContext* bsfc) {} | |
52 | |
53 static bool g_stream_filter_registered = false; | |
54 static AVBitStreamFilter g_stream_filter = { | |
55 kTestFilterName, | |
56 0, // Private Data Size | |
57 DoFilter, | |
58 DoClose, | |
59 0, // Next filter pointer. | |
60 }; | |
61 | |
62 class BitstreamConverterTest : public testing::Test { | |
63 protected: | |
64 BitstreamConverterTest() { | |
65 memset(&test_stream_context_, 0, sizeof(test_stream_context_)); | |
66 memset(&test_packet_, 0, sizeof(test_packet_)); | |
67 test_packet_.data = kFailData; | |
68 test_packet_.size = kFailSize; | |
69 } | |
70 | |
71 virtual ~BitstreamConverterTest() { | |
72 av_free_packet(&test_packet_); | |
73 } | |
74 | |
75 static void SetUpTestCase() { | |
76 // Register g_stream_filter if it isn't already registered. | |
77 if (!g_stream_filter_registered) { | |
78 av_register_bitstream_filter(&g_stream_filter); | |
79 g_stream_filter_registered = true; | |
80 } | |
81 } | |
82 | |
83 AVCodecContext test_stream_context_; | |
84 AVPacket test_packet_; | |
85 | |
86 private: | |
87 DISALLOW_COPY_AND_ASSIGN(BitstreamConverterTest); | |
88 }; | |
89 | |
90 TEST_F(BitstreamConverterTest, InitializeFailed) { | |
91 FFmpegBitstreamConverter converter("BAD_FILTER_NAME", &test_stream_context_); | |
92 | |
93 EXPECT_FALSE(converter.Initialize()); | |
94 } | |
95 | |
96 TEST_F(BitstreamConverterTest, InitializeSuccess) { | |
97 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_); | |
98 EXPECT_TRUE(converter.Initialize()); | |
99 } | |
100 | |
101 TEST_F(BitstreamConverterTest, ConvertPacket_NotInitialized) { | |
102 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_); | |
103 | |
104 EXPECT_FALSE(converter.ConvertPacket(&test_packet_)); | |
105 } | |
106 | |
107 TEST_F(BitstreamConverterTest, ConvertPacket_FailedFilter) { | |
108 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_); | |
109 | |
110 EXPECT_TRUE(converter.Initialize()); | |
111 | |
112 EXPECT_FALSE(converter.ConvertPacket(&test_packet_)); | |
113 } | |
114 | |
115 TEST_F(BitstreamConverterTest, ConvertPacket_Success) { | |
116 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_); | |
117 | |
118 EXPECT_TRUE(converter.Initialize()); | |
119 | |
120 // Ensure our packet doesn't already have a destructor. | |
121 ASSERT_TRUE(test_packet_.destruct == NULL); | |
122 | |
123 test_packet_.data = kNewBufferData; | |
124 test_packet_.size = kNewBufferSize; | |
125 | |
126 EXPECT_TRUE(converter.ConvertPacket(&test_packet_)); | |
127 EXPECT_NE(kNewBufferData, test_packet_.data); | |
128 EXPECT_EQ(kNewBufferSize + 1, test_packet_.size); | |
129 EXPECT_TRUE(test_packet_.destruct != NULL); | |
130 } | |
131 | |
132 TEST_F(BitstreamConverterTest, ConvertPacket_SuccessInPlace) { | |
133 FFmpegBitstreamConverter converter(kTestFilterName, &test_stream_context_); | |
134 | |
135 EXPECT_TRUE(converter.Initialize()); | |
136 | |
137 // Ensure our packet is in a sane start state. | |
138 ASSERT_TRUE(test_packet_.destruct == NULL); | |
139 test_packet_.data = kInPlaceData; | |
140 test_packet_.size = kInPlaceSize; | |
141 | |
142 EXPECT_TRUE(converter.ConvertPacket(&test_packet_)); | |
143 EXPECT_EQ(kInPlaceData, test_packet_.data); | |
144 EXPECT_EQ(kInPlaceSize, test_packet_.size); | |
145 EXPECT_TRUE(test_packet_.destruct == NULL); | |
146 } | |
147 | |
148 } // namespace media | |
OLD | NEW |