mirror of
https://github.com/pion/mediadevices.git
synced 2025-10-09 18:40:13 +08:00
Add test to validate Linux camera label rule
This commit is contained in:
@@ -73,8 +73,11 @@ 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.
|
||||||
@@ -88,7 +91,6 @@ func init() {
|
|||||||
} else {
|
} else {
|
||||||
reallink = filepath.Base(reallink)
|
reallink = filepath.Base(reallink)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := discovered[reallink]; ok {
|
if _, ok := discovered[reallink]; ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -107,10 +109,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
discover("/dev/v4l/by-path/*")
|
|
||||||
discover("/dev/video*")
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCamera(path string) *camera {
|
func newCamera(path string) *camera {
|
||||||
formats := map[webcam.PixelFormat]frame.Format{
|
formats := map[webcam.PixelFormat]frame.Format{
|
||||||
webcam.PixelFormat(C.V4L2_PIX_FMT_YUV420): frame.FormatI420,
|
webcam.PixelFormat(C.V4L2_PIX_FMT_YUV420): frame.FormatI420,
|
||||||
|
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