Skip to content

linux-v4l2: Save device by path - #3437

Closed
kkartaltepe wants to merge 1 commit into
obsproject:masterfrom
kkartaltepe:v4l2-by-path
Closed

kkartaltepe wants to merge 1 commit into
obsproject:masterfrom
kkartaltepe:v4l2-by-path

Conversation

@kkartaltepe

Copy link
Copy Markdown
Collaborator

Description

This adjusts device listing to be by /dev/v4l/by-path which is based
on where the device is plugged in instead of when it is initialized.
This should help people with multiple devices across restarts.

Old plugin data will still work unchanged, when users modify settings
the new options will be transparently backed by the new keys but names
presented remain consistent.

Motivation and Context

/dev/videoX is not consistent across reboots. This fixes #3003 for users with multiple devices.

How Has This Been Tested?

Works with my devices. Seems it should also work with the freeBSD assuming they have readlink. Manually editing scene to have the previous config works fine.

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.

@WizardCM WizardCM added the kind/bug Categorizes issue or PR as related to a bug. label Sep 9, 2020
@kkartaltepe
kkartaltepe force-pushed the v4l2-by-path branch 2 times, most recently from 8f3b820 to e0dd0ca Compare September 9, 2020 02:02
@kkartaltepe

Copy link
Copy Markdown
Collaborator Author

formatting was a bit weird, fixed up now.

This adjusts device listing to be by /dev/v4l/by-path which is based
on where the device is plugged in instead of when it is initialized.
This should help people with multiple devices across restarts.

Old plugin data will still work unchanged, when users modify settings
the new options will be transparently backed by the new keys but names
presented remain consistent.
@cbm755

cbm755 commented Sep 10, 2020

Copy link
Copy Markdown

Thank you this will really help! I will test next time I have a long enough window to muck with my OBS install!

@emaste

emaste commented Oct 2, 2020

Copy link
Copy Markdown
Contributor

@khng300 we should check this on FreeBSD

@kkartaltepe

Copy link
Copy Markdown
Collaborator Author

This should leave freebsd unchanged other than trying to resolve links which should harmlessly fail. If BSDs present /dev/v4l/by-path/ and it works im happy to remove the fork for BSD systems.

@khng300

khng300 commented Oct 2, 2020

Copy link
Copy Markdown
Contributor

This should leave freebsd unchanged other than trying to resolve links which should harmlessly fail. If BSDs present /dev/v4l/by-path/ and it works im happy to remove the fork for BSD systems.

This path should be a product of systemd setup on linux right?

@khng300

khng300 commented Oct 2, 2020

Copy link
Copy Markdown
Contributor

image
On FreeBSD the v4l device description looks like what's shown above. Video works.

@kkartaltepe

kkartaltepe commented Oct 3, 2020 •

Copy link
Copy Markdown
Collaborator Author

image
On FreeBSD the v4l device description looks like what's shown above. Video works.

The stuff in quotes may be a bug due to different paths on freebsd but its unrelated to this PR i think (on linux it shows the device address (usb-0000:00:14.0-6) on my machine for example).

@flexiondotorg

Copy link
Copy Markdown

I use this patch in my build of OBS Studio 26.0.2 and I have to say it is an essential addition for anyone using multiple capture devices. I used 4, 6 or 8 capture devices (misxture of PCIe and USB) and this patch prevents the random shuffling of camera sources 👍

@jp9000

jp9000 commented Nov 13, 2020

Copy link
Copy Markdown
Member

Are earlier versions forward-compatible with this?

@kkartaltepe

Copy link
Copy Markdown
Collaborator Author

This only changes how devices are enumerated in the settings, old source configurations/device initialization will continue to work unchanged.

@DarkDefender

Copy link
Copy Markdown

Any specific reason why this is not merged?

This patch really saved my ass when we had a studio setup with a lot of v4l devices connected.

On each reboot the 8 devices would be scrambled and it took considerable amount of time to set up everything again correctly.

However by creating a custom build with this patch applied, no additional setup is required after the initial configuration of the v4l devices.

@Fenrirthviti

Copy link
Copy Markdown
Member

Because we have hundreds of PRs, tens of thousands of lines of code to review, and only two full-time developers. This, while maybe important to your specific setup and an inconvenience, is not a high priority compared to other issues/bug fixes. We'll get to it when we can.

@DarkDefender

Copy link
Copy Markdown

Thanks for the respons. Now I know that it hasn't been forgotten :)

@flexiondotorg

Copy link
Copy Markdown

This patch applies cleanly on OBS 27.0.0-rc3 and continues to work reliably.

@kkartaltepe

Copy link
Copy Markdown
Collaborator Author

I was thinking about this patch again, but it seems like it v4l2loopback devices dont end up registered under the /dev/v4l/ tree. So there is probably more work to also include those video devices without paths.

@kkartaltepe
kkartaltepe marked this pull request as draft May 28, 2021 23:59
const char *basedir = "/dev/";
#else
dirp = opendir("/sys/class/video4linux");
const char *basedir = "/dev/v4l/by-path/";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can you use by-id instead of by-path? This will allow plugging capture devices in different USB ports without the need to change the config (the ID will stay the same, but the path will change).

@joschro

joschro commented Nov 11, 2021

Copy link
Copy Markdown

this issue is really a showstopper when frequently moving between office and home office - no one there who can accept the patch provided?

@RytoEX

RytoEX commented Nov 12, 2021

Copy link
Copy Markdown
Member

this issue is really a showstopper when frequently moving between office and home office - no one there who can accept the patch provided?

The patch author set this to Draft on May 28, 2021 for the following reason:

I was thinking about this patch again, but it seems like it v4l2loopback devices dont end up registered under the /dev/v4l/ tree. So there is probably more work to also include those video devices without paths.

We don't merge Draft PRs. Even if we did, the conditions previously explained also still apply. We'll get to this when we can.

@ggodlewski ggodlewski mentioned this pull request May 16, 2022
6 tasks done
@kkartaltepe

Copy link
Copy Markdown
Collaborator Author

Closing this out as I doubt ill un-draft this.

@kkartaltepe kkartaltepe closed this Mar 1, 2023
@johnebgood

johnebgood commented Mar 1, 2024 •

Copy link
Copy Markdown

This merge will fix a problem that makes the OBS configuration break on every reboot for people with multiple V4L2 cameras. Random camera changes don't fail in a graceful way, some cameras don't support the same settings so when the devices randomly switch you never know what's going to happen on start, including crashes. I have to spend 30+ minutes every time I reboot fixing the camera settings and order. One can't just quickly fix the cameras by switching the device combo box either, each device has to deleted all of the devices and configured from scratch because they get bound to the incorrect source and you can't pick a video device that's already bound to another source. They can't be renamed because some settings and encoders don't apply to the switched devices and the configurations don't match up. Retaining stateful configuration settings on what has to be one of the primary sources used in OBS should be at the very top of the bug list in my opinion. Anyone who uses OBS on Linux with more than 1 V4L2 video device is affected by this.

@DylanRLive

This comment was marked as spam.

@majenkotech

majenkotech commented Apr 2, 2024 •

Copy link
Copy Markdown

I have been suffering from this stupid problem which could have been fixed years ago for a long long time. So much so that I have written a small PHP script (sorry it's PHP, but I'm a bit strange like that...) which will edit the OBS configuration file for a set of scenes and replace any /dev/video* entries with their /dev/v4l/by-id/ equivalents. This locks the devices to the right paths and tries to keep it working across reboots. It's not perfect, because you have to run it any time you make changes to your setup to re-bind the paths, but it's better than the apathy that the OBS devs seem to have towards us Linux users with more than one camera.

#!/usr/bin/php
<?php

$file = @$argv[1];
if (!$file) {
	print "Usage: fixvid <json file>\n";
	exit(10);
}

if (!file_exists($file)) {
	printf("fixvid: Unable to open $file for reading\n");
	exit(10);
}

$devices = array();

$dir = opendir("/dev/v4l/by-id");
while ($link = readdir($dir)) {
	if (substr($link, 0, 1) == ".") continue;
	$fullpath = "/dev/v4l/by-id/" . $link;
	$l = $fullpath;
	$target = readlink($l);
	$target = str_replace("../../", "/dev/", $target);
	$devices[$target] = "/dev/v4l/by-id/" . $link;
}

$json = json_decode(file_get_contents($file), false);

foreach ($json->sources as $num=>$source) {
	if ($source->id == "v4l2_input") {
		$dev = $source->settings->device_id;
		if (array_key_exists($dev, $devices)) {
			$json->sources[$num]->settings->device_id = $devices[$dev];
		}
	}
}

copy($file, $file . ".bak");
file_put_contents($file, json_encode($json, JSON_PRETTY_PRINT));

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

Labels

kind/bug Categorizes issue or PR as related to a bug.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Webcam ordering is not persistent in Linux