-
Notifications
You must be signed in to change notification settings - Fork 0
/
representation.go
102 lines (93 loc) · 2.55 KB
/
representation.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
package dash
import (
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"strings"
)
type IndexRange struct {
Start int
End int
}
func (i *IndexRange) UnmarshalText(b []byte) error {
_, err := fmt.Sscanf(string(b), "%v-%v", &i.Start, &i.End)
if err != nil {
return err
}
return nil
}
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 {
IndexRange IndexRange `xml:"indexRange,attr"`
}
// this might not exist, or might be under AdaptationSet
SegmentTemplate *SegmentTemplate
// this might not exist
Width *int `xml:"width,attr"`
}
func (r Representation) Default_KID() ([]byte, error) {
for _, c := range r.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("default_KID")
}
func (r Representation) Ext() (string, bool) {
switch r.MimeType {
case "audio/mp4":
return ".m4a", true
case "video/mp4":
return ".m4v", true
}
return "", false
}
func (r Representation) Initialization() (string, bool) {
if v := r.SegmentTemplate; v != nil {
if v := v.Initialization; v != "" {
return strings.Replace(v, "$RepresentationID$", r.ID, 1), true
}
}
return "", false
}
func (r Representation) Media() ([]string, bool) {
t := r.SegmentTemplate
if t == nil {
return nil, false
}
var media []string
for _, segment := range t.SegmentTimeline.S {
for segment.R >= 0 {
number := fmt.Sprint(t.StartNumber)
medium := strings.Replace(t.Media, "$Number$", number, 1)
medium = strings.Replace(medium, "$RepresentationID$", r.ID, 1)
media = append(media, medium)
segment.R--
t.StartNumber++
}
}
return media, true
}
func (r Representation) PSSH() ([]byte, error) {
for _, c := range r.ContentProtection {
if c.SchemeIdUri == "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed" {
return base64.StdEncoding.DecodeString(c.PSSH)
}
}
return nil, errors.New("PSSH")
}