mirror of
https://github.com/stryan/mumble-discord-bridge.git
synced 2024-12-28 01:15:38 -05:00
sleepct and tests
This commit is contained in:
parent
bad460e57c
commit
cc387ba3b9
6
Makefile
6
Makefile
@ -12,6 +12,12 @@ dev-race: $(GOFILES)
|
|||||||
dev-profile: $(GOFILES)
|
dev-profile: $(GOFILES)
|
||||||
goreleaser build --skip-validate --rm-dist && sudo ./dist/mumble-discord-bridge_linux_amd64/mumble-discord-bridge -cpuprofile cpu.prof
|
goreleaser build --skip-validate --rm-dist && sudo ./dist/mumble-discord-bridge_linux_amd64/mumble-discord-bridge -cpuprofile cpu.prof
|
||||||
|
|
||||||
|
test-chart: SHELL:=/bin/bash
|
||||||
|
test-chart:
|
||||||
|
go test &
|
||||||
|
until pidof mumble-discord-bridge.test; do continue; done;
|
||||||
|
psrecord --plot test-cpu-memory.png $$(pidof mumble-discord-bridge.test)
|
||||||
|
|
||||||
docker-latest:
|
docker-latest:
|
||||||
docker build -t stieneee/mumble-discord-bridge:latest .
|
docker build -t stieneee/mumble-discord-bridge:latest .
|
||||||
|
|
||||||
|
23
discord.go
23
discord.go
@ -61,7 +61,11 @@ func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, wg *sync.WaitGroup,
|
|||||||
opusSilence = append(opusSilence, 0x00)
|
opusSilence = append(opusSilence, 0x00)
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker := NewTickerCT(20 * time.Millisecond)
|
// ticker := NewTickerCT(20 * time.Millisecond)
|
||||||
|
sleepTick := SleepCT{
|
||||||
|
d: 20 * time.Millisecond,
|
||||||
|
t: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
lastReady := true
|
lastReady := true
|
||||||
var readyTimeout *time.Timer
|
var readyTimeout *time.Timer
|
||||||
@ -97,7 +101,10 @@ func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, wg *sync.WaitGroup,
|
|||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
<-ticker.C
|
|
||||||
|
// <-ticker.C
|
||||||
|
sleepTick.SleepNextTarget()
|
||||||
|
|
||||||
if (len(pcm) > 1 && streaming) || (len(pcm) > dd.Bridge.BridgeConfig.DiscordStartStreamingCount && !streaming) {
|
if (len(pcm) > 1 && streaming) || (len(pcm) > dd.Bridge.BridgeConfig.DiscordStartStreamingCount && !streaming) {
|
||||||
if !streaming {
|
if !streaming {
|
||||||
log.Println("Debug: Discord start speaking")
|
log.Println("Debug: Discord start speaking")
|
||||||
@ -134,7 +141,8 @@ func (dd *DiscordDuplex) discordSendPCM(ctx context.Context, wg *sync.WaitGroup,
|
|||||||
// We want to do this after alerting the user of possible short speaking cycles
|
// We want to do this after alerting the user of possible short speaking cycles
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
internalSend(opusSilence)
|
internalSend(opusSilence)
|
||||||
<-ticker.C
|
// <-ticker.C
|
||||||
|
sleepTick.SleepNextTarget()
|
||||||
}
|
}
|
||||||
|
|
||||||
dd.Bridge.DiscordVoice.Speaking(false)
|
dd.Bridge.DiscordVoice.Speaking(false)
|
||||||
@ -235,7 +243,10 @@ 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, wg *sync.WaitGroup, toMumble chan<- gumble.AudioBuffer) {
|
||||||
ticker := NewTickerCT(10 * time.Millisecond)
|
sleepTick := SleepCT{
|
||||||
|
d: 10 * time.Millisecond,
|
||||||
|
t: time.Now(),
|
||||||
|
}
|
||||||
sendAudio := false
|
sendAudio := false
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
@ -244,9 +255,11 @@ func (dd *DiscordDuplex) fromDiscordMixer(ctx context.Context, wg *sync.WaitGrou
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
wg.Done()
|
wg.Done()
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sleepTick.SleepNextTarget()
|
||||||
|
|
||||||
dd.discordMutex.Lock()
|
dd.discordMutex.Lock()
|
||||||
|
|
||||||
sendAudio = false
|
sendAudio = false
|
||||||
|
BIN
docs/test-cpu-memory.png
Normal file
BIN
docs/test-cpu-memory.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 36 KiB |
48
mumble.go
48
mumble.go
@ -44,7 +44,10 @@ func (m MumbleDuplex) OnAudioStream(e *gumble.AudioStreamEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m MumbleDuplex) fromMumbleMixer(ctx context.Context, wg *sync.WaitGroup, toDiscord chan []int16) {
|
func (m MumbleDuplex) fromMumbleMixer(ctx context.Context, wg *sync.WaitGroup, toDiscord chan []int16) {
|
||||||
ticker := NewTickerCT(10 * time.Millisecond)
|
sleepTick := SleepCT{
|
||||||
|
d: 10 * time.Millisecond,
|
||||||
|
t: time.Now(),
|
||||||
|
}
|
||||||
sendAudio := false
|
sendAudio := false
|
||||||
bufferWarning := false
|
bufferWarning := false
|
||||||
|
|
||||||
@ -58,7 +61,7 @@ func (m MumbleDuplex) fromMumbleMixer(ctx context.Context, wg *sync.WaitGroup, t
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
<-ticker.C
|
sleepTick.SleepNextTarget()
|
||||||
|
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
|
|
||||||
@ -86,27 +89,28 @@ func (m MumbleDuplex) fromMumbleMixer(ctx context.Context, wg *sync.WaitGroup, t
|
|||||||
|
|
||||||
mutex.Unlock()
|
mutex.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 len(toDiscord) > 20 {
|
|
||||||
if !bufferWarning {
|
|
||||||
log.Println("Warning: toDiscord buffer size")
|
|
||||||
bufferWarning = true
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if bufferWarning {
|
|
||||||
log.Println("Resolved: toDiscord buffer size")
|
|
||||||
bufferWarning = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if sendAudio {
|
if sendAudio {
|
||||||
|
|
||||||
|
outBuf := make([]int16, 480)
|
||||||
|
|
||||||
|
for i := 0; i < len(outBuf); i++ {
|
||||||
|
for j := 0; j < len(internalMixerArr); j++ {
|
||||||
|
outBuf[i] += (internalMixerArr[j])[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case toDiscord <- outBuf:
|
case toDiscord <- outBuf:
|
||||||
default:
|
default:
|
||||||
|
41
sleepct.go
Normal file
41
sleepct.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SleepCT - Sleep constant time step crates a sleep based ticker
|
||||||
|
// designed maintain a sleep/tick interval
|
||||||
|
type SleepCT struct {
|
||||||
|
sync.Mutex
|
||||||
|
d time.Duration // duration
|
||||||
|
t time.Time // last time target
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SleepCT) SleepNextTarget() {
|
||||||
|
s.Lock()
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
var last time.Time
|
||||||
|
if s.t.IsZero() {
|
||||||
|
fmt.Println("SleepCT reset")
|
||||||
|
last = now.Add(-s.d)
|
||||||
|
} else {
|
||||||
|
last = s.t
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next Target
|
||||||
|
s.t = last.Add(s.d)
|
||||||
|
|
||||||
|
d := s.t.Sub(now)
|
||||||
|
|
||||||
|
time.Sleep(d)
|
||||||
|
|
||||||
|
// delta := now.Sub(s.t)
|
||||||
|
// fmt.Println("delta", delta, d, time.Since(s.t))
|
||||||
|
|
||||||
|
s.Unlock()
|
||||||
|
}
|
BIN
test-cpu-memory.png
Normal file
BIN
test-cpu-memory.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 32 KiB |
@ -7,7 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Ticker holds a channel that delivers ``ticks'' of a clock
|
// A Ticker holds a channel that delivers ``ticks'' of a clockinter
|
||||||
// at intervals.
|
// at intervals.
|
||||||
type TickerCT struct {
|
type TickerCT struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
@ -70,3 +70,33 @@ func TestTickerCT(t *testing.T) {
|
|||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testSleepCT(wg *sync.WaitGroup) {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(interval time.Duration) {
|
||||||
|
now := time.Now()
|
||||||
|
start := now
|
||||||
|
// start the ticker
|
||||||
|
s := SleepCT{
|
||||||
|
d: interval,
|
||||||
|
t: time.Now(),
|
||||||
|
}
|
||||||
|
var i int64
|
||||||
|
for i = 0; i < testCount; i++ {
|
||||||
|
if i+1 < testCount {
|
||||||
|
time.Sleep(time.Duration(float64(maxSleepInterval) * rand.Float64()))
|
||||||
|
}
|
||||||
|
s.SleepNextTarget()
|
||||||
|
}
|
||||||
|
fmt.Println("SleepCT after", testDuration, "drifts", time.Since(start)-testDuration)
|
||||||
|
wg.Done()
|
||||||
|
}(tickerInterval)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSleepCT(t *testing.T) {
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
|
||||||
|
testSleepCT(&wg)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user