v4l2loopback devices for preproduction of webcam streams

Privacy and Home Office

As the Covid19 pandemia leads to a certain need of adaption I had to think about managing my consultant work from my home office. After finding a way to use an unused smartphone as a high level webcam with my PC I found myself exposed to another problem. That wide screen, high resolution cam also meant that my consultees would receive a very exclusive insight in my privacy.

HowTo cut out and make use of a Green Screen

So I thought about buying some printed gradient cardboard background and started my searx instance. And stumbled upon an offer introducing the Elgato Green Screen. You know - green fabric as an background so you can easily cut out a person and let it appear on any other picture behind it. Like you've seen thousands of times in movies, news shows or - of course - YouTube channels!

So I ordered the green screen (which is unfortunately yet to come) and bothered about which software might allow me to preproduce a video stream like this and loop it back to my conferencing services like e.g. browsers' WebRTC. On Linux. And finally came to the following answers:

HowTo edit a video stream

Of course! OBS. The OpenBroadcasterSoftware project is the default solution for any amateur video streamer. And even most famous YouTubers do their work predominantly with that great piece of software. It comes with filters like 'chroma key' for replacing any coloured screen with another picture. And to cut out a smaller fraction of the stream so that not your whole living room goes to public but only your face and some margin around. Not to speak of any combination of multiple camera streams, clips, pictures and whatever visual source one could think of. With a quite handy user interface if you'd ask me. But as I didn't want to record a Let's Play and neither live stream it on twitch ...

HowTo send the processed stream back to my Video Chat application?

As I want to communicate with others via Matrix, JitsiMeet, Nextcloud Talk or any other WebRTC solution I needed a way to send the altered stream back to a /dev/video[n] file on my Linux PC. Fortunately I was obviously not the first to have that idea an a quick search took me to loopback device plugin for OBS. Even better: Someone decided to produce a plugin even for linux! After some fiddling around with that solution I learned:

  • The v4l2loopback .deb package at the github-repo doesn't seem to work with the v4l2loopback sink for OBS.

  • You should compile kernel module and plugin from the current git. I finally found the solution which works well for me:

    sudo apt install obs-studio libobs-dev libobs0
    git clone https://github.com/umlaeute/v4l2loopback.git
    make && sudo make install
    sudo depmod -a
    sudo modprobe v4l2loopback devices=1 video_nr=10 card_label="OBS Cam" exclusive_caps=1
    git clone --recursive https://github.com/obsproject/obs-studio.git
    git clone https://github.com/CatxFish/obs-v4l2sink.git
    cd obs-v4l2sink
    mkdir build && cd build
    cmake -DLIBOBS_INCLUDE_DIR="../../obs-studio/libobs" -DCMAKE_INSTALL_PREFIX=/usr ..
    make -j4
    sudo make install
    mkdir -p ~/.config/obs-studio/plugins/v4l2sink/bin/64bit
    cp /usr/lib/obs-plugins/v4l2sink.so ~/.config/obs-studio/plugins/v4l2sink/bin/64bit

  • You will find then a /dev/video10 device if everything worked well.

  • If it doesn't this has probably to do with v4l2loopback already in use by some software like e.g. the iriun webcam app.
    • You should load your OBS Cam before starting the iriun app or
    • load both with e.g. modprobe v4l2loopback devices=2 video_nr=1,10 card_label="Iriun Cam","OBS Cam" exclusive_caps=1.
  • Don't forget to set the device in the OBS v4l2loopback sink plugin to /dev/video10 and start that service.
  • You should now be able to choose the OBS Cam in your WebRTC application when you start it.

If you experience annoying video lag

You may experience some annoying asynchronicity of video and audio following this procedure (only inside OBS! Iriun App shows perfect synchronicity.). Especially if you use a smartphone as webcam like obviously me.

By some desperate trials and errors I found a solution which actually works fine for me:

  • In Properties of your Video Capture Source
  • uncheck the Use buffering box (or similar - Pufferung benutzen in german.)
  • Without buffering your stream may stutter eventually but at least everything stays in sync!
    • I don't know how that might affect streaming or recording. But in video conferencing that does exactly the job for me.