mirror of https://github.com/jetkvm/kvm.git
Fix: prevent concurrent Opus decoder access during session transfers
During session transfers, multiple handleInputTrackForSession goroutines could call WriteMessage() concurrently on the shared inputSource, causing Opus SILK decoder state corruption and assertion failures. Added inputSourceMutex to serialize WriteMessage() calls and prevent race conditions in both input and output audio paths.
This commit is contained in:
parent
abe61c8959
commit
33ee474865
10
audio.go
10
audio.go
|
|
@ -13,6 +13,8 @@ import (
|
|||
|
||||
var (
|
||||
audioMutex sync.Mutex
|
||||
setAudioTrackMutex sync.Mutex // Prevents concurrent setAudioTrack() calls
|
||||
inputSourceMutex sync.Mutex // Serializes WriteMessage() calls to input source
|
||||
outputSource audio.AudioSource
|
||||
inputSource atomic.Pointer[audio.AudioSource]
|
||||
outputRelay *audio.OutputRelay
|
||||
|
|
@ -130,6 +132,10 @@ func onWebRTCDisconnect() {
|
|||
}
|
||||
|
||||
func setAudioTrack(audioTrack *webrtc.TrackLocalStaticSample) {
|
||||
// Prevent concurrent calls to avoid creating multiple relays
|
||||
setAudioTrackMutex.Lock()
|
||||
defer setAudioTrackMutex.Unlock()
|
||||
|
||||
audioMutex.Lock()
|
||||
currentAudioTrack = audioTrack
|
||||
oldRelay := outputRelay
|
||||
|
|
@ -146,7 +152,6 @@ func setAudioTrack(audioTrack *webrtc.TrackLocalStaticSample) {
|
|||
oldSource.Disconnect()
|
||||
}
|
||||
|
||||
// Create new source and relay for the new track
|
||||
audioMutex.Lock()
|
||||
if currentAudioTrack != nil && audioOutputEnabled.Load() {
|
||||
alsaDevice := "hw:1,0"
|
||||
|
|
@ -265,9 +270,12 @@ func handleInputTrackForSession(track *webrtc.TrackRemote) {
|
|||
}
|
||||
}
|
||||
|
||||
// Serialize WriteMessage() to prevent concurrent session handlers from corrupting Opus decoder
|
||||
inputSourceMutex.Lock()
|
||||
if err := (*source).WriteMessage(0, opusData); err != nil {
|
||||
audioLogger.Warn().Err(err).Msg("failed to write audio message")
|
||||
(*source).Disconnect()
|
||||
}
|
||||
inputSourceMutex.Unlock()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue