mirror of
https://github.com/pion/mediadevices.git
synced 2025-10-22 00:01:58 +08:00
Remove Kind in favor of type assertion
This commit is contained in:
@@ -60,8 +60,7 @@ func (m *mediaDevices) GetUserMedia(constraints MediaStreamConstraints) (MediaSt
|
||||
// videoSelect implements SelectSettings algorithm for video type.
|
||||
// Reference: https://w3c.github.io/mediacapture-main/#dfn-selectsettings
|
||||
func (m *mediaDevices) videoSelect(constraints VideoTrackConstraints) (Tracker, error) {
|
||||
videoFilterFn := driver.FilterKind(driver.Video)
|
||||
drivers := driver.GetManager().Query(videoFilterFn)
|
||||
drivers := driver.GetManager().VideoDrivers()
|
||||
|
||||
var bestDriver driver.VideoDriver
|
||||
var bestSetting driver.VideoSetting
|
||||
@@ -111,8 +110,7 @@ func (m *mediaDevices) videoSelect(constraints VideoTrackConstraints) (Tracker,
|
||||
// audioSelect implements SelectSettings algorithm for audio type.
|
||||
// Reference: https://w3c.github.io/mediacapture-main/#dfn-selectsettings
|
||||
func (m *mediaDevices) audioSelect(constraints AudioTrackConstraints) (Tracker, error) {
|
||||
audioFilterFn := driver.FilterKind(driver.Audio)
|
||||
drivers := driver.GetManager().Query(audioFilterFn)
|
||||
drivers := driver.GetManager().AudioDrivers()
|
||||
|
||||
var bestDriver driver.AudioDriver
|
||||
var bestSetting driver.AudioSetting
|
||||
|
@@ -15,11 +15,11 @@ import (
|
||||
// Camera implementation using v4l2
|
||||
// Reference: https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/videodev.html#videodev
|
||||
type camera struct {
|
||||
path string
|
||||
cam *webcam.Webcam
|
||||
formats map[webcam.PixelFormat]frame.Format
|
||||
path string
|
||||
cam *webcam.Webcam
|
||||
formats map[webcam.PixelFormat]frame.Format
|
||||
reversedFormats map[frame.Format]webcam.PixelFormat
|
||||
settings []VideoSetting
|
||||
settings []VideoSetting
|
||||
}
|
||||
|
||||
var _ VideoAdapter = &camera{}
|
||||
@@ -34,8 +34,8 @@ func init() {
|
||||
|
||||
func newCamera(path string) *camera {
|
||||
formats := map[webcam.PixelFormat]frame.Format{
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_YUYV): frame.FormatYUYV,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_NV12): frame.FormatNV21,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_YUYV): frame.FormatYUYV,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_NV12): frame.FormatNV21,
|
||||
webcam.PixelFormat(C.V4L2_PIX_FMT_MJPEG): frame.FormatMJPEG,
|
||||
}
|
||||
|
||||
@@ -45,8 +45,8 @@ func newCamera(path string) *camera {
|
||||
}
|
||||
|
||||
return &camera{
|
||||
path: path,
|
||||
formats: formats,
|
||||
path: path,
|
||||
formats: formats,
|
||||
reversedFormats: reversedFormats,
|
||||
}
|
||||
}
|
||||
@@ -61,8 +61,8 @@ func (c *camera) Open() error {
|
||||
for format := range cam.GetSupportedFormats() {
|
||||
for _, frameSize := range cam.GetSupportedFrameSizes(format) {
|
||||
settings = append(settings, VideoSetting{
|
||||
Width: int(frameSize.MaxWidth),
|
||||
Height: int(frameSize.MaxHeight),
|
||||
Width: int(frameSize.MaxWidth),
|
||||
Height: int(frameSize.MaxHeight),
|
||||
FrameFormat: c.formats[format],
|
||||
})
|
||||
}
|
||||
@@ -135,7 +135,6 @@ func (c *camera) Stop() error {
|
||||
|
||||
func (c *camera) Info() Info {
|
||||
return Info{
|
||||
Kind: Video,
|
||||
DeviceType: Camera,
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,5 @@
|
||||
package driver
|
||||
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
Video Kind = "video"
|
||||
Audio = "audio"
|
||||
)
|
||||
|
||||
type DeviceType string
|
||||
|
||||
const (
|
||||
|
@@ -1,10 +1,11 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/pion/mediadevices/pkg/frame"
|
||||
"github.com/pion/mediadevices/pkg/io/audio"
|
||||
"github.com/pion/mediadevices/pkg/io/video"
|
||||
"time"
|
||||
)
|
||||
|
||||
type OpenCloser interface {
|
||||
@@ -17,7 +18,6 @@ type Infoer interface {
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
Kind Kind
|
||||
DeviceType DeviceType
|
||||
}
|
||||
|
||||
|
@@ -2,14 +2,10 @@ package driver
|
||||
|
||||
import "fmt"
|
||||
|
||||
// FilterFn is being used to decide if a driver should be included in the
|
||||
// query result.
|
||||
type FilterFn func(Driver) bool
|
||||
|
||||
func FilterKind(k Kind) FilterFn {
|
||||
return func(d Driver) bool {
|
||||
return d.Info().Kind == k
|
||||
}
|
||||
}
|
||||
|
||||
// Manager is a singleton to manage multiple drivers and their states
|
||||
type Manager struct {
|
||||
drivers map[string]Driver
|
||||
@@ -46,3 +42,19 @@ func (m *Manager) Query(f FilterFn) []Driver {
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// VideoDrivers gets a list of registered VideoDriver
|
||||
func (m *Manager) VideoDrivers() []Driver {
|
||||
return m.Query(func(d Driver) bool {
|
||||
_, ok := d.(VideoDriver)
|
||||
return ok
|
||||
})
|
||||
}
|
||||
|
||||
// AudioDrivers gets a list of registered AudioDriver
|
||||
func (m *Manager) AudioDrivers() []Driver {
|
||||
return m.Query(func(d Driver) bool {
|
||||
_, ok := d.(AudioDriver)
|
||||
return ok
|
||||
})
|
||||
}
|
||||
|
@@ -100,7 +100,6 @@ func (m *microphone) Stop() error {
|
||||
|
||||
func (m *microphone) Info() Info {
|
||||
return Info{
|
||||
Kind: Audio,
|
||||
DeviceType: Microphone,
|
||||
}
|
||||
}
|
||||
|
6
track.go
6
track.go
@@ -27,10 +27,10 @@ type track struct {
|
||||
|
||||
func newTrack(pc *webrtc.PeerConnection, d driver.Driver, codecName string) (*track, error) {
|
||||
var kind webrtc.RTPCodecType
|
||||
switch d.Info().Kind {
|
||||
case driver.Video:
|
||||
switch d.(type) {
|
||||
case driver.VideoDriver:
|
||||
kind = webrtc.RTPCodecTypeVideo
|
||||
case driver.Audio:
|
||||
case driver.AudioDriver:
|
||||
kind = webrtc.RTPCodecTypeAudio
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user