在android车机上调试USB Camera录像拍照功能
USB Camera在上一篇文章中已经跑起来了,上次的情况是camera预览可以,拍照、录像不行。除了强制设置视频设备设备文件为/dev/video2,也就是这个usb
camera插入系统后自动分配的设备文件,其他没有修改。一拍照或者录像,程序就死了,错误打印如下:
/*****************************************************************************************************/
声明:本博内容均由http://blog.csdn.net/sundesheng125原创,转载请注明出处,谢谢!
/*****************************************************************************************************/
01-01 09:33:59.490 V/CAM_FocusManager( 2990): Start autofocus. 01-01 09:33:59.580 V/CAM_PhotoModule( 2990): mAutoFocusTime = 87ms 01-01 09:33:59.650 V/CAM_PhotoModule( 2990): onShutterButtonClic[ 263.837941] uvcvideo: Trying format 0x56595559 (YUYV): 960x720. [ 263.837964] uvcvideo: Using default frame interval 66666.6 us (15.0 fps). k: mCameraState=1 01-01 09:33:59.650 D/V4L2CameraDevice( 1077): in V4L2Camera::TryFmtSize: w: 960, h: 720 01-01 09:33:59.700 D/V4L2CameraDevice( 1077): out V4L2Camera::TryFmtSize: w: 960, h: 720 [ 263.897491] uvcvideo: Trying format 0x56595559 (YUYV): 640x480. 01-01 09:33:59.710 D/V4L2CameraDevice( 1077): in V4L2Camera::Try[ 263.904745] uvcvideo: Using default frame interval 33333.3 us (30.0 fps). FmtSize: w: 640, h: 480 01-01 09:33:59.750 D/V4L2CameraDevice( 1077): out V4L2Camera::TryFmtSize: w: 640, h: 480 [ 264.008038] Binder_2 used greatest stack depth: 5032 bytes left 01-01 09:33:59.870 D/CameraHardware( 1077): Starting camera for [ 264.056815] uvcvideo: Setting frame interval to 1/30 (333333). picture: NV12(jpeg)[640x480] 01-01 09:33:59.870 D/V4L2CameraDevice( 1077): startDevice, wxh: 640x480, fmt: 0x3231564e [ 264.101826] fuction=uvc_v4l2_do_ioctl, line=793 01-01 09:33:59.910 D/V4L2CameraDevice( 1077): v4l2SetVideoParams[ 264.106430] fuction=uvc_v4l2_set_format, line=300 , line: 903, w: 640, h: 480, pfmt: 0x3231564e 01-01 09:33:59.91[ 264.116702] fuction=uvc_v4l2_set_format, line=304 0 D/V4L2CameraDevice( 1077): v4l2SetVideoParams, line: 919, mCap[ 264.126906] uvcvideo: Trying format 0x56595559 (YUYV): 640x480. tureFormat: 0x56595559, format.fmt.pix.pixelformat: 0X56595559, [ 264.138377] uvcvideo: Using default frame interval 33333.3 us (30.0 fps). V4L2_PIX_FMT_YUYV: 0x56595559 [ 264.173788] uvcvideo: Setting frame interval to 1/15 (666666). 01-01 09:33:59.990 E/V4L2CameraDevice( 1077): VIDIOC_S_FMT Failed: Device or resource busy [ 264.227065] uvcvideo: uvc_v4l2_mmap 01-01 09:34:00.040 E/sunxi_alloc( 1077): can not alloc size 0 [ 264.247304] uvcvideo: Device requested 3072 B/frame bandwidth. [ 264.247322] uvcvideo: Selecting alternate setting 6 (3072 B/frame bandwidth). [ 264.251846] uvcvideo: Allocated 5 URB buffers of 32x3072 bytes each. 01-01 09:34:00.080 W/V4L2CameraDevice( 1077): preview thread dose not started 01-01 09:34:00.170 W/V4L2CameraDevice( 1077): preview thread dose not started 01-01 09:34:00.270 W/V4L2CameraDevice( 1077): preview thread dose not started 01-01 09:34:00.380 W/V4L2CameraDevice( 1077): preview thread dose not started 01-01 09:34:00.480 W/V4L2CameraDevice( 1077): preview thread dose not started 01-01 09:34:00.580 W/V4L2CameraDevice( 1077): preview thread dose not started
跟了一下uvc的driver,头很大,代码一大堆,千丝万缕中找到错误出在uvc_v4l2_set_format函数里。又加了一些打印发现错误出在:
if (uvc_queue_allocated(&stream->queue)) { printk("fuction=%s, line=%d\n",__FUNCTION__, __LINE__); ret = -EBUSY; goto done; }
看了一下uvc_queue_allocated这个接口,发现里面是判断这个queue的buffer size是否大于0,大于0的话就说明已经分配过了,调用uvc_queue_allocated这个接口就会返回true,从而出错出去,会造成camera
hal层接着出错。
因为在android camera apk应用中一进去就是preview的状态,切到录像或者拍照要另外执行动作,整个处理会有变化,然而可能是因为UVC的设备打开后操作产生的stream的queue没有释放,造成在重现uvc_v4l2_set_format的时候,就会在中间出错跳出,所以笔者稍作了一下改动,在调用uvc_queue_allocated返回出错的地方屏蔽掉,也就是你有这个queue,我就继续用你,理论上应该是可行。重新编译一下,烧进去,一跑,果然可以拍照跟录像了。但是拍的照片有一点小问题,就是在照片中莫名的多了一些短的黑线,预览的时候没有这些短线,笔者还不知道是什么原因。另外笔者还需要研究的是多个usb
camera在android中看看是否能同时工作,预计这里面有很多工作,还是继续朝着这个方向前进。下面贴修改的代码以及排的一张照片:
static int uvc_v4l2_set_format(struct uvc_streaming *stream, struct v4l2_format *fmt) { struct uvc_streaming_control probe; struct uvc_format *format; struct uvc_frame *frame; int ret; printk("fuction=%s, line=%d\n",__FUNCTION__, __LINE__); if (fmt->type != stream->type) return -EINVAL; printk("fuction=%s, line=%d\n",__FUNCTION__, __LINE__); ret = uvc_v4l2_try_format(stream, fmt, &probe, &format, &frame); if (ret < 0) return ret; printk("fuction=%s, line=%d\n",__FUNCTION__, __LINE__); mutex_lock(&stream->mutex); if (uvc_queue_allocated(&stream->queue)) { printk("fuction=%s, line=%d\n",__FUNCTION__, __LINE__); // ret = -EBUSY; // goto done; } memcpy(&stream->ctrl, &probe, sizeof probe); stream->cur_format = format; stream->cur_frame = frame; printk("fuction=%s, line=%d\n",__FUNCTION__, __LINE__); done: mutex_unlock(&stream->mutex); return ret; }