constraints

package
v0.1.0-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package constraints solves MediaStreamConstraints against what the capture providers can really do.

It follows the SelectSettings algorithm of the Media Capture spec: hard constraints (min, max, exact) remove candidates, "advanced" sets are applied in order when they leave something, and among the survivors the candidate with the smallest fitness distance to the "ideal" values wins. The result is always a mode the device really has, so MediaStreamTrack.getSettings() reports what the camera delivers rather than what was asked.

The package has no dependency on the engine or on any device API; the engine feeds it JSON-decoded constraints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AudioDevice

type AudioDevice struct {
	ID, GroupID               string
	Rates                     []int
	MinChannels, MaxChannels  int
	DefaultRate, DefaultChans int
}

AudioDevice is one microphone as the solver sees it.

type AudioResult

type AudioResult struct {
	Device   int
	Rate     int
	Channels int
	// the voice processing of the track (it runs on the WebRTC send path, internal/rtcmedia/audioproc)
	EchoCancellation, AutoGainControl, NoiseSuppression bool
}

AudioResult is the winning combination.

func SolveAudio

func SolveAudio(c Constraints, devs []AudioDevice) (AudioResult, error)

SolveAudio picks the microphone. Echo cancellation, noise suppression and gain control are each available on and off; as in Chrome they are on unless the page says otherwise (the candidates with everything on come first).

type Bool

type Bool struct{ Exact, Ideal *bool }

Bool is a ConstrainBoolean.

type Constraints

type Constraints struct {
	Set
	Advanced []Set
}

Constraints is a parsed MediaTrackConstraints.

func Parse

func Parse(v any) (Constraints, error)

Parse reads a JSON-decoded MediaTrackConstraints: true (or nil) means "no constraints", an object is read key by key, unknown keys are ignored like browsers do.

type DisplayResult

type DisplayResult struct {
	W, H int
	FPS  float64
}

DisplayResult is the size and rate a display source should deliver.

func SolveDisplay

func SolveDisplay(c Constraints, w, h int) (DisplayResult, error)

SolveDisplay applies width/height/frameRate to a surface of native size w x h. Dimensions are upper bounds: the surface is scaled down (never up) to fit, keeping its aspect ratio. frameRate defaults to 30 and is capped at 60.

type Mode

type Mode struct {
	W, H           int
	FPSMin, FPSMax float64
}

Mode is a resolution with the frame-rate range available at it.

type Num

type Num struct{ Min, Max, Exact, Ideal *float64 }

Num is a ConstrainDouble: a plain number is an ideal.

type Overconstrained

type Overconstrained struct{ Constraint string }

Overconstrained is returned when no device/mode satisfies the hard constraints. Constraint names the property that removed the last candidates.

func (*Overconstrained) Error

func (e *Overconstrained) Error() string

type Set

type Set struct {
	Width, Height, FrameRate, AspectRatio Num
	FacingMode, ResizeMode                Str
	DeviceID, GroupID                     Str
	DisplaySurface                        Str

	SampleRate, SampleSize, ChannelCount, Latency Num
	EchoCancellation, AutoGainControl             Bool
	NoiseSuppression                              Bool
}

Set is one constraint dictionary (the basic set or one "advanced" entry).

type Str

type Str struct{ Exact, Ideal []string }

Str is a ConstrainDOMString.

type TypeError

type TypeError struct{ Msg string }

TypeError is returned for malformed constraints; the engine turns it into a JS TypeError.

func (*TypeError) Error

func (e *TypeError) Error() string

type VideoDevice

type VideoDevice struct {
	ID, GroupID string
	Facing      string // "user", "environment" or "" (a laptop webcam: treated as "user")
	Modes       []Mode
}

VideoDevice is one camera as the solver sees it. ID and GroupID are the identifiers the page sees (already hashed per origin); the caller maps the result back with Device.

type VideoResult

type VideoResult struct {
	Device int
	Mode   int
	W, H   int
	FPS    float64
	Facing string
}

VideoResult is the winning combination.

func SolveVideo

func SolveVideo(c Constraints, devs []VideoDevice) (VideoResult, error)

SolveVideo picks the device and mode for a video request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL