blob: 632a389d8ce122c811cd4741575036a6253d15b4 [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(
421 const LeAudioConfiguration& leaudio_config) {
Josh Wu75462aa2022-01-21 21:51:21 -0800422 Lc3CodecConfig_2_1 hidl_lc3_codec_config = {
423 .audioChannelAllocation = 0,
424 };
425 if (leaudio_config.modeConfig.getTag() ==
426 LeAudioConfiguration::LeAudioModeConfig::unicastConfig) {
427 auto& unicast_config =
428 leaudio_config.modeConfig
429 .get<LeAudioConfiguration::LeAudioModeConfig::unicastConfig>();
430 if (unicast_config.leAudioCodecConfig.getTag() ==
431 LeAudioCodecConfiguration::lc3Config) {
432 LOG(FATAL) << __func__ << ": unexpected codec type(vendor?)";
433 }
434 auto& le_codec_config = unicast_config.leAudioCodecConfig
435 .get<LeAudioCodecConfiguration::lc3Config>();
Josh Wu20bac522021-12-29 23:52:39 -0800436
Josh Wu75462aa2022-01-21 21:51:21 -0800437 hidl_lc3_codec_config.lc3Config = to_hidl_lc3_config_2_1(le_codec_config);
Josh Wu20bac522021-12-29 23:52:39 -0800438
Josh Wu75462aa2022-01-21 21:51:21 -0800439 for (const auto& map : unicast_config.streamMap) {
440 hidl_lc3_codec_config.audioChannelAllocation |=
441 map.audioChannelAllocation;
442 }
443 } else {
444 // NOTE: Broadcast is not officially supported in HIDL
445 auto& bcast_config =
446 leaudio_config.modeConfig
447 .get<LeAudioConfiguration::LeAudioModeConfig::broadcastConfig>();
448 if (bcast_config.streamMap.empty()) {
449 return hidl_lc3_codec_config;
450 }
451 if (bcast_config.streamMap[0].leAudioCodecConfig.getTag() !=
452 LeAudioCodecConfiguration::lc3Config) {
453 LOG(FATAL) << __func__ << ": unexpected codec type(vendor?)";
454 }
455 auto& le_codec_config =
456 bcast_config.streamMap[0]
457 .leAudioCodecConfig.get<LeAudioCodecConfiguration::lc3Config>();
458 hidl_lc3_codec_config.lc3Config = to_hidl_lc3_config_2_1(le_codec_config);
Josh Wu20bac522021-12-29 23:52:39 -0800459
Josh Wu75462aa2022-01-21 21:51:21 -0800460 for (const auto& map : bcast_config.streamMap) {
461 hidl_lc3_codec_config.audioChannelAllocation |=
462 map.audioChannelAllocation;
463 }
464 }
Josh Wu20bac522021-12-29 23:52:39 -0800465
466 return hidl_lc3_codec_config;
467}
468
469inline LeAudioConfig_2_2 to_hidl_leaudio_config_2_2(
470 const LeAudioConfiguration& leaudio_config) {
471 LeAudioConfig_2_2 hidl_leaudio_config;
Josh Wu20bac522021-12-29 23:52:39 -0800472
473 if (leaudio_config.modeConfig.getTag() ==
474 LeAudioConfiguration::LeAudioModeConfig::unicastConfig) {
Josh Wu75462aa2022-01-21 21:51:21 -0800475 hidl_leaudio_config.mode = LeAudioMode_2_2::UNICAST;
Josh Wu20bac522021-12-29 23:52:39 -0800476 auto& unicast_config =
477 leaudio_config.modeConfig
478 .get<LeAudioConfiguration::LeAudioModeConfig::unicastConfig>();
479 ::android::hardware::bluetooth::audio::V2_2::UnicastConfig
480 hidl_unicast_config;
481 hidl_unicast_config.peerDelay =
482 static_cast<uint32_t>(unicast_config.peerDelay);
483
484 auto& lc3_config = unicast_config.leAudioCodecConfig
485 .get<LeAudioCodecConfiguration::lc3Config>();
486 hidl_unicast_config.lc3Config = to_hidl_lc3_config_2_1(lc3_config);
487
488 hidl_unicast_config.streamMap.resize(unicast_config.streamMap.size());
489 for (int i = 0; i < unicast_config.streamMap.size(); i++) {
490 hidl_unicast_config.streamMap[i].audioChannelAllocation =
491 static_cast<uint32_t>(
492 unicast_config.streamMap[i].audioChannelAllocation);
493 hidl_unicast_config.streamMap[i].streamHandle =
494 static_cast<uint16_t>(unicast_config.streamMap[i].streamHandle);
495 }
496 } else if (leaudio_config.modeConfig.getTag() ==
497 LeAudioConfiguration::LeAudioModeConfig::broadcastConfig) {
Josh Wu75462aa2022-01-21 21:51:21 -0800498 hidl_leaudio_config.mode = LeAudioMode_2_2::BROADCAST;
Josh Wu20bac522021-12-29 23:52:39 -0800499 auto bcast_config =
500 leaudio_config.modeConfig
501 .get<LeAudioConfiguration::LeAudioModeConfig::broadcastConfig>();
502 ::android::hardware::bluetooth::audio::V2_2::BroadcastConfig
503 hidl_bcast_config;
504 hidl_bcast_config.streamMap.resize(bcast_config.streamMap.size());
505 for (int i = 0; i < bcast_config.streamMap.size(); i++) {
506 hidl_bcast_config.streamMap[i].audioChannelAllocation =
507 static_cast<uint32_t>(
508 bcast_config.streamMap[i].audioChannelAllocation);
509 hidl_bcast_config.streamMap[i].streamHandle =
510 static_cast<uint16_t>(bcast_config.streamMap[i].streamHandle);
511 hidl_bcast_config.streamMap[i].lc3Config = to_hidl_lc3_config_2_1(
512 bcast_config.streamMap[i]
513 .leAudioCodecConfig.get<LeAudioCodecConfiguration::lc3Config>());
514 }
515 }
516 return hidl_leaudio_config;
517}
518
519inline AudioConfig_2_1 to_hidl_audio_config_2_1(
520 const AudioConfiguration& audio_config) {
521 AudioConfig_2_1 hidl_audio_config;
522 switch (audio_config.getTag()) {
523 case AudioConfiguration::pcmConfig:
524 hidl_audio_config.pcmConfig(to_hidl_pcm_config_2_1(
525 audio_config.get<AudioConfiguration::pcmConfig>()));
526 break;
527 case AudioConfiguration::a2dpConfig:
528 hidl_audio_config.codecConfig(to_hidl_codec_config_2_0(
529 audio_config.get<AudioConfiguration::a2dpConfig>()));
530 break;
531 case AudioConfiguration::leAudioConfig:
532 hidl_audio_config.leAudioCodecConfig(to_hidl_leaudio_config_2_1(
533 audio_config.get<AudioConfiguration::leAudioConfig>()));
534 break;
535 }
536 return hidl_audio_config;
537}
538
539inline AudioConfig_2_2 to_hidl_audio_config_2_2(
540 const AudioConfiguration& audio_config) {
541 AudioConfig_2_2 hidl_audio_config;
542 switch (audio_config.getTag()) {
543 case AudioConfiguration::pcmConfig:
544 hidl_audio_config.pcmConfig(to_hidl_pcm_config_2_1(
545 audio_config.get<AudioConfiguration::pcmConfig>()));
546 break;
547 case AudioConfiguration::a2dpConfig:
548 hidl_audio_config.codecConfig(to_hidl_codec_config_2_0(
549 audio_config.get<AudioConfiguration::a2dpConfig>()));
550 break;
551 case AudioConfiguration::leAudioConfig:
552 hidl_audio_config.leAudioConfig(to_hidl_leaudio_config_2_2(
553 audio_config.get<AudioConfiguration::leAudioConfig>()));
554 break;
555 }
556 return hidl_audio_config;
557}
558
559/***
560 *
561 * 2.0
562 *
563 ***/
564
565bool HidlToAidlMiddleware_2_0::IsSessionReady(
566 const SessionType_2_0& session_type) {
567 return BluetoothAudioSessionControl::IsSessionReady(
568 from_session_type_2_0(session_type));
569}
570
571uint16_t HidlToAidlMiddleware_2_0::RegisterControlResultCback(
572 const SessionType_2_0& session_type,
573 const PortStatusCallbacks_2_0& cbacks) {
574 PortStatusCallbacks_2_2 callback_2_2{
575 .control_result_cb_ = cbacks.control_result_cb_,
576 .session_changed_cb_ = cbacks.session_changed_cb_,
577 };
578 return HidlToAidlMiddleware_2_2::RegisterControlResultCback(
579 static_cast<SessionType_2_1>(session_type), callback_2_2);
580}
581
582void HidlToAidlMiddleware_2_0::UnregisterControlResultCback(
583 const SessionType_2_0& session_type, uint16_t cookie) {
584 HidlToAidlMiddleware_2_2::UnregisterControlResultCback(
585 static_cast<SessionType_2_1>(session_type), cookie);
586}
587
588const AudioConfig_2_0 HidlToAidlMiddleware_2_0::GetAudioConfig(
589 const SessionType_2_0& session_type) {
590 return to_hidl_audio_config_2_0(BluetoothAudioSessionControl::GetAudioConfig(
591 from_session_type_2_0(session_type)));
592}
593
594bool HidlToAidlMiddleware_2_0::StartStream(
595 const SessionType_2_0& session_type) {
596 return BluetoothAudioSessionControl::StartStream(
597 from_session_type_2_0(session_type));
598}
599
600void HidlToAidlMiddleware_2_0::StopStream(const SessionType_2_0& session_type) {
601 return BluetoothAudioSessionControl::StopStream(
602 from_session_type_2_0(session_type));
603}
604
605bool HidlToAidlMiddleware_2_0::SuspendStream(
606 const SessionType_2_0& session_type) {
607 return BluetoothAudioSessionControl::SuspendStream(
608 from_session_type_2_0(session_type));
609}
610
611bool HidlToAidlMiddleware_2_0::GetPresentationPosition(
612 const SessionType_2_0& session_type, uint64_t* remote_delay_report_ns,
613 uint64_t* total_bytes_readed, timespec* data_position) {
614 PresentationPosition presentation_position;
615 auto ret_val = BluetoothAudioSessionControl::GetPresentationPosition(
616 from_session_type_2_0(session_type), presentation_position);
617 if (remote_delay_report_ns)
618 *remote_delay_report_ns = presentation_position.remoteDeviceAudioDelayNanos;
619 if (total_bytes_readed)
620 *total_bytes_readed = presentation_position.transmittedOctets;
621 if (data_position)
622 *data_position = {
623 .tv_sec = static_cast<__kernel_old_time_t>(
624 presentation_position.transmittedOctetsTimestamp.tvSec),
625 .tv_nsec = static_cast<long>(
626 presentation_position.transmittedOctetsTimestamp.tvNSec)};
627 return ret_val;
628}
629
630void HidlToAidlMiddleware_2_0::UpdateTracksMetadata(
631 const SessionType_2_0& session_type,
632 const struct source_metadata* source_metadata) {
633 return BluetoothAudioSessionControl::UpdateSourceMetadata(
634 from_session_type_2_0(session_type), *source_metadata);
635}
636
637size_t HidlToAidlMiddleware_2_0::OutWritePcmData(
638 const SessionType_2_0& session_type, const void* buffer, size_t bytes) {
639 return BluetoothAudioSessionControl::OutWritePcmData(
640 from_session_type_2_0(session_type), buffer, bytes);
641}
642
Josh Wu9880f6b2022-01-21 20:49:49 -0800643size_t HidlToAidlMiddleware_2_0::InReadPcmData(
644 const SessionType_2_0& session_type, void* buffer, size_t bytes) {
645 return BluetoothAudioSessionControl::InReadPcmData(
646 from_session_type_2_0(session_type), buffer, bytes);
647}
648
Josh Wu20bac522021-12-29 23:52:39 -0800649bool HidlToAidlMiddleware_2_0::IsAidlAvailable() {
650 return BluetoothAudioSession::IsAidlAvailable();
651}
652
653/***
654 *
655 * 2.1
656 *
657 ***/
658
659const AudioConfig_2_1 HidlToAidlMiddleware_2_1::GetAudioConfig(
660 const SessionType_2_1& session_type) {
661 return to_hidl_audio_config_2_1(BluetoothAudioSessionControl::GetAudioConfig(
662 from_session_type_2_1(session_type)));
663}
664
665/***
666 *
667 * 2.2
668 *
669 ***/
670
671bool HidlToAidlMiddleware_2_2::IsSessionReady(
672 const SessionType_2_1& session_type) {
673 return BluetoothAudioSessionControl::IsSessionReady(
674 from_session_type_2_1(session_type));
675}
676
677uint16_t HidlToAidlMiddleware_2_2::RegisterControlResultCback(
678 const SessionType_2_1& session_type,
679 const PortStatusCallbacks_2_2& cbacks) {
680 LOG(INFO) << __func__ << ": " << toString(session_type);
681 auto aidl_session_type = from_session_type_2_1(session_type);
682 // Pass the exact reference to the lambda
683 auto& session_legacy_callback_table =
684 legacy_callback_table[aidl_session_type];
685 PortStatusCallbacks aidl_callbacks{};
686 if (cbacks.control_result_cb_) {
687 aidl_callbacks.control_result_cb_ =
688 [&session_legacy_callback_table](uint16_t cookie, bool start_resp,
689 const BluetoothAudioStatus& status) {
690 if (session_legacy_callback_table.find(cookie) ==
691 session_legacy_callback_table.end()) {
692 LOG(ERROR) << __func__ << ": Unknown callback invoked!";
693 return;
694 }
695 auto& cback = session_legacy_callback_table[cookie];
696 cback->control_result_cb_(cookie, start_resp, to_hidl_status(status));
697 };
698 }
699 if (cbacks.session_changed_cb_) {
700 aidl_callbacks.session_changed_cb_ =
701 [&session_legacy_callback_table](uint16_t cookie) {
702 if (session_legacy_callback_table.find(cookie) ==
703 session_legacy_callback_table.end()) {
704 LOG(ERROR) << __func__ << ": Unknown callback invoked!";
705 return;
706 }
707 auto& cback = session_legacy_callback_table[cookie];
708 cback->session_changed_cb_(cookie);
709 };
710 };
711 if (cbacks.audio_configuration_changed_cb_) {
712 aidl_callbacks.audio_configuration_changed_cb_ =
713 [&session_legacy_callback_table](uint16_t cookie) {
714 if (session_legacy_callback_table.find(cookie) ==
715 session_legacy_callback_table.end()) {
716 LOG(ERROR) << __func__ << ": Unknown callback invoked!";
717 return;
718 }
719 auto& cback = session_legacy_callback_table[cookie];
720 cback->audio_configuration_changed_cb_(cookie);
721 };
722 };
723 auto cookie = BluetoothAudioSessionControl::RegisterControlResultCback(
724 aidl_session_type, aidl_callbacks);
725 {
726 std::lock_guard<std::mutex> guard(legacy_callback_lock);
727 session_legacy_callback_table[cookie] =
728 std::make_shared<PortStatusCallbacks_2_2>(cbacks);
729 }
730 return cookie;
731}
732
733void HidlToAidlMiddleware_2_2::UnregisterControlResultCback(
734 const SessionType_2_1& session_type, uint16_t cookie) {
735 LOG(INFO) << __func__ << ": " << toString(session_type);
736 auto aidl_session_type = from_session_type_2_1(session_type);
737 BluetoothAudioSessionControl::UnregisterControlResultCback(aidl_session_type,
738 cookie);
739 auto& session_callback_table = legacy_callback_table[aidl_session_type];
740 if (session_callback_table.find(cookie) != session_callback_table.end()) {
741 std::lock_guard<std::mutex> guard(legacy_callback_lock);
742 session_callback_table.erase(cookie);
743 }
744}
745
746const AudioConfig_2_2 HidlToAidlMiddleware_2_2::GetAudioConfig(
747 const SessionType_2_1& session_type) {
748 return to_hidl_audio_config_2_2(BluetoothAudioSessionControl::GetAudioConfig(
749 from_session_type_2_1(session_type)));
750}
751
752bool HidlToAidlMiddleware_2_2::StartStream(
753 const SessionType_2_1& session_type) {
754 return BluetoothAudioSessionControl::StartStream(
755 from_session_type_2_1(session_type));
756}
757
758bool HidlToAidlMiddleware_2_2::SuspendStream(
759 const SessionType_2_1& session_type) {
760 return BluetoothAudioSessionControl::SuspendStream(
761 from_session_type_2_1(session_type));
762}
763
764void HidlToAidlMiddleware_2_2::StopStream(const SessionType_2_1& session_type) {
765 return BluetoothAudioSessionControl::StopStream(
766 from_session_type_2_1(session_type));
767}
768
Alice Kuodea3e802022-01-20 14:46:44 +0800769void HidlToAidlMiddleware_2_2::UpdateTracksMetadata(
770 const SessionType_2_1& session_type,
771 const struct source_metadata* source_metadata) {
772 return BluetoothAudioSessionControl::UpdateSourceMetadata(
773 from_session_type_2_1(session_type), *source_metadata);
774}
775
Josh Wu20bac522021-12-29 23:52:39 -0800776void HidlToAidlMiddleware_2_2::UpdateSinkMetadata(
777 const SessionType_2_1& session_type,
778 const struct sink_metadata* sink_metadata) {
779 return BluetoothAudioSessionControl::UpdateSinkMetadata(
780 from_session_type_2_1(session_type), *sink_metadata);
781}
782
783} // namespace audio
784} // namespace bluetooth
785} // namespace hardware
786} // namespace android
787} // namespace aidl