-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
89 lines (73 loc) · 1.82 KB
/
common.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
package m3u8
import (
"fmt"
"io"
"time"
)
// Start represents the attributes associated with an EXT-X-START tag.
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.5.2.
type Start struct {
// TimeOffset indicates a time offset from the beginning of the
// Playlist.
//
// A negative value indicates a negative time offset from the end of the
// last Media Segment in the Playlist.
//
// TimeOffset is REQUIRED.
TimeOffset time.Duration
// Precise indicates whether or not to start playback at the Media Segment
// containing the Time Offset.
//
// Precise is OPTIONAL.
Precise bool
}
func (s *Start) attrs() (attributes, error) {
attrs := attributes{
attrTimeOffset: s.TimeOffset.Seconds(),
}
if s.Precise {
attrs[attrPrecise] = true
}
return attrs, nil
}
// GenericPlaylist encompasses the tags described in rfc8216 section 4.3.5.
//
// See https://tools.ietf.org/html/rfc8216#section-4.3.5
type GenericPlaylist struct {
// IndependentSegments indicates that all media samples in a Media Segment
// can be decoded without information from other segments.
IndependentSegments bool
// Start indicates a preferred point at which to start playing a Playlist.
//
// Start is OPTIONAL.
Start *Start
Version int
}
func (p *GenericPlaylist) encode(w io.Writer) error {
if _, err := fmt.Fprintln(w, headerTag); err != nil {
return err
}
if _, err := fmt.Fprintf(w, versionTag+":%d\n", p.Version); err != nil {
return err
}
if p.IndependentSegments {
if _, err := fmt.Fprintln(w, independentSegmentsTag); err != nil {
return err
}
}
if p.Start != nil {
attrs, err := p.Start.attrs()
if err != nil {
return err
}
encodedAttrs, err := attrs.encode()
if err != nil {
return err
}
if _, err := fmt.Fprintln(w, startTag+":"+encodedAttrs); err != nil {
return err
}
}
return nil
}