yammerbot/discord.go

266 lines
6.0 KiB
Go
Raw Permalink Normal View History

2020-10-29 02:21:07 -04:00
package main
import (
2021-02-01 16:03:38 -05:00
"context"
2020-10-29 02:21:07 -04:00
"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
}
// DiscordDuplex Handle discord voice stream
type DiscordDuplex struct {
Bridge *BridgeState
discordMutex sync.Mutex
fromDiscordMap 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
2021-02-01 16:03:38 -05:00
func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, wg *sync.WaitGroup, cancel context.CancelFunc, pcm <-chan []int16) {
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
2021-02-01 16:03:38 -05:00
wg.Add(1)
2020-10-29 02:21:07 -04:00
for {
select {
2021-02-01 16:03:38 -05:00
case <-ctx.Done():
wg.Done()
return
default:
}
2020-10-29 02:21:07 -04:00
<-ticker.C
if len(pcm) > 1 {
if !streaming {
dd.Bridge.DiscordVoice.Speaking(true)
2020-10-29 02:21:07 -04:00
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
}
dd.Bridge.DiscordVoice.RWMutex.RLock()
2021-04-06 22:34:38 -04:00
if !dd.Bridge.DiscordVoice.Ready || dd.Bridge.DiscordVoice.OpusSend == nil {
if lastReady {
OnError(fmt.Sprintf("Discordgo not ready for opus packets. %+v : %+v", dd.Bridge.DiscordVoice.Ready, dd.Bridge.DiscordVoice.OpusSend), nil)
readyTimeout = time.AfterFunc(30*time.Second, func() {
2021-02-01 16:03:38 -05:00
log.Println("set ready timeout")
cancel()
})
lastReady = false
}
2021-04-06 22:34:38 -04:00
} else if !lastReady {
fmt.Println("Discordgo ready to send opus packets")
lastReady = true
readyTimeout.Stop()
} else {
dd.Bridge.DiscordVoice.OpusSend <- opus
2020-10-29 02:21:07 -04:00
}
dd.Bridge.DiscordVoice.RWMutex.RUnlock()
2020-10-29 02:21:07 -04:00
} else {
if streaming {
dd.Bridge.DiscordVoice.Speaking(false)
2020-10-29 02:21:07 -04:00
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.
2021-02-01 16:03:38 -05:00
func (dd *DiscordDuplex) discordReceivePCM(ctx context.Context, wg *sync.WaitGroup, cancel context.CancelFunc) {
2020-10-29 02:21:07 -04:00
var err error
lastReady := true
var readyTimeout *time.Timer
2021-02-01 16:03:38 -05:00
wg.Add(1)
2020-10-29 02:21:07 -04:00
for {
dd.Bridge.DiscordVoice.RWMutex.RLock()
2021-04-06 22:34:38 -04:00
if !dd.Bridge.DiscordVoice.Ready || dd.Bridge.DiscordVoice.OpusRecv == nil {
if lastReady {
OnError(fmt.Sprintf("Discordgo not to receive opus packets. %+v : %+v", dd.Bridge.DiscordVoice.Ready, dd.Bridge.DiscordVoice.OpusSend), nil)
readyTimeout = time.AfterFunc(30*time.Second, func() {
log.Println("set ready timeout")
2021-02-01 16:03:38 -05:00
cancel()
})
lastReady = false
}
continue
2021-04-06 22:34:38 -04:00
} else if !lastReady {
fmt.Println("Discordgo ready to receive packets")
lastReady = true
readyTimeout.Stop()
2020-10-29 02:21:07 -04:00
}
dd.Bridge.DiscordVoice.RWMutex.RUnlock()
var ok bool
var p *discordgo.Packet
select {
2021-02-01 16:03:38 -05:00
case <-ctx.Done():
wg.Done()
return
2021-02-01 16:03:38 -05:00
case p, ok = <-dd.Bridge.DiscordVoice.OpusRecv:
}
2020-10-29 02:21:07 -04:00
if !ok {
log.Println("Opus not ok")
continue
2020-10-29 02:21:07 -04:00
}
dd.discordMutex.Lock()
_, ok = dd.fromDiscordMap[p.SSRC]
dd.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
}
dd.discordMutex.Lock()
dd.fromDiscordMap[p.SSRC] = newStream
dd.discordMutex.Unlock()
2020-10-29 02:21:07 -04:00
}
dd.discordMutex.Lock()
p.PCM, err = dd.fromDiscordMap[p.SSRC].decoder.Decode(p.Opus, 960, false)
dd.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
}
2020-10-29 02:21:07 -04:00
dd.discordMutex.Lock()
select {
case dd.fromDiscordMap[p.SSRC].pcm <- p.PCM[0:480]:
default:
log.Println("fromDiscordMap buffer full. Dropping packet")
dd.discordMutex.Unlock()
2021-01-05 21:56:37 -05:00
continue
}
select {
case dd.fromDiscordMap[p.SSRC].pcm <- p.PCM[480:960]:
default:
log.Println("fromDiscordMap buffer full. Dropping packet")
}
dd.discordMutex.Unlock()
}
}
2021-02-01 16:03:38 -05:00
func (dd *DiscordDuplex) fromDiscordMixer(ctx context.Context, wg *sync.WaitGroup, toMumble chan<- gumble.AudioBuffer) {
ticker := time.NewTicker(10 * time.Millisecond)
sendAudio := false
2021-02-01 16:03:38 -05:00
wg.Add(1)
for {
select {
2021-02-01 16:03:38 -05:00
case <-ctx.Done():
wg.Done()
return
case <-ticker.C:
}
dd.discordMutex.Lock()
sendAudio = false
internalMixerArr := make([][]int16, 0)
// Work through each channel
for i := range dd.fromDiscordMap {
if len(dd.fromDiscordMap[i].pcm) > 0 {
sendAudio = true
2021-04-06 22:34:38 -04:00
if !dd.fromDiscordMap[i].streaming {
x := dd.fromDiscordMap[i]
x.streaming = true
dd.fromDiscordMap[i] = x
}
x1 := (<-dd.fromDiscordMap[i].pcm)
internalMixerArr = append(internalMixerArr, x1)
} else {
2021-04-06 22:34:38 -04:00
if dd.fromDiscordMap[i].streaming {
x := dd.fromDiscordMap[i]
x.streaming = false
dd.fromDiscordMap[i] = x
}
}
}
dd.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
}
}