-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash.go
188 lines (175 loc) · 4.48 KB
/
dash.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
package dash
import (
"encoding/xml"
"strconv"
"strings"
)
func Unmarshal(b []byte) ([]Representation, error) {
var s struct {
Period []period
}
err := xml.Unmarshal(b, &s)
if err != nil {
return nil, err
}
var rs []Representation
for _, p := range s.Period {
for _, a := range p.AdaptationSet {
a.period = &p
for _, r := range a.Representation {
r.adaptation_set = &a
rs = append(rs, r)
}
}
}
return rs, nil
}
func (r Representation) Ext() (string, bool) {
switch r.mime_type() {
case "audio/mp4":
return ".m4a", true
case "video/mp4":
return ".m4v", true
}
return "", false
}
func (r Representation) Initialization() (string, bool) {
if st, ok := r.segment_template(); ok {
if i := st.Initialization; i != "" {
return strings.Replace(i, "$RepresentationID$", r.ID, 1), true
}
}
return "", false
}
func (r Representation) Media() []string {
st, ok := r.segment_template()
if !ok {
return nil
}
replace := func(s, old string) string {
s = strings.Replace(s, "$RepresentationID$", r.ID, 1)
return strings.Replace(s, old, strconv.Itoa(st.StartNumber), 1)
}
var media []string
for _, segment := range st.SegmentTimeline.S {
for segment.R >= 0 {
var medium string
if strings.Contains(st.Media, "$Time$") {
medium = replace(st.Media, "$Time$")
st.StartNumber += segment.D
} else {
medium = replace(st.Media, "$Number$")
st.StartNumber++
}
media = append(media, medium)
segment.R--
}
}
return media
}
func (r Representation) String() string {
var b []byte
if v := r.Width; v >= 1 {
b = append(b, "width = "...)
b = strconv.AppendInt(b, v, 10)
}
if v := r.Height; v >= 1 {
if b != nil {
b = append(b, '\n')
}
b = append(b, "height = "...)
b = strconv.AppendInt(b, v, 10)
}
if b != nil {
b = append(b, '\n')
}
b = append(b, "bandwidth = "...)
b = strconv.AppendInt(b, r.Bandwidth, 10)
if v, ok := r.codecs(); ok {
b = append(b, "\ncodecs = "...)
b = append(b, v...)
}
b = append(b, "\ntype = "...)
b = append(b, r.mime_type()...)
if v := r.adaptation_set.Role; v != nil {
b = append(b, "\nrole = "...)
b = append(b, v.Value...)
}
if v := r.adaptation_set.Lang; v != "" {
b = append(b, "\nlang = "...)
b = append(b, v...)
}
b = append(b, "\nid = "...)
b = append(b, r.ID...)
if v := r.adaptation_set.period.ID; v != "" {
b = append(b, "\nperiod = "...)
b = append(b, v...)
}
return string(b)
}
func (r Representation) codecs() (string, bool) {
if v := r.Codecs; v != "" {
return v, true
}
if v := r.adaptation_set.Codecs; v != "" {
return v, true
}
return "", false
}
func (r Representation) content_protection() []ContentProtection {
if v := r.ContentProtection; v != nil {
return v
}
return r.adaptation_set.ContentProtection
}
func (r Representation) mime_type() string {
if v := r.MimeType; v != "" {
return v
}
return r.adaptation_set.MimeType
}
func (r Representation) segment_template() (*SegmentTemplate, bool) {
if v := r.SegmentTemplate; v != nil {
return v, true
}
if v := r.adaptation_set.SegmentTemplate; v != nil {
return v, true
}
return nil, false
}
type SegmentTemplate struct {
Media string `xml:"media,attr"`
SegmentTimeline struct {
S []struct {
// duration
D int `xml:"d,attr"`
// repeat. this may not exist
R int `xml:"r,attr"`
}
}
StartNumber int `xml:"startNumber,attr"`
// this may not exist
Initialization string `xml:"initialization,attr"`
}
type adaptation_set struct {
period *period
// this might not exist, or might be under Representation
Codecs string `xml:"codecs,attr"`
// this might be under Representation
ContentProtection []ContentProtection
// this might not exist
Lang string `xml:"lang,attr"`
// this might be under Representation
MimeType string `xml:"mimeType,attr"`
Representation []Representation
// this might not exist
Role *struct {
Value string `xml:"value,attr"`
}
// this might not exist, or might be under Representation
SegmentTemplate *SegmentTemplate
}
type period struct {
AdaptationSet []adaptation_set
ID string `xml:"id,attr"`
}