File: mediaItem.go

package info (click to toggle)
golang-github-etherlabsio-go-m3u8 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: makefile: 3
file content (85 lines) | stat: -rw-r--r-- 2,877 bytes parent folder | download | duplicates (2)
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
package m3u8

import (
	"fmt"
	"strings"
)

// MediaItem represents a set of EXT-X-MEDIA attributes
type MediaItem struct {
	Type              string
	GroupID           string
	Name              string
	Language          *string
	AssocLanguage     *string
	AutoSelect        *bool
	Default           *bool
	Forced            *bool
	URI               *string
	InStreamID        *string
	Characteristics   *string
	Channels          *string
	StableRenditionId *string
}

// NewMediaItem parses a text line and returns a *MediaItem
func NewMediaItem(text string) (*MediaItem, error) {
	attributes := ParseAttributes(text)

	return &MediaItem{
		Type:              attributes[TypeTag],
		GroupID:           attributes[GroupIDTag],
		Name:              attributes[NameTag],
		Language:          pointerTo(attributes, LanguageTag),
		AssocLanguage:     pointerTo(attributes, AssocLanguageTag),
		AutoSelect:        parseYesNo(attributes, AutoSelectTag),
		Default:           parseYesNo(attributes, DefaultTag),
		Forced:            parseYesNo(attributes, ForcedTag),
		URI:               pointerTo(attributes, URITag),
		InStreamID:        pointerTo(attributes, InStreamIDTag),
		Characteristics:   pointerTo(attributes, CharacteristicsTag),
		Channels:          pointerTo(attributes, ChannelsTag),
		StableRenditionId: pointerTo(attributes, StableRenditionIDTag),
	}, nil
}

func (mi *MediaItem) String() string {
	slice := []string{
		fmt.Sprintf(formatString, TypeTag, mi.Type),
		fmt.Sprintf(quotedFormatString, GroupIDTag, mi.GroupID),
	}

	if mi.Language != nil {
		slice = append(slice, fmt.Sprintf(quotedFormatString, LanguageTag, *mi.Language))
	}
	if mi.AssocLanguage != nil {
		slice = append(slice, fmt.Sprintf(quotedFormatString, AssocLanguageTag, *mi.AssocLanguage))
	}
	slice = append(slice, fmt.Sprintf(quotedFormatString, NameTag, mi.Name))
	if mi.AutoSelect != nil {
		slice = append(slice, fmt.Sprintf(formatString, AutoSelectTag, formatYesNo(*mi.AutoSelect)))
	}
	if mi.Default != nil {
		slice = append(slice, fmt.Sprintf(formatString, DefaultTag, formatYesNo(*mi.Default)))
	}
	if mi.URI != nil {
		slice = append(slice, fmt.Sprintf(quotedFormatString, URITag, *mi.URI))
	}
	if mi.Forced != nil {
		slice = append(slice, fmt.Sprintf(formatString, ForcedTag, formatYesNo(*mi.Forced)))
	}
	if mi.InStreamID != nil {
		slice = append(slice, fmt.Sprintf(quotedFormatString, InStreamIDTag, *mi.InStreamID))
	}
	if mi.Characteristics != nil {
		slice = append(slice, fmt.Sprintf(quotedFormatString, CharacteristicsTag, *mi.Characteristics))
	}
	if mi.Channels != nil {
		slice = append(slice, fmt.Sprintf(quotedFormatString, ChannelsTag, *mi.Channels))
	}
	if mi.StableRenditionId != nil {
		slice = append(slice, fmt.Sprintf(quotedFormatString, StableRenditionIDTag, *mi.StableRenditionId))
	}

	return fmt.Sprintf("%s:%s", MediaItemTag, strings.Join(slice, ","))
}