From 67447e4e5e4f3470a33e9cfc1ddc2e5b2c908f8c Mon Sep 17 00:00:00 2001 From: Alex P Date: Thu, 2 Oct 2025 00:08:42 +0300 Subject: [PATCH] [WIP] Updates: reduce PR complexity --- internal/audio/c/audio_common.c | 81 +++++++++++++++++++++++++++++++++ internal/audio/c/audio_common.h | 57 +++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 internal/audio/c/audio_common.c create mode 100644 internal/audio/c/audio_common.h diff --git a/internal/audio/c/audio_common.c b/internal/audio/c/audio_common.c new file mode 100644 index 00000000..0b7b14ec --- /dev/null +++ b/internal/audio/c/audio_common.c @@ -0,0 +1,81 @@ +/* + * JetKVM Audio Common Utilities + * + * Shared functions used by both audio input and output servers + */ + +#include "audio_common.h" +#include +#include +#include +#include + +// ============================================================================ +// 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); +} + +// ============================================================================ +// CONFIGURATION PARSING +// ============================================================================ + +int audio_common_parse_env_int(const char *name, int default_value) { + const char *str = getenv(name); + if (str == NULL || str[0] == '\0') { + return default_value; + } + return 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; +} + +int audio_common_is_trace_enabled(void) { + const char *pion_trace = getenv("PION_LOG_TRACE"); + if (pion_trace == NULL) { + return 0; + } + + // Check if "audio" is in comma-separated list + if (strstr(pion_trace, "audio") != NULL) { + return 1; + } + + return 0; +} diff --git a/internal/audio/c/audio_common.h b/internal/audio/c/audio_common.h new file mode 100644 index 00000000..ae8b9a10 --- /dev/null +++ b/internal/audio/c/audio_common.h @@ -0,0 +1,57 @@ +/* + * 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 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); + +// ============================================================================ +// CONFIGURATION PARSING +// ============================================================================ + +/** + * 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 + */ +int audio_common_parse_env_int(const char *name, int 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); + +/** + * Check if trace logging is enabled for audio subsystem. + * Looks for "audio" in PION_LOG_TRACE comma-separated list. + * + * @return 1 if enabled, 0 otherwise + */ +int audio_common_is_trace_enabled(void); + +#endif // JETKVM_AUDIO_COMMON_H