-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash.go
187 lines (170 loc) · 4.75 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
package dash
import (
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
)
type Representation struct {
Bandwidth int `xml:"bandwidth,attr"`
ID string `xml:"id,attr"`
// this might not exist
BaseURL string
// this might be under AdaptationSet
Codecs string `xml:"codecs,attr"`
// this might be under AdaptationSet
ContentProtection []ContentProtection
// this might not exist
Height int `xml:"height,attr"`
// this might be under AdaptationSet
MimeType string `xml:"mimeType,attr"`
// this might not exist
SegmentBase *struct {
Initialization struct {
Range Range `xml:"range,attr"`
}
IndexRange Range `xml:"indexRange,attr"`
}
// this might not exist, or might be under AdaptationSet
SegmentTemplate *SegmentTemplate
// this might not exist
Width int `xml:"width,attr"`
}
type AdaptationSet struct {
// this 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 []AdaptationSet
ID string `xml:"id,attr"`
}
func (p Pointer) contentProtection() []ContentProtection {
if a := p.AdaptationSet; a.ContentProtection != nil {
return a.ContentProtection
}
return p.Representation.ContentProtection
}
type ContentProtection struct {
SchemeIdUri string `xml:"schemeIdUri,attr"`
// this might not exist
Default_KID string `xml:"default_KID,attr"`
// this might not exist
PSSH string `xml:"pssh"`
}
func (p Pointer) PSSH() ([]byte, error) {
for _, c := range p.contentProtection() {
if c.SchemeIdUri == "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed" {
if c.PSSH != "" {
return base64.StdEncoding.DecodeString(c.PSSH)
}
}
}
return nil, errors.New("Pointer.PSSH")
}
func (p Pointer) Default_KID() ([]byte, error) {
for _, c := range p.contentProtection() {
if c.SchemeIdUri == "urn:mpeg:dash:mp4protection:2011" {
c.Default_KID = strings.ReplaceAll(c.Default_KID, "-", "")
return hex.DecodeString(c.Default_KID)
}
}
return nil, errors.New("Pointer.Default_KID")
}
type Pointer struct {
AdaptationSet *AdaptationSet
Period *Period
Representation *Representation
}
func (p Pointer) Codecs() string {
if a := p.AdaptationSet; a.Codecs != "" {
return a.Codecs
}
return p.Representation.Codecs
}
func (p Pointer) Ext() (string, bool) {
switch p.MimeType() {
case "audio/mp4":
return ".m4a", true
case "video/mp4":
return ".m4v", true
}
return "", false
}
func (p Pointer) Initialization() (string, bool) {
if st := p.segmentTemplate(); st != nil {
if i := st.Initialization; i != "" {
i = strings.Replace(i, "$RepresentationID$", p.Representation.ID, 1)
return i, true
}
}
return "", false
}
// return a slice so we can measure progress
func (p Pointer) Media() []string {
replace := func(s string, i int) string {
s = strings.Replace(s, "$RepresentationID$", p.Representation.ID, 1)
return strings.Replace(s, "$Number$", strconv.Itoa(i), 1)
}
var media []string
if st := p.segmentTemplate(); st != nil {
for _, segment := range st.SegmentTimeline.S {
for segment.R >= 0 {
medium := replace(st.Media, st.StartNumber)
media = append(media, medium)
segment.R--
st.StartNumber++
}
}
}
return media
}
func (p Pointer) MimeType() string {
if a := p.AdaptationSet; a.MimeType != "" {
return a.MimeType
}
return p.Representation.MimeType
}
func (p Pointer) segmentTemplate() *SegmentTemplate {
if a := p.AdaptationSet; a.SegmentTemplate != nil {
return a.SegmentTemplate
}
return p.Representation.SegmentTemplate
}
type Range string
func (r Range) Scan() (int, int, error) {
var start, end int
_, err := fmt.Sscanf(string(r), "%v-%v", &start, &end)
if err != nil {
return 0, 0, err
}
return start, end, nil
}
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"`
}