• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::DISABLED,
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     .bitsPerSample = BitsPerSample::BITS_16,
98     .channelMode = ChannelMode::STEREO};
99 
100 // aptX HD: mSampleRate:(44100|48000), mBitsPerSample:(24),
101 //          mChannelMode:(STEREO)
102 static const AptxParameters kDefaultOffloadAptxHdCapability = {
103     .sampleRate = static_cast<SampleRate>(SampleRate::RATE_44100 |
104                                           SampleRate::RATE_48000),
105     .bitsPerSample = BitsPerSample::BITS_24,
106     .channelMode = ChannelMode::STEREO};
107 
108 const std::vector<CodecCapabilities> kDefaultOffloadA2dpCodecCapabilities = {
109     {.codecType = CodecType::SBC, .capabilities = {}},
110     {.codecType = CodecType::AAC, .capabilities = {}},
111     {.codecType = CodecType::LDAC, .capabilities = {}},
112     {.codecType = CodecType::APTX, .capabilities = {}},
113     {.codecType = CodecType::APTX_HD, .capabilities = {}}};
114 
IsSingleBit(uint32_t bitmasks,uint32_t bitfield)115 static bool IsSingleBit(uint32_t bitmasks, uint32_t bitfield) {
116   bool single = false;
117   uint32_t test_bit = 0x00000001;
118   while (test_bit <= bitmasks && test_bit <= bitfield) {
119     if (bitfield & test_bit && bitmasks & test_bit) {
120       if (single) return false;
121       single = true;
122     }
123     if (test_bit == 0x80000000) break;
124     test_bit <<= 1;
125   }
126   return single;
127 }
128 
129 static bool IsOffloadSbcConfigurationValid(
130     const CodecConfiguration::CodecSpecific& codec_specific);
131 static bool IsOffloadAacConfigurationValid(
132     const CodecConfiguration::CodecSpecific& codec_specific);
133 static bool IsOffloadLdacConfigurationValid(
134     const CodecConfiguration::CodecSpecific& codec_specific);
135 static bool IsOffloadAptxConfigurationValid(
136     const CodecConfiguration::CodecSpecific& codec_specific);
137 static bool IsOffloadAptxHdConfigurationValid(
138     const CodecConfiguration::CodecSpecific& codec_specific);
139 
IsOffloadSbcConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)140 static bool IsOffloadSbcConfigurationValid(
141     const CodecConfiguration::CodecSpecific& codec_specific) {
142   if (codec_specific.getDiscriminator() !=
143       CodecConfiguration::CodecSpecific::hidl_discriminator::sbcConfig) {
144     LOG(WARNING) << __func__
145                  << ": Invalid CodecSpecific=" << toString(codec_specific);
146     return false;
147   }
148   const SbcParameters sbc_data = codec_specific.sbcConfig();
149   if (!IsSingleBit(static_cast<uint32_t>(sbc_data.sampleRate), 0xff) ||
150       !IsSingleBit(static_cast<uint32_t>(sbc_data.channelMode), 0x0f) ||
151       !IsSingleBit(static_cast<uint32_t>(sbc_data.blockLength), 0xf0) ||
152       !IsSingleBit(static_cast<uint32_t>(sbc_data.numSubbands), 0x0c) ||
153       !IsSingleBit(static_cast<uint32_t>(sbc_data.allocMethod), 0x03) ||
154       !IsSingleBit(static_cast<uint32_t>(sbc_data.bitsPerSample), 0x07) ||
155       sbc_data.minBitpool > sbc_data.maxBitpool) {
156     LOG(WARNING) << __func__
157                  << ": Invalid CodecSpecific=" << toString(codec_specific);
158     return false;
159   } else if ((sbc_data.sampleRate & kDefaultOffloadSbcCapability.sampleRate) &&
160              (sbc_data.channelMode &
161               kDefaultOffloadSbcCapability.channelMode) &&
162              (sbc_data.blockLength &
163               kDefaultOffloadSbcCapability.blockLength) &&
164              (sbc_data.numSubbands &
165               kDefaultOffloadSbcCapability.numSubbands) &&
166              (sbc_data.allocMethod &
167               kDefaultOffloadSbcCapability.allocMethod) &&
168              (sbc_data.bitsPerSample &
169               kDefaultOffloadSbcCapability.bitsPerSample) &&
170              (kDefaultOffloadSbcCapability.minBitpool <= sbc_data.minBitpool &&
171               sbc_data.maxBitpool <= kDefaultOffloadSbcCapability.maxBitpool)) {
172     return true;
173   }
174   LOG(WARNING) << __func__
175                << ": Unsupported CodecSpecific=" << toString(codec_specific);
176   return false;
177 }
178 
IsOffloadAacConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)179 static bool IsOffloadAacConfigurationValid(
180     const CodecConfiguration::CodecSpecific& codec_specific) {
181   if (codec_specific.getDiscriminator() !=
182       CodecConfiguration::CodecSpecific::hidl_discriminator::aacConfig) {
183     LOG(WARNING) << __func__
184                  << ": Invalid CodecSpecific=" << toString(codec_specific);
185     return false;
186   }
187   const AacParameters aac_data = codec_specific.aacConfig();
188   if (!IsSingleBit(static_cast<uint32_t>(aac_data.objectType), 0xf0) ||
189       !IsSingleBit(static_cast<uint32_t>(aac_data.sampleRate), 0xff) ||
190       !IsSingleBit(static_cast<uint32_t>(aac_data.channelMode), 0x03) ||
191       !IsSingleBit(static_cast<uint32_t>(aac_data.bitsPerSample), 0x07)) {
192     LOG(WARNING) << __func__
193                  << ": Invalid CodecSpecific=" << toString(codec_specific);
194     return false;
195   } else if ((aac_data.objectType & kDefaultOffloadAacCapability.objectType) &&
196              (aac_data.sampleRate & kDefaultOffloadAacCapability.sampleRate) &&
197              (aac_data.channelMode &
198               kDefaultOffloadAacCapability.channelMode) &&
199              (aac_data.variableBitRateEnabled == AacVariableBitRate::DISABLED ||
200               kDefaultOffloadAacCapability.variableBitRateEnabled ==
201                   AacVariableBitRate::ENABLED) &&
202              (aac_data.bitsPerSample &
203               kDefaultOffloadAacCapability.bitsPerSample)) {
204     return true;
205   }
206   LOG(WARNING) << __func__
207                << ": Unsupported CodecSpecific=" << toString(codec_specific);
208   return false;
209 }
210 
IsOffloadLdacConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)211 static bool IsOffloadLdacConfigurationValid(
212     const CodecConfiguration::CodecSpecific& codec_specific) {
213   if (codec_specific.getDiscriminator() !=
214       CodecConfiguration::CodecSpecific::hidl_discriminator::ldacConfig) {
215     LOG(WARNING) << __func__
216                  << ": Invalid CodecSpecific=" << toString(codec_specific);
217     return false;
218   }
219   const LdacParameters ldac_data = codec_specific.ldacConfig();
220   if (!IsSingleBit(static_cast<uint32_t>(ldac_data.sampleRate), 0xff) ||
221       !IsSingleBit(static_cast<uint32_t>(ldac_data.channelMode), 0x07) ||
222       (ldac_data.qualityIndex > LdacQualityIndex::QUALITY_LOW &&
223        ldac_data.qualityIndex != LdacQualityIndex::QUALITY_ABR) ||
224       !IsSingleBit(static_cast<uint32_t>(ldac_data.bitsPerSample), 0x07)) {
225     LOG(WARNING) << __func__
226                  << ": Invalid CodecSpecific=" << toString(codec_specific);
227     return false;
228   } else if ((ldac_data.sampleRate &
229               kDefaultOffloadLdacCapability.sampleRate) &&
230              (ldac_data.channelMode &
231               kDefaultOffloadLdacCapability.channelMode) &&
232              (ldac_data.bitsPerSample &
233               kDefaultOffloadLdacCapability.bitsPerSample)) {
234     return true;
235   }
236   LOG(WARNING) << __func__
237                << ": Unsupported CodecSpecific=" << toString(codec_specific);
238   return false;
239 }
240 
IsOffloadAptxConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)241 static bool IsOffloadAptxConfigurationValid(
242     const CodecConfiguration::CodecSpecific& codec_specific) {
243   if (codec_specific.getDiscriminator() !=
244       CodecConfiguration::CodecSpecific::hidl_discriminator::aptxConfig) {
245     LOG(WARNING) << __func__
246                  << ": Invalid CodecSpecific=" << toString(codec_specific);
247     return false;
248   }
249   const AptxParameters aptx_data = codec_specific.aptxConfig();
250   if (!IsSingleBit(static_cast<uint32_t>(aptx_data.sampleRate), 0xff) ||
251       !IsSingleBit(static_cast<uint32_t>(aptx_data.channelMode), 0x03) ||
252       !IsSingleBit(static_cast<uint32_t>(aptx_data.bitsPerSample), 0x07)) {
253     LOG(WARNING) << __func__
254                  << ": Invalid CodecSpecific=" << toString(codec_specific);
255     return false;
256   } else if ((aptx_data.sampleRate &
257               kDefaultOffloadAptxCapability.sampleRate) &&
258              (aptx_data.channelMode &
259               kDefaultOffloadAptxCapability.channelMode) &&
260              (aptx_data.bitsPerSample &
261               kDefaultOffloadAptxCapability.bitsPerSample)) {
262     return true;
263   }
264   LOG(WARNING) << __func__
265                << ": Unsupported CodecSpecific=" << toString(codec_specific);
266   return false;
267 }
268 
IsOffloadAptxHdConfigurationValid(const CodecConfiguration::CodecSpecific & codec_specific)269 static bool IsOffloadAptxHdConfigurationValid(
270     const CodecConfiguration::CodecSpecific& codec_specific) {
271   if (codec_specific.getDiscriminator() !=
272       CodecConfiguration::CodecSpecific::hidl_discriminator::aptxConfig) {
273     LOG(WARNING) << __func__
274                  << ": Invalid CodecSpecific=" << toString(codec_specific);
275     return false;
276   }
277   const AptxParameters aptx_data = codec_specific.aptxConfig();
278   if (!IsSingleBit(static_cast<uint32_t>(aptx_data.sampleRate), 0xff) ||
279       !IsSingleBit(static_cast<uint32_t>(aptx_data.channelMode), 0x03) ||
280       !IsSingleBit(static_cast<uint32_t>(aptx_data.bitsPerSample), 0x07)) {
281     LOG(WARNING) << __func__
282                  << ": Invalid CodecSpecific=" << toString(codec_specific);
283     return false;
284   } else if ((aptx_data.sampleRate &
285               kDefaultOffloadAptxHdCapability.sampleRate) &&
286              (aptx_data.channelMode &
287               kDefaultOffloadAptxHdCapability.channelMode) &&
288              (aptx_data.bitsPerSample &
289               kDefaultOffloadAptxHdCapability.bitsPerSample)) {
290     return true;
291   }
292   LOG(WARNING) << __func__
293                << ": Unsupported CodecSpecific=" << toString(codec_specific);
294   return false;
295 }
296 
GetSoftwarePcmCapabilities()297 std::vector<PcmParameters> GetSoftwarePcmCapabilities() {
298   return std::vector<PcmParameters>(1, kDefaultSoftwarePcmCapabilities);
299 }
300 
GetOffloadCodecCapabilities(const SessionType & session_type)301 std::vector<CodecCapabilities> GetOffloadCodecCapabilities(
302     const SessionType& session_type) {
303   if (session_type != SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
304     return std::vector<CodecCapabilities>(0);
305   }
306   std::vector<CodecCapabilities> offload_a2dp_codec_capabilities =
307       kDefaultOffloadA2dpCodecCapabilities;
308   for (auto& codec_capability : offload_a2dp_codec_capabilities) {
309     switch (codec_capability.codecType) {
310       case CodecType::SBC:
311         codec_capability.capabilities.sbcCapabilities(
312             kDefaultOffloadSbcCapability);
313         break;
314       case CodecType::AAC:
315         codec_capability.capabilities.aacCapabilities(
316             kDefaultOffloadAacCapability);
317         break;
318       case CodecType::LDAC:
319         codec_capability.capabilities.ldacCapabilities(
320             kDefaultOffloadLdacCapability);
321         break;
322       case CodecType::APTX:
323         codec_capability.capabilities.aptxCapabilities(
324             kDefaultOffloadAptxCapability);
325         break;
326       case CodecType::APTX_HD:
327         codec_capability.capabilities.aptxCapabilities(
328             kDefaultOffloadAptxHdCapability);
329         break;
330       case CodecType::UNKNOWN:
331         codec_capability = {};
332         break;
333     }
334   }
335   return offload_a2dp_codec_capabilities;
336 }
337 
IsSoftwarePcmConfigurationValid(const PcmParameters & pcm_config)338 bool IsSoftwarePcmConfigurationValid(const PcmParameters& pcm_config) {
339   if ((pcm_config.sampleRate != SampleRate::RATE_44100 &&
340        pcm_config.sampleRate != SampleRate::RATE_48000 &&
341        pcm_config.sampleRate != SampleRate::RATE_88200 &&
342        pcm_config.sampleRate != SampleRate::RATE_96000 &&
343        pcm_config.sampleRate != SampleRate::RATE_16000 &&
344        pcm_config.sampleRate != SampleRate::RATE_24000) ||
345       (pcm_config.bitsPerSample != BitsPerSample::BITS_16 &&
346        pcm_config.bitsPerSample != BitsPerSample::BITS_24 &&
347        pcm_config.bitsPerSample != BitsPerSample::BITS_32) ||
348       (pcm_config.channelMode != ChannelMode::MONO &&
349        pcm_config.channelMode != ChannelMode::STEREO)) {
350     LOG(WARNING) << __func__
351                  << ": Invalid PCM Configuration=" << toString(pcm_config);
352     return false;
353   } else if (pcm_config.sampleRate &
354                  kDefaultSoftwarePcmCapabilities.sampleRate &&
355              pcm_config.bitsPerSample &
356                  kDefaultSoftwarePcmCapabilities.bitsPerSample &&
357              pcm_config.channelMode &
358                  kDefaultSoftwarePcmCapabilities.channelMode) {
359     return true;
360   }
361   LOG(WARNING) << __func__
362                << ": Unsupported PCM Configuration=" << toString(pcm_config);
363   return false;
364 }
365 
IsOffloadCodecConfigurationValid(const SessionType & session_type,const CodecConfiguration & codec_config)366 bool IsOffloadCodecConfigurationValid(const SessionType& session_type,
367                                       const CodecConfiguration& codec_config) {
368   if (session_type != SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH) {
369     LOG(ERROR) << __func__
370                << ": Invalid SessionType=" << toString(session_type);
371     return false;
372   } else if (codec_config.encodedAudioBitrate < 0x00000001 ||
373              0x00ffffff < codec_config.encodedAudioBitrate) {
374     LOG(ERROR) << __func__ << ": Unsupported Codec Configuration="
375                << toString(codec_config);
376     return false;
377   }
378   const CodecConfiguration::CodecSpecific& codec_specific = codec_config.config;
379   switch (codec_config.codecType) {
380     case CodecType::SBC:
381       if (IsOffloadSbcConfigurationValid(codec_specific)) {
382         return true;
383       }
384       return false;
385     case CodecType::AAC:
386       if (IsOffloadAacConfigurationValid(codec_specific)) {
387         return true;
388       }
389       return false;
390     case CodecType::LDAC:
391       if (IsOffloadLdacConfigurationValid(codec_specific)) {
392         return true;
393       }
394       return false;
395     case CodecType::APTX:
396       if (IsOffloadAptxConfigurationValid(codec_specific)) {
397         return true;
398       }
399       return false;
400     case CodecType::APTX_HD:
401       if (IsOffloadAptxHdConfigurationValid(codec_specific)) {
402         return true;
403       }
404       return false;
405     case CodecType::UNKNOWN:
406       return false;
407   }
408   return false;
409 }
410 
411 }  // namespace audio
412 }  // namespace bluetooth
413 }  // namespace android
414