Attempt to fix a bug that can randomly drop audio streams.

This commit is contained in:
Storm Dragon
2026-08-11 20:15:49 -04:00
parent 4c5a54c2dd
commit 0f34831c5b
6 changed files with 47 additions and 10 deletions
+21 -10
View File
@@ -41,6 +41,23 @@ func shouldStartPlayback(state openal.State, queued int32, final bool) bool {
return state != openal.Playing && queued > 0 && (queued >= playbackPrebufferBuffers || final)
}
func playbackLayout(packet *gumble.AudioPacket) (openal.Format, int, bool) {
if packet == nil || len(packet.AudioBuffer) == 0 {
return 0, 0, false
}
switch packet.Channels {
case gumble.AudioMonoChannels:
return openal.FormatMono16, len(packet.AudioBuffer), true
case gumble.AudioChannels:
if len(packet.AudioBuffer)%gumble.AudioChannels != 0 {
return 0, 0, false
}
return openal.FormatStereo16, len(packet.AudioBuffer) / gumble.AudioChannels, true
default:
return 0, 0, false
}
}
type playbackCommand struct {
gain float32
muted bool
@@ -550,14 +567,14 @@ func (s *Stream) OnAudioStream(e *gumble.AudioStreamEvent) {
}
var boost uint16 = uint16(1)
samples := len(packet.AudioBuffer)
if samples == 0 {
if len(packet.AudioBuffer) == 0 {
if shouldStartPlayback(source.State(), source.BuffersQueued(), packet.Final) {
source.Play()
}
continue
}
if samples > cap(raw)/2 {
format, samples, validLayout := playbackLayout(packet)
if !validLayout || len(packet.AudioBuffer) > cap(raw)/2 {
continue
}
@@ -569,13 +586,7 @@ func (s *Stream) OnAudioStream(e *gumble.AudioStreamEvent) {
recordBuffer = make([]int16, len(packet.AudioBuffer)*gumble.AudioChannels)
}
// Check if sample count suggests stereo data
isStereo := samples > gumble.AudioDefaultFrameSize && samples%2 == 0
format := openal.FormatMono16
if isStereo {
format = openal.FormatStereo16
samples = samples / 2
}
isStereo := packet.Channels == gumble.AudioChannels
rawPtr := 0
if isStereo {
+18
View File
@@ -32,6 +32,24 @@ func TestShouldStartPlayback(t *testing.T) {
}
}
func TestPlaybackLayoutUsesExplicitStereoChannelCount(t *testing.T) {
packet := &gumble.AudioPacket{
Channels: gumble.AudioChannels,
AudioBuffer: make(gumble.AudioBuffer, gumble.AudioDefaultFrameSize),
}
format, frames, ok := playbackLayout(packet)
if !ok {
t.Fatal("expected a valid short stereo packet")
}
if format != openal.FormatStereo16 {
t.Fatalf("format = %v, want stereo", format)
}
if frames != gumble.AudioDefaultFrameSize/gumble.AudioChannels {
t.Fatalf("frames = %d, want %d", frames, gumble.AudioDefaultFrameSize/gumble.AudioChannels)
}
}
func TestSendOutgoingAudioStopsWhileSendIsBlocked(t *testing.T) {
stop := make(chan struct{})
outgoing := make(chan gumble.AudioBuffer)