Bookmarks

You haven't yet saved any bookmarks. To bookmark a post, just click .

  • No webcam? Use your mobile as webcam on Linux.

  • This mini-guide is divided into two parts. The first part is how I tried the nerdy way and the second part in much simpler and straight forward way.

    The basic idea of using mobile camera as webcam on Linux is this way:

    1. We get a video stream from the mobile, a server on smartphone device streaming the feed.
    2. The streamed feed is captured by a computer device and dump into a dummy/virtual camera device.
    3. Other applications can then view virtual camera as if it's an actual camera.

    Part 1. Manually creating virtual device on Linux and dumping a video feed.

    Step 1, I needed an android application which can make my smartphone act as a IP webcam. I chose "IP Webcam" application.

    IP Webcam on Google Play Store

    Step 2, then I created a virtual device on Linux. We'll install v4l2loopback on Linux using distribution package manager.

    # from RPM Fusion
    # On fedora, you'll only need to install v4l2loopback, not -utils or -dkms
    sudo dnf install v4l2loopback

    To create a dummy virtual camera, use these set of commands:

    # remove the module
    sudo rmmod v4l2loopback
    
    # create a virtual camera device
    sudo modprobe v4l2loopback video_nr=10 card_label="Dummy cam" exclusive_caps=1
    
    # start and reload the module
    sudo modprobe v4l2loopback exclusive_caps=1
    
    # WARNING; you may need to disable secure boot
    sudo modprobe -r v4l2loopback
    
    # list video-for-linux devices 
    v4l2-ctl --list-devices  # this should show you the device 'Dummy cam'
    

    Step 3, start the video server on the smartphone, It should start the server on 192.168.1.204:8080. If you visit this IP address, you'll see options like this. This application sends RTSP stream which we can then use to dump into our newly created virtual camera on Linux.

    Step 3, I used ffmpeg to dump the RTSP video stream into the virtual camera like this:

    # use ffmpeg to stream the rtsp stream to your virtual camera device
    ffmpeg -fflags nobuffer \
           -flags low_delay \
           -rtsp_transport udp \
           -reorder_queue_size 0 \
           -i rtsp://192.168.1.204:8080/h264.sdp \
           -fps_mode passthrough \
           -max_delay 0 \
           -copytb 0 \
           -copyts \
           -probesize 32 \
           -analyzeduration 0 \
           -buffer_size 8192 \
           -vcodec rawvideo \
           -pix_fmt yuv420p \
           -threads 4 \
           -thread_type frame \
           -f v4l2 \
           /dev/video10 	# the virtual camera device number

    or using this one:

    ffmpeg -fflags nobuffer \
           -flags low_delay \
           -rtsp_transport tcp \
           -reorder_queue_size 0 \
           -use_wallclock_as_timestamps 1 \
           -probesize 4096 \
           -analyzeduration 0 \
           -buffer_size 16384 \
           -max_delay 100000 \
           -i rtsp://192.168.1.204:8080/h264.sdp \
           -fps_mode passthrough \
           -vcodec rawvideo \
           -pix_fmt yuv420p \
           -threads 4 \
           -thread_type frame \
           -f v4l2 \
           /dev/video21
    

    Part 2. The straight forward way.

    I did noticed that the video stream via my nerdy way, is a bit choppy and has latency.

    I stumbled upon "DroidCam". It's a similar application to part 1, but it differs in how it's supposed to be used. It integrates very easily with OBS.

    On Linux, OBS or Open Broadcast Software is very easy to install and is used by many live streamers or content creators for video related tasks.

    We can easily obtain OBS on Linux from Flatpak.

    flatpak install flathub com.obsproject.Studio

    Then to work with "DroidCam", we'll need to install a plugin for OBS:

    flatpak install flathub com.obsproject.Studio.Plugin.DroidCam

    We'll also need to check if OBS virtual camera is listed in v4l2-ctl --list-devices. If it isn't we'll need to add it again:

    # if you did tried method 1; we'll need to add another virtual device at /dev/video0
    sudo rmmod v4l2loopback # stop the module
    sudo modprobe v4l2loopback video_nr=10,0 card_label="Dummy cam","OBS Virtual Camera" exclusive_caps=1,1
    sudo modprobe v4l2loopback exclusive_caps=1 # restart the module
    
    v4l2-ctl --list-devices # should show the OBS as /dev/video0 and Dummy Cam as /dev/video10

    Restart OBS, and you should see "DroidCam OBS" options in "Sources" tab.

    Start your DroidCam application on smartphone and add "DroidCam OBS" as sources.

    By default OBS should pick up the IP address of the DroidCam, but incase it doesn't you can manually enter it when you're adding it to sources or edit it afterwards.

    And volla, you've a video streamed from smartphone into your OBS.

    One last step to this procedure is to start virtual camera by enabling "Start Virtual Camera" in "Controls" panel, so that other application can pick up this stream as camera device.

    Start your browser and fire a webcam test if it's working. On Linux, I've noticed Firefox to pick it up very easily, but for Chromium based browser you may need to disable Hardware accelerated rendering.

    That's it for today. This is Anurag Dhadse, signing off.