1
0
mirror of https://github.com/stryan/mumble-discord-bridge.git synced 2024-11-14 19:45:41 -05:00
mumble-discord-bridge/discord.go

252 lines
5.5 KiB
Go
Raw Normal View History

2020-10-29 02:21:07 -04:00
package main
import (
"fmt"
"log"
"sync"
2020-10-29 02:21:07 -04:00
"time"
"github.com/bwmarrin/discordgo"
"layeh.com/gopus"
"layeh.com/gumble/gumble"
_ "layeh.com/gumble/opus"
)
type fromDiscord struct {
decoder *gopus.Decoder
pcm chan []int16
streaming bool
}
var discordMutex sync.Mutex
var discordMixerMutex sync.Mutex
var fromDiscordMap = make(map[uint32]fromDiscord)
2020-10-29 02:21:07 -04:00
// OnError gets called by dgvoice when an error is encountered.
// By default logs to STDERR
var OnError = func(str string, err error) {
prefix := "dgVoice: " + str
if err != nil {
log.Println(prefix + ": " + err.Error())
2020-10-29 02:21:07 -04:00
} else {
log.Println(prefix)
2020-10-29 02:21:07 -04:00
}
}
// SendPCM will receive on the provied channel encode
// received PCM data into Opus then send that to Discordgo
func discordSendPCM(v *discordgo.VoiceConnection, pcm <-chan []int16, die chan bool) {
2020-10-29 02:21:07 -04:00
const channels int = 1
const frameRate int = 48000 // audio sampling rate
const frameSize int = 960 // uint16 size of each audio frame
const maxBytes int = (frameSize * 2) * 2 // max size of opus data
streaming := false
opusEncoder, err := gopus.NewEncoder(frameRate, channels, gopus.Audio)
if err != nil {
OnError("NewEncoder Error", err)
panic(err)
2020-10-29 02:21:07 -04:00
}
ticker := time.NewTicker(20 * time.Millisecond)
lastReady := true
var readyTimeout *time.Timer
2020-10-29 02:21:07 -04:00
for {
select {
case <-die:
log.Println("Killing discordSendPCM")
return
default:
}
2020-10-29 02:21:07 -04:00
<-ticker.C
if len(pcm) > 1 {
if !streaming {
v.Speaking(true)
streaming = true
}
r1 := <-pcm
r2 := <-pcm
// try encoding pcm frame with Opus
opus, err := opusEncoder.Encode(append(r1, r2...), frameSize, maxBytes)
if err != nil {
OnError("Encoding Error", err)
continue
2020-10-29 02:21:07 -04:00
}
if v.Ready == false || v.OpusSend == nil {
if lastReady == true {
OnError(fmt.Sprintf("Discordgo not ready for opus packets. %+v : %+v", v.Ready, v.OpusSend), nil)
readyTimeout = time.AfterFunc(30*time.Second, func() {
die <- true
})
lastReady = false
}
continue
} else if lastReady == false {
fmt.Println("Discordgo ready to send opus packets")
lastReady = true
readyTimeout.Stop()
2020-10-29 02:21:07 -04:00
}
v.OpusSend <- opus
} else {
if streaming {
v.Speaking(false)
streaming = false
}
}
}
}
// ReceivePCM will receive on the the Discordgo OpusRecv channel and decode
// the opus audio into PCM then send it on the provided channel.
func discordReceivePCM(v *discordgo.VoiceConnection, die chan bool) {
2020-10-29 02:21:07 -04:00
var err error
lastReady := true
var readyTimeout *time.Timer
2020-10-29 02:21:07 -04:00
for {
select {
case <-die:
log.Println("killing discord ReceivePCM")
return
default:
}
2020-10-29 02:21:07 -04:00
if v.Ready == false || v.OpusRecv == nil {
if lastReady == true {
OnError(fmt.Sprintf("Discordgo not to receive opus packets. %+v : %+v", v.Ready, v.OpusSend), nil)
readyTimeout = time.AfterFunc(30*time.Second, func() {
log.Println("set ready timeout")
die <- true
})
lastReady = false
}
continue
} else if lastReady == false {
fmt.Println("Discordgo ready to receive packets")
lastReady = true
readyTimeout.Stop()
2020-10-29 02:21:07 -04:00
}
var ok bool
var p *discordgo.Packet
select {
case p, ok = <-v.OpusRecv:
case <-die:
log.Println("killing discord ReceivePCM")
return
}
2020-10-29 02:21:07 -04:00
if !ok {
log.Println("Opus not ok")
continue
2020-10-29 02:21:07 -04:00
}
discordMutex.Lock()
_, ok = fromDiscordMap[p.SSRC]
discordMutex.Unlock()
2020-10-29 02:21:07 -04:00
if !ok {
newStream := fromDiscord{}
newStream.pcm = make(chan []int16, 100)
newStream.streaming = false
newStream.decoder, err = gopus.NewDecoder(48000, 1)
2020-10-29 02:21:07 -04:00
if err != nil {
OnError("error creating opus decoder", err)
continue
}
discordMutex.Lock()
fromDiscordMap[p.SSRC] = newStream
discordMutex.Unlock()
2020-10-29 02:21:07 -04:00
}
discordMutex.Lock()
p.PCM, err = fromDiscordMap[p.SSRC].decoder.Decode(p.Opus, 960, false)
discordMutex.Unlock()
2020-10-29 02:21:07 -04:00
if err != nil {
OnError("Error decoding opus data", err)
continue
}
if len(p.PCM) != 960 {
log.Println("Opus size error")
continue
}
discordMutex.Lock()
select {
case fromDiscordMap[p.SSRC].pcm <- p.PCM[0:480]:
default:
log.Println("fromDiscordMap buffer full. Dropping packet")
2021-01-05 21:56:37 -05:00
discordMutex.Unlock()
continue
}
select {
case fromDiscordMap[p.SSRC].pcm <- p.PCM[480:960]:
default:
log.Println("fromDiscordMap buffer full. Dropping packet")
}
discordMutex.Unlock()
}
}
func fromDiscordMixer(toMumble chan<- gumble.AudioBuffer, die chan bool) {
ticker := time.NewTicker(10 * time.Millisecond)
sendAudio := false
for {
select {
case <-die:
log.Println("killing fromDiscordMixer")
return
default:
}
<-ticker.C
discordMutex.Lock()
sendAudio = false
internalMixerArr := make([][]int16, 0)
// Work through each channel
for i := range fromDiscordMap {
if len(fromDiscordMap[i].pcm) > 0 {
sendAudio = true
if fromDiscordMap[i].streaming == false {
x := fromDiscordMap[i]
x.streaming = true
fromDiscordMap[i] = x
}
x1 := (<-fromDiscordMap[i].pcm)
internalMixerArr = append(internalMixerArr, x1)
} else {
if fromDiscordMap[i].streaming == true {
x := fromDiscordMap[i]
x.streaming = false
fromDiscordMap[i] = x
}
}
}
discordMutex.Unlock()
outBuf := make([]int16, 480)
for i := 0; i < len(outBuf); i++ {
for j := 0; j < len(internalMixerArr); j++ {
outBuf[i] += (internalMixerArr[j])[i]
}
}
if sendAudio {
select {
case toMumble <- outBuf:
default:
log.Println("toMumble buffer full. Dropping packet")
}
}
2020-10-29 02:21:07 -04:00
}
}