Documentation
¶
Index ¶
- Constants
- Variables
- func Decode(data bytes.Buffer, strict bool) (Playlist, ListType, error)
- func DecodeFrom(reader io.Reader, strict bool) (Playlist, ListType, error)
- func DecodeWith(input interface{}, strict bool, customDecoders []CustomDecoder) (Playlist, ListType, error)
- func FullTimeParse(value string) (time.Time, error)
- func StrictTimeParse(value string) (time.Time, error)
- type Alternative
- type Attribute
- type Channels
- type ContentSteering
- type CustomDecoder
- type CustomMap
- type CustomTag
- type DateRange
- type Define
- type DefineType
- type Key
- type ListType
- type Map
- type MasterPlaylist
- func (p *MasterPlaylist) Append(uri string, chunklist *MediaPlaylist, params VariantParams)
- func (p *MasterPlaylist) AppendDefine(d Define) error
- func (p *MasterPlaylist) CalcMinVersion() (ver uint8, reason string)
- func (p *MasterPlaylist) Decode(data bytes.Buffer, strict bool) error
- func (p *MasterPlaylist) DecodeFrom(reader io.Reader, strict bool) error
- func (p *MasterPlaylist) Encode() *bytes.Buffer
- func (p *MasterPlaylist) GetAllAlternatives() []*Alternative
- func (p *MasterPlaylist) IndependentSegments() bool
- func (p *MasterPlaylist) ReleasePlaylist()
- func (p *MasterPlaylist) ResetCache()
- func (p *MasterPlaylist) SetCustomTag(tag CustomTag)
- func (p *MasterPlaylist) SetIndependentSegments(b bool)
- func (p *MasterPlaylist) SetVersion(ver uint8)
- func (p *MasterPlaylist) SetWritePrecision(nrDecimals int)
- func (p *MasterPlaylist) String() string
- func (p *MasterPlaylist) Version() uint8
- func (p *MasterPlaylist) WithCustomDecoders(customDecoders []CustomDecoder) Playlist
- func (p *MasterPlaylist) WritePrecision() int
- type MediaPlaylist
- func (p *MediaPlaylist) Append(uri string, duration float64, title string) error
- func (p *MediaPlaylist) AppendDefine(d Define)
- func (p *MediaPlaylist) AppendPartial(uri string, duration float64, independent bool) error
- func (p *MediaPlaylist) AppendPartialSegment(ps *PartialSegment) error
- func (p *MediaPlaylist) AppendSegment(seg *MediaSegment) error
- func (p *MediaPlaylist) CalcMinVersion() (ver uint8, reason string)
- func (p *MediaPlaylist) CalculateTargetDuration(hlsVer uint8) uint
- func (p *MediaPlaylist) Close()
- func (p *MediaPlaylist) Count() uint
- func (p *MediaPlaylist) Decode(data bytes.Buffer, strict bool) error
- func (p *MediaPlaylist) DecodeFrom(reader io.Reader, strict bool) error
- func (p *MediaPlaylist) Encode() *bytes.Buffer
- func (p *MediaPlaylist) EncodeWithSkip(skipped uint64) (*bytes.Buffer, error)
- func (p *MediaPlaylist) GetAllSegments() []*MediaSegment
- func (p *MediaPlaylist) GetNextSequenceAndPart() (seq uint64, part uint64)
- func (p *MediaPlaylist) HasPartialSegments() bool
- func (p *MediaPlaylist) IndependentSegments() bool
- func (p *MediaPlaylist) IsSegmentReady(uri string) bool
- func (p *MediaPlaylist) LastPartSegIndex() uint64
- func (p *MediaPlaylist) LastSegIndex() uint64
- func (p *MediaPlaylist) ReleasePlaylist()
- func (p *MediaPlaylist) Remove() (err error)
- func (p *MediaPlaylist) ResetCache()
- func (p *MediaPlaylist) SCTE35Syntax() SCTE35Syntax
- func (p *MediaPlaylist) SetCustomSegmentTag(tag CustomTag) error
- func (p *MediaPlaylist) SetCustomTag(tag CustomTag)
- func (p *MediaPlaylist) SetDefaultKey(method, uri, iv, keyformat, keyformatversions string) error
- func (p *MediaPlaylist) SetDefaultMap(uri string, limit, offset int64)
- func (p *MediaPlaylist) SetDiscontinuity() error
- func (p *MediaPlaylist) SetGap() error
- func (p *MediaPlaylist) SetIframeOnly()
- func (p *MediaPlaylist) SetIndependentSegments(b bool)
- func (p *MediaPlaylist) SetKey(method, uri, iv, keyformat, keyformatversions string) error
- func (p *MediaPlaylist) SetMap(uri string, limit, offset int64) error
- func (p *MediaPlaylist) SetPreloadHint(hintType, uri string)
- func (p *MediaPlaylist) SetProgramDateTime(value time.Time) error
- func (p *MediaPlaylist) SetRange(limit, offset int64) error
- func (p *MediaPlaylist) SetSCTE(cue string, id string, time float64) errordeprecated
- func (p *MediaPlaylist) SetSCTE35(scte35 *SCTE) error
- func (p *MediaPlaylist) SetServerControl(control *ServerControl) error
- func (p *MediaPlaylist) SetSkipped(skipped uint64)
- func (p *MediaPlaylist) SetTargetDuration(duration uint)
- func (p *MediaPlaylist) SetVersion(ver uint8)
- func (p *MediaPlaylist) SetWinSize(winsize uint) error
- func (p *MediaPlaylist) SetWritePrecision(nrDecimals int)
- func (p *MediaPlaylist) SkippedSegments() uint64
- func (p *MediaPlaylist) Slide(uri string, duration float64, title string)
- func (p *MediaPlaylist) String() string
- func (p *MediaPlaylist) TotalDuration() float64
- func (p *MediaPlaylist) Version() uint8
- func (p *MediaPlaylist) WinSize() uint
- func (p *MediaPlaylist) WithCustomDecoders(customDecoders []CustomDecoder) Playlist
- func (p *MediaPlaylist) WritePrecision() int
- type MediaSegment
- type MediaType
- type PartialSegment
- type Playlist
- type PreloadHint
- type SCTE
- type SCTE35CueType
- type SCTE35Syntax
- type SegmentIndexing
- type ServerControl
- type SessionData
- type Variant
- type VariantParams
Examples ¶
Constants ¶
const ( // DefaultFloatPrecision is the default number of decimal places for float values (milliseconds) DefaultFloatPrecision int = 3 // DATETIME represents format for EXT-X-PROGRAM-DATE-TIME timestamps. // Format is [ISO/IEC 8601:2004] according to the [HLS spec]. DATETIME = time.RFC3339Nano )
Variables ¶
var ErrAlreadySkipped = errors.New("can not change the existing skip tag in a playlist")
var ErrCannotDetectPlaylistType = errors.New("cannot detect playlist type")
var ErrDanglingSCTE35DateRange = errors.New("dangling SCTE-35 DateRange tag after last segment not supported")
var ErrExtM3UAbsent = errors.New("#EXTM3U absent")
var ErrNotYesOrNo = errors.New("value must be YES or NO")
var ErrPlaylistEmpty = errors.New("playlist is empty")
var ErrPlaylistFull = errors.New("playlist is full")
var ErrWinSizeTooSmall = errors.New("window size must be >= capacity")
var TimeParse func(value string) (time.Time, error) = FullTimeParse
TimeParse allows globally apply and/or override Time Parser function. Available variants:
- FullTimeParse - implements full featured ISO/IEC 8601:2004
- StrictTimeParse - implements only RFC3339 Nanoseconds format
Functions ¶
func Decode ¶
Decode detects type of playlist and decodes it.
Example (MediaPlaylistSegmentsSCTE35OATCLS) ¶
f, _ := os.Open("sample-playlists/media-playlist-with-oatcls-scte35.m3u8")
p, _, _ := DecodeFrom(bufio.NewReader(f), true)
pp := p.(*MediaPlaylist)
defer pp.ReleasePlaylist()
fmt.Print(pp)
Output: #EXTM3U #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-TARGETDURATION:10 #EXT-OATCLS-SCTE35:/DAlAAAAAAAAAP/wFAUAAAABf+/+ANgNkv4AFJlwAAEBAQAA5xULLA== #EXT-X-CUE-OUT:15.000 #EXTINF:8.844, media0.ts #EXT-X-CUE-OUT-CONT:ElapsedTime=8.844,Duration=15.000,SCTE35=/DAlAAAAAAAAAP/wFAUAAAABf+/+ANgNkv4AFJlwAAEBAQAA5xULLA== #EXTINF:6.156, media1.ts #EXT-X-CUE-IN #EXTINF:3.844, media2.ts
func DecodeFrom ¶
DecodeFrom detects type of playlist and decodes it.
Example (WithDiscontinuityAndOutput) ¶
f, _ := os.Open("sample-playlists/media-playlist-with-discontinuity.m3u8")
p, _, _ := DecodeFrom(bufio.NewReader(f), true)
pp := p.(*MediaPlaylist)
fmt.Printf("%s", pp)
Output: #EXTM3U #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-TARGETDURATION:10 #EXTINF:10.000, ad0.ts #EXTINF:8.000, ad1.ts #EXT-X-DISCONTINUITY #EXTINF:10.000, movieA.ts #EXTINF:10.000, movieB.ts
func DecodeWith ¶
func DecodeWith(input interface{}, strict bool, customDecoders []CustomDecoder) (Playlist, ListType, error)
DecodeWith detects the type of playlist and decodes it. It accepts either bytes.Buffer or io.Reader as input. Any custom decoders provided will be used during decoding.
func FullTimeParse ¶
FullTimeParse implements ISO/IEC 8601:2004.
Types ¶
type Alternative ¶
type Alternative struct {
Type string // TYPE parameter
URI string // URI parameter
GroupId string // GROUP-ID parameter
Language string // LANGUAGE parameter
AssocLanguage string // ASSOC-LANGUAGE parameter
Name string // NAME parameter
StableRenditionId string // STABLE-RENDITION-ID parameter
Default bool // DEFAULT parameter
Autoselect bool // AUTOSELECT parameter
Forced bool // FORCED parameter
InstreamId string // INSTREAM-ID parameter
BitDepth byte // BIT-DEPTH parameter
SampleRate uint32 // SAMPLE-RATE parameter
Characteristics string // CHARACTERISTICS parameter
Channels *Channels // CHANNELS parameter
}
Alternative represents an EXT-X-MEDIA tag. Attributes are listed in same order as in specification for easy comparison.
type Attribute ¶
type Attribute struct {
Key string // Name of the attribute
Val string // Value including quotes if a quoted string, and 0x if hexadecimal value
}
Attribute provides a raw key-value pair for an attribute. Quotes and 0x are included
type ContentSteering ¶
type ContentSteering struct {
ServerURI string // SERVER-URI is a quoted-string containing a URI to a Steering Manifest
PathwayId string // PATHWAY-ID is a quoted-string containing a unique identifier for the pathway
}
ContentSteering represents an EXT-X-CONTENT-STEERING tag.
type CustomDecoder ¶
type CustomDecoder interface {
// TagName should return the full identifier including the leading '#' as well as the
// trailing ':' if the tag also contains a value or attribute list
TagName() string
// Decode parses a line from the playlist and returns the CustomTag representation
Decode(line string) (CustomTag, error)
// SegmentTag should return true if this CustomDecoder should apply per segment.
// Should returns false if it a MediaPlaylist header tag.
// This value is ignored for MasterPlaylists.
SegmentTag() bool
}
CustomDecoder interface for decoding custom and unsupported tags
type CustomTag ¶
type CustomTag interface {
// TagName should return the full identifier including the leading '#' as well as the
// trailing ':' if the tag also contains a value or attribute list
TagName() string
// Encode should return the complete tag string as a *bytes.Buffer. This will
// be used by Playlist.Decode to write the tag to the m3u8.
// Return nil to not write anything to the m3u8.
Encode() *bytes.Buffer
// String should return the encoded tag as a string.
String() string
}
CustomTag interface for encoding custom and unsupported tags
type DateRange ¶
type DateRange struct {
ID string // ID is mandatory quoted string ID
Class string // CLASS is a client-defined quoted string
StartDate time.Time // START-DATE is mandatory start time
EndDate *time.Time // END-DATE is optional end time
Cue string // CUE is an enumerated-string-list of Trigger Identifiers, PRE, POST, or ONCE.
Duration *float64 // DURATION is optional duration in seconds
PlannedDuration *float64 // PLANNED-DURATION is optional planned duration in seconds
XAttrs []Attribute // XAttrs is a list of X-<client-attribute>
SCTE35Cmd string // SCTE35-CMD is a optional hex value for SCTE35 command
SCTE35Out string // SCTE35-OUT is a optional hex value for SCTE35 CUE-OUT command
SCTE35In string // SCTE35-IN is a optional hex value for SCTE35 CUE-IN command
EndOnNext bool // END-ON-NEXT is enumerated YES/NO
}
DateRange corresponds to EXT-X-DATERANGE tag. It is used for signaling SCTE-35 messages,interstitials, and other metadata events.
type Define ¶
type Define struct {
Name string // Specifies the Variable Name.
Type DefineType // name-VALUE pair, QUERYPARAM or IMPORT.
Value string // Only used if type is VALUE.
}
Define represents an EXT-X-DEFINE tag and provides a Playlist variable definition or declaration.
type Key ¶
type Key struct {
Method string // METHOD parameter
URI string // URI parameter
IV string // IV parameter
Keyformat string // KEYFORMAT parameter
Keyformatversions string // KEYFORMATVERSIONS parameter
}
Key structure represents information about stream encryption (EXT-X-KEY tag)
type Map ¶
type Map struct {
URI string // URI is the path to the Media Initialization Section.
Limit int64 // <n> is length in bytes for the file under URI
Offset int64 // [@o] is offset from the start of the file under URI
}
Map (EXT-X-MAP tag) specifies how obtain the Media Initialization Section required to parse the applicable Media Segments.
It applies to every Media Segment that appears after it in the Playlist until the next EXT-X-MAP tag or until the end of the playlist.
type MasterPlaylist ¶
type MasterPlaylist struct {
Variants []*Variant // Variants is a list of media playlists
Args string // optional query placed after URI (URI?Args)
StartTime float64 // EXT-X-START:TIME-OFFSET=<n> (positive or negative)
StartTimePrecise bool // EXT-X-START:PRECISE=YES
Defines []Define // EXT-X-DEFINE tags
SessionDatas []*SessionData // EXT-X-SESSION-DATA tags
SessionKeys []*Key // EXT-X-SESSION-KEY tags
ContentSteering *ContentSteering // EXT-X-CONTENT-STEERING tag
Alternatives []*Alternative // EXT-X-MEDIA tags for alternative renditions (audio, video, subtitles)
Custom CustomMap // Custom-provided tags for encoding
// contains filtered or unexported fields
}
MasterPlaylist represents a master (multivariant) playlist which provides parameters and lists one or more media playlists. URI lines in the playlist identify media playlists.
func NewMasterPlaylist ¶
func NewMasterPlaylist() *MasterPlaylist
NewMasterPlaylist creates a new empty master playlist.
Example (String) ¶
Create new master playlist Add media playlist Encode structures to HLS
m := NewMasterPlaylist()
p, _ := NewMediaPlaylist(3, 5)
defer p.ReleasePlaylist()
for i := 0; i < 5; i++ {
_ = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "")
}
m.Append("chunklist1.m3u8", p, VariantParams{Bandwidth: 1500000, AverageBandwidth: 1500000,
Resolution: "576x480", FrameRate: 25.000})
m.Append("chunklist2.m3u8", p, VariantParams{Bandwidth: 1500000, AverageBandwidth: 1500000,
Resolution: "576x480", FrameRate: 25.000})
fmt.Printf("%s", m)
Output: #EXTM3U #EXT-X-VERSION:3 #EXT-X-STREAM-INF:BANDWIDTH=1500000,AVERAGE-BANDWIDTH=1500000,RESOLUTION=576x480,FRAME-RATE=25.000 chunklist1.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1500000,AVERAGE-BANDWIDTH=1500000,RESOLUTION=576x480,FRAME-RATE=25.000 chunklist2.m3u8
Example (StringWithHLSv7) ¶
m := NewMasterPlaylist()
defer m.ReleasePlaylist()
m.SetVersion(7)
m.SetIndependentSegments(true)
p, _ := NewMediaPlaylist(3, 5)
m.Append("hdr10_1080/prog_index.m3u8", p, VariantParams{AverageBandwidth: 7964551, Bandwidth: 12886714, VideoRange: "PQ", Codecs: "hvc1.2.4.L123.B0", Resolution: "1920x1080", FrameRate: 23.976, Captions: "NONE", HDCPLevel: "TYPE-0"})
m.Append("hdr10_1080/iframe_index.m3u8", p, VariantParams{Iframe: true, AverageBandwidth: 364552, Bandwidth: 905053, VideoRange: "PQ", Codecs: "hvc1.2.4.L123.B0", Resolution: "1920x1080", HDCPLevel: "TYPE-0"})
fmt.Printf("%s", m)
Output: #EXTM3U #EXT-X-VERSION:7 #EXT-X-INDEPENDENT-SEGMENTS #EXT-X-STREAM-INF:BANDWIDTH=12886714,AVERAGE-BANDWIDTH=7964551,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1920x1080,FRAME-RATE=23.976,HDCP-LEVEL=TYPE-0,VIDEO-RANGE=PQ,CLOSED-CAPTIONS=NONE hdr10_1080/prog_index.m3u8 #EXT-X-I-FRAME-STREAM-INF:BANDWIDTH=905053,AVERAGE-BANDWIDTH=364552,CODECS="hvc1.2.4.L123.B0",RESOLUTION=1920x1080,HDCP-LEVEL=TYPE-0,VIDEO-RANGE=PQ,URI="hdr10_1080/iframe_index.m3u8"
func (*MasterPlaylist) Append ¶
func (p *MasterPlaylist) Append(uri string, chunklist *MediaPlaylist, params VariantParams)
Append appends a variant to master playlist. This operation resets the cache.
func (*MasterPlaylist) AppendDefine ¶
func (p *MasterPlaylist) AppendDefine(d Define) error
func (*MasterPlaylist) CalcMinVersion ¶
func (p *MasterPlaylist) CalcMinVersion() (ver uint8, reason string)
CalcMinVersion returns the minimal version of the HLS protocol that is required to support the playlist according to the [HLS Prococcol Version Compatibility]. The reason is a human-readable string explaining why the version is required.
func (*MasterPlaylist) Decode ¶
func (p *MasterPlaylist) Decode(data bytes.Buffer, strict bool) error
Decode parses a master playlist passed from the buffer. If `strict` parameter is true then it returns first syntax error.
func (*MasterPlaylist) DecodeFrom ¶
func (p *MasterPlaylist) DecodeFrom(reader io.Reader, strict bool) error
DecodeFrom parses a master playlist passed from an io.Reader. If strict parameter is true then it returns first syntax error.
func (*MasterPlaylist) Encode ¶
func (p *MasterPlaylist) Encode() *bytes.Buffer
Encode generates the output in M3U8 format and provides a pointer to its buffer.
func (*MasterPlaylist) GetAllAlternatives ¶
func (p *MasterPlaylist) GetAllAlternatives() []*Alternative
GetAllAlternatives returns all alternative renditions sorted by groupID, type, name, and language.
func (*MasterPlaylist) IndependentSegments ¶
func (p *MasterPlaylist) IndependentSegments() bool
IndependentSegments returns true if all media samples in a segment can be decoded without information from other buf.
func (*MasterPlaylist) ReleasePlaylist ¶
func (p *MasterPlaylist) ReleasePlaylist()
ReleasePlaylist returns buffer to pool for reuse Do not use the playlist after this
func (*MasterPlaylist) ResetCache ¶
func (p *MasterPlaylist) ResetCache()
ResetCache resets the playlist's cache (its buffer).
func (*MasterPlaylist) SetCustomTag ¶
func (p *MasterPlaylist) SetCustomTag(tag CustomTag)
SetCustomTag sets the provided tag on the master playlist for its TagName
func (*MasterPlaylist) SetIndependentSegments ¶
func (p *MasterPlaylist) SetIndependentSegments(b bool)
SetIndependentSegments sets the master playlist #EXT-X--INDEPENDENT-SEGMENTS tag.
func (*MasterPlaylist) SetVersion ¶
func (p *MasterPlaylist) SetVersion(ver uint8)
SetVersion sets the HLS protocol version as signaled by EXT-X-VERSION
func (*MasterPlaylist) SetWritePrecision ¶
func (p *MasterPlaylist) SetWritePrecision(nrDecimals int)
SetWritePrecision sets the number of decimal places used when writing float values. Default is 3 (milliseconds). Set to -1 to use the necessary number of decimal places.
func (*MasterPlaylist) String ¶
func (p *MasterPlaylist) String() string
String provides the playlist fulfilling the Stringer interface.
func (*MasterPlaylist) Version ¶
func (p *MasterPlaylist) Version() uint8
Version returns the HLS protocol version as signaled by EXT-X-VERSION
func (*MasterPlaylist) WithCustomDecoders ¶
func (p *MasterPlaylist) WithCustomDecoders(customDecoders []CustomDecoder) Playlist
WithCustomDecoders adds custom tag decoders to the master playlist for decoding
func (*MasterPlaylist) WritePrecision ¶
func (p *MasterPlaylist) WritePrecision() int
WritePrecision returns the current write precision for float values.
type MediaPlaylist ¶
type MediaPlaylist struct {
TargetDuration uint // TargetDuration is max media segment duration. Rounding depends on version.
SeqNo uint64 // EXT-X-MEDIA-SEQUENCE
Segments []*MediaSegment // List of segments in the playlist. Output may be limited by winsize.
Args string // optional query placed after URIs (URI?Args)
Defines []Define // EXT-X-DEFINE tags
Iframe bool // EXT-X-I-FRAMES-ONLY
Closed bool // is this VOD/EVENT (closed) or Live (sliding) playlist?
MediaType MediaType // EXT-X-PLAYLIST-TYPE (EVENT, VOD or empty)
DiscontinuitySeq uint64 // EXT-X-DISCONTINUITY-SEQUENCE
StartTime float64 // EXT-X-START:TIME-OFFSET=<n> (positive or negative)
StartTimePrecise bool // EXT-X-START:PRECISE=YES
Keys []Key // EXT-X-KEY is initial key tag for encrypted segments
Map *Map // EXT-X-MAP provides a Media Initialization Section. Segments can redefine.
DateRanges []*DateRange // EXT-X-DATERANGE tags not associated with SCTE-35
AllowCache *bool // EXT-X-ALLOW-CACHE tag YES/NO, removed in version 7
Custom CustomMap // Custom-provided tags for encoding
PartTargetDuration float64 // EXT-X-PART-INF:PART-TARGET
PartialSegments []*PartialSegment // List of partial segments in the playlist.
SegmentIndexing SegmentIndexing // The indexing parameters for media and partial segments.
PreloadHints *PreloadHint // EXT-X-PRELOAD-HINT tags
ServerControl *ServerControl // EXT-X-SERVER-CONTROL tags, MAY appear in any Media Playlist
// contains filtered or unexported fields
}
MediaPlaylist represents a single bitrate playlist aka media playlist. It is used for both VOD, EVENT and sliding window live media playlists with window size. URI lines in the Playlist point to media segments.
func NewMediaPlaylist ¶
func NewMediaPlaylist(winsize uint, capacity uint) (*MediaPlaylist, error)
NewMediaPlaylist creates a new media playlist structure. Winsize defines live window for playlist generation. Set to zero for VOD or EVENT playlists. Capacity is the total size of the backing segment list.. For VOD playlists, call Close() after the last segment is added.
Example (GetAllSegments) ¶
Range over segments of media playlist. Check for ring buffer corner cases.
m, _ := NewMediaPlaylist(3, 3)
defer m.ReleasePlaylist()
_ = m.Append("t00.ts", 10, "")
_ = m.Append("t01.ts", 10, "")
_ = m.Append("t02.ts", 10, "")
for _, v := range m.GetAllSegments() {
fmt.Printf("%s\n", v.URI)
}
_ = m.Remove()
_ = m.Remove()
_ = m.Remove()
_ = m.Append("t03.ts", 10, "")
_ = m.Append("t04.ts", 10, "")
for _, v := range m.GetAllSegments() {
fmt.Printf("%s\n", v.URI)
}
_ = m.Remove()
_ = m.Remove()
_ = m.Append("t05.ts", 10, "")
_ = m.Append("t06.ts", 10, "")
_ = m.Remove()
_ = m.Remove()
// empty because removed two elements
for _, v := range m.GetAllSegments() {
fmt.Printf("%s\n", v.URI)
}
Output: t00.ts t01.ts t02.ts t03.ts t04.ts
Example (String) ¶
Create new media playlist Add two segments, but to a window-limited list, so only the last is kept Print it
// Set winsize to 1 so there will be only one segment in the playlist
p, _ := NewMediaPlaylist(1, 2)
defer p.ReleasePlaylist()
_ = p.Append("test01.ts", 5.0, "")
_ = p.Append("test02.ts", 6.0, "")
fmt.Printf("%s\n", p)
Output: #EXTM3U #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-TARGETDURATION:6 #EXTINF:6.000, test02.ts
Example (StringWinsize0) ¶
Create new media playlist Add two segments to media playlist Print it
p, _ := NewMediaPlaylist(0, 2)
defer p.ReleasePlaylist()
_ = p.Append("test01.ts", 5.0, "")
_ = p.Append("test02.ts", 6.0, "")
fmt.Printf("%s\n", p)
Output: #EXTM3U #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-TARGETDURATION:6 #EXTINF:5.000, test01.ts #EXTINF:6.000, test02.ts
Example (StringWinsize0VOD) ¶
Create new media playlist Add two segments to media playlist Print it
p, _ := NewMediaPlaylist(0, 2)
defer p.ReleasePlaylist()
_ = p.Append("test01.ts", 5.0, "")
_ = p.Append("test02.ts", 6.0, "")
p.Close()
fmt.Printf("%s\n", p)
Output: #EXTM3U #EXT-X-VERSION:3 #EXT-X-MEDIA-SEQUENCE:0 #EXT-X-TARGETDURATION:6 #EXTINF:5.000, test01.ts #EXTINF:6.000, test02.ts #EXT-X-ENDLIST
func (*MediaPlaylist) Append ¶
func (p *MediaPlaylist) Append(uri string, duration float64, title string) error
Append general chunk to the tail of chunk slice for a media playlist. This operation resets playlist cache.
func (*MediaPlaylist) AppendDefine ¶
func (p *MediaPlaylist) AppendDefine(d Define)
func (*MediaPlaylist) AppendPartial ¶
func (p *MediaPlaylist) AppendPartial(uri string, duration float64, independent bool) error
AppendPartial creates and and appends a partial segment to the media playlist.
func (*MediaPlaylist) AppendPartialSegment ¶
func (p *MediaPlaylist) AppendPartialSegment(ps *PartialSegment) error
AppendPartialSegment appends a PartialSegment to the MediaPlaylist. If the partial segment belongs to the last full segment, it assigns the same SeqID. Otherwise, it assigns the SeqID of the next segment. The MaxPartIndex is updated if necessary, and the NextPartIndex is incremented. Finally, it removes any expired partial segments.
func (*MediaPlaylist) AppendSegment ¶
func (p *MediaPlaylist) AppendSegment(seg *MediaSegment) error
AppendSegment appends a MediaSegment to the tail of chunk slice for a media playlist. This operation resets playlist cache.
func (*MediaPlaylist) CalcMinVersion ¶
func (p *MediaPlaylist) CalcMinVersion() (ver uint8, reason string)
CalcMinVersion returns the minimal version of the HLS protocol that is required to support the playlist according to the [HLS Prococcol Version Compatibility]. The reason is a human-readable string explaining why the version is required.
func (*MediaPlaylist) CalculateTargetDuration ¶
func (p *MediaPlaylist) CalculateTargetDuration(hlsVer uint8) uint
CalculateTargetDuration calculates the target duration for the playlist. For HLS v5 and earlier, it is the maximum segment duration as rounded up. For HLS v6 and later, it is the maximum segment duration as rounded to the nearest integer. It is not allowed to change when the playlist is updated.
func (*MediaPlaylist) Close ¶
func (p *MediaPlaylist) Close()
Close sliding playlist and by setting the EXT-X-ENDLIST tag and setting the Closed flag.
func (*MediaPlaylist) Count ¶
func (p *MediaPlaylist) Count() uint
Count tells us the number of items that are currently in the media playlist.
func (*MediaPlaylist) Decode ¶
func (p *MediaPlaylist) Decode(data bytes.Buffer, strict bool) error
Decode parses a media playlist passed from the buffer. If strict parameter is true then return first syntax error.
func (*MediaPlaylist) DecodeFrom ¶
func (p *MediaPlaylist) DecodeFrom(reader io.Reader, strict bool) error
DecodeFrom parses a media playlist passed from the io.Reader stream. If strict parameter is true then it returns first syntax error.
func (*MediaPlaylist) Encode ¶
func (p *MediaPlaylist) Encode() *bytes.Buffer
func (*MediaPlaylist) EncodeWithSkip ¶
func (p *MediaPlaylist) EncodeWithSkip(skipped uint64) (*bytes.Buffer, error)
EncodeWithSkip sets the skip tag and encodes the playlist. If skipped > 0, the first `skipped` segments will be skipped. If playlist has a skip tag already, it will return an error.
func (*MediaPlaylist) GetAllSegments ¶
func (p *MediaPlaylist) GetAllSegments() []*MediaSegment
GetAllSegments could get all segments currently added to playlist. Winsize is ignored.
func (*MediaPlaylist) GetNextSequenceAndPart ¶
func (p *MediaPlaylist) GetNextSequenceAndPart() (seq uint64, part uint64)
func (*MediaPlaylist) HasPartialSegments ¶
func (p *MediaPlaylist) HasPartialSegments() bool
func (*MediaPlaylist) IndependentSegments ¶
func (p *MediaPlaylist) IndependentSegments() bool
func (*MediaPlaylist) IsSegmentReady ¶
func (p *MediaPlaylist) IsSegmentReady(uri string) bool
func (*MediaPlaylist) LastPartSegIndex ¶
func (p *MediaPlaylist) LastPartSegIndex() uint64
func (*MediaPlaylist) LastSegIndex ¶
func (p *MediaPlaylist) LastSegIndex() uint64
LastSegIndex returns the index of the last segment in the media playlist. It calculates the index based on the next media sequence number and the number of segments already skipped. If the NextPartIndex is 0, indicating that it has just rolled over to the next segment, it returns the previous sequence number. Otherwise, it returns the current sequence number.
func (*MediaPlaylist) ReleasePlaylist ¶
func (p *MediaPlaylist) ReleasePlaylist()
ReleasePlaylist returns buffer and segment slice to pool for reuse Do not use the playlist after this
func (*MediaPlaylist) Remove ¶
func (p *MediaPlaylist) Remove() (err error)
Remove current segment from the head of chunk slice form a media playlist. Useful for sliding playlists. This operation resets playlist cache.
func (*MediaPlaylist) ResetCache ¶
func (p *MediaPlaylist) ResetCache()
ResetCache resets playlist cache (internal buffer). Next call to Encode() fills buffer/cache again.
func (*MediaPlaylist) SCTE35Syntax ¶
func (p *MediaPlaylist) SCTE35Syntax() SCTE35Syntax
SCTE35Syntax returns the SCTE35 syntax version detected as used in the playlist.
func (*MediaPlaylist) SetCustomSegmentTag ¶
func (p *MediaPlaylist) SetCustomSegmentTag(tag CustomTag) error
SetCustomSegmentTag sets the provided tag on the current media segment for its TagName.
func (*MediaPlaylist) SetCustomTag ¶
func (p *MediaPlaylist) SetCustomTag(tag CustomTag)
SetCustomTag sets the provided tag on the media playlist for its TagName.
func (*MediaPlaylist) SetDefaultKey ¶
func (p *MediaPlaylist) SetDefaultKey(method, uri, iv, keyformat, keyformatversions string) error
SetDefaultKey sets encryption key to appear before segments in the media playlist.
func (*MediaPlaylist) SetDefaultMap ¶
func (p *MediaPlaylist) SetDefaultMap(uri string, limit, offset int64)
SetDefaultMap sets default Media Initialization Section (EXT-X-MAP) at start of playlist. May be overridden by individual segments.
func (*MediaPlaylist) SetDiscontinuity ¶
func (p *MediaPlaylist) SetDiscontinuity() error
SetDiscontinuity sets discontinuity flag for the currently last media segment. EXT-X-DISCONTINUITY indicates an encoding discontinuity between the media segment that follows it and the one that preceded it.
func (*MediaPlaylist) SetGap ¶
func (p *MediaPlaylist) SetGap() error
SetGap sets the gap flag for the currently last media segment. The EXT-X-GAP tag indicates that the segment URI to which it applies does not contain media data and SHOULD NOT be loaded by clients. It applies only to the latest Media Segment.
func (*MediaPlaylist) SetIframeOnly ¶
func (p *MediaPlaylist) SetIframeOnly()
SetIframeOnly marks medialist of only I-frames (Intra frames).
func (*MediaPlaylist) SetIndependentSegments ¶
func (p *MediaPlaylist) SetIndependentSegments(b bool)
func (*MediaPlaylist) SetKey ¶
func (p *MediaPlaylist) SetKey(method, uri, iv, keyformat, keyformatversions string) error
SetKey sets encryption key for the current (and following) segment of media playlist
func (*MediaPlaylist) SetMap ¶
func (p *MediaPlaylist) SetMap(uri string, limit, offset int64) error
SetMap sets map for the currently last segment of media playlist.
func (*MediaPlaylist) SetPreloadHint ¶
func (p *MediaPlaylist) SetPreloadHint(hintType, uri string)
func (*MediaPlaylist) SetProgramDateTime ¶
func (p *MediaPlaylist) SetProgramDateTime(value time.Time) error
SetProgramDateTime sets program date and time for the currently last media segment. EXT-X-PROGRAM-DATE-TIME tag associates the first sample of a media segment with an absolute date and/or time. It applies only to the current media segment. Date/time format is YYYY-MM-DDThh:mm:ssZ (ISO8601) and includes time zone.
func (*MediaPlaylist) SetRange ¶
func (p *MediaPlaylist) SetRange(limit, offset int64) error
SetRange sets byte range limit and offset for the currently last media segment.
func (*MediaPlaylist) SetSCTE35 ¶
func (p *MediaPlaylist) SetSCTE35(scte35 *SCTE) error
SetSCTE35 sets the SCTE cue format for the currently last media segment.
func (*MediaPlaylist) SetServerControl ¶
func (p *MediaPlaylist) SetServerControl(control *ServerControl) error
func (*MediaPlaylist) SetSkipped ¶
func (p *MediaPlaylist) SetSkipped(skipped uint64)
SetSkipped sets the number of segments that have been skipped in the playlist. This method should only be used when converting some custom representation to a MediaPlaylist, and the skipping of segments has already been handled.
func (*MediaPlaylist) SetTargetDuration ¶
func (p *MediaPlaylist) SetTargetDuration(duration uint)
SetTargetDuration sets the target duration for the playlist and stops automatic calculation. Since the target duration is not allowed to change, it is locked after the first call.
func (*MediaPlaylist) SetVersion ¶
func (p *MediaPlaylist) SetVersion(ver uint8)
SetVersion sets the playlist version number, note the version have increased automatically by other Set methods.
func (*MediaPlaylist) SetWinSize ¶
func (p *MediaPlaylist) SetWinSize(winsize uint) error
SetWinSize overwrites the playlist's window size.
func (*MediaPlaylist) SetWritePrecision ¶
func (p *MediaPlaylist) SetWritePrecision(nrDecimals int)
SetWritePrecision sets the number of decimal places used when writing float values. Default is 3 (milliseconds). Set to -1 to use the necessary number of decimal places.
func (*MediaPlaylist) SkippedSegments ¶
func (p *MediaPlaylist) SkippedSegments() uint64
SkippedSegments returns the value of SKIPPED-SEGMENTS tag in the media playlist.
func (*MediaPlaylist) Slide ¶
func (p *MediaPlaylist) Slide(uri string, duration float64, title string)
Slide combines two operations: first it removes one chunk from the head of chunk slice and move pointer to next chunk. Secondly it appends one chunk to the tail of chunk slice. Useful for sliding playlists. This operation resets the cache.
func (*MediaPlaylist) String ¶
func (p *MediaPlaylist) String() string
String provides the playlist fulfilling the Stringer interface.
func (*MediaPlaylist) TotalDuration ¶
func (p *MediaPlaylist) TotalDuration() float64
func (*MediaPlaylist) Version ¶
func (p *MediaPlaylist) Version() uint8
Version returns playlist's version number.
func (*MediaPlaylist) WinSize ¶
func (p *MediaPlaylist) WinSize() uint
WinSize returns the playlist's window size.
func (*MediaPlaylist) WithCustomDecoders ¶
func (p *MediaPlaylist) WithCustomDecoders(customDecoders []CustomDecoder) Playlist
WithCustomDecoders adds custom tag decoders to the media playlist for decoding.
func (*MediaPlaylist) WritePrecision ¶
func (p *MediaPlaylist) WritePrecision() int
WritePrecision returns the current write precision for float values.
type MediaSegment ¶
type MediaSegment struct {
SeqId uint64 // SeqId is the sequence number of the segment. Should be unique and consecutive.
URI string // URI is the path to the media segment.
Duration float64 // EXTINF first parameter. Duration in seconds.
Title string // EXTINF optional second parameter.
Limit int64 // EXT-X-BYTERANGE <n> is length in bytes for the file under URI.
Offset int64 // EXT-X-BYTERANGE [@o] is offset from the start of the file under URI.
Keys []Key // EXT-X-KEY changes the key for encryption until next EXT-X-KEY tag.
Map *Map // EXT-X-MAP changes the Media Initialization Section until next EXT-X-MAP tag.
Discontinuity bool // EXT-X-DISCONTINUITY signals a discontinuity between the surrounding segments.
SCTE *SCTE // SCTE-35 used for Ad signaling in HLS.
SCTE35DateRanges []*DateRange // SCTE-35 date-range tags preceeding this segment
ProgramDateTime time.Time // EXT-X-PROGRAM-DATE-TIME associates first sample with an absolute date and/or time.
Custom CustomMap // Custom holds custom tags
Gap bool
}
MediaSegment represents a media segment included in a media playlist. Media segment may be encrypted.
type PartialSegment ¶
type PartialSegment struct {
SeqID uint64 // Sequence ID of the partial segment
URI string // EXT-X-PART:URI
Duration float64 // EXT-X-PART:DURATION
Independent bool // EXT-X-PART:INDEPENDENT
ProgramDateTime time.Time // EXT-X-PROGRAM-DATE-TIME
Offset int64 // EXT-X-PART:BYTERANGE [@o] is offset from the start of the file under URI.
Limit int64 // EXT-X-PART:BYTERANGE <n> is length in bytes for the file under URI.
Gap bool // EXT-X-PART:GAP enumerated-string ("YES" if the Partial Segment is not available)
}
PartialSegment represents a partial segment included in a low-latency media playlist.
type Playlist ¶
type Playlist interface {
Encode() *bytes.Buffer
Decode(bytes.Buffer, bool) error
DecodeFrom(reader io.Reader, strict bool) error
WithCustomDecoders([]CustomDecoder) Playlist
String() string
CalcMinVersion() (ver uint8, reason string)
Version() uint8
SetVersion(ver uint8)
WritePrecision() int
}
Playlist interface applied to various playlist types.
type PreloadHint ¶
type SCTE ¶
type SCTE struct {
Syntax SCTE35Syntax // Syntax defines the format of the SCTE-35 cue tag
CueType SCTE35CueType // CueType defines whether the cue is a start, mid, end, cmd (as applicable)
Cue string // Base64 encoded SCTE-35 cue message
ID string // Unique ID
Time float64 // TIME for SCTE-67 and OATCLS SCTE-35 signalling
Elapsed float64 // ELAPSED for OATCLS SCTE-35 signalling
Duration *float64 // DURATION in seconds for OATCLS signalling
}
SCTE holds custom SCTE-35 tags.
type SCTE35CueType ¶
type SCTE35CueType uint
SCTE35CueType defines the type of cue point
const ( SCTE35Cue_Start SCTE35CueType = iota // SCTE35Cue_Start indicates an cue-out point SCTE35Cue_Mid // SCTE35Cue_Mid indicates a segment between start and end cue points SCTE35Cue_End // SCTE35Cue_End indicates a cue-in point )
type SCTE35Syntax ¶
type SCTE35Syntax uint
SCTE35Syntax defines the format of the SCTE-35 cue points including EXT-X-DATERANGE version.
const ( SCTE35_NONE SCTE35Syntax = iota // No SCTE markers set or seen SCTE35_67_2014 // SCTE35_67_2014 defined in [scte67] SCTE35_OATCLS // SCTE35_OATCLS is a non-standard but common format SCTE35_DATERANGE // SCTE35_DATERANGE is standard format for HLS. Stored separately )
func (SCTE35Syntax) String ¶
func (s SCTE35Syntax) String() string
type SegmentIndexing ¶
type SegmentIndexing struct {
// NextMSNIndex represents the index to be used for the next full media segment in the playlist.
// It is incremented each time a new full media segment is appended to the playlist,
// maintaining the sequence number for the next media segment to be added.
NextMSNIndex uint64
// NextPartIndex represents the index to be used for the next partial segment in the playlist.
// It starts at 0 and is incremented each time a new partial segment is appended to the playlist.
// It helps in maintaining the sequence number for the next partial segment to be added.
// When a new full segment is added, this index is reset to 0.
NextPartIndex uint64
// MaxPartIndex represents the maximum index of a partial segment that has been added to the playlist.
// It is updated whenever a new partial segment is appended, ensuring that it always holds the highest
// index value of the partial segments. This helps in tracking the highest partial segment index for
// the current full segment.
MaxPartIndex uint64
}
SegmentIndexing holds the indexing parameters for media and partial segments in the low-latency media playlist.
type ServerControl ¶
type SessionData ¶
type SessionData struct {
DataId string // DATA-ID is a mandatory quoted-string
Value string // VALUE is a quoted-string
URI string // URI is a quoted-string
Format string // FORMAT is enumerated string. Values are JSON and RAW (default is JSON)
Language string // LANGUAGE is a quoted-string containing an [RFC5646] language tag
}
SessionData represents an EXT-X-SESSION-DATA tag.
type Variant ¶
type Variant struct {
URI string // URI is the path to the media playlist. Parameter for I-frame playlist.
Chunklist *MediaPlaylist // Chunklist is the media playlist for the variant.
VariantParams
}
Variant structure represents media playlist variants in master playlists.
type VariantParams ¶
type VariantParams struct {
Bandwidth uint32 // BANDWIDTH parameter
AverageBandwidth uint32 // AVERAGE-BANDWIDTH parameter
Score float64 // SCORE parameter
Codecs string // CODECS parameter
SupplementalCodecs string // SUPPLEMENTAL-CODECS parameter
Resolution string // RESOLUTION parameter (WxH)
FrameRate float64 // FRAME-RATE parameter.
HDCPLevel string // HDCP-LEVEL parameter: NONE, TYPE-0, TYPE-1
AllowedCPC string // ALLOWED-CPC parameter
VideoRange string // VIDEO-RANGE parameter: SDR, HLG, PQ
ReqVideoLayout string // REQ-VIDEO-LAYOUT parameter
StableVariantId string // STABLE-VARIANT-ID parameter
Audio string // AUDIO alternative renditions group ID. EXT-X-STREAM-INF only
Video string // VIDEO alternative renditions group ID. EXT-X-STREAM-INF only
Subtitles string // SUBTITLESalternative renditions group ID. EXT-X-STREAM-INF only
Captions string // CLOSED-CAPTIONS parameter, NONE or Quoted String
PathwayId string // PATHWAY-ID parameter for Content Steering
Name string // NAME parameter. EXT-X-STREAM-INF only. Non-standard Wowza/JWPlayer extension
ProgramId *int // PROGRAM-ID parameter. Removed in version 6
Iframe bool // EXT-X-I-FRAME-STREAM-INF flag.
Alternatives []*Alternative // EXT-X-MEDIA parameters
}
VariantParams represents parameters for a Variant. Used in EXT-X-STREAM-INF and EXT-X-I-FRAME-STREAM-INF. URI parameter for EXT-X-I-FRAME-STREAM-INF is in Variant.