-
Notifications
You must be signed in to change notification settings - Fork 0
/
hls.go
316 lines (280 loc) · 7.67 KB
/
hls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Package hls implement an HLS codec for Master and Media files in m3u format
// At this time, the codec only supports decoding
package hls
import (
"errors"
"image"
"io"
"net/url"
"strconv"
"strings"
"time"
"github.com/as/hls/m3u"
)
// Media playlist types
const (
Vod = "VOD" // immutable
Event = "EVENT" // append-only
Live = "" // sliding-window
)
var (
ErrHeader = errors.New("hls: no m3u8 tag")
ErrEmpty = errors.New("hls: empty playlist")
ErrType = errors.New("hls: playlist type mismatch")
)
// Decode reads an HLS playlist from the reader and tokenizes
// it into a list of tags. Master is true if and only if the input looks
// like a master playlist.
func Decode(r io.Reader) (t []m3u.Tag, master bool, err error) {
t, err = m3u.Parse(r)
for _, v := range t {
switch v.Name {
case "EXT-X-MEDIA":
fallthrough
case "EXT-X-STREAM-INF":
fallthrough
case "EXT-X-I-FRAME-STREAM-INF":
return t, true, err // master
case "EXTINF":
return t, false, err // media
}
}
// may be empty live media
return t, false, err
}
// Master is a master playlist. It contains a list of streams (variants) and
// media information associated by group id. By convention, the master playlist is immutable.
type Master struct {
M3U bool `hls:"EXTM3U"`
Version int `hls:"EXT-X-VERSION"`
Independent bool `hls:"EXT-X-INDEPENDENT-SEGMENTS"`
Media []MediaInfo `hls:"EXT-X-MEDIA"`
Stream []StreamInfo `hls:"EXT-X-STREAM-INF"`
}
// Decode decodes the master playlist into m.
func (m *Master) Decode(r io.Reader) error {
t, master, err := Decode(r)
if err != nil {
return err
}
if !master {
return ErrType
}
return m.DecodeTag(t...)
}
func (m *Master) DecodeTag(t ...m3u.Tag) error {
if err := unmarshalTag0(m, t...); err != nil {
return err
}
if !m.M3U {
return ErrHeader
}
if len(m.Stream) == 0 {
return ErrEmpty
}
return nil
}
// Len returns the number of variant streams
func (m *Master) Len() int {
return len(m.Stream)
}
// Media is a media playlist. It consists of a header and one or more files. A file
// is EXTINF and the content of any additional tags that apply to that EXTINF tag.
type Media struct {
MediaHeader
File []File `hls:""`
}
type MediaHeader struct {
M3U bool `hls:"EXTM3U"`
Version int `hls:"EXT-X-VERSION"`
Independent bool `hls:"EXT-X-INDEPENDENT-SEGMENTS"`
Type string `hls:"EXT-X-PLAYLIST-TYPE"`
Target time.Duration `hls:"EXT-X-TARGETDURATION"`
Start Start `hls:"EXT-X-START"`
Sequence int `hls:"EXT-X-MEDIA-SEQUENCE"`
Discontinuity int `hls:"EXT-X-DISCONTINUITY-SEQUENCE"`
End bool `hls:"EXT-X-ENDLIST"`
}
// Runtime measures the cumulative duration of the given
// window of segments (files)
func Runtime(f ...File) (cumulative time.Duration) {
for _, f := range f {
cumulative += f.Inf.Duration
}
return
}
// Decode decodes the playlist in r and stores the
// result in m. It returns ErrEmpty if the playlist is
// well-formed, but contains no variant streams.
func (m *Media) Decode(r io.Reader) error {
t, master, err := Decode(r)
if err != nil {
return err
}
if master {
return ErrType
}
return m.DecodeTag(t...)
}
// DecodeTag decodes the list of tags as a media playlist
func (m *Media) DecodeTag(t ...m3u.Tag) error {
if err := unmarshalTag0(&m.MediaHeader, t...); err != nil {
return err
}
if !m.M3U {
return ErrHeader
}
file := File{}
i := 0
for j := range t {
if t[j].Name != "EXTINF" {
continue
}
if err := unmarshalTag0(&file, t[i:j+1]...); err != nil {
return err
}
i = j
m.File = append(m.File, file)
file = file.sticky()
}
if m.Len() == 0 {
return ErrEmpty
}
return nil
}
// Current returns the most-recent segment in the stream
func (m *Media) Current() (f File) {
if len(m.File) == 0 {
return
}
return m.File[len(m.File)-1]
}
// Len returns the number of segments visibile to the playlist
func (m *Media) Len() int {
return len(m.File)
}
func (m Media) EncodeTag() (t []m3u.Tag, err error) {
if t, err = marshalTag0(m.MediaHeader); err != nil {
return t, err
}
for _, v := range m.File {
tmp, err := marshalTag0(v)
t = append(t, tmp...)
if err != nil {
return t, err
}
}
return t, err
}
type File struct {
Discontinuous bool `hls:"EXT-X-DISCONTINUITY,omitempty"`
Time time.Time `hls:"EXT-X-PROGRAM-DATE-TIME,omitempty"`
Range Range `hls:"EXT-X-BYTERANGE,omitempty"`
Map Map `hls:"EXT-X-MAP,omitempty"`
Key Key `hls:"EXT-X-KEY,omitempty"`
Inf Inf `hls:"EXTINF"`
}
// Location returns the media URL relative to base. It conditionally
// applies the base URL in cases where the media URL is a relative
// path. Base may be nil. This function never returns nil, but may
// return an empty URL. For error handling, process f.Inf.URL manually
func (f File) Location(base *url.URL) *url.URL {
return location(base, f.Inf.URL)
}
// Init returns the initialization segment for fragmented mp4 files
// as an absolute url relative to base. If there is no initialization
// segment it returns an empty URL.
func (f File) Init(base *url.URL) *url.URL {
u := f.Map.URI
if u == "" {
return &url.URL{}
}
return location(base, u)
}
// Duration returns the segment duration. An optional target can
// be provided as a fallback in case the duration was not set.
func (f File) Duration(target time.Duration) time.Duration {
if f.Inf.Duration == 0 {
return target
}
return f.Inf.Duration
}
// sticky returns a copy of f with only sticky field set
// a sticky field is a field that propagates across Inf blocks
//
func (f File) sticky() File {
return File{
Map: f.Map,
Key: f.Key,
}
}
type Range struct {
V string `hls:""`
}
func (r Range) Value(n int) (at, size int, err error) {
a := strings.Split(r.V, "@")
if size, err = strconv.Atoi(a[0]); err != nil {
return at, size, err
}
if len(a) == 1 {
return n, size, nil
}
at, err = strconv.Atoi(a[1])
return at, size, err
}
type Key struct {
}
type Map struct {
URI string `hls:"URI"`
}
type Start struct {
Offset time.Duration `hls:"TIME-OFFSET"`
Precise bool `hls:"PRECISE"`
}
type Inf struct {
Duration time.Duration `hls:"$1"`
Description string `hls:"$2"`
URL string `hls:"$file"`
}
type MediaInfo struct {
Type string `hls:"TYPE"`
Group string `hls:"GROUP-ID"`
Name string `hls:"NAME"`
Default bool `hls:"DEFAULT"`
Autoselect bool `hls:"AUTOSELECT"`
Forced bool `hls:"FORCED"`
Lang string `hls:"LANGUAGE"`
URI string `hls:"URI"`
}
type StreamInfo struct {
URL string `hls:"$file"`
Index int `hls:"PROGRAM-ID"`
Framerate float64 `hls:"FRAME-RATE"`
Bandwidth int `hls:"BANDWIDTH"`
BandwidthAvg int `hls:"AVERAGE-BANDWIDTH"`
Codecs []string `hls:"CODECS"`
Resolution image.Point `hls:"RESOLUTION"`
VideoRange string `hls:"VIDEO-RANGE"`
HDCP string `hls:"HDCP-LEVEL"`
Audio string `hls:"AUDIO"`
Video string `hls:"VIDEO"`
Subtitle string `hls:"SUBTITLES"`
Caption string `hls:"CLOSED-CAPTIONS"`
}
// Location returns the stream URL relative to base. It conditionally
// applies the base URL in cases where the stream URL is a relative
// path. Base may be nil. This function never returns nil, but may
// return an empty URL. For error handling, process s.URLmanually.
func (s StreamInfo) Location(base *url.URL) *url.URL {
return location(base, s.URL)
}
func location(base *url.URL, ref string) *url.URL {
if base == nil {
base = &url.URL{}
}
u, err := url.Parse(ref)
if err != nil {
return u
}
return base.ResolveReference(u)
}