forked from grafov/m3u8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
298 lines (277 loc) · 7.45 KB
/
reader_test.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
/*
Playlist parsing tests.
Copyleft 2013-2015 Alexander I.Grafov aka Axel <grafov@gmail.com>
Copyleft 2013-2015 library authors (see AUTHORS file).
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
ॐ तारे तुत्तारे तुरे स्व
*/
package m3u8
import (
"bufio"
"fmt"
"os"
"testing"
)
func TestDecodeMasterPlaylist(t *testing.T) {
f, err := os.Open("sample-playlists/master.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
// check parsed values
if p.ver != 3 {
t.Errorf("Version of parsed playlist = %d (must = 3)", p.ver)
}
if len(p.Variants) != 5 {
t.Error("Not all variants in master playlist parsed.")
}
// TODO check other values
// fmt.Println(p.Encode().String())
}
func TestDecodeMasterPlaylistWithMultipleCodecs(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-multiple-codecs.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
// check parsed values
if p.ver != 3 {
t.Errorf("Version of parsed playlist = %d (must = 3)", p.ver)
}
if len(p.Variants) != 5 {
t.Error("Not all variants in master playlist parsed.")
}
for _, v := range p.Variants {
if v.Codecs != "avc1.42c015,mp4a.40.2" {
t.Error("Codec string is wrong")
}
}
// TODO check other values
// fmt.Println(p.Encode().String())
}
func TestDecodeMasterPlaylistWithAlternatives(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-alternatives.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
// check parsed values
if p.ver != 3 {
t.Errorf("Version of parsed playlist = %d (must = 3)", p.ver)
}
// if len(p.Variants) != 5 {
// t.Fatal("Not all variants in master playlist parsed.")
// }
// TODO check other values
//fmt.Println(p.Encode().String())
}
// Decode a master playlist with Name tag in EXT-X-STREAM-INF
func TestDecodeMasterPlaylistWithStreamInfName(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-stream-inf-name.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
for _, variant := range p.Variants {
if variant.Name == "" {
t.Errorf("Empty name tag on variant URI: %s", variant.URI)
}
}
}
func TestDecodeMediaPlaylist(t *testing.T) {
f, err := os.Open("sample-playlists/wowza-vod-chunklist.m3u8")
if err != nil {
t.Fatal(err)
}
p, err := NewMediaPlaylist(5, 798)
if err != nil {
t.Fatalf("Create media playlist failed: %s", err)
}
err = p.DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
//fmt.Printf("Playlist object: %+v\n", p)
// check parsed values
if p.ver != 3 {
t.Errorf("Version of parsed playlist = %d (must = 3)", p.ver)
}
if p.TargetDuration != 12 {
t.Errorf("TargetDuration of parsed playlist = %f (must = 12.0)", p.TargetDuration)
}
if !p.Closed {
t.Error("This is a closed (VOD) playlist but Close field = false")
}
// TODO check other values…
//fmt.Println(p.Encode().String()), stream.Name}
}
func TestDecodeMediaPlaylistWithWidevine(t *testing.T) {
f, err := os.Open("sample-playlists/widevine-bitrate.m3u8")
if err != nil {
t.Fatal(err)
}
p, err := NewMediaPlaylist(5, 798)
if err != nil {
t.Fatalf("Create media playlist failed: %s", err)
}
err = p.DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
//fmt.Printf("Playlist object: %+v\n", p)
// check parsed values
if p.ver != 2 {
t.Errorf("Version of parsed playlist = %d (must = 2)", p.ver)
}
if p.TargetDuration != 9 {
t.Errorf("TargetDuration of parsed playlist = %f (must = 9.0)", p.TargetDuration)
}
// TODO check other values…
//fmt.Printf("%+v\n", p.Key)
//fmt.Println(p.Encode().String())
}
func TestDecodeMasterPlaylistWithAutodetection(t *testing.T) {
print("test")
f, err := os.Open("sample-playlists/master.m3u8")
if err != nil {
t.Fatal(err)
}
m, listType, err := DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
if listType != MASTER {
t.Error("Sample not recognized as master playlist.")
}
mp := m.(*MasterPlaylist)
// fmt.Printf(">%+v\n", mp)
// for _, v := range mp.Variants {
// fmt.Printf(">>%+v +v\n", v)
// }
//fmt.Println("Type below must be MasterPlaylist:")
CheckType(t, mp)
}
func TestDecodeMediaPlaylistWithAutodetection(t *testing.T) {
f, err := os.Open("sample-playlists/wowza-vod-chunklist.m3u8")
if err != nil {
t.Fatal(err)
}
p, listType, err := DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
pp := p.(*MediaPlaylist)
CheckType(t, pp)
if listType != MEDIA {
t.Error("Sample not recognized as media playlist.")
}
// check parsed values
if pp.TargetDuration != 12 {
t.Errorf("TargetDuration of parsed playlist = %f (must = 12.0)", pp.TargetDuration)
}
if !pp.Closed {
t.Error("This is a closed (VOD) playlist but Close field = false")
}
// TODO check other values…
// fmt.Println(pp.Encode().String())
}
func TestDecodeMediaPlaylistWithCue(t *testing.T) {
f, err := os.Open("sample-playlists/cue-mid.m3u8")
if err != nil {
t.Fatal(err)
}
p, listType, err := DecodeFrom(bufio.NewReader(f), true)
if err != nil {
t.Fatal(err)
}
pp := p.(*MediaPlaylist)
CheckType(t, pp)
if listType != MEDIA {
t.Error("Cue not recognized as media playlist.")
}
co := pp.Segments[2].CueOut
cs := pp.Segments[3].CueSpan
ci := pp.Segments[5].CueIn
// CueOut
if co == nil {
t.Error("CueOut not recognized")
}
if co.ID != "1234" {
t.Errorf("CueOut has invalid ID %s", co.ID)
}
if co.Duration != 30 {
t.Errorf("CueOut has invalid duration %f", co.Duration)
}
if co.CueID == "" {
t.Errorf("CueOut has no cueID")
}
if cs == nil {
t.Error("CueSpan not recognized")
}
if cs.ID != "1234" {
t.Errorf("CueSpan has invalid ID %s", cs.ID)
}
if cs.TimeFromSignal != "PT10.01S" {
t.Errorf("CueSpan has invalid TimeFromSignal %s", cs.TimeFromSignal)
}
if ci == nil {
t.Error("CueIn not recognized")
}
if ci.ID != "1234" {
t.Errorf("CueIn has invalid ID %s", ci.ID)
}
if ci.CueID == "" {
t.Error("CueIn has no cueID")
}
}
/***************************
* Code parsing examples *
***************************/
// Example of parsing a playlist with EXT-X-DISCONTINIUTY tag
// and output it with integer segment durations.
func ExampleMediaPlaylist_DurationAsInt() {
f, _ := os.Open("sample-playlists/media-playlist-with-discontinuity.m3u8")
p, _, _ := DecodeFrom(bufio.NewReader(f), true)
pp := p.(*MediaPlaylist)
pp.DurationAsInt(true)
fmt.Printf("%s", pp)
// Output:
// #EXTM3U
// #EXT-X-VERSION:3
// #EXT-X-MEDIA-SEQUENCE:1
// #EXT-X-TARGETDURATION:10
// #EXTINF:10,
// ad0.ts
// #EXTINF:8,
// ad1.ts
// #EXT-X-DISCONTINUITY
// #EXTINF:10,
// movieA.ts
// #EXTINF:10,
// movieB.ts
}