api

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MPL-2.0 Imports: 8 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 api is the live worker's wire contract: what a stream origin sends to have one segment of a live rung made (POST /v1/segment), a rung's initialization segment (POST /v1/init) or a text subtitle track as WebVTT (POST /v1/subtitle), and what comes back. The supervisor and its worker processes speak it to each other too, and a caller imports this package, so the two sides cannot drift.

A segment is a stateless job: "rung r, segment k of source s" comes out the same from any worker process, so a failed one is made again elsewhere.

Index

Constants

View Source
const (
	TrackVideo = "video"
	TrackAudio = "audio"
)

Tracks.

View Source
const (
	PriorityPlayhead = "playhead"
	PriorityPrefetch = "prefetch"
)

Priorities. A playhead job is one a player waits for: it may queue for an encode slot until its deadline. A prefetch job is made ahead of the play head: it runs only when a slot is free beyond the ones kept for playhead jobs, and is refused (503) otherwise.

View Source
const (
	TonemapAuto = "auto"
	TonemapOff  = "off"
)

Tone mapping of a video rung: auto maps HDR10/PQ, HLG and Dolby Vision profile 5 to SDR BT.709; off passes the source's range through.

View Source
const (
	KindSegment  = "segment"
	KindInit     = "init"
	KindSubtitle = "subtitle"
)

Kinds of request.

View Source
const (
	HeaderResult     = "Live-Worker-Result"      // JSON Result
	HeaderInitLength = "Live-Worker-Init-Length" // bytes of the body that are the init
	HeaderCodec      = "Live-Worker-Codec"       // the codec configuration's hash (SPS/PPS, AudioSpecificConfig)
	HeaderSlot       = "Live-Worker-Slot"        // the worker process that made it
	HeaderJob        = "Live-Worker-Job"
	HeaderAttempts   = "Live-Worker-Attempts" // how many worker processes were tried
	HeaderCoalesced  = "Live-Worker-Coalesced"
)

A segment's response body is the rung's initialization segment followed by the segment's fragment, both CMAF: the first HeaderInitLength bytes are the init (ftyp+moov), the rest the fragment (styp+moof+mdat). The two come from the same encoder, so the fragment always decodes with that init; a caller that already holds the rung's init compares HeaderCodec with it. The whole body is a playable fragmented MP4 as it is.

POST /v1/init answers the init alone, POST /v1/subtitle a WebVTT document. A failure answers ErrorBody as JSON with the status of its class.

View Source
const (
	// ClassSource: the source cannot make this (unreadable, undecodable,
	// missing the stream, ending before the segment does). Retrying does not
	// help: 422.
	ClassSource = "source"
	// ClassCircuit: the source failed too often lately; its circuit is open
	// for RetryAfterMs: 422.
	ClassCircuit = "circuit"
	// ClassBusy: no encode slot before the deadline, or a prefetch job with
	// no slot to spare: 503, retry later.
	ClassBusy = "busy"
	// ClassDeadline: the job ran out of time (or stalled past it): 504.
	ClassDeadline = "deadline"
	// ClassBad: the request is malformed or names a path outside the
	// media roots: 400.
	ClassBad = "bad-request"
	// ClassUnauthorized: a token is configured and the request lacks it: 401.
	ClassUnauthorized = "unauthorized"
	// ClassInternal: the worker processes failed it every time they were
	// tried (crashed, killed, an error of their own): 502.
	ClassInternal = "internal"
)

Error classes. Each has its status (Status).

View Source
const (
	ScopeSource  = "source"
	ScopeSegment = "segment"
)

Scopes of a source failure: the whole source (it cannot be opened, holds no such stream) or this segment of it.

View Source
const AlignPackage = "package"

AlignPackage asks for a package rendition's own segment boundaries.

View Source
const DefaultAudioKbps = 160

DefaultAudioKbps is the AAC-LC stereo bitrate when a request names none.

View Source
const MaxSegmentSec = 20

MaxSegmentSec bounds one segment. The encoder's GOP is sized for it (one IDR per segment, at its start), and a packager that cuts at keyframes can make a segment longer than its target; 20 s covers that.

Variables

View Source
var ErrBadRequest = errors.New("bad request")

ErrBadRequest wraps every validation error.

Functions

func CheckPath

func CheckPath(p string) error

CheckPath refuses what is not a plain absolute path: a relative one, one with "." or ".." in it, a URL (the media libraries would open it), or one holding the concatenation separator "|".

func EncodeResult

func EncodeResult(r Result) string

EncodeResult is r as the HeaderResult header.

func ParseFrameRate

func ParseFrameRate(s string) (num, den int, err error)

ParseFrameRate reads "24000/1001", "25" or "23.976".

func Retryable

func Retryable(class string) bool

Retryable says whether a failure of class may succeed when tried again on another worker process.

func Status

func Status(class string) int

Status is the HTTP status of a class.

Types

type Audio

type Audio struct {
	// Stream is the n-th audio stream of a file (0-based among its audio
	// streams); a rendition has one.
	Stream      int `json:"stream,omitempty"`
	BitrateKbps int `json:"bitrateKbps,omitempty"`
}

Audio is the stereo AAC rendition of one audio stream.

type ErrorBody

type ErrorBody struct {
	Error string `json:"error"`
	Class string `json:"class"`
	Scope string `json:"scope,omitempty"`
	Job   string `json:"job,omitempty"`
	// RetryAfterMs is when to try again (busy, circuit), also sent as
	// Retry-After in seconds.
	RetryAfterMs int `json:"retryAfterMs,omitempty"`
}

ErrorBody is the JSON body of a failure.

type Limits

type Limits struct {
	DecodeThreads int `json:"decodeThreads,omitempty"`
	EncodeThreads int `json:"encodeThreads,omitempty"`
	FilterThreads int `json:"filterThreads,omitempty"`
	// ExtraHWFrames is the decoder's hardware frames beyond its own pool
	// (the frames the filter graph and the encoder may hold).
	ExtraHWFrames int `json:"extraHwFrames,omitempty"`
	// StallSec ends a job that makes no progress for this long.
	StallSec float64 `json:"stallSec,omitempty"`
}

Limits bound one job. Zero values mean the worker's defaults.

type Request

type Request struct {
	Source  Source   `json:"source"`
	Track   string   `json:"track,omitempty"` // video (default) | audio
	Video   *Video   `json:"video,omitempty"`
	Audio   *Audio   `json:"audio,omitempty"`
	Segment *Segment `json:"segment,omitempty"`
	// Priority is playhead (default) or prefetch.
	Priority string `json:"priority,omitempty"`
	// DeadlineMs bounds the job, queueing and retries included (0: the
	// worker's default).
	DeadlineMs int    `json:"deadlineMs,omitempty"`
	Limits     Limits `json:"limits,omitempty"`
}

Request is one job: a segment (POST /v1/segment) or the initialization segment (POST /v1/init, Segment unset) of a rung of a source.

func (Request) Key

func (r Request) Key(kind string) string

Key identifies the job: requests with the same key make the same bytes (frames, timestamps and parameter sets), so concurrent ones are served by one job and a failed one is remembered under it. Priority, deadline and limits are not part of it.

func (*Request) Normalize

func (r *Request) Normalize(kind string) error

Normalize fills defaults and checks the request for kind (KindSegment or KindInit).

type Result

type Result struct {
	Job  string `json:"job"`
	Slot string `json:"slot,omitempty"`
	// Path is the pipeline that made it: cuda, cuda+libplacebo,
	// cuda+libplacebo-roundtrip, cuda+zscale-tonemap, cpu,
	// cpu+zscale-tonemap; aac for audio.
	Path    string `json:"path"`
	Encoder string `json:"encoder,omitempty"`
	Index   int    `json:"index"`
	// Start and End are the segment's boundary as resolved, seconds on the
	// request's timeline.
	Start float64 `json:"start"`
	End   float64 `json:"end"`
	Last  bool    `json:"last,omitempty"`
	// Width and Height are the frame encoded.
	Width     int    `json:"width,omitempty"`
	Height    int    `json:"height,omitempty"`
	Frames    int    `json:"frames"`
	Bytes     int    `json:"bytes"`
	Timescale uint32 `json:"timescale"`
	// FirstPTS and EndPTS are the fragment's first presentation time and
	// its end on its track's own timeline (timescale units): the next
	// segment's fragment starts at EndPTS.
	FirstPTS   int64   `json:"firstPts"`
	EndPTS     int64   `json:"endPts"`
	Dropped    int     `json:"dropped"` // frames decoded before the start and dropped
	InitLength int     `json:"initLength"`
	CodecHash  string  `json:"codecHash"`
	Kbps       float64 `json:"kbps"`
	Timing     Timing  `json:"timing"`
	Note       string  `json:"note,omitempty"`
}

Result describes what was made.

func DecodeResult

func DecodeResult(s string) (Result, error)

DecodeResult reads the HeaderResult header.

type Segment

type Segment struct {
	Index int       `json:"index"`
	Start float64   `json:"start,omitempty"`
	End   float64   `json:"end,omitempty"`
	Cuts  []float64 `json:"cuts,omitempty"`
	Align string    `json:"align,omitempty"`
	Last  bool      `json:"last,omitempty"`
}

Segment says which segment to make and where it starts and ends, in one of three forms:

  • a boundary: Start and End, seconds on the source's presentation timeline (a file's starts at zero: its container start time is taken off every timestamp);
  • a cut list: Cuts holds every segment's start, ascending; segment Index runs from Cuts[Index] to Cuts[Index+1], the last one to the source's end;
  • Align "package": a package rendition's own segment Index, its boundaries read from the rendition's media.

The segment holds the frames presented from the frame nearest its start to the frame nearest its end; consecutive segments made by any processes tile the timeline exactly. Last says the segment ends with the source, so the source may end inside it (a cut list's last segment, and one whose End is at the source's end, are last without it).

func (Segment) Resolve

func (s Segment) Resolve(sourceEnd float64) (start, end float64, last, ok bool, err error)

Resolve is the boundary of a segment given as a boundary or a cut list, for a source that ends at sourceEnd seconds (0: unknown). An aligned segment is the rendition's to resolve (ok false).

type Source

type Source struct {
	Path      string `json:"path,omitempty"`
	Rendition string `json:"rendition,omitempty"`
	// FrameRate is the title's frame rate ("24000/1001" or "23.976") when
	// the caller knows it, e.g. from a package manifest. The encoder writes
	// the rate into every segment's SPS, so it must be the title's, not an
	// estimate from the frames one job reads; given here, a rendition's
	// segments need not all be read to work it out.
	FrameRate string `json:"frameRate,omitempty"`
}

Source is what a job reads: an original media file, or a package rendition (its media playlist; its segments are read as one input). Paths are absolute and must lie under one of the worker's media roots.

func (Source) IsRendition

func (s Source) IsRendition() bool

IsRendition says whether the source is a package rendition.

func (Source) Key

func (s Source) Key() string

Key is the source's identity: its path or its rendition's playlist (the circuit breaker's key).

type SubtitleRequest

type SubtitleRequest struct {
	Source     Source  `json:"source"`
	Stream     int     `json:"stream"`
	StartSec   float64 `json:"startSec,omitempty"`
	DeadlineMs int     `json:"deadlineMs,omitempty"`
}

SubtitleRequest asks for the text subtitle stream Stream of a file as WebVTT (POST /v1/subtitle): its cues from StartSec on, shifted so StartSec is 0.

func (SubtitleRequest) Key

func (r SubtitleRequest) Key() string

Key identifies the job (see Request.Key).

func (*SubtitleRequest) Normalize

func (r *SubtitleRequest) Normalize() error

Normalize checks the request.

type Timing

type Timing struct {
	Queue   float64 `json:"queue"`   // waiting for a slot
	Open    float64 `json:"open"`    // opening the input
	Seek    float64 `json:"seek"`    // seeking to the keyframe before the start
	Setup   float64 `json:"setup"`   // decoder, filter graph and encoder
	First   float64 `json:"first"`   // to the first encoded packet
	Process float64 `json:"process"` // decode, filter, encode
	Mux     float64 `json:"mux"`     // writing the CMAF fragment
	Total   float64 `json:"total"`
}

Timing is where a job's time went, in milliseconds.

type Video

type Video struct {
	Width       int    `json:"width,omitempty"`
	Height      int    `json:"height"`
	BitrateKbps int    `json:"bitrateKbps"`
	Tonemap     string `json:"tonemap,omitempty"`
}

Video is a video rung: the frame it encodes and its bitrate. Width 0 keeps the source's aspect ratio at Height (even, never upscaled, fitted into the 16:9 box of that height); a caller that advertises a resolution sends both, so the frame encoded is the one its playlist names.

Jump to

Keyboard shortcuts

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