Camera服務之--Client

Camera服務分爲ClientServer兩部分,本文主要分析Client部分。

1. Camera Client介紹



主要由以下幾個文件組成:

Camera.h/ Camera.cpp

ICameraClient.h/ IcameraClient.h

如圖中所示,Camera.class繼承自IcameraClient.class。Camera.class主要由libandroid_runtime.so中的android_hardware_Camera.cpp調用(frameworks/base/core/jni/android_hardware_Camera.cpp),也就是說Camera.h是Camera服務框架,對上層的接口。


2. ICameraClient.h分析

class ICameraClient: public IInterface
{
public:
    DECLARE_META_INTERFACE(CameraClient);

    virtual void            notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
    virtual void            dataCallback(int32_t msgType, const sp<IMemory>& data) = 0;
    virtual void            dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& data) = 0;
};

// ----------------------------------------------------------------------------

class BnCameraClient: public BnInterface<ICameraClient>
{
public:
    virtual status_t    onTransact( uint32_t code,
                                    const Parcel& data,
                                    Parcel* reply,
                                    uint32_t flags = 0);
};

根據BnInterface類模版的定義BnInterface<ICameraClient>類相當於雙繼承了BnInterfaceICameraClient。

大家如果熟悉Binder結構的話,看到這個文件可能會迷惑了,這不是一個Client嗎?怎麼還要繼承自IInterface並且要實現BnCameraClient呢?這在Binder結構中是一個service應該做的。因爲camera服務和一般的android服務不太一樣,一般的android服務,只需要client通過binder調用service的一些功能,並得到返回結果。再說的直接一點兒,就是client只需要單向調用service即可,所以一般的client只需要實現BpXXX,service只需要實現BnXXX。但是Camera服務中,service需要回調Client,所以就是說Camera服務中的client需要雙向調用,所以client和service都要實現BnXXX和BpXXX。

service需要回調client的接口就是:

    virtual void  notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
    virtual void  dataCallback(int32_t msgType, const sp<IMemory>& data) = 0;
    virtual void  dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& data) = 0;

這幾個接口與Camera.h中定義的CameraListener接口中的函數是對應的,從它們的函數名稱和參數列表就可以看出。


3.Camera.h

class CameraListener: virtual public RefBase
{
public:
    virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2) = 0;
    virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr) = 0;
    virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) = 0;
};

class Camera : public BnCameraClient, public IBinder::DeathRecipient
{
public:
     //對上層的接口
    static  sp<Camera>  create(const sp<ICamera>& camera);
    static  int32_t     getNumberOfCameras();
    static  status_t    getCameraInfo(int cameraId,
                                      struct CameraInfo* cameraInfo);
    static  sp<Camera>  connect(int cameraId);
                        ~Camera();
            void        init();
            status_t    reconnect();
            void        disconnect();
            status_t    startPreview();
            void        stopPreview();
  //對上層的接口


    // ICameraClient interface
    virtual void        notifyCallback(int32_t msgType, int32_t ext, int32_t ext2);
    virtual void        dataCallback(int32_t msgType, const sp<IMemory>& dataPtr);
    virtual void        dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);

private:
                        Camera();
                        Camera(const Camera&);
                        Camera& operator=(const Camera);
                        virtual void binderDied(const wp<IBinder>& who);
            class DeathNotifier: public IBinder::DeathRecipient
            {
            public:
                DeathNotifier() {
                }
                virtual void binderDied(const wp<IBinder>& who);
            };
            static sp<DeathNotifier> mDeathNotifier;
            // helper function to obtain camera service handle
            static const sp<ICameraService>& getCameraService();
            sp<ICamera>         mCamera;
            status_t            mStatus;
            sp<CameraListener>  mListener;
            friend class DeathNotifier;
            static  Mutex               mLock;
            static  sp<ICameraService>  mCameraService;

};

從接口中可以看出Camera類剛好實現了一個Camera的基本操作,例如播放(startPreview)、停止(stopPreview)、暫停(takePicture)等。在Camera類中connect()是一個靜態函數,它用於得到一個Camera的實例。


BnCameraClient繼承了BnInterface<ICameraClient>,這是爲基於Android的基礎類Binder機制實現在進程通訊而構建的。


4.ICameraClient.cpp分析

class BpCameraClient: public BpInterface<ICameraClient>
{
public:
    BpCameraClient(const sp<IBinder>& impl)
        : BpInterface<ICameraClient>(impl)
    {
    }

    // generic callback from camera service to app
    void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
    {
        remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
    }

    // generic data callback from camera service to app with image data
    void dataCallback(int32_t msgType, const sp<IMemory>& imageData)
    {
        remote()->transact(DATA_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY);
    }

    // generic data callback from camera service to app with image data
    void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& imageData)
    {  
        remote()->transact(DATA_CALLBACK_TIMESTAMP, data, &reply, IBinder::FLAG_ONEWAY);
    }
};

IMPLEMENT_META_INTERFACE(CameraClient, "android.hardware.ICameraClient");

// ----------------------------------------------------------------------

status_t BnCameraClient::onTransact(
    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
    switch(code) {
        case NOTIFY_CALLBACK: {
            notifyCallback(msgType, ext1, ext2);
        } break;
        case DATA_CALLBACK: {
            dataCallback(msgType, imageData);
        } break;
        case DATA_CALLBACK_TIMESTAMP: {
            dataCallbackTimestamp(timestamp, msgType, imageData);
        } break;
        default:
            return BBinder::onTransact(code, data, reply, flags);
    }
}

ICameraClient.cpp實現了BnCameraClient和BpCameraClient。BnCameraClient中調用的notifyCallback,dataCallback和dataCallbackTimestamp(ICameraClient.h中定義)是在Camera.cpp中實現的。關於BnCameraClient和BpCameraClient,可以看一下Binder的結構。

5.Camera.cpp分析

    首先獲取Camera的service:

// establish binder interface to camera service
const sp<ICameraService>& Camera::getCameraService()
{
    Mutex::Autolock _l(mLock);
    if (mCameraService.get() == 0) {
        sp<IServiceManager> sm = defaultServiceManager();
        sp<IBinder> binder;
        do {
            binder = sm->getService(String16("media.camera"));
            if (binder != 0)
                break;
            LOGW("CameraService not published, waiting...");
            usleep(500000); // 0.5 s
        } while(true);
        if (mDeathNotifier == NULL) {
            mDeathNotifier = new DeathNotifier();
        }
        binder->linkToDeath(mDeathNotifier);
        mCameraService = interface_cast<ICameraService>(binder);
    }
    LOGE_IF(mCameraService==0, "no CameraService!?");
    return mCameraService;
}

sp<Camera> Camera::connect(int cameraId)
{
    LOGV("connect");
    sp<Camera> c = new Camera();
    const sp<ICameraService>& cs = getCameraService();
    if (cs != 0) {
        c->mCamera = cs->connect(c, cameraId);
    }
    if (c->mCamera != 0) {
        c->mCamera->asBinder()->linkToDeath(c);
        c->mStatus = NO_ERROR;
    } else {
        c.clear();
    }
    return c;
}

在getCameraService()中首先通過ServiceManager獲取到ICameraService,然後在connect方法中通過調用ICameraService的connect方法,獲取到一個ICamera,並賦值給Camera的成員變量mCamera。可以自己去看一下Camera.cpp的剩餘代碼,幾乎所有的函數中,都是對ICameraService或者mCamera的調用。


實現ICameraClient接口:

// callback from camera service
void Camera::notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)
{
    sp<CameraListener> listener;
    {
        Mutex::Autolock _l(mLock);
        listener = mListener;
    }
    if (listener != NULL) {
        listener->notify(msgType, ext1, ext2);
    }
}

可以看出,其實就是對CameraListener的調用。

mListener由

void Camera::setListener(const sp<CameraListener>& listener)
{
    Mutex::Autolock _l(mLock);
    mListener = listener;
}

方法設定。

發佈了53 篇原創文章 · 獲贊 12 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章