diff --git a/components/camera/videosource/webcam.go b/components/camera/videosource/webcam.go index 2a1b9d05bae..e5fffb22c45 100644 --- a/components/camera/videosource/webcam.go +++ b/components/camera/videosource/webcam.go @@ -4,7 +4,6 @@ import ( "context" "image" "path/filepath" - "regexp" "strings" "github.com/edaniels/golog" @@ -206,43 +205,16 @@ func NewWebcamSource(ctx context.Context, attrs *WebcamAttrs, logger golog.Logge return tryWebcamOpen(ctx, attrs, attrs.Path, false, constraints, logger) } - var pattern *regexp.Regexp - if attrs.PathPattern != "" { - pattern, err = regexp.Compile(attrs.PathPattern) - if err != nil { - return nil, err - } - } - - labels := gostream.QueryVideoDeviceLabels() - for _, label := range labels { - if debug { - logger.Debugf("%s", label) - } - - if pattern != nil && !pattern.MatchString(label) { - if debug { - logger.Debug("\t skipping because of pattern") - } - continue - } - - s, err := tryWebcamOpen(ctx, attrs, label, true, constraints, logger) - if err == nil { - if debug { - logger.Debug("\t USING") - } - - return s, nil - } - if debug { - logger.Debugf("\t %w", err) - } + source, err := gostream.GetAnyVideoSource(constraints, logger) + if err != nil { + return nil, errors.Wrapf(err, "found no webcams") } - return nil, errors.New("found no webcams") + return makeCameraFromSource(ctx, source, attrs) } +// tryWebcamOpen uses getNamedVideoSource to try and find a video device (gostream.MediaSource). +// If successful, it will wrap that MediaSource in a camera. func tryWebcamOpen(ctx context.Context, attrs *WebcamAttrs, path string, @@ -254,6 +226,19 @@ func tryWebcamOpen(ctx context.Context, if err != nil { return nil, err } + return makeCameraFromSource(ctx, source, attrs) +} + +// makeCameraFromSource takes a gostream.MediaSource and wraps it so that the return +// is an RDK camera object. +func makeCameraFromSource(ctx context.Context, + source gostream.MediaSource[image.Image], + attrs *WebcamAttrs, +) (camera.Camera, error) { + if source == nil { + return nil, errors.New("media source not found") + } + return camera.NewFromSource( ctx, source,