Skip to content

linux-v4l2: Fix virtual camera start failure - #11906

Merged
RytoEX merged 1 commit into
obsproject:masterfrom
stephematician:fix_loopback
Apr 24, 2025
Merged

RytoEX merged 1 commit into
obsproject:masterfrom
stephematician:fix_loopback

Conversation

@stephematician

@stephematician stephematician commented Feb 28, 2025 •

Copy link
Copy Markdown
Contributor

Skip the 'non-compliant' usage of STREAMON ioctl for V4L2 compliant v4l2loopback module

Description

When starting the virtual camera:

  • Check the OUTPUT capability is reported for the device.
    • If has OUTPUT: use compliant calls only (do not use STREAMON/OFF workaround).
    • If no OUTPUT capability: attempt to reset using the STREAMON/OFF workaround for loopback 0.12.5-0.12.7, then continue to use workaround each time opened.

Motivation and Context

Fixes #11891

v4l2loopback was recently updated to have stronger compliance with V4L2 UAPI; thus, the invocation of STREAMON by OBS when no streaming I/O buffers have been negotiated (e.g. via REQBUFS) results in an error.

This is also a revision of the fix for #4808 - where it was identified that the STREAMON call would trigger the reset of the OUTPUT capability when the loopback node was closed (thus allowing the camera to be restarted).

How Has This Been Tested?

Built on Ubuntu 24.04, x64 machine, with v4l2loopback:

  1. @ 0.12.5-0.12.7 - confirm no regression for Virtual Camera will start one time and will not restart until reboot #4808;
  2. @ 0.13.0-0.13.2 - confirm no longer use workaround as loopback was already patched, and;
  3. @ 0.14.0 - confirm fix Failed to start virtual camera #11891

Starting, stopping, and re-starting the virtual camera succeeded in all versions tested.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.
  • I have included updates to all appropriate documentation.

@Fenrirthviti

Copy link
Copy Markdown
Member

For clarification, is there a reason to hold on to the old behavior for older versions? If the issue is stricter requirements on the v4l2loopback side, wouldn't this just be a backwards compatible change?

What breaks in the old versions with this change, or is that untested and just an attempt to not change what has been working?

@stephematician

Copy link
Copy Markdown
Contributor Author

The existing behaviour should be kept when using loopback <= 0.13.2, otherwise there'll be regression to #4808

An alternative solution: Simply ignore any errors from STREAMON or STREAMOFF. In a way; this would be cleaner - but there is a relatively harmless issue where, say, a user has an older loopback module and STREAMON fails for a legitimate reason. In that case - presumably the next call to write() will fail and if the user tries to view the virtual camera, they'll see nothing (but also have no feedback as to why it isn't working).

@stephematician

Copy link
Copy Markdown
Contributor Author

I just remembered I forgot to close the file pointer. Such a noob mistake!

@Fenrirthviti

Copy link
Copy Markdown
Member

I'm mostly just confused by the presentation of what this is actually fixing.

It sounds like this is actually resulting from what was a bug in v4l2loopback, and we had previously put a workaround in place for, and now that bug is fixed (in the name of better compliance, presumably) and the workaround is now causing issues.

@stephematician

Copy link
Copy Markdown
Contributor Author

I'm mostly just confused by the presentation of what this is actually fixing.

It sounds like this is actually resulting from what was a bug in v4l2loopback, and we had previously put a workaround in place for, and now that bug is fixed (in the name of better compliance, presumably) and the workaround is now causing issues.

Yes. Sorry, I should have answered your quetion.

That is exactly the issue.

  1. loopback <= 0.13.2 had a problem with internal state, meaning that cameras did not restart.
  2. OBS introduced a call to STREAMON call as a workaround.
  3. loopback > 0.13.2 (once released) no longer has a problem with internal state, but it will complain about STREAMON (because this use of STREAMON is not V4L2 UAPI compliant - pretty sure it will fail on every compliant device).
  4. as long as OBS doesn't call STREAMON (or ignores errors); then the camera starts/stops/restarts cleanly with loopback > 0.13.2

Happy to change approach with this PR if needed.

@Fenrirthviti

Copy link
Copy Markdown
Member

I'll leave the technical specifics to others to comment on, we were just a bit confused on the context around this.

Makes perfect sense now, thanks!

@stephematician

Copy link
Copy Markdown
Contributor Author

just a bit confused

I can relate. It took me a ridiculously long time to get my head about the loopback module as I was updating it. Thanks for your questions!

@stephematician

Copy link
Copy Markdown
Contributor Author

FWIW: the "simpler" fix would be to treat the failed STREAMON (or STREAMOFF) as an info/warning message and not an error; e.g. in v4l2-output.c:

	if (ioctl(vcam->device, VIDIOC_STREAMON, &parm) < 0) {
-		blog(LOG_ERROR, "Failed to start streaming on '%s' (%s)", device, strerror(errno));
-		goto fail_close_device;
+		if (errno == -EINVAL) {
+			blog(LOG_INFO, "EINVAL returned from loopback STREAMON, likely an error in loopback version <= 0.13.2");
+		} else {
+			blog(LOG_ERROR, "Failed to start streaming on '%s' (%s)", device, strerror(errno));
+			goto fail_close_device;
+		}
	}

	blog(LOG_INFO, "Virtual camera started");

@WizardCM WizardCM added the kind/bug Categorizes issue or PR as related to a bug. label Mar 1, 2025
@stephematician

Copy link
Copy Markdown
Contributor Author

Somewhat tidied up; also noticed that the dimensions (and pixel format) returned by S_FMT were being ignored; which can lead to unexpected behaviour if the (virtual) device chooses a different size (or pixel format) to the request. So that's fixed now, too.

@kkartaltepe kkartaltepe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First commit is fine. The 2nd commit changes a bit too much can we split it out into another PR so we can merge this one right now?

@stephematician

Copy link
Copy Markdown
Contributor Author

Sure, no problem; I didn't carefully plan the second commit - it just sort of "happened" when I had a closer look at the sequence.

Comment thread plugins/linux-v4l2/v4l2-output.c Resolved
Comment thread plugins/linux-v4l2/v4l2-output.c Resolved
@stephematician
stephematician force-pushed the fix_loopback branch 2 times, most recently from 74c3fa0 to e340a44 Compare March 13, 2025 01:01
@mutax

mutax commented Mar 14, 2025

Copy link
Copy Markdown

Just compiled the branch in an arch linux and can confirm virtual camera works again. Thanks a lot!
fwiw:
v4l2loopback-dkms 0.14.0-1
kernel 6.13.6-arch1-1

Comment thread plugins/linux-v4l2/v4l2-output.c Resolved
@kkartaltepe

kkartaltepe commented Mar 16, 2025 •

Copy link
Copy Markdown
Collaborator

FWIW: the "simpler" fix would be to treat the failed STREAMON (or STREAMOFF) as an info/warning message and not an error; e.g. in v4l2-output.c:

Yea we probably need to just do this to support flatpak since we cannot query modules and at least from v4l2-ctrl it looks like v4l2loopback does not actually report its version in the device query.

@stephematician

stephematician commented Mar 16, 2025 •

Copy link
Copy Markdown
Contributor Author

FWIW: the "simpler" fix would be to treat the failed STREAMON (or STREAMOFF) as an info/warning message and not an error; e.g. in v4l2-output.c:

Yea we probably need to just do this to support flatpak since we cannot query modules

Good to know. I haven't been able to figure out how to test in Flatpak (is there a guide?).

I assume when you say:

adding a new sandbox hole won't be accepted

you mean to say that it's not worth it to resolve this bug?

Would it makes sense to do a "hybrid" of the two fixes? E.g. attempt to read the version;

  1. if we can't read it - keep the existing behaviour (workaround) except ignore the expected error for loopback versions >= 0.14.0 (probably with a warning), otherwise;
  2. if we can read the version; skip the workaround (i.e. the non-compliant STREAMON/OFF)?

This means that at least for non-flatpak; the non-compliant calls are eliminated.

and at least from v4l2-ctrl it looks like v4l2loopback does not actually report its version in the device query.

Correct; most (?) devices report the kernel version; not the "out-of-tree" version - so it doesn't seem useful to us.

There is also a third possibility that might be better altogether... Instead of testing the version of the loopback module - test for the fault that occurs when the STREAMON is not called in earlier versions. If the fault appears; then try the STREAMON as a potential fix. Only then declare that the device could not be opened.

@stephematician

stephematician commented Mar 16, 2025 •

Copy link
Copy Markdown
Contributor Author

There is also a third possibility that might be better altogether... Instead of testing the version of the loopback module - test for the fault that occurs when the STREAMON is not called in earlier versions. If the fault appears; then try the STREAMON as a potential fix. Only then declare that the device could not be opened.

The more I think about it; the more I'd rather go in this direction.

The original problem was that a virtual camera could not be (re)started; it would report no OUTPUT capability.

By some quirk of the 0.12.5 loopback code; this could be avoided by calling STREAMON every time the device was opened, because this had the result that when the device was closed it would reset its capabilities correctly.

Proposed solution

When the loopback is opened; the OUTPUT capability is tested:

  • If NO OUTPUT:
    1. Log "attempting to reset loopback output caps":
    2. Call STREAMON (with output buffer) then STREAMOFF, and close the device; then re-open and query OUTPUT again.
      • NO OUTPUT capability or any errors: report that the device could not be opened.
      • YES OUTPUT: set a flag that the STREAMON should be invoked each time the loopback is opened (after format is set, of course).
  • If YES OUTPUT; proceed with V4L2-compliant approach OR use the non-compliant STREAMON kludge if flagged.

@stephematician

Copy link
Copy Markdown
Contributor Author

Thanks for your patience everyone; I'm the monster that broke this when fixing up v4l2loopback - but there was no other way to keep the module compliant with the kernel.

I wasn't expecting an OBS PR to fall on my shoulders, too, so this has taken much longer than if someone with experience in flatpak environment development was running the lead.

@FineWolf

FineWolf commented Apr 23, 2025 •

Copy link
Copy Markdown

@stephematician

You have nothing to apologize for, nor has anyone else here for that matter. Software development does take time, and you are doing great work.

My message was simply to offer my help if there needed to be extra work to be done, and, since this issue has sat idle waiting for reviews for the past month, it was an attempt to bring it back to the forefront.

Yes, rolling releases do get breaking changes faster than other distros, but it's also an opportunity to catch them and fix them before it affects point release distros. Unfortunately, this MR has sat in review phase without much interest for long enough that point release distros are now encountering this issue as well, so a little reminder was needed.

@tytan652 tytan652 added the Seeking Testers Build artifacts on CI label Apr 23, 2025
@FineWolf

FineWolf commented Apr 23, 2025 •

Copy link
Copy Markdown

@tytan652

Artifact obs-studio-flatpak-90516132a-x86_64.flatpak tested on two systems.


System 1:

Arch Linux (up to date as of this morning)
❯ v4l2loopback-ctl --version
v4l2loopback-ctl v0.14.0
v4l2loopback module v0.14.0

Virtual camera starts properly; both from a fresh run after a reboot, and after the module has been initialized.

EDIT: Did a 2 hours stream with no issue on this build.


System 2:

Bazzite 42.20250417
❯ v4l2loopback-ctl --version
v4l2loopback-ctl v0.14.0
v4l2loopback module v0.14.0

Virtual camera starts properly; both from a fresh run after a reboot, and after the module has been initialized.


I don't have a system with v0.13.2; however I can spin one up if you are lacking testers.

@RytoEX RytoEX left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay. Received approvals off-thread.

@RytoEX
RytoEX merged commit 12c6feb into obsproject:master Apr 24, 2025
@RytoEX RytoEX added this to the OBS Studio 31.1 milestone Apr 24, 2025
@stephematician
stephematician deleted the fix_loopback branch April 24, 2025 02:07
@akrz

akrz commented May 12, 2025

Copy link
Copy Markdown

Is there a way to have this in flathub?

@philg-dev

philg-dev commented May 12, 2025 •

Copy link
Copy Markdown

@akrz this is not part of any official release yet. As you can see above, it's been added to the 31.1 milestone when the PR got merged into the master branch.

You'll have to wait until that version gets released or build it yourself from source.

Edit: or see below for a flatpak build, as people pointed out. It's still not "from flathub" that way, but it's definitely more accessible than building from source.

@JacobCallahan

Copy link
Copy Markdown

@akrz you can also download the flatpak build artifact from the GitHub actions in this pull request and use that to install an alternate version locally.

@stephematician

Copy link
Copy Markdown
Contributor Author

@JacobCallahan @akrz FWIW - navigating to artifacts always seems tricky - I find them by navigating to the checks tab at the top of the PR, then selecting Pull on the left. You should end up here; https://github.com/obsproject/obs-studio/actions/runs/14606426833

@jpVm5jYYRE1VIKL

Copy link
Copy Markdown

Hey . Can you please form HOTFIX release because whole functionality in current moment broken .

@p-m-j

p-m-j commented May 20, 2025

Copy link
Copy Markdown

@jpVm5jYYRE1VIKL why not just run the pre-release as was previously suggested? obs-studio-flatpak-90516132a-x86_64.flatpak

@dbear496

Copy link
Copy Markdown

@jpVm5jYYRE1VIKL why not just run the pre-release as was previously suggested? obs-studio-flatpak-90516132a-x86_64.flatpak

A hotfix release could get this fix into package repositories faster than waiting until the next minor version release. Sure, everyone could uninstall the obs-studio package provided by their distribution repository and install the pre-release version; then when the next release comes, uninstall the pre-release and re-install the repository package. But no one wants to go through that.

@akrz

akrz commented May 20, 2025 •

Copy link
Copy Markdown

I tried the artifact build but had issues with plugins (droidcam). I just removed the flatpak and went with the native fedora package that has this fix applied. I think some other distros backported this as a patch too.

I'm a bit surprised that the official flatpak image of obs doesn't have this fix yet in the stable version. This broke the workflow of several users and it wasn't trivial to know what was going on.

@stephematician

Copy link
Copy Markdown
Contributor Author

Hey . Can you please form HOTFIX release because whole functionality in current moment broken .

I'd say that unless you can build OBS, use the flatpak artefact, or are on a distribution which has a backported patch (i.e. Fedora); then roll v4l2loopback to 0.13.2 and hold it there.

@burtonis

burtonis commented Jun 6, 2025 •

Copy link
Copy Markdown

Garuda Linux (Arch-based) and I upgraded to aur/obs-studio-git. The v4l2loopback module no longer complains.
6.14.9-zen1-1-zen
aur/obs-studio-git 31.0.3.r236.g000437f-1
extra/v4l2loopback-dkms 0.14.0-1

Is there any way to do a minor point release to include just this fix?

@stephematician

Copy link
Copy Markdown
Contributor Author

Garuda Linux (Arch-based) and I upgraded to aur/obs-studio-git. The v4l2loopback module no longer complains. 6.14.9-zen1-1-zen aur/obs-studio-git 31.0.3.r236.g000437f-1 extra/v4l2loopback-dkms 0.14.0-1

Is there any way to do a minor point release to include just this fix?

There are beta releases on GitHub with the fix, e.g.: https://github.com/obsproject/obs-studio/releases/tag/31.1.0-beta2

@jpVm5jYYRE1VIKL

jpVm5jYYRE1VIKL commented Jun 9, 2025 •

Copy link
Copy Markdown

@jpVm5jYYRE1VIKL why not just run the pre-release as was previously suggested? obs-studio-flatpak-90516132a-x86_64.flatpak

@p-m-j . Because nobody want to fill OS with multiply garbage packages which are in reality not needed. And nobody want to install it only to have posibility to start OBS.

@farnoy

farnoy commented Jun 16, 2025

Copy link
Copy Markdown

Just experiencing this issue now - so this is a regression that's been fixed nearly two months ago but there's still no stable release since then that includes it? How is it not a red flag when some distros are pulling in patches like this directly?

@stephematician

Copy link
Copy Markdown
Contributor Author

Just experiencing this issue now - so this is a regression that's been fixed nearly two months ago but there's still no stable release since then that includes it? How is it not a red flag when some distros are pulling in patches like this directly?

@farnoy - I would be a little frustrated, too. I can accept a little bit of muddling around if I'm on a rolling distro, but, yes, even Debian Trixie (testing) has backported this fix.

I think it's clear there won't be releases between now and 31.1 (which will have this fix). So I guess fingers crossed OBS 31.1 lands soon 🤞!

@jpVm5jYYRE1VIKL

Copy link
Copy Markdown

Well done OBS . IN current fresh kernel not possble anymore to use old v4l2loopback-0.13.2 . So DKMS not build anymore.
OBS completely broken and no release.

@tiberium-v

Copy link
Copy Markdown

Well done OBS . IN current fresh kernel not possble anymore to use old v4l2loopback-0.13.2 . So DKMS not build anymore. OBS completely broken and no release.

That is the reason for the LTS Kernel on a bleeding edge distro... and the fix is in the beta and target the supported platform Linux:Ubuntu and Linux:Flatpak.

All other obs packages are not provided by the obs team itself. So when a distro use an incompatible dependency, they have to fix it - what some distro already did.

@stephematician

Copy link
Copy Markdown
Contributor Author

Well done OBS . IN current fresh kernel not possble anymore to use old v4l2loopback-0.13.2 . So DKMS not build anymore. OBS completely broken and no release.

The solution in your case is either; roll back the kernel (and then loopback), or use flatpak (which I note you don't want to do), or build OBS. If you don't like having development libraries sitting around on your system, you can build within a container instead (then delete the container).

@tqwewe

tqwewe commented Jun 19, 2025

Copy link
Copy Markdown

Thanks so much for this PR!

I'm just going to share a snippet of nix code if anyone wants to use this PR before OBS 31.1 is released:

programs.obs-studio.package = unstable.obs-studio.overrideAttrs (oldAttrs: {
  src = pkgs.fetchFromGitHub {
    owner = "obsproject";
    repo = "obs-studio";
    rev = "12c6febae21f369da50f09d511b54eadc1dc1342"; # https://github.com/obsproject/obs-studio/pull/11906
    sha256 = "sha256-DIlAMCdve7wfbMV5YCd3qJnZ2xwJMmQD6LamGP7ECOA=";
    fetchSubmodules = true;
  };
  version = "31.1.0-beta1";
  patches = builtins.filter (
    patch:
    !(
      builtins.baseNameOf (toString patch) == "Enable-file-access-and-universal-access-for-file-URL.patch"
    )
  ) oldAttrs.patches;
});

@dbear496

Copy link
Copy Markdown

Can we please get a hotfix release for this? Virtual camera has been broken for 4 months, and for 2 months it has been fixed in master but no release. How hard could it be to release a 31.0.1, instead of having each user individually go through installing the pre-release version manually.

@obsproject obsproject locked as resolved and limited conversation to collaborators Jun 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

kind/bug Categorizes issue or PR as related to a bug. Seeking Testers Build artifacts on CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failed to start virtual camera