Linux-Fake-Background-Webcam icon indicating copy to clipboard operation
Linux-Fake-Background-Webcam copied to clipboard

"Consumers" fluctuating between 1 and 2, causing stutters

Open Hubro opened this issue 4 years ago • 8 comments

When not consuming the fake webcam, "Consumers" seems to fluctuate between 0 and 1:

Consumers: 1
No consumers remaining, paused
Consumers: 1
No consumers remaining, paused
Consumers: 1
No consumers remaining, paused
Consumers: 1
No consumers remaining, paused

These 8 lines of output are printed approximately every 3 seconds.

When I do consume the webcam, for example via Chrome, the output changes to:

Consumers: 2
Consumers: 1
Consumers: 2
Consumers: 1
Consumers: 2
Consumers: 1
Consumers: 2
Consumers: 1

These 8 lines of output are again printed approximately every 3 seconds. Right before they are printed, the fake webcam output freezes for almost half a second. The end result is that every 3 seconds, the webcam stutters.

During those 3 seconds though, I'm getting a completely solid, stable 30 FPS and the camera is looking great!

Since I can't see that anybody else has this problem, I assume there's something weird with my setup. How can I start troubleshooting?

Hubro avatar Oct 02 '21 13:10 Hubro

Alright, I found the culprit. As long as Discord is open in the background, it will consume the fake exactly 4 times in an instant, approximately every 3 seconds, which makes the camera stutter. Very weird... Anyone else have the same issue? Any suggestions for a workaround?

Hubro avatar Oct 02 '21 14:10 Hubro

The --no-ondemand flag?

fangfufu avatar Oct 02 '21 19:10 fangfufu

This (https://github.com/fangfufu/Linux-Fake-Background-Webcam/pull/162) would have improved the issue, but it causes the script to crash.

fangfufu avatar Oct 02 '21 19:10 fangfufu

The --no-ondemand flag?

Right, I was planning to try that. That does indeed solve the problem, but it also causes too much CPU activity to keep this script always running.

I guess I'll wrap this up into a script I can run while doing video meeting.

Unrelated - I just rebooted my machine, and for some reason I'm now capped to exactly 15 FPS no matter the FPS value I choose. This is the exact same command I was running earlier:

python3 fake.py --no-ondemand -W 1920 -H 1080 -F 30 --no-background --background-blur 20 --no-foreground -w /dev/video0 -v /dev/video2

Output:

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
Real camera original values are set as: 640x480 with 30 FPS and video codec 1448695129
Real camera new values are set as: 1920x1080 with 30 FPS and video codec 1196444237
Running...
Please CTRL-C to pause and reload the background / foreground images
Please CTRL-\ to exit
FPS:  14.98

Before I rebooted, I was getting 30 FPS with fairly low CPU usage. It seems to be capped to 15 FPS for some reason. Any ideas?

Hubro avatar Oct 02 '21 21:10 Hubro

Can you get your Discord to use another webcam? I am a bit busy right now. I don't have time to move inotify to another thread.

Why don't you run the benchmark script to see if it is webcam problem? Have a look at https://github.com/fangfufu/Linux-Fake-Background-Webcam/issues/160 It could be lighting or some other issue that causes the real webcam to lock up at 15 FPS.

fangfufu avatar Oct 03 '21 16:10 fangfufu

It could be lighting or some other issue that causes the real webcam to lock up at 15 FPS.

That makes a lot of sense, I'll keep that in mind.

On an unrelated topic, I made a script to automate setting up the loopback interface and start fake.py:

#!/bin/bash

FAKER_PATH="$HOME/src/github/fangfufu/Linux-Fake-Background-Webcam"

# Print an informational status message to the user
function inform() {
    if [[ -t 1 ]]; then
        echo -e "\e[34;1m ==> $*\e[0m"
    else
        echo " ==> $*"
    fi
}

# Print an error message
function error() {
    if [[ -t 1 ]]; then
        echo -e "\e[31;1m ==> $*\e[0m" >&2
    else
        echo " ==> $*" >&2
    fi
}

function blurred_cam_video_nr() {
    v4l2-ctl --list-devices \
        | grep -F -A1 "Blurred background virtual cam" \
        | tail -n1 \
        | tr -d '[:space:]'
}

function create_loopback_cam() {
    sudo modprobe -r v4l2loopback

    sudo modprobe v4l2loopback \
        devices=1 \
        exclusive_caps=1 \
        card_label="Blurred background virtual cam"
}

function start_fake_cam() {
    local LOOPBACK_DEVICE="$1"

    cd "$FAKER_PATH"

    exec python3 fake.py \
        --no-ondemand \
        -W 1920 -H 1080 -F 30 \
        --no-background \
        --no-foreground \
        --background-blur 30 \
        -w /dev/video0 \
        -v "$LOOPBACK_DEVICE"
}

function check_dependencies() {
    local ERROR=no

    if ! which v4l2-ctl &>/dev/null; then
        error "Missing required command: v4l2-ctl"
        ERROR=yes
    fi

    if [[ ! -d $FAKER_PATH ]]; then
        error "Missing Linux-Fake-Background-Webcam checked out at:"
        error "$FAKER_PATH"
        ERROR=yes
    fi

    if [[ $ERROR == "yes" ]]; then
        error "One or more dependencies missing, exiting"
        exit 1
    fi
}

function main() {
    check_dependencies

    local LOOPBACK_VIDEO="$(blurred_cam_video_nr)"

    if [[ -z "$LOOPBACK_VIDEO" ]]; then
        inform "No V4L2 loopback device found, creating one..."

        create_loopback_cam
        sleep 0.2
        LOOPBACK_VIDEO="$(blurred_cam_video_nr)"

        inform "Created V4L2 loopback device on $LOOPBACK_VIDEO"
    fi

    inform "Starting fake cam on $LOOPBACK_VIDEO"

    start_fake_cam "$LOOPBACK_VIDEO"
}

main

Hubro avatar Oct 03 '21 20:10 Hubro

Reopening the issue because it contains an interesting script, I might incorporate it in future :slightly_smiling_face:

fangfufu avatar Oct 03 '21 20:10 fangfufu

Alright, I found the culprit. As long as Discord is open in the background, it will consume the fake exactly 4 times in an instant, approximately every 3 seconds, which makes the camera stutter. Very weird... Anyone else have the same issue? Any suggestions for a workaround?

Would just like to add that this was my exact issue as well. Closing Discord solved the problem. Would love to have discord open in the background as well though.

soliis-jones avatar Aug 10 '22 19:08 soliis-jones