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

Feature: Support MediaSegment@SeqId. #124

Merged
merged 1 commit into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't better to use an assert here? I'm new to golang anyway

  assert.Equal(t, 522, p.Count(), "Expected segments quantity: 522.")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If yes I think there is lots of places where it can be asserted instead of conditional checked.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes Leandro, I use assert to write test cases in some of my projects, it seems good.
But the assert is not contained in Golang's official packages, I do not want to introduce a 3rd package for the time being, I hope keep this project to be simple.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I didn't know, thanks 👍

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, LGTM

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