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 ¶
- Variables
- func YUVFrameBytes(width, height int) int
- type Audible
- type AudioFormat
- type FrameSource
- type Info
- type Seeker
- type Source
- type StillSource
- type Stream
- func (s *Stream) Close()
- func (s *Stream) Closed() bool
- func (s *Stream) DecodesOnGPU() bool
- func (s *Stream) Duration() time.Duration
- func (s *Stream) Ended() bool
- func (s *Stream) Err() error
- func (s *Stream) Fail(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) ReleaseDevice()
- 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) Texture() *gpu.VideoFrame
- func (s *Stream) UseDevice(dev *gpu.Device, lost func(error)) error
- func (s *Stream) Volume() float64
- func (s *Stream) Wait() error
- func (s *Stream) YUV() (YUVColor, bool)
- type TextureSource
- type VideoDecoder
- type YUVColor
- type YUVFrame
- type YUVMatrix
- type YUVRange
- type YUVSource
Examples ¶
Constants ¶
This section is empty.
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 {
// 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 ¶
Seeker is a Source that can reposition: after Seek(t) the next frame read is the one showing at t.
type Source ¶
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 ¶
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 ¶
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 ¶
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) DecodesOnGPU ¶ added in v0.2.20
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) Fail ¶ added in v0.2.20
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) 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) 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 ¶
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.
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
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.
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 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.