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

Attempt to separate webcam and gostream entirely #4604

Draft
wants to merge 64 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
a643d22
Init craziness
hexbabe Oct 24, 2024
41cb592
Use camera pkg scoped ReadImage in webcam
hexbabe Oct 25, 2024
f6e3d69
Merge branch 'main' into RSDK-9132
hexbabe Nov 4, 2024
d6439dd
Use agreed upon Image signature
hexbabe Nov 6, 2024
6417a56
Merge branch 'main' into RSDK-9132
hexbabe Nov 6, 2024
59c36ec
Fix tests
hexbabe Nov 6, 2024
16079fa
Delete ReadImager and fix mimetype formatting in data collector
hexbabe Nov 6, 2024
9084264
Fix up obstacle depth; Delete custom extra type;
hexbabe Nov 7, 2024
c44afa2
Update video source comment spec; Add helper to DRY up .Image calls w…
hexbabe Nov 7, 2024
11b1d7d
Fix obstacle depth mimetype (it needs it for re-encode since we can't…
hexbabe Nov 7, 2024
438d550
Add image metadata replacing mimetype return; Add back non empty stri…
hexbabe Nov 7, 2024
0d8081b
Forgot to include mimetype in test resp oops
hexbabe Nov 7, 2024
824c30f
Add width and height to ImageMetadata struct
hexbabe Nov 8, 2024
e744b68
Use GetGoImage in camera client Stream
hexbabe Nov 8, 2024
fd50881
Remove jpeg default in vision
hexbabe Nov 11, 2024
e570393
Revert detections, classifications, and capture all's image call to R…
hexbabe Nov 11, 2024
6646d78
Don't modify vision at all
hexbabe Nov 12, 2024
612e91c
Remove width and height from ImageMetadata; DRY up webcam and videoso…
hexbabe Nov 12, 2024
9029a05
Move ReadImageBytes to videosourcewrappers
hexbabe Nov 13, 2024
6ec0041
Rename GetGoImage -> ImageFromVideoSource
hexbabe Nov 13, 2024
ef1bd0e
Make lint
hexbabe Nov 13, 2024
d01159a
Move mimetype unmarshaling outside of capture func
hexbabe Nov 13, 2024
44611d5
Update components/camera/client.go
hexbabe Nov 13, 2024
146345f
Add error for empty bytes responses in server & client
hexbabe Nov 14, 2024
c698e16
Add empty image bytes tests
hexbabe Nov 14, 2024
9da582f
Fix random things that I am doing wrong
hexbabe Nov 14, 2024
1b51109
Revert
hexbabe Nov 14, 2024
eaf28d7
RSDK-9218: Change all of the artifacts to v4. (#4535)
dgottlieb Nov 6, 2024
4fe7e36
RSDK-8359: Update smarty to avoid unnecessary data races. (#4534)
dgottlieb Nov 6, 2024
b4e1960
RSDK-8819: Implement FTDC file rotation. (#4510)
dgottlieb Nov 6, 2024
fc6665d
APP-6785: Remove local control page - remove web workflows (#4523)
ethanlookpotts Nov 7, 2024
8afb714
APP-6850 update android build for go 1.23 (#4539)
abe-winter Nov 8, 2024
375a35f
RSDK-8611: Have test make a tls config copy to avoid concurrent acces…
dgottlieb Nov 8, 2024
5bf744c
RSDK-8837: Remove the short timeout for testing reconnects that are e…
dgottlieb Nov 8, 2024
2a1cd8b
make errors reported during discovery (#4546)
erh Nov 9, 2024
1233170
Update gripper.go (#4540)
npentrel Nov 11, 2024
95f3f42
RSDK-9090 clarify package cleanup (#4542)
maximpertsov Nov 11, 2024
b208d2c
RSDK-9240: Remove github action code coverage stuff we do not use. (#…
dgottlieb Nov 11, 2024
0b85975
[Data-3312] add basic data capture support for capturing tabular data…
nicksanford Nov 11, 2024
c44454b
RSDK-9136: issues when stopping motor with controls (#4550)
martha-johnston Nov 12, 2024
d848d20
RSDK-9037: Add AttachDirectionalAwareness to DoCommand (#4552)
martha-johnston Nov 13, 2024
09e295c
RSDK-8767 - populate local_name and remote_path to all resource names…
Kschappacher Nov 13, 2024
3edf860
APP-6696 include `os_version` tag on GOOS=darwin (#4536)
abe-winter Nov 13, 2024
b7c4635
RSDK-8926: Rover canary motor test fail because failure to set pins (…
martha-johnston Nov 14, 2024
63b7681
[DATA-3338] - collector test improvements (#4551)
nicksanford Nov 14, 2024
c91b68d
Merge branch 'main' into RSDK-9132
hexbabe Nov 14, 2024
d63980e
Fix new collector tests
hexbabe Nov 14, 2024
6ffbae2
Change image.Image getter wrapper to take in a camera resource not a …
hexbabe Nov 14, 2024
65b5bad
Merge branch 'viamrobotics:main' into RSDK-9132
hexbabe Nov 14, 2024
9a9612f
If err, then bytes and metadata should be falsy
hexbabe Nov 14, 2024
ab7e736
Use imgbuf bytes directly for a robot client test rather than encode …
hexbabe Nov 14, 2024
8244dfd
Add back ReadImage/Stream tests where it makes sense
hexbabe Nov 15, 2024
5feb0ed
Getting started with the refactor
hexbabe Nov 20, 2024
5c0191d
Remove debugging crap
hexbabe Nov 22, 2024
a1fcc2f
Fix videosourcewrappers
hexbabe Nov 25, 2024
859d027
Merge branch 'main' into RSDK-9132-beta-timeline
hexbabe Dec 3, 2024
de2cb6a
Try to remove Stream
hexbabe Dec 3, 2024
21fe63a
Merge branch 'main' into RSDK-9132-beta-timeline
hexbabe Dec 3, 2024
2fdabbf
Make lint
hexbabe Dec 3, 2024
f3e9fd5
Align comments
hexbabe Dec 4, 2024
b27c121
Merge branch 'main' into RSDK-9132-beta-timeline
hexbabe Dec 4, 2024
4a8ad4a
Delete some Stream documentations; Default to jpeg encoding in webcam…
hexbabe Dec 4, 2024
5ecc6ed
Change streamcamera comment; Add more exclamation points; Change came…
hexbabe Dec 4, 2024
46059cd
Add initial webcam/gostream refactor
hexbabe Dec 5, 2024
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
225 changes: 225 additions & 0 deletions components/camera/videosource/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package videosource

import (
"math"
"strings"
"time"

"github.com/pion/mediadevices"
"github.com/pion/mediadevices/pkg/driver"
"github.com/pion/mediadevices/pkg/driver/availability"
"github.com/pion/mediadevices/pkg/driver/camera"
"github.com/pion/mediadevices/pkg/io/video"
"github.com/pion/mediadevices/pkg/prop"
"github.com/pkg/errors"

"go.viam.com/rdk/logging"
)

// Below is adapted from github.com/pion/mediadevices.
// It is further adapted from gostream's query.go
// However, this is the minimum code needed for webcam to work, placed in this directory.
// This vastly improves the debugging and feature development experience, by not over-DRY-ing.

// GetNamedVideoSource attempts to find a device (not a screen) by the given name.
// If name is empty, it finds any device.
func getReaderAndDriver(
name string,
constraints mediadevices.MediaStreamConstraints,
logger logging.Logger,
) (video.Reader, driver.Driver, error) {
var ptr *string
if name == "" {
ptr = nil
} else {
ptr = &name
}
d, selectedMedia, err := getUserVideoDriver(constraints, ptr, logger)
if err != nil {
return nil, nil, err
}
reader, err := newReaderFromDriver(d, selectedMedia)
if err != nil {
return nil, nil, err
}
return reader, d, nil
}

func getUserVideoDriver(
constraints mediadevices.MediaStreamConstraints,
label *string,
logger logging.Logger,
) (driver.Driver, prop.Media, error) {
var videoConstraints mediadevices.MediaTrackConstraints
if constraints.Video != nil {
constraints.Video(&videoConstraints)
}
return selectVideo(videoConstraints, label, logger)
}

func newReaderFromDriver(
videoDriver driver.Driver,
mediaProp prop.Media,
) (video.Reader, error) {
recorder, ok := videoDriver.(driver.VideoRecorder)
if !ok {
return nil, errors.New("driver not a driver.VideoRecorder")
}

if ok, err := driver.IsAvailable(videoDriver); !errors.Is(err, availability.ErrUnimplemented) && !ok {
return nil, errors.Wrap(err, "video driver not available")
} else if driverStatus := videoDriver.Status(); driverStatus != driver.StateClosed {
return nil, errors.New("video driver in use")
} else if err := videoDriver.Open(); err != nil {
return nil, errors.Wrap(err, "cannot open video driver")
}

mediaProp.DiscardFramesOlderThan = time.Second
reader, err := recorder.VideoRecord(mediaProp)
if err != nil {
return nil, err
}
return reader, nil
}

func labelFilter(target string, useSep bool) driver.FilterFn {
return driver.FilterFn(func(d driver.Driver) bool {
if !useSep {
return d.Info().Label == target
}
labels := strings.Split(d.Info().Label, camera.LabelSeparator)
for _, label := range labels {
if label == target {
return true
}
}
return false
})
}

func selectVideo(
constraints mediadevices.MediaTrackConstraints,
label *string,
logger logging.Logger,
) (driver.Driver, prop.Media, error) {
return selectBestDriver(getVideoFilterBase(), getVideoFilter(label), constraints, logger)
}

func getVideoFilterBase() driver.FilterFn {
typeFilter := driver.FilterVideoRecorder()
notScreenFilter := driver.FilterNot(driver.FilterDeviceType(driver.Screen))
return driver.FilterAnd(typeFilter, notScreenFilter)
}

func getVideoFilter(label *string) driver.FilterFn {
filter := getVideoFilterBase()
if label != nil {
filter = driver.FilterAnd(filter, labelFilter(*label, true))
}
return filter
}

// select implements SelectSettings algorithm.
// Reference: https://w3c.github.io/mediacapture-main/#dfn-selectsettings
func selectBestDriver(
baseFilter driver.FilterFn,
filter driver.FilterFn,
constraints mediadevices.MediaTrackConstraints,
logger logging.Logger,
) (driver.Driver, prop.Media, error) {
var bestDriver driver.Driver
var bestProp prop.Media
minFitnessDist := math.Inf(1)

baseDrivers := driver.GetManager().Query(baseFilter)
logger.Debugw("before specific filter, we found the following drivers", "count", len(baseDrivers))
for _, d := range baseDrivers {
logger.Debugw(d.Info().Label, "priority", float32(d.Info().Priority), "type", d.Info().DeviceType)
}

driverProperties := queryDriverProperties(filter, logger)
if len(driverProperties) == 0 {
logger.Debugw("found no drivers matching filter")
} else {
logger.Debugw("found drivers matching specific filter", "count", len(driverProperties))
}
for d, props := range driverProperties {
priority := float64(d.Info().Priority)
logger.Debugw(
"considering driver",
"label", d.Info().Label,
"priority", priority)
for _, p := range props {
fitnessDist, ok := constraints.MediaConstraints.FitnessDistance(p)
if !ok {
logger.Debugw("driver does not satisfy any constraints", "label", d.Info().Label)
continue
}
fitnessDistWithPriority := fitnessDist - priority
logger.Debugw(
"driver properties satisfy some constraints",
"label", d.Info().Label,
"props", p,
"distance", fitnessDist,
"distance_with_priority", fitnessDistWithPriority)
if fitnessDistWithPriority < minFitnessDist {
minFitnessDist = fitnessDistWithPriority
bestDriver = d
bestProp = p
}
}
}

if bestDriver == nil {
return nil, prop.Media{}, errors.New("failed to find the best driver that fits the constraints")
}

logger.Debugw("winning driver", "label", bestDriver.Info().Label, "props", bestProp)
selectedMedia := prop.Media{}
selectedMedia.MergeConstraints(constraints.MediaConstraints)
selectedMedia.Merge(bestProp)
return bestDriver, selectedMedia, nil
}

func queryDriverProperties(
filter driver.FilterFn,
logger logging.Logger,
) map[driver.Driver][]prop.Media {
var needToClose []driver.Driver
drivers := driver.GetManager().Query(filter)
m := make(map[driver.Driver][]prop.Media)

for _, d := range drivers {
var status string
isAvailable, err := driver.IsAvailable(d)
if errors.Is(err, availability.ErrUnimplemented) {
s := d.Status()
status = string(s)
isAvailable = s == driver.StateClosed
} else if err != nil {
status = err.Error()
}

if isAvailable {
err := d.Open()
if err != nil {
logger.Debugw("error opening driver for querying", "error", err)
// Skip this driver if we failed to open because we can't get the properties
continue
}
needToClose = append(needToClose, d)
m[d] = d.Properties()
} else {
logger.Debugw("driver not available", "name", d.Info().Name, "label", d.Info().Label, "status", status)
}
}

for _, d := range needToClose {
// Since it was closed, we should close it to avoid a leak
if err := d.Close(); err != nil {
logger.Errorw("error closing driver", "error", err)
}
}

return m
}
Loading
Loading