Add basic Playlist listener functionality
This commit is contained in:
parent
afbe654de0
commit
5727cd3c12
|
@ -9,7 +9,7 @@ import (
|
||||||
const DefaultPlaylistDuration = 20 * time.Second
|
const DefaultPlaylistDuration = 20 * time.Second
|
||||||
|
|
||||||
type PlaylistListener interface {
|
type PlaylistListener interface {
|
||||||
OnUpdate(p *Playlist)
|
OnUpdate(p Playlist)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Playlist interface {
|
type Playlist interface {
|
||||||
|
@ -18,16 +18,18 @@ type Playlist interface {
|
||||||
TargetDuration() time.Duration
|
TargetDuration() time.Duration
|
||||||
AddSegment(s *segment.Segment) error
|
AddSegment(s *segment.Segment) error
|
||||||
Render() string
|
Render() string
|
||||||
//AddListener(l PlaylistListener)
|
AddListener(l PlaylistListener)
|
||||||
}
|
}
|
||||||
|
|
||||||
type MediaPlaylist struct {
|
type MediaPlaylist struct {
|
||||||
Segments []*segment.Segment
|
Segments []*segment.Segment
|
||||||
|
Listeners []PlaylistListener
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMediaPlaylist() *MediaPlaylist {
|
func NewMediaPlaylist() *MediaPlaylist {
|
||||||
return &MediaPlaylist{
|
return &MediaPlaylist{
|
||||||
Segments: make([]*segment.Segment, 0, 10),
|
Segments: make([]*segment.Segment, 0, 10),
|
||||||
|
Listeners: make([]PlaylistListener, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,16 +44,16 @@ func (p *MediaPlaylist) TargetDuration() time.Duration {
|
||||||
func (p *MediaPlaylist) AddSegment(s *segment.Segment) error {
|
func (p *MediaPlaylist) AddSegment(s *segment.Segment) error {
|
||||||
p.Segments = append(p.Segments, s)
|
p.Segments = append(p.Segments, s)
|
||||||
|
|
||||||
if len(p.Segments) == 1 {
|
if len(p.Segments) > 1 {
|
||||||
return nil
|
for {
|
||||||
|
if p.durationOf(p.Segments[1:]) > p.TargetDuration() {
|
||||||
|
p.Segments = p.Segments[1:]
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
p.updateListeners()
|
||||||
if p.durationOf(p.Segments[1:]) > p.TargetDuration() {
|
|
||||||
p.Segments = p.Segments[1:]
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -77,6 +79,18 @@ func (p *MediaPlaylist) Render() string {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Listeners
|
||||||
|
|
||||||
|
func (p *MediaPlaylist) AddListener(l PlaylistListener) {
|
||||||
|
p.Listeners = append(p.Listeners, l)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *MediaPlaylist) updateListeners() {
|
||||||
|
for _, l := range p.Listeners {
|
||||||
|
l.OnUpdate(p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *MediaPlaylist) Run() error {
|
func (p *MediaPlaylist) Run() error {
|
||||||
for {
|
for {
|
||||||
// TODO block here and listen to the channel of incoming segments.
|
// TODO block here and listen to the channel of incoming segments.
|
||||||
|
|
|
@ -46,3 +46,23 @@ func TestMediaPlaylistRender(t *testing.T) {
|
||||||
require.Equal(t, "#EXTINF:2.70000", lines[5])
|
require.Equal(t, "#EXTINF:2.70000", lines[5])
|
||||||
require.Equal(t, "http://www.example.com/x.mp3", lines[6])
|
require.Equal(t, "http://www.example.com/x.mp3", lines[6])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type listener struct {
|
||||||
|
count int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *listener) OnUpdate(p playlist.Playlist) {
|
||||||
|
l.count++
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMediaPlaylistListener(t *testing.T) {
|
||||||
|
playlist := playlist.NewMediaPlaylist()
|
||||||
|
l := &listener{}
|
||||||
|
playlist.AddListener(l)
|
||||||
|
|
||||||
|
s := segment.NewSegment(3*time.Second, 0)
|
||||||
|
s.IncrementDuration(3 * time.Second)
|
||||||
|
playlist.AddSegment(s)
|
||||||
|
|
||||||
|
require.Equal(t, 1, l.count)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue