From 15ff312e11cf93ee2e82f8d333c16b64eca6c5f2 Mon Sep 17 00:00:00 2001 From: erkin-io <9297263+erkin-io@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:44:14 +0100 Subject: [PATCH] Feeding MP3 audio to the MP4 muxer only while something reads MP4 Co-Authored-By: Claude Opus 5.5 --- src/media.c | 12 +++++++++--- src/server.c | 9 +++++++++ src/server.h | 1 + 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/media.c b/src/media.c index 87b1715e..efc61ae8 100644 --- a/src/media.c +++ b/src/media.c @@ -116,9 +116,15 @@ void *aenc_thread(void) { pthread_mutex_lock(&aencMtx); if (mp3Buf.offset >= mp3FrmSize) { send_mp3_to_client(mp3Buf.buf, mp3FrmSize); - pthread_mutex_lock(&mp4Mtx); - mp4_ingest_audio(mp3Buf.buf, mp3FrmSize); - pthread_mutex_unlock(&mp4Mtx); + /* Only the video path empties the MP4 audio buffer, and only while + * an MP4 client or the recorder is there. Fed without one (the + * MP3 encoder also runs for RTSP and RTMP), it grew for as long as + * the program ran. */ + if (record_active() || any_http_mp4()) { + pthread_mutex_lock(&mp4Mtx); + mp4_ingest_audio(mp3Buf.buf, mp3FrmSize); + pthread_mutex_unlock(&mp4Mtx); + } if (app_config.rtsp_enable && !rtsp_g711_active()) rtp_send_mp3(rtspHandle, mp3Buf.buf, mp3FrmSize); rtmp_ingest_audio(mp3Buf.buf, mp3FrmSize); diff --git a/src/server.c b/src/server.c index b47cde51..d6d9666b 100644 --- a/src/server.c +++ b/src/server.c @@ -229,6 +229,15 @@ char any_http_audio(void) { return any; } +char any_http_mp4(void) { + char any = 0; + pthread_mutex_lock(&client_fds_mutex); + for (unsigned int i = 0; i < HTTP_MAX_CLIENTS; ++i) + if (client_fds[i].sockFd >= 0 && client_fds[i].type == STREAM_MP4) { any = 1; break; } + pthread_mutex_unlock(&client_fds_mutex); + return any; +} + void send_mp4_to_client(char index, hal_vidstream *stream, char isH265) { char anyClient = 0; pthread_mutex_lock(&client_fds_mutex); diff --git a/src/server.h b/src/server.h index 28f4bde7..1ae2364f 100644 --- a/src/server.h +++ b/src/server.h @@ -39,5 +39,6 @@ void send_mjpeg_to_client(char index, char *buf, ssize_t size); void send_h26x_to_client(char index, hal_vidstream *stream); void send_mp3_to_client(char *buf, ssize_t size); char any_http_audio(void); +char any_http_mp4(void); void send_mp4_to_client(char index, hal_vidstream *stream, char isH265); void send_pcm_to_client(hal_audframe *frame);