flathub / com.obsproject.Studio

This repository is no longer used to build OBS. Issues should be reported at https://github.com/obsproject/obs-studio
https://github.com/obsproject/obs-studio
29 stars 21 forks source link

v4l2_sink flatpak support in 26.1.0 needs love #94

Closed bnordgren closed 3 years ago

bnordgren commented 3 years ago

On fedora silverblue 33 (basecommit 56c8ba8529da528d68252247fbfa0c0c4ec2361f442fa4eda3ee9bd5a94e1ff7)

I compiled v4l2loopback in a toolbox and manually insmoded it. I now have a /dev/video2.

Started a shiny new OBS Studio 26.1.0 from flatpak and there's no "start virtual camera" button. As described on #65, I looked at the currrent logs to find:

01:07:28 PM.656: No blackmagic support
01:07:28 PM.663: v4l2loopback not installed, virtual camera disabled
01:07:28 PM.665: NVENC supported
01:07:28 PM.665: FFMPEG VAAPI supported
01:07:28 PM.666: os_dlopen(/app//lib/obs-plugins/obs-ndi.so->/app//lib/obs-plugins/obs-ndi.so): /app//lib/obs-plugins/obs-ndi.so: cannot open shared object file: No such file or directory
01:07:28 PM.666: 
01:07:28 PM.666: Module '/app//lib/obs-plugins/obs-ndi.so' not loaded
01:07:28 PM.669: os_dlopen(/app//lib/obs-plugins/obs-v4l2sink.so->/app//lib/obs-plugins/obs-v4l2sink.so): /app//lib/obs-plugins/obs-v4l2sink.so: cannot open shared object file: No such file or directory
01:07:28 PM.669: 
01:07:28 PM.669: Module '/app//lib/obs-plugins/obs-v4l2sink.so' not loaded

Entering the running flatpak and listing the contents of /app/lib/obs-plugins shows that both things it's complaining about are symlinks which did not make it to the flatpak packaging... So that's one thing that needs to be addressed...

[šŸ“¦ com.obsproject.Studio obs-plugins]$ ls -l 
total 2120
-rwxr-xr-x 2 nfsnobody nfsnobody  43288 Dec 31  1969 decklink-captions.so
-rwxr-xr-x 2 nfsnobody nfsnobody 264472 Dec 31  1969 decklink-ouput-ui.so
-rwxr-xr-x 2 nfsnobody nfsnobody 560424 Dec 31  1969 frontend-tools.so
-rwxr-xr-x 2 nfsnobody nfsnobody  40888 Dec 31  1969 image-source.so
-rwxr-xr-x 2 nfsnobody nfsnobody  84496 Dec 31  1969 linux-capture.so
-rwxr-xr-x 2 nfsnobody nfsnobody  96456 Dec 31  1969 linux-decklink.so
-rwxr-xr-x 2 nfsnobody nfsnobody  31552 Dec 31  1969 linux-pulseaudio.so
-rwxr-xr-x 2 nfsnobody nfsnobody  43664 Dec 31  1969 linux-v4l2.so
-rwxr-xr-x 2 nfsnobody nfsnobody 127176 Dec 31  1969 obs-ffmpeg.so
-rwxr-xr-x 2 nfsnobody nfsnobody 239240 Dec 31  1969 obs-filters.so
-rwxr-xr-x 2 nfsnobody nfsnobody  22752 Dec 31  1969 obs-libfdk.so
lrwxrwxrwx 1 nfsnobody nfsnobody     23 Dec 25 12:28 obs-ndi.so -> /app/lib/ndi/obs-ndi.so
-rwxr-xr-x 2 nfsnobody nfsnobody 269200 Dec 31  1969 obs-outputs.so
-rwxr-xr-x 2 nfsnobody nfsnobody  41664 Dec 31  1969 obs-transitions.so
lrwxrwxrwx 1 nfsnobody nfsnobody     29 Dec 25 12:28 obs-v4l2sink.so -> /app/lib/v4l2sink/v4l2sink.so
-rwxr-xr-x 2 nfsnobody nfsnobody  71952 Dec 31  1969 obs-vst.so
-rwxr-xr-x 2 nfsnobody nfsnobody  35112 Dec 31  1969 obs-x264.so
-rwxr-xr-x 2 nfsnobody nfsnobody  30840 Dec 31  1969 obs-xdg-portal.so
-rwxr-xr-x 2 nfsnobody nfsnobody  88552 Dec 31  1969 rtmp-services.so
-rwxr-xr-x 2 nfsnobody nfsnobody  39904 Dec 31  1969 text-freetype2.so
[šŸ“¦ com.obsproject.Studio obs-plugins]$ ls /app/lib/v4l2sink/
[šŸ“¦ com.obsproject.Studio obs-plugins]$ ls /app/lib/ndi/       
[šŸ“¦ com.obsproject.Studio obs-plugins]$ 

Moving on, an indication that the loopback device is functional and available inside the flatpak:

[šŸ“¦ com.obsproject.Studio obs-plugins]$ ls /sys/devices/virtual/video4linux
video2
[šŸ“¦ com.obsproject.Studio obs-plugins]$ v4l2-ctl -d /dev/video2 -l 

User Controls

                    keep_format 0x0098f900 (bool)   : default=0 value=0
              sustain_framerate 0x0098f901 (bool)   : default=0 value=0
                        timeout 0x0098f902 (int)    : min=0 max=100000 step=1 default=0 value=0
               timeout_image_io 0x0098f903 (bool)   : default=0 value=0

I just don't know what criteria OBS is using to decide that v4l2loopback isn't installed. The v4l2 sink plugin is missing from the flatpak, but that appears to be a separate issue, as the log file doesn't emit that error until well after it complains that loopback support is missing.

By the way, you can probably close #65 as moot now, and #91 has a rather hypothetical "probably won't work until the advent of this pending PipeWire technology", which seems a little wrong given that #65 got it to work manually...somehow...

bnordgren commented 3 years ago

Aha!

This is how OBS determines whether the v4l2loopback module is loaded:

https://github.com/obsproject/obs-studio/blob/eac6604a83bef6699ef88a2e273af8a5ff1a1b0d/plugins/linux-v4l2/linux-v4l2.c#L34

static bool v4l2loopback_installed()
{
    bool loaded = false;

    int ret = system("modinfo v4l2loopback >/dev/null 2>&1");

    if (ret == 0)
        loaded = true;

    return loaded;
}

This detection method fails inside the flatpak not because the kernel module is missing, but because there's no modinfo command inside the flatpak. Any chance you could coordinate with OBS to determine whether modinfo should be added to the flatpak or whether the test should be changed to something like grep v4l2loopback /proc/modules? (i.e. What implications does the latter solution have for which versions of the linux kernel are supported?)

eszlari commented 3 years ago

https://github.com/obsproject/obs-studio/issues/3845

bnordgren commented 3 years ago

So, I think the missing *.so libraries still need to be included in the flatpak.

There seems to be a lot of discussion of possibilities in that other issue, most of which assume that OBS is not running in a sandbox...or at least is running in an environment (sandbox or no) which has the ability to introspect the state of kernel modules with modinfo. I did not see a decision made as to the future direction.

One person raises the issue that there are two tests, one to determine the presence of the module (loaded or not) and the other to try and load it. Is it even possible to load kernel modules within a flatpak, or does that require special permission? Seems like even in a non-sandboxed environment you'd have to run the application as root for it to actually load a kernel module...

Is there any possibility of packaging the current version of OBS with a shim script (and the missing libraries) so that the current upstream version will work? Even a simple modinfo bash script in the $PATH which contains the above grep command could provide the environmental support that OBS assumes is available. It doesn't have to be a generic replacement for modinfo, it just has to perform that one function.. Future packaging efforts could react to whatever decisions the upstream makes.

bnordgren commented 3 years ago

I believe I can outline a draft resolution to this ticket now. The following mimics the non-sandboxed functionality. Let me know if I missed anything below. It's also conceivable that the modinfo script below could be simplified using one of the suggestions in https://github.com/obsproject/obs-studio/issues/3845, eliminating also the need to flatpak-spawn anything or specify the KERNEL_MOD_COMPILE_DIR variable.

For the moment, I will still have to uninstall the flatpak and layer the obs-studio package from rpmfusion.

Step one: package these files

Step two: provide "modinfo"

place the following shim script in /app/bin/modinfo:

#!/bin/bash

flatpak-spawn --host modinfo $KERNEL_MOD_COMPILE_DIR/v4l2loopback.ko

Step three: enable flatpak-spawn

add "talk" permission so flatpak-spawn --host works. (--talk-name=org.freedesktop.Flatpak)

Step four: document

Users will need to

  1. Manually load v4l2loopback before running OBS.
  2. Provide the KERNEL_MOD_COMPILE_DIR environment variable to this flatpak as an override.
bnordgren commented 3 years ago

One more update. After tinkering with the package from rpmfusion updates testing (OBS 26.1.0) on my Fedora Silverblue 33 host, I was finally able to get it to work. Note that the system's modinfo utility didn't work either (because OBS doesn't specify a file, and therefore modinfo looks in /lib/modules...) I modified my modinfo script in $HOME/bin to:

#!/bin/bash

#flatpak-spawn --host modinfo $KERNEL_MOD_COMPILE_DIR/v4l2loopback.ko

grep v4l2loopback /proc/modules

And I start OBS on the host with: QT_QPA_PLATFORM=xcb PATH=$HOME/bin:$PATH /usr/bin/obs

I think the implications of this experiment for flatpak packaging are:

  1. Put this modified modinfo in /app/bin; and
  2. Forget about step three.
  3. Forget about step 4.2.

I think that simplifies things greatly...

anthr76 commented 3 years ago

Has any progress been made with this? Wondering if it's worth hacking this together.

GeorgesStavracas commented 3 years ago

OBS Studio 27, which will be released soon, will have a Flatpak-friendly implementation of VirtualCam (see obsproject/obs-studio#4552). No workarounds needed. The same contraints apply though: you'll need to have v4l2loopback on your host system.

I'll go ahead and close this since it's being tracked upstream, and there's nothing else to do besides waiting for the release to happen :slightly_smiling_face: