blob: f2bf4c70dc1feb0394e07245380b11a5f1ff35f3 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
31using cricket::AudioCodec;
32using cricket::Codec;
33using cricket::DataCodec;
34using cricket::FeedbackParam;
35using cricket::VideoCodec;
36using cricket::VideoEncoderConfig;
37
38class CodecTest : public testing::Test {
39 public:
40 CodecTest() {}
41};
42
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000043TEST_F(CodecTest, TestCodecOperators) {
44 Codec c0(96, "D", 1000, 0);
45 c0.SetParam("a", 1);
46
47 Codec c1 = c0;
48 EXPECT_TRUE(c1 == c0);
49
50 int param_value0;
51 int param_value1;
52 EXPECT_TRUE(c0.GetParam("a", &param_value0));
53 EXPECT_TRUE(c1.GetParam("a", &param_value1));
54 EXPECT_EQ(param_value0, param_value1);
55
56 c1.id = 86;
57 EXPECT_TRUE(c0 != c1);
58
59 c1 = c0;
60 c1.name = "x";
61 EXPECT_TRUE(c0 != c1);
62
63 c1 = c0;
64 c1.clockrate = 2000;
65 EXPECT_TRUE(c0 != c1);
66
67 c1 = c0;
68 c1.preference = 1;
69 EXPECT_TRUE(c0 != c1);
70
71 c1 = c0;
72 c1.SetParam("a", 2);
73 EXPECT_TRUE(c0 != c1);
74
75 Codec c5;
76 Codec c6(0, "", 0, 0);
77 EXPECT_TRUE(c5 == c6);
78}
79
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080TEST_F(CodecTest, TestAudioCodecOperators) {
81 AudioCodec c0(96, "A", 44100, 20000, 2, 3);
82 AudioCodec c1(95, "A", 44100, 20000, 2, 3);
83 AudioCodec c2(96, "x", 44100, 20000, 2, 3);
84 AudioCodec c3(96, "A", 48000, 20000, 2, 3);
85 AudioCodec c4(96, "A", 44100, 10000, 2, 3);
86 AudioCodec c5(96, "A", 44100, 20000, 1, 3);
87 AudioCodec c6(96, "A", 44100, 20000, 2, 1);
88 EXPECT_TRUE(c0 != c1);
89 EXPECT_TRUE(c0 != c2);
90 EXPECT_TRUE(c0 != c3);
91 EXPECT_TRUE(c0 != c4);
92 EXPECT_TRUE(c0 != c5);
93 EXPECT_TRUE(c0 != c6);
94
95 AudioCodec c7;
96 AudioCodec c8(0, "", 0, 0, 0, 0);
97 AudioCodec c9 = c0;
98 EXPECT_TRUE(c8 == c7);
99 EXPECT_TRUE(c9 != c7);
100 EXPECT_TRUE(c9 == c0);
101
102 AudioCodec c10(c0);
103 AudioCodec c11(c0);
104 AudioCodec c12(c0);
105 AudioCodec c13(c0);
106 c10.params["x"] = "abc";
107 c11.params["x"] = "def";
108 c12.params["y"] = "abc";
109 c13.params["x"] = "abc";
110 EXPECT_TRUE(c10 != c0);
111 EXPECT_TRUE(c11 != c0);
112 EXPECT_TRUE(c11 != c10);
113 EXPECT_TRUE(c12 != c0);
114 EXPECT_TRUE(c12 != c10);
115 EXPECT_TRUE(c12 != c11);
116 EXPECT_TRUE(c13 == c10);
117}
118
119TEST_F(CodecTest, TestAudioCodecMatches) {
120 // Test a codec with a static payload type.
121 AudioCodec c0(95, "A", 44100, 20000, 1, 3);
122 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 1, 0)));
123 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 0, 0)));
124 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 0, 0, 0)));
125 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 0, 0, 0, 0)));
126 EXPECT_FALSE(c0.Matches(AudioCodec(96, "", 44100, 20000, 1, 0)));
127 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 20000, 1, 0)));
128 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 30000, 1, 0)));
129 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 20000, 2, 0)));
130 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 30000, 2, 0)));
131
132 // Test a codec with a dynamic payload type.
133 AudioCodec c1(96, "A", 44100, 20000, 1, 3);
134 EXPECT_TRUE(c1.Matches(AudioCodec(96, "A", 0, 0, 0, 0)));
135 EXPECT_TRUE(c1.Matches(AudioCodec(97, "A", 0, 0, 0, 0)));
136 EXPECT_TRUE(c1.Matches(AudioCodec(96, "a", 0, 0, 0, 0)));
137 EXPECT_TRUE(c1.Matches(AudioCodec(97, "a", 0, 0, 0, 0)));
138 EXPECT_FALSE(c1.Matches(AudioCodec(95, "A", 0, 0, 0, 0)));
139 EXPECT_FALSE(c1.Matches(AudioCodec(96, "", 44100, 20000, 2, 0)));
140 EXPECT_FALSE(c1.Matches(AudioCodec(96, "A", 55100, 30000, 1, 0)));
141
142 // Test a codec with a dynamic payload type, and auto bitrate.
143 AudioCodec c2(97, "A", 16000, 0, 1, 3);
144 // Use default bitrate.
145 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 1, 0)));
146 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 0, 0)));
147 // Use explicit bitrate.
148 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 32000, 1, 0)));
149 // Backward compatibility with clients that might send "-1" (for default).
150 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, -1, 1, 0)));
151
152 // Stereo doesn't match channels = 0.
153 AudioCodec c3(96, "A", 44100, 20000, 2, 3);
154 EXPECT_TRUE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 2, 3)));
155 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 1, 3)));
156 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 0, 3)));
157}
158
159TEST_F(CodecTest, TestVideoCodecOperators) {
160 VideoCodec c0(96, "V", 320, 200, 30, 3);
161 VideoCodec c1(95, "V", 320, 200, 30, 3);
162 VideoCodec c2(96, "x", 320, 200, 30, 3);
163 VideoCodec c3(96, "V", 120, 200, 30, 3);
164 VideoCodec c4(96, "V", 320, 100, 30, 3);
165 VideoCodec c5(96, "V", 320, 200, 10, 3);
166 VideoCodec c6(96, "V", 320, 200, 30, 1);
167 EXPECT_TRUE(c0 != c1);
168 EXPECT_TRUE(c0 != c2);
169 EXPECT_TRUE(c0 != c3);
170 EXPECT_TRUE(c0 != c4);
171 EXPECT_TRUE(c0 != c5);
172 EXPECT_TRUE(c0 != c6);
173
174 VideoCodec c7;
175 VideoCodec c8(0, "", 0, 0, 0, 0);
176 VideoCodec c9 = c0;
177 EXPECT_TRUE(c8 == c7);
178 EXPECT_TRUE(c9 != c7);
179 EXPECT_TRUE(c9 == c0);
180
181 VideoCodec c10(c0);
182 VideoCodec c11(c0);
183 VideoCodec c12(c0);
184 VideoCodec c13(c0);
185 c10.params["x"] = "abc";
186 c11.params["x"] = "def";
187 c12.params["y"] = "abc";
188 c13.params["x"] = "abc";
189 EXPECT_TRUE(c10 != c0);
190 EXPECT_TRUE(c11 != c0);
191 EXPECT_TRUE(c11 != c10);
192 EXPECT_TRUE(c12 != c0);
193 EXPECT_TRUE(c12 != c10);
194 EXPECT_TRUE(c12 != c11);
195 EXPECT_TRUE(c13 == c10);
196}
197
198TEST_F(CodecTest, TestVideoCodecMatches) {
199 // Test a codec with a static payload type.
200 VideoCodec c0(95, "V", 320, 200, 30, 3);
201 EXPECT_TRUE(c0.Matches(VideoCodec(95, "", 640, 400, 15, 0)));
202 EXPECT_FALSE(c0.Matches(VideoCodec(96, "", 320, 200, 30, 0)));
203
204 // Test a codec with a dynamic payload type.
205 VideoCodec c1(96, "V", 320, 200, 30, 3);
206 EXPECT_TRUE(c1.Matches(VideoCodec(96, "V", 640, 400, 15, 0)));
207 EXPECT_TRUE(c1.Matches(VideoCodec(97, "V", 640, 400, 15, 0)));
208 EXPECT_TRUE(c1.Matches(VideoCodec(96, "v", 640, 400, 15, 0)));
209 EXPECT_TRUE(c1.Matches(VideoCodec(97, "v", 640, 400, 15, 0)));
210 EXPECT_FALSE(c1.Matches(VideoCodec(96, "", 320, 200, 30, 0)));
211 EXPECT_FALSE(c1.Matches(VideoCodec(95, "V", 640, 400, 15, 0)));
212}
213
214TEST_F(CodecTest, TestVideoEncoderConfigOperators) {
215 VideoEncoderConfig c1(VideoCodec(
216 96, "SVC", 320, 200, 30, 3), 1, 2);
217 VideoEncoderConfig c2(VideoCodec(
218 95, "SVC", 320, 200, 30, 3), 1, 2);
219 VideoEncoderConfig c3(VideoCodec(
220 96, "xxx", 320, 200, 30, 3), 1, 2);
221 VideoEncoderConfig c4(VideoCodec(
222 96, "SVC", 120, 200, 30, 3), 1, 2);
223 VideoEncoderConfig c5(VideoCodec(
224 96, "SVC", 320, 100, 30, 3), 1, 2);
225 VideoEncoderConfig c6(VideoCodec(
226 96, "SVC", 320, 200, 10, 3), 1, 2);
227 VideoEncoderConfig c7(VideoCodec(
228 96, "SVC", 320, 200, 30, 1), 1, 2);
229 VideoEncoderConfig c8(VideoCodec(
230 96, "SVC", 320, 200, 30, 3), 0, 2);
231 VideoEncoderConfig c9(VideoCodec(
232 96, "SVC", 320, 200, 30, 3), 1, 1);
233 EXPECT_TRUE(c1 != c2);
234 EXPECT_TRUE(c1 != c2);
235 EXPECT_TRUE(c1 != c3);
236 EXPECT_TRUE(c1 != c4);
237 EXPECT_TRUE(c1 != c5);
238 EXPECT_TRUE(c1 != c6);
239 EXPECT_TRUE(c1 != c7);
240 EXPECT_TRUE(c1 != c8);
241 EXPECT_TRUE(c1 != c9);
242
243 VideoEncoderConfig c10;
244 VideoEncoderConfig c11(VideoCodec(
245 0, "", 0, 0, 0, 0));
246 VideoEncoderConfig c12(VideoCodec(
247 0, "", 0, 0, 0, 0),
248 VideoEncoderConfig::kDefaultMaxThreads,
249 VideoEncoderConfig::kDefaultCpuProfile);
250 VideoEncoderConfig c13 = c1;
251 VideoEncoderConfig c14(VideoCodec(
252 0, "", 0, 0, 0, 0), 0, 0);
253
254 EXPECT_TRUE(c11 == c10);
255 EXPECT_TRUE(c12 == c10);
256 EXPECT_TRUE(c13 != c10);
257 EXPECT_TRUE(c13 == c1);
258 EXPECT_TRUE(c14 != c11);
259 EXPECT_TRUE(c14 != c12);
260}
261
262TEST_F(CodecTest, TestDataCodecMatches) {
263 // Test a codec with a static payload type.
264 DataCodec c0(95, "D", 0);
265 EXPECT_TRUE(c0.Matches(DataCodec(95, "", 0)));
266 EXPECT_FALSE(c0.Matches(DataCodec(96, "", 0)));
267
268 // Test a codec with a dynamic payload type.
269 DataCodec c1(96, "D", 3);
270 EXPECT_TRUE(c1.Matches(DataCodec(96, "D", 0)));
271 EXPECT_TRUE(c1.Matches(DataCodec(97, "D", 0)));
272 EXPECT_TRUE(c1.Matches(DataCodec(96, "d", 0)));
273 EXPECT_TRUE(c1.Matches(DataCodec(97, "d", 0)));
274 EXPECT_FALSE(c1.Matches(DataCodec(96, "", 0)));
275 EXPECT_FALSE(c1.Matches(DataCodec(95, "D", 0)));
276}
277
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278TEST_F(CodecTest, TestSetParamAndGetParam) {
279 AudioCodec codec;
280 codec.SetParam("a", "1");
281 codec.SetParam("b", "x");
282
283 int int_value = 0;
284 EXPECT_TRUE(codec.GetParam("a", &int_value));
285 EXPECT_EQ(1, int_value);
286 EXPECT_FALSE(codec.GetParam("b", &int_value));
287 EXPECT_FALSE(codec.GetParam("c", &int_value));
288
289 std::string str_value;
290 EXPECT_TRUE(codec.GetParam("a", &str_value));
291 EXPECT_EQ("1", str_value);
292 EXPECT_TRUE(codec.GetParam("b", &str_value));
293 EXPECT_EQ("x", str_value);
294 EXPECT_FALSE(codec.GetParam("c", &str_value));
295}
296
297TEST_F(CodecTest, TestIntersectFeedbackParams) {
298 const FeedbackParam a1("a", "1");
299 const FeedbackParam b2("b", "2");
300 const FeedbackParam b3("b", "3");
301 const FeedbackParam c3("c", "3");
302 Codec c1;
303 c1.AddFeedbackParam(a1); // Only match with c2.
304 c1.AddFeedbackParam(b2); // Same param different values.
305 c1.AddFeedbackParam(c3); // Not in c2.
306 Codec c2;
307 c2.AddFeedbackParam(a1);
308 c2.AddFeedbackParam(b3);
309
310 c1.IntersectFeedbackParams(c2);
311 EXPECT_TRUE(c1.HasFeedbackParam(a1));
312 EXPECT_FALSE(c1.HasFeedbackParam(b2));
313 EXPECT_FALSE(c1.HasFeedbackParam(c3));
314}