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