-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
174 lines (149 loc) · 3.58 KB
/
decoder.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bufio"
"errors"
"io"
"os/exec"
"strings"
"sync"
)
var (
ffmpegBinaryName = "ffmpeg"
ffmpegOptions = []string{
"-loglevel", "warning", // only print warnings and errors
"-c:a", "pcm_s16le", // 16-bit signed PCM audio codec
"-ar", "48000", // 48 kHz sample rate
"-ac", "2", // stereo (we downmix to mono ourselves where necessary)
"-f", "s16le", // use raw PCM audio for output
}
)
// Decoder launches ffmpeg to transcode whatever media input into 16-bit signed
// PCM 48kHz audio, then feeds it out via a channel ready to inject into the
// TeamSpeak voice buffer.
type Decoder struct {
ffmpeg *exec.Cmd
ffmpegIn io.WriteCloser
ffmpegOut io.ReadCloser
ffmpegErr *bufio.Reader
mutex sync.Mutex
closed bool
errorC <-chan error
samplesC <-chan []int16
LogStderr func(string)
}
func newDecoder() (decoder *Decoder, err error) {
logDebug("Creating new decoder instance...")
ffmpegArgs := []string{
"-i", "-", // stdin for input
}
ffmpegArgs = append(ffmpegArgs, ffmpegOptions...)
ffmpegArgs = append(ffmpegArgs,
"pipe:", // output everything to stdout
)
ffmpeg := exec.Command(ffmpegBinaryName, ffmpegArgs...)
ffmpegIn, err := ffmpeg.StdinPipe()
if err != nil {
return
}
ffmpegOut, err := ffmpeg.StdoutPipe()
if err != nil {
return
}
ffmpegErr, err := ffmpeg.StderrPipe()
if err != nil {
return
}
ffmpegErrBuffered := bufio.NewReader(ffmpegErr)
err = ffmpeg.Start()
if err != nil {
logDebug("Starting ffmpeg failed: %s", err.Error())
return
}
samplesC := make(chan []int16, 0.250*48000)
errorC := make(chan error)
decoder = &Decoder{
ffmpeg: ffmpeg,
ffmpegIn: ffmpegIn,
ffmpegOut: ffmpegOut,
ffmpegErr: ffmpegErrBuffered,
samplesC: samplesC,
errorC: errorC,
}
// stderr reading
go func() {
logDebug("Decoder stderr reading started")
defer logDebug("Decoder stderr reading stopped")
defer close(errorC)
lineReader := bufio.NewReader(ffmpegErr)
for {
line, err := lineReader.ReadString('\n')
if err != nil {
return
}
// remove any leftover line breaks
line = strings.Trim(line, "\r\n")
if decoder.LogStderr != nil {
decoder.LogStderr(line)
}
}
}()
// stdout sample decoding loop
go func() {
logDebug("Decoder stdout sample decoding loop started")
defer logDebug("Decoder stdout sample decoing loop stopped")
defer decoder.Close()
defer close(samplesC)
// Just in case short reads do occur and we get less than the wanted
// amount of samples, buffer them here.
previousSampleArray := []int16{}
for {
sample, _, err := readSamples(ffmpegOut, 2-len(previousSampleArray))
if err == io.EOF {
return
}
if err != nil {
select {
case errorC <- err:
default:
}
return
}
sample = append(previousSampleArray, sample...)
if len(sample) < 2 {
continue
}
if len(sample) > 2 {
select {
case errorC <- errors.New("Got more than 2 short values for sample, logic error!"):
default:
}
return
}
samplesC <- sample
}
}()
return
}
func (decoder *Decoder) Write(p []byte) (n int, err error) {
decoder.mutex.Lock()
defer decoder.mutex.Unlock()
return decoder.ffmpegIn.Write(p)
}
func (decoder *Decoder) Close() (err error) {
decoder.mutex.Lock()
defer decoder.mutex.Unlock()
if decoder.closed {
err = errors.New("Decoder was already closed")
return
}
err = decoder.ffmpegIn.Close()
decoder.ffmpeg.Wait()
decoder.closed = true
return
}
func (decoder *Decoder) SamplesC() <-chan []int16 {
return decoder.samplesC
}
func (decoder *Decoder) ErrorC() <-chan error {
return decoder.errorC
}