Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EvergreenTheTree/90182e76bafecdd4de9dd21adf72a3e7 to your computer and use it in GitHub Desktop.
Save EvergreenTheTree/90182e76bafecdd4de9dd21adf72a3e7 to your computer and use it in GitHub Desktop.
obs-studio v4l2loopback patch
From 52d57cf70ca70f55378112f6eeb5708fb7680a6b Mon Sep 17 00:00:00 2001
From: stephematician <[email protected]>
Date: Wed, 19 Mar 2025 13:59:21 +1100
Subject: [PATCH] linux-v4l2: Fix virtual camera start failure
Add function that tries to reset v4l2loopback output for module versions
from 0.12.5 to 0.12.7. If successful, then set flag that STREAMON and
STREAMOFF are necessary each time the device is opened/closed.
---
plugins/linux-v4l2/v4l2-output.c | 57 ++++++++++++++++++++++++++++++--
1 file changed, 54 insertions(+), 3 deletions(-)
diff --git a/plugins/linux-v4l2/v4l2-output.c b/plugins/linux-v4l2/v4l2-output.c
index 366fc474f69d..d5e4e0f6ad57 100644
--- a/plugins/linux-v4l2/v4l2-output.c
+++ b/plugins/linux-v4l2/v4l2-output.c
@@ -15,6 +15,7 @@ struct virtualcam_data {
obs_output_t *output;
int device;
uint32_t frame_size;
+ bool use_caps_workaround;
};
static const char *virtualcam_name(void *unused)
@@ -110,11 +111,54 @@ static void *virtualcam_create(obs_data_t *settings, obs_output_t *output)
return vcam;
}
+bool try_reset_output_caps(const char *device)
+{
+ struct v4l2_capability capability;
+ struct v4l2_format format;
+ int fd;
+
+ blog(LOG_INFO, "Attempting to reset output capability of '%s'", device);
+
+ fd = open(device, O_RDWR);
+ if (fd < 0)
+ return false;
+
+ format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
+ if (ioctl(fd, VIDIOC_G_FMT, &format) < 0)
+ goto reset_fail_close_fd;
+
+ if (ioctl(fd, VIDIOC_S_FMT, &format) < 0)
+ goto reset_fail_close_fd;
+
+ if (ioctl(fd, VIDIOC_STREAMON, &format.type) < 0)
+ goto reset_fail_close_fd;
+
+ if (ioctl(fd, VIDIOC_STREAMOFF, &format.type) < 0)
+ goto reset_fail_close_fd;
+
+ close(fd);
+
+ fd = open(device, O_RDWR);
+ if (fd < 0)
+ return false;
+
+ if (ioctl(fd, VIDIOC_QUERYCAP, &capability) < 0)
+ goto reset_fail_close_fd;
+
+ close(fd);
+ return (capability.device_caps & V4L2_CAP_VIDEO_OUTPUT) != 0;
+
+reset_fail_close_fd:
+ close(fd);
+ return false;
+}
+
static bool try_connect(void *data, const char *device)
{
+ static bool use_caps_workaround = false;
struct virtualcam_data *vcam = (struct virtualcam_data *)data;
- struct v4l2_format format;
struct v4l2_capability capability;
+ struct v4l2_format format;
struct v4l2_streamparm parm;
uint32_t width = obs_output_get_width(vcam->output);
@@ -130,6 +174,13 @@ static bool try_connect(void *data, const char *device)
if (ioctl(vcam->device, VIDIOC_QUERYCAP, &capability) < 0)
goto fail_close_device;
+ if (!use_caps_workaround && !(capability.device_caps & V4L2_CAP_VIDEO_OUTPUT)) {
+ if (!try_reset_output_caps(device))
+ goto fail_close_device;
+ use_caps_workaround = true;
+ }
+ vcam->use_caps_workaround = use_caps_workaround;
+
format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
if (ioctl(vcam->device, VIDIOC_G_FMT, &format) < 0)
@@ -165,7 +216,7 @@ static bool try_connect(void *data, const char *device)
memset(&parm, 0, sizeof(parm));
parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
- if (ioctl(vcam->device, VIDIOC_STREAMON, &parm) < 0) {
+ if (vcam->use_caps_workaround && ioctl(vcam->device, VIDIOC_STREAMON, &parm.type) < 0) {
blog(LOG_ERROR, "Failed to start streaming on '%s' (%s)", device, strerror(errno));
goto fail_close_device;
}
@@ -241,7 +292,7 @@ static void virtualcam_stop(void *data, uint64_t ts)
struct v4l2_streamparm parm = {0};
parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
- if (ioctl(vcam->device, VIDIOC_STREAMOFF, &parm) < 0) {
+ if (vcam->use_caps_workaround && ioctl(vcam->device, VIDIOC_STREAMOFF, &parm) < 0) {
blog(LOG_WARNING, "Failed to stop streaming on video device %d (%s)", vcam->device, strerror(errno));
}
--
2.49.0
# Maintainer: Jonathan Steel <jsteel at archlinux.org>
# Contributor: Benjamin Klettbach <[email protected]>
# Contributor: Maciek Marciniak <mm2pl at kotmisia.pl>
pkgname=obs-studio
pkgver=31.0.1
pkgrel=2
pkgdesc="Free, open source software for live streaming and recording"
arch=('x86_64')
url="https://obsproject.com"
license=('GPL2')
depends=('ffmpeg' 'jansson' 'libxinerama' 'libxkbcommon-x11' 'mbedtls' 'rnnoise' 'pciutils'
'qt6-svg' 'curl' 'jack' 'gtk-update-icon-cache' 'pipewire' 'libxcomposite'
'libdatachannel' 'uthash')
makedepends=('cmake' 'libfdk-aac' 'x264' 'swig' 'python' 'luajit' 'sndio' 'nlohmann-json'
'ffnvcodec-headers' 'websocketpp' 'asio' 'qrcodegencpp-cmake')
optdepends=('libfdk-aac: FDK AAC codec support'
'libva-intel-driver: hardware encoding'
'libva-mesa-driver: hardware encoding'
'luajit: scripting support'
'python: scripting support'
'sndio: Sndio input client'
'qrcodegencpp-cmake: websocket support'
'v4l2loopback-dkms: virtual camera support')
source=($pkgname-$pkgver.tar.gz::https://github.com/obsproject/obs-studio/releases/download/$pkgver/OBS-Studio-$pkgver-Sources.tar.gz
0001-linux-v4l2-Fix-virtual-camera-start-failure.patch)
sha256sums=('d9b280b5d0a1a958e0017ef47049492e336b7b98acbd0bfd372a7ad923c6f660'
'0af9122346ea4266ea578b39d3dfc05583e316f4894ddaea5c067862281c305f')
prepare() {
patch -d $pkgname-$pkgver-sources -Np1 -i ../0001-linux-v4l2-Fix-virtual-camera-start-failure.patch
}
build() {
export CFLAGS+=" -Wno-error=deprecated-declarations"
cmake -B build -S $pkgname-$pkgver-sources \
-DCMAKE_INSTALL_PREFIX="/usr" \
-DENABLE_BROWSER=OFF \
-DENABLE_VST=ON \
-DENABLE_VLC=OFF \
-DENABLE_NEW_MPEGTS_OUTPUT=OFF \
-DENABLE_AJA=OFF \
-DENABLE_JACK=ON \
-DENABLE_LIBFDK=ON \
-DENABLE_WEBRTC=ON \
-DOBS_VERSION_OVERRIDE="$pkgver" \
-DCALM_DEPRECATION=ON \
-DENABLE_WEBSOCKET=ON \
-Wno-dev
cmake --build build
}
package() {
DESTDIR="$pkgdir" cmake --install build
}
@djsigmann
Copy link

The hash listed in the sha256sums array in the PKGBUILD and the file's actual hash differ:

source=($pkgname-$pkgver.tar.gz::https://github.com/obsproject/obs-studio/releases/download/$pkgver/OBS-Studio-$pkgver-Sources.tar.gz
	0001-linux-v4l2-Fix-virtual-camera-start-failure.patch)
sha256sums=('d9b280b5d0a1a958e0017ef47049492e336b7b98acbd0bfd372a7ad923c6f660'
            '4aef6c2a77b942cb4f93720ab216e3fbab803c9876baf8dd032e9e2faea49d86')
$ wget -q https://gist.githubusercontent.com/EvergreenTheTree/90182e76bafecdd4de9dd21adf72a3e7/raw/f0b9fe3c561cf25cb652c5700566d8b5e2b38df6/0001-linux-v4l2-Fix-virtual-camera-start-failure.patch -O- | sha256sum
0af9122346ea4266ea578b39d3dfc05583e316f4894ddaea5c067862281c305f  -

@EvergreenTheTree
Copy link
Author

Updated, not sure how that happened.

@kode54
Copy link

kode54 commented Apr 2, 2025

Gist likes to reformat files to match the tab/indent settings you specify when you upload them through the web interface.

@kode54
Copy link

kode54 commented Apr 19, 2025

Patch still needed for 31.0.3-1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment