Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 17 | #include <algorithm> |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 18 | |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 19 | #define LOG_TAG "AHAL_StreamBluetooth" |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 20 | #include <Utils.h> |
| 21 | #include <android-base/logging.h> |
| 22 | #include <audio_utils/clock.h> |
| 23 | |
Mikhail Naganov | 55045b5 | 2023-10-24 17:03:50 -0700 | [diff] [blame] | 24 | #include "BluetoothAudioSession.h" |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 25 | #include "core-impl/StreamBluetooth.h" |
| 26 | |
Mikhail Naganov | 55045b5 | 2023-10-24 17:03:50 -0700 | [diff] [blame] | 27 | using aidl::android::hardware::audio::common::frameCountFromDurationUs; |
| 28 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 29 | using aidl::android::hardware::audio::common::SourceMetadata; |
| 30 | using aidl::android::hardware::audio::core::VendorParameter; |
| 31 | using aidl::android::hardware::bluetooth::audio::ChannelMode; |
| 32 | using aidl::android::hardware::bluetooth::audio::PcmConfiguration; |
| 33 | using aidl::android::hardware::bluetooth::audio::PresentationPosition; |
| 34 | using aidl::android::media::audio::common::AudioChannelLayout; |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 35 | using aidl::android::media::audio::common::AudioConfigBase; |
Mikhail Naganov | 55045b5 | 2023-10-24 17:03:50 -0700 | [diff] [blame] | 36 | using aidl::android::media::audio::common::AudioDevice; |
| 37 | using aidl::android::media::audio::common::AudioDeviceAddress; |
| 38 | using aidl::android::media::audio::common::AudioFormatDescription; |
| 39 | using aidl::android::media::audio::common::AudioFormatType; |
| 40 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 41 | using aidl::android::media::audio::common::MicrophoneDynamicInfo; |
| 42 | using aidl::android::media::audio::common::MicrophoneInfo; |
| 43 | using android::bluetooth::audio::aidl::BluetoothAudioPortAidl; |
| 44 | using android::bluetooth::audio::aidl::BluetoothAudioPortAidlIn; |
| 45 | using android::bluetooth::audio::aidl::BluetoothAudioPortAidlOut; |
| 46 | using android::bluetooth::audio::aidl::BluetoothStreamState; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 47 | |
Mikhail Naganov | 55045b5 | 2023-10-24 17:03:50 -0700 | [diff] [blame] | 48 | namespace aidl::android::hardware::audio::core { |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 49 | |
| 50 | constexpr int kBluetoothDefaultInputBufferMs = 20; |
| 51 | constexpr int kBluetoothDefaultOutputBufferMs = 10; |
| 52 | // constexpr int kBluetoothSpatializerOutputBufferMs = 10; |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 53 | constexpr int kBluetoothDefaultRemoteDelayMs = 200; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 54 | |
Ram Mohan | 18f0d51 | 2023-07-01 00:47:09 +0530 | [diff] [blame] | 55 | StreamBluetooth::StreamBluetooth(StreamContext* context, const Metadata& metadata, |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 56 | ModuleBluetooth::BtProfileHandles&& btHandles, |
| 57 | const std::shared_ptr<BluetoothAudioPortAidl>& btDeviceProxy, |
| 58 | const PcmConfiguration& pcmConfig) |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 59 | : StreamCommonImpl(context, metadata), |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 60 | mFrameSizeBytes(getContext().getFrameSize()), |
Ram Mohan | 18f0d51 | 2023-07-01 00:47:09 +0530 | [diff] [blame] | 61 | mIsInput(isInput(metadata)), |
Mikhail Naganov | 55045b5 | 2023-10-24 17:03:50 -0700 | [diff] [blame] | 62 | mBluetoothA2dp(std::move(std::get<ModuleBluetooth::BtInterface::BTA2DP>(btHandles))), |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 63 | mBluetoothLe(std::move(std::get<ModuleBluetooth::BtInterface::BTLE>(btHandles))), |
| 64 | mPreferredDataIntervalUs(pcmConfig.dataIntervalUs != 0 |
| 65 | ? pcmConfig.dataIntervalUs |
| 66 | : (mIsInput ? kBluetoothDefaultInputBufferMs |
| 67 | : kBluetoothDefaultOutputBufferMs) * |
| 68 | 1000), |
| 69 | mPreferredFrameCount( |
| 70 | frameCountFromDurationUs(mPreferredDataIntervalUs, pcmConfig.sampleRateHz)), |
| 71 | mBtDeviceProxy(btDeviceProxy) {} |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 72 | |
| 73 | ::android::status_t StreamBluetooth::init() { |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 74 | std::lock_guard guard(mLock); |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 75 | if (mBtDeviceProxy == nullptr) { |
| 76 | // This is a normal situation in VTS tests. |
| 77 | LOG(INFO) << __func__ << ": no BT HAL proxy, stream is non-functional"; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 78 | } |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 79 | return ::android::OK; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ::android::status_t StreamBluetooth::drain(StreamDescriptor::DrainMode) { |
| 83 | usleep(1000); |
| 84 | return ::android::OK; |
| 85 | } |
| 86 | |
| 87 | ::android::status_t StreamBluetooth::flush() { |
| 88 | usleep(1000); |
| 89 | return ::android::OK; |
| 90 | } |
| 91 | |
| 92 | ::android::status_t StreamBluetooth::pause() { |
| 93 | return standby(); |
| 94 | } |
| 95 | |
| 96 | ::android::status_t StreamBluetooth::transfer(void* buffer, size_t frameCount, |
| 97 | size_t* actualFrameCount, int32_t* latencyMs) { |
| 98 | std::lock_guard guard(mLock); |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 99 | if (mBtDeviceProxy == nullptr || mBtDeviceProxy->getState() == BluetoothStreamState::DISABLED) { |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 100 | *actualFrameCount = 0; |
| 101 | *latencyMs = StreamDescriptor::LATENCY_UNKNOWN; |
| 102 | return ::android::OK; |
| 103 | } |
| 104 | *actualFrameCount = 0; |
| 105 | *latencyMs = 0; |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 106 | if (!mBtDeviceProxy->start()) { |
| 107 | LOG(ERROR) << __func__ << ": state= " << mBtDeviceProxy->getState() << " failed to start"; |
| 108 | return -EIO; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 109 | } |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 110 | const size_t fc = std::min(frameCount, mPreferredFrameCount); |
| 111 | const size_t bytesToTransfer = fc * mFrameSizeBytes; |
| 112 | if (mIsInput) { |
| 113 | const size_t totalRead = mBtDeviceProxy->readData(buffer, bytesToTransfer); |
| 114 | *actualFrameCount = std::max(*actualFrameCount, totalRead / mFrameSizeBytes); |
| 115 | } else { |
| 116 | const size_t totalWrite = mBtDeviceProxy->writeData(buffer, bytesToTransfer); |
| 117 | *actualFrameCount = std::max(*actualFrameCount, totalWrite / mFrameSizeBytes); |
| 118 | } |
| 119 | PresentationPosition presentation_position; |
| 120 | if (!mBtDeviceProxy->getPresentationPosition(presentation_position)) { |
| 121 | presentation_position.remoteDeviceAudioDelayNanos = |
| 122 | kBluetoothDefaultRemoteDelayMs * NANOS_PER_MILLISECOND; |
| 123 | LOG(WARNING) << __func__ << ": getPresentationPosition failed, latency info is unavailable"; |
| 124 | } |
| 125 | // TODO(b/317117580): incorporate logic from |
| 126 | // packages/modules/Bluetooth/system/audio_bluetooth_hw/stream_apis.cc |
| 127 | // out_calculate_feeding_delay_ms / in_calculate_starving_delay_ms |
| 128 | *latencyMs = std::max(*latencyMs, (int32_t)(presentation_position.remoteDeviceAudioDelayNanos / |
| 129 | NANOS_PER_MILLISECOND)); |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 130 | return ::android::OK; |
| 131 | } |
| 132 | |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 133 | // static |
| 134 | bool StreamBluetooth::checkConfigParams(const PcmConfiguration& pcmConfig, |
| 135 | const AudioConfigBase& config) { |
| 136 | if ((int)config.sampleRate != pcmConfig.sampleRateHz) { |
| 137 | LOG(ERROR) << __func__ << ": sample rate mismatch, stream value=" << config.sampleRate |
| 138 | << ", BT HAL value=" << pcmConfig.sampleRateHz; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 139 | return false; |
| 140 | } |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 141 | const auto channelCount = |
| 142 | aidl::android::hardware::audio::common::getChannelCount(config.channelMask); |
| 143 | if ((pcmConfig.channelMode == ChannelMode::MONO && channelCount != 1) || |
| 144 | (pcmConfig.channelMode == ChannelMode::STEREO && channelCount != 2)) { |
| 145 | LOG(ERROR) << __func__ << ": Channel count mismatch, stream value=" << channelCount |
| 146 | << ", BT HAL value=" << toString(pcmConfig.channelMode); |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 147 | return false; |
| 148 | } |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 149 | if (config.format.type != AudioFormatType::PCM) { |
| 150 | LOG(ERROR) << __func__ |
| 151 | << ": unexpected stream format type: " << toString(config.format.type); |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 152 | return false; |
| 153 | } |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 154 | const int8_t bitsPerSample = |
| 155 | aidl::android::hardware::audio::common::getPcmSampleSizeInBytes(config.format.pcm) * 8; |
| 156 | if (bitsPerSample != pcmConfig.bitsPerSample) { |
| 157 | LOG(ERROR) << __func__ << ": bits per sample mismatch, stream value=" << bitsPerSample |
| 158 | << ", BT HAL value=" << pcmConfig.bitsPerSample; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 159 | return false; |
| 160 | } |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | ndk::ScopedAStatus StreamBluetooth::prepareToClose() { |
| 165 | std::lock_guard guard(mLock); |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 166 | if (mBtDeviceProxy != nullptr) { |
| 167 | if (mBtDeviceProxy->getState() != BluetoothStreamState::DISABLED) { |
| 168 | mBtDeviceProxy->stop(); |
| 169 | } |
| 170 | } |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 171 | return ndk::ScopedAStatus::ok(); |
| 172 | } |
| 173 | |
| 174 | ::android::status_t StreamBluetooth::standby() { |
| 175 | std::lock_guard guard(mLock); |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 176 | if (mBtDeviceProxy != nullptr) mBtDeviceProxy->suspend(); |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 177 | return ::android::OK; |
| 178 | } |
| 179 | |
| 180 | ::android::status_t StreamBluetooth::start() { |
| 181 | std::lock_guard guard(mLock); |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 182 | if (mBtDeviceProxy != nullptr) mBtDeviceProxy->start(); |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 183 | return ::android::OK; |
| 184 | } |
| 185 | |
| 186 | void StreamBluetooth::shutdown() { |
| 187 | std::lock_guard guard(mLock); |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 188 | if (mBtDeviceProxy != nullptr) { |
| 189 | mBtDeviceProxy->stop(); |
| 190 | mBtDeviceProxy = nullptr; |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 191 | } |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | ndk::ScopedAStatus StreamBluetooth::updateMetadataCommon(const Metadata& metadata) { |
| 195 | std::lock_guard guard(mLock); |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 196 | if (mBtDeviceProxy == nullptr) { |
| 197 | return ndk::ScopedAStatus::ok(); |
| 198 | } |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 199 | bool isOk = true; |
| 200 | if (isInput(metadata)) { |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 201 | isOk = mBtDeviceProxy->updateSinkMetadata(std::get<SinkMetadata>(metadata)); |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 202 | } else { |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 203 | isOk = mBtDeviceProxy->updateSourceMetadata(std::get<SourceMetadata>(metadata)); |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 204 | } |
| 205 | return isOk ? ndk::ScopedAStatus::ok() |
| 206 | : ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 207 | } |
| 208 | |
Ram Mohan | 18f0d51 | 2023-07-01 00:47:09 +0530 | [diff] [blame] | 209 | ndk::ScopedAStatus StreamBluetooth::bluetoothParametersUpdated() { |
| 210 | if (mIsInput) { |
Ram Mohan | 18f0d51 | 2023-07-01 00:47:09 +0530 | [diff] [blame] | 211 | return ndk::ScopedAStatus::ok(); |
| 212 | } |
| 213 | auto applyParam = [](const std::shared_ptr<BluetoothAudioPortAidl>& proxy, |
| 214 | bool isEnabled) -> bool { |
| 215 | if (!isEnabled) { |
| 216 | if (proxy->suspend()) return proxy->setState(BluetoothStreamState::DISABLED); |
| 217 | return false; |
| 218 | } |
| 219 | return proxy->standby(); |
| 220 | }; |
| 221 | bool hasA2dpParam, enableA2dp; |
| 222 | auto btA2dp = mBluetoothA2dp.lock(); |
| 223 | hasA2dpParam = btA2dp != nullptr && btA2dp->isEnabled(&enableA2dp).isOk(); |
| 224 | bool hasLeParam, enableLe; |
| 225 | auto btLe = mBluetoothLe.lock(); |
| 226 | hasLeParam = btLe != nullptr && btLe->isEnabled(&enableLe).isOk(); |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 227 | std::lock_guard guard(mLock); |
| 228 | if (mBtDeviceProxy != nullptr) { |
| 229 | if ((hasA2dpParam && mBtDeviceProxy->isA2dp() && !applyParam(mBtDeviceProxy, enableA2dp)) || |
| 230 | (hasLeParam && mBtDeviceProxy->isLeAudio() && !applyParam(mBtDeviceProxy, enableLe))) { |
Ram Mohan | 18f0d51 | 2023-07-01 00:47:09 +0530 | [diff] [blame] | 231 | LOG(DEBUG) << __func__ << ": applyParam failed"; |
| 232 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 233 | } |
| 234 | } |
| 235 | return ndk::ScopedAStatus::ok(); |
| 236 | } |
| 237 | |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 238 | // static |
| 239 | int32_t StreamInBluetooth::getNominalLatencyMs(size_t dataIntervalUs) { |
| 240 | if (dataIntervalUs == 0) dataIntervalUs = kBluetoothDefaultInputBufferMs * 1000LL; |
| 241 | return dataIntervalUs / 1000LL; |
| 242 | } |
| 243 | |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 244 | StreamInBluetooth::StreamInBluetooth(StreamContext&& context, const SinkMetadata& sinkMetadata, |
Ram Mohan | 18f0d51 | 2023-07-01 00:47:09 +0530 | [diff] [blame] | 245 | const std::vector<MicrophoneInfo>& microphones, |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 246 | ModuleBluetooth::BtProfileHandles&& btProfileHandles, |
| 247 | const std::shared_ptr<BluetoothAudioPortAidl>& btDeviceProxy, |
| 248 | const PcmConfiguration& pcmConfig) |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 249 | : StreamIn(std::move(context), microphones), |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 250 | StreamBluetooth(&mContextInstance, sinkMetadata, std::move(btProfileHandles), btDeviceProxy, |
| 251 | pcmConfig) {} |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 252 | |
| 253 | ndk::ScopedAStatus StreamInBluetooth::getActiveMicrophones( |
| 254 | std::vector<MicrophoneDynamicInfo>* _aidl_return __unused) { |
| 255 | LOG(DEBUG) << __func__ << ": not supported"; |
| 256 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 257 | } |
| 258 | |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 259 | // static |
| 260 | int32_t StreamOutBluetooth::getNominalLatencyMs(size_t dataIntervalUs) { |
| 261 | if (dataIntervalUs == 0) dataIntervalUs = kBluetoothDefaultOutputBufferMs * 1000LL; |
| 262 | return dataIntervalUs / 1000LL; |
| 263 | } |
| 264 | |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 265 | StreamOutBluetooth::StreamOutBluetooth(StreamContext&& context, |
| 266 | const SourceMetadata& sourceMetadata, |
Ram Mohan | 18f0d51 | 2023-07-01 00:47:09 +0530 | [diff] [blame] | 267 | const std::optional<AudioOffloadInfo>& offloadInfo, |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 268 | ModuleBluetooth::BtProfileHandles&& btProfileHandles, |
| 269 | const std::shared_ptr<BluetoothAudioPortAidl>& btDeviceProxy, |
| 270 | const PcmConfiguration& pcmConfig) |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 271 | : StreamOut(std::move(context), offloadInfo), |
Mikhail Naganov | a88cf60 | 2023-12-13 14:35:11 -0800 | [diff] [blame] | 272 | StreamBluetooth(&mContextInstance, sourceMetadata, std::move(btProfileHandles), btDeviceProxy, |
| 273 | pcmConfig) {} |
Mikhail Naganov | b03b5c4 | 2023-07-26 13:13:35 -0700 | [diff] [blame] | 274 | |
| 275 | } // namespace aidl::android::hardware::audio::core |