audio DevicesFactory

xref: /hardware/interfaces/audio/common/all-versions/default/service/service.cpp

int main(int /* argc */, char* /* argv */ []) {
    android::ProcessState::initWithDriver("/dev/vndbinder");
    // start a threadpool for vndbinder interactions
    android::ProcessState::self()->startThreadPool();
    configureRpcThreadpool(16, true /*callerWillJoin*/);

    bool fail = registerPassthroughServiceImplementation<audio::V4_0::IDevicesFactory>() != OK &&
                registerPassthroughServiceImplementation<audio::V2_0::IDevicesFactory>() != OK;
    LOG_ALWAYS_FATAL_IF(fail, "Could not register audio core API 2.0 nor 4.0");

    fail = registerPassthroughServiceImplementation<audio::effect::V4_0::IEffectsFactory>() != OK &&
           registerPassthroughServiceImplementation<audio::effect::V2_0::IEffectsFactory>() != OK,
    LOG_ALWAYS_FATAL_IF(fail, "Could not register audio effect API 2.0 nor 4.0");

    fail = registerPassthroughServiceImplementation<soundtrigger::V2_1::ISoundTriggerHw>() != OK &&
           registerPassthroughServiceImplementation<soundtrigger::V2_0::ISoundTriggerHw>() != OK,
    ALOGW_IF(fail, "Could not register soundtrigger API 2.0 nor 2.1");

    fail =
        registerPassthroughServiceImplementation<bluetooth::a2dp::V1_0::IBluetoothAudioOffload>() !=
        OK;
    ALOGW_IF(fail, "Could not register Bluetooth audio offload 1.0");

    joinRpcThreadpool();
}
registerPassthroughServiceImplementation()

xref: /system/libhidl/transport/include/hidl/LegacySupport.h

template<class Interface>
__attribute__((warn_unused_result))
status_t registerPassthroughServiceImplementation(
        std::string name = "default") {
    sp<Interface> service = Interface::getService(name, true /* getStub */);

    if (service == nullptr) {
        ALOGE("Could not get passthrough implementation for %s/%s.",
            Interface::descriptor, name.c_str());
        return EXIT_FAILURE;
    }

    LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!",
            Interface::descriptor, name.c_str());

    status_t status = service->registerAsService(name);

    if (status == OK) {
        ALOGI("Registration complete for %s/%s.",
            Interface::descriptor, name.c_str());
    } else {
        ALOGE("Could not register service %s/%s (%d).",
            Interface::descriptor, name.c_str(), status);
    }

    return status;
}

上面Interface::getService()中的getStub参数为true,所以在ServiceManagement.cpp中的getRawServiceInternal()中,会直接走下面的pm = getPassthroughServiceManager(); pm->get()
所以会执行PassthroughServiceManager::get(),这个函数也在ServiceManagement.cpp里。这个函数会去open so库,以IDevicesFactory为例,会去open [email protected]。open后会call这个so里的HIDL_FETCH_IDevicesFactory(),而这个函数会new一个DevicesFactory对象,如下:
hardware\interfaces\audio\core\all-versions\default\include\core\all-versions\default\DevicesFactory.impl.h
IDevicesFactory* HIDL_FETCH_IDevicesFactory(const char* /* name */) {

return new DevicesFactory();

}

 DevicesFactory继承了IDevicesFactory(这个类在soong/.intermediates下)

继续PassthroughServiceManager::get(),这个函数接下来会call RegisterReference(actualFqName, name),这个actualFqName就是在DevicesFactoryAll.cpp里的IDevicesFactory::descriptor,它的值为[email protected]::IDevicesFactory

所以getRawServiceInternal() return了一个DevicesFactory对象,回到getServiceInternal(),因为DevicesFactory是继承IDevicesFactory,而IDevicesFactory::isRemote()会返回false,所以会执行下面的IType::castFrom(base),这个函数是直接return parent,即是return base参数,也就是DevicesFactory对象。

然后返回到registerPassthroughServiceImplementation(),然后执行service->registerAsService(),即为执行IDevicesFactory::registerAsService()

上面会去open [email protected],这个so的查找路径在:

xref: /system/libhidl/base/include/hidl/HidlInternal.h

#define HAL_LIBRARY_PATH_SYSTEM_64BIT "/system/lib64/hw/"
#define HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION "/system/lib64/vndk-sp%s/hw/"
#define HAL_LIBRARY_PATH_VENDOR_64BIT "/vendor/lib64/hw/"
#define HAL_LIBRARY_PATH_ODM_64BIT    "/odm/lib64/hw/"
#define HAL_LIBRARY_PATH_SYSTEM_32BIT "/system/lib/hw/"
#define HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION "/system/lib/vndk-sp%s/hw/"
#define HAL_LIBRARY_PATH_VENDOR_32BIT "/vendor/lib/hw/"
#define HAL_LIBRARY_PATH_ODM_32BIT    "/odm/lib/hw/"

#if defined(__LP64__)
#define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_64BIT
#define HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION HAL_LIBRARY_PATH_VNDK_SP_64BIT_FOR_VERSION
#define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_64BIT
#define HAL_LIBRARY_PATH_ODM    HAL_LIBRARY_PATH_ODM_64BIT
#else
#define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_32BIT
#define HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION HAL_LIBRARY_PATH_VNDK_SP_32BIT_FOR_VERSION
#define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_32BIT
#define HAL_LIBRARY_PATH_ODM    HAL_LIBRARY_PATH_ODM_32BIT
#endif

那这个[email protected]是哪个makefile去build它的呢?如下:

cc_library_shared {
    name: "[email protected]",
    relative_install_path: "hw",
    proprietary: true,
    vendor: true,
    srcs: [
        "Conversions.cpp",
        "Device.cpp",
        "DevicesFactory.cpp",
        "ParametersUtil.cpp",
        "PrimaryDevice.cpp",
        "Stream.cpp",
        "StreamIn.cpp",
        "StreamOut.cpp",
    ],

    cflags: [
        "-DAUDIO_HAL_VERSION_4_0",
    ],

    defaults: ["hidl_defaults"],

    export_include_dirs: ["include"],

    shared_libs: [
        "libbase",
        "libcutils",
        "libfmq",
        "libhardware",
        "libhidlbase",
        "libhidltransport",
        "liblog",
        "libutils",
        "[email protected]",
        "[email protected]",
        "[email protected]",
        "android.hardware.audio.common-util",
    ],

    header_libs: [
        "android.hardware.audio.common.util@all-versions",
        "android.hardware.audio.core@all-versions-impl",
        "libaudioclient_headers",
        "libaudio_system_headers",
        "libhardware_headers",
        "libmedia_headers",
    ],

    whole_static_libs: [
        "libmedia_helper",
    ],

}
02-13 16:05