ffruntime

package
v0.0.0-...-d18ddae Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: GPL-3.0 Imports: 19 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 ffruntime is the one runtime gate every binary that links ffgo passes through (spec 2026-10-06 §7.4). Load maps FFmpeg 9 and the ffgo shim once and checks the shim's API; Require checks the demuxers, muxers, decoders, encoders and filters a caller needs; one process-wide log trampoline routes FFmpeg's log to slog (RouteLog), to a run's own sink (Capture) or nowhere (Mute); and Do bounds every in-process FFmpeg call by its context plus Grace, counting calls it had to abandon until they return, and reports the process wedged at MaxAbandoned outstanding calls; it recovers a panic in a call as the call's error (ErrPanicked, Panics). Do and Busy mark FFmpeg work in flight, and once none has been for IdleTrim the process hands its C heap's free pages back (TrimHeap).

ffgo dlopens FFmpeg at package init (avutil/avutil.go's init), not at Load, so any binary linking this package has mapped FFmpeg before main. It imports the standard library and ffgo only (TestFFRuntimeImportsOnlyFFgo).

Index

Constants

View Source
const (
	// AVCodecMajor is FFmpeg 9's libavcodec major.
	AVCodecMajor = 63
	// MinShimAPI is FFSHIM_API_VERSION of the ffgo this module builds
	// against: 2 since v0.0.0-clustarr.14 (frame_set_pict_type, a forced
	// keyframe). A shim before 1 (v0.0.0-clustarr.13) reads FFmpeg 9's
	// structs at FFmpeg 4-7's offsets (spec §7.1 fact 8).
	MinShimAPI = 2
	// Grace is how long Do waits for a call after its context ends: the old
	// subprocess WaitDelay.
	Grace = 5 * time.Second
	// MaxAbandoned outstanding abandoned calls wedge the process.
	MaxAbandoned = 4
)
View Source
const IdleTrim = 2 * time.Second

IdleTrim is how long no FFmpeg work may be in flight (Do, Busy) before the C heap is trimmed: long enough that a burst -- an import's season pack of probes, a season's segment decodes -- trims once, at its end.

Variables

View Source
var (
	// ErrUnavailable wraps every reason FFmpeg cannot be used here.
	ErrUnavailable = errors.New("ffruntime: FFmpeg is unavailable")
	// ErrWedged refuses calls once MaxAbandoned are outstanding.
	ErrWedged = errors.New("ffruntime: FFmpeg calls are stuck; restart the process")
	// ErrAbandoned is a call that outlived its context plus Grace.
	ErrAbandoned = errors.New("ffruntime: an FFmpeg call outlived its context and was abandoned")
	// ErrPanicked is wrapped by every *PanicError: an FFmpeg call that
	// panicked and was recovered by Do.
	ErrPanicked = errors.New("ffruntime: an FFmpeg call panicked")
)

Functions

func Abandoned

func Abandoned() int

Abandoned is how many abandoned calls are still running.

func Busy

func Busy() (done func())

Busy marks FFmpeg work in flight until the func it returns is called; Do marks its own calls. Once the last work in flight ends and none starts for IdleTrim, the C heap is trimmed (TrimHeap). The func may be called more than once; only the first call counts.

func Capture

func Capture(sink func(ffgo.LogLevel, string)) (release func())

Capture hands every FFmpeg line to sink until release is called.

func Do

func Do(ctx context.Context, f func(context.Context) error) error

Do runs f on its own goroutine. When ctx ends and f has not returned within Grace, Do returns an error wrapping ErrAbandoned and ctx.Err() and leaves f running; Abandoned counts it until it returns. At MaxAbandoned outstanding calls Wedged closes and Do refuses every call with ErrWedged. A caller records an abandoned result as non-transient (spec §6.6), except markers', which retries the season after a delay: its abandonments are NFS stalls (app/segments/worker; §6.6 as amended 2026-10-15).

A panic in f is recovered and returned as a *PanicError (ErrPanicked), and counted (Panics). f runs on Do's own goroutine, out of reach of any recover its caller holds -- the bus's handler recover among them -- so an unrecovered panic in a binding killed the whole process: on 2026-11-11 one FLAC's 4,216-byte lyrics tag (ffgo's goString, fixed in v0.0.0-clustarr.17) took the import agent down at every delivery of the music root's scan until the scan's attempts ran out. f's own defers run before the recover, so what it opened is closed; a binding left holding a lock wedges the next call, which Do abandons and, at MaxAbandoned, the liveness check restarts. A fault in C code (a segfault) is no Go panic and still ends the process.

func DoOn

func DoOn(ctx context.Context, file string, f func(context.Context) error) error

DoOn is Do for a call that reads file: every line FFmpeg logs from the call's thread while f runs says so ("file" on the ffmpeg record), and a message is counted as a repeat within its own file only. f runs locked to one OS thread (onThread), which is what ties a line to it. An empty file is Do.

func Healthz

func Healthz(*http.Request) error

Healthz is the process-level healthz check "ffgo" (spec §3.3): an error once wedged, so the kubelet restarts a pod a restart can cure.

func Mute

func Mute() (release func())

Mute stops FFmpeg's errors reaching slog until release is called; Capture sinks still see them.

func Panics

func Panics() int64

Panics is how many FFmpeg calls have panicked in this process, each recovered by Do.

func Require

func Require(n Needs) error

Require loads FFmpeg (Load) and fails naming every missing piece of n.

func RouteLog

func RouteLog(l *slog.Logger)

RouteLog sends FFmpeg's lines to l from now on: the caller hands it the ffmpeg subsystem's logger and wires SetVerbose to that subsystem's level (pkg/ffruntime/ffroute.Log does both), since this package links nothing of clustarr's but ffgo (TestFFRuntimeImportsOnlyFFgo).

func SetVerbose

func SetVerbose(on bool)

SetVerbose raises FFmpeg's own level to verbose and routes every line it then prints, or lowers it to warning and routes errors and worse again (the 2026-11-10 logging design §4: an ffmpeg: debug override in force).

func TrimHeap

func TrimHeap() bool

TrimHeap hands every free page of the C heap back to the kernel through glibc's malloc_trim(0), and reports whether it could: false where the C library has none (not glibc). It frees nothing in use, and it is safe beside running FFmpeg calls -- glibc trims each arena under that arena's lock -- though a call running then may wait a few milliseconds for it.

func Wedged

func Wedged() <-chan struct{}

Wedged closes once MaxAbandoned calls are outstanding; it never reopens.

Types

type Needs

type Needs struct{ Demuxers, Muxers, Decoders, Encoders, Filters []string }

Needs names what a caller asks of FFmpeg, by FFmpeg's names.

type PanicError

type PanicError struct {
	// Value is what was passed to panic.
	Value any
	// Frame is the function that panicked and its file:line, the first
	// frame below the panic outside the runtime; empty when none was found.
	Frame string
	// Stack is the panicking goroutine's stack, for a caller to log.
	Stack []byte
}

PanicError is a panic Do recovered from an FFmpeg call. It wraps ErrPanicked, and Value when Value is an error (a runtime.Error for a binding's out-of-range slice).

func (*PanicError) Error

func (e *PanicError) Error() string

Error names the panic and where it happened, short enough for a status message; the whole stack is in Stack.

func (*PanicError) Unwrap

func (e *PanicError) Unwrap() []error

Unwrap is ErrPanicked, and the panic's value when it is an error.

type Report

type Report struct {
	FFmpegMajor int    `json:"ffmpegMajor"`
	ShimAPI     int    `json:"shimAPI"`
	AVUtil      string `json:"avutil,omitempty"`
	AVCodec     string `json:"avcodec,omitempty"`
	AVFormat    string `json:"avformat,omitempty"`
	ShimPath    string `json:"shimPath,omitempty"`
}

Report is what Load found; the library paths are the files the dynamic loader mapped (/proc/self/maps), not the names asked for.

func Load

func Load() (Report, error)

Load loads FFmpeg and the shim once per process and keeps the first result: ffgo's own loader remembers a failure and cannot be retried.

Directories

Path Synopsis
Package ffmetrics exports pkg/ffruntime's abandoned-call and panic counts.
Package ffmetrics exports pkg/ffruntime's abandoned-call and panic counts.
Package ffroute wires FFmpeg's log lines into a process's logger under the ffmpeg subsystem (the 2026-11-10 logging design §4).
Package ffroute wires FFmpeg's log lines into a process's logger under the ffmpeg subsystem (the 2026-11-10 logging design §4).

Jump to

Keyboard shortcuts

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