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);
//!++
#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()
{
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相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
