Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "BTAudioClientIf" |
| 18 | |
| 19 | #include "client_interface.h" |
| 20 | |
| 21 | #include <android/hardware/bluetooth/audio/2.0/IBluetoothAudioPort.h> |
| 22 | #include <android/hardware/bluetooth/audio/2.0/IBluetoothAudioProvidersFactory.h> |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame^] | 23 | #include <android/hidl/manager/1.2/IServiceManager.h> |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 24 | #include <base/logging.h> |
| 25 | #include <hidl/MQDescriptor.h> |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame^] | 26 | #include <hidl/ServiceManagement.h> |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 27 | #include <future> |
| 28 | |
| 29 | #include "osi/include/log.h" |
| 30 | |
| 31 | namespace bluetooth { |
| 32 | namespace audio { |
| 33 | |
| 34 | using ::android::hardware::hidl_vec; |
| 35 | using ::android::hardware::Return; |
| 36 | using ::android::hardware::Void; |
| 37 | using ::android::hardware::audio::common::V5_0::SourceMetadata; |
| 38 | using ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioPort; |
| 39 | using ::android::hardware::bluetooth::audio::V2_0:: |
| 40 | IBluetoothAudioProvidersFactory; |
| 41 | using DataMQ = ::android::hardware::MessageQueue< |
| 42 | uint8_t, ::android::hardware::kSynchronizedReadWrite>; |
| 43 | |
| 44 | static constexpr int kDefaultDataReadTimeoutMs = 10; // 10 ms |
| 45 | static constexpr int kDefaultDataReadPollIntervalMs = 1; // non-blocking poll |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame^] | 46 | static constexpr char kFullyQualifiedInterfaceName[] = |
| 47 | "android.hardware.bluetooth.audio@2.0::IBluetoothAudioProvidersFactory"; |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 48 | |
| 49 | std::ostream& operator<<(std::ostream& os, const BluetoothAudioCtrlAck& ack) { |
| 50 | switch (ack) { |
| 51 | case BluetoothAudioCtrlAck::SUCCESS_FINISHED: |
| 52 | return os << "SUCCESS_FINISHED"; |
| 53 | case BluetoothAudioCtrlAck::PENDING: |
| 54 | return os << "PENDING"; |
| 55 | case BluetoothAudioCtrlAck::FAILURE_UNSUPPORTED: |
| 56 | return os << "FAILURE_UNSUPPORTED"; |
| 57 | case BluetoothAudioCtrlAck::FAILURE_BUSY: |
| 58 | return os << "FAILURE_BUSY"; |
| 59 | case BluetoothAudioCtrlAck::FAILURE_DISCONNECTING: |
| 60 | return os << "FAILURE_DISCONNECTING"; |
| 61 | case BluetoothAudioCtrlAck::FAILURE: |
| 62 | return os << "FAILURE"; |
| 63 | default: |
| 64 | return os << "UNDEFINED " << static_cast<int8_t>(ack); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Internal class within BluetoothAudioClientInterfaceace to implement |
| 69 | // IBluetoothAudioPort (control interface used by Bluetooth Audio HAL) |
| 70 | class BluetoothAudioPortImpl : public IBluetoothAudioPort { |
| 71 | public: |
| 72 | BluetoothAudioPortImpl(IBluetoothTransportInstance* sink, |
| 73 | const android::sp<IBluetoothAudioProvider>& provider) |
| 74 | : sink_(sink), provider_(provider){}; |
| 75 | |
| 76 | Return<void> startStream() { |
| 77 | BluetoothAudioCtrlAck ack = sink_->StartRequest(); |
| 78 | if (ack != BluetoothAudioCtrlAck::PENDING) { |
| 79 | auto hidl_retval = |
| 80 | provider_->streamStarted(BluetoothAudioCtrlAckToHalStatus(ack)); |
| 81 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 82 | LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return Void(); |
| 86 | } |
| 87 | |
| 88 | Return<void> suspendStream() { |
| 89 | BluetoothAudioCtrlAck ack = sink_->SuspendRequest(); |
| 90 | if (ack != BluetoothAudioCtrlAck::PENDING) { |
| 91 | auto hidl_retval = |
| 92 | provider_->streamSuspended(BluetoothAudioCtrlAckToHalStatus(ack)); |
| 93 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 94 | LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | return Void(); |
| 98 | } |
| 99 | |
| 100 | Return<void> stopStream() { |
| 101 | sink_->StopRequest(); |
| 102 | return Void(); |
| 103 | } |
| 104 | |
| 105 | Return<void> getPresentationPosition(getPresentationPosition_cb _hidl_cb) { |
| 106 | uint64_t remote_delay_report_ns; |
| 107 | uint64_t total_bytes_read; |
| 108 | timespec data_position; |
| 109 | bool retval = sink_->GetPresentationPosition( |
| 110 | &remote_delay_report_ns, &total_bytes_read, &data_position); |
| 111 | |
| 112 | TimeSpec transmittedOctetsTimeStamp; |
| 113 | if (retval) { |
| 114 | transmittedOctetsTimeStamp = timespec_convert_to_hal(data_position); |
| 115 | } else { |
| 116 | remote_delay_report_ns = 0; |
| 117 | total_bytes_read = 0; |
| 118 | transmittedOctetsTimeStamp = {}; |
| 119 | } |
| 120 | VLOG(2) << __func__ << ": result=" << retval |
| 121 | << ", delay=" << remote_delay_report_ns |
| 122 | << ", data=" << total_bytes_read |
| 123 | << " byte(s), timestamp=" << toString(transmittedOctetsTimeStamp); |
| 124 | _hidl_cb((retval ? BluetoothAudioStatus::SUCCESS |
| 125 | : BluetoothAudioStatus::FAILURE), |
| 126 | remote_delay_report_ns, total_bytes_read, |
| 127 | transmittedOctetsTimeStamp); |
| 128 | return Void(); |
| 129 | } |
| 130 | |
| 131 | Return<void> updateMetadata(const SourceMetadata& sourceMetadata) { |
| 132 | LOG(INFO) << __func__ << ": " << sourceMetadata.tracks.size() |
| 133 | << " track(s)"; |
| 134 | // refer to StreamOut.impl.h within Audio HAL (AUDIO_HAL_VERSION_5_0) |
| 135 | std::vector<playback_track_metadata> metadata_vec; |
| 136 | metadata_vec.reserve(sourceMetadata.tracks.size()); |
| 137 | for (const auto& metadata : sourceMetadata.tracks) { |
| 138 | metadata_vec.push_back({ |
| 139 | .usage = static_cast<audio_usage_t>(metadata.usage), |
| 140 | .content_type = |
| 141 | static_cast<audio_content_type_t>(metadata.contentType), |
| 142 | .gain = metadata.gain, |
| 143 | }); |
| 144 | } |
| 145 | const source_metadata_t source_metadata = { |
| 146 | .track_count = metadata_vec.size(), .tracks = metadata_vec.data()}; |
| 147 | sink_->MetadataChanged(source_metadata); |
| 148 | return Void(); |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | IBluetoothTransportInstance* sink_; |
| 153 | const android::sp<IBluetoothAudioProvider> provider_; |
| 154 | TimeSpec timespec_convert_to_hal(const timespec& ts) { |
| 155 | return {.tvSec = static_cast<uint64_t>(ts.tv_sec), |
| 156 | .tvNSec = static_cast<uint64_t>(ts.tv_nsec)}; |
| 157 | } |
| 158 | }; |
| 159 | |
| 160 | class BluetoothAudioDeathRecipient |
| 161 | : public ::android::hardware::hidl_death_recipient { |
| 162 | public: |
| 163 | BluetoothAudioDeathRecipient( |
| 164 | BluetoothAudioClientInterface* clientif, |
| 165 | bluetooth::common::MessageLoopThread* message_loop) |
| 166 | : bluetooth_audio_clientif_(clientif), message_loop_(message_loop) {} |
| 167 | void serviceDied( |
| 168 | uint64_t /*cookie*/, |
| 169 | const ::android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) { |
| 170 | LOG(WARNING) << __func__ << ": restarting connection with new Audio Hal"; |
| 171 | if (bluetooth_audio_clientif_ != nullptr && message_loop_ != nullptr) { |
| 172 | // restart the session on the correct thread |
| 173 | message_loop_->DoInThread( |
| 174 | FROM_HERE, |
| 175 | base::BindOnce(&RenewAudioProviderAndSession, |
| 176 | base::Unretained(bluetooth_audio_clientif_))); |
| 177 | } else { |
| 178 | LOG(ERROR) << __func__ << ": BluetoothAudioClientInterface corrupted"; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private: |
| 183 | BluetoothAudioClientInterface* bluetooth_audio_clientif_; |
| 184 | bluetooth::common::MessageLoopThread* message_loop_; |
| 185 | static void RenewAudioProviderAndSession( |
| 186 | BluetoothAudioClientInterface* bluetooth_audio_clientif) { |
| 187 | if (bluetooth_audio_clientif != nullptr) { |
| 188 | bluetooth_audio_clientif->RenewAudioProviderAndSession(); |
| 189 | } |
| 190 | } |
| 191 | }; |
| 192 | |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame^] | 193 | BluetoothAudioClientInterface::BluetoothAudioClientInterface(IBluetoothTransportInstance* sink, |
| 194 | bluetooth::common::MessageLoopThread* message_loop) |
| 195 | : sink_(sink), provider_(nullptr), session_started_(false), mDataMQ(nullptr), |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 196 | death_recipient_(new BluetoothAudioDeathRecipient(this, message_loop)) { |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame^] | 197 | auto service_manager = android::hardware::defaultServiceManager1_2(); |
| 198 | CHECK(service_manager != nullptr); |
| 199 | size_t instance_count = 0; |
| 200 | auto listManifestByInterface_cb = [&instance_count](const hidl_vec<android::hardware::hidl_string>& instanceNames) { |
| 201 | instance_count = instanceNames.size(); |
| 202 | LOG(INFO) << "listManifestByInterface_cb returns " << instance_count << " instance(s)"; |
| 203 | }; |
| 204 | auto hidl_retval = service_manager->listManifestByInterface(kFullyQualifiedInterfaceName, listManifestByInterface_cb); |
| 205 | if (!hidl_retval.isOk()) { |
| 206 | LOG(FATAL) << __func__ << ": IServiceManager::listByInterface failure: " << hidl_retval.description(); |
| 207 | } |
| 208 | if (instance_count > 0) { |
| 209 | fetch_audio_provider(); |
| 210 | } else { |
| 211 | LOG(WARNING) << "IBluetoothAudioProvidersFactory not declared"; |
| 212 | } |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | BluetoothAudioClientInterface::~BluetoothAudioClientInterface() { |
| 216 | if (provider_ != nullptr) { |
| 217 | auto hidl_retval = provider_->unlinkToDeath(death_recipient_); |
| 218 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 219 | LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | std::vector<AudioCapabilities> |
| 225 | BluetoothAudioClientInterface::GetAudioCapabilities() const { |
| 226 | return capabilities_; |
| 227 | } |
| 228 | |
| 229 | void BluetoothAudioClientInterface::fetch_audio_provider() { |
| 230 | if (provider_ != nullptr) { |
| 231 | LOG(WARNING) << __func__ << ": reflash"; |
| 232 | } |
| 233 | |
| 234 | android::sp<IBluetoothAudioProvidersFactory> providersFactory = |
| 235 | IBluetoothAudioProvidersFactory::getService(); |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame^] | 236 | CHECK(providersFactory != nullptr) << "IBluetoothAudioProvidersFactory::getService() failed"; |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 237 | LOG(INFO) << "IBluetoothAudioProvidersFactory::getService() returned " |
| 238 | << providersFactory.get() |
| 239 | << (providersFactory->isRemote() ? " (remote)" : " (local)"); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 240 | |
| 241 | std::promise<void> getProviderCapabilities_promise; |
| 242 | auto getProviderCapabilities_future = |
| 243 | getProviderCapabilities_promise.get_future(); |
| 244 | auto getProviderCapabilities_cb = |
| 245 | [& capabilities = this->capabilities_, &getProviderCapabilities_promise]( |
| 246 | const hidl_vec<AudioCapabilities>& audioCapabilities) { |
| 247 | for (auto capability : audioCapabilities) { |
| 248 | capabilities.push_back(capability); |
| 249 | } |
| 250 | getProviderCapabilities_promise.set_value(); |
| 251 | }; |
| 252 | auto hidl_retval = providersFactory->getProviderCapabilities( |
| 253 | sink_->GetSessionType(), getProviderCapabilities_cb); |
| 254 | getProviderCapabilities_future.get(); |
| 255 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 256 | LOG(FATAL) << __func__ << ": BluetoothAudioHal::getProviderCapabilities failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 257 | return; |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 258 | } |
| 259 | if (capabilities_.empty()) { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 260 | LOG(WARNING) << __func__ |
| 261 | << ": SessionType=" << toString(sink_->GetSessionType()) |
| 262 | << " Not supported by BluetoothAudioHal"; |
| 263 | return; |
| 264 | } |
| 265 | LOG(INFO) << __func__ << ": BluetoothAudioHal SessionType=" |
| 266 | << toString(sink_->GetSessionType()) << " has " |
| 267 | << capabilities_.size() << " AudioCapabilities"; |
| 268 | |
| 269 | std::promise<void> openProvider_promise; |
| 270 | auto openProvider_future = openProvider_promise.get_future(); |
| 271 | auto openProvider_cb = |
| 272 | [& provider_ = this->provider_, &openProvider_promise]( |
| 273 | BluetoothAudioStatus status, |
| 274 | const android::sp<IBluetoothAudioProvider>& provider) { |
| 275 | LOG(INFO) << "openProvider_cb(" << toString(status) << ")"; |
| 276 | if (status == BluetoothAudioStatus::SUCCESS) { |
| 277 | provider_ = provider; |
| 278 | } |
| 279 | ALOGE_IF(!provider_, "Failed to open BluetoothAudio provider"); |
| 280 | openProvider_promise.set_value(); |
| 281 | }; |
| 282 | hidl_retval = |
| 283 | providersFactory->openProvider(sink_->GetSessionType(), openProvider_cb); |
| 284 | openProvider_future.get(); |
| 285 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 286 | LOG(FATAL) << __func__ << ": BluetoothAudioHal::openProvider failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 287 | } |
| 288 | CHECK(provider_ != nullptr); |
| 289 | |
| 290 | if (!provider_->linkToDeath(death_recipient_, 0).isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 291 | LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | LOG(INFO) << "IBluetoothAudioProvidersFactory::openProvider() returned " |
| 295 | << provider_.get() |
| 296 | << (provider_->isRemote() ? " (remote)" : " (local)"); |
| 297 | } |
| 298 | |
| 299 | bool BluetoothAudioClientInterface::UpdateAudioConfig( |
| 300 | const AudioConfiguration& audio_config) { |
| 301 | bool is_software_session = |
| 302 | (sink_->GetSessionType() == |
| 303 | SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH || |
| 304 | sink_->GetSessionType() == |
| 305 | SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH); |
| 306 | bool is_offload_session = |
| 307 | (sink_->GetSessionType() == SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH); |
| 308 | auto audio_config_discriminator = audio_config.getDiscriminator(); |
| 309 | bool is_software_audio_config = |
| 310 | (is_software_session && |
| 311 | audio_config_discriminator == |
| 312 | AudioConfiguration::hidl_discriminator::pcmConfig); |
| 313 | bool is_offload_audio_config = |
| 314 | (is_offload_session && |
| 315 | audio_config_discriminator == |
| 316 | AudioConfiguration::hidl_discriminator::codecConfig); |
| 317 | if (!is_software_audio_config && !is_offload_audio_config) { |
| 318 | return false; |
| 319 | } |
| 320 | sink_->UpdateAudioConfiguration(audio_config); |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | int BluetoothAudioClientInterface::StartSession() { |
| 325 | std::lock_guard<std::mutex> guard(internal_mutex_); |
| 326 | if (provider_ == nullptr) { |
| 327 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 328 | session_started_ = false; |
| 329 | return -EINVAL; |
| 330 | } |
| 331 | if (session_started_) { |
| 332 | LOG(ERROR) << __func__ << ": session started already"; |
| 333 | return -EBUSY; |
| 334 | } |
| 335 | |
| 336 | android::sp<IBluetoothAudioPort> stack_if = |
| 337 | new BluetoothAudioPortImpl(sink_, provider_); |
| 338 | std::unique_ptr<DataMQ> tempDataMQ; |
| 339 | BluetoothAudioStatus session_status; |
| 340 | |
| 341 | std::promise<void> hidl_startSession_promise; |
| 342 | auto hidl_startSession_future = hidl_startSession_promise.get_future(); |
| 343 | auto hidl_cb = [&session_status, &tempDataMQ, &hidl_startSession_promise]( |
| 344 | BluetoothAudioStatus status, |
| 345 | const DataMQ::Descriptor& dataMQ) { |
| 346 | LOG(INFO) << "startSession_cb(" << toString(status) << ")"; |
| 347 | session_status = status; |
| 348 | if (status == BluetoothAudioStatus::SUCCESS && dataMQ.isHandleValid()) { |
| 349 | tempDataMQ.reset(new DataMQ(dataMQ)); |
| 350 | } |
| 351 | hidl_startSession_promise.set_value(); |
| 352 | }; |
| 353 | auto hidl_retval = provider_->startSession( |
| 354 | stack_if, sink_->GetAudioConfiguration(), hidl_cb); |
| 355 | hidl_startSession_future.get(); |
| 356 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 357 | LOG(FATAL) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 358 | return -EPROTO; |
| 359 | } |
| 360 | |
| 361 | if (tempDataMQ && tempDataMQ->isValid()) { |
| 362 | mDataMQ = std::move(tempDataMQ); |
| 363 | } else if (sink_->GetSessionType() == |
| 364 | SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH && |
| 365 | session_status == BluetoothAudioStatus::SUCCESS) { |
| 366 | sink_->ResetPresentationPosition(); |
| 367 | session_started_ = true; |
| 368 | return 0; |
| 369 | } |
| 370 | if (mDataMQ && mDataMQ->isValid()) { |
| 371 | sink_->ResetPresentationPosition(); |
| 372 | session_started_ = true; |
| 373 | return 0; |
| 374 | } else { |
| 375 | ALOGE_IF(!mDataMQ, "Failed to obtain audio data path"); |
| 376 | ALOGE_IF(mDataMQ && !mDataMQ->isValid(), "Audio data path is invalid"); |
| 377 | session_started_ = false; |
| 378 | return -EIO; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void BluetoothAudioClientInterface::StreamStarted( |
| 383 | const BluetoothAudioCtrlAck& ack) { |
| 384 | if (provider_ == nullptr) { |
| 385 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 386 | return; |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 387 | } |
| 388 | if (ack == BluetoothAudioCtrlAck::PENDING) { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 389 | LOG(INFO) << __func__ << ": " << ack << " ignored"; |
| 390 | return; |
| 391 | } |
| 392 | BluetoothAudioStatus status = BluetoothAudioCtrlAckToHalStatus(ack); |
| 393 | auto hidl_retval = provider_->streamStarted(status); |
| 394 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 395 | LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | void BluetoothAudioClientInterface::StreamSuspended( |
| 400 | const BluetoothAudioCtrlAck& ack) { |
| 401 | if (provider_ == nullptr) { |
| 402 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 403 | return; |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 404 | } |
| 405 | if (ack == BluetoothAudioCtrlAck::PENDING) { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 406 | LOG(INFO) << __func__ << ": " << ack << " ignored"; |
| 407 | return; |
| 408 | } |
| 409 | BluetoothAudioStatus status = BluetoothAudioCtrlAckToHalStatus(ack); |
| 410 | auto hidl_retval = provider_->streamSuspended(status); |
| 411 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 412 | LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | int BluetoothAudioClientInterface::EndSession() { |
| 417 | std::lock_guard<std::mutex> guard(internal_mutex_); |
| 418 | if (!session_started_) { |
| 419 | LOG(INFO) << __func__ << ": sessoin ended already"; |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | session_started_ = false; |
| 424 | if (provider_ == nullptr) { |
| 425 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 426 | return -EINVAL; |
| 427 | } |
| 428 | mDataMQ = nullptr; |
| 429 | auto hidl_retval = provider_->endSession(); |
| 430 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 431 | LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 432 | return -EPROTO; |
| 433 | } |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | size_t BluetoothAudioClientInterface::ReadAudioData(uint8_t* p_buf, |
| 438 | uint32_t len) { |
| 439 | if (provider_ == nullptr) { |
| 440 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 441 | return 0; |
| 442 | } |
| 443 | if (p_buf == nullptr || len == 0) return 0; |
| 444 | |
| 445 | std::lock_guard<std::mutex> guard(internal_mutex_); |
| 446 | |
| 447 | size_t total_read = 0; |
| 448 | int timeout_ms = kDefaultDataReadTimeoutMs; |
| 449 | do { |
| 450 | if (mDataMQ == nullptr || !mDataMQ->isValid()) break; |
| 451 | |
| 452 | size_t avail_to_read = mDataMQ->availableToRead(); |
| 453 | if (avail_to_read) { |
| 454 | if (avail_to_read > len - total_read) { |
| 455 | avail_to_read = len - total_read; |
| 456 | } |
| 457 | if (mDataMQ->read(p_buf + total_read, avail_to_read) == 0) { |
| 458 | LOG(WARNING) << __func__ << ": len=" << len |
| 459 | << " total_read=" << total_read << " failed"; |
| 460 | break; |
| 461 | } |
| 462 | total_read += avail_to_read; |
| 463 | } else if (timeout_ms >= kDefaultDataReadPollIntervalMs) { |
| 464 | std::this_thread::sleep_for( |
| 465 | std::chrono::milliseconds(kDefaultDataReadPollIntervalMs)); |
| 466 | timeout_ms -= kDefaultDataReadPollIntervalMs; |
| 467 | continue; |
| 468 | } else { |
| 469 | LOG(WARNING) << __func__ << ": " << (len - total_read) << "/" << len |
| 470 | << " no data " << (kDefaultDataReadTimeoutMs - timeout_ms) |
| 471 | << " ms"; |
| 472 | break; |
| 473 | } |
| 474 | } while (total_read < len); |
| 475 | |
| 476 | if (timeout_ms < |
| 477 | (kDefaultDataReadTimeoutMs - kDefaultDataReadPollIntervalMs) && |
| 478 | timeout_ms >= kDefaultDataReadPollIntervalMs) { |
| 479 | VLOG(1) << __func__ << ": underflow " << len << " -> " << total_read |
| 480 | << " read " << (kDefaultDataReadTimeoutMs - timeout_ms) << " ms"; |
| 481 | } else { |
| 482 | VLOG(2) << __func__ << ": " << len << " -> " << total_read << " read"; |
| 483 | } |
| 484 | |
| 485 | sink_->LogBytesRead(total_read); |
| 486 | return total_read; |
| 487 | } |
| 488 | |
| 489 | size_t BluetoothAudioClientInterface::WriteAudioData(uint8_t* p_buf, |
| 490 | uint32_t len) { |
| 491 | // Not implemented! |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | void BluetoothAudioClientInterface::RenewAudioProviderAndSession() { |
| 496 | // NOTE: must be invoked on the same thread where this |
| 497 | // BluetoothAudioClientInterface is running |
| 498 | fetch_audio_provider(); |
| 499 | session_started_ = false; |
| 500 | StartSession(); |
| 501 | } |
| 502 | |
| 503 | } // namespace audio |
| 504 | } // namespace bluetooth |