Implement basic playlist rendering

This commit is contained in:
Rob Watson 2020-07-07 23:28:50 +02:00
parent 5727cd3c12
commit 9a50d96f0a
6 changed files with 115 additions and 22 deletions

3
go.mod
View File

@ -3,6 +3,9 @@ module segmento
go 1.14
require (
github.com/aws/aws-sdk-go v1.33.3 // indirect
github.com/stretchr/testify v1.6.1
github.com/tcolgate/mp3 v0.0.0-20170426193717-e79c5a46d300
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
golang.org/x/text v0.3.3 // indirect
)

25
go.sum
View File

@ -1,13 +1,38 @@
github.com/aws/aws-sdk-go v1.33.3 h1:wjhURjD/xuBBxdCan0F5yuW7qzkSlYY4/RdYGlyab9s=
github.com/aws/aws-sdk-go v1.33.3/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tcolgate/mp3 v0.0.0-20170426193717-e79c5a46d300 h1:XQdibLKagjdevRB6vAjVY4qbSr8rQ610YzTkWcxzxSI=
github.com/tcolgate/mp3 v0.0.0-20170426193717-e79c5a46d300/go.mod h1:FNa/dfN95vAYCNFrIKRrlRo+MBLbwmR9Asa5f2ljmBI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -2,37 +2,51 @@ package playlist
import (
"fmt"
"segmento/internal/segment"
"log"
"time"
segmentpkg "segmento/internal/segment"
)
const DefaultPlaylistDuration = 20 * time.Second
type segment struct {
segmentpkg.Segment
seqId int
}
type PlaylistListener interface {
OnUpdate(p Playlist)
}
type Playlist interface {
// These could be moved to an interface?
Duration() time.Duration
TargetDuration() time.Duration
AddSegment(s *segment.Segment) error
Render() string
ReadSegments(chan *segmentpkg.Segment) error
AddListener(l PlaylistListener)
}
type MediaPlaylist struct {
Segments []*segment.Segment
Segments []*segment
Listeners []PlaylistListener
seqId int
}
func NewMediaPlaylist() *MediaPlaylist {
return &MediaPlaylist{
Segments: make([]*segment.Segment, 0, 10),
Segments: make([]*segment, 0, 10),
Listeners: make([]PlaylistListener, 0),
}
}
func (p *MediaPlaylist) nextSeqId() int {
next := p.seqId
p.seqId++
return next
}
func (p *MediaPlaylist) Duration() time.Duration {
return p.durationOf(p.Segments)
}
@ -41,8 +55,13 @@ func (p *MediaPlaylist) TargetDuration() time.Duration {
return DefaultPlaylistDuration
}
func (p *MediaPlaylist) AddSegment(s *segment.Segment) error {
p.Segments = append(p.Segments, s)
// pass by pointer or value here?
// I think value is better, the struct is tiny and only contains a pointer
// to the data buffer
func (p *MediaPlaylist) AddSegment(s *segmentpkg.Segment) error {
nextId := p.nextSeqId()
ss := segment{*s, nextId}
p.Segments = append(p.Segments, &ss)
if len(p.Segments) > 1 {
for {
@ -58,7 +77,7 @@ func (p *MediaPlaylist) AddSegment(s *segment.Segment) error {
return nil
}
func (p *MediaPlaylist) durationOf(ss []*segment.Segment) time.Duration {
func (p *MediaPlaylist) durationOf(ss []*segment) time.Duration {
var t time.Duration
for _, s := range ss {
t += s.Duration()
@ -91,11 +110,11 @@ func (p *MediaPlaylist) updateListeners() {
}
}
func (p *MediaPlaylist) Run() error {
for {
// TODO block here and listen to the channel of incoming segments.
// As the reader is Read and segments are produced, update the Playlist
// struct and possibly notify consumers.
// What would actually be a useful API and/or Go best practices?
func (p *MediaPlaylist) ReadSegments(segments chan *segmentpkg.Segment) error {
for s := range segments {
log.Println("got segment with duration", s.Duration(), "and len", s.Len(), "bytes")
p.AddSegment(s)
}
log.Println("exiting ReadSegments")
return nil
}

21
internal/s3/s3.go Normal file
View File

@ -0,0 +1,21 @@
package s3
import "segmento/internal/playlist"
//import (
//"github.com/aws/aws-sdk-go/aws"
//"github.com/aws/aws-sdk-go/aws/session"
//"github.com/aws/aws-sdk-go/service/s3"
//)
type S3PlaylistUploader struct {
AWSAccessKeyID string
AWSSecretAccessKey string
AWSRegion string
BucketName string
PathPrefix string
}
func (u *S3PlaylistUploader) OnUpdate(p playlist.Playlist) {
}

13
internal/s3/s3_test.go Normal file
View File

@ -0,0 +1,13 @@
package s3_test
import (
"segmento/internal/playlist"
"segmento/internal/s3"
"testing"
"github.com/stretchr/testify/require"
)
func TestS3PlaylistUploaderImplements(t *testing.T) {
require.Implements(t, (*playlist.PlaylistListener)(nil), &s3.S3PlaylistUploader{})
}

28
main.go
View File

@ -2,14 +2,25 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"segmento/internal/playlist"
"segmento/internal/segment"
)
type listener struct {
}
func (l *listener) OnUpdate(p playlist.Playlist) {
fmt.Println("Playlist updated")
fmt.Println("Playlist:")
fmt.Println(p.Render())
fmt.Println("")
}
func main() {
// TODO accept some flags with:
// URL - source of stream
@ -38,19 +49,20 @@ func main() {
defer resp.Body.Close()
listener := listener{}
playlist := playlist.NewMediaPlaylist()
playlist.AddListener(&listener)
segmenter := segment.NewMP3HTTPSegmenter()
segments, err := segmenter.Segment(resp.Body)
if err != nil {
log.Fatal(err)
}
go func() {
for s := range segments {
log.Println("got segment with duration", s.Duration(), "and len", s.Len(), "bytes")
}
}()
time.Sleep(10 * time.Second)
// Block while reading the segments into the playlist:
if err = playlist.ReadSegments(segments); err != nil {
log.Fatal(err)
}
log.Println("exiting")
}