blob: e6d6a6b6de30273f983bcdb855649615823140bc [file] [log] [blame]
Cheney Niad05f3e2018-11-08 16:41:02 +08001/*
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 Ni3abcc012019-03-14 20:58:59 +080023#include <android/hidl/manager/1.2/IServiceManager.h>
Cheney Niad05f3e2018-11-08 16:41:02 +080024#include <base/logging.h>
25#include <hidl/MQDescriptor.h>
Cheney Ni3abcc012019-03-14 20:58:59 +080026#include <hidl/ServiceManagement.h>
Cheney Niad05f3e2018-11-08 16:41:02 +080027#include <future>
28
29#include "osi/include/log.h"
30
31namespace bluetooth {
32namespace audio {
33
34using ::android::hardware::hidl_vec;
35using ::android::hardware::Return;
36using ::android::hardware::Void;
37using ::android::hardware::audio::common::V5_0::SourceMetadata;
38using ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioPort;
39using ::android::hardware::bluetooth::audio::V2_0::
40 IBluetoothAudioProvidersFactory;
41using DataMQ = ::android::hardware::MessageQueue<
42 uint8_t, ::android::hardware::kSynchronizedReadWrite>;
43
44static constexpr int kDefaultDataReadTimeoutMs = 10; // 10 ms
45static constexpr int kDefaultDataReadPollIntervalMs = 1; // non-blocking poll
Cheney Ni3abcc012019-03-14 20:58:59 +080046static constexpr char kFullyQualifiedInterfaceName[] =
47 "android.hardware.bluetooth.audio@2.0::IBluetoothAudioProvidersFactory";
Cheney Niad05f3e2018-11-08 16:41:02 +080048
49std::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)
70class 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 Nifff829f2019-03-07 14:15:27 +080082 LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +080083 }
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 Nifff829f2019-03-07 14:15:27 +080094 LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +080095 }
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
160class 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 Ni3abcc012019-03-14 20:58:59 +0800193BluetoothAudioClientInterface::BluetoothAudioClientInterface(IBluetoothTransportInstance* sink,
194 bluetooth::common::MessageLoopThread* message_loop)
195 : sink_(sink), provider_(nullptr), session_started_(false), mDataMQ(nullptr),
Cheney Niad05f3e2018-11-08 16:41:02 +0800196 death_recipient_(new BluetoothAudioDeathRecipient(this, message_loop)) {
Cheney Ni3abcc012019-03-14 20:58:59 +0800197 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 Niad05f3e2018-11-08 16:41:02 +0800213}
214
215BluetoothAudioClientInterface::~BluetoothAudioClientInterface() {
216 if (provider_ != nullptr) {
217 auto hidl_retval = provider_->unlinkToDeath(death_recipient_);
218 if (!hidl_retval.isOk()) {
Cheney Nifff829f2019-03-07 14:15:27 +0800219 LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +0800220 }
221 }
222}
223
224std::vector<AudioCapabilities>
225BluetoothAudioClientInterface::GetAudioCapabilities() const {
226 return capabilities_;
227}
228
229void BluetoothAudioClientInterface::fetch_audio_provider() {
230 if (provider_ != nullptr) {
231 LOG(WARNING) << __func__ << ": reflash";
232 }
233
234 android::sp<IBluetoothAudioProvidersFactory> providersFactory =
235 IBluetoothAudioProvidersFactory::getService();
Cheney Ni3abcc012019-03-14 20:58:59 +0800236 CHECK(providersFactory != nullptr) << "IBluetoothAudioProvidersFactory::getService() failed";
Cheney Niad05f3e2018-11-08 16:41:02 +0800237 LOG(INFO) << "IBluetoothAudioProvidersFactory::getService() returned "
238 << providersFactory.get()
239 << (providersFactory->isRemote() ? " (remote)" : " (local)");
Cheney Niad05f3e2018-11-08 16:41:02 +0800240
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 Nifff829f2019-03-07 14:15:27 +0800256 LOG(FATAL) << __func__ << ": BluetoothAudioHal::getProviderCapabilities failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +0800257 return;
Cheney Nifff829f2019-03-07 14:15:27 +0800258 }
259 if (capabilities_.empty()) {
Cheney Niad05f3e2018-11-08 16:41:02 +0800260 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 Nifff829f2019-03-07 14:15:27 +0800286 LOG(FATAL) << __func__ << ": BluetoothAudioHal::openProvider failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +0800287 }
288 CHECK(provider_ != nullptr);
289
290 if (!provider_->linkToDeath(death_recipient_, 0).isOk()) {
Cheney Nifff829f2019-03-07 14:15:27 +0800291 LOG(FATAL) << __func__ << ": BluetoothAudioDeathRecipient failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +0800292 }
293
294 LOG(INFO) << "IBluetoothAudioProvidersFactory::openProvider() returned "
295 << provider_.get()
296 << (provider_->isRemote() ? " (remote)" : " (local)");
297}
298
299bool 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
324int 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 Nifff829f2019-03-07 14:15:27 +0800357 LOG(FATAL) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +0800358 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
382void BluetoothAudioClientInterface::StreamStarted(
383 const BluetoothAudioCtrlAck& ack) {
384 if (provider_ == nullptr) {
385 LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr";
386 return;
Cheney Nifff829f2019-03-07 14:15:27 +0800387 }
388 if (ack == BluetoothAudioCtrlAck::PENDING) {
Cheney Niad05f3e2018-11-08 16:41:02 +0800389 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 Nifff829f2019-03-07 14:15:27 +0800395 LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +0800396 }
397}
398
399void BluetoothAudioClientInterface::StreamSuspended(
400 const BluetoothAudioCtrlAck& ack) {
401 if (provider_ == nullptr) {
402 LOG(ERROR) << __func__ << ": BluetoothAudioHal nullptr";
403 return;
Cheney Nifff829f2019-03-07 14:15:27 +0800404 }
405 if (ack == BluetoothAudioCtrlAck::PENDING) {
Cheney Niad05f3e2018-11-08 16:41:02 +0800406 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 Nifff829f2019-03-07 14:15:27 +0800412 LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +0800413 }
414}
415
416int 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 Nifff829f2019-03-07 14:15:27 +0800431 LOG(ERROR) << __func__ << ": BluetoothAudioHal failure: " << hidl_retval.description();
Cheney Niad05f3e2018-11-08 16:41:02 +0800432 return -EPROTO;
433 }
434 return 0;
435}
436
437size_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
489size_t BluetoothAudioClientInterface::WriteAudioData(uint8_t* p_buf,
490 uint32_t len) {
491 // Not implemented!
492 return 0;
493}
494
495void 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