溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Camera Service的結(jié)構(gòu)是什么

發(fā)布時(shí)間:2022-03-25 09:25:37 來源:億速云 閱讀:299 作者:iii 欄目:互聯(lián)網(wǎng)科技

本篇內(nèi)容介紹了“Camera Service的結(jié)構(gòu)是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

在camera service這端的結(jié)構(gòu)還是很容易讓人迷惑的,我就是看了好久,才縷清楚關(guān)系。

service這部分包括以下幾個(gè)頭文件:ICamera.h, ICameraService.h, CameraService.h,對(duì)應(yīng)的實(shí)現(xiàn)ICamera.cpp, ICameraService.cpp, CameraService.cpp。

CameraService中包含了一個(gè)內(nèi)部類CameraService::Client,這個(gè)CameraService::Client是對(duì)ICamera的實(shí)現(xiàn),camera client得到的ICamera就是這個(gè)CameraService::Client。

也就是說與camera client真正通信的,是這個(gè)CameraService::Client。下面我們來逐步分析service這塊的實(shí)現(xiàn)。


1.ICameraService

(1)ICameraService.h

其中只定義了3個(gè)方法:

    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個(gè)函數(shù)的真正實(shí)現(xiàn)在CameraService.cpp中。
sp<ICameraClient> cameraClient = interface_cast<ICameraClient>(data.readStrongBinder());中得到此次連接的client,并且傳遞到connect函數(shù)中。保存client信息的目的是保存在CameraService::Client中,在適當(dāng)?shù)臅r(shí)機(jī)回調(diào)client。

2.CameraService

(1)CameraService.h
class CameraService :  public BinderService<CameraService>, public BnCameraService

這個(gè) 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注冊。

其實(shí)這個(gè)模板類,就是為native service提供了一向service注冊的統(tǒng)一方式。

publish和publishAndJoinThreadPool的區(qū)別就是在于,注冊之后是否開啟線程池來監(jiān)聽。

因?yàn)橹翱催^media server的代碼,在media server的main函數(shù)里,調(diào)用的就是CameraService.instantiate來注冊的,因?yàn)閏amera service是跑在media server進(jìn)程里的,所以camera service不需要自己來開啟線程來循環(huán)監(jiān)聽。

所以,我覺得調(diào)用publish和publishAndJoinThreadPool的不同場景是:如果這個(gè)service是跑在別的進(jìn)程里的,就調(diào)用publish,如果是自己單獨(dú)一個(gè)進(jìn)程,就調(diào)用publishAndJoinThreadPool。

(2)CameraService.cpp
實(shí)現(xiàn)在ICameraService定義的3個(gè)函數(shù)。
getNumberOfCameras()和getCameraInfo()的實(shí)現(xiàn)很簡單。

int32_t CameraService::getNumberOfCameras() {
    return mNumberOfCameras; //mNumberOfCameras是在構(gòu)造函數(shù)中初始化的,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函數(shù):

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接口,在下邊的代碼構(gòu)造Client時(shí)傳遞進(jìn)入
    …………
    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是對(duì)ICamera的實(shí)現(xiàn)。
構(gòu)造函數(shù):

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

主要進(jìn)行一些成員變量的初始化,主要有mCameraClient, mHardware等。都是在CameraService::connect時(shí)進(jìn)行的。

Client的各個(gè)函數(shù)可以自己看一下,主要就是對(duì)mHardware的調(diào)用,這是一個(gè)CameraHardwareInterface接口,也就是對(duì)HAL層的一個(gè)封裝。比如stopRecording函數(shù):

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


4.ICamera

最后再來說一下ICamera。

這個(gè)ICamera定義的其實(shí)就是camera client與camera service的接口。

當(dāng)camera client連接camera service時(shí),也就是調(diào)用CameraService::connect時(shí),返回一個(gè)ICamera接口給camera client調(diào)用,這個(gè)ICamera接口的真正實(shí)現(xiàn)是CameraService::Client。

“Camera Service的結(jié)構(gòu)是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI