Skip to content

Commit

Permalink
Merge pull request #13 from jwplayer/ssai-500-add-func-setmediasegments
Browse files Browse the repository at this point in the history
Add new SetMediaSegments for Playlist Segments overwrite
  • Loading branch information
azombor authored Aug 8, 2024
2 parents b6d4c8c + 513e3b3 commit 054b353
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,17 @@ func (p *MediaPlaylist) InsertSegments(segments []*MediaSegment, seqID uint64) e
return nil
}

// SetMediaSegments takes in []*MediaSegment and sets the playlist segments to it
// This operation does reset playlist cache
func (p *MediaPlaylist) SetMediaSegments(segments []*MediaSegment) {
p.Segments = segments

p.count = uint(len(segments))
p.tail = p.count
p.capacity = p.count
p.buf.Reset()
}

// last returns the previously written segment's index
func (p *MediaPlaylist) last() uint {
if p.tail == 0 {
Expand Down
28 changes: 28 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,34 @@ func TestInsertSegments(t *testing.T) {
})
}

func TestSetMediaSegments(t *testing.T) {
p, e := NewMediaPlaylist(3, 4)
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
p.Close()
if e = p.Append("test01.ts", 5.0, ""); e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
if e = p.Append("test02.ts", 5.0, ""); e != nil {
t.Errorf("Add 2nd segment to a media playlist failed: %s", e)
}

newSegments := []*MediaSegment{
{SeqId: 1, URI: "test03.ts"},
{SeqId: 2, URI: "test04.ts"},
}

p.SetMediaSegments(newSegments)

require.Equal(t, 2, len(p.Segments))
require.Equal(t, uint(2), p.capacity)
require.Equal(t, uint(2), p.tail)
require.Equal(t, uint(2), p.count)
require.Equal(t, "test03.ts", p.Segments[0].URI)
require.Equal(t, "test04.ts", p.Segments[1].URI)
}

// Create new media playlist
// Add three segments to media playlist
// Set gap tag for the 2nd segment.
Expand Down

0 comments on commit 054b353

Please sign in to comment.