hls

package
v0.3.0 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: 18 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 hls packages goav output as HTTP Live Streaming (RFC 8216): fragmented MP4 segments and a media playlist, for live or on-demand delivery.

A packager is a provider.Destination. goav muxes the streams routed to it into fragmented MP4, cut at video keyframes, and the packager splits that byte stream: the ftyp and moov become the init segment, and fragments group into media segments of about the target duration, each starting at a keyframe. After every segment it republishes the playlist; at the end it marks the playlist complete.

live := goav.Custom(hls.New("live.m3u8", hls.Dir("/srv/hls"), hls.Window(6)))
err := bundle.Run(ctx, video.To(live), audio.To(live))

With Window(n), a live playlist lists the last n segments and the packager removes older ones once players can no longer be fetching them; without it, the playlist keeps every segment, an event that becomes an on-demand playlist when the run ends.

A Ladder is an adaptive-bitrate set: each of its variants is a packager, and it writes the multivariant playlist that lists them with their bandwidth, codecs (RFC 6381, read from each init segment), and resolution.

With Parts, a playlist is low-latency HLS: the muxer cuts fragments of at most the part duration, each published as an EXT-X-PART with the next one hinted (EXT-X-PRELOAD-HINT) and wall-clock times on the segments (EXT-X-PROGRAM-DATE-TIME), delta updates for long playlists (EXT-X-SKIP past six target durations), and, in a ladder, rendition reports of where the other renditions stand. A Server holds what LL-HLS asks a server to hold: blocking playlist reloads (_HLS_msn, _HLS_part) and requests for a hinted part. Serve it over HTTPS, so players get HTTP/2.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(playlist string, store Store, options ...Option) provider.Destination

New returns a packager writing the playlist (a name such as "live.m3u8") and its segments to store. Segment names derive from the playlist's: live-init.mp4, live-00000.m4s, and so on.

Types

type Ladder

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

Ladder is an adaptive-bitrate set of renditions. Each Variant is a packager with its own playlist; the ladder writes the multivariant playlist that lists them with their bandwidth, codecs, and resolution, so a player picks the rendition its connection carries and switches as that changes.

ladder := hls.NewLadder("master.m3u8", hls.Dir("/srv/hls"))
hd := goav.Custom(ladder.Variant("hd.m3u8", hls.Window(6)))
sd := goav.Custom(ladder.Variant("sd.m3u8", hls.Window(6)))

It publishes the multivariant playlist once every variant has a segment, and again when a variant's peak bandwidth rises.

func NewLadder

func NewLadder(name string, store Store) *Ladder

NewLadder returns a ladder writing the multivariant playlist name to store, beside its variants' playlists.

func (*Ladder) Variant

func (l *Ladder) Variant(playlist string, options ...Option) provider.Destination

Variant returns the packager of one rendition, writing its playlist and segments to the ladder's store. Route each rendition's streams to its own variant; audio routed to every variant rides in each.

type Option

type Option func(*config)

Option configures a packager.

func Parts

func Parts(d time.Duration) Option

Parts makes the playlist low-latency (LL-HLS): segments are also published in parts of at most d (200ms to 1s is typical), each a CMAF chunk listed as an EXT-X-PART with the next one hinted, so a player plays within a few parts of the live edge. LL-HLS needs a server that holds blocking playlist reloads and hinted-part requests: package into an hls.Server and serve it.

func SegmentDuration

func SegmentDuration(d time.Duration) Option

SegmentDuration sets the target segment length (default 4s). A segment ends at the first fragment boundary, a video keyframe, at or past it, so segments are as long as the stream's keyframe interval allows.

func Window

func Window(n int) Option

Window makes the playlist live: it lists the last n segments, and the packager removes a segment once it has been out of the playlist for another n. Zero, the default, keeps every segment.

type Server

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

Server keeps packagers' files in memory and serves them over HTTP, the origin a CDN or a player reads from. It is a Store, and it holds what LL-HLS asks a server to hold: a playlist request with _HLS_msn (and _HLS_part) waits until the playlist lists that segment (or part), and a request for the part a playlist hints waits until it is published. A request waits at most three target durations, then gets 503.

srv := hls.NewServer()
live := goav.Custom(hls.New("live.m3u8", srv, hls.Parts(250*time.Millisecond), hls.Window(6)))
http.Handle("/live/", http.StripPrefix("/live/", srv))

func NewServer

func NewServer() *Server

NewServer returns an empty server.

func (*Server) Create

func (s *Server) Create(name string) (io.WriteCloser, error)

Create returns a writer whose Close publishes name.

func (*Server) Remove

func (s *Server) Remove(name string) error

Remove deletes name.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the file the path names, relative to where the server is mounted.

type Store

type Store interface {
	Create(name string) (io.WriteCloser, error)
	Remove(name string) error
}

Store is where a packager writes its files. Create returns a writer whose Close publishes the file whole, so a player never reads half a playlist; Remove deletes a segment that has left a live playlist.

func Dir

func Dir(path string) Store

Dir is a Store in a directory of the local file system. It publishes a file by writing a temporary one beside it and renaming it into place, readable by the web server that serves the directory.

Jump to

Keyboard shortcuts

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