Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DATA-683] Default webcam selection sent to mediadevices #1591

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 19 additions & 34 deletions components/camera/videosource/webcam.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"image"
"path/filepath"
"regexp"
"strings"

"github.com/edaniels/golog"
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down