Make audio source switching instant with async initialization

When switching audio output source between HDMI and USB, the HDMI
audio device (hw:0,0) can take 18-31 seconds to initialize due to
hardware characteristics of the TC358743 chip. This caused the UI
to freeze during source changes.

Changes:
- Move startAudio() to background goroutine in SetAudioOutputSource
- Move SaveConfig() to background goroutine to avoid blocking on disk I/O
- Return immediately after updating in-memory config
- Audio will initialize in background while UI remains responsive

The in-memory config is updated synchronously so subsequent calls
see the new source immediately. Both async operations are protected
by their respective mutexes (audioMutex, configLock).
This commit is contained in:
Alex P 2025-11-19 13:58:23 +02:00
parent ee23e3bf22
commit 7fc90b86a8
1 changed files with 13 additions and 5 deletions

View File

@ -278,14 +278,22 @@ func SetAudioOutputSource(source string) error {
return nil return nil
} }
stopOutputAudio()
config.AudioOutputSource = source config.AudioOutputSource = source
if err := startAudio(); err != nil { go func() {
audioLogger.Error().Err(err).Str("source", source).Msg("Failed to start audio output after source change") stopOutputAudio()
} if err := startAudio(); err != nil {
audioLogger.Error().Err(err).Str("source", source).Msg("Failed to start audio output after source change")
}
}()
return SaveConfig() go func() {
if err := SaveConfig(); err != nil {
audioLogger.Error().Err(err).Msg("Failed to save config after audio source change")
}
}()
return nil
} }
func RestartAudioOutput() error { func RestartAudioOutput() error {