Release headless audio contexts, clients and timers on chat stop

This commit is contained in:
evgeny committed 2026-10-26 09:08:42 +01:00
1 parent 563bbc72a2
commit 0fb268bd16
3 files changed
+28 -10

No files matched your search

+6 -2
View File
@@ -346,7 +346,10 @@ int call_headless_init(struct UASYNC* ua, struct UTUN_INSTANCE* inst,
call_set_media_recv_cb(inst, ca_media_recv, ca);
ca->cleanup_timer = uasync_set_timeout(ua, 5000, ca, ca_cleanup, "ca_cleanup");
if (!ca->cleanup_timer) DEBUG_ERROR(DEBUG_CATEGORY_CALL, "%s: cleanup timer scheduling failed", CH_ID);
if (!ca->cleanup_timer) {
DEBUG_ERROR(DEBUG_CATEGORY_CALL, "%s: cleanup timer scheduling failed", CH_ID);
call_headless_destroy(inst); return -1;
}
ca->running = 1;
DEBUG_INFO(DEBUG_CATEGORY_CALL, "%s: audio socket listening on %s:%u", CH_ID, bind_ip, bound_port);
@@ -361,7 +364,7 @@ uint16_t call_headless_get_port(struct UTUN_INSTANCE* inst) {
void call_headless_destroy(struct UTUN_INSTANCE* inst) {
struct call_audio* ca = ca_of(inst);
if (!ca || !ca->running) return;
if (!ca) return;
ca->running = 0;
if (ca->cleanup_timer) { uasync_cancel_timeout(ca->ua, ca->cleanup_timer); ca->cleanup_timer = NULL; }
call_set_media_recv_cb(inst, NULL, NULL);
@@ -379,4 +382,5 @@ void call_headless_destroy(struct UTUN_INSTANCE* inst) {
ca->clients = NULL; ca->client_count = 0;
DEBUG_INFO(DEBUG_CATEGORY_CALL, "%s: audio socket destroyed (enc=%u dec=%u enc_drop=%u dec_drop=%u)",
CH_ID, ca->c_enc_frames, ca->c_dec_frames, ca->c_enc_dropped, ca->c_dec_dropped);
inst->call_audio = NULL; u_free(ca);
}
+15 -4
View File
@@ -82,6 +82,7 @@ struct radio_headless {
opus_codec_encoder_t* enc; /* TX-кодер активного канала */
struct rh_source sources[RH_MAX_SOURCES];
void* mix_timer;
void* cleanup_timer; // отменяется вместе с mix_timer до освобождения контекста
int32_t mix[RH_FRAME_SAMPLES];
/* диагностика */
uint32_t c_enc_frames, c_dec_frames, c_enc_dropped, c_dec_dropped;
@@ -294,6 +295,7 @@ static void rh_mix_tick(void* arg) {
}
rh->mix_timer = uasync_set_timeout(rh->ua, RH_MIX_TIMER_TB, rh, rh_mix_tick, "rh_mix");
if (!rh->mix_timer) DEBUG_ERROR(DEBUG_CATEGORY_RADIO, "%s: mix timer scheduling failed", RH_ID);
}
/* ── разбор входящих фреймов ── */
@@ -431,6 +433,8 @@ static void rh_accept_callback(socket_t fd, void* arg) {
static void rh_cleanup(void* arg) {
struct radio_headless* rh = (struct radio_headless*)arg;
rh->cleanup_timer = NULL;
if (!rh->running) return;
struct radio_headless_client** prev = &rh->clients;
while (*prev) {
if ((*prev)->closing) {
@@ -439,7 +443,8 @@ static void rh_cleanup(void* arg) {
u_free(dead); rh->client_count--;
} else prev = &(*prev)->next;
}
if (rh->ua) uasync_set_timeout(rh->ua, 5000, rh, rh_cleanup, "rh_cleanup");
rh->cleanup_timer = uasync_set_timeout(rh->ua, 5000, rh, rh_cleanup, "rh_cleanup");
if (!rh->cleanup_timer) DEBUG_ERROR(DEBUG_CATEGORY_RADIO, "%s: cleanup timer scheduling failed", RH_ID);
}
/* ── init / destroy ── */
@@ -480,8 +485,12 @@ int radio_headless_init(struct UASYNC* ua, struct UTUN_INSTANCE* inst,
radio_set_frame_cb(inst, rh_frame_cb, rh);
radio_set_mute_cb(inst, rh_member_mute, rh);
uasync_set_timeout(ua, 5000, rh, rh_cleanup, "rh_cleanup");
rh->cleanup_timer = uasync_set_timeout(ua, 5000, rh, rh_cleanup, "rh_cleanup");
rh->mix_timer = uasync_set_timeout(ua, RH_MIX_TIMER_TB, rh, rh_mix_tick, "rh_mix");
if (!rh->cleanup_timer || !rh->mix_timer) {
DEBUG_ERROR(DEBUG_CATEGORY_RADIO, "%s: startup timer scheduling failed cleanup=%p mix=%p", RH_ID, rh->cleanup_timer, rh->mix_timer);
radio_headless_destroy(inst); return -1;
}
rh->running = 1;
DEBUG_INFO(DEBUG_CATEGORY_RADIO, "%s: audio socket listening on %s:%u", RH_ID, bind_ip, bound_port);
@@ -496,11 +505,12 @@ uint16_t radio_headless_get_port(struct UTUN_INSTANCE* inst) {
void radio_headless_destroy(struct UTUN_INSTANCE* inst) {
struct radio_headless* rh = rh_of(inst);
if (!rh || !rh->running) return;
if (!rh) return;
rh->running = 0;
radio_set_frame_cb(inst, NULL, NULL);
radio_set_mute_cb(inst, NULL, NULL);
if (rh->mix_timer && rh->ua) { uasync_cancel_timeout(rh->ua, rh->mix_timer); rh->mix_timer = NULL; }
if (rh->cleanup_timer && rh->ua) { uasync_cancel_timeout(rh->ua, rh->cleanup_timer); rh->cleanup_timer = NULL; }
if (rh->listen_sock_id && rh->ua) uasync_remove_socket_t(rh->ua, rh->listen_fd);
if (rh->listen_fd != SOCKET_INVALID) { socket_close_wrapper(rh->listen_fd); rh->listen_fd = SOCKET_INVALID; }
if (rh->enc) { opus_codec_encoder_destroy(rh->enc); rh->enc = NULL; }
@@ -508,11 +518,12 @@ void radio_headless_destroy(struct UTUN_INSTANCE* inst) {
struct radio_headless_client* c = rh->clients;
while (c) {
struct radio_headless_client* n = c->next;
if (c->fd != SOCKET_INVALID) socket_close_wrapper(c->fd);
rh_close_client(c);
u_free(c);
c = n;
}
rh->clients = NULL; rh->client_count = 0;
DEBUG_INFO(DEBUG_CATEGORY_RADIO, "%s: audio socket destroyed (enc=%u dec=%u enc_drop=%u dec_drop=%u)",
RH_ID, rh->c_enc_frames, rh->c_dec_frames, rh->c_enc_dropped, rh->c_dec_dropped);
inst->radio_headless = NULL; u_free(rh);
}
+7 -4
View File
@@ -855,10 +855,11 @@ int chat_service_start(struct UTUN_INSTANCE* instance) {
const char* bind;
const char* name;
int (*start)(struct UASYNC*, struct UTUN_INSTANCE*, const char*, int);
void (*stop)(struct UTUN_INSTANCE*);
} headless[] = {
{instance->config->global.headless_control_bind, "chat control", chat_headless_control_init},
{instance->config->global.call_audio_bind, "call audio", call_headless_init},
{instance->config->global.radio_audio_bind, "radio audio", radio_headless_init}
{instance->config->global.headless_control_bind, "chat control", chat_headless_control_init, chat_headless_control_destroy},
{instance->config->global.call_audio_bind, "call audio", call_headless_init, call_headless_destroy},
{instance->config->global.radio_audio_bind, "radio audio", radio_headless_init, radio_headless_destroy}
};
for (size_t i = 0; i < sizeof(headless) / sizeof(headless[0]); i++) {
const char* bind = headless[i].bind;
@@ -876,8 +877,10 @@ int chat_service_start(struct UTUN_INSTANCE* instance) {
}
}
if (!valid) { DEBUG_WARN(DEBUG_CATEGORY_CONFIG, "headless %s disabled: invalid IPv4:port '%s'", headless[i].name, bind); continue; }
if (headless[i].start(instance->ua, instance, ip, (int)port) != 0)
if (headless[i].start(instance->ua, instance, ip, (int)port) != 0) {
DEBUG_WARN(DEBUG_CATEGORY_GENERAL, "headless %s listener failed bind=%s (non-fatal)", headless[i].name, bind);
headless[i].stop(instance);
}
}