2021-04-24 14:36:34 -04:00
|
|
|
package bridge
|
2020-10-29 02:21:07 -04:00
|
|
|
|
|
|
|
import (
|
2021-02-01 16:03:38 -05:00
|
|
|
"context"
|
2020-10-29 02:21:07 -04:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-04-24 18:32:54 -04:00
|
|
|
"github.com/stieneee/gumble/gumble"
|
|
|
|
_ "github.com/stieneee/gumble/opus"
|
2021-04-24 14:36:34 -04:00
|
|
|
"github.com/stieneee/mumble-discord-bridge/pkg/sleepct"
|
2020-10-29 02:21:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var mutex sync.Mutex
|
|
|
|
var fromMumbleArr []chan gumble.AudioBuffer
|
|
|
|
var mumbleStreamingArr []bool
|
|
|
|
|
|
|
|
// MumbleDuplex - listenera and outgoing
|
2021-02-01 16:03:38 -05:00
|
|
|
type MumbleDuplex struct{}
|
2020-12-29 18:19:44 -05:00
|
|
|
|
2020-10-29 02:21:07 -04:00
|
|
|
// OnAudioStream - Spawn routines to handle incoming packets
|
|
|
|
func (m MumbleDuplex) OnAudioStream(e *gumble.AudioStreamEvent) {
|
|
|
|
|
|
|
|
// hold a reference ot the channel in the closure
|
|
|
|
localMumbleArray := make(chan gumble.AudioBuffer, 100)
|
|
|
|
|
|
|
|
mutex.Lock()
|
|
|
|
fromMumbleArr = append(fromMumbleArr, localMumbleArray)
|
|
|
|
mumbleStreamingArr = append(mumbleStreamingArr, false)
|
|
|
|
mutex.Unlock()
|
|
|
|
|
2021-08-23 00:00:39 -04:00
|
|
|
promMumbleArraySize.Set(float64(len(fromMumbleArr)))
|
|
|
|
|
2021-01-19 01:06:08 -05:00
|
|
|
go func() {
|
2021-04-06 22:34:38 -04:00
|
|
|
name := e.User.Name
|
2021-04-24 14:36:34 -04:00
|
|
|
log.Println("New mumble audio stream", name)
|
2021-04-06 22:34:38 -04:00
|
|
|
for p := range e.C {
|
|
|
|
// log.Println("audio packet", p.Sender.Name, len(p.AudioBuffer))
|
|
|
|
|
|
|
|
// 480 per 10ms
|
|
|
|
for i := 0; i < len(p.AudioBuffer)/480; i++ {
|
|
|
|
localMumbleArray <- p.AudioBuffer[480*i : 480*(i+1)]
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
2021-08-23 00:00:39 -04:00
|
|
|
promReceivedMumblePackets.Inc()
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
2021-04-24 14:36:34 -04:00
|
|
|
log.Println("Mumble audio stream ended", name)
|
2021-01-19 01:06:08 -05:00
|
|
|
}()
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
func (m MumbleDuplex) fromMumbleMixer(ctx context.Context, wg *sync.WaitGroup, toDiscord chan []int16) {
|
2021-04-24 14:36:34 -04:00
|
|
|
sleepTick := sleepct.SleepCT{}
|
|
|
|
sleepTick.Start(10 * time.Millisecond)
|
|
|
|
|
2020-10-29 02:21:07 -04:00
|
|
|
sendAudio := false
|
2021-04-15 00:43:12 -04:00
|
|
|
bufferWarning := false
|
|
|
|
|
2021-02-01 16:03:38 -05:00
|
|
|
wg.Add(1)
|
2020-10-29 02:21:07 -04:00
|
|
|
|
|
|
|
for {
|
2020-12-29 18:19:44 -05:00
|
|
|
select {
|
2021-02-01 16:03:38 -05:00
|
|
|
case <-ctx.Done():
|
|
|
|
wg.Done()
|
2020-12-29 18:19:44 -05:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2021-02-01 16:03:38 -05:00
|
|
|
|
2021-09-11 13:39:37 -04:00
|
|
|
promTimerMumbleMixer.Observe(float64(sleepTick.SleepNextTarget(true)))
|
2020-10-29 02:21:07 -04:00
|
|
|
|
|
|
|
mutex.Lock()
|
|
|
|
|
|
|
|
sendAudio = false
|
|
|
|
internalMixerArr := make([]gumble.AudioBuffer, 0)
|
2021-08-23 00:00:39 -04:00
|
|
|
streamingCount := 0
|
2020-10-29 02:21:07 -04:00
|
|
|
|
|
|
|
// Work through each channel
|
|
|
|
for i := 0; i < len(fromMumbleArr); i++ {
|
|
|
|
if len(fromMumbleArr[i]) > 0 {
|
|
|
|
sendAudio = true
|
2021-04-06 22:34:38 -04:00
|
|
|
if !mumbleStreamingArr[i] {
|
2020-10-29 02:21:07 -04:00
|
|
|
mumbleStreamingArr[i] = true
|
2021-08-23 00:00:39 -04:00
|
|
|
streamingCount++
|
2021-04-24 14:36:34 -04:00
|
|
|
// log.Println("Mumble starting", i)
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
x1 := (<-fromMumbleArr[i])
|
|
|
|
internalMixerArr = append(internalMixerArr, x1)
|
|
|
|
} else {
|
2021-04-06 22:34:38 -04:00
|
|
|
if mumbleStreamingArr[i] {
|
2020-10-29 02:21:07 -04:00
|
|
|
mumbleStreamingArr[i] = false
|
2021-04-24 14:36:34 -04:00
|
|
|
// log.Println("Mumble stopping", i)
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex.Unlock()
|
|
|
|
|
2021-08-23 00:00:39 -04:00
|
|
|
promMumbleStreaming.Set(float64(streamingCount))
|
|
|
|
|
2021-04-18 00:30:27 -04:00
|
|
|
if sendAudio {
|
2020-10-29 02:21:07 -04:00
|
|
|
|
2021-04-18 00:30:27 -04:00
|
|
|
outBuf := make([]int16, 480)
|
2020-10-29 02:21:07 -04:00
|
|
|
|
2021-04-18 00:30:27 -04:00
|
|
|
for i := 0; i < len(outBuf); i++ {
|
|
|
|
for j := 0; j < len(internalMixerArr); j++ {
|
|
|
|
outBuf[i] += (internalMixerArr[j])[i]
|
|
|
|
}
|
2021-04-15 00:43:12 -04:00
|
|
|
}
|
2021-04-18 00:30:27 -04:00
|
|
|
|
|
|
|
if len(toDiscord) > 20 {
|
|
|
|
if !bufferWarning {
|
|
|
|
log.Println("Warning: toDiscord buffer size")
|
|
|
|
bufferWarning = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if bufferWarning {
|
|
|
|
log.Println("Resolved: toDiscord buffer size")
|
|
|
|
bufferWarning = false
|
|
|
|
}
|
2021-04-15 00:43:12 -04:00
|
|
|
}
|
2021-04-08 01:14:24 -04:00
|
|
|
|
2021-08-23 00:00:39 -04:00
|
|
|
promToDiscordBufferSize.Set(float64(len(toDiscord)))
|
2020-11-04 01:12:43 -05:00
|
|
|
select {
|
|
|
|
case toDiscord <- outBuf:
|
|
|
|
default:
|
2021-04-15 00:43:12 -04:00
|
|
|
log.Println("Error: toDiscord buffer full. Dropping packet")
|
2021-08-23 00:00:39 -04:00
|
|
|
promToDiscordDropped.Inc()
|
2020-11-04 01:12:43 -05:00
|
|
|
}
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|