aboutsummaryrefslogtreecommitdiff
path: root/downloader/downloader.go
diff options
context:
space:
mode:
authorNathan Perry <avaglir@gmail.com>2017-07-29 17:05:26 -0400
committerNathan Perry <avaglir@gmail.com>2017-07-29 17:05:26 -0400
commit4cd4870a85ed3b3d69d8455faeb7ca466dc42b7b (patch)
treed035f16224bb0a9d756afbee20ad1dc2779dadb6 /downloader/downloader.go
parenteb12be6f8315973bfb3e39c259c00d325889c32c (diff)
first refactoring pass
Diffstat (limited to 'downloader/downloader.go')
-rw-r--r--downloader/downloader.go32
1 files changed, 17 insertions, 15 deletions
diff --git a/downloader/downloader.go b/downloader/downloader.go
index c9fc864..6abe7f0 100644
--- a/downloader/downloader.go
+++ b/downloader/downloader.go
@@ -15,9 +15,9 @@ import (
type downloader struct {
Url string
- Start time.Duration
- Duration time.Duration
- End time.Duration
+ StartTime time.Duration
+ Duration time.Duration
+ EndTime time.Duration
once sync.Once
done chan struct{}
@@ -42,9 +42,9 @@ func newDownload(url string, startTime, dur time.Duration) (*downloader, error)
dl := &downloader{
Url: url,
- Start: startTime,
- Duration: dur,
- End: startTime + dur,
+ StartTime: startTime,
+ Duration: dur,
+ EndTime: startTime + dur,
done: make(chan struct{}, 1),
pb: make(chan *wavBundle, preloadCount),
@@ -62,13 +62,15 @@ func (d *downloader) Stop() {
})
}
-func (d *downloader) SendOn(ch chan<- []byte) <-chan struct{} {
- out := make(chan struct{}, 1)
+func (d *downloader) Start() (<-chan []byte, <-chan struct{}) {
+ out := make(chan []byte, 1024)
+ done := make(chan struct{}, 1)
go func() {
+ defer close(done)
defer close(out)
for wavB := range d.pb {
- wavB.wav.Start(ch)
+ wavB.wav.Start(out)
select {
case <-d.done:
@@ -81,22 +83,22 @@ func (d *downloader) SendOn(ch chan<- []byte) <-chan struct{} {
}
}()
- return out
+ return out, done
}
func (d *downloader) schedule() {
defer close(d.pb)
for i := 0; ; i++ {
- clipStart := time.Duration(i)*clipTime + d.Start
- clipEnd := time.Duration(i+1)*clipTime + d.Start
+ clipStart := time.Duration(i)*clipTime + d.StartTime
+ clipEnd := time.Duration(i+1)*clipTime + d.StartTime
- if clipStart >= d.End {
+ if clipStart >= d.EndTime {
return
}
dur := clipTime
- if clipEnd > d.End {
- dur = d.End - clipStart
+ if clipEnd > d.EndTime {
+ dur = d.EndTime - clipStart
}
wavb, err := d.downloadSegment(clipStart, dur)