If you stream with OBS with a number of cameras on Linux, you may discover that cameras are loaded as they’re detected throughout boot. You most likely do not give it a lot thought, usually, however if in case you have a everlasting streaming setup with advanced OBS templates, that you must know which digital camera within the bodily world goes to point out up during which display within the digital one. In different phrases, you do not need to assign one machine as Camera A in the present day solely to have it find yourself as Camera B tomorrow.
To standardize a fancy digital camera setup, you possibly can impose some particular guidelines on how cameras get assigned to places within the Linux filesystem.
The udev subsystem
The system coping with {hardware} peripherals on Linux known as udev. It detects and manages all gadgets you plug into your pc. You’re most likely not conscious of it as a result of it would not draw an excessive amount of consideration to itself, though you have definitely interacted with it whenever you plug in a USB thumb drive to open in your desktop or connected a printer.
Hardware detection
Assume you have got two USB cameras: One on the left of your pc and one on the suitable. The left digital camera is capturing a close-up, the suitable digital camera is capturing a protracted shot, and you turn between the 2 throughout your stream. In OBS, you add every digital camera to your Sources panel and, intuitively, name one camLEFT and the opposite camRIGHT.
Assuming the worst-case state of affairs, say you have got two of the similar cameras: They’re the identical model and the identical mannequin quantity. This is the worst-case state of affairs as a result of when two items of {hardware} are an identical, there’s little likelihood every one has any sort of distinctive ID to your pc to distinguish them from each other.
There’s an answer to this puzzle, although, and it simply requires somewhat investigation utilizing some easy terminal instructions.
1. Get the seller and product IDs
First, plugin only one digital camera into the USB port you need it assigned to. Then difficulty this command:
$ lsusb
Bus 006 Device 002: ID 0951:1666 Kingston Technology DataTraveler G4
Bus 005 Device 003: ID 03f0:3817 Hewlett-Packard LaserJet P2015 collection
Bus 003 Device 006: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
Bus 003 Device 002: ID 8087:0025 Intel Corp.
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:c216 Logitech, Inc. Dual Action Gamepad
Bus 001 Device 002: ID 048d:5702 Integrated Technology Express, Inc.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
[...]
You can normally search particularly for the string “cam” to slim down the outcomes as a result of most (however not all) cameras report as a digital camera.
$ lsusb | grep -i cam
Bus 003 Device 006: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
There’s a number of data right here. The ID is listed as 045e:0779
. The first quantity is the seller ID, and the second is the product ID. Write these down since you’ll want them later.
2. Get the USB identifier
You’ve additionally obtained the machine path to the digital camera: bus 3, machine 6. There’s a saying in Linux that “everything is a file,” and certainly USB gadgets are described to udev as a file path beginning with /dev/bus/usb/
and ending with the bus (003 on this case) and the machine (006 on this case). Look on the bus and machine numbers within the lsusb
output. They inform you that this digital camera is situated on /dev/bus/usb/003/006
.
You can use the udevadm
command to acquire the kernel’s designator for this USB machine:
$ sudo udevadm data
--attribute-walk
/dev/bus/usb/003/006 | grep "KERNEL="KERNEL=="3-6.2.1"
The kernel USB identifier on this instance is 3-6.2.1
. Write down the identifier to your system since you’ll additionally want it later.
3. Repeat for every digital camera
Attach the opposite digital camera (or cameras, if in case you have greater than two) to the USB port you need it assigned to. This is completely different than the USB port you used for the opposite digital camera!
Repeat the method, acquiring the seller and product ID (if the cameras are the identical make and mannequin, these needs to be the identical as the primary one) and the kernel USB identifier.
$ lsusb | grep -i cam
Bus 001 Device 004: ID 045e:0779 Microsoft Corp. LifeCam HD-3000
$ sudo udevadm data
--attribute-walk
/dev/bus/usb/001/004 | grep "KERNEL="KERNEL=="1-6"
In this instance, I’ve decided that I’ve my cameras connected to 1-6 and 3-6.2.1 (the primary one is a USB port on my machine, the opposite one is a hub plugged into the monitor plugged into my machine, which is why one is extra advanced than the opposite.)
Write a udev rule
You have every little thing you want, so now you possibly can write a rule to inform udev to offer every digital camera a constant identifier when one is discovered at a particular USB port.
Create and open a file referred to as /and so on/udev/guidelines.d/50-camera.conf
, and enter these two guidelines, utilizing the seller and product IDs and kernel identifiers applicable to your personal system:
SUBSYSTEM=="usb", KERNEL=="1-6", ATTR{idVendor}=="045e", ATTR{idProduct}=="0779", SYMLINK+="video100"SUBSYSTEM=="usb", KERNEL=="3-6.2.1", ATTR{idVendor}=="045e", ATTR{idProduct}=="0779", SYMLINK+="video101"
These guidelines inform udev that when it finds a tool matching a particular vendor and product ID at these particular USB places, to create a symlink (typically additionally referred to as an “alias”) named video100
and video101
. The symlinks are largely arbitrary. I give them excessive numbers, in order that they’re simple to identify and since the quantity should not conflict with current gadgets. If you truly do have greater than 101 cameras connected to your pc, use video200
and video201
simply to be secure (and get involved! I’d like to study extra about that mission).
Reboot
Reboot your pc. You can depart the cameras connected for now, however it would not truly matter. Once udev has a rule loaded, it follows these guidelines whether or not a tool was connected throughout boot or is plugged in later.
Many folks say that Linux by no means must reboot, however udev masses its guidelines throughout boot, and apart from, you need to show that your udev guidelines are working throughout reboots.
Once your pc is again up and operating, have a look in /dev/video
, the place cameras are registered:
$ ls -1 /dev/video*
/dev/video0
/dev/video1
/dev/video100
/dev/video101
/dev/video2
/dev/video3
As you possibly can see, there are entries at video100
and video101
. Today, these are symlinks to /dev/video2
and /dev/video3
, however tomorrow they might be symlinks to /dev/video1
and /dev/video2
, or some other mixture primarily based on when Linux detected and assigned them a file.
You can use the symlinks in OBS, although, in order that camLEFT is at all times camLEFT, and camRIGHT is at all times camRIGHT.