Documentation
¶
Overview ¶
Package media is lys-ui's playback framework, GTK's GtkMediaStream in pure Go: a Source decodes frames (and, when Audible, sound), and a Stream plays one on the loop with play, pause, seek, loop, volume and a clock that follows the audio when there is any. widget.Video paints a Stream and widget.MediaControls drives it.
Built-in sources: animated images (NewAnimation), YUV4MPEG2 (NewY4M, decoded here), and any file ffmpeg reads (OpenFile, which runs ffmpeg and ffprobe from PATH as subprocesses). Sound plays through the PulseAudio native protocol, which PulseAudio and pipewire-pulse serve.
src, err := media.OpenFile("talk.webm")
stream := media.NewStream(src)
video := widget.NewVideo(face, 13, stream)
stream.Play()
Index ¶
- Constants
- Variables
- func YUVFrameBytes(width, height int) int
- type Audible
- type Info
- type Seeker
- type Source
- type StillSource
- type Stream
- func (s *Stream) Close()
- func (s *Stream) Closed() bool
- func (s *Stream) Duration() time.Duration
- func (s *Stream) Ended() bool
- func (s *Stream) Err() error
- func (s *Stream) Frame() render.VersionedImage
- func (s *Stream) FrameSeq() uint64
- func (s *Stream) FrameYUV() *YUVFrame
- func (s *Stream) HasAudio() bool
- func (s *Stream) Loop() bool
- func (s *Stream) Muted() bool
- func (s *Stream) Pause()
- func (s *Stream) Play()
- func (s *Stream) Playing() bool
- func (s *Stream) Position() time.Duration
- func (s *Stream) Seek(t time.Duration) error
- func (s *Stream) Seekable() bool
- func (s *Stream) SetLoop(on bool)
- func (s *Stream) SetMuted(on bool)
- func (s *Stream) SetVolume(v float64)
- func (s *Stream) Size() (w, h int)
- func (s *Stream) SkipRGBA(on bool)
- func (s *Stream) Subscribe(fn func()) (cancel func())
- func (s *Stream) Volume() float64
- func (s *Stream) Wait() error
- func (s *Stream) YUV() (YUVColor, bool)
- type YUVColor
- type YUVFrame
- type YUVMatrix
- type YUVRange
- type YUVSource
Examples ¶
Constants ¶
const ( AudioRate = 48000 AudioChannels = 2 )
The PCM layout every Audible track delivers: interleaved signed 16-bit little-endian samples.
Variables ¶
var ErrNotSeekable = errors.New("media: the stream cannot seek")
ErrNotSeekable reports a Seek on a Stream whose source cannot seek.
Functions ¶
func YUVFrameBytes ¶ added in v0.2.15
YUVFrameBytes is the size of a width x height YUVFrame's Pix: width x height x 1.5 for even sizes.
Types ¶
type Audible ¶
type Audible interface {
// OpenAudio returns the track from t as PCM in the AudioRate,
// AudioChannels layout; closing it stops the decoding.
OpenAudio(t time.Duration) (io.ReadCloser, error)
}
Audible is a Source with a sound track.
type Info ¶
type Info struct {
// Width and Height are the frame size in pixels.
Width, Height int
// Duration is the length; zero when the source cannot know it.
Duration time.Duration
}
Info describes a source's video.
type Seeker ¶
Seeker is a Source that can reposition: after Seek(t) the next ReadFrame returns the frame showing at t.
type Source ¶
type Source interface {
Info() Info
// ReadFrame decodes the next frame into dst, which is Info's size,
// and returns its presentation time. It returns io.EOF after the
// last frame.
ReadFrame(dst *image.RGBA) (time.Duration, error)
Close() error
}
Source decodes a video's frames in presentation order. ReadFrame and Seek are called from one goroutine; Close may be called from another at any time and must unblock a ReadFrame in progress.
func NewAnimation ¶
NewAnimation plays a decoded GIF or APNG (render.DecodeImage) as a seekable source; the image's own loop count is left to the Stream's SetLoop.
func NewY4M ¶
NewY4M reads a YUV4MPEG2 stream (8-bit 4:2:0, 4:2:2, 4:4:4 or mono, BT.601, limited range unless XCOLORRANGE=FULL). When r is also an io.ReadSeeker the source seeks and knows its duration; that needs every frame header to be a bare FRAME line, as ffmpeg writes. Close closes r when it is an io.Closer.
Example ¶
raw := "YUV4MPEG2 W2 H2 F25:1 XCOLORRANGE=FULL\nFRAME\n" + strings.Repeat("\xff", 4) + "\x80\x80"
src, _ := NewY4M(strings.NewReader(raw))
dst := image.NewRGBA(image.Rect(0, 0, 2, 2))
pts, _ := src.ReadFrame(dst)
fmt.Printf("%v %v %v\n", src.Info().Duration, pts, dst.Pix[:4])
Output: 40ms 0s [255 255 255 255]
func OpenFile ¶
OpenFile opens a media file through ffmpeg, which decodes anything it knows (H.264, VP9, AV1, ...): ffprobe reads the streams, an ffmpeg subprocess decodes the first video stream, and a second one decodes the first audio stream when there is one, so the source is Audible exactly when the file has sound. Both binaries come from PATH; their absence is an error, as is a file without video. Seeking restarts the decoders at the new position.
func WithoutAudio ¶
WithoutAudio returns src with its sound track hidden, for playback with no audio output at all (a muted Stream still opens one).
type StillSource ¶ added in v0.2.10
type StillSource interface {
ReadStill() (render.StillImage, time.Duration, error)
}
StillSource is a Source whose frames are rasters it keeps unchanged for its whole life, a decoded animation's: the stream shows them as they are, under their own identity, so each is copied nowhere and a GPU uploads it once however often it plays. ReadStill takes the place of ReadFrame.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream plays a Source, GTK's GtkMediaStream: it runs the decoder on its own goroutine a few frames ahead, presents the frame due at the playback position on the loop's frame clock, and, when the source is Audible, plays the sound through PulseAudio and follows its clock so picture and sound stay together. Every method runs on the loop goroutine and waits on nothing: the decoder, the sound server and every subprocess are behind goroutines of their own. Subscribers hear every change: the frame, the position while playing, and the state.
func NewStream ¶
NewStream plays src, named name in the desktop's volume mixer. It starts paused and decodes the first frame right away; Close releases the source and its decoders.
func (*Stream) Close ¶
func (s *Stream) Close()
Close stops playback and releases the source and decoders. It returns at once: a decoder or sound server that takes its time to go, or never goes, holds up its own goroutine and not the caller's. Wait joins it.
func (*Stream) Frame ¶
func (s *Stream) Frame() render.VersionedImage
Frame returns the frame to show, its RGBA nil before the first is decoded and after Close, under its raster's identity: a pooled buffer's at a version that moves whenever its pixels do, a StillSource's frame at version 0, since its pixels never change. The image belongs to the stream and stays valid until the next change or Close. FrameSeq tells when the frame changed.
func (*Stream) FrameYUV ¶ added in v0.2.15
FrameYUV returns the frame to show as its decoded YUV planes, nil before the first is decoded, after Close and for a source decoding no YUV. It belongs to the stream and stays valid until the next change or Close; FrameSeq tells when it changed.
func (*Stream) Play ¶
func (s *Stream) Play()
Play starts or resumes playback; at the end it starts over.
func (*Stream) Seek ¶
Seek moves playback to t, clamped to the stream. Playback continues from there if it was playing.
func (*Stream) SetLoop ¶
SetLoop makes playback restart at the end; it needs a seekable source and is ignored otherwise.
func (*Stream) SetVolume ¶
SetVolume sets the volume, 0 to 1 on a perceptual scale. Until it is called the desktop picks the stream's volume.
func (*Stream) SkipRGBA ¶ added in v0.2.15
SkipRGBA stops converting the frames of a source decoding YUV to RGBA as they are decoded, for a viewer that shows FrameYUV; off resumes it. Frame keeps working either way: it converts the frame it returns when the decoder did not.
func (*Stream) Subscribe ¶
func (s *Stream) Subscribe(fn func()) (cancel func())
Subscribe calls fn on the loop after every change; cancel stops it.
type YUVFrame ¶ added in v0.2.15
YUVFrame is a frame as 8-bit 4:2:0 planes (I420): Pix holds the Y plane, Width by Height samples, then the U and the V plane, each ChromaSize, every row packed, so a plane's stride is its width.
func (*YUVFrame) ChromaSize ¶ added in v0.2.15
ChromaSize is the size of the U and V planes: half the frame's, rounded up.
type YUVMatrix ¶ added in v0.2.15
type YUVMatrix uint8
YUVMatrix is the matrix a YUV frame's samples were encoded with.
type YUVRange ¶ added in v0.2.15
type YUVRange uint8
YUVRange is the span a YUV frame's samples cover.
type YUVSource ¶ added in v0.2.15
type YUVSource interface {
// YUV reports the colour of the frames ReadFrameYUV decodes, false
// when the source's frames are not 4:2:0 and it decodes none.
YUV() (YUVColor, bool)
// ReadFrameYUV decodes the next frame into dst, of Info's size,
// and returns its presentation time. It returns io.EOF after the
// last frame.
ReadFrameYUV(dst *YUVFrame) (time.Duration, error)
}
YUVSource is a Source that decodes YUV: ReadFrameYUV takes the place of ReadFrame for a viewer that shows the planes as they are, on a YUV dmabuf, with no conversion to RGBA.