Add playlist-generation

This commit is contained in:
2021-09-14 22:51:36 +02:00
parent 6e15d9835c
commit 584ffffe83
10 changed files with 192 additions and 23 deletions

33
m3u/m3u.go Normal file
View File

@@ -0,0 +1,33 @@
package m3u
import (
"bytes"
"fmt"
"io"
)
type Playlist struct {
items []*PlaylistItem
}
type PlaylistItem struct {
Name string
Path string
Time int
}
func (p Playlist) WriteTo(w io.Writer) (int64, error) {
var buf bytes.Buffer
buf.Write([]byte("#EXTM3U\n"))
for i, item := range p.items {
buf.WriteString(fmt.Sprintf("#EXTINF:%d,%s\n%s", item.Time, item.Name, item.Path))
if i+1 < len(p.items) {
buf.WriteString("\n")
}
}
return io.Copy(w, &buf)
}
func (p *Playlist) Add(item *PlaylistItem) {
p.items = append(p.items, item)
}

42
m3u/m3u_test.go Normal file
View File

@@ -0,0 +1,42 @@
package m3u_test
import (
"bytes"
"testing"
"github.uio.no/torjus/dogtamer/m3u"
)
func TestPlaylist(t *testing.T) {
t.Run("TestWriteSingle", func(t *testing.T) {
var p m3u.Playlist
p.Add(&m3u.PlaylistItem{Name: "TestItem", Path: "rtmp://localhost:5566/view/test", Time: -1})
var buf bytes.Buffer
_, err := p.WriteTo(&buf)
if err != nil {
t.Fatalf("Unable to write playlist: %s", err)
}
expected := "#EXTM3U\n#EXTINF:-1,TestItem\nrtmp://localhost:5566/view/test"
if buf.String() != expected {
t.Errorf("Output does not match expected. Got '%s' want '%s'", buf.String(), expected)
}
})
t.Run("TestWriteMultiple", func(t *testing.T) {
var p m3u.Playlist
p.Add(&m3u.PlaylistItem{Name: "TestItem", Path: "rtmp://localhost:5566/view/test", Time: -1})
p.Add(&m3u.PlaylistItem{Name: "TestTwo", Path: "rtmp://localhost:5566/view/testtwo", Time: 5})
var buf bytes.Buffer
_, err := p.WriteTo(&buf)
if err != nil {
t.Fatalf("Unable to write playlist: %s", err)
}
expected := "#EXTM3U\n#EXTINF:-1,TestItem\nrtmp://localhost:5566/view/test\n#EXTINF:5,TestTwo\nrtmp://localhost:5566/view/testtwo"
if buf.String() != expected {
t.Errorf("Output does not match expected. Got '%s' want '%s'", buf.String(), expected)
}
})
}