Skip to content

Commit

Permalink
Add driver priority information
Browse files Browse the repository at this point in the history
Make system default device selected by default.
  • Loading branch information
at-wat authored and lherman-cs committed Feb 15, 2020
1 parent f3dce68 commit 53d55a3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mediadevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ func selectBestDriver(filter driver.FilterFn, constraints MediaTrackConstraints)

driverProperties := queryDriverProperties(filter)
for d, props := range driverProperties {
priority := float64(d.Info().Priority)
for _, p := range props {
fitnessDist := constraints.Media.FitnessDistance(p)
fitnessDist := constraints.Media.FitnessDistance(p) - priority
if fitnessDist < minFitnessDist {
minFitnessDist = fitnessDist
bestDriver = d
Expand Down
13 changes: 13 additions & 0 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ type AudioRecorder interface {
AudioRecord(p prop.Media) (r audio.Reader, err error)
}

// Priority represents device selection priority level
type Priority float32

const (
// PriorityHigh is a value for system default devices
PriorityHigh Priority = 0.1
// PriorityNormal is a value for normal devices
PriorityNormal Priority = 0.0
// PriorityLow is a value for unrecommended devices
PriorityLow Priority = -0.1
)

type Info struct {
Label string
DeviceType DeviceType
Priority Priority
}

type Adapter interface {
Expand Down
9 changes: 9 additions & 0 deletions pkg/driver/microphone/microphone_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ func init() {
if err != nil {
panic(err)
}
defaultSource, err := pa.DefaultSource()
if err != nil {
panic(err)
}
for _, source := range sources {
priority := driver.PriorityNormal
if defaultSource.ID() == source.ID() {
priority = driver.PriorityHigh
}
driver.GetManager().Register(&microphone{id: source.ID()}, driver.Info{
Label: source.ID(),
DeviceType: driver.Microphone,
Priority: priority,
})
}
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/driver/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (

func wrapAdapter(a Adapter, info Info) Driver {
id := uuid.NewV4().String()
d := &adapterWrapper{Adapter: a, id: id, info: info, state: StateClosed}
d := &adapterWrapper{
Adapter: a,
id: id,
info: info,
state: StateClosed,
}

switch v := a.(type) {
case VideoRecorder:
Expand Down

0 comments on commit 53d55a3

Please sign in to comment.