DSP

Android Camera HAL浅析

2019-07-13 20:24发布

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硬件。       --------------------------------------------------------------------------------------------   -----------------------------------------------------------------------------------------------       3.Camera HAL层部分 
代码存放目录:hardware k29camera
编译:   [cpp] view plaincopy
  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 plaincopy
  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 plaincopy
  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. ",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. ",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
  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 plaincopy
  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 plaincopy
  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
  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 plaincopy
  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 ", 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.         /* ddl@rock-chips.com: 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.