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

Commit

Permalink
Merge pull request #124 from leikao/master
Browse files Browse the repository at this point in the history
Feature: Support MediaSegment@SeqId, to close #107 #116
  • Loading branch information
leikao authored Feb 1, 2019
2 parents f845940 + cea445a commit 1e6f30f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
31 changes: 28 additions & 3 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ func TestDecodeMediaPlaylistByteRange(t *testing.T) {
p, _ := NewMediaPlaylist(3, 3)
_ = p.DecodeFrom(bufio.NewReader(f), true)
expected := []*MediaSegment{
{URI: "video.ts", Duration: 10, Limit: 75232},
{URI: "video.ts", Duration: 10, Limit: 82112, Offset: 752321},
{URI: "video.ts", Duration: 10, Limit: 69864},
{URI: "video.ts", Duration: 10, Limit: 75232, SeqId: 0},
{URI: "video.ts", Duration: 10, Limit: 82112, Offset: 752321, SeqId: 1},
{URI: "video.ts", Duration: 10, Limit: 69864, SeqId: 2},
}
for i, seg := range p.Segments {
if *seg != *expected[i] {
Expand Down Expand Up @@ -218,6 +218,10 @@ func TestDecodeMasterPlaylistWithStreamInfFrameRate(t *testing.T) {
}
}

/****************************
* Begin Test MediaPlaylist *
****************************/

func TestDecodeMediaPlaylist(t *testing.T) {
f, err := os.Open("sample-playlists/wowza-vod-chunklist.m3u8")
if err != nil {
Expand Down Expand Up @@ -251,6 +255,15 @@ func TestDecodeMediaPlaylist(t *testing.T) {
t.Errorf("Segment %v's title = %v (must = %q)", i, s.Title, titles[i])
}
}
if p.Count() != 522 {
t.Errorf("Excepted segments quantity: 522, got: %v", p.Count())
}
var seqId, idx uint
for seqId, idx = 1, 0; idx < p.Count(); seqId, idx = seqId+1, idx+1 {
if p.Segments[idx].SeqId != uint64(seqId) {
t.Errorf("Excepted SeqId for %vth segment: %v, got: %v", idx+1, seqId, p.Segments[idx].SeqId)
}
}
// TODO check other values…
//fmt.Println(p.Encode().String()), stream.Name}
}
Expand Down Expand Up @@ -498,6 +511,18 @@ func TestDecodeMediaPlaylistWithDiscontinuitySeq(t *testing.T) {
if pp.DiscontinuitySeq == 0 {
t.Error("Empty discontinuity sequenece tag")
}
if pp.Count() != 4 {
t.Errorf("Excepted segments quantity: 4, got: %v", pp.Count())
}
if pp.SeqNo != 0 {
t.Errorf("Excepted SeqNo: 0, got: %v", pp.SeqNo)
}
var seqId, idx uint
for seqId, idx = 0, 0; idx < pp.Count(); seqId, idx = seqId+1, idx+1 {
if pp.Segments[idx].SeqId != uint64(seqId) {
t.Errorf("Excepted SeqId for %vth segment: %v, got: %v", idx+1, seqId, pp.Segments[idx].SeqId)
}
}
}

/***************************
Expand Down
4 changes: 4 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ func (p *MediaPlaylist) AppendSegment(seg *MediaSegment) error {
if p.head == p.tail && p.count > 0 {
return ErrPlaylistFull
}
seg.SeqId = p.SeqNo
if p.count > 0 {
seg.SeqId = p.Segments[(p.capacity+p.tail-1)%p.capacity].SeqId + 1
}
p.Segments[p.tail] = seg
p.tail = (p.tail + 1) % p.capacity
p.count++
Expand Down
69 changes: 69 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func TestAddSegmentToMediaPlaylist(t *testing.T) {
if p.Segments[0].Title != "title" {
t.Errorf("Expected: title, got: %v", p.Segments[0].Title)
}
if p.Segments[0].SeqId != 0 {
t.Errorf("Excepted SeqId: 0, got: %v", p.Segments[0].SeqId)
}
}

func TestAppendSegmentToMediaPlaylist(t *testing.T) {
Expand All @@ -94,6 +97,12 @@ func TestAppendSegmentToMediaPlaylist(t *testing.T) {
if e != ErrPlaylistFull {
t.Errorf("Add 3rd expected full error, got: %s", e)
}
if p.Count() != 2 {
t.Errorf("Except segments in playlist: 2, got: %v", p.Count())
}
if p.SeqNo != 0 || p.Segments[0].SeqId != 0 || p.Segments[1].SeqId != 1 {
t.Errorf("Excepted SeqNo and SeqId: 0/0/1, got: %v/%v/%v", p.SeqNo, p.Segments[0].SeqId, p.Segments[1].SeqId)
}
}

// Create new media playlist
Expand Down Expand Up @@ -591,6 +600,66 @@ func TestMediaSetWinSize(t *testing.T) {
}
}

func TestMediaPlaylist_Slide(t *testing.T) {
m, e := NewMediaPlaylist(3, 4)
if e != nil {
t.Fatalf("Failed to create media playlist: %v", e)
}

_ = m.Append("t00.ts", 10, "")
_ = m.Append("t01.ts", 10, "")
_ = m.Append("t02.ts", 10, "")
_ = m.Append("t03.ts", 10, "")
if m.Count() != 4 {
t.Fatalf("Excepted segments in media playlist: 4, got: %v", m.Count())
}
if m.SeqNo != 0 {
t.Errorf("Excepted SeqNo of media playlist: 0, got: %v", m.SeqNo)
}
var seqId, idx uint
for idx, seqId = 0, 0; idx < 3; idx, seqId = idx+1, seqId+1 {
segIdx := (m.head + idx) % m.capacity
segUri := fmt.Sprintf("t%02d.ts", seqId)
seg := m.Segments[segIdx]
if seg.URI != segUri || seg.SeqId != uint64(seqId) {
t.Errorf("Excepted segment: %s with SeqId: %v, got: %v/%v", segUri, seqId, seg.URI, seg.SeqId)
}
}

m.Slide("t04.ts", 10, "")
if m.Count() != 4 {
t.Fatalf("Excepted segments in media playlist: 4, got: %v", m.Count())
}
if m.SeqNo != 1 {
t.Errorf("Excepted SeqNo of media playlist: 1, got: %v", m.SeqNo)
}
for idx, seqId = 0, 1; idx < 3; idx, seqId = idx+1, seqId+1 {
segIdx := (m.head + idx) % m.capacity
segUri := fmt.Sprintf("t%02d.ts", seqId)
seg := m.Segments[segIdx]
if seg.URI != segUri || seg.SeqId != uint64(seqId) {
t.Errorf("Excepted segment: %s with SeqId: %v, got: %v/%v", segUri, seqId, seg.URI, seg.SeqId)
}
}

m.Slide("t05.ts", 10, "")
m.Slide("t06.ts", 10, "")
if m.Count() != 4 {
t.Fatalf("Excepted segments in media playlist: 4, got: %v", m.Count())
}
if m.SeqNo != 3 {
t.Errorf("Excepted SeqNo of media playlist: 1, got: %v", m.SeqNo)
}
for idx, seqId = 0, 3; idx < 3; idx, seqId = idx+1, seqId+1 {
segIdx := (m.head + idx) % m.capacity
segUri := fmt.Sprintf("t%02d.ts", seqId)
seg := m.Segments[segIdx]
if seg.URI != segUri || seg.SeqId != uint64(seqId) {
t.Errorf("Excepted segment: %s with SeqId: %v, got: %v/%v", segUri, seqId, seg.URI, seg.SeqId)
}
}
}

// Create new master playlist without params
// Add media playlist
func TestNewMasterPlaylist(t *testing.T) {
Expand Down

0 comments on commit 1e6f30f

Please sign in to comment.