1 /*
2 * Copyright 2021 HIMSA II K/S - www.himsa.com. Represented by EHIMA -
3 * www.ehima.com
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #define LOG_TAG "BTAudioClientLeAudioStub"
19
20 #include "le_audio_software.h"
21
22 #include <bluetooth/log.h>
23 #include <com_android_bluetooth_flags.h>
24
25 #include <vector>
26
27 #include "aidl/android/hardware/bluetooth/audio/AudioContext.h"
28 #include "aidl/le_audio_software_aidl.h"
29 #include "aidl/le_audio_utils.h"
30 #include "bta/le_audio/codec_manager.h"
31 #include "bta/le_audio/le_audio_types.h"
32 #include "hal_version_manager.h"
33 #include "hidl/le_audio_software_hidl.h"
34 #include "os/log.h"
35 #include "osi/include/properties.h"
36
37 namespace bluetooth {
38 namespace audio {
39
40 using aidl::GetAidlLeAudioBroadcastConfigurationRequirementFromStackFormat;
41 using aidl::GetAidlLeAudioDeviceCapabilitiesFromStackFormat;
42 using aidl::GetAidlLeAudioUnicastConfigurationRequirementsFromStackFormat;
43 using aidl::GetStackBroadcastConfigurationFromAidlFormat;
44 using aidl::GetStackUnicastConfigurationFromAidlFormat;
45
46 namespace le_audio {
47
48 namespace {
49
50 using ::android::hardware::bluetooth::audio::V2_1::PcmParameters;
51 using AudioConfiguration_2_1 =
52 ::android::hardware::bluetooth::audio::V2_1::AudioConfiguration;
53 using AudioConfigurationAIDL =
54 ::aidl::android::hardware::bluetooth::audio::AudioConfiguration;
55 using ::aidl::android::hardware::bluetooth::audio::AudioContext;
56 using ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProvider;
57 using ::aidl::android::hardware::bluetooth::audio::LatencyMode;
58 using ::aidl::android::hardware::bluetooth::audio::LeAudioCodecConfiguration;
59
60 using ::bluetooth::le_audio::CodecManager;
61 using ::bluetooth::le_audio::set_configurations::AudioSetConfiguration;
62 using ::bluetooth::le_audio::types::CodecLocation;
63 } // namespace
64
get_offload_capabilities()65 OffloadCapabilities get_offload_capabilities() {
66 if (HalVersionManager::GetHalTransport() ==
67 BluetoothAudioHalTransport::HIDL) {
68 return {std::vector<AudioSetConfiguration>(0),
69 std::vector<AudioSetConfiguration>(0)};
70 }
71 return aidl::le_audio::get_offload_capabilities();
72 }
73
get_aidl_client_interface(bool is_broadcaster)74 aidl::BluetoothAudioSinkClientInterface* get_aidl_client_interface(
75 bool is_broadcaster) {
76 if (is_broadcaster)
77 return aidl::le_audio::LeAudioSinkTransport::interface_broadcast_;
78
79 return aidl::le_audio::LeAudioSinkTransport::interface_unicast_;
80 }
81
get_aidl_transport_instance(bool is_broadcaster)82 aidl::le_audio::LeAudioSinkTransport* get_aidl_transport_instance(
83 bool is_broadcaster) {
84 if (is_broadcaster)
85 return aidl::le_audio::LeAudioSinkTransport::instance_broadcast_;
86
87 return aidl::le_audio::LeAudioSinkTransport::instance_unicast_;
88 }
89
is_aidl_offload_encoding_session(bool is_broadcaster)90 bool is_aidl_offload_encoding_session(bool is_broadcaster) {
91 return get_aidl_client_interface(is_broadcaster)
92 ->GetTransportInstance()
93 ->GetSessionType() ==
94 aidl::SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
95 get_aidl_client_interface(is_broadcaster)
96 ->GetTransportInstance()
97 ->GetSessionType() ==
98 aidl::SessionType::
99 LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH;
100 }
101
102 LeAudioClientInterface* LeAudioClientInterface::interface = nullptr;
Get()103 LeAudioClientInterface* LeAudioClientInterface::Get() {
104 if (osi_property_get_bool(BLUETOOTH_AUDIO_HAL_PROP_DISABLED, false)) {
105 log::error("BluetoothAudio HAL is disabled");
106 return nullptr;
107 }
108
109 if (LeAudioClientInterface::interface == nullptr)
110 LeAudioClientInterface::interface = new LeAudioClientInterface();
111
112 return LeAudioClientInterface::interface;
113 }
114
Cleanup()115 void LeAudioClientInterface::Sink::Cleanup() {
116 log::info("HAL transport: 0x{:02x}, is broadcast: {}",
117 static_cast<int>(HalVersionManager::GetHalTransport()),
118 is_broadcaster_);
119
120 /* Cleanup transport interface and instance according to type and role */
121 if (HalVersionManager::GetHalTransport() ==
122 BluetoothAudioHalTransport::HIDL) {
123 if (hidl::le_audio::LeAudioSinkTransport::interface) {
124 delete hidl::le_audio::LeAudioSinkTransport::interface;
125 hidl::le_audio::LeAudioSinkTransport::interface = nullptr;
126 }
127 if (hidl::le_audio::LeAudioSinkTransport::instance) {
128 delete hidl::le_audio::LeAudioSinkTransport::instance;
129 hidl::le_audio::LeAudioSinkTransport::instance = nullptr;
130 }
131 } else if (HalVersionManager::GetHalTransport() ==
132 BluetoothAudioHalTransport::AIDL) {
133 if (IsBroadcaster()) {
134 if (aidl::le_audio::LeAudioSinkTransport::interface_broadcast_) {
135 delete aidl::le_audio::LeAudioSinkTransport::interface_broadcast_;
136 aidl::le_audio::LeAudioSinkTransport::interface_broadcast_ = nullptr;
137 }
138 if (aidl::le_audio::LeAudioSinkTransport::instance_broadcast_) {
139 delete aidl::le_audio::LeAudioSinkTransport::instance_broadcast_;
140 aidl::le_audio::LeAudioSinkTransport::instance_broadcast_ = nullptr;
141 }
142 } else {
143 if (aidl::le_audio::LeAudioSinkTransport::interface_unicast_) {
144 delete aidl::le_audio::LeAudioSinkTransport::interface_unicast_;
145 aidl::le_audio::LeAudioSinkTransport::interface_unicast_ = nullptr;
146 }
147 if (aidl::le_audio::LeAudioSinkTransport::instance_unicast_) {
148 delete aidl::le_audio::LeAudioSinkTransport::instance_unicast_;
149 aidl::le_audio::LeAudioSinkTransport::instance_unicast_ = nullptr;
150 }
151 }
152 } else {
153 log::error("Invalid HAL transport: 0x{:02x}",
154 static_cast<int>(HalVersionManager::GetHalTransport()));
155 }
156 }
157
SetPcmParameters(const PcmParameters & params)158 void LeAudioClientInterface::Sink::SetPcmParameters(
159 const PcmParameters& params) {
160 if (HalVersionManager::GetHalTransport() ==
161 BluetoothAudioHalTransport::HIDL) {
162 return hidl::le_audio::LeAudioSinkTransport::instance
163 ->LeAudioSetSelectedHalPcmConfig(
164 params.sample_rate, params.bits_per_sample, params.channels_count,
165 params.data_interval_us);
166 }
167 return get_aidl_transport_instance(is_broadcaster_)
168 ->LeAudioSetSelectedHalPcmConfig(
169 params.sample_rate, params.bits_per_sample, params.channels_count,
170 params.data_interval_us);
171 }
172
173 // Update Le Audio delay report to BluetoothAudio HAL
SetRemoteDelay(uint16_t delay_report_ms)174 void LeAudioClientInterface::Sink::SetRemoteDelay(uint16_t delay_report_ms) {
175 log::info("delay_report_ms={} ms", delay_report_ms);
176 if (HalVersionManager::GetHalTransport() ==
177 BluetoothAudioHalTransport::HIDL) {
178 hidl::le_audio::LeAudioSinkTransport::instance->SetRemoteDelay(
179 delay_report_ms);
180 return;
181 }
182 get_aidl_transport_instance(is_broadcaster_)->SetRemoteDelay(delay_report_ms);
183 }
184
StartSession()185 void LeAudioClientInterface::Sink::StartSession() {
186 log::info("");
187 if (HalVersionManager::GetHalVersion() ==
188 BluetoothAudioHalVersion::VERSION_2_1) {
189 AudioConfiguration_2_1 audio_config;
190 audio_config.pcmConfig(hidl::le_audio::LeAudioSinkTransport::instance
191 ->LeAudioGetSelectedHalPcmConfig());
192 if (!hidl::le_audio::LeAudioSinkTransport::interface->UpdateAudioConfig_2_1(
193 audio_config)) {
194 log::error("cannot update audio config to HAL");
195 return;
196 }
197 hidl::le_audio::LeAudioSinkTransport::interface->StartSession_2_1();
198 return;
199 } else if (HalVersionManager::GetHalTransport() ==
200 BluetoothAudioHalTransport::AIDL) {
201 AudioConfigurationAIDL audio_config;
202 if (is_aidl_offload_encoding_session(is_broadcaster_)) {
203 if (is_broadcaster_) {
204 audio_config.set<AudioConfigurationAIDL::leAudioBroadcastConfig>(
205 get_aidl_transport_instance(is_broadcaster_)
206 ->LeAudioGetBroadcastConfig());
207 } else {
208 aidl::le_audio::LeAudioConfiguration le_audio_config = {};
209 audio_config.set<AudioConfigurationAIDL::leAudioConfig>(
210 le_audio_config);
211 }
212 } else {
213 audio_config.set<AudioConfigurationAIDL::pcmConfig>(
214 get_aidl_transport_instance(is_broadcaster_)
215 ->LeAudioGetSelectedHalPcmConfig());
216 }
217 if (!get_aidl_client_interface(is_broadcaster_)
218 ->UpdateAudioConfig(audio_config)) {
219 log::error("cannot update audio config to HAL");
220 return;
221 }
222 get_aidl_client_interface(is_broadcaster_)->StartSession();
223 }
224 }
225
ConfirmStreamingRequest()226 void LeAudioClientInterface::Sink::ConfirmStreamingRequest() {
227 if (HalVersionManager::GetHalTransport() ==
228 BluetoothAudioHalTransport::HIDL) {
229 auto hidl_instance = hidl::le_audio::LeAudioSinkTransport::instance;
230 auto start_request_state = hidl_instance->GetStartRequestState();
231
232 switch (start_request_state) {
233 case StartRequestState::IDLE:
234 log::warn(", no pending start stream request");
235 return;
236 case StartRequestState::PENDING_BEFORE_RESUME:
237 log::info("Response before sending PENDING to audio HAL");
238 hidl_instance->SetStartRequestState(StartRequestState::CONFIRMED);
239 return;
240 case StartRequestState::PENDING_AFTER_RESUME:
241 log::info("Response after sending PENDING to audio HAL");
242 hidl_instance->ClearStartRequestState();
243 hidl::le_audio::LeAudioSinkTransport::interface->StreamStarted(
244 hidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
245 return;
246 case StartRequestState::CONFIRMED:
247 case StartRequestState::CANCELED:
248 log::error("Invalid state, start stream already confirmed");
249 return;
250 }
251 }
252
253 auto aidl_instance = get_aidl_transport_instance(is_broadcaster_);
254 auto start_request_state = aidl_instance->GetStartRequestState();
255 switch (start_request_state) {
256 case StartRequestState::IDLE:
257 log::warn(", no pending start stream request");
258 return;
259 case StartRequestState::PENDING_BEFORE_RESUME:
260 log::info("Response before sending PENDING to audio HAL");
261 aidl_instance->SetStartRequestState(StartRequestState::CONFIRMED);
262 return;
263 case StartRequestState::PENDING_AFTER_RESUME:
264 log::info("Response after sending PENDING to audio HAL");
265 aidl_instance->ClearStartRequestState();
266 get_aidl_client_interface(is_broadcaster_)
267 ->StreamStarted(aidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
268 return;
269 case StartRequestState::CONFIRMED:
270 case StartRequestState::CANCELED:
271 log::error("Invalid state, start stream already confirmed");
272 return;
273 }
274 }
275
ConfirmStreamingRequestV2()276 void LeAudioClientInterface::Sink::ConfirmStreamingRequestV2() {
277 auto lambda = [&](StartRequestState currect_start_request_state)
278 -> std::pair<StartRequestState, bool> {
279 switch (currect_start_request_state) {
280 case StartRequestState::IDLE:
281 log::warn(", no pending start stream request");
282 return std::make_pair(StartRequestState::IDLE, false);
283 case StartRequestState::PENDING_BEFORE_RESUME:
284 log::info("Response before sending PENDING to audio HAL");
285 return std::make_pair(StartRequestState::CONFIRMED, false);
286 case StartRequestState::PENDING_AFTER_RESUME:
287 log::info("Response after sending PENDING to audio HAL");
288 return std::make_pair(StartRequestState::IDLE, true);
289 case StartRequestState::CONFIRMED:
290 case StartRequestState::CANCELED:
291 log::error("Invalid state, start stream already confirmed");
292 return std::make_pair(currect_start_request_state, false);
293 }
294 };
295
296 if (HalVersionManager::GetHalTransport() ==
297 BluetoothAudioHalTransport::HIDL) {
298 auto hidl_instance = hidl::le_audio::LeAudioSinkTransport::instance;
299 if (hidl_instance->IsRequestCompletedAfterUpdate(lambda)) {
300 hidl::le_audio::LeAudioSinkTransport::interface->StreamStarted(
301 hidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
302 }
303
304 return;
305 }
306
307 auto aidl_instance = get_aidl_transport_instance(is_broadcaster_);
308 if (aidl_instance->IsRequestCompletedAfterUpdate(lambda)) {
309 get_aidl_client_interface(is_broadcaster_)
310 ->StreamStarted(aidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
311 }
312 }
313
CancelStreamingRequest()314 void LeAudioClientInterface::Sink::CancelStreamingRequest() {
315 if (HalVersionManager::GetHalTransport() ==
316 BluetoothAudioHalTransport::HIDL) {
317 auto hidl_instance = hidl::le_audio::LeAudioSinkTransport::instance;
318 auto start_request_state = hidl_instance->GetStartRequestState();
319 switch (start_request_state) {
320 case StartRequestState::IDLE:
321 log::warn(", no pending start stream request");
322 return;
323 case StartRequestState::PENDING_BEFORE_RESUME:
324 log::info("Response before sending PENDING to audio HAL");
325 hidl_instance->SetStartRequestState(StartRequestState::CANCELED);
326 return;
327 case StartRequestState::PENDING_AFTER_RESUME:
328 log::info("Response after sending PENDING to audio HAL");
329 hidl_instance->ClearStartRequestState();
330 hidl::le_audio::LeAudioSinkTransport::interface->StreamStarted(
331 hidl::BluetoothAudioCtrlAck::FAILURE);
332 return;
333 case StartRequestState::CONFIRMED:
334 case StartRequestState::CANCELED:
335 log::error("Invalid state, start stream already confirmed");
336 break;
337 }
338 }
339
340 auto aidl_instance = get_aidl_transport_instance(is_broadcaster_);
341 auto start_request_state = aidl_instance->GetStartRequestState();
342 switch (start_request_state) {
343 case StartRequestState::IDLE:
344 log::warn(", no pending start stream request");
345 return;
346 case StartRequestState::PENDING_BEFORE_RESUME:
347 log::info("Response before sending PENDING to audio HAL");
348 aidl_instance->SetStartRequestState(StartRequestState::CANCELED);
349 return;
350 case StartRequestState::PENDING_AFTER_RESUME:
351 log::info("Response after sending PENDING to audio HAL");
352 aidl_instance->ClearStartRequestState();
353 get_aidl_client_interface(is_broadcaster_)
354 ->StreamStarted(aidl::BluetoothAudioCtrlAck::FAILURE);
355 return;
356 case StartRequestState::CONFIRMED:
357 case StartRequestState::CANCELED:
358 log::error("Invalid state, start stream already confirmed");
359 break;
360 }
361 }
362
CancelStreamingRequestV2()363 void LeAudioClientInterface::Sink::CancelStreamingRequestV2() {
364 auto lambda = [&](StartRequestState currect_start_request_state)
365 -> std::pair<StartRequestState, bool> {
366 switch (currect_start_request_state) {
367 case StartRequestState::IDLE:
368 log::warn(", no pending start stream request");
369 return std::make_pair(StartRequestState::IDLE, false);
370 case StartRequestState::PENDING_BEFORE_RESUME:
371 log::info("Response before sending PENDING to audio HAL");
372 return std::make_pair(StartRequestState::CANCELED, false);
373 case StartRequestState::PENDING_AFTER_RESUME:
374 log::info("Response after sending PENDING to audio HAL");
375 return std::make_pair(StartRequestState::IDLE, true);
376 case StartRequestState::CONFIRMED:
377 case StartRequestState::CANCELED:
378 log::error("Invalid state, start stream already confirmed");
379 return std::make_pair(currect_start_request_state, false);
380 }
381 };
382
383 if (HalVersionManager::GetHalTransport() ==
384 BluetoothAudioHalTransport::HIDL) {
385 auto hidl_instance = hidl::le_audio::LeAudioSinkTransport::instance;
386 if (hidl_instance->IsRequestCompletedAfterUpdate(lambda)) {
387 hidl::le_audio::LeAudioSinkTransport::interface->StreamStarted(
388 hidl::BluetoothAudioCtrlAck::FAILURE);
389 }
390 return;
391 }
392
393 auto aidl_instance = get_aidl_transport_instance(is_broadcaster_);
394 if (aidl_instance->IsRequestCompletedAfterUpdate(lambda)) {
395 get_aidl_client_interface(is_broadcaster_)
396 ->StreamStarted(aidl::BluetoothAudioCtrlAck::FAILURE);
397 }
398 }
399
StopSession()400 void LeAudioClientInterface::Sink::StopSession() {
401 log::info("sink");
402 if (HalVersionManager::GetHalTransport() ==
403 BluetoothAudioHalTransport::HIDL) {
404 hidl::le_audio::LeAudioSinkTransport::instance->ClearStartRequestState();
405 hidl::le_audio::LeAudioSinkTransport::interface->EndSession();
406 return;
407 }
408 get_aidl_transport_instance(is_broadcaster_)->ClearStartRequestState();
409 get_aidl_client_interface(is_broadcaster_)->EndSession();
410 }
411
UpdateAudioConfigToHal(const::bluetooth::le_audio::offload_config & offload_config)412 void LeAudioClientInterface::Sink::UpdateAudioConfigToHal(
413 const ::bluetooth::le_audio::offload_config& offload_config) {
414 if (HalVersionManager::GetHalTransport() ==
415 BluetoothAudioHalTransport::HIDL) {
416 return;
417 }
418
419 if (is_broadcaster_ || !is_aidl_offload_encoding_session(is_broadcaster_)) {
420 return;
421 }
422
423 get_aidl_client_interface(is_broadcaster_)
424 ->UpdateAudioConfig(
425 aidl::le_audio::offload_config_to_hal_audio_config(offload_config));
426 }
427
428 std::optional<::bluetooth::le_audio::broadcaster::BroadcastConfiguration>
GetBroadcastConfig(const std::vector<std::pair<::bluetooth::le_audio::types::LeAudioContextType,uint8_t>> & subgroup_quality,const std::optional<std::vector<::bluetooth::le_audio::types::acs_ac_record>> & pacs) const429 LeAudioClientInterface::Sink::GetBroadcastConfig(
430 const std::vector<
431 std::pair<::bluetooth::le_audio::types::LeAudioContextType, uint8_t>>&
432 subgroup_quality,
433 const std::optional<
434 std::vector<::bluetooth::le_audio::types::acs_ac_record>>& pacs) const {
435 if (HalVersionManager::GetHalTransport() ==
436 BluetoothAudioHalTransport::HIDL) {
437 return std::nullopt;
438 }
439
440 if (!is_broadcaster_ || !is_aidl_offload_encoding_session(is_broadcaster_)) {
441 return std::nullopt;
442 }
443
444 auto aidl_pacs = GetAidlLeAudioDeviceCapabilitiesFromStackFormat(pacs);
445 auto reqs = GetAidlLeAudioBroadcastConfigurationRequirementFromStackFormat(
446 subgroup_quality);
447 auto aidl_broadcast_config =
448 aidl::le_audio::LeAudioSourceTransport::
449 interface->getLeAudioBroadcastConfiguration(aidl_pacs, reqs);
450
451 return GetStackBroadcastConfigurationFromAidlFormat(aidl_broadcast_config);
452 }
453
454 // This API is for requesting a single configuration.
455 // Note: We need a bulk API as well to get multiple configurations for caching
456 std::optional<::bluetooth::le_audio::set_configurations::AudioSetConfiguration>
GetUnicastConfig(const::bluetooth::le_audio::CodecManager::UnicastConfigurationRequirements & requirements) const457 LeAudioClientInterface::Sink::GetUnicastConfig(
458 const ::bluetooth::le_audio::CodecManager::UnicastConfigurationRequirements&
459 requirements) const {
460 log::debug("Requirements: {}", requirements);
461
462 auto aidl_sink_pacs =
463 GetAidlLeAudioDeviceCapabilitiesFromStackFormat(requirements.sink_pacs);
464
465 auto aidl_source_pacs =
466 GetAidlLeAudioDeviceCapabilitiesFromStackFormat(requirements.source_pacs);
467
468 std::vector<IBluetoothAudioProvider::LeAudioConfigurationRequirement> reqs;
469 reqs.push_back(GetAidlLeAudioUnicastConfigurationRequirementsFromStackFormat(
470 requirements.audio_context_type, requirements.sink_requirements,
471 requirements.source_requirements));
472
473 log::debug("Making an AIDL call");
474 auto aidl_configs =
475 get_aidl_client_interface(is_broadcaster_)
476 ->GetLeAudioAseConfiguration(aidl_sink_pacs, aidl_source_pacs, reqs);
477
478 log::debug("Received {} configs", aidl_configs.size());
479
480 if (aidl_configs.size() == 0) {
481 log::error("Expecting a single configuration, but received none.");
482 return std::nullopt;
483 }
484
485 /* Given a single requirement we should get a single response config
486 * Note: For a bulk request we need to implement GetUnicastConfigs() method
487 */
488 if (aidl_configs.size() > 1) {
489 log::warn("Expected a single configuration, but received {}",
490 aidl_configs.size());
491 }
492 return GetStackUnicastConfigurationFromAidlFormat(
493 requirements.audio_context_type, aidl_configs.at(0));
494 }
495
UpdateBroadcastAudioConfigToHal(const::bluetooth::le_audio::broadcast_offload_config & offload_config)496 void LeAudioClientInterface::Sink::UpdateBroadcastAudioConfigToHal(
497 const ::bluetooth::le_audio::broadcast_offload_config& offload_config) {
498 if (HalVersionManager::GetHalTransport() ==
499 BluetoothAudioHalTransport::HIDL) {
500 return;
501 }
502
503 if (!is_broadcaster_ || !is_aidl_offload_encoding_session(is_broadcaster_)) {
504 return;
505 }
506
507 get_aidl_transport_instance(is_broadcaster_)
508 ->LeAudioSetBroadcastConfig(offload_config);
509 }
510
SuspendedForReconfiguration()511 void LeAudioClientInterface::Sink::SuspendedForReconfiguration() {
512 if (HalVersionManager::GetHalTransport() ==
513 BluetoothAudioHalTransport::HIDL) {
514 hidl::le_audio::LeAudioSinkTransport::interface->StreamSuspended(
515 hidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
516 return;
517 }
518
519 get_aidl_client_interface(is_broadcaster_)
520 ->StreamSuspended(aidl::BluetoothAudioCtrlAck::SUCCESS_RECONFIGURATION);
521 }
522
ReconfigurationComplete()523 void LeAudioClientInterface::Sink::ReconfigurationComplete() {
524 // This is needed only for AIDL since SuspendedForReconfiguration()
525 // already calls StreamSuspended(SUCCESS_FINISHED) for HIDL
526 if (HalVersionManager::GetHalTransport() ==
527 BluetoothAudioHalTransport::AIDL) {
528 // FIXME: For now we have to workaround the missing API and use
529 // StreamSuspended() with SUCCESS_FINISHED ack code.
530 get_aidl_client_interface(is_broadcaster_)
531 ->StreamSuspended(aidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
532 }
533 }
534
Read(uint8_t * p_buf,uint32_t len)535 size_t LeAudioClientInterface::Sink::Read(uint8_t* p_buf, uint32_t len) {
536 if (HalVersionManager::GetHalTransport() ==
537 BluetoothAudioHalTransport::HIDL) {
538 return hidl::le_audio::LeAudioSinkTransport::interface->ReadAudioData(p_buf,
539 len);
540 }
541 return get_aidl_client_interface(is_broadcaster_)->ReadAudioData(p_buf, len);
542 }
543
Cleanup()544 void LeAudioClientInterface::Source::Cleanup() {
545 log::info("source");
546 if (hidl::le_audio::LeAudioSourceTransport::interface) {
547 delete hidl::le_audio::LeAudioSourceTransport::interface;
548 hidl::le_audio::LeAudioSourceTransport::interface = nullptr;
549 }
550 if (hidl::le_audio::LeAudioSourceTransport::instance) {
551 delete hidl::le_audio::LeAudioSourceTransport::instance;
552 hidl::le_audio::LeAudioSourceTransport::instance = nullptr;
553 }
554 if (aidl::le_audio::LeAudioSourceTransport::interface) {
555 delete aidl::le_audio::LeAudioSourceTransport::interface;
556 aidl::le_audio::LeAudioSourceTransport::interface = nullptr;
557 }
558 if (aidl::le_audio::LeAudioSourceTransport::instance) {
559 delete aidl::le_audio::LeAudioSourceTransport::instance;
560 aidl::le_audio::LeAudioSourceTransport::instance = nullptr;
561 }
562 }
563
SetPcmParameters(const PcmParameters & params)564 void LeAudioClientInterface::Source::SetPcmParameters(
565 const PcmParameters& params) {
566 if (HalVersionManager::GetHalTransport() ==
567 BluetoothAudioHalTransport::HIDL) {
568 hidl::le_audio::LeAudioSourceTransport::instance
569 ->LeAudioSetSelectedHalPcmConfig(
570 params.sample_rate, params.bits_per_sample, params.channels_count,
571 params.data_interval_us);
572 return;
573 }
574 return aidl::le_audio::LeAudioSourceTransport::instance
575 ->LeAudioSetSelectedHalPcmConfig(
576 params.sample_rate, params.bits_per_sample, params.channels_count,
577 params.data_interval_us);
578 }
579
SetRemoteDelay(uint16_t delay_report_ms)580 void LeAudioClientInterface::Source::SetRemoteDelay(uint16_t delay_report_ms) {
581 log::info("delay_report_ms={} ms", delay_report_ms);
582 if (HalVersionManager::GetHalTransport() ==
583 BluetoothAudioHalTransport::HIDL) {
584 hidl::le_audio::LeAudioSourceTransport::instance->SetRemoteDelay(
585 delay_report_ms);
586 return;
587 }
588 return aidl::le_audio::LeAudioSourceTransport::instance->SetRemoteDelay(
589 delay_report_ms);
590 }
591
StartSession()592 void LeAudioClientInterface::Source::StartSession() {
593 log::info("");
594 if (HalVersionManager::GetHalVersion() ==
595 BluetoothAudioHalVersion::VERSION_2_1) {
596 AudioConfiguration_2_1 audio_config;
597 audio_config.pcmConfig(hidl::le_audio::LeAudioSourceTransport::instance
598 ->LeAudioGetSelectedHalPcmConfig());
599 if (!hidl::le_audio::LeAudioSourceTransport::
600 interface->UpdateAudioConfig_2_1(audio_config)) {
601 log::error("cannot update audio config to HAL");
602 return;
603 }
604 hidl::le_audio::LeAudioSourceTransport::interface->StartSession_2_1();
605 return;
606 } else if (HalVersionManager::GetHalTransport() ==
607 BluetoothAudioHalTransport::AIDL) {
608 AudioConfigurationAIDL audio_config;
609 if (aidl::le_audio::LeAudioSourceTransport::
610 interface->GetTransportInstance()
611 ->GetSessionType() ==
612 aidl::SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
613 aidl::le_audio::LeAudioConfiguration le_audio_config;
614 audio_config.set<AudioConfigurationAIDL::leAudioConfig>(
615 aidl::le_audio::LeAudioConfiguration{});
616 } else {
617 audio_config.set<AudioConfigurationAIDL::pcmConfig>(
618 aidl::le_audio::LeAudioSourceTransport::instance
619 ->LeAudioGetSelectedHalPcmConfig());
620 }
621
622 if (!aidl::le_audio::LeAudioSourceTransport::interface->UpdateAudioConfig(
623 audio_config)) {
624 log::error("cannot update audio config to HAL");
625 return;
626 }
627 aidl::le_audio::LeAudioSourceTransport::interface->StartSession();
628 }
629 }
630
SuspendedForReconfiguration()631 void LeAudioClientInterface::Source::SuspendedForReconfiguration() {
632 if (HalVersionManager::GetHalTransport() ==
633 BluetoothAudioHalTransport::HIDL) {
634 hidl::le_audio::LeAudioSourceTransport::interface->StreamSuspended(
635 hidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
636 return;
637 }
638
639 aidl::le_audio::LeAudioSourceTransport::interface->StreamSuspended(
640 aidl::BluetoothAudioCtrlAck::SUCCESS_RECONFIGURATION);
641 }
642
ReconfigurationComplete()643 void LeAudioClientInterface::Source::ReconfigurationComplete() {
644 // This is needed only for AIDL since SuspendedForReconfiguration()
645 // already calls StreamSuspended(SUCCESS_FINISHED) for HIDL
646 if (HalVersionManager::GetHalTransport() ==
647 BluetoothAudioHalTransport::AIDL) {
648 // FIXME: For now we have to workaround the missing API and use
649 // StreamSuspended() with SUCCESS_FINISHED ack code.
650 aidl::le_audio::LeAudioSourceTransport::interface->StreamSuspended(
651 aidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
652 }
653 }
654
ConfirmStreamingRequest()655 void LeAudioClientInterface::Source::ConfirmStreamingRequest() {
656 if (HalVersionManager::GetHalTransport() ==
657 BluetoothAudioHalTransport::HIDL) {
658 auto hidl_instance = hidl::le_audio::LeAudioSourceTransport::instance;
659 auto start_request_state = hidl_instance->GetStartRequestState();
660
661 switch (start_request_state) {
662 case StartRequestState::IDLE:
663 log::warn(", no pending start stream request");
664 return;
665 case StartRequestState::PENDING_BEFORE_RESUME:
666 log::info("Response before sending PENDING to audio HAL");
667 hidl_instance->SetStartRequestState(StartRequestState::CONFIRMED);
668 return;
669 case StartRequestState::PENDING_AFTER_RESUME:
670 log::info("Response after sending PENDING to audio HAL");
671 hidl_instance->ClearStartRequestState();
672 hidl::le_audio::LeAudioSourceTransport::interface->StreamStarted(
673 hidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
674 return;
675 case StartRequestState::CONFIRMED:
676 case StartRequestState::CANCELED:
677 log::error("Invalid state, start stream already confirmed");
678 return;
679 }
680 }
681
682 auto aidl_instance = aidl::le_audio::LeAudioSourceTransport::instance;
683 auto start_request_state = aidl_instance->GetStartRequestState();
684 switch (start_request_state) {
685 case StartRequestState::IDLE:
686 log::warn(", no pending start stream request");
687 return;
688 case StartRequestState::PENDING_BEFORE_RESUME:
689 log::info("Response before sending PENDING to audio HAL");
690 aidl_instance->SetStartRequestState(StartRequestState::CONFIRMED);
691 return;
692 case StartRequestState::PENDING_AFTER_RESUME:
693 log::info("Response after sending PENDING to audio HAL");
694 aidl_instance->ClearStartRequestState();
695 aidl::le_audio::LeAudioSourceTransport::interface->StreamStarted(
696 aidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
697 return;
698 case StartRequestState::CONFIRMED:
699 case StartRequestState::CANCELED:
700 log::error("Invalid state, start stream already confirmed");
701 return;
702 }
703 }
704
ConfirmStreamingRequestV2()705 void LeAudioClientInterface::Source::ConfirmStreamingRequestV2() {
706 auto lambda = [&](StartRequestState currect_start_request_state)
707 -> std::pair<StartRequestState, bool> {
708 switch (currect_start_request_state) {
709 case StartRequestState::IDLE:
710 log::warn(", no pending start stream request");
711 return std::make_pair(StartRequestState::IDLE, false);
712 case StartRequestState::PENDING_BEFORE_RESUME:
713 log::info("Response before sending PENDING to audio HAL");
714 return std::make_pair(StartRequestState::CONFIRMED, false);
715 case StartRequestState::PENDING_AFTER_RESUME:
716 log::info("Response after sending PENDING to audio HAL");
717 return std::make_pair(StartRequestState::IDLE, true);
718 case StartRequestState::CONFIRMED:
719 case StartRequestState::CANCELED:
720 log::error("Invalid state, start stream already confirmed");
721 return std::make_pair(currect_start_request_state, false);
722 }
723 };
724
725 if (HalVersionManager::GetHalTransport() ==
726 BluetoothAudioHalTransport::HIDL) {
727 auto hidl_instance = hidl::le_audio::LeAudioSourceTransport::instance;
728
729 if (hidl_instance->IsRequestCompletedAfterUpdate(lambda)) {
730 hidl::le_audio::LeAudioSourceTransport::interface->StreamStarted(
731 hidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
732 }
733 return;
734 }
735
736 auto aidl_instance = aidl::le_audio::LeAudioSourceTransport::instance;
737 if (aidl_instance->IsRequestCompletedAfterUpdate(lambda)) {
738 aidl::le_audio::LeAudioSourceTransport::interface->StreamStarted(
739 aidl::BluetoothAudioCtrlAck::SUCCESS_FINISHED);
740 }
741 }
742
CancelStreamingRequest()743 void LeAudioClientInterface::Source::CancelStreamingRequest() {
744 if (HalVersionManager::GetHalTransport() ==
745 BluetoothAudioHalTransport::HIDL) {
746 auto hidl_instance = hidl::le_audio::LeAudioSourceTransport::instance;
747 auto start_request_state = hidl_instance->GetStartRequestState();
748 switch (start_request_state) {
749 case StartRequestState::IDLE:
750 log::warn(", no pending start stream request");
751 return;
752 case StartRequestState::PENDING_BEFORE_RESUME:
753 log::info("Response before sending PENDING to audio HAL");
754 hidl_instance->SetStartRequestState(StartRequestState::CANCELED);
755 return;
756 case StartRequestState::PENDING_AFTER_RESUME:
757 log::info("Response after sending PENDING to audio HAL");
758 hidl_instance->ClearStartRequestState();
759 hidl::le_audio::LeAudioSourceTransport::interface->StreamStarted(
760 hidl::BluetoothAudioCtrlAck::FAILURE);
761 return;
762 case StartRequestState::CONFIRMED:
763 case StartRequestState::CANCELED:
764 log::error("Invalid state, start stream already confirmed");
765 break;
766 }
767 }
768
769 auto aidl_instance = aidl::le_audio::LeAudioSourceTransport::instance;
770 auto start_request_state = aidl_instance->GetStartRequestState();
771 switch (start_request_state) {
772 case StartRequestState::IDLE:
773 log::warn(", no pending start stream request");
774 return;
775 case StartRequestState::PENDING_BEFORE_RESUME:
776 log::info("Response before sending PENDING to audio HAL");
777 aidl_instance->SetStartRequestState(StartRequestState::CANCELED);
778 return;
779 case StartRequestState::PENDING_AFTER_RESUME:
780 log::info("Response after sending PENDING to audio HAL");
781 aidl_instance->ClearStartRequestState();
782 aidl::le_audio::LeAudioSourceTransport::interface->StreamStarted(
783 aidl::BluetoothAudioCtrlAck::FAILURE);
784 return;
785 case StartRequestState::CONFIRMED:
786 case StartRequestState::CANCELED:
787 log::error("Invalid state, start stream already confirmed");
788 break;
789 }
790 }
791
CancelStreamingRequestV2()792 void LeAudioClientInterface::Source::CancelStreamingRequestV2() {
793 auto lambda = [&](StartRequestState currect_start_request_state)
794 -> std::pair<StartRequestState, bool> {
795 switch (currect_start_request_state) {
796 case StartRequestState::IDLE:
797 log::warn(", no pending start stream request");
798 return std::make_pair(StartRequestState::IDLE, false);
799 case StartRequestState::PENDING_BEFORE_RESUME:
800 log::info("Response before sending PENDING to audio HAL");
801 return std::make_pair(StartRequestState::CANCELED, false);
802 case StartRequestState::PENDING_AFTER_RESUME:
803 log::info("Response after sending PENDING to audio HAL");
804 return std::make_pair(StartRequestState::IDLE, true);
805 case StartRequestState::CONFIRMED:
806 case StartRequestState::CANCELED:
807 log::error("Invalid state, start stream already confirmed");
808 return std::make_pair(currect_start_request_state, false);
809 }
810 };
811
812 if (HalVersionManager::GetHalTransport() ==
813 BluetoothAudioHalTransport::HIDL) {
814 auto hidl_instance = hidl::le_audio::LeAudioSourceTransport::instance;
815 if (hidl_instance->IsRequestCompletedAfterUpdate(lambda)) {
816 hidl::le_audio::LeAudioSourceTransport::interface->StreamStarted(
817 hidl::BluetoothAudioCtrlAck::FAILURE);
818 }
819 return;
820 }
821
822 auto aidl_instance = aidl::le_audio::LeAudioSourceTransport::instance;
823 if (aidl_instance->IsRequestCompletedAfterUpdate(lambda)) {
824 aidl::le_audio::LeAudioSourceTransport::interface->StreamStarted(
825 aidl::BluetoothAudioCtrlAck::FAILURE);
826 }
827 }
828
StopSession()829 void LeAudioClientInterface::Source::StopSession() {
830 log::info("source");
831 if (HalVersionManager::GetHalTransport() ==
832 BluetoothAudioHalTransport::HIDL) {
833 hidl::le_audio::LeAudioSourceTransport::instance->ClearStartRequestState();
834 hidl::le_audio::LeAudioSourceTransport::interface->EndSession();
835 return;
836 }
837 aidl::le_audio::LeAudioSourceTransport::instance->ClearStartRequestState();
838 aidl::le_audio::LeAudioSourceTransport::interface->EndSession();
839 }
840
UpdateAudioConfigToHal(const::bluetooth::le_audio::offload_config & offload_config)841 void LeAudioClientInterface::Source::UpdateAudioConfigToHal(
842 const ::bluetooth::le_audio::offload_config& offload_config) {
843 if (HalVersionManager::GetHalTransport() ==
844 BluetoothAudioHalTransport::HIDL) {
845 return;
846 }
847
848 if (aidl::le_audio::LeAudioSourceTransport::interface->GetTransportInstance()
849 ->GetSessionType() !=
850 aidl::SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
851 return;
852 }
853 aidl::le_audio::LeAudioSourceTransport::interface->UpdateAudioConfig(
854 aidl::le_audio::offload_config_to_hal_audio_config(offload_config));
855 }
856
Write(const uint8_t * p_buf,uint32_t len)857 size_t LeAudioClientInterface::Source::Write(const uint8_t* p_buf,
858 uint32_t len) {
859 if (HalVersionManager::GetHalTransport() ==
860 BluetoothAudioHalTransport::HIDL) {
861 return hidl::le_audio::LeAudioSourceTransport::interface->WriteAudioData(
862 p_buf, len);
863 }
864 return aidl::le_audio::LeAudioSourceTransport::interface->WriteAudioData(
865 p_buf, len);
866 }
867
GetSink(StreamCallbacks stream_cb,bluetooth::common::MessageLoopThread * message_loop,bool is_broadcasting_session_type)868 LeAudioClientInterface::Sink* LeAudioClientInterface::GetSink(
869 StreamCallbacks stream_cb,
870 bluetooth::common::MessageLoopThread* message_loop,
871 bool is_broadcasting_session_type) {
872 if (is_broadcasting_session_type && HalVersionManager::GetHalTransport() ==
873 BluetoothAudioHalTransport::HIDL) {
874 log::warn("No support for broadcasting Le Audio on HIDL");
875 return nullptr;
876 }
877
878 auto& sink = is_broadcasting_session_type ? broadcast_sink_ : unicast_sink_;
879 if (sink == nullptr) {
880 sink = new Sink(is_broadcasting_session_type);
881 } else {
882 log::warn("Sink is already acquired");
883 return nullptr;
884 }
885
886 log::info("");
887
888 if (HalVersionManager::GetHalTransport() ==
889 BluetoothAudioHalTransport::HIDL) {
890 hidl::SessionType_2_1 session_type =
891 hidl::SessionType_2_1::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH;
892
893 hidl::le_audio::LeAudioSinkTransport::instance =
894 new hidl::le_audio::LeAudioSinkTransport(session_type,
895 std::move(stream_cb));
896 hidl::le_audio::LeAudioSinkTransport::interface =
897 new hidl::BluetoothAudioSinkClientInterface(
898 hidl::le_audio::LeAudioSinkTransport::instance, message_loop);
899 if (!hidl::le_audio::LeAudioSinkTransport::interface->IsValid()) {
900 log::warn("BluetoothAudio HAL for Le Audio is invalid?!");
901 delete hidl::le_audio::LeAudioSinkTransport::interface;
902 hidl::le_audio::LeAudioSinkTransport::interface = nullptr;
903 delete hidl::le_audio::LeAudioSinkTransport::instance;
904 hidl::le_audio::LeAudioSinkTransport::instance = nullptr;
905 delete sink;
906 sink = nullptr;
907
908 return nullptr;
909 }
910 } else {
911 aidl::SessionType session_type =
912 is_broadcasting_session_type
913 ? aidl::SessionType::LE_AUDIO_BROADCAST_SOFTWARE_ENCODING_DATAPATH
914 : aidl::SessionType::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH;
915 if (CodecManager::GetInstance()->GetCodecLocation() !=
916 CodecLocation::HOST) {
917 session_type =
918 is_broadcasting_session_type
919 ? aidl::SessionType::
920 LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH
921 : aidl::SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH;
922 }
923
924 if (session_type ==
925 aidl::SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
926 session_type ==
927 aidl::SessionType::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH) {
928 aidl::le_audio::LeAudioSinkTransport::instance_unicast_ =
929 new aidl::le_audio::LeAudioSinkTransport(session_type,
930 std::move(stream_cb));
931 aidl::le_audio::LeAudioSinkTransport::interface_unicast_ =
932 new aidl::BluetoothAudioSinkClientInterface(
933 aidl::le_audio::LeAudioSinkTransport::instance_unicast_);
934 if (!aidl::le_audio::LeAudioSinkTransport::interface_unicast_
935 ->IsValid()) {
936 log::warn("BluetoothAudio HAL for Le Audio is invalid?!");
937 delete aidl::le_audio::LeAudioSinkTransport::interface_unicast_;
938 aidl::le_audio::LeAudioSinkTransport::interface_unicast_ = nullptr;
939 delete aidl::le_audio::LeAudioSinkTransport::instance_unicast_;
940 aidl::le_audio::LeAudioSinkTransport::instance_unicast_ = nullptr;
941 delete sink;
942 sink = nullptr;
943
944 return nullptr;
945 }
946 } else {
947 aidl::le_audio::LeAudioSinkTransport::instance_broadcast_ =
948 new aidl::le_audio::LeAudioSinkTransport(session_type,
949 std::move(stream_cb));
950 aidl::le_audio::LeAudioSinkTransport::interface_broadcast_ =
951 new aidl::BluetoothAudioSinkClientInterface(
952 aidl::le_audio::LeAudioSinkTransport::instance_broadcast_);
953 if (!aidl::le_audio::LeAudioSinkTransport::interface_broadcast_
954 ->IsValid()) {
955 log::warn("BluetoothAudio HAL for Le Audio is invalid?!");
956 delete aidl::le_audio::LeAudioSinkTransport::interface_broadcast_;
957 aidl::le_audio::LeAudioSinkTransport::interface_broadcast_ = nullptr;
958 delete aidl::le_audio::LeAudioSinkTransport::instance_broadcast_;
959 aidl::le_audio::LeAudioSinkTransport::instance_broadcast_ = nullptr;
960 delete sink;
961 sink = nullptr;
962
963 return nullptr;
964 }
965 }
966 }
967
968 return sink;
969 }
970
IsUnicastSinkAcquired()971 bool LeAudioClientInterface::IsUnicastSinkAcquired() {
972 return unicast_sink_ != nullptr;
973 }
IsBroadcastSinkAcquired()974 bool LeAudioClientInterface::IsBroadcastSinkAcquired() {
975 return broadcast_sink_ != nullptr;
976 }
977
ReleaseSink(LeAudioClientInterface::Sink * sink)978 bool LeAudioClientInterface::ReleaseSink(LeAudioClientInterface::Sink* sink) {
979 if (sink != unicast_sink_ && sink != broadcast_sink_) {
980 log::warn("can't release not acquired sink");
981 return false;
982 }
983
984 if ((hidl::le_audio::LeAudioSinkTransport::interface &&
985 hidl::le_audio::LeAudioSinkTransport::instance) ||
986 (aidl::le_audio::LeAudioSinkTransport::interface_unicast_ &&
987 aidl::le_audio::LeAudioSinkTransport::instance_unicast_) ||
988 (aidl::le_audio::LeAudioSinkTransport::interface_broadcast_ &&
989 aidl::le_audio::LeAudioSinkTransport::instance_broadcast_))
990 sink->Cleanup();
991
992 if (sink == unicast_sink_) {
993 delete (unicast_sink_);
994 unicast_sink_ = nullptr;
995 } else if (sink == broadcast_sink_) {
996 delete (broadcast_sink_);
997 broadcast_sink_ = nullptr;
998 }
999
1000 return true;
1001 }
1002
GetSource(StreamCallbacks stream_cb,bluetooth::common::MessageLoopThread * message_loop)1003 LeAudioClientInterface::Source* LeAudioClientInterface::GetSource(
1004 StreamCallbacks stream_cb,
1005 bluetooth::common::MessageLoopThread* message_loop) {
1006 if (source_ == nullptr) {
1007 source_ = new Source();
1008 } else {
1009 log::warn("Source is already acquired");
1010 return nullptr;
1011 }
1012
1013 log::info("");
1014
1015 if (HalVersionManager::GetHalTransport() ==
1016 BluetoothAudioHalTransport::HIDL) {
1017 hidl::SessionType_2_1 session_type =
1018 hidl::SessionType_2_1::LE_AUDIO_SOFTWARE_DECODED_DATAPATH;
1019 if (CodecManager::GetInstance()->GetCodecLocation() !=
1020 CodecLocation::HOST) {
1021 session_type =
1022 hidl::SessionType_2_1::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH;
1023 }
1024
1025 hidl::le_audio::LeAudioSourceTransport::instance =
1026 new hidl::le_audio::LeAudioSourceTransport(session_type,
1027 std::move(stream_cb));
1028 hidl::le_audio::LeAudioSourceTransport::interface =
1029 new hidl::BluetoothAudioSourceClientInterface(
1030 hidl::le_audio::LeAudioSourceTransport::instance, message_loop);
1031 if (!hidl::le_audio::LeAudioSourceTransport::interface->IsValid()) {
1032 log::warn("BluetoothAudio HAL for Le Audio is invalid?!");
1033 delete hidl::le_audio::LeAudioSourceTransport::interface;
1034 hidl::le_audio::LeAudioSourceTransport::interface = nullptr;
1035 delete hidl::le_audio::LeAudioSourceTransport::instance;
1036 hidl::le_audio::LeAudioSourceTransport::instance = nullptr;
1037 delete source_;
1038 source_ = nullptr;
1039
1040 return nullptr;
1041 }
1042 } else {
1043 aidl::SessionType session_type =
1044 aidl::SessionType::LE_AUDIO_SOFTWARE_DECODING_DATAPATH;
1045 if (CodecManager::GetInstance()->GetCodecLocation() !=
1046 CodecLocation::HOST) {
1047 session_type =
1048 aidl::SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH;
1049 }
1050
1051 aidl::le_audio::LeAudioSourceTransport::instance =
1052 new aidl::le_audio::LeAudioSourceTransport(session_type,
1053 std::move(stream_cb));
1054 aidl::le_audio::LeAudioSourceTransport::interface =
1055 new aidl::BluetoothAudioSourceClientInterface(
1056 aidl::le_audio::LeAudioSourceTransport::instance);
1057 if (!aidl::le_audio::LeAudioSourceTransport::interface->IsValid()) {
1058 log::warn("BluetoothAudio HAL for Le Audio is invalid?!");
1059 delete aidl::le_audio::LeAudioSourceTransport::interface;
1060 aidl::le_audio::LeAudioSourceTransport::interface = nullptr;
1061 delete aidl::le_audio::LeAudioSourceTransport::instance;
1062 aidl::le_audio::LeAudioSourceTransport::instance = nullptr;
1063 delete source_;
1064 source_ = nullptr;
1065
1066 return nullptr;
1067 }
1068 }
1069
1070 return source_;
1071 }
1072
IsSourceAcquired()1073 bool LeAudioClientInterface::IsSourceAcquired() { return source_ != nullptr; }
1074
ReleaseSource(LeAudioClientInterface::Source * source)1075 bool LeAudioClientInterface::ReleaseSource(
1076 LeAudioClientInterface::Source* source) {
1077 if (source != source_) {
1078 log::warn("can't release not acquired source");
1079 return false;
1080 }
1081
1082 if ((hidl::le_audio::LeAudioSourceTransport::interface &&
1083 hidl::le_audio::LeAudioSourceTransport::instance) ||
1084 (aidl::le_audio::LeAudioSourceTransport::interface &&
1085 aidl::le_audio::LeAudioSourceTransport::instance))
1086 source->Cleanup();
1087
1088 delete (source_);
1089 source_ = nullptr;
1090
1091 return true;
1092 }
1093
SetAllowedDsaModes(DsaModes dsa_modes)1094 void LeAudioClientInterface::SetAllowedDsaModes(DsaModes dsa_modes) {
1095 if (!com::android::bluetooth::flags::leaudio_dynamic_spatial_audio()) {
1096 return;
1097 }
1098
1099 if (HalVersionManager::GetHalTransport() ==
1100 BluetoothAudioHalTransport::AIDL) {
1101 if (aidl::le_audio::LeAudioSinkTransport::interface_unicast_ == nullptr ||
1102 aidl::le_audio::LeAudioSinkTransport::instance_unicast_ == nullptr) {
1103 log::warn("LeAudioSourceTransport::interface is null");
1104 return;
1105 }
1106
1107 std::vector<LatencyMode> latency_modes = {LatencyMode::FREE};
1108 for (auto dsa_mode : dsa_modes) {
1109 switch (dsa_mode) {
1110 case DsaMode::DISABLED:
1111 // Already added
1112 break;
1113 case DsaMode::ACL:
1114 latency_modes.push_back(LatencyMode::LOW_LATENCY);
1115 break;
1116 case DsaMode::ISO_SW:
1117 latency_modes.push_back(LatencyMode::DYNAMIC_SPATIAL_AUDIO_SOFTWARE);
1118 break;
1119 case DsaMode::ISO_HW:
1120 latency_modes.push_back(LatencyMode::DYNAMIC_SPATIAL_AUDIO_HARDWARE);
1121 break;
1122 default:
1123 log::warn("Unsupported latency mode ignored: {}", (int)dsa_mode);
1124 break;
1125 }
1126 }
1127 aidl::le_audio::LeAudioSinkTransport::interface_unicast_
1128 ->SetAllowedLatencyModes(latency_modes);
1129 }
1130 }
1131
1132 } // namespace le_audio
1133 } // namespace audio
1134 } // namespace bluetooth
1135