client_interface.cc 18 KB

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