Cleanup: removed redundant code, comments, etc.

This commit is contained in:
Alex P 2025-09-16 15:31:10 +03:00
parent 647eca4292
commit 7ffb9e1d59
2 changed files with 8 additions and 26 deletions

View File

@ -396,14 +396,13 @@ int jetkvm_audio_capture_init() {
* -1: Initialization error or unrecoverable failure * -1: Initialization error or unrecoverable failure
*/ */
int jetkvm_audio_read_encode(void *opus_buf) { int jetkvm_audio_read_encode(void *opus_buf) {
short pcm_buffer[1920]; // max 2ch*960 static short pcm_buffer[1920]; // max 2ch*960
unsigned char *out = (unsigned char*)opus_buf; unsigned char *out = (unsigned char*)opus_buf;
int err = 0; int err = 0;
int recovery_attempts = 0; int recovery_attempts = 0;
const int max_recovery_attempts = 3; const int max_recovery_attempts = 3;
// Safety checks if (__builtin_expect(!capture_initialized || !pcm_capture_handle || !encoder || !opus_buf, 0)) {
if (!capture_initialized || !pcm_capture_handle || !encoder || !opus_buf) {
if (trace_logging_enabled) { if (trace_logging_enabled) {
printf("[AUDIO_OUTPUT] jetkvm_audio_read_encode: Failed safety checks - capture_initialized=%d, pcm_capture_handle=%p, encoder=%p, opus_buf=%p\n", printf("[AUDIO_OUTPUT] jetkvm_audio_read_encode: Failed safety checks - capture_initialized=%d, pcm_capture_handle=%p, encoder=%p, opus_buf=%p\n",
capture_initialized, pcm_capture_handle, encoder, opus_buf); capture_initialized, pcm_capture_handle, encoder, opus_buf);
@ -416,7 +415,7 @@ retry_read:
int pcm_rc = snd_pcm_readi(pcm_capture_handle, pcm_buffer, frame_size); int pcm_rc = snd_pcm_readi(pcm_capture_handle, pcm_buffer, frame_size);
// Handle ALSA errors with robust recovery strategies // Handle ALSA errors with robust recovery strategies
if (pcm_rc < 0) { if (__builtin_expect(pcm_rc < 0, 0)) {
if (pcm_rc == -EPIPE) { if (pcm_rc == -EPIPE) {
// Buffer underrun - implement progressive recovery // Buffer underrun - implement progressive recovery
recovery_attempts++; recovery_attempts++;
@ -432,9 +431,6 @@ retry_read:
err = snd_pcm_prepare(pcm_capture_handle); err = snd_pcm_prepare(pcm_capture_handle);
if (err < 0) return -1; if (err < 0) return -1;
} }
// Wait before retry to allow device to stabilize
snd_pcm_wait(pcm_capture_handle, sleep_microseconds * recovery_attempts / 1000);
goto retry_read; goto retry_read;
} else if (pcm_rc == -EAGAIN) { } else if (pcm_rc == -EAGAIN) {
// No data available - return 0 to indicate no frame // No data available - return 0 to indicate no frame
@ -457,9 +453,7 @@ retry_read:
err = snd_pcm_prepare(pcm_capture_handle); err = snd_pcm_prepare(pcm_capture_handle);
if (err < 0) return -1; if (err < 0) return -1;
} }
// Wait before retry to allow device to stabilize return 0;
snd_pcm_wait(pcm_capture_handle, sleep_microseconds * recovery_attempts / 1000);
return 0; // Skip this frame but don't fail
} else if (pcm_rc == -ENODEV) { } else if (pcm_rc == -ENODEV) {
// Device disconnected - critical error // Device disconnected - critical error
return -1; return -1;
@ -470,7 +464,6 @@ retry_read:
snd_pcm_drop(pcm_capture_handle); snd_pcm_drop(pcm_capture_handle);
err = snd_pcm_prepare(pcm_capture_handle); err = snd_pcm_prepare(pcm_capture_handle);
if (err >= 0) { if (err >= 0) {
snd_pcm_wait(pcm_capture_handle, sleep_microseconds / 1000);
goto retry_read; goto retry_read;
} }
} }
@ -479,8 +472,6 @@ retry_read:
// Other errors - limited retry for transient issues // Other errors - limited retry for transient issues
recovery_attempts++; recovery_attempts++;
if (recovery_attempts <= 1 && pcm_rc == -EINTR) { if (recovery_attempts <= 1 && pcm_rc == -EINTR) {
// Interrupted system call - use device-aware wait
snd_pcm_wait(pcm_capture_handle, sleep_microseconds / 2000);
goto retry_read; goto retry_read;
} else if (recovery_attempts <= 1 && pcm_rc == -EBUSY) { } else if (recovery_attempts <= 1 && pcm_rc == -EBUSY) {
// Device busy - simple sleep to allow other operations to complete // Device busy - simple sleep to allow other operations to complete
@ -604,14 +595,14 @@ int jetkvm_audio_playback_init() {
* -2: Unrecoverable ALSA error * -2: Unrecoverable ALSA error
*/ */
int jetkvm_audio_decode_write(void *opus_buf, int opus_size) { int jetkvm_audio_decode_write(void *opus_buf, int opus_size) {
short pcm_buffer[1920]; // max 2ch*960 static short pcm_buffer[1920]; // max 2ch*960
unsigned char *in = (unsigned char*)opus_buf; unsigned char *in = (unsigned char*)opus_buf;
int err = 0; int err = 0;
int recovery_attempts = 0; int recovery_attempts = 0;
const int max_recovery_attempts = 3; const int max_recovery_attempts = 3;
// Safety checks // Safety checks
if (!playback_initialized || !pcm_playback_handle || !decoder || !opus_buf || opus_size <= 0) { if (__builtin_expect(!playback_initialized || !pcm_playback_handle || !decoder || !opus_buf || opus_size <= 0, 0)) {
if (trace_logging_enabled) { if (trace_logging_enabled) {
printf("[AUDIO_INPUT] jetkvm_audio_decode_write: Failed safety checks - playback_initialized=%d, pcm_playback_handle=%p, decoder=%p, opus_buf=%p, opus_size=%d\n", printf("[AUDIO_INPUT] jetkvm_audio_decode_write: Failed safety checks - playback_initialized=%d, pcm_playback_handle=%p, decoder=%p, opus_buf=%p, opus_size=%d\n",
playback_initialized, pcm_playback_handle, decoder, opus_buf, opus_size); playback_initialized, pcm_playback_handle, decoder, opus_buf, opus_size);
@ -633,7 +624,7 @@ int jetkvm_audio_decode_write(void *opus_buf, int opus_size) {
// Decode Opus to PCM with error handling // Decode Opus to PCM with error handling
int pcm_frames = opus_decode(decoder, in, opus_size, pcm_buffer, frame_size, 0); int pcm_frames = opus_decode(decoder, in, opus_size, pcm_buffer, frame_size, 0);
if (pcm_frames < 0) { if (__builtin_expect(pcm_frames < 0, 0)) {
if (trace_logging_enabled) { if (trace_logging_enabled) {
printf("[AUDIO_INPUT] jetkvm_audio_decode_write: Opus decode failed with error %d, attempting packet loss concealment\n", pcm_frames); printf("[AUDIO_INPUT] jetkvm_audio_decode_write: Opus decode failed with error %d, attempting packet loss concealment\n", pcm_frames);
} }
@ -656,7 +647,7 @@ retry_write:
; ;
// Write PCM to playback device with robust recovery // Write PCM to playback device with robust recovery
int pcm_rc = snd_pcm_writei(pcm_playback_handle, pcm_buffer, pcm_frames); int pcm_rc = snd_pcm_writei(pcm_playback_handle, pcm_buffer, pcm_frames);
if (pcm_rc < 0) { if (__builtin_expect(pcm_rc < 0, 0)) {
if (trace_logging_enabled) { if (trace_logging_enabled) {
printf("[AUDIO_INPUT] jetkvm_audio_decode_write: ALSA write failed with error %d (%s), attempt %d/%d\n", printf("[AUDIO_INPUT] jetkvm_audio_decode_write: ALSA write failed with error %d (%s), attempt %d/%d\n",
pcm_rc, snd_strerror(pcm_rc), recovery_attempts + 1, max_recovery_attempts); pcm_rc, snd_strerror(pcm_rc), recovery_attempts + 1, max_recovery_attempts);
@ -692,8 +683,6 @@ retry_write:
} }
} }
// Wait before retry to allow device to stabilize
snd_pcm_wait(pcm_playback_handle, sleep_microseconds * recovery_attempts / 1000);
if (trace_logging_enabled) { if (trace_logging_enabled) {
printf("[AUDIO_INPUT] jetkvm_audio_decode_write: Buffer underrun recovery successful, retrying write\n"); printf("[AUDIO_INPUT] jetkvm_audio_decode_write: Buffer underrun recovery successful, retrying write\n");
} }
@ -730,8 +719,6 @@ retry_write:
return -2; return -2;
} }
} }
// Wait before retry to allow device to stabilize
snd_pcm_wait(pcm_playback_handle, sleep_microseconds * recovery_attempts / 1000);
if (trace_logging_enabled) { if (trace_logging_enabled) {
printf("[AUDIO_INPUT] jetkvm_audio_decode_write: Device suspend recovery successful, skipping frame\n"); printf("[AUDIO_INPUT] jetkvm_audio_decode_write: Device suspend recovery successful, skipping frame\n");
} }
@ -752,7 +739,6 @@ retry_write:
snd_pcm_drop(pcm_playback_handle); snd_pcm_drop(pcm_playback_handle);
err = snd_pcm_prepare(pcm_playback_handle); err = snd_pcm_prepare(pcm_playback_handle);
if (err >= 0) { if (err >= 0) {
snd_pcm_wait(pcm_playback_handle, sleep_microseconds / 1000);
if (trace_logging_enabled) { if (trace_logging_enabled) {
printf("[AUDIO_INPUT] jetkvm_audio_decode_write: I/O error recovery successful, retrying write\n"); printf("[AUDIO_INPUT] jetkvm_audio_decode_write: I/O error recovery successful, retrying write\n");
} }

View File

@ -1225,8 +1225,6 @@ func (ais *AudioInputServer) startMonitorGoroutine() {
atomic.StoreInt64(&ais.processingTime, newAvg) atomic.StoreInt64(&ais.processingTime, newAvg)
} }
if err != nil { if err != nil {
atomic.AddInt64(&ais.droppedFrames, 1) atomic.AddInt64(&ais.droppedFrames, 1)
} }
@ -1267,8 +1265,6 @@ func (ais *AudioInputServer) UpdateBufferSize() {
atomic.StoreInt64(&ais.bufferSize, newSize) atomic.StoreInt64(&ais.bufferSize, newSize)
} }
// GetMessagePoolStats returns detailed statistics about the message pool // GetMessagePoolStats returns detailed statistics about the message pool
func (mp *MessagePool) GetMessagePoolStats() MessagePoolStats { func (mp *MessagePool) GetMessagePoolStats() MessagePoolStats {
mp.mutex.RLock() mp.mutex.RLock()