do not change other classes

This commit is contained in:
Clyde Bazile
2023-04-21 18:11:09 -04:00
committed by Clyde Bazile
parent f0f6be7350
commit 8568b1b20d
6 changed files with 142 additions and 5 deletions
+35
View File
@@ -6,12 +6,14 @@ import "C"
import (
"context"
"errors"
"github.com/pion/mediadevices/pkg/driver/availability"
"image"
"io"
"os"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"
"github.com/blackjack/webcam"
@@ -350,3 +352,36 @@ func (c *camera) Properties() []prop.Media {
}
return properties
}
func (c *camera) IsAvailable() (bool, availability.Error) {
var err error
// close the opened file descriptor as quickly as possible and in all cases, including panics
func() {
var cam *webcam.Webcam
if cam, err = webcam.Open(c.path); err == nil {
defer cam.Close()
var index int32
// "Drivers must implement all the input ioctls when the device has one or more inputs..."
// Source: https://www.kernel.org/doc/html/latest/userspace-api/media/v4l/video.html?highlight=vidioc_enuminput
if index, err = cam.GetInput(); err == nil {
err = cam.SelectInput(uint32(index))
}
}
}()
var errno syscall.Errno
errors.As(err, &errno)
// See https://man7.org/linux/man-pages/man3/errno.3.html
switch {
case err == nil:
return true, nil
case errno == syscall.EBUSY:
return false, availability.ErrBusy
case errno == syscall.ENODEV || errno == syscall.ENOENT:
return false, availability.ErrNoDevice
default:
return false, availability.NewError(errno.Error())
}
}