media

package
v0.6.10 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotSeekable = errors.New("media: source is not seekable")

ErrNotSeekable is returned by OpenAt when the source has no seekable input.

Functions

func SetFFmpegPath

func SetFFmpegPath(p string)

SetFFmpegPath overrides the binary used for transcoding. Empty resets to "ffmpeg".

func SetLogger added in v0.3.2

func SetLogger(l *slog.Logger)

SetLogger sets the logger used by media-package ffmpeg shell readers. Pass nil to disable logging.

func SetStderrLog added in v0.6.0

func SetStderrLog(on bool)

SetStderrLog toggles the live-tee of ffmpeg stderr to the package logger.

func StderrLogEnabled added in v0.6.0

func StderrLogEnabled() bool

StderrLogEnabled reports the current setting (read by io.ShellReader).

Types

type EncodeOptions

type EncodeOptions struct {
	VideoBitrateKbps int
	VideoWidth       int
	VideoHeight      int
	VideoFPS         int
	AudioBitrateKbps int
	AudioChannels    int
	// Tracks limits which tracks to produce. Zero means audio+video.
	Tracks Track
}

EncodeOptions tunes the ffmpeg encode for transcoding sources. Zero values become sensible defaults.

type FrameReader

type FrameReader = frameReader

FrameReader is the exported alias for the internal frameReader so other packages in the module can hold one.

func NewOpusFrameReader

func NewOpusFrameReader(r stdio.Reader) (FrameReader, error)

NewOpusFrameReader exposes the internal opus reader for callers that have a raw ogg byte stream (e.g. instances/group_call).

func NewVP8FrameReader

func NewVP8FrameReader(r stdio.Reader, fps int) (FrameReader, error)

NewVP8FrameReader exposes the internal vp8 reader.

type SeekableSource

type SeekableSource interface {
	Source
	OpenAt(ctx context.Context, offset time.Duration) (*Streams, error)
}

SeekableSource is a Source that can begin playback at an offset. Only file/URL transcode sources implement it; pre-encoded passthrough sources and stdin-fed reader sources do not.

type Source

type Source interface {
	Tracks() Track
	Open(ctx context.Context) (*Streams, error)
}

Source is the public input abstraction. A Source is created lazily; Open spawns whatever processes are needed and returns the encoded audio+video byte streams.

func FromFile

func FromFile(path string, opt EncodeOptions) Source

FromFile streams any ffmpeg-decodable file (mp4, mkv, webm, mp3, wav, ...). Seekable. Pass EncodeOptions{} for defaults.

func FromShell

func FromShell(cmdline string, track Track) Source

FromShell parses cmdline as a shell command (handling double-quoted arguments) and spawns it directly via exec — NOT via /bin/sh, so shell metacharacters in filenames cannot inject commands.

The command must produce Opus-in-OGG on stdout for audio or VP8-in-IVF for video. Missing essentials are filled in automatically:

  • input-side fast-probe flags (`-analyzeduration 0`, `-probesize 64k`) are inserted before `-i` if absent — cuts ~1-2 s of startup latency.
  • output-side flags (`-c:a libopus`, `-f ogg`, opus pacing/codec params, `pipe:1`) for audio, or (`-c:v libvpx`, `-f ivf`, `pipe:1`) for video, are appended if not already present.

Raw PCM output codecs (pcm_s16le, etc.) are still rejected up front since the frame readers can't parse them.

func FromShells added in v0.5.9

func FromShells(audioCmd, videoCmd string) Source

FromShells builds a Source from two separate ffmpeg commands — one for audio, one for video. Either string may be empty to skip that track. Mirrors ntgcalls' MediaDescription(microphone, camera) pattern for users who want full control over both legs.

Each cmd goes through the same auto-flag injection as FromShell, so a minimal `ffmpeg -i movie.mp4` works for either leg.

func FromURL

func FromURL(url string, opt EncodeOptions) Source

FromURL streams from a URL (http(s), hls/.m3u8, rtmp, ...). Seekable. Pass EncodeOptions{} for defaults.

type SourcePath

type SourcePath interface {
	InputPath() string
	InputArgs() []string
	EncodeOpts() EncodeOptions
}

SourcePath is implemented by Sources backed by a plain file path or URL. The RTMP transport uses this to feed ffmpeg directly without going through the Source.Open() OGG/IVF pipeline.

type Streamer

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

Streamer pulls Samples from a FrameReader at the sample's natural cadence and pushes them to a Writer. Mute (audio) skips WriteSample but keeps the clock advancing. Pause (set via SetPaused) blocks the pull loop on a channel without tearing down the underlying ffmpeg process — the OS pipe buffer absorbs ~1s of OGG bytes while paused; on resume the loop wakes and drains them at real-time pace.

func NewStreamer

func NewStreamer(parent context.Context, src FrameReader, writer Writer, log *slog.Logger, onEnd func(error)) *Streamer

func (*Streamer) Done

func (s *Streamer) Done() <-chan struct{}

Done is closed when the streamer has finished (EOF, error, or Stop).

func (*Streamer) ElapsedMs

func (s *Streamer) ElapsedMs() uint64

ElapsedMs returns cumulative ms of samples handed to the pacing loop.

func (*Streamer) IsPaused added in v0.3.0

func (s *Streamer) IsPaused() bool

IsPaused reports whether the gate is currently blocking the loop.

func (*Streamer) Muted

func (s *Streamer) Muted() bool

func (*Streamer) SetMuted

func (s *Streamer) SetMuted(m bool)

func (*Streamer) SetPaused added in v0.3.0

func (s *Streamer) SetPaused(p bool)

SetPaused gates the run loop without canceling the context or closing the source. While paused the pull loop blocks on a channel; ffmpeg keeps running and its stdout pipe buffers the next ~1s of frames. On resume the loop wakes and resumes at real time.

func (*Streamer) Start

func (s *Streamer) Start()

Start kicks off the pacing goroutine. Returns immediately. The run loop services its own ctx cancellation via channel select — no separate cancel-watcher goroutine is needed.

func (*Streamer) Stop

func (s *Streamer) Stop()

Stop cancels the run loop and closes the underlying frame reader. Safe to call from any goroutine. Blocks until the run loop exits.

type Streams

type Streams struct {
	Audio stdio.Reader
	Video stdio.Reader
	// contains filtered or unexported fields
}

Streams is the encoded output of a Source: ogg/Opus audio and/or IVF/VP8 video. A nil reader means that track is absent. Close releases any underlying ffmpeg processes and pipes.

func (*Streams) Close

func (s *Streams) Close() error

type Track

type Track int

Track is a bitmask selecting which tracks a Source provides.

const (
	TrackAudio Track = 1 << iota
	TrackVideo
)

func (Track) Has

func (t Track) Has(x Track) bool

type Writer

type Writer interface {
	WriteSample(s media.Sample) error
}

Writer consumes encoded samples. Implemented by pion's TrackLocalStaticSample and by test fakes.

Jump to

Keyboard shortcuts

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