-
Notifications
You must be signed in to change notification settings - Fork 14
/
m3u_test.go
191 lines (149 loc) · 5.48 KB
/
m3u_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
package m3u
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestPlaylist(t *testing.T) {
playlist, _ := Parse("testdata/playlist.m3u")
if len(playlist.Tracks) != 5 {
t.Fatalf("Expected track count to be 5")
}
for i := 0; i < 5; i++ {
if playlist.Tracks[i].Length != i+1 {
t.Fatalf("Expected track Length to be %d but was %d", i+1, playlist.Tracks[i].Length)
}
if playlist.Tracks[i].Name != fmt.Sprintf("Track %d", i+1) {
t.Fatalf("Expected track name to be Track %d but was '%s'", i+1, playlist.Tracks[i].Name)
}
if playlist.Tracks[i].URI != fmt.Sprintf("Track%d.mp4", i+1) {
t.Fatalf("Expected track URI to be Track%d.mp4 but was '%s'", i+1, playlist.Tracks[i].URI)
}
if playlist.Tracks[i].Tags[0].Name != "group-title" {
t.Fatalf("Expected tag to be group-title but was '%s'", playlist.Tracks[i].Tags[0].Name)
}
if playlist.Tracks[i].Tags[0].Value != "Album1" {
t.Fatalf("Expected group-title tag value to be Album1 but was '%s'", playlist.Tracks[i].Tags[0].Value)
}
}
}
func TestRemotePlaylist(t *testing.T) {
playlist, _ := Parse("https://raw.githubusercontent.com/jamesnetherton/m3u/master/testdata/playlist.m3u")
if len(playlist.Tracks) != 5 {
t.Fatalf("Expected track count to be 5")
}
for i := 0; i < 5; i++ {
if playlist.Tracks[i].Length != i+1 {
t.Fatalf("Expected track Length to be %d but was %d", i+1, playlist.Tracks[i].Length)
}
if playlist.Tracks[i].Name != fmt.Sprintf("Track %d", i+1) {
t.Fatalf("Expected track name to be Track %d but was '%s'", i+1, playlist.Tracks[i].Name)
}
if playlist.Tracks[i].URI != fmt.Sprintf("Track%d.mp4", i+1) {
t.Fatalf("Expected track URI to be Track%d.mp4 but was '%s'", i+1, playlist.Tracks[i].URI)
}
if playlist.Tracks[i].Tags[0].Name != "group-title" {
t.Fatalf("Expected tag to be group-title but was '%s'", playlist.Tracks[i].Tags[0].Name)
}
if playlist.Tracks[i].Tags[0].Value != "Album1" {
t.Fatalf("Expected group-title tag value to be Album1 but was '%s'", playlist.Tracks[i].Tags[0].Value)
}
}
}
func TestPlaylistWithStreamInfo(t *testing.T) {
playlist, _ := Parse("testdata/playlist_stream_info.m3u")
if len(playlist.VariantStreams) != 4 {
t.Fatalf("Expected track count to be 4")
}
for i := 0; i < 4; i++ {
if playlist.VariantStreams[i].Bandwidth == 0 {
t.Fatalf("Expected bandwidth to be 128000 but was %d", playlist.VariantStreams[i].Bandwidth)
}
if playlist.VariantStreams[i].Resolution == "" {
t.Fatalf("Expected resolution to be set but was %s", playlist.VariantStreams[i].Resolution)
}
if playlist.VariantStreams[i].AverageBandwith == 0 {
t.Fatalf("Expected average bandwidth to be set but was %d", playlist.VariantStreams[i].AverageBandwith)
}
if playlist.VariantStreams[i].Codecs == "" {
t.Fatalf("Expected codecs to be set but was %s", playlist.VariantStreams[i].Codecs)
}
if playlist.VariantStreams[i].Name == "" {
t.Fatalf("Expected name to be set but was %s", playlist.VariantStreams[i].Name)
}
if playlist.VariantStreams[i].FrameRate == 0 {
t.Fatalf("Expected frame rate to be set but was %f", playlist.VariantStreams[i].FrameRate)
}
if playlist.VariantStreams[i].HdcpLevel == "" {
t.Fatalf("Expected hdcp level to be set but was %s", playlist.VariantStreams[i].HdcpLevel)
}
if playlist.VariantStreams[i].Video == "" {
t.Fatalf("Expected video to be set but was %s", playlist.VariantStreams[i].Video)
}
if playlist.VariantStreams[i].Audio == "" {
t.Fatalf("Expected audio to be set but was %s", playlist.VariantStreams[i].Audio)
}
if playlist.VariantStreams[i].URI == "" {
t.Fatalf("Expected URI to be set but was %s", playlist.VariantStreams[i].URI)
}
}
}
func TestPlaylistInvalidHeader(t *testing.T) {
_, err := Parse("testdata/playlist_no_header.m3u")
if err == nil {
t.Fatalf("Expected parse error")
}
}
func TestPlaylistBadExtinf(t *testing.T) {
_, err := Parse("testdata/playlist_bad_extinf_format.m3u")
if err == nil {
t.Fatalf("Expected parse error")
}
}
func TestPlaylistFileNotFound(t *testing.T) {
_, err := Parse("testdata/playlist_no_exists.m3u")
if err == nil {
t.Fatalf("Expected parse error")
}
}
func TestPlaylistMissingInf(t *testing.T) {
_, err := Parse("testdata/playlist_missing_inf.m3u")
if err == nil {
t.Fatalf("Expected parse error")
}
}
func TestMarshallPlaylist(t *testing.T) {
playlist, err := Parse("testdata/playlist.m3u")
if err != nil {
t.Fatal(err)
}
reader, err := Marshall(playlist)
if err != nil {
t.Fatal(err)
}
b := reader.(*bytes.Buffer)
ioutil.WriteFile("/tmp/test_m3u_marshalling.m3u", b.Bytes(), os.ModePerm)
playlist, _ = Parse("/tmp/test_m3u_marshalling.m3u")
if len(playlist.Tracks) != 5 {
t.Fatalf("Expected track count to be 5")
}
for i := 0; i < 5; i++ {
if playlist.Tracks[i].Length != i+1 {
t.Fatalf("Expected track Length to be %d but was %d", i+1, playlist.Tracks[i].Length)
}
if playlist.Tracks[i].Name != fmt.Sprintf("Track %d", i+1) {
t.Fatalf("Expected track name to be Track %d but was '%s'", i+1, playlist.Tracks[i].Name)
}
if playlist.Tracks[i].URI != fmt.Sprintf("Track%d.mp4", i+1) {
t.Fatalf("Expected track URI to be Track%d.mp4 but was '%s'", i+1, playlist.Tracks[i].URI)
}
if playlist.Tracks[i].Tags[0].Name != "group-title" {
t.Fatalf("Expected tag to be group-title but was '%s'", playlist.Tracks[i].Tags[0].Name)
}
if playlist.Tracks[i].Tags[0].Value != "Album1" {
t.Fatalf("Expected group-title tag value to be Album1 but was '%s'", playlist.Tracks[i].Tags[0].Value)
}
}
}