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