-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmumbler.go
205 lines (173 loc) · 4.12 KB
/
mumbler.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 mumbler
import (
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"strconv"
"time"
"github.com/layeh/gumble/gumble"
"github.com/layeh/gumble/gumbleffmpeg"
_ "github.com/layeh/gumble/opus"
)
type Mumbler struct {
playlist []Source
config *gumble.Config
client *gumble.Client
stream *gumbleffmpeg.Stream
command string
loop bool
audioDucking bool
volume float32
duckRatio float32
}
func New() *Mumbler {
config := gumble.NewConfig()
return &Mumbler{config: config}
}
func (m *Mumbler) Name(n string) {
m.config.Username = n
}
func (m *Mumbler) Password(n string) {
m.config.Password = n
}
func (m *Mumbler) MoveToChannel(name string, create bool) error {
target := m.client.Channels.Find(name)
if target == nil {
root := m.client.Channels.Find()
if !create {
return errors.New("Nonexistent channel " + name)
}
if (*root.Permission() & gumble.PermissionMakeTemporaryChannel) == 0 {
return errors.New("Permission error: Cannot create channels in root")
}
root.Add(name, true)
target := m.client.Channels.Find(name)
if target == nil {
return errors.New("Failed to create channel " + name)
}
}
m.client.Self.Move(target)
return nil
}
func (m *Mumbler) Volume(v float32) {
m.volume = v
}
func (m *Mumbler) AddFile(file ...string) {
for _, v := range file {
m.playlist = append(m.playlist, NewFileSource(v))
}
}
func (m *Mumbler) AddReader(reader ...io.Reader) {
for _, v := range reader {
m.playlist = append(m.playlist, NewReaderSource(v))
}
}
func (m *Mumbler) AddReadCloser(reader ...io.ReadCloser) {
for _, v := range reader {
m.playlist = append(m.playlist, NewReadCloserSource(v))
}
}
func (m *Mumbler) ClearPlaylist() {
m.playlist = []Source{}
}
func (m *Mumbler) Command(c string) {
m.command = c
}
func (m *Mumbler) Certificate(file, keyfile string) error {
if keyfile == "" {
keyfile = file
}
cert, err := tls.LoadX509KeyPair(file, keyfile)
if err != nil {
return err
}
m.config.TLSConfig.Certificates = append(m.config.TLSConfig.Certificates, cert)
return nil
}
func (m *Mumbler) SetTLSInsecureSkipVerify(b bool) {
m.config.TLSConfig.InsecureSkipVerify = b
}
func (m *Mumbler) Play() error {
for {
for _, playlistItem := range m.playlist {
source := playlistItem.GetSource()
m.stream = gumbleffmpeg.New(m.client, source)
m.stream.Volume = m.volume
if m.command != "" {
m.stream.Command = m.command
}
if err := m.stream.Play(); err != nil {
fmt.Println(err)
continue
}
fmt.Printf("Now playing: %v\n", source)
m.stream.Wait()
}
if !m.loop {
break
}
}
return nil
}
func (m *Mumbler) Repeat(l bool) {
m.loop = l
}
func (m *Mumbler) AudioDucking(volume float32) {
if volume != 0.0 {
m.audioDucking = true
m.duckRatio = 1 - volume
}
}
func (m *Mumbler) Server(address string) {
host, port, err := net.SplitHostPort(address)
if err != nil {
host = address
port = strconv.Itoa(gumble.DefaultPort)
}
m.config.Address = net.JoinHostPort(host, port)
}
func (m *Mumbler) Connect() error {
m.client = gumble.NewClient(m.config)
if m.audioDucking {
m.client.AttachAudio(m)
}
if err := m.client.Connect(); err != nil {
return err
}
// wait until connected
for m.client.State() == gumble.StateConnecting {
}
return nil
}
func (m *Mumbler) Disconnect() error {
return m.client.Disconnect()
}
func (m *Mumbler) OnAudioStream(e *gumble.AudioStreamEvent) {
//BEWARE: here be hax
if m.audioDucking {
go func() {
t := time.Tick(500 * time.Millisecond)
currVol := m.volume
duckvol := m.volume * m.duckRatio
for {
select {
case <-t:
for ; currVol < m.volume; currVol += 0.1 {
time.Sleep(50 * time.Millisecond) // for a nice fade
m.stream.Volume = currVol
select {
case <-e.C:
break
default: // empty default -> non-blocking
}
}
case <-e.C:
m.stream.Volume = duckvol
currVol = duckvol
}
}
}()
}
}