blob: bf0addb5a354ff4eb13cf75a8184da7a9f1591ec [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"
Emircan Uysaler98badbc2018-06-28 10:59:02 -070012
Steve Anton2dbc6272019-05-31 13:08:58 -070013#include <tuple>
14
Emircan Uysaler98badbc2018-06-28 10:59:02 -070015#include "media/base/vp9_profile.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/gunit.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017
18using cricket::AudioCodec;
19using cricket::Codec;
20using cricket::DataCodec;
21using cricket::FeedbackParam;
22using cricket::VideoCodec;
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +000023using cricket::kCodecParamAssociatedPayloadType;
24using cricket::kCodecParamMaxBitrate;
25using cricket::kCodecParamMinBitrate;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000026
htab39db842016-12-08 01:50:48 -080027class TestCodec : public Codec {
28 public:
Mirko Bonadeic61ce0d2017-11-21 17:04:20 +010029 TestCodec(int id, const std::string& name, int clockrate)
htab39db842016-12-08 01:50:48 -080030 : Codec(id, name, clockrate) {}
31 TestCodec() : Codec() {}
32 TestCodec(const TestCodec& c) : Codec(c) {}
33};
34
magjed9c41e472016-10-31 09:06:03 -070035TEST(CodecTest, TestCodecOperators) {
htab39db842016-12-08 01:50:48 -080036 TestCodec c0(96, "D", 1000);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000037 c0.SetParam("a", 1);
38
htab39db842016-12-08 01:50:48 -080039 TestCodec c1 = c0;
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000040 EXPECT_TRUE(c1 == c0);
41
42 int param_value0;
43 int param_value1;
44 EXPECT_TRUE(c0.GetParam("a", &param_value0));
45 EXPECT_TRUE(c1.GetParam("a", &param_value1));
46 EXPECT_EQ(param_value0, param_value1);
47
48 c1.id = 86;
49 EXPECT_TRUE(c0 != c1);
50
51 c1 = c0;
52 c1.name = "x";
53 EXPECT_TRUE(c0 != c1);
54
55 c1 = c0;
56 c1.clockrate = 2000;
57 EXPECT_TRUE(c0 != c1);
58
59 c1 = c0;
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000060 c1.SetParam("a", 2);
61 EXPECT_TRUE(c0 != c1);
62
htab39db842016-12-08 01:50:48 -080063 TestCodec c5;
64 TestCodec c6(0, "", 0);
jiayl@webrtc.org9c16c392014-05-01 18:30:30 +000065 EXPECT_TRUE(c5 == c6);
66}
67
magjed9c41e472016-10-31 09:06:03 -070068TEST(CodecTest, TestAudioCodecOperators) {
deadbeef67cf2c12016-04-13 10:07:16 -070069 AudioCodec c0(96, "A", 44100, 20000, 2);
70 AudioCodec c1(95, "A", 44100, 20000, 2);
71 AudioCodec c2(96, "x", 44100, 20000, 2);
72 AudioCodec c3(96, "A", 48000, 20000, 2);
73 AudioCodec c4(96, "A", 44100, 10000, 2);
74 AudioCodec c5(96, "A", 44100, 20000, 1);
kwibergb94491d2017-02-21 06:16:19 -080075 EXPECT_NE(c0, c1);
76 EXPECT_NE(c0, c2);
77 EXPECT_NE(c0, c3);
78 EXPECT_NE(c0, c4);
79 EXPECT_NE(c0, c5);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080
81 AudioCodec c7;
deadbeef67cf2c12016-04-13 10:07:16 -070082 AudioCodec c8(0, "", 0, 0, 0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083 AudioCodec c9 = c0;
kwibergb94491d2017-02-21 06:16:19 -080084 EXPECT_EQ(c8, c7);
85 EXPECT_NE(c9, c7);
86 EXPECT_EQ(c9, c0);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087
88 AudioCodec c10(c0);
89 AudioCodec c11(c0);
90 AudioCodec c12(c0);
91 AudioCodec c13(c0);
92 c10.params["x"] = "abc";
93 c11.params["x"] = "def";
94 c12.params["y"] = "abc";
95 c13.params["x"] = "abc";
kwibergb94491d2017-02-21 06:16:19 -080096 EXPECT_NE(c10, c0);
97 EXPECT_NE(c11, c0);
98 EXPECT_NE(c11, c10);
99 EXPECT_NE(c12, c0);
100 EXPECT_NE(c12, c10);
101 EXPECT_NE(c12, c11);
102 EXPECT_EQ(c13, c10);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000103}
104
magjed9c41e472016-10-31 09:06:03 -0700105TEST(CodecTest, TestAudioCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 // Test a codec with a static payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700107 AudioCodec c0(95, "A", 44100, 20000, 1);
108 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 1)));
109 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 0)));
110 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 0, 0)));
111 EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 0, 0, 0)));
112 EXPECT_FALSE(c0.Matches(AudioCodec(96, "", 44100, 20000, 1)));
113 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 20000, 1)));
114 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 30000, 1)));
115 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 20000, 2)));
116 EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 30000, 2)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117
118 // Test a codec with a dynamic payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700119 AudioCodec c1(96, "A", 44100, 20000, 1);
120 EXPECT_TRUE(c1.Matches(AudioCodec(96, "A", 0, 0, 0)));
121 EXPECT_TRUE(c1.Matches(AudioCodec(97, "A", 0, 0, 0)));
122 EXPECT_TRUE(c1.Matches(AudioCodec(96, "a", 0, 0, 0)));
123 EXPECT_TRUE(c1.Matches(AudioCodec(97, "a", 0, 0, 0)));
124 EXPECT_FALSE(c1.Matches(AudioCodec(95, "A", 0, 0, 0)));
125 EXPECT_FALSE(c1.Matches(AudioCodec(96, "", 44100, 20000, 2)));
126 EXPECT_FALSE(c1.Matches(AudioCodec(96, "A", 55100, 30000, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127
128 // Test a codec with a dynamic payload type, and auto bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700129 AudioCodec c2(97, "A", 16000, 0, 1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 // Use default bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700131 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 1)));
132 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 0)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 // Use explicit bitrate.
deadbeef67cf2c12016-04-13 10:07:16 -0700134 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 32000, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 // Backward compatibility with clients that might send "-1" (for default).
deadbeef67cf2c12016-04-13 10:07:16 -0700136 EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, -1, 1)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000137
138 // Stereo doesn't match channels = 0.
deadbeef67cf2c12016-04-13 10:07:16 -0700139 AudioCodec c3(96, "A", 44100, 20000, 2);
140 EXPECT_TRUE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 2)));
141 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 1)));
142 EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 0)));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143}
144
magjed9c41e472016-10-31 09:06:03 -0700145TEST(CodecTest, TestVideoCodecOperators) {
perkj26752742016-10-24 01:21:16 -0700146 VideoCodec c0(96, "V");
147 VideoCodec c1(95, "V");
148 VideoCodec c2(96, "x");
149
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 EXPECT_TRUE(c0 != c1);
151 EXPECT_TRUE(c0 != c2);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
153 VideoCodec c7;
perkj26752742016-10-24 01:21:16 -0700154 VideoCodec c8(0, "");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 VideoCodec c9 = c0;
156 EXPECT_TRUE(c8 == c7);
157 EXPECT_TRUE(c9 != c7);
158 EXPECT_TRUE(c9 == c0);
159
160 VideoCodec c10(c0);
161 VideoCodec c11(c0);
162 VideoCodec c12(c0);
163 VideoCodec c13(c0);
164 c10.params["x"] = "abc";
165 c11.params["x"] = "def";
166 c12.params["y"] = "abc";
167 c13.params["x"] = "abc";
168 EXPECT_TRUE(c10 != c0);
169 EXPECT_TRUE(c11 != c0);
170 EXPECT_TRUE(c11 != c10);
171 EXPECT_TRUE(c12 != c0);
172 EXPECT_TRUE(c12 != c10);
173 EXPECT_TRUE(c12 != c11);
174 EXPECT_TRUE(c13 == c10);
175}
176
Mirta Dvornicic479a3c02019-06-04 15:38:50 +0200177TEST(CodecTest, TestVideoCodecIntersectPacketization) {
178 VideoCodec c1;
179 c1.packetization = "raw";
180 VideoCodec c2;
181 c2.packetization = "raw";
182 VideoCodec c3;
183
184 EXPECT_EQ(VideoCodec::IntersectPacketization(c1, c2), "raw");
185 EXPECT_EQ(VideoCodec::IntersectPacketization(c1, c3), absl::nullopt);
186}
187
188TEST(CodecTest, TestVideoCodecEqualsWithDifferentPacketization) {
189 VideoCodec c0(100, cricket::kVp8CodecName);
190 VideoCodec c1(100, cricket::kVp8CodecName);
191 VideoCodec c2(100, cricket::kVp8CodecName);
192 c2.packetization = "raw";
193
194 EXPECT_EQ(c0, c1);
195 EXPECT_NE(c0, c2);
196 EXPECT_NE(c2, c0);
197 EXPECT_EQ(c2, c2);
198}
199
magjed9c41e472016-10-31 09:06:03 -0700200TEST(CodecTest, TestVideoCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 // Test a codec with a static payload type.
perkj26752742016-10-24 01:21:16 -0700202 VideoCodec c0(95, "V");
203 EXPECT_TRUE(c0.Matches(VideoCodec(95, "")));
204 EXPECT_FALSE(c0.Matches(VideoCodec(96, "")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205
206 // Test a codec with a dynamic payload type.
perkj26752742016-10-24 01:21:16 -0700207 VideoCodec c1(96, "V");
208 EXPECT_TRUE(c1.Matches(VideoCodec(96, "V")));
209 EXPECT_TRUE(c1.Matches(VideoCodec(97, "V")));
210 EXPECT_TRUE(c1.Matches(VideoCodec(96, "v")));
211 EXPECT_TRUE(c1.Matches(VideoCodec(97, "v")));
212 EXPECT_FALSE(c1.Matches(VideoCodec(96, "")));
213 EXPECT_FALSE(c1.Matches(VideoCodec(95, "V")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214}
215
Mirta Dvornicic479a3c02019-06-04 15:38:50 +0200216TEST(CodecTest, TestVideoCodecMatchesWithDifferentPacketization) {
217 VideoCodec c0(100, cricket::kVp8CodecName);
218 VideoCodec c1(101, cricket::kVp8CodecName);
219 c1.packetization = "raw";
220
221 EXPECT_TRUE(c0.Matches(c1));
222 EXPECT_TRUE(c1.Matches(c0));
223}
224
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700225// VP9 codecs compare profile information.
226TEST(CodecTest, TestVP9CodecMatches) {
227 const char kProfile0[] = "0";
228 const char kProfile2[] = "2";
229
230 VideoCodec c_no_profile(95, cricket::kVp9CodecName);
231 VideoCodec c_profile0(95, cricket::kVp9CodecName);
232 c_profile0.params[webrtc::kVP9FmtpProfileId] = kProfile0;
233
234 EXPECT_TRUE(c_profile0.Matches(c_no_profile));
235
236 {
237 VideoCodec c_profile0_eq(95, cricket::kVp9CodecName);
238 c_profile0_eq.params[webrtc::kVP9FmtpProfileId] = kProfile0;
239 EXPECT_TRUE(c_profile0.Matches(c_profile0_eq));
240 }
241
242 {
243 VideoCodec c_profile2(95, cricket::kVp9CodecName);
244 c_profile2.params[webrtc::kVP9FmtpProfileId] = kProfile2;
245 EXPECT_FALSE(c_profile0.Matches(c_profile2));
246 EXPECT_FALSE(c_no_profile.Matches(c_profile2));
247 }
248
249 {
250 VideoCodec c_no_profile_eq(95, cricket::kVp9CodecName);
251 EXPECT_TRUE(c_no_profile.Matches(c_no_profile_eq));
252 }
253}
254
Steve Anton9c1fb1e2018-02-26 15:09:41 -0800255// Matching H264 codecs also need to have matching profile-level-id and
256// packetization-mode.
257TEST(CodecTest, TestH264CodecMatches) {
258 const char kProfileLevelId1[] = "42e01f";
259 const char kProfileLevelId2[] = "42a01e";
260
261 VideoCodec pli_1_pm_0(95, "H264");
262 pli_1_pm_0.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
263 pli_1_pm_0.params[cricket::kH264FmtpPacketizationMode] = "0";
264
265 {
266 VideoCodec pli_1_pm_blank(95, "H264");
267 pli_1_pm_blank.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
268 pli_1_pm_blank.params.erase(
269 pli_1_pm_blank.params.find(cricket::kH264FmtpPacketizationMode));
270
271 // Matches since if packetization-mode is not specified it defaults to "0".
272 EXPECT_TRUE(pli_1_pm_0.Matches(pli_1_pm_blank));
273 }
274
275 {
276 VideoCodec pli_1_pm_1(95, "H264");
277 pli_1_pm_1.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
278 pli_1_pm_1.params[cricket::kH264FmtpPacketizationMode] = "1";
279
280 // Does not match since packetization-mode is different.
281 EXPECT_FALSE(pli_1_pm_0.Matches(pli_1_pm_1));
282 }
283
284 {
285 VideoCodec pli_2_pm_0(95, "H264");
286 pli_2_pm_0.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId2;
287 pli_2_pm_0.params[cricket::kH264FmtpPacketizationMode] = "0";
288
289 // Does not match since profile-level-id is different.
290 EXPECT_FALSE(pli_1_pm_0.Matches(pli_2_pm_0));
291 }
292}
293
magjed9c41e472016-10-31 09:06:03 -0700294TEST(CodecTest, TestDataCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000295 // Test a codec with a static payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700296 DataCodec c0(95, "D");
297 EXPECT_TRUE(c0.Matches(DataCodec(95, "")));
298 EXPECT_FALSE(c0.Matches(DataCodec(96, "")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
300 // Test a codec with a dynamic payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700301 DataCodec c1(96, "D");
302 EXPECT_TRUE(c1.Matches(DataCodec(96, "D")));
303 EXPECT_TRUE(c1.Matches(DataCodec(97, "D")));
304 EXPECT_TRUE(c1.Matches(DataCodec(96, "d")));
305 EXPECT_TRUE(c1.Matches(DataCodec(97, "d")));
306 EXPECT_FALSE(c1.Matches(DataCodec(96, "")));
307 EXPECT_FALSE(c1.Matches(DataCodec(95, "D")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308}
309
magjed9c41e472016-10-31 09:06:03 -0700310TEST(CodecTest, TestSetParamGetParamAndRemoveParam) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 AudioCodec codec;
312 codec.SetParam("a", "1");
313 codec.SetParam("b", "x");
314
315 int int_value = 0;
316 EXPECT_TRUE(codec.GetParam("a", &int_value));
317 EXPECT_EQ(1, int_value);
318 EXPECT_FALSE(codec.GetParam("b", &int_value));
319 EXPECT_FALSE(codec.GetParam("c", &int_value));
320
321 std::string str_value;
322 EXPECT_TRUE(codec.GetParam("a", &str_value));
323 EXPECT_EQ("1", str_value);
324 EXPECT_TRUE(codec.GetParam("b", &str_value));
325 EXPECT_EQ("x", str_value);
326 EXPECT_FALSE(codec.GetParam("c", &str_value));
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000327 EXPECT_TRUE(codec.RemoveParam("a"));
328 EXPECT_FALSE(codec.RemoveParam("c"));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329}
330
magjed9c41e472016-10-31 09:06:03 -0700331TEST(CodecTest, TestIntersectFeedbackParams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 const FeedbackParam a1("a", "1");
333 const FeedbackParam b2("b", "2");
334 const FeedbackParam b3("b", "3");
335 const FeedbackParam c3("c", "3");
htab39db842016-12-08 01:50:48 -0800336 TestCodec c1;
Steve Antone78bcb92017-10-31 09:53:08 -0700337 c1.AddFeedbackParam(a1); // Only match with c2.
338 c1.AddFeedbackParam(b2); // Same param different values.
339 c1.AddFeedbackParam(c3); // Not in c2.
htab39db842016-12-08 01:50:48 -0800340 TestCodec c2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000341 c2.AddFeedbackParam(a1);
342 c2.AddFeedbackParam(b3);
343
344 c1.IntersectFeedbackParams(c2);
345 EXPECT_TRUE(c1.HasFeedbackParam(a1));
346 EXPECT_FALSE(c1.HasFeedbackParam(b2));
347 EXPECT_FALSE(c1.HasFeedbackParam(c3));
348}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000349
magjed9c41e472016-10-31 09:06:03 -0700350TEST(CodecTest, TestGetCodecType) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000351 // Codec type comparison should be case insenstive on names.
perkj26752742016-10-24 01:21:16 -0700352 const VideoCodec codec(96, "V");
353 const VideoCodec rtx_codec(96, "rTx");
354 const VideoCodec ulpfec_codec(96, "ulpFeC");
brandtr87d7d772016-11-07 03:03:41 -0800355 const VideoCodec flexfec_codec(96, "FlExFeC-03");
perkj26752742016-10-24 01:21:16 -0700356 const VideoCodec red_codec(96, "ReD");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000357 EXPECT_EQ(VideoCodec::CODEC_VIDEO, codec.GetCodecType());
358 EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
359 EXPECT_EQ(VideoCodec::CODEC_ULPFEC, ulpfec_codec.GetCodecType());
brandtr87d7d772016-11-07 03:03:41 -0800360 EXPECT_EQ(VideoCodec::CODEC_FLEXFEC, flexfec_codec.GetCodecType());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000361 EXPECT_EQ(VideoCodec::CODEC_RED, red_codec.GetCodecType());
362}
363
magjed9c41e472016-10-31 09:06:03 -0700364TEST(CodecTest, TestCreateRtxCodec) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000365 VideoCodec rtx_codec = VideoCodec::CreateRtxCodec(96, 120);
366 EXPECT_EQ(96, rtx_codec.id);
367 EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
368 int associated_payload_type;
369 ASSERT_TRUE(rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
370 &associated_payload_type));
371 EXPECT_EQ(120, associated_payload_type);
372}
373
magjed9c41e472016-10-31 09:06:03 -0700374TEST(CodecTest, TestValidateCodecFormat) {
perkj26752742016-10-24 01:21:16 -0700375 const VideoCodec codec(96, "V");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000376 ASSERT_TRUE(codec.ValidateCodecFormat());
377
378 // Accept 0-127 as payload types.
379 VideoCodec low_payload_type = codec;
380 low_payload_type.id = 0;
381 VideoCodec high_payload_type = codec;
382 high_payload_type.id = 127;
383 ASSERT_TRUE(low_payload_type.ValidateCodecFormat());
384 EXPECT_TRUE(high_payload_type.ValidateCodecFormat());
385
386 // Reject negative payloads.
387 VideoCodec negative_payload_type = codec;
388 negative_payload_type.id = -1;
389 EXPECT_FALSE(negative_payload_type.ValidateCodecFormat());
390
391 // Reject too-high payloads.
392 VideoCodec too_high_payload_type = codec;
393 too_high_payload_type.id = 128;
394 EXPECT_FALSE(too_high_payload_type.ValidateCodecFormat());
395
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000396 // Reject codecs with min bitrate > max bitrate.
397 VideoCodec incorrect_bitrates = codec;
398 incorrect_bitrates.params[kCodecParamMinBitrate] = "100";
399 incorrect_bitrates.params[kCodecParamMaxBitrate] = "80";
400 EXPECT_FALSE(incorrect_bitrates.ValidateCodecFormat());
401
402 // Accept min bitrate == max bitrate.
403 VideoCodec equal_bitrates = codec;
404 equal_bitrates.params[kCodecParamMinBitrate] = "100";
405 equal_bitrates.params[kCodecParamMaxBitrate] = "100";
406 EXPECT_TRUE(equal_bitrates.ValidateCodecFormat());
407
408 // Accept min bitrate < max bitrate.
409 VideoCodec different_bitrates = codec;
410 different_bitrates.params[kCodecParamMinBitrate] = "99";
411 different_bitrates.params[kCodecParamMaxBitrate] = "100";
412 EXPECT_TRUE(different_bitrates.ValidateCodecFormat());
413}
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700414
magjed9c41e472016-10-31 09:06:03 -0700415TEST(CodecTest, TestToCodecParameters) {
Florent Castellib7d9d832018-05-15 18:14:14 +0200416 VideoCodec v(96, "V");
417 v.SetParam("p1", "v1");
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700418 webrtc::RtpCodecParameters codec_params_1 = v.ToCodecParameters();
419 EXPECT_EQ(96, codec_params_1.payload_type);
deadbeefe702b302017-02-04 12:09:01 -0800420 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, codec_params_1.kind);
421 EXPECT_EQ("V", codec_params_1.name);
Oskar Sundbom78807582017-11-16 11:09:55 +0100422 EXPECT_EQ(cricket::kVideoCodecClockrate, codec_params_1.clock_rate);
Danil Chapovalov00c71832018-06-15 15:58:38 +0200423 EXPECT_EQ(absl::nullopt, codec_params_1.num_channels);
Mirko Bonadeif859e552018-05-30 15:31:29 +0200424 ASSERT_EQ(1u, codec_params_1.parameters.size());
Florent Castellib7d9d832018-05-15 18:14:14 +0200425 EXPECT_EQ("p1", codec_params_1.parameters.begin()->first);
426 EXPECT_EQ("v1", codec_params_1.parameters.begin()->second);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700427
Florent Castellib7d9d832018-05-15 18:14:14 +0200428 AudioCodec a(97, "A", 44100, 20000, 2);
429 a.SetParam("p1", "a1");
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700430 webrtc::RtpCodecParameters codec_params_2 = a.ToCodecParameters();
431 EXPECT_EQ(97, codec_params_2.payload_type);
deadbeefe702b302017-02-04 12:09:01 -0800432 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, codec_params_2.kind);
433 EXPECT_EQ("A", codec_params_2.name);
Oskar Sundbom78807582017-11-16 11:09:55 +0100434 EXPECT_EQ(44100, codec_params_2.clock_rate);
435 EXPECT_EQ(2, codec_params_2.num_channels);
Mirko Bonadeif859e552018-05-30 15:31:29 +0200436 ASSERT_EQ(1u, codec_params_2.parameters.size());
Florent Castellib7d9d832018-05-15 18:14:14 +0200437 EXPECT_EQ("p1", codec_params_2.parameters.begin()->first);
438 EXPECT_EQ("a1", codec_params_2.parameters.begin()->second);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700439}
Steve Anton2dbc6272019-05-31 13:08:58 -0700440
441// Tests that the helper IsSameCodec returns the correct value for codecs that
442// must also be matched on particular parameter values.
443using IsSameCodecParamsTestCase =
444 std::tuple<cricket::CodecParameterMap, cricket::CodecParameterMap>;
445class IsSameCodecParamsTest
446 : public ::testing::TestWithParam<
447 std::tuple<std::string, bool, IsSameCodecParamsTestCase>> {
448 protected:
449 IsSameCodecParamsTest() {
450 name_ = std::get<0>(GetParam());
451 expected_ = std::get<1>(GetParam());
452 const auto& test_case = std::get<2>(GetParam());
453 params_left_ = std::get<0>(test_case);
454 params_right_ = std::get<1>(test_case);
455 }
456
457 std::string name_;
458 bool expected_;
459 cricket::CodecParameterMap params_left_;
460 cricket::CodecParameterMap params_right_;
461};
462
463TEST_P(IsSameCodecParamsTest, Expected) {
464 EXPECT_EQ(expected_,
465 cricket::IsSameCodec(name_, params_left_, name_, params_right_));
466}
467
468TEST_P(IsSameCodecParamsTest, Commutative) {
469 EXPECT_EQ(expected_,
470 cricket::IsSameCodec(name_, params_right_, name_, params_left_));
471}
472
473IsSameCodecParamsTestCase MakeTestCase(cricket::CodecParameterMap left,
474 cricket::CodecParameterMap right) {
475 return std::make_tuple(left, right);
476}
477
478const IsSameCodecParamsTestCase kH264ParamsSameTestCases[] = {
479 // Both have the same defaults.
480 MakeTestCase({}, {}),
481 // packetization-mode: 0 is the default.
482 MakeTestCase({{cricket::kH264FmtpPacketizationMode, "0"}}, {}),
483 // Non-default packetization-mode matches.
484 MakeTestCase({{cricket::kH264FmtpPacketizationMode, "1"}},
485 {{cricket::kH264FmtpPacketizationMode, "1"}}),
486};
487INSTANTIATE_TEST_SUITE_P(
488 H264_Same,
489 IsSameCodecParamsTest,
490 ::testing::Combine(::testing::Values("H264"),
491 ::testing::Values(true),
492 ::testing::ValuesIn(kH264ParamsSameTestCases)));
493
494const IsSameCodecParamsTestCase kH264ParamsNotSameTestCases[] = {
495 // packetization-mode does not match the default of "0".
496 MakeTestCase({{cricket::kH264FmtpPacketizationMode, "1"}}, {}),
497};
498INSTANTIATE_TEST_SUITE_P(
499 H264_NotSame,
500 IsSameCodecParamsTest,
501 ::testing::Combine(::testing::Values("H264"),
502 ::testing::Values(false),
503 ::testing::ValuesIn(kH264ParamsNotSameTestCases)));
504
505const IsSameCodecParamsTestCase kVP9ParamsSameTestCases[] = {
506 // Both have the same defaults.
507 MakeTestCase({}, {}),
508 // profile-id: 0 is the default.
509 MakeTestCase({{webrtc::kVP9FmtpProfileId, "0"}}, {}),
510 // Non-default profile-id matches.
511 MakeTestCase({{webrtc::kVP9FmtpProfileId, "2"}},
512 {{webrtc::kVP9FmtpProfileId, "2"}}),
513};
514INSTANTIATE_TEST_SUITE_P(
515 VP9_Same,
516 IsSameCodecParamsTest,
517 ::testing::Combine(::testing::Values("VP9"),
518 ::testing::Values(true),
519 ::testing::ValuesIn(kVP9ParamsSameTestCases)));
520
521const IsSameCodecParamsTestCase kVP9ParamsNotSameTestCases[] = {
522 // profile-id missing from right.
523 MakeTestCase({{webrtc::kVP9FmtpProfileId, "2"}}, {}),
524};
525INSTANTIATE_TEST_SUITE_P(
526 VP9_NotSame,
527 IsSameCodecParamsTest,
528 ::testing::Combine(::testing::Values("VP9"),
529 ::testing::Values(false),
530 ::testing::ValuesIn(kVP9ParamsNotSameTestCases)));