1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "BTAudioProviderSessionCodecsDB"
18
19 #include "BluetoothAudioSupportedCodecsDB.h"
20
21 #include <android-base/logging.h>
22
23 namespace android {
24 namespace bluetooth {
25 namespace audio {
26
27 using ::android::hardware::bluetooth::audio::V2_0::AacObjectType;
28 using ::android::hardware::bluetooth::audio::V2_0::AacParameters;
29 using ::android::hardware::bluetooth::audio::V2_0::AacVariableBitRate;
30 using ::android::hardware::bluetooth::audio::V2_0::AptxParameters;
31 using ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
32 using ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
33 using ::android::hardware::bluetooth::audio::V2_0::CodecType;
34 using ::android::hardware::bluetooth::audio::V2_0::LdacChannelMode;
35 using ::android::hardware::bluetooth::audio::V2_0::LdacParameters;
36 using ::android::hardware::bluetooth::audio::V2_0::LdacQualityIndex;
37 using ::android::hardware::bluetooth::audio::V2_0::SampleRate;
38 using ::android::hardware::bluetooth::audio::V2_0::SbcAllocMethod;
39 using ::android::hardware::bluetooth::audio::V2_0::SbcBlockLength;
40 using ::android::hardware::bluetooth::audio::V2_0::SbcChannelMode;
41 using ::android::hardware::bluetooth::audio::V2_0::SbcNumSubbands;
42 using ::android::hardware::bluetooth::audio::V2_0::SbcParameters;
43
44 // Default Supported PCM Parameters
45 static const PcmParameters kDefaultSoftwarePcmCapabilities = {
46 .sampleRate = static_cast<SampleRate>(
47 SampleRate::RATE_44100 | SampleRate::RATE_48000 |
48 SampleRate::RATE_88200 | SampleRate::RATE_96000 |
49 SampleRate::RATE_16000 | SampleRate::RATE_24000),
50 .channelMode =
51 static_cast<ChannelMode>(ChannelMode::MONO | ChannelMode::STEREO),
52 .bitsPerSample = static_cast<BitsPerSample>(BitsPerSample::BITS_16 |
53 BitsPerSample::BITS_24 |
54 BitsPerSample::BITS_32)};
55
56 // Default Supported Codecs
57 // SBC: mSampleRate:(44100), mBitsPerSample:(16), mChannelMode:(MONO|STEREO)
58 // all blocks | subbands 8 | Loudness
59 static const SbcParameters kDefaultOffloadSbcCapability = {
60 .sampleRate = SampleRate::RATE_44100,
61 .channelMode = static_cast<SbcChannelMode>(SbcChannelMode::MONO |
62 SbcChannelMode::JOINT_STEREO),
63 .blockLength = static_cast<SbcBlockLength>(
64 SbcBlockLength::BLOCKS_4 | SbcBlockLength::BLOCKS_8 |
65 SbcBlockLength::BLOCKS_12 | SbcBlockLength::BLOCKS_16),
66 .numSubbands = SbcNumSubbands::SUBBAND_8,
67 .allocMethod = SbcAllocMethod::ALLOC_MD_L,
68 .bitsPerSample = BitsPerSample::BITS_16,
69 .minBitpool = 2,
70 .maxBitpool = 53};
71
72 // AAC: mSampleRate:(44100), mBitsPerSample:(16), mChannelMode:(STEREO)
73 static const AacParameters kDefaultOffloadAacCapability = {
74 .objectType = AacObjectType::MPEG2_LC,
75 .sampleRate = SampleRate::RATE_44100,
76 .channelMode = ChannelMode::STEREO,
77 .variableBitRateEnabled = AacVariableBitRate::ENABLED,
78 .bitsPerSample = BitsPerSample::BITS_16};
79
80 // LDAC: mSampleRate:(44100|48000|88200|96000), mBitsPerSample:(16|24|32),
81 // mChannelMode:(DUAL|STEREO)
82 static const LdacParameters kDefaultOffloadLdacCapability = {
83 .sampleRate = static_cast<SampleRate>(
84 SampleRate::RATE_44100 | SampleRate::RATE_48000 |
85 SampleRate::RATE_88200 | SampleRate::RATE_96000),
86 .channelMode = static_cast<LdacChannelMode>(LdacChannelMode::DUAL |
87 LdacChannelMode::STEREO),
88 .qualityIndex = LdacQualityIndex::QUALITY_HIGH,
89 .bitsPerSample = static_cast<BitsPerSample>(BitsPerSample::BITS_16 |
90 BitsPerSample::BITS_24 |
91 BitsPerSample::BITS_32)};
92
93 // aptX: mSampleRate:(44100|48000), mBitsPerSample:(16), mChannelMode:(STEREO)
94 static const AptxParameters kDefaultOffloadAptxCapability = {
95 .sampleRate = static_cast<SampleRate>(SampleRate::RATE_44100 |
96 SampleRate::RATE_48000),
97 .channelMode = ChannelMode::STEREO,
98 .bitsPerSample = BitsPerSample::BITS_16,
99 };
100
101 // aptX HD: mSampleRate:(44100|48000), mBitsPerSample:(24),
102 // mChannelMode:(STEREO)
103 static const AptxParameters kDefaultOffloadAptxHdCapability = {
104 .sampleRate = static_cast<SampleRate>(SampleRate::RATE_44100 |
105 SampleRate::RATE_48000),
106 .channelMode = ChannelMode::STEREO,
107 .bitsPerSample = BitsPerSample::BITS_24,
108 };
109
110 const std::vector<CodecCapabilities> kDefaultOffloadA2dpCodecCapabilities = {
111 {.codecType = CodecType::SBC, .capabilities = {}},
112 {.codecType = CodecType::AAC, .capabilities = {}},
113 {.codecType = CodecType::LDAC, .capabilities = {}},
114 {.codecType = CodecType::APTX, .capabilities = {}},
115 {.codecType = CodecType::APTX_HD, .capabilities = {}}};
116
IsSingleBit(uint32_t bitmasks,uint32_t bitfield)117 static bool IsSingleBit(uint32_t bitmasks, uint32_t bitfield) {
118 bool single = false;
119 uint32_t test_bit = 0x00000001;
120 while (test_bit <= bitmasks && test_bit <= bitfield) {
121 if (bitfield & test_bit && bitmasks & test_bit) {
122 if (single) return false;
123 single = true;
124 }
125 if (test_bit == 0x80000000) break;
126 test_bit <<= 1;
127 }
128 return single;
129 }
130
131 static bool IsOffloadSbcConfigurationValid(
132 const CodecConfiguration::CodecSpecific& codec_specific);
133 static bool IsOffloadAacConfigurationValid(
134 const CodecConfiguration::CodecSpecific& codec_specific);
135 static bool IsOffloadLdacConfigurationValid(
136 const CodecConfiguration::CodecSpecific& codec_specific);
137 static bool IsOffloadAptxConfigurationValid(
138 const CodecConfiguration::CodecSpecific& codec_specific);
139 static bool IsOffloadAptxHdConfigurationValid(
140 const CodecConfiguration::CodecSpecific& codec_specific);
141
IsOffloadSbcConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)142 static bool IsOffloadSbcConfigurationValid(
143 const CodecConfiguration::CodecSpecific& codec_specific) {
144 if (codec_specific.getDiscriminator() !=
145 CodecConfiguration::CodecSpecific::hidl_discriminator::sbcConfig) {
146 LOG(WARNING) << __func__
147 << ": Invalid CodecSpecific=" << toString(codec_specific);
148 return false;
149 }
150 const SbcParameters sbc_data = codec_specific.sbcConfig();
151 if (!IsSingleBit(static_cast<uint32_t>(sbc_data.sampleRate), 0xff) ||
152 !IsSingleBit(static_cast<uint32_t>(sbc_data.channelMode), 0x0f) ||
153 !IsSingleBit(static_cast<uint32_t>(sbc_data.blockLength), 0xf0) ||
154 !IsSingleBit(static_cast<uint32_t>(sbc_data.numSubbands), 0x0c) ||
155 !IsSingleBit(static_cast<uint32_t>(sbc_data.allocMethod), 0x03) ||
156 !IsSingleBit(static_cast<uint32_t>(sbc_data.bitsPerSample), 0x07) ||
157 sbc_data.minBitpool > sbc_data.maxBitpool) {
158 LOG(WARNING) << __func__
159 << ": Invalid CodecSpecific=" << toString(codec_specific);
160 return false;
161 } else if ((sbc_data.sampleRate & kDefaultOffloadSbcCapability.sampleRate) &&
162 (sbc_data.channelMode &
163 kDefaultOffloadSbcCapability.channelMode) &&
164 (sbc_data.blockLength &
165 kDefaultOffloadSbcCapability.blockLength) &&
166 (sbc_data.numSubbands &
167 kDefaultOffloadSbcCapability.numSubbands) &&
168 (sbc_data.allocMethod &
169 kDefaultOffloadSbcCapability.allocMethod) &&
170 (sbc_data.bitsPerSample &
171 kDefaultOffloadSbcCapability.bitsPerSample) &&
172 (kDefaultOffloadSbcCapability.minBitpool <= sbc_data.minBitpool &&
173 sbc_data.maxBitpool <= kDefaultOffloadSbcCapability.maxBitpool)) {
174 return true;
175 }
176 LOG(WARNING) << __func__
177 << ": Unsupported CodecSpecific=" << toString(codec_specific);
178 return false;
179 }
180
IsOffloadAacConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)181 static bool IsOffloadAacConfigurationValid(
182 const CodecConfiguration::CodecSpecific& codec_specific) {
183 if (codec_specific.getDiscriminator() !=
184 CodecConfiguration::CodecSpecific::hidl_discriminator::aacConfig) {
185 LOG(WARNING) << __func__
186 << ": Invalid CodecSpecific=" << toString(codec_specific);
187 return false;
188 }
189 const AacParameters aac_data = codec_specific.aacConfig();
190 if (!IsSingleBit(static_cast<uint32_t>(aac_data.objectType), 0xf0) ||
191 !IsSingleBit(static_cast<uint32_t>(aac_data.sampleRate), 0xff) ||
192 !IsSingleBit(static_cast<uint32_t>(aac_data.channelMode), 0x03) ||
193 !IsSingleBit(static_cast<uint32_t>(aac_data.bitsPerSample), 0x07)) {
194 LOG(WARNING) << __func__
195 << ": Invalid CodecSpecific=" << toString(codec_specific);
196 return false;
197 } else if ((aac_data.objectType & kDefaultOffloadAacCapability.objectType) &&
198 (aac_data.sampleRate & kDefaultOffloadAacCapability.sampleRate) &&
199 (aac_data.channelMode &
200 kDefaultOffloadAacCapability.channelMode) &&
201 (aac_data.variableBitRateEnabled == AacVariableBitRate::DISABLED ||
202 kDefaultOffloadAacCapability.variableBitRateEnabled ==
203 AacVariableBitRate::ENABLED) &&
204 (aac_data.bitsPerSample &
205 kDefaultOffloadAacCapability.bitsPerSample)) {
206 return true;
207 }
208 LOG(WARNING) << __func__
209 << ": Unsupported CodecSpecific=" << toString(codec_specific);
210 return false;
211 }
212
IsOffloadLdacConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)213 static bool IsOffloadLdacConfigurationValid(
214 const CodecConfiguration::CodecSpecific& codec_specific) {
215 if (codec_specific.getDiscriminator() !=
216 CodecConfiguration::CodecSpecific::hidl_discriminator::ldacConfig) {
217 LOG(WARNING) << __func__
218 << ": Invalid CodecSpecific=" << toString(codec_specific);
219 return false;
220 }
221 const LdacParameters ldac_data = codec_specific.ldacConfig();
222 if (!IsSingleBit(static_cast<uint32_t>(ldac_data.sampleRate), 0xff) ||
223 !IsSingleBit(static_cast<uint32_t>(ldac_data.channelMode), 0x07) ||
224 (ldac_data.qualityIndex > LdacQualityIndex::QUALITY_LOW &&
225 ldac_data.qualityIndex != LdacQualityIndex::QUALITY_ABR) ||
226 !IsSingleBit(static_cast<uint32_t>(ldac_data.bitsPerSample), 0x07)) {
227 LOG(WARNING) << __func__
228 << ": Invalid CodecSpecific=" << toString(codec_specific);
229 return false;
230 } else if ((ldac_data.sampleRate &
231 kDefaultOffloadLdacCapability.sampleRate) &&
232 (ldac_data.channelMode &
233 kDefaultOffloadLdacCapability.channelMode) &&
234 (ldac_data.bitsPerSample &
235 kDefaultOffloadLdacCapability.bitsPerSample)) {
236 return true;
237 }
238 LOG(WARNING) << __func__
239 << ": Unsupported CodecSpecific=" << toString(codec_specific);
240 return false;
241 }
242
IsOffloadAptxConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)243 static bool IsOffloadAptxConfigurationValid(
244 const CodecConfiguration::CodecSpecific& codec_specific) {
245 if (codec_specific.getDiscriminator() !=
246 CodecConfiguration::CodecSpecific::hidl_discriminator::aptxConfig) {
247 LOG(WARNING) << __func__
248 << ": Invalid CodecSpecific=" << toString(codec_specific);
249 return false;
250 }
251 const AptxParameters aptx_data = codec_specific.aptxConfig();
252 if (!IsSingleBit(static_cast<uint32_t>(aptx_data.sampleRate), 0xff) ||
253 !IsSingleBit(static_cast<uint32_t>(aptx_data.channelMode), 0x03) ||
254 !IsSingleBit(static_cast<uint32_t>(aptx_data.bitsPerSample), 0x07)) {
255 LOG(WARNING) << __func__
256 << ": Invalid CodecSpecific=" << toString(codec_specific);
257 return false;
258 } else if ((aptx_data.sampleRate &
259 kDefaultOffloadAptxCapability.sampleRate) &&
260 (aptx_data.channelMode &
261 kDefaultOffloadAptxCapability.channelMode) &&
262 (aptx_data.bitsPerSample &
263 kDefaultOffloadAptxCapability.bitsPerSample)) {
264 return true;
265 }
266 LOG(WARNING) << __func__
267 << ": Unsupported CodecSpecific=" << toString(codec_specific);
268 return false;
269 }
270
IsOffloadAptxHdConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)271 static bool IsOffloadAptxHdConfigurationValid(
272 const CodecConfiguration::CodecSpecific& codec_specific) {
273 if (codec_specific.getDiscriminator() !=
274 CodecConfiguration::CodecSpecific::hidl_discriminator::aptxConfig) {
275 LOG(WARNING) << __func__
276 << ": Invalid CodecSpecific=" << toString(codec_specific);
277 return false;
278 }
279 const AptxParameters aptx_data = codec_specific.aptxConfig();
280 if (!IsSingleBit(static_cast<uint32_t>(aptx_data.sampleRate), 0xff) ||
281 !IsSingleBit(static_cast<uint32_t>(aptx_data.channelMode), 0x03) ||
282 !IsSingleBit(static_cast<uint32_t>(aptx_data.bitsPerSample), 0x07)) {
283 LOG(WARNING) << __func__
284 << ": Invalid CodecSpecific=" << toString(codec_specific);
285 return false;
286 } else if ((aptx_data.sampleRate &
287 kDefaultOffloadAptxHdCapability.sampleRate) &&
288 (aptx_data.channelMode &
289 kDefaultOffloadAptxHdCapability.channelMode) &&
290 (aptx_data.bitsPerSample &
291 kDefaultOffloadAptxHdCapability.bitsPerSample)) {
292 return true;
293 }
294 LOG(WARNING) << __func__
295 << ": Unsupported CodecSpecific=" << toString(codec_specific);
296 return false;
297 }
298
GetSoftwarePcmCapabilities()299 std::vector<PcmParameters> GetSoftwarePcmCapabilities() {
300 return std::vector<PcmParameters>(1, kDefaultSoftwarePcmCapabilities);
301 }
302
GetOffloadCodecCapabilities(const SessionType & session_type)303 std::vector<CodecCapabilities> GetOffloadCodecCapabilities(
304 const SessionType& session_type) {
305 if (session_type != SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
306 return std::vector<CodecCapabilities>(0);
307 }
308 std::vector<CodecCapabilities> offload_a2dp_codec_capabilities =
309 kDefaultOffloadA2dpCodecCapabilities;
310 for (auto& codec_capability : offload_a2dp_codec_capabilities) {
311 switch (codec_capability.codecType) {
312 case CodecType::SBC:
313 codec_capability.capabilities.sbcCapabilities(
314 kDefaultOffloadSbcCapability);
315 break;
316 case CodecType::AAC:
317 codec_capability.capabilities.aacCapabilities(
318 kDefaultOffloadAacCapability);
319 break;
320 case CodecType::LDAC:
321 codec_capability.capabilities.ldacCapabilities(
322 kDefaultOffloadLdacCapability);
323 break;
324 case CodecType::APTX:
325 codec_capability.capabilities.aptxCapabilities(
326 kDefaultOffloadAptxCapability);
327 break;
328 case CodecType::APTX_HD:
329 codec_capability.capabilities.aptxCapabilities(
330 kDefaultOffloadAptxHdCapability);
331 break;
332 case CodecType::UNKNOWN:
333 codec_capability = {};
334 break;
335 }
336 }
337 return offload_a2dp_codec_capabilities;
338 }
339
IsSoftwarePcmConfigurationValid(const PcmParameters & pcm_config)340 bool IsSoftwarePcmConfigurationValid(const PcmParameters& pcm_config) {
341 if ((pcm_config.sampleRate != SampleRate::RATE_44100 &&
342 pcm_config.sampleRate != SampleRate::RATE_48000 &&
343 pcm_config.sampleRate != SampleRate::RATE_88200 &&
344 pcm_config.sampleRate != SampleRate::RATE_96000 &&
345 pcm_config.sampleRate != SampleRate::RATE_16000 &&
346 pcm_config.sampleRate != SampleRate::RATE_24000) ||
347 (pcm_config.bitsPerSample != BitsPerSample::BITS_16 &&
348 pcm_config.bitsPerSample != BitsPerSample::BITS_24 &&
349 pcm_config.bitsPerSample != BitsPerSample::BITS_32) ||
350 (pcm_config.channelMode != ChannelMode::MONO &&
351 pcm_config.channelMode != ChannelMode::STEREO)) {
352 LOG(WARNING) << __func__
353 << ": Invalid PCM Configuration=" << toString(pcm_config);
354 return false;
355 } else if (pcm_config.sampleRate &
356 kDefaultSoftwarePcmCapabilities.sampleRate &&
357 pcm_config.bitsPerSample &
358 kDefaultSoftwarePcmCapabilities.bitsPerSample &&
359 pcm_config.channelMode &
360 kDefaultSoftwarePcmCapabilities.channelMode) {
361 return true;
362 }
363 LOG(WARNING) << __func__
364 << ": Unsupported PCM Configuration=" << toString(pcm_config);
365 return false;
366 }
367
IsOffloadCodecConfigurationValid(const SessionType & session_type,const CodecConfiguration & codec_config)368 bool IsOffloadCodecConfigurationValid(const SessionType& session_type,
369 const CodecConfiguration& codec_config) {
370 if (session_type != SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
371 LOG(ERROR) << __func__
372 << ": Invalid SessionType=" << toString(session_type);
373 return false;
374 } else if (codec_config.encodedAudioBitrate < 0x00000001 ||
375 0x00ffffff < codec_config.encodedAudioBitrate) {
376 LOG(ERROR) << __func__ << ": Unsupported Codec Configuration="
377 << toString(codec_config);
378 return false;
379 }
380 const CodecConfiguration::CodecSpecific& codec_specific = codec_config.config;
381 switch (codec_config.codecType) {
382 case CodecType::SBC:
383 if (IsOffloadSbcConfigurationValid(codec_specific)) {
384 return true;
385 }
386 return false;
387 case CodecType::AAC:
388 if (IsOffloadAacConfigurationValid(codec_specific)) {
389 return true;
390 }
391 return false;
392 case CodecType::LDAC:
393 if (IsOffloadLdacConfigurationValid(codec_specific)) {
394 return true;
395 }
396 return false;
397 case CodecType::APTX:
398 if (IsOffloadAptxConfigurationValid(codec_specific)) {
399 return true;
400 }
401 return false;
402 case CodecType::APTX_HD:
403 if (IsOffloadAptxHdConfigurationValid(codec_specific)) {
404 return true;
405 }
406 return false;
407 case CodecType::UNKNOWN:
408 return false;
409 }
410 return false;
411 }
412
413 } // namespace audio
414 } // namespace bluetooth
415 } // namespace android
416