sdl

package
v1.8.2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package sdl provides SDL2-based audio and video output for go-media.

This package requires SDL2 to be installed.

The package provides:

  • Context: SDL initialization and event loop management
  • Window: Video rendering using SDL windows and textures
  • Audio: Audio playback using SDL audio devices
  • Player: High-level player combining video and audio

Example usage:

ctx, err := sdl.New(sdl.INIT_VIDEO | sdl.INIT_AUDIO)
if err != nil {
    log.Fatal(err)
}
defer ctx.Close()

window, err := ctx.NewWindow("Video Player", 1920, 1080)
if err != nil {
    log.Fatal(err)
}
defer window.Close()

ctx.Run(context.Background())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Audio

type Audio struct {
	// contains filtered or unexported fields
}

Audio represents an SDL audio device for playback.

func (*Audio) Clear added in v1.8.1

func (a *Audio) Clear() error

Clear clears any queued audio data.

func (*Audio) Close

func (a *Audio) Close() error

Close closes the audio device.

func (*Audio) Pause added in v1.8.1

func (a *Audio) Pause() error

Pause pauses audio playback.

func (*Audio) Queue added in v1.8.1

func (a *Audio) Queue(data []byte) error

Queue queues audio data for playback. The data should be in the format specified when creating the device (float32).

func (*Audio) QueuedSize added in v1.8.1

func (a *Audio) QueuedSize() uint32

QueuedSize returns the number of bytes currently queued for playback.

func (*Audio) Resume added in v1.8.1

func (a *Audio) Resume() error

Resume resumes audio playback.

func (*Audio) Spec added in v1.8.1

func (a *Audio) Spec() *sdl.AudioSpec

Spec returns the audio device specification.

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context manages SDL initialization and the event loop.

func New

func New(flags uint32) (*Context, error)

New creates a new SDL context with the specified initialization flags. Common flags are sdl.INIT_VIDEO, sdl.INIT_AUDIO, or combine them.

func (*Context) Close

func (c *Context) Close() error

Close shuts down SDL and releases all resources.

func (*Context) NewAudio

func (c *Context) NewAudio(sampleRate int32, channels uint8, samples uint16) (*Audio, error)

NewAudio creates a new SDL audio device with the specified parameters. sampleRate is in Hz (e.g., 44100, 48000) channels is the number of audio channels (1 for mono, 2 for stereo) samples is the audio buffer size in samples (power of 2, e.g., 4096)

func (*Context) NewPlayer added in v1.8.1

func (c *Context) NewPlayer() *Player

NewPlayer creates a new player for displaying video and playing audio. It will auto-setup when the first frame is received.

func (*Context) NewWindow added in v1.8.1

func (c *Context) NewWindow(title string, width, height int32) (*Window, error)

NewWindow creates a new SDL window with the specified title and dimensions. The window is created with a renderer and texture ready for video display.

func (*Context) Post

func (c *Context) Post(evt uint32, userInfo interface{}) error

Post posts a custom event to the event queue.

func (*Context) Register

func (c *Context) Register(fn func(userInfo interface{})) uint32

Register registers a custom event handler. Returns the event ID that can be used with Post to trigger the handler.

func (*Context) Run

func (c *Context) Run(ctx context.Context) error

Run starts the SDL event loop and blocks until the context is cancelled or a quit event is received.

type FrameLoop added in v1.8.1

type FrameLoop struct {
	// contains filtered or unexported fields
}

FrameLoop runs an SDL event that pulls frames from a channel and hands them to a handler on the main thread. It keeps posting events with small delays to avoid busy looping while still staying responsive.

func NewFrameLoop added in v1.8.1

func NewFrameLoop(ctx *Context, handler func(*ffmpeg.Frame) error, buffer int, opts ...FrameLoopOpt) (*FrameLoop, error)

NewFrameLoop creates a frame loop with the given buffer size and handler. Use Start to begin posting events.

func (*FrameLoop) CloseInput added in v1.8.1

func (l *FrameLoop) CloseInput()

CloseInput signals that no more frames will arrive and triggers final processing.

func (*FrameLoop) Done added in v1.8.1

func (l *FrameLoop) Done() <-chan struct{}

Done is closed when the loop stops after input closes.

func (*FrameLoop) Enqueue added in v1.8.1

func (l *FrameLoop) Enqueue(frame *ffmpeg.Frame) error

Enqueue adds a frame for processing on the main thread.

func (*FrameLoop) Start added in v1.8.1

func (l *FrameLoop) Start()

Start kicks off the loop by posting the first event.

func (*FrameLoop) Stop added in v1.8.1

func (l *FrameLoop) Stop()

Stop halts further event posting and closes Done.

type FrameLoopOpt added in v1.8.1

type FrameLoopOpt func(*FrameLoop)

FrameLoopOpt configures a FrameLoop.

func WithFrameDelayFunc added in v1.8.1

func WithFrameDelayFunc(fn func(*ffmpeg.Frame) time.Duration) FrameLoopOpt

WithFrameDelayFunc uses a custom delay calculator per frame (e.g., based on PTS).

type FrameWriter added in v1.8.1

type FrameWriter struct {
	// contains filtered or unexported fields
}

FrameWriter adapts a FrameLoop to the ffmpeg task writer interface.

func NewFrameWriter added in v1.8.1

func NewFrameWriter(loop *FrameLoop) *FrameWriter

NewFrameWriter creates a writer that forwards frames into the loop.

func (*FrameWriter) Write added in v1.8.1

func (w *FrameWriter) Write(p []byte) (n int, err error)

Write satisfies io.Writer but discards data; decoding writes frames via WriteFrame.

func (*FrameWriter) WriteFrame added in v1.8.1

func (w *FrameWriter) WriteFrame(streamIndex int, frame interface{}) error

WriteFrame queues ffmpeg frames into the loop.

type Player added in v1.8.1

type Player struct {
	// contains filtered or unexported fields
}

Player combines window and audio for easy playback of decoded frames.

func (*Player) Audio added in v1.8.1

func (p *Player) Audio() *Audio

Audio returns the player's audio device (may be nil if no audio frames yet).

func (*Player) Close added in v1.8.1

func (p *Player) Close() error

Close closes the player and releases all resources.

func (*Player) PlayFrame added in v1.8.1

func (p *Player) PlayFrame(ctx *Context, frame *ffmpeg.Frame) error

PlayFrame plays a decoded frame. For video frames, displays them in the window. For audio frames, queues them for playback. Auto-creates window/audio on first frame.

func (*Player) SetWindow added in v1.8.1

func (p *Player) SetWindow(w *Window)

SetWindow sets the window for video playback

func (*Player) VideoDelay added in v1.8.1

func (p *Player) VideoDelay(frame *ffmpeg.Frame) time.Duration

VideoDelay returns how long to wait before presenting the next frame. Implements A/V sync following the FFmpeg tutorial algorithm. Returns 0 for audio frames (never delay audio).

func (*Player) Window added in v1.8.1

func (p *Player) Window() *Window

Window returns the current window, if any

type Window

type Window struct {
	// contains filtered or unexported fields
}

Window represents an SDL window with renderer and texture for video display.

func (*Window) Close

func (w *Window) Close() error

Close destroys the window and releases all resources.

func (*Window) Render added in v1.8.1

func (w *Window) Render() error

Render renders the current texture to the window.

func (*Window) SetTitle added in v1.8.1

func (w *Window) SetTitle(title string)

SetTitle sets the window title.

func (*Window) Size added in v1.8.1

func (w *Window) Size() (width, height int32)

Size returns the current window dimensions.

func (*Window) Update added in v1.8.1

func (w *Window) Update(y, u, v []byte, yPitch, uPitch, vPitch int, width, height int32) error

Update updates the texture with new video frame data. The data should be in YUV420P format with planes laid out sequentially.

func (*Window) UpdateRGB added in v1.8.1

func (w *Window) UpdateRGB(pixels []byte, pitch int, width, height int32) error

UpdateRGB updates the texture with RGB24 data. The data should be in packed RGB24 format (3 bytes per pixel).

Jump to

Keyboard shortcuts

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