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

Commit

Permalink
Add media playlist AppendSegment(*MediaSegment) method (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyfalzon committed Apr 26, 2016
1 parent 85b9413 commit e34965e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
20 changes: 15 additions & 5 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import (
"time"
)

var (
ErrPlaylistFull = errors.New("playlist is full")
)

// Set version of the playlist accordingly with section 7
func version(ver *uint8, newver uint8) {
if *ver < newver {
Expand Down Expand Up @@ -276,18 +280,24 @@ func (p *MediaPlaylist) Remove() (err error) {
// Append general chunk to the tail of chunk slice for a media playlist.
// This operation does reset playlist cache.
func (p *MediaPlaylist) Append(uri string, duration float64, title string) error {
if p.head == p.tail && p.count > 0 {
return errors.New("playlist is full")
}
seg := new(MediaSegment)
seg.URI = uri
seg.Duration = duration
seg.Title = title
return p.AppendSegment(seg)
}

// AppendSegment appends a MediaSegment to the tail of chunk slice for a media playlist.
// This operation does reset playlist cache.
func (p *MediaPlaylist) AppendSegment(seg *MediaSegment) error {
if p.head == p.tail && p.count > 0 {
return ErrPlaylistFull
}
p.Segments[p.tail] = seg
p.tail = (p.tail + 1) % p.capacity
p.count++
if p.TargetDuration < duration {
p.TargetDuration = math.Ceil(duration)
if p.TargetDuration < seg.Duration {
p.TargetDuration = math.Ceil(seg.Duration)
}
p.buf.Reset()
return nil
Expand Down
28 changes: 26 additions & 2 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,38 @@ func TestAddSegmentToMediaPlaylist(t *testing.T) {
if e != nil {
t.Fatalf("Create media playlist failed: %s", e)
}
e = p.Append("test01.ts", 5.0, "")
e = p.Append("test01.ts", 10.0, "title")
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
e = p.Append("test02.ts", 6.0, "")
if p.Segments[0].URI != "test01.ts" {
t.Errorf("Expected: test01.ts, got: %v", p.Segments[0].URI)
}
if p.Segments[0].Duration != 10 {
t.Errorf("Expected: 10, got: %v", p.Segments[0].Duration)
}
if p.Segments[0].Title != "title" {
t.Errorf("Expected: title, got: %v", p.Segments[0].Title)
}
}

func TestAppendSegmentToMediaPlaylist(t *testing.T) {
p, _ := NewMediaPlaylist(2, 2)
e := p.AppendSegment(&MediaSegment{Duration: 10})
if e != nil {
t.Errorf("Add 1st segment to a media playlist failed: %s", e)
}
if p.TargetDuration != 10 {
t.Errorf("Failed to increase TargetDuration, expected: 10, got: %v", p.TargetDuration)
}
e = p.AppendSegment(&MediaSegment{Duration: 10})
if e != nil {
t.Errorf("Add 2nd segment to a media playlist failed: %s", e)
}
e = p.AppendSegment(&MediaSegment{Duration: 10})
if e != ErrPlaylistFull {
t.Errorf("Add 3rd expected full error, got: %s", e)
}
}

// Create new media playlist
Expand Down

0 comments on commit e34965e

Please sign in to comment.