Skip to content

Commit

Permalink
update CustomDecoder.Segment to CustomDecoder.SegmentTag
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Neil committed Jul 16, 2019
1 parent e08fbda commit b6f2ecc
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example/template/custom-playlist-tag-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (tag *CustomPlaylistTag) Decode(line string) (m3u8.CustomTag, error) {
}

// This is a playlist tag example
func (tag *CustomPlaylistTag) Segment() bool {
func (tag *CustomPlaylistTag) SegmentTag() bool {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion example/template/custom-segment-tag-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (tag *CustomSegmentTag) Decode(line string) (m3u8.CustomTag, error) {
}

// This is a playlist tag example
func (tag *CustomSegmentTag) Segment() bool {
func (tag *CustomSegmentTag) SegmentTag() bool {
return true
}

Expand Down
2 changes: 1 addition & 1 deletion reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func decodeLineOfMediaPlaylist(p *MediaPlaylist, wv *WV, state *decodingState, l
return err
}

if v.Segment() {
if v.SegmentTag() {
state.tagCustom = true
state.custom[v.TagName()] = t
} else {
Expand Down
14 changes: 13 additions & 1 deletion structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,27 @@ type Playlist interface {

// Interface for decoding custom and unsupported tags
type CustomDecoder interface {
// TagName should return the full indentifier including the leading '#' as well as the
// trailing ':' if the tag also contains a value or attribute list
TagName() string
// Decode parses a line from the playlist and returns the CustomTag representation
Decode(line string) (CustomTag, error)
Segment() bool
// SegmentTag should return true if this CustomDecoder should apply per segment.
// Should returns false if it a MediaPlaylist header tag.
// This value is ignored for MasterPlaylists.
SegmentTag() bool
}

// Interface for encoding custom and unsupported tags
type CustomTag interface {
// TagName should return the full indentifier including the leading '#' as well as the
// trailing ':' if the tag also contains a value or attribute list
TagName() string
// Encode should return the complete tag string as a *bytes.Buffer. This will
// be used by Playlist.Decode to write the tag to the m3u8.
// Return nil to not write anything to the m3u8.
Encode() *bytes.Buffer
// String should return the encoded tag as a string.
String() string
}

Expand Down
6 changes: 5 additions & 1 deletion structure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func (t *MockCustomTag) Decode(line string) (CustomTag, error) {
}

func (t *MockCustomTag) Encode() *bytes.Buffer {
if t.encodedString == "" {
return nil
}

buf := new(bytes.Buffer)

buf.WriteString(t.encodedString)
Expand All @@ -53,6 +57,6 @@ func (t *MockCustomTag) String() string {
return t.encodedString
}

func (t *MockCustomTag) Segment() bool {
func (t *MockCustomTag) SegmentTag() bool {
return t.segment
}
6 changes: 3 additions & 3 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (p *MasterPlaylist) Encode() *bytes.Buffer {
// Write any custom master tags
if p.Custom != nil {
for _, v := range p.Custom {
if customBuf := v.Encode(); customBuf.Len() > 0 {
if customBuf := v.Encode(); customBuf != nil {
p.buf.WriteString(customBuf.String())
p.buf.WriteRune('\n')
}
Expand Down Expand Up @@ -407,7 +407,7 @@ func (p *MediaPlaylist) Encode() *bytes.Buffer {
// Write any custom master tags
if p.Custom != nil {
for _, v := range p.Custom {
if customBuf := v.Encode(); customBuf.Len() > 0 {
if customBuf := v.Encode(); customBuf != nil {
p.buf.WriteString(customBuf.String())
p.buf.WriteRune('\n')
}
Expand Down Expand Up @@ -670,7 +670,7 @@ func (p *MediaPlaylist) Encode() *bytes.Buffer {
// Add Custom Segment Tags here
if seg.Custom != nil {
for _, v := range seg.Custom {
if customBuf := v.Encode(); customBuf.Len() > 0 {
if customBuf := v.Encode(); customBuf != nil {
p.buf.WriteString(customBuf.String())
p.buf.WriteRune('\n')
}
Expand Down
21 changes: 21 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,12 @@ func TestEncodeMediaPlaylistWithCustomTags(t *testing.T) {
}
p.SetCustomTag(customPTag)

customEmptyPTag := &MockCustomTag{
name: "#CustomEmptyPTag",
encodedString: "",
}
p.SetCustomTag(customEmptyPTag)

e = p.Append("test01.ts", 5.0, "")
if e != nil {
t.Fatalf("Add 1st segment to a media playlist failed: %s", e)
Expand All @@ -407,13 +413,28 @@ func TestEncodeMediaPlaylistWithCustomTags(t *testing.T) {
t.Fatalf("Set CustomTag to segment failed: %s", e)
}

customEmptySTag := &MockCustomTag{
name: "#CustomEmptySTag",
encodedString: "",
}
e = p.SetCustomSegmentTag(customEmptySTag)
if e != nil {
t.Fatalf("Set CustomTag to segment failed: %s", e)
}

encoded := p.String()
expectedStrings := []string{"#CustomPTag", "#CustomSTag"}
for _, expected := range expectedStrings {
if !strings.Contains(encoded, expected) {
t.Fatalf("Media playlist does not contain custom tag: %s\nMedia Playlist:\n%v", expected, encoded)
}
}
unexpectedStrings := []string{"#CustomEmptyPTag", "#CustomEmptySTag"}
for _, unexpected := range unexpectedStrings {
if strings.Contains(encoded, unexpected) {
t.Fatalf("Media playlist contains unexpected custom tag: %s\nMedia Playlist:\n%v", unexpected, encoded)
}
}
}

// Create new media playlist
Expand Down

0 comments on commit b6f2ecc

Please sign in to comment.