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
- type Audible
- type Info
- type Seeker
- type Source
- type Stream
- func (s *Stream) Close() error
- 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) 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) Subscribe(fn func()) (cancel func())
- func (s *Stream) Volume() float64
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 ¶
This section is empty.
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 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. 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) Frame ¶
func (s *Stream) Frame() render.VersionedImage
Frame returns the frame to show, its RGBA nil before the first is decoded and after Close: a pooled buffer's pixels under the buffer's identity, at a version that changes whenever the frame does. The image belongs to the stream and stays valid until the next change or Close.
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.