Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Commit

Permalink
Append Args if playlist already has query params
Browse files Browse the repository at this point in the history
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 "?".
  • Loading branch information
bradleyfalzon committed Oct 15, 2015
1 parent a65d5bf commit 76df961
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"fmt"
"math"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -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')
Expand Down
23 changes: 23 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package m3u8

import (
"fmt"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 76df961

Please sign in to comment.