-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix overflow mixing #3601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix overflow mixing #3601
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
| /* Optimized inline function to clamp 16-bit audio samples */ | ||
| static inline opus_int16 overflow_check(int sum) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing for this function here, so something like As a side note, better to define |
||
| 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 ( | ||
|
|
@@ -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); | ||
|
|
||
There was a problem hiding this comment.
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_TUNEDetc.