-
Notifications
You must be signed in to change notification settings - Fork 31
/
api.go
80 lines (68 loc) · 1.72 KB
/
api.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
package stream
import (
"bytes"
"context"
"encoding/binary"
"errors"
"github.com/digital-dream-labs/vector-cloud/internal/util"
"github.com/digital-dream-labs/api-clients/chipper"
)
func NewStreamer(ctx context.Context, receiver Receiver, streamSize int, opts ...Option) *Streamer {
strm := &Streamer{
byteChan: make(chan []byte),
audioStream: make(chan []byte, 10),
receiver: receiver}
// set default connector before applying options
strm.opts.connectFn = strm.newChipperConn
strm.opts.streamOpts = new(chipper.StreamOpts)
for _, o := range opts {
o(&strm.opts)
}
var cancel context.CancelFunc
if timeout := strm.opts.streamOpts.Timeout; timeout != 0 {
strm.ctx, cancel = context.WithTimeout(ctx, timeout)
} else {
strm.ctx, cancel = context.WithCancel(ctx)
}
strm.cancel = func() {
strm.closed = true
cancel()
}
go strm.init(streamSize)
return strm
}
func (strm *Streamer) AddSamples(samples []int16) {
if strm.opts.checkOpts != nil {
// no external audio input during connection check
return
}
var buf bytes.Buffer
binary.Write(&buf, binary.LittleEndian, samples)
strm.addBytes(buf.Bytes())
}
func (strm *Streamer) AddBytes(bytes []byte) {
if strm.opts.checkOpts != nil {
// no external audio input during connection check
return
}
strm.addBytes(bytes)
}
func (strm *Streamer) Close() error {
strm.cancel()
var err util.Errors
if strm.conn != nil {
err.Append(strm.conn.Close())
}
return err.Error()
}
func (strm *Streamer) CloseSend() error {
// ignore if conn check?
if strm.conn != nil {
return strm.conn.CloseSend()
}
return errors.New("cannot CloseSend on nil stream")
}
// SetVerbose enables or disables verbose logging
func SetVerbose(value bool) {
verbose = value
}