-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaterange.go
242 lines (198 loc) · 5.66 KB
/
daterange.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
package m3u8
import (
"strings"
"time"
)
type DateRange struct {
// ID uniquely identifies a Date Range in the Playlist.
//
// ID is REQUIRED.
ID string
// Class specifies some set of attributes and their associated value
// semantics. All Date Ranges with the same Class value MUST adhere to
// these semantics.
//
// Class is REQUIRED.
Class string
// StartDate is a ISO-8601 formatted date at which the Date Range begins.
//
// StartDate is REQUIRED.
StartDate string
// EndDate is a ISO-8601 formatted date at which the Date Range ends.
//
// It MUST be equal to or later than the value of StartDate.
//
// EndDate is OPTIONAL.
EndDate string
// Duration is the duration of the Date Range.
//
// Duration is OPTIONAL.
Duration time.Duration
// PlannedDuration is the expected duration of the Date Range.
//
// PlannedDuration is OPTIONAL.
PlannedDuration time.Duration
// ClientAttributes are client-defined attributes. The map keys may only
// use uppercase alphanumeric characters and hyphens. The map values may
// only be of type string, []byte or float64.
//
// ClientAttributes are OPTIONAL.
ClientAttributes map[string]interface{}
// SCTE35Command is the big-endian binary representation of the
// splice_info_section().
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.2.7.1.
SCTE35Command []byte
// SCTE35Out is the big-endian binary representation of the "out"
// splice_info_section().
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.2.7.1.
SCTE35Out []byte
// SCTE35In is the big-endian binary representation of the "in"
// splice_info_section().
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.2.7.1.
SCTE35In []byte
// EndOnNext indicates that the end of the range is equal to the StartDate
// of the Following Range. The Following Range is the Date Range with the
// same Class value that has the earliest StartDate after the StartDate of
// the range in question.
//
// EndOnNext is OPTIONAL.
EndOnNext bool
}
func parseDateRange(meta string) (*DateRange, error) {
attrs, err := parseAttributeList(meta)
if err != nil {
return nil, err
}
var dr DateRange
dr.ID, err = attrs.string(attrID)
if err != nil {
return nil, err
}
dr.Class, err = attrs.string(attrClass)
if err != nil && !isMissingAttr(err) {
return nil, err
}
dr.StartDate, err = attrs.string(attrStartDate)
if err != nil {
return nil, err
}
if err = validateDate(dr.StartDate); err != nil {
return nil, err
}
dr.EndDate, err = attrs.string(attrEndDate)
if missing := isMissingAttr(err); err != nil && !missing {
return nil, err
} else if !missing {
if err = validateDate(dr.EndDate); err != nil {
return nil, err
}
}
var duration float64
duration, err = attrs.float(attrDuration)
if missing := isMissingAttr(err); err != nil && !missing {
return nil, err
} else if !missing {
dr.Duration = secondsToDuration(duration)
}
var plannedDuration float64
plannedDuration, err = attrs.float(attrPlannedDuration)
if missing := isMissingAttr(err); err != nil && !missing {
return nil, err
} else if !missing {
dr.PlannedDuration = secondsToDuration(plannedDuration)
}
clientAttributes := make(map[string]interface{})
for name, value := range attrs {
if name[:2] != "X-" {
continue
}
switch v := value.(type) {
case unsignedFloat:
clientAttributes[name] = float64(v)
case string, []byte:
clientAttributes[name] = value
default:
return nil, &Error{"illegal client attribute value"}
}
}
if len(clientAttributes) > 0 {
dr.ClientAttributes = clientAttributes
}
var scte35Cmd, scte35Out, scte35In []byte
scte35Cmd, err = attrs.bytes(attrSCTE35Command)
if missing := isMissingAttr(err); err != nil && !missing {
return nil, err
} else if !missing {
dr.SCTE35Command = scte35Cmd
}
scte35Out, err = attrs.bytes(attrSCTE35Out)
if missing := isMissingAttr(err); err != nil && !missing {
return nil, err
} else if !missing {
dr.SCTE35Out = scte35Out
}
scte35In, err = attrs.bytes(attrSCTE35In)
if missing := isMissingAttr(err); err != nil && !missing {
return nil, err
} else if !missing {
dr.SCTE35In = scte35In
}
var endOnNext string
endOnNext, err = attrs.enum(attrEndOnNext)
if missing := isMissingAttr(err); err != nil && !missing {
return nil, err
} else if !missing {
if endOnNext != "YES" {
return nil, &invalidAttributeValueError{attrEndOnNext}
}
if dr.Class == "" {
return nil, &Error{`this tag must have attribute, "` + attrClass + `", with attribute, "` + attrEndOnNext + `",`}
}
if dr.Duration != 0 {
return nil, &Error{`this tag may not have attribute, "` + attrDuration + `", with attribute, "` + attrEndOnNext + `",`}
}
if dr.EndDate != "" {
return nil, &Error{`this tag may not have attribute, "` + attrEndDate + `", with attribute, "` + attrEndOnNext + `",`}
}
}
return &dr, nil
}
func (r DateRange) attrs() (attributes, error) {
attrs := attributes{
attrID: r.ID,
attrStartDate: r.StartDate,
}
if r.Class != "" {
attrs[attrClass] = r.Class
}
if r.EndDate != "" {
attrs[attrEndDate] = r.EndDate
}
if r.Duration > 0 {
attrs[attrDuration] = unsignedFloat(r.Duration.Seconds())
}
if r.PlannedDuration > 0 {
attrs[attrPlannedDuration] = unsignedFloat(r.PlannedDuration.Seconds())
}
if len(r.ClientAttributes) > 0 {
for name, value := range r.ClientAttributes {
attrs[strings.ToUpper(name)] = value
}
}
if len(r.SCTE35Command) > 0 {
attrs[attrSCTE35Command] = r.SCTE35Command
}
if len(r.SCTE35Out) > 0 {
attrs[attrSCTE35Out] = r.SCTE35Out
}
if len(r.SCTE35In) > 0 {
attrs[attrSCTE35In] = r.SCTE35In
}
if r.EndOnNext {
attrs[attrEndOnNext] = enumeratedString("YES")
}
return attrs, nil
}