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