From 55bf170a14e8f32e9f8e8df9a357d36261116480 Mon Sep 17 00:00:00 2001 From: Alex P Date: Tue, 25 Nov 2025 06:54:03 +0200 Subject: [PATCH] Add memory barrier and improve error logging in audio implementation - Add missing __sync_synchronize() in capture init to match playback init path - Add error handling for os.Setenv calls with warning logs on failure - Add logging when ALSA channel map is unavailable (assumes standard L/R order) --- internal/audio/c/audio.c | 5 +++++ internal/audio/cgo_source.go | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/audio/c/audio.c b/internal/audio/c/audio.c index 2704340e..028f0151 100644 --- a/internal/audio/c/audio.c +++ b/internal/audio/c/audio.c @@ -588,6 +588,10 @@ static int configure_alsa_device(snd_pcm_t *handle, const char *device_name, uin *channels_swapped_out = is_swapped; } free(chmap); + } else { + fprintf(stdout, "INFO: %s: Channel map not available, assuming standard L/R order\n", + device_name); + fflush(stdout); } } @@ -627,6 +631,7 @@ int jetkvm_audio_capture_init() { if (encoder != NULL || pcm_capture_handle != NULL) { capture_initialized = 0; atomic_store(&capture_stop_requested, 1); + __sync_synchronize(); if (pcm_capture_handle) { snd_pcm_drop(pcm_capture_handle); diff --git a/internal/audio/cgo_source.go b/internal/audio/cgo_source.go index 0fb130c3..65edc28f 100644 --- a/internal/audio/cgo_source.go +++ b/internal/audio/cgo_source.go @@ -81,7 +81,9 @@ func (c *CgoSource) Connect() error { } func (c *CgoSource) connectOutput() error { - os.Setenv("ALSA_CAPTURE_DEVICE", c.alsaDevice) + if err := os.Setenv("ALSA_CAPTURE_DEVICE", c.alsaDevice); err != nil { + c.logger.Warn().Err(err).Str("device", c.alsaDevice).Msg("Failed to set ALSA_CAPTURE_DEVICE") + } const sampleRate = 48000 const frameSize = uint16(sampleRate * 20 / 1000) // 20ms frames @@ -124,7 +126,9 @@ func (c *CgoSource) connectOutput() error { } func (c *CgoSource) connectInput() error { - os.Setenv("ALSA_PLAYBACK_DEVICE", c.alsaDevice) + if err := os.Setenv("ALSA_PLAYBACK_DEVICE", c.alsaDevice); err != nil { + c.logger.Warn().Err(err).Str("device", c.alsaDevice).Msg("Failed to set ALSA_PLAYBACK_DEVICE") + } // USB Audio Gadget uses fixed 48kHz sample rate const inputSampleRate = 48000