-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment.go
281 lines (230 loc) · 6.2 KB
/
segment.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
package m3u8
import (
"fmt"
"io"
"strings"
"time"
)
// MediaSegment represents a Media Segment.
//
// See https://tools.ietf.org/html/rfc8216#section-3 for information about
// Media Segments.
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.2 for information about
// Media Segment Tags.
type MediaSegment struct {
// URI is the resource of the Media Segment.
URI string
// Duration specifies the duration of the Media Segment.
Duration time.Duration
// Title is an optional human-readable informative title of the Media
// Segment.
Title string
// ByteRange indicates that a Media Segment is a sub-range of the resource
// identified by the URI value. It applies only to this Media Segment.
//
// A Media Segment without a ByteRange value consists of the entire
// resource identified by the URI value.
//
// Use of ByteRange REQUIRES a compatibility version number of 4 or
// greater.
ByteRange *ByteRange
// Discontinuity indicates a discontinuity between the Media Segment that
// follows it and the one that preceded it.
//
// Discontinuity MUST be true if there is a change in any of the following
// characteristics:
//
// - file format
//
// - number, type, and identifiers of tracks
//
// - timestamp sequence
//
// - encoding parameters
//
// - encoding sequence
Discontinuity bool
// Key specifies how to decrypt encrypted Media Segments. It applies to
// every Media Segment and to every Media Initialization Section declared
// by a non-nil Map value that appears between it and the next non-nil Key
// value in the Playlist file with the same KeyFormat value (or the end of
// the Playlist file).
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.2.4.
Key *Key
// Map specifies how to 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 Media Segment with
// a non-nil Map value or until the end of the Playlist.
Map *Map
// ProgramDateTime associates the first sample of a Media Segment with an
// absolute date and/or time. It applies only to the next Media Segment.
//
// NOTE: This is left as a string because there is no easy way to parse the
// expansive variations of ISO 8601:2004 formats and section 4.3.2.6 of rfc
// 8216 does not require a specific accuracy.
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.2.6.
ProgramDateTime string
// DateRange associates a Date Range (i.e., a range of time defined by a
// starting and ending date) with a set of properties.
DateRange *DateRange
}
func parseMediaSegment(p *MediaPlaylist, version int, lines []line) (skip int, err error) {
var segment MediaSegment
LinesLoop:
for i, line := range lines {
if uri, ok := line.(uri); ok {
if i == 0 {
return 0, ErrUnexpectedURI
}
segment.URI = string(uri)
p.Segments = append(p.Segments, &segment)
return i, nil
}
s := line.(*split)
var attrs attributes
switch s.tag {
case infTag:
comma := strings.IndexRune(s.meta, ',')
if comma == -1 {
return 0, ise(s, "missing comma")
}
duration := s.meta[:comma]
segment.Title = s.meta[comma+1:]
if version < 3 {
if decimal := strings.IndexRune(duration, '.'); decimal != -1 {
return 0, &CompatibilityVersionError{s, 3}
}
}
segment.Duration, err = parseDuration(duration)
if err != nil {
return 0, isew(s, err)
}
case byterangeTag:
if version < 4 {
return 0, &CompatibilityVersionError{s, 4}
}
segment.ByteRange, err = parseByteRange(s.meta)
if err != nil && (err != ErrNoRangeStart || !p.last().hasDependableRange()) {
return 0, isew(s, err)
}
case discontinuityTag:
segment.Discontinuity = true
case keyTag:
segment.Key, err = parseKey(version, s.meta)
if err != nil {
return 0, isew(s, err)
}
case mapTag:
attrs, err = parseAttributeList(s.meta)
if err != nil {
return 0, isew(s, err)
}
var m Map
m.URI, err = attrs.string(attrURI)
if err != nil {
return 0, isew(s, err)
}
var strbr string
strbr, err = attrs.string(attrByteRange)
if err != nil && !isMissingAttr(err) {
return 0, isew(s, err)
}
m.ByteRange, err = parseByteRange(strbr)
if err != nil {
return 0, isew(s, err)
}
m.s = s
case programDateTimeTag:
if err = validateDate(s.meta); err != nil {
return 0, isew(s, err)
}
segment.ProgramDateTime = s.meta
case daterangeTag:
segment.DateRange, err = parseDateRange(s.meta)
if err != nil {
return 0, isew(s, err)
}
default:
if i == 0 {
break LinesLoop
}
return 0, &Error{msg: `unexpected tag, "` + s.tag + `"`}
}
}
return 0, ErrNotASegment
}
func (s *MediaSegment) hasDependableRange() bool {
if s == nil {
return false
}
if s.ByteRange == nil {
return false
}
return s.ByteRange.closed()
}
func (s *MediaSegment) encode(w io.Writer) error {
if _, err := fmt.Fprintf(w, infTag+":%g,%s\n", s.Duration.Seconds(), s.Title); err != nil {
return err
}
if s.ByteRange != nil {
if _, err := fmt.Fprintln(w, byterangeTag+":"+s.ByteRange.String()); err != nil {
return err
}
}
if s.Discontinuity {
if _, err := fmt.Fprintln(w, discontinuityTag); err != nil {
return err
}
}
if s.Key != nil {
attrs, err := s.Key.attrs()
if err != nil {
return err
}
encodedAttrs, err := attrs.encode()
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, keyTag+":"+encodedAttrs); err != nil {
return err
}
}
if s.Map != nil {
attrs, err := s.Map.attrs()
if err != nil {
return err
}
encodedAttrs, err := attrs.encode()
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, mapTag+":"+encodedAttrs); err != nil {
return err
}
}
if s.ProgramDateTime != "" {
if _, err := fmt.Fprintln(w, programDateTimeTag+":"+s.ProgramDateTime); err != nil {
return err
}
}
if s.DateRange != nil {
attrs, err := s.DateRange.attrs()
if err != nil {
return err
}
encodedAttrs, err := attrs.encode()
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, daterangeTag+":"+encodedAttrs); err != nil {
return err
}
}
if _, err := fmt.Fprintln(w, s.URI); err != nil {
return err
}
return nil
}