Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1d2dcc6e authored by Bao Do's avatar Bao Do
Browse files

Allow retry when fetching AudioProvider from Audio HAL.

This helps guard BT processes against audioserver restarts.
This patch also fix some cpplint errors.

Bug: 341856622
Test: mmm packages/modules/Bluetooth
Change-Id: I63deef75cf70d1dc8f978e3ad9c95700bbcce56e
parent 006fc3d8
Loading
Loading
Loading
Loading
+41 −28
Original line number Diff line number Diff line
@@ -16,17 +16,22 @@

#define LOG_TAG "BTAudioClientAIDL"

#include "client_interface_aidl.h"
#include "aidl/client_interface_aidl.h"

#include <android/binder_manager.h>
#include <bluetooth/log.h>
#include <com_android_bluetooth_flags.h>

#include <memory>
#include <set>
#include <thread>
#include <utility>
#include <vector>

#include "bta/ag/bta_ag_int.h"

const uint8_t kFetchAudioProviderRetryNumber = 3;

namespace bluetooth {
namespace audio {
namespace aidl {
@@ -174,6 +179,10 @@ void BluetoothAudioClientInterface::FetchAudioProvider() {
  if (provider_ != nullptr) {
    log::warn("refetch");
  }
  // Retry if audioserver restarts in the middle of fetching.
  // When audioserver restarts, IBluetoothAudioProviderFactory service is also
  // re-registered, so we need to re-fetch the service.
  for (int retry_no = 0; retry_no < kFetchAudioProviderRetryNumber; ++retry_no) {
    auto provider_factory = IBluetoothAudioProviderFactory::fromBinder(::ndk::SpAIBinder(
            AServiceManager_waitForService(kDefaultAudioProviderFactoryInterface.c_str())));

@@ -200,16 +209,20 @@ void BluetoothAudioClientInterface::FetchAudioProvider() {

    aidl_retval = provider_factory->openProvider(transport_->GetSessionType(), &provider_);
    if (!aidl_retval.isOk()) {
    log::fatal("BluetoothAudioHal::openProvider failure: {}", aidl_retval.getDescription());
      log::error("BluetoothAudioHal::openProvider failure: {}, retry number {}",
                 aidl_retval.getDescription(), retry_no + 1);
    } else {
      provider_factory_ = std::move(provider_factory);
      break;
    }
  }
  log::assert_that(provider_ != nullptr, "assert failed: provider_ != nullptr");

  binder_status_t binder_status =
          AIBinder_linkToDeath(provider_factory->asBinder().get(), death_recipient_.get(), this);
          AIBinder_linkToDeath(provider_factory_->asBinder().get(), death_recipient_.get(), this);
  if (binder_status != STATUS_OK) {
    log::error("Failed to linkToDeath {}", static_cast<int>(binder_status));
  }
  provider_factory_ = std::move(provider_factory);

  log::info("IBluetoothAudioProvidersFactory::openProvider() returned {}{}",
            fmt::ptr(provider_.get()), (provider_->isRemote() ? " (remote)" : " (local)"));
@@ -524,7 +537,7 @@ size_t BluetoothAudioSinkClientInterface::ReadAudioData(uint8_t* p_buf, uint32_t
      if (avail_to_read > len - total_read) {
        avail_to_read = len - total_read;
      }
      if (data_mq_->read((MqDataType*)p_buf + total_read, avail_to_read) == 0) {
      if (data_mq_->read(reinterpret_cast<MqDataType*>(p_buf) + total_read, avail_to_read) == 0) {
        log::warn("len={} total_read={} failed", len, total_read);
        break;
      }