当前位置:操作系统 > 安卓/Android >>

Android Camera AP到Camera Hal调用流程

1.       首先是调用framework层的Camera.java(frameworks\base\core\java\android\hardware)的takePicture方法
2.       接着会调用到JNI层的android_hardware_Camera.cpp(frameworks\base\core\jni)的android_hardware_Camera_takePicture方法,其中会通过get_native_camera获取一个Camera对象。
3.       上述Camera对象的定义在Camera.h(frameworks\av\include\camera),camera.h同时定义了CameraInfo。Camera对象继承自publicBnCameraClient,DeathRecipient。Camera对象的真正实现是在Camera.cpp(frameworks\av\camera)   。BnCameraClient是作为CameraService回调而存在的。Camera对象内含有一个mCameraService变量用于保存CameraService引用。
class Camera : public BnCameraClient,public IBinder::DeathRecipient
constsp<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;
            ALOGW("CameraService notpublished, waiting...");
            usleep(500000); // 0.5 s
        } while(true);
        if (mDeathNotifier == NULL) {
            mDeathNotifier = newDeathNotifier();
        }
        binder->linkToDeath(mDeathNotifier);
        mCameraService =inte易做图ce_cast<ICameraService>(binder);
    }
4.       在ICameraClient.h和ICameraService.h文件中,ICameraClient和ICameraService都继承自IInte易做图ce,也就是Binder通信的基础,此外ICamera.h也继承自IInte易做图ce
class ICameraClient:public IInte易做图ce
class ICameraService :public IInte易做图ce
class ICamera: publicIInte易做图ce
然后发现BnCameraClient继承自BnInte易做图ce<ICameraClient>,作为ICameraClient的服务器端存在:
class BnCameraClient: publicBnInte易做图ce<ICameraClient>
BnCameraService继承自BnInte易做图ce<ICameraService>,作为ICameraService的服务器端存在:
class BnCameraService: publicBnInte易做图ce<ICameraService>
BnCamera继承自BnInte易做图ce<ICamera>,作为ICamera的服务器端存在:
class BnCamera: public BnInte易做图ce<ICamera>
对应的上述三个分别存在一个作为Binder客户端的类存在,为Bp***:
class BpCameraClient:public BpInte易做图ce<ICameraClient>
class BpCameraService:public BpInte易做图ce<ICameraService>
class BpCamera: publicBpInte易做图ce<ICamera>
上述三个的实现分别在ICameraClient.cpp、ICameraService.cpp、ICamera.cpp,在这三支文件中分别实现了三组Binder的Server端和Client端,Camera常用的方法都在ICamera中实现。
5.       Camera.cpp文件connect方法在JNI层去setupCamera的时候会调用,JNI层是在上层opencamera的时候被调用的,在camera.cpp的connect方法中获取一个ICamera对象来完成对Camera的真正操作。
sp<Camera>Camera::connect(int cameraId)
{
    ALOGV("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;
}
6.       在CameraService.h文件中可以看到CameraService定义内部类Client,CameraClient继承此类,在CameraService中持有对mClient对象的一个数组,这个数组也是Camera.cpp客户端调用connect方法后需要返回的Binder引用对象
 class Client : public BnCamera
class CameraClient : public CameraService::Client
wp<Client>         mClient[MAX_CAMERAS];
    switch(deviceVersion) {
      case CAMERA_DEVICE_API_VERSION_1_0:
        client = new CameraClient(this,cameraClient, cameraId,
                info.facing, callingPid, getpid());
        break;
      case CAMERA_DEVICE_API_VERSION_2_0:
        client = new Camera2Client(this,cameraClient, cameraId,
                info.facing, callingPid,getpid());
并且Google好像有开发新的API2.0,在此处通过判断不同的version实例化不同的CameraClient的Binder引用对象回去,目前Camera2文件夹有Burst等实现
7.       在CameraService层的CameraClient对象的定义中,持有CameraHardwareInte易做图ce对象的引用mHardware.在CameraClient的initilize方法中会创建此对象,在对象的构造方法中传入需要打开的Camera的ID。
sp<CameraHardwareInte易做图ce>     mHardware;
status_t CameraClient::initialize(camera_module_t *module) {
    int callingPid = getCallingPid();
    LOG1("CameraClient::initialize E (pid%d, id %d)", callingPid, mCameraId);
 
    char camera_device_name[10];
    status_t res;
    snprintf(camera_device_name, sizeof(camera_device_name),"%d", mCameraId);
    mHardware = new CameraHardwareInte易做图ce(camera_device_name);
    res = mHardware->initialize(&module->common);
    if (res != OK) {
        ALOGE("%s: Camera %d: unable toinitialize device: %s (%d)",
                __FUNCTION__, mCameraId,strerror(-res), res);
        mHardware.clear();
        return NO_INIT;
    }
    mHardware->setCallbacks(notifyCallback,
           dataCallback,
           dataCallbackTimestamp,
            (void*)mCameraId);
    //Enable zoom, error, focus, and metadata messages by default
    enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
                 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
//!++
#ifdef  MTK_CAMERA_BSP_SUPPORT
    // Enable MTK-extended messages by default
    enableMsgType(MTK_CAMERA_MSG_EXT_NOTIFY |MTK_CAMERA_MSG_EXT_DATA);
#endif
//!--
    LOG1("CameraClient::initialize X (pid%d, id %d)", callingPid, mCameraId);
    return OK;
}
8.       在CameraService的onFirstRef方法中,会加载CameraHal  Module
void CameraService:: onFirstRef()
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,