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

Commit 7dfc94a0 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic "fix-b-149854039-dynamic-hal-modules" into rvc-dev

* changes:
  Plumb the notification about audio HAL services update to APM
  libaudiohal: Allow late registration of HIDL HAL services
  Allow for late audio module discovery in APM
parents 1cd41d5c 88b30d2c
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -792,6 +792,13 @@ const sp<IAudioPolicyService> AudioSystem::get_audio_policy_service()

// ---------------------------------------------------------------------------

void AudioSystem::onNewAudioModulesAvailable()
{
    const sp<IAudioPolicyService>& aps = AudioSystem::get_audio_policy_service();
    if (aps == 0) return;
    aps->onNewAudioModulesAvailable();
}

status_t AudioSystem::setDeviceConnectionState(audio_devices_t device,
                                               audio_policy_dev_state_t state,
                                               const char *device_address,
+16 −1
Original line number Diff line number Diff line
@@ -113,6 +113,7 @@ enum {
    REMOVE_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY,
    GET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY,
    GET_DEVICES_FOR_ATTRIBUTES,
    AUDIO_MODULES_UPDATED,  // oneway
};

#define MAX_ITEMS_PER_LIST 1024
@@ -1451,6 +1452,13 @@ public:
        }
        return NO_ERROR;
    }

    virtual void onNewAudioModulesAvailable()
    {
        Parcel data, reply;
        data.writeInterfaceToken(IAudioPolicyService::getInterfaceDescriptor());
        remote()->transact(AUDIO_MODULES_UPDATED, data, &reply, IBinder::FLAG_ONEWAY);
    }
};

IMPLEMENT_META_INTERFACE(AudioPolicyService, "android.media.IAudioPolicyService");
@@ -1522,7 +1530,8 @@ status_t BnAudioPolicyService::onTransact(
        case REMOVE_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY:
        case GET_PREFERRED_DEVICE_FOR_PRODUCT_STRATEGY:
        case GET_DEVICES_FOR_ATTRIBUTES:
        case SET_ALLOWED_CAPTURE_POLICY: {
        case SET_ALLOWED_CAPTURE_POLICY:
        case AUDIO_MODULES_UPDATED: {
            if (!isServiceUid(IPCThreadState::self()->getCallingUid())) {
                ALOGW("%s: transaction %d received from PID %d unauthorized UID %d",
                      __func__, code, IPCThreadState::self()->getCallingPid(),
@@ -2672,6 +2681,12 @@ status_t BnAudioPolicyService::onTransact(
            return NO_ERROR;
        }

        case AUDIO_MODULES_UPDATED: {
            CHECK_INTERFACE(IAudioPolicyService, data, reply);
            onNewAudioModulesAvailable();
            return NO_ERROR;
        } break;

        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
+1 −0
Original line number Diff line number Diff line
@@ -221,6 +221,7 @@ public:
    //
    // IAudioPolicyService interface (see AudioPolicyInterface for method descriptions)
    //
    static void onNewAudioModulesAvailable();
    static status_t setDeviceConnectionState(audio_devices_t device, audio_policy_dev_state_t state,
                                             const char *device_address, const char *device_name,
                                             audio_format_t encodedFormat);
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ public:
    //
    // IAudioPolicyService interface (see AudioPolicyInterface for method descriptions)
    //
    virtual void onNewAudioModulesAvailable() = 0;
    virtual status_t setDeviceConnectionState(audio_devices_t device,
                                              audio_policy_dev_state_t state,
                                              const char *device_address,
+90 −22
Original line number Diff line number Diff line
@@ -15,12 +15,13 @@
 */

#include <string.h>
#include <vector>
#include <set>

#define LOG_TAG "DevicesFactoryHalHidl"
//#define LOG_NDEBUG 0

#include <android/hidl/manager/1.0/IServiceManager.h>
#include <android/hidl/manager/1.0/IServiceNotification.h>
#include PATH(android/hardware/audio/FILE_VERSION/IDevice.h)
#include <media/audiohal/hidl/HalDeathHandler.h>
#include <utils/Log.h>
@@ -29,33 +30,57 @@
#include "DeviceHalHidl.h"
#include "DevicesFactoryHalHidl.h"

#include <set>

using ::android::hardware::audio::CPP_VERSION::IDevice;
using ::android::hardware::audio::CPP_VERSION::Result;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hidl::manager::V1_0::IServiceManager;
using ::android::hidl::manager::V1_0::IServiceNotification;

namespace android {
namespace CPP_VERSION {

DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
    ALOG_ASSERT(devicesFactory != nullptr, "Provided IDevicesFactory service is NULL");

    mDeviceFactories.push_back(devicesFactory);
    if (MAJOR_VERSION >= 4) {
        // The MSD factory is optional and only available starting at HAL 4.0
        sp<IDevicesFactory> msdFactory{IDevicesFactory::getService(AUDIO_HAL_SERVICE_NAME_MSD)};
        if (msdFactory) {
            mDeviceFactories.push_back(msdFactory);
        }
class ServiceNotificationListener : public IServiceNotification {
  public:
    explicit ServiceNotificationListener(sp<DevicesFactoryHalHidl> factory)
            : mFactory(factory) {}

    Return<void> onRegistration(const hidl_string& /*fully_qualified_name*/,
            const hidl_string& instance_name,
            bool /*pre_existing*/) override {
        if (static_cast<std::string>(instance_name) == "default") return Void();
        sp<DevicesFactoryHalHidl> factory = mFactory.promote();
        if (!factory) return Void();
        sp<IDevicesFactory> halFactory = IDevicesFactory::getService(instance_name);
        if (halFactory) {
            factory->addDeviceFactory(halFactory, true /*needToNotify*/);
        }
    for (const auto& factory : mDeviceFactories) {
        // It is assumed that the DevicesFactoryHalInterface instance is owned
        // by AudioFlinger and thus have the same lifespan.
        factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
        return Void();
    }

  private:
    wp<DevicesFactoryHalHidl> mFactory;
};

DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
    ALOG_ASSERT(devicesFactory != nullptr, "Provided default IDevicesFactory service is NULL");
    addDeviceFactory(devicesFactory, false /*needToNotify*/);
}

void DevicesFactoryHalHidl::onFirstRef() {
    sp<IServiceManager> sm = IServiceManager::getService();
    ALOG_ASSERT(sm != nullptr, "Hardware service manager is not running");
    sp<ServiceNotificationListener> listener = new ServiceNotificationListener(this);
    Return<bool> result = sm->registerForNotifications(
            IDevicesFactory::descriptor, "", listener);
    if (result.isOk()) {
        ALOGE_IF(!static_cast<bool>(result),
                "Hardware service manager refused to register listener");
    } else {
        ALOGE("Failed to register for hardware service manager notifications: %s",
                result.description().c_str());
    }
}

#if MAJOR_VERSION == 2
static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
@@ -83,12 +108,13 @@ static const char* idFromHal(const char *name, status_t* status) {
#endif

status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
    if (mDeviceFactories.empty()) return NO_INIT;
    auto factories = copyDeviceFactories();
    if (factories.empty()) return NO_INIT;
    status_t status;
    auto hidlId = idFromHal(name, &status);
    if (status != OK) return status;
    Result retval = Result::NOT_INITIALIZED;
    for (const auto& factory : mDeviceFactories) {
    for (const auto& factory : factories) {
        Return<void> ret = factory->openDevice(
                hidlId,
                [&](Result r, const sp<IDevice>& result) {
@@ -113,10 +139,9 @@ status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterfa

status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
    std::set<pid_t> pidsSet;

    for (const auto& factory : mDeviceFactories) {
    auto factories = copyDeviceFactories();
    for (const auto& factory : factories) {
        using ::android::hidl::base::V1_0::DebugInfo;
        using android::hidl::manager::V1_0::IServiceManager;

        DebugInfo debugInfo;
        auto ret = factory->getDebugInfo([&] (const auto &info) {
@@ -135,5 +160,48 @@ status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
    return NO_ERROR;
}

status_t DevicesFactoryHalHidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
    ALOG_ASSERT(callback != nullptr);
    bool needToCallCallback = false;
    {
        std::lock_guard<std::mutex> lock(mLock);
        if (mCallback.unsafe_get()) return INVALID_OPERATION;
        mCallback = callback;
        if (mHaveUndeliveredNotifications) {
            needToCallCallback = true;
            mHaveUndeliveredNotifications = false;
        }
    }
    if (needToCallCallback) {
        callback->onNewDevicesAvailable();
    }
    return NO_ERROR;
}

void DevicesFactoryHalHidl::addDeviceFactory(sp<IDevicesFactory> factory, bool needToNotify) {
    // It is assumed that the DevicesFactoryHalInterface instance is owned
    // by AudioFlinger and thus have the same lifespan.
    factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
    sp<DevicesFactoryHalCallback> callback;
    {
        std::lock_guard<std::mutex> lock(mLock);
        mDeviceFactories.push_back(factory);
        if (needToNotify) {
            callback = mCallback.promote();
            if (!callback) {
                mHaveUndeliveredNotifications = true;
            }
        }
    }
    if (callback) {
        callback->onNewDevicesAvailable();
    }
}

std::vector<sp<IDevicesFactory>> DevicesFactoryHalHidl::copyDeviceFactories() {
    std::lock_guard<std::mutex> lock(mLock);
    return mDeviceFactories;
}

} // namespace CPP_VERSION
} // namespace android
Loading