Skip to content

Commit

Permalink
normalize playlist primitives (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Dec 24, 2024
1 parent b9f0e27 commit d2288ae
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 68 deletions.
17 changes: 8 additions & 9 deletions pkg/playlist/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (m Media) isPlaylist() {}
func (m *Media) Unmarshal(buf []byte) error {
s := string(buf)

s, err := primitives.HeaderUnmarshal(s)
s, err := primitives.SkipHeader(s)
if err != nil {
return err
}
Expand Down Expand Up @@ -280,29 +280,28 @@ func (m *Media) Unmarshal(buf []byte) error {
return fmt.Errorf("invalid EXTINF: %s", line)
}

var du time.Duration
du, err = primitives.DurationUnmarshal(parts[0])
var d primitives.Duration
err = d.Unmarshal(parts[0])
if err != nil {
return err
}

curSegment.Duration = du
curSegment.Duration = time.Duration(d)
curSegment.Title = strings.TrimSpace(parts[1])

curSegment.Key = curKey

case strings.HasPrefix(line, "#EXT-X-BYTERANGE:"):
line = line[len("#EXT-X-BYTERANGE:"):]

var tmp1 uint64
var tmp2 *uint64
tmp1, tmp2, err = primitives.ByteRangeUnmarshal(line)
var br primitives.ByteRange
err = br.Unmarshal(line)
if err != nil {
return err
}

curSegment.ByteRangeLength = &tmp1
curSegment.ByteRangeStart = tmp2
curSegment.ByteRangeLength = &br.Length
curSegment.ByteRangeStart = br.Start

case strings.HasPrefix(line, "#EXT-X-PART:"):
line = line[len("#EXT-X-PART:"):]
Expand Down
3 changes: 2 additions & 1 deletion pkg/playlist/media_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type MediaKey struct {
}

func (t *MediaKey) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/playlist/media_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type MediaMap struct {
}

func (t *MediaMap) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}
Expand All @@ -29,13 +30,14 @@ func (t *MediaMap) unmarshal(v string) error {
t.URI = val

case "BYTERANGE":
length, start, err := primitives.ByteRangeUnmarshal(val)
var br primitives.ByteRange
err := br.Unmarshal(val)
if err != nil {
return err
}

t.ByteRangeLength = &length
t.ByteRangeStart = start
t.ByteRangeLength = &br.Length
t.ByteRangeStart = br.Start
}
}

Expand All @@ -50,7 +52,10 @@ func (t MediaMap) marshal() string {
ret := "#EXT-X-MAP:URI=\"" + t.URI + "\""

if t.ByteRangeLength != nil {
ret += ",BYTERANGE=" + primitives.ByteRangeMarshal(*t.ByteRangeLength, t.ByteRangeStart) + ""
ret += ",BYTERANGE=" + primitives.ByteRange{
Length: *t.ByteRangeLength,
Start: t.ByteRangeStart,
}.Marshal() + ""
}

ret += "\n"
Expand Down
20 changes: 13 additions & 7 deletions pkg/playlist/media_part.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ type MediaPart struct {
}

func (p *MediaPart) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}

for key, val := range attrs {
switch key {
case "DURATION":
tmp, err := primitives.DurationUnmarshal(val)
var d primitives.Duration
err := d.Unmarshal(val)
if err != nil {
return err
}
p.Duration = tmp
p.Duration = time.Duration(d)

case "URI":
p.URI = val
Expand All @@ -51,12 +53,13 @@ func (p *MediaPart) unmarshal(v string) error {
p.Independent = (val == "YES")

case "BYTERANGE":
length, start, err := primitives.ByteRangeUnmarshal(val)
var br primitives.ByteRange
err := br.Unmarshal(val)
if err != nil {
return err
}
p.ByteRangeLength = &length
p.ByteRangeStart = start
p.ByteRangeLength = &br.Length
p.ByteRangeStart = br.Start

case "GAP":
p.Gap = true
Expand All @@ -83,7 +86,10 @@ func (p MediaPart) marshal() string {
}

if p.ByteRangeLength != nil {
ret += ",BYTERANGE=" + primitives.ByteRangeMarshal(*p.ByteRangeLength, p.ByteRangeStart) + ""
ret += ",BYTERANGE=" + primitives.ByteRange{
Length: *p.ByteRangeLength,
Start: p.ByteRangeStart,
}.Marshal() + ""
}

if p.Gap {
Expand Down
8 changes: 5 additions & 3 deletions pkg/playlist/media_part_inf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ type MediaPartInf struct {
}

func (t *MediaPartInf) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}

for key, val := range attrs {
if key == "PART-TARGET" {
tmp, err := primitives.DurationUnmarshal(val)
var d primitives.Duration
err := d.Unmarshal(val)
if err != nil {
return err
}
t.PartTarget = tmp
t.PartTarget = time.Duration(d)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/playlist/media_preload_hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type MediaPreloadHint struct {
}

func (t *MediaPreloadHint) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/playlist/media_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ func (s MediaSegment) marshal() string {
ret += "#EXTINF:" + strconv.FormatFloat(s.Duration.Seconds(), 'f', 5, 64) + "," + s.Title + "\n"

if s.ByteRangeLength != nil {
ret += "#EXT-X-BYTERANGE:" + primitives.ByteRangeMarshal(*s.ByteRangeLength, s.ByteRangeStart) + "\n"
ret += "#EXT-X-BYTERANGE:" + primitives.ByteRange{
Length: *s.ByteRangeLength,
Start: s.ByteRangeStart,
}.Marshal() + "\n"
}

ret += s.URI + "\n"
Expand Down
11 changes: 8 additions & 3 deletions pkg/playlist/media_server_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type MediaServerControl struct {
}

func (t *MediaServerControl) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}
Expand All @@ -41,17 +42,21 @@ func (t *MediaServerControl) unmarshal(v string) error {
t.CanBlockReload = (val == "YES")

case "PART-HOLD-BACK":
tmp, err := primitives.DurationUnmarshal(val)
var d primitives.Duration
err := d.Unmarshal(val)
if err != nil {
return err
}
tmp := time.Duration(d)
t.PartHoldBack = &tmp

case "CAN-SKIP-UNTIL":
tmp, err := primitives.DurationUnmarshal(val)
var d primitives.Duration
err := d.Unmarshal(val)
if err != nil {
return err
}
tmp := time.Duration(d)
t.CanSkipUntil = &tmp
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/playlist/media_skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type MediaSkip struct {
}

func (t *MediaSkip) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/playlist/multivariant.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (m Multivariant) isPlaylist() {}
func (m *Multivariant) Unmarshal(buf []byte) error {
s := string(buf)

s, err := primitives.HeaderUnmarshal(s)
s, err := primitives.SkipHeader(s)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/playlist/multivariant_rendition.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ type MultivariantRendition struct {
}

func (t *MultivariantRendition) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/playlist/multivariant_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ type MultivariantStart struct {
}

func (t *MultivariantStart) unmarshal(v string) error {
attrs, err := primitives.AttributesUnmarshal(v)
var attrs primitives.Attributes
err := attrs.Unmarshal(v)
if err != nil {
return err
}

for key, val := range attrs {
if key == "TIME-OFFSET" {
tmp, err := primitives.DurationUnmarshal(val)
var d primitives.Duration
err := d.Unmarshal(val)
if err != nil {
return err
}
t.TimeOffset = tmp
t.TimeOffset = time.Duration(d)
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/playlist/multivariant_variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ type MultivariantVariant struct {
func (v *MultivariantVariant) unmarshal(va string) error {
lines := strings.Split(va, "\n")

attrs, err := primitives.AttributesUnmarshal(lines[0])
var attrs primitives.Attributes
err := attrs.Unmarshal(lines[0])
if err != nil {
return err
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/playlist/primitives/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"strings"
)

// AttributesUnmarshal decodes attributes.
func AttributesUnmarshal(v string) (map[string]string, error) {
ret := make(map[string]string)
// Attributes are playlist attributes.
type Attributes map[string]string

// Unmarshal decodes attributes.
func (a *Attributes) Unmarshal(v string) error {
*a = make(Attributes)

for {
if len(v) == 0 {
Expand All @@ -17,7 +20,7 @@ func AttributesUnmarshal(v string) (map[string]string, error) {
// read key
i := strings.IndexByte(v, '=')
if i < 0 {
return nil, fmt.Errorf("key not found")
return fmt.Errorf("key not found")
}
var key string
key, v = v[:i], v[i+1:]
Expand All @@ -29,29 +32,29 @@ func AttributesUnmarshal(v string) (map[string]string, error) {
v = v[1:]
i = strings.IndexByte(v, '"')
if i < 0 {
return nil, fmt.Errorf("value end delimiter not found")
return fmt.Errorf("value end delimiter not found")
}
val, v = v[:i], v[i+1:]
ret[key] = val
(*a)[key] = val

if len(v) != 0 {
if v[0] != ',' {
return nil, fmt.Errorf("delimiter not found")
return fmt.Errorf("delimiter not found")
}
v = v[1:]
}
} else {
i = strings.IndexByte(v, ',')
if i >= 0 {
val, v = v[:i], v[i+1:]
ret[key] = val
(*a)[key] = val
} else {
val = v
ret[key] = val
(*a)[key] = val
break
}
}
}

return ret, nil
return nil
}
Loading

0 comments on commit d2288ae

Please sign in to comment.