-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
222 lines (177 loc) · 4.07 KB
/
decoder.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
package m3u8
import (
"bufio"
"bytes"
"io"
"regexp"
"strconv"
"strings"
"time"
)
type Decoder struct {
r io.Reader
Strict bool
}
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
r: r,
Strict: true,
}
}
func (d *Decoder) Decode() (Playlist, error) {
scanner := bufio.NewScanner(d.r)
if !scanner.Scan() {
return nil, io.ErrUnexpectedEOF
}
if firstLine := scanner.Text(); firstLine != headerTag {
return nil, ErrNoHeader
}
p, err := decode(scanner, d.Strict)
if err != nil {
return nil, err
}
return p, nil
}
func DecodePlaylist(data []byte) (Playlist, error) {
return NewDecoder(bytes.NewBuffer(data)).Decode()
}
type line interface {
line() string
}
type uri string
func (u uri) line() string {
return string(u)
}
type split struct {
num int
tag string
meta string
}
func (s *split) line() string {
if s.meta == "" {
return s.tag
}
return s.tag + ":" + s.meta
}
func secondsToDuration(s float64) time.Duration {
return time.Duration(s * float64(time.Second))
}
func parseDuration(str string) (time.Duration, error) {
seconds, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, &Error{msg: "failed to parse duration"}
}
return secondsToDuration(seconds), nil
}
var rxISO8601 = regexp.MustCompile("^[+-]?\\d{4,}(?:-?(?:\\d{2}(?:-?\\d{2})?|W\\d{2}(?:-?\\d)?|\\d{3}))?(?:T\\d{2}(?::?\\d{2}(?::?\\d{2}(?:\\.\\d+)?)?)?(?:Z|[+-]\\d{2}(?::?\\d{2})?)?)?$")
func validateDate(str string) error {
if rxISO8601.MatchString(str) {
return nil
}
return &Error{msg: "invalid date format"}
}
// decode determines the playlist type, parses common tags, and buffers
// important lines for further processing.
func decode(scanner *bufio.Scanner, strict bool) (Playlist, error) {
var pType Type
var lines []line
var base GenericPlaylist
for lineNumber := 2; scanner.Scan(); lineNumber++ {
line := scanner.Text()
if len(line) == 0 {
// ignore blank lines
continue
}
if line[0] != '#' {
// this is a url line
lines = append(lines, uri(line))
continue
}
if line[:4] != tagPrefix {
// ignore comments
continue
}
var s split
if colon := strings.IndexRune(line, ':'); colon >= 0 {
s.tag = line[:colon]
s.meta = line[colon+1:]
} else {
s.tag = line
}
switch s.tag {
case versionTag:
num, err := strconv.ParseInt(s.meta, 0, 64)
if err != nil {
return nil, ErrBadVersionNumber
}
base.Version = int(num)
continue
case infTag, byterangeTag, discontinuityTag, keyTag, mapTag, programDateTimeTag, daterangeTag:
// media segment tags
fallthrough
case targetdurationTag, mediaSequenceTag, discontinuitySequenceTag, endlistTag, playlistTypeTag, iFramesOnlyTag:
// media playlist tags
if pType == 0 {
pType = Media
}
if pType != Media {
return nil, ErrMixedTags
}
case mediaTag, streamInfTag, iFrameStreamInfTag, sessionDataTag, sessionKeyTag:
// master playlist tags
if pType == 0 {
pType = Master
}
if pType != Master {
return nil, ErrMixedTags
}
case independentSegmentsTag:
base.IndependentSegments = true
case startTag:
attrs, err := parseAttributeList(s.meta)
if err != nil {
return nil, isew(&s, err)
}
var start Start
timeOffset, err := attrs.float(attrTimeOffset)
if err != nil {
return nil, isew(&s, err)
}
start.TimeOffset = secondsToDuration(timeOffset)
precise, err := attrs.enum(attrPrecise)
if missing := isMissingAttr(err); err != nil && !missing {
return nil, isew(&s, err)
} else if !missing {
switch precise {
case "YES":
start.Precise = true
case "NO":
default:
return nil, &invalidAttributeValueError{attrPrecise}
}
}
default:
if strict {
return nil, (*UnexpectedTagError)(&s)
}
}
s.num = lineNumber
lines = append(lines, &s)
}
switch pType {
case Media:
p, err := parseMediaPlaylist(&base, lines)
if err != nil {
return nil, err
}
return p, nil
case Master:
p, err := parseMasterPlaylist(&base, lines)
if err != nil {
return nil, err
}
return p, nil
default:
return nil, ErrUnknownType
}
}