diff --git a/pkg/driver/camera/camera_linux.go b/pkg/driver/camera/camera_linux.go index ec8dded..9e0f375 100644 --- a/pkg/driver/camera/camera_linux.go +++ b/pkg/driver/camera/camera_linux.go @@ -73,42 +73,40 @@ type camera struct { func init() { discovered := make(map[string]struct{}) + discover(discovered, "/dev/v4l/by-path/*") + discover(discovered, "/dev/video*") +} - 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) - priority := driver.PriorityNormal - if reallink == prioritizedDevice { - priority = driver.PriorityHigh - } - driver.GetManager().Register(cam, driver.Info{ - Label: label + LabelSeparator + reallink, - DeviceType: driver.Camera, - Priority: priority, - }) - } +func discover(discovered map[string]struct{}, 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 + } - discover("/dev/v4l/by-path/*") - discover("/dev/video*") + discovered[reallink] = struct{}{} + cam := newCamera(device) + priority := driver.PriorityNormal + if reallink == prioritizedDevice { + priority = driver.PriorityHigh + } + driver.GetManager().Register(cam, driver.Info{ + Label: label + LabelSeparator + reallink, + DeviceType: driver.Camera, + Priority: priority, + }) + } } func newCamera(path string) *camera { diff --git a/pkg/driver/camera/camera_linux_test.go b/pkg/driver/camera/camera_linux_test.go new file mode 100644 index 0000000..57ed419 --- /dev/null +++ b/pkg/driver/camera/camera_linux_test.go @@ -0,0 +1,62 @@ +package camera + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/pion/mediadevices/pkg/driver" +) + +func TestDiscover(t *testing.T) { + const ( + shortName = "video0" + shortName2 = "video1" + longName = "long-device-name:0:1:2:3" + ) + + dir, err := ioutil.TempDir("", "") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(dir) + + byPathDir := filepath.Join(dir, "v4l", "by-path") + if err := os.MkdirAll(byPathDir, 0755); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(filepath.Join(dir, shortName), []byte{}, 0644); err != nil { + t.Fatal(err) + } + if err := ioutil.WriteFile(filepath.Join(dir, shortName2), []byte{}, 0644); err != nil { + t.Fatal(err) + } + if err := os.Symlink( + filepath.Join(dir, shortName), + filepath.Join(byPathDir, longName), + ); err != nil { + t.Fatal(err) + } + + discovered := make(map[string]struct{}) + discover(discovered, filepath.Join(byPathDir, "*")) + discover(discovered, filepath.Join(dir, "video*")) + + drvs := driver.GetManager().Query(func(d driver.Driver) bool { + return d.Info().DeviceType == driver.Camera + }) + if len(drvs) != 2 { + t.Fatalf("Expected 2 driver, got %d drivers", len(drvs)) + } + + expected := longName + LabelSeparator + shortName + if label := drvs[0].Info().Label; label != expected { + t.Errorf("Expected label: %s, got: %s", expected, label) + } + + expectedNoLink := shortName2 + LabelSeparator + shortName2 + if label := drvs[1].Info().Label; label != expectedNoLink { + t.Errorf("Expected label: %s, got: %s", expectedNoLink, label) + } +}