mirror of https://github.com/jetkvm/kvm.git
Remove dead code and redundant checks in audio implementation
- Delete unused audio_common.c and audio_common.h (237 lines of dead code) - Remove redundant encoder/decoder pointer comparisons (mutex already held) - Remove unused err variables in encode/decode hot paths
This commit is contained in:
parent
f324496bd4
commit
3d1c2a13b9
|
|
@ -814,7 +814,6 @@ __attribute__((hot)) int jetkvm_audio_read_encode(void * __restrict__ opus_buf)
|
||||||
static uint16_t sample_rate_check_counter = 0;
|
static uint16_t sample_rate_check_counter = 0;
|
||||||
unsigned char * __restrict__ out = (unsigned char*)opus_buf;
|
unsigned char * __restrict__ out = (unsigned char*)opus_buf;
|
||||||
int32_t pcm_rc, nb_bytes;
|
int32_t pcm_rc, nb_bytes;
|
||||||
int32_t err = 0;
|
|
||||||
uint8_t recovery_attempts = 0;
|
uint8_t recovery_attempts = 0;
|
||||||
const uint8_t max_recovery_attempts = 3;
|
const uint8_t max_recovery_attempts = 3;
|
||||||
|
|
||||||
|
|
@ -907,7 +906,7 @@ retry_read:
|
||||||
}
|
}
|
||||||
|
|
||||||
OpusEncoder *enc = encoder;
|
OpusEncoder *enc = encoder;
|
||||||
if (!enc || enc != encoder) {
|
if (!enc) {
|
||||||
pthread_mutex_unlock(&capture_mutex);
|
pthread_mutex_unlock(&capture_mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
@ -1021,7 +1020,7 @@ int jetkvm_audio_playback_init() {
|
||||||
__attribute__((hot)) int jetkvm_audio_decode_write(void * __restrict__ opus_buf, int32_t opus_size) {
|
__attribute__((hot)) int jetkvm_audio_decode_write(void * __restrict__ opus_buf, int32_t opus_size) {
|
||||||
static short CACHE_ALIGN pcm_buffer[960 * 2]; // Cache-aligned
|
static short CACHE_ALIGN pcm_buffer[960 * 2]; // Cache-aligned
|
||||||
unsigned char * __restrict__ in = (unsigned char*)opus_buf;
|
unsigned char * __restrict__ in = (unsigned char*)opus_buf;
|
||||||
int32_t pcm_frames, pcm_rc, err = 0;
|
int32_t pcm_frames, pcm_rc;
|
||||||
uint8_t recovery_attempts = 0;
|
uint8_t recovery_attempts = 0;
|
||||||
const uint8_t max_recovery_attempts = 3;
|
const uint8_t max_recovery_attempts = 3;
|
||||||
|
|
||||||
|
|
@ -1044,7 +1043,7 @@ __attribute__((hot)) int jetkvm_audio_decode_write(void * __restrict__ opus_buf,
|
||||||
}
|
}
|
||||||
|
|
||||||
OpusDecoder *dec = decoder;
|
OpusDecoder *dec = decoder;
|
||||||
if (!dec || dec != decoder) {
|
if (!dec) {
|
||||||
pthread_mutex_unlock(&playback_mutex);
|
pthread_mutex_unlock(&playback_mutex);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,101 +0,0 @@
|
||||||
/*
|
|
||||||
* JetKVM Audio Common Utilities
|
|
||||||
*
|
|
||||||
* Shared functions for audio processing
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "audio_common.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <signal.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
// GLOBAL STATE FOR SIGNAL HANDLER
|
|
||||||
|
|
||||||
// Pointer to the running flag that will be set to 0 on shutdown
|
|
||||||
static volatile sig_atomic_t *g_running_ptr = NULL;
|
|
||||||
|
|
||||||
// SIGNAL HANDLERS
|
|
||||||
|
|
||||||
static void signal_handler(int signo) {
|
|
||||||
if (signo == SIGTERM || signo == SIGINT) {
|
|
||||||
printf("Audio server: Received signal %d, shutting down...\n", signo);
|
|
||||||
if (g_running_ptr != NULL) {
|
|
||||||
*g_running_ptr = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void audio_common_setup_signal_handlers(volatile sig_atomic_t *running) {
|
|
||||||
g_running_ptr = running;
|
|
||||||
|
|
||||||
struct sigaction sa;
|
|
||||||
memset(&sa, 0, sizeof(sa));
|
|
||||||
sa.sa_handler = signal_handler;
|
|
||||||
sigemptyset(&sa.sa_mask);
|
|
||||||
sa.sa_flags = 0;
|
|
||||||
|
|
||||||
sigaction(SIGTERM, &sa, NULL);
|
|
||||||
sigaction(SIGINT, &sa, NULL);
|
|
||||||
|
|
||||||
// Ignore SIGPIPE (write to closed socket should return error, not crash)
|
|
||||||
signal(SIGPIPE, SIG_IGN);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int32_t audio_common_parse_env_int(const char *name, int32_t default_value) {
|
|
||||||
const char *str = getenv(name);
|
|
||||||
if (str == NULL || str[0] == '\0') {
|
|
||||||
return default_value;
|
|
||||||
}
|
|
||||||
return (int32_t)atoi(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* audio_common_parse_env_string(const char *name, const char *default_value) {
|
|
||||||
const char *str = getenv(name);
|
|
||||||
if (str == NULL || str[0] == '\0') {
|
|
||||||
return default_value;
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
// COMMON CONFIGURATION
|
|
||||||
|
|
||||||
void audio_common_load_config(audio_config_t *config, int is_output) {
|
|
||||||
// ALSA device configuration
|
|
||||||
if (is_output) {
|
|
||||||
config->alsa_device = audio_common_parse_env_string("ALSA_CAPTURE_DEVICE", "hw:1,0");
|
|
||||||
} else {
|
|
||||||
config->alsa_device = audio_common_parse_env_string("ALSA_PLAYBACK_DEVICE", "hw:1,0");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Common Opus configuration
|
|
||||||
config->opus_bitrate = audio_common_parse_env_int("OPUS_BITRATE", 128000);
|
|
||||||
config->opus_complexity = audio_common_parse_env_int("OPUS_COMPLEXITY", 2);
|
|
||||||
|
|
||||||
// Audio format
|
|
||||||
config->sample_rate = audio_common_parse_env_int("AUDIO_SAMPLE_RATE", 48000);
|
|
||||||
config->channels = audio_common_parse_env_int("AUDIO_CHANNELS", 2);
|
|
||||||
config->frame_size = audio_common_parse_env_int("AUDIO_FRAME_SIZE", 960);
|
|
||||||
|
|
||||||
// Log configuration
|
|
||||||
printf("Audio %s Server Configuration:\n", is_output ? "Output" : "Input");
|
|
||||||
printf(" ALSA Device: %s\n", config->alsa_device);
|
|
||||||
printf(" Sample Rate: %d Hz\n", config->sample_rate);
|
|
||||||
printf(" Channels: %d\n", config->channels);
|
|
||||||
printf(" Frame Size: %d samples\n", config->frame_size);
|
|
||||||
if (is_output) {
|
|
||||||
printf(" Opus Bitrate: %d bps\n", config->opus_bitrate);
|
|
||||||
printf(" Opus Complexity: %d\n", config->opus_complexity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void audio_common_print_startup(const char *server_name) {
|
|
||||||
printf("JetKVM %s Starting...\n", server_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void audio_common_print_shutdown(const char *server_name) {
|
|
||||||
printf("Shutting down %s...\n", server_name);
|
|
||||||
}
|
|
||||||
|
|
@ -1,135 +0,0 @@
|
||||||
/*
|
|
||||||
* JetKVM Audio Common Utilities
|
|
||||||
*
|
|
||||||
* Shared functions used by both audio input and output servers
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef JETKVM_AUDIO_COMMON_H
|
|
||||||
#define JETKVM_AUDIO_COMMON_H
|
|
||||||
|
|
||||||
#include <signal.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
// SHARED CONSTANTS
|
|
||||||
|
|
||||||
// Audio processing parameters
|
|
||||||
#define AUDIO_MAX_PACKET_SIZE 1500 // Maximum Opus packet size
|
|
||||||
#define AUDIO_SLEEP_MICROSECONDS 1000 // Default sleep time in microseconds
|
|
||||||
#define AUDIO_MAX_ATTEMPTS 5 // Maximum retry attempts
|
|
||||||
#define AUDIO_MAX_BACKOFF_US 500000 // Maximum backoff in microseconds
|
|
||||||
|
|
||||||
// Error handling
|
|
||||||
#define AUDIO_MAX_CONSECUTIVE_ERRORS 10 // Maximum consecutive errors before giving up
|
|
||||||
|
|
||||||
// Performance monitoring
|
|
||||||
#define AUDIO_TRACE_MASK 0x3FF // Log every 1024th frame (bit mask for efficiency)
|
|
||||||
|
|
||||||
// SIGNAL HANDLERS
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setup signal handlers for graceful shutdown.
|
|
||||||
* Handles SIGTERM and SIGINT by setting the running flag to 0.
|
|
||||||
* Ignores SIGPIPE to prevent crashes on broken pipe writes.
|
|
||||||
*
|
|
||||||
* @param running Pointer to the volatile running flag to set on shutdown
|
|
||||||
*/
|
|
||||||
void audio_common_setup_signal_handlers(volatile sig_atomic_t *running);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse integer from environment variable.
|
|
||||||
* Returns default_value if variable is not set or empty.
|
|
||||||
*
|
|
||||||
* @param name Environment variable name
|
|
||||||
* @param default_value Default value if not set
|
|
||||||
* @return Parsed integer value or default
|
|
||||||
*/
|
|
||||||
int32_t audio_common_parse_env_int(const char *name, int32_t default_value);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse string from environment variable.
|
|
||||||
* Returns default_value if variable is not set or empty.
|
|
||||||
*
|
|
||||||
* @param name Environment variable name
|
|
||||||
* @param default_value Default value if not set
|
|
||||||
* @return Environment variable value or default (not duplicated)
|
|
||||||
*/
|
|
||||||
const char* audio_common_parse_env_string(const char *name, const char *default_value);
|
|
||||||
|
|
||||||
|
|
||||||
// COMMON CONFIGURATION
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Common audio configuration structure
|
|
||||||
*/
|
|
||||||
typedef struct {
|
|
||||||
const char *alsa_device; // ALSA device path
|
|
||||||
int opus_bitrate; // Opus bitrate
|
|
||||||
int opus_complexity; // Opus complexity
|
|
||||||
int sample_rate; // Sample rate
|
|
||||||
int channels; // Number of channels
|
|
||||||
int frame_size; // Frame size in samples
|
|
||||||
} audio_config_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load common audio configuration from environment
|
|
||||||
* @param config Output configuration
|
|
||||||
* @param is_output true for output server, false for input
|
|
||||||
*/
|
|
||||||
void audio_common_load_config(audio_config_t *config, int is_output);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print server startup message
|
|
||||||
* @param server_name Name of the server (e.g., "Audio Output Server")
|
|
||||||
*/
|
|
||||||
void audio_common_print_startup(const char *server_name);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Print server shutdown message
|
|
||||||
* @param server_name Name of the server
|
|
||||||
*/
|
|
||||||
void audio_common_print_shutdown(const char *server_name);
|
|
||||||
|
|
||||||
// ERROR TRACKING
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Error tracking state for audio processing loops
|
|
||||||
*/
|
|
||||||
typedef struct {
|
|
||||||
uint8_t consecutive_errors; // Current consecutive error count
|
|
||||||
uint32_t frame_count; // Total frames processed
|
|
||||||
} audio_error_tracker_t;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize error tracker
|
|
||||||
*/
|
|
||||||
static inline void audio_error_tracker_init(audio_error_tracker_t *tracker) {
|
|
||||||
tracker->consecutive_errors = 0;
|
|
||||||
tracker->frame_count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Record an error and check if we should give up
|
|
||||||
* Returns 1 if too many errors, 0 to continue
|
|
||||||
*/
|
|
||||||
static inline uint8_t audio_error_tracker_record_error(audio_error_tracker_t *tracker) {
|
|
||||||
tracker->consecutive_errors++;
|
|
||||||
return (tracker->consecutive_errors >= AUDIO_MAX_CONSECUTIVE_ERRORS) ? 1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Record success and increment frame count
|
|
||||||
*/
|
|
||||||
static inline void audio_error_tracker_record_success(audio_error_tracker_t *tracker) {
|
|
||||||
tracker->consecutive_errors = 0;
|
|
||||||
tracker->frame_count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if we should log trace info for this frame
|
|
||||||
*/
|
|
||||||
static inline uint8_t audio_error_tracker_should_trace(audio_error_tracker_t *tracker) {
|
|
||||||
return ((tracker->frame_count & AUDIO_TRACE_MASK) == 1) ? 1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // JETKVM_AUDIO_COMMON_H
|
|
||||||
Loading…
Reference in New Issue