Skip to content

Commit

Permalink
Support for EXT-X-INDEPENDENT-SEGMENTS tag
Browse files Browse the repository at this point in the history
  • Loading branch information
mlafeldt committed Apr 9, 2019
1 parent 94253f1 commit 1b8a1e7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
2 changes: 2 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ func decodeLineOfMasterPlaylist(p *MasterPlaylist, state *decodingState, line st
if strict && err != nil {
return err
}
case line == "#EXT-X-INDEPENDENT-SEGMENTS":
p.SetIndependentSegments(true)
case strings.HasPrefix(line, "#EXT-X-MEDIA:"):
var alt Alternative
state.listType = MASTER
Expand Down
15 changes: 15 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ func TestDecodeMasterPlaylistWithStreamInfFrameRate(t *testing.T) {
}
}

func TestDecodeMasterPlaylistWithIndependentSegments(t *testing.T) {
f, err := os.Open("sample-playlists/master-with-independent-segments.m3u8")
if err != nil {
t.Fatal(err)
}
p := NewMasterPlaylist()
err = p.DecodeFrom(bufio.NewReader(f), false)
if err != nil {
t.Fatal(err)
}
if !p.IndependentSegments() {
t.Error("Expected independent segments to be true")
}
}

/****************************
* Begin Test MediaPlaylist *
****************************/
Expand Down
5 changes: 5 additions & 0 deletions sample-playlists/master-with-independent-segments.m3u8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1828000,NAME="3 high",RESOLUTION=896x504
chunklist_b1828000_t64NCBoaWdo.m3u8
11 changes: 6 additions & 5 deletions structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ type MediaPlaylist struct {
http://example.com/audio-only.m3u8
*/
type MasterPlaylist struct {
Variants []*Variant
Args string // optional arguments placed after URI (URI?Args)
CypherVersion string // non-standard tag for Widevine (see also WV struct)
buf bytes.Buffer
ver uint8
Variants []*Variant
Args string // optional arguments placed after URI (URI?Args)
CypherVersion string // non-standard tag for Widevine (see also WV struct)
buf bytes.Buffer
ver uint8
independentSegments bool
}

// This structure represents variants for master playlist.
Expand Down
16 changes: 16 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func (p *MasterPlaylist) Encode() *bytes.Buffer {
p.buf.WriteString(strver(p.ver))
p.buf.WriteRune('\n')

if p.IndependentSegments() {
p.buf.WriteString("#EXT-X-INDEPENDENT-SEGMENTS\n")
}

var altsWritten map[string]bool = make(map[string]bool)

for _, pl := range p.Variants {
Expand Down Expand Up @@ -249,6 +253,18 @@ func (p *MasterPlaylist) SetVersion(ver uint8) {
p.ver = ver
}

// IndependentSegments returns true if all media samples in a segment can be
// decoded without information from other segments.
func (p *MasterPlaylist) IndependentSegments() bool {
return p.independentSegments
}

// SetIndependentSegments sets whether all media samples in a segment can be
// decoded without information from other segments.
func (p *MasterPlaylist) SetIndependentSegments(b bool) {
p.independentSegments = b
}

// For compatibility with Stringer interface
// For example fmt.Printf("%s", sampleMediaList) will encode
// playist and print its string representation.
Expand Down
14 changes: 14 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,20 @@ func TestMediaSetWinSize(t *testing.T) {
}
}

func TestIndependentSegments(t *testing.T) {
m := NewMasterPlaylist()
if m.IndependentSegments() != false {
t.Errorf("Expected independent segments to be false by default")
}
m.SetIndependentSegments(true)
if m.IndependentSegments() != true {
t.Errorf("Expected independent segments to be true")
}
if !strings.Contains(m.Encode().String(), "#EXT-X-INDEPENDENT-SEGMENTS") {
t.Error("Expected playlist to contain EXT-X-INDEPENDENT-SEGMENTS tag")
}
}

func TestMediaPlaylist_Slide(t *testing.T) {
m, e := NewMediaPlaylist(3, 4)
if e != nil {
Expand Down

0 comments on commit 1b8a1e7

Please sign in to comment.