Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/plugins/janus_audiobridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,17 @@ void janus_audiobridge_hangup_media(janus_plugin_session *handle);
void janus_audiobridge_destroy_session(janus_plugin_session *handle, int *error);
json_t *janus_audiobridge_query_session(janus_plugin_session *handle);

/* Audio mixing with overflow protection - tuned to 0.9 of MAX/MIN to prevent clipping */
#define SHRT_MAX_TUNED 29491
#define SHRT_MIN_TUNED -29491

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid risks of clashes, better to add a unique prefix to those defines, e.g. JANUS_AUDIOBRIDGE_SHRT_MAX_TUNED etc.


/* Optimized inline function to clamp 16-bit audio samples */
static inline opus_int16 overflow_check(int sum) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing for this function here, so something like janus_audiobridge_overflow_check.

As a side note, better to define sum as int32_t, as that's what the original sumBuffer is. A generic int should be fine in general anyway, but considering this could be compiled on different systems, better be explicit about it and avoid surprises.

sum = sum > SHRT_MAX_TUNED ? SHRT_MAX_TUNED : sum;
sum = sum < SHRT_MIN_TUNED ? SHRT_MIN_TUNED : sum;
return (opus_int16)sum;
}

/* Plugin setup */
static janus_plugin janus_audiobridge_plugin =
JANUS_PLUGIN_INIT (
Expand Down Expand Up @@ -8900,7 +8911,7 @@ static void *janus_audiobridge_mixer_thread(void *data) {
}
for(i=0; i<samples; i++)
/* FIXME Smoothen/Normalize instead of truncating? */
outBuffer[i] = sumBuffer[i];
outBuffer[i] = overflow_check(sumBuffer[i]);
/* Enqueue this mixed frame for encoding in the participant thread */
janus_audiobridge_rtp_relay_packet *mixedpkt = g_malloc(sizeof(janus_audiobridge_rtp_relay_packet));
mixedpkt->data = g_malloc(samples*2);
Expand Down