-
Notifications
You must be signed in to change notification settings - Fork 2
/
matroska_test.go
140 lines (127 loc) · 3.42 KB
/
matroska_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
package matroska
import (
"fmt"
"github.com/coding-socks/ebml"
"io"
"net/http"
"os"
"path/filepath"
"testing"
)
func Test_init(t *testing.T) {
found := false
for _, docType := range ebml.DocTypes() {
if docType == "matroska" {
found = true
break
}
}
if !found {
t.Error("matroska doctype not found")
}
}
func downloadTestFile(filename, source string) error {
resp, err := http.Get(source)
if err != nil {
return err
}
defer resp.Body.Close()
f, err := os.Create(filepath.Join(".", filename))
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
return err
}
func TestDecode(t *testing.T) {
tests := []struct {
name string
filename string
source string
wantErr bool
wantClusterLen int // Result of `mkvinfo -o matroska/test8.mkv | grep '|+ Cluster' | wc -l`
}{
{
name: "Basic file",
filename: "test1.mkv",
source: "https://github.com/Matroska-Org/matroska-test-files/blob/master/test_files/test1.mkv?raw=true",
wantClusterLen: 11,
},
{
name: "Non default timecodescale & aspect ratio",
filename: "test2.mkv",
source: "https://github.com/Matroska-Org/matroska-test-files/blob/master/test_files/test2.mkv?raw=true",
wantClusterLen: 47,
},
{
name: "Header stripping & standard block",
filename: "test3.mkv",
source: "https://github.com/Matroska-Org/matroska-test-files/blob/master/test_files/test3.mkv?raw=true",
wantClusterLen: 47,
},
{
name: "Live stream recording",
filename: "test4.mkv",
source: "https://github.com/Matroska-Org/matroska-test-files/blob/master/test_files/test4.mkv?raw=true",
wantClusterLen: 36,
},
{
name: "Multiple audio/subtitles",
filename: "test5.mkv",
source: "https://github.com/Matroska-Org/matroska-test-files/blob/master/test_files/test5.mkv?raw=true",
wantClusterLen: 25,
},
{
name: "Different EBML head sizes & cue-less seeking",
filename: "test6.mkv",
source: "https://github.com/Matroska-Org/matroska-test-files/blob/master/test_files/test6.mkv?raw=true",
wantClusterLen: 11,
},
{
name: "Extra unknown/junk elements & damaged",
filename: "test7.mkv",
source: "https://github.com/Matroska-Org/matroska-test-files/blob/master/test_files/test7.mkv?raw=true",
wantErr: true,
wantClusterLen: 37,
},
{
name: "Audio gap",
filename: "test8.mkv",
source: "https://github.com/Matroska-Org/matroska-test-files/blob/master/test_files/test8.mkv?raw=true",
wantClusterLen: 47,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := os.Open(filepath.Join(".", tt.filename))
if err != nil {
if !os.IsNotExist(err) {
t.Fatal(err)
}
if err := downloadTestFile(tt.filename, tt.source); err != nil {
t.Fatal(err)
}
if f, err = os.Open(filepath.Join(".", tt.filename)); err != nil {
t.Fatal(err)
}
}
defer f.Close()
d := ebml.NewDecoder(f)
header, err := d.DecodeHeader()
if err != nil {
t.Fatal(err)
}
fmt.Printf("%+v\n", header)
var b Segment
if err = d.DecodeBody(&b); tt.wantErr != (err != nil && err != io.EOF) {
t.Errorf("DecodeBody() error = %v, wantErr %v", err, tt.wantErr)
}
fmt.Printf("%+v\n", b.Info)
if got := len(b.Cluster); got != tt.wantClusterLen {
t.Errorf("len(DecodeBody().Cluster) got = %v, want %v", got, tt.wantClusterLen)
}
fmt.Printf("%+v\n", len(b.Cluster))
})
}
}