Fix undiscovered some camera devices

/dev/v4l/by-path doesn't return all available devices. So, to make sure
that we include all available devices, the list of devices will also
complement with /dev/video*.
This commit is contained in:
Lukas Herman
2020-10-26 12:18:23 -07:00
parent 0b6549eb8f
commit 2022a4b7f7
+33 -13
View File
@@ -8,7 +8,8 @@ import (
"errors"
"image"
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/blackjack/webcam"
@@ -40,19 +41,38 @@ type camera struct {
}
func init() {
searchPath := "/dev/v4l/by-path/"
devices, err := ioutil.ReadDir(searchPath)
if err != nil {
// No v4l device.
return
}
for _, device := range devices {
cam := newCamera(searchPath + device.Name())
driver.GetManager().Register(cam, driver.Info{
Label: device.Name(),
DeviceType: driver.Camera,
})
discovered := make(map[string]struct{})
discover := func(pattern string) {
devices, err := filepath.Glob(pattern)
if err != nil {
// No v4l device.
return
}
for _, device := range devices {
label := filepath.Base(device)
reallink, err := os.Readlink(device)
if err != nil {
reallink = label
} else {
reallink = filepath.Base(reallink)
}
if _, ok := discovered[reallink]; ok {
continue
}
discovered[reallink] = struct{}{}
cam := newCamera(device)
driver.GetManager().Register(cam, driver.Info{
Label: label,
DeviceType: driver.Camera,
})
}
}
discover("/dev/v4l/by-path/*")
discover("/dev/video*")
}
func newCamera(path string) *camera {