mirror of https://github.com/jetkvm/kvm.git
fix: use atomic.Bool for audio source to prevent mutex contention during switching
This commit is contained in:
parent
4bc60c3f1b
commit
802166ba23
16
audio.go
16
audio.go
|
|
@ -22,7 +22,7 @@ var (
|
||||||
audioLogger zerolog.Logger
|
audioLogger zerolog.Logger
|
||||||
currentAudioTrack *webrtc.TrackLocalStaticSample
|
currentAudioTrack *webrtc.TrackLocalStaticSample
|
||||||
inputTrackHandling atomic.Bool
|
inputTrackHandling atomic.Bool
|
||||||
useUSBForAudioOutput bool
|
useUSBForAudioOutput atomic.Bool
|
||||||
audioOutputEnabled atomic.Bool
|
audioOutputEnabled atomic.Bool
|
||||||
audioInputEnabled atomic.Bool
|
audioInputEnabled atomic.Bool
|
||||||
)
|
)
|
||||||
|
|
@ -32,7 +32,7 @@ func initAudio() {
|
||||||
|
|
||||||
// Load audio output source from config
|
// Load audio output source from config
|
||||||
ensureConfigLoaded()
|
ensureConfigLoaded()
|
||||||
useUSBForAudioOutput = config.AudioOutputSource == "usb"
|
useUSBForAudioOutput.Store(config.AudioOutputSource == "usb")
|
||||||
|
|
||||||
// Enable both by default
|
// Enable both by default
|
||||||
audioOutputEnabled.Store(true)
|
audioOutputEnabled.Store(true)
|
||||||
|
|
@ -57,7 +57,7 @@ func startAudio() error {
|
||||||
// Start output audio if not running and enabled
|
// Start output audio if not running and enabled
|
||||||
if outputSource == nil && audioOutputEnabled.Load() {
|
if outputSource == nil && audioOutputEnabled.Load() {
|
||||||
alsaDevice := "hw:0,0" // HDMI
|
alsaDevice := "hw:0,0" // HDMI
|
||||||
if useUSBForAudioOutput {
|
if useUSBForAudioOutput.Load() {
|
||||||
alsaDevice = "hw:1,0" // USB
|
alsaDevice = "hw:1,0" // USB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,17 +167,17 @@ func SetAudioOutputSource(useUSB bool) error {
|
||||||
audioMutex.Lock()
|
audioMutex.Lock()
|
||||||
defer audioMutex.Unlock()
|
defer audioMutex.Unlock()
|
||||||
|
|
||||||
if useUSBForAudioOutput == useUSB {
|
if useUSBForAudioOutput.Load() == useUSB {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
audioLogger.Info().
|
audioLogger.Info().
|
||||||
Bool("old_usb", useUSBForAudioOutput).
|
Bool("old_usb", useUSBForAudioOutput.Load()).
|
||||||
Bool("new_usb", useUSB).
|
Bool("new_usb", useUSB).
|
||||||
Msg("Switching audio output source")
|
Msg("Switching audio output source")
|
||||||
|
|
||||||
oldValue := useUSBForAudioOutput
|
oldValue := useUSBForAudioOutput.Load()
|
||||||
useUSBForAudioOutput = useUSB
|
useUSBForAudioOutput.Store(useUSB)
|
||||||
|
|
||||||
ensureConfigLoaded()
|
ensureConfigLoaded()
|
||||||
if useUSB {
|
if useUSB {
|
||||||
|
|
@ -187,7 +187,7 @@ func SetAudioOutputSource(useUSB bool) error {
|
||||||
}
|
}
|
||||||
if err := SaveConfig(); err != nil {
|
if err := SaveConfig(); err != nil {
|
||||||
audioLogger.Error().Err(err).Msg("Failed to save config")
|
audioLogger.Error().Err(err).Msg("Failed to save config")
|
||||||
useUSBForAudioOutput = oldValue
|
useUSBForAudioOutput.Store(oldValue)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -899,7 +899,7 @@ func updateUsbRelatedConfig(wasAudioEnabled bool) error {
|
||||||
if config.UsbDevices != nil && !config.UsbDevices.Audio && config.AudioOutputSource == "usb" {
|
if config.UsbDevices != nil && !config.UsbDevices.Audio && config.AudioOutputSource == "usb" {
|
||||||
audioMutex.Lock()
|
audioMutex.Lock()
|
||||||
config.AudioOutputSource = "hdmi"
|
config.AudioOutputSource = "hdmi"
|
||||||
useUSBForAudioOutput = false
|
useUSBForAudioOutput.Store(false)
|
||||||
audioSourceChanged = true
|
audioSourceChanged = true
|
||||||
audioMutex.Unlock()
|
audioMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
@ -908,7 +908,7 @@ func updateUsbRelatedConfig(wasAudioEnabled bool) error {
|
||||||
if config.UsbDevices != nil && config.UsbDevices.Audio && !wasAudioEnabled {
|
if config.UsbDevices != nil && config.UsbDevices.Audio && !wasAudioEnabled {
|
||||||
audioMutex.Lock()
|
audioMutex.Lock()
|
||||||
config.AudioOutputSource = "usb"
|
config.AudioOutputSource = "usb"
|
||||||
useUSBForAudioOutput = true
|
useUSBForAudioOutput.Store(true)
|
||||||
audioSourceChanged = true
|
audioSourceChanged = true
|
||||||
audioMutex.Unlock()
|
audioMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
@ -970,10 +970,7 @@ func rpcSetUsbDeviceState(device string, enabled bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func rpcGetAudioOutputSource() (string, error) {
|
func rpcGetAudioOutputSource() (string, error) {
|
||||||
audioMutex.Lock()
|
if useUSBForAudioOutput.Load() {
|
||||||
defer audioMutex.Unlock()
|
|
||||||
|
|
||||||
if useUSBForAudioOutput {
|
|
||||||
return "usb", nil
|
return "usb", nil
|
||||||
}
|
}
|
||||||
return "hdmi", nil
|
return "hdmi", nil
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue