Opened 9 years ago

Closed 9 years ago

#1560 closed defect (fixed)

v4l2 device won't open after opening/closing it 16 times

Reported by: Tristan Matthews Owned by:
Priority: normal Component: avdevice
Version: git-master Keywords: v4l2
Cc: Blocked By:
Blocking: Reproduced by developer: yes
Analyzed by developer: no

Description (last modified by Nicolas George)

Summary of the bug:

In the attached case, I open and close a v4l2 device (/dev/video0) 17 times. On the 17th time, it fails to open with the following errors:

libv4l2: error attempting to open more then 16 video devices
[video4linux2,v4l2 @ 0x1f6a5c0] ioctl(VIDIOC_QUERYCAP): Bad file descriptor
Could not open input "/dev/video0"

How to reproduce:
Build and run the attached test case with a v4l2 device connected.

I'm using the ffmpeg git master branch:

ffmpeg version N-42636-ga8d8e86 Copyright (c) 2000-2012 the FFmpeg developers
  built on Jul 19 2012 17:32:59 with gcc 4.7.0 (GCC) 20120507 (Red Hat 4.7.0-5)
  configuration: --prefix=/opt/ --enable-bzlib --enable-gnutls --enable-libv4l2 --enable-libx264 --enable-x11grab --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --enable-pic

Attachments (2)

v4l2_testcase.c (1.0 KB ) - added by Tristan Matthews 9 years ago.
compile with "cc v4l2_testcase.c -o v4l2_testcase pkg-config --cflags --libs libavdevice libavformat"
v4l2.patch (824 bytes ) - added by Tristan Matthews 9 years ago.
fix error handling for v4l2

Download all attachments as: .zip

Change History (9)

by Tristan Matthews, 9 years ago

Attachment: v4l2_testcase.c added

compile with "cc v4l2_testcase.c -o v4l2_testcase pkg-config --cflags --libs libavdevice libavformat"

comment:1 by Tristan Matthews, 9 years ago

I've also realized that this bug is only present when the --enable-libv4l2 option is given to configure.

comment:2 by Nicolas George, 9 years ago

Description: modified (diff)
Reproduced by developer: set

Same thing on one of my boxes. libavdevice compiled without libv4l2 works.
Try removing the paragraph under #ifdef CONFIG_LIBV4L2 in device_open() in libavdevice/v4l2.c and see if it fixes things.

comment:3 by Tristan Matthews, 9 years ago

Yup that did it, I'm not sure how the code should be adjusted though, should we only be calling v4l2_fd_open if fd is < 0? If so, I've attached a patch.

comment:4 by Tristan Matthews, 9 years ago

Earlier patch was no good, I've replaced it with this patch which just fixes the error handling. The code currently in master is off, fd would never be < 0 since the function would have already returned.

The original problem still stands. From the documentation it seems that v4l2_fd_open takes an already open file descriptor and tries to make it open for further use by libv4l2, but I can't see why it's not being closed properly.

by Tristan Matthews, 9 years ago

Attachment: v4l2.patch added

fix error handling for v4l2

comment:5 by Nicolas George, 9 years ago

Your patch would clearly be right by itself, but the whole paragraph is just redundant:
http://git.linuxtv.org/v4l-utils.git/blob/HEAD:/lib/libv4l2/libv4l2.c#l578
v4l2_open already calls v4l2_fd_open, calling a second time only uses an additional slot in libv4l2 structures that will never be deallocated.
I sent a patch to the devel mailing list.

in reply to:  5 comment:6 by Tristan Matthews, 9 years ago

Replying to Cigaes:

Your patch would clearly be right by itself, but the whole paragraph is just redundant:
http://git.linuxtv.org/v4l-utils.git/blob/HEAD:/lib/libv4l2/libv4l2.c#l578
v4l2_open already calls v4l2_fd_open, calling a second time only uses an additional slot in libv4l2 structures that will never be deallocated.
I sent a patch to the devel mailing list.

Well spotted, it wasn't until I went through with gdb that I noticed v4l2_fd_open was getting called twice. Thanks for sending the patch.

comment:7 by Nicolas George, 9 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.