Android Camera HAL浅析

1、Camera成像原理介绍

Camera工作流程图


Camera的成像原理可以简单概括如下:

景物(SCENE)通过镜头(LENS)生成的光学图像投射到图像传感器(Sensor)表面上,然后转为电信号,经过A/D(模数转换)转换后变为数字图像信号,再送到数字信号处理芯片(DSP)中加工处理,再通过IO接口传输到CPU中处理,通过DISPLAY就可以看到图像了。

电荷耦合器件(CCD)互补金属氧化物半导体(CMOS)接收光学镜头传递来的影像,经模/数转换器(A/D)转换成数字信号,经过编码后存储。

流程如下: 
1)、CCD/CMOS将被摄体的光信号转变为电信号—电子图像(模拟信号) 
2)、由模/数转换器(ADC)芯片来将模拟信号转化为数字信号 
3)、数字信号形成后,由DSP或编码库对信号进行压缩并转化为特定的图像文件格式储存

数码相机的光学镜头与传统相机相同,将影像聚到感光器件上,即(光)电荷耦合器件(CCD) 。CCD替代了传统相机中的感光胶片的位置,其功能是将光信号转换成电信号,与电视摄像相同。

CCD是半导体器件,是数码相机的核心,其内含器件的单元数量决定了数码相机的成像质量——像素,单元越多,即像素数高,成像质量越好,通常情况下像素的高低代表了数码相机的档次和技术指标。


2、Android Camera框架

Android的Camera子系统提供一个拍照和录制视频的框架。

它将Camera的上层应用与Application Framework、用户库串接起来,而正是这个用户库来与Camera的硬件层通信,从而实现操作camera硬件。

Android Camera HAL浅析_第1张图片




--------------------------------------------------------------------------------------------


Android Camera HAL浅析_第2张图片

-----------------------------------------------------------------------------------------------

Android Camera HAL浅析_第3张图片




3.Camera HAL层部分 
代码存放目录:hardware\rk29\camera
编译:

[cpp] view plain copy
  1. LOCAL_PATH:= $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3. LOCAL_SRC_FILES:=\  
  4.     CameraHal_Module.cpp\  
  5.     CameraHal.cpp\  
  6.     CameraHal_Utils.cpp\  
  7.     MessageQueue.cpp\  
  8.     CameraHal_Mem.cpp  
  9. ...................  
  10. ifeq ($(strip $(TARGET_BOARD_HARDWARE)),rk30board)  
  11. LOCAL_MODULE:= camera.rk30board  

为了实现一个具体功能的Camera,在HAL层需要一个硬件相关的Camera库(例如通过调用video for linux驱动程序和Jpeg编码程序实现或者直接用各个chip厂商实现的私有库来实现,比如Qualcomm实现的libcamera.so和libqcamera.so),此处为camera.rk30board.so实现CameraHardwareInterface规定的接口,来调用相关的库,驱动相关的driver,实现对camera硬件的操作。这个库将被Camera的服务库libcameraservice.so调用。


3.1CameraHal_Module.cpp主要是Camera HAL对上层提供的接口,和实际设备无关,上层的本地库都直接调用这个文件里面提供的接口。

[cpp] view plain copy
  1. static int camera_device_open(const hw_module_t* module, const char* name,  
  2.                 hw_device_t** device);  
  3. static int camera_device_close(hw_device_t* device);  
  4. static int camera_get_number_of_cameras(void);  
  5. static int camera_get_camera_info(int camera_id, struct camera_info *info);  
  6.   
  7.   
  8. static struct hw_module_methods_t camera_module_methods = {  
  9.         open: camera_device_open  
  10. };  
  11.   
  12.   
  13. camera_module_t HAL_MODULE_INFO_SYM = {  
  14.     common: {  
  15.          tag: HARDWARE_MODULE_TAG,  
  16.          version_major: ((CONFIG_CAMERAHAL_VERSION&0xff00)>>8),  
  17.          version_minor: CONFIG_CAMERAHAL_VERSION&0xff,  
  18.          id: CAMERA_HARDWARE_MODULE_ID,  
  19.          name: CAMERA_MODULE_NAME,  
  20.          author: "RockChip",  
  21.          methods: &camera_module_methods,  
  22.          dso: NULL, /* remove compilation warnings */  
  23.          reserved: {0}, /* remove compilation warnings */  
  24.     },  
  25.     get_number_of_cameras: camera_get_number_of_cameras,  
  26.     get_camera_info: camera_get_camera_info,  
  27. };  

//CAMERA_DEVICE_NAME              "/dev/video" 以下都是通过读取节点信息来获取摄像头的数目及摄像头设备信息
[cpp] view plain copy
  1. int camera_device_close(hw_device_t* device)  
  2. {  
  3.     int ret = 0;  
  4.     rk_camera_device_t* rk_dev = NULL;  
  5.   
  6.     LOGD("%s", __FUNCTION__);  
  7.   
  8.     android::Mutex::Autolock lock(gCameraHalDeviceLock);  
  9.   
  10.     if (!device) {  
  11.         ret = -EINVAL;  
  12.         goto done;  
  13.     }  
  14.   
  15.     rk_dev = (rk_camera_device_t*) device;  
  16.   
  17.     if (rk_dev) {  
  18.         if (gCameraHals[rk_dev->cameraid]) {  
  19.             delete gCameraHals[rk_dev->cameraid];  
  20.             gCameraHals[rk_dev->cameraid] = NULL;  
  21.             gCamerasOpen--;  
  22.         }  
  23.   
  24.         if (rk_dev->base.ops) {  
  25.             free(rk_dev->base.ops);  
  26.         }  
  27.         free(rk_dev);  
  28.     }  
  29. done:  
  30.   
  31.     return ret;  
  32. }  
  33.   
  34. /******************************************************************* 
  35.  * implementation of camera_module functions 
  36.  *******************************************************************/  
  37.   
  38. /* open device handle to one of the cameras 
  39.  * 
  40.  * assume camera service will keep singleton of each camera 
  41.  * so this function will always only be called once per camera instance 
  42.  */  
  43.   
  44. int camera_device_open(const hw_module_t* module, const char* name,  
  45.                 hw_device_t** device)  
  46. {  
  47.     int rv = 0;  
  48.     int cameraid;  
  49.     rk_camera_device_t* camera_device = NULL;  
  50.     camera_device_ops_t* camera_ops = NULL;  
  51.     android::CameraHal* camera = NULL;  
  52.   
  53.     android::Mutex::Autolock lock(gCameraHalDeviceLock);  
  54.   
  55.     LOGI("camera_device open");  
  56.   
  57.     if (name != NULL) {  
  58.         cameraid = atoi(name);  
  59.   
  60.         if(cameraid > gCamerasNumber) {  
  61.             LOGE("camera service provided cameraid out of bounds, "  
  62.                     "cameraid = %d, num supported = %d",  
  63.                     cameraid, gCamerasNumber);  
  64.             rv = -EINVAL;  
  65.             goto fail;  
  66.         }  
  67.   
  68.         if(gCamerasOpen >= CAMERAS_SUPPORTED_SIMUL_MAX) {  
  69.             LOGE("maximum number(%d) of cameras already open",gCamerasOpen);  
  70.             rv = -ENOMEM;  
  71.             goto fail;  
  72.         }  
  73.   
  74.         camera_device = (rk_camera_device_t*)malloc(sizeof(*camera_device));  
  75.         if(!camera_device) {  
  76.             LOGE("camera_device allocation fail");  
  77.             rv = -ENOMEM;  
  78.             goto fail;  
  79.         }  
  80.   
  81.         camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops));  
  82.         if(!camera_ops) {  
  83.             LOGE("camera_ops allocation fail");  
  84.             rv = -ENOMEM;  
  85.             goto fail;  
  86.         }  
  87.   
  88.         memset(camera_device, 0, sizeof(*camera_device));  
  89.         memset(camera_ops, 0, sizeof(*camera_ops));  
  90.   
  91.         camera_device->base.common.tag = HARDWARE_DEVICE_TAG;  
  92.         camera_device->base.common.version = 0;  
  93.         camera_device->base.common.module = (hw_module_t *)(module);  
  94.         camera_device->base.common.close = camera_device_close;  
  95.         camera_device->base.ops = camera_ops;  
  96.   
  97.         camera_ops->set_preview_window = camera_set_preview_window;  
  98.         camera_ops->set_callbacks = camera_set_callbacks;  
  99.         camera_ops->enable_msg_type = camera_enable_msg_type;  
  100.         camera_ops->disable_msg_type = camera_disable_msg_type;  
  101.         camera_ops->msg_type_enabled = camera_msg_type_enabled;  
  102.         camera_ops->start_preview = camera_start_preview;  
  103.         camera_ops->stop_preview = camera_stop_preview;  
  104.         camera_ops->preview_enabled = camera_preview_enabled;  
  105.         camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers;  
  106.         camera_ops->start_recording = camera_start_recording;  
  107.         camera_ops->stop_recording = camera_stop_recording;  
  108.         camera_ops->recording_enabled = camera_recording_enabled;  
  109.         camera_ops->release_recording_frame = camera_release_recording_frame;  
  110.         camera_ops->auto_focus = camera_auto_focus;  
  111.         camera_ops->cancel_auto_focus = camera_cancel_auto_focus;  
  112.         camera_ops->take_picture = camera_take_picture;  
  113.         camera_ops->cancel_picture = camera_cancel_picture;  
  114.         camera_ops->set_parameters = camera_set_parameters;  
  115.         camera_ops->get_parameters = camera_get_parameters;  
  116.         camera_ops->put_parameters = camera_put_parameters;  
  117.         camera_ops->send_command = camera_send_command;  
  118.         camera_ops->release = camera_release;  
  119.         camera_ops->dump = camera_dump;  
  120.   
  121.         *device = &camera_device->base.common;  
  122.   
  123.         // -------- RockChip specific stuff --------  
  124.   
  125.         camera_device->cameraid = cameraid;  
  126.           
  127.         camera = new android::CameraHal(cameraid);  
  128.   
  129.         if(!camera) {  
  130.             LOGE("Couldn't create instance of CameraHal class");  
  131.             rv = -ENOMEM;  
  132.             goto fail;  
  133.         }  
  134.   
  135.         gCameraHals[cameraid] = camera;  
  136.         gCamerasOpen++;  
  137.     }  
  138.   
  139.     return rv;  
  140.   
  141. fail:  
  142.     if(camera_device) {  
  143.         free(camera_device);  
  144.         camera_device = NULL;  
  145.     }  
  146.     if(camera_ops) {  
  147.         free(camera_ops);  
  148.         camera_ops = NULL;  
  149.     }  
  150.     if(camera) {  
  151.         delete camera;  
  152.         camera = NULL;  
  153.     }  
  154.     *device = NULL;  
  155.     return rv;  
  156. }  
  157.   
  158. int camera_get_number_of_cameras(void)  
  159. {  
  160.     char cam_path[20];  
  161.     char cam_num[3],i;  
  162.     int cam_cnt=0,fd=-1,rk29_cam[CAMERAS_SUPPORT_MAX];  
  163.     struct v4l2_capability capability;  
  164.     rk_cam_info_t camInfoTmp[CAMERAS_SUPPORT_MAX];  
  165.     char *ptr,**ptrr;  
  166.     char version[PROPERTY_VALUE_MAX];  
  167.   
  168.     if (gCamerasNumber > 0)  
  169.         goto camera_get_number_of_cameras_end;  
  170.       
  171.     memset(version,0x00,sizeof(version));  
  172.     sprintf(version,"%d.%d.%d",((CONFIG_CAMERAHAL_VERSION&0xff0000)>>16),  
  173.         ((CONFIG_CAMERAHAL_VERSION&0xff00)>>8),CONFIG_CAMERAHAL_VERSION&0xff);  
  174.     property_set(CAMERAHAL_VERSION_PROPERTY_KEY,version);  
  175.       
  176.     memset(&camInfoTmp[0],0x00,sizeof(rk_cam_info_t));  
  177.     memset(&camInfoTmp[1],0x00,sizeof(rk_cam_info_t));  
  178.       
  179.     for (i=0; i<10; i++) {  
  180.         cam_path[0] = 0x00;  
  181.         strcat(cam_path, CAMERA_DEVICE_NAME);  
  182.         sprintf(cam_num, "%d", i);  
  183.         strcat(cam_path,cam_num);  
  184.         fd = open(cam_path, O_RDONLY);  
  185.         if (fd < 0)  
  186.             break;  
  187.   
  188.         memset(&capability, 0, sizeof(struct v4l2_capability));  
  189.         if (ioctl(fd, VIDIOC_QUERYCAP, &capability) < 0) {  
  190.             LOGE("Video device(%s): query capability not supported.\n",cam_path);  
  191.             goto loop_continue;  
  192.         }  
  193.           
  194.         if ((capability.capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING)) != (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING)) {  
  195.             LOGD("Video device(%s): video capture not supported.\n",cam_path);  
  196.         } else {  
  197.             memset(camInfoTmp[cam_cnt&0x01].device_path,0x00, sizeof(camInfoTmp[cam_cnt&0x01].device_path));  
  198.             strcat(camInfoTmp[cam_cnt&0x01].device_path,cam_path);  
  199.             memset(camInfoTmp[cam_cnt&0x01].fival_list,0x00, sizeof(camInfoTmp[cam_cnt&0x01].fival_list));  
  200.             memcpy(camInfoTmp[cam_cnt&0x01].driver,capability.driver, sizeof(camInfoTmp[cam_cnt&0x01].driver));  
  201.             camInfoTmp[cam_cnt&0x01].version = capability.version;  
  202.             if (strstr((char*)&capability.card[0], "front") != NULL) {  
  203.                 camInfoTmp[cam_cnt&0x01].facing_info.facing = CAMERA_FACING_FRONT;  
  204.             } else {  
  205.                 camInfoTmp[cam_cnt&0x01].facing_info.facing = CAMERA_FACING_BACK;  
  206.             }    
  207.             ptr = strstr((char*)&capability.card[0],"-");  
  208.             if (ptr != NULL) {  
  209.                 ptr++;  
  210.                 camInfoTmp[cam_cnt&0x01].facing_info.orientation = atoi(ptr);  
  211.             } else {  
  212.                 camInfoTmp[cam_cnt&0x01].facing_info.orientation = 0;  
  213.             }  
  214.             cam_cnt++;  
  215.   
  216.             memset(version,0x00,sizeof(version));  
  217.             sprintf(version,"%d.%d.%d",((capability.version&0xff0000)>>16),  
  218.                 ((capability.version&0xff00)>>8),capability.version&0xff);  
  219.             property_set(CAMERADRIVER_VERSION_PROPERTY_KEY,version);  
  220.   
  221.             LOGD("%s(%d): %s:%s",__FUNCTION__,__LINE__,CAMERADRIVER_VERSION_PROPERTY_KEY,version);  
  222.               
  223.             if (cam_cnt >= CAMERAS_SUPPORT_MAX)  
  224.                 i = 10;  
  225.         }  
  226. loop_continue:  
  227.         if (fd > 0) {  
  228.             close(fd);  
  229.             fd = -1;  
  230.         }  
  231.         continue;      
  232.     }  
  233.     //zyc , change the camera infomation if there is a usb camera  
  234.     if((strcmp(camInfoTmp[0].driver,"uvcvideo") == 0)) {  
  235.         camInfoTmp[0].facing_info.facing = (camInfoTmp[1].facing_info.facing == CAMERA_FACING_FRONT) ? CAMERA_FACING_BACK:CAMERA_FACING_FRONT;  
  236.         camInfoTmp[0].facing_info.orientation = (camInfoTmp[0].facing_info.facing == CAMERA_FACING_FRONT)?270:90;  
  237.     } else if((strcmp(camInfoTmp[1].driver,"uvcvideo") == 0)) {  
  238.         camInfoTmp[1].facing_info.facing = (camInfoTmp[0].facing_info.facing == CAMERA_FACING_FRONT) ? CAMERA_FACING_BACK:CAMERA_FACING_FRONT;  
  239.         camInfoTmp[1].facing_info.orientation = (camInfoTmp[1].facing_info.facing == CAMERA_FACING_FRONT)?270:90;  
  240.     }  
  241.     gCamerasNumber = cam_cnt;  
  242.   
  243. #if CONFIG_AUTO_DETECT_FRAMERATE  
  244.     rk29_cam[0] = 0xff;  
  245.     rk29_cam[1] = 0xff;  
  246.     for (i=0; i<cam_cnt; i++) {  
  247.         if (strcmp((char*)&camInfoTmp[i].driver[0],"rk29xx-camera") == 0) {  
  248.             if (strcmp((char*)&camInfoTmp[i].driver[0],(char*)&gCamInfos[i].driver[0]) != 0) {  
  249.                 rk29_cam[i] = i;   
  250.             }  
  251.         } else {  
  252.             rk29_cam[i] = 0xff;  
  253.         }  
  254.     }  
  255.   
  256.     if ((rk29_cam[0] != 0xff) || (rk29_cam[1] != 0xff)) {  
  257.         if (gCameraFpsDetectThread == NULL) {  
  258.             gCameraFpsDetectThread = new CameraFpsDetectThread();  
  259.             LOGD("%s create CameraFpsDetectThread for enum camera framerate!!",__FUNCTION__);  
  260.             gCameraFpsDetectThread->run("CameraFpsDetectThread", ANDROID_PRIORITY_AUDIO);  
  261.         }  
  262.     }  
  263. #endif  
  264.     #if CONFIG_CAMERA_SINGLE_SENSOR_FORCE_BACK_FOR_CTS  
  265.     if ((gCamerasNumber==1) && (camInfoTmp[0].facing_info.facing==CAMERA_FACING_FRONT)) {  
  266.         gCamerasNumber = 2;  
  267.         memcpy(&camInfoTmp[1],&camInfoTmp[0], sizeof(rk_cam_info_t));  
  268.         camInfoTmp[1].facing_info.facing = CAMERA_FACING_BACK;  
  269.     }  
  270.     #endif  
  271.       
  272.     memcpy(&gCamInfos[0], &camInfoTmp[0], sizeof(rk_cam_info_t));  
  273.     memcpy(&gCamInfos[1], &camInfoTmp[1], sizeof(rk_cam_info_t));  
  274.       
  275. camera_get_number_of_cameras_end:  
  276.     LOGD("%s(%d): Current board have %d cameras attached.",__FUNCTION__, __LINE__, gCamerasNumber);  
  277.     return gCamerasNumber;  
  278. }  
  279.   
  280. int camera_get_camera_info(int camera_id, struct camera_info *info)  
  281. {  
  282.     int rv = 0,fp;  
  283.     int face_value = CAMERA_FACING_BACK;  
  284.     int orientation = 0;  
  285.     char process_name[30];  
  286.           
  287.     if(camera_id > gCamerasNumber) {  
  288.         LOGE("%s camera_id out of bounds, camera_id = %d, num supported = %d",__FUNCTION__,  
  289.                 camera_id, gCamerasNumber);  
  290.         rv = -EINVAL;  
  291.         goto end;  
  292.     }  
  293.   
  294.     info->facing = gCamInfos[camera_id].facing_info.facing;  
  295.     info->orientation = gCamInfos[camera_id].facing_info.orientation;         
  296. end:  
  297.     LOGD("%s(%d): camera_%d facing(%d), orientation(%d)",__FUNCTION__,__LINE__,camera_id,info->facing,info->orientation);  
  298.     return rv;  
  299. }  
而对于为上层提供的HAL层接口函数,并不直接操作节点,而是间接的去调用CameraHal.cpp去操作节点。
[cpp] view plain copy
  1. int camera_start_preview(struct camera_device * device)  
  2. {  
  3.     int rv = -EINVAL;  
  4.     rk_camera_device_t* rk_dev = NULL;  
  5.   
  6.     LOGV("%s", __FUNCTION__);  
  7.   
  8.     if(!device)  
  9.         return rv;  
  10.   
  11.     rk_dev = (rk_camera_device_t*) device;  
  12.   
  13.     rv = gCameraHals[rk_dev->cameraid]->startPreview();  
  14.   
  15.     return rv;  
  16. }  
  17.   
  18. void camera_stop_preview(struct camera_device * device)  
  19. {  
  20.     rk_camera_device_t* rk_dev = NULL;  
  21.   
  22.     LOGV("%s", __FUNCTION__);  
  23.   
  24.     if(!device)  
  25.         return;  
  26.   
  27.     rk_dev = (rk_camera_device_t*) device;  
  28.   
  29.     gCameraHals[rk_dev->cameraid]->stopPreview();  
  30. }  

3.2CameraHal.cpp去操作节点来进行实际的操作。
//这个几个线程很关键,分别对应着各种不同的情况,但是一直在运行
[cpp] view plain copy
  1. CameraHal::CameraHal(int cameraId)  
  2.             :mParameters(),  
  3.             mSnapshotRunning(-1),  
  4.             mCommandRunning(-1),  
  5.             mPreviewRunning(STA_PREVIEW_PAUSE),  
  6.             mPreviewLock(),  
  7.             mPreviewCond(),  
  8.             mDisplayRuning(STA_DISPLAY_PAUSE),  
  9.             mDisplayLock(),  
  10.             mDisplayCond(),  
  11.             mANativeWindowLock(),  
  12.             mANativeWindowCond(),  
  13.             mANativeWindow(NULL),  
  14.             mPreviewErrorFrameCount(0),  
  15.             mPreviewFrameSize(0),  
  16.             mCamDriverFrmHeightMax(0),  
  17.             mCamDriverFrmWidthMax(0),  
  18.             mPreviewBufferCount(0),  
  19.             mCamDriverPreviewFmt(0),  
  20.             mCamDriverPictureFmt(0),  
  21.             mCamDriverV4l2BufferLen(0),  
  22.             mPreviewMemory(NULL),  
  23.             mRawBufferSize(0),  
  24.             mJpegBufferSize(0),  
  25.             mMsgEnabled(0),  
  26.             mEffect_number(0),  
  27.             mScene_number(0),  
  28.             mWhiteBalance_number(0),  
  29.             mFlashMode_number(0),  
  30.             mGps_latitude(-1),  
  31.             mGps_longitude(-1),  
  32.             mGps_altitude(-1),  
  33.             mGps_timestamp(-1),  
  34.             displayThreadCommandQ("displayCmdQ"),  
  35.             displayThreadAckQ("displayAckQ"),              
  36.             previewThreadCommandQ("previewCmdQ"),  
  37.             previewThreadAckQ("previewAckQ"),  
  38.             commandThreadCommandQ("commandCmdQ"),  
  39.             commandThreadAckQ("commandAckQ"),  
  40.             snapshotThreadCommandQ("snapshotCmdQ"),  
  41.             snapshotThreadAckQ("snapshotAckQ"),  
  42.             mCamBuffer(NULL)  
  43. {  
  44.     int fp,i;  
  45.       
  46.     cameraCallProcess[0] = 0x00;   
  47.     sprintf(cameraCallProcess,"/proc/%d/cmdline",getCallingPid());  
  48.     fp = open(cameraCallProcess, O_RDONLY);  
  49.     if (fp < 0) {  
  50.         memset(cameraCallProcess,0x00,sizeof(cameraCallProcess));  
  51.         LOGE("Obtain calling process info failed");  
  52.     } else {  
  53.         memset(cameraCallProcess,0x00,sizeof(cameraCallProcess));  
  54.         read(fp, cameraCallProcess, 30);  
  55.         close(fp);  
  56.         fp = -1;  
  57.         LOGD("Calling process is: %s",cameraCallProcess);  
  58.     }  
  59.       
  60.     iCamFd = -1;  
  61.     memset(&mCamDriverSupportFmt[0],0, sizeof(mCamDriverSupportFmt));  
  62.     mRecordRunning = false;  
  63.     mPictureRunning = STA_PICTURE_STOP;  
  64.     mExitAutoFocusThread = false;  
  65.     mDriverMirrorSupport = false;  
  66.     mDriverFlipSupport = false;  
  67.     mPreviewCmdReceived = false;  
  68.     mPreviewStartTimes = 0x00;      
  69.     memset(mCamDriverV4l2Buffer, 0x00, sizeof(mCamDriverV4l2Buffer));  
  70.     memset(mDisplayFormat,0x00,sizeof(mDisplayFormat));  
  71.     for (i=0; i<CONFIG_CAMERA_PRVIEW_BUF_CNT; i++) {  
  72.         mPreviewBufferMap[i] = NULL;  
  73.         mDisplayBufferMap[i] = NULL;  
  74.         memset(&mGrallocBufferMap[i],0x00,sizeof(rk_previewbuf_info_t));  
  75.         mPreviewBufs[i] = NULL;  
  76.         mVideoBufs[i] = NULL;  
  77.   
  78.         mPreviewBuffer[i] = NULL;  
  79.     }  
  80.       
  81.     //open the rga device,zyc  
  82.     mRGAFd = -1;  
  83.   
  84.     if (cameraCreate(cameraId) == 0) {  
  85.         initDefaultParameters();  
  86.   
  87.         cameraRawJpegBufferCreate(mRawBufferSize,mJpegBufferSize);  
  88.   
  89.         mDisplayThread = new DisplayThread(this);    
  90.         mPreviewThread = new PreviewThread(this);  
  91.         mCommandThread = new CommandThread(this);  
  92.         mPictureThread = new PictureThread(this);  
  93.     mSnapshotThread = new SnapshotThread(this);  
  94.         mAutoFocusThread = new AutoFocusThread(this);  
  95.         mDisplayThread->run("CameraDispThread",ANDROID_PRIORITY_URGENT_DISPLAY);  
  96.         mPreviewThread->run("CameraPreviewThread",ANDROID_PRIORITY_DISPLAY);  
  97.         mCommandThread->run("CameraCmdThread", ANDROID_PRIORITY_URGENT_DISPLAY);  
  98.         mAutoFocusThread->run("CameraAutoFocusThread", ANDROID_PRIORITY_DISPLAY);  
  99.         mSnapshotThread->run("CameraSnapshotThread", ANDROID_PRIORITY_NORMAL);  
  100.   
  101.         LOGD("CameraHal create success!");  
  102.     } else {  
  103.         mPreviewThread = NULL;  
  104.         mDisplayThread = NULL;  
  105.         mCommandThread = NULL;  
  106.         mPictureThread = NULL;  
  107.         mSnapshotThread = NULL;       
  108.         mAutoFocusThread = NULL;  
  109.     }  
  110.   
  111. }  


初始化时参数的配置,默认参数图片大小,分辨率,帧等:
[cpp] view plain copy
  1. void CameraHal::initDefaultParameters()  
  2. {  
  3.     CameraParameters params;  
  4.     String8 parameterString;  
  5.     int i,j,previewFrameSizeMax;  
  6.     char cur_param[32],cam_size[10];  
  7.     char str_picturesize[100];//We support at most 4 resolutions: 2592x1944,2048x1536,1600x1200,1024x768   
  8.     int ret,picture_size_bit;  
  9.     struct v4l2_format fmt;      
  10.       
  11.     LOG_FUNCTION_NAME      
  12.     memset(str_picturesize,0x00,sizeof(str_picturesize));  
  13.     if (CAMERA_IS_UVC_CAMERA()) {  
  14.         /*preview size setting*/  
  15.         struct v4l2_frmsizeenum fsize;                  
  16.           
  17.         memset(&fsize, 0, sizeof(fsize));           
  18.         picture_size_bit = 0;  
  19.         fsize.index = 0;         
  20.         fsize.pixel_format = mCamDriverPreviewFmt;  
  21.         while ((ret = ioctl(iCamFd, VIDIOC_ENUM_FRAMESIZES, &fsize)) == 0) {  
  22.             if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {                   
  23.                 if ((fsize.discrete.width == 320) && (fsize.discrete.height == 240)) {  
  24.                     if (strcmp(cameraCallProcess,"com.tencent.android.pad") == 0) {  
  25.                         fsize.index++;  
  26.                         continue;  
  27.                     }  
  28.                 }  
  29.                 memset(cam_size,0x00,sizeof(cam_size));  
  30.                 if (parameterString.size() != 0)   
  31.                     cam_size[0]=',';  
  32.                 sprintf((char*)(&cam_size[strlen(cam_size)]),"%d",fsize.discrete.width);  
  33.                 strcat(cam_size, "x");  
  34.                 sprintf((char*)(&cam_size[strlen(cam_size)]),"%d",fsize.discrete.height);  
  35.                 parameterString.append((const char*)cam_size);  
  36.   
  37.                 if ((strlen(str_picturesize)+strlen(cam_size))<sizeof(str_picturesize)) {  
  38.                     if (fsize.discrete.width <= 2592) {  
  39.                         strcat(str_picturesize, cam_size);  
  40.                         if (fsize.discrete.width > mCamDriverFrmWidthMax) {  
  41.                             mCamDriverFrmWidthMax = fsize.discrete.width;  
  42.                             mCamDriverFrmHeightMax = fsize.discrete.height;  
  43.                         }   
  44.                     }  
  45.                 } else {  
  46.                     break;  
  47.                 }  
  48.             } else if (fsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {  
  49.   
  50.                 break;  
  51.             } else if (fsize.type == V4L2_FRMSIZE_TYPE_STEPWISE) {  
  52.                   
  53.                 break;  
  54.             }  
  55.             fsize.index++;  
  56.         }  
  57.         if (ret != 0 && errno != EINVAL) {  
  58.             LOGE("ERROR enumerating frame sizes: %d\n", errno);  
  59.         }  
  60.   
  61.         params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, parameterString.string());  
  62.         params.setPreviewSize(640,480);  
  63.         /*picture size setting*/        
  64.         params.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES, str_picturesize);          
  65.         params.setPictureSize(mCamDriverFrmWidthMax,  mCamDriverFrmHeightMax);          
  66.   
  67.         if (mCamDriverFrmWidthMax <= 1024) {  
  68.             mRawBufferSize = RAW_BUFFER_SIZE_1M;  
  69.             mJpegBufferSize = JPEG_BUFFER_SIZE_1M;  
  70.         } else if (mCamDriverFrmWidthMax <= 1600) {  
  71.             mRawBufferSize = RAW_BUFFER_SIZE_2M;  
  72.             mJpegBufferSize = JPEG_BUFFER_SIZE_2M;  
  73.         } else if (mCamDriverFrmWidthMax <= 2048) {  
  74.             mRawBufferSize = RAW_BUFFER_SIZE_3M;  
  75.             mJpegBufferSize = JPEG_BUFFER_SIZE_3M;  
  76.         } else if (mCamDriverFrmWidthMax <= 2592) {                                
  77.             mRawBufferSize = RAW_BUFFER_SIZE_5M;  
  78.             mJpegBufferSize = JPEG_BUFFER_SIZE_5M;  
  79.         } else {  
  80.             LOGE("%s(%d):Camera Hal is only support 5Mega camera, but the uvc camera is %dx%d",  
  81.                  __FUNCTION__,__LINE__,mCamDriverFrmWidthMax, mCamDriverFrmHeightMax);  
  82.             mRawBufferSize = RAW_BUFFER_SIZE_5M;  
  83.             mJpegBufferSize = JPEG_BUFFER_SIZE_5M;  
  84.         }  
  85.           
  86.         /* set framerate */  
  87.         struct v4l2_streamparm setfps;            
  88.           
  89.         memset(&setfps, 0, sizeof(struct v4l2_streamparm));  
  90.         setfps.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  91.         setfps.parm.capture.timeperframe.numerator=1;  
  92.         setfps.parm.capture.timeperframe.denominator=15;  
  93.         ret = ioctl(iCamFd, VIDIOC_S_PARM, &setfps);   
  94.   
  95.         /*frame rate setting*/      
  96.         params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES, "15");  
  97.         params.setPreviewFrameRate(15);  
  98.         /*frame per second setting*/  
  99.         parameterString = "15000,15000";  
  100.         params.set(CameraParameters::KEY_PREVIEW_FPS_RANGE, parameterString.string());  
  101.         parameterString = "(15000,15000)";  
  102.         params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE, parameterString.string());  
  103.         /*not support zoom */  
  104.         params.set(CameraParameters::KEY_ZOOM_SUPPORTED, "false");  
  105.   
  106.     } else if (CAMERA_IS_RKSOC_CAMERA()) {  
  107.       
  108.         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;  
  109.         fmt.fmt.pix.pixelformat= mCamDriverPreviewFmt;  
  110.         fmt.fmt.pix.field = V4L2_FIELD_NONE;  
  111.           
  112.         /*picture size setting*/  
  113.         fmt.fmt.pix.width = 10000;  
  114.         fmt.fmt.pix.height = 10000;  
  115.         ret = ioctl(iCamFd, VIDIOC_TRY_FMT, &fmt);  
  116.           
  117.         mCamDriverFrmWidthMax = fmt.fmt.pix.width;  
  118.         mCamDriverFrmHeightMax = fmt.fmt.pix.height;          
  119.   
  120.         if (mCamDriverFrmWidthMax > 2592) {  
  121.             LOGE("Camera driver support maximum resolution(%dx%d) is overflow 5Mega!",mCamDriverFrmWidthMax,mCamDriverFrmHeightMax);  
  122.             mCamDriverFrmWidthMax = 2592;  
  123.             mCamDriverFrmHeightMax = 1944;  
  124.         }  
  125.           
  126.         /*preview size setting*/   
  127.         if (mCamDriverFrmWidthMax >= 176) {              
  128.             fmt.fmt.pix.width = 176;  
  129.             fmt.fmt.pix.height = 144;  
  130.             if (ioctl(iCamFd, VIDIOC_TRY_FMT, &fmt) == 0) {  
  131.                 if ((fmt.fmt.pix.width == 176) && (fmt.fmt.pix.height == 144)) {  
  132.                     parameterString.append("176x144");  
  133.                     params.setPreviewSize(176, 144);  
  134.                     previewFrameSizeMax =  PAGE_ALIGN(176*144*2)*2;          // 176*144*2     rgb565  
  135.                     //params.set(CameraParameters::KEY_PREFERRED_PREVIEW_SIZE_FOR_VIDEO,"176x144");  
  136.                 }  
  137.             }  
  138.         }  
  139.   
  140.         if ((mCamDriverCapability.version & 0xff) >= 0x07) {  
  141.             int tmp0,tmp1;  
  142.             if (cameraFramerateQuery(mCamDriverPreviewFmt, 240,160,&tmp1,&tmp0) == 0) {  
  143.                 if (mCamDriverFrmWidthMax >= 240) {              
  144.                     fmt.fmt.pix.width = 240;  
  145.                     fmt.fmt.pix.height = 160;  
  146.                     if (ioctl(iCamFd, VIDIOC_TRY_FMT, &fmt) == 0) {  
  147.                         if ((fmt.fmt.pix.width == 240) && (fmt.fmt.pix.height == 160)) {  
  148.                             parameterString.append(",240x160");  
  149.                             params.setPreviewSize(240, 160);  
  150.                             previewFrameSizeMax =  PAGE_ALIGN(240*160*2)*2;          // 240*160*2     rgb565  
  151.                               
  152.                         }  
  153.                     }  
  154.                 }  
  155.             }  
  156.         }  
  157.           
  158.         if (strcmp(cameraCallProcess,"com.tencent.android.pad")) {  
  159.             if (mCamDriverFrmWidthMax >= 320) {              
  160.                 fmt.fmt.pix.width = 320;  
  161.                 fmt.fmt.pix.height = 240;  
  162.                 if (ioctl(iCamFd, VIDIOC_TRY_FMT, &fmt) == 0) {  
  163.                     if ((fmt.fmt.pix.width == 320) && (fmt.fmt.pix.height == 240)) {  
  164.                         parameterString.append(",320x240");  
  165.                         params.setPreviewSize(320, 240);  
  166.                         previewFrameSizeMax =  PAGE_ALIGN(320*240*2)*2;          // 320*240*2  
  167.                           
  168.                     }  
  169.                 }  
  170.             }  
  171.         }  
  172.         if (mCamDriverFrmWidthMax >= 352) {              
  173.             fmt.fmt.pix.width = 352;  
  174.             fmt.fmt.pix.height = 288;  
  175.             if (ioctl(iCamFd, VIDIOC_TRY_FMT, &fmt) == 0) {  
  176.                 if ((fmt.fmt.pix.width == 352) && (fmt.fmt.pix.height == 288)) {  
  177.                     parameterString.append(",352x288");  
  178.                     params.setPreviewSize(352, 288);  
  179.                     previewFrameSizeMax =  PAGE_ALIGN(352*288*2)*2;          // 352*288*1.5*2  
  180.                       
  181.                 }  
  182.             }  
  183.         }  
  184.           
  185.         if (mCamDriverFrmWidthMax >= 640) {              
  186.             fmt.fmt.pix.width = 640;  
  187.             fmt.fmt.pix.height = 480;  
  188.             if (ioctl(iCamFd, VIDIOC_TRY_FMT, &fmt) == 0) {  
  189.                 if ((fmt.fmt.pix.width == 640) && (fmt.fmt.pix.height == 480)) {  
  190.                     parameterString.append(",640x480");  
  191.                     params.setPreviewSize(640, 480);  
  192.                     previewFrameSizeMax =  PAGE_ALIGN(640*480*2)*2;          // 640*480*1.5*2  
  193.                       
  194.                 }  
  195.             }  
  196.         }  
  197.   
  198.         if (mCamDriverFrmWidthMax >= 720) {  
  199.             fmt.fmt.pix.width = 720;  
  200.             fmt.fmt.pix.height = 480;  
  201.             if (ioctl(iCamFd, VIDIOC_TRY_FMT, &fmt) == 0) {  
  202.                 if ((fmt.fmt.pix.width == 720) && (fmt.fmt.pix.height == 480)) {  
  203.                     parameterString.append(",720x480");  
  204.                     previewFrameSizeMax =  PAGE_ALIGN(720*480*2)*2;          // 720*480*1.5*2  
  205.                       
  206.                 }  
  207.             }  
  208.         }  
  209.   
  210.         if (mCamDriverFrmWidthMax >= 1280) {  
  211.             fmt.fmt.pix.width = 1280;  
  212.             fmt.fmt.pix.height = 720;  
  213.             if (ioctl(iCamFd, VIDIOC_TRY_FMT, &fmt) == 0) {  
  214.                 if ((fmt.fmt.pix.width == 1280) && (fmt.fmt.pix.height == 720)) {  
  215.                     parameterString.append(",1280x720");  
  216.                     previewFrameSizeMax =  PAGE_ALIGN(1280*720*2)*2;          // 1280*720*1.5*2  
  217.                       
  218.                 }  
  219.             }  
  220.         }  
  221.         mSupportPreviewSizeReally = parameterString;  
  222.         /* [email protected]: Facelock speed is low, so scale down preview data to facelock for speed up */  
  223.         if ((strcmp(cameraCallProcess,"com.android.facelock")==0)) {              
  224.             if (strstr(mSupportPreviewSizeReally.string(),"640x480")||  
  225.                 strstr(mSupportPreviewSizeReally.string(),"320x240")) {  
  226.                 parameterString = "160x120";  
  227.                 params.setPreviewSize(160, 120);      
  228.             }  
  229.         }  
  230.         params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES, parameterString.string());  
  231.           
  232.         strcat(str_picturesize,parameterString.string());  
  233.         strcat(str_picturesize,",");  
  234.         if(mCamDriverFrmWidthMax <= 640){  
  235.             strcat( str_picturesize,"640x480,320x240");  
  236.             mRawBufferSize = RAW_BUFFER_SIZE_0M3;  
  237.             mJpegBufferSize = JPEG_BUFFER_SIZE_0M3;  
  238.             params.setPictureSize(640,480);  
  239.         }else if (mCamDriverFrmWidthMax <= 1280) {  
  240.             strcat( str_picturesize,"1024x768,640x480,320x240");  
  241.             mRawBufferSize = RAW_BUFFER_SIZE_1M;  
  242.             mJpegBufferSize = JPEG_BUFFER_SIZE_1M;  
  243.             params.setPictureSize(1024,768);  
  244.         } else if (mCamDriverFrmWidthMax <= 1600) {  
  245.             strcat( str_picturesize,"1600x1200,1024x768,640x480");  
  246.             mRawBufferSize = RAW_BUFFER_SIZE_2M;  
  247.             mJpegBufferSize = JPEG_BUFFER_SIZE_2M;  
  248.             params.setPictureSize(1600,1200);  
  249.         } else if (mCamDriverFrmWidthMax <= 2048) {  
  250.             strcat( str_picturesize,"2048x1536,1600x1200,1024x768");  
  251.             mRawBufferSize = RAW_BUFFER_SIZE_3M;  
  252.             mJpegBufferSize = JPEG_BUFFER_SIZE_3M;    
  253.             params.setPictureSize(2048,1536);  
  254.         } else if (mCamDriverFrmWidthMax <= 2592) {                                
  255.             strcat( str_picturesize,"2592x1944,2048x1536,1600x1200,1024x768");  
  256.             params.setPictureSize(2592,1944);  
  257.             mRawBufferSize = RAW_BUFFER_SIZE_5M;  
  258.             mJpegBufferSize = JPEG_BUFFER_SIZE_5M;  
  259.         } else {  
  260.             sprintf(str_picturesize, "%dx%d", mCamDriverFrmWidthMax,mCamDriverFrmHeightMax);  
  261.             mRawBufferSize = RAW_BUFFER_SIZE_5M;  
  262.             mJpegBufferSize = JPEG_BUFFER_SIZE_5M;  
  263.             params.setPictureSize(mCamDriverFrmWidthMax,mCamDriverFrmHeightMax);  
  264.         }  
  265.           
  266.         params.set(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES, str_picturesize);  
  267.   
  268.         /*frame rate setting*/  
  269.         cameraFpsInfoSet(params);  
  270.           
  271.         /*zoom setting*/  
  272.         struct v4l2_queryctrl zoom;  
  273.         char str_zoom_max[3],str_zoom_element[5];  
  274.         char str_zoom[200];  
  275.         strcpy(str_zoom, "");//default zoom  
  276.         int max;  
  277.   
  278.         zoom.id = V4L2_CID_ZOOM_ABSOLUTE;  
  279.         if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &zoom)) {  
  280.             mZoomMax = zoom.maximum;  
  281.             mZoomMin= zoom.minimum;  
  282.             mZoomStep = zoom.step;    
  283.   
  284.             max = (mZoomMax - mZoomMin)/mZoomStep;  
  285.             sprintf(str_zoom_max,"%d",max);  
  286.             params.set(CameraParameters::KEY_ZOOM_SUPPORTED, "true");  
  287.             params.set(CameraParameters::KEY_MAX_ZOOM, str_zoom_max);  
  288.             params.set(CameraParameters::KEY_ZOOM, "0");  
  289.             for (i=mZoomMin; i<=mZoomMax; i+=mZoomStep) {  
  290.                 sprintf(str_zoom_element,"%d,", i);  
  291.                 strcat(str_zoom,str_zoom_element);  
  292.             }  
  293.             params.set(CameraParameters::KEY_ZOOM_RATIOS, str_zoom);  
  294.         }  
  295.     }  
  296.     /*preview format setting*/  
  297.     params.set(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS, "yuv420sp,rgb565,yuv420p");  
  298.     params.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT,CameraParameters::PIXEL_FORMAT_YUV420SP);  
  299.     if (strcmp(cameraCallProcess,"com.android.camera")==0) {    //for PanoramaActivity  
  300.         params.setPreviewFormat(CameraParameters::PIXEL_FORMAT_RGB565);     
  301.     } else {  
  302.         params.setPreviewFormat(CameraParameters::PIXEL_FORMAT_YUV420SP);     
  303.     }  
  304.     /* [email protected]: preset the displayformat for cts */  
  305.     strcpy(mDisplayFormat,CAMERA_DISPLAY_FORMAT_NV12);  
  306.   
  307.   
  308.     params.set(CameraParameters::KEY_VIDEO_FRAME_FORMAT,CameraParameters::PIXEL_FORMAT_YUV420SP);  
  309.   
  310.     /*picture format setting*/  
  311.     params.set(CameraParameters::KEY_SUPPORTED_PICTURE_FORMATS, CameraParameters::PIXEL_FORMAT_JPEG);  
  312.     params.setPictureFormat(CameraParameters::PIXEL_FORMAT_JPEG);  
  313.   
  314.     /*jpeg quality setting*/  
  315.     params.set(CameraParameters::KEY_JPEG_QUALITY, "70");  
  316.   
  317.     /*white balance setting*/  
  318.     struct v4l2_queryctrl whiteBalance;  
  319.     struct v4l2_querymenu *whiteBalance_menu = mWhiteBalance_menu;  
  320.     char str_whitebalance[200];  
  321.     strcpy(str_whitebalance, "");//default whitebalance  
  322.     whiteBalance.id = V4L2_CID_DO_WHITE_BALANCE;  
  323.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &whiteBalance)) {  
  324.         for (i = whiteBalance.minimum; i <= whiteBalance.maximum; i += whiteBalance.step) {  
  325.             whiteBalance_menu->id = V4L2_CID_DO_WHITE_BALANCE;  
  326.             whiteBalance_menu->index = i;  
  327.             if (!ioctl(iCamFd, VIDIOC_QUERYMENU, whiteBalance_menu)) {  
  328.                 if (i != whiteBalance.minimum)  
  329.                     strcat(str_whitebalance, ",");  
  330.                 strcat(str_whitebalance, (char *)whiteBalance_menu->name);  
  331.                 if (whiteBalance.default_value == i) {  
  332.                     strcpy(cur_param, (char *)whiteBalance_menu->name);  
  333.                 }  
  334.                 mWhiteBalance_number++;  
  335.             }  
  336.             whiteBalance_menu++;  
  337.         }  
  338.         params.set(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE, str_whitebalance);  
  339.         params.set(CameraParameters::KEY_WHITE_BALANCE, cur_param);  
  340.     }  
  341.   
  342.     /*color effect setting*/  
  343.     struct v4l2_queryctrl effect;  
  344.     struct v4l2_querymenu *effect_menu = mEffect_menu;  
  345.     char str_effect[200];  
  346.     strcpy(str_effect, "");//default effect  
  347.     effect.id = V4L2_CID_EFFECT;  
  348.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &effect)) {  
  349.         for (i = effect.minimum; i <= effect.maximum; i += effect.step) {  
  350.             effect_menu->id = V4L2_CID_EFFECT;  
  351.             effect_menu->index = i;  
  352.             if (!ioctl(iCamFd, VIDIOC_QUERYMENU, effect_menu)) {  
  353.                 if (i != effect.minimum)  
  354.                     strcat(str_effect, ",");  
  355.                 strcat(str_effect, (char *)effect_menu->name);  
  356.                 if (effect.default_value == i) {  
  357.                     strcpy(cur_param, (char *)effect_menu->name);  
  358.                 }  
  359.                 mEffect_number++;  
  360.             }  
  361.             effect_menu++;  
  362.         }  
  363.         params.set(CameraParameters::KEY_SUPPORTED_EFFECTS, str_effect);  
  364.         params.set(CameraParameters::KEY_EFFECT, cur_param);  
  365.     }  
  366.   
  367.     /*scene setting*/  
  368.     struct v4l2_queryctrl scene;  
  369.     struct v4l2_querymenu *scene_menu = mScene_menu;  
  370.     char str_scene[200];  
  371.     strcpy(str_scene, "");//default scene  
  372.     scene.id = V4L2_CID_SCENE;  
  373.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &scene)) {  
  374.         for (i=scene.minimum; i<=scene.maximum; i+=scene.step) {  
  375.             scene_menu->id = V4L2_CID_SCENE;  
  376.             scene_menu->index = i;  
  377.             if (!ioctl(iCamFd, VIDIOC_QUERYMENU, scene_menu)) {  
  378.                 if (i != scene.minimum)  
  379.                     strcat(str_scene, ",");  
  380.                 strcat(str_scene, (char *)scene_menu->name);  
  381.                 if (scene.default_value == i) {  
  382.                     strcpy(cur_param, (char *)scene_menu->name);  
  383.                 }  
  384.                 mScene_number++;  
  385.             }  
  386.             scene_menu++;  
  387.         }  
  388.         params.set(CameraParameters::KEY_SUPPORTED_SCENE_MODES, str_scene);  
  389.         params.set(CameraParameters::KEY_SCENE_MODE, cur_param);  
  390.   
  391.     }  
  392.   
  393.     /*flash mode setting*/  
  394.     struct v4l2_queryctrl flashMode;  
  395.     struct v4l2_querymenu *flashMode_menu = mFlashMode_menu;  
  396.     char str_flash[200];  
  397.     strcpy(str_flash, "");//default flash  
  398.     flashMode.id = V4L2_CID_FLASH;  
  399.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &flashMode)) {  
  400.         for (i = flashMode.minimum; i <= flashMode.maximum; i += flashMode.step) {  
  401.             flashMode_menu->id = V4L2_CID_FLASH;  
  402.             flashMode_menu->index = i;  
  403.             if (!ioctl(iCamFd, VIDIOC_QUERYMENU, flashMode_menu)) {  
  404.                 if (i != flashMode.minimum)  
  405.                     strcat(str_flash, ",");  
  406.                 strcat(str_flash, (char *)flashMode_menu->name);  
  407.                 if (flashMode.default_value == i) {  
  408.                     strcpy(cur_param, (char *)flashMode_menu->name);  
  409.                 }  
  410.                 mFlashMode_number++;  
  411.             }  
  412.             flashMode_menu++;  
  413.         }  
  414.         params.set(CameraParameters::KEY_SUPPORTED_FLASH_MODES, str_flash);  
  415.         params.set(CameraParameters::KEY_FLASH_MODE, cur_param);  
  416.     }  
  417.       
  418.     /*focus mode setting*/  
  419.     struct v4l2_queryctrl focus;  
  420.   
  421.     parameterString = CameraParameters::FOCUS_MODE_FIXED;  
  422.     params.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_FIXED);  
  423.     focus.id = V4L2_CID_FOCUS_AUTO;  
  424.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &focus)) {  
  425.         parameterString.append(",");  
  426.         parameterString.append(CameraParameters::FOCUS_MODE_AUTO);  
  427.         params.set(CameraParameters::KEY_FOCUS_MODE, CameraParameters::FOCUS_MODE_AUTO);  
  428.     }  
  429.   
  430.     focus.id = V4L2_CID_FOCUS_CONTINUOUS;  
  431.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &focus)) {  
  432.         parameterString.append(",");  
  433.         parameterString.append(CameraParameters::FOCUS_MODE_EDOF);  
  434.     }  
  435.   
  436.     focus.id = V4L2_CID_FOCUS_ABSOLUTE;  
  437.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &focus)) {  
  438.         parameterString.append(",");  
  439.         parameterString.append(CameraParameters::FOCUS_MODE_INFINITY);  
  440.         parameterString.append(",");  
  441.         parameterString.append(CameraParameters::FOCUS_MODE_MACRO);  
  442.     }  
  443.   
  444.     params.set(CameraParameters::KEY_SUPPORTED_FOCUS_MODES, parameterString.string());  
  445.   
  446.     /*mirror and flip query*/  
  447.     struct v4l2_queryctrl mirror,flip;  
  448.       
  449.     mirror.id = V4L2_CID_HFLIP;  
  450.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &mirror)) {  
  451.         mDriverMirrorSupport = true;  
  452.     } else {  
  453.         mDriverMirrorSupport = false;  
  454.     }  
  455.   
  456.     flip.id = V4L2_CID_VFLIP;  
  457.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &flip)) {  
  458.         mDriverFlipSupport = true;  
  459.     } else {  
  460.         mDriverFlipSupport = false;  
  461.     }  
  462.   
  463.   
  464.     /*Exposure setting*/  
  465.     struct v4l2_queryctrl exposure;  
  466.     char str_exposure[16];  
  467.     exposure.id = V4L2_CID_EXPOSURE;  
  468.     if (!ioctl(iCamFd, VIDIOC_QUERYCTRL, &exposure)) {  
  469.         sprintf(str_exposure,"%d",exposure.default_value);  
  470.         params.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, str_exposure);  
  471.         sprintf(str_exposure,"%d",exposure.maximum);          
  472.         params.set(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, str_exposure);  
  473.         sprintf(str_exposure,"%d",exposure.minimum);          
  474.         params.set(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, str_exposure);  
  475.         sprintf(str_exposure,"%d",exposure.step);   
  476.         params.set(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, str_exposure);  
  477.     } else {  
  478.         params.set(CameraParameters::KEY_EXPOSURE_COMPENSATION, "0");  
  479.         params.set(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION, "0");  
  480.         params.set(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION, "0");  
  481.         params.set(CameraParameters::KEY_EXPOSURE_COMPENSATION_STEP, "0.000001f");  
  482.     }  
  483.     /*rotation setting*/  
  484.     params.set(CameraParameters::KEY_ROTATION, "0");  
  485.   
  486.     /*[email protected] :add some settings to pass cts*/      
  487.     /*focus distance setting ,no much meaning ,only for passing cts */  
  488.     parameterString = "0.3,50,Infinity";  
  489.     params.set(CameraParameters::KEY_FOCUS_DISTANCES, parameterString.string());  
  490.     /*focus length setting ,no much meaning ,only for passing cts */  
  491.     parameterString = "35";  
  492.     params.set(CameraParameters::KEY_FOCAL_LENGTH, parameterString.string());  
  493.    /*horizontal angle of view setting ,no much meaning ,only for passing cts */  
  494.     parameterString = "100";  
  495.     params.set(CameraParameters::KEY_HORIZONTAL_VIEW_ANGLE, parameterString.string());  
  496.     /*vertical angle of view setting ,no much meaning ,only for passing cts */  
  497.     parameterString = "100";  
  498.     params.set(CameraParameters::KEY_VERTICAL_VIEW_ANGLE, parameterString.string());  
  499.   
  500.    /*quality of the EXIF thumbnail in Jpeg picture setting */  
  501.     parameterString = "50";  
  502.     params.set(CameraParameters::KEY_JPEG_THUMBNAIL_QUALITY, parameterString.string());  
  503.    /*supported size of the EXIF thumbnail in Jpeg picture setting */  
  504.     parameterString = "0x0,160x128";  
  505.     params.set(CameraParameters::KEY_SUPPORTED_JPEG_THUMBNAIL_SIZES, parameterString.string());  
  506.     parameterString = "160";  
  507.     params.set(CameraParameters::KEY_JPEG_THUMBNAIL_WIDTH, parameterString.string());  
  508.     parameterString = "128";  
  509.     params.set(CameraParameters::KEY_JPEG_THUMBNAIL_HEIGHT, parameterString.string());   
  510.     /* [email protected]: for cts ,KEY_MAX_NUM_DETECTED_FACES_HW should not be 0 */  
  511.     params.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW, "0");  
  512.     params.set(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_SW, "0");  
  513.     params.set(CameraParameters::KEY_RECORDING_HINT,"false");  
  514.     params.set(CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED,"false");  
  515.     params.set(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED,"true");  
  516.     params.set(CameraParameters::KEY_MAX_NUM_METERING_AREAS,"0");  
  517.   
  518.     LOGD ("Support Preview format: %s ",params.get(CameraParameters::KEY_SUPPORTED_PREVIEW_FORMATS));  
  519.     LOGD ("Support Preview sizes: %s ",params.get(CameraParameters::KEY_SUPPORTED_PREVIEW_SIZES));  
  520.     LOGD ("Support Preview FPS range: %s",params.get(CameraParameters::KEY_SUPPORTED_PREVIEW_FPS_RANGE));  
  521.     LOGD ("Support Preview framerate: %s",params.get(CameraParameters::KEY_SUPPORTED_PREVIEW_FRAME_RATES));   
  522.     LOGD ("Support Picture sizes: %s ",params.get(CameraParameters::KEY_SUPPORTED_PICTURE_SIZES));  
  523.     if (params.get(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE))  
  524.         LOGD ("Support white balance: %s",params.get(CameraParameters::KEY_SUPPORTED_WHITE_BALANCE));  
  525.     if (params.get(CameraParameters::KEY_SUPPORTED_EFFECTS))  
  526.         LOGD ("Support color effect: %s",params.get(CameraParameters::KEY_SUPPORTED_EFFECTS));  
  527.     if (params.get(CameraParameters::KEY_SUPPORTED_SCENE_MODES))  
  528.         LOGD ("Support scene: %s",params.get(CameraParameters::KEY_SUPPORTED_SCENE_MODES));  
  529.     if (params.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES))  
  530.         LOGD ("Support flash: %s",params.get(CameraParameters::KEY_SUPPORTED_FLASH_MODES));  
  531.     LOGD ("Support focus: %s",params.get(CameraParameters::KEY_SUPPORTED_FOCUS_MODES));  
  532.     LOGD ("Support zoom: %s(ratios: %s)",params.get(CameraParameters::KEY_ZOOM_SUPPORTED),  
  533.         params.get(CameraParameters::KEY_ZOOM_RATIOS));  
  534.     if (strcmp("0", params.get(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION))  
  535.         || strcmp("0", params.get(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION))) {  
  536.         LOGD ("Support exposure: (%s -> %s)",params.get(CameraParameters::KEY_MIN_EXPOSURE_COMPENSATION),  
  537.             params.get(CameraParameters::KEY_MAX_EXPOSURE_COMPENSATION));  
  538.     }  
  539.     LOGD ("Support hardware faces detecte: %s",params.get(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_HW));  
  540.     LOGD ("Support software faces detecte: %s",params.get(CameraParameters::KEY_MAX_NUM_DETECTED_FACES_SW));  
  541.     LOGD ("Support video stabilization: %s",params.get(CameraParameters::KEY_VIDEO_STABILIZATION_SUPPORTED));  
  542.     LOGD ("Support recording hint: %s",params.get(CameraParameters::KEY_RECORDING_HINT));  
  543.     LOGD ("Support video snapshot: %s",params.get(CameraParameters::KEY_VIDEO_SNAPSHOT_SUPPORTED));  
  544.     LOGD ("Support Mirror and Filp: %s",(mDriverMirrorSupport && mDriverFlipSupport)? "true":"false");  
  545.   
  546.     cameraConfig(params);  
  547.     LOG_FUNCTION_NAME_EXIT  
  548.   
  549. }  
然后剩下的大部分都是针对这个线程的运行实现以及对于CameraHal_Module.cpp中实现的为上层提供的接口的具体实现,比如:
[cpp] view plain copy
  1. int CameraHal::startPreview()  
  2. {  
  3.     LOG_FUNCTION_NAME  
  4.     Message msg;      
  5.     Mutex::Autolock lock(mLock);  
  6.       
  7.     if ((mPreviewThread != NULL) && (mCommandThread != NULL)) {  
  8.         msg.command = CMD_PREVIEW_START;  
  9.         msg.arg1 = (void*)CMDARG_NACK;  
  10.         commandThreadCommandQ.put(&msg);  
  11.     }  
  12.     mPreviewCmdReceived = true;  
  13.     LOG_FUNCTION_NAME_EXIT  
  14.     return NO_ERROR ;  
  15. }  
  16.   
  17. void CameraHal::stopPreview()  
  18. {  
  19.     LOG_FUNCTION_NAME      
  20.     Message msg;  
  21.     int ret = 0;  
  22.     Mutex::Autolock lock(mLock);  
  23.   
  24.     if ((mPreviewThread != NULL) && (mCommandThread != NULL)) {  
  25.         msg.command = CMD_PREVIEW_STOP;  
  26.         msg.arg1 = (void*)CMDARG_ACK;  
  27.         commandThreadCommandQ.put(&msg);  
  28.   
  29.         if (mANativeWindow == NULL) {  
  30.             mANativeWindowCond.signal();  
  31.             LOGD("%s(%d): wake up command thread for stop preview",__FUNCTION__,__LINE__);  
  32.         }  
  33.           
  34.         while (ret == 0) {              
  35.             ret = commandThreadAckQ.get(&msg);  
  36.             if (ret == 0) {  
  37.                 if (msg.command == CMD_PREVIEW_STOP) {                      
  38.                     ret = 1;  
  39.                 }  
  40.             }  
  41.         }  
  42.     } else {  
  43.         LOGE("%s(%d): cancel, because thread (%s %s) is NULL", __FUNCTION__,__LINE__,(mPreviewThread == NULL)?"mPreviewThread":" ",  
  44.             (mCommandThread == NULL)?"mCommandThread":" ");  
  45.     }  
  46.     mPreviewCmdReceived = false;  
  47.     LOG_FUNCTION_NAME_EXIT  
  48. }  
  49.   
  50. int CameraHal::autoFocus()  
  51. {  
  52.     LOG_FUNCTION_NAME  
  53.     int ret = 0;  
  54.     Message msg;  
  55.     Mutex::Autolock lock(mLock);  
  56.   
  57.     if ((mPreviewThread != NULL) && (mCommandThread != NULL)) {  
  58.         msg.command = CMD_AF_START;  
  59.         msg.arg1 = (void*)CMDARG_ACK;  
  60.         commandThreadCommandQ.put(&msg);  
  61.         while (ret == 0) {              
  62.             ret = commandThreadAckQ.get(&msg,5000);  
  63.             if (ret == 0) {  
  64.                 if (msg.command == CMD_AF_START) {                      
  65.                     ret = 1;  
  66.                 }  
  67.             } else {  
  68.                 LOGE("%s(%d): AutoFocus is time out!!!\n",__FUNCTION__,__LINE__);  
  69.             }  
  70.         }  
  71.     } else {  
  72.         LOGE("%s(%d):  cancel, because thread (%s %s) is NULL", __FUNCTION__,__LINE__,(mPreviewThread == NULL)?"mPreviewThread":" ",  
  73.             (mCommandThread == NULL)?"mCommandThread":" ");  
  74.     }  
  75.     LOG_FUNCTION_NAME_EXIT  
  76.     return NO_ERROR;  
  77. }  

[cpp] view plain copy
  1. <pre name="code" class="cpp"><pre name="code" class="cpp"><pre name="code" class="cpp"><pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7. <pre></pre>  
  8. <pre></pre>  
  9. <pre></pre>  
  10. <pre></pre>  
  11. <pre></pre>  
  12. <pre></pre>  
  13. <pre></pre>  
  14. <pre></pre>  
  15. <pre></pre>  
  16.   
  17. </pre></pre></pre> 

你可能感兴趣的:(Android Camera HAL浅析)