get all 0 data when capture video using v4l2 on android

纵饮孤独 提交于 2019-12-24 15:53:34

问题


i am trying to capture video on android using v4l2 under jni. i found some guide and followed the step:

fd = open("/dev/video0", O_RDWR);

/* init part */
ioctl(fd, VIDIOC_QUERYCAP, &caps);
ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc);
ioctl(fd, VIDIOC_S_FMT, &fmt);
ioctl(fd, VIDIOC_REQBUFS, &req);
ioctl(fd, VIDIOC_QUERYBUF, &buf);
ioctl(fd, VIDIOC_QBUF, &buf);

/* capture part */
FILE *fp = fopen("/sdcard/img.yuv", "wb");
for (i = 0; i < 20; i++)
{
    ioctl(fd, VIDIOC_DQBUF, &buf);
    fwrite(buffers[buf.index].start, 1, buf.bytesused, fp);
    ioctl(fd, VIDIOC_QBUF, &buf);
}
fclose(fp);

this is the main structure of my code. all the function run correctly and return 0. however, when i open the output file with binary viewer, i found that all the data is 0.

is there any problem with my code? i got confused because all the functions returned 0.

Thanks!!


回答1:


You are using an array called buffers[]. But I can't see where it's declared or what it stands for. If there is no code missing above, you will always get zeros cause you are writing buffer[] to the file and not the stuff you get from v4l2. Further more, the initial values of caps, fmtdesc, fmt, req and buf prior to the ioctl command would be interesting too. Depending on their inital values, you will have different communication interfaces. Issues could be hidden in these parts.

As you wrote in your question, all ioctl commands would return 0, there should be no error. If everything behaves as expected. Another way to check for issues is calling

perror("<your comment or hint to line above>");

after each ioctl command. This would print you more information about errors on your std-out. ( more details about perror can be found in this thread When should I use perror("...") and fprintf(stderr, "...")?)

Are you trying to get the images from the camera? (on some phones video0 you used above is the back cam) On some android devices the camera has to be started by complex procedure using other device drivers besides videoXY. And trying to get the images from video0 while the official camera app is running might be difficult. The official v4l2 api says:

V4L2 drivers should not support multiple applications reading or writing the same data stream on a device by copying buffers, time multiplexing or similar means. This is better handled by a proxy application in user space.

From: http://linuxtv.org/downloads/v4l-dvb-apis/common.html#idp18553208

Can you post more (detailed) code? I might be able to help, as I'm doing very similar stuff.

To be able to reproduce it, it would be very interesting with which android device you are working (type / model number / android version).



来源:https://stackoverflow.com/questions/19421958/get-all-0-data-when-capture-video-using-v4l2-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!