blob: 1ef9365918a5beac7a4d386028f51e76a4158f41 [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
Josh Wu20bac522021-12-29 23:52:39 -0800192inline SessionType from_session_type_2_1(
193 const SessionType_2_1& session_type_hidl) {
194 auto it = session_type_2_1_to_aidl_map.find(session_type_hidl);
195 if (it != session_type_2_1_to_aidl_map.end()) return it->second;
196 return SessionType::UNKNOWN;
197}
198
Josh Wuce6ca9e2022-01-20 09:48:08 -0800199inline SessionType from_session_type_2_0(
200 const SessionType_2_0& session_type_hidl) {
201 return from_session_type_2_1(static_cast<SessionType_2_1>(session_type_hidl));
202}
203
Josh Wu20bac522021-12-29 23:52:39 -0800204inline HidlStatus to_hidl_status(const BluetoothAudioStatus& status) {
205 switch (status) {
206 case BluetoothAudioStatus::SUCCESS:
207 return HidlStatus::SUCCESS;
208 case BluetoothAudioStatus::UNSUPPORTED_CODEC_CONFIGURATION:
209 return HidlStatus::UNSUPPORTED_CODEC_CONFIGURATION;
210 default:
211 return HidlStatus::FAILURE;
212 }
213}
214
Josh Wu20bac522021-12-29 23:52:39 -0800215inline SampleRate_2_1 to_hidl_sample_rate_2_1(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()) return it->second;
218 return SampleRate_2_1::RATE_UNKNOWN;
219}
220
Josh Wu653108c2022-01-21 22:33:43 -0800221inline SampleRate_2_0 to_hidl_sample_rate_2_0(const int32_t sample_rate_hz) {
222 auto it = sample_rate_to_hidl_2_1_map.find(sample_rate_hz);
223 if (it != sample_rate_to_hidl_2_1_map.end())
224 return static_cast<SampleRate_2_0>(it->second);
225 return SampleRate_2_0::RATE_UNKNOWN;
226}
227
Josh Wu20bac522021-12-29 23:52:39 -0800228inline BitsPerSample_2_0 to_hidl_bits_per_sample(const int8_t bit_per_sample) {
229 switch (bit_per_sample) {
230 case 16:
231 return BitsPerSample_2_0::BITS_16;
232 case 24:
233 return BitsPerSample_2_0::BITS_24;
234 case 32:
235 return BitsPerSample_2_0::BITS_32;
236 default:
237 return BitsPerSample_2_0::BITS_UNKNOWN;
238 }
239}
240
241inline ChannelMode_2_0 to_hidl_channel_mode(const ChannelMode channel_mode) {
242 switch (channel_mode) {
243 case ChannelMode::MONO:
244 return ChannelMode_2_0::MONO;
245 case ChannelMode::STEREO:
246 return ChannelMode_2_0::STEREO;
247 default:
248 return ChannelMode_2_0::UNKNOWN;
249 }
250}
251
252inline PcmConfig_2_0 to_hidl_pcm_config_2_0(
253 const PcmConfiguration& pcm_config) {
254 PcmConfig_2_0 hidl_pcm_config;
255 hidl_pcm_config.sampleRate = to_hidl_sample_rate_2_0(pcm_config.sampleRateHz);
256 hidl_pcm_config.channelMode = to_hidl_channel_mode(pcm_config.channelMode);
257 hidl_pcm_config.bitsPerSample =
258 to_hidl_bits_per_sample(pcm_config.bitsPerSample);
259 return hidl_pcm_config;
260}
261
262inline CodecType_2_0 to_hidl_codec_type_2_0(const CodecType codec_type) {
263 auto it = codec_type_to_hidl_2_0_map.find(codec_type);
264 if (it != codec_type_to_hidl_2_0_map.end()) return it->second;
265 return CodecType_2_0::UNKNOWN;
266}
267
268inline SbcConfig_2_0 to_hidl_sbc_config(const SbcConfiguration sbc_config) {
269 SbcConfig_2_0 hidl_sbc_config;
270 hidl_sbc_config.minBitpool = sbc_config.minBitpool;
271 hidl_sbc_config.maxBitpool = sbc_config.maxBitpool;
272 hidl_sbc_config.sampleRate = to_hidl_sample_rate_2_0(sbc_config.sampleRateHz);
273 hidl_sbc_config.bitsPerSample =
274 to_hidl_bits_per_sample(sbc_config.bitsPerSample);
275 if (sbc_channel_mode_to_hidl_2_0_map.find(sbc_config.channelMode) !=
276 sbc_channel_mode_to_hidl_2_0_map.end()) {
277 hidl_sbc_config.channelMode =
278 sbc_channel_mode_to_hidl_2_0_map.at(sbc_config.channelMode);
279 }
280 if (sbc_block_length_to_hidl_map.find(sbc_config.blockLength) !=
281 sbc_block_length_to_hidl_map.end()) {
282 hidl_sbc_config.blockLength =
283 sbc_block_length_to_hidl_map.at(sbc_config.blockLength);
284 }
285 if (sbc_subbands_to_hidl_map.find(sbc_config.numSubbands) !=
286 sbc_subbands_to_hidl_map.end()) {
287 hidl_sbc_config.numSubbands =
288 sbc_subbands_to_hidl_map.at(sbc_config.numSubbands);
289 }
290 if (sbc_alloc_method_to_hidl_map.find(sbc_config.allocMethod) !=
291 sbc_alloc_method_to_hidl_map.end()) {
292 hidl_sbc_config.allocMethod =
293 sbc_alloc_method_to_hidl_map.at(sbc_config.allocMethod);
294 }
295 return hidl_sbc_config;
296}
297
298inline AacConfig_2_0 to_hidl_aac_config(const AacConfiguration aac_config) {
299 AacConfig_2_0 hidl_aac_config;
300 hidl_aac_config.sampleRate = to_hidl_sample_rate_2_0(aac_config.sampleRateHz);
301 hidl_aac_config.bitsPerSample =
302 to_hidl_bits_per_sample(aac_config.bitsPerSample);
303 hidl_aac_config.channelMode = to_hidl_channel_mode(aac_config.channelMode);
304 if (aac_object_type_to_hidl_map.find(aac_config.objectType) !=
305 aac_object_type_to_hidl_map.end()) {
306 hidl_aac_config.objectType =
307 aac_object_type_to_hidl_map.at(aac_config.objectType);
308 }
309 hidl_aac_config.variableBitRateEnabled = aac_config.variableBitRateEnabled
310 ? AacVarBitRate_2_0::ENABLED
311 : AacVarBitRate_2_0::DISABLED;
312 return hidl_aac_config;
313}
314
315inline LdacConfig_2_0 to_hidl_ldac_config(const LdacConfiguration ldac_config) {
316 LdacConfig_2_0 hidl_ldac_config;
317 hidl_ldac_config.sampleRate =
318 to_hidl_sample_rate_2_0(ldac_config.sampleRateHz);
319 hidl_ldac_config.bitsPerSample =
320 to_hidl_bits_per_sample(ldac_config.bitsPerSample);
321 if (ldac_channel_mode_to_hidl_map.find(ldac_config.channelMode) !=
322 ldac_channel_mode_to_hidl_map.end()) {
323 hidl_ldac_config.channelMode =
324 ldac_channel_mode_to_hidl_map.at(ldac_config.channelMode);
325 }
326 if (ldac_qindex_to_hidl_map.find(ldac_config.qualityIndex) !=
327 ldac_qindex_to_hidl_map.end()) {
328 hidl_ldac_config.qualityIndex =
329 ldac_qindex_to_hidl_map.at(ldac_config.qualityIndex);
330 }
331 return hidl_ldac_config;
332}
333
334inline AptxConfig_2_0 to_hidl_aptx_config(const AptxConfiguration aptx_config) {
335 AptxConfig_2_0 hidl_aptx_config;
336 hidl_aptx_config.sampleRate =
337 to_hidl_sample_rate_2_0(aptx_config.sampleRateHz);
338 hidl_aptx_config.bitsPerSample =
339 to_hidl_bits_per_sample(aptx_config.bitsPerSample);
340 hidl_aptx_config.channelMode = to_hidl_channel_mode(aptx_config.channelMode);
341 return hidl_aptx_config;
342}
343
344inline CodecConfig_2_0 to_hidl_codec_config_2_0(
345 const CodecConfiguration& codec_config) {
346 CodecConfig_2_0 hidl_codec_config;
347 hidl_codec_config.codecType = to_hidl_codec_type_2_0(codec_config.codecType);
348 hidl_codec_config.encodedAudioBitrate =
349 static_cast<uint32_t>(codec_config.encodedAudioBitrate);
350 hidl_codec_config.peerMtu = static_cast<uint32_t>(codec_config.peerMtu);
351 hidl_codec_config.isScmstEnabled = codec_config.isScmstEnabled;
352 switch (codec_config.config.getTag()) {
353 case CodecConfiguration::CodecSpecific::sbcConfig:
354 hidl_codec_config.config.sbcConfig(to_hidl_sbc_config(
355 codec_config.config
356 .get<CodecConfiguration::CodecSpecific::sbcConfig>()));
357 break;
358 case CodecConfiguration::CodecSpecific::aacConfig:
359 hidl_codec_config.config.aacConfig(to_hidl_aac_config(
360 codec_config.config
361 .get<CodecConfiguration::CodecSpecific::aacConfig>()));
362 break;
363 case CodecConfiguration::CodecSpecific::ldacConfig:
364 hidl_codec_config.config.ldacConfig(to_hidl_ldac_config(
365 codec_config.config
366 .get<CodecConfiguration::CodecSpecific::ldacConfig>()));
367 break;
368 case CodecConfiguration::CodecSpecific::aptxConfig:
369 hidl_codec_config.config.aptxConfig(to_hidl_aptx_config(
370 codec_config.config
371 .get<CodecConfiguration::CodecSpecific::aptxConfig>()));
372 break;
373 default:
374 break;
375 }
376 return hidl_codec_config;
377}
378
379inline AudioConfig_2_0 to_hidl_audio_config_2_0(
380 const AudioConfiguration& audio_config) {
381 AudioConfig_2_0 hidl_audio_config;
382 if (audio_config.getTag() == AudioConfiguration::pcmConfig) {
383 hidl_audio_config.pcmConfig(to_hidl_pcm_config_2_0(
384 audio_config.get<AudioConfiguration::pcmConfig>()));
385 } else if (audio_config.getTag() == AudioConfiguration::a2dpConfig) {
386 hidl_audio_config.codecConfig(to_hidl_codec_config_2_0(
387 audio_config.get<AudioConfiguration::a2dpConfig>()));
388 }
389 return hidl_audio_config;
390}
391
392inline PcmConfig_2_1 to_hidl_pcm_config_2_1(
393 const PcmConfiguration& pcm_config) {
394 PcmConfig_2_1 hidl_pcm_config;
395 hidl_pcm_config.sampleRate = to_hidl_sample_rate_2_1(pcm_config.sampleRateHz);
396 hidl_pcm_config.channelMode = to_hidl_channel_mode(pcm_config.channelMode);
397 hidl_pcm_config.bitsPerSample =
398 to_hidl_bits_per_sample(pcm_config.bitsPerSample);
399 hidl_pcm_config.dataIntervalUs =
400 static_cast<uint32_t>(pcm_config.dataIntervalUs);
401 return hidl_pcm_config;
402}
403
404inline Lc3Config_2_1 to_hidl_lc3_config_2_1(
405 const Lc3Configuration& lc3_config) {
406 Lc3Config_2_1 hidl_lc3_config;
407 hidl_lc3_config.pcmBitDepth = to_hidl_bits_per_sample(lc3_config.pcmBitDepth);
408 hidl_lc3_config.samplingFrequency =
409 to_hidl_sample_rate_2_1(lc3_config.samplingFrequencyHz);
410 if (lc3_config.samplingFrequencyHz == 10000)
411 hidl_lc3_config.frameDuration = Lc3FrameDuration_2_1::DURATION_10000US;
412 else if (lc3_config.samplingFrequencyHz == 7500)
413 hidl_lc3_config.frameDuration = Lc3FrameDuration_2_1::DURATION_7500US;
414 hidl_lc3_config.octetsPerFrame =
415 static_cast<uint32_t>(lc3_config.octetsPerFrame);
416 hidl_lc3_config.blocksPerSdu = static_cast<uint32_t>(lc3_config.blocksPerSdu);
417 return hidl_lc3_config;
418}
419
420inline Lc3CodecConfig_2_1 to_hidl_leaudio_config_2_1(
Jack He16a15472022-02-08 11:56:22 -0800421 const LeAudioConfiguration& unicast_config) {
Josh Wu75462aa2022-01-21 21:51:21 -0800422 Lc3CodecConfig_2_1 hidl_lc3_codec_config = {
423 .audioChannelAllocation = 0,
424 };
Jack He16a15472022-02-08 11:56:22 -0800425 if (unicast_config.leAudioCodecConfig.getTag() ==
426 LeAudioCodecConfiguration::lc3Config) {
427 LOG(FATAL) << __func__ << ": unexpected codec type(vendor?)";
Josh Wu75462aa2022-01-21 21:51:21 -0800428 }
Jack He16a15472022-02-08 11:56:22 -0800429 auto& le_codec_config = unicast_config.leAudioCodecConfig
430 .get<LeAudioCodecConfiguration::lc3Config>();
Josh Wu20bac522021-12-29 23:52:39 -0800431
Jack He16a15472022-02-08 11:56:22 -0800432 hidl_lc3_codec_config.lc3Config = to_hidl_lc3_config_2_1(le_codec_config);
433
434 for (const auto& map : unicast_config.streamMap) {
435 hidl_lc3_codec_config.audioChannelAllocation |= map.audioChannelAllocation;
436 }
437 return hidl_lc3_codec_config;
438}
439
440inline Lc3CodecConfig_2_1 to_hidl_leaudio_broadcast_config_2_1(
441 const LeAudioBroadcastConfiguration& broadcast_config) {
442 Lc3CodecConfig_2_1 hidl_lc3_codec_config = {
443 .audioChannelAllocation = 0,
444 };
445 // NOTE: Broadcast is not officially supported in HIDL
446 if (broadcast_config.streamMap.empty()) {
447 return hidl_lc3_codec_config;
448 }
449 if (broadcast_config.streamMap[0].leAudioCodecConfig.getTag() !=
450 LeAudioCodecConfiguration::lc3Config) {
451 LOG(FATAL) << __func__ << ": unexpected codec type(vendor?)";
452 }
453 auto& le_codec_config =
454 broadcast_config.streamMap[0]
455 .leAudioCodecConfig.get<LeAudioCodecConfiguration::lc3Config>();
456 hidl_lc3_codec_config.lc3Config = to_hidl_lc3_config_2_1(le_codec_config);
457
458 for (const auto& map : broadcast_config.streamMap) {
459 hidl_lc3_codec_config.audioChannelAllocation |= map.audioChannelAllocation;
460 }
Josh Wu20bac522021-12-29 23:52:39 -0800461 return hidl_lc3_codec_config;
462}
463
464inline LeAudioConfig_2_2 to_hidl_leaudio_config_2_2(
Jack He16a15472022-02-08 11:56:22 -0800465 const LeAudioConfiguration& unicast_config) {
Josh Wu20bac522021-12-29 23:52:39 -0800466 LeAudioConfig_2_2 hidl_leaudio_config;
Jack He16a15472022-02-08 11:56:22 -0800467 hidl_leaudio_config.mode = LeAudioMode_2_2::UNICAST;
468 ::android::hardware::bluetooth::audio::V2_2::UnicastConfig
469 hidl_unicast_config;
470 hidl_unicast_config.peerDelay =
471 static_cast<uint32_t>(unicast_config.peerDelayUs / 1000);
Josh Wu20bac522021-12-29 23:52:39 -0800472
Jack He16a15472022-02-08 11:56:22 -0800473 auto& lc3_config = unicast_config.leAudioCodecConfig
474 .get<LeAudioCodecConfiguration::lc3Config>();
475 hidl_unicast_config.lc3Config = to_hidl_lc3_config_2_1(lc3_config);
Josh Wu20bac522021-12-29 23:52:39 -0800476
Jack He16a15472022-02-08 11:56:22 -0800477 hidl_unicast_config.streamMap.resize(unicast_config.streamMap.size());
478 for (int i = 0; i < unicast_config.streamMap.size(); i++) {
479 hidl_unicast_config.streamMap[i].audioChannelAllocation =
480 static_cast<uint32_t>(
481 unicast_config.streamMap[i].audioChannelAllocation);
482 hidl_unicast_config.streamMap[i].streamHandle =
483 static_cast<uint16_t>(unicast_config.streamMap[i].streamHandle);
484 }
485 return hidl_leaudio_config;
486}
Josh Wu20bac522021-12-29 23:52:39 -0800487
Jack He16a15472022-02-08 11:56:22 -0800488inline LeAudioConfig_2_2 to_hidl_leaudio_broadcast_config_2_2(
489 const LeAudioBroadcastConfiguration& broadcast_config) {
490 LeAudioConfig_2_2 hidl_leaudio_config;
491 hidl_leaudio_config.mode = LeAudioMode_2_2::BROADCAST;
492 ::android::hardware::bluetooth::audio::V2_2::BroadcastConfig
493 hidl_bcast_config;
494 hidl_bcast_config.streamMap.resize(broadcast_config.streamMap.size());
495 for (int i = 0; i < broadcast_config.streamMap.size(); i++) {
496 hidl_bcast_config.streamMap[i].audioChannelAllocation =
497 static_cast<uint32_t>(
498 broadcast_config.streamMap[i].audioChannelAllocation);
499 hidl_bcast_config.streamMap[i].streamHandle =
500 static_cast<uint16_t>(broadcast_config.streamMap[i].streamHandle);
501 hidl_bcast_config.streamMap[i].lc3Config = to_hidl_lc3_config_2_1(
502 broadcast_config.streamMap[i]
503 .leAudioCodecConfig.get<LeAudioCodecConfiguration::lc3Config>());
Josh Wu20bac522021-12-29 23:52:39 -0800504 }
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;
Jack He16a15472022-02-08 11:56:22 -0800524 case AudioConfiguration::leAudioBroadcastConfig:
525 hidl_audio_config.leAudioCodecConfig(to_hidl_leaudio_broadcast_config_2_1(
526 audio_config.get<AudioConfiguration::leAudioBroadcastConfig>()));
527 break;
Josh Wu20bac522021-12-29 23:52:39 -0800528 }
529 return hidl_audio_config;
530}
531
532inline AudioConfig_2_2 to_hidl_audio_config_2_2(
533 const AudioConfiguration& audio_config) {
534 AudioConfig_2_2 hidl_audio_config;
535 switch (audio_config.getTag()) {
536 case AudioConfiguration::pcmConfig:
537 hidl_audio_config.pcmConfig(to_hidl_pcm_config_2_1(
538 audio_config.get<AudioConfiguration::pcmConfig>()));
539 break;
540 case AudioConfiguration::a2dpConfig:
541 hidl_audio_config.codecConfig(to_hidl_codec_config_2_0(
542 audio_config.get<AudioConfiguration::a2dpConfig>()));
543 break;
544 case AudioConfiguration::leAudioConfig:
545 hidl_audio_config.leAudioConfig(to_hidl_leaudio_config_2_2(
546 audio_config.get<AudioConfiguration::leAudioConfig>()));
547 break;
Jack He16a15472022-02-08 11:56:22 -0800548 case AudioConfiguration::leAudioBroadcastConfig:
549 hidl_audio_config.leAudioConfig(to_hidl_leaudio_broadcast_config_2_2(
550 audio_config.get<AudioConfiguration::leAudioBroadcastConfig>()));
551 break;
Josh Wu20bac522021-12-29 23:52:39 -0800552 }
553 return hidl_audio_config;
554}
555
556/***
557 *
558 * 2.0
559 *
560 ***/
561
562bool HidlToAidlMiddleware_2_0::IsSessionReady(
563 const SessionType_2_0& session_type) {
564 return BluetoothAudioSessionControl::IsSessionReady(
565 from_session_type_2_0(session_type));
566}
567
568uint16_t HidlToAidlMiddleware_2_0::RegisterControlResultCback(
569 const SessionType_2_0& session_type,
570 const PortStatusCallbacks_2_0& cbacks) {
571 PortStatusCallbacks_2_2 callback_2_2{
572 .control_result_cb_ = cbacks.control_result_cb_,
573 .session_changed_cb_ = cbacks.session_changed_cb_,
574 };
575 return HidlToAidlMiddleware_2_2::RegisterControlResultCback(
576 static_cast<SessionType_2_1>(session_type), callback_2_2);
577}
578
579void HidlToAidlMiddleware_2_0::UnregisterControlResultCback(
580 const SessionType_2_0& session_type, uint16_t cookie) {
581 HidlToAidlMiddleware_2_2::UnregisterControlResultCback(
582 static_cast<SessionType_2_1>(session_type), cookie);
583}
584
585const AudioConfig_2_0 HidlToAidlMiddleware_2_0::GetAudioConfig(
586 const SessionType_2_0& session_type) {
587 return to_hidl_audio_config_2_0(BluetoothAudioSessionControl::GetAudioConfig(
588 from_session_type_2_0(session_type)));
589}
590
591bool HidlToAidlMiddleware_2_0::StartStream(
592 const SessionType_2_0& session_type) {
593 return BluetoothAudioSessionControl::StartStream(
594 from_session_type_2_0(session_type));
595}
596
597void HidlToAidlMiddleware_2_0::StopStream(const SessionType_2_0& session_type) {
598 return BluetoothAudioSessionControl::StopStream(
599 from_session_type_2_0(session_type));
600}
601
602bool HidlToAidlMiddleware_2_0::SuspendStream(
603 const SessionType_2_0& session_type) {
604 return BluetoothAudioSessionControl::SuspendStream(
605 from_session_type_2_0(session_type));
606}
607
608bool HidlToAidlMiddleware_2_0::GetPresentationPosition(
609 const SessionType_2_0& session_type, uint64_t* remote_delay_report_ns,
610 uint64_t* total_bytes_readed, timespec* data_position) {
611 PresentationPosition presentation_position;
612 auto ret_val = BluetoothAudioSessionControl::GetPresentationPosition(
613 from_session_type_2_0(session_type), presentation_position);
614 if (remote_delay_report_ns)
615 *remote_delay_report_ns = presentation_position.remoteDeviceAudioDelayNanos;
616 if (total_bytes_readed)
617 *total_bytes_readed = presentation_position.transmittedOctets;
618 if (data_position)
619 *data_position = {
620 .tv_sec = static_cast<__kernel_old_time_t>(
621 presentation_position.transmittedOctetsTimestamp.tvSec),
622 .tv_nsec = static_cast<long>(
623 presentation_position.transmittedOctetsTimestamp.tvNSec)};
624 return ret_val;
625}
626
627void HidlToAidlMiddleware_2_0::UpdateTracksMetadata(
628 const SessionType_2_0& session_type,
629 const struct source_metadata* source_metadata) {
630 return BluetoothAudioSessionControl::UpdateSourceMetadata(
631 from_session_type_2_0(session_type), *source_metadata);
632}
633
634size_t HidlToAidlMiddleware_2_0::OutWritePcmData(
635 const SessionType_2_0& session_type, const void* buffer, size_t bytes) {
636 return BluetoothAudioSessionControl::OutWritePcmData(
637 from_session_type_2_0(session_type), buffer, bytes);
638}
639
Josh Wu9880f6b2022-01-21 20:49:49 -0800640size_t HidlToAidlMiddleware_2_0::InReadPcmData(
641 const SessionType_2_0& session_type, void* buffer, size_t bytes) {
642 return BluetoothAudioSessionControl::InReadPcmData(
643 from_session_type_2_0(session_type), buffer, bytes);
644}
645
Josh Wu20bac522021-12-29 23:52:39 -0800646bool HidlToAidlMiddleware_2_0::IsAidlAvailable() {
647 return BluetoothAudioSession::IsAidlAvailable();
648}
649
650/***
651 *
652 * 2.1
653 *
654 ***/
655
656const AudioConfig_2_1 HidlToAidlMiddleware_2_1::GetAudioConfig(
657 const SessionType_2_1& session_type) {
658 return to_hidl_audio_config_2_1(BluetoothAudioSessionControl::GetAudioConfig(
659 from_session_type_2_1(session_type)));
660}
661
662/***
663 *
664 * 2.2
665 *
666 ***/
667
668bool HidlToAidlMiddleware_2_2::IsSessionReady(
669 const SessionType_2_1& session_type) {
670 return BluetoothAudioSessionControl::IsSessionReady(
671 from_session_type_2_1(session_type));
672}
673
674uint16_t HidlToAidlMiddleware_2_2::RegisterControlResultCback(
675 const SessionType_2_1& session_type,
676 const PortStatusCallbacks_2_2& cbacks) {
677 LOG(INFO) << __func__ << ": " << toString(session_type);
678 auto aidl_session_type = from_session_type_2_1(session_type);
679 // Pass the exact reference to the lambda
680 auto& session_legacy_callback_table =
681 legacy_callback_table[aidl_session_type];
682 PortStatusCallbacks aidl_callbacks{};
683 if (cbacks.control_result_cb_) {
684 aidl_callbacks.control_result_cb_ =
685 [&session_legacy_callback_table](uint16_t cookie, bool start_resp,
686 const BluetoothAudioStatus& status) {
687 if (session_legacy_callback_table.find(cookie) ==
688 session_legacy_callback_table.end()) {
689 LOG(ERROR) << __func__ << ": Unknown callback invoked!";
690 return;
691 }
692 auto& cback = session_legacy_callback_table[cookie];
693 cback->control_result_cb_(cookie, start_resp, to_hidl_status(status));
694 };
695 }
696 if (cbacks.session_changed_cb_) {
697 aidl_callbacks.session_changed_cb_ =
698 [&session_legacy_callback_table](uint16_t cookie) {
699 if (session_legacy_callback_table.find(cookie) ==
700 session_legacy_callback_table.end()) {
701 LOG(ERROR) << __func__ << ": Unknown callback invoked!";
702 return;
703 }
704 auto& cback = session_legacy_callback_table[cookie];
705 cback->session_changed_cb_(cookie);
706 };
707 };
708 if (cbacks.audio_configuration_changed_cb_) {
709 aidl_callbacks.audio_configuration_changed_cb_ =
710 [&session_legacy_callback_table](uint16_t cookie) {
711 if (session_legacy_callback_table.find(cookie) ==
712 session_legacy_callback_table.end()) {
713 LOG(ERROR) << __func__ << ": Unknown callback invoked!";
714 return;
715 }
716 auto& cback = session_legacy_callback_table[cookie];
717 cback->audio_configuration_changed_cb_(cookie);
718 };
719 };
720 auto cookie = BluetoothAudioSessionControl::RegisterControlResultCback(
721 aidl_session_type, aidl_callbacks);
722 {
723 std::lock_guard<std::mutex> guard(legacy_callback_lock);
724 session_legacy_callback_table[cookie] =
725 std::make_shared<PortStatusCallbacks_2_2>(cbacks);
726 }
727 return cookie;
728}
729
730void HidlToAidlMiddleware_2_2::UnregisterControlResultCback(
731 const SessionType_2_1& session_type, uint16_t cookie) {
732 LOG(INFO) << __func__ << ": " << toString(session_type);
733 auto aidl_session_type = from_session_type_2_1(session_type);
734 BluetoothAudioSessionControl::UnregisterControlResultCback(aidl_session_type,
735 cookie);
736 auto& session_callback_table = legacy_callback_table[aidl_session_type];
737 if (session_callback_table.find(cookie) != session_callback_table.end()) {
738 std::lock_guard<std::mutex> guard(legacy_callback_lock);
739 session_callback_table.erase(cookie);
740 }
741}
742
743const AudioConfig_2_2 HidlToAidlMiddleware_2_2::GetAudioConfig(
744 const SessionType_2_1& session_type) {
745 return to_hidl_audio_config_2_2(BluetoothAudioSessionControl::GetAudioConfig(
746 from_session_type_2_1(session_type)));
747}
748
749bool HidlToAidlMiddleware_2_2::StartStream(
750 const SessionType_2_1& session_type) {
751 return BluetoothAudioSessionControl::StartStream(
752 from_session_type_2_1(session_type));
753}
754
755bool HidlToAidlMiddleware_2_2::SuspendStream(
756 const SessionType_2_1& session_type) {
757 return BluetoothAudioSessionControl::SuspendStream(
758 from_session_type_2_1(session_type));
759}
760
761void HidlToAidlMiddleware_2_2::StopStream(const SessionType_2_1& session_type) {
762 return BluetoothAudioSessionControl::StopStream(
763 from_session_type_2_1(session_type));
764}
765
Alice Kuodea3e802022-01-20 14:46:44 +0800766void HidlToAidlMiddleware_2_2::UpdateTracksMetadata(
767 const SessionType_2_1& session_type,
768 const struct source_metadata* source_metadata) {
769 return BluetoothAudioSessionControl::UpdateSourceMetadata(
770 from_session_type_2_1(session_type), *source_metadata);
771}
772
Josh Wu20bac522021-12-29 23:52:39 -0800773void HidlToAidlMiddleware_2_2::UpdateSinkMetadata(
774 const SessionType_2_1& session_type,
775 const struct sink_metadata* sink_metadata) {
776 return BluetoothAudioSessionControl::UpdateSinkMetadata(
777 from_session_type_2_1(session_type), *sink_metadata);
778}
779
780} // namespace audio
781} // namespace bluetooth
782} // namespace hardware
783} // namespace android
784} // namespace aidl