blob: 03d8684c64a50ab79320435e66bca453dd532d5e [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "media/base/codec.h"
12#include "rtc_base/gunit.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
htab39db842016-12-08 01:50:48 -080023class TestCodec : public Codec {
24 public:
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010025 TestCodec(int id, const std::string& name, int clockrate)
htab39db842016-12-08 01:50:48 -080026 : Codec(id, name, clockrate) {}
27 TestCodec() : Codec() {}
28 TestCodec(const TestCodec& c) : Codec(c) {}
29};
30
magjed9c41e472016-10-31 09:06:03 -070031TEST(CodecTest, TestCodecOperators) {
htab39db842016-12-08 01:50:48 -080032 TestCodec c0(96, "D", 1000);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000033 c0.SetParam("a", 1);
34
htab39db842016-12-08 01:50:48 -080035 TestCodec c1 = c0;
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000036 EXPECT_TRUE(c1 == c0);
37
38 int param_value0;
39 int param_value1;
40 EXPECT_TRUE(c0.GetParam("a", &param_value0));
41 EXPECT_TRUE(c1.GetParam("a", &param_value1));
42 EXPECT_EQ(param_value0, param_value1);
43
44 c1.id = 86;
45 EXPECT_TRUE(c0 != c1);
46
47 c1 = c0;
48 c1.name = "x";
49 EXPECT_TRUE(c0 != c1);
50
51 c1 = c0;
52 c1.clockrate = 2000;
53 EXPECT_TRUE(c0 != c1);
54
55 c1 = c0;
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000056 c1.SetParam("a", 2);
57 EXPECT_TRUE(c0 != c1);
58
htab39db842016-12-08 01:50:48 -080059 TestCodec c5;
60 TestCodec c6(0, "", 0);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000061 EXPECT_TRUE(c5 == c6);
62}
63
magjed9c41e472016-10-31 09:06:03 -070064TEST(CodecTest, TestAudioCodecOperators) {
deadbeef67cf2c12016-04-13 10:07:16 -070065 AudioCodec c0(96, "A", 44100, 20000, 2);
66 AudioCodec c1(95, "A", 44100, 20000, 2);
67 AudioCodec c2(96, "x", 44100, 20000, 2);
68 AudioCodec c3(96, "A", 48000, 20000, 2);
69 AudioCodec c4(96, "A", 44100, 10000, 2);
70 AudioCodec c5(96, "A", 44100, 20000, 1);
kwibergb94491d2017-02-21 06:16:19 -080071 EXPECT_NE(c0, c1);
72 EXPECT_NE(c0, c2);
73 EXPECT_NE(c0, c3);
74 EXPECT_NE(c0, c4);
75 EXPECT_NE(c0, c5);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076
77 AudioCodec c7;
deadbeef67cf2c12016-04-13 10:07:16 -070078 AudioCodec c8(0, "", 0, 0, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 AudioCodec c9 = c0;
kwibergb94491d2017-02-21 06:16:19 -080080 EXPECT_EQ(c8, c7);
81 EXPECT_NE(c9, c7);
82 EXPECT_EQ(c9, c0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083
84 AudioCodec c10(c0);
85 AudioCodec c11(c0);
86 AudioCodec c12(c0);
87 AudioCodec c13(c0);
88 c10.params["x"] = "abc";
89 c11.params["x"] = "def";
90 c12.params["y"] = "abc";
91 c13.params["x"] = "abc";
kwibergb94491d2017-02-21 06:16:19 -080092 EXPECT_NE(c10, c0);
93 EXPECT_NE(c11, c0);
94 EXPECT_NE(c11, c10);
95 EXPECT_NE(c12, c0);
96 EXPECT_NE(c12, c10);
97 EXPECT_NE(c12, c11);
98 EXPECT_EQ(c13, c10);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099}
100
magjed9c41e472016-10-31 09:06:03 -0700101TEST(CodecTest, TestAudioCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102 // Test a codec with a static payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700103 AudioCodec c0(95, "A", 44100, 20000, 1);
104 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 1)));
105 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 0)));
106 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 0, 0)));
107 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 0, 0, 0)));
108 EXPECT_FALSE(c0.Matches(AudioCodec(96, "", 44100, 20000, 1)));
109 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 20000, 1)));
110 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 30000, 1)));
111 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 20000, 2)));
112 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 30000, 2)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113
114 // Test a codec with a dynamic payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700115 AudioCodec c1(96, "A", 44100, 20000, 1);
116 EXPECT_TRUE(c1.Matches(AudioCodec(96, "A", 0, 0, 0)));
117 EXPECT_TRUE(c1.Matches(AudioCodec(97, "A", 0, 0, 0)));
118 EXPECT_TRUE(c1.Matches(AudioCodec(96, "a", 0, 0, 0)));
119 EXPECT_TRUE(c1.Matches(AudioCodec(97, "a", 0, 0, 0)));
120 EXPECT_FALSE(c1.Matches(AudioCodec(95, "A", 0, 0, 0)));
121 EXPECT_FALSE(c1.Matches(AudioCodec(96, "", 44100, 20000, 2)));
122 EXPECT_FALSE(c1.Matches(AudioCodec(96, "A", 55100, 30000, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123
124 // Test a codec with a dynamic payload type, and auto bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700125 AudioCodec c2(97, "A", 16000, 0, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 // Use default bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700127 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 1)));
128 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 0)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 // Use explicit bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700130 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 32000, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 // Backward compatibility with clients that might send "-1" (for default).
deadbeef67cf2c12016-04-13 10:07:16 -0700132 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, -1, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133
134 // Stereo doesn't match channels = 0.
deadbeef67cf2c12016-04-13 10:07:16 -0700135 AudioCodec c3(96, "A", 44100, 20000, 2);
136 EXPECT_TRUE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 2)));
137 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 1)));
138 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 0)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139}
140
magjed9c41e472016-10-31 09:06:03 -0700141TEST(CodecTest, TestVideoCodecOperators) {
perkj26752742016-10-24 01:21:16 -0700142 VideoCodec c0(96, "V");
143 VideoCodec c1(95, "V");
144 VideoCodec c2(96, "x");
145
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000146 EXPECT_TRUE(c0 != c1);
147 EXPECT_TRUE(c0 != c2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148
149 VideoCodec c7;
perkj26752742016-10-24 01:21:16 -0700150 VideoCodec c8(0, "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 VideoCodec c9 = c0;
152 EXPECT_TRUE(c8 == c7);
153 EXPECT_TRUE(c9 != c7);
154 EXPECT_TRUE(c9 == c0);
155
156 VideoCodec c10(c0);
157 VideoCodec c11(c0);
158 VideoCodec c12(c0);
159 VideoCodec c13(c0);
160 c10.params["x"] = "abc";
161 c11.params["x"] = "def";
162 c12.params["y"] = "abc";
163 c13.params["x"] = "abc";
164 EXPECT_TRUE(c10 != c0);
165 EXPECT_TRUE(c11 != c0);
166 EXPECT_TRUE(c11 != c10);
167 EXPECT_TRUE(c12 != c0);
168 EXPECT_TRUE(c12 != c10);
169 EXPECT_TRUE(c12 != c11);
170 EXPECT_TRUE(c13 == c10);
171}
172
magjed9c41e472016-10-31 09:06:03 -0700173TEST(CodecTest, TestVideoCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 // Test a codec with a static payload type.
perkj26752742016-10-24 01:21:16 -0700175 VideoCodec c0(95, "V");
176 EXPECT_TRUE(c0.Matches(VideoCodec(95, "")));
177 EXPECT_FALSE(c0.Matches(VideoCodec(96, "")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178
179 // Test a codec with a dynamic payload type.
perkj26752742016-10-24 01:21:16 -0700180 VideoCodec c1(96, "V");
181 EXPECT_TRUE(c1.Matches(VideoCodec(96, "V")));
182 EXPECT_TRUE(c1.Matches(VideoCodec(97, "V")));
183 EXPECT_TRUE(c1.Matches(VideoCodec(96, "v")));
184 EXPECT_TRUE(c1.Matches(VideoCodec(97, "v")));
185 EXPECT_FALSE(c1.Matches(VideoCodec(96, "")));
186 EXPECT_FALSE(c1.Matches(VideoCodec(95, "V")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187}
188
magjed9c41e472016-10-31 09:06:03 -0700189TEST(CodecTest, TestDataCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 // Test a codec with a static payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700191 DataCodec c0(95, "D");
192 EXPECT_TRUE(c0.Matches(DataCodec(95, "")));
193 EXPECT_FALSE(c0.Matches(DataCodec(96, "")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194
195 // Test a codec with a dynamic payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700196 DataCodec c1(96, "D");
197 EXPECT_TRUE(c1.Matches(DataCodec(96, "D")));
198 EXPECT_TRUE(c1.Matches(DataCodec(97, "D")));
199 EXPECT_TRUE(c1.Matches(DataCodec(96, "d")));
200 EXPECT_TRUE(c1.Matches(DataCodec(97, "d")));
201 EXPECT_FALSE(c1.Matches(DataCodec(96, "")));
202 EXPECT_FALSE(c1.Matches(DataCodec(95, "D")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000203}
204
magjed9c41e472016-10-31 09:06:03 -0700205TEST(CodecTest, TestSetParamGetParamAndRemoveParam) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 AudioCodec codec;
207 codec.SetParam("a", "1");
208 codec.SetParam("b", "x");
209
210 int int_value = 0;
211 EXPECT_TRUE(codec.GetParam("a", &int_value));
212 EXPECT_EQ(1, int_value);
213 EXPECT_FALSE(codec.GetParam("b", &int_value));
214 EXPECT_FALSE(codec.GetParam("c", &int_value));
215
216 std::string str_value;
217 EXPECT_TRUE(codec.GetParam("a", &str_value));
218 EXPECT_EQ("1", str_value);
219 EXPECT_TRUE(codec.GetParam("b", &str_value));
220 EXPECT_EQ("x", str_value);
221 EXPECT_FALSE(codec.GetParam("c", &str_value));
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000222 EXPECT_TRUE(codec.RemoveParam("a"));
223 EXPECT_FALSE(codec.RemoveParam("c"));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000224}
225
magjed9c41e472016-10-31 09:06:03 -0700226TEST(CodecTest, TestIntersectFeedbackParams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227 const FeedbackParam a1("a", "1");
228 const FeedbackParam b2("b", "2");
229 const FeedbackParam b3("b", "3");
230 const FeedbackParam c3("c", "3");
htab39db842016-12-08 01:50:48 -0800231 TestCodec c1;
Steve Antone78bcb92017-10-31 09:53:08 -0700232 c1.AddFeedbackParam(a1); // Only match with c2.
233 c1.AddFeedbackParam(b2); // Same param different values.
234 c1.AddFeedbackParam(c3); // Not in c2.
htab39db842016-12-08 01:50:48 -0800235 TestCodec c2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236 c2.AddFeedbackParam(a1);
237 c2.AddFeedbackParam(b3);
238
239 c1.IntersectFeedbackParams(c2);
240 EXPECT_TRUE(c1.HasFeedbackParam(a1));
241 EXPECT_FALSE(c1.HasFeedbackParam(b2));
242 EXPECT_FALSE(c1.HasFeedbackParam(c3));
243}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000244
magjed9c41e472016-10-31 09:06:03 -0700245TEST(CodecTest, TestGetCodecType) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000246 // Codec type comparison should be case insenstive on names.
perkj26752742016-10-24 01:21:16 -0700247 const VideoCodec codec(96, "V");
248 const VideoCodec rtx_codec(96, "rTx");
249 const VideoCodec ulpfec_codec(96, "ulpFeC");
brandtr87d7d772016-11-07 03:03:41 -0800250 const VideoCodec flexfec_codec(96, "FlExFeC-03");
perkj26752742016-10-24 01:21:16 -0700251 const VideoCodec red_codec(96, "ReD");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000252 EXPECT_EQ(VideoCodec::CODEC_VIDEO, codec.GetCodecType());
253 EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
254 EXPECT_EQ(VideoCodec::CODEC_ULPFEC, ulpfec_codec.GetCodecType());
brandtr87d7d772016-11-07 03:03:41 -0800255 EXPECT_EQ(VideoCodec::CODEC_FLEXFEC, flexfec_codec.GetCodecType());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000256 EXPECT_EQ(VideoCodec::CODEC_RED, red_codec.GetCodecType());
257}
258
magjed9c41e472016-10-31 09:06:03 -0700259TEST(CodecTest, TestCreateRtxCodec) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000260 VideoCodec rtx_codec = VideoCodec::CreateRtxCodec(96, 120);
261 EXPECT_EQ(96, rtx_codec.id);
262 EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
263 int associated_payload_type;
264 ASSERT_TRUE(rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
265 &associated_payload_type));
266 EXPECT_EQ(120, associated_payload_type);
267}
268
magjed9c41e472016-10-31 09:06:03 -0700269TEST(CodecTest, TestValidateCodecFormat) {
perkj26752742016-10-24 01:21:16 -0700270 const VideoCodec codec(96, "V");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000271 ASSERT_TRUE(codec.ValidateCodecFormat());
272
273 // Accept 0-127 as payload types.
274 VideoCodec low_payload_type = codec;
275 low_payload_type.id = 0;
276 VideoCodec high_payload_type = codec;
277 high_payload_type.id = 127;
278 ASSERT_TRUE(low_payload_type.ValidateCodecFormat());
279 EXPECT_TRUE(high_payload_type.ValidateCodecFormat());
280
281 // Reject negative payloads.
282 VideoCodec negative_payload_type = codec;
283 negative_payload_type.id = -1;
284 EXPECT_FALSE(negative_payload_type.ValidateCodecFormat());
285
286 // Reject too-high payloads.
287 VideoCodec too_high_payload_type = codec;
288 too_high_payload_type.id = 128;
289 EXPECT_FALSE(too_high_payload_type.ValidateCodecFormat());
290
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000291 // Reject codecs with min bitrate > max bitrate.
292 VideoCodec incorrect_bitrates = codec;
293 incorrect_bitrates.params[kCodecParamMinBitrate] = "100";
294 incorrect_bitrates.params[kCodecParamMaxBitrate] = "80";
295 EXPECT_FALSE(incorrect_bitrates.ValidateCodecFormat());
296
297 // Accept min bitrate == max bitrate.
298 VideoCodec equal_bitrates = codec;
299 equal_bitrates.params[kCodecParamMinBitrate] = "100";
300 equal_bitrates.params[kCodecParamMaxBitrate] = "100";
301 EXPECT_TRUE(equal_bitrates.ValidateCodecFormat());
302
303 // Accept min bitrate < max bitrate.
304 VideoCodec different_bitrates = codec;
305 different_bitrates.params[kCodecParamMinBitrate] = "99";
306 different_bitrates.params[kCodecParamMaxBitrate] = "100";
307 EXPECT_TRUE(different_bitrates.ValidateCodecFormat());
308}
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700309
magjed9c41e472016-10-31 09:06:03 -0700310TEST(CodecTest, TestToCodecParameters) {
perkj26752742016-10-24 01:21:16 -0700311 const VideoCodec v(96, "V");
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700312 webrtc::RtpCodecParameters codec_params_1 = v.ToCodecParameters();
313 EXPECT_EQ(96, codec_params_1.payload_type);
deadbeefe702b302017-02-04 12:09:01 -0800314 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, codec_params_1.kind);
315 EXPECT_EQ("V", codec_params_1.name);
Oskar Sundbom78807582017-11-16 11:09:55 +0100316 EXPECT_EQ(cricket::kVideoCodecClockrate, codec_params_1.clock_rate);
317 EXPECT_EQ(rtc::nullopt, codec_params_1.num_channels);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700318
319 const AudioCodec a(97, "A", 44100, 20000, 2);
320 webrtc::RtpCodecParameters codec_params_2 = a.ToCodecParameters();
321 EXPECT_EQ(97, codec_params_2.payload_type);
deadbeefe702b302017-02-04 12:09:01 -0800322 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, codec_params_2.kind);
323 EXPECT_EQ("A", codec_params_2.name);
Oskar Sundbom78807582017-11-16 11:09:55 +0100324 EXPECT_EQ(44100, codec_params_2.clock_rate);
325 EXPECT_EQ(2, codec_params_2.num_channels);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700326}