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 #include <gtest/gtest.h>
21
22 #include <set>
23 #include <vector>
24
25 #include "stack/include/a2dp_aac.h"
26 #include "stack/include/a2dp_api.h"
27 #include "stack/include/a2dp_codec_api.h"
28 #include "stack/include/a2dp_sbc.h"
29 #include "stack/include/a2dp_vendor.h"
30 #include "stack/include/a2dp_vendor_opus_constants.h"
31 #include "stack/include/bt_hdr.h"
32
33 namespace {
34 const uint8_t codec_info_sbc[AVDT_CODEC_SIZE] = {
35 6, // Length (A2DP_SBC_INFO_LEN)
36 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
37 0, // Media Codec Type: A2DP_MEDIA_CT_SBC
38 0x20 | 0x01, // Sample Frequency: A2DP_SBC_IE_SAMP_FREQ_44 |
39 // Channel Mode: A2DP_SBC_IE_CH_MD_JOINT
40 0x10 | 0x04 | 0x01, // Block Length: A2DP_SBC_IE_BLOCKS_16 |
41 // Subbands: A2DP_SBC_IE_SUBBAND_8 |
42 // Allocation Method: A2DP_SBC_IE_ALLOC_MD_L
43 2, // MinimumBitpool Value: A2DP_SBC_IE_MIN_BITPOOL
44 53, // Maximum Bitpool Value: A2DP_SBC_MAX_BITPOOL
45 7, // Fake
46 8, // Fake
47 9 // Fake
48 };
49
50 const uint8_t codec_info_sbc_capability[AVDT_CODEC_SIZE] = {
51 6, // Length (A2DP_SBC_INFO_LEN)
52 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
53 0, // Media Codec Type: A2DP_MEDIA_CT_SBC
54 0x20 | // Sample Frequency: A2DP_SBC_IE_SAMP_FREQ_44 |
55 0x08 | 0x01, // Channel Mode: A2DP_SBC_IE_CH_MD_MONO |
56 // A2DP_SBC_IE_CH_MD_JOINT
57 0x80 | 0x40 | 0x20 | 0x10 | // Block Length: A2DP_SBC_IE_BLOCKS_4 |
58 // A2DP_SBC_IE_BLOCKS_8 |
59 // A2DP_SBC_IE_BLOCKS_12 |
60 // A2DP_SBC_IE_BLOCKS_16 |
61 0x04 | // Subbands: A2DP_SBC_IE_SUBBAND_8 |
62 0x01, // Allocation Method: A2DP_SBC_IE_ALLOC_MD_L
63 2, // MinimumBitpool Value: A2DP_SBC_IE_MIN_BITPOOL
64 53, // Maximum Bitpool Value: A2DP_SBC_MAX_BITPOOL
65 7, // Fake
66 8, // Fake
67 9 // Fake
68 };
69
70 const uint8_t codec_info_sbc_sink_capability[AVDT_CODEC_SIZE] = {
71 6, // Length (A2DP_SBC_INFO_LEN)
72 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
73 0, // Media Codec Type: A2DP_MEDIA_CT_SBC
74 0x20 | 0x10 | // Sample Frequency: A2DP_SBC_IE_SAMP_FREQ_44 |
75 // A2DP_SBC_IE_SAMP_FREQ_48 |
76 0x08 | 0x04 | 0x02 | 0x01, // Channel Mode: A2DP_SBC_IE_CH_MD_MONO |
77 // A2DP_SBC_IE_CH_MD_DUAL |
78 // A2DP_SBC_IE_CH_MD_STEREO |
79 // A2DP_SBC_IE_CH_MD_JOINT
80 0x80 | 0x40 | 0x20 | 0x10 | // Block Length: A2DP_SBC_IE_BLOCKS_4 |
81 // A2DP_SBC_IE_BLOCKS_8 |
82 // A2DP_SBC_IE_BLOCKS_12 |
83 // A2DP_SBC_IE_BLOCKS_16 |
84 0x08 | 0x04 | // Subbands: A2DP_SBC_IE_SUBBAND_4 |
85 // A2DP_SBC_IE_SUBBAND_8 |
86 0x02 | 0x01, // Allocation Method: A2DP_SBC_IE_ALLOC_MD_S |
87 // A2DP_SBC_IE_ALLOC_MD_L
88 2, // MinimumBitpool Value: A2DP_SBC_IE_MIN_BITPOOL
89 53, // Maximum Bitpool Value: A2DP_SBC_MAX_BITPOOL
90 7, // Fake
91 8, // Fake
92 9 // Fake
93 };
94
95 const uint8_t codec_info_aac[AVDT_CODEC_SIZE] = {
96 8, // Length (A2DP_AAC_INFO_LEN)
97 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
98 2, // Media Codec Type: A2DP_MEDIA_CT_AAC
99 0x80, // Object Type: A2DP_AAC_OBJECT_TYPE_MPEG2_LC
100 0x01, // Sampling Frequency: A2DP_AAC_SAMPLING_FREQ_44100
101 0x04, // Channels: A2DP_AAC_CHANNEL_MODE_STEREO
102 0x00 | 0x4, // Variable Bit Rate:
103 // A2DP_AAC_VARIABLE_BIT_RATE_DISABLED
104 // Bit Rate: 320000 = 0x4e200
105 0xe2, // Bit Rate: 320000 = 0x4e200
106 0x00, // Bit Rate: 320000 = 0x4e200
107 7, // Unused
108 8, // Unused
109 9 // Unused
110 };
111
112 const uint8_t codec_info_aac_vbr[AVDT_CODEC_SIZE] = {
113 8, // Length (A2DP_AAC_INFO_LEN)
114 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
115 2, // Media Codec Type: A2DP_MEDIA_CT_AAC
116 0x80, // Object Type: A2DP_AAC_OBJECT_TYPE_MPEG2_LC
117 0x01, // Sampling Frequency: A2DP_AAC_SAMPLING_FREQ_44100
118 0x04, // Channels: A2DP_AAC_CHANNEL_MODE_STEREO
119 0x80 | 0x4, // Variable Bit Rate:
120 // A2DP_AAC_VARIABLE_BIT_RATE_ENABLED
121 // Bit Rate: 320000 = 0x4e200
122 0xe2, // Bit Rate: 320000 = 0x4e200
123 0x00, // Bit Rate: 320000 = 0x4e200
124 7, // Unused
125 8, // Unused
126 9 // Unused
127 };
128
129 const uint8_t codec_info_aac_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, // Object Type: A2DP_AAC_OBJECT_TYPE_MPEG2_LC
134 0x01, // Sampling Frequency: A2DP_AAC_SAMPLING_FREQ_44100
135 // TODO: AAC 48.0kHz sampling rate should be added back - see b/62301376
136 0x04, // Channels: A2DP_AAC_CHANNEL_MODE_STEREO
137 0x00 | 0x4, // Variable Bit Rate:
138 // A2DP_AAC_VARIABLE_BIT_RATE_DISABLED
139 // Bit Rate: 320000 = 0x4e200
140 0xe2, // Bit Rate: 320000 = 0x4e200
141 0x00, // Bit Rate: 320000 = 0x4e200
142 7, // Unused
143 8, // Unused
144 9 // Unused
145 };
146
147 const uint8_t codec_info_aac_vbr_capability[AVDT_CODEC_SIZE] = {
148 8, // Length (A2DP_AAC_INFO_LEN)
149 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
150 2, // Media Codec Type: A2DP_MEDIA_CT_AAC
151 0x80, // Object Type: A2DP_AAC_OBJECT_TYPE_MPEG2_LC
152 0x01, // Sampling Frequency: A2DP_AAC_SAMPLING_FREQ_44100
153 // TODO: AAC 48.0kHz sampling rate should be added back - see b/62301376
154 0x04, // Channels: A2DP_AAC_CHANNEL_MODE_STEREO
155 0x80 | 0x4, // Variable Bit Rate:
156 // A2DP_AAC_VARIABLE_BIT_RATE_ENABLED
157 // Bit Rate: 320000 = 0x4e200
158 0xe2, // Bit Rate: 320000 = 0x4e200
159 0x00, // Bit Rate: 320000 = 0x4e200
160 7, // Unused
161 8, // Unused
162 9 // Unused
163 };
164
165 const uint8_t codec_info_aac_sink_capability[AVDT_CODEC_SIZE] = {
166 8, // Length (A2DP_AAC_INFO_LEN)
167 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
168 2, // Media Codec Type: A2DP_MEDIA_CT_AAC
169 0x80 | 0x40 | 0x20 | 0x10, // Object Type: A2DP_AAC_OBJECT_TYPE_MPEG2_LC |
170 // A2DP_AAC_OBJECT_TYPE_MPEG4_LC
171 // A2DP_AAC_OBJECT_TYPE_MPEG4_LTP
172 // A2DP_AAC_OBJECT_TYPE_MPEG4_SCALABLE
173 0x01, // Sampling Frequency: A2DP_AAC_SAMPLING_FREQ_44100
174 0x80 | 0x20 | 0x10 | 0x08 | 0x04, // Sampling Frequency:
175 // A2DP_AAC_SAMPLING_FREQ_48000 |
176 // A2DP_AAC_SAMPLING_FREQ_88200 |
177 // A2DP_AAC_SAMPLING_FREQ_96000 |
178 // Channels:
179 // A2DP_AAC_CHANNEL_MODE_MONO |
180 // A2DP_AAC_CHANNEL_MODE_STEREO
181 0x80 | 0x4, // Variable Bit Rate:
182 // A2DP_AAC_VARIABLE_BIT_RATE_ENABLED
183 // Bit Rate: 320000 = 0x4e200
184 0xe2, // Bit Rate: 320000 = 0x4e200
185 0x00, // Bit Rate: 320000 = 0x4e200
186 7, // Fake
187 8, // Fake
188 9 // Fake
189 };
190
191 const uint8_t codec_info_opus[AVDT_CODEC_SIZE] = {
192 A2DP_OPUS_CODEC_LEN, // Length
193 AVDT_MEDIA_TYPE_AUDIO << 4, // Media Type
194 A2DP_MEDIA_CT_NON_A2DP, // Media Codec Type Vendor
195 (A2DP_OPUS_VENDOR_ID & 0x000000FF),
196 (A2DP_OPUS_VENDOR_ID & 0x0000FF00) >> 8,
197 (A2DP_OPUS_VENDOR_ID & 0x00FF0000) >> 16,
198 (A2DP_OPUS_VENDOR_ID & 0xFF000000) >> 24,
199 (A2DP_OPUS_CODEC_ID & 0x00FF),
200 (A2DP_OPUS_CODEC_ID & 0xFF00) >> 8,
201 A2DP_OPUS_CHANNEL_MODE_STEREO | A2DP_OPUS_20MS_FRAMESIZE |
202 A2DP_OPUS_SAMPLING_FREQ_48000};
203
204 const uint8_t codec_info_opus_capability[AVDT_CODEC_SIZE] = {
205 A2DP_OPUS_CODEC_LEN, // Length
206 AVDT_MEDIA_TYPE_AUDIO << 4, // Media Type
207 A2DP_MEDIA_CT_NON_A2DP, // Media Codec Type Vendor
208 (A2DP_OPUS_VENDOR_ID & 0x000000FF),
209 (A2DP_OPUS_VENDOR_ID & 0x0000FF00) >> 8,
210 (A2DP_OPUS_VENDOR_ID & 0x00FF0000) >> 16,
211 (A2DP_OPUS_VENDOR_ID & 0xFF000000) >> 24,
212 (A2DP_OPUS_CODEC_ID & 0x00FF),
213 (A2DP_OPUS_CODEC_ID & 0xFF00) >> 8,
214 A2DP_OPUS_CHANNEL_MODE_MONO | A2DP_OPUS_CHANNEL_MODE_STEREO |
215 A2DP_OPUS_10MS_FRAMESIZE | A2DP_OPUS_20MS_FRAMESIZE |
216 A2DP_OPUS_SAMPLING_FREQ_48000};
217
218 const uint8_t codec_info_opus_sink_capability[AVDT_CODEC_SIZE] = {
219 A2DP_OPUS_CODEC_LEN, // Length
220 AVDT_MEDIA_TYPE_AUDIO << 4, // Media Type
221 A2DP_MEDIA_CT_NON_A2DP, // Media Codec Type Vendor
222 (A2DP_OPUS_VENDOR_ID & 0x000000FF),
223 (A2DP_OPUS_VENDOR_ID & 0x0000FF00) >> 8,
224 (A2DP_OPUS_VENDOR_ID & 0x00FF0000) >> 16,
225 (A2DP_OPUS_VENDOR_ID & 0xFF000000) >> 24,
226 (A2DP_OPUS_CODEC_ID & 0x00FF),
227 (A2DP_OPUS_CODEC_ID & 0xFF00) >> 8,
228 A2DP_OPUS_CHANNEL_MODE_MONO | A2DP_OPUS_CHANNEL_MODE_STEREO |
229 A2DP_OPUS_10MS_FRAMESIZE | A2DP_OPUS_20MS_FRAMESIZE |
230 A2DP_OPUS_SAMPLING_FREQ_48000};
231
232 const uint8_t codec_info_non_a2dp[AVDT_CODEC_SIZE] = {
233 8, // Length
234 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
235 0xFF, // Media Codec Type: A2DP_MEDIA_CT_NON_A2DP
236 3, 4, 0, 0, // Vendor ID: LSB first, upper two octets should be 0
237 7, 8, // Codec ID: LSB first
238 9 // Fake
239 };
240
241 const uint8_t codec_info_non_a2dp_fake[AVDT_CODEC_SIZE] = {
242 8, // Length
243 0, // Media Type: AVDT_MEDIA_TYPE_AUDIO
244 0xFF, // Media Codec Type: A2DP_MEDIA_CT_NON_A2DP
245 3, 4, 0, 0, // Vendor ID: LSB first, upper two octets should be 0
246 7, 8, // Codec ID: LSB first
247 10 // Unused
248 };
249
250 static const char* APTX_ENCODER_LIB_NAME = "libaptX_encoder.so";
251 static const char* APTX_HD_ENCODER_LIB_NAME = "libaptXHD_encoder.so";
252 static const char* LDAC_ENCODER_LIB_NAME = "libldacBT_enc.so";
253 static const char* LDAC_DECODER_LIB_NAME = "libldacBT_dec.so";
254
has_shared_library(const char * name)255 static bool has_shared_library(const char* name) {
256 void* lib_handle = dlopen(name, RTLD_NOW);
257 if (lib_handle != nullptr) {
258 dlclose(lib_handle);
259 return true;
260 }
261 return false;
262 }
263
264 } // namespace
265
266 class StackA2dpTest : public ::testing::Test {
267 protected:
StackA2dpTest()268 StackA2dpTest() {
269 // Create the set with all supported codecs
270 for (int i = BTAV_A2DP_CODEC_INDEX_MIN; i < BTAV_A2DP_CODEC_INDEX_MAX;
271 i++) {
272 btav_a2dp_codec_index_t codec_index =
273 static_cast<btav_a2dp_codec_index_t>(i);
274
275 bool supported = false;
276 switch (codec_index) {
277 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
278 supported = true;
279 break;
280 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
281 supported = true;
282 break;
283 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
284 // Codec aptX is supported only if the device has the corresponding
285 // shared library installed.
286 supported = has_shared_library(APTX_ENCODER_LIB_NAME);
287 break;
288 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
289 // Codec aptX-HD is supported only if the device has the corresponding
290 // shared library installed.
291 supported = has_shared_library(APTX_HD_ENCODER_LIB_NAME);
292 break;
293 case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
294 // Codec LDAC is supported only if the device has the corresponding
295 // shared library installed.
296 supported = has_shared_library(LDAC_ENCODER_LIB_NAME);
297 break;
298 case BTAV_A2DP_CODEC_INDEX_SINK_SBC:
299 supported = true;
300 break;
301 case BTAV_A2DP_CODEC_INDEX_SINK_AAC:
302 supported = true;
303 break;
304 case BTAV_A2DP_CODEC_INDEX_SINK_LDAC:
305 // Codec LDAC is supported only if the device has the corresponding
306 // shared library installed.
307 supported = has_shared_library(LDAC_DECODER_LIB_NAME);
308 break;
309 case BTAV_A2DP_CODEC_INDEX_SOURCE_LC3:
310 break;
311 case BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS:
312 case BTAV_A2DP_CODEC_INDEX_SINK_OPUS:
313 supported = true;
314 break;
315 case BTAV_A2DP_CODEC_INDEX_MAX:
316 case BTAV_A2DP_CODEC_INDEX_SOURCE_MAX:
317 case BTAV_A2DP_CODEC_INDEX_SINK_MAX:
318 // Needed to avoid using "default:" case so we can capture when
319 // a new codec is added, and it can be included here.
320 break;
321 }
322
323 if (supported) {
324 supported_codecs_.insert(codec_index);
325 }
326 }
327 }
328
has_codec_support(btav_a2dp_codec_index_t codec_index)329 bool has_codec_support(btav_a2dp_codec_index_t codec_index) {
330 return supported_codecs_.find(codec_index) != supported_codecs_.end();
331 }
332
333 private:
334 std::set<btav_a2dp_codec_index_t> supported_codecs_;
335 };
336
337 class A2dpCodecConfigTest : public StackA2dpTest {};
338
TEST_F(StackA2dpTest,test_a2dp_bits_set)339 TEST_F(StackA2dpTest, test_a2dp_bits_set) {
340 EXPECT_TRUE(A2DP_BitsSet(0x0) == A2DP_SET_ZERO_BIT);
341 EXPECT_TRUE(A2DP_BitsSet(0x1) == A2DP_SET_ONE_BIT);
342 EXPECT_TRUE(A2DP_BitsSet(0x2) == A2DP_SET_ONE_BIT);
343 EXPECT_TRUE(A2DP_BitsSet(0x3) == A2DP_SET_MULTL_BIT);
344 EXPECT_TRUE(A2DP_BitsSet(0x7f) == A2DP_SET_MULTL_BIT);
345 EXPECT_TRUE(A2DP_BitsSet(0x80) == A2DP_SET_ONE_BIT);
346 EXPECT_TRUE(A2DP_BitsSet(0x81) == A2DP_SET_MULTL_BIT);
347 EXPECT_TRUE(A2DP_BitsSet(0xc0) == A2DP_SET_MULTL_BIT);
348 EXPECT_TRUE(A2DP_BitsSet(0xff) == A2DP_SET_MULTL_BIT);
349 EXPECT_TRUE(A2DP_BitsSet(0x8000) == A2DP_SET_ONE_BIT);
350 EXPECT_TRUE(A2DP_BitsSet(0x8001) == A2DP_SET_MULTL_BIT);
351 EXPECT_TRUE(A2DP_BitsSet(0xc000) == A2DP_SET_MULTL_BIT);
352 EXPECT_TRUE(A2DP_BitsSet(0xffff) == A2DP_SET_MULTL_BIT);
353 EXPECT_TRUE(A2DP_BitsSet(0x80000) == A2DP_SET_ONE_BIT);
354 EXPECT_TRUE(A2DP_BitsSet(0x80001) == A2DP_SET_MULTL_BIT);
355 EXPECT_TRUE(A2DP_BitsSet(0xc0000) == A2DP_SET_MULTL_BIT);
356 EXPECT_TRUE(A2DP_BitsSet(0xfffff) == A2DP_SET_MULTL_BIT);
357 EXPECT_TRUE(A2DP_BitsSet(0x80000000) == A2DP_SET_ONE_BIT);
358 EXPECT_TRUE(A2DP_BitsSet(0x80000001) == A2DP_SET_MULTL_BIT);
359 EXPECT_TRUE(A2DP_BitsSet(0xc0000000) == A2DP_SET_MULTL_BIT);
360 EXPECT_TRUE(A2DP_BitsSet(0xffffffff) == A2DP_SET_MULTL_BIT);
361 EXPECT_TRUE(A2DP_BitsSet(0x8000000000000000) == A2DP_SET_ONE_BIT);
362 EXPECT_TRUE(A2DP_BitsSet(0x8000000000000001) == A2DP_SET_MULTL_BIT);
363 EXPECT_TRUE(A2DP_BitsSet(0xc000000000000000) == A2DP_SET_MULTL_BIT);
364 EXPECT_TRUE(A2DP_BitsSet(0xffffffffffffffff) == A2DP_SET_MULTL_BIT);
365 }
366
TEST_F(StackA2dpTest,test_a2dp_is_codec_valid_sbc)367 TEST_F(StackA2dpTest, test_a2dp_is_codec_valid_sbc) {
368 EXPECT_TRUE(A2DP_IsSourceCodecValid(codec_info_sbc));
369 EXPECT_TRUE(A2DP_IsSourceCodecValid(codec_info_sbc_capability));
370 EXPECT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_sbc));
371 EXPECT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_sbc_capability));
372
373 EXPECT_TRUE(A2DP_IsSinkCodecValid(codec_info_sbc_sink_capability));
374 EXPECT_TRUE(A2DP_IsPeerSinkCodecValid(codec_info_sbc_sink_capability));
375
376 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_non_a2dp));
377 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_non_a2dp));
378 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_non_a2dp));
379 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_non_a2dp));
380
381 // Test with invalid SBC codecs
382 uint8_t codec_info_sbc_invalid[AVDT_CODEC_SIZE];
383 memset(codec_info_sbc_invalid, 0, sizeof(codec_info_sbc_invalid));
384 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_sbc_invalid));
385 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_sbc_invalid));
386 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_sbc_invalid));
387 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_sbc_invalid));
388
389 memcpy(codec_info_sbc_invalid, codec_info_sbc, sizeof(codec_info_sbc));
390 codec_info_sbc_invalid[0] = 0; // Corrupt the Length field
391 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_sbc_invalid));
392 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_sbc_invalid));
393 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_sbc_invalid));
394 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_sbc_invalid));
395
396 memcpy(codec_info_sbc_invalid, codec_info_sbc, sizeof(codec_info_sbc));
397 codec_info_sbc_invalid[1] = 0xff; // Corrupt the Media Type field
398 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_sbc_invalid));
399 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_sbc_invalid));
400 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_sbc_invalid));
401 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_sbc_invalid));
402 }
403
TEST_F(StackA2dpTest,test_a2dp_is_codec_valid_aac)404 TEST_F(StackA2dpTest, test_a2dp_is_codec_valid_aac) {
405 ASSERT_TRUE(A2DP_IsSourceCodecValid(codec_info_aac_vbr));
406 ASSERT_TRUE(A2DP_IsSourceCodecValid(codec_info_aac_vbr_capability));
407 ASSERT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_aac_vbr));
408 ASSERT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_aac_vbr_capability));
409 EXPECT_TRUE(A2DP_IsSourceCodecValid(codec_info_aac));
410 EXPECT_TRUE(A2DP_IsSourceCodecValid(codec_info_aac_capability));
411 EXPECT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_aac));
412 EXPECT_TRUE(A2DP_IsPeerSourceCodecValid(codec_info_aac_capability));
413
414 EXPECT_TRUE(A2DP_IsSinkCodecValid(codec_info_aac_sink_capability));
415 EXPECT_TRUE(A2DP_IsPeerSinkCodecValid(codec_info_aac_sink_capability));
416
417 // Test with invalid AAC codecs
418 uint8_t codec_info_aac_invalid[AVDT_CODEC_SIZE];
419 memcpy(codec_info_aac_invalid, codec_info_aac, sizeof(codec_info_aac));
420 codec_info_aac_invalid[0] = 0; // Corrupt the Length field
421 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_aac_invalid));
422 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_aac_invalid));
423 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_aac_invalid));
424 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_aac_invalid));
425
426 memcpy(codec_info_aac_invalid, codec_info_aac, sizeof(codec_info_aac));
427 codec_info_aac_invalid[1] = 0xff; // Corrupt the Media Type field
428 EXPECT_FALSE(A2DP_IsSourceCodecValid(codec_info_aac_invalid));
429 EXPECT_FALSE(A2DP_IsSinkCodecValid(codec_info_aac_invalid));
430 EXPECT_FALSE(A2DP_IsPeerSourceCodecValid(codec_info_aac_invalid));
431 EXPECT_FALSE(A2DP_IsPeerSinkCodecValid(codec_info_aac_invalid));
432 }
433
TEST_F(StackA2dpTest,test_a2dp_is_codec_valid_opus)434 TEST_F(StackA2dpTest, test_a2dp_is_codec_valid_opus) {
435 ASSERT_TRUE(A2DP_IsVendorSourceCodecValid(codec_info_opus));
436 ASSERT_TRUE(A2DP_IsVendorSourceCodecValid(codec_info_opus_capability));
437 ASSERT_TRUE(A2DP_IsVendorPeerSourceCodecValid(codec_info_opus));
438 ASSERT_TRUE(A2DP_IsVendorPeerSourceCodecValid(codec_info_opus_capability));
439
440 ASSERT_TRUE(A2DP_IsVendorSinkCodecValid(codec_info_opus_sink_capability));
441 ASSERT_TRUE(A2DP_IsVendorPeerSinkCodecValid(codec_info_opus_sink_capability));
442
443 // Test with invalid Opus configuration
444 uint8_t codec_info_opus_invalid[AVDT_CODEC_SIZE];
445 memcpy(codec_info_opus_invalid, codec_info_opus, sizeof(codec_info_opus));
446 codec_info_opus_invalid[0] = 0; // Corrupt the Length field
447 ASSERT_FALSE(A2DP_IsVendorSourceCodecValid(codec_info_opus_invalid));
448 ASSERT_FALSE(A2DP_IsVendorSinkCodecValid(codec_info_opus_invalid));
449 ASSERT_FALSE(A2DP_IsVendorPeerSourceCodecValid(codec_info_opus_invalid));
450 ASSERT_FALSE(A2DP_IsVendorPeerSinkCodecValid(codec_info_opus_invalid));
451
452 memcpy(codec_info_opus_invalid, codec_info_opus, sizeof(codec_info_opus));
453 codec_info_opus_invalid[1] = 0xff; // Corrupt the Media Type field
454 ASSERT_FALSE(A2DP_IsVendorSourceCodecValid(codec_info_opus_invalid));
455 ASSERT_FALSE(A2DP_IsVendorSinkCodecValid(codec_info_opus_invalid));
456 ASSERT_FALSE(A2DP_IsVendorPeerSourceCodecValid(codec_info_opus_invalid));
457 ASSERT_FALSE(A2DP_IsVendorPeerSinkCodecValid(codec_info_opus_invalid));
458 }
459
TEST_F(StackA2dpTest,test_a2dp_get_codec_type)460 TEST_F(StackA2dpTest, test_a2dp_get_codec_type) {
461 tA2DP_CODEC_TYPE codec_type = A2DP_GetCodecType(codec_info_sbc);
462 EXPECT_EQ(codec_type, A2DP_MEDIA_CT_SBC);
463
464 codec_type = A2DP_GetCodecType(codec_info_aac);
465 EXPECT_EQ(codec_type, A2DP_MEDIA_CT_AAC);
466
467 codec_type = A2DP_GetCodecType(codec_info_opus);
468 ASSERT_EQ(codec_type, A2DP_MEDIA_CT_NON_A2DP);
469
470 codec_type = A2DP_GetCodecType(codec_info_non_a2dp);
471 EXPECT_EQ(codec_type, A2DP_MEDIA_CT_NON_A2DP);
472 }
473
TEST_F(StackA2dpTest,test_a2dp_is_sink_codec_supported)474 TEST_F(StackA2dpTest, test_a2dp_is_sink_codec_supported) {
475 EXPECT_TRUE(A2DP_IsSinkCodecSupported(codec_info_sbc));
476 EXPECT_FALSE(A2DP_IsSinkCodecSupported(codec_info_sbc_capability));
477 EXPECT_FALSE(A2DP_IsSinkCodecSupported(codec_info_sbc_sink_capability));
478
479 EXPECT_TRUE(A2DP_IsSinkCodecSupported(codec_info_aac));
480 // NOTE: The test below should be EXPECT_FALSE.
481 // However, codec_info_aac_capability is practically same as codec_info_aac,
482 // therefore we cannot differentiate it as a capability.
483 EXPECT_TRUE(A2DP_IsSinkCodecSupported(codec_info_aac_capability));
484 EXPECT_FALSE(A2DP_IsSinkCodecSupported(codec_info_aac_sink_capability));
485
486 EXPECT_FALSE(A2DP_IsSinkCodecSupported(codec_info_non_a2dp));
487 }
488
TEST_F(StackA2dpTest,test_a2dp_is_peer_source_codec_supported)489 TEST_F(StackA2dpTest, test_a2dp_is_peer_source_codec_supported) {
490 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_sbc));
491 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_sbc_capability));
492 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_sbc_sink_capability));
493
494 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_aac));
495 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_aac_capability));
496 EXPECT_TRUE(A2DP_IsPeerSourceCodecSupported(codec_info_aac_sink_capability));
497
498 EXPECT_FALSE(A2DP_IsPeerSourceCodecSupported(codec_info_non_a2dp));
499 }
500
TEST_F(StackA2dpTest,test_init_default_codec)501 TEST_F(StackA2dpTest, test_init_default_codec) {
502 uint8_t codec_info_result[AVDT_CODEC_SIZE];
503
504 memset(codec_info_result, 0, sizeof(codec_info_result));
505 A2DP_InitDefaultCodec(codec_info_result);
506
507 // Compare the result codec with the local test codec info
508 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) {
509 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]);
510 }
511 }
512
TEST_F(StackA2dpTest,test_a2dp_uses_rtp_header)513 TEST_F(StackA2dpTest, test_a2dp_uses_rtp_header) {
514 EXPECT_TRUE(A2DP_UsesRtpHeader(true, codec_info_sbc));
515 EXPECT_TRUE(A2DP_UsesRtpHeader(false, codec_info_sbc));
516
517 EXPECT_TRUE(A2DP_UsesRtpHeader(true, codec_info_aac));
518 EXPECT_TRUE(A2DP_UsesRtpHeader(false, codec_info_aac));
519
520 ASSERT_TRUE(A2DP_VendorUsesRtpHeader(true, codec_info_opus));
521 ASSERT_TRUE(A2DP_VendorUsesRtpHeader(false, codec_info_opus));
522
523 EXPECT_TRUE(A2DP_UsesRtpHeader(true, codec_info_non_a2dp));
524 EXPECT_TRUE(A2DP_UsesRtpHeader(false, codec_info_non_a2dp));
525 }
526
TEST_F(StackA2dpTest,test_a2dp_get_media_type)527 TEST_F(StackA2dpTest, test_a2dp_get_media_type) {
528 uint8_t codec_info_test[AVDT_CODEC_SIZE];
529
530 EXPECT_EQ(A2DP_GetMediaType(codec_info_sbc), AVDT_MEDIA_TYPE_AUDIO);
531 EXPECT_EQ(A2DP_GetMediaType(codec_info_aac), AVDT_MEDIA_TYPE_AUDIO);
532 EXPECT_EQ(A2DP_GetMediaType(codec_info_non_a2dp), AVDT_MEDIA_TYPE_AUDIO);
533
534 // Prepare fake codec info for video and for multimedia
535 memset(codec_info_test, 0, sizeof(codec_info_test));
536 codec_info_test[0] = sizeof(codec_info_test);
537 codec_info_test[1] = 0x01 << 4;
538 EXPECT_EQ(A2DP_GetMediaType(codec_info_test), AVDT_MEDIA_TYPE_VIDEO);
539 codec_info_test[1] = 0x02 << 4;
540 EXPECT_EQ(A2DP_GetMediaType(codec_info_test), AVDT_MEDIA_TYPE_MULTI);
541 }
542
TEST_F(StackA2dpTest,test_a2dp_codec_name)543 TEST_F(StackA2dpTest, test_a2dp_codec_name) {
544 uint8_t codec_info_test[AVDT_CODEC_SIZE];
545
546 // Explicit tests for known codecs
547 EXPECT_STREQ(A2DP_CodecName(codec_info_sbc), "SBC");
548 EXPECT_STREQ(A2DP_CodecName(codec_info_sbc_capability), "SBC");
549 EXPECT_STREQ(A2DP_CodecName(codec_info_sbc_sink_capability), "SBC");
550 EXPECT_STREQ(A2DP_CodecName(codec_info_aac), "AAC");
551 EXPECT_STREQ(A2DP_CodecName(codec_info_aac_capability), "AAC");
552 EXPECT_STREQ(A2DP_CodecName(codec_info_aac_sink_capability), "AAC");
553 ASSERT_STREQ(A2DP_CodecName(codec_info_opus), "Opus");
554 ASSERT_STREQ(A2DP_CodecName(codec_info_opus_capability), "Opus");
555 ASSERT_STREQ(A2DP_CodecName(codec_info_opus_sink_capability), "Opus");
556 EXPECT_STREQ(A2DP_CodecName(codec_info_non_a2dp), "UNKNOWN VENDOR CODEC");
557
558 // Test all unknown codecs
559 memcpy(codec_info_test, codec_info_sbc, sizeof(codec_info_sbc));
560 for (uint8_t codec_type = A2DP_MEDIA_CT_AAC + 1;
561 codec_type < A2DP_MEDIA_CT_NON_A2DP; codec_type++) {
562 codec_info_test[2] = codec_type; // Unknown codec type
563 EXPECT_STREQ(A2DP_CodecName(codec_info_test), "UNKNOWN CODEC");
564 }
565 }
566
TEST_F(StackA2dpTest,test_a2dp_vendor)567 TEST_F(StackA2dpTest, test_a2dp_vendor) {
568 EXPECT_EQ(A2DP_VendorCodecGetVendorId(codec_info_non_a2dp),
569 (uint32_t)0x00000403);
570 EXPECT_EQ(A2DP_VendorCodecGetCodecId(codec_info_non_a2dp), (uint16_t)0x0807);
571 EXPECT_TRUE(A2DP_VendorUsesRtpHeader(true, codec_info_non_a2dp));
572 EXPECT_TRUE(A2DP_VendorUsesRtpHeader(false, codec_info_non_a2dp));
573 }
574
TEST_F(StackA2dpTest,test_a2dp_codec_type_equals)575 TEST_F(StackA2dpTest, test_a2dp_codec_type_equals) {
576 EXPECT_TRUE(A2DP_CodecTypeEquals(codec_info_sbc, codec_info_sbc_capability));
577 EXPECT_TRUE(
578 A2DP_CodecTypeEquals(codec_info_sbc, codec_info_sbc_sink_capability));
579
580 EXPECT_TRUE(A2DP_CodecTypeEquals(codec_info_aac, codec_info_aac_capability));
581 EXPECT_TRUE(
582 A2DP_CodecTypeEquals(codec_info_aac, codec_info_aac_sink_capability));
583
584 ASSERT_TRUE(
585 A2DP_VendorCodecTypeEquals(codec_info_opus, codec_info_opus_capability));
586 ASSERT_TRUE(A2DP_VendorCodecTypeEquals(codec_info_opus,
587 codec_info_opus_sink_capability));
588
589 EXPECT_TRUE(
590 A2DP_CodecTypeEquals(codec_info_non_a2dp, codec_info_non_a2dp_fake));
591
592 EXPECT_FALSE(A2DP_CodecTypeEquals(codec_info_sbc, codec_info_non_a2dp));
593 EXPECT_FALSE(A2DP_CodecTypeEquals(codec_info_aac, codec_info_non_a2dp));
594 EXPECT_FALSE(A2DP_CodecTypeEquals(codec_info_sbc, codec_info_aac));
595 }
596
TEST_F(StackA2dpTest,test_a2dp_codec_equals)597 TEST_F(StackA2dpTest, test_a2dp_codec_equals) {
598 uint8_t codec_info_sbc_test[AVDT_CODEC_SIZE];
599 uint8_t codec_info_aac_test[AVDT_CODEC_SIZE];
600 uint8_t codec_info_opus_test[AVDT_CODEC_SIZE];
601 uint8_t codec_info_non_a2dp_test[AVDT_CODEC_SIZE];
602
603 // Test two identical SBC codecs
604 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test));
605 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc));
606 EXPECT_TRUE(A2DP_CodecEquals(codec_info_sbc, codec_info_sbc_test));
607
608 // Test two identical AAC codecs
609 memset(codec_info_aac_test, 0xAB, sizeof(codec_info_aac_test));
610 memcpy(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac));
611 EXPECT_TRUE(A2DP_CodecEquals(codec_info_aac, codec_info_aac_test));
612
613 // Test two identical Opus codecs
614 memset(codec_info_opus_test, 0xAB, sizeof(codec_info_opus_test));
615 memcpy(codec_info_opus_test, codec_info_opus, sizeof(codec_info_opus));
616 ASSERT_TRUE(A2DP_VendorCodecEquals(codec_info_opus, codec_info_opus_test));
617
618 // Test two identical non-A2DP codecs that are not recognized
619 memset(codec_info_non_a2dp_test, 0xAB, sizeof(codec_info_non_a2dp_test));
620 memcpy(codec_info_non_a2dp_test, codec_info_non_a2dp,
621 sizeof(codec_info_non_a2dp));
622 EXPECT_FALSE(A2DP_CodecEquals(codec_info_non_a2dp, codec_info_non_a2dp_test));
623
624 // Test two codecs that have different types
625 EXPECT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_non_a2dp));
626 ASSERT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_aac));
627 ASSERT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_opus));
628
629 // Test two SBC codecs that are slightly different
630 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test));
631 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc));
632 codec_info_sbc_test[5] = codec_info_sbc[5] + 1;
633 EXPECT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_sbc_test));
634 codec_info_sbc_test[5] = codec_info_sbc[5];
635 codec_info_sbc_test[6] = codec_info_sbc[6] + 1;
636 EXPECT_FALSE(A2DP_CodecEquals(codec_info_sbc, codec_info_sbc_test));
637
638 // Test two AAC codecs that are slightly different
639 memset(codec_info_aac_test, 0xAB, sizeof(codec_info_aac_test));
640 memcpy(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac));
641 codec_info_aac_test[7] = codec_info_aac[7] + 1;
642 EXPECT_FALSE(A2DP_CodecEquals(codec_info_aac, codec_info_aac_test));
643 codec_info_aac_test[7] = codec_info_aac[7];
644 codec_info_aac_test[8] = codec_info_aac[8] + 1;
645 EXPECT_FALSE(A2DP_CodecEquals(codec_info_aac, codec_info_aac_test));
646
647 // Test two SBC codecs that are identical, but with different fake
648 // trailer data.
649 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test));
650 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc));
651 codec_info_sbc_test[7] = codec_info_sbc[7] + 1;
652 EXPECT_TRUE(A2DP_CodecEquals(codec_info_sbc, codec_info_sbc_test));
653
654 // Test two AAC codecs that are identical, but with different fake
655 // trailer data.
656 memset(codec_info_aac_test, 0xAB, sizeof(codec_info_aac_test));
657 memcpy(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac));
658 codec_info_aac_test[9] = codec_info_aac[9] + 1;
659 EXPECT_TRUE(A2DP_CodecEquals(codec_info_aac, codec_info_aac_test));
660 }
661
TEST_F(StackA2dpTest,test_a2dp_get_track_sample_rate)662 TEST_F(StackA2dpTest, test_a2dp_get_track_sample_rate) {
663 EXPECT_EQ(A2DP_GetTrackSampleRate(codec_info_sbc), 44100);
664 EXPECT_EQ(A2DP_GetTrackSampleRate(codec_info_aac), 44100);
665 ASSERT_EQ(A2DP_VendorGetTrackSampleRate(codec_info_opus), 48000);
666 EXPECT_EQ(A2DP_GetTrackSampleRate(codec_info_non_a2dp), -1);
667 }
668
TEST_F(StackA2dpTest,test_a2dp_get_track_channel_count)669 TEST_F(StackA2dpTest, test_a2dp_get_track_channel_count) {
670 EXPECT_EQ(A2DP_GetTrackChannelCount(codec_info_sbc), 2);
671 EXPECT_EQ(A2DP_GetTrackChannelCount(codec_info_aac), 2);
672 ASSERT_EQ(A2DP_VendorGetTrackChannelCount(codec_info_opus), 2);
673 EXPECT_EQ(A2DP_GetTrackChannelCount(codec_info_non_a2dp), -1);
674 }
675
TEST_F(StackA2dpTest,test_a2dp_get_number_of_subbands_sbc)676 TEST_F(StackA2dpTest, test_a2dp_get_number_of_subbands_sbc) {
677 EXPECT_EQ(A2DP_GetNumberOfSubbandsSbc(codec_info_sbc), 8);
678 EXPECT_EQ(A2DP_GetNumberOfSubbandsSbc(codec_info_aac), -1);
679 EXPECT_EQ(A2DP_GetNumberOfSubbandsSbc(codec_info_non_a2dp), -1);
680 }
681
TEST_F(StackA2dpTest,test_a2dp_get_number_of_blocks_sbc)682 TEST_F(StackA2dpTest, test_a2dp_get_number_of_blocks_sbc) {
683 EXPECT_EQ(A2DP_GetNumberOfBlocksSbc(codec_info_sbc), 16);
684 EXPECT_EQ(A2DP_GetNumberOfBlocksSbc(codec_info_aac), -1);
685 EXPECT_EQ(A2DP_GetNumberOfBlocksSbc(codec_info_non_a2dp), -1);
686 }
687
TEST_F(StackA2dpTest,test_a2dp_get_allocation_method_code_sbc)688 TEST_F(StackA2dpTest, test_a2dp_get_allocation_method_code_sbc) {
689 EXPECT_EQ(A2DP_GetAllocationMethodCodeSbc(codec_info_sbc), 0);
690 EXPECT_EQ(A2DP_GetAllocationMethodCodeSbc(codec_info_aac), -1);
691 EXPECT_EQ(A2DP_GetAllocationMethodCodeSbc(codec_info_non_a2dp), -1);
692 }
693
TEST_F(StackA2dpTest,test_a2dp_get_channel_mode_code_sbc)694 TEST_F(StackA2dpTest, test_a2dp_get_channel_mode_code_sbc) {
695 EXPECT_EQ(A2DP_GetChannelModeCodeSbc(codec_info_sbc), 3);
696 EXPECT_EQ(A2DP_GetChannelModeCodeSbc(codec_info_aac), -1);
697 EXPECT_EQ(A2DP_GetChannelModeCodeSbc(codec_info_non_a2dp), -1);
698 }
699
TEST_F(StackA2dpTest,test_a2dp_get_sampling_frequency_code_sbc)700 TEST_F(StackA2dpTest, test_a2dp_get_sampling_frequency_code_sbc) {
701 EXPECT_EQ(A2DP_GetSamplingFrequencyCodeSbc(codec_info_sbc), 2);
702 EXPECT_EQ(A2DP_GetSamplingFrequencyCodeSbc(codec_info_aac), -1);
703 EXPECT_EQ(A2DP_GetSamplingFrequencyCodeSbc(codec_info_non_a2dp), -1);
704 }
705
TEST_F(StackA2dpTest,test_a2dp_get_min_bitpool_sbc)706 TEST_F(StackA2dpTest, test_a2dp_get_min_bitpool_sbc) {
707 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_sbc), 2);
708 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_sbc_capability), 2);
709 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_sbc_sink_capability), 2);
710 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_aac), -1);
711 EXPECT_EQ(A2DP_GetMinBitpoolSbc(codec_info_non_a2dp), -1);
712 }
713
TEST_F(StackA2dpTest,test_a2dp_get_max_bitpool_sbc)714 TEST_F(StackA2dpTest, test_a2dp_get_max_bitpool_sbc) {
715 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_sbc), 53);
716 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_sbc_capability), 53);
717 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_sbc_sink_capability), 53);
718 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_aac), -1);
719 EXPECT_EQ(A2DP_GetMaxBitpoolSbc(codec_info_non_a2dp), -1);
720 }
721
TEST_F(StackA2dpTest,test_a2dp_get_sink_track_channel_type)722 TEST_F(StackA2dpTest, test_a2dp_get_sink_track_channel_type) {
723 EXPECT_EQ(A2DP_GetSinkTrackChannelType(codec_info_sbc), 3);
724 EXPECT_EQ(A2DP_GetSinkTrackChannelType(codec_info_aac), 3);
725 ASSERT_EQ(A2DP_VendorGetSinkTrackChannelType(codec_info_opus), 2);
726 EXPECT_EQ(A2DP_GetSinkTrackChannelType(codec_info_non_a2dp), -1);
727 }
728
TEST_F(StackA2dpTest,test_a2dp_get_object_type_code_aac)729 TEST_F(StackA2dpTest, test_a2dp_get_object_type_code_aac) {
730 EXPECT_EQ(A2DP_GetObjectTypeCodeAac(codec_info_sbc), -1);
731 EXPECT_EQ(A2DP_GetObjectTypeCodeAac(codec_info_aac), 0x80);
732 EXPECT_EQ(A2DP_GetObjectTypeCodeAac(codec_info_non_a2dp), -1);
733 }
734
TEST_F(StackA2dpTest,test_a2dp_get_channel_mode_code_aac)735 TEST_F(StackA2dpTest, test_a2dp_get_channel_mode_code_aac) {
736 EXPECT_EQ(A2DP_GetChannelModeCodeAac(codec_info_sbc), -1);
737 EXPECT_EQ(A2DP_GetChannelModeCodeAac(codec_info_aac), 0x04);
738 EXPECT_EQ(A2DP_GetChannelModeCodeAac(codec_info_non_a2dp), -1);
739 }
740
TEST_F(StackA2dpTest,test_a2dp_get_variable_bit_rate_support_aac)741 TEST_F(StackA2dpTest, test_a2dp_get_variable_bit_rate_support_aac) {
742 EXPECT_EQ(A2DP_GetVariableBitRateSupportAac(codec_info_sbc), -1);
743 EXPECT_EQ(A2DP_GetVariableBitRateSupportAac(codec_info_aac), 0);
744 EXPECT_EQ(A2DP_GetVariableBitRateSupportAac(codec_info_non_a2dp), -1);
745 }
746
TEST_F(StackA2dpTest,test_a2dp_get_bit_rate_aac)747 TEST_F(StackA2dpTest, test_a2dp_get_bit_rate_aac) {
748 EXPECT_EQ(A2DP_GetBitRateAac(codec_info_sbc), -1);
749 EXPECT_EQ(A2DP_GetBitRateAac(codec_info_aac), 320000);
750 EXPECT_EQ(A2DP_GetBitRateAac(codec_info_non_a2dp), -1);
751 }
752
TEST_F(StackA2dpTest,test_a2dp_get_packet_timestamp)753 TEST_F(StackA2dpTest, test_a2dp_get_packet_timestamp) {
754 uint8_t a2dp_data[1000];
755 uint32_t timestamp;
756 uint32_t* p_ts = reinterpret_cast<uint32_t*>(a2dp_data);
757
758 memset(a2dp_data, 0xAB, sizeof(a2dp_data));
759 *p_ts = 0x12345678;
760 timestamp = 0xFFFFFFFF;
761 EXPECT_TRUE(A2DP_GetPacketTimestamp(codec_info_sbc, a2dp_data, ×tamp));
762 EXPECT_EQ(timestamp, static_cast<uint32_t>(0x12345678));
763
764 memset(a2dp_data, 0xAB, sizeof(a2dp_data));
765 *p_ts = 0x12345678;
766 timestamp = 0xFFFFFFFF;
767 EXPECT_TRUE(A2DP_GetPacketTimestamp(codec_info_aac, a2dp_data, ×tamp));
768 EXPECT_EQ(timestamp, static_cast<uint32_t>(0x12345678));
769
770 memset(a2dp_data, 0xAB, sizeof(a2dp_data));
771 *p_ts = 0x12345678;
772 timestamp = 0xFFFFFFFF;
773 ASSERT_TRUE(
774 A2DP_VendorGetPacketTimestamp(codec_info_opus, a2dp_data, ×tamp));
775 ASSERT_EQ(timestamp, static_cast<uint32_t>(0x12345678));
776
777 memset(a2dp_data, 0xAB, sizeof(a2dp_data));
778 *p_ts = 0x12345678;
779 timestamp = 0xFFFFFFFF;
780 EXPECT_FALSE(
781 A2DP_GetPacketTimestamp(codec_info_non_a2dp, a2dp_data, ×tamp));
782 }
783
TEST_F(StackA2dpTest,test_a2dp_build_codec_header)784 TEST_F(StackA2dpTest, test_a2dp_build_codec_header) {
785 uint8_t a2dp_data[1000];
786 BT_HDR* p_buf = reinterpret_cast<BT_HDR*>(a2dp_data);
787 const uint16_t BT_HDR_LEN = 500;
788 const uint16_t BT_HDR_OFFSET = 50;
789 const uint8_t FRAMES_PER_PACKET = 0xCD;
790
791 memset(a2dp_data, 0xAB, sizeof(a2dp_data));
792 p_buf->len = BT_HDR_LEN;
793 p_buf->offset = BT_HDR_OFFSET;
794 EXPECT_TRUE(A2DP_BuildCodecHeader(codec_info_sbc, p_buf, FRAMES_PER_PACKET));
795 EXPECT_EQ(p_buf->offset + 1,
796 BT_HDR_OFFSET); // Modified by A2DP_SBC_MPL_HDR_LEN
797 EXPECT_EQ(p_buf->len - 1, BT_HDR_LEN); // Modified by A2DP_SBC_MPL_HDR_LEN
798 const uint8_t* p =
799 reinterpret_cast<const uint8_t*>(p_buf + 1) + p_buf->offset;
800 EXPECT_EQ(
801 *p, static_cast<uint8_t>(0x0D)); // 0xCD masked with A2DP_SBC_HDR_NUM_MSK
802
803 memset(a2dp_data, 0xAB, sizeof(a2dp_data));
804 p_buf->len = BT_HDR_LEN;
805 p_buf->offset = BT_HDR_OFFSET;
806 EXPECT_TRUE(A2DP_BuildCodecHeader(codec_info_aac, p_buf, FRAMES_PER_PACKET));
807
808 memset(a2dp_data, 0xAB, sizeof(a2dp_data));
809 p_buf->len = BT_HDR_LEN;
810 p_buf->offset = BT_HDR_OFFSET;
811 EXPECT_FALSE(
812 A2DP_BuildCodecHeader(codec_info_non_a2dp, p_buf, FRAMES_PER_PACKET));
813 }
814
TEST_F(StackA2dpTest,test_a2dp_adjust_codec)815 TEST_F(StackA2dpTest, test_a2dp_adjust_codec) {
816 uint8_t codec_info_sbc_test[AVDT_CODEC_SIZE];
817 uint8_t codec_info_aac_test[AVDT_CODEC_SIZE];
818 uint8_t codec_info_non_a2dp_test[AVDT_CODEC_SIZE];
819
820 // Test updating a valid SBC codec that doesn't need adjustment
821 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test));
822 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc));
823 EXPECT_TRUE(A2DP_AdjustCodec(codec_info_sbc_test));
824 EXPECT_TRUE(
825 memcmp(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)) == 0);
826
827 // Test updating a valid SBC codec that needs adjustment
828 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test));
829 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc));
830 codec_info_sbc_test[6] = 54; // A2DP_SBC_MAX_BITPOOL + 1
831 EXPECT_TRUE(A2DP_AdjustCodec(codec_info_sbc_test));
832 EXPECT_TRUE(
833 memcmp(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc)) == 0);
834
835 // Test updating an invalid SBC codec
836 memset(codec_info_sbc_test, 0xAB, sizeof(codec_info_sbc_test));
837 memcpy(codec_info_sbc_test, codec_info_sbc, sizeof(codec_info_sbc));
838 codec_info_sbc_test[6] = 255; // Invalid MAX_BITPOOL
839 EXPECT_FALSE(A2DP_AdjustCodec(codec_info_sbc_test));
840
841 // Test updating a valid AAC codec that doesn't need adjustment
842 memset(codec_info_aac_test, 0xAB, sizeof(codec_info_aac_test));
843 memcpy(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac));
844 EXPECT_TRUE(A2DP_AdjustCodec(codec_info_aac_test));
845 EXPECT_TRUE(
846 memcmp(codec_info_aac_test, codec_info_aac, sizeof(codec_info_aac)) == 0);
847
848 // Test updating a non-A2DP codec that is not recognized
849 memset(codec_info_non_a2dp_test, 0xAB, sizeof(codec_info_non_a2dp_test));
850 memcpy(codec_info_non_a2dp_test, codec_info_non_a2dp,
851 sizeof(codec_info_non_a2dp));
852 EXPECT_FALSE(A2DP_AdjustCodec(codec_info_non_a2dp_test));
853 }
854
TEST_F(StackA2dpTest,test_a2dp_source_codec_index)855 TEST_F(StackA2dpTest, test_a2dp_source_codec_index) {
856 // Explicit tests for known Source codecs
857 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_sbc),
858 BTAV_A2DP_CODEC_INDEX_SOURCE_SBC);
859 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_sbc_capability),
860 BTAV_A2DP_CODEC_INDEX_SOURCE_SBC);
861 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_sbc_sink_capability),
862 BTAV_A2DP_CODEC_INDEX_SOURCE_SBC);
863 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_aac),
864 BTAV_A2DP_CODEC_INDEX_SOURCE_AAC);
865 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_aac_capability),
866 BTAV_A2DP_CODEC_INDEX_SOURCE_AAC);
867 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_aac_sink_capability),
868 BTAV_A2DP_CODEC_INDEX_SOURCE_AAC);
869 ASSERT_EQ(A2DP_VendorSourceCodecIndex(codec_info_opus),
870 BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS);
871 ASSERT_EQ(A2DP_VendorSourceCodecIndex(codec_info_opus_capability),
872 BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS);
873 ASSERT_EQ(A2DP_VendorSourceCodecIndex(codec_info_opus_sink_capability),
874 BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS);
875 EXPECT_EQ(A2DP_SourceCodecIndex(codec_info_non_a2dp),
876 BTAV_A2DP_CODEC_INDEX_MAX);
877 }
878
TEST_F(StackA2dpTest,test_a2dp_sink_codec_index)879 TEST_F(StackA2dpTest, test_a2dp_sink_codec_index) {
880 // Explicit tests for known Sink codecs
881 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_sbc),
882 BTAV_A2DP_CODEC_INDEX_SINK_SBC);
883 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_sbc_capability),
884 BTAV_A2DP_CODEC_INDEX_SINK_SBC);
885 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_sbc_sink_capability),
886 BTAV_A2DP_CODEC_INDEX_SINK_SBC);
887 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_aac),
888 BTAV_A2DP_CODEC_INDEX_SINK_AAC);
889 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_aac_capability),
890 BTAV_A2DP_CODEC_INDEX_SINK_AAC);
891 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_aac_sink_capability),
892 BTAV_A2DP_CODEC_INDEX_SINK_AAC);
893 ASSERT_EQ(A2DP_VendorSinkCodecIndex(codec_info_opus),
894 BTAV_A2DP_CODEC_INDEX_SINK_OPUS);
895 ASSERT_EQ(A2DP_VendorSinkCodecIndex(codec_info_opus_capability),
896 BTAV_A2DP_CODEC_INDEX_SINK_OPUS);
897 ASSERT_EQ(A2DP_VendorSinkCodecIndex(codec_info_opus_sink_capability),
898 BTAV_A2DP_CODEC_INDEX_SINK_OPUS);
899 EXPECT_EQ(A2DP_SinkCodecIndex(codec_info_non_a2dp),
900 BTAV_A2DP_CODEC_INDEX_MAX);
901 }
902
TEST_F(StackA2dpTest,test_a2dp_codec_index_str)903 TEST_F(StackA2dpTest, test_a2dp_codec_index_str) {
904 // Explicit tests for known codecs
905 EXPECT_STREQ(A2DP_CodecIndexStr(BTAV_A2DP_CODEC_INDEX_SOURCE_SBC), "SBC");
906 EXPECT_STREQ(A2DP_CodecIndexStr(BTAV_A2DP_CODEC_INDEX_SINK_SBC), "SBC SINK");
907 EXPECT_STREQ(A2DP_CodecIndexStr(BTAV_A2DP_CODEC_INDEX_SOURCE_AAC), "AAC");
908 ASSERT_STREQ(A2DP_VendorCodecIndexStr(BTAV_A2DP_CODEC_INDEX_SOURCE_OPUS),
909 "Opus");
910 ASSERT_STREQ(A2DP_VendorCodecIndexStr(BTAV_A2DP_CODEC_INDEX_SINK_OPUS),
911 "Opus SINK");
912
913 // Test that the unknown codec string has not changed
914 EXPECT_STREQ(A2DP_CodecIndexStr(BTAV_A2DP_CODEC_INDEX_MAX),
915 "UNKNOWN CODEC INDEX");
916
917 // Test that each codec has a known string
918 for (int i = BTAV_A2DP_CODEC_INDEX_MIN; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
919 if ((i >= BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MIN &&
920 i < BTAV_A2DP_CODEC_INDEX_SOURCE_EXT_MAX) ||
921 (i >= BTAV_A2DP_CODEC_INDEX_SINK_EXT_MIN &&
922 i < BTAV_A2DP_CODEC_INDEX_SINK_EXT_MAX)) {
923 continue;
924 }
925 btav_a2dp_codec_index_t codec_index =
926 static_cast<btav_a2dp_codec_index_t>(i);
927 EXPECT_STRNE(A2DP_CodecIndexStr(codec_index), "UNKNOWN CODEC INDEX");
928 }
929 }
930
TEST_F(StackA2dpTest,test_a2dp_init_codec_config)931 TEST_F(StackA2dpTest, test_a2dp_init_codec_config) {
932 AvdtpSepConfig avdt_cfg;
933
934 //
935 // Test for SBC Source
936 //
937 memset(&avdt_cfg, 0, sizeof(avdt_cfg));
938 EXPECT_TRUE(
939 A2DP_InitCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_SBC, &avdt_cfg));
940 // Compare the result codec with the local test codec info
941 for (size_t i = 0; i < codec_info_sbc_capability[0] + 1; i++) {
942 EXPECT_EQ(avdt_cfg.codec_info[i], codec_info_sbc_capability[i]);
943 }
944
945 //
946 // Test for SBC Sink
947 //
948 memset(&avdt_cfg, 0, sizeof(avdt_cfg));
949 EXPECT_TRUE(A2DP_InitCodecConfig(BTAV_A2DP_CODEC_INDEX_SINK_SBC, &avdt_cfg));
950 // Compare the result codec with the local test codec info
951 for (size_t i = 0; i < codec_info_sbc_sink_capability[0] + 1; i++) {
952 EXPECT_EQ(avdt_cfg.codec_info[i], codec_info_sbc_sink_capability[i]);
953 }
954
955 //
956 // Test for AAC Source
957 //
958 memset(&avdt_cfg, 0, sizeof(avdt_cfg));
959 ASSERT_TRUE(
960 A2DP_InitCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_AAC, &avdt_cfg));
961 // Check the vbr mode status.
962 bool aac_vbr_mode_enabled =
963 avdt_cfg.codec_info[6] & A2DP_AAC_VARIABLE_BIT_RATE_MASK;
964 // Compare the result codec with the local test codec info
965 if (aac_vbr_mode_enabled) {
966 for (size_t i = 0; i < codec_info_aac_vbr_capability[0] + 1; i++) {
967 ASSERT_EQ(avdt_cfg.codec_info[i], codec_info_aac_vbr_capability[i]);
968 }
969 } else {
970 for (size_t i = 0; i < codec_info_aac_capability[0] + 1; i++) {
971 ASSERT_EQ(avdt_cfg.codec_info[i], codec_info_aac_capability[i]);
972 }
973 }
974 }
975
TEST_F(A2dpCodecConfigTest,createCodec)976 TEST_F(A2dpCodecConfigTest, createCodec) {
977 for (int i = BTAV_A2DP_CODEC_INDEX_MIN; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
978 btav_a2dp_codec_index_t codec_index =
979 static_cast<btav_a2dp_codec_index_t>(i);
980
981 // Ignore codecs that are not supported on the device
982 if (!has_codec_support(codec_index)) {
983 continue;
984 }
985
986 A2dpCodecConfig* codec_config = A2dpCodecConfig::createCodec(codec_index);
987 EXPECT_NE(codec_config, nullptr);
988 EXPECT_EQ(codec_config->codecIndex(), codec_index);
989 EXPECT_FALSE(codec_config->name().empty());
990 EXPECT_NE(codec_config->codecPriority(), BTAV_A2DP_CODEC_PRIORITY_DISABLED);
991 EXPECT_NE(codec_config->codecPriority(), BTAV_A2DP_CODEC_PRIORITY_DEFAULT);
992 delete codec_config;
993 }
994 }
995
TEST_F(A2dpCodecConfigTest,setCodecConfig)996 TEST_F(A2dpCodecConfigTest, setCodecConfig) {
997 uint8_t codec_info_result[AVDT_CODEC_SIZE];
998 btav_a2dp_codec_index_t peer_codec_index;
999 A2dpCodecs* a2dp_codecs =
1000 new A2dpCodecs(std::vector<btav_a2dp_codec_config_t>());
1001 A2dpCodecConfig* codec_config;
1002
1003 EXPECT_TRUE(a2dp_codecs->init());
1004
1005 // Create the codec capability - SBC
1006 memset(codec_info_result, 0, sizeof(codec_info_result));
1007 peer_codec_index = A2DP_SourceCodecIndex(codec_info_sbc_sink_capability);
1008 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1009 codec_config =
1010 a2dp_codecs->findSourceCodecConfig(codec_info_sbc_sink_capability);
1011 EXPECT_NE(codec_config, nullptr);
1012 EXPECT_TRUE(a2dp_codecs->setCodecConfig(
1013 codec_info_sbc_sink_capability, true /* is_capability */,
1014 codec_info_result, true /* select_current_codec */));
1015 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1016 // Compare the result codec with the local test codec info
1017 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) {
1018 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]);
1019 }
1020 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16);
1021
1022 // Create the codec config - SBC
1023 memset(codec_info_result, 0, sizeof(codec_info_result));
1024 peer_codec_index = A2DP_SourceCodecIndex(codec_info_sbc);
1025 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1026 codec_config = a2dp_codecs->findSourceCodecConfig(codec_info_sbc);
1027 EXPECT_NE(codec_config, nullptr);
1028 EXPECT_TRUE(a2dp_codecs->setCodecConfig(
1029 codec_info_sbc, false /* is_capability */, codec_info_result,
1030 true /* select_current_codec */));
1031 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1032 // Compare the result codec with the local test codec info
1033 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) {
1034 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]);
1035 }
1036 EXPECT_FALSE(codec_config->useRtpHeaderMarkerBit());
1037
1038 // Create the codec config - AAC
1039 memset(codec_info_result, 0, sizeof(codec_info_result));
1040 peer_codec_index = A2DP_SourceCodecIndex(codec_info_aac);
1041 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1042 codec_config = a2dp_codecs->findSourceCodecConfig(codec_info_aac);
1043 EXPECT_NE(codec_config, nullptr);
1044 EXPECT_TRUE(a2dp_codecs->setCodecConfig(
1045 codec_info_aac, false /* is_capability */, codec_info_result,
1046 true /* select_current_codec */));
1047 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1048 // Compare the result codec with the local test codec info
1049 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) {
1050 EXPECT_EQ(codec_info_result[i], codec_info_aac[i]);
1051 }
1052 EXPECT_TRUE(codec_config->useRtpHeaderMarkerBit());
1053
1054 // Create the codec capability - SBC Sink
1055 memset(codec_info_result, 0, sizeof(codec_info_result));
1056 peer_codec_index = A2DP_SinkCodecIndex(codec_info_sbc_capability);
1057 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1058 codec_config = a2dp_codecs->findSinkCodecConfig(codec_info_sbc_capability);
1059 EXPECT_NE(codec_config, nullptr);
1060 EXPECT_TRUE(a2dp_codecs->setSinkCodecConfig(
1061 codec_info_sbc_capability, true /* is_capability */, codec_info_result,
1062 true /* select_current_codec */));
1063 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1064 // Compare the result codec with the local test codec info
1065 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) {
1066 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]);
1067 }
1068 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16);
1069
1070 // Create the codec capability - AAC Sink
1071 memset(codec_info_result, 0, sizeof(codec_info_result));
1072 peer_codec_index = A2DP_SinkCodecIndex(codec_info_aac_capability);
1073 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1074 codec_config = a2dp_codecs->findSinkCodecConfig(codec_info_aac_capability);
1075 EXPECT_NE(codec_config, nullptr);
1076 EXPECT_TRUE(a2dp_codecs->setSinkCodecConfig(
1077 codec_info_aac_capability, true /* is_capability */, codec_info_result,
1078 true /* select_current_codec */));
1079 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1080 // Compare the result codec with the local test codec info
1081 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) {
1082 EXPECT_EQ(codec_info_result[i], codec_info_aac[i]);
1083 }
1084 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16);
1085
1086 // Create the codec config - SBC Sink
1087 memset(codec_info_result, 0, sizeof(codec_info_result));
1088 peer_codec_index = A2DP_SinkCodecIndex(codec_info_sbc);
1089 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1090 codec_config = a2dp_codecs->findSinkCodecConfig(codec_info_sbc);
1091 EXPECT_NE(codec_config, nullptr);
1092 EXPECT_TRUE(a2dp_codecs->setSinkCodecConfig(
1093 codec_info_sbc, false /* is_capability */, codec_info_result,
1094 true /* select_current_codec */));
1095 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1096 // Compare the result codec with the local test codec info
1097 for (size_t i = 0; i < codec_info_sbc[0] + 1; i++) {
1098 EXPECT_EQ(codec_info_result[i], codec_info_sbc[i]);
1099 }
1100 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16);
1101
1102 // Create the codec config - AAC Sink
1103 memset(codec_info_result, 0, sizeof(codec_info_result));
1104 peer_codec_index = A2DP_SinkCodecIndex(codec_info_aac);
1105 EXPECT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1106 codec_config = a2dp_codecs->findSinkCodecConfig(codec_info_aac);
1107 EXPECT_NE(codec_config, nullptr);
1108 EXPECT_TRUE(a2dp_codecs->setSinkCodecConfig(
1109 codec_info_aac, false /* is_capability */, codec_info_result,
1110 true /* select_current_codec */));
1111 EXPECT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1112 // Compare the result codec with the local test codec info
1113 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) {
1114 EXPECT_EQ(codec_info_result[i], codec_info_aac[i]);
1115 }
1116 EXPECT_EQ(codec_config->getAudioBitsPerSample(), 16);
1117
1118 // Test invalid codec info
1119 uint8_t codec_info_sbc_test1[AVDT_CODEC_SIZE];
1120 memset(codec_info_result, 0, sizeof(codec_info_result));
1121 memset(codec_info_sbc_test1, 0, sizeof(codec_info_sbc_test1));
1122 EXPECT_FALSE(a2dp_codecs->setCodecConfig(
1123 codec_info_sbc_test1, true /* is_capability */, codec_info_result,
1124 true /* select_current_codec */));
1125
1126 AvdtpSepConfig avdt_cfg;
1127 memset(&avdt_cfg, 0, sizeof(avdt_cfg));
1128 ASSERT_TRUE(
1129 A2DP_InitCodecConfig(BTAV_A2DP_CODEC_INDEX_SOURCE_AAC, &avdt_cfg));
1130 bool aac_vbr_mode_enabled =
1131 avdt_cfg.codec_info[6] & A2DP_AAC_VARIABLE_BIT_RATE_MASK;
1132
1133 // Create the codec capability - AAC
1134 memset(codec_info_result, 0, sizeof(codec_info_result));
1135 peer_codec_index = A2DP_SourceCodecIndex(codec_info_aac_sink_capability);
1136 ASSERT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1137 codec_config =
1138 a2dp_codecs->findSourceCodecConfig(codec_info_aac_sink_capability);
1139 ASSERT_NE(codec_config, nullptr);
1140 ASSERT_TRUE(a2dp_codecs->setCodecConfig(
1141 codec_info_aac_sink_capability, true /* is_capability */,
1142 codec_info_result, true /* select_current_codec */));
1143 ASSERT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1144 // Compare the result codec with the local test codec info
1145 if (aac_vbr_mode_enabled) {
1146 for (size_t i = 0; i < codec_info_aac_vbr[0] + 1; i++) {
1147 ASSERT_EQ(codec_info_result[i], codec_info_aac_vbr[i]);
1148 }
1149 } else {
1150 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) {
1151 ASSERT_EQ(codec_info_result[i], codec_info_aac[i]);
1152 }
1153 }
1154 ASSERT_EQ(codec_config->getAudioBitsPerSample(), 16);
1155
1156 // Create the codec config - AAC with vbr
1157 memset(codec_info_result, 0, sizeof(codec_info_result));
1158 peer_codec_index = A2DP_SourceCodecIndex(codec_info_aac_vbr);
1159 ASSERT_NE(peer_codec_index, BTAV_A2DP_CODEC_INDEX_MAX);
1160 codec_config = a2dp_codecs->findSourceCodecConfig(codec_info_aac_vbr);
1161 ASSERT_NE(codec_config, nullptr);
1162 ASSERT_TRUE(a2dp_codecs->setCodecConfig(
1163 codec_info_aac_vbr, false /* is_capability */, codec_info_result,
1164 true /* select_current_codec */));
1165 ASSERT_EQ(a2dp_codecs->getCurrentCodecConfig(), codec_config);
1166 // Compare the result codec with the local test codec info
1167 if (aac_vbr_mode_enabled) {
1168 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) {
1169 ASSERT_EQ(codec_info_result[i], codec_info_aac_vbr[i]);
1170 }
1171 } else {
1172 for (size_t i = 0; i < codec_info_aac[0] + 1; i++) {
1173 ASSERT_EQ(codec_info_result[i], codec_info_aac[i]);
1174 }
1175 }
1176 ASSERT_TRUE(codec_config->useRtpHeaderMarkerBit());
1177
1178 delete a2dp_codecs;
1179 }
1180
TEST_F(A2dpCodecConfigTest,init)1181 TEST_F(A2dpCodecConfigTest, init) {
1182 std::vector<btav_a2dp_codec_config_t> default_priorities;
1183 A2dpCodecs codecs(default_priorities);
1184
1185 EXPECT_TRUE(codecs.init());
1186
1187 const std::list<A2dpCodecConfig*> orderedSourceCodecs =
1188 codecs.orderedSourceCodecs();
1189 EXPECT_FALSE(orderedSourceCodecs.empty());
1190
1191 const std::list<A2dpCodecConfig*> orderedSinkCodecs =
1192 codecs.orderedSinkCodecs();
1193 EXPECT_FALSE(orderedSinkCodecs.empty());
1194 }
1195