mirror of
https://github.com/pion/mediadevices.git
synced 2025-10-10 19:10:16 +08:00
Add test to validate Linux camera label rule
This commit is contained in:
@@ -73,42 +73,40 @@ type camera struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
discovered := make(map[string]struct{})
|
discovered := make(map[string]struct{})
|
||||||
|
discover(discovered, "/dev/v4l/by-path/*")
|
||||||
|
discover(discovered, "/dev/video*")
|
||||||
|
}
|
||||||
|
|
||||||
discover := func(pattern string) {
|
func discover(discovered map[string]struct{}, pattern string) {
|
||||||
devices, err := filepath.Glob(pattern)
|
devices, err := filepath.Glob(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// No v4l device.
|
// No v4l device.
|
||||||
return
|
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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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/*")
|
discovered[reallink] = struct{}{}
|
||||||
discover("/dev/video*")
|
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 {
|
func newCamera(path string) *camera {
|
||||||
|
62
pkg/driver/camera/camera_linux_test.go
Normal file
62
pkg/driver/camera/camera_linux_test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user