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
- Variables
- func CheckPath(p string) error
- func EncodeResult(r Result) string
- func ParseFrameRate(s string) (num, den int, err error)
- func Retryable(class string) bool
- func Status(class string) int
- type Audio
- type ErrorBody
- type Limits
- type Request
- type Result
- type Segment
- type Source
- type SubtitleRequest
- type Timing
- type Video
Constants ¶
const ( TrackVideo = "video" TrackAudio = "audio" )
Tracks.
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.
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.
const ( KindSegment = "segment" KindInit = "init" KindSubtitle = "subtitle" )
Kinds of request.
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.
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 = "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).
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.
const AlignPackage = "package"
AlignPackage asks for a package rendition's own segment boundaries.
const DefaultAudioKbps = 160
DefaultAudioKbps is the AAC-LC stereo bitrate when a request names none.
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 ¶
var ErrBadRequest = errors.New("bad request")
ErrBadRequest wraps every validation error.
Functions ¶
func CheckPath ¶
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 ¶
EncodeResult is r as the HeaderResult header.
func ParseFrameRate ¶
ParseFrameRate reads "24000/1001", "25" or "23.976".
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.
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 ¶
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).
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 ¶
IsRendition says whether the source is a package rendition.
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.