我有运行Android 4.2的OMAP5432 EVM,带有4个USB连接的罗技C270相机。我使用NDK C代码中的V4L2驱动程序在MJPEG模式下打开并从相机流式传输。一切都很好,除了电动自行车后的相机。
在电源循环后,相机中的两个或有时三个正确出现,但一个开始在缓冲器的开头吐出缺少MJPEG SOI 0xFF、0xD8的帧缓冲器,而EOI 0xFF和0xD9存在。
使用posix close()关闭并重新打开有问题的摄像机文件/dev/videoX可以永久修复问题,直到下一次电源循环。
连接单个摄像头时永远不会发生这种情况,只有其中的3或4个。
我试过更换USB集线器,直接连接摄像头,但都无济于事。
struct v4l2_format fm = {0};
fm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
int r = xioctl(c->fd, VIDIOC_G_FMT, &fm);
if (r != 0) {
printf("VIDIOC_G_FMT %s r=%d error %d, %s", c->name, r, errno, strerror(errno));
return -1;
}
fm.fmt.pix.width = c->width; // 640
fm.fmt.pix.height = c->height; // 480
fm.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
fm.fmt.pix.field = V4L2_FIELD_ANY;
fm.fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
if (c->fm != V4L2_PIX_FMT_MJPEG) {
unsigned int min = fm.fmt.pix.width * 2;
if (fm.fmt.pix.bytesperline < min) {
fm.fmt.pix.bytesperline = min;
}
min = fm.fmt.pix.bytesperline * fm.fmt.pix.height;
if (fm.fmt.pix.sizeimage < min) {
fm.fmt.pix.sizeimage = min;
}
} else {
fm.fmt.pix.bytesperline = 0;
fm.fmt.pix.sizeimage = 0;
}
r = xioctl(c->fd, VIDIOC_S_FMT, &fm);
然后是
static int init_mmap(camera_t_* c) {
struct v4l2_requestbuffers rq = {0};
rq.count = BUFFERS;
rq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
rq.memory = V4L2_MEMORY_MMAP;
if (xioctl(c->fd, VIDIOC_REQBUFS, &rq) != 0) {
if (EINVAL == errno) {
printf("%s does not support memory mapping", c->name);
return -1;
} else {
printf("VIDIOC_REQBUFS error %d, %s", errno, strerror(errno));
return -1;
}
}
if (rq.count < 2) {
printf("Insufficient buffer memory on %s", c->name);
return -1;
}
c->buffers = malloc(rq.count * sizeof(buffer_t));
if (c->buffers == null) {
printf("out of memory");
return -1;
}
c->n_buffers = rq.count;
for (int i = 0; i < rq.count; i++) {
struct v4l2_buffer b = {0};
b.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
b.memory = V4L2_MEMORY_MMAP;
b.index = i;
if (xioctl(c->fd, VIDIOC_QUERYBUF, &b) != 0) {
printf("VIDIOC_QUERYBUF error %d, %s", errno, strerror(errno));
return -1;
}
c->buffers[i].length = b.length;
c->buffers[i].start = mmap(null, b.length, PROT_READ | PROT_WRITE, MAP_SHARED, c->fd, b.m.offset);
if (c->buffers[i].start == MAP_FAILED) {
printf("mmap error %d, %s", errno, strerror(errno));
return -1;
}
}
return 0;
}
我得到的数据是这样的:
missing FFD8 tag 0x688fb000 bytes=6946 length=213333
0x688fb000: AB 74 10 0C EE FD 34 FB B3 FA 3E D1 60 9D 33 B0 ?t????4???>?`?3?
0x688fb010: F1 83 7B 8A AF B8 F9 BF EF 33 EE FF 89 91 7F 9F ??{??????3?????
0x688fb020: 47 EF A4 B9 73 1C BC FB AD 46 6A D5 22 A2 2C 32 G???s????Fj?"?,2
0x688fb030: 2B A8 71 7E 56 56 73 23 15 7B 11 B2 F0 FA AA D3 +?q~VVs#?{??????
.............
0x688fcae2: 00 A2 80 0A 28 03 FF D5 C6 A2 80 0A 28 00 A2 80 ????(???????(???
0x688fcaf2: 0A 28 00 A2 80 0A 28 00 A2 80 0A 28 00 A2 80 0A ?(????(????(????
0x688fcb02: 28 03 FF D6 C6 A2 80 0A 28 00 A2 80 0A 28 00 A2 (???????(????(??
0x688fcb12: 80 0A 28 00 A2 80 0A 28 00 A2 80 0A 28 03 FF D9 ??(????(????(???
并且对于每个出队列的缓冲区都是相同的数据。。。
是C270或V4L2出了问题,还是我的代码出了问题?有人经历过类似的问题吗?
好吧,看起来我已经找到了问题的根源,使用查看内核日志
adb shell sudo echo 8 > /proc/sys/kernel/printk
adb >/tmp/adb_klog.txt 2>/tmp/adb_klog.txt shell sudo cat /proc/kmsg
命令。看起来VIDIOC_G_FMT在4个摄像头中有1个或2个出现故障,但没有向用户报告错误land ioctl()调用:
<7>[ 7274.495178] uvcvideo: uvc_v4l2_open
<7>[ 7274.495239] ehci-omap ehci-omap.0: reused qh ca5bf1c0 schedule
<7>[ 7274.495269] usb 1-2.2.1.2: link qh16-0001/ca5bf1c0 start 1 [1/0 us]
<7>[ 7274.495330] uvcvideo: uvc_v4l2_ioctl(VIDIOC_G_FMT)
<7>[ 7274.495452] uvcvideo: uvc_v4l2_ioctl(VIDIOC_S_FMT)
<7>[ 7274.495483] uvcvideo: Trying format 0x47504a4d (MJPG): 640x480.
<7>[ 7274.495513] uvcvideo: Using default frame interval 33333.3 us (30.0 fps).
<7>[ 7279.495208] usb 1-2.2.1.2: .<process_name> timed out on ep0out len=0/26
<3>[ 7279.495239] uvcvideo: Failed to set UVC probe control : -110 (exp. 26).
<7>[ 7279.502746] uvcvideo: uvc_v4l2_ioctl(VIDIOC_G_FMT)
<7>[ 7279.507080] uvcvideo: uvc_v4l2_ioctl(VIDIOC_LOG_STATUS)
<7>[ 7279.507080] uvcvideo: Unknown ioctl 0x00005646
我想出的最简单的解决方法是在打开相机后开始流式传输,等待(超时)直到帧开始流动,如果损坏的帧出现,请关闭并重新打开相机。
在我的代码中,它看起来像这样:
static bool has_bad_frames(camera_t* camera) {
/* Logitech C270 randomly spits corrupted MJPEGs after power cycle. Known workaround is to reopen camera */
camera_t_* c = (camera_t_*)camera;
camera_set_callback(c, empty_callback); // otherwise MJPEG frames are not going to be decompressed
camera_start(camera);
int retry = 300; // 3 seconds max
while (retry > 0 && c->good_frames == 0 && c->bad_frames == 0) {
nsleep(NANOSECONDS_IN_SECOND / 100); /* 1/100 second */
retry--;
}
bool ok = c->bad_frames == 0 && c->good_frames > 0;
camera_stop(camera);
return !ok;
}
int camera_open(void* that, camera_t* o, int id, int w, int h, int bpp) {
int r = try_to_open(that, o, id, w, h, bpp);
if (r == 0) {
if (has_bad_frames(*o)) {
camera_t_* c = (camera_t_*)*o;
if (c->bad_frames + c->good_frames == 0) {
trace("%s is not streaming; retrying...", c->name);
} else {
trace("%s spits corrupted frames. Probable VIDIOC_S_FMT silently failed; retrying...", c->name);
}
camera_close(*o);
*o = null;
return try_to_open(that, o, id, w, h, bpp);
}
}
return r;
}