From 210ae73aa727bcb0c07bfb7a5863aff7f3829583 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 27 Jun 2023 16:09:07 -0700 Subject: [PATCH] add second function call --- README.md | 3 +-- vidnumerator.go | 27 ++++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 52a6753..845e6e6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # VidNumerator -This is a tiny library that only exports one function: `EnumerateVideoDevices`. -This function uses syscalls to efficiently determine which `/dev/videoN` devices +This is a tiny library that uses syscalls to efficiently determine which `/dev/videoN` devices are webcams and which are the additional metadata control handles. The list of strings returned are the full filepaths to valid devices. diff --git a/vidnumerator.go b/vidnumerator.go index 0312b89..8133ac9 100644 --- a/vidnumerator.go +++ b/vidnumerator.go @@ -56,6 +56,21 @@ func (r *cap) QueryFd(fileDesciptor int) error { return nil } +// this function checks the ioctl for VIDIOC_QUERYCAP to see if the device is a video capture device +func IsVideoCapture(path string) (bool, error) { + f, err := os.OpenFile(path, os.O_RDONLY, 0o755) + if err != nil { + return false, err + } + fd := f.Fd() + ic := cap{} + err = ic.QueryFd(int(fd)) + if err != nil { + return false, err + } + return ic.deviceCaps == 69206017, nil +} + // this function checks the ioctl for VIDIOC_QUERYCAP to see if the device is a video capture device func EnumeratedVideoDevices() []string { // list all files in the /dev directory @@ -74,17 +89,7 @@ func EnumeratedVideoDevices() []string { continue } fname = filepath.Join("/dev/", fname) - f, err := os.OpenFile(fname, os.O_RDONLY, 0o755) - if err != nil { - continue - } - fd := f.Fd() - ic := cap{} - err = ic.QueryFd(int(fd)) - if err != nil { - continue - } - if ic.deviceCaps == 69206017 { + if isVidCap, _ := IsVideoCapture(fname); isVidCap { devNames = append(devNames, fname) } }