1 /*
2 * Copyright (C) 2022 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 "BtAudioNakahara"
18
19 #include <aidl/android/hardware/bluetooth/audio/AudioConfiguration.h>
20 #include <aidl/android/hardware/bluetooth/audio/BluetoothAudioStatus.h>
21 #include <aidl/android/hardware/bluetooth/audio/SessionType.h>
22 #include <android-base/logging.h>
23
24 #include <functional>
25 #include <unordered_map>
26
27 #include "../aidl_session/BluetoothAudioSession.h"
28 #include "../aidl_session/BluetoothAudioSessionControl.h"
29 #include "HidlToAidlMiddleware_2_0.h"
30 #include "HidlToAidlMiddleware_2_1.h"
31
32 namespace aidl {
33 namespace android {
34 namespace hardware {
35 namespace bluetooth {
36 namespace audio {
37
38 using HidlStatus = ::android::hardware::bluetooth::audio::V2_0::Status;
39 using PcmConfig_2_0 =
40 ::android::hardware::bluetooth::audio::V2_0::PcmParameters;
41 using SampleRate_2_0 = ::android::hardware::bluetooth::audio::V2_0::SampleRate;
42 using ChannelMode_2_0 =
43 ::android::hardware::bluetooth::audio::V2_0::ChannelMode;
44 using BitsPerSample_2_0 =
45 ::android::hardware::bluetooth::audio::V2_0::BitsPerSample;
46 using CodecConfig_2_0 =
47 ::android::hardware::bluetooth::audio::V2_0::CodecConfiguration;
48 using CodecType_2_0 = ::android::hardware::bluetooth::audio::V2_0::CodecType;
49 using SbcConfig_2_0 =
50 ::android::hardware::bluetooth::audio::V2_0::SbcParameters;
51 using AacConfig_2_0 =
52 ::android::hardware::bluetooth::audio::V2_0::AacParameters;
53 using LdacConfig_2_0 =
54 ::android::hardware::bluetooth::audio::V2_0::LdacParameters;
55 using AptxConfig_2_0 =
56 ::android::hardware::bluetooth::audio::V2_0::AptxParameters;
57 using SbcAllocMethod_2_0 =
58 ::android::hardware::bluetooth::audio::V2_0::SbcAllocMethod;
59 using SbcBlockLength_2_0 =
60 ::android::hardware::bluetooth::audio::V2_0::SbcBlockLength;
61 using SbcChannelMode_2_0 =
62 ::android::hardware::bluetooth::audio::V2_0::SbcChannelMode;
63 using SbcNumSubbands_2_0 =
64 ::android::hardware::bluetooth::audio::V2_0::SbcNumSubbands;
65 using AacObjectType_2_0 =
66 ::android::hardware::bluetooth::audio::V2_0::AacObjectType;
67 using AacVarBitRate_2_0 =
68 ::android::hardware::bluetooth::audio::V2_0::AacVariableBitRate;
69 using LdacChannelMode_2_0 =
70 ::android::hardware::bluetooth::audio::V2_0::LdacChannelMode;
71 using LdacQualityIndex_2_0 =
72 ::android::hardware::bluetooth::audio::V2_0::LdacQualityIndex;
73
74 using PcmConfig_2_1 =
75 ::android::hardware::bluetooth::audio::V2_1::PcmParameters;
76 using SampleRate_2_1 = ::android::hardware::bluetooth::audio::V2_1::SampleRate;
77 using Lc3CodecConfig_2_1 =
78 ::android::hardware::bluetooth::audio::V2_1::Lc3CodecConfiguration;
79 using Lc3Config_2_1 =
80 ::android::hardware::bluetooth::audio::V2_1::Lc3Parameters;
81 using Lc3FrameDuration_2_1 =
82 ::android::hardware::bluetooth::audio::V2_1::Lc3FrameDuration;
83
84 std::mutex legacy_callback_lock;
85 std::unordered_map<
86 SessionType,
87 std::unordered_map<uint16_t, std::shared_ptr<PortStatusCallbacks_2_0>>>
88 legacy_callback_table;
89
90 const static std::unordered_map<SessionType_2_1, SessionType>
91 session_type_2_1_to_aidl_map{
92 {SessionType_2_1::A2DP_SOFTWARE_ENCODING_DATAPATH,
93 SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH},
94 {SessionType_2_1::A2DP_HARDWARE_OFFLOAD_DATAPATH,
95 SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH},
96 {SessionType_2_1::HEARING_AID_SOFTWARE_ENCODING_DATAPATH,
97 SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH},
98 {SessionType_2_1::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH,
99 SessionType::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH},
100 {SessionType_2_1::LE_AUDIO_SOFTWARE_DECODED_DATAPATH,
101 SessionType::LE_AUDIO_SOFTWARE_DECODING_DATAPATH},
102 {SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH,
103 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH},
104 {SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH,
105 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH},
106 };
107
108 const static std::unordered_map<int32_t, SampleRate_2_1>
109 sample_rate_to_hidl_2_1_map{
110 {44100, SampleRate_2_1::RATE_44100},
111 {48000, SampleRate_2_1::RATE_48000},
112 {88200, SampleRate_2_1::RATE_88200},
113 {96000, SampleRate_2_1::RATE_96000},
114 {176400, SampleRate_2_1::RATE_176400},
115 {192000, SampleRate_2_1::RATE_192000},
116 {16000, SampleRate_2_1::RATE_16000},
117 {24000, SampleRate_2_1::RATE_24000},
118 {8000, SampleRate_2_1::RATE_8000},
119 {32000, SampleRate_2_1::RATE_32000},
120 };
121
122 const static std::unordered_map<CodecType, CodecType_2_0>
123 codec_type_to_hidl_2_0_map{
124 {CodecType::UNKNOWN, CodecType_2_0::UNKNOWN},
125 {CodecType::SBC, CodecType_2_0::SBC},
126 {CodecType::AAC, CodecType_2_0::AAC},
127 {CodecType::APTX, CodecType_2_0::APTX},
128 {CodecType::APTX_HD, CodecType_2_0::APTX_HD},
129 {CodecType::LDAC, CodecType_2_0::LDAC},
130 {CodecType::LC3, CodecType_2_0::UNKNOWN},
131 };
132
133 const static std::unordered_map<SbcChannelMode, SbcChannelMode_2_0>
134 sbc_channel_mode_to_hidl_2_0_map{
135 {SbcChannelMode::UNKNOWN, SbcChannelMode_2_0::UNKNOWN},
136 {SbcChannelMode::JOINT_STEREO, SbcChannelMode_2_0::JOINT_STEREO},
137 {SbcChannelMode::STEREO, SbcChannelMode_2_0::STEREO},
138 {SbcChannelMode::DUAL, SbcChannelMode_2_0::DUAL},
139 {SbcChannelMode::MONO, SbcChannelMode_2_0::MONO},
140 };
141
142 const static std::unordered_map<int8_t, SbcBlockLength_2_0>
143 sbc_block_length_to_hidl_map{
144 {4, SbcBlockLength_2_0::BLOCKS_4},
145 {8, SbcBlockLength_2_0::BLOCKS_8},
146 {12, SbcBlockLength_2_0::BLOCKS_12},
147 {16, SbcBlockLength_2_0::BLOCKS_16},
148 };
149
150 const static std::unordered_map<int8_t, SbcNumSubbands_2_0>
151 sbc_subbands_to_hidl_map{
152 {4, SbcNumSubbands_2_0::SUBBAND_4},
153 {8, SbcNumSubbands_2_0::SUBBAND_8},
154 };
155
156 const static std::unordered_map<SbcAllocMethod, SbcAllocMethod_2_0>
157 sbc_alloc_method_to_hidl_map{
158 {SbcAllocMethod::ALLOC_MD_S, SbcAllocMethod_2_0::ALLOC_MD_S},
159 {SbcAllocMethod::ALLOC_MD_L, SbcAllocMethod_2_0::ALLOC_MD_L},
160 };
161
162 const static std::unordered_map<AacObjectType, AacObjectType_2_0>
163 aac_object_type_to_hidl_map{
164 {AacObjectType::MPEG2_LC, AacObjectType_2_0::MPEG2_LC},
165 {AacObjectType::MPEG4_LC, AacObjectType_2_0::MPEG4_LC},
166 {AacObjectType::MPEG4_LTP, AacObjectType_2_0::MPEG4_LTP},
167 {AacObjectType::MPEG4_SCALABLE, AacObjectType_2_0::MPEG4_SCALABLE},
168 };
169
170 const static std::unordered_map<LdacChannelMode, LdacChannelMode_2_0>
171 ldac_channel_mode_to_hidl_map{
172 {LdacChannelMode::UNKNOWN, LdacChannelMode_2_0::UNKNOWN},
173 {LdacChannelMode::STEREO, LdacChannelMode_2_0::STEREO},
174 {LdacChannelMode::DUAL, LdacChannelMode_2_0::DUAL},
175 {LdacChannelMode::MONO, LdacChannelMode_2_0::MONO},
176 };
177
178 const static std::unordered_map<LdacQualityIndex, LdacQualityIndex_2_0>
179 ldac_qindex_to_hidl_map{
180 {LdacQualityIndex::HIGH, LdacQualityIndex_2_0::QUALITY_HIGH},
181 {LdacQualityIndex::MID, LdacQualityIndex_2_0::QUALITY_MID},
182 {LdacQualityIndex::LOW, LdacQualityIndex_2_0::QUALITY_LOW},
183 {LdacQualityIndex::ABR, LdacQualityIndex_2_0::QUALITY_ABR},
184 };
185
from_session_type_2_1(const SessionType_2_1 & session_type_hidl)186 inline SessionType from_session_type_2_1(
187 const SessionType_2_1& session_type_hidl) {
188 auto it = session_type_2_1_to_aidl_map.find(session_type_hidl);
189 if (it != session_type_2_1_to_aidl_map.end()) return it->second;
190 return SessionType::UNKNOWN;
191 }
192
from_session_type_2_0(const SessionType_2_0 & session_type_hidl)193 inline SessionType from_session_type_2_0(
194 const SessionType_2_0& session_type_hidl) {
195 return from_session_type_2_1(static_cast<SessionType_2_1>(session_type_hidl));
196 }
197
to_hidl_status(const BluetoothAudioStatus & status)198 inline HidlStatus to_hidl_status(const BluetoothAudioStatus& status) {
199 switch (status) {
200 case BluetoothAudioStatus::SUCCESS:
201 return HidlStatus::SUCCESS;
202 case BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION:
203 return HidlStatus::UNSUPPORTED_CODEC_CONFIGURATION;
204 default:
205 return HidlStatus::FAILURE;
206 }
207 }
208
to_hidl_sample_rate_2_1(const int32_t sample_rate_hz)209 inline SampleRate_2_1 to_hidl_sample_rate_2_1(const int32_t sample_rate_hz) {
210 auto it = sample_rate_to_hidl_2_1_map.find(sample_rate_hz);
211 if (it != sample_rate_to_hidl_2_1_map.end()) return it->second;
212 return SampleRate_2_1::RATE_UNKNOWN;
213 }
214
to_hidl_sample_rate_2_0(const int32_t sample_rate_hz)215 inline SampleRate_2_0 to_hidl_sample_rate_2_0(const int32_t sample_rate_hz) {
216 auto it = sample_rate_to_hidl_2_1_map.find(sample_rate_hz);
217 if (it != sample_rate_to_hidl_2_1_map.end())
218 return static_cast<SampleRate_2_0>(it->second);
219 return SampleRate_2_0::RATE_UNKNOWN;
220 }
221
to_hidl_bits_per_sample(const int8_t bit_per_sample)222 inline BitsPerSample_2_0 to_hidl_bits_per_sample(const int8_t bit_per_sample) {
223 switch (bit_per_sample) {
224 case 16:
225 return BitsPerSample_2_0::BITS_16;
226 case 24:
227 return BitsPerSample_2_0::BITS_24;
228 case 32:
229 return BitsPerSample_2_0::BITS_32;
230 default:
231 return BitsPerSample_2_0::BITS_UNKNOWN;
232 }
233 }
234
to_hidl_channel_mode(const ChannelMode channel_mode)235 inline ChannelMode_2_0 to_hidl_channel_mode(const ChannelMode channel_mode) {
236 switch (channel_mode) {
237 case ChannelMode::MONO:
238 return ChannelMode_2_0::MONO;
239 case ChannelMode::STEREO:
240 return ChannelMode_2_0::STEREO;
241 default:
242 return ChannelMode_2_0::UNKNOWN;
243 }
244 }
245
to_hidl_pcm_config_2_0(const PcmConfiguration & pcm_config)246 inline PcmConfig_2_0 to_hidl_pcm_config_2_0(
247 const PcmConfiguration& pcm_config) {
248 PcmConfig_2_0 hidl_pcm_config;
249 hidl_pcm_config.sampleRate = to_hidl_sample_rate_2_0(pcm_config.sampleRateHz);
250 hidl_pcm_config.channelMode = to_hidl_channel_mode(pcm_config.channelMode);
251 hidl_pcm_config.bitsPerSample =
252 to_hidl_bits_per_sample(pcm_config.bitsPerSample);
253 return hidl_pcm_config;
254 }
255
to_hidl_codec_type_2_0(const CodecType codec_type)256 inline CodecType_2_0 to_hidl_codec_type_2_0(const CodecType codec_type) {
257 auto it = codec_type_to_hidl_2_0_map.find(codec_type);
258 if (it != codec_type_to_hidl_2_0_map.end()) return it->second;
259 return CodecType_2_0::UNKNOWN;
260 }
261
to_hidl_sbc_config(const SbcConfiguration sbc_config)262 inline SbcConfig_2_0 to_hidl_sbc_config(const SbcConfiguration sbc_config) {
263 SbcConfig_2_0 hidl_sbc_config;
264 hidl_sbc_config.minBitpool = sbc_config.minBitpool;
265 hidl_sbc_config.maxBitpool = sbc_config.maxBitpool;
266 hidl_sbc_config.sampleRate = to_hidl_sample_rate_2_0(sbc_config.sampleRateHz);
267 hidl_sbc_config.bitsPerSample =
268 to_hidl_bits_per_sample(sbc_config.bitsPerSample);
269 if (sbc_channel_mode_to_hidl_2_0_map.find(sbc_config.channelMode) !=
270 sbc_channel_mode_to_hidl_2_0_map.end()) {
271 hidl_sbc_config.channelMode =
272 sbc_channel_mode_to_hidl_2_0_map.at(sbc_config.channelMode);
273 }
274 if (sbc_block_length_to_hidl_map.find(sbc_config.blockLength) !=
275 sbc_block_length_to_hidl_map.end()) {
276 hidl_sbc_config.blockLength =
277 sbc_block_length_to_hidl_map.at(sbc_config.blockLength);
278 }
279 if (sbc_subbands_to_hidl_map.find(sbc_config.numSubbands) !=
280 sbc_subbands_to_hidl_map.end()) {
281 hidl_sbc_config.numSubbands =
282 sbc_subbands_to_hidl_map.at(sbc_config.numSubbands);
283 }
284 if (sbc_alloc_method_to_hidl_map.find(sbc_config.allocMethod) !=
285 sbc_alloc_method_to_hidl_map.end()) {
286 hidl_sbc_config.allocMethod =
287 sbc_alloc_method_to_hidl_map.at(sbc_config.allocMethod);
288 }
289 return hidl_sbc_config;
290 }
291
to_hidl_aac_config(const AacConfiguration aac_config)292 inline AacConfig_2_0 to_hidl_aac_config(const AacConfiguration aac_config) {
293 AacConfig_2_0 hidl_aac_config;
294 hidl_aac_config.sampleRate = to_hidl_sample_rate_2_0(aac_config.sampleRateHz);
295 hidl_aac_config.bitsPerSample =
296 to_hidl_bits_per_sample(aac_config.bitsPerSample);
297 hidl_aac_config.channelMode = to_hidl_channel_mode(aac_config.channelMode);
298 if (aac_object_type_to_hidl_map.find(aac_config.objectType) !=
299 aac_object_type_to_hidl_map.end()) {
300 hidl_aac_config.objectType =
301 aac_object_type_to_hidl_map.at(aac_config.objectType);
302 }
303 hidl_aac_config.variableBitRateEnabled = aac_config.variableBitRateEnabled
304 ? AacVarBitRate_2_0::ENABLED
305 : AacVarBitRate_2_0::DISABLED;
306 return hidl_aac_config;
307 }
308
to_hidl_ldac_config(const LdacConfiguration ldac_config)309 inline LdacConfig_2_0 to_hidl_ldac_config(const LdacConfiguration ldac_config) {
310 LdacConfig_2_0 hidl_ldac_config;
311 hidl_ldac_config.sampleRate =
312 to_hidl_sample_rate_2_0(ldac_config.sampleRateHz);
313 hidl_ldac_config.bitsPerSample =
314 to_hidl_bits_per_sample(ldac_config.bitsPerSample);
315 if (ldac_channel_mode_to_hidl_map.find(ldac_config.channelMode) !=
316 ldac_channel_mode_to_hidl_map.end()) {
317 hidl_ldac_config.channelMode =
318 ldac_channel_mode_to_hidl_map.at(ldac_config.channelMode);
319 }
320 if (ldac_qindex_to_hidl_map.find(ldac_config.qualityIndex) !=
321 ldac_qindex_to_hidl_map.end()) {
322 hidl_ldac_config.qualityIndex =
323 ldac_qindex_to_hidl_map.at(ldac_config.qualityIndex);
324 }
325 return hidl_ldac_config;
326 }
327
to_hidl_aptx_config(const AptxConfiguration aptx_config)328 inline AptxConfig_2_0 to_hidl_aptx_config(const AptxConfiguration aptx_config) {
329 AptxConfig_2_0 hidl_aptx_config;
330 hidl_aptx_config.sampleRate =
331 to_hidl_sample_rate_2_0(aptx_config.sampleRateHz);
332 hidl_aptx_config.bitsPerSample =
333 to_hidl_bits_per_sample(aptx_config.bitsPerSample);
334 hidl_aptx_config.channelMode = to_hidl_channel_mode(aptx_config.channelMode);
335 return hidl_aptx_config;
336 }
337
to_hidl_codec_config_2_0(const CodecConfiguration & codec_config)338 inline CodecConfig_2_0 to_hidl_codec_config_2_0(
339 const CodecConfiguration& codec_config) {
340 CodecConfig_2_0 hidl_codec_config;
341 hidl_codec_config.codecType = to_hidl_codec_type_2_0(codec_config.codecType);
342 hidl_codec_config.encodedAudioBitrate =
343 static_cast<uint32_t>(codec_config.encodedAudioBitrate);
344 hidl_codec_config.peerMtu = static_cast<uint32_t>(codec_config.peerMtu);
345 hidl_codec_config.isScmstEnabled = codec_config.isScmstEnabled;
346 switch (codec_config.config.getTag()) {
347 case CodecConfiguration::CodecSpecific::sbcConfig:
348 hidl_codec_config.config.sbcConfig(to_hidl_sbc_config(
349 codec_config.config
350 .get<CodecConfiguration::CodecSpecific::sbcConfig>()));
351 break;
352 case CodecConfiguration::CodecSpecific::aacConfig:
353 hidl_codec_config.config.aacConfig(to_hidl_aac_config(
354 codec_config.config
355 .get<CodecConfiguration::CodecSpecific::aacConfig>()));
356 break;
357 case CodecConfiguration::CodecSpecific::ldacConfig:
358 hidl_codec_config.config.ldacConfig(to_hidl_ldac_config(
359 codec_config.config
360 .get<CodecConfiguration::CodecSpecific::ldacConfig>()));
361 break;
362 case CodecConfiguration::CodecSpecific::aptxConfig:
363 hidl_codec_config.config.aptxConfig(to_hidl_aptx_config(
364 codec_config.config
365 .get<CodecConfiguration::CodecSpecific::aptxConfig>()));
366 break;
367 default:
368 break;
369 }
370 return hidl_codec_config;
371 }
372
to_hidl_audio_config_2_0(const AudioConfiguration & audio_config)373 inline AudioConfig_2_0 to_hidl_audio_config_2_0(
374 const AudioConfiguration& audio_config) {
375 AudioConfig_2_0 hidl_audio_config;
376 if (audio_config.getTag() == AudioConfiguration::pcmConfig) {
377 hidl_audio_config.pcmConfig(to_hidl_pcm_config_2_0(
378 audio_config.get<AudioConfiguration::pcmConfig>()));
379 } else if (audio_config.getTag() == AudioConfiguration::a2dpConfig) {
380 hidl_audio_config.codecConfig(to_hidl_codec_config_2_0(
381 audio_config.get<AudioConfiguration::a2dpConfig>()));
382 }
383 return hidl_audio_config;
384 }
385
to_hidl_pcm_config_2_1(const PcmConfiguration & pcm_config)386 inline PcmConfig_2_1 to_hidl_pcm_config_2_1(
387 const PcmConfiguration& pcm_config) {
388 PcmConfig_2_1 hidl_pcm_config;
389 hidl_pcm_config.sampleRate = to_hidl_sample_rate_2_1(pcm_config.sampleRateHz);
390 hidl_pcm_config.channelMode = to_hidl_channel_mode(pcm_config.channelMode);
391 hidl_pcm_config.bitsPerSample =
392 to_hidl_bits_per_sample(pcm_config.bitsPerSample);
393 hidl_pcm_config.dataIntervalUs =
394 static_cast<uint32_t>(pcm_config.dataIntervalUs);
395 return hidl_pcm_config;
396 }
397
to_hidl_lc3_config_2_1(const Lc3Configuration & lc3_config)398 inline Lc3Config_2_1 to_hidl_lc3_config_2_1(
399 const Lc3Configuration& lc3_config) {
400 Lc3Config_2_1 hidl_lc3_config;
401 hidl_lc3_config.pcmBitDepth = to_hidl_bits_per_sample(lc3_config.pcmBitDepth);
402 hidl_lc3_config.samplingFrequency =
403 to_hidl_sample_rate_2_1(lc3_config.samplingFrequencyHz);
404 if (lc3_config.samplingFrequencyHz == 10000)
405 hidl_lc3_config.frameDuration = Lc3FrameDuration_2_1::DURATION_10000US;
406 else if (lc3_config.samplingFrequencyHz == 7500)
407 hidl_lc3_config.frameDuration = Lc3FrameDuration_2_1::DURATION_7500US;
408 hidl_lc3_config.octetsPerFrame =
409 static_cast<uint32_t>(lc3_config.octetsPerFrame);
410 hidl_lc3_config.blocksPerSdu = static_cast<uint32_t>(lc3_config.blocksPerSdu);
411 return hidl_lc3_config;
412 }
413
to_hidl_leaudio_config_2_1(const LeAudioConfiguration & unicast_config)414 inline Lc3CodecConfig_2_1 to_hidl_leaudio_config_2_1(
415 const LeAudioConfiguration& unicast_config) {
416 Lc3CodecConfig_2_1 hidl_lc3_codec_config = {
417 .audioChannelAllocation = 0,
418 };
419 if (unicast_config.leAudioCodecConfig.getTag() ==
420 LeAudioCodecConfiguration::lc3Config) {
421 LOG(FATAL) << __func__ << ": unexpected codec type(vendor?)";
422 }
423 auto& le_codec_config = unicast_config.leAudioCodecConfig
424 .get<LeAudioCodecConfiguration::lc3Config>();
425
426 hidl_lc3_codec_config.lc3Config = to_hidl_lc3_config_2_1(le_codec_config);
427
428 for (const auto& map : unicast_config.streamMap) {
429 hidl_lc3_codec_config.audioChannelAllocation |= map.audioChannelAllocation;
430 }
431 return hidl_lc3_codec_config;
432 }
433
to_hidl_leaudio_broadcast_config_2_1(const LeAudioBroadcastConfiguration & broadcast_config)434 inline Lc3CodecConfig_2_1 to_hidl_leaudio_broadcast_config_2_1(
435 const LeAudioBroadcastConfiguration& broadcast_config) {
436 Lc3CodecConfig_2_1 hidl_lc3_codec_config = {
437 .audioChannelAllocation = 0,
438 };
439 // NOTE: Broadcast is not officially supported in HIDL
440 if (broadcast_config.streamMap.empty()) {
441 return hidl_lc3_codec_config;
442 }
443 if (broadcast_config.streamMap[0].leAudioCodecConfig.getTag() !=
444 LeAudioCodecConfiguration::lc3Config) {
445 LOG(FATAL) << __func__ << ": unexpected codec type(vendor?)";
446 }
447 auto& le_codec_config =
448 broadcast_config.streamMap[0]
449 .leAudioCodecConfig.get<LeAudioCodecConfiguration::lc3Config>();
450 hidl_lc3_codec_config.lc3Config = to_hidl_lc3_config_2_1(le_codec_config);
451
452 for (const auto& map : broadcast_config.streamMap) {
453 hidl_lc3_codec_config.audioChannelAllocation |= map.audioChannelAllocation;
454 }
455 return hidl_lc3_codec_config;
456 }
457
to_hidl_audio_config_2_1(const AudioConfiguration & audio_config)458 inline AudioConfig_2_1 to_hidl_audio_config_2_1(
459 const AudioConfiguration& audio_config) {
460 AudioConfig_2_1 hidl_audio_config;
461 switch (audio_config.getTag()) {
462 case AudioConfiguration::pcmConfig:
463 hidl_audio_config.pcmConfig(to_hidl_pcm_config_2_1(
464 audio_config.get<AudioConfiguration::pcmConfig>()));
465 break;
466 case AudioConfiguration::a2dpConfig:
467 hidl_audio_config.codecConfig(to_hidl_codec_config_2_0(
468 audio_config.get<AudioConfiguration::a2dpConfig>()));
469 break;
470 case AudioConfiguration::a2dp:
471 break;
472 case AudioConfiguration::leAudioConfig:
473 hidl_audio_config.leAudioCodecConfig(to_hidl_leaudio_config_2_1(
474 audio_config.get<AudioConfiguration::leAudioConfig>()));
475 break;
476 case AudioConfiguration::leAudioBroadcastConfig:
477 hidl_audio_config.leAudioCodecConfig(to_hidl_leaudio_broadcast_config_2_1(
478 audio_config.get<AudioConfiguration::leAudioBroadcastConfig>()));
479 break;
480 default:
481 LOG(FATAL) << __func__ << ": unexpected AudioConfiguration";
482 }
483 return hidl_audio_config;
484 }
485
486 /***
487 *
488 * 2.0
489 *
490 ***/
491
IsSessionReady(const SessionType_2_0 & session_type)492 bool HidlToAidlMiddleware_2_0::IsSessionReady(
493 const SessionType_2_0& session_type) {
494 return BluetoothAudioSessionControl::IsSessionReady(
495 from_session_type_2_0(session_type));
496 }
497
RegisterControlResultCback(const SessionType_2_0 & session_type,const PortStatusCallbacks_2_0 & cbacks)498 uint16_t HidlToAidlMiddleware_2_0::RegisterControlResultCback(
499 const SessionType_2_0& session_type,
500 const PortStatusCallbacks_2_0& cbacks) {
501 LOG(INFO) << __func__ << ": " << toString(session_type);
502 auto aidl_session_type = from_session_type_2_0(session_type);
503 // Pass the exact reference to the lambda
504 auto& session_legacy_callback_table =
505 legacy_callback_table[aidl_session_type];
506 PortStatusCallbacks aidl_callbacks{};
507 if (cbacks.control_result_cb_) {
508 aidl_callbacks.control_result_cb_ =
509 [&session_legacy_callback_table](uint16_t cookie, bool start_resp,
510 const BluetoothAudioStatus& status) {
511 if (session_legacy_callback_table.find(cookie) ==
512 session_legacy_callback_table.end()) {
513 LOG(ERROR) << __func__ << ": Unknown callback invoked!";
514 return;
515 }
516 auto& cback = session_legacy_callback_table[cookie];
517 cback->control_result_cb_(cookie, start_resp, to_hidl_status(status));
518 };
519 }
520 if (cbacks.session_changed_cb_) {
521 aidl_callbacks.session_changed_cb_ =
522 [&session_legacy_callback_table](uint16_t cookie) {
523 if (session_legacy_callback_table.find(cookie) ==
524 session_legacy_callback_table.end()) {
525 LOG(ERROR) << __func__ << ": Unknown callback invoked!";
526 return;
527 }
528 auto& cback = session_legacy_callback_table[cookie];
529 cback->session_changed_cb_(cookie);
530 };
531 };
532 auto cookie = BluetoothAudioSessionControl::RegisterControlResultCback(
533 aidl_session_type, aidl_callbacks);
534 {
535 std::lock_guard<std::mutex> guard(legacy_callback_lock);
536 session_legacy_callback_table[cookie] =
537 std::make_shared<PortStatusCallbacks_2_0>(cbacks);
538 }
539 return cookie;
540 }
541
UnregisterControlResultCback(const SessionType_2_0 & session_type,uint16_t cookie)542 void HidlToAidlMiddleware_2_0::UnregisterControlResultCback(
543 const SessionType_2_0& session_type, uint16_t cookie) {
544 LOG(INFO) << __func__ << ": " << toString(session_type);
545 auto aidl_session_type = from_session_type_2_0(session_type);
546 BluetoothAudioSessionControl::UnregisterControlResultCback(aidl_session_type,
547 cookie);
548 auto& session_callback_table = legacy_callback_table[aidl_session_type];
549 if (session_callback_table.find(cookie) != session_callback_table.end()) {
550 std::lock_guard<std::mutex> guard(legacy_callback_lock);
551 session_callback_table.erase(cookie);
552 }
553 }
554
GetAudioConfig(const SessionType_2_0 & session_type)555 const AudioConfig_2_0 HidlToAidlMiddleware_2_0::GetAudioConfig(
556 const SessionType_2_0& session_type) {
557 return to_hidl_audio_config_2_0(BluetoothAudioSessionControl::GetAudioConfig(
558 from_session_type_2_0(session_type)));
559 }
560
StartStream(const SessionType_2_0 & session_type)561 bool HidlToAidlMiddleware_2_0::StartStream(
562 const SessionType_2_0& session_type) {
563 return BluetoothAudioSessionControl::StartStream(
564 from_session_type_2_0(session_type));
565 }
566
StopStream(const SessionType_2_0 & session_type)567 void HidlToAidlMiddleware_2_0::StopStream(const SessionType_2_0& session_type) {
568 return BluetoothAudioSessionControl::StopStream(
569 from_session_type_2_0(session_type));
570 }
571
SuspendStream(const SessionType_2_0 & session_type)572 bool HidlToAidlMiddleware_2_0::SuspendStream(
573 const SessionType_2_0& session_type) {
574 return BluetoothAudioSessionControl::SuspendStream(
575 from_session_type_2_0(session_type));
576 }
577
GetPresentationPosition(const SessionType_2_0 & session_type,uint64_t * remote_delay_report_ns,uint64_t * total_bytes_readed,timespec * data_position)578 bool HidlToAidlMiddleware_2_0::GetPresentationPosition(
579 const SessionType_2_0& session_type, uint64_t* remote_delay_report_ns,
580 uint64_t* total_bytes_readed, timespec* data_position) {
581 PresentationPosition presentation_position;
582 auto ret_val = BluetoothAudioSessionControl::GetPresentationPosition(
583 from_session_type_2_0(session_type), presentation_position);
584 if (remote_delay_report_ns)
585 *remote_delay_report_ns = presentation_position.remoteDeviceAudioDelayNanos;
586 if (total_bytes_readed)
587 *total_bytes_readed = presentation_position.transmittedOctets;
588 if (data_position)
589 *data_position = {
590 .tv_sec = static_cast<long>(
591 presentation_position.transmittedOctetsTimestamp.tvSec),
592 .tv_nsec = static_cast<long>(
593 presentation_position.transmittedOctetsTimestamp.tvNSec)};
594 return ret_val;
595 }
596
UpdateTracksMetadata(const SessionType_2_0 & session_type,const struct source_metadata * source_metadata)597 void HidlToAidlMiddleware_2_0::UpdateTracksMetadata(
598 const SessionType_2_0& session_type,
599 const struct source_metadata* source_metadata) {
600 return BluetoothAudioSessionControl::UpdateSourceMetadata(
601 from_session_type_2_0(session_type), *source_metadata);
602 }
603
OutWritePcmData(const SessionType_2_0 & session_type,const void * buffer,size_t bytes)604 size_t HidlToAidlMiddleware_2_0::OutWritePcmData(
605 const SessionType_2_0& session_type, const void* buffer, size_t bytes) {
606 return BluetoothAudioSessionControl::OutWritePcmData(
607 from_session_type_2_0(session_type), buffer, bytes);
608 }
609
InReadPcmData(const SessionType_2_0 & session_type,void * buffer,size_t bytes)610 size_t HidlToAidlMiddleware_2_0::InReadPcmData(
611 const SessionType_2_0& session_type, void* buffer, size_t bytes) {
612 return BluetoothAudioSessionControl::InReadPcmData(
613 from_session_type_2_0(session_type), buffer, bytes);
614 }
615
IsAidlAvailable()616 bool HidlToAidlMiddleware_2_0::IsAidlAvailable() {
617 return BluetoothAudioSession::IsAidlAvailable();
618 }
619
620 /***
621 *
622 * 2.1
623 *
624 ***/
625
GetAudioConfig(const SessionType_2_1 & session_type)626 const AudioConfig_2_1 HidlToAidlMiddleware_2_1::GetAudioConfig(
627 const SessionType_2_1& session_type) {
628 return to_hidl_audio_config_2_1(BluetoothAudioSessionControl::GetAudioConfig(
629 from_session_type_2_1(session_type)));
630 }
631
632 } // namespace audio
633 } // namespace bluetooth
634 } // namespace hardware
635 } // namespace android
636 } // namespace aidl
637