温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Camera Service的结构是什么

发布时间:2022-03-25 09:25:37 来源:亿速云 阅读:307 作者:iii 栏目:互联网科技

本篇内容介绍了“Camera Service的结构是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在camera service这端的结构还是很容易让人迷惑的,我就是看了好久,才缕清楚关系。

service这部分包括以下几个头文件:ICamera.h, ICameraService.h, CameraService.h,对应的实现ICamera.cpp, ICameraService.cpp, CameraService.cpp。

CameraService中包含了一个内部类CameraService::Client,这个CameraService::Client是对ICamera的实现,camera client得到的ICamera就是这个CameraService::Client。

也就是说与camera client真正通信的,是这个CameraService::Client。下面我们来逐步分析service这块的实现。


1.ICameraService

(1)ICameraService.h

其中只定义了3个方法:

    virtual int32_t         getNumberOfCameras() = 0;
    virtual status_t        getCameraInfo(int cameraId, struct CameraInfo* cameraInfo) = 0;
    virtual sp<ICamera>     connect(const sp<ICameraClient>& cameraClient,  int cameraId) = 0;


(2) ICameraService.cpp

看代码:

status_t BnCameraService::onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case GET_NUMBER_OF_CAMERAS: {
            CHECK_INTERFACE(ICameraService, data, reply);
            reply->writeInt32(getNumberOfCameras());
            return NO_ERROR;
        } break;
        case GET_CAMERA_INFO: {
            CHECK_INTERFACE(ICameraService, data, reply);
            CameraInfo cameraInfo;
            memset(&cameraInfo, 0, sizeof(cameraInfo));
            status_t result = getCameraInfo(data.readInt32(), &cameraInfo);
            reply->writeInt32(cameraInfo.facing);
            reply->writeInt32(cameraInfo.orientation);
            reply->writeInt32(result);
            return NO_ERROR;
        } break;
        case CONNECT: {
            CHECK_INTERFACE(ICameraService, data, reply);
            sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());
            sp<ICamera> camera = connect(cameraClient, data.readInt32());
            reply->writeStrongBinder(camera->asBinder());
            return NO_ERROR;
        } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}
这3个函数的真正实现在CameraService.cpp中。
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());中得到此次连接的client,并且传递到connect函数中。保存client信息的目的是保存在CameraService::Client中,在适当的时机回调client。

2.CameraService

(1)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;
}

3.CameraService::Client

class Client : public BnCamera, Client是对ICamera的实现。
构造函数:

CameraService::Client::Client(const sp<CameraService>& cameraService,
        const sp<ICameraClient>& cameraClient,
        const sp<CameraHardwareInterface>& hardware,
        int cameraId, int cameraFacing, int clientPid) {
    int callingPid = getCallingPid();
    LOG1("Client::Client E (pid %d)", callingPid);

    mCameraService = cameraService;
    mCameraClient = cameraClient;
    mHardware = hardware;
    mCameraId = cameraId;
    mCameraFacing = cameraFacing;
    mClientPid = clientPid;
    mUseOverlay = mHardware->useOverlay();
    mMsgEnabled = 0;

    mHardware->setCallbacks(notifyCallback,
                            dataCallback,
                            dataCallbackTimestamp,
                            (void *)cameraId);

    // Enable zoom, error, and focus messages by default
    enableMsgType(CAMERA_MSG_ERROR |
                  CAMERA_MSG_ZOOM |
                  CAMERA_MSG_FOCUS);
    mOverlayW = 0;
    mOverlayH = 0;

    // Callback is disabled by default
    mPreviewCallbackFlag = FRAME_CALLBACK_FLAG_NOOP;
    mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
    mOrientationChanged = false;
    cameraService->setCameraBusy(cameraId);
    cameraService->loadSound();
    LOG1("Client::Client X (pid %d)", callingPid);
}

主要进行一些成员变量的初始化,主要有mCameraClient, mHardware等。都是在CameraService::connect时进行的。

Client的各个函数可以自己看一下,主要就是对mHardware的调用,这是一个CameraHardwareInterface接口,也就是对HAL层的一个封装。比如stopRecording函数:

// stop recording mode
void CameraService::Client::stopRecording() {
    ……
    mCameraService->playSound(SOUND_RECORDING);
    disableMsgType(CAMERA_MSG_VIDEO_FRAME);
    mHardware->stopRecording();
    …………
}


4.ICamera

最后再来说一下ICamera。

这个ICamera定义的其实就是camera client与camera service的接口。

当camera client连接camera service时,也就是调用CameraService::connect时,返回一个ICamera接口给camera client调用,这个ICamera接口的真正实现是CameraService::Client。

“Camera Service的结构是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI