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