Documentation
¶
Overview ¶
Package stream serves media files directly or through on-the-fly HLS transcodes.
Index ¶
- Variables
- func MimeType(path string) string
- func Playlist(steps []Step) string
- func ServeDirect(w http.ResponseWriter, r *http.Request, it store.Item)
- type Accelerator
- type Step
- type Transcoder
- func (t *Transcoder) Active() int
- func (t *Transcoder) Close()
- func (t *Transcoder) Kill(id string)
- func (t *Transcoder) ServeFile(w http.ResponseWriter, r *http.Request, userID int64, sessionID, name string)
- func (t *Transcoder) Start(ctx context.Context, userID int64, it store.Item, startSec float64, ...) (string, error)
Constants ¶
This section is empty.
Variables ¶
var ErrSuperseded = errors.New("replaced by a newer transcode")
ErrSuperseded means a newer Start for the same viewer and item replaced this one before it was ready, as happens while dragging the seek bar.
var Steps = []Step{
{ID: "1080p", Label: "1080p", Width: 1920, Height: 1080, VideoBPS: 8_000_000, AudioBPS: 192_000},
{ID: "720p", Label: "720p", Width: 1280, Height: 720, VideoBPS: 4_000_000, AudioBPS: 128_000},
{ID: "480p", Label: "480p", Width: 854, Height: 480, VideoBPS: 1_500_000, AudioBPS: 96_000},
{ID: "360p", Label: "360p", Width: 640, Height: 360, VideoBPS: 700_000, AudioBPS: 96_000},
}
Steps are every quality Matinee offers, best first. An admin picks which ones adaptive streams use.
Functions ¶
func MimeType ¶
MimeType guesses a file's content type for clients deciding whether they can play it.
func Playlist ¶ added in v0.1.5
Playlist is the playlist a transcode of steps starts from: a master playlist naming one playlist per step for an adaptive stream, or the single stream's own.
func ServeDirect ¶
ServeDirect streams the original file with range support, so seeking is instant.
Types ¶
type Accelerator ¶
Accelerator says which hardware accelerator new transcodes use (see hwaccel.Manager); nil means the CPU.
type Step ¶ added in v0.1.5
type Step struct {
ID string `json:"id"`
Label string `json:"label"`
// Width caps the picture's width; narrower video keeps its size.
Width int `json:"width"`
Height int `json:"height"`
// VideoBPS is the peak video bitrate and AudioBPS the audio's, in bits
// per second.
VideoBPS int `json:"videoBps"`
AudioBPS int `json:"audioBps"`
}
Step is one picture quality a transcode can make. A transcode made of several steps is an adaptive stream: players move between them as the connection allows.
func Ladder ¶ added in v0.1.5
Ladder picks the steps an adaptive stream of it makes, best first: the ones offered that aren't bigger than the file itself. A file smaller than every step still gets the smallest. onCPU keeps it to the best step and one from the middle, so there's somewhere to step down to.
type Transcoder ¶
type Transcoder struct {
FFmpeg string
Dir string
Log *slog.Logger
// HW picks the hardware accelerator, if any; nil transcodes on the CPU.
HW Accelerator
// contains filtered or unexported fields
}
Transcoder runs one FFmpeg process per playback session and serves its HLS output.
func NewTranscoder ¶
func NewTranscoder(ffmpeg, dir string, hw Accelerator, log *slog.Logger) *Transcoder
func (*Transcoder) Active ¶
func (t *Transcoder) Active() int
Active is how many transcodes are running, so background work can wait until nobody is watching.
func (*Transcoder) Kill ¶
func (t *Transcoder) Kill(id string)
Kill stops a session and deletes its files.
func (*Transcoder) ServeFile ¶
func (t *Transcoder) ServeFile(w http.ResponseWriter, r *http.Request, userID int64, sessionID, name string)
ServeFile serves a playlist or segment, waiting briefly for segments FFmpeg hasn't written yet.
func (*Transcoder) Start ¶
func (t *Transcoder) Start(ctx context.Context, userID int64, it store.Item, startSec float64, burnSub, audio int, steps ...Step) (string, error)
Start begins a transcode of item from startSec and returns the session id once the first segment is ready. A user watching an item has at most one session for it, so seeking far ahead replaces the old one. burnSub is an embedded subtitle stream to draw into the video, or -1 for none. audio is which of the file's audio streams to play (FFmpeg's 0:a:N), 0 for the first.
steps picks the picture quality. None keeps the original as far as it can (video that's already H.264 is copied, anything else re-encoded at up to 1080p). One makes that quality. Several make an adaptive stream of the ones that suit the file (see Ladder), with a master playlist; Playlist says which playlist the session starts from.
With a hardware accelerator, a transcode that fails to start on the GPU starts again with less of it on the GPU, and finally on the CPU alone.