stream

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: AGPL-3.0 Imports: 26 Imported by: 0 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 stream serves media files directly or through on-the-fly HLS transcodes.

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
var ErrHomeNetwork = errors.New("addresses on the home network aren't allowed")

ErrHomeNetwork is a connection Remote refused to make.

View Source
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.

View Source
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.

View Source
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

func Input(it store.Item) []string

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

func MimeType(path string) string

MimeType guesses a file's content type for clients deciding whether they can play it.

func Playlist added in v0.1.5

func Playlist(steps []Step) string

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

func ServeDirect(w http.ResponseWriter, r *http.Request, it store.Item)

ServeDirect streams the original file with range support, so seeking is instant.

Types

type Accelerator

type Accelerator interface {
	Active() *hwaccel.Accel
}

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

func Ladder(it store.Item, offered []Step, onCPU bool) []Step

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.

func StepByID added in v0.1.5

func StepByID(id string) (Step, bool)

StepByID finds one of Steps.

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) Close

func (t *Transcoder) Close()

Close stops every session.

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.

Jump to

Keyboard shortcuts

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