-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster.go
170 lines (134 loc) · 3.45 KB
/
master.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
package m3u8
import (
"fmt"
"io"
)
type MasterPlaylist struct {
*GenericPlaylist
// RenditionMap is used to relate Media Playlists that contain alternative
// Renditions of the same content.
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.4.2.1.
RenditionMap []Rendition
// VariantStreams represents a set of Renditions that can be combined to
// play the presentation.
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.4.2.
VariantStreams []*VariantStream
// IFrameStreams identifies Media Playlist files containing the I-frames of
// a multimedia presentation.
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.4.3.
IFrameStreams []*Stream
// SessionData allows arbitrary session data to be carried in a Master
// Playlist.
//
// SessionData is OPTIONAL.
SessionData SessionData
// SessionKeys allows encryption keys from Media Playlists to be specified
// in a Master Playlist. This allows the client to preload these keys
// without having to read the Media Playlist(s) first.
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.4.5.
//
// SessionKeys is OPTIONAL.
SessionKeys []*Key
}
func parseMasterPlaylist(base *GenericPlaylist, lines []line) (*MasterPlaylist, error) {
var p MasterPlaylist
for i := 0; i < len(lines); i++ {
s, ok := lines[i].(*split)
if !ok {
return nil, isew(s, ErrUnexpectedURI)
}
switch s.tag {
case mediaTag:
rendition, err := parseRendition(s.meta)
if err != nil {
return nil, isew(s, err)
}
p.RenditionMap = append(p.RenditionMap, rendition)
case streamInfTag:
uri, ok := lines[i+1].(uri)
if !ok {
return nil, isew(s, ErrMissingURI)
}
i++
vs, err := parseVariantStream(s.meta)
if err != nil {
return nil, isew(s, err)
}
vs.URI = string(uri)
p.VariantStreams = append(p.VariantStreams, vs)
case iFrameStreamInfTag:
ifs, err := parseIFrameStream(s.meta)
if err != nil {
return nil, isew(s, err)
}
p.IFrameStreams = append(p.IFrameStreams, ifs)
case sessionDataTag:
sde, err := parseSessionDataEntry(s.meta)
if err != nil {
return nil, isew(s, err)
}
p.SessionData = append(p.SessionData, sde)
case sessionKeyTag:
key, err := parseKey(base.Version, s.meta)
if err != nil {
return nil, isew(s, err)
}
p.SessionKeys = append(p.SessionKeys, key)
}
}
p.GenericPlaylist = base
return &p, nil
}
func (*MasterPlaylist) Type() Type {
return Master
}
func (p *MasterPlaylist) encode(w io.Writer) error {
if err := p.GenericPlaylist.encode(w); err != nil {
return err
}
if len(p.RenditionMap) > 0 {
if err := renditions(p.RenditionMap).validate(); err != nil {
return err
}
for _, a := range p.RenditionMap {
attrs, err := a.attrs()
if err != nil {
return err
}
encodedAttrs, err := attrs.encode()
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, mediaTag+":"+encodedAttrs); err != nil {
return err
}
}
}
if len(p.VariantStreams) > 0 {
// TODO validate variant streams
for _, stream := range p.VariantStreams {
attrs, err := stream.attrs()
if err != nil {
return err
}
encodedAttrs, err := attrs.encode()
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, streamInfTag+":"+encodedAttrs); err != nil {
return err
}
if _, err := fmt.Fprintln(w, stream.URI); err != nil {
return err
}
if _, err := fmt.Fprintln(w); err != nil {
return err
}
}
}
return nil
}