Android Camera Framework Stream

 

轉自 http://blog.163.com/shawpin@126/blog/static/116663752201092394147937/

 

現在我們嘗試從最開始的啓動流程來熟悉android camera的整體framework流程:

首先從上圖的各個步驟來逐一分析流程,後續會根據具體的一些點進行內容的添加和擴充:

 

Camera.java

packages/apps/camera/src/com/android/

最上層的應用就是從這個文件開始。

該文件集中了整個android上層應用的所有相關內容,當然更多的則爲界面的代碼實現。

如果出現了camera應用界面的問題(當然除了camera拍攝區域內容外),可以從android的代碼入手。

 

Camera.java
frameworks/base/core/java/android/hardware/
該文件中主要是對native函數接口的調用,當然也包括一些本地的函數實現。
也可以認爲該文件是實現了從java層調用c++層代碼函數接口。

也就是我們需要去了解的一點JNI機制。

 

android_hardware_Camera.cpp
該文件就是JNIc++層的代碼實現。
通過camera的類實例來調用camera類的相關接口。

 

Camera.cpp/Camera.h
對於上層應用來說,camera.cpp是最爲直接的函數調用和實現。
繼承於ICameraClient,典型的Client端的接口實例。

 

BnCameraClient/BpCameraClient
IPC
通訊所需的函數接口實現,繼承於ICameraClient類。

 

ICameraClient.cpp/ICameraClient.h

Client/Service模式下的Client端實現

 

ICameraService.cpp/ICameraService.h

Client/Service模式下service端實現

 

BnCameraService/BpCameraService

IPC通訊所需的函數接口實現,繼承於ICameraService類。

 

CameraService.cpp/CameraService.h

繼承於BnCameraService類。

是對BnCameraService函數接口的實現,其本質也是對CameraService的內部類Client函數接口的調用。

 

Client(CameraService內部類)

該類纔是真正的底層函數實現,其通過openCameraHardware()得到camera硬件實例對象進行操作。

其繼承於ICamera,是對ICamera類函數接口的實現。

 

接下來,我們通過對流程的步步分析來將camera整體串接起來:

1.    首先則看看camera.java onCreate函數入口,針對android的所有應用,onCreate函數入口作爲跟蹤和了解應用架構的首選。

             @Override

    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        devlatch = new CountDownLatch(1);

 

         CountDownLatch()關於這個類,可以簡單的理解爲它是用來線程之間的等待處理,當然這裏採用的計數爲1,則可以簡單理解爲一個計數開關來控制調用了tlatch.await()函數的進程,方式就是將devlatch的計數減爲0(countDown() )

         這裏啓動了一個線程用來打開camera服務,而打開過程則比較費時(一般在2s左右),故單獨啓用一個線程避免應用線程阻塞。

Thread startPreviewThread = new Thread(new Runnable() {

            CountDownLatch tlatch = devlatch;

            public void run() {

                try {

                    mStartPreviewFail = false;

                    ensureCameraDevice();

 

                    // Wait for framework initialization to be complete before

                    // starting preview

                    try {

                        tlatch.await();

                    } catch (InterruptedException ie) {

                        mStartPreviewFail = true;

                    }

                    startPreview();

                } catch (CameraHardwareException e) {

                    // In eng build, we throw the exception so that test tool

                    // can detect it and report it

                    if ("eng".equals(Build.TYPE)) {

                        throw new RuntimeException(e);

                    }

                    mStartPreviewFail = true;

                }

            }

        });

        startPreviewThread.start();

         在這裏,需要跟進ensureCameraDevice();該函數,可以看到其實現爲:

    private void ensureCameraDevice() throws CameraHardwareException {

        if (mCameraDevice == null) {

            mCameraDevice = CameraHolder.instance().open();

            mInitialParams = mCameraDevice.getParameters();

        }

    }

         當前mCameraDevice()實例爲null,則會調用CameraHolder.instance().open()函數來創建mCameraDevice對象實例。

private android.hardware.Camera mCameraDevice;

         跟進CameraHolder.instance().open(),進入到了CameraHolder類中:

public synchronized android.hardware.Camera open()

            throws CameraHardwareException {

        Assert(mUsers == 0);

        if (mCameraDevice == null) {

            try {

                mCameraDevice = android.hardware.Camera.open();

            } catch (RuntimeException e) {

                Log.e(TAG, "fail to connect Camera", e);

                throw new CameraHardwareException(e);

            }

            mParameters = mCameraDevice.getParameters();

        } else {

……

         下面大概介紹下我對CameraHolder的理解:

         1CameraHoldermCameraDevice實例進行短暫的保留(keep()函數中可以設定這個保留時長,一般默認爲3000ms),避免用戶在短暫退出camera又重新進入時,縮短camera啓動時長(正如之前所說,打開CameraDevice時間較長)

2CameraHolder並有一個關鍵的計數mUsers用來保證open()release()的配套調用,避免多次重複釋放或者打開(上層應用的保護措施之一)

                  

2.    第一步的完成,進而跳轉到了android.hardware.Camera類中的open()函數接口調用。

public static Camera open() {

    return new Camera();

}

靜態函數,也就可以通過類名直接調用,open()函數中去創建一個Camera的實例。

Camera() {

        mShutterCallback = null;

        mRawImageCallback = null;

        mJpegCallback = null;

        mPreviewCallback = null;

        mPostviewCallback = null;

        mZoomListener = null;

 

        Looper looper;

        if ((looper = Looper.myLooper()) != null) {

            mEventHandler = new EventHandler(this, looper);

        } else if ((looper = Looper.getMainLooper()) != null) {

            mEventHandler = new EventHandler(this, looper);

        } else {

            mEventHandler = null;

        }

 

        native_setup(new WeakReference<Camera>(this));

    }

Camera構造函數中有這個關鍵的一步,最開始的一些callback可以認爲它們最終被底層調用到(至於具體流程後面會講到)EventHandlerLooper我們暫時跳過,知道它是消息處理就行了。最後也就是最爲關鍵的函數接口調用:native_setup

      private native final void native_setup(Object camera_this);

典型的native函數接口聲明,說明並非camera類的本地函數實現,也就意味着會通過JNI(Java Native Interface)調用對用C++文件中的函數接口。

 

3.    通過代碼搜索,或者如果你清楚JNI文件路徑也可以去該路徑下找。

 

其實這邊有個小技巧,雖然不一定都通用,但可以試試看:

java類的package名往往可以作爲尋找相應JNI文件的途徑:

package android.hardware;

則就可以通過android.hardware.camera.cpp來尋找(其實還是歸咎於android的規範命名規則)

 

         跳轉到android_hardware_Camera.cpp中尋找native_setup()所對應的JNI函數接口:

       static JNINativeMethod camMethods[] = {

  { "native_setup",

    "(Ljava/lang/Object;)V",

    (void*)android_hardware_Camera_native_setup },

  { "native_release",

    "()V",

    (void*)android_hardware_Camera_release },

  { "setPreviewDisplay",

    "(Landroid/view/Surface;)V",

    (void *)android_hardware_Camera_setPreviewDisplay },

         ……

camMethods[]在什麼時候映射的那?繼續看:

int register_android_hardware_Camera(JNIEnv *env) {

…..

// Register native functions

    return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera",

                                           camMethods,NELEM(camMethods));

}

最終在AndroidRuntime.cpp中被調用:

         REG_JNI(register_android_hardware_Camera),

說明如果我們自己要添加JNI接口實現的話,這些地方也需要添加相應的代碼(具體在AndroidRuntime.cpp的細節我沒深看,也不做介紹)

簡單介紹: JNINativeMethod的第一個成員是一個字符 串,表示了JAVA本地調用方法的名稱,這個名稱是在JAVA程序中調用的名稱;第二個成員也是一個字符串,表示JAVA本地調用方法的參數和返回值;第三個成員是JAVA本地調用方法對應的C語言函數

 

         跟進觀察android_hardware_Camera_native_setup()函數的實現:

    // connect to camera service

static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)

{

    sp<Camera> camera = Camera::connect();

 

    if (camera == NULL) {

        jniThrowException(env, "java/lang/RuntimeException",

                          "Fail to connect to camera service");

        return;

    }

    ….

}

         初步可以認爲Camera::connect()的函數調用時返回了一個Camera 的實例對象。

 

4.    通過上述的跟進流程來到了針對上層應用而言最爲直接的類:camera.cpp

Camera::connect函數的調用如下:

    sp<Camera> Camera::connect()

    {

        LOGV("connect");

        sp<Camera> c = new Camera();

        const sp<ICameraService>& cs = getCameraService();

        if (cs != 0) {

            c->mCamera = cs->connect(c);

        }

        if (c->mCamera != 0) {

            c->mCamera->asBinder()->linkToDeath(c);

            c->mStatus = NO_ERROR;

        } else {

            c.clear();

        }

        return c;

    }

首先是創建一個camera對象實例,然後通過調用getCameraService()去取得ICameraService的服務實例:

// 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;

}

這邊就涉及到了ServiceManager()對服務的管理,在這之前Camera的服務已經註冊到了ServiceManager中,我們可以通過服務字串(media.camera)來獲得camera service(其本質得到的是CameraService的實例對象,雖然通過類型上溯轉換成父類ICameraService,對ICameraService對象的函數調用本質是調用到了CameraService的函數實現)

在得到camera service後,返回之前的步驟:當得到的cscameraservice實例存在時,通過調用cs->connect(c)去得到ICamera實例,並賦值給了camera實例的一個類成員ICamera  mCamera

if (cs != 0) {

    c->mCamera = cs->connect(c);

5.    接下來則涉及到ICamraService的相關調用關係,其實這個地方需要去弄清楚一些函數接口的實現在具體哪些文件中,因爲存在較多的虛函數。

繼續流程,上一步走到了cs->connect(),也就是ICameraServiceconnect()函數接口。

class ICameraService : public IInterface

{

public:

    enum {

        CONNECT = IBinder::FIRST_CALL_TRANSACTION,

    };

 

public:

    DECLARE_META_INTERFACE(CameraService);

 

    virtual sp<ICamera>     connect(const sp<ICameraClient>& cameraClient) = 0;

};

可以發現該connect()接口爲一個純虛函數,需要ICameraService的子類對該接口進行實現,從而對connect()的調用則會映射到ICameraService子類的具體實現。

關於ICameraService的實例問題,目前暫時跳過(後面馬上就會講到),簡單認爲這個時候會調用到其一個子類的實現:

class BpCameraService: public BpInterface<ICameraService>

{

public:

    BpCameraService(const sp<IBinder>& impl)

        BpInterface<ICameraService>(impl)

    {

    }

 

    // connect to camera service

    virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient)

    {

        Parcel data, reply;

        data.writeInterfaceToken(ICameraService::getInterfaceDescriptor());

        data.writeStrongBinder(cameraClient->asBinder());

        remote()->transact(BnCameraService::CONNECT, data, &reply);

        return interface_cast<ICamera>(reply.readStrongBinder());

    }

};

 

         BpCameraService爲代理類,其主要用途爲Binder通訊機制即進程間的通訊(Client/Service),最終還是會調用BnCameraService的具體實現,即:

status_t BnCameraService::onTransact(

    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)

{

    switch(code) {

        case CONNECT: {

            CHECK_INTERFACE(ICameraService, data, reply);

            sp<ICameraClient> cameraClient

             = interface_cast<ICameraClient>(data.readStrongBinder());

            sp<ICamera> camera = connect(cameraClient);

            reply->writeStrongBinder(camera->asBinder());

            return NO_ERROR;

        } break;

        default:

            return BBinder::onTransact(code, data, reply, flags);

    }

}

         BnCameraService(爲實現類)類繼承於ICameraService,並且也並沒有對connect()純虛函數進行了實現,同樣意味着其實該調用的實質是BnCameraService的子類實現。

         畢竟虛函數的調用沒有實例肯定是沒有意義的,說明我們需要找到對connect()純虛函數的實現子類即繼承於BnCameraService

 

6.    結合上面所述,可以尋找到了繼承於BnCameraService的子類CameraService.cpp

這時雖然找到了CameraService該類,但是你肯定會問到該類實例的創建在什麼地方哪?再後頭看CameraService啓動註冊的地方:

int main(int argc, char** argv)

{

    sp<ProcessState> proc(ProcessState::self());

    sp<IServiceManager> sm = defaultServiceManager();

    LOGI("ServiceManager: %p", sm.get());

    AudioFlinger::instantiate();

    MediaPlayerService::instantiate();

    CameraService::instantiate();

    AudioPolicyService::instantiate();

    ProcessState::self()->startThreadPool();

    IPCThreadState::self()->joinThreadPool();

}

這個main函數位於main_mediaserver.cpp中,而mediaserver是在系統開始的時候就啓動起來的server端(MediaServer,在系統啓動時由init所啓動,具可參考init.rc文件),進而將相關的服務也創建了實例。

         跟進CameraService::instantiate()函數實現,可以發現:

void CameraService::instantiate() {

    defaultServiceManager()->addService(

            String16("media.camera"), new CameraService());

}

創建了一個CameraService實例 ,並給定了CameraService的服務字串爲”media.camera”,而之前在通過ServiceManager獲取CameraService的時候,所調用的接口爲binder = sm->getService(String16("media.camera"));,兩者保持了一樣的字符串。

 

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

}

結合上述分析,此處的binder對象其實爲CameraService類實例(多態類型轉換)

         interface_cast<ICameraService>(binder)宏映射,需要展開:

                  template<typename INTERFACE>

inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)

{

    return INTERFACE::asInterface(obj);

}

INTERFACE::asInterface(obj);宏映射,繼續展開可得:

sp<I##INTERFACE> I##INTERFACE::asInterface(const sp<IBinder>& obj)  /

    {                                                                  /

        sp<I##INTERFACE> intr;                                        /

        if (obj != NULL) {                                              /

            intr = static_cast<I##INTERFACE*>(                           /

                obj->queryLocalInterface(                               /

                        I##INTERFACE::descriptor).get());                 /

            if (intr == NULL) {                                          /

                intr = new Bp##INTERFACE(obj);                          /

            }                                                       /

        }                                                           /

        return intr;                                                   /

}  

                   (其上的宏展開都是在IMPLEMENT_META_INTERFACE(CameraService, "android.hardware.ICameraService");中實現的)

                   此處又創建了一個BpCameraService(new Bp##INTERFACE)對象並將binder對象(obj)傳入到BpCameraService的構造函數中。

雖然獲取的時候通過多態將CameraService實例轉換成了BnCameraService  也進一步解釋了爲什麼ICameraService子類BnCameraservice中的connect函數實質會調用到CameraService中函數實現了。

         於是就調用到了CameraServiceconnect函數接口:

sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient) {

…..

// create a new Client object

    client = new Client(this, cameraClient, callingPid);

    mClient = client;

    if (client->mHardware == NULL) {

        client = NULL;

        mClient = NULL;

        return client;

}

…..

}

         創建了一個Client實例對象,並將該實例對象賦值給CameraSevice的類成員mClient,方便其實函數接口對Client的調用。

         在這之前需要提及它的一個內部類Client,該類纔是最爲關鍵的函數實現,CameraService的一些接口都會調用到其Client實例的具體函數。

 

 

7.    那麼現在的關鍵就是Client類了·進一步跟進:

CameraService::Client::Client(const sp<CameraService>& cameraService,

        const sp<ICameraClient>& cameraClient, pid_t clientPid)

{

         …..

         mCameraService = cameraService;

    mCameraClient = cameraClient;

    mClientPid = clientPid;

    mHardware = openCameraHardware();

}

cameraServicecameraClient的實例分別賦值給了Client的類成員變量。

另外openCameraHardware()是值得注意的地方,也就是連接上層應用和底層驅動的關鍵,通過調用openCameraHardware()得到了一個CameraHardwareInterface實例對象,並賦值給自己的類成員:`

      sp<CameraHardwareInterface> mHardware;

         hardware的操作就是通過該對象完成的,所以說真正意義上的功能實現其實就是在這裏,即client類的函數接口調用。

         對於hardware的東東咱們暫時不去關注吧。

         那麼我們再次仔細研究下Client類的繼承關係(這些繼承關係很容易混亂,涉及到較多的多態類型轉換),這個其實往往都很關鍵:

 

[原創]Android Camera Framework Stream(三) - shawpin - 西靠生活

Client繼承於BnCamera,而BnCamera則繼承於ICamera,也就是說Client繼承了ICamera,實現了ICamera中的函數。

         進而發現,原來繞一個大圈,把最開始的圖簡化下:

 

[原創]Android Camera Framework Stream(三) - shawpin - 西靠生活
 

 

8.    除此之外還有兩個步驟或許需要去研究下:

先從單一函數去跟進,看具體一些callback的實現流程:

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

    }

}

這是Camera類中一個callback函數實現,但其本質在哪?先看camera類的繼承關係:

 

 

[原創]Android Camera Framework Stream(三) - shawpin - 西靠生活

通過以上的繼承關係,繼續跟進其父類ICameraClient:

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;

};

其中notifyCallback()又是純虛函數,則同樣說明實現在其子類BpCameraClient中:

    // generic callback from camera service to app

    void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2)

    {

        LOGV("notifyCallback");

        Parcel data, reply;

        data.writeInterfaceToken(ICameraClient::getInterfaceDescriptor());

        data.writeInt32(msgType);

        data.writeInt32(ext1);

        data.writeInt32(ext2);

    remote()->transact(NOTIFY_CALLBACK,data, &reply, IBinder::FLAG_ONEWAY);

    }

然後通過Binder通訊調用到BnCameraClient中實現:

status_t BnCameraClient::onTransact(

    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)

{

    switch(code) {

        case NOTIFY_CALLBACK: {

            LOGV("NOTIFY_CALLBACK");

            CHECK_INTERFACE(ICameraClient, data, reply);

            int32_t msgType = data.readInt32();

            int32_t ext1 = data.readInt32();

            int32_t ext2 = data.readInt32();

            notifyCallback(msgType, ext1, ext2);

            return NO_ERROR;

        } break;

                   ….

}

進而調用到了Camera.cpp中的函數實現了,但或許你有疑問,這些callback是涉及到一些驅動的callback,哪怎麼跟驅動聯繫起來那?

         結合之前對hardware接口調用的類Client,進一步可以發現callback的處理同樣是在Client類實例化的時候:

CameraService::Client::Client(const sp<CameraService>& cameraService,

        const sp<ICameraClient>& cameraClient, pid_t clientPid)

{

         …..

         mHardware->setCallbacks(notifyCallback,

                               dataCallback,

                               dataCallbackTimestamp,

                               mCameraService.get());

…..

}

         調用了mHardwarecallback傳入,但此處的notifyCallback並不是camera.cpp中的函數,而是client類的notifyCallback函數。

         再繼續看client類中的notifyCallback函數實現:

void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,int32_t ext2, void* user)

{

         …..

        default:

            sp<ICameraClient> c = client->mCameraClient;

            if (c != NULL) {

                c->notifyCallback(msgType, ext1, ext2);

            }

            break;

         …..

    }

         通過得到ICameraClient實例進而調用到了具體的對象CameranotifyCallback()函數。這個地方估計會遇見跟ICameraService函數調用一樣的問題,ICameraClient函數調用所需要的函數實例在哪?

         記得上述ICameraService講到的connect()函數嘛?其中有一個參數不能被忽略掉的,就是ICameraClient,但它在真正傳入的時候卻是一個ICameraClient子類camera的實例對象。

         CameraService:

         sp<ICamera> CameraService::connect(const sp<ICameraClient>& cameraClient)

          {

                   …..

         // create a new Client object

             client = new Client(this, cameraClient, callingPid);

                   …..

}

        

         Client:

CameraService::Client::Client(const sp<CameraService>& cameraService,

        const sp<ICameraClient>& cameraClient, pid_t clientPid)

{

    ….

    mCameraService = cameraService;

    mCameraClient = cameraClient;

    ….

}

這樣就清楚了,其實Client在調用設置callback的調用最終還是調用到了camera.cpp中的callback函數,進而將具體內容通過callback反饋給上層應用做出相應的處理。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章