展会信息港展会大全

Android Camera AP到Camera Hal调用流程
来源:互联网   发布日期:2015-09-29 10:14:46   浏览:1622次  

导读: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_c...

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 =interface_cast<ICameraService>(binder);

}

4.在ICameraClient.h和ICameraService.h文件中,ICameraClient和ICameraService都继承自IInterface,也就是Binder通信的基础,此外ICamera.h也继承自IInterface

class ICameraClient:public IInterface

class ICameraService :public IInterface

class ICamera: publicIInterface

然后发现BnCameraClient继承自BnInterface<ICameraClient>,作为ICameraClient的服务器端存在:

class BnCameraClient: publicBnInterface<ICameraClient>

BnCameraService继承自BnInterface<ICameraService>,作为ICameraService的服务器端存在:

class BnCameraService: publicBnInterface<ICameraService>

BnCamera继承自BnInterface<ICamera>,作为ICamera的服务器端存在:

class BnCamera: public BnInterface<ICamera>

对应的上述三个分别存在一个作为Binder客户端的类存在,为Bp***:

class BpCameraClient:public BpInterface<ICameraClient>

class BpCameraService:public BpInterface<ICameraService>

class BpCamera: publicBpInterface<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对象的定义中,持有CameraHardwareInterface对象的引用mHardware.在CameraClient的initilize方法中会创建此对象,在对象的构造方法中传入需要打开的Camera的ID。

sp<CameraHardwareInterface>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 CameraHardwareInterface(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);

//!++

#ifdefMTK_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方法中,会加载CameraHalModule

void CameraService:: onFirstRef()

{

BnCameraService::onFirstRef();

if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,

(const hw_module_t**)&mModule) < 0) {

ALOGE("Could not load camera HALmodule");

mNumberOfCameras = 0;

}

else {

mNumberOfCameras =mModule->get_number_of_cameras();

if (mNumberOfCameras > MAX_CAMERAS){

ALOGE("Number of cameras(%d)> MAX_CAMERAS(%d).",

mNumberOfCameras, MAX_CAMERAS);

mNumberOfCameras = MAX_CAMERAS;

}

for (int i = 0; i <mNumberOfCameras; i++) {

setCameraFree(i);

}

}

}

hw_get_module方法的实现在hardware/libhardware/Hardware.c文件中实现

int hw_get_module(constchar *id, const struct hw_module_t **module)

{

return hw_get_module_by_class(id, NULL,module);

}

Camera_module_t的结构体定义在camera_common.h文件中

typedef structcamera_module {

hw_module_t common;

int (*get_number_of_cameras)(void);

int (*get_camera_info)(int camera_id,struct camera_info *info);

} camera_module_t;

Hw_module_t的定义在hardware.h文件中,hw_module_t持有对hw_module_methods_t引用,其中有定义一个open指针

typedef structhw_module_methods_t {

/** Open a specific device */

int (*open)(const struct hw_module_t*module, const char* id,

struct hw_device_t** device);

} hw_module_methods_t;

而加载CamDevice的操作是在CameraHardwareInterface的init方法中调用open

int rc =module->methods->open(module, mName.string(),

(hw_device_t **)&mDevice);

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港