1 /****************************************************************************** 2 * 3 * Copyright 2016 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #include <dlfcn.h> 20 21 #include <set> 22 #include <vector> 23 24 #include <gtest/gtest.h> 25 26 #include "stack/include/a2dp_aac.h" 27 #include "stack/include/a2dp_api.h" 28 #include "stack/include/a2dp_codec_api.h" 29 #include "stack/include/a2dp_sbc.h" 30 #include "stack/include/a2dp_vendor.h" 31 32 namespace { 33 const uint8_t codec_info_sbc[AVDT_CODEC_SIZE] = { 34 6, // Length (A2DP_SBC_INFO_LEN) 35 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO 36 0, // Media Codec Type: A2DP_MEDIA_CT_SBC 37 0x20 | 0x01, // Sample Frequency: A2DP_SBC_IE_SAMP_FREQ_44 | 38 // Channel Mode: A2DP_SBC_IE_CH_MD_JOINT 39 0x10 | 0x04 | 0x01, // Block Length: A2DP_SBC_IE_BLOCKS_16 | 40 // Subbands: A2DP_SBC_IE_SUBBAND_8 | 41 // Allocation Method: A2DP_SBC_IE_ALLOC_MD_L 42 2, // MinimumBitpool Value: A2DP_SBC_IE_MIN_BITPOOL 43 53, // Maximum Bitpool Value: A2DP_SBC_MAX_BITPOOL 44 7, // Dummy 45 8, // Dummy 46 9 // Dummy 47 }; 48 49 const uint8_t codec_info_sbc_capability[AVDT_CODEC_SIZE] = { 50 6, // Length (A2DP_SBC_INFO_LEN) 51 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO 52 0, // Media Codec Type: A2DP_MEDIA_CT_SBC 53 0x20 | // Sample Frequency: A2DP_SBC_IE_SAMP_FREQ_44 | 54 0x08 | 0x01, // Channel Mode: A2DP_SBC_IE_CH_MD_MONO | 55 // A2DP_SBC_IE_CH_MD_JOINT 56 0x80 | 0x40 | 0x20 | 0x10 | // Block Length: A2DP_SBC_IE_BLOCKS_4 | 57 // A2DP_SBC_IE_BLOCKS_8 | 58 // A2DP_SBC_IE_BLOCKS_12 | 59 // A2DP_SBC_IE_BLOCKS_16 | 60 0x04 | // Subbands: A2DP_SBC_IE_SUBBAND_8 | 61 0x01, // Allocation Method: A2DP_SBC_IE_ALLOC_MD_L 62 2, // MinimumBitpool Value: A2DP_SBC_IE_MIN_BITPOOL 63 53, // Maximum Bitpool Value: A2DP_SBC_MAX_BITPOOL 64 7, // Dummy 65 8, // Dummy 66 9 // Dummy 67 }; 68 69 const uint8_t codec_info_sbc_sink_capability[AVDT_CODEC_SIZE] = { 70 6, // Length (A2DP_SBC_INFO_LEN) 71 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO 72 0, // Media Codec Type: A2DP_MEDIA_CT_SBC 73 0x20 | 0x10 | // Sample Frequency: A2DP_SBC_IE_SAMP_FREQ_44 | 74 // A2DP_SBC_IE_SAMP_FREQ_48 | 75 0x08 | 0x04 | 0x02 | 0x01, // Channel Mode: A2DP_SBC_IE_CH_MD_MONO | 76 // A2DP_SBC_IE_CH_MD_DUAL | 77 // A2DP_SBC_IE_CH_MD_STEREO | 78 // A2DP_SBC_IE_CH_MD_JOINT 79 0x80 | 0x40 | 0x20 | 0x10 | // Block Length: A2DP_SBC_IE_BLOCKS_4 | 80 // A2DP_SBC_IE_BLOCKS_8 | 81 // A2DP_SBC_IE_BLOCKS_12 | 82 // A2DP_SBC_IE_BLOCKS_16 | 83 0x08 | 0x04 | // Subbands: A2DP_SBC_IE_SUBBAND_4 | 84 // A2DP_SBC_IE_SUBBAND_8 | 85 0x02 | 0x01, // Allocation Method: A2DP_SBC_IE_ALLOC_MD_S | 86 // A2DP_SBC_IE_ALLOC_MD_L 87 2, // MinimumBitpool Value: A2DP_SBC_IE_MIN_BITPOOL 88 53, // Maximum Bitpool Value: A2DP_SBC_MAX_BITPOOL 89 7, // Dummy 90 8, // Dummy 91 9 // Dummy 92 }; 93 94 const uint8_t codec_info_aac[AVDT_CODEC_SIZE] = { 95 8, // Length (A2DP_AAC_INFO_LEN) 96 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO 97 2, // Media Codec Type: A2DP_MEDIA_CT_AAC 98 0x80, // Object Type: A2DP_AAC_OBJECT_TYPE_MPEG2_LC 99 0x01, // Sampling Frequency: A2DP_AAC_SAMPLING_FREQ_44100 100 0x04, // Channels: A2DP_AAC_CHANNEL_MODE_STEREO 101 0x00 | 0x4, // Variable Bit Rate: 102 // A2DP_AAC_VARIABLE_BIT_RATE_DISABLED 103 // Bit Rate: 320000 = 0x4e200 104 0xe2, // Bit Rate: 320000 = 0x4e200 105 0x00, // Bit Rate: 320000 = 0x4e200 106 7, // Dummy 107 8, // Dummy 108 9 // Dummy 109 }; 110 111 const uint8_t codec_info_aac_capability[AVDT_CODEC_SIZE] = { 112 8, // Length (A2DP_AAC_INFO_LEN) 113 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO 114 2, // Media Codec Type: A2DP_MEDIA_CT_AAC 115 0x80, // Object Type: A2DP_AAC_OBJECT_TYPE_MPEG2_LC 116 0x01, // Sampling Frequency: A2DP_AAC_SAMPLING_FREQ_44100 117 // TODO: AAC 48.0kHz sampling rate should be added back - see b/62301376 118 0x04, // Channels: A2DP_AAC_CHANNEL_MODE_STEREO 119 0x00 | 0x4, // Variable Bit Rate: 120 // A2DP_AAC_VARIABLE_BIT_RATE_DISABLED 121 // Bit Rate: 320000 = 0x4e200 122 0xe2, // Bit Rate: 320000 = 0x4e200 123 0x00, // Bit Rate: 320000 = 0x4e200 124 7, // Dummy 125 8, // Dummy 126 9 // Dummy 127 }; 128 129 const uint8_t codec_info_aac_sink_capability[AVDT_CODEC_SIZE] = { 130 8, // Length (A2DP_AAC_INFO_LEN) 131 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO 132 2, // Media Codec Type: A2DP_MEDIA_CT_AAC 133 0x80 | 0x40 | 0x20 | 0x10, // Object Type: A2DP_AAC_OBJECT_TYPE_MPEG2_LC | 134 // A2DP_AAC_OBJECT_TYPE_MPEG4_LC 135 // A2DP_AAC_OBJECT_TYPE_MPEG4_LTP 136 // A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE 137 0x01, // Sampling Frequency: A2DP_AAC_SAMPLING_FREQ_44100 138 0x80 | 0x20 | 0x10 | 0x08 | 0x04, // Sampling Frequency: 139 // A2DP_AAC_SAMPLING_FREQ_48000 | 140 // A2DP_AAC_SAMPLING_FREQ_88200 | 141 // A2DP_AAC_SAMPLING_FREQ_96000 | 142 // Channels: 143 // A2DP_AAC_CHANNEL_MODE_MONO | 144 // A2DP_AAC_CHANNEL_MODE_STEREO 145 0x80 | 0x4, // Variable Bit Rate: 146 // A2DP_AAC_VARIABLE_BIT_RATE_ENABLED 147 // Bit Rate: 320000 = 0x4e200 148 0xe2, // Bit Rate: 320000 = 0x4e200 149 0x00, // Bit Rate: 320000 = 0x4e200 150 7, // Dummy 151 8, // Dummy 152 9 // Dummy 153 }; 154 155 const uint8_t codec_info_non_a2dp[AVDT_CODEC_SIZE] = { 156 8, // Length 157 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO 158 0xFF, // Media Codec Type: A2DP_MEDIA_CT_NON_A2DP 159 3, 4, 0, 0, // Vendor ID: LSB first, upper two octets should be 0 160 7, 8, // Codec ID: LSB first 161 9 // Dummy 162 }; 163 164 const uint8_t codec_info_non_a2dp_dummy[AVDT_CODEC_SIZE] = { 165 8, // Length 166 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO 167 0xFF, // Media Codec Type: A2DP_MEDIA_CT_NON_A2DP 168 3, 4, 0, 0, // Vendor ID: LSB first, upper two octets should be 0 169 7, 8, // Codec ID: LSB first 170 10 // Dummy 171 }; 172 173 static const char* APTX_ENCODER_LIB_NAME = "libaptX_encoder.so"; 174 static const char* APTX_HD_ENCODER_LIB_NAME = "libaptXHD_encoder.so"; 175 static const char* LDAC_ENCODER_LIB_NAME = "libldacBT_enc.so"; 176 177 static bool has_shared_library(const char* name) { 178 void* lib_handle = dlopen(name, RTLD_NOW); 179 if (lib_handle != nullptr) { 180 dlclose(lib_handle); 181 return true; 182 } 183 return false; 184 } 185 186 } // namespace 187 188 class StackA2dpTest : public ::testing::Test { 189 protected: 190 StackA2dpTest() { 191 // Create the set with all supported codecs 192 for (int i = BTAV_A2DP_CODEC_INDEX_MIN; i < BTAV_A2DP_CODEC_INDEX_MAX; 193 i++) { 194 btav_a2dp_codec_index_t codec_index = 195 static_cast<btav_a2dp_codec_index_t>(i); 196 197 bool supported = false; 198 switch (codec_index) { 199 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC: 200 supported = true; 201 break; 202 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC: 203 supported = true; 204 break; 205 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX: 206 // Codec aptX is supported only if the device has the corresponding 207 // shared library installed. 208 supported = has_shared_library(APTX_ENCODER_LIB_NAME); 209 break; 210 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD: 211 // Codec aptX-HD is supported only if the device has the corresponding 212 // shared library installed. 213 supported = has_shared_library(APTX_HD_ENCODER_LIB_NAME); 214 break; 215 case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC: 216 // Codec LDAC is supported only if the device has the corresponding 217 // shared library installed. 218 supported = has_shared_library(LDAC_ENCODER_LIB_NAME); 219 break; 220 case BTAV_A2DP_CODEC_INDEX_SINK_SBC: 221 supported = true; 222 break; 223 case BTAV_A2DP_CODEC_INDEX_SINK_AAC: 224 supported = true; 225 break; 226 case BTAV_A2DP_CODEC_INDEX_MAX: 227 // Needed to avoid using "default:" case so we can capture when 228 // a new codec is added, and it can be included here. 229 break; 230 } 231 232 if (supported) { 233 supported_codecs_.insert(codec_index); 234 } 235 } 236 } 237 238 bool has_codec_support(btav_a2dp_codec_index_t codec_index) { 239 return supported_codecs_.find(codec_index) != supported_codecs_.end(); 240 } 241 242 private: 243 std::set<btav_a2dp_codec_index_t> supported_codecs_; 244 }; 245 246 class A2dpCodecConfigTest : public StackA2dpTest {}; 247 248 TEST_F(StackA2dpTest, test_a2dp_bits_set) { 249 EXPECT_TRUE(A2DP_BitsSet(0x0) == A2DP_SET_ZERO_BIT); 250 EXPECT_TRUE(A2DP_BitsSet(0x1) == A2DP_SET_ONE_BIT); 251 EXPECT_TRUE(A2DP_BitsSet(0x2) == A2DP_SET_ONE_BIT); 252 EXPECT_TRUE(A2DP_BitsSet(0x3) == A2DP_SET_MULTL_BIT); 253 EXPECT_TRUE(A2DP_BitsSet(0x7f) == A2DP_SET_MULTL_BIT); 254 EXPECT_TRUE(A2DP_BitsSet(0x80) == A2DP_SET_ONE_BIT); 255 EXPECT_TRUE(A2DP_BitsSet(0x81) == A2DP_SET_MULTL_BIT); 256 EXPECT_TRUE(A2DP_BitsSet(0xc0) == A2DP_SET_MULTL_BIT); 257 EXPECT_TRUE(A2DP_BitsSet(0xff) == A2DP_SET_MULTL_BIT); 258 EXPECT_TRUE(A2DP_BitsSet(0x8000) == A2DP_SET_ONE_BIT); 259 EXPECT_TRUE(A2DP_BitsSet(0x8001) == A2DP_SET_MULTL_BIT); 260 EXPECT_TRUE(A2DP_BitsSet(0xc000) == A2DP_SET_MULTL_BIT); 261 EXPECT_TRUE(A2DP_BitsSet(0xffff) == A2DP_SET_MULTL_BIT); 262 EXPECT_TRUE(A2DP_BitsSet(0x80000) == A2DP_SET_ONE_BIT); 263 EXPECT_TRUE(A2DP_BitsSet(0x80001) == A2DP_SET_MULTL_BIT); 264 EXPECT_TRUE(A2DP_BitsSet(0xc0000) == A2DP_SET_MULTL_BIT); 265 EXPECT_TRUE(A2DP_BitsSet(0xfffff) == A2DP_SET_MULTL_BIT); 266 EXPECT_TRUE(A2DP_BitsSet(0x80000000) == A2DP_SET_ONE_BIT); 267 EXPECT_TRUE(A2DP_BitsSet(0x80000001) == A2DP_SET_MULTL_BIT); 268 EXPECT_TRUE(A2DP_BitsSet(0xc0000000) == A2DP_SET_MULTL_BIT); 269 EXPECT_TRUE(A2DP_BitsSet(0xffffffff) == A2DP_SET_MULTL_BIT); 270 EXPECT_TRUE(A2DP_BitsSet(0x8000000000000000) == A2DP_SET_ONE_BIT); 271 EXPECT_TRUE(A2DP_BitsSet(0x8000000000000001) == A2DP_SET_MULTL_BIT); 272 EXPECT_TRUE(A2DP_BitsSet(0xc000000000000000) == A2DP_SET_MULTL_BIT); 273 EXPECT_TRUE(A2DP_BitsSet(0xffffffffffffffff) == A2DP_SET_MULTL_BIT); 274 } 275 276 TEST_F(StackA2dpTest, test_a2dp_is_codec_valid_sbc) { 277 EXPECT_TRUE(A2DP_IsSourceCodecValid(codec_info_sbc)); 278 EXPECT_TRUE(A2DP_IsSourceCodecValid(codec_info_sbc_capability)); 279 EXPECT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_sbc)); 280 EXPECT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_sbc_capability)); 281 282 EXPECT_TRUE(A2DP_IsSinkCodecValid(codec_info_sbc_sink_capability)); 283 EXPECT_TRUE(A2DP_IsPeerSinkCodecValid(codec_info_sbc_sink_capability)); 284 285 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_non_a2dp)); 286 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_non_a2dp)); 287 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_non_a2dp)); 288 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_non_a2dp)); 289 290 // Test with invalid SBC codecs 291 uint8_t codec_info_sbc_invalid[AVDT_CODEC_SIZE]; 292 memset(codec_info_sbc_invalid, 0, sizeof(codec_info_sbc_invalid)); 293 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_sbc_invalid)); 294 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_sbc_invalid)); 295 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_sbc_invalid)); 296 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_sbc_invalid)); 297 298 memcpy(codec_info_sbc_invalid, codec_info_sbc, sizeof(codec_info_sbc)); 299 codec_info_sbc_invalid[0] = 0; // Corrupt the Length field 300 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_sbc_invalid)); 301 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_sbc_invalid)); 302 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_sbc_invalid)); 303 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_sbc_invalid)); 304 305 memcpy(codec_info_sbc_invalid, codec_info_sbc, sizeof(codec_info_sbc)); 306 codec_info_sbc_invalid[1] = 0xff; // Corrupt the Media Type field 307 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_sbc_invalid)); 308 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_sbc_invalid)); 309 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_sbc_invalid)); 310 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_sbc_invalid)); 311 } 312 313 TEST_F(StackA2dpTest, test_a2dp_is_codec_valid_aac) { 314 EXPECT_TRUE(A2DP_IsSourceCodecValid(codec_info_aac)); 315 EXPECT_TRUE(A2DP_IsSourceCodecValid(codec_info_aac_capability)); 316 EXPECT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_aac)); 317 EXPECT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_aac_capability)); 318 319 EXPECT_TRUE(A2DP_IsSinkCodecValid(codec_info_aac_sink_capability)); 320 EXPECT_TRUE(A2DP_IsPeerSinkCodecValid(codec_info_aac_sink_capability)); 321 322 // Test with invalid AAC codecs 323 uint8_t codec_info_aac_invalid[AVDT_CODEC_SIZE]; 324 memcpy(codec_info_aac_invalid, codec_info_aac, sizeof(codec_info_aac)); 325 codec_info_aac_invalid[0] = 0; // Corrupt the Length field 326 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_aac_invalid)); 327 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_aac_invalid)); 328 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_aac_invalid)); 329 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_aac_invalid)); 330 331 memcpy(codec_info_aac_invalid, codec_info_aac, sizeof(codec_info_aac)); 332 codec_info_aac_invalid[1] = 0xff; // Corrupt the Media Type field 333 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_aac_invalid)); 334 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_aac_invalid)); 335 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_aac_invalid)); 336 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_aac_invalid)); 337 } 338 339 TEST_F(StackA2dpTest, test_a2dp_get_codec_type) { 340 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(codec_info_sbc); 341 EXPECT_EQ(codec_type, A2DP_MEDIA_CT_SBC); 342 343 codec_type = A2DP_GetCodecType(codec_info_aac); 344 EXPECT_EQ(codec_type, A2DP_MEDIA_CT_AAC); 345 346 codec_type = A2DP_GetCodecType(codec_info_non_a2dp); 347 EXPECT_EQ(codec_type, A2DP_MEDIA_CT_NON_A2DP); 348 } 349 350 TEST_F(StackA2dpTest, test_a2dp_is_sink_codec_supported) { 351 EXPECT_TRUE(A2DP_IsSinkCodecSupported(codec_info_sbc)); 352 EXPECT_FALSE(A2DP_IsSinkCodecSupported(codec_info_sbc_capability)); 353 EXPECT_FALSE(A2DP_IsSinkCodecSupported(codec_info_sbc_sink_capability)); 354 355 EXPECT_TRUE(A2DP_IsSinkCodecSupported(codec_info_aac)); 356 // NOTE: The test below should be EXPECT_FALSE. 357 // However, codec_info_aac_capability is practically same as codec_info_aac, 358 // therefore we cannot differentiate it as a capability. 359 EXPECT_TRUE(A2DP_IsSinkCodecSupported(codec_info_aac_capability)); 360 EXPECT_FALSE(A2DP_IsSinkCodecSupported(codec_info_aac_sink_capability)); 361 362 EXPECT_FALSE(A2DP_IsSinkCodecSupported(codec_info_non_a2dp)); 363 } 364 365 TEST_F(StackA2dpTest, test_a2dp_is_peer_source_codec_supported) { 366 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_sbc)); 367 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_sbc_capability)); 368 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_sbc_sink_capability)); 369 370 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_aac)); 371 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_aac_capability)); 372 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_aac_sink_capability)); 373 374 EXPECT_FALSE(A2DP_IsPeerSourceCodecSupported(codec_info_non_a2dp)); 375 } 376 377 TEST_F(StackA2dpTest, test_init_default_codec) { 378 uint8_t codec_info_result[AVDT_CODEC_SIZE]; 379 380 memset(codec_info_result, 0, sizeof(codec_info_result)); 381 A2DP_InitDefaultCodec(codec_info_result); 382 383 // Compare the result codec with the local test codec info 384 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) { 385 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]); 386 } 387 } 388 389 TEST_F(StackA2dpTest, test_a2dp_uses_rtp_header) { 390 EXPECT_TRUE(A2DP_UsesRtpHeader(true, codec_info_sbc)); 391 EXPECT_TRUE(A2DP_UsesRtpHeader(false, codec_info_sbc)); 392 393 EXPECT_TRUE(A2DP_UsesRtpHeader(true, codec_info_aac)); 394 EXPECT_TRUE(A2DP_UsesRtpHeader(false, codec_info_aac)); 395 396 EXPECT_TRUE(A2DP_UsesRtpHeader(true, codec_info_non_a2dp)); 397 EXPECT_TRUE(A2DP_UsesRtpHeader(false, codec_info_non_a2dp)); 398 } 399 400 TEST_F(StackA2dpTest, test_a2dp_get_media_type) { 401 uint8_t codec_info_test[AVDT_CODEC_SIZE]; 402 403 EXPECT_EQ(A2DP_GetMediaType(codec_info_sbc), AVDT_MEDIA_TYPE_AUDIO); 404 EXPECT_EQ(A2DP_GetMediaType(codec_info_aac), AVDT_MEDIA_TYPE_AUDIO); 405 EXPECT_EQ(A2DP_GetMediaType(codec_info_non_a2dp), AVDT_MEDIA_TYPE_AUDIO); 406 407 // Prepare dummy codec info for video and for multimedia 408 memset(codec_info_test, 0, sizeof(codec_info_test)); 409 codec_info_test[0] = sizeof(codec_info_test); 410 codec_info_test[1] = 0x01 << 4; 411 EXPECT_EQ(A2DP_GetMediaType(codec_info_test), AVDT_MEDIA_TYPE_VIDEO); 412 codec_info_test[1] = 0x02 << 4; 413 EXPECT_EQ(A2DP_GetMediaType(codec_info_test), AVDT_MEDIA_TYPE_MULTI); 414 } 415 416 TEST_F(StackA2dpTest, test_a2dp_codec_name) { 417 uint8_t codec_info_test[AVDT_CODEC_SIZE]; 418 419 // Explicit tests for known codecs 420 EXPECT_STREQ(A2DP_CodecName(codec_info_sbc), "SBC"); 421 EXPECT_STREQ(A2DP_CodecName(codec_info_sbc_capability), "SBC"); 422 EXPECT_STREQ(A2DP_CodecName(codec_info_sbc_sink_capability), "SBC"); 423 EXPECT_STREQ(A2DP_CodecName(codec_info_aac), "AAC"); 424 EXPECT_STREQ(A2DP_CodecName(codec_info_aac_capability), "AAC"); 425 EXPECT_STREQ(A2DP_CodecName(codec_info_aac_sink_capability), "AAC"); 426 EXPECT_STREQ(A2DP_CodecName(codec_info_non_a2dp), "UNKNOWN VENDOR CODEC"); 427 428 // Test all unknown codecs 429 memcpy(codec_info_test, codec_info_sbc, sizeof(codec_info_sbc)); 430 for (uint8_t codec_type = A2DP_MEDIA_CT_AAC + 1; 431 codec_type < A2DP_MEDIA_CT_NON_A2DP; codec_type++) { 432 codec_info_test[2] = codec_type; // Unknown codec type 433 EXPECT_STREQ(A2DP_CodecName(codec_info_test), "UNKNOWN CODEC"); 434 } 435 } 436 437 TEST_F(StackA2dpTest, test_a2dp_vendor) { 438 EXPECT_EQ(A2DP_VendorCodecGetVendorId(codec_info_non_a2dp), 439 (uint32_t)0x00000403); 440 EXPECT_EQ(A2DP_VendorCodecGetCodecId(codec_info_non_a2dp), (uint16_t)0x0807); 441 EXPECT_TRUE(A2DP_VendorUsesRtpHeader(true, codec_info_non_a2dp)); 442 EXPECT_TRUE(A2DP_VendorUsesRtpHeader(false, codec_info_non_a2dp)); 443 } 444 445 TEST_F(StackA2dpTest, test_a2dp_codec_type_equals) { 446 EXPECT_TRUE(A2DP_CodecTypeEquals(codec_info_sbc, codec_info_sbc_capability)); 447 EXPECT_TRUE( 448 A2DP_CodecTypeEquals(codec_info_sbc, codec_info_sbc_sink_capability)); 449 EXPECT_TRUE(A2DP_CodecTypeEquals(codec_info_aac, codec_info_aac_capability)); 450 EXPECT_TRUE( 451 A2DP_CodecTypeEquals(codec_info_aac, codec_info_aac_sink_capability)); 452 EXPECT_TRUE( 453 A2DP_CodecTypeEquals(codec_info_non_a2dp, codec_info_non_a2dp_dummy)); 454 EXPECT_FALSE(A2DP_CodecTypeEquals(codec_info_sbc, codec_info_non_a2dp)); 455 EXPECT_FALSE(A2DP_CodecTypeEquals(codec_info_aac, codec_info_non_a2dp)); 456 EXPECT_FALSE(A2DP_CodecTypeEquals(codec_info_sbc, codec_info_aac)); 457 } 458 459 TEST_F(StackA2dpTest, test_a2dp_codec_equals) { 460 uint8_t codec_info_sbc_test[AVDT_CODEC_SIZE]; 461 uint8_t codec_info_aac_test[AVDT_CODEC_SIZE]; 462 uint8_t codec_info_non_a2dp_test[AVDT_CODEC_SIZE]; 463 464 // Test two identical SBC codecs 465 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test)); 466 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)); 467 EXPECT_TRUE(A2DP_CodecEquals(codec_info_sbc, codec_info_sbc_test)); 468 469 // Test two identical AAC codecs 470 memset(codec_info_aac_test, 0xAB, sizeof(codec_info_aac_test)); 471 memcpy(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac)); 472 EXPECT_TRUE(A2DP_CodecEquals(codec_info_aac, codec_info_aac_test)); 473 474 // Test two identical non-A2DP codecs that are not recognized 475 memset(codec_info_non_a2dp_test, 0xAB, sizeof(codec_info_non_a2dp_test)); 476 memcpy(codec_info_non_a2dp_test, codec_info_non_a2dp, 477 sizeof(codec_info_non_a2dp)); 478 EXPECT_FALSE(A2DP_CodecEquals(codec_info_non_a2dp, codec_info_non_a2dp_test)); 479 480 // Test two codecs that have different types 481 EXPECT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_non_a2dp)); 482 EXPECT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_aac)); 483 484 // Test two SBC codecs that are slightly different 485 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test)); 486 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)); 487 codec_info_sbc_test[5] = codec_info_sbc[5] + 1; 488 EXPECT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_sbc_test)); 489 codec_info_sbc_test[5] = codec_info_sbc[5]; 490 codec_info_sbc_test[6] = codec_info_sbc[6] + 1; 491 EXPECT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_sbc_test)); 492 493 // Test two AAC codecs that are slightly different 494 memset(codec_info_aac_test, 0xAB, sizeof(codec_info_aac_test)); 495 memcpy(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac)); 496 codec_info_aac_test[7] = codec_info_aac[7] + 1; 497 EXPECT_FALSE(A2DP_CodecEquals(codec_info_aac, codec_info_aac_test)); 498 codec_info_aac_test[7] = codec_info_aac[7]; 499 codec_info_aac_test[8] = codec_info_aac[8] + 1; 500 EXPECT_FALSE(A2DP_CodecEquals(codec_info_aac, codec_info_aac_test)); 501 502 // Test two SBC codecs that are identical, but with different dummy 503 // trailer data. 504 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test)); 505 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)); 506 codec_info_sbc_test[7] = codec_info_sbc[7] + 1; 507 EXPECT_TRUE(A2DP_CodecEquals(codec_info_sbc, codec_info_sbc_test)); 508 509 // Test two AAC codecs that are identical, but with different dummy 510 // trailer data. 511 memset(codec_info_aac_test, 0xAB, sizeof(codec_info_aac_test)); 512 memcpy(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac)); 513 codec_info_aac_test[9] = codec_info_aac[9] + 1; 514 EXPECT_TRUE(A2DP_CodecEquals(codec_info_aac, codec_info_aac_test)); 515 } 516 517 TEST_F(StackA2dpTest, test_a2dp_get_track_sample_rate) { 518 EXPECT_EQ(A2DP_GetTrackSampleRate(codec_info_sbc), 44100); 519 EXPECT_EQ(A2DP_GetTrackSampleRate(codec_info_aac), 44100); 520 EXPECT_EQ(A2DP_GetTrackSampleRate(codec_info_non_a2dp), -1); 521 } 522 523 TEST_F(StackA2dpTest, test_a2dp_get_track_channel_count) { 524 EXPECT_EQ(A2DP_GetTrackChannelCount(codec_info_sbc), 2); 525 EXPECT_EQ(A2DP_GetTrackChannelCount(codec_info_aac), 2); 526 EXPECT_EQ(A2DP_GetTrackChannelCount(codec_info_non_a2dp), -1); 527 } 528 529 TEST_F(StackA2dpTest, test_a2dp_get_number_of_subbands_sbc) { 530 EXPECT_EQ(A2DP_GetNumberOfSubbandsSbc(codec_info_sbc), 8); 531 EXPECT_EQ(A2DP_GetNumberOfSubbandsSbc(codec_info_aac), -1); 532 EXPECT_EQ(A2DP_GetNumberOfSubbandsSbc(codec_info_non_a2dp), -1); 533 } 534 535 TEST_F(StackA2dpTest, test_a2dp_get_number_of_blocks_sbc) { 536 EXPECT_EQ(A2DP_GetNumberOfBlocksSbc(codec_info_sbc), 16); 537 EXPECT_EQ(A2DP_GetNumberOfBlocksSbc(codec_info_aac), -1); 538 EXPECT_EQ(A2DP_GetNumberOfBlocksSbc(codec_info_non_a2dp), -1); 539 } 540 541 TEST_F(StackA2dpTest, test_a2dp_get_allocation_method_code_sbc) { 542 EXPECT_EQ(A2DP_GetAllocationMethodCodeSbc(codec_info_sbc), 0); 543 EXPECT_EQ(A2DP_GetAllocationMethodCodeSbc(codec_info_aac), -1); 544 EXPECT_EQ(A2DP_GetAllocationMethodCodeSbc(codec_info_non_a2dp), -1); 545 } 546 547 TEST_F(StackA2dpTest, test_a2dp_get_channel_mode_code_sbc) { 548 EXPECT_EQ(A2DP_GetChannelModeCodeSbc(codec_info_sbc), 3); 549 EXPECT_EQ(A2DP_GetChannelModeCodeSbc(codec_info_aac), -1); 550 EXPECT_EQ(A2DP_GetChannelModeCodeSbc(codec_info_non_a2dp), -1); 551 } 552 553 TEST_F(StackA2dpTest, test_a2dp_get_sampling_frequency_code_sbc) { 554 EXPECT_EQ(A2DP_GetSamplingFrequencyCodeSbc(codec_info_sbc), 2); 555 EXPECT_EQ(A2DP_GetSamplingFrequencyCodeSbc(codec_info_aac), -1); 556 EXPECT_EQ(A2DP_GetSamplingFrequencyCodeSbc(codec_info_non_a2dp), -1); 557 } 558 559 TEST_F(StackA2dpTest, test_a2dp_get_min_bitpool_sbc) { 560 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_sbc), 2); 561 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_sbc_capability), 2); 562 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_sbc_sink_capability), 2); 563 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_aac), -1); 564 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_non_a2dp), -1); 565 } 566 567 TEST_F(StackA2dpTest, test_a2dp_get_max_bitpool_sbc) { 568 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_sbc), 53); 569 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_sbc_capability), 53); 570 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_sbc_sink_capability), 53); 571 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_aac), -1); 572 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_non_a2dp), -1); 573 } 574 575 TEST_F(StackA2dpTest, test_a2dp_get_sink_track_channel_type) { 576 EXPECT_EQ(A2DP_GetSinkTrackChannelType(codec_info_sbc), 3); 577 EXPECT_EQ(A2DP_GetSinkTrackChannelType(codec_info_aac), 3); 578 EXPECT_EQ(A2DP_GetSinkTrackChannelType(codec_info_non_a2dp), -1); 579 } 580 581 TEST_F(StackA2dpTest, test_a2dp_get_object_type_code_aac) { 582 EXPECT_EQ(A2DP_GetObjectTypeCodeAac(codec_info_sbc), -1); 583 EXPECT_EQ(A2DP_GetObjectTypeCodeAac(codec_info_aac), 0x80); 584 EXPECT_EQ(A2DP_GetObjectTypeCodeAac(codec_info_non_a2dp), -1); 585 } 586 587 TEST_F(StackA2dpTest, test_a2dp_get_channel_mode_code_aac) { 588 EXPECT_EQ(A2DP_GetChannelModeCodeAac(codec_info_sbc), -1); 589 EXPECT_EQ(A2DP_GetChannelModeCodeAac(codec_info_aac), 0x04); 590 EXPECT_EQ(A2DP_GetChannelModeCodeAac(codec_info_non_a2dp), -1); 591 } 592 593 TEST_F(StackA2dpTest, test_a2dp_get_variable_bit_rate_support_aac) { 594 EXPECT_EQ(A2DP_GetVariableBitRateSupportAac(codec_info_sbc), -1); 595 EXPECT_EQ(A2DP_GetVariableBitRateSupportAac(codec_info_aac), 0); 596 EXPECT_EQ(A2DP_GetVariableBitRateSupportAac(codec_info_non_a2dp), -1); 597 } 598 599 TEST_F(StackA2dpTest, test_a2dp_get_bit_rate_aac) { 600 EXPECT_EQ(A2DP_GetBitRateAac(codec_info_sbc), -1); 601 EXPECT_EQ(A2DP_GetBitRateAac(codec_info_aac), 320000); 602 EXPECT_EQ(A2DP_GetBitRateAac(codec_info_non_a2dp), -1); 603 } 604 605 TEST_F(StackA2dpTest, test_a2dp_get_packet_timestamp) { 606 uint8_t a2dp_data[1000]; 607 uint32_t timestamp; 608 uint32_t* p_ts = reinterpret_cast<uint32_t*>(a2dp_data); 609 610 memset(a2dp_data, 0xAB, sizeof(a2dp_data)); 611 *p_ts = 0x12345678; 612 timestamp = 0xFFFFFFFF; 613 EXPECT_TRUE(A2DP_GetPacketTimestamp(codec_info_sbc, a2dp_data, ×tamp)); 614 EXPECT_EQ(timestamp, static_cast<uint32_t>(0x12345678)); 615 616 memset(a2dp_data, 0xAB, sizeof(a2dp_data)); 617 *p_ts = 0x12345678; 618 timestamp = 0xFFFFFFFF; 619 EXPECT_TRUE(A2DP_GetPacketTimestamp(codec_info_aac, a2dp_data, ×tamp)); 620 EXPECT_EQ(timestamp, static_cast<uint32_t>(0x12345678)); 621 622 memset(a2dp_data, 0xAB, sizeof(a2dp_data)); 623 *p_ts = 0x12345678; 624 timestamp = 0xFFFFFFFF; 625 EXPECT_FALSE( 626 A2DP_GetPacketTimestamp(codec_info_non_a2dp, a2dp_data, ×tamp)); 627 } 628 629 TEST_F(StackA2dpTest, test_a2dp_build_codec_header) { 630 uint8_t a2dp_data[1000]; 631 BT_HDR* p_buf = reinterpret_cast<BT_HDR*>(a2dp_data); 632 const uint16_t BT_HDR_LEN = 500; 633 const uint16_t BT_HDR_OFFSET = 50; 634 const uint8_t FRAMES_PER_PACKET = 0xCD; 635 636 memset(a2dp_data, 0xAB, sizeof(a2dp_data)); 637 p_buf->len = BT_HDR_LEN; 638 p_buf->offset = BT_HDR_OFFSET; 639 EXPECT_TRUE(A2DP_BuildCodecHeader(codec_info_sbc, p_buf, FRAMES_PER_PACKET)); 640 EXPECT_EQ(p_buf->offset + 1, 641 BT_HDR_OFFSET); // Modified by A2DP_SBC_MPL_HDR_LEN 642 EXPECT_EQ(p_buf->len - 1, BT_HDR_LEN); // Modified by A2DP_SBC_MPL_HDR_LEN 643 const uint8_t* p = 644 reinterpret_cast<const uint8_t*>(p_buf + 1) + p_buf->offset; 645 EXPECT_EQ( 646 *p, static_cast<uint8_t>(0x0D)); // 0xCD masked with A2DP_SBC_HDR_NUM_MSK 647 648 memset(a2dp_data, 0xAB, sizeof(a2dp_data)); 649 p_buf->len = BT_HDR_LEN; 650 p_buf->offset = BT_HDR_OFFSET; 651 EXPECT_TRUE(A2DP_BuildCodecHeader(codec_info_aac, p_buf, FRAMES_PER_PACKET)); 652 653 memset(a2dp_data, 0xAB, sizeof(a2dp_data)); 654 p_buf->len = BT_HDR_LEN; 655 p_buf->offset = BT_HDR_OFFSET; 656 EXPECT_FALSE( 657 A2DP_BuildCodecHeader(codec_info_non_a2dp, p_buf, FRAMES_PER_PACKET)); 658 } 659 660 TEST_F(StackA2dpTest, test_a2dp_adjust_codec) { 661 uint8_t codec_info_sbc_test[AVDT_CODEC_SIZE]; 662 uint8_t codec_info_aac_test[AVDT_CODEC_SIZE]; 663 uint8_t codec_info_non_a2dp_test[AVDT_CODEC_SIZE]; 664 665 // Test updating a valid SBC codec that doesn't need adjustment 666 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test)); 667 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)); 668 EXPECT_TRUE(A2DP_AdjustCodec(codec_info_sbc_test)); 669 EXPECT_TRUE( 670 memcmp(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)) == 0); 671 672 // Test updating a valid SBC codec that needs adjustment 673 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test)); 674 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)); 675 codec_info_sbc_test[6] = 54; // A2DP_SBC_MAX_BITPOOL + 1 676 EXPECT_TRUE(A2DP_AdjustCodec(codec_info_sbc_test)); 677 EXPECT_TRUE( 678 memcmp(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)) == 0); 679 680 // Test updating an invalid SBC codec 681 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test)); 682 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)); 683 codec_info_sbc_test[6] = 255; // Invalid MAX_BITPOOL 684 EXPECT_FALSE(A2DP_AdjustCodec(codec_info_sbc_test)); 685 686 // Test updating a valid AAC codec that doesn't need adjustment 687 memset(codec_info_aac_test, 0xAB, sizeof(codec_info_aac_test)); 688 memcpy(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac)); 689 EXPECT_TRUE(A2DP_AdjustCodec(codec_info_aac_test)); 690 EXPECT_TRUE( 691 memcmp(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac)) == 0); 692 693 // Test updating a non-A2DP codec that is not recognized 694 memset(codec_info_non_a2dp_test, 0xAB, sizeof(codec_info_non_a2dp_test)); 695 memcpy(codec_info_non_a2dp_test, codec_info_non_a2dp, 696 sizeof(codec_info_non_a2dp)); 697 EXPECT_FALSE(A2DP_AdjustCodec(codec_info_non_a2dp_test)); 698 } 699 700 TEST_F(StackA2dpTest, test_a2dp_source_codec_index) { 701 // Explicit tests for known Source codecs 702 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_sbc), 703 BTAV_A2DP_CODEC_INDEX_SOURCE_SBC); 704 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_sbc_capability), 705 BTAV_A2DP_CODEC_INDEX_SOURCE_SBC); 706 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_sbc_sink_capability), 707 BTAV_A2DP_CODEC_INDEX_SOURCE_SBC); 708 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_aac), 709 BTAV_A2DP_CODEC_INDEX_SOURCE_AAC); 710 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_aac_capability), 711 BTAV_A2DP_CODEC_INDEX_SOURCE_AAC); 712 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_aac_sink_capability), 713 BTAV_A2DP_CODEC_INDEX_SOURCE_AAC); 714 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_non_a2dp), 715 BTAV_A2DP_CODEC_INDEX_MAX); 716 } 717 718 TEST_F(StackA2dpTest, test_a2dp_sink_codec_index) { 719 // Explicit tests for known Sink codecs 720 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_sbc), 721 BTAV_A2DP_CODEC_INDEX_SINK_SBC); 722 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_sbc_capability), 723 BTAV_A2DP_CODEC_INDEX_SINK_SBC); 724 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_sbc_sink_capability), 725 BTAV_A2DP_CODEC_INDEX_SINK_SBC); 726 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_aac), 727 BTAV_A2DP_CODEC_INDEX_SINK_AAC); 728 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_aac_capability), 729 BTAV_A2DP_CODEC_INDEX_SINK_AAC); 730 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_aac_sink_capability), 731 BTAV_A2DP_CODEC_INDEX_SINK_AAC); 732 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_non_a2dp), 733 BTAV_A2DP_CODEC_INDEX_MAX); 734 } 735 736 TEST_F(StackA2dpTest, test_a2dp_codec_index_str) { 737 // Explicit tests for known codecs 738 EXPECT_STREQ(A2DP_CodecIndexStr(BTAV_A2DP_CODEC_INDEX_SOURCE_SBC), "SBC"); 739 EXPECT_STREQ(A2DP_CodecIndexStr(BTAV_A2DP_CODEC_INDEX_SINK_SBC), "SBC SINK"); 740 EXPECT_STREQ(A2DP_CodecIndexStr(BTAV_A2DP_CODEC_INDEX_SOURCE_AAC), "AAC"); 741 742 // Test that the unknown codec string has not changed 743 EXPECT_STREQ(A2DP_CodecIndexStr(BTAV_A2DP_CODEC_INDEX_MAX), 744 "UNKNOWN CODEC INDEX"); 745 746 // Test that each codec has a known string 747 for (int i = BTAV_A2DP_CODEC_INDEX_MIN; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) { 748 btav_a2dp_codec_index_t codec_index = 749 static_cast<btav_a2dp_codec_index_t>(i); 750 EXPECT_STRNE(A2DP_CodecIndexStr(codec_index), "UNKNOWN CODEC INDEX"); 751 } 752 } 753 754 TEST_F(StackA2dpTest, test_a2dp_init_codec_config) { 755 AvdtpSepConfig avdt_cfg; 756 757 // 758 // Test for SBC Source 759 // 760 memset(&avdt_cfg, 0, sizeof(avdt_cfg)); 761 EXPECT_TRUE( 762 A2DP_InitCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_SBC, &avdt_cfg)); 763 // Compare the result codec with the local test codec info 764 for (size_t i = 0; i < codec_info_sbc_capability[0] + 1; i++) { 765 EXPECT_EQ(avdt_cfg.codec_info[i], codec_info_sbc_capability[i]); 766 } 767 // Test for content protection 768 #if (BTA_AV_CO_CP_SCMS_T == TRUE) 769 EXPECT_EQ(avdt_cfg.protect_info[0], AVDT_CP_LOSC); 770 EXPECT_EQ(avdt_cfg.protect_info[1], (AVDT_CP_SCMS_T_ID & 0xFF)); 771 EXPECT_EQ(avdt_cfg.protect_info[2], ((AVDT_CP_SCMS_T_ID >> 8) & 0xFF)); 772 EXPECT_EQ(avdt_cfg.num_protect, 1); 773 #endif 774 775 // 776 // Test for SBC Sink 777 // 778 memset(&avdt_cfg, 0, sizeof(avdt_cfg)); 779 EXPECT_TRUE(A2DP_InitCodecConfig(BTAV_A2DP_CODEC_INDEX_SINK_SBC, &avdt_cfg)); 780 // Compare the result codec with the local test codec info 781 for (size_t i = 0; i < codec_info_sbc_sink_capability[0] + 1; i++) { 782 EXPECT_EQ(avdt_cfg.codec_info[i], codec_info_sbc_sink_capability[i]); 783 } 784 785 // 786 // Test for AAC Source 787 // 788 memset(&avdt_cfg, 0, sizeof(avdt_cfg)); 789 EXPECT_TRUE( 790 A2DP_InitCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_AAC, &avdt_cfg)); 791 // Compare the result codec with the local test codec info 792 for (size_t i = 0; i < codec_info_aac_capability[0] + 1; i++) { 793 EXPECT_EQ(avdt_cfg.codec_info[i], codec_info_aac_capability[i]); 794 } 795 // Test for content protection 796 #if (BTA_AV_CO_CP_SCMS_T == TRUE) 797 EXPECT_EQ(avdt_cfg.protect_info[0], AVDT_CP_LOSC); 798 EXPECT_EQ(avdt_cfg.protect_info[1], (AVDT_CP_SCMS_T_ID & 0xFF)); 799 EXPECT_EQ(avdt_cfg.protect_info[2], ((AVDT_CP_SCMS_T_ID >> 8) & 0xFF)); 800 EXPECT_EQ(avdt_cfg.num_protect, 1); 801 #endif 802 } 803 804 TEST_F(A2dpCodecConfigTest, createCodec) { 805 for (int i = BTAV_A2DP_CODEC_INDEX_MIN; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) { 806 btav_a2dp_codec_index_t codec_index = 807 static_cast<btav_a2dp_codec_index_t>(i); 808 809 // Ignore codecs that are not supported on the device 810 if (!has_codec_support(codec_index)) { 811 continue; 812 } 813 814 A2dpCodecConfig* codec_config = A2dpCodecConfig::createCodec(codec_index); 815 EXPECT_NE(codec_config, nullptr); 816 EXPECT_EQ(codec_config->codecIndex(), codec_index); 817 EXPECT_FALSE(codec_config->name().empty()); 818 EXPECT_NE(codec_config->codecPriority(), BTAV_A2DP_CODEC_PRIORITY_DISABLED); 819 EXPECT_NE(codec_config->codecPriority(), BTAV_A2DP_CODEC_PRIORITY_DEFAULT); 820 delete codec_config; 821 } 822 } 823 824 TEST_F(A2dpCodecConfigTest, setCodecConfig) { 825 uint8_t codec_info_result[AVDT_CODEC_SIZE]; 826 btav_a2dp_codec_index_t peer_codec_index; 827 A2dpCodecs* a2dp_codecs = 828 new A2dpCodecs(std::vector<btav_a2dp_codec_config_t>()); 829 A2dpCodecConfig* codec_config; 830 831 EXPECT_TRUE(a2dp_codecs->init()); 832 833 // Create the codec capability - SBC 834 memset(codec_info_result, 0, sizeof(codec_info_result)); 835 peer_codec_index = A2DP_SourceCodecIndex(codec_info_sbc_sink_capability); 836 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX); 837 codec_config = 838 a2dp_codecs->findSourceCodecConfig(codec_info_sbc_sink_capability); 839 EXPECT_NE(codec_config, nullptr); 840 EXPECT_TRUE(a2dp_codecs->setCodecConfig( 841 codec_info_sbc_sink_capability, true /* is_capability */, 842 codec_info_result, true /* select_current_codec */)); 843 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config); 844 // Compare the result codec with the local test codec info 845 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) { 846 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]); 847 } 848 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16); 849 850 // Create the codec capability - AAC 851 memset(codec_info_result, 0, sizeof(codec_info_result)); 852 peer_codec_index = A2DP_SourceCodecIndex(codec_info_aac_sink_capability); 853 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX); 854 codec_config = 855 a2dp_codecs->findSourceCodecConfig(codec_info_aac_sink_capability); 856 EXPECT_NE(codec_config, nullptr); 857 EXPECT_TRUE(a2dp_codecs->setCodecConfig( 858 codec_info_aac_sink_capability, true /* is_capability */, 859 codec_info_result, true /* select_current_codec */)); 860 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config); 861 // Compare the result codec with the local test codec info 862 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) { 863 EXPECT_EQ(codec_info_result[i], codec_info_aac[i]); 864 } 865 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16); 866 867 // Create the codec config - SBC 868 memset(codec_info_result, 0, sizeof(codec_info_result)); 869 peer_codec_index = A2DP_SourceCodecIndex(codec_info_sbc); 870 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX); 871 codec_config = a2dp_codecs->findSourceCodecConfig(codec_info_sbc); 872 EXPECT_NE(codec_config, nullptr); 873 EXPECT_TRUE(a2dp_codecs->setCodecConfig( 874 codec_info_sbc, false /* is_capability */, codec_info_result, 875 true /* select_current_codec */)); 876 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config); 877 // Compare the result codec with the local test codec info 878 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) { 879 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]); 880 } 881 EXPECT_FALSE(codec_config->useRtpHeaderMarkerBit()); 882 883 // Create the codec config - AAC 884 memset(codec_info_result, 0, sizeof(codec_info_result)); 885 peer_codec_index = A2DP_SourceCodecIndex(codec_info_aac); 886 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX); 887 codec_config = a2dp_codecs->findSourceCodecConfig(codec_info_aac); 888 EXPECT_NE(codec_config, nullptr); 889 EXPECT_TRUE(a2dp_codecs->setCodecConfig( 890 codec_info_aac, false /* is_capability */, codec_info_result, 891 true /* select_current_codec */)); 892 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config); 893 // Compare the result codec with the local test codec info 894 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) { 895 EXPECT_EQ(codec_info_result[i], codec_info_aac[i]); 896 } 897 EXPECT_TRUE(codec_config->useRtpHeaderMarkerBit()); 898 899 // Create the codec capability - SBC Sink 900 memset(codec_info_result, 0, sizeof(codec_info_result)); 901 peer_codec_index = A2DP_SinkCodecIndex(codec_info_sbc_capability); 902 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX); 903 codec_config = a2dp_codecs->findSinkCodecConfig(codec_info_sbc_capability); 904 EXPECT_NE(codec_config, nullptr); 905 EXPECT_TRUE(a2dp_codecs->setSinkCodecConfig( 906 codec_info_sbc_capability, true /* is_capability */, codec_info_result, 907 true /* select_current_codec */)); 908 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config); 909 // Compare the result codec with the local test codec info 910 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) { 911 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]); 912 } 913 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16); 914 915 // Create the codec capability - AAC Sink 916 memset(codec_info_result, 0, sizeof(codec_info_result)); 917 peer_codec_index = A2DP_SinkCodecIndex(codec_info_aac_capability); 918 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX); 919 codec_config = a2dp_codecs->findSinkCodecConfig(codec_info_aac_capability); 920 EXPECT_NE(codec_config, nullptr); 921 EXPECT_TRUE(a2dp_codecs->setSinkCodecConfig( 922 codec_info_aac_capability, true /* is_capability */, codec_info_result, 923 true /* select_current_codec */)); 924 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config); 925 // Compare the result codec with the local test codec info 926 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) { 927 EXPECT_EQ(codec_info_result[i], codec_info_aac[i]); 928 } 929 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16); 930 931 // Create the codec config - SBC Sink 932 memset(codec_info_result, 0, sizeof(codec_info_result)); 933 peer_codec_index = A2DP_SinkCodecIndex(codec_info_sbc); 934 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX); 935 codec_config = a2dp_codecs->findSinkCodecConfig(codec_info_sbc); 936 EXPECT_NE(codec_config, nullptr); 937 EXPECT_TRUE(a2dp_codecs->setSinkCodecConfig( 938 codec_info_sbc, false /* is_capability */, codec_info_result, 939 true /* select_current_codec */)); 940 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config); 941 // Compare the result codec with the local test codec info 942 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) { 943 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]); 944 } 945 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16); 946 947 // Create the codec config - AAC Sink 948 memset(codec_info_result, 0, sizeof(codec_info_result)); 949 peer_codec_index = A2DP_SinkCodecIndex(codec_info_aac); 950 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX); 951 codec_config = a2dp_codecs->findSinkCodecConfig(codec_info_aac); 952 EXPECT_NE(codec_config, nullptr); 953 EXPECT_TRUE(a2dp_codecs->setSinkCodecConfig( 954 codec_info_aac, false /* is_capability */, codec_info_result, 955 true /* select_current_codec */)); 956 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config); 957 // Compare the result codec with the local test codec info 958 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) { 959 EXPECT_EQ(codec_info_result[i], codec_info_aac[i]); 960 } 961 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16); 962 963 // Test invalid codec info 964 uint8_t codec_info_sbc_test1[AVDT_CODEC_SIZE]; 965 memset(codec_info_result, 0, sizeof(codec_info_result)); 966 memset(codec_info_sbc_test1, 0, sizeof(codec_info_sbc_test1)); 967 EXPECT_FALSE(a2dp_codecs->setCodecConfig( 968 codec_info_sbc_test1, true /* is_capability */, codec_info_result, 969 true /* select_current_codec */)); 970 delete a2dp_codecs; 971 } 972 973 TEST_F(A2dpCodecConfigTest, init) { 974 std::vector<btav_a2dp_codec_config_t> default_priorities; 975 A2dpCodecs codecs(default_priorities); 976 977 EXPECT_TRUE(codecs.init()); 978 979 const std::list<A2dpCodecConfig*> orderedSourceCodecs = 980 codecs.orderedSourceCodecs(); 981 EXPECT_FALSE(orderedSourceCodecs.empty()); 982 983 const std::list<A2dpCodecConfig*> orderedSinkCodecs = 984 codecs.orderedSinkCodecs(); 985 EXPECT_FALSE(orderedSinkCodecs.empty()); 986 } 987