When finding cameras on linux systems, look at camera ID before path or video* #476
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Description
Currently, it is not possible to detect a camera that has moved from one USB port to another on a linux system. This is the case because the
camera_linux.go
file implementation first looks at the paths in/dev/v4l/by-path/*
and then in/dev/video*
when looking for a camera object. The problem is that when USB cameras are moved around between USB ports on the linux system, theby-path
andvideo*
addresses may change. If there is a reference to the camera path, and then the camera is physically moved to a different USB port, the old path no longer exists and the camera cannot be opened.For example, observe the naming schema in
/dev/v4l/by-path/*
for a camera mapped to/dev/video0
The USB port is part of the name. For comparison, here is the same camera moved to a different USB port
Note that the path has changed. When the camera is first connected to
video0
, the associated path is saved, and when the camera is unplugged and plugged back in to a different port, it may still be identified asvideo0
, however the associated path is no longer the same. If the path is identical, i.e., it has been plugged back into the same USB port, then there’s no issue. However, if it moves USB ports, the old path no longer exists so it cannot be opened.Solution
In order to allow cameras to be switched between USB ports and still be accessible, the cameras must be identified by their ID, as that will never change regardless of where the camera is plugged in. This is a very simple change of looking in
dev/v4l/by-id/*
beforeby-path
orvideo*
.Here is an example of a camera ID found in the newly created
dev/v4l/by-id
folder.And after the camera is moved to a different USB port, the path remains exactly the same.
This change will allow for more flexibility for users who want to adjust their physical set up, as well as hopefully decrease troubleshooting and reinitializing when users do change their physical set ups and run in to issues.
Testing
For manually testing, I attached two webcams to a Raspberry Pi and switched them around to different USB ports to observe their behavior. I tried every combination of the two webcams, and with this new implementation, the cameras will consistently reconnect, and the video stream will resume normally. Previously, the cameras would only reconnect and resume the video stream if they were plugged in to the same USB port. Additionally, I added a new test to the
camera_linux_test.go
file that tests the camera discoveriesby-id
in addition to the currently existing test that testsby-path