2.CameraService

CameraService.h

class CameraService :  public BinderService<CameraService>, public BnCameraService

这个 BinderService<CameraService>是什么东西呢?看看它的定义,在frameworks/base/include/binder下BinderService.h。

template<typename SERVICE>
class BinderService
{
public:
    static status_t publish() {
        sp<IServiceManager> sm(defaultServiceManager());
        return sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
    }

    static void publishAndJoinThreadPool() {
        sp<ProcessState> proc(ProcessState::self());
        sp<IServiceManager> sm(defaultServiceManager());
        sm->addService(String16(SERVICE::getServiceName()), new SERVICE());
        ProcessState::self()->startThreadPool();
        IPCThreadState::self()->joinThreadPool();
    }

    static void instantiate() { publish(); }

    static status_t shutdown() {
        return NO_ERROR;
    }
};

return sm->addService(String16(SERVICE::getServiceName()), new SERVICE());从这句代码,可以看出,这是native service在向SM注册。

其实这个模板类,就是为native service提供了一向service注册的统一方式。

publish和publishAndJoinThreadPool的区别就是在于,注册之后是否开启线程池来监听。

因为之前看过media server的代码,在media server的main函数里,调用的就是CameraService.instantiate来注册的,因为camera service是跑在media server进程里的,所以camera service不需要自己来开启线程来循环监听。

所以,我觉得调用publish和publishAndJoinThreadPool的不同场景是:如果这个service是跑在别的进程里的,就调用publish,如果是自己单独一个进程,就调用publishAndJoinThreadPool、

2)CameraService.cpp

实现在ICameraService定义的3个函数。

getNumberOfCameras()和getCameraInfo()的实现很简单。

int32_t CameraService::getNumberOfCameras() {
    return mNumberOfCameras; //mNumberOfCameras是在构造函数中初始化的,mNumberOfCameras = HAL_getNumberOfCameras();


status_t CameraService::getCameraInfo(int cameraId,
                                      struct CameraInfo* cameraInfo) {
    if (cameraId < 0 || cameraId >= mNumberOfCameras) {
        return BAD_VALUE;
    }

    HAL_getCameraInfo(cameraId, cameraInfo);
    return OK;
}

主要看connect函数:

sp<ICamera> CameraService::connect( const sp<ICameraClient>& cameraClient, int cameraId) {
    sp<Client> client;  //CameraService::Client
    …………
    if (mClient[cameraId] != 0) {
        client = mClient[cameraId].promote();
        if (client != 0) {
            if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
                LOG1("CameraService::connect X (pid %d) (the same client)",
                    callingPid);
                return client;
            } 
        }
    }
    …………
    sp<CameraHardwareInterface> hardware = HAL_openCameraHardware(cameraId); //获取CameraHardwareInterface接口,在下边的代码构造Client时传递进入
    …………
    CameraInfo info;
    HAL_getCameraInfo(cameraId, &info);
    client = new Client(this, cameraClient, hardware, cameraId, info.facing, callingPid); //这里就是之前说过的,在onTransact保存的client
    mClient[cameraId] = client;
    return client;
}

原文地址:https://www.cnblogs.com/chjgongzuo/p/9391283.html