/
track.go
206 lines (188 loc) · 5.86 KB
/
track.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gomble
import (
"regexp"
"time"
"errors"
"io"
"fmt"
"os"
"github.com/CodingVoid/gomble/logger"
"github.com/CodingVoid/gomble/gomble/audioformats"
"github.com/CodingVoid/gomble/gomble/tracksources"
"github.com/CodingVoid/gomble/gomble/tracksources/youtube"
)
type Track struct {
trackSrc tracksources.TrackSource
// should be a multiple of audioformats.OPUS_FRAME_DURATION
buffer_ms int
// Never ever change that outside of this source file
Done bool
}
var stop chan bool = make(chan bool)
var pause chan bool = make(chan bool)
var currTrack *Track
const (
TRACK_TYPE_YOUTUBE = iota
TRACK_TYPE_OGGFILE
)
var ytregex *regexp.Regexp = regexp.MustCompile(`https://www.youtube.com/watch\?v=([a-zA-Z0-9\-\_]+)`) // need to use ` character otherwise \character are recognized as escape characters
func LoadTrack(url string) (*Track, error) {
ytmatches := ytregex.FindStringSubmatch(url)
if len(ytmatches) > 0 {
surl := ytmatches[1]
var err error
var src tracksources.TrackSource
if _, err := os.Stat("/bin/youtube-dl"); err == nil {
// use youtube-dl if it exists
src, err = youtube.NewYoutubedlVideo(surl)
} else {
// otherwise use native youtube stream implementation (probably doesn't work, but worth a try)
// src, err = youtube.NewYoutubeVideo(surl)
logger.Fatalf("No Youtube-dl installed. exiting\n")
}
if err != nil {
return nil, err
}
return &Track{
trackSrc: src,
}, nil
}
return nil, errors.New("LoadTrack (track.go): No Youtube Video Found under URL");
}
func (t *Track) GetTitle() string {
return t.trackSrc.GetTitle()
}
func GetCurrentTrack() *Track {
return currTrack
}
func Play(t *Track) bool { // {{{
if t.Done || currTrack != nil {
return false
}
currTrack = t
t.buffer_ms = 500 // buffering of 500 ms should be enough, I think...
go audioroutine(t)
return true
} // }}}
// Stops the current Track if one is playing
func Stop() {
stop <- true
}
// Pauses the current Track if one is playing
func Pause() {
pause <- true
}
func Resume() {
pause <- false
}
// This audioroutine gets called whenever a new audio stream should be played
func audioroutine(t *Track) { // {{{
enc, err := audioformats.NewOpusEncoder() // initializes a new encoder
if err != nil {
eventpuffer <- TrackExceptionEvent{
Track: t,
err: fmt.Errorf("Could not create Opus Encoder: %v\n", err),
}
return
}
opusbuf := make(chan []byte, t.buffer_ms / audioformats.OPUS_FRAME_DURATION) // make channel with buffer_ms/frame_duration number of frames as buffer
// our producer
go func () {
for true {
opusPayload, err := getNextOpusFrame(t, enc)
if err != nil {
logger.Errorf("Could not get next opus frame: %v\n", err)
opusbuf <- nil
return
}
if opusPayload == nil {
// close channel instead of writing nil to channel (cleaner i think...)
close(opusbuf)
return
}
select {
case opusbuf <- opusPayload:
break
case <- stop:
return
}
}
}()
timer := time.NewTicker((audioformats.OPUS_FRAME_DURATION) * time.Millisecond)
var last bool
//file, err := os.OpenFile("send.opus", os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0666)
//if err != nil {
// logger.Fatal("...")
//}
loop:for range timer.C {
select {
case <- stop:
t.Done = true
currTrack = nil
eventpuffer <- TrackStoppedEvent{
Track: t,
}
break loop
case <- pause:
eventpuffer <- TrackPausedEvent {
Track: t,
}
select {
case <- stop:
t.Done = true
currTrack = nil
eventpuffer <- TrackStoppedEvent{
Track: t,
}
break loop
case <- pause:
// Track resumed
//TODO Check if pause is true or false (pause or resume)
}
default:
}
logger.Debugf("Get next opus frame, number of frames in buffer: %d\n", len(opusbuf))
lastTime := time.Now()
opusPayload, ok := <-opusbuf
elapsed := time.Since(lastTime)
if elapsed.Microseconds() > 4000 {
logger.Warnf("elapsed time from getting next opus frame: %s\n", elapsed)
}
if !ok {
// Channel got closed -> track is done
t.Done = true
currTrack = nil
eventpuffer <- TrackFinishedEvent {
Track: t,
}
break
}
if opusPayload == nil {
// happens if there was an error getting the opus payload from our producer
t.Done = true
currTrack = nil
eventpuffer <- TrackExceptionEvent {
Track: t,
}
break
}
sendAudioPacket(opusPayload, uint16(len(opusPayload)), last)
//file.Write(opusPayload)
}
//WriteInt16InFile("pcm.final", allpcm)
} // }}}
//var allpcm []int16
func getNextOpusFrame(t *Track, encoder *audioformats.OpusEncoder) ([]byte, error) { // {{{
pcm, err := t.trackSrc.GetPCMFrame(audioformats.OPUS_FRAME_DURATION)
if err != nil {
if err == io.EOF {
return nil, nil
}
return nil, fmt.Errorf("Could not get PCM-Frame: %v", err)
}
opus, err := encoder.Encode(pcm)
if err != nil {
return nil, fmt.Errorf("Could not encode PCM-Frame: %v", err)
}
return opus, nil
} // }}}