From b9705f4bac29515adc04771c379a63304dc452d5 Mon Sep 17 00:00:00 2001 From: Alex P Date: Mon, 10 Nov 2025 15:42:10 +0200 Subject: [PATCH] Revert "refactor: use atomic.Pointer for thread-safe inputSource access" This reverts commit 41345b0527c69b8de85e4afafb92ec5f7a9b03ff. --- Makefile | 6 ++++-- audio.go | 35 +++++++++++++++++------------------ main.go | 2 +- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 8c2b18ef..fc1acb7b 100644 --- a/Makefile +++ b/Makefile @@ -205,12 +205,14 @@ lint-go-fix: build_audio_deps # Run UI linting locally (mirrors GitHub workflow ui-lint.yml) lint-ui: @echo "Running UI lint..." - @cd ui && npm ci && npm run lint + @cd ui && npm ci + @cd ui && npm run lint # Run UI linting with auto-fix lint-ui-fix: @echo "Running UI lint with auto-fix..." - @cd ui && npm ci && npm run lint:fix + @cd ui && npm ci + @cd ui && npm run lint:fix # Legacy alias for UI linting (for backward compatibility) ui-lint: lint-ui diff --git a/audio.go b/audio.go index 348dda04..374a98b7 100644 --- a/audio.go +++ b/audio.go @@ -14,7 +14,7 @@ import ( var ( audioMutex sync.Mutex outputSource audio.AudioSource - inputSource atomic.Pointer[audio.AudioSource] + inputSource audio.AudioSource outputRelay *audio.OutputRelay inputRelay *audio.InputRelay audioInitialized bool @@ -63,15 +63,13 @@ func startAudio() error { // Start input audio if not running, USB audio enabled, and input enabled ensureConfigLoaded() - if inputSource.Load() == nil && audioInputEnabled.Load() && config.UsbDevices != nil && config.UsbDevices.Audio { + if inputSource == nil && audioInputEnabled.Load() && config.UsbDevices != nil && config.UsbDevices.Audio { alsaPlaybackDevice := "hw:1,0" // USB speakers // Create CGO audio source - newInputSource := audio.NewCgoInputSource(alsaPlaybackDevice) - var audioSrc audio.AudioSource = newInputSource - inputSource.Store(&audioSrc) + inputSource = audio.NewCgoInputSource(alsaPlaybackDevice) - inputRelay = audio.NewInputRelay(newInputSource) + inputRelay = audio.NewInputRelay(inputSource) if err := inputRelay.Start(); err != nil { audioLogger.Error().Err(err).Msg("Failed to start input relay") } @@ -98,9 +96,9 @@ func stopInputLocked() { inputRelay.Stop() inputRelay = nil } - if source := inputSource.Load(); source != nil { - (*source).Disconnect() - inputSource.Store(nil) + if inputSource != nil { + inputSource.Disconnect() + inputSource = nil } } @@ -171,8 +169,8 @@ func SetAudioOutputEnabled(enabled bool) error { } } else { audioMutex.Lock() - defer audioMutex.Unlock() stopOutputLocked() + audioMutex.Unlock() } return nil @@ -190,8 +188,8 @@ func SetAudioInputEnabled(enabled bool) error { } } else { audioMutex.Lock() - defer audioMutex.Unlock() stopInputLocked() + audioMutex.Unlock() } return nil @@ -240,22 +238,23 @@ func handleInputTrackForSession(track *webrtc.TrackRemote) { continue // Drop frame but keep reading } - // Get source atomically (hot path optimization) - source := inputSource.Load() + // Get source in single mutex operation (hot path optimization) + audioMutex.Lock() + source := inputSource + audioMutex.Unlock() if source == nil { continue // No relay, drop frame but keep reading } - if !(*source).IsConnected() { - if err := (*source).Connect(); err != nil { + if !source.IsConnected() { + if err := source.Connect(); err != nil { continue } } - if err := (*source).WriteMessage(0, opusData); err != nil { - (*source).Disconnect() - audioLogger.Warn().Err(err).Str("track_id", myTrackID).Msg("failed to write audio message") + if err := source.WriteMessage(0, opusData); err != nil { + source.Disconnect() } } } diff --git a/main.go b/main.go index 5ccd78d9..4df1a746 100644 --- a/main.go +++ b/main.go @@ -133,7 +133,7 @@ func Main() { signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) <-sigs - logger.Log().Msg("JetKVM Shutting Down") + logger.Info().Msg("JetKVM Shutting Down") stopAudio()