xref: /aosp_15_r20/external/webrtc/media/base/codec_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2009 The WebRTC project authors. All Rights Reserved.
3  *
4  *  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.
9  */
10 
11 #include "media/base/codec.h"
12 
13 #include <tuple>
14 
15 #include "api/video_codecs/av1_profile.h"
16 #include "api/video_codecs/h264_profile_level_id.h"
17 #include "api/video_codecs/vp9_profile.h"
18 #include "modules/video_coding/codecs/h264/include/h264.h"
19 #include "rtc_base/gunit.h"
20 
21 using cricket::AudioCodec;
22 using cricket::Codec;
23 using cricket::FeedbackParam;
24 using cricket::kCodecParamAssociatedPayloadType;
25 using cricket::kCodecParamMaxBitrate;
26 using cricket::kCodecParamMinBitrate;
27 using cricket::VideoCodec;
28 
29 class TestCodec : public Codec {
30  public:
TestCodec(int id,const std::string & name,int clockrate)31   TestCodec(int id, const std::string& name, int clockrate)
32       : Codec(id, name, clockrate) {}
TestCodec()33   TestCodec() : Codec() {}
34   TestCodec(const TestCodec& c) = default;
35   TestCodec& operator=(const TestCodec& c) = default;
36 };
37 
TEST(CodecTest,TestCodecOperators)38 TEST(CodecTest, TestCodecOperators) {
39   TestCodec c0(96, "D", 1000);
40   c0.SetParam("a", 1);
41 
42   TestCodec c1 = c0;
43   EXPECT_TRUE(c1 == c0);
44 
45   int param_value0;
46   int param_value1;
47   EXPECT_TRUE(c0.GetParam("a", &param_value0));
48   EXPECT_TRUE(c1.GetParam("a", &param_value1));
49   EXPECT_EQ(param_value0, param_value1);
50 
51   c1.id = 86;
52   EXPECT_TRUE(c0 != c1);
53 
54   c1 = c0;
55   c1.name = "x";
56   EXPECT_TRUE(c0 != c1);
57 
58   c1 = c0;
59   c1.clockrate = 2000;
60   EXPECT_TRUE(c0 != c1);
61 
62   c1 = c0;
63   c1.SetParam("a", 2);
64   EXPECT_TRUE(c0 != c1);
65 
66   TestCodec c5;
67   TestCodec c6(0, "", 0);
68   EXPECT_TRUE(c5 == c6);
69 }
70 
TEST(CodecTest,TestAudioCodecOperators)71 TEST(CodecTest, TestAudioCodecOperators) {
72   AudioCodec c0(96, "A", 44100, 20000, 2);
73   AudioCodec c1(95, "A", 44100, 20000, 2);
74   AudioCodec c2(96, "x", 44100, 20000, 2);
75   AudioCodec c3(96, "A", 48000, 20000, 2);
76   AudioCodec c4(96, "A", 44100, 10000, 2);
77   AudioCodec c5(96, "A", 44100, 20000, 1);
78   EXPECT_NE(c0, c1);
79   EXPECT_NE(c0, c2);
80   EXPECT_NE(c0, c3);
81   EXPECT_NE(c0, c4);
82   EXPECT_NE(c0, c5);
83 
84   AudioCodec c7;
85   AudioCodec c8(0, "", 0, 0, 0);
86   AudioCodec c9 = c0;
87   EXPECT_EQ(c8, c7);
88   EXPECT_NE(c9, c7);
89   EXPECT_EQ(c9, c0);
90 
91   AudioCodec c10(c0);
92   AudioCodec c11(c0);
93   AudioCodec c12(c0);
94   AudioCodec c13(c0);
95   c10.params["x"] = "abc";
96   c11.params["x"] = "def";
97   c12.params["y"] = "abc";
98   c13.params["x"] = "abc";
99   EXPECT_NE(c10, c0);
100   EXPECT_NE(c11, c0);
101   EXPECT_NE(c11, c10);
102   EXPECT_NE(c12, c0);
103   EXPECT_NE(c12, c10);
104   EXPECT_NE(c12, c11);
105   EXPECT_EQ(c13, c10);
106 }
107 
TEST(CodecTest,TestAudioCodecMatches)108 TEST(CodecTest, TestAudioCodecMatches) {
109   // Test a codec with a static payload type.
110   AudioCodec c0(34, "A", 44100, 20000, 1);
111   EXPECT_TRUE(c0.Matches(AudioCodec(34, "", 44100, 20000, 1)));
112   EXPECT_TRUE(c0.Matches(AudioCodec(34, "", 44100, 20000, 0)));
113   EXPECT_TRUE(c0.Matches(AudioCodec(34, "", 44100, 0, 0)));
114   EXPECT_TRUE(c0.Matches(AudioCodec(34, "", 0, 0, 0)));
115   EXPECT_FALSE(c0.Matches(AudioCodec(96, "A", 44100, 20000, 1)));
116   EXPECT_FALSE(c0.Matches(AudioCodec(96, "", 44100, 20000, 1)));
117   EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 20000, 1)));
118   EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 30000, 1)));
119   EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 20000, 2)));
120   EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 30000, 2)));
121 
122   // Test a codec with a dynamic payload type.
123   AudioCodec c1(96, "A", 44100, 20000, 1);
124   EXPECT_TRUE(c1.Matches(AudioCodec(96, "A", 0, 0, 0)));
125   EXPECT_TRUE(c1.Matches(AudioCodec(97, "A", 0, 0, 0)));
126   EXPECT_TRUE(c1.Matches(AudioCodec(96, "a", 0, 0, 0)));
127   EXPECT_TRUE(c1.Matches(AudioCodec(97, "a", 0, 0, 0)));
128   EXPECT_TRUE(c1.Matches(AudioCodec(35, "a", 0, 0, 0)));
129   EXPECT_TRUE(c1.Matches(AudioCodec(42, "a", 0, 0, 0)));
130   EXPECT_TRUE(c1.Matches(AudioCodec(65, "a", 0, 0, 0)));
131   EXPECT_FALSE(c1.Matches(AudioCodec(95, "A", 0, 0, 0)));
132   EXPECT_FALSE(c1.Matches(AudioCodec(34, "A", 0, 0, 0)));
133   EXPECT_FALSE(c1.Matches(AudioCodec(96, "", 44100, 20000, 2)));
134   EXPECT_FALSE(c1.Matches(AudioCodec(96, "A", 55100, 30000, 1)));
135 
136   // Test a codec with a dynamic payload type, and auto bitrate.
137   AudioCodec c2(97, "A", 16000, 0, 1);
138   // Use default bitrate.
139   EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 1)));
140   EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 0)));
141   // Use explicit bitrate.
142   EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 32000, 1)));
143   // Backward compatibility with clients that might send "-1" (for default).
144   EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, -1, 1)));
145 
146   // Stereo doesn't match channels = 0.
147   AudioCodec c3(96, "A", 44100, 20000, 2);
148   EXPECT_TRUE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 2)));
149   EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 1)));
150   EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 0)));
151 }
152 
TEST(CodecTest,TestVideoCodecOperators)153 TEST(CodecTest, TestVideoCodecOperators) {
154   VideoCodec c0(96, "V");
155   VideoCodec c1(95, "V");
156   VideoCodec c2(96, "x");
157 
158   EXPECT_TRUE(c0 != c1);
159   EXPECT_TRUE(c0 != c2);
160 
161   VideoCodec c7;
162   VideoCodec c8(0, "");
163   VideoCodec c9 = c0;
164   EXPECT_TRUE(c8 == c7);
165   EXPECT_TRUE(c9 != c7);
166   EXPECT_TRUE(c9 == c0);
167 
168   VideoCodec c10(c0);
169   VideoCodec c11(c0);
170   VideoCodec c12(c0);
171   VideoCodec c13(c0);
172   c10.params["x"] = "abc";
173   c11.params["x"] = "def";
174   c12.params["y"] = "abc";
175   c13.params["x"] = "abc";
176   EXPECT_TRUE(c10 != c0);
177   EXPECT_TRUE(c11 != c0);
178   EXPECT_TRUE(c11 != c10);
179   EXPECT_TRUE(c12 != c0);
180   EXPECT_TRUE(c12 != c10);
181   EXPECT_TRUE(c12 != c11);
182   EXPECT_TRUE(c13 == c10);
183 }
184 
TEST(CodecTest,TestVideoCodecIntersectPacketization)185 TEST(CodecTest, TestVideoCodecIntersectPacketization) {
186   VideoCodec c1;
187   c1.packetization = "raw";
188   VideoCodec c2;
189   c2.packetization = "raw";
190   VideoCodec c3;
191 
192   EXPECT_EQ(VideoCodec::IntersectPacketization(c1, c2), "raw");
193   EXPECT_EQ(VideoCodec::IntersectPacketization(c1, c3), absl::nullopt);
194 }
195 
TEST(CodecTest,TestVideoCodecEqualsWithDifferentPacketization)196 TEST(CodecTest, TestVideoCodecEqualsWithDifferentPacketization) {
197   VideoCodec c0(100, cricket::kVp8CodecName);
198   VideoCodec c1(100, cricket::kVp8CodecName);
199   VideoCodec c2(100, cricket::kVp8CodecName);
200   c2.packetization = "raw";
201 
202   EXPECT_EQ(c0, c1);
203   EXPECT_NE(c0, c2);
204   EXPECT_NE(c2, c0);
205   EXPECT_EQ(c2, c2);
206 }
207 
TEST(CodecTest,TestVideoCodecMatches)208 TEST(CodecTest, TestVideoCodecMatches) {
209   // Test a codec with a static payload type.
210   VideoCodec c0(34, "V");
211   EXPECT_TRUE(c0.Matches(VideoCodec(34, "")));
212   EXPECT_FALSE(c0.Matches(VideoCodec(96, "")));
213   EXPECT_FALSE(c0.Matches(VideoCodec(96, "V")));
214 
215   // Test a codec with a dynamic payload type.
216   VideoCodec c1(96, "V");
217   EXPECT_TRUE(c1.Matches(VideoCodec(96, "V")));
218   EXPECT_TRUE(c1.Matches(VideoCodec(97, "V")));
219   EXPECT_TRUE(c1.Matches(VideoCodec(96, "v")));
220   EXPECT_TRUE(c1.Matches(VideoCodec(97, "v")));
221   EXPECT_TRUE(c1.Matches(VideoCodec(35, "v")));
222   EXPECT_TRUE(c1.Matches(VideoCodec(42, "v")));
223   EXPECT_TRUE(c1.Matches(VideoCodec(65, "v")));
224   EXPECT_FALSE(c1.Matches(VideoCodec(96, "")));
225   EXPECT_FALSE(c1.Matches(VideoCodec(95, "V")));
226   EXPECT_FALSE(c1.Matches(VideoCodec(34, "V")));
227 }
228 
TEST(CodecTest,TestVideoCodecMatchesWithDifferentPacketization)229 TEST(CodecTest, TestVideoCodecMatchesWithDifferentPacketization) {
230   VideoCodec c0(100, cricket::kVp8CodecName);
231   VideoCodec c1(101, cricket::kVp8CodecName);
232   c1.packetization = "raw";
233 
234   EXPECT_TRUE(c0.Matches(c1));
235   EXPECT_TRUE(c1.Matches(c0));
236 }
237 
238 // AV1 codecs compare profile information.
TEST(CodecTest,TestAV1CodecMatches)239 TEST(CodecTest, TestAV1CodecMatches) {
240   const char kProfile0[] = "0";
241   const char kProfile1[] = "1";
242   const char kProfile2[] = "2";
243 
244   VideoCodec c_no_profile(95, cricket::kAv1CodecName);
245   VideoCodec c_profile0(95, cricket::kAv1CodecName);
246   c_profile0.params[webrtc::kAV1FmtpProfile] = kProfile0;
247   VideoCodec c_profile1(95, cricket::kAv1CodecName);
248   c_profile1.params[webrtc::kAV1FmtpProfile] = kProfile1;
249   VideoCodec c_profile2(95, cricket::kAv1CodecName);
250   c_profile2.params[webrtc::kAV1FmtpProfile] = kProfile2;
251 
252   // An AV1 entry with no profile specified should be treated as profile-0.
253   EXPECT_TRUE(c_profile0.Matches(c_no_profile));
254 
255   {
256     // Two AV1 entries without a profile specified are treated as duplicates.
257     VideoCodec c_no_profile_eq(95, cricket::kAv1CodecName);
258     EXPECT_TRUE(c_no_profile.Matches(c_no_profile_eq));
259   }
260 
261   {
262     // Two AV1 entries with profile 0 specified are treated as duplicates.
263     VideoCodec c_profile0_eq(95, cricket::kAv1CodecName);
264     c_profile0_eq.params[webrtc::kAV1FmtpProfile] = kProfile0;
265     EXPECT_TRUE(c_profile0.Matches(c_profile0_eq));
266   }
267 
268   {
269     // Two AV1 entries with profile 1 specified are treated as duplicates.
270     VideoCodec c_profile1_eq(95, cricket::kAv1CodecName);
271     c_profile1_eq.params[webrtc::kAV1FmtpProfile] = kProfile1;
272     EXPECT_TRUE(c_profile1.Matches(c_profile1_eq));
273   }
274 
275   // AV1 entries with different profiles (0 and 1) are seen as distinct.
276   EXPECT_FALSE(c_profile0.Matches(c_profile1));
277   EXPECT_FALSE(c_no_profile.Matches(c_profile1));
278 
279   // AV1 entries with different profiles (0 and 2) are seen as distinct.
280   EXPECT_FALSE(c_profile0.Matches(c_profile2));
281   EXPECT_FALSE(c_no_profile.Matches(c_profile2));
282 }
283 
284 // VP9 codecs compare profile information.
TEST(CodecTest,TestVP9CodecMatches)285 TEST(CodecTest, TestVP9CodecMatches) {
286   const char kProfile0[] = "0";
287   const char kProfile2[] = "2";
288 
289   VideoCodec c_no_profile(95, cricket::kVp9CodecName);
290   VideoCodec c_profile0(95, cricket::kVp9CodecName);
291   c_profile0.params[webrtc::kVP9FmtpProfileId] = kProfile0;
292 
293   EXPECT_TRUE(c_profile0.Matches(c_no_profile));
294 
295   {
296     VideoCodec c_profile0_eq(95, cricket::kVp9CodecName);
297     c_profile0_eq.params[webrtc::kVP9FmtpProfileId] = kProfile0;
298     EXPECT_TRUE(c_profile0.Matches(c_profile0_eq));
299   }
300 
301   {
302     VideoCodec c_profile2(95, cricket::kVp9CodecName);
303     c_profile2.params[webrtc::kVP9FmtpProfileId] = kProfile2;
304     EXPECT_FALSE(c_profile0.Matches(c_profile2));
305     EXPECT_FALSE(c_no_profile.Matches(c_profile2));
306   }
307 
308   {
309     VideoCodec c_no_profile_eq(95, cricket::kVp9CodecName);
310     EXPECT_TRUE(c_no_profile.Matches(c_no_profile_eq));
311   }
312 }
313 
314 // Matching H264 codecs also need to have matching profile-level-id and
315 // packetization-mode.
TEST(CodecTest,TestH264CodecMatches)316 TEST(CodecTest, TestH264CodecMatches) {
317   const char kProfileLevelId1[] = "42e01f";
318   const char kProfileLevelId2[] = "42a01e";
319 
320   VideoCodec pli_1_pm_0(95, "H264");
321   pli_1_pm_0.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
322   pli_1_pm_0.params[cricket::kH264FmtpPacketizationMode] = "0";
323 
324   {
325     VideoCodec pli_1_pm_blank(95, "H264");
326     pli_1_pm_blank.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
327     pli_1_pm_blank.params.erase(
328         pli_1_pm_blank.params.find(cricket::kH264FmtpPacketizationMode));
329 
330     // Matches since if packetization-mode is not specified it defaults to "0".
331     EXPECT_TRUE(pli_1_pm_0.Matches(pli_1_pm_blank));
332   }
333 
334   {
335     VideoCodec pli_1_pm_1(95, "H264");
336     pli_1_pm_1.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId1;
337     pli_1_pm_1.params[cricket::kH264FmtpPacketizationMode] = "1";
338 
339     // Does not match since packetization-mode is different.
340     EXPECT_FALSE(pli_1_pm_0.Matches(pli_1_pm_1));
341   }
342 
343   {
344     VideoCodec pli_2_pm_0(95, "H264");
345     pli_2_pm_0.params[cricket::kH264FmtpProfileLevelId] = kProfileLevelId2;
346     pli_2_pm_0.params[cricket::kH264FmtpPacketizationMode] = "0";
347 
348     // Does not match since profile-level-id is different.
349     EXPECT_FALSE(pli_1_pm_0.Matches(pli_2_pm_0));
350   }
351 }
352 
TEST(CodecTest,TestSetParamGetParamAndRemoveParam)353 TEST(CodecTest, TestSetParamGetParamAndRemoveParam) {
354   AudioCodec codec;
355   codec.SetParam("a", "1");
356   codec.SetParam("b", "x");
357 
358   int int_value = 0;
359   EXPECT_TRUE(codec.GetParam("a", &int_value));
360   EXPECT_EQ(1, int_value);
361   EXPECT_FALSE(codec.GetParam("b", &int_value));
362   EXPECT_FALSE(codec.GetParam("c", &int_value));
363 
364   std::string str_value;
365   EXPECT_TRUE(codec.GetParam("a", &str_value));
366   EXPECT_EQ("1", str_value);
367   EXPECT_TRUE(codec.GetParam("b", &str_value));
368   EXPECT_EQ("x", str_value);
369   EXPECT_FALSE(codec.GetParam("c", &str_value));
370   EXPECT_TRUE(codec.RemoveParam("a"));
371   EXPECT_FALSE(codec.RemoveParam("c"));
372 }
373 
TEST(CodecTest,TestIntersectFeedbackParams)374 TEST(CodecTest, TestIntersectFeedbackParams) {
375   const FeedbackParam a1("a", "1");
376   const FeedbackParam b2("b", "2");
377   const FeedbackParam b3("b", "3");
378   const FeedbackParam c3("c", "3");
379   TestCodec c1;
380   c1.AddFeedbackParam(a1);  // Only match with c2.
381   c1.AddFeedbackParam(b2);  // Same param different values.
382   c1.AddFeedbackParam(c3);  // Not in c2.
383   TestCodec c2;
384   c2.AddFeedbackParam(a1);
385   c2.AddFeedbackParam(b3);
386 
387   c1.IntersectFeedbackParams(c2);
388   EXPECT_TRUE(c1.HasFeedbackParam(a1));
389   EXPECT_FALSE(c1.HasFeedbackParam(b2));
390   EXPECT_FALSE(c1.HasFeedbackParam(c3));
391 }
392 
TEST(CodecTest,TestGetCodecType)393 TEST(CodecTest, TestGetCodecType) {
394   // Codec type comparison should be case insenstive on names.
395   const VideoCodec codec(96, "V");
396   const VideoCodec rtx_codec(96, "rTx");
397   const VideoCodec ulpfec_codec(96, "ulpFeC");
398   const VideoCodec flexfec_codec(96, "FlExFeC-03");
399   const VideoCodec red_codec(96, "ReD");
400   EXPECT_EQ(VideoCodec::CODEC_VIDEO, codec.GetCodecType());
401   EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
402   EXPECT_EQ(VideoCodec::CODEC_ULPFEC, ulpfec_codec.GetCodecType());
403   EXPECT_EQ(VideoCodec::CODEC_FLEXFEC, flexfec_codec.GetCodecType());
404   EXPECT_EQ(VideoCodec::CODEC_RED, red_codec.GetCodecType());
405 }
406 
TEST(CodecTest,TestCreateRtxCodec)407 TEST(CodecTest, TestCreateRtxCodec) {
408   VideoCodec rtx_codec = VideoCodec::CreateRtxCodec(96, 120);
409   EXPECT_EQ(96, rtx_codec.id);
410   EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType());
411   int associated_payload_type;
412   ASSERT_TRUE(rtx_codec.GetParam(kCodecParamAssociatedPayloadType,
413                                  &associated_payload_type));
414   EXPECT_EQ(120, associated_payload_type);
415 }
416 
TEST(CodecTest,TestValidateCodecFormat)417 TEST(CodecTest, TestValidateCodecFormat) {
418   const VideoCodec codec(96, "V");
419   ASSERT_TRUE(codec.ValidateCodecFormat());
420 
421   // Accept 0-127 as payload types.
422   VideoCodec low_payload_type = codec;
423   low_payload_type.id = 0;
424   VideoCodec high_payload_type = codec;
425   high_payload_type.id = 127;
426   ASSERT_TRUE(low_payload_type.ValidateCodecFormat());
427   EXPECT_TRUE(high_payload_type.ValidateCodecFormat());
428 
429   // Reject negative payloads.
430   VideoCodec negative_payload_type = codec;
431   negative_payload_type.id = -1;
432   EXPECT_FALSE(negative_payload_type.ValidateCodecFormat());
433 
434   // Reject too-high payloads.
435   VideoCodec too_high_payload_type = codec;
436   too_high_payload_type.id = 128;
437   EXPECT_FALSE(too_high_payload_type.ValidateCodecFormat());
438 
439   // Reject codecs with min bitrate > max bitrate.
440   VideoCodec incorrect_bitrates = codec;
441   incorrect_bitrates.params[kCodecParamMinBitrate] = "100";
442   incorrect_bitrates.params[kCodecParamMaxBitrate] = "80";
443   EXPECT_FALSE(incorrect_bitrates.ValidateCodecFormat());
444 
445   // Accept min bitrate == max bitrate.
446   VideoCodec equal_bitrates = codec;
447   equal_bitrates.params[kCodecParamMinBitrate] = "100";
448   equal_bitrates.params[kCodecParamMaxBitrate] = "100";
449   EXPECT_TRUE(equal_bitrates.ValidateCodecFormat());
450 
451   // Accept min bitrate < max bitrate.
452   VideoCodec different_bitrates = codec;
453   different_bitrates.params[kCodecParamMinBitrate] = "99";
454   different_bitrates.params[kCodecParamMaxBitrate] = "100";
455   EXPECT_TRUE(different_bitrates.ValidateCodecFormat());
456 }
457 
TEST(CodecTest,TestToCodecParameters)458 TEST(CodecTest, TestToCodecParameters) {
459   VideoCodec v(96, "V");
460   v.SetParam("p1", "v1");
461   webrtc::RtpCodecParameters codec_params_1 = v.ToCodecParameters();
462   EXPECT_EQ(96, codec_params_1.payload_type);
463   EXPECT_EQ(cricket::MEDIA_TYPE_VIDEO, codec_params_1.kind);
464   EXPECT_EQ("V", codec_params_1.name);
465   EXPECT_EQ(cricket::kVideoCodecClockrate, codec_params_1.clock_rate);
466   EXPECT_EQ(absl::nullopt, codec_params_1.num_channels);
467   ASSERT_EQ(1u, codec_params_1.parameters.size());
468   EXPECT_EQ("p1", codec_params_1.parameters.begin()->first);
469   EXPECT_EQ("v1", codec_params_1.parameters.begin()->second);
470 
471   AudioCodec a(97, "A", 44100, 20000, 2);
472   a.SetParam("p1", "a1");
473   webrtc::RtpCodecParameters codec_params_2 = a.ToCodecParameters();
474   EXPECT_EQ(97, codec_params_2.payload_type);
475   EXPECT_EQ(cricket::MEDIA_TYPE_AUDIO, codec_params_2.kind);
476   EXPECT_EQ("A", codec_params_2.name);
477   EXPECT_EQ(44100, codec_params_2.clock_rate);
478   EXPECT_EQ(2, codec_params_2.num_channels);
479   ASSERT_EQ(1u, codec_params_2.parameters.size());
480   EXPECT_EQ("p1", codec_params_2.parameters.begin()->first);
481   EXPECT_EQ("a1", codec_params_2.parameters.begin()->second);
482 }
483 
TEST(CodecTest,H264CostrainedBaselineIsAddedIfH264IsSupported)484 TEST(CodecTest, H264CostrainedBaselineIsAddedIfH264IsSupported) {
485   const std::vector<webrtc::SdpVideoFormat> kExplicitlySupportedFormats = {
486       webrtc::CreateH264Format(webrtc::H264Profile::kProfileBaseline,
487                                webrtc::H264Level::kLevel3_1, "1"),
488       webrtc::CreateH264Format(webrtc::H264Profile::kProfileBaseline,
489                                webrtc::H264Level::kLevel3_1, "0")};
490 
491   std::vector<webrtc::SdpVideoFormat> supported_formats =
492       kExplicitlySupportedFormats;
493   cricket::AddH264ConstrainedBaselineProfileToSupportedFormats(
494       &supported_formats);
495 
496   const webrtc::SdpVideoFormat kH264ConstrainedBasedlinePacketization1 =
497       webrtc::CreateH264Format(webrtc::H264Profile::kProfileConstrainedBaseline,
498                                webrtc::H264Level::kLevel3_1, "1");
499   const webrtc::SdpVideoFormat kH264ConstrainedBasedlinePacketization0 =
500       webrtc::CreateH264Format(webrtc::H264Profile::kProfileConstrainedBaseline,
501                                webrtc::H264Level::kLevel3_1, "0");
502 
503   EXPECT_EQ(supported_formats[0], kExplicitlySupportedFormats[0]);
504   EXPECT_EQ(supported_formats[1], kExplicitlySupportedFormats[1]);
505   EXPECT_EQ(supported_formats[2], kH264ConstrainedBasedlinePacketization1);
506   EXPECT_EQ(supported_formats[3], kH264ConstrainedBasedlinePacketization0);
507 }
508 
TEST(CodecTest,H264CostrainedBaselineIsNotAddedIfH264IsUnsupported)509 TEST(CodecTest, H264CostrainedBaselineIsNotAddedIfH264IsUnsupported) {
510   const std::vector<webrtc::SdpVideoFormat> kExplicitlySupportedFormats = {
511       {cricket::kVp9CodecName,
512        {{webrtc::kVP9FmtpProfileId,
513          VP9ProfileToString(webrtc::VP9Profile::kProfile0)}}}};
514 
515   std::vector<webrtc::SdpVideoFormat> supported_formats =
516       kExplicitlySupportedFormats;
517   cricket::AddH264ConstrainedBaselineProfileToSupportedFormats(
518       &supported_formats);
519 
520   EXPECT_EQ(supported_formats[0], kExplicitlySupportedFormats[0]);
521   EXPECT_EQ(supported_formats.size(), kExplicitlySupportedFormats.size());
522 }
523 
TEST(CodecTest,H264CostrainedBaselineNotAddedIfAlreadySpecified)524 TEST(CodecTest, H264CostrainedBaselineNotAddedIfAlreadySpecified) {
525   const std::vector<webrtc::SdpVideoFormat> kExplicitlySupportedFormats = {
526       webrtc::CreateH264Format(webrtc::H264Profile::kProfileBaseline,
527                                webrtc::H264Level::kLevel3_1, "1"),
528       webrtc::CreateH264Format(webrtc::H264Profile::kProfileBaseline,
529                                webrtc::H264Level::kLevel3_1, "0"),
530       webrtc::CreateH264Format(webrtc::H264Profile::kProfileConstrainedBaseline,
531                                webrtc::H264Level::kLevel3_1, "1"),
532       webrtc::CreateH264Format(webrtc::H264Profile::kProfileConstrainedBaseline,
533                                webrtc::H264Level::kLevel3_1, "0")};
534 
535   std::vector<webrtc::SdpVideoFormat> supported_formats =
536       kExplicitlySupportedFormats;
537   cricket::AddH264ConstrainedBaselineProfileToSupportedFormats(
538       &supported_formats);
539 
540   EXPECT_EQ(supported_formats[0], kExplicitlySupportedFormats[0]);
541   EXPECT_EQ(supported_formats[1], kExplicitlySupportedFormats[1]);
542   EXPECT_EQ(supported_formats[2], kExplicitlySupportedFormats[2]);
543   EXPECT_EQ(supported_formats[3], kExplicitlySupportedFormats[3]);
544   EXPECT_EQ(supported_formats.size(), kExplicitlySupportedFormats.size());
545 }
546