RaspberryPi HLS streaming with nginx and ffmpeg; v4l2 error: ioctl(VIDIOC_STREAMON): Protocol error

2.1k views Asked by At

I'm trying to realize a baby monitoring with a Raspberry Pi (Model 4B, 4GB RAM) and an ordinary Webcam (with integrated Mic). I followed this Tutorial: https://github.com/DeTeam/webcam-stream/blob/master/Tutorial.md

Shortly described:

  1. I installed and configured an nginx server with rtmp module enabled.
  2. I installed ffmpeg with this configuration --enable-gpl --enable-nonfree --enable-mmal --enable-omx-rpi
  3. I tried to stream ;)

The configuration of nginx seems to be working (sometimes streaming works, the server starts without any complication and when the server is up and running, the webpage is displayed). The configuration of ffmpeg seems to be fine as well, since streaming sometimes works...

I was trying a couple of different ffmpeg-commands; all of them are sometimes working and sometimes resulting in an error. The command looks like following:

ffmpeg -re
-f v4l2
-i /dev/video0
-f alsa
-ac 1
-thread_queue_size 4096
-i hw:CARD=Camera,DEV=0
-profile:v high
-level:v 4.1
-vcodec h264_omx
-r 10
-b:v 512k
-s 640x360
-acodec aac
-strict
-2
-ac 2
-ab 32k
-ar 44100
-f flv
rtmp://localhost/show/stream;

Note: I rearranged the code to make it easier to read. In the terminal, it is all in one line. Note: There is no difference when using -f video4linux2 instead of -f v4l2

The camera is recognized by the system:

pi@raspberrypi:~ $ v4l2-ctl --list-devices
bcm2835-codec-decode (platform:bcm2835-codec):
    /dev/video10
    /dev/video11
    /dev/video12

bcm2835-isp (platform:bcm2835-isp):
    /dev/video13
    /dev/video14
    /dev/video15
    /dev/video16

HD Web Camera: HD Web Camera (usb-0000:01:00.0-1.2):
    /dev/video0
    /dev/video1

When only using -i /dev/video0, audio transmission never worked. The output of arecord -L was:

pi@raspberrypi:~ $ arecord -L
default
    Playback/recording through the PulseAudio sound server
null
    Discard all samples (playback) or generate zero samples (capture)
jack
    JACK Audio Connection Kit
pulse
    PulseAudio Sound Server
usbstream:CARD=Headphones
    bcm2835 Headphones
    USB Stream Output
sysdefault:CARD=Camera
    HD Web Camera, USB Audio
    Default Audio Device
front:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    Front speakers
surround21:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    Direct sample mixing device
dsnoop:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    Direct sample snooping device
hw:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    Direct hardware device without any conversions
plughw:CARD=Camera,DEV=0
    HD Web Camera, USB Audio
    Hardware device with all software conversions
usbstream:CARD=Camera
    HD Web Camera
    USB Stream Output

that's why i added -i hw:CARD=Camera,DEV=0.

As mentioned above, it worked very well a couple of times with this configuration and commands. But very often, i get the following error message when starting to stream:

pi@raspberrypi:~ $ ffmpeg -re -f video4linux2 -i /dev/video0 -f alsa -ac 1 -thread_queue_size 4096 -i hw:CARD=Camera,DEV=0 -profile:v high -level:v 4.1 -vcodec h264_omx -r 10 -b:v 512k -s 640x360 -acodec aac -strict -2 -ac 2 -ab 32k -ar 44100 -f flv rtmp://localhost/show/stream
ffmpeg version N-100673-g553eb07737 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 8 (Raspbian 8.3.0-6+rpi1)
  configuration: --enable-gpl --enable-nonfree --enable-mmal --enable-omx-rpi --extra-ldflags=-latomic
  libavutil      56. 63.101 / 56. 63.101
  libavcodec     58.117.101 / 58.117.101
  libavformat    58. 65.101 / 58. 65.101
  libavdevice    58. 11.103 / 58. 11.103
  libavfilter     7. 96.100 /  7. 96.100
  libswscale      5.  8.100 /  5.  8.100
  libswresample   3.  8.100 /  3.  8.100
  libpostproc    55.  8.100 / 55.  8.100
[video4linux2,v4l2 @ 0x2ea4600] ioctl(VIDIOC_STREAMON): Protocol error
/dev/video0: Protocol error

And when I'm swithing to /dev/video1 (since this was also an output for v4l2-ctl --list-devices), I get the following error message:

pi@raspberrypi:~ $ ffmpeg -re -f v4l2 -i /dev/video1 -f alsa -ac 1 -thread_queue_size 4096 -i hw:CARD=Camera,DEV=0 -profile:v high -level:v 4.1 -vcodec h264_omx -r 10 -b:v 512k -s 640x360 -acodec aac -strict -2 -ac 2 -ab 32k -ar 44100 -f flv rtmp://localhost/show/stream
ffmpeg version N-100673-g553eb07737 Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 8 (Raspbian 8.3.0-6+rpi1)
  configuration: --enable-gpl --enable-nonfree --enable-mmal --enable-omx-rpi --extra-ldflags=-latomic
  libavutil      56. 63.101 / 56. 63.101
  libavcodec     58.117.101 / 58.117.101
  libavformat    58. 65.101 / 58. 65.101
  libavdevice    58. 11.103 / 58. 11.103
  libavfilter     7. 96.100 /  7. 96.100
  libswscale      5.  8.100 /  5.  8.100
  libswresample   3.  8.100 /  3.  8.100
  libpostproc    55.  8.100 / 55.  8.100
[video4linux2,v4l2 @ 0x1aa4610] ioctl(VIDIOC_G_INPUT): Inappropriate ioctl for device
/dev/video1: Inappropriate ioctl for device

When using the video0 input, the webcam's LED that recognizes an access is constantly on. When using video1not.

After hours and days of googling and tears and whiskey, for the sake of my liver, my marriage and my physical and mental health, I'm very sincerly asking for your help... What the f**k is happening and what can I do to make it work???

Thanks everybody :)

UPDATE 1:

  1. using the full path to ffmpeg does not change anything...
  2. /dev/video0 and /dev/video1 have access rights for everybody
  3. sudo ffmpeg ... does not change anything as well
  4. the problem seems to be at an "early stage". Stripping the command down to ffmpeg -i /dev/video0 results in the same problem

UPDATE 2:
It seems that everything is working when I first start another Application that needs access to the webcam and then ffmpeg... Might be some driver issue, but when I'm looking for loaded modules with lsmod, there is absolutely no change before and after I started the application... Any help still appreciated...

UPDATE 3:
I was checking the output of dmesg.
When I started the first application I received this message:
uvcvideo: Failed to query (GET_DEF) UVC control 12 on unit 2: -32 (exp. 4).
And when I started ffmpeg, nothing happend but everything worked...

1

There are 1 answers

1
AureliuS On

I got the same issue, noticed your UPDATE 2 and did the following (python):

for i in range(3):
    cam1 = cv2.VideoCapture(0)
    ret, frame = cam1.read()
    cam1.release()
cam1 = cv2.VideoCapture(0)

Now I still get the error message (only once), but the camera works fine.

No idea why or how, but... this is the way (apparently)