Skip to content

Commit

Permalink
Add test to validate Linux camera label rule
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat committed Mar 8, 2021
1 parent fcec5a9 commit dafd208
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 30 deletions.
58 changes: 28 additions & 30 deletions pkg/driver/camera/camera_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 {
// No v4l device.
return
reallink = label
} else {
reallink = filepath.Base(reallink)
}
if _, ok := discovered[reallink]; ok {
continue
}
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,
})
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,
})
}

discover("/dev/v4l/by-path/*")
discover("/dev/video*")
}

func newCamera(path string) *camera {
Expand Down
62 changes: 62 additions & 0 deletions pkg/driver/camera/camera_linux_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit dafd208

Please sign in to comment.