1 /*
2 * libjingle
3 * Copyright 2009 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "talk/base/gunit.h"
29 #include "talk/media/base/codec.h"
30
31 using cricket::AudioCodec;
32 using cricket::Codec;
33 using cricket::DataCodec;
34 using cricket::FeedbackParam;
35 using cricket::VideoCodec;
36 using cricket::VideoEncoderConfig;
37
38 class CodecTest : public testing::Test {
39 public:
CodecTest()40 CodecTest() {}
41 };
42
TEST_F(CodecTest,TestAudioCodecOperators)43 TEST_F(CodecTest, TestAudioCodecOperators) {
44 AudioCodec c0(96, "A", 44100, 20000, 2, 3);
45 AudioCodec c1(95, "A", 44100, 20000, 2, 3);
46 AudioCodec c2(96, "x", 44100, 20000, 2, 3);
47 AudioCodec c3(96, "A", 48000, 20000, 2, 3);
48 AudioCodec c4(96, "A", 44100, 10000, 2, 3);
49 AudioCodec c5(96, "A", 44100, 20000, 1, 3);
50 AudioCodec c6(96, "A", 44100, 20000, 2, 1);
51 EXPECT_TRUE(c0 != c1);
52 EXPECT_TRUE(c0 != c2);
53 EXPECT_TRUE(c0 != c3);
54 EXPECT_TRUE(c0 != c4);
55 EXPECT_TRUE(c0 != c5);
56 EXPECT_TRUE(c0 != c6);
57
58 AudioCodec c7;
59 AudioCodec c8(0, "", 0, 0, 0, 0);
60 AudioCodec c9 = c0;
61 EXPECT_TRUE(c8 == c7);
62 EXPECT_TRUE(c9 != c7);
63 EXPECT_TRUE(c9 == c0);
64
65 AudioCodec c10(c0);
66 AudioCodec c11(c0);
67 AudioCodec c12(c0);
68 AudioCodec c13(c0);
69 c10.params["x"] = "abc";
70 c11.params["x"] = "def";
71 c12.params["y"] = "abc";
72 c13.params["x"] = "abc";
73 EXPECT_TRUE(c10 != c0);
74 EXPECT_TRUE(c11 != c0);
75 EXPECT_TRUE(c11 != c10);
76 EXPECT_TRUE(c12 != c0);
77 EXPECT_TRUE(c12 != c10);
78 EXPECT_TRUE(c12 != c11);
79 EXPECT_TRUE(c13 == c10);
80 }
81
TEST_F(CodecTest,TestAudioCodecMatches)82 TEST_F(CodecTest, TestAudioCodecMatches) {
83 // Test a codec with a static payload type.
84 AudioCodec c0(95, "A", 44100, 20000, 1, 3);
85 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 1, 0)));
86 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 0, 0)));
87 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 0, 0, 0)));
88 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 0, 0, 0, 0)));
89 EXPECT_FALSE(c0.Matches(AudioCodec(96, "", 44100, 20000, 1, 0)));
90 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 20000, 1, 0)));
91 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 30000, 1, 0)));
92 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 20000, 2, 0)));
93 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 30000, 2, 0)));
94
95 // Test a codec with a dynamic payload type.
96 AudioCodec c1(96, "A", 44100, 20000, 1, 3);
97 EXPECT_TRUE(c1.Matches(AudioCodec(96, "A", 0, 0, 0, 0)));
98 EXPECT_TRUE(c1.Matches(AudioCodec(97, "A", 0, 0, 0, 0)));
99 EXPECT_TRUE(c1.Matches(AudioCodec(96, "a", 0, 0, 0, 0)));
100 EXPECT_TRUE(c1.Matches(AudioCodec(97, "a", 0, 0, 0, 0)));
101 EXPECT_FALSE(c1.Matches(AudioCodec(95, "A", 0, 0, 0, 0)));
102 EXPECT_FALSE(c1.Matches(AudioCodec(96, "", 44100, 20000, 2, 0)));
103 EXPECT_FALSE(c1.Matches(AudioCodec(96, "A", 55100, 30000, 1, 0)));
104
105 // Test a codec with a dynamic payload type, and auto bitrate.
106 AudioCodec c2(97, "A", 16000, 0, 1, 3);
107 // Use default bitrate.
108 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 1, 0)));
109 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 0, 0)));
110 // Use explicit bitrate.
111 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 32000, 1, 0)));
112 // Backward compatibility with clients that might send "-1" (for default).
113 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, -1, 1, 0)));
114
115 // Stereo doesn't match channels = 0.
116 AudioCodec c3(96, "A", 44100, 20000, 2, 3);
117 EXPECT_TRUE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 2, 3)));
118 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 1, 3)));
119 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 0, 3)));
120 }
121
TEST_F(CodecTest,TestVideoCodecOperators)122 TEST_F(CodecTest, TestVideoCodecOperators) {
123 VideoCodec c0(96, "V", 320, 200, 30, 3);
124 VideoCodec c1(95, "V", 320, 200, 30, 3);
125 VideoCodec c2(96, "x", 320, 200, 30, 3);
126 VideoCodec c3(96, "V", 120, 200, 30, 3);
127 VideoCodec c4(96, "V", 320, 100, 30, 3);
128 VideoCodec c5(96, "V", 320, 200, 10, 3);
129 VideoCodec c6(96, "V", 320, 200, 30, 1);
130 EXPECT_TRUE(c0 != c1);
131 EXPECT_TRUE(c0 != c2);
132 EXPECT_TRUE(c0 != c3);
133 EXPECT_TRUE(c0 != c4);
134 EXPECT_TRUE(c0 != c5);
135 EXPECT_TRUE(c0 != c6);
136
137 VideoCodec c7;
138 VideoCodec c8(0, "", 0, 0, 0, 0);
139 VideoCodec c9 = c0;
140 EXPECT_TRUE(c8 == c7);
141 EXPECT_TRUE(c9 != c7);
142 EXPECT_TRUE(c9 == c0);
143
144 VideoCodec c10(c0);
145 VideoCodec c11(c0);
146 VideoCodec c12(c0);
147 VideoCodec c13(c0);
148 c10.params["x"] = "abc";
149 c11.params["x"] = "def";
150 c12.params["y"] = "abc";
151 c13.params["x"] = "abc";
152 EXPECT_TRUE(c10 != c0);
153 EXPECT_TRUE(c11 != c0);
154 EXPECT_TRUE(c11 != c10);
155 EXPECT_TRUE(c12 != c0);
156 EXPECT_TRUE(c12 != c10);
157 EXPECT_TRUE(c12 != c11);
158 EXPECT_TRUE(c13 == c10);
159 }
160
TEST_F(CodecTest,TestVideoCodecMatches)161 TEST_F(CodecTest, TestVideoCodecMatches) {
162 // Test a codec with a static payload type.
163 VideoCodec c0(95, "V", 320, 200, 30, 3);
164 EXPECT_TRUE(c0.Matches(VideoCodec(95, "", 640, 400, 15, 0)));
165 EXPECT_FALSE(c0.Matches(VideoCodec(96, "", 320, 200, 30, 0)));
166
167 // Test a codec with a dynamic payload type.
168 VideoCodec c1(96, "V", 320, 200, 30, 3);
169 EXPECT_TRUE(c1.Matches(VideoCodec(96, "V", 640, 400, 15, 0)));
170 EXPECT_TRUE(c1.Matches(VideoCodec(97, "V", 640, 400, 15, 0)));
171 EXPECT_TRUE(c1.Matches(VideoCodec(96, "v", 640, 400, 15, 0)));
172 EXPECT_TRUE(c1.Matches(VideoCodec(97, "v", 640, 400, 15, 0)));
173 EXPECT_FALSE(c1.Matches(VideoCodec(96, "", 320, 200, 30, 0)));
174 EXPECT_FALSE(c1.Matches(VideoCodec(95, "V", 640, 400, 15, 0)));
175 }
176
TEST_F(CodecTest,TestVideoEncoderConfigOperators)177 TEST_F(CodecTest, TestVideoEncoderConfigOperators) {
178 VideoEncoderConfig c1(VideoCodec(
179 96, "SVC", 320, 200, 30, 3), 1, 2);
180 VideoEncoderConfig c2(VideoCodec(
181 95, "SVC", 320, 200, 30, 3), 1, 2);
182 VideoEncoderConfig c3(VideoCodec(
183 96, "xxx", 320, 200, 30, 3), 1, 2);
184 VideoEncoderConfig c4(VideoCodec(
185 96, "SVC", 120, 200, 30, 3), 1, 2);
186 VideoEncoderConfig c5(VideoCodec(
187 96, "SVC", 320, 100, 30, 3), 1, 2);
188 VideoEncoderConfig c6(VideoCodec(
189 96, "SVC", 320, 200, 10, 3), 1, 2);
190 VideoEncoderConfig c7(VideoCodec(
191 96, "SVC", 320, 200, 30, 1), 1, 2);
192 VideoEncoderConfig c8(VideoCodec(
193 96, "SVC", 320, 200, 30, 3), 0, 2);
194 VideoEncoderConfig c9(VideoCodec(
195 96, "SVC", 320, 200, 30, 3), 1, 1);
196 EXPECT_TRUE(c1 != c2);
197 EXPECT_TRUE(c1 != c2);
198 EXPECT_TRUE(c1 != c3);
199 EXPECT_TRUE(c1 != c4);
200 EXPECT_TRUE(c1 != c5);
201 EXPECT_TRUE(c1 != c6);
202 EXPECT_TRUE(c1 != c7);
203 EXPECT_TRUE(c1 != c8);
204 EXPECT_TRUE(c1 != c9);
205
206 VideoEncoderConfig c10;
207 VideoEncoderConfig c11(VideoCodec(
208 0, "", 0, 0, 0, 0));
209 VideoEncoderConfig c12(VideoCodec(
210 0, "", 0, 0, 0, 0),
211 VideoEncoderConfig::kDefaultMaxThreads,
212 VideoEncoderConfig::kDefaultCpuProfile);
213 VideoEncoderConfig c13 = c1;
214 VideoEncoderConfig c14(VideoCodec(
215 0, "", 0, 0, 0, 0), 0, 0);
216
217 EXPECT_TRUE(c11 == c10);
218 EXPECT_TRUE(c12 == c10);
219 EXPECT_TRUE(c13 != c10);
220 EXPECT_TRUE(c13 == c1);
221 EXPECT_TRUE(c14 != c11);
222 EXPECT_TRUE(c14 != c12);
223 }
224
TEST_F(CodecTest,TestDataCodecMatches)225 TEST_F(CodecTest, TestDataCodecMatches) {
226 // Test a codec with a static payload type.
227 DataCodec c0(95, "D", 0);
228 EXPECT_TRUE(c0.Matches(DataCodec(95, "", 0)));
229 EXPECT_FALSE(c0.Matches(DataCodec(96, "", 0)));
230
231 // Test a codec with a dynamic payload type.
232 DataCodec c1(96, "D", 3);
233 EXPECT_TRUE(c1.Matches(DataCodec(96, "D", 0)));
234 EXPECT_TRUE(c1.Matches(DataCodec(97, "D", 0)));
235 EXPECT_TRUE(c1.Matches(DataCodec(96, "d", 0)));
236 EXPECT_TRUE(c1.Matches(DataCodec(97, "d", 0)));
237 EXPECT_FALSE(c1.Matches(DataCodec(96, "", 0)));
238 EXPECT_FALSE(c1.Matches(DataCodec(95, "D", 0)));
239 }
240
TEST_F(CodecTest,TestDataCodecOperators)241 TEST_F(CodecTest, TestDataCodecOperators) {
242 DataCodec c0(96, "D", 3);
243 DataCodec c1(95, "D", 3);
244 DataCodec c2(96, "x", 3);
245 DataCodec c3(96, "D", 1);
246 EXPECT_TRUE(c0 != c1);
247 EXPECT_TRUE(c0 != c2);
248 EXPECT_TRUE(c0 != c3);
249
250 DataCodec c4;
251 DataCodec c5(0, "", 0);
252 DataCodec c6 = c0;
253 EXPECT_TRUE(c5 == c4);
254 EXPECT_TRUE(c6 != c4);
255 EXPECT_TRUE(c6 == c0);
256 }
257
TEST_F(CodecTest,TestSetParamAndGetParam)258 TEST_F(CodecTest, TestSetParamAndGetParam) {
259 AudioCodec codec;
260 codec.SetParam("a", "1");
261 codec.SetParam("b", "x");
262
263 int int_value = 0;
264 EXPECT_TRUE(codec.GetParam("a", &int_value));
265 EXPECT_EQ(1, int_value);
266 EXPECT_FALSE(codec.GetParam("b", &int_value));
267 EXPECT_FALSE(codec.GetParam("c", &int_value));
268
269 std::string str_value;
270 EXPECT_TRUE(codec.GetParam("a", &str_value));
271 EXPECT_EQ("1", str_value);
272 EXPECT_TRUE(codec.GetParam("b", &str_value));
273 EXPECT_EQ("x", str_value);
274 EXPECT_FALSE(codec.GetParam("c", &str_value));
275 }
276
TEST_F(CodecTest,TestIntersectFeedbackParams)277 TEST_F(CodecTest, TestIntersectFeedbackParams) {
278 const FeedbackParam a1("a", "1");
279 const FeedbackParam b2("b", "2");
280 const FeedbackParam b3("b", "3");
281 const FeedbackParam c3("c", "3");
282 Codec c1;
283 c1.AddFeedbackParam(a1); // Only match with c2.
284 c1.AddFeedbackParam(b2); // Same param different values.
285 c1.AddFeedbackParam(c3); // Not in c2.
286 Codec c2;
287 c2.AddFeedbackParam(a1);
288 c2.AddFeedbackParam(b3);
289
290 c1.IntersectFeedbackParams(c2);
291 EXPECT_TRUE(c1.HasFeedbackParam(a1));
292 EXPECT_FALSE(c1.HasFeedbackParam(b2));
293 EXPECT_FALSE(c1.HasFeedbackParam(c3));
294 }
295