From b6f2ecc8d3d81c8c4e68508c3554ef477fd1f339 Mon Sep 17 00:00:00 2001 From: Matthew Neil Date: Tue, 16 Jul 2019 11:27:18 -0400 Subject: [PATCH] update CustomDecoder.Segment to CustomDecoder.SegmentTag --- .../template/custom-playlist-tag-template.go | 2 +- .../template/custom-segment-tag-template.go | 2 +- reader.go | 2 +- structure.go | 14 ++++++++++++- structure_test.go | 6 +++++- writer.go | 6 +++--- writer_test.go | 21 +++++++++++++++++++ 7 files changed, 45 insertions(+), 8 deletions(-) diff --git a/example/template/custom-playlist-tag-template.go b/example/template/custom-playlist-tag-template.go index 1690117a..c077d222 100644 --- a/example/template/custom-playlist-tag-template.go +++ b/example/template/custom-playlist-tag-template.go @@ -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 } diff --git a/example/template/custom-segment-tag-template.go b/example/template/custom-segment-tag-template.go index f1855fb9..bfa52c76 100644 --- a/example/template/custom-segment-tag-template.go +++ b/example/template/custom-segment-tag-template.go @@ -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 } diff --git a/reader.go b/reader.go index c82b8079..86134762 100644 --- a/reader.go +++ b/reader.go @@ -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 { diff --git a/structure.go b/structure.go index 58e0c783..ea0d5c85 100644 --- a/structure.go +++ b/structure.go @@ -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 } diff --git a/structure_test.go b/structure_test.go index 757a62e1..4ab7d36a 100644 --- a/structure_test.go +++ b/structure_test.go @@ -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) @@ -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 } diff --git a/writer.go b/writer.go index 8e9577e1..ae736067 100644 --- a/writer.go +++ b/writer.go @@ -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') } @@ -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') } @@ -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') } diff --git a/writer_test.go b/writer_test.go index 5c4b61dd..bfa2a798 100644 --- a/writer_test.go +++ b/writer_test.go @@ -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) @@ -407,6 +413,15 @@ 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 { @@ -414,6 +429,12 @@ func TestEncodeMediaPlaylistWithCustomTags(t *testing.T) { 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