http://blog.sina.com.cn/s/blog_9026a07a01016xrk.html
一.Camera构架分析
Android的Camera包含取景(preview)和拍摄照片(takepicture)的功能。目前Android发布版的Camera程序虽然功能比较简单,但是其程序的架构分成客户端和服务器两个部分,它们建立在Android的进程间通讯Binder的结构上。Android中Camera模块同样遵循Andorid的框架,CameraArchitectureCamera模块主要包含了libandroid_runtime.so、libui.so和libcameraservice.so等几个库文件,它们之间的调用关系如下所示:
在Camera模块的各个库中,libui.so位于核心的位置,它对上层的提供的接口主要是Camera类。libcameraservice.so是Camera的server程序,它通过继承libui.so中的类实现server的功能,并且与libui.so中的另外一部分内容通过进程间通讯(即Binder机制)的方式进行通讯。libandroid_runtime.so和libui.so两个库是公用的,其中除了Camera还有其他方面的功能。
整个Camera在运行的时候,可以大致上分成Client和Server两个部分,它们分别在两个进程中运行,它们之间使用Binder机制实现进程间通讯。这样在client调用接口,功能则在server中实现,但是在client中调用就好像直接调用server中的功能,进程间通讯的部分对上层程序不可见。
从框架结构上来看,源码中ICameraService.h、ICameraClient.h和ICamera.h三个类定义了MeidaPlayer的接口和架构,ICameraService.cpp和Camera.cpp两个文件则用于Camera架构的实现,Camera的具体功能在下层调用硬件相关的接口来实现。
从Camera的整体结构上,类Camera是整个系统核心,ICamera类提供了Camera主要功能的接口,在客户端方面调用;CameraService是Camera服务,它通过调用实际的Camera硬件接口来实现功能。事实上,图中红色虚线框的部分都是Camera程序的框架部分,它主要利用了Android的系统的Binder机制来完成通讯。蓝色的部分通过调用Camera硬件相关的接口完成具体的Camera服务功能,其它的部分是为上层的JAVA程序提供JNI接口。
在整体结构上,左边可以视为一个客户端,右边是一个可以视为服务器,二者通过Android的Bimder来实现进程间的通讯。二.Camera工作流程概述1.Camera Service的启动 ①.App_main process:进程通过AndroidRuntime调用register_jni_procs向JNI注册模块的native函数供JVM调用。AndroidRuntime::registerNativeMethods(env,"android/hardware/Camera", camMethods, NELEM(camMethods));其中camMethods定义如下:
- 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 }, { "startPreview", "()V", (void *)android_hardware_Camera_startPreview }, { "stopPreview", "()V", (void *)android_hardware_Camera_stopPreview }, { "previewEnabled", "()Z", (void *)android_hardware_Camera_previewEnabled }, { "setHasPreviewCallback", "(ZZ)V", (void *)android_hardware_Camera_setHasPreviewCallback }, { "native_autoFocus", "()V", (void *)android_hardware_Camera_autoFocus }, { "native_takePicture", "()V", (void *)android_hardware_Camera_takePicture }, { "native_setParameters", "(Ljava/lang/String;)V", (void *)android_hardware_Camera_setParameters }, { "native_getParameters", "()Ljava/lang/String;", (void *)android_hardware_Camera_getParameters }, { "reconnect", "()V", (void*)android_hardware_Camera_reconnect }, { "lock", "()I", (void*)android_hardware_Camera_lock }, { "unlock", "()I", (void*)android_hardware_Camera_unlock }, };
- int
main(int argc, char** argv) { sp proc(ProcessState::self()); sp sm = defaultServiceManager(); LOGI("ServiceManager: %p", sm.get()); AudioFlinger::instantiate(); MediaPlayerService::instantiate(); CameraService::instantiate(); ProcessState::self()->startThreadPool(); IPCThreadState::self()->joinThreadPool(); }
- public
void onCreate(Bundle icicle) { super.onCreate(icicle); Thread openCameraThread = new Thread( new Runnable() { public void run() { mCameraDevice = android.hardware.Camera.open(); } } ); ……………………… }
- LOCAL_PATH:=
$(call my-dir) # # Set USE_CAMERA_STUB for non-emulator and non-simulator builds, if you want # the camera service to use the fake camera. For emulator or simulator builds, # we always use the fake camera. ifeq ($(USE_CAMERA_STUB),) USE_CAMERA_STUB:=false ifneq ($(filter sooner generic sim,$(TARGET_DEVICE)),) USE_CAMERA_STUB:=true endif #libcamerastub endif ifeq ($(USE_CAMERA_STUB),true) # # libcamerastub # include $(CLEAR_VARS) LOCAL_SRC_FILES:= \ CameraHardwareStub.cpp \ FakeCamera.cpp LOCAL_MODULE:= libcamerastub LOCAL_SHARED_LIBRARIES:= libui include $(BUILD_STATIC_LIBRARY) endif # USE_CAMERA_STUB # # libcameraservice # include $(CLEAR_VARS) LOCAL_SRC_FILES:= \ CameraService.cpp LOCAL_SHARED_LIBRARIES:= \ libui \ libutils \ libcutils \ libmedia LOCAL_MODULE:= libcameraservice LOCAL_CFLAGS+=-DLOG_TAG=\"CameraService\" ifeq ($(USE_CAMERA_STUB), true) LOCAL_STATIC_LIBRARIES += libcamerastub LOCAL_CFLAGS += -include CameraHardwareStub.h else LOCAL_SHARED_LIBRARIES += libcamera endif include $(BUILD_SHARED_LIBRARY)
在上面的构建规则中可以看到使用了宏USE_CAMERA_STUB决定 是否使用真的Camera,如果宏为真,则使用CameraHardwareStub.cpp和FakeCamera.cpp构造一个假的Camera,如果为假则使用libcamera来构造一个实际上的Camera服务。在CameraHardwareStub.cpp中定义了CameraHardwareStub类,它继承并实现了抽象类CameraHardwareInterface中定义的真正操作Camera设备的所有的纯虚函数。通过openCameraHardware()将返回一个CameraHardwareInterface类的对象,但由于CameraHardwareInterface类是抽象类所以它并不能创建对象,而它的派生类CameraHardwareStub完全实现了其父类的纯虚函数所以openCameraHardware()返回一个指向派生类对象的基类指针用于底层设备的操作。由于CameraHardwareStub类定义的函数是去操作一个假的Camera,故通过openCameraHardware返回的指针主要用于仿真环境对Camera的模拟操作,要想通过openCameraHardware返回的指针操作真正的硬件设备则需完成以下步骤:
1. 将CameraHardwareInterface类中的所有纯虚函数的声明改为虚函数的声明(即去掉虚函数声明后的“= 0”); class CameraHardwareInterface :
- public
virtual RefBase { public: virtual ~CameraHardwareInterface() { } virtual sp getPreviewHeap() const; virtual sp getRawHeap() const; virtual status_t startPreview(preview_callback cb, void* user); virtual bool useOverlay() {return false;} virtual status_t setOverlay(const sp &overlay) {return BAD_VALUE;} virtual void stopPreview(); virtual bool previewEnabled(); virtual status_t startRecording(recording_callback cb, void* user); virtual void stopRecording(); virtual bool recordingEnabled(); virtual void releaseRecordingFrame(const sp& mem); virtual status_t autoFocus(autofocus_callback, void* user); virtual status_t takePicture(shutter_callback, raw_callback, jpeg_callback, void* user); virtual status_t cancelPicture(bool cancel_shutter, bool cancel_raw, bool cancel_jpeg); virtual status_t setParameters(const CameraParameters& params); virtual CameraParameters getParameters() const; virtual void release(); virtual status_t dump(int fd, const Vector& args) const ; };
2.编写一个源文件去定义CameraHardwareInterface类中声明的所有虚函数,并实现openCameraHardware()函数让该函数返回一个CameraHardwareInterface类对象的指针;例如:extern "C" sp openCameraHardware() { CameraHardwareInterfacerealCamera; return &realCamera; }
3.仿照其他.mk文件编写Android.mk文件用于生成一个包含步骤2编写的源文件和其他相关文件的libcamera.so文件;例如
- LOCAL_PATH
:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := libcamera LOCAL_SHARED_LIBRARIES := \ libutils \ librpc \ liblog LOCAL_SRC_FILES += MyCameraHardware.cpp LOCAL_CFLAGS += LOCAL_C_INCLUDES += LOCAL_STATIC_LIBRARIES += \ libcamera-common \ libclock-rpc \ libcommondefs-rpc include $(BUILD_SHARED_LIBRARY)