Documentation
¶
Overview ¶
Package gostream implements a simple server for serving video streams over WebRTC.
Index ¶
- Variables
- func DriverFromMediaSource[T, U any](src MediaSource[T]) (driver.Driver, error)
- func LabelsFromMediaSource[T, U any](src MediaSource[T]) ([]string, error)
- func MIMETypeHint(ctx context.Context, defaultType string) string
- func PropertiesFromMediaSource[T, U any](src MediaSource[T]) ([]prop.Media, error)
- func QueryScreenDevicesLabels() []string
- func QueryVideoDeviceLabels() []string
- func ReadImage(ctx context.Context, source VideoSource) (image.Image, func(), error)
- func ReadMedia[T any](ctx context.Context, source MediaSource[T]) (T, func(), error)
- func StreamVideoSource(ctx context.Context, vs VideoSource, stream Stream, logger logging.Logger) error
- func StreamVideoSourceWithErrorHandler(ctx context.Context, vs VideoSource, stream Stream, errHandler ErrorHandler, ...) error
- func WithMIMETypeHint(ctx context.Context, mimeType string) context.Context
- type DeviceInfo
- type DriverInUseError
- type ErrorHandler
- type HotSwappableMediaSource
- type HotSwappableVideoSource
- type MediaPropertyProvider
- type MediaReader
- type MediaReaderFunc
- type MediaReleasePair
- type MediaReleasePairWithError
- type MediaSource
- func GetAnyScreenSource(constraints mediadevices.MediaStreamConstraints, logger logging.Logger) (MediaSource[image.Image], error)
- func GetAnyVideoSource(constraints mediadevices.MediaStreamConstraints, logger logging.Logger) (MediaSource[image.Image], error)
- func GetNamedScreenSource(name string, constraints mediadevices.MediaStreamConstraints, ...) (MediaSource[image.Image], error)
- func GetNamedVideoSource(name string, constraints mediadevices.MediaStreamConstraints, ...) (MediaSource[image.Image], error)
- func GetPatternedScreenSource(labelPattern *regexp.Regexp, constraints mediadevices.MediaStreamConstraints, ...) (MediaSource[image.Image], error)
- func GetPatternedVideoSource(labelPattern *regexp.Regexp, constraints mediadevices.MediaStreamConstraints, ...) (MediaSource[image.Image], error)
- type MediaStream
- type Stream
- type StreamConfig
- type VideoPropertyProvider
- type VideoReader
- type VideoReaderFunc
- type VideoSource
- type VideoStream
Constants ¶
This section is empty.
Variables ¶
var Debug = false
Debug is helpful to turn on when the library isn't working quite right.
var DefaultConstraints = mediadevices.MediaStreamConstraints{ Video: func(constraint *mediadevices.MediaTrackConstraints) { constraint.Width = prop.IntRanged{Min: 640, Max: 4096, Ideal: 1920} constraint.Height = prop.IntRanged{Min: 400, Max: 2160, Ideal: 1080} constraint.FrameRate = prop.FloatRanged{Min: 0, Max: 200, Ideal: 60} constraint.FrameFormat = prop.FrameFormatOneOf{ frame.FormatI420, frame.FormatI444, frame.FormatYUY2, frame.FormatYUYV, frame.FormatUYVY, frame.FormatRGBA, frame.FormatMJPEG, frame.FormatNV12, frame.FormatZ16, frame.FormatNV21, } }, }
DefaultConstraints are suitable for finding any available device.
var ErrNotFound = errors.New("failed to find the best driver that fits the constraints")
ErrNotFound happens when there is no driver found in a query.
Functions ¶
func DriverFromMediaSource ¶
DriverFromMediaSource returns the underlying driver from the MediaSource.
func LabelsFromMediaSource ¶
LabelsFromMediaSource returns the labels from the underlying driver in the MediaSource.
func MIMETypeHint ¶
MIMETypeHint gets the hint of what MIME type to use in encoding; if nothing is set, the default provided is used.
func PropertiesFromMediaSource ¶
PropertiesFromMediaSource returns properties from underlying driver in the given MediaSource.
func QueryScreenDevicesLabels ¶
func QueryScreenDevicesLabels() []string
QueryScreenDevicesLabels lists all known screen devices.
func QueryVideoDeviceLabels ¶
func QueryVideoDeviceLabels() []string
QueryVideoDeviceLabels lists all known video devices (not a screen).
func ReadImage ¶
ReadImage gets a single image from a video source. Using this has less of a guarantee than VideoSource.Stream that the Nth image follows the N-1th image.
func ReadMedia ¶
ReadMedia gets a single media from a source. Using this has less of a guarantee than MediaSource.Stream that the Nth media element follows the N-1th media element.
func StreamVideoSource ¶
func StreamVideoSource(ctx context.Context, vs VideoSource, stream Stream, logger logging.Logger) error
StreamVideoSource streams the given video source to the stream forever until context signals cancellation.
func StreamVideoSourceWithErrorHandler ¶
func StreamVideoSourceWithErrorHandler( ctx context.Context, vs VideoSource, stream Stream, errHandler ErrorHandler, logger logging.Logger, ) error
StreamVideoSourceWithErrorHandler streams the given video source to the stream forever until context signals cancellation, frame errors are sent via the error handler.
Types ¶
type DeviceInfo ¶
type DeviceInfo struct {
ID string
Labels []string
Properties []prop.Media
Priority driver.Priority
Error error
}
DeviceInfo describes a driver.
func QueryScreenDevices ¶
func QueryScreenDevices() []DeviceInfo
QueryScreenDevices lists all known screen devices.
func QueryVideoDevices ¶
func QueryVideoDevices() []DeviceInfo
QueryVideoDevices lists all known video devices (not a screen).
type DriverInUseError ¶
type DriverInUseError struct {
// contains filtered or unexported fields
}
DriverInUseError is returned when closing drivers that still being read from.
func (*DriverInUseError) Error ¶
func (err *DriverInUseError) Error() string
type ErrorHandler ¶
ErrorHandler receives the error returned by a TSource.Next regardless of whether or not the error is nil (This allows for error handling logic based on consecutively retrieved errors).
type HotSwappableMediaSource ¶
type HotSwappableMediaSource[T, U any] interface { MediaSource[T] MediaPropertyProvider[U] Swap(src MediaSource[T]) }
A HotSwappableMediaSource allows for continuous streaming of media of swappable underlying media sources.
func NewHotSwappableMediaSource ¶
func NewHotSwappableMediaSource[T, U any](src MediaSource[T]) HotSwappableMediaSource[T, U]
NewHotSwappableMediaSource returns a hot swappable media source.
type HotSwappableVideoSource ¶
type HotSwappableVideoSource = HotSwappableMediaSource[image.Image, prop.Video]
A HotSwappableVideoSource allows for continuous streaming of video of swappable underlying video sources.
func NewHotSwappableVideoSource ¶
func NewHotSwappableVideoSource(src VideoSource) HotSwappableVideoSource
NewHotSwappableVideoSource returns a hot swappable video source.
type MediaPropertyProvider ¶
MediaPropertyProvider providers information about a source.
type MediaReader ¶
type MediaReader[T any] interface { Read(ctx context.Context) (data T, release func(), err error) Close(ctx context.Context) error }
A MediaReader is anything that can read and recycle data. It is expected that reader can handle multiple reads at the same time. This would ideally only happen during streaming when a specific MIME type is requested. In the future, we may be able to notice multiple MIME types and either do deferred encode/decode or have the reader do it for us.
type MediaReaderFunc ¶
A MediaReaderFunc is a helper to turn a function into a MediaReader.
type MediaReleasePair ¶
type MediaReleasePair[T any] struct { Media T Release func() }
MediaReleasePair associates a media with a corresponding function to release its resources once the receiver of a pair is finished with the media.
type MediaReleasePairWithError ¶
MediaReleasePairWithError contains the result of fetching media.
type MediaSource ¶
type MediaSource[T any] interface { // Stream returns a stream that makes a best effort to return consecutive media elements // that may have a MIME type hint dictated in the context via WithMIMETypeHint. Stream(ctx context.Context, errHandlers ...ErrorHandler) (MediaStream[T], error) // Close cleans up any associated resources with the Source (e.g. a Driver). Close(ctx context.Context) error }
A MediaSource can produce Streams of Ts.
func GetAnyScreenSource ¶
func GetAnyScreenSource( constraints mediadevices.MediaStreamConstraints, logger logging.Logger, ) (MediaSource[image.Image], error)
GetAnyScreenSource attempts to find any suitable screen device.
func GetAnyVideoSource ¶
func GetAnyVideoSource( constraints mediadevices.MediaStreamConstraints, logger logging.Logger, ) (MediaSource[image.Image], error)
GetAnyVideoSource attempts to find any suitable video device (not a screen).
func GetNamedScreenSource ¶
func GetNamedScreenSource( name string, constraints mediadevices.MediaStreamConstraints, logger logging.Logger, ) (MediaSource[image.Image], error)
GetNamedScreenSource attempts to find a screen device by the given name.
func GetNamedVideoSource ¶
func GetNamedVideoSource( name string, constraints mediadevices.MediaStreamConstraints, logger logging.Logger, ) (MediaSource[image.Image], error)
GetNamedVideoSource attempts to find a video device (not a screen) by the given name.
func GetPatternedScreenSource ¶
func GetPatternedScreenSource( labelPattern *regexp.Regexp, constraints mediadevices.MediaStreamConstraints, logger logging.Logger, ) (MediaSource[image.Image], error)
GetPatternedScreenSource attempts to find a screen device by the given label pattern.
func GetPatternedVideoSource ¶
func GetPatternedVideoSource( labelPattern *regexp.Regexp, constraints mediadevices.MediaStreamConstraints, logger logging.Logger, ) (MediaSource[image.Image], error)
GetPatternedVideoSource attempts to find a video device (not a screen) by the given label pattern.
type MediaStream ¶
type MediaStream[T any] interface { // Next returns the next media element in the sequence (best effort). // Note: This element is mutable and shared globally; it MUST be copied // before it is mutated. Next(ctx context.Context) (T, func(), error) // Close signals this stream is no longer needed and releases associated // resources. Close(ctx context.Context) error }
A MediaStream streams media forever until closed.
func NewEmbeddedMediaStream ¶
func NewEmbeddedMediaStream[T, U any](src MediaSource[T]) MediaStream[T]
NewEmbeddedMediaStream returns a media stream from a media source that is intended to be embedded/composed by another source. It defers the creation of its media stream.
func NewEmbeddedMediaStreamFromReader ¶
func NewEmbeddedMediaStreamFromReader[T, U any](reader MediaReader[T], p U) MediaStream[T]
NewEmbeddedMediaStreamFromReader returns a media stream from a media reader that is intended to be embedded/composed by another source. It defers the creation of its media stream.
func NewMediaStreamForChannel ¶
func NewMediaStreamForChannel[T any](ctx context.Context) (context.Context, MediaStream[T], chan<- MediaReleasePairWithError[T])
NewMediaStreamForChannel returns a MediaStream backed by a channel.
type Stream ¶
type Stream interface {
Name() string
// Start starts processing frames.
Start()
WriteRTP(pkt *rtp.Packet) error
// Ready signals that there is at least one client connected and that
// streams are ready for input. The returned context should be used for
// signaling that streaming is no longer ready.
StreamingReady() (<-chan struct{}, context.Context)
InputVideoFrames(props prop.Video) (chan<- MediaReleasePair[image.Image], error)
// Stop stops further processing of frames.
Stop()
// contains filtered or unexported methods
}
A Stream is sink that accepts any image frames for the purpose of displaying in a WebRTC video track.
type StreamConfig ¶
type StreamConfig struct {
Name string
VideoEncoderFactory codec.VideoEncoderFactory
// TargetFrameRate will hint to the stream to try to maintain this frame rate.
TargetFrameRate int
}
A StreamConfig describes how a Stream should be managed.
type VideoPropertyProvider ¶
VideoPropertyProvider providers information about a video source.
type VideoReader ¶
type VideoReader = MediaReader[image.Image]
An VideoReader is anything that can read and recycle video data.
type VideoReaderFunc ¶
An VideoReaderFunc is a helper to turn a function into an VideoReader.
type VideoSource ¶
A VideoSource is responsible for producing images when requested. A source should produce the image as quickly as possible and introduce no rate limiting of its own as that is handled internally.
func NewResizeVideoSource ¶
func NewResizeVideoSource(src VideoSource, width, height int) VideoSource
NewResizeVideoSource returns a source that resizes images to the set dimensions.
func NewVideoSource ¶
func NewVideoSource(r VideoReader, p prop.Video) VideoSource
NewVideoSource instantiates a new video source.
func NewVideoSourceForDriver ¶
func NewVideoSourceForDriver(d driver.Driver, r VideoReader, p prop.Video) VideoSource
NewVideoSourceForDriver instantiates a new video source and references the given driver.
type VideoStream ¶
An VideoStream streams video forever until closed.
func NewEmbeddedVideoStream ¶
func NewEmbeddedVideoStream(src VideoSource) VideoStream
NewEmbeddedVideoStream returns a video stream from a video source that is intended to be embedded/composed by another source. It defers the creation of its stream.
func NewEmbeddedVideoStreamFromReader ¶
func NewEmbeddedVideoStreamFromReader(reader VideoReader) VideoStream
NewEmbeddedVideoStreamFromReader returns a video stream from a video reader that is intended to be embedded/composed by another source. It defers the creation of its stream.