I recently needed a single webcam to be shared simultaneously by 3 applications (a web browser, a videoconferencing app, and ffmpeg to save the stream).
It's not possible to simply share the /dev/video* stream because as soon as one application is using it, the others cannot, and anything else will get a "device or resource busy" or equivalent.
So I turned to v4l2-loopback with the intention of mirroring the webcam to 3 loopbacks.
Using 3 loopbacks does work as expected, but what has really surprised me is it turns out I don't actually need 3 loopbacks, but only 1.
If I create a single loopback and feed it with ffmpeg, then the single mirrored loopback can be used by all 3 applications at the same time, with no "device or resource busy" issue.
So this is even better than I planned, and there is no practical problem I need help with.
But my question is, how is this possible with the loopback? And why not using the original source directly?
Example command to create the single loopback:
sudo modprobe v4l2loopback video_nr=30 exclusive_caps=1 card_label="loopback cam"
Example command using ffmpeg to mirror /dev/video5 to the loopback (/dev/video30). This will default to raw, but recent builds of ffmpeg can use an alternative stream like MJPEG, the behaviour is the same regardless:
ffmpeg -f v4l2 -i /dev/video5 -codec copy -f v4l2 /dev/video30
After doing this, try to access /dev/video30 with multiple applications, here are some examples:
ffmpeg -f v4l2 -i /dev/video30 -codec libx264 recordstream.mp4
ffplay -f video4linux2 -i /dev/video30
System info in case it's relevant:
- Ubuntu 20.04
- Kernel: 5.4.0-31-generic
- package: v4l2loopback-dkms 0.12.3-1