blob: 8dd2e44f0048571576efc0a7e7831d586aa365fd [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2009 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000011#include "webrtc/base/gunit.h"
kjellandera96e2d72016-02-04 23:52:28 -080012#include "webrtc/media/base/codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14using cricket::AudioCodec;
15using cricket::Codec;
16using cricket::DataCodec;
17using cricket::FeedbackParam;
18using cricket::VideoCodec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000019using cricket::kCodecParamAssociatedPayloadType;
20using cricket::kCodecParamMaxBitrate;
21using cricket::kCodecParamMinBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000022
magjed9c41e472016-10-31 09:06:03 -070023TEST(CodecTest, TestCodecOperators) {
deadbeef67cf2c12016-04-13 10:07:16 -070024 Codec c0(96, "D", 1000);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000025 c0.SetParam("a", 1);
26
27 Codec c1 = c0;
28 EXPECT_TRUE(c1 == c0);
29
30 int param_value0;
31 int param_value1;
32 EXPECT_TRUE(c0.GetParam("a", &param_value0));
33 EXPECT_TRUE(c1.GetParam("a", &param_value1));
34 EXPECT_EQ(param_value0, param_value1);
35
36 c1.id = 86;
37 EXPECT_TRUE(c0 != c1);
38
39 c1 = c0;
40 c1.name = "x";
41 EXPECT_TRUE(c0 != c1);
42
43 c1 = c0;
44 c1.clockrate = 2000;
45 EXPECT_TRUE(c0 != c1);
46
47 c1 = c0;
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000048 c1.SetParam("a", 2);
49 EXPECT_TRUE(c0 != c1);
50
51 Codec c5;
deadbeef67cf2c12016-04-13 10:07:16 -070052 Codec c6(0, "", 0);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000053 EXPECT_TRUE(c5 == c6);
54}
55
magjed9c41e472016-10-31 09:06:03 -070056TEST(CodecTest, TestAudioCodecOperators) {
deadbeef67cf2c12016-04-13 10:07:16 -070057 AudioCodec c0(96, "A", 44100, 20000, 2);
58 AudioCodec c1(95, "A", 44100, 20000, 2);
59 AudioCodec c2(96, "x", 44100, 20000, 2);
60 AudioCodec c3(96, "A", 48000, 20000, 2);
61 AudioCodec c4(96, "A", 44100, 10000, 2);
62 AudioCodec c5(96, "A", 44100, 20000, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 EXPECT_TRUE(c0 != c1);
64 EXPECT_TRUE(c0 != c2);
65 EXPECT_TRUE(c0 != c3);
66 EXPECT_TRUE(c0 != c4);
67 EXPECT_TRUE(c0 != c5);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068
69 AudioCodec c7;
deadbeef67cf2c12016-04-13 10:07:16 -070070 AudioCodec c8(0, "", 0, 0, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 AudioCodec c9 = c0;
72 EXPECT_TRUE(c8 == c7);
73 EXPECT_TRUE(c9 != c7);
74 EXPECT_TRUE(c9 == c0);
75
76 AudioCodec c10(c0);
77 AudioCodec c11(c0);
78 AudioCodec c12(c0);
79 AudioCodec c13(c0);
80 c10.params["x"] = "abc";
81 c11.params["x"] = "def";
82 c12.params["y"] = "abc";
83 c13.params["x"] = "abc";
84 EXPECT_TRUE(c10 != c0);
85 EXPECT_TRUE(c11 != c0);
86 EXPECT_TRUE(c11 != c10);
87 EXPECT_TRUE(c12 != c0);
88 EXPECT_TRUE(c12 != c10);
89 EXPECT_TRUE(c12 != c11);
90 EXPECT_TRUE(c13 == c10);
91}
92
magjed9c41e472016-10-31 09:06:03 -070093TEST(CodecTest, TestAudioCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 // Test a codec with a static payload type.
deadbeef67cf2c12016-04-13 10:07:16 -070095 AudioCodec c0(95, "A", 44100, 20000, 1);
96 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 1)));
97 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 0)));
98 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 0, 0)));
99 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 0, 0, 0)));
100 EXPECT_FALSE(c0.Matches(AudioCodec(96, "", 44100, 20000, 1)));
101 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 20000, 1)));
102 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 30000, 1)));
103 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 20000, 2)));
104 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 30000, 2)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
106 // Test a codec with a dynamic payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700107 AudioCodec c1(96, "A", 44100, 20000, 1);
108 EXPECT_TRUE(c1.Matches(AudioCodec(96, "A", 0, 0, 0)));
109 EXPECT_TRUE(c1.Matches(AudioCodec(97, "A", 0, 0, 0)));
110 EXPECT_TRUE(c1.Matches(AudioCodec(96, "a", 0, 0, 0)));
111 EXPECT_TRUE(c1.Matches(AudioCodec(97, "a", 0, 0, 0)));
112 EXPECT_FALSE(c1.Matches(AudioCodec(95, "A", 0, 0, 0)));
113 EXPECT_FALSE(c1.Matches(AudioCodec(96, "", 44100, 20000, 2)));
114 EXPECT_FALSE(c1.Matches(AudioCodec(96, "A", 55100, 30000, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
116 // Test a codec with a dynamic payload type, and auto bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700117 AudioCodec c2(97, "A", 16000, 0, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118 // Use default bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700119 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 1)));
120 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 0)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 // Use explicit bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700122 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 32000, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 // Backward compatibility with clients that might send "-1" (for default).
deadbeef67cf2c12016-04-13 10:07:16 -0700124 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, -1, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125
126 // Stereo doesn't match channels = 0.
deadbeef67cf2c12016-04-13 10:07:16 -0700127 AudioCodec c3(96, "A", 44100, 20000, 2);
128 EXPECT_TRUE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 2)));
129 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 1)));
130 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 0)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131}
132
magjed9c41e472016-10-31 09:06:03 -0700133TEST(CodecTest, TestVideoCodecOperators) {
perkj26752742016-10-24 01:21:16 -0700134 VideoCodec c0(96, "V");
135 VideoCodec c1(95, "V");
136 VideoCodec c2(96, "x");
137
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 EXPECT_TRUE(c0 != c1);
139 EXPECT_TRUE(c0 != c2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140
141 VideoCodec c7;
perkj26752742016-10-24 01:21:16 -0700142 VideoCodec c8(0, "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 VideoCodec c9 = c0;
144 EXPECT_TRUE(c8 == c7);
145 EXPECT_TRUE(c9 != c7);
146 EXPECT_TRUE(c9 == c0);
147
148 VideoCodec c10(c0);
149 VideoCodec c11(c0);
150 VideoCodec c12(c0);
151 VideoCodec c13(c0);
152 c10.params["x"] = "abc";
153 c11.params["x"] = "def";
154 c12.params["y"] = "abc";
155 c13.params["x"] = "abc";
156 EXPECT_TRUE(c10 != c0);
157 EXPECT_TRUE(c11 != c0);
158 EXPECT_TRUE(c11 != c10);
159 EXPECT_TRUE(c12 != c0);
160 EXPECT_TRUE(c12 != c10);
161 EXPECT_TRUE(c12 != c11);
162 EXPECT_TRUE(c13 == c10);
163}
164
magjed9c41e472016-10-31 09:06:03 -0700165TEST(CodecTest, TestVideoCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 // Test a codec with a static payload type.
perkj26752742016-10-24 01:21:16 -0700167 VideoCodec c0(95, "V");
168 EXPECT_TRUE(c0.Matches(VideoCodec(95, "")));
169 EXPECT_FALSE(c0.Matches(VideoCodec(96, "")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170
171 // Test a codec with a dynamic payload type.
perkj26752742016-10-24 01:21:16 -0700172 VideoCodec c1(96, "V");
173 EXPECT_TRUE(c1.Matches(VideoCodec(96, "V")));
174 EXPECT_TRUE(c1.Matches(VideoCodec(97, "V")));
175 EXPECT_TRUE(c1.Matches(VideoCodec(96, "v")));
176 EXPECT_TRUE(c1.Matches(VideoCodec(97, "v")));
177 EXPECT_FALSE(c1.Matches(VideoCodec(96, "")));
178 EXPECT_FALSE(c1.Matches(VideoCodec(95, "V")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179}
180
magjed9c41e472016-10-31 09:06:03 -0700181TEST(CodecTest, TestDataCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 // Test a codec with a static payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700183 DataCodec c0(95, "D");
184 EXPECT_TRUE(c0.Matches(DataCodec(95, "")));
185 EXPECT_FALSE(c0.Matches(DataCodec(96, "")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186
187 // Test a codec with a dynamic payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700188 DataCodec c1(96, "D");
189 EXPECT_TRUE(c1.Matches(DataCodec(96, "D")));
190 EXPECT_TRUE(c1.Matches(DataCodec(97, "D")));
191 EXPECT_TRUE(c1.Matches(DataCodec(96, "d")));
192 EXPECT_TRUE(c1.Matches(DataCodec(97, "d")));
193 EXPECT_FALSE(c1.Matches(DataCodec(96, "")));
194 EXPECT_FALSE(c1.Matches(DataCodec(95, "D")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195}
196
magjed9c41e472016-10-31 09:06:03 -0700197TEST(CodecTest, TestSetParamGetParamAndRemoveParam) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 AudioCodec codec;
199 codec.SetParam("a", "1");
200 codec.SetParam("b", "x");
201
202 int int_value = 0;
203 EXPECT_TRUE(codec.GetParam("a", &int_value));
204 EXPECT_EQ(1, int_value);
205 EXPECT_FALSE(codec.GetParam("b", &int_value));
206 EXPECT_FALSE(codec.GetParam("c", &int_value));
207
208 std::string str_value;
209 EXPECT_TRUE(codec.GetParam("a", &str_value));
210 EXPECT_EQ("1", str_value);
211 EXPECT_TRUE(codec.GetParam("b", &str_value));
212 EXPECT_EQ("x", str_value);
213 EXPECT_FALSE(codec.GetParam("c", &str_value));
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000214 EXPECT_TRUE(codec.RemoveParam("a"));
215 EXPECT_FALSE(codec.RemoveParam("c"));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000216}
217
magjed9c41e472016-10-31 09:06:03 -0700218TEST(CodecTest, TestIntersectFeedbackParams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000219 const FeedbackParam a1("a", "1");
220 const FeedbackParam b2("b", "2");
221 const FeedbackParam b3("b", "3");
222 const FeedbackParam c3("c", "3");
223 Codec c1;
224 c1.AddFeedbackParam(a1); // Only match with c2.
225 c1.AddFeedbackParam(b2); // Same param different values.
226 c1.AddFeedbackParam(c3); // Not in c2.
227 Codec c2;
228 c2.AddFeedbackParam(a1);
229 c2.AddFeedbackParam(b3);
230
231 c1.IntersectFeedbackParams(c2);
232 EXPECT_TRUE(c1.HasFeedbackParam(a1));
233 EXPECT_FALSE(c1.HasFeedbackParam(b2));
234 EXPECT_FALSE(c1.HasFeedbackParam(c3));
235}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000236
magjed9c41e472016-10-31 09:06:03 -0700237TEST(CodecTest, TestGetCodecType) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000238 // Codec type comparison should be case insenstive on names.
perkj26752742016-10-24 01:21:16 -0700239 const VideoCodec codec(96, "V");
240 const VideoCodec rtx_codec(96, "rTx");
241 const VideoCodec ulpfec_codec(96, "ulpFeC");
brandtr87d7d772016-11-07 03:03:41 -0800242 const VideoCodec flexfec_codec(96, "FlExFeC-03");
perkj26752742016-10-24 01:21:16 -0700243 const VideoCodec red_codec(96, "ReD");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000244 EXPECT_EQ(VideoCodec::CODEC_VIDEO, codec.GetCodecType());
245 EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
246 EXPECT_EQ(VideoCodec::CODEC_ULPFEC, ulpfec_codec.GetCodecType());
brandtr87d7d772016-11-07 03:03:41 -0800247 EXPECT_EQ(VideoCodec::CODEC_FLEXFEC, flexfec_codec.GetCodecType());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000248 EXPECT_EQ(VideoCodec::CODEC_RED, red_codec.GetCodecType());
249}
250
magjed9c41e472016-10-31 09:06:03 -0700251TEST(CodecTest, TestCreateRtxCodec) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000252 VideoCodec rtx_codec = VideoCodec::CreateRtxCodec(96, 120);
253 EXPECT_EQ(96, rtx_codec.id);
254 EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
255 int associated_payload_type;
256 ASSERT_TRUE(rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
257 &associated_payload_type));
258 EXPECT_EQ(120, associated_payload_type);
259}
260
magjed9c41e472016-10-31 09:06:03 -0700261TEST(CodecTest, TestValidateCodecFormat) {
perkj26752742016-10-24 01:21:16 -0700262 const VideoCodec codec(96, "V");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000263 ASSERT_TRUE(codec.ValidateCodecFormat());
264
265 // Accept 0-127 as payload types.
266 VideoCodec low_payload_type = codec;
267 low_payload_type.id = 0;
268 VideoCodec high_payload_type = codec;
269 high_payload_type.id = 127;
270 ASSERT_TRUE(low_payload_type.ValidateCodecFormat());
271 EXPECT_TRUE(high_payload_type.ValidateCodecFormat());
272
273 // Reject negative payloads.
274 VideoCodec negative_payload_type = codec;
275 negative_payload_type.id = -1;
276 EXPECT_FALSE(negative_payload_type.ValidateCodecFormat());
277
278 // Reject too-high payloads.
279 VideoCodec too_high_payload_type = codec;
280 too_high_payload_type.id = 128;
281 EXPECT_FALSE(too_high_payload_type.ValidateCodecFormat());
282
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000283 // Reject codecs with min bitrate > max bitrate.
284 VideoCodec incorrect_bitrates = codec;
285 incorrect_bitrates.params[kCodecParamMinBitrate] = "100";
286 incorrect_bitrates.params[kCodecParamMaxBitrate] = "80";
287 EXPECT_FALSE(incorrect_bitrates.ValidateCodecFormat());
288
289 // Accept min bitrate == max bitrate.
290 VideoCodec equal_bitrates = codec;
291 equal_bitrates.params[kCodecParamMinBitrate] = "100";
292 equal_bitrates.params[kCodecParamMaxBitrate] = "100";
293 EXPECT_TRUE(equal_bitrates.ValidateCodecFormat());
294
295 // Accept min bitrate < max bitrate.
296 VideoCodec different_bitrates = codec;
297 different_bitrates.params[kCodecParamMinBitrate] = "99";
298 different_bitrates.params[kCodecParamMaxBitrate] = "100";
299 EXPECT_TRUE(different_bitrates.ValidateCodecFormat());
300}
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700301
magjed9c41e472016-10-31 09:06:03 -0700302TEST(CodecTest, TestToCodecParameters) {
perkj26752742016-10-24 01:21:16 -0700303 const VideoCodec v(96, "V");
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700304 webrtc::RtpCodecParameters codec_params_1 = v.ToCodecParameters();
305 EXPECT_EQ(96, codec_params_1.payload_type);
306 EXPECT_EQ("V", codec_params_1.mime_type);
307 EXPECT_EQ(cricket::kVideoCodecClockrate, codec_params_1.clock_rate);
308 EXPECT_EQ(1, codec_params_1.channels);
309
310 const AudioCodec a(97, "A", 44100, 20000, 2);
311 webrtc::RtpCodecParameters codec_params_2 = a.ToCodecParameters();
312 EXPECT_EQ(97, codec_params_2.payload_type);
313 EXPECT_EQ("A", codec_params_2.mime_type);
314 EXPECT_EQ(44100, codec_params_2.clock_rate);
315 EXPECT_EQ(2, codec_params_2.channels);
316}