format

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 16 Imported by: ? modules, 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 format defines the container contracts: probing detects what bytes are (format and streams) before anything opens, demuxers read packets out of containers, and muxers write them back in. The per-runtime registry maps format ids to adapters; demuxers that implement Seeker make file inputs honour the Seek/Segment time controls.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound reports a format with no registered adapter, or a probe with
	// no matching rule.
	ErrNotFound = errors.New("format: not found")
	// ErrNilDemuxer reports a nil demuxer where one is required.
	ErrNilDemuxer = errors.New("format: nil demuxer")
	// ErrNilMuxer reports a nil muxer where one is required.
	ErrNilMuxer = errors.New("format: nil muxer")
	// ErrNilPacket reports a nil packet handed to a mux write.
	ErrNilPacket = errors.New("format: nil packet")
	// ErrResultFull reports a result buffer at capacity (AddEvent).
	ErrResultFull = errors.New("format: result capacity full")
)

Format sentinels, matched with errors.Is.

View Source
var ErrNotSeekable = errors.New("format: input is not seekable")

ErrNotSeekable reports a reposition requested on input that cannot seek (a pipe, a network stream, a plain io.Reader). Demuxers wrap it from Seek so callers can tell "this input cannot seek" from a failed seek on seekable input.

View Source
var ErrRateUnsupported = errors.New("format: offline demux pump runs unpaced; rate control needs a realtime (paced) task")

ErrRateUnsupported reports a playback-rate control sent to a task with no paced delivery to scale: an offline (non-realtime) task pumps as fast as the graph drains, so control.Rate is rejected honestly instead of pretending to change speed. Task.Control wraps it when the task's shared timeline has no paced consumer (no realtime demux pacer, no clock-aware source).

Functions

func NewDemuxSource

func NewDemuxSource(config DemuxSourceConfig) (pipeline.Source, error)

NewDemuxSource builds the pump that drives Demuxer.ReadInto into a pipeline. When the demuxer implements Seeker the returned source also implements pipeline.ControllableSource, honouring av.EventSeek and av.EventSegment by repositioning through the demuxer (Control records the request; the Start loop applies it and emits av.EventDiscontinuity before the first message at the new position). A demuxer without Seeker yields a plain source — time controls report the standard not-controllable error at the graph seam, never a silent ignore.

With Realtime set the pump paces delivery on the configured clock — each packet emits when its media time is due — and every reposition (seek, segment start, demuxer discontinuity) re-anchors the pacing anchor so playback resumes paced at the new position. Playback rate is not a source control: it scales the task's shared timeline clock instead.

Types

type DemuxSource

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

DemuxSource adapts a Demuxer into a pipeline.Source: it pumps ReadInto results downstream, paces realtime playback on the configured clock, and — when the demuxer implements Seeker — honours Seek/Segment time controls.

func (*DemuxSource) ByTime

func (s *DemuxSource) ByTime(interleave.Token) bool

ByTime marks an offline pump as one a direct graph may interleave by packet time with its sibling file sources (the module-internal interleave contract); a paced, realtime pump runs on its own.

func (*DemuxSource) Close

func (s *DemuxSource) Close() error

Close closes the wrapped demuxer once.

func (*DemuxSource) DescribeNode

func (s *DemuxSource) DescribeNode() pipeline.NodeSpec

DescribeNode reports the source's spec entry for Describe.

func (*DemuxSource) Name

func (s *DemuxSource) Name() string

Name returns the source's node name.

func (*DemuxSource) Start

func (s *DemuxSource) Start(ctx context.Context, emitter pipeline.Emitter) error

Start announces the demuxer's streams, then pumps packets and events until end of media, applying recorded time controls between reads and pacing delivery when realtime.

type DemuxSourceConfig

type DemuxSourceConfig struct {
	Name string
	// Detail is optional graph-rendering context for humans.
	Detail string
	// Demuxer reads container input into caller-owned Result scratch.
	Demuxer Demuxer
	// Result owns the packet pointer and event slice reused for every read.
	Result ReadResult
	// Realtime paces the pump on a clock: each packet is delivered when its
	// media time is due (PTS-paced playback). False (offline) pumps as fast as
	// the sink drains — a transcode job must not run at 1x.
	Realtime bool
	// Clock is the time source realtime pacing runs on — for recipe-built
	// tasks the task's shared timeline, so task-wide rate changes scale the
	// pace; nil defaults to av.MonotonicClock(). Tests inject a fake so
	// nothing sleeps for real. Ignored when Realtime is false.
	Clock av.Clock
}

DemuxSourceConfig configures a DemuxSource: the opened demuxer, the caller-owned read scratch, and the pacing mode/clock.

type Demuxer

type Demuxer interface {
	Format() av.FormatID
	Open(context.Context, Input, OpenOptions) error
	Streams() []av.Stream
	ReadInto(context.Context, *ReadResult) error
	Close() error
}

Demuxer reads one container: Open binds the input, Streams reports what it carries, and ReadInto fills caller-owned results until io.EOF. A demuxer that also implements Seeker honours Seek/Segment controls.

type DemuxerFactory

type DemuxerFactory interface {
	NewDemuxer(context.Context, ProbeResult) (Demuxer, error)
}

DemuxerFactory opens demuxers for a probed format; registries hold factories so nothing opens during planning.

type Descriptor

type Descriptor struct {
	Format     av.FormatID
	Media      []av.MediaType
	Codecs     []av.CodecID
	MinStreams int
	MaxStreams int
	Realtime   bool
	Metadata   av.Metadata
}

Descriptor declares a container format's capabilities — which media kinds, codecs, and stream counts it can carry — so destination validation refuses impossible mux plans before anything opens.

type Input

type Input struct {
	Name     string
	URI      string
	Protocol av.ProtocolID
	MIMEType string
	Reader   io.Reader
	ReaderAt io.ReaderAt
	Size     int64
	Realtime bool
	Metadata av.Metadata
}

Input describes where demuxable bytes come from: a reader (or random-access ReaderAt), the naming facts probing uses (Name, URI, MIMEType), and pacing.

type Magic

type Magic struct {
	Offset int
	Bytes  []byte
}

Magic is one byte signature a probe rule matches at a fixed offset.

type MuxStage

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

MuxStage adapts a Muxer into a pipeline.Stage: packet messages are written to the container in delivery order; events pass through.

func NewMuxStage

func NewMuxStage(config MuxStageConfig) (*MuxStage, error)

NewMuxStage wraps an opened muxer as a stage; the muxer is required.

func (*MuxStage) Close

func (s *MuxStage) Close() error

Close closes the wrapped muxer once, finalizing the container.

func (*MuxStage) DescribeNode

func (s *MuxStage) DescribeNode() pipeline.NodeSpec

DescribeNode reports the stage's spec entry for Describe.

func (*MuxStage) Handle

func (s *MuxStage) Handle(ctx context.Context, msg *pipeline.Message, emitter pipeline.Emitter) error

Handle writes one packet message to the muxer. A muxer consumes packets, so a frame message is refused (a decoded frame reaching a muxer is a wiring bug).

func (*MuxStage) MarkFailed

func (s *MuxStage) MarkFailed()

MarkFailed tells a transactional muxer the run failed, so its Close aborts instead of committing.

func (*MuxStage) Name

func (s *MuxStage) Name() string

Name returns the stage's node name.

type MuxStageConfig

type MuxStageConfig struct {
	Name string
	// Detail is optional graph-rendering context for humans.
	Detail string
	// Muxer writes packet messages to the configured output.
	Muxer Muxer
	// Result owns the event slice reused for every write.
	Result WriteResult
}

MuxStageConfig configures a MuxStage: the opened muxer and the caller-owned write scratch.

type Muxer

type Muxer interface {
	Format() av.FormatID
	Open(context.Context, Output, []av.Stream, OpenOptions) error
	Write(context.Context, *av.Packet, *WriteResult) error
	Close() error
}

Muxer writes one container: Open binds the output and the final stream set, Write appends packets in delivery order, Close finalizes the file.

type MuxerFactory

type MuxerFactory interface {
	NewMuxer(context.Context, av.FormatID) (Muxer, error)
}

MuxerFactory opens muxers for a format id; registries hold factories so nothing opens during planning.

type OpenOptions

type OpenOptions struct {
	Realtime bool
	Metadata av.Metadata
}

OpenOptions carries open-time flags for demuxers and muxers, notably realtime pacing.

type Output

type Output struct {
	Name     string
	URI      string
	Protocol av.ProtocolID
	MIMEType string
	Writer   io.Writer
	Realtime bool
	Fragment time.Duration
	Metadata av.Metadata
}

Output describes where muxed bytes go: the writer plus the naming facts output-format probing uses. Fragment asks a muxer that writes fragments, such as fragmented MP4, to keep each no longer than it, as a low-latency packager's parts need; zero leaves the muxer's own cadence.

type ProbeRequest

type ProbeRequest struct {
	Name     string
	MIMEType string
	Header   []byte
	Input    Input
}

ProbeRequest is what a prober sees: the name and MIME hints, the leading header bytes when available, and the full input.

type ProbeResult

type ProbeResult struct {
	Format   av.FormatID
	Score    int
	Streams  []av.Stream
	Reason   string
	Metadata av.Metadata
}

ProbeResult is a prober's verdict: the detected format, a confidence score (higher wins across probers), and the streams discovered without opening a demuxer.

type ProbeRule

type ProbeRule struct {
	Format     av.FormatID
	Protocol   av.ProtocolID
	Extensions []string
	MIMETypes  []string
	Magic      []Magic
	Score      int
	Reason     string
}

ProbeRule maps name extensions, MIME types, protocols, and byte signatures to one format with a confidence score.

func DefaultProbeRules

func DefaultProbeRules() []ProbeRule

DefaultProbeRules returns the built-in detection table: Ogg, IVF, FLV, Matroska/WebM, MP4, Annex B, ADTS, MP3, FLAC, and WAV. A byte signature scores above a MIME type, which scores above an extension, so content decides and the name is a hint.

type Prober

type Prober interface {
	Probe(context.Context, ProbeRequest) (ProbeResult, error)
}

Prober detects a format from a probe request; registries query every registered prober and keep the highest-scoring result.

type ReadResult

type ReadResult struct {
	// Packet is caller-owned scratch filled by Demuxer.ReadInto when
	// PacketReady is true. Its payload need only stay valid until the next
	// ReadInto: the source copies it once into a pooled buffer to publish
	// it, unless the demuxer filled it from av.NewBuffer.
	Packet *av.Packet
	// PacketReady reports whether Packet contains a packet for this read.
	PacketReady bool
	// Events is caller-owned scratch filled without growing past capacity.
	Events []av.Event
}

ReadResult is the caller-owned output buffer Demuxer.ReadInto fills: at most one packet plus bounded events per read, so demuxing stays allocation-free.

func (*ReadResult) AddEvent

func (r *ReadResult) AddEvent(event av.Event) error

AddEvent appends an event without growing past capacity; ErrResultFull reports an exhausted buffer.

func (*ReadResult) Reset

func (r *ReadResult) Reset()

Reset clears the result for reuse, keeping allocated capacity.

type SeekChecker

type SeekChecker interface {
	CanSeek() bool
}

SeekChecker is the optional capability of a Seeker whose seekability depends on its open input (e.g. a seekable file vs. a pipe). Returning false causes DemuxSource to refuse seeks up front with ErrNotSeekable.

type Seeker

type Seeker interface {
	Seek(ctx context.Context, position time.Duration) error
}

Seeker is an optional Demuxer capability, discovered by type assertion like every capability in this codebase: a demuxer that can reposition its input to a media time. Seek repositions the demuxer so the next ReadInto resumes at the nearest independently-decodable point at or before position (keyframe-aligned where the container indexes keyframes); when position precedes the first indexed point the demuxer repositions to the earliest available data. Position is measured from the start of the media.

Seek is never called concurrently with ReadInto: DemuxSource records a requested reposition in Control and applies it from its pump loop between reads, so implementations need no locking against their own read path.

A demuxer whose seeking depends on its input also implements CanSeek() bool, answered once it is open: false (a pipe, a plain io.Reader) makes the source it feeds refuse seeks up front with ErrNotSeekable and keep playing. Seek on such input reports an error wrapping ErrNotSeekable.

type SimpleRegistry

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

SimpleRegistry is the per-runtime format registry: probers for detection plus demuxer/muxer factories and capability descriptors keyed by format id. Registration is last-wins; it is populated at runtime construction and read-only afterwards.

func NewRegistry

func NewRegistry() *SimpleRegistry

NewRegistry returns an empty format registry.

func (*SimpleRegistry) DemuxerDescriptor

func (r *SimpleRegistry) DemuxerDescriptor(format av.FormatID) (Descriptor, error)

DemuxerDescriptor returns the capability descriptor registered for a format's demuxer, or ErrNotFound.

func (*SimpleRegistry) DemuxerDescriptors

func (r *SimpleRegistry) DemuxerDescriptors() []Descriptor

DemuxerDescriptors lists every registered demuxer descriptor sorted by format.

func (*SimpleRegistry) DemuxerFactory

func (r *SimpleRegistry) DemuxerFactory(format av.FormatID) (DemuxerFactory, error)

DemuxerFactory returns the demuxer factory registered for a format id, or ErrNotFound.

func (*SimpleRegistry) MuxerDescriptor

func (r *SimpleRegistry) MuxerDescriptor(format av.FormatID) (Descriptor, error)

MuxerDescriptor returns the capability descriptor registered for a format's muxer, or ErrNotFound.

func (*SimpleRegistry) MuxerDescriptors

func (r *SimpleRegistry) MuxerDescriptors() []Descriptor

MuxerDescriptors lists every registered muxer descriptor sorted by format.

func (*SimpleRegistry) MuxerFactory

func (r *SimpleRegistry) MuxerFactory(format av.FormatID) (MuxerFactory, error)

MuxerFactory returns the muxer factory registered for a format id, or ErrNotFound.

func (*SimpleRegistry) Probe

func (r *SimpleRegistry) Probe(ctx context.Context, request ProbeRequest) (ProbeResult, error)

Probe runs every registered prober and returns the highest-scoring result, or ErrNotFound when none match.

func (*SimpleRegistry) RegisterDemuxer

func (r *SimpleRegistry) RegisterDemuxer(format av.FormatID, factory DemuxerFactory)

RegisterDemuxer records a demuxer factory for a format id; last registration wins.

func (*SimpleRegistry) RegisterDemuxerDescriptor

func (r *SimpleRegistry) RegisterDemuxerDescriptor(desc Descriptor, factory DemuxerFactory)

RegisterDemuxerDescriptor records a demuxer factory together with the capability descriptor validation reads.

func (*SimpleRegistry) RegisterMuxer

func (r *SimpleRegistry) RegisterMuxer(format av.FormatID, factory MuxerFactory)

RegisterMuxer records a muxer factory for a format id; last registration wins.

func (*SimpleRegistry) RegisterMuxerDescriptor

func (r *SimpleRegistry) RegisterMuxerDescriptor(desc Descriptor, factory MuxerFactory)

RegisterMuxerDescriptor records a muxer factory together with the capability descriptor destination validation reads.

func (*SimpleRegistry) RegisterProber

func (r *SimpleRegistry) RegisterProber(prober Prober)

RegisterProber adds a format prober; Probe consults every registered one.

type StaticProber

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

StaticProber detects formats from a fixed rule table — the default content sniffing every runtime starts with.

func DefaultProber

func DefaultProber() StaticProber

DefaultProber returns a StaticProber over DefaultProbeRules — the content sniffing every runtime registers at construction.

func NewStaticProber

func NewStaticProber(rules ...ProbeRule) StaticProber

NewStaticProber builds a prober over the given rules.

func (StaticProber) Probe

func (p StaticProber) Probe(ctx context.Context, request ProbeRequest) (ProbeResult, error)

Probe matches the request against the rule table and returns the best match, or ErrNotFound when nothing matches.

type WriteResult

type WriteResult struct {
	Events []av.Event
}

WriteResult is the caller-owned output buffer Muxer.Write fills with any events the write produces.

func (*WriteResult) AddEvent

func (r *WriteResult) AddEvent(event av.Event) error

AddEvent appends an event without growing past capacity; ErrResultFull reports an exhausted buffer.

func (*WriteResult) Reset

func (r *WriteResult) Reset()

Reset clears the result for reuse, keeping allocated capacity.

Jump to

Keyboard shortcuts

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