0

I've just started using v4l2 (for Python 3) on Ubuntu 16.04.

I'm trying to run the sample from the v4l2 Python documentation, namely:

$ cat demo4SO.py
#!/usr/bin/env python3

import v4l2
import fcntl

vd = open('/dev/video0', 'rw')
#vd = open('/dev/video0', 'r')
cp = v4l2.v4l2_capability()
fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp)

print("driver:",cp.driver)
print("card:",cp.card)

But am getting:

$ python3 demo4SO.py
Traceback (most recent call last):
  File "demo4SO.py", line 6, in <module>
    vd = open('/dev/video0', 'rw')
ValueError: must have exactly one of create/read/write/append mode

I get the same error even when running with sudo.

Trying to open with 'w+' (or 'r+'), I get:

$ python3 demo4SO.py
Traceback (most recent call last):
  File "demo4SO.py", line 8, in <module>
    vd = open('/dev/video0', "w+")
io.UnsupportedOperation: File or stream is not seekable.

If I change 'rw' (or 'w+' or 'r+') to 'r', the code works as expected.

$ cat demo4SO.py
#!/usr/bin/env python3

import v4l2
import fcntl

#vd = open('/dev/video0', 'rw')
vd = open('/dev/video0', 'r')
cp = v4l2.v4l2_capability()
fcntl.ioctl(vd, v4l2.VIDIOC_QUERYCAP, cp)

print("driver:",cp.driver)
print("card:",cp.card)


$ python3 demo4SO.py
driver: b'uvcvideo'
card: b'Intel(R) RealSense(TM) 410'

My username belongs to the group of the video device, so the problem does not seem to be related to permissions:

$ groups
openstack root sudo video staff
$ ls -ls /dev/video0
0 crw-rw----+ 1 root video 81, 0 פבר 14 11:31 /dev/video0

Any idea why I cannot run the documentation's sample code as is (with 'rw')?

boardrider
  • 5,882
  • 7
  • 49
  • 86

1 Answers1

1

Changing

vd = open('/dev/video0', 'rw')

to

vd = open('/dev/video0', 'rb+', buffering=0)

enables opening the file with O_RDWR permissions.

boardrider
  • 5,882
  • 7
  • 49
  • 86