/
stream.go
153 lines (132 loc) · 3.32 KB
/
stream.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
package gumbleopenal
import (
"encoding/binary"
"errors"
"time"
"github.com/dchote/go-openal/openal"
"layeh.com/gumble/gumble"
)
var (
ErrState = errors.New("gumbleopenal: invalid state")
)
type Stream struct {
client *gumble.Client
link gumble.Detacher
deviceSource *openal.CaptureDevice
sourceFrameSize int
sourceStop chan bool
deviceSink *openal.Device
contextSink *openal.Context
}
func New(client *gumble.Client) (*Stream, error) {
s := &Stream{
client: client,
sourceFrameSize: client.Config.AudioFrameSize(),
}
s.deviceSource = openal.CaptureOpenDevice("", gumble.AudioSampleRate, openal.FormatMono16, uint32(s.sourceFrameSize))
s.deviceSink = openal.OpenDevice("")
s.contextSink = s.deviceSink.CreateContext()
s.contextSink.Activate()
s.link = client.Config.AttachAudio(s)
return s, nil
}
func (s *Stream) Destroy() {
s.link.Detach()
if s.deviceSource != nil {
s.StopSource()
s.deviceSource.CaptureCloseDevice()
s.deviceSource = nil
}
if s.deviceSink != nil {
s.contextSink.Destroy()
s.deviceSink.CloseDevice()
s.contextSink = nil
s.deviceSink = nil
}
}
func (s *Stream) StartSource() error {
if s.sourceStop != nil {
return ErrState
}
s.deviceSource.CaptureStart()
s.sourceStop = make(chan bool)
go s.sourceRoutine()
return nil
}
func (s *Stream) StopSource() error {
if s.sourceStop == nil {
return ErrState
}
close(s.sourceStop)
s.sourceStop = nil
s.deviceSource.CaptureStop()
return nil
}
func (s *Stream) OnAudioStream(e *gumble.AudioStreamEvent) {
go func() {
source := openal.NewSource()
emptyBufs := openal.NewBuffers(8)
reclaim := func() {
if n := source.BuffersProcessed(); n > 0 {
reclaimedBufs := make(openal.Buffers, n)
source.UnqueueBuffers(reclaimedBufs)
emptyBufs = append(emptyBufs, reclaimedBufs...)
}
}
var raw [gumble.AudioMaximumFrameSize * 2]byte
for packet := range e.C {
samples := len(packet.AudioBuffer)
if samples > cap(raw) {
continue
}
for i, value := range packet.AudioBuffer {
binary.LittleEndian.PutUint16(raw[i*2:], uint16(value))
}
reclaim()
if len(emptyBufs) == 0 {
continue
}
last := len(emptyBufs) - 1
buffer := emptyBufs[last]
emptyBufs = emptyBufs[:last]
buffer.SetData(openal.FormatMono16, raw[:samples*2], gumble.AudioSampleRate)
source.QueueBuffer(buffer)
if source.State() != openal.Playing {
source.Play()
}
}
reclaim()
emptyBufs.Delete()
source.Delete()
}()
}
func (s *Stream) sourceRoutine() {
interval := s.client.Config.AudioInterval
frameSize := s.client.Config.AudioFrameSize()
if frameSize != s.sourceFrameSize {
s.deviceSource.CaptureCloseDevice()
s.sourceFrameSize = frameSize
s.deviceSource = openal.CaptureOpenDevice("", gumble.AudioSampleRate, openal.FormatMono16, uint32(s.sourceFrameSize))
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
stop := s.sourceStop
outgoing := s.client.AudioOutgoing()
defer close(outgoing)
for {
select {
case <-stop:
return
case <-ticker.C:
buff := s.deviceSource.CaptureSamples(uint32(frameSize))
if len(buff) != frameSize*2 {
continue
}
int16Buffer := make([]int16, frameSize)
for i := range int16Buffer {
int16Buffer[i] = int16(binary.LittleEndian.Uint16(buff[i*2 : (i+1)*2]))
}
outgoing <- gumble.AudioBuffer(int16Buffer)
}
}
}