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 | |
Yi Kong | 183eace | 2019-03-29 15:42:58 -0700 | [diff] [blame^] | 76 | Return<void> startStream() override { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 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 | |
Yi Kong | 183eace | 2019-03-29 15:42:58 -0700 | [diff] [blame^] | 88 | Return<void> suspendStream() override { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 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 | |
Yi Kong | 183eace | 2019-03-29 15:42:58 -0700 | [diff] [blame^] | 100 | Return<void> stopStream() override { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 101 | sink_->StopRequest(); |
| 102 | return Void(); |
| 103 | } |
| 104 | |
Yi Kong | 183eace | 2019-03-29 15:42:58 -0700 | [diff] [blame^] | 105 | Return<void> getPresentationPosition( |
| 106 | getPresentationPosition_cb _hidl_cb) override { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 107 | uint64_t remote_delay_report_ns; |
| 108 | uint64_t total_bytes_read; |
| 109 | timespec data_position; |
| 110 | bool retval = sink_->GetPresentationPosition( |
| 111 | &remote_delay_report_ns, &total_bytes_read, &data_position); |
| 112 | |
| 113 | TimeSpec transmittedOctetsTimeStamp; |
| 114 | if (retval) { |
| 115 | transmittedOctetsTimeStamp = timespec_convert_to_hal(data_position); |
| 116 | } else { |
| 117 | remote_delay_report_ns = 0; |
| 118 | total_bytes_read = 0; |
| 119 | transmittedOctetsTimeStamp = {}; |
| 120 | } |
| 121 | VLOG(2) << __func__ << ": result=" << retval |
| 122 | << ", delay=" << remote_delay_report_ns |
| 123 | << ", data=" << total_bytes_read |
| 124 | << " byte(s), timestamp=" << toString(transmittedOctetsTimeStamp); |
| 125 | _hidl_cb((retval ? BluetoothAudioStatus::SUCCESS |
| 126 | : BluetoothAudioStatus::FAILURE), |
| 127 | remote_delay_report_ns, total_bytes_read, |
| 128 | transmittedOctetsTimeStamp); |
| 129 | return Void(); |
| 130 | } |
| 131 | |
Yi Kong | 183eace | 2019-03-29 15:42:58 -0700 | [diff] [blame^] | 132 | Return<void> updateMetadata(const SourceMetadata& sourceMetadata) override { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 133 | LOG(INFO) << __func__ << ": " << sourceMetadata.tracks.size() |
| 134 | << " track(s)"; |
| 135 | // refer to StreamOut.impl.h within Audio HAL (AUDIO_HAL_VERSION_5_0) |
| 136 | std::vector<playback_track_metadata> metadata_vec; |
| 137 | metadata_vec.reserve(sourceMetadata.tracks.size()); |
| 138 | for (const auto& metadata : sourceMetadata.tracks) { |
| 139 | metadata_vec.push_back({ |
| 140 | .usage = static_cast<audio_usage_t>(metadata.usage), |
| 141 | .content_type = |
| 142 | static_cast<audio_content_type_t>(metadata.contentType), |
| 143 | .gain = metadata.gain, |
| 144 | }); |
| 145 | } |
| 146 | const source_metadata_t source_metadata = { |
| 147 | .track_count = metadata_vec.size(), .tracks = metadata_vec.data()}; |
| 148 | sink_->MetadataChanged(source_metadata); |
| 149 | return Void(); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | IBluetoothTransportInstance* sink_; |
| 154 | const android::sp<IBluetoothAudioProvider> provider_; |
| 155 | TimeSpec timespec_convert_to_hal(const timespec& ts) { |
| 156 | return {.tvSec = static_cast<uint64_t>(ts.tv_sec), |
| 157 | .tvNSec = static_cast<uint64_t>(ts.tv_nsec)}; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | class BluetoothAudioDeathRecipient |
| 162 | : public ::android::hardware::hidl_death_recipient { |
| 163 | public: |
| 164 | BluetoothAudioDeathRecipient( |
| 165 | BluetoothAudioClientInterface* clientif, |
| 166 | bluetooth::common::MessageLoopThread* message_loop) |
| 167 | : bluetooth_audio_clientif_(clientif), message_loop_(message_loop) {} |
| 168 | void serviceDied( |
| 169 | uint64_t /*cookie*/, |
Yi Kong | 183eace | 2019-03-29 15:42:58 -0700 | [diff] [blame^] | 170 | const ::android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) |
| 171 | override { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 172 | LOG(WARNING) << __func__ << ": restarting connection with new Audio Hal"; |
| 173 | if (bluetooth_audio_clientif_ != nullptr && message_loop_ != nullptr) { |
| 174 | // restart the session on the correct thread |
| 175 | message_loop_->DoInThread( |
| 176 | FROM_HERE, |
| 177 | base::BindOnce(&RenewAudioProviderAndSession, |
| 178 | base::Unretained(bluetooth_audio_clientif_))); |
| 179 | } else { |
| 180 | LOG(ERROR) << __func__ << ": BluetoothAudioClientInterface corrupted"; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private: |
| 185 | BluetoothAudioClientInterface* bluetooth_audio_clientif_; |
| 186 | bluetooth::common::MessageLoopThread* message_loop_; |
| 187 | static void RenewAudioProviderAndSession( |
| 188 | BluetoothAudioClientInterface* bluetooth_audio_clientif) { |
| 189 | if (bluetooth_audio_clientif != nullptr) { |
| 190 | bluetooth_audio_clientif->RenewAudioProviderAndSession(); |
| 191 | } |
| 192 | } |
| 193 | }; |
| 194 | |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame] | 195 | BluetoothAudioClientInterface::BluetoothAudioClientInterface(IBluetoothTransportInstance* sink, |
| 196 | bluetooth::common::MessageLoopThread* message_loop) |
| 197 | : sink_(sink), provider_(nullptr), session_started_(false), mDataMQ(nullptr), |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 198 | death_recipient_(new BluetoothAudioDeathRecipient(this, message_loop)) { |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame] | 199 | auto service_manager = android::hardware::defaultServiceManager1_2(); |
| 200 | CHECK(service_manager != nullptr); |
| 201 | size_t instance_count = 0; |
| 202 | auto listManifestByInterface_cb = [&instance_count](const hidl_vec<android::hardware::hidl_string>& instanceNames) { |
| 203 | instance_count = instanceNames.size(); |
| 204 | LOG(INFO) << "listManifestByInterface_cb returns " << instance_count << " instance(s)"; |
| 205 | }; |
| 206 | auto hidl_retval = service_manager->listManifestByInterface(kFullyQualifiedInterfaceName, listManifestByInterface_cb); |
| 207 | if (!hidl_retval.isOk()) { |
| 208 | LOG(FATAL) << __func__ << ": IServiceManager::listByInterface failure: " << hidl_retval.description(); |
| 209 | } |
| 210 | if (instance_count > 0) { |
| 211 | fetch_audio_provider(); |
| 212 | } else { |
| 213 | LOG(WARNING) << "IBluetoothAudioProvidersFactory not declared"; |
| 214 | } |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | BluetoothAudioClientInterface::~BluetoothAudioClientInterface() { |
| 218 | if (provider_ != nullptr) { |
| 219 | auto hidl_retval = provider_->unlinkToDeath(death_recipient_); |
| 220 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 221 | LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | std::vector<AudioCapabilities> |
| 227 | BluetoothAudioClientInterface::GetAudioCapabilities() const { |
| 228 | return capabilities_; |
| 229 | } |
| 230 | |
| 231 | void BluetoothAudioClientInterface::fetch_audio_provider() { |
| 232 | if (provider_ != nullptr) { |
| 233 | LOG(WARNING) << __func__ << ": reflash"; |
| 234 | } |
| 235 | |
| 236 | android::sp<IBluetoothAudioProvidersFactory> providersFactory = |
| 237 | IBluetoothAudioProvidersFactory::getService(); |
Cheney Ni | 3abcc01 | 2019-03-14 20:58:59 +0800 | [diff] [blame] | 238 | CHECK(providersFactory != nullptr) << "IBluetoothAudioProvidersFactory::getService() failed"; |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 239 | LOG(INFO) << "IBluetoothAudioProvidersFactory::getService() returned " |
| 240 | << providersFactory.get() |
| 241 | << (providersFactory->isRemote() ? " (remote)" : " (local)"); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 242 | |
| 243 | std::promise<void> getProviderCapabilities_promise; |
| 244 | auto getProviderCapabilities_future = |
| 245 | getProviderCapabilities_promise.get_future(); |
| 246 | auto getProviderCapabilities_cb = |
| 247 | [& capabilities = this->capabilities_, &getProviderCapabilities_promise]( |
| 248 | const hidl_vec<AudioCapabilities>& audioCapabilities) { |
| 249 | for (auto capability : audioCapabilities) { |
| 250 | capabilities.push_back(capability); |
| 251 | } |
| 252 | getProviderCapabilities_promise.set_value(); |
| 253 | }; |
| 254 | auto hidl_retval = providersFactory->getProviderCapabilities( |
| 255 | sink_->GetSessionType(), getProviderCapabilities_cb); |
| 256 | getProviderCapabilities_future.get(); |
| 257 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 258 | LOG(FATAL) << __func__ << ": BluetoothAudioHal::getProviderCapabilities failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 259 | return; |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 260 | } |
| 261 | if (capabilities_.empty()) { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 262 | LOG(WARNING) << __func__ |
| 263 | << ": SessionType=" << toString(sink_->GetSessionType()) |
| 264 | << " Not supported by BluetoothAudioHal"; |
| 265 | return; |
| 266 | } |
| 267 | LOG(INFO) << __func__ << ": BluetoothAudioHal SessionType=" |
| 268 | << toString(sink_->GetSessionType()) << " has " |
| 269 | << capabilities_.size() << " AudioCapabilities"; |
| 270 | |
| 271 | std::promise<void> openProvider_promise; |
| 272 | auto openProvider_future = openProvider_promise.get_future(); |
| 273 | auto openProvider_cb = |
| 274 | [& provider_ = this->provider_, &openProvider_promise]( |
| 275 | BluetoothAudioStatus status, |
| 276 | const android::sp<IBluetoothAudioProvider>& provider) { |
| 277 | LOG(INFO) << "openProvider_cb(" << toString(status) << ")"; |
| 278 | if (status == BluetoothAudioStatus::SUCCESS) { |
| 279 | provider_ = provider; |
| 280 | } |
| 281 | ALOGE_IF(!provider_, "Failed to open BluetoothAudio provider"); |
| 282 | openProvider_promise.set_value(); |
| 283 | }; |
| 284 | hidl_retval = |
| 285 | providersFactory->openProvider(sink_->GetSessionType(), openProvider_cb); |
| 286 | openProvider_future.get(); |
| 287 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 288 | LOG(FATAL) << __func__ << ": BluetoothAudioHal::openProvider failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 289 | } |
| 290 | CHECK(provider_ != nullptr); |
| 291 | |
| 292 | if (!provider_->linkToDeath(death_recipient_, 0).isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 293 | LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | LOG(INFO) << "IBluetoothAudioProvidersFactory::openProvider() returned " |
| 297 | << provider_.get() |
| 298 | << (provider_->isRemote() ? " (remote)" : " (local)"); |
| 299 | } |
| 300 | |
| 301 | bool BluetoothAudioClientInterface::UpdateAudioConfig( |
| 302 | const AudioConfiguration& audio_config) { |
| 303 | bool is_software_session = |
| 304 | (sink_->GetSessionType() == |
| 305 | SessionType::A2DP_SOFTWARE_ENCODING_DATAPATH || |
| 306 | sink_->GetSessionType() == |
| 307 | SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH); |
| 308 | bool is_offload_session = |
| 309 | (sink_->GetSessionType() == SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH); |
| 310 | auto audio_config_discriminator = audio_config.getDiscriminator(); |
| 311 | bool is_software_audio_config = |
| 312 | (is_software_session && |
| 313 | audio_config_discriminator == |
| 314 | AudioConfiguration::hidl_discriminator::pcmConfig); |
| 315 | bool is_offload_audio_config = |
| 316 | (is_offload_session && |
| 317 | audio_config_discriminator == |
| 318 | AudioConfiguration::hidl_discriminator::codecConfig); |
| 319 | if (!is_software_audio_config && !is_offload_audio_config) { |
| 320 | return false; |
| 321 | } |
| 322 | sink_->UpdateAudioConfiguration(audio_config); |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | int BluetoothAudioClientInterface::StartSession() { |
| 327 | std::lock_guard<std::mutex> guard(internal_mutex_); |
| 328 | if (provider_ == nullptr) { |
| 329 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 330 | session_started_ = false; |
| 331 | return -EINVAL; |
| 332 | } |
| 333 | if (session_started_) { |
| 334 | LOG(ERROR) << __func__ << ": session started already"; |
| 335 | return -EBUSY; |
| 336 | } |
| 337 | |
| 338 | android::sp<IBluetoothAudioPort> stack_if = |
| 339 | new BluetoothAudioPortImpl(sink_, provider_); |
| 340 | std::unique_ptr<DataMQ> tempDataMQ; |
| 341 | BluetoothAudioStatus session_status; |
| 342 | |
| 343 | std::promise<void> hidl_startSession_promise; |
| 344 | auto hidl_startSession_future = hidl_startSession_promise.get_future(); |
| 345 | auto hidl_cb = [&session_status, &tempDataMQ, &hidl_startSession_promise]( |
| 346 | BluetoothAudioStatus status, |
| 347 | const DataMQ::Descriptor& dataMQ) { |
| 348 | LOG(INFO) << "startSession_cb(" << toString(status) << ")"; |
| 349 | session_status = status; |
| 350 | if (status == BluetoothAudioStatus::SUCCESS && dataMQ.isHandleValid()) { |
| 351 | tempDataMQ.reset(new DataMQ(dataMQ)); |
| 352 | } |
| 353 | hidl_startSession_promise.set_value(); |
| 354 | }; |
| 355 | auto hidl_retval = provider_->startSession( |
| 356 | stack_if, sink_->GetAudioConfiguration(), hidl_cb); |
| 357 | hidl_startSession_future.get(); |
| 358 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 359 | LOG(FATAL) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 360 | return -EPROTO; |
| 361 | } |
| 362 | |
| 363 | if (tempDataMQ && tempDataMQ->isValid()) { |
| 364 | mDataMQ = std::move(tempDataMQ); |
| 365 | } else if (sink_->GetSessionType() == |
| 366 | SessionType::A2DP_HARDWARE_OFFLOAD_DATAPATH && |
| 367 | session_status == BluetoothAudioStatus::SUCCESS) { |
| 368 | sink_->ResetPresentationPosition(); |
| 369 | session_started_ = true; |
| 370 | return 0; |
| 371 | } |
| 372 | if (mDataMQ && mDataMQ->isValid()) { |
| 373 | sink_->ResetPresentationPosition(); |
| 374 | session_started_ = true; |
| 375 | return 0; |
| 376 | } else { |
| 377 | ALOGE_IF(!mDataMQ, "Failed to obtain audio data path"); |
| 378 | ALOGE_IF(mDataMQ && !mDataMQ->isValid(), "Audio data path is invalid"); |
| 379 | session_started_ = false; |
| 380 | return -EIO; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void BluetoothAudioClientInterface::StreamStarted( |
| 385 | const BluetoothAudioCtrlAck& ack) { |
| 386 | if (provider_ == nullptr) { |
| 387 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 388 | return; |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 389 | } |
| 390 | if (ack == BluetoothAudioCtrlAck::PENDING) { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 391 | LOG(INFO) << __func__ << ": " << ack << " ignored"; |
| 392 | return; |
| 393 | } |
| 394 | BluetoothAudioStatus status = BluetoothAudioCtrlAckToHalStatus(ack); |
| 395 | auto hidl_retval = provider_->streamStarted(status); |
| 396 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 397 | LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | void BluetoothAudioClientInterface::StreamSuspended( |
| 402 | const BluetoothAudioCtrlAck& ack) { |
| 403 | if (provider_ == nullptr) { |
| 404 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 405 | return; |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 406 | } |
| 407 | if (ack == BluetoothAudioCtrlAck::PENDING) { |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 408 | LOG(INFO) << __func__ << ": " << ack << " ignored"; |
| 409 | return; |
| 410 | } |
| 411 | BluetoothAudioStatus status = BluetoothAudioCtrlAckToHalStatus(ack); |
| 412 | auto hidl_retval = provider_->streamSuspended(status); |
| 413 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 414 | LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
| 418 | int BluetoothAudioClientInterface::EndSession() { |
| 419 | std::lock_guard<std::mutex> guard(internal_mutex_); |
| 420 | if (!session_started_) { |
| 421 | LOG(INFO) << __func__ << ": sessoin ended already"; |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | session_started_ = false; |
| 426 | if (provider_ == nullptr) { |
| 427 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 428 | return -EINVAL; |
| 429 | } |
| 430 | mDataMQ = nullptr; |
| 431 | auto hidl_retval = provider_->endSession(); |
| 432 | if (!hidl_retval.isOk()) { |
Cheney Ni | fff829f | 2019-03-07 14:15:27 +0800 | [diff] [blame] | 433 | LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description(); |
Cheney Ni | ad05f3e | 2018-11-08 16:41:02 +0800 | [diff] [blame] | 434 | return -EPROTO; |
| 435 | } |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | size_t BluetoothAudioClientInterface::ReadAudioData(uint8_t* p_buf, |
| 440 | uint32_t len) { |
| 441 | if (provider_ == nullptr) { |
| 442 | LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr"; |
| 443 | return 0; |
| 444 | } |
| 445 | if (p_buf == nullptr || len == 0) return 0; |
| 446 | |
| 447 | std::lock_guard<std::mutex> guard(internal_mutex_); |
| 448 | |
| 449 | size_t total_read = 0; |
| 450 | int timeout_ms = kDefaultDataReadTimeoutMs; |
| 451 | do { |
| 452 | if (mDataMQ == nullptr || !mDataMQ->isValid()) break; |
| 453 | |
| 454 | size_t avail_to_read = mDataMQ->availableToRead(); |
| 455 | if (avail_to_read) { |
| 456 | if (avail_to_read > len - total_read) { |
| 457 | avail_to_read = len - total_read; |
| 458 | } |
| 459 | if (mDataMQ->read(p_buf + total_read, avail_to_read) == 0) { |
| 460 | LOG(WARNING) << __func__ << ": len=" << len |
| 461 | << " total_read=" << total_read << " failed"; |
| 462 | break; |
| 463 | } |
| 464 | total_read += avail_to_read; |
| 465 | } else if (timeout_ms >= kDefaultDataReadPollIntervalMs) { |
| 466 | std::this_thread::sleep_for( |
| 467 | std::chrono::milliseconds(kDefaultDataReadPollIntervalMs)); |
| 468 | timeout_ms -= kDefaultDataReadPollIntervalMs; |
| 469 | continue; |
| 470 | } else { |
| 471 | LOG(WARNING) << __func__ << ": " << (len - total_read) << "/" << len |
| 472 | << " no data " << (kDefaultDataReadTimeoutMs - timeout_ms) |
| 473 | << " ms"; |
| 474 | break; |
| 475 | } |
| 476 | } while (total_read < len); |
| 477 | |
| 478 | if (timeout_ms < |
| 479 | (kDefaultDataReadTimeoutMs - kDefaultDataReadPollIntervalMs) && |
| 480 | timeout_ms >= kDefaultDataReadPollIntervalMs) { |
| 481 | VLOG(1) << __func__ << ": underflow " << len << " -> " << total_read |
| 482 | << " read " << (kDefaultDataReadTimeoutMs - timeout_ms) << " ms"; |
| 483 | } else { |
| 484 | VLOG(2) << __func__ << ": " << len << " -> " << total_read << " read"; |
| 485 | } |
| 486 | |
| 487 | sink_->LogBytesRead(total_read); |
| 488 | return total_read; |
| 489 | } |
| 490 | |
| 491 | size_t BluetoothAudioClientInterface::WriteAudioData(uint8_t* p_buf, |
| 492 | uint32_t len) { |
| 493 | // Not implemented! |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | void BluetoothAudioClientInterface::RenewAudioProviderAndSession() { |
| 498 | // NOTE: must be invoked on the same thread where this |
| 499 | // BluetoothAudioClientInterface is running |
| 500 | fetch_audio_provider(); |
| 501 | session_started_ = false; |
| 502 | StartSession(); |
| 503 | } |
| 504 | |
| 505 | } // namespace audio |
| 506 | } // namespace bluetooth |