blob: b3cda813b2927080a1c1d1370a1b89ae730c5389 [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
magjed9c41e472016-10-31 09:06:03 -0700177TEST(CodecTest, TestVideoCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 // Test a codec with a static payload type.
perkj26752742016-10-24 01:21:16 -0700179 VideoCodec c0(95, "V");
180 EXPECT_TRUE(c0.Matches(VideoCodec(95, "")));
181 EXPECT_FALSE(c0.Matches(VideoCodec(96, "")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182
183 // Test a codec with a dynamic payload type.
perkj26752742016-10-24 01:21:16 -0700184 VideoCodec c1(96, "V");
185 EXPECT_TRUE(c1.Matches(VideoCodec(96, "V")));
186 EXPECT_TRUE(c1.Matches(VideoCodec(97, "V")));
187 EXPECT_TRUE(c1.Matches(VideoCodec(96, "v")));
188 EXPECT_TRUE(c1.Matches(VideoCodec(97, "v")));
189 EXPECT_FALSE(c1.Matches(VideoCodec(96, "")));
190 EXPECT_FALSE(c1.Matches(VideoCodec(95, "V")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191}
192
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700193// VP9 codecs compare profile information.
194TEST(CodecTest, TestVP9CodecMatches) {
195 const char kProfile0[] = "0";
196 const char kProfile2[] = "2";
197
198 VideoCodec c_no_profile(95, cricket::kVp9CodecName);
199 VideoCodec c_profile0(95, cricket::kVp9CodecName);
200 c_profile0.params[webrtc::kVP9FmtpProfileId] = kProfile0;
201
202 EXPECT_TRUE(c_profile0.Matches(c_no_profile));
203
204 {
205 VideoCodec c_profile0_eq(95, cricket::kVp9CodecName);
206 c_profile0_eq.params[webrtc::kVP9FmtpProfileId] = kProfile0;
207 EXPECT_TRUE(c_profile0.Matches(c_profile0_eq));
208 }
209
210 {
211 VideoCodec c_profile2(95, cricket::kVp9CodecName);
212 c_profile2.params[webrtc::kVP9FmtpProfileId] = kProfile2;
213 EXPECT_FALSE(c_profile0.Matches(c_profile2));
214 EXPECT_FALSE(c_no_profile.Matches(c_profile2));
215 }
216
217 {
218 VideoCodec c_no_profile_eq(95, cricket::kVp9CodecName);
219 EXPECT_TRUE(c_no_profile.Matches(c_no_profile_eq));
220 }
221}
222
Steve Anton9c1fb1e2018-02-26 15:09:41 -0800223// Matching H264 codecs also need to have matching profile-level-id and
224// packetization-mode.
225TEST(CodecTest, TestH264CodecMatches) {
226 const char kProfileLevelId1[] = "42e01f";
227 const char kProfileLevelId2[] = "42a01e";
228
229 VideoCodec pli_1_pm_0(95, "H264");
230 pli_1_pm_0.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
231 pli_1_pm_0.params[cricket::kH264FmtpPacketizationMode] = "0";
232
233 {
234 VideoCodec pli_1_pm_blank(95, "H264");
235 pli_1_pm_blank.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
236 pli_1_pm_blank.params.erase(
237 pli_1_pm_blank.params.find(cricket::kH264FmtpPacketizationMode));
238
239 // Matches since if packetization-mode is not specified it defaults to "0".
240 EXPECT_TRUE(pli_1_pm_0.Matches(pli_1_pm_blank));
241 }
242
243 {
244 VideoCodec pli_1_pm_1(95, "H264");
245 pli_1_pm_1.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
246 pli_1_pm_1.params[cricket::kH264FmtpPacketizationMode] = "1";
247
248 // Does not match since packetization-mode is different.
249 EXPECT_FALSE(pli_1_pm_0.Matches(pli_1_pm_1));
250 }
251
252 {
253 VideoCodec pli_2_pm_0(95, "H264");
254 pli_2_pm_0.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId2;
255 pli_2_pm_0.params[cricket::kH264FmtpPacketizationMode] = "0";
256
257 // Does not match since profile-level-id is different.
258 EXPECT_FALSE(pli_1_pm_0.Matches(pli_2_pm_0));
259 }
260}
261
magjed9c41e472016-10-31 09:06:03 -0700262TEST(CodecTest, TestDataCodecMatches) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000263 // Test a codec with a static payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700264 DataCodec c0(95, "D");
265 EXPECT_TRUE(c0.Matches(DataCodec(95, "")));
266 EXPECT_FALSE(c0.Matches(DataCodec(96, "")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000267
268 // Test a codec with a dynamic payload type.
deadbeef67cf2c12016-04-13 10:07:16 -0700269 DataCodec c1(96, "D");
270 EXPECT_TRUE(c1.Matches(DataCodec(96, "D")));
271 EXPECT_TRUE(c1.Matches(DataCodec(97, "D")));
272 EXPECT_TRUE(c1.Matches(DataCodec(96, "d")));
273 EXPECT_TRUE(c1.Matches(DataCodec(97, "d")));
274 EXPECT_FALSE(c1.Matches(DataCodec(96, "")));
275 EXPECT_FALSE(c1.Matches(DataCodec(95, "D")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276}
277
magjed9c41e472016-10-31 09:06:03 -0700278TEST(CodecTest, TestSetParamGetParamAndRemoveParam) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279 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));
buildbot@webrtc.orgfbd13282014-06-19 19:50:55 +0000295 EXPECT_TRUE(codec.RemoveParam("a"));
296 EXPECT_FALSE(codec.RemoveParam("c"));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000297}
298
magjed9c41e472016-10-31 09:06:03 -0700299TEST(CodecTest, TestIntersectFeedbackParams) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000300 const FeedbackParam a1("a", "1");
301 const FeedbackParam b2("b", "2");
302 const FeedbackParam b3("b", "3");
303 const FeedbackParam c3("c", "3");
htab39db842016-12-08 01:50:48 -0800304 TestCodec c1;
Steve Antone78bcb92017-10-31 09:53:08 -0700305 c1.AddFeedbackParam(a1); // Only match with c2.
306 c1.AddFeedbackParam(b2); // Same param different values.
307 c1.AddFeedbackParam(c3); // Not in c2.
htab39db842016-12-08 01:50:48 -0800308 TestCodec c2;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000309 c2.AddFeedbackParam(a1);
310 c2.AddFeedbackParam(b3);
311
312 c1.IntersectFeedbackParams(c2);
313 EXPECT_TRUE(c1.HasFeedbackParam(a1));
314 EXPECT_FALSE(c1.HasFeedbackParam(b2));
315 EXPECT_FALSE(c1.HasFeedbackParam(c3));
316}
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000317
magjed9c41e472016-10-31 09:06:03 -0700318TEST(CodecTest, TestGetCodecType) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000319 // Codec type comparison should be case insenstive on names.
perkj26752742016-10-24 01:21:16 -0700320 const VideoCodec codec(96, "V");
321 const VideoCodec rtx_codec(96, "rTx");
322 const VideoCodec ulpfec_codec(96, "ulpFeC");
brandtr87d7d772016-11-07 03:03:41 -0800323 const VideoCodec flexfec_codec(96, "FlExFeC-03");
perkj26752742016-10-24 01:21:16 -0700324 const VideoCodec red_codec(96, "ReD");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000325 EXPECT_EQ(VideoCodec::CODEC_VIDEO, codec.GetCodecType());
326 EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
327 EXPECT_EQ(VideoCodec::CODEC_ULPFEC, ulpfec_codec.GetCodecType());
brandtr87d7d772016-11-07 03:03:41 -0800328 EXPECT_EQ(VideoCodec::CODEC_FLEXFEC, flexfec_codec.GetCodecType());
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000329 EXPECT_EQ(VideoCodec::CODEC_RED, red_codec.GetCodecType());
330}
331
magjed9c41e472016-10-31 09:06:03 -0700332TEST(CodecTest, TestCreateRtxCodec) {
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000333 VideoCodec rtx_codec = VideoCodec::CreateRtxCodec(96, 120);
334 EXPECT_EQ(96, rtx_codec.id);
335 EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
336 int associated_payload_type;
337 ASSERT_TRUE(rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
338 &associated_payload_type));
339 EXPECT_EQ(120, associated_payload_type);
340}
341
magjed9c41e472016-10-31 09:06:03 -0700342TEST(CodecTest, TestValidateCodecFormat) {
perkj26752742016-10-24 01:21:16 -0700343 const VideoCodec codec(96, "V");
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000344 ASSERT_TRUE(codec.ValidateCodecFormat());
345
346 // Accept 0-127 as payload types.
347 VideoCodec low_payload_type = codec;
348 low_payload_type.id = 0;
349 VideoCodec high_payload_type = codec;
350 high_payload_type.id = 127;
351 ASSERT_TRUE(low_payload_type.ValidateCodecFormat());
352 EXPECT_TRUE(high_payload_type.ValidateCodecFormat());
353
354 // Reject negative payloads.
355 VideoCodec negative_payload_type = codec;
356 negative_payload_type.id = -1;
357 EXPECT_FALSE(negative_payload_type.ValidateCodecFormat());
358
359 // Reject too-high payloads.
360 VideoCodec too_high_payload_type = codec;
361 too_high_payload_type.id = 128;
362 EXPECT_FALSE(too_high_payload_type.ValidateCodecFormat());
363
pbos@webrtc.orgb5a22b12014-05-13 11:07:01 +0000364 // Reject codecs with min bitrate > max bitrate.
365 VideoCodec incorrect_bitrates = codec;
366 incorrect_bitrates.params[kCodecParamMinBitrate] = "100";
367 incorrect_bitrates.params[kCodecParamMaxBitrate] = "80";
368 EXPECT_FALSE(incorrect_bitrates.ValidateCodecFormat());
369
370 // Accept min bitrate == max bitrate.
371 VideoCodec equal_bitrates = codec;
372 equal_bitrates.params[kCodecParamMinBitrate] = "100";
373 equal_bitrates.params[kCodecParamMaxBitrate] = "100";
374 EXPECT_TRUE(equal_bitrates.ValidateCodecFormat());
375
376 // Accept min bitrate < max bitrate.
377 VideoCodec different_bitrates = codec;
378 different_bitrates.params[kCodecParamMinBitrate] = "99";
379 different_bitrates.params[kCodecParamMaxBitrate] = "100";
380 EXPECT_TRUE(different_bitrates.ValidateCodecFormat());
381}
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700382
magjed9c41e472016-10-31 09:06:03 -0700383TEST(CodecTest, TestToCodecParameters) {
Florent Castellib7d9d832018-05-15 18:14:14 +0200384 VideoCodec v(96, "V");
385 v.SetParam("p1", "v1");
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700386 webrtc::RtpCodecParameters codec_params_1 = v.ToCodecParameters();
387 EXPECT_EQ(96, codec_params_1.payload_type);
deadbeefe702b302017-02-04 12:09:01 -0800388 EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, codec_params_1.kind);
389 EXPECT_EQ("V", codec_params_1.name);
Oskar Sundbom78807582017-11-16 11:09:55 +0100390 EXPECT_EQ(cricket::kVideoCodecClockrate, codec_params_1.clock_rate);
Danil Chapovalov00c71832018-06-15 15:58:38 +0200391 EXPECT_EQ(absl::nullopt, codec_params_1.num_channels);
Mirko Bonadeif859e552018-05-30 15:31:29 +0200392 ASSERT_EQ(1u, codec_params_1.parameters.size());
Florent Castellib7d9d832018-05-15 18:14:14 +0200393 EXPECT_EQ("p1", codec_params_1.parameters.begin()->first);
394 EXPECT_EQ("v1", codec_params_1.parameters.begin()->second);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700395
Florent Castellib7d9d832018-05-15 18:14:14 +0200396 AudioCodec a(97, "A", 44100, 20000, 2);
397 a.SetParam("p1", "a1");
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700398 webrtc::RtpCodecParameters codec_params_2 = a.ToCodecParameters();
399 EXPECT_EQ(97, codec_params_2.payload_type);
deadbeefe702b302017-02-04 12:09:01 -0800400 EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, codec_params_2.kind);
401 EXPECT_EQ("A", codec_params_2.name);
Oskar Sundbom78807582017-11-16 11:09:55 +0100402 EXPECT_EQ(44100, codec_params_2.clock_rate);
403 EXPECT_EQ(2, codec_params_2.num_channels);
Mirko Bonadeif859e552018-05-30 15:31:29 +0200404 ASSERT_EQ(1u, codec_params_2.parameters.size());
Florent Castellib7d9d832018-05-15 18:14:14 +0200405 EXPECT_EQ("p1", codec_params_2.parameters.begin()->first);
406 EXPECT_EQ("a1", codec_params_2.parameters.begin()->second);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700407}
Steve Anton2dbc6272019-05-31 13:08:58 -0700408
409// Tests that the helper IsSameCodec returns the correct value for codecs that
410// must also be matched on particular parameter values.
411using IsSameCodecParamsTestCase =
412 std::tuple<cricket::CodecParameterMap, cricket::CodecParameterMap>;
413class IsSameCodecParamsTest
414 : public ::testing::TestWithParam<
415 std::tuple<std::string, bool, IsSameCodecParamsTestCase>> {
416 protected:
417 IsSameCodecParamsTest() {
418 name_ = std::get<0>(GetParam());
419 expected_ = std::get<1>(GetParam());
420 const auto& test_case = std::get<2>(GetParam());
421 params_left_ = std::get<0>(test_case);
422 params_right_ = std::get<1>(test_case);
423 }
424
425 std::string name_;
426 bool expected_;
427 cricket::CodecParameterMap params_left_;
428 cricket::CodecParameterMap params_right_;
429};
430
431TEST_P(IsSameCodecParamsTest, Expected) {
432 EXPECT_EQ(expected_,
433 cricket::IsSameCodec(name_, params_left_, name_, params_right_));
434}
435
436TEST_P(IsSameCodecParamsTest, Commutative) {
437 EXPECT_EQ(expected_,
438 cricket::IsSameCodec(name_, params_right_, name_, params_left_));
439}
440
441IsSameCodecParamsTestCase MakeTestCase(cricket::CodecParameterMap left,
442 cricket::CodecParameterMap right) {
443 return std::make_tuple(left, right);
444}
445
446const IsSameCodecParamsTestCase kH264ParamsSameTestCases[] = {
447 // Both have the same defaults.
448 MakeTestCase({}, {}),
449 // packetization-mode: 0 is the default.
450 MakeTestCase({{cricket::kH264FmtpPacketizationMode, "0"}}, {}),
451 // Non-default packetization-mode matches.
452 MakeTestCase({{cricket::kH264FmtpPacketizationMode, "1"}},
453 {{cricket::kH264FmtpPacketizationMode, "1"}}),
454};
455INSTANTIATE_TEST_SUITE_P(
456 H264_Same,
457 IsSameCodecParamsTest,
458 ::testing::Combine(::testing::Values("H264"),
459 ::testing::Values(true),
460 ::testing::ValuesIn(kH264ParamsSameTestCases)));
461
462const IsSameCodecParamsTestCase kH264ParamsNotSameTestCases[] = {
463 // packetization-mode does not match the default of "0".
464 MakeTestCase({{cricket::kH264FmtpPacketizationMode, "1"}}, {}),
465};
466INSTANTIATE_TEST_SUITE_P(
467 H264_NotSame,
468 IsSameCodecParamsTest,
469 ::testing::Combine(::testing::Values("H264"),
470 ::testing::Values(false),
471 ::testing::ValuesIn(kH264ParamsNotSameTestCases)));
472
473const IsSameCodecParamsTestCase kVP9ParamsSameTestCases[] = {
474 // Both have the same defaults.
475 MakeTestCase({}, {}),
476 // profile-id: 0 is the default.
477 MakeTestCase({{webrtc::kVP9FmtpProfileId, "0"}}, {}),
478 // Non-default profile-id matches.
479 MakeTestCase({{webrtc::kVP9FmtpProfileId, "2"}},
480 {{webrtc::kVP9FmtpProfileId, "2"}}),
481};
482INSTANTIATE_TEST_SUITE_P(
483 VP9_Same,
484 IsSameCodecParamsTest,
485 ::testing::Combine(::testing::Values("VP9"),
486 ::testing::Values(true),
487 ::testing::ValuesIn(kVP9ParamsSameTestCases)));
488
489const IsSameCodecParamsTestCase kVP9ParamsNotSameTestCases[] = {
490 // profile-id missing from right.
491 MakeTestCase({{webrtc::kVP9FmtpProfileId, "2"}}, {}),
492};
493INSTANTIATE_TEST_SUITE_P(
494 VP9_NotSame,
495 IsSameCodecParamsTest,
496 ::testing::Combine(::testing::Values("VP9"),
497 ::testing::Values(false),
498 ::testing::ValuesIn(kVP9ParamsNotSameTestCases)));