-
Notifications
You must be signed in to change notification settings - Fork 0
/
playback.go
101 lines (92 loc) · 1.76 KB
/
playback.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/codegangsta/cli"
)
func playAction(context *cli.Context) {
termStream, err := getTermStream(context.Args().First())
if err != nil {
logger.Fatal(err)
}
defer termStream.Close()
fmt.Println("starting playback of recoding!\n")
p := newPlayer(termStream, os.Stdout)
if err := p.play(); err != nil {
logger.Fatal(err)
}
fmt.Println("recoding playback complete!")
}
func getTermStream(path string) (io.ReadCloser, error) {
if path == "" {
return nil, fmt.Errorf("path must be specified")
}
if _, err := url.ParseRequestURI(path); err == nil {
resp, err := http.Get(path)
if err != nil {
return nil, err
}
return resp.Body, nil
}
return os.Open(path)
}
type playback struct {
Pause time.Duration
Content []byte
}
// newPlayer returns a play to write the TTY stream to w which
// should also support a TTY termnal.
func newPlayer(termStream io.Reader, w *os.File) *player {
return &player{
s: termStream,
w: w,
}
}
type player struct {
s io.Reader
w *os.File
}
func (p *player) play() error {
var (
dec = json.NewDecoder(p.s)
playbacks = make(chan *playback, 64)
wg = &sync.WaitGroup{}
)
wg.Add(1)
go p.writer(playbacks, wg)
var prev *recoding
for {
var c *recoding
if err := dec.Decode(&c); err != nil {
if err == io.EOF {
break
}
return err
}
var d time.Duration
if prev != nil {
d = c.Timestamp.Sub(prev.Timestamp)
}
playbacks <- &playback{
Pause: d,
Content: c.Content,
}
prev = c
}
close(playbacks)
wg.Wait()
return nil
}
func (p *player) writer(c chan *playback, wg *sync.WaitGroup) {
defer wg.Done()
for pc := range c {
time.Sleep(pc.Pause)
p.w.Write(pc.Content)
}
}