2020-10-29 02:21:07 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-04 01:12:43 -05:00
|
|
|
"log"
|
2020-10-30 01:38:01 -04:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2020-10-30 01:38:01 -04:00
|
|
|
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 {
|
2020-11-04 01:12:43 -05:00
|
|
|
log.Println(prefix + ": " + err.Error())
|
2020-10-29 02:21:07 -04:00
|
|
|
} else {
|
2020-11-04 01:12:43 -05:00
|
|
|
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
|
2020-11-11 22:57:51 -05:00
|
|
|
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)
|
2020-11-04 01:12:43 -05:00
|
|
|
panic(err)
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(20 * time.Millisecond)
|
|
|
|
|
2020-11-11 22:57:51 -05:00
|
|
|
lastReady := true
|
|
|
|
var readyTimeout *time.Timer
|
|
|
|
|
2020-10-29 02:21:07 -04:00
|
|
|
for {
|
|
|
|
<-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)
|
2020-11-04 01:12:43 -05:00
|
|
|
continue
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if v.Ready == false || v.OpusSend == nil {
|
2020-11-11 22:57:51 -05:00
|
|
|
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
|
|
|
|
}
|
2020-11-04 01:12:43 -05:00
|
|
|
continue
|
2020-11-11 22:57:51 -05:00
|
|
|
} 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.
|
2020-11-11 22:57:51 -05:00
|
|
|
func discordReceivePCM(v *discordgo.VoiceConnection, die chan bool) {
|
2020-10-29 02:21:07 -04:00
|
|
|
var err error
|
|
|
|
|
2020-11-11 22:57:51 -05:00
|
|
|
lastReady := true
|
|
|
|
var readyTimeout *time.Timer
|
|
|
|
|
2020-10-29 02:21:07 -04:00
|
|
|
for {
|
|
|
|
if v.Ready == false || v.OpusRecv == nil {
|
2020-11-11 22:57:51 -05:00
|
|
|
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() {
|
|
|
|
die <- true
|
|
|
|
})
|
|
|
|
lastReady = false
|
|
|
|
}
|
2020-11-04 01:12:43 -05:00
|
|
|
continue
|
2020-11-11 22:57:51 -05:00
|
|
|
} else if lastReady == false {
|
|
|
|
fmt.Println("Discordgo ready to receive packets")
|
|
|
|
lastReady = true
|
|
|
|
readyTimeout.Stop()
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p, ok := <-v.OpusRecv
|
|
|
|
if !ok {
|
2020-11-04 01:12:43 -05:00
|
|
|
log.Println("Opus not ok")
|
|
|
|
continue
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
|
2020-10-30 01:38:01 -04:00
|
|
|
discordMutex.Lock()
|
|
|
|
_, ok = fromDiscordMap[p.SSRC]
|
|
|
|
discordMutex.Unlock()
|
2020-10-29 02:21:07 -04:00
|
|
|
if !ok {
|
2020-10-30 01:38:01 -04:00
|
|
|
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
|
|
|
|
}
|
2020-10-30 01:38:01 -04:00
|
|
|
discordMutex.Lock()
|
|
|
|
fromDiscordMap[p.SSRC] = newStream
|
|
|
|
discordMutex.Unlock()
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
|
2020-10-30 01:38:01 -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
|
|
|
|
}
|
|
|
|
|
2020-10-30 01:38:01 -04:00
|
|
|
discordMutex.Lock()
|
|
|
|
fromDiscordMap[p.SSRC].pcm <- p.PCM[0:480]
|
|
|
|
fromDiscordMap[p.SSRC].pcm <- p.PCM[480:960]
|
|
|
|
discordMutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromDiscordMixer(toMumble chan<- gumble.AudioBuffer) {
|
|
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
|
|
sendAudio := false
|
|
|
|
|
|
|
|
for {
|
|
|
|
<-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 {
|
2020-11-04 01:12:43 -05:00
|
|
|
select {
|
|
|
|
case toMumble <- outBuf:
|
|
|
|
default:
|
|
|
|
log.Println("toMumble buffer full. Dropping packet")
|
|
|
|
}
|
|
|
|
|
2020-10-30 01:38:01 -04:00
|
|
|
}
|
2020-10-29 02:21:07 -04:00
|
|
|
}
|
|
|
|
}
|