mirror of
https://github.com/stryan/mumble-discord-bridge.git
synced 2024-11-22 21:35:44 -05:00
refactor wait group
added debug message for bridge cancel
This commit is contained in:
parent
0f57c5d33a
commit
8ca66fb500
@ -183,7 +183,11 @@ func (b *BridgeState) StartBridge() {
|
||||
// Start Passing Between
|
||||
|
||||
// From Mumble
|
||||
go b.MumbleStream.fromMumbleMixer(ctx, &wg, cancel, toDiscord)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
b.MumbleStream.fromMumbleMixer(ctx, cancel, toDiscord)
|
||||
}()
|
||||
|
||||
// From Discord
|
||||
b.DiscordStream = &DiscordDuplex{
|
||||
@ -191,15 +195,28 @@ func (b *BridgeState) StartBridge() {
|
||||
fromDiscordMap: make(map[uint32]fromDiscord),
|
||||
}
|
||||
|
||||
go b.DiscordStream.discordReceivePCM(ctx, &wg, cancel)
|
||||
go b.DiscordStream.fromDiscordMixer(ctx, &wg, toMumble)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
b.DiscordStream.discordReceivePCM(ctx, cancel)
|
||||
}()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
b.DiscordStream.fromDiscordMixer(ctx, toMumble)
|
||||
}()
|
||||
|
||||
// To Discord
|
||||
go b.DiscordStream.discordSendPCM(ctx, &wg, cancel, toDiscord)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
b.DiscordStream.discordSendPCM(ctx, cancel, toDiscord)
|
||||
}()
|
||||
|
||||
// Monitor Mumble
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
ticker := time.NewTicker(500 * time.Millisecond)
|
||||
for {
|
||||
select {
|
||||
@ -213,7 +230,6 @@ func (b *BridgeState) StartBridge() {
|
||||
cancel()
|
||||
}
|
||||
case <-ctx.Done():
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ var OnError = func(str string, err error) {
|
||||
|
||||
// SendPCM will receive on the provied channel encode
|
||||
// received PCM data into Opus then send that to Discordgo
|
||||
func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, wg *sync.WaitGroup, cancel context.CancelFunc, pcm <-chan []int16) {
|
||||
func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, cancel context.CancelFunc, pcm <-chan []int16) {
|
||||
const channels int = 1
|
||||
const frameRate int = 48000 // audio sampling rate
|
||||
const frameSize int = 960 // uint16 size of each audio frame
|
||||
@ -70,8 +70,6 @@ func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, wg *sync.WaitGroup,
|
||||
var readyTimeout *time.Timer
|
||||
var speakingStart time.Time
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
internalSend := func(opus []byte) {
|
||||
dd.Bridge.DiscordVoice.RWMutex.RLock()
|
||||
if !dd.Bridge.DiscordVoice.Ready || dd.Bridge.DiscordVoice.OpusSend == nil {
|
||||
@ -97,7 +95,7 @@ func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, wg *sync.WaitGroup,
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
wg.Done()
|
||||
log.Println("Stopping Discord send PCM")
|
||||
return
|
||||
default:
|
||||
}
|
||||
@ -151,7 +149,7 @@ func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, wg *sync.WaitGroup,
|
||||
|
||||
// ReceivePCM will receive on the the Discordgo OpusRecv channel and decode
|
||||
// the opus audio into PCM then send it on the provided channel.
|
||||
func (dd *DiscordDuplex) discordReceivePCM(ctx context.Context, wg *sync.WaitGroup, cancel context.CancelFunc) {
|
||||
func (dd *DiscordDuplex) discordReceivePCM(ctx context.Context, cancel context.CancelFunc) {
|
||||
var err error
|
||||
|
||||
lastReady := true
|
||||
@ -162,8 +160,6 @@ func (dd *DiscordDuplex) discordReceivePCM(ctx context.Context, wg *sync.WaitGro
|
||||
zeros[i] = 0
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
for {
|
||||
dd.Bridge.DiscordVoice.RWMutex.RLock()
|
||||
if !dd.Bridge.DiscordVoice.Ready || dd.Bridge.DiscordVoice.OpusRecv == nil {
|
||||
@ -188,7 +184,7 @@ func (dd *DiscordDuplex) discordReceivePCM(ctx context.Context, wg *sync.WaitGro
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
wg.Done()
|
||||
log.Println("Stopping Discord receive PCM")
|
||||
return
|
||||
case p, ok = <-dd.Bridge.DiscordVoice.OpusRecv:
|
||||
}
|
||||
@ -268,7 +264,7 @@ func (dd *DiscordDuplex) discordReceivePCM(ctx context.Context, wg *sync.WaitGro
|
||||
}
|
||||
}
|
||||
|
||||
func (dd *DiscordDuplex) fromDiscordMixer(ctx context.Context, wg *sync.WaitGroup, toMumble chan<- gumble.AudioBuffer) {
|
||||
func (dd *DiscordDuplex) fromDiscordMixer(ctx context.Context, toMumble chan<- gumble.AudioBuffer) {
|
||||
mumbleSilence := gumble.AudioBuffer{}
|
||||
for i := 3; i < 480; i++ {
|
||||
mumbleSilence = append(mumbleSilence, 0x00)
|
||||
@ -279,12 +275,11 @@ func (dd *DiscordDuplex) fromDiscordMixer(ctx context.Context, wg *sync.WaitGrou
|
||||
|
||||
sendAudio := false
|
||||
toMumbleStreaming := false
|
||||
wg.Add(1)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
wg.Done()
|
||||
log.Println("Stopping from Discord mixer")
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ func (m MumbleDuplex) OnAudioStream(e *gumble.AudioStreamEvent) {
|
||||
}()
|
||||
}
|
||||
|
||||
func (m MumbleDuplex) fromMumbleMixer(ctx context.Context, wg *sync.WaitGroup, cancel context.CancelFunc, toDiscord chan []int16) {
|
||||
func (m MumbleDuplex) fromMumbleMixer(ctx context.Context, cancel context.CancelFunc, toDiscord chan []int16) {
|
||||
mumbleSleepTick.Start(10 * time.Millisecond)
|
||||
|
||||
sendAudio := false
|
||||
@ -60,12 +60,10 @@ func (m MumbleDuplex) fromMumbleMixer(ctx context.Context, wg *sync.WaitGroup, c
|
||||
droppingPackets := false
|
||||
droppingPacketCount := 0
|
||||
|
||||
wg.Add(1)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
wg.Done()
|
||||
log.Println("Stopping From Mumble Mixer")
|
||||
return
|
||||
default:
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user