Documentation
¶
Overview ¶
Package stream serves media files directly or through on-the-fly HLS transcodes.
Index ¶
- Variables
- func Input(it store.Item) []string
- func MimeType(path string) string
- func Playlist(steps []Step) string
- func ServeDirect(w http.ResponseWriter, r *http.Request, it store.Item)
- type Accelerator
- type SessionInfo
- 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) Sessions() []SessionInfo
- func (t *Transcoder) Start(ctx context.Context, userID int64, it store.Item, startSec float64, ...) (string, error)
Constants ¶
This section is empty.
Variables ¶
var AllowHomeNetwork atomic.Bool
AllowHomeNetwork lets Remote reach addresses on the home network and the server itself. Off, anyone who can add a podcast can't use a feed to make the server fetch from other devices at home, like a router's page.
var ErrHomeNetwork = errors.New("addresses on the home network aren't allowed")
ErrHomeNetwork is a connection Remote refused to make.
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 Remote = &http.Client{Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: (&net.Dialer{Timeout: 15 * time.Second, Control: publicOnly}).DialContext, ResponseHeaderTimeout: 20 * time.Second, IdleConnTimeout: 90 * time.Second, MaxIdleConnsPerHost: 4, }}
Remote is how the server fetches media that lives on the internet, such as podcast episodes: no overall time limit, since an episode can play for hours, but a quick one for the answer to start.
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 Input ¶ added in v0.1.7
Input is FFmpeg's input options for an item's media: its file, or its address on the internet, reconnecting if the host drops the connection.
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 SessionInfo ¶ added in v0.1.11
type SessionInfo struct {
ID string `json:"id"`
UserID int64 `json:"userId"`
ItemID int64 `json:"itemId"`
// Started is when FFmpeg started (milliseconds), StartSec where in the
// file it started, and LastSeen when a player last asked for a piece.
Started int64 `json:"started"`
StartSec float64 `json:"startSec"`
LastSeen int64 `json:"lastSeen"`
// Kind is what FFmpeg does: "video" converts the picture, "copy" only
// repackages it (the sound may still be converted), "audio" converts
// sound alone.
Kind string `json:"kind"`
// Accel names the hardware doing the work, such as "VAAPI"; empty for
// the CPU. Mode is how much of it is on the GPU: cpu, encode or full.
Accel string `json:"accel,omitempty"`
AccelID string `json:"accelId,omitempty"`
Mode string `json:"mode"`
Quality []string `json:"quality,omitempty"`
BurnSub bool `json:"burnSub,omitempty"`
Adaptive bool `json:"adaptive,omitempty"`
FFmpegPID int `json:"pid,omitempty"`
}
SessionInfo describes a running transcode, for the admin dashboard.
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) Sessions ¶ added in v0.1.11
func (t *Transcoder) Sessions() []SessionInfo
Sessions lists the running transcodes, oldest first.
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.