Android Camera 流程学习记录(三)—— Camera hw_get_module() 相关逻辑

简介

  • 这一篇笔记,我们将从 hw_get_module() 函数入手,去探究 Libraries 层是如何调用 HAL 层的库中的函数的。
  • CameraService 是在开机时就会启动的,而当它第一次启动时,就会调用一个名为 onFirstRef() 的成员函数,我们所要探究的内容就是从这里开始的。
  • NOTE:
    • 这一部分主要参考:
    • 本来想一天内应该能搞定这篇记录,结果公司这边正好有个项目要做,于是只能在闲暇时间慢慢写出来了。


hw_get_module()


1. CameraService

1.1 CameraService.cpp

  • 位置:framework/av/services/camera/libcameraservice/CameraService.cpp
  • CameraService::onFirstRef()
    • 首先调用其基类的 onFirstRef 函数。
    • 更新 notifier (这个 BatteryNotifier 好像是个单例,看类名好像和电池有关)。
    • 通过 hw_get_module 函数获取 rawModule
    • 注意 rawModulecamera_module_t 类型。
    • 利用 rawModule 创建 mModule 的实例,mModule CameraModule 类。
    BnCameraService::onFirstRef();

    // Update battery life tracking if service is restarting
    BatteryNotifier& notifier(BatteryNotifier::getInstance());
    notifier.noteResetCamera();
    notifier.noteResetFlashlight();

    camera_module_t *rawModule;
    /*** NOTE THIS ***/
    int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
            (const hw_module_t **)&rawModule);
    if (err < 0) {
        ALOGE("Could not load camera HAL module: %d (%s)", err, strerror(-err));
        logServiceError("Could not load camera HAL module", err);
        return;
    }

    /*** NOTE THIS ***/
    mModule = new CameraModule(rawModule);
    err = mModule->init();

2. hardware

2.1 hardware.h

  • 位置:hardware/libhardware/include/hardware/hardware.h
  • 注意两个宏定义:
/**
 * Name of the hal_module_info
 */
#define HAL_MODULE_INFO_SYM         HMI

/**
 * Name of the hal_module_info as a string
 */
#define HAL_MODULE_INFO_SYM_AS_STR  "HMI"
  • 声明了这两个函数:
    • hw_get_module()
      • 作用是通过传入的 id 来获取模块相关的信息。
      • 成功则返回 0,出错则返回值小于 0*module == NULL
    • hw_get_module_by_class()
      • 作用是通过 class_id 获取与模块实例相关的信息。
      • 提供模块信息的库文件应该是带有这样命名规范的:
        - audio.primary.<variant>.so
        - audio.a2dp.<variant>.so
/**
 * Get the module info associated with a module by id.
 *
 * @return: 0 == success, <0 == error and *module == NULL
 */
int hw_get_module(const char *id, const struct hw_module_t **module);

/**
 * Get the module info associated with a module instance by class 'class_id'
 * and instance 'inst'.
 *
 * Some modules types necessitate multiple instances. For example audio supports
 * multiple concurrent interfaces and thus 'audio' is the module class
 * and 'primary' or 'a2dp' are module interfaces. This implies that the files
 * providing these modules would be named audio.primary.<variant>.so and
 * audio.a2dp.<variant>.so
 *
 * @return: 0 == success, <0 == error and *module == NULL
 */
int hw_get_module_by_class(const char *class_id, const char *inst,
                           const struct hw_module_t **module);

2.2 hardware.c

  • 位置:hardware/libhardware/hardware.c
  • 注意这个数组:
static const char *variant_keys[] = {
    "ro.hardware",  /* This goes first so that it can pick up a different
                       file on the emulator. */
    "ro.product.board",
    "ro.board.platform",
    "ro.arch"
};
  • hw_get_module():
    • 这是我们重点追踪的函数。
    • 它直接返回调用另一个函数。
int hw_get_module(const char *id, const struct hw_module_t **module)
{
    return hw_get_module_by_class(id, NULL, module);
}
  • hw_get_module_by_class()
    • 读取库文件,尝试的顺序是:
      • ro.hardware
      • ro.product.board
      • ro.board.platform
      • ro.arch
      • default
    • 通过 load 函数加载模块。
    /* First try a property specific to the class and possibly instance */
    snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);
    if (property_get(prop_name, prop, NULL) > 0) {
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
            goto found;
        }
    }

    /* Loop through the configuration variants looking for a module */
    for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
        if (property_get(variant_keys[i], prop, NULL) == 0) {
            continue;
        }
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
            goto found;
        }
    }

    /* Nothing found, try the default */
    if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
        goto found;
    }

    return -ENOENT;

found:
    /* load the module, if this fails, we're doomed, and we should not try
     * to load a different variant. */
    /*** NOTE THIS ***/
    return load(class_id, path, module);
  • load()
    • 调用 dlopen() 函数获取一个 handle
    • 调用 dlsym() 函数从动态链接库中获取 hw_module_t 类型的 hmi
    • NOTE
      • 为了获取动态链接库中的结构体,我们需要用到一个字符串 sym
      • sym 对应宏 HAL_MODULE_INFO_SYM_AS_STR,即 “HMI”
      • 我们的动态链接库 .so 文件,是一个 ELF 文件。
      • ELF:Executable and Linkable Format,可执行链接格式。
      • ELF 文件头保存了一个路线图,用于描述文件的组织结构。
      • 通过 readelf -s 命令,我们可以查看对应的 .so 文件描述,可以看到其中有一个 Name 属性为 HMI ,其对应的位置就是我们所需要的结构体 hw_module_t
      • 于是我们通过 HMI 字段,就可以从动态链接库中读取出相应的结构体,从而得以在 Libraries 层中调用 HAL 层的库函数。
  • 至此,我们就获得了最终的 rawModule,然后我们回到 onFirstRef() 中继续分析。
static int load(const char *id,
        const char *path,
        const struct hw_module_t **pHmi)
{
    int status = -EINVAL;
    void *handle = NULL;
    struct hw_module_t *hmi = NULL;

    /*
     * load the symbols resolving undefined symbols before
     * dlopen returns. Since RTLD_GLOBAL is not or'd in with
     * RTLD_NOW the external symbols will not be global
     */
    /*** NOTE THIS ***/
    handle = dlopen(path, RTLD_NOW);
    if (handle == NULL) {
        char const *err_str = dlerror();
        ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
        status = -EINVAL;
        goto done;
    }

    /* Get the address of the struct hal_module_info. */
    /*** NOTE THIS ***/
    const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
    hmi = (struct hw_module_t *)dlsym(handle, sym);
    if (hmi == NULL) {
        ALOGE("load: couldn't find symbol %s", sym);
        status = -EINVAL;
        goto done;
    }

    /* Check that the id matches */
    if (strcmp(id, hmi->id) != 0) {
        ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        status = -EINVAL;
        goto done;
    }

    hmi->dso = handle;
        /* success */
    status = 0;

/*** NOTE THIS ***/
done:
    if (status != 0) {
        hmi = NULL;
        if (handle != NULL) {
            dlclose(handle);
            handle = NULL;
        }
    } else {
        ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
                id, path, *pHmi, handle);
    }

    *pHmi = hmi;

    return status;
}

3. CameraModule

3.1 CameraModule.cpp

  • 位置:frameworks/av/services/camera/libcameraservice/common/CameraModule.cpp
  • 构造函数:
    • 注意,这里的 mModulecamera_module_t 类型。
CameraModule::CameraModule(camera_module_t *module) {
    if (module == NULL) {
        ALOGE("%s: camera hardware module must not be null", 
                __FUNCTION__);
        assert(0);
    }
    mModule = module;
}
  • init()
    • 调用 mModuleinit() 函数。
    • 如果没有指定的 init() 函数,则 init 流程到这里就可以结束了。
int CameraModule::init() {
    ATRACE_CALL();
    int res = OK;
    if (getModuleApiVersion() >= CAMERA_MODULE_API_VERSION_2_4 &&
            mModule->init != NULL) {
        ATRACE_BEGIN("camera_module->init");
        res = mModule->init();
        ATRACE_END();
    }
    mCameraInfoMap.setCapacity(getNumberOfCameras());
    return res;
}

3.2 camera_common.h

  • 位置:hardware/libhardware/include/hardware/camera_common.h
  • 声明了 camera_module_t
    • 结构体中声明了许多函数指针。
    • 其中就有 init 函数指针。
    • 这个指针指向的具体函数,是根据具体的 Camera 设备确定的,其中我查看了 QCamera2Factory.cpp ,这里就实现了对应的函数。
    • 到这里暂时就没必要再深入下去了,因为业务关系现在优先把 Framework 到 Libraries 的部分搞清楚。
    /**
     * init:
     *
     * This method is called by the camera service before any other methods
     * are invoked, right after the camera HAL library has been successfully
     * loaded. It may be left as NULL by the HAL module, if no initialization
     * in needed.
     *
     * It can be used by HAL implementations to perform initialization and
     * other one-time operations.
     *
     * Version information (based on camera_module_t.common.module_api_version):
     *
     * CAMERA_MODULE_API_VERSION_1_x/2_0/2_1/2_2/2_3:
     *   Not provided by HAL module. Framework will not call this function.
     *
     * CAMERA_MODULE_API_VERSION_2_4:
     *   If not NULL, will always be called by the framework once after the HAL
     *   module is loaded, before any other HAL module method is called.
     *
     * Return values:
     *
     * 0:           On a successful operation.
     *
     * -ENODEV:     Initialization cannot be completed due to an internal
     *              error. The HAL must be assumed to be in a nonfunctional
     *              state.
     *
     */
    int (*init)();

3.3* QCamera2Factory

  • 看路径,这几个文件应该是和高通平台有关的。
3.3.1* QCamera2Factory.h
  • 位置:hardware/qcom/camera/QCamera2/QCamera2Factory.h
3.3.2* QCamera2Factory.cpp
  • 位置:hardware/qcom/camera/QCamera2/QCamera2Factory.cpp
3.3.3* QCamera2Hal.cpp
  • 位置:hardware/qcom/camera/QCamera2/QCamera2Hal.cpp
  • 这个文件中有如下定义:
    • 详细描述了 camera_common
    • 确定了函数指针的指向
static hw_module_t camera_common = {
    .tag                    = HARDWARE_MODULE_TAG,
    .module_api_version     = CAMERA_MODULE_API_VERSION_2_4,
    .hal_api_version        = HARDWARE_HAL_API_VERSION,
    .id                     = CAMERA_HARDWARE_MODULE_ID,
    .name                   = "QCamera Module",
    .author                 = "Qualcomm Innovation Center Inc",
    .methods                = &qcamera::QCamera2Factory::mModuleMethods,
    .dso                    = NULL,
    .reserved               = {0}
};

camera_module_t HAL_MODULE_INFO_SYM = {
    .common                 = camera_common,
    .get_number_of_cameras  = qcamera::QCamera2Factory::get_number_of_cameras,
    .get_camera_info        = qcamera::QCamera2Factory::get_camera_info,
    .set_callbacks          = qcamera::QCamera2Factory::set_callbacks,
    .get_vendor_tag_ops     = qcamera::QCamera3VendorTags::get_vendor_tag_ops,
    .open_legacy            = qcamera::QCamera2Factory::open_legacy,
    .set_torch_mode         = qcamera::QCamera2Factory::set_torch_mode,
    .init                   = NULL,
    .reserved               = {0}
};

4. 流程简图

流程简图


小结

  • 这篇笔记中,我们从 CameraService::onFirstRef() 入手,逐渐理顺了以 hw_get_module() 为中心的一个调用逻辑。
  • 实际上,Android HAL 层有一个通用的入口,即宏 HAL_MODULE_INFO_SYM,通过它,我们获取 HAL 层中的模块实例,从而使得我们可以调用 HAL 层所提供的函数。
  • 理解了 HAL 层的入口,接下来我们可以去对 Camera.startPreview() 的控制流程进行分析,从而再次加深我们对 Camera 控制流的理解。
### 关于 SpinnakerNET_v140.dll 的下载与使用 SpinnakerNET_v140.dll 是 Teledyne FLIR 提供的一个用于 C# 开发环境下的动态链接库,主要用于支持基于 Spinnaker SDK 的相机控制功能。以下是关于该 DLL 文件的相关信息以及可能遇到的问题及其解决方法。 #### 1. **DLL 下载** SpinnakerNET_v140.dll 并未单独提供下载服务,通常作为 Spinnaker SDK 安装包的一部分被安装至本地系统。如果需要获取此文件,可以通过以下方式完成: - 访问官方文档页面并下载完整的 Spinnaker SDK 软件包[^3]。 - 默认情况下,SDK 将其安装路径设为 `C:\Program Files\Teledyne\Spinnaker`,其中包含多个版本的 DLL 和 LIB 文件。 - 对应的 Debug 版本 DLL 命名为 `SpinnakerNETd_v140.dll`,而 Release 版本则命名为 `SpinnakerNET_v140.dll`。 #### 2. **DLL 使用说明** ##### (1)项目配置 为了正确引用 SpinnakerNET_v140.dll,需按照如下方式进行配置: - 在 Visual Studio 中创建一个新的 C# 工程。 - 右键点击项目的“引用”,选择“添加引用”选项卡。 - 浏览至上述默认安装路径中的 `\bin64\vs2015` 子目录,手动添加所需的 DLL 文件。 ##### (2)代码实现示例 下面是一个简单的代码片段展示如何初始化 Spinnaker API 来枚举连接设备列表: ```csharp using Spinnaker; using Spinnaker.Net; class Program { static void Main(string[] args) { // 初始化 Spinnaker 系统实例 using (SystemPtr system = new SystemPtr()) { CameraList camList = system.GetCameras(); Console.WriteLine($"Number of cameras detected: {camList.GetSize()}"); foreach (CameraPtr camera in camList) { try { camera.Init(); // 初始化摄像头对象 string deviceName = camera.DeviceSerialNumber.ToString(); Console.WriteLine($"Device Name: {deviceName}"); } catch (Exception ex) { Console.WriteLine(ex.Message); } } camList.Clear(); // 清理资源 } } } ``` ##### (3)常见问题及解决方案 ###### a) 缺少依赖项 当尝试运行应用程序时可能会抛出类似于 “未能加载文件或程序集” 的错误消息。这通常是由于某些必要的依赖项缺失所致[^2]。建议执行以下操作来排查此类问题: - 检查目标平台是否已正确设置为 x86 或 x64 架构模式; - 验证所有必需的第方组件均已完整部署到输出目录中(例如 GMap.NET.Core.dll),必要时可借助工具如 Dependency Walker 辅助分析具体缺少哪些模块。 ###### b) 加载失败 有时即使所有的外部依赖都已经满足仍然会出现无法加载指定过程的情况[^4]。此时应该考虑以下几个方面: - 确认当前使用的 .NET Framework 版本号不低于所选 Spinnaker NET 组件的要求; - 若涉及非托管代码部分,则还需要额外开启允许不安全代码的支持开关,并调整编译器优化参数以减少潜在冲突风险; --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值