Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 1 | /* |
| 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> |
Mikhail Naganov | d5f0d13 | 2023-07-26 17:26:02 -0700 | [diff] [blame] | 23 | #include <hardware/audio.h> |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 24 | |
| 25 | #include "BluetoothAudioSession.h" |
| 26 | |
| 27 | namespace aidl { |
| 28 | namespace android { |
| 29 | namespace hardware { |
| 30 | namespace bluetooth { |
| 31 | namespace audio { |
| 32 | |
| 33 | static constexpr int kFmqSendTimeoutMs = 1000; // 1000 ms timeout for sending |
| 34 | static constexpr int kFmqReceiveTimeoutMs = |
| 35 | 1000; // 1000 ms timeout for receiving |
| 36 | static constexpr int kWritePollMs = 1; // polled non-blocking interval |
| 37 | static constexpr int kReadPollMs = 1; // polled non-blocking interval |
| 38 | |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 39 | BluetoothAudioSession::BluetoothAudioSession(const SessionType& session_type) |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 40 | : session_type_(session_type), stack_iface_(nullptr), data_mq_(nullptr) {} |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 41 | |
| 42 | /*** |
| 43 | * |
| 44 | * Callback methods |
| 45 | * |
| 46 | ***/ |
| 47 | |
| 48 | void BluetoothAudioSession::OnSessionStarted( |
| 49 | const std::shared_ptr<IBluetoothAudioPort> stack_iface, |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 50 | const DataMQDesc* mq_desc, const AudioConfiguration& audio_config, |
| 51 | const std::vector<LatencyMode>& latency_modes) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 52 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 53 | if (stack_iface == nullptr) { |
| 54 | LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_) |
| 55 | << ", IBluetoothAudioPort Invalid"; |
| 56 | } else if (!UpdateAudioConfig(audio_config)) { |
| 57 | LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_) |
| 58 | << ", AudioConfiguration=" << audio_config.toString() |
| 59 | << " Invalid"; |
| 60 | } else if (!UpdateDataPath(mq_desc)) { |
| 61 | LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_) |
| 62 | << " MqDescriptor Invalid"; |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 63 | audio_config_ = nullptr; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 64 | } else { |
| 65 | stack_iface_ = stack_iface; |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 66 | latency_modes_ = latency_modes; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 67 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_) |
| 68 | << ", AudioConfiguration=" << audio_config.toString(); |
| 69 | ReportSessionStatus(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void BluetoothAudioSession::OnSessionEnded() { |
| 74 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 75 | bool toggled = IsSessionReady(); |
| 76 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_); |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 77 | audio_config_ = nullptr; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 78 | stack_iface_ = nullptr; |
| 79 | UpdateDataPath(nullptr); |
| 80 | if (toggled) { |
| 81 | ReportSessionStatus(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /*** |
| 86 | * |
| 87 | * Util methods |
| 88 | * |
| 89 | ***/ |
| 90 | |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 91 | const AudioConfiguration BluetoothAudioSession::GetAudioConfig() { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 92 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 93 | if (!IsSessionReady()) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 94 | switch (session_type_) { |
| 95 | case SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH: |
Alice Kuo | adcceec | 2022-03-28 13:28:43 +0800 | [diff] [blame] | 96 | case SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH: |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 97 | return AudioConfiguration(CodecConfiguration{}); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 98 | case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH: |
| 99 | case SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH: |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 100 | return AudioConfiguration(LeAudioConfiguration{}); |
Alice Kuo | e80a576 | 2022-02-09 14:44:29 +0800 | [diff] [blame] | 101 | case SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH: |
| 102 | return AudioConfiguration(LeAudioBroadcastConfiguration{}); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 103 | default: |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 104 | return AudioConfiguration(PcmConfiguration{}); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | return *audio_config_; |
| 108 | } |
| 109 | |
| 110 | void BluetoothAudioSession::ReportAudioConfigChanged( |
| 111 | const AudioConfiguration& audio_config) { |
| 112 | if (session_type_ != |
| 113 | SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH && |
| 114 | session_type_ != |
Alice Kuo | 851ef34 | 2022-08-25 02:45:02 +0800 | [diff] [blame] | 115 | SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 116 | return; |
| 117 | } |
Alice Kuo | 851ef34 | 2022-08-25 02:45:02 +0800 | [diff] [blame] | 118 | |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 119 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
Alice Kuo | 851ef34 | 2022-08-25 02:45:02 +0800 | [diff] [blame] | 120 | if (audio_config.getTag() != AudioConfiguration::leAudioConfig) { |
| 121 | LOG(ERROR) << __func__ << " invalid audio config type for SessionType =" |
| 122 | << toString(session_type_); |
| 123 | return; |
| 124 | } |
| 125 | |
Patty Huang | f5b38af | 2023-03-23 23:19:00 +0800 | [diff] [blame] | 126 | audio_config_ = std::make_unique<AudioConfiguration>(audio_config); |
Alice Kuo | 851ef34 | 2022-08-25 02:45:02 +0800 | [diff] [blame] | 127 | |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 128 | if (observers_.empty()) { |
| 129 | LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_) |
| 130 | << " has NO port state observer"; |
| 131 | return; |
| 132 | } |
| 133 | for (auto& observer : observers_) { |
| 134 | uint16_t cookie = observer.first; |
| 135 | std::shared_ptr<struct PortStatusCallbacks> cb = observer.second; |
| 136 | LOG(INFO) << __func__ << " for SessionType=" << toString(session_type_) |
| 137 | << ", bluetooth_audio=0x" |
| 138 | << ::android::base::StringPrintf("%04x", cookie); |
Patty Huang | f5b38af | 2023-03-23 23:19:00 +0800 | [diff] [blame] | 139 | if (cb->audio_configuration_changed_cb_ != nullptr) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 140 | cb->audio_configuration_changed_cb_(cookie); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | bool BluetoothAudioSession::IsSessionReady() { |
| 146 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 147 | |
| 148 | bool is_mq_valid = |
| 149 | (session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH || |
| 150 | session_type_ == |
| 151 | SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH || |
| 152 | session_type_ == |
| 153 | SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH || |
Alice Kuo | e80a576 | 2022-02-09 14:44:29 +0800 | [diff] [blame] | 154 | session_type_ == |
| 155 | SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH || |
Alice Kuo | adcceec | 2022-03-28 13:28:43 +0800 | [diff] [blame] | 156 | session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH || |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 157 | (data_mq_ != nullptr && data_mq_->isValid())); |
Josh Wu | 75462aa | 2022-01-21 21:51:21 -0800 | [diff] [blame] | 158 | return stack_iface_ != nullptr && is_mq_valid && audio_config_ != nullptr; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /*** |
| 162 | * |
| 163 | * Status callback methods |
| 164 | * |
| 165 | ***/ |
| 166 | |
| 167 | uint16_t BluetoothAudioSession::RegisterStatusCback( |
| 168 | const PortStatusCallbacks& callbacks) { |
| 169 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 170 | uint16_t cookie = ObserversCookieGetInitValue(session_type_); |
| 171 | uint16_t cookie_upper_bound = ObserversCookieGetUpperBound(session_type_); |
| 172 | |
| 173 | while (cookie < cookie_upper_bound) { |
| 174 | if (observers_.find(cookie) == observers_.end()) { |
| 175 | break; |
| 176 | } |
| 177 | ++cookie; |
| 178 | } |
| 179 | if (cookie >= cookie_upper_bound) { |
| 180 | LOG(ERROR) << __func__ << " - SessionType=" << toString(session_type_) |
| 181 | << " has " << observers_.size() |
| 182 | << " observers already (No Resource)"; |
| 183 | return kObserversCookieUndefined; |
| 184 | } |
| 185 | std::shared_ptr<PortStatusCallbacks> cb = |
| 186 | std::make_shared<PortStatusCallbacks>(); |
| 187 | *cb = callbacks; |
| 188 | observers_[cookie] = cb; |
| 189 | return cookie; |
| 190 | } |
| 191 | |
| 192 | void BluetoothAudioSession::UnregisterStatusCback(uint16_t cookie) { |
| 193 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 194 | if (observers_.erase(cookie) != 1) { |
| 195 | LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_) |
| 196 | << " no such provider=0x" |
| 197 | << ::android::base::StringPrintf("%04x", cookie); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /*** |
| 202 | * |
| 203 | * Stream methods |
| 204 | * |
| 205 | ***/ |
| 206 | |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 207 | bool BluetoothAudioSession::StartStream(bool is_low_latency) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 208 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 209 | if (!IsSessionReady()) { |
| 210 | LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_) |
| 211 | << " has NO session"; |
| 212 | return false; |
| 213 | } |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 214 | auto hal_retval = stack_iface_->startStream(is_low_latency); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 215 | if (!hal_retval.isOk()) { |
| 216 | LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType=" |
| 217 | << toString(session_type_) << " failed"; |
| 218 | return false; |
| 219 | } |
| 220 | return true; |
| 221 | } |
| 222 | |
| 223 | bool BluetoothAudioSession::SuspendStream() { |
| 224 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 225 | if (!IsSessionReady()) { |
| 226 | LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_) |
| 227 | << " has NO session"; |
| 228 | return false; |
| 229 | } |
| 230 | auto hal_retval = stack_iface_->suspendStream(); |
| 231 | if (!hal_retval.isOk()) { |
| 232 | LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType=" |
| 233 | << toString(session_type_) << " failed"; |
| 234 | return false; |
| 235 | } |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | void BluetoothAudioSession::StopStream() { |
| 240 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 241 | if (!IsSessionReady()) { |
| 242 | return; |
| 243 | } |
| 244 | auto hal_retval = stack_iface_->stopStream(); |
| 245 | if (!hal_retval.isOk()) { |
| 246 | LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType=" |
| 247 | << toString(session_type_) << " failed"; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /*** |
| 252 | * |
| 253 | * Private methods |
| 254 | * |
| 255 | ***/ |
| 256 | |
| 257 | bool BluetoothAudioSession::UpdateDataPath(const DataMQDesc* mq_desc) { |
| 258 | if (mq_desc == nullptr) { |
| 259 | // usecase of reset by nullptr |
| 260 | data_mq_ = nullptr; |
| 261 | return true; |
| 262 | } |
| 263 | std::unique_ptr<DataMQ> temp_mq; |
| 264 | temp_mq.reset(new DataMQ(*mq_desc)); |
| 265 | if (!temp_mq || !temp_mq->isValid()) { |
| 266 | data_mq_ = nullptr; |
| 267 | return false; |
| 268 | } |
| 269 | data_mq_ = std::move(temp_mq); |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | bool BluetoothAudioSession::UpdateAudioConfig( |
| 274 | const AudioConfiguration& audio_config) { |
| 275 | bool is_software_session = |
| 276 | (session_type_ == SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH || |
| 277 | session_type_ == SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH || |
| 278 | session_type_ == SessionType::LE_AUDIO_SOFTWARE_DECODING_DATAPATH || |
Alice Kuo | e80a576 | 2022-02-09 14:44:29 +0800 | [diff] [blame] | 279 | session_type_ == SessionType::LE_AUDIO_SOFTWARE_ENCODING_DATAPATH || |
| 280 | session_type_ == |
Alice Kuo | adcceec | 2022-03-28 13:28:43 +0800 | [diff] [blame] | 281 | SessionType::LE_AUDIO_BROADCAST_SOFTWARE_ENCODING_DATAPATH || |
| 282 | session_type_ == SessionType::A2DP_SOFTWARE_DECODING_DATAPATH); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 283 | bool is_offload_a2dp_session = |
Alice Kuo | adcceec | 2022-03-28 13:28:43 +0800 | [diff] [blame] | 284 | (session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH || |
| 285 | session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH); |
Alice Kuo | ee398a9 | 2022-07-10 23:59:18 +0800 | [diff] [blame] | 286 | bool is_offload_le_audio_unicast_session = |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 287 | (session_type_ == |
| 288 | SessionType::LE_AUDIO_HARDWARE_OFFLOAD_ENCODING_DATAPATH || |
| 289 | session_type_ == |
| 290 | SessionType::LE_AUDIO_HARDWARE_OFFLOAD_DECODING_DATAPATH); |
Alice Kuo | ee398a9 | 2022-07-10 23:59:18 +0800 | [diff] [blame] | 291 | bool is_offload_le_audio_broadcast_session = |
| 292 | (session_type_ == |
| 293 | SessionType::LE_AUDIO_BROADCAST_HARDWARE_OFFLOAD_ENCODING_DATAPATH); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 294 | auto audio_config_tag = audio_config.getTag(); |
| 295 | bool is_software_audio_config = |
| 296 | (is_software_session && |
| 297 | audio_config_tag == AudioConfiguration::pcmConfig); |
| 298 | bool is_a2dp_offload_audio_config = |
| 299 | (is_offload_a2dp_session && |
| 300 | audio_config_tag == AudioConfiguration::a2dpConfig); |
Alice Kuo | ee398a9 | 2022-07-10 23:59:18 +0800 | [diff] [blame] | 301 | bool is_le_audio_offload_unicast_audio_config = |
| 302 | (is_offload_le_audio_unicast_session && |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 303 | audio_config_tag == AudioConfiguration::leAudioConfig); |
Alice Kuo | ee398a9 | 2022-07-10 23:59:18 +0800 | [diff] [blame] | 304 | bool is_le_audio_offload_broadcast_audio_config = |
| 305 | (is_offload_le_audio_broadcast_session && |
| 306 | audio_config_tag == AudioConfiguration::leAudioBroadcastConfig); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 307 | if (!is_software_audio_config && !is_a2dp_offload_audio_config && |
Alice Kuo | ee398a9 | 2022-07-10 23:59:18 +0800 | [diff] [blame] | 308 | !is_le_audio_offload_unicast_audio_config && |
| 309 | !is_le_audio_offload_broadcast_audio_config) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 310 | return false; |
| 311 | } |
| 312 | audio_config_ = std::make_unique<AudioConfiguration>(audio_config); |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | void BluetoothAudioSession::ReportSessionStatus() { |
| 317 | // This is locked already by OnSessionStarted / OnSessionEnded |
| 318 | if (observers_.empty()) { |
| 319 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_) |
| 320 | << " has NO port state observer"; |
| 321 | return; |
| 322 | } |
| 323 | for (auto& observer : observers_) { |
| 324 | uint16_t cookie = observer.first; |
| 325 | std::shared_ptr<PortStatusCallbacks> callback = observer.second; |
| 326 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_) |
| 327 | << " notify to bluetooth_audio=0x" |
| 328 | << ::android::base::StringPrintf("%04x", cookie); |
| 329 | callback->session_changed_cb_(cookie); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /*** |
| 334 | * |
| 335 | * PCM methods |
| 336 | * |
| 337 | ***/ |
| 338 | |
| 339 | size_t BluetoothAudioSession::OutWritePcmData(const void* buffer, |
| 340 | size_t bytes) { |
| 341 | if (buffer == nullptr || bytes <= 0) { |
| 342 | return 0; |
| 343 | } |
| 344 | size_t total_written = 0; |
| 345 | int timeout_ms = kFmqSendTimeoutMs; |
| 346 | do { |
| 347 | std::unique_lock<std::recursive_mutex> lock(mutex_); |
| 348 | if (!IsSessionReady()) { |
| 349 | break; |
| 350 | } |
| 351 | size_t num_bytes_to_write = data_mq_->availableToWrite(); |
| 352 | if (num_bytes_to_write) { |
| 353 | if (num_bytes_to_write > (bytes - total_written)) { |
| 354 | num_bytes_to_write = bytes - total_written; |
| 355 | } |
| 356 | |
| 357 | if (!data_mq_->write( |
| 358 | static_cast<const MQDataType*>(buffer) + total_written, |
| 359 | num_bytes_to_write)) { |
| 360 | LOG(ERROR) << "FMQ datapath writing " << total_written << "/" << bytes |
| 361 | << " failed"; |
| 362 | return total_written; |
| 363 | } |
| 364 | total_written += num_bytes_to_write; |
| 365 | } else if (timeout_ms >= kWritePollMs) { |
| 366 | lock.unlock(); |
| 367 | usleep(kWritePollMs * 1000); |
| 368 | timeout_ms -= kWritePollMs; |
| 369 | } else { |
| 370 | LOG(DEBUG) << "Data " << total_written << "/" << bytes << " overflow " |
| 371 | << (kFmqSendTimeoutMs - timeout_ms) << " ms"; |
| 372 | return total_written; |
| 373 | } |
| 374 | } while (total_written < bytes); |
| 375 | return total_written; |
| 376 | } |
| 377 | |
| 378 | size_t BluetoothAudioSession::InReadPcmData(void* buffer, size_t bytes) { |
| 379 | if (buffer == nullptr || bytes <= 0) { |
| 380 | return 0; |
| 381 | } |
| 382 | size_t total_read = 0; |
| 383 | int timeout_ms = kFmqReceiveTimeoutMs; |
| 384 | do { |
| 385 | std::unique_lock<std::recursive_mutex> lock(mutex_); |
| 386 | if (!IsSessionReady()) { |
| 387 | break; |
| 388 | } |
| 389 | size_t num_bytes_to_read = data_mq_->availableToRead(); |
| 390 | if (num_bytes_to_read) { |
| 391 | if (num_bytes_to_read > (bytes - total_read)) { |
| 392 | num_bytes_to_read = bytes - total_read; |
| 393 | } |
| 394 | if (!data_mq_->read(static_cast<MQDataType*>(buffer) + total_read, |
| 395 | num_bytes_to_read)) { |
| 396 | LOG(ERROR) << "FMQ datapath reading " << total_read << "/" << bytes |
| 397 | << " failed"; |
| 398 | return total_read; |
| 399 | } |
| 400 | total_read += num_bytes_to_read; |
| 401 | } else if (timeout_ms >= kReadPollMs) { |
| 402 | lock.unlock(); |
| 403 | usleep(kReadPollMs * 1000); |
| 404 | timeout_ms -= kReadPollMs; |
| 405 | continue; |
| 406 | } else { |
| 407 | LOG(DEBUG) << "Data " << total_read << "/" << bytes << " overflow " |
| 408 | << (kFmqReceiveTimeoutMs - timeout_ms) << " ms"; |
| 409 | return total_read; |
| 410 | } |
| 411 | } while (total_read < bytes); |
| 412 | return total_read; |
| 413 | } |
| 414 | |
| 415 | /*** |
| 416 | * |
| 417 | * Other methods |
| 418 | * |
| 419 | ***/ |
| 420 | |
| 421 | void BluetoothAudioSession::ReportControlStatus(bool start_resp, |
| 422 | BluetoothAudioStatus status) { |
| 423 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 424 | if (observers_.empty()) { |
| 425 | LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_) |
| 426 | << " has NO port state observer"; |
| 427 | return; |
| 428 | } |
| 429 | for (auto& observer : observers_) { |
| 430 | uint16_t cookie = observer.first; |
| 431 | std::shared_ptr<PortStatusCallbacks> callback = observer.second; |
| 432 | LOG(INFO) << __func__ << " - status=" << toString(status) |
| 433 | << " for SessionType=" << toString(session_type_) |
| 434 | << ", bluetooth_audio=0x" |
| 435 | << ::android::base::StringPrintf("%04x", cookie) |
| 436 | << (start_resp ? " started" : " suspended"); |
| 437 | callback->control_result_cb_(cookie, start_resp, status); |
| 438 | } |
| 439 | } |
| 440 | |
Chen Chen | 81f38e5 | 2022-02-09 13:27:35 -0800 | [diff] [blame] | 441 | void BluetoothAudioSession::ReportLowLatencyModeAllowedChanged(bool allowed) { |
| 442 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 443 | low_latency_allowed_ = allowed; |
kuanyuhuang | 25b4819 | 2023-08-07 05:05:52 +0000 | [diff] [blame] | 444 | // TODO(b/294498919): Remove this after there is API to update latency mode |
| 445 | // after audio session started. If low_latency_allowed_ is true, the session |
| 446 | // can support LOW_LATENCY and FREE LatencyMode. |
| 447 | if (low_latency_allowed_) { |
| 448 | if (std::find(latency_modes_.begin(), latency_modes_.end(), |
| 449 | LatencyMode::LOW_LATENCY) == latency_modes_.end()) { |
| 450 | LOG(INFO) << __func__ << " - insert LOW_LATENCY LatencyMode"; |
| 451 | latency_modes_.push_back(LatencyMode::LOW_LATENCY); |
| 452 | } |
| 453 | } |
Chen Chen | 81f38e5 | 2022-02-09 13:27:35 -0800 | [diff] [blame] | 454 | if (observers_.empty()) { |
| 455 | LOG(WARNING) << __func__ << " - SessionType=" << toString(session_type_) |
| 456 | << " has NO port state observer"; |
| 457 | return; |
| 458 | } |
| 459 | for (auto& observer : observers_) { |
| 460 | uint16_t cookie = observer.first; |
| 461 | std::shared_ptr<PortStatusCallbacks> callback = observer.second; |
Greg Kaiser | 42d931a | 2022-02-11 06:43:59 -0800 | [diff] [blame] | 462 | LOG(INFO) << __func__ |
| 463 | << " - allowed=" << (allowed ? " allowed" : " disallowed"); |
Chen Chen | 3b46c05 | 2022-03-14 13:16:44 -0700 | [diff] [blame] | 464 | if (callback->low_latency_mode_allowed_cb_ != nullptr) { |
| 465 | callback->low_latency_mode_allowed_cb_(cookie, allowed); |
| 466 | } |
Chen Chen | 81f38e5 | 2022-02-09 13:27:35 -0800 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 470 | bool BluetoothAudioSession::GetPresentationPosition( |
| 471 | PresentationPosition& presentation_position) { |
| 472 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 473 | if (!IsSessionReady()) { |
| 474 | LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_) |
| 475 | << " has NO session"; |
| 476 | return false; |
| 477 | } |
| 478 | bool retval = false; |
| 479 | |
| 480 | if (!stack_iface_->getPresentationPosition(&presentation_position).isOk()) { |
| 481 | LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType=" |
| 482 | << toString(session_type_) << " failed"; |
| 483 | return false; |
| 484 | } |
| 485 | return retval; |
| 486 | } |
| 487 | |
| 488 | void BluetoothAudioSession::UpdateSourceMetadata( |
| 489 | const struct source_metadata& source_metadata) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 490 | ssize_t track_count = source_metadata.track_count; |
| 491 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_) << "," |
| 492 | << track_count << " track(s)"; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 493 | SourceMetadata hal_source_metadata; |
| 494 | hal_source_metadata.tracks.resize(track_count); |
| 495 | for (int i = 0; i < track_count; i++) { |
| 496 | hal_source_metadata.tracks[i].usage = |
| 497 | static_cast<media::audio::common::AudioUsage>( |
| 498 | source_metadata.tracks[i].usage); |
| 499 | hal_source_metadata.tracks[i].contentType = |
| 500 | static_cast<media::audio::common::AudioContentType>( |
| 501 | source_metadata.tracks[i].content_type); |
| 502 | hal_source_metadata.tracks[i].gain = source_metadata.tracks[i].gain; |
| 503 | LOG(VERBOSE) << __func__ << " - SessionType=" << toString(session_type_) |
| 504 | << ", usage=" << toString(hal_source_metadata.tracks[i].usage) |
| 505 | << ", content=" |
| 506 | << toString(hal_source_metadata.tracks[i].contentType) |
| 507 | << ", gain=" << hal_source_metadata.tracks[i].gain; |
| 508 | } |
Mikhail Naganov | d5f0d13 | 2023-07-26 17:26:02 -0700 | [diff] [blame] | 509 | UpdateSourceMetadata(hal_source_metadata); |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | void BluetoothAudioSession::UpdateSinkMetadata( |
| 513 | const struct sink_metadata& sink_metadata) { |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 514 | ssize_t track_count = sink_metadata.track_count; |
| 515 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_) << "," |
| 516 | << track_count << " track(s)"; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 517 | SinkMetadata hal_sink_metadata; |
| 518 | hal_sink_metadata.tracks.resize(track_count); |
| 519 | for (int i = 0; i < track_count; i++) { |
| 520 | hal_sink_metadata.tracks[i].source = |
| 521 | static_cast<media::audio::common::AudioSource>( |
| 522 | sink_metadata.tracks[i].source); |
| 523 | hal_sink_metadata.tracks[i].gain = sink_metadata.tracks[i].gain; |
| 524 | LOG(INFO) << __func__ << " - SessionType=" << toString(session_type_) |
| 525 | << ", source=" << sink_metadata.tracks[i].source |
| 526 | << ", dest_device=" << sink_metadata.tracks[i].dest_device |
| 527 | << ", gain=" << sink_metadata.tracks[i].gain |
| 528 | << ", dest_device_address=" |
| 529 | << sink_metadata.tracks[i].dest_device_address; |
| 530 | } |
Mikhail Naganov | d5f0d13 | 2023-07-26 17:26:02 -0700 | [diff] [blame] | 531 | UpdateSinkMetadata(hal_sink_metadata); |
| 532 | } |
| 533 | |
| 534 | bool BluetoothAudioSession::UpdateSourceMetadata( |
| 535 | const SourceMetadata& hal_source_metadata) { |
| 536 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 537 | if (!IsSessionReady()) { |
| 538 | LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_) |
| 539 | << " has NO session"; |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | if (session_type_ == SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH || |
| 544 | session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH || |
| 545 | session_type_ == SessionType::A2DP_SOFTWARE_DECODING_DATAPATH || |
| 546 | session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH) { |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | auto hal_retval = stack_iface_->updateSourceMetadata(hal_source_metadata); |
| 551 | if (!hal_retval.isOk()) { |
| 552 | LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType=" |
| 553 | << toString(session_type_) << " failed"; |
| 554 | return false; |
| 555 | } |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | bool BluetoothAudioSession::UpdateSinkMetadata( |
| 560 | const SinkMetadata& hal_sink_metadata) { |
| 561 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 562 | if (!IsSessionReady()) { |
| 563 | LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_) |
| 564 | << " has NO session"; |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | if (session_type_ == SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH || |
| 569 | session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_ENCODING_DATAPATH || |
| 570 | session_type_ == SessionType::A2DP_SOFTWARE_DECODING_DATAPATH || |
| 571 | session_type_ == SessionType::A2DP_HARDWARE_OFFLOAD_DECODING_DATAPATH) { |
| 572 | return false; |
| 573 | } |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 574 | |
| 575 | auto hal_retval = stack_iface_->updateSinkMetadata(hal_sink_metadata); |
| 576 | if (!hal_retval.isOk()) { |
| 577 | LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType=" |
| 578 | << toString(session_type_) << " failed"; |
Mikhail Naganov | d5f0d13 | 2023-07-26 17:26:02 -0700 | [diff] [blame] | 579 | return false; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 580 | } |
Mikhail Naganov | d5f0d13 | 2023-07-26 17:26:02 -0700 | [diff] [blame] | 581 | return true; |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Cheney Ni | 6ecbc76 | 2022-03-03 00:12:48 +0800 | [diff] [blame] | 584 | std::vector<LatencyMode> BluetoothAudioSession::GetSupportedLatencyModes() { |
| 585 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 586 | if (!IsSessionReady()) { |
| 587 | LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_) |
| 588 | << " has NO session"; |
| 589 | return std::vector<LatencyMode>(); |
| 590 | } |
| 591 | if (low_latency_allowed_) return latency_modes_; |
| 592 | std::vector<LatencyMode> modes; |
| 593 | for (LatencyMode mode : latency_modes_) { |
| 594 | if (mode == LatencyMode::LOW_LATENCY) |
| 595 | // ignore those low latency mode if Bluetooth stack doesn't allow |
| 596 | continue; |
| 597 | modes.push_back(mode); |
| 598 | } |
| 599 | return modes; |
| 600 | } |
| 601 | |
| 602 | void BluetoothAudioSession::SetLatencyMode(const LatencyMode& latency_mode) { |
Chen Chen | a4c4c61 | 2022-02-07 18:01:05 -0800 | [diff] [blame] | 603 | std::lock_guard<std::recursive_mutex> guard(mutex_); |
| 604 | if (!IsSessionReady()) { |
| 605 | LOG(DEBUG) << __func__ << " - SessionType=" << toString(session_type_) |
| 606 | << " has NO session"; |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | auto hal_retval = stack_iface_->setLatencyMode(latency_mode); |
| 611 | if (!hal_retval.isOk()) { |
| 612 | LOG(WARNING) << __func__ << " - IBluetoothAudioPort SessionType=" |
| 613 | << toString(session_type_) << " failed"; |
| 614 | } |
| 615 | } |
| 616 | |
Josh Wu | 20bac52 | 2021-12-29 23:52:39 -0800 | [diff] [blame] | 617 | bool BluetoothAudioSession::IsAidlAvailable() { |
| 618 | if (is_aidl_checked) return is_aidl_available; |
| 619 | is_aidl_available = |
| 620 | (AServiceManager_checkService( |
| 621 | kDefaultAudioProviderFactoryInterface.c_str()) != nullptr); |
| 622 | is_aidl_checked = true; |
| 623 | return is_aidl_available; |
| 624 | } |
| 625 | |
| 626 | /*** |
| 627 | * |
| 628 | * BluetoothAudioSessionInstance |
| 629 | * |
| 630 | ***/ |
| 631 | std::mutex BluetoothAudioSessionInstance::mutex_; |
| 632 | std::unordered_map<SessionType, std::shared_ptr<BluetoothAudioSession>> |
| 633 | BluetoothAudioSessionInstance::sessions_map_; |
| 634 | |
| 635 | std::shared_ptr<BluetoothAudioSession> |
| 636 | BluetoothAudioSessionInstance::GetSessionInstance( |
| 637 | const SessionType& session_type) { |
| 638 | std::lock_guard<std::mutex> guard(mutex_); |
| 639 | |
| 640 | if (!sessions_map_.empty()) { |
| 641 | auto entry = sessions_map_.find(session_type); |
| 642 | if (entry != sessions_map_.end()) { |
| 643 | return entry->second; |
| 644 | } |
| 645 | } |
| 646 | std::shared_ptr<BluetoothAudioSession> session_ptr = |
| 647 | std::make_shared<BluetoothAudioSession>(session_type); |
| 648 | sessions_map_[session_type] = session_ptr; |
| 649 | return session_ptr; |
| 650 | } |
| 651 | |
| 652 | } // namespace audio |
| 653 | } // namespace bluetooth |
| 654 | } // namespace hardware |
| 655 | } // namespace android |
Greg Kaiser | 42d931a | 2022-02-11 06:43:59 -0800 | [diff] [blame] | 656 | } // namespace aidl |