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

Commit 445a7f4b authored by Cheney Ni's avatar Cheney Ni
Browse files

BluetoothAudioHAL: Don't cache the providers factory locally

This object is provided by Audio HAL, and we should get the
service every time while fetching a provider. It fixes the crash
which is due to out-of-sync in case of Audio HAL restarting.

Fixes: 173538746
Tag: #stability
Test: atest bluetooth-test-audio-hal-interface
      restart Audio HAL when Bluetooth is ON
Change-Id: I1e88136c768a1d5fdcd70cfd9e5f1a3a13645333
parent a531e8e7
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -221,7 +221,7 @@ BluetoothAudioClientInterface::GetAudioCapabilities(SessionType session_type) {
  }
  }


  android::sp<IBluetoothAudioProvidersFactory_2_0> providersFactory =
  android::sp<IBluetoothAudioProvidersFactory_2_0> providersFactory =
      HalVersionManager::GetProviderFactory_2_0();
      HalVersionManager::GetProvidersFactory_2_0();


  auto getProviderCapabilities_cb =
  auto getProviderCapabilities_cb =
      [&capabilities](const hidl_vec<AudioCapabilities>& audioCapabilities) {
      [&capabilities](const hidl_vec<AudioCapabilities>& audioCapabilities) {
@@ -245,7 +245,7 @@ void BluetoothAudioClientInterface::FetchAudioProvider() {
  }
  }


  android::sp<IBluetoothAudioProvidersFactory_2_0> providersFactory =
  android::sp<IBluetoothAudioProvidersFactory_2_0> providersFactory =
      HalVersionManager::GetProviderFactory_2_0();
      HalVersionManager::GetProvidersFactory_2_0();
  CHECK(providersFactory != nullptr)
  CHECK(providersFactory != nullptr)
      << "IBluetoothAudioProvidersFactory::getService() failed";
      << "IBluetoothAudioProvidersFactory::getService() failed";


+32 −32
Original line number Original line Diff line number Diff line
@@ -47,27 +47,41 @@ class HalVersionManager {
 public:
 public:
  static BluetoothAudioHalVersion GetHalVersion() {
  static BluetoothAudioHalVersion GetHalVersion() {
    std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
    std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
    if (instance_ptr->providersFactory_2_1) {
    return instance_ptr->hal_version_;
      return BluetoothAudioHalVersion::VERSION_2_1;
    } else if (instance_ptr->providersFactory) {
      return BluetoothAudioHalVersion::VERSION_2_0;
    }
    return BluetoothAudioHalVersion::VERSION_UNAVAILABLE;
  }
  }


  static android::sp<IBluetoothAudioProvidersFactory_2_1>
  static android::sp<IBluetoothAudioProvidersFactory_2_1>
  GetProviderFactory_2_1() {
  GetProvidersFactory_2_1() {
    std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
    std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
    return instance_ptr->providersFactory_2_1;
    if (instance_ptr->hal_version_ != BluetoothAudioHalVersion::VERSION_2_1) {
      return nullptr;
    }
    android::sp<IBluetoothAudioProvidersFactory_2_1> providers_factory =
        IBluetoothAudioProvidersFactory_2_1::getService();
    CHECK(providers_factory)
        << "V2_1::IBluetoothAudioProvidersFactory::getService() failed";

    LOG(INFO) << "V2_1::IBluetoothAudioProvidersFactory::getService() returned "
              << providers_factory.get()
              << (providers_factory->isRemote() ? " (remote)" : " (local)");
    return providers_factory;
  }
  }


  static android::sp<IBluetoothAudioProvidersFactory_2_0>
  static android::sp<IBluetoothAudioProvidersFactory_2_0>
  GetProviderFactory_2_0() {
  GetProvidersFactory_2_0() {
    std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
    std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
    if (instance_ptr->providersFactory_2_1)
    if (instance_ptr->hal_version_ == BluetoothAudioHalVersion::VERSION_2_1) {
      return instance_ptr->providersFactory_2_1;
      return instance_ptr->GetProvidersFactory_2_1();
    }
    android::sp<IBluetoothAudioProvidersFactory_2_0> providers_factory =
        IBluetoothAudioProvidersFactory_2_0::getService();
    CHECK(providers_factory)
        << "V2_0::IBluetoothAudioProvidersFactory::getService() failed";


    return instance_ptr->providersFactory;
    LOG(INFO) << "V2_0::IBluetoothAudioProvidersFactory::getService() returned "
              << providers_factory.get()
              << (providers_factory->isRemote() ? " (remote)" : " (local)");
    return providers_factory;
  }
  }


  HalVersionManager() {
  HalVersionManager() {
@@ -88,14 +102,7 @@ class HalVersionManager {
    }
    }


    if (instance_count > 0) {
    if (instance_count > 0) {
      providersFactory_2_1 = IBluetoothAudioProvidersFactory_2_1::getService();
      hal_version_ = BluetoothAudioHalVersion::VERSION_2_1;
      CHECK(providersFactory_2_1)
          << "V2_1::IBluetoothAudioProvidersFactory::getService() failed";

      LOG(INFO)
          << "V2_1::IBluetoothAudioProvidersFactory::getService() returned "
          << providersFactory_2_1.get()
          << (providersFactory_2_1->isRemote() ? " (remote)" : " (local)");
      return;
      return;
    }
    }


@@ -108,26 +115,19 @@ class HalVersionManager {
    }
    }


    if (instance_count > 0) {
    if (instance_count > 0) {
      providersFactory = IBluetoothAudioProvidersFactory_2_0::getService();
      hal_version_ = BluetoothAudioHalVersion::VERSION_2_0;
      CHECK(providersFactory)
          << "V2_0::IBluetoothAudioProvidersFactory::getService() failed";

      LOG(INFO)
          << "V2_0::IBluetoothAudioProvidersFactory::getService() returned "
          << providersFactory.get()
          << (providersFactory->isRemote() ? " (remote)" : " (local)");
      return;
      return;
    }
    }


    LOG(INFO) << __func__ << " No supported HAL version";
    hal_version_ = BluetoothAudioHalVersion::VERSION_UNAVAILABLE;
    LOG(ERROR) << __func__ << " No supported HAL version";
  }
  }


 private:
 private:
  static std::unique_ptr<HalVersionManager> instance_ptr;
  static std::unique_ptr<HalVersionManager> instance_ptr;
  std::mutex mutex_;
  std::mutex mutex_;


  android::sp<IBluetoothAudioProvidersFactory_2_0> providersFactory;
  BluetoothAudioHalVersion hal_version_;
  android::sp<IBluetoothAudioProvidersFactory_2_1> providersFactory_2_1;
};
};


}  // namespace audio
}  // namespace audio