Skip to content

Commit dafd208

Browse files
committed
Add test to validate Linux camera label rule
1 parent fcec5a9 commit dafd208

File tree

2 files changed

+90
-30
lines changed

2 files changed

+90
-30
lines changed

pkg/driver/camera/camera_linux.go

+28-30
Original file line numberDiff line numberDiff line change
@@ -73,42 +73,40 @@ type camera struct {
7373

7474
func init() {
7575
discovered := make(map[string]struct{})
76+
discover(discovered, "/dev/v4l/by-path/*")
77+
discover(discovered, "/dev/video*")
78+
}
7679

77-
discover := func(pattern string) {
78-
devices, err := filepath.Glob(pattern)
80+
func discover(discovered map[string]struct{}, pattern string) {
81+
devices, err := filepath.Glob(pattern)
82+
if err != nil {
83+
// No v4l device.
84+
return
85+
}
86+
for _, device := range devices {
87+
label := filepath.Base(device)
88+
reallink, err := os.Readlink(device)
7989
if err != nil {
80-
// No v4l device.
81-
return
90+
reallink = label
91+
} else {
92+
reallink = filepath.Base(reallink)
93+
}
94+
if _, ok := discovered[reallink]; ok {
95+
continue
8296
}
83-
for _, device := range devices {
84-
label := filepath.Base(device)
85-
reallink, err := os.Readlink(device)
86-
if err != nil {
87-
reallink = label
88-
} else {
89-
reallink = filepath.Base(reallink)
90-
}
91-
92-
if _, ok := discovered[reallink]; ok {
93-
continue
94-
}
9597

96-
discovered[reallink] = struct{}{}
97-
cam := newCamera(device)
98-
priority := driver.PriorityNormal
99-
if reallink == prioritizedDevice {
100-
priority = driver.PriorityHigh
101-
}
102-
driver.GetManager().Register(cam, driver.Info{
103-
Label: label + LabelSeparator + reallink,
104-
DeviceType: driver.Camera,
105-
Priority: priority,
106-
})
98+
discovered[reallink] = struct{}{}
99+
cam := newCamera(device)
100+
priority := driver.PriorityNormal
101+
if reallink == prioritizedDevice {
102+
priority = driver.PriorityHigh
107103
}
104+
driver.GetManager().Register(cam, driver.Info{
105+
Label: label + LabelSeparator + reallink,
106+
DeviceType: driver.Camera,
107+
Priority: priority,
108+
})
108109
}
109-
110-
discover("/dev/v4l/by-path/*")
111-
discover("/dev/video*")
112110
}
113111

114112
func newCamera(path string) *camera {
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package camera
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/pion/mediadevices/pkg/driver"
10+
)
11+
12+
func TestDiscover(t *testing.T) {
13+
const (
14+
shortName = "video0"
15+
shortName2 = "video1"
16+
longName = "long-device-name:0:1:2:3"
17+
)
18+
19+
dir, err := ioutil.TempDir("", "")
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
defer os.RemoveAll(dir)
24+
25+
byPathDir := filepath.Join(dir, "v4l", "by-path")
26+
if err := os.MkdirAll(byPathDir, 0755); err != nil {
27+
t.Fatal(err)
28+
}
29+
if err := ioutil.WriteFile(filepath.Join(dir, shortName), []byte{}, 0644); err != nil {
30+
t.Fatal(err)
31+
}
32+
if err := ioutil.WriteFile(filepath.Join(dir, shortName2), []byte{}, 0644); err != nil {
33+
t.Fatal(err)
34+
}
35+
if err := os.Symlink(
36+
filepath.Join(dir, shortName),
37+
filepath.Join(byPathDir, longName),
38+
); err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
discovered := make(map[string]struct{})
43+
discover(discovered, filepath.Join(byPathDir, "*"))
44+
discover(discovered, filepath.Join(dir, "video*"))
45+
46+
drvs := driver.GetManager().Query(func(d driver.Driver) bool {
47+
return d.Info().DeviceType == driver.Camera
48+
})
49+
if len(drvs) != 2 {
50+
t.Fatalf("Expected 2 driver, got %d drivers", len(drvs))
51+
}
52+
53+
expected := longName + LabelSeparator + shortName
54+
if label := drvs[0].Info().Label; label != expected {
55+
t.Errorf("Expected label: %s, got: %s", expected, label)
56+
}
57+
58+
expectedNoLink := shortName2 + LabelSeparator + shortName2
59+
if label := drvs[1].Info().Label; label != expectedNoLink {
60+
t.Errorf("Expected label: %s, got: %s", expectedNoLink, label)
61+
}
62+
}

0 commit comments

Comments
 (0)