gohlslib

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2025 License: MIT Imports: 31 Imported by: 5

README

gohlslib

Test Lint Go Report Card CodeCov PkgGoDev

HLS client and muxer library for the Go programming language, written for MediaMTX.

Go ≥ 1.21 is required.

Features:

  • Client

    • Read streams in MPEG-TS, fMP4 or Low-latency format
    • Read a single video track and/or multiple audio tracks
    • Read tracks encoded with AV1, VP9, H265, H264, Opus, MPEG-4 Audio (AAC)
    • Get absolute timestamp of incoming data
  • Muxer

    • Generate streams in MPEG-TS, fMP4 or Low-latency format
    • Write a single video track and/or multiple audio tracks
    • Write tracks encoded with AV1, VP9, H265, H264, Opus, MPEG-4 audio (AAC)
    • Save generated segments on disk
  • General

    • Parse and produce M3U8 playlists
    • Examples

Table of contents

Examples

API Documentation

Click to open the API Documentation

Specifications

name area
RFC2616, HTTP 1.1 protocol
RFC8216, HLS protocol
HLS v2 protocol
HTTP Live Streaming by Apple protocol
Codec specifications codecs
Golang project layout project layout

Documentation

Overview

Package gohlslib is a HLS client and muxer library for the Go programming language.

Examples are available at https://github.com/bluenviron/gohlslib/tree/main/examples

Index

Constants

This section is empty.

Variables

View Source
var ErrClientEOS = errors.New("end of stream")

ErrClientEOS is returned by Wait() when the stream has ended.

Functions

This section is empty.

Types

type Client

type Client struct {
	//
	// parameters (all optional except URI)
	//
	// URI of the playlist.
	URI string
	// HTTP client.
	// It defaults to http.DefaultClient.
	HTTPClient *http.Client

	//
	// callbacks (all optional)
	//
	// called when sending a request to the server.
	OnRequest ClientOnRequestFunc
	// called when tracks are available.
	OnTracks ClientOnTracksFunc
	// called before downloading a primary playlist.
	OnDownloadPrimaryPlaylist ClientOnDownloadPrimaryPlaylistFunc
	// called before downloading a stream playlist.
	OnDownloadStreamPlaylist ClientOnDownloadStreamPlaylistFunc
	// called before downloading a segment.
	OnDownloadSegment ClientOnDownloadSegmentFunc
	// called before downloading a part.
	OnDownloadPart ClientOnDownloadPartFunc
	// called when a non-fatal decode error occurs.
	OnDecodeError ClientOnDecodeErrorFunc
	// contains filtered or unexported fields
}

Client is a HLS client.

func (*Client) AbsoluteTime

func (c *Client) AbsoluteTime(track *Track) (time.Time, bool)

AbsoluteTime returns the absolute timestamp of the last sample.

func (*Client) Close

func (c *Client) Close()

Close closes all the Client resources.

func (*Client) OnDataAV1

func (c *Client) OnDataAV1(track *Track, cb ClientOnDataAV1Func)

OnDataAV1 sets a callback that is called when data from an AV1 track is received.

func (*Client) OnDataH26x

func (c *Client) OnDataH26x(track *Track, cb ClientOnDataH26xFunc)

OnDataH26x sets a callback that is called when data from an H26x track is received.

func (*Client) OnDataMPEG4Audio

func (c *Client) OnDataMPEG4Audio(track *Track, cb ClientOnDataMPEG4AudioFunc)

OnDataMPEG4Audio sets a callback that is called when data from a MPEG-4 Audio track is received.

func (*Client) OnDataOpus

func (c *Client) OnDataOpus(track *Track, cb ClientOnDataOpusFunc)

OnDataOpus sets a callback that is called when data from an Opus track is received.

func (*Client) OnDataVP9

func (c *Client) OnDataVP9(track *Track, cb ClientOnDataVP9Func)

OnDataVP9 sets a callback that is called when data from a VP9 track is received.

func (*Client) Start

func (c *Client) Start() error

Start starts the client.

func (*Client) Wait

func (c *Client) Wait() chan error

Wait waits for any error of the Client.

type ClientOnDataAV1Func

type ClientOnDataAV1Func func(pts int64, tu [][]byte)

ClientOnDataAV1Func is the prototype of the function passed to OnDataAV1().

type ClientOnDataH26xFunc

type ClientOnDataH26xFunc func(pts int64, dts int64, au [][]byte)

ClientOnDataH26xFunc is the prototype of the function passed to OnDataH26x().

type ClientOnDataMPEG4AudioFunc

type ClientOnDataMPEG4AudioFunc func(pts int64, aus [][]byte)

ClientOnDataMPEG4AudioFunc is the prototype of the function passed to OnDataMPEG4Audio().

type ClientOnDataOpusFunc

type ClientOnDataOpusFunc func(pts int64, packets [][]byte)

ClientOnDataOpusFunc is the prototype of the function passed to OnDataOpus().

type ClientOnDataVP9Func

type ClientOnDataVP9Func func(pts int64, frame []byte)

ClientOnDataVP9Func is the prototype of the function passed to OnDataVP9().

type ClientOnDecodeErrorFunc

type ClientOnDecodeErrorFunc func(err error)

ClientOnDecodeErrorFunc is the prototype of Client.OnDecodeError.

type ClientOnDownloadPartFunc

type ClientOnDownloadPartFunc func(url string)

ClientOnDownloadPartFunc is the prototype of Client.OnDownloadPart.

type ClientOnDownloadPrimaryPlaylistFunc

type ClientOnDownloadPrimaryPlaylistFunc func(url string)

ClientOnDownloadPrimaryPlaylistFunc is the prototype of Client.OnDownloadPrimaryPlaylist.

type ClientOnDownloadSegmentFunc

type ClientOnDownloadSegmentFunc func(url string)

ClientOnDownloadSegmentFunc is the prototype of Client.OnDownloadSegment.

type ClientOnDownloadStreamPlaylistFunc

type ClientOnDownloadStreamPlaylistFunc func(url string)

ClientOnDownloadStreamPlaylistFunc is the prototype of Client.OnDownloadStreamPlaylist.

type ClientOnRequestFunc

type ClientOnRequestFunc func(*http.Request)

ClientOnRequestFunc is the prototype of the function passed to OnRequest().

type ClientOnTracksFunc

type ClientOnTracksFunc func([]*Track) error

ClientOnTracksFunc is the prototype of the function passed to OnTracks().

type Muxer

type Muxer struct {
	//
	// parameters (all optional except Tracks).
	//
	// tracks.
	Tracks []*Track
	// Variant to use.
	// It defaults to MuxerVariantLowLatency
	Variant MuxerVariant
	// Number of HLS segments to keep on the server.
	// Segments allow to seek through the stream.
	// Their number doesn't influence latency.
	// It defaults to 7.
	SegmentCount int
	// Minimum duration of each segment.
	// This is adjusted in order to include at least one IDR frame in each segment.
	// A player usually puts 3 segments in a buffer before reproducing the stream.
	// It defaults to 1sec.
	SegmentMinDuration time.Duration
	// Minimum duration of each part.
	// Parts are used in Low-Latency HLS in place of segments.
	// This is adjusted in order to produce segments with a similar duration.
	// A player usually puts 3 parts in a buffer before reproducing the stream.
	// It defaults to 200ms.
	PartMinDuration time.Duration
	// Maximum size of each segment.
	// This prevents RAM exhaustion.
	// It defaults to 50MB.
	SegmentMaxSize uint64
	// Directory in which to save segments.
	// This decreases performance, since saving segments on disk is less performant
	// than saving them on RAM, but allows to preserve RAM.
	Directory string

	//
	// callbacks (all optional)
	//
	// called when a non-fatal encode error occurs.
	OnEncodeError MuxerOnEncodeErrorFunc
	// contains filtered or unexported fields
}

Muxer is a HLS muxer.

func (*Muxer) Close

func (m *Muxer) Close()

Close closes a Muxer.

func (*Muxer) Handle

func (m *Muxer) Handle(w http.ResponseWriter, r *http.Request)

Handle handles a HTTP request.

func (*Muxer) Start

func (m *Muxer) Start() error

Start initializes the muxer.

func (*Muxer) WriteAV1

func (m *Muxer) WriteAV1(
	track *Track,
	ntp time.Time,
	pts int64,
	tu [][]byte,
) error

WriteAV1 writes an AV1 temporal unit.

func (*Muxer) WriteH264

func (m *Muxer) WriteH264(
	track *Track,
	ntp time.Time,
	pts int64,
	au [][]byte,
) error

WriteH264 writes an H264 access unit.

func (*Muxer) WriteH265

func (m *Muxer) WriteH265(
	track *Track,
	ntp time.Time,
	pts int64,
	au [][]byte,
) error

WriteH265 writes an H265 access unit.

func (*Muxer) WriteMPEG4Audio

func (m *Muxer) WriteMPEG4Audio(
	track *Track,
	ntp time.Time,
	pts int64,
	aus [][]byte,
) error

WriteMPEG4Audio writes MPEG-4 Audio access units.

func (*Muxer) WriteOpus

func (m *Muxer) WriteOpus(
	track *Track,
	ntp time.Time,
	pts int64,
	packets [][]byte,
) error

WriteOpus writes Opus packets.

func (*Muxer) WriteVP9

func (m *Muxer) WriteVP9(
	track *Track,
	ntp time.Time,
	pts int64,
	frame []byte,
) error

WriteVP9 writes a VP9 frame.

type MuxerOnEncodeErrorFunc

type MuxerOnEncodeErrorFunc func(err error)

MuxerOnEncodeErrorFunc is the prototype of Muxer.OnEncodeError.

type MuxerVariant

type MuxerVariant int

MuxerVariant is a muxer variant.

const (
	MuxerVariantMPEGTS MuxerVariant = iota + 1
	MuxerVariantFMP4
	MuxerVariantLowLatency
)

supported variants.

type Track

type Track struct {
	// Codec
	Codec codecs.Codec

	// Clock rate
	ClockRate int

	// Name
	// For audio renditions only.
	Name string

	// Language
	// For audio renditions only.
	Language string

	// whether this is the default track.
	// For audio renditions only.
	IsDefault bool
}

Track is a HLS track.

Directories

Path Synopsis
examples
pkg
codecparams
Package codecparams contains utilities to deal with codec parameters.
Package codecparams contains utilities to deal with codec parameters.
codecs
Package codecs contains codec definitions.
Package codecs contains codec definitions.
playlist
Package playlist contains a M3U8 playlist decoder and encoder.
Package playlist contains a M3U8 playlist decoder and encoder.
playlist/primitives
Package primitives contains playlist primitives.
Package primitives contains playlist primitives.
storage
Package storage contains the storage mechanism of segments and parts.
Package storage contains the storage mechanism of segments and parts.

Jump to

Keyboard shortcuts

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