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