blob: 86f56ae011ed87feda5044db998ef1a06890dc48 [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#include <sys/types.h>
18#define LOG_TAG "BTAudioSessionAidl"
19
20#include <android-base/logging.h>
21#include <android-base/stringprintf.h>
22#include <android/binder_manager.h>
23
24#include "BluetoothAudioSession.h"
25
26namespace aidl {
27namespace android {
28namespace hardware {
29namespace bluetooth {
30namespace audio {
31
32static constexpr int kFmqSendTimeoutMs = 1000; // 1000 ms timeout for sending
33static constexpr int kFmqReceiveTimeoutMs =
34 1000; // 1000 ms timeout for receiving
35static constexpr int kWritePollMs = 1; // polled non-blocking interval
36static constexpr int kReadPollMs = 1; // polled non-blocking interval
37
Josh Wu20bac522021-12-29 23:52:39 -080038BluetoothAudioSession::BluetoothAudioSession(const SessionType& session_type)
Josh Wu75462aa2022-01-21 21:51:21 -080039 : session_type_(session_type), stack_iface_(nullptr), data_mq_(nullptr) {}
Josh Wu20bac522021-12-29 23:52:39 -080040
41/***
42 *
43 * Callback methods
44 *
45 ***/
46
47void BluetoothAudioSession::OnSessionStarted(
48 const std::shared_ptr<IBluetoothAudioPort> stack_iface,
Cheney Ni6ecbc762022-03-03 00:12:48 +080049 const DataMQDesc* mq_desc, const AudioConfiguration& audio_config,
50 const std::vector<LatencyMode>& latency_modes) {
Josh Wu20bac522021-12-29 23:52:39 -080051 std::lock_guard<std::recursive_mutex> guard(mutex_);
52 if (stack_iface == nullptr) {
53 LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_)
54 << ", IBluetoothAudioPort Invalid";
55 } else if (!UpdateAudioConfig(audio_config)) {
56 LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_)
57 << ", AudioConfiguration=" << audio_config.toString()
58 << " Invalid";
59 } else if (!UpdateDataPath(mq_desc)) {
60 LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_)
61 << " MqDescriptor Invalid";
Josh Wu75462aa2022-01-21 21:51:21 -080062 audio_config_ = nullptr;
Josh Wu20bac522021-12-29 23:52:39 -080063 } else {
64 stack_iface_ = stack_iface;
Cheney Ni6ecbc762022-03-03 00:12:48 +080065 latency_modes_ = latency_modes;
Josh Wu20bac522021-12-29 23:52:39 -080066 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
67 << ", AudioConfiguration=" << audio_config.toString();
68 ReportSessionStatus();
69 }
70}
71
72void BluetoothAudioSession::OnSessionEnded() {
73 std::lock_guard<std::recursive_mutex> guard(mutex_);
74 bool toggled = IsSessionReady();
75 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_);
Josh Wu75462aa2022-01-21 21:51:21 -080076 audio_config_ = nullptr;
Josh Wu20bac522021-12-29 23:52:39 -080077 stack_iface_ = nullptr;
78 UpdateDataPath(nullptr);
79 if (toggled) {
80 ReportSessionStatus();
81 }
82}
83
84/***
85 *
86 * Util methods
87 *
88 ***/
89
Josh Wu75462aa2022-01-21 21:51:21 -080090const AudioConfiguration BluetoothAudioSession::GetAudioConfig() {
Josh Wu20bac522021-12-29 23:52:39 -080091 std::lock_guard<std::recursive_mutex> guard(mutex_);
92 if (!IsSessionReady()) {
Josh Wu20bac522021-12-29 23:52:39 -080093 switch (session_type_) {
94 case SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
Alice Kuoadcceec2022-03-28 13:28:43 +080095 case SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH:
Josh Wu75462aa2022-01-21 21:51:21 -080096 return AudioConfiguration(CodecConfiguration{});
Josh Wu20bac522021-12-29 23:52:39 -080097 case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
98 case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH:
Josh Wu75462aa2022-01-21 21:51:21 -080099 return AudioConfiguration(LeAudioConfiguration{});
Alice Kuoe80a5762022-02-09 14:44:29 +0800100 case SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH:
101 return AudioConfiguration(LeAudioBroadcastConfiguration{});
Josh Wu20bac522021-12-29 23:52:39 -0800102 default:
Josh Wu75462aa2022-01-21 21:51:21 -0800103 return AudioConfiguration(PcmConfiguration{});
Josh Wu20bac522021-12-29 23:52:39 -0800104 }
105 }
106 return *audio_config_;
107}
108
109void BluetoothAudioSession::ReportAudioConfigChanged(
110 const AudioConfiguration& audio_config) {
111 if (session_type_ !=
112 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH &&
113 session_type_ !=
Alice Kuo36a87972022-07-08 00:32:04 +0800114 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH &&
115 session_type_ !=
116 SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH) {
Josh Wu20bac522021-12-29 23:52:39 -0800117 return;
118 }
119 std::lock_guard<std::recursive_mutex> guard(mutex_);
120 audio_config_ = std::make_unique<AudioConfiguration>(audio_config);
121 if (observers_.empty()) {
122 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
123 << " has NO port state observer";
124 return;
125 }
126 for (auto& observer : observers_) {
127 uint16_t cookie = observer.first;
128 std::shared_ptr<struct PortStatusCallbacks> cb = observer.second;
129 LOG(INFO) << __func__ << " for SessionType=" << toString(session_type_)
130 << ", bluetooth_audio=0x"
131 << ::android::base::StringPrintf("%04x", cookie);
132 if (cb->audio_configuration_changed_cb_ != nullptr) {
133 cb->audio_configuration_changed_cb_(cookie);
134 }
135 }
136}
137
138bool BluetoothAudioSession::IsSessionReady() {
139 std::lock_guard<std::recursive_mutex> guard(mutex_);
140
141 bool is_mq_valid =
142 (session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
143 session_type_ ==
144 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
145 session_type_ ==
146 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH ||
Alice Kuoe80a5762022-02-09 14:44:29 +0800147 session_type_ ==
148 SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
Alice Kuoadcceec2022-03-28 13:28:43 +0800149 session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH ||
Josh Wu20bac522021-12-29 23:52:39 -0800150 (data_mq_ != nullptr && data_mq_->isValid()));
Josh Wu75462aa2022-01-21 21:51:21 -0800151 return stack_iface_ != nullptr && is_mq_valid && audio_config_ != nullptr;
Josh Wu20bac522021-12-29 23:52:39 -0800152}
153
154/***
155 *
156 * Status callback methods
157 *
158 ***/
159
160uint16_t BluetoothAudioSession::RegisterStatusCback(
161 const PortStatusCallbacks& callbacks) {
162 std::lock_guard<std::recursive_mutex> guard(mutex_);
163 uint16_t cookie = ObserversCookieGetInitValue(session_type_);
164 uint16_t cookie_upper_bound = ObserversCookieGetUpperBound(session_type_);
165
166 while (cookie < cookie_upper_bound) {
167 if (observers_.find(cookie) == observers_.end()) {
168 break;
169 }
170 ++cookie;
171 }
172 if (cookie >= cookie_upper_bound) {
173 LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_)
174 << " has " << observers_.size()
175 << " observers already (No Resource)";
176 return kObserversCookieUndefined;
177 }
178 std::shared_ptr<PortStatusCallbacks> cb =
179 std::make_shared<PortStatusCallbacks>();
180 *cb = callbacks;
181 observers_[cookie] = cb;
182 return cookie;
183}
184
185void BluetoothAudioSession::UnregisterStatusCback(uint16_t cookie) {
186 std::lock_guard<std::recursive_mutex> guard(mutex_);
187 if (observers_.erase(cookie) != 1) {
188 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
189 << " no such provider=0x"
190 << ::android::base::StringPrintf("%04x", cookie);
191 }
192}
193
194/***
195 *
196 * Stream methods
197 *
198 ***/
199
Cheney Ni6ecbc762022-03-03 00:12:48 +0800200bool BluetoothAudioSession::StartStream(bool is_low_latency) {
Josh Wu20bac522021-12-29 23:52:39 -0800201 std::lock_guard<std::recursive_mutex> guard(mutex_);
202 if (!IsSessionReady()) {
203 LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_)
204 << " has NO session";
205 return false;
206 }
Cheney Ni6ecbc762022-03-03 00:12:48 +0800207 auto hal_retval = stack_iface_->startStream(is_low_latency);
Josh Wu20bac522021-12-29 23:52:39 -0800208 if (!hal_retval.isOk()) {
209 LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
210 << toString(session_type_) << " failed";
211 return false;
212 }
213 return true;
214}
215
216bool BluetoothAudioSession::SuspendStream() {
217 std::lock_guard<std::recursive_mutex> guard(mutex_);
218 if (!IsSessionReady()) {
219 LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_)
220 << " has NO session";
221 return false;
222 }
223 auto hal_retval = stack_iface_->suspendStream();
224 if (!hal_retval.isOk()) {
225 LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
226 << toString(session_type_) << " failed";
227 return false;
228 }
229 return true;
230}
231
232void BluetoothAudioSession::StopStream() {
233 std::lock_guard<std::recursive_mutex> guard(mutex_);
234 if (!IsSessionReady()) {
235 return;
236 }
237 auto hal_retval = stack_iface_->stopStream();
238 if (!hal_retval.isOk()) {
239 LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
240 << toString(session_type_) << " failed";
241 }
242}
243
244/***
245 *
246 * Private methods
247 *
248 ***/
249
250bool BluetoothAudioSession::UpdateDataPath(const DataMQDesc* mq_desc) {
251 if (mq_desc == nullptr) {
252 // usecase of reset by nullptr
253 data_mq_ = nullptr;
254 return true;
255 }
256 std::unique_ptr<DataMQ> temp_mq;
257 temp_mq.reset(new DataMQ(*mq_desc));
258 if (!temp_mq || !temp_mq->isValid()) {
259 data_mq_ = nullptr;
260 return false;
261 }
262 data_mq_ = std::move(temp_mq);
263 return true;
264}
265
266bool BluetoothAudioSession::UpdateAudioConfig(
267 const AudioConfiguration& audio_config) {
268 bool is_software_session =
269 (session_type_ == SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH ||
270 session_type_ == SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH ||
271 session_type_ == SessionType::LE_AUDIO_SOFTWARE_DECODING_DATAPATH ||
Alice Kuoe80a5762022-02-09 14:44:29 +0800272 session_type_ == SessionType::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH ||
273 session_type_ ==
Alice Kuoadcceec2022-03-28 13:28:43 +0800274 SessionType::LE_AUDIO_BROADCAST_SOFTWARE_ENCODING_DATAPATH ||
275 session_type_ == SessionType::A2DP_SOFTWARE_DECODING_DATAPATH);
Josh Wu20bac522021-12-29 23:52:39 -0800276 bool is_offload_a2dp_session =
Alice Kuoadcceec2022-03-28 13:28:43 +0800277 (session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
278 session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH);
Josh Wu20bac522021-12-29 23:52:39 -0800279 bool is_offload_le_audio_session =
280 (session_type_ ==
281 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
282 session_type_ ==
283 SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH);
284 auto audio_config_tag = audio_config.getTag();
285 bool is_software_audio_config =
286 (is_software_session &&
287 audio_config_tag == AudioConfiguration::pcmConfig);
288 bool is_a2dp_offload_audio_config =
289 (is_offload_a2dp_session &&
290 audio_config_tag == AudioConfiguration::a2dpConfig);
291 bool is_le_audio_offload_audio_config =
292 (is_offload_le_audio_session &&
293 audio_config_tag == AudioConfiguration::leAudioConfig);
294 if (!is_software_audio_config && !is_a2dp_offload_audio_config &&
295 !is_le_audio_offload_audio_config) {
296 return false;
297 }
298 audio_config_ = std::make_unique<AudioConfiguration>(audio_config);
299 return true;
300}
301
302void BluetoothAudioSession::ReportSessionStatus() {
303 // This is locked already by OnSessionStarted / OnSessionEnded
304 if (observers_.empty()) {
305 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
306 << " has NO port state observer";
307 return;
308 }
309 for (auto& observer : observers_) {
310 uint16_t cookie = observer.first;
311 std::shared_ptr<PortStatusCallbacks> callback = observer.second;
312 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
313 << " notify to bluetooth_audio=0x"
314 << ::android::base::StringPrintf("%04x", cookie);
315 callback->session_changed_cb_(cookie);
316 }
317}
318
319/***
320 *
321 * PCM methods
322 *
323 ***/
324
325size_t BluetoothAudioSession::OutWritePcmData(const void* buffer,
326 size_t bytes) {
327 if (buffer == nullptr || bytes <= 0) {
328 return 0;
329 }
330 size_t total_written = 0;
331 int timeout_ms = kFmqSendTimeoutMs;
332 do {
333 std::unique_lock<std::recursive_mutex> lock(mutex_);
334 if (!IsSessionReady()) {
335 break;
336 }
337 size_t num_bytes_to_write = data_mq_->availableToWrite();
338 if (num_bytes_to_write) {
339 if (num_bytes_to_write > (bytes - total_written)) {
340 num_bytes_to_write = bytes - total_written;
341 }
342
343 if (!data_mq_->write(
344 static_cast<const MQDataType*>(buffer) + total_written,
345 num_bytes_to_write)) {
346 LOG(ERROR) << "FMQ datapath writing " << total_written << "/" << bytes
347 << " failed";
348 return total_written;
349 }
350 total_written += num_bytes_to_write;
351 } else if (timeout_ms >= kWritePollMs) {
352 lock.unlock();
353 usleep(kWritePollMs * 1000);
354 timeout_ms -= kWritePollMs;
355 } else {
356 LOG(DEBUG) << "Data " << total_written << "/" << bytes << " overflow "
357 << (kFmqSendTimeoutMs - timeout_ms) << " ms";
358 return total_written;
359 }
360 } while (total_written < bytes);
361 return total_written;
362}
363
364size_t BluetoothAudioSession::InReadPcmData(void* buffer, size_t bytes) {
365 if (buffer == nullptr || bytes <= 0) {
366 return 0;
367 }
368 size_t total_read = 0;
369 int timeout_ms = kFmqReceiveTimeoutMs;
370 do {
371 std::unique_lock<std::recursive_mutex> lock(mutex_);
372 if (!IsSessionReady()) {
373 break;
374 }
375 size_t num_bytes_to_read = data_mq_->availableToRead();
376 if (num_bytes_to_read) {
377 if (num_bytes_to_read > (bytes - total_read)) {
378 num_bytes_to_read = bytes - total_read;
379 }
380 if (!data_mq_->read(static_cast<MQDataType*>(buffer) + total_read,
381 num_bytes_to_read)) {
382 LOG(ERROR) << "FMQ datapath reading " << total_read << "/" << bytes
383 << " failed";
384 return total_read;
385 }
386 total_read += num_bytes_to_read;
387 } else if (timeout_ms >= kReadPollMs) {
388 lock.unlock();
389 usleep(kReadPollMs * 1000);
390 timeout_ms -= kReadPollMs;
391 continue;
392 } else {
393 LOG(DEBUG) << "Data " << total_read << "/" << bytes << " overflow "
394 << (kFmqReceiveTimeoutMs - timeout_ms) << " ms";
395 return total_read;
396 }
397 } while (total_read < bytes);
398 return total_read;
399}
400
401/***
402 *
403 * Other methods
404 *
405 ***/
406
407void BluetoothAudioSession::ReportControlStatus(bool start_resp,
408 BluetoothAudioStatus status) {
409 std::lock_guard<std::recursive_mutex> guard(mutex_);
410 if (observers_.empty()) {
411 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
412 << " has NO port state observer";
413 return;
414 }
415 for (auto& observer : observers_) {
416 uint16_t cookie = observer.first;
417 std::shared_ptr<PortStatusCallbacks> callback = observer.second;
418 LOG(INFO) << __func__ << " - status=" << toString(status)
419 << " for SessionType=" << toString(session_type_)
420 << ", bluetooth_audio=0x"
421 << ::android::base::StringPrintf("%04x", cookie)
422 << (start_resp ? " started" : " suspended");
423 callback->control_result_cb_(cookie, start_resp, status);
424 }
425}
426
Chen Chen81f38e52022-02-09 13:27:35 -0800427void BluetoothAudioSession::ReportLowLatencyModeAllowedChanged(bool allowed) {
428 std::lock_guard<std::recursive_mutex> guard(mutex_);
Cheney Ni6ecbc762022-03-03 00:12:48 +0800429 low_latency_allowed_ = allowed;
Chen Chen81f38e52022-02-09 13:27:35 -0800430 if (observers_.empty()) {
431 LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_)
432 << " has NO port state observer";
433 return;
434 }
435 for (auto& observer : observers_) {
436 uint16_t cookie = observer.first;
437 std::shared_ptr<PortStatusCallbacks> callback = observer.second;
Greg Kaiser42d931a2022-02-11 06:43:59 -0800438 LOG(INFO) << __func__
439 << " - allowed=" << (allowed ? " allowed" : " disallowed");
Chen Chen3b46c052022-03-14 13:16:44 -0700440 if (callback->low_latency_mode_allowed_cb_ != nullptr) {
441 callback->low_latency_mode_allowed_cb_(cookie, allowed);
442 }
Chen Chen81f38e52022-02-09 13:27:35 -0800443 }
444}
445
Josh Wu20bac522021-12-29 23:52:39 -0800446bool BluetoothAudioSession::GetPresentationPosition(
447 PresentationPosition& presentation_position) {
448 std::lock_guard<std::recursive_mutex> guard(mutex_);
449 if (!IsSessionReady()) {
450 LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_)
451 << " has NO session";
452 return false;
453 }
454 bool retval = false;
455
456 if (!stack_iface_->getPresentationPosition(&presentation_position).isOk()) {
457 LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
458 << toString(session_type_) << " failed";
459 return false;
460 }
461 return retval;
462}
463
464void BluetoothAudioSession::UpdateSourceMetadata(
465 const struct source_metadata& source_metadata) {
466 std::lock_guard<std::recursive_mutex> guard(mutex_);
467 if (!IsSessionReady()) {
468 LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_)
469 << " has NO session";
470 return;
471 }
472
473 ssize_t track_count = source_metadata.track_count;
474 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_) << ","
475 << track_count << " track(s)";
476 if (session_type_ == SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH ||
Alice Kuoadcceec2022-03-28 13:28:43 +0800477 session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
478 session_type_ == SessionType::A2DP_SOFTWARE_DECODING_DATAPATH ||
479 session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
Josh Wu20bac522021-12-29 23:52:39 -0800480 return;
481 }
482
483 SourceMetadata hal_source_metadata;
484 hal_source_metadata.tracks.resize(track_count);
485 for (int i = 0; i < track_count; i++) {
486 hal_source_metadata.tracks[i].usage =
487 static_cast<media::audio::common::AudioUsage>(
488 source_metadata.tracks[i].usage);
489 hal_source_metadata.tracks[i].contentType =
490 static_cast<media::audio::common::AudioContentType>(
491 source_metadata.tracks[i].content_type);
492 hal_source_metadata.tracks[i].gain = source_metadata.tracks[i].gain;
493 LOG(VERBOSE) << __func__ << " - SessionType=" << toString(session_type_)
494 << ", usage=" << toString(hal_source_metadata.tracks[i].usage)
495 << ", content="
496 << toString(hal_source_metadata.tracks[i].contentType)
497 << ", gain=" << hal_source_metadata.tracks[i].gain;
498 }
499
500 auto hal_retval = stack_iface_->updateSourceMetadata(hal_source_metadata);
501 if (!hal_retval.isOk()) {
502 LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
503 << toString(session_type_) << " failed";
504 }
505}
506
507void BluetoothAudioSession::UpdateSinkMetadata(
508 const struct sink_metadata& sink_metadata) {
509 std::lock_guard<std::recursive_mutex> guard(mutex_);
510 if (!IsSessionReady()) {
511 LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_)
512 << " has NO session";
513 return;
514 }
515
516 ssize_t track_count = sink_metadata.track_count;
517 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_) << ","
518 << track_count << " track(s)";
519 if (session_type_ == SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH ||
Alice Kuoadcceec2022-03-28 13:28:43 +0800520 session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH ||
521 session_type_ == SessionType::A2DP_SOFTWARE_DECODING_DATAPATH ||
522 session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH) {
Josh Wu20bac522021-12-29 23:52:39 -0800523 return;
524 }
525
526 SinkMetadata hal_sink_metadata;
527 hal_sink_metadata.tracks.resize(track_count);
528 for (int i = 0; i < track_count; i++) {
529 hal_sink_metadata.tracks[i].source =
530 static_cast<media::audio::common::AudioSource>(
531 sink_metadata.tracks[i].source);
532 hal_sink_metadata.tracks[i].gain = sink_metadata.tracks[i].gain;
533 LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_)
534 << ", source=" << sink_metadata.tracks[i].source
535 << ", dest_device=" << sink_metadata.tracks[i].dest_device
536 << ", gain=" << sink_metadata.tracks[i].gain
537 << ", dest_device_address="
538 << sink_metadata.tracks[i].dest_device_address;
539 }
540
541 auto hal_retval = stack_iface_->updateSinkMetadata(hal_sink_metadata);
542 if (!hal_retval.isOk()) {
543 LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
544 << toString(session_type_) << " failed";
545 }
546}
547
Cheney Ni6ecbc762022-03-03 00:12:48 +0800548std::vector<LatencyMode> BluetoothAudioSession::GetSupportedLatencyModes() {
549 std::lock_guard<std::recursive_mutex> guard(mutex_);
550 if (!IsSessionReady()) {
551 LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_)
552 << " has NO session";
553 return std::vector<LatencyMode>();
554 }
555 if (low_latency_allowed_) return latency_modes_;
556 std::vector<LatencyMode> modes;
557 for (LatencyMode mode : latency_modes_) {
558 if (mode == LatencyMode::LOW_LATENCY)
559 // ignore those low latency mode if Bluetooth stack doesn't allow
560 continue;
561 modes.push_back(mode);
562 }
563 return modes;
564}
565
566void BluetoothAudioSession::SetLatencyMode(const LatencyMode& latency_mode) {
Chen Chena4c4c612022-02-07 18:01:05 -0800567 std::lock_guard<std::recursive_mutex> guard(mutex_);
568 if (!IsSessionReady()) {
569 LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_)
570 << " has NO session";
571 return;
572 }
573
574 auto hal_retval = stack_iface_->setLatencyMode(latency_mode);
575 if (!hal_retval.isOk()) {
576 LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType="
577 << toString(session_type_) << " failed";
578 }
579}
580
Josh Wu20bac522021-12-29 23:52:39 -0800581bool BluetoothAudioSession::IsAidlAvailable() {
582 if (is_aidl_checked) return is_aidl_available;
583 is_aidl_available =
584 (AServiceManager_checkService(
585 kDefaultAudioProviderFactoryInterface.c_str()) != nullptr);
586 is_aidl_checked = true;
587 return is_aidl_available;
588}
589
590/***
591 *
592 * BluetoothAudioSessionInstance
593 *
594 ***/
595std::mutex BluetoothAudioSessionInstance::mutex_;
596std::unordered_map<SessionType, std::shared_ptr<BluetoothAudioSession>>
597 BluetoothAudioSessionInstance::sessions_map_;
598
599std::shared_ptr<BluetoothAudioSession>
600BluetoothAudioSessionInstance::GetSessionInstance(
601 const SessionType& session_type) {
602 std::lock_guard<std::mutex> guard(mutex_);
603
604 if (!sessions_map_.empty()) {
605 auto entry = sessions_map_.find(session_type);
606 if (entry != sessions_map_.end()) {
607 return entry->second;
608 }
609 }
610 std::shared_ptr<BluetoothAudioSession> session_ptr =
611 std::make_shared<BluetoothAudioSession>(session_type);
612 sessions_map_[session_type] = session_ptr;
613 return session_ptr;
614}
615
616} // namespace audio
617} // namespace bluetooth
618} // namespace hardware
619} // namespace android
Greg Kaiser42d931a2022-02-11 06:43:59 -0800620} // namespace aidl