media

package
v0.2.25 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 31 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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), both decoded on the CPU, and MP4 and Matroska/WebM files (OpenFile), demuxed in process, their H.264 and H.265 video decoded on the GPU the windows draw with and their AAC, Opus and Vorbis sound in Go. Sound plays through the PulseAudio native protocol, which PulseAudio and pipewire-pulse serve.

src, err := media.OpenFile("talk.mp4")
stream := media.NewStream(application, src, "talk")
video := widget.NewVideo(face, 13, stream)
stream.Play()

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func YUVFrameBytes(width, height int) int

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 {
	// AudioFormat is the layout of the PCM OpenAudio returns.
	AudioFormat() AudioFormat
	// OpenAudio returns the track from t as PCM in AudioFormat;
	// closing it stops the decoding.
	OpenAudio(t time.Duration) (io.ReadCloser, error)
}

Audible is a Source with a sound track.

type AudioFormat added in v0.2.20

type AudioFormat struct {
	Rate   int
	Layout native.ChannelMap
}

AudioFormat is the PCM an Audible source delivers: interleaved signed 16-bit little-endian samples at Rate, one per speaker of Layout in its order. The sound server converts it to its sink's.

type FrameSource added in v0.2.20

type FrameSource interface {
	Source
	// 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)
}

FrameSource is a Source decoding its frames on the CPU, in presentation order. ReadFrame and Seek are called from one goroutine.

func NewAnimation

func NewAnimation(a *render.Animation) (FrameSource, error)

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

func NewY4M(r io.Reader) (FrameSource, error)

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. 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]

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

type Seeker interface {
	Seek(t time.Duration) error
}

Seeker is a Source that can reposition: after Seek(t) the next frame read is the one showing at t.

type Source

type Source interface {
	Info() Info
	Close() error
}

Source is a video a Stream plays: a FrameSource, decoded on the CPU, or a TextureSource, decoded on the GPU. Close may be called from any goroutine at any time and must unblock a read in progress.

func OpenFile

func OpenFile(path string) (Source, error)

OpenFile opens an MP4 or Matroska/WebM file, demuxed in process: its first video track decodes on the GPU of the Video showing it (a TextureSource), and its first sound track in AAC-LC, Opus or Vorbis decodes in Go, so the source is Audible exactly when the file has sound. A file without video is an error, as is a sound track in any other codec; a video codec the device does not decode fails when the Video first shows the stream. Seeking lands on the keyframe before the target and decodes from there.

func WithoutAudio

func WithoutAudio(src Source) Source

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 decodes a few frames ahead (a FrameSource's on a goroutine of its own, a TextureSource's packets read on one and decoded on the loop's GPU device), 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 no file and no sound server: those are behind goroutines of their own. Subscribers hear every change: the frame, the position while playing, and the state.

func NewStream

func NewStream(owner anim.Owner, src Source, name string) *Stream

NewStream plays src, named name in the desktop's volume mixer, on owner's frame clock: an app.Application, or a widget, whose application's windows pace it once one shows the widget. 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) Closed

func (s *Stream) Closed() bool

Closed reports whether Close was called.

func (*Stream) DecodesOnGPU added in v0.2.20

func (s *Stream) DecodesOnGPU() bool

DecodesOnGPU reports whether the stream plays a TextureSource: its frames are GPU textures (Texture), decoded on the device UseDevice names, and Frame and FrameYUV return none.

func (*Stream) Duration

func (s *Stream) Duration() time.Duration

Duration returns the length, zero when unknown.

func (*Stream) Ended

func (s *Stream) Ended() bool

Ended reports whether playback reached the end (and is not looping).

func (*Stream) Err

func (s *Stream) Err() error

Err returns the failure that stopped the stream, nil while healthy.

func (*Stream) Fail added in v0.2.20

func (s *Stream) Fail(err error)

Fail stops the stream with err, what a viewer that cannot show its frames calls; Err returns it. A stream that failed already keeps its first error.

func (*Stream) Frame

func (s *Stream) Frame() render.VersionedImage

Frame returns the frame to show, its RGBA nil before the first is decoded, after Close and for a stream decoding on the GPU (Texture), 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) FrameSeq added in v0.2.10

func (s *Stream) FrameSeq() uint64

FrameSeq moves whenever Frame shows another frame.

func (*Stream) FrameYUV added in v0.2.15

func (s *Stream) FrameYUV() *YUVFrame

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) HasAudio

func (s *Stream) HasAudio() bool

HasAudio reports whether the stream plays sound.

func (*Stream) Loop

func (s *Stream) Loop() bool

Loop reports whether playback restarts at the end.

func (*Stream) Muted

func (s *Stream) Muted() bool

Muted reports whether the sound is muted.

func (*Stream) Pause

func (s *Stream) Pause()

Pause pauses playback at the current position.

func (*Stream) Play

func (s *Stream) Play()

Play starts or resumes playback; at the end it starts over.

func (*Stream) Playing

func (s *Stream) Playing() bool

Playing reports whether the stream is playing.

func (*Stream) Position

func (s *Stream) Position() time.Duration

Position returns the playback position.

func (*Stream) ReleaseDevice added in v0.2.20

func (s *Stream) ReleaseDevice()

ReleaseDevice lets go of the device UseDevice named, which is going away: the frames decoded on it and its decoder are released, and the stream goes on as it was, playing or paused, its sound and its clock untouched, showing no frame until UseDevice names another device. Decoding resumes there at the playback position. A stream decoding on no device stays as it is.

func (*Stream) Seek

func (s *Stream) Seek(t time.Duration) error

Seek moves playback to t, clamped to the stream. Playback continues from there if it was playing.

func (*Stream) Seekable

func (s *Stream) Seekable() bool

Seekable reports whether Seek works.

func (*Stream) SetLoop

func (s *Stream) SetLoop(on bool)

SetLoop makes playback restart at the end; it needs a seekable source and is ignored otherwise.

func (*Stream) SetMuted

func (s *Stream) SetMuted(on bool)

SetMuted mutes or unmutes the sound.

func (*Stream) SetVolume

func (s *Stream) SetVolume(v float64)

SetVolume sets the volume, 0 to 1 on a perceptual scale. Until it is called the desktop picks the stream's volume.

func (*Stream) Size

func (s *Stream) Size() (w, h int)

Size returns the frame size in pixels.

func (*Stream) SkipRGBA added in v0.2.15

func (s *Stream) SkipRGBA(on bool)

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.

func (*Stream) Texture added in v0.2.20

func (s *Stream) Texture() *gpu.VideoFrame

Texture returns the frame to show of a stream decoding on the GPU, nil before the first is decoded, after Close and for a stream decoding on the CPU. Its picture has finished decoding: draw it (gpu.Pass.DrawFrame) on the device UseDevice named, and nothing waits for the decoder. It belongs to the stream and stays valid until the next change or Close; FrameSeq tells when it changed.

func (*Stream) UseDevice added in v0.2.20

func (s *Stream) UseDevice(dev *gpu.Device, lost func(error)) error

UseDevice decodes a TextureSource's frames on dev, the device that draws them, from the playback position on; until then the stream shows no frame. A stream decodes on one device at a time: naming another lets go of the one before, as ReleaseDevice does. A device that does not decode the video's codec fails the stream with an error naming the codec and the file, which UseDevice returns too.

lost is called on the loop when decoding finds dev lost (gpu.ErrDeviceLost), the stream having let go of it as ReleaseDevice does: whoever named the device has its owner open the next and names that. With a nil lost a lost device fails the stream.

func (*Stream) Volume

func (s *Stream) Volume() float64

Volume returns the volume set through SetVolume, 1 until then.

func (*Stream) Wait added in v0.2.5

func (s *Stream) Wait() error

Wait blocks until a closed Stream let go of everything it held and returns the failure closing its source met. It is for the end of a program, off the loop goroutine: a source that never exits holds it up.

func (*Stream) YUV added in v0.2.15

func (s *Stream) YUV() (YUVColor, bool)

YUV reports the colour of the frames FrameYUV returns, false when the source decodes no YUV and FrameYUV returns nil.

type TextureSource added in v0.2.20

type TextureSource interface {
	Source
	Seeker
	// NewVideoDecoder opens a decoder of the video track on dev, an
	// error naming the codec and the file where dev decodes none. A
	// Stream that moves to another device closes the decoder and opens
	// one on the next.
	NewVideoDecoder(dev *gpu.Device) (VideoDecoder, error)
	// ReadPacket returns the video track's next packet, its Data valid
	// until the next ReadPacket or Seek, and io.EOF after the last.
	// After Seek(t) the packets start at the keyframe at or before t.
	ReadPacket() (demux.Packet, error)
}

TextureSource is a Source whose video the GPU decodes: it hands out the video track's packets in decode order, and the Stream decodes them on the device of the Video showing it (Stream.UseDevice). ReadPacket and Seek are called from one goroutine, NewVideoDecoder from the device's.

type VideoDecoder added in v0.2.20

type VideoDecoder interface {
	Busy() (bool, error)
	Decode(p demux.Packet) error
	Next() *gpu.VideoFrame
	Flush()
	Reset() error
	Close()
}

VideoDecoder decodes a TextureSource's packets on one GPU device, what a *gpu.VideoDecoder does: Decode takes the packets in decode order, Next hands out the frames they complete in presentation order, nil when there is none yet, Flush hands out the frames held back at the track's end, Reset drops what is decoding for a seek, and Close releases the decoder with every frame it handed out. Next waits for nothing: a frame's picture may still be decoding (its Decoded tells). Busy reports, without waiting, a decoder so far behind the packets it took that Decode of another could wait for it: the Stream holds its next packet back until the following tick. A device that was lost fails Decode, Reset and Busy with gpu.ErrDeviceLost.

type YUVColor added in v0.2.15

type YUVColor struct {
	Matrix YUVMatrix
	Range  YUVRange
}

YUVColor is how a YUV frame's samples become RGB.

type YUVFrame added in v0.2.15

type YUVFrame struct {
	Width, Height int
	Color         YUVColor
	Pix           []byte
}

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

func (f *YUVFrame) ChromaSize() (w, h int)

ChromaSize is the size of the U and V planes: half the frame's, rounded up.

func (*YUVFrame) Planes added in v0.2.15

func (f *YUVFrame) Planes() (y, u, v []byte)

Planes returns the Y, U and V planes, slices of Pix.

type YUVMatrix added in v0.2.15

type YUVMatrix uint8

YUVMatrix is the matrix a YUV frame's samples were encoded with.

const BT601 YUVMatrix = iota

BT601 is the matrix every built-in source decodes to.

type YUVRange added in v0.2.15

type YUVRange uint8

YUVRange is the span a YUV frame's samples cover.

const (
	LimitedRange YUVRange = iota
	FullRange
)

The ranges: limited (16 to 235 for luma) and full (0 to 255).

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL