Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Consumers" fluctuating between 1 and 2, causing stutters #167

Open
Hubro opened this issue Oct 2, 2021 · 8 comments
Open

"Consumers" fluctuating between 1 and 2, causing stutters #167

Hubro opened this issue Oct 2, 2021 · 8 comments
Labels
enhancement New feature or request

Comments

@Hubro
Copy link

Hubro commented Oct 2, 2021

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
Copy link
Author

Hubro commented Oct 2, 2021

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?

@fangfufu
Copy link
Owner

fangfufu commented Oct 2, 2021

The --no-ondemand flag?

@fangfufu fangfufu added the enhancement New feature or request label Oct 2, 2021
@fangfufu fangfufu linked a pull request Oct 2, 2021 that will close this issue
@fangfufu
Copy link
Owner

fangfufu commented Oct 2, 2021

This (#162) would have improved the issue, but it causes the script to crash.

@Hubro
Copy link
Author

Hubro commented Oct 2, 2021

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?

@fangfufu
Copy link
Owner

fangfufu commented Oct 3, 2021

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 #160 It could be lighting or some other issue that causes the real webcam to lock up at 15 FPS.

@Hubro
Copy link
Author

Hubro commented Oct 3, 2021

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 Hubro closed this as completed Oct 3, 2021
@fangfufu
Copy link
Owner

fangfufu commented Oct 3, 2021

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

@fangfufu fangfufu reopened this Oct 3, 2021
@soliis-jones
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants