diff options
| author | Nathan Perry <avaglir@gmail.com> | 2017-07-29 16:39:57 -0400 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2017-07-29 16:39:57 -0400 |
| commit | eb12be6f8315973bfb3e39c259c00d325889c32c (patch) | |
| tree | edf28ba06a7184ff630401ea5ef967524b5fc596 /downloader/downloader.go | |
| parent | b562764a74caa986b99b523dbf78beced6fc1e88 (diff) | |
refactor works
Diffstat (limited to 'downloader/downloader.go')
| -rw-r--r-- | downloader/downloader.go | 79 |
1 files changed, 32 insertions, 47 deletions
diff --git a/downloader/downloader.go b/downloader/downloader.go index cab561b..c9fc864 100644 --- a/downloader/downloader.go +++ b/downloader/downloader.go @@ -11,18 +11,17 @@ import ( "github.com/mammothbane/thulani-go/wav" ) -// Downloader handles a download for a particular song. -type Downloader struct { +// downloader handles a download for a particular song. +type downloader struct { Url string Start time.Duration Duration time.Duration End time.Duration - pause chan wav.State - once sync.Once - done chan struct{} - pb chan *wavBundle + once sync.Once + done chan struct{} + pb chan *wavBundle info videoInfo } @@ -30,7 +29,7 @@ type Downloader struct { const clipTime = 10 * time.Second const preloadCount = 5 -func NewDownload(url string, startTime, dur time.Duration) (*Downloader, error) { +func newDownload(url string, startTime, dur time.Duration) (*downloader, error) { vInfo, err := info(url) if err != nil { return nil, err @@ -40,17 +39,16 @@ func NewDownload(url string, startTime, dur time.Duration) (*Downloader, error) dur = vInfo.Duration - startTime } - dl := &Downloader{ + dl := &downloader{ Url: url, Start: startTime, Duration: dur, End: startTime + dur, - pause: make(chan wav.State), - done: make(chan struct{}, 1), - pb: make(chan *wavBundle, preloadCount), - info: *vInfo, + done: make(chan struct{}, 1), + pb: make(chan *wavBundle, preloadCount), + info: *vInfo, } go dl.schedule() @@ -58,21 +56,13 @@ func NewDownload(url string, startTime, dur time.Duration) (*Downloader, error) return dl, nil } -func (d *Downloader) Stop() { +func (d *downloader) Stop() { d.once.Do(func() { close(d.done) }) } -func (d *Downloader) Resume() { - d.pause <- wav.Resume -} - -func (d *Downloader) Pause() { - d.pause <- wav.Pause -} - -func (d *Downloader) SendOn(ch chan<- []byte) <-chan struct{} { +func (d *downloader) SendOn(ch chan<- []byte) <-chan struct{} { out := make(chan struct{}, 1) go func() { @@ -87,9 +77,6 @@ func (d *Downloader) SendOn(ch chan<- []byte) <-chan struct{} { case <-wavB.wav.Done: break - - case elem := <-d.pause: - wavB.wav.PlayState <- elem } } }() @@ -97,34 +84,32 @@ func (d *Downloader) SendOn(ch chan<- []byte) <-chan struct{} { return out } -func (d *Downloader) schedule() { - go func() { - defer close(d.pb) - for i := 0; ; i++ { - clipStart := time.Duration(i)*clipTime + d.Start - clipEnd := time.Duration(i+1)*clipTime + d.Start - - if clipStart >= d.End { - return - } +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 - dur := clipTime - if clipEnd > d.End { - dur = d.End - clipStart - } + if clipStart >= d.End { + return + } - wavb, err := d.download(clipStart, dur) - if err != nil { - log.Errorf("error setting up download: %q", err) - return - } + dur := clipTime + if clipEnd > d.End { + dur = d.End - clipStart + } - d.pb <- wavb + wavb, err := d.downloadSegment(clipStart, dur) + if err != nil { + log.Errorf("error setting up download: %q", err) + return } - }() + + d.pb <- wavb + } } -func (d *Downloader) download(startTime, duration time.Duration) (*wavBundle, error) { +func (d *downloader) downloadSegment(startTime, duration time.Duration) (*wavBundle, error) { startSecond := int(startTime.Seconds()) args := []string{ "-ss", strconv.Itoa(startSecond), |
