henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2009 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 11 | #include "webrtc/base/gunit.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 12 | #include "webrtc/media/base/codec.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
| 14 | using cricket::AudioCodec; |
| 15 | using cricket::Codec; |
| 16 | using cricket::DataCodec; |
| 17 | using cricket::FeedbackParam; |
| 18 | using cricket::VideoCodec; |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 19 | using cricket::kCodecParamAssociatedPayloadType; |
| 20 | using cricket::kCodecParamMaxBitrate; |
| 21 | using cricket::kCodecParamMinBitrate; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 22 | |
| 23 | class CodecTest : public testing::Test { |
| 24 | public: |
| 25 | CodecTest() {} |
| 26 | }; |
| 27 | |
jiayl@webrtc.org | 9c16c39 | 2014-05-01 18:30:30 +0000 | [diff] [blame] | 28 | TEST_F(CodecTest, TestCodecOperators) { |
| 29 | Codec c0(96, "D", 1000, 0); |
| 30 | c0.SetParam("a", 1); |
| 31 | |
| 32 | Codec c1 = c0; |
| 33 | EXPECT_TRUE(c1 == c0); |
| 34 | |
| 35 | int param_value0; |
| 36 | int param_value1; |
| 37 | EXPECT_TRUE(c0.GetParam("a", ¶m_value0)); |
| 38 | EXPECT_TRUE(c1.GetParam("a", ¶m_value1)); |
| 39 | EXPECT_EQ(param_value0, param_value1); |
| 40 | |
| 41 | c1.id = 86; |
| 42 | EXPECT_TRUE(c0 != c1); |
| 43 | |
| 44 | c1 = c0; |
| 45 | c1.name = "x"; |
| 46 | EXPECT_TRUE(c0 != c1); |
| 47 | |
| 48 | c1 = c0; |
| 49 | c1.clockrate = 2000; |
| 50 | EXPECT_TRUE(c0 != c1); |
| 51 | |
| 52 | c1 = c0; |
| 53 | c1.preference = 1; |
| 54 | EXPECT_TRUE(c0 != c1); |
| 55 | |
| 56 | c1 = c0; |
| 57 | c1.SetParam("a", 2); |
| 58 | EXPECT_TRUE(c0 != c1); |
| 59 | |
| 60 | Codec c5; |
| 61 | Codec c6(0, "", 0, 0); |
| 62 | EXPECT_TRUE(c5 == c6); |
| 63 | } |
| 64 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | TEST_F(CodecTest, TestAudioCodecOperators) { |
| 66 | AudioCodec c0(96, "A", 44100, 20000, 2, 3); |
| 67 | AudioCodec c1(95, "A", 44100, 20000, 2, 3); |
| 68 | AudioCodec c2(96, "x", 44100, 20000, 2, 3); |
| 69 | AudioCodec c3(96, "A", 48000, 20000, 2, 3); |
| 70 | AudioCodec c4(96, "A", 44100, 10000, 2, 3); |
| 71 | AudioCodec c5(96, "A", 44100, 20000, 1, 3); |
| 72 | AudioCodec c6(96, "A", 44100, 20000, 2, 1); |
| 73 | EXPECT_TRUE(c0 != c1); |
| 74 | EXPECT_TRUE(c0 != c2); |
| 75 | EXPECT_TRUE(c0 != c3); |
| 76 | EXPECT_TRUE(c0 != c4); |
| 77 | EXPECT_TRUE(c0 != c5); |
| 78 | EXPECT_TRUE(c0 != c6); |
| 79 | |
| 80 | AudioCodec c7; |
| 81 | AudioCodec c8(0, "", 0, 0, 0, 0); |
| 82 | AudioCodec c9 = c0; |
| 83 | EXPECT_TRUE(c8 == c7); |
| 84 | EXPECT_TRUE(c9 != c7); |
| 85 | EXPECT_TRUE(c9 == c0); |
| 86 | |
| 87 | AudioCodec c10(c0); |
| 88 | AudioCodec c11(c0); |
| 89 | AudioCodec c12(c0); |
| 90 | AudioCodec c13(c0); |
| 91 | c10.params["x"] = "abc"; |
| 92 | c11.params["x"] = "def"; |
| 93 | c12.params["y"] = "abc"; |
| 94 | c13.params["x"] = "abc"; |
| 95 | EXPECT_TRUE(c10 != c0); |
| 96 | EXPECT_TRUE(c11 != c0); |
| 97 | EXPECT_TRUE(c11 != c10); |
| 98 | EXPECT_TRUE(c12 != c0); |
| 99 | EXPECT_TRUE(c12 != c10); |
| 100 | EXPECT_TRUE(c12 != c11); |
| 101 | EXPECT_TRUE(c13 == c10); |
| 102 | } |
| 103 | |
| 104 | TEST_F(CodecTest, TestAudioCodecMatches) { |
| 105 | // Test a codec with a static payload type. |
| 106 | AudioCodec c0(95, "A", 44100, 20000, 1, 3); |
| 107 | EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 1, 0))); |
| 108 | EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 20000, 0, 0))); |
| 109 | EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 44100, 0, 0, 0))); |
| 110 | EXPECT_TRUE(c0.Matches(AudioCodec(95, "", 0, 0, 0, 0))); |
| 111 | EXPECT_FALSE(c0.Matches(AudioCodec(96, "", 44100, 20000, 1, 0))); |
| 112 | EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 20000, 1, 0))); |
| 113 | EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 30000, 1, 0))); |
| 114 | EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 44100, 20000, 2, 0))); |
| 115 | EXPECT_FALSE(c0.Matches(AudioCodec(95, "", 55100, 30000, 2, 0))); |
| 116 | |
| 117 | // Test a codec with a dynamic payload type. |
| 118 | AudioCodec c1(96, "A", 44100, 20000, 1, 3); |
| 119 | EXPECT_TRUE(c1.Matches(AudioCodec(96, "A", 0, 0, 0, 0))); |
| 120 | EXPECT_TRUE(c1.Matches(AudioCodec(97, "A", 0, 0, 0, 0))); |
| 121 | EXPECT_TRUE(c1.Matches(AudioCodec(96, "a", 0, 0, 0, 0))); |
| 122 | EXPECT_TRUE(c1.Matches(AudioCodec(97, "a", 0, 0, 0, 0))); |
| 123 | EXPECT_FALSE(c1.Matches(AudioCodec(95, "A", 0, 0, 0, 0))); |
| 124 | EXPECT_FALSE(c1.Matches(AudioCodec(96, "", 44100, 20000, 2, 0))); |
| 125 | EXPECT_FALSE(c1.Matches(AudioCodec(96, "A", 55100, 30000, 1, 0))); |
| 126 | |
| 127 | // Test a codec with a dynamic payload type, and auto bitrate. |
| 128 | AudioCodec c2(97, "A", 16000, 0, 1, 3); |
| 129 | // Use default bitrate. |
| 130 | EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 1, 0))); |
| 131 | EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 0, 0, 0))); |
| 132 | // Use explicit bitrate. |
| 133 | EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, 32000, 1, 0))); |
| 134 | // Backward compatibility with clients that might send "-1" (for default). |
| 135 | EXPECT_TRUE(c2.Matches(AudioCodec(97, "A", 16000, -1, 1, 0))); |
| 136 | |
| 137 | // Stereo doesn't match channels = 0. |
| 138 | AudioCodec c3(96, "A", 44100, 20000, 2, 3); |
| 139 | EXPECT_TRUE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 2, 3))); |
| 140 | EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 1, 3))); |
| 141 | EXPECT_FALSE(c3.Matches(AudioCodec(96, "A", 44100, 20000, 0, 3))); |
| 142 | } |
| 143 | |
| 144 | TEST_F(CodecTest, TestVideoCodecOperators) { |
| 145 | VideoCodec c0(96, "V", 320, 200, 30, 3); |
| 146 | VideoCodec c1(95, "V", 320, 200, 30, 3); |
| 147 | VideoCodec c2(96, "x", 320, 200, 30, 3); |
| 148 | VideoCodec c3(96, "V", 120, 200, 30, 3); |
| 149 | VideoCodec c4(96, "V", 320, 100, 30, 3); |
| 150 | VideoCodec c5(96, "V", 320, 200, 10, 3); |
| 151 | VideoCodec c6(96, "V", 320, 200, 30, 1); |
| 152 | EXPECT_TRUE(c0 != c1); |
| 153 | EXPECT_TRUE(c0 != c2); |
| 154 | EXPECT_TRUE(c0 != c3); |
| 155 | EXPECT_TRUE(c0 != c4); |
| 156 | EXPECT_TRUE(c0 != c5); |
| 157 | EXPECT_TRUE(c0 != c6); |
| 158 | |
| 159 | VideoCodec c7; |
| 160 | VideoCodec c8(0, "", 0, 0, 0, 0); |
| 161 | VideoCodec c9 = c0; |
| 162 | EXPECT_TRUE(c8 == c7); |
| 163 | EXPECT_TRUE(c9 != c7); |
| 164 | EXPECT_TRUE(c9 == c0); |
| 165 | |
| 166 | VideoCodec c10(c0); |
| 167 | VideoCodec c11(c0); |
| 168 | VideoCodec c12(c0); |
| 169 | VideoCodec c13(c0); |
| 170 | c10.params["x"] = "abc"; |
| 171 | c11.params["x"] = "def"; |
| 172 | c12.params["y"] = "abc"; |
| 173 | c13.params["x"] = "abc"; |
| 174 | EXPECT_TRUE(c10 != c0); |
| 175 | EXPECT_TRUE(c11 != c0); |
| 176 | EXPECT_TRUE(c11 != c10); |
| 177 | EXPECT_TRUE(c12 != c0); |
| 178 | EXPECT_TRUE(c12 != c10); |
| 179 | EXPECT_TRUE(c12 != c11); |
| 180 | EXPECT_TRUE(c13 == c10); |
| 181 | } |
| 182 | |
| 183 | TEST_F(CodecTest, TestVideoCodecMatches) { |
| 184 | // Test a codec with a static payload type. |
| 185 | VideoCodec c0(95, "V", 320, 200, 30, 3); |
| 186 | EXPECT_TRUE(c0.Matches(VideoCodec(95, "", 640, 400, 15, 0))); |
| 187 | EXPECT_FALSE(c0.Matches(VideoCodec(96, "", 320, 200, 30, 0))); |
| 188 | |
| 189 | // Test a codec with a dynamic payload type. |
| 190 | VideoCodec c1(96, "V", 320, 200, 30, 3); |
| 191 | EXPECT_TRUE(c1.Matches(VideoCodec(96, "V", 640, 400, 15, 0))); |
| 192 | EXPECT_TRUE(c1.Matches(VideoCodec(97, "V", 640, 400, 15, 0))); |
| 193 | EXPECT_TRUE(c1.Matches(VideoCodec(96, "v", 640, 400, 15, 0))); |
| 194 | EXPECT_TRUE(c1.Matches(VideoCodec(97, "v", 640, 400, 15, 0))); |
| 195 | EXPECT_FALSE(c1.Matches(VideoCodec(96, "", 320, 200, 30, 0))); |
| 196 | EXPECT_FALSE(c1.Matches(VideoCodec(95, "V", 640, 400, 15, 0))); |
| 197 | } |
| 198 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 199 | TEST_F(CodecTest, TestDataCodecMatches) { |
| 200 | // Test a codec with a static payload type. |
| 201 | DataCodec c0(95, "D", 0); |
| 202 | EXPECT_TRUE(c0.Matches(DataCodec(95, "", 0))); |
| 203 | EXPECT_FALSE(c0.Matches(DataCodec(96, "", 0))); |
| 204 | |
| 205 | // Test a codec with a dynamic payload type. |
| 206 | DataCodec c1(96, "D", 3); |
| 207 | EXPECT_TRUE(c1.Matches(DataCodec(96, "D", 0))); |
| 208 | EXPECT_TRUE(c1.Matches(DataCodec(97, "D", 0))); |
| 209 | EXPECT_TRUE(c1.Matches(DataCodec(96, "d", 0))); |
| 210 | EXPECT_TRUE(c1.Matches(DataCodec(97, "d", 0))); |
| 211 | EXPECT_FALSE(c1.Matches(DataCodec(96, "", 0))); |
| 212 | EXPECT_FALSE(c1.Matches(DataCodec(95, "D", 0))); |
| 213 | } |
| 214 | |
buildbot@webrtc.org | fbd1328 | 2014-06-19 19:50:55 +0000 | [diff] [blame] | 215 | TEST_F(CodecTest, TestSetParamGetParamAndRemoveParam) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 216 | AudioCodec codec; |
| 217 | codec.SetParam("a", "1"); |
| 218 | codec.SetParam("b", "x"); |
| 219 | |
| 220 | int int_value = 0; |
| 221 | EXPECT_TRUE(codec.GetParam("a", &int_value)); |
| 222 | EXPECT_EQ(1, int_value); |
| 223 | EXPECT_FALSE(codec.GetParam("b", &int_value)); |
| 224 | EXPECT_FALSE(codec.GetParam("c", &int_value)); |
| 225 | |
| 226 | std::string str_value; |
| 227 | EXPECT_TRUE(codec.GetParam("a", &str_value)); |
| 228 | EXPECT_EQ("1", str_value); |
| 229 | EXPECT_TRUE(codec.GetParam("b", &str_value)); |
| 230 | EXPECT_EQ("x", str_value); |
| 231 | EXPECT_FALSE(codec.GetParam("c", &str_value)); |
buildbot@webrtc.org | fbd1328 | 2014-06-19 19:50:55 +0000 | [diff] [blame] | 232 | EXPECT_TRUE(codec.RemoveParam("a")); |
| 233 | EXPECT_FALSE(codec.RemoveParam("c")); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | TEST_F(CodecTest, TestIntersectFeedbackParams) { |
| 237 | const FeedbackParam a1("a", "1"); |
| 238 | const FeedbackParam b2("b", "2"); |
| 239 | const FeedbackParam b3("b", "3"); |
| 240 | const FeedbackParam c3("c", "3"); |
| 241 | Codec c1; |
| 242 | c1.AddFeedbackParam(a1); // Only match with c2. |
| 243 | c1.AddFeedbackParam(b2); // Same param different values. |
| 244 | c1.AddFeedbackParam(c3); // Not in c2. |
| 245 | Codec c2; |
| 246 | c2.AddFeedbackParam(a1); |
| 247 | c2.AddFeedbackParam(b3); |
| 248 | |
| 249 | c1.IntersectFeedbackParams(c2); |
| 250 | EXPECT_TRUE(c1.HasFeedbackParam(a1)); |
| 251 | EXPECT_FALSE(c1.HasFeedbackParam(b2)); |
| 252 | EXPECT_FALSE(c1.HasFeedbackParam(c3)); |
| 253 | } |
pbos@webrtc.org | b5a22b1 | 2014-05-13 11:07:01 +0000 | [diff] [blame] | 254 | |
| 255 | TEST_F(CodecTest, TestGetCodecType) { |
| 256 | // Codec type comparison should be case insenstive on names. |
| 257 | const VideoCodec codec(96, "V", 320, 200, 30, 3); |
| 258 | const VideoCodec rtx_codec(96, "rTx", 320, 200, 30, 3); |
| 259 | const VideoCodec ulpfec_codec(96, "ulpFeC", 320, 200, 30, 3); |
| 260 | const VideoCodec red_codec(96, "ReD", 320, 200, 30, 3); |
| 261 | EXPECT_EQ(VideoCodec::CODEC_VIDEO, codec.GetCodecType()); |
| 262 | EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType()); |
| 263 | EXPECT_EQ(VideoCodec::CODEC_ULPFEC, ulpfec_codec.GetCodecType()); |
| 264 | EXPECT_EQ(VideoCodec::CODEC_RED, red_codec.GetCodecType()); |
| 265 | } |
| 266 | |
| 267 | TEST_F(CodecTest, TestCreateRtxCodec) { |
| 268 | VideoCodec rtx_codec = VideoCodec::CreateRtxCodec(96, 120); |
| 269 | EXPECT_EQ(96, rtx_codec.id); |
| 270 | EXPECT_EQ(VideoCodec::CODEC_RTX, rtx_codec.GetCodecType()); |
| 271 | int associated_payload_type; |
| 272 | ASSERT_TRUE(rtx_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 273 | &associated_payload_type)); |
| 274 | EXPECT_EQ(120, associated_payload_type); |
| 275 | } |
| 276 | |
| 277 | TEST_F(CodecTest, TestValidateCodecFormat) { |
| 278 | const VideoCodec codec(96, "V", 320, 200, 30, 3); |
| 279 | ASSERT_TRUE(codec.ValidateCodecFormat()); |
| 280 | |
| 281 | // Accept 0-127 as payload types. |
| 282 | VideoCodec low_payload_type = codec; |
| 283 | low_payload_type.id = 0; |
| 284 | VideoCodec high_payload_type = codec; |
| 285 | high_payload_type.id = 127; |
| 286 | ASSERT_TRUE(low_payload_type.ValidateCodecFormat()); |
| 287 | EXPECT_TRUE(high_payload_type.ValidateCodecFormat()); |
| 288 | |
| 289 | // Reject negative payloads. |
| 290 | VideoCodec negative_payload_type = codec; |
| 291 | negative_payload_type.id = -1; |
| 292 | EXPECT_FALSE(negative_payload_type.ValidateCodecFormat()); |
| 293 | |
| 294 | // Reject too-high payloads. |
| 295 | VideoCodec too_high_payload_type = codec; |
| 296 | too_high_payload_type.id = 128; |
| 297 | EXPECT_FALSE(too_high_payload_type.ValidateCodecFormat()); |
| 298 | |
| 299 | // Reject zero-width codecs. |
| 300 | VideoCodec zero_width = codec; |
| 301 | zero_width.width = 0; |
| 302 | EXPECT_FALSE(zero_width.ValidateCodecFormat()); |
| 303 | |
| 304 | // Reject zero-height codecs. |
| 305 | VideoCodec zero_height = codec; |
| 306 | zero_height.height = 0; |
| 307 | EXPECT_FALSE(zero_height.ValidateCodecFormat()); |
| 308 | |
| 309 | // Accept non-video codecs with zero dimensions. |
| 310 | VideoCodec zero_width_rtx_codec = VideoCodec::CreateRtxCodec(96, 120); |
| 311 | zero_width_rtx_codec.width = 0; |
| 312 | EXPECT_TRUE(zero_width_rtx_codec.ValidateCodecFormat()); |
| 313 | |
| 314 | // Reject codecs with min bitrate > max bitrate. |
| 315 | VideoCodec incorrect_bitrates = codec; |
| 316 | incorrect_bitrates.params[kCodecParamMinBitrate] = "100"; |
| 317 | incorrect_bitrates.params[kCodecParamMaxBitrate] = "80"; |
| 318 | EXPECT_FALSE(incorrect_bitrates.ValidateCodecFormat()); |
| 319 | |
| 320 | // Accept min bitrate == max bitrate. |
| 321 | VideoCodec equal_bitrates = codec; |
| 322 | equal_bitrates.params[kCodecParamMinBitrate] = "100"; |
| 323 | equal_bitrates.params[kCodecParamMaxBitrate] = "100"; |
| 324 | EXPECT_TRUE(equal_bitrates.ValidateCodecFormat()); |
| 325 | |
| 326 | // Accept min bitrate < max bitrate. |
| 327 | VideoCodec different_bitrates = codec; |
| 328 | different_bitrates.params[kCodecParamMinBitrate] = "99"; |
| 329 | different_bitrates.params[kCodecParamMaxBitrate] = "100"; |
| 330 | EXPECT_TRUE(different_bitrates.ValidateCodecFormat()); |
| 331 | } |