From 76df961ea334500856ac5c6964e5852fa87f1599 Mon Sep 17 00:00:00 2001 From: Bradley Falzon Date: Thu, 15 Oct 2015 14:10:54 +1030 Subject: [PATCH] Append Args if playlist already has query params Previously media playlists were assumed to have no query parameters when encoding a master manifest with Args. This commit first checks if the URI contains existing params (checks for "?") and if so, appends new Args with a leading "&", else it will add "?". --- writer.go | 7 ++++++- writer_test.go | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/writer.go b/writer.go index 927d490a..ac423127 100644 --- a/writer.go +++ b/writer.go @@ -29,6 +29,7 @@ import ( "fmt" "math" "strconv" + "strings" "time" ) @@ -208,7 +209,11 @@ func (p *MasterPlaylist) Encode() *bytes.Buffer { p.buf.WriteRune('\n') p.buf.WriteString(pl.URI) if p.Args != "" { - p.buf.WriteRune('?') + if strings.Contains(pl.URI, "?") { + p.buf.WriteRune('&') + } else { + p.buf.WriteRune('?') + } p.buf.WriteString(p.Args) } p.buf.WriteRune('\n') diff --git a/writer_test.go b/writer_test.go index 6b53e97c..7c99c066 100644 --- a/writer_test.go +++ b/writer_test.go @@ -23,6 +23,7 @@ package m3u8 import ( "fmt" + "strings" "testing" "time" ) @@ -427,6 +428,28 @@ func TestNewMasterPlaylistWithParams(t *testing.T) { m.Append("chunklist1.m3u8", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, Resolution: "576x480"}) } +// Create new master playlist +// Add media playlist with existing query params in URI +// Append more query params and ensure it encodes correctly +func TestEncodeMasterPlaylistWithExistingQuery(t *testing.T) { + m := NewMasterPlaylist() + p, e := NewMediaPlaylist(3, 5) + if e != nil { + t.Fatalf("Create media playlist failed: %s", e) + } + for i := 0; i < 5; i++ { + e = p.Append(fmt.Sprintf("test%d.ts", i), 5.0, "") + if e != nil { + t.Errorf("Add segment #%d to a media playlist failed: %s", i, e) + } + } + m.Append("chunklist1.m3u8?k1=v1&k2=v2", p, VariantParams{ProgramId: 123, Bandwidth: 1500000, Resolution: "576x480"}) + m.Args = "k3=v3" + if !strings.Contains(m.String(), "chunklist1.m3u8?k1=v1&k2=v2&k3=v3\n") { + t.Errorf("Encode master with existing args failed") + } +} + // Create new master playlist // Add media playlist // Encode structures to HLS