From 191aca2c5808802a2878d8704a5f97b6325ed409 Mon Sep 17 00:00:00 2001 From: Dragos Oancea Date: Sat, 1 Oct 2016 19:00:04 -0400 Subject: [PATCH] FS-9601: mod_opus: make adjustable bitrate mutually exclusive with FEC enforcing on the decreasing trend, add step calculation for bitrate adjustment, fix bug on context settings --- src/mod/codecs/mod_opus/mod_opus.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/mod/codecs/mod_opus/mod_opus.c b/src/mod/codecs/mod_opus/mod_opus.c index d4b5b81ac3..8eb3396db9 100644 --- a/src/mod/codecs/mod_opus/mod_opus.c +++ b/src/mod/codecs/mod_opus/mod_opus.c @@ -496,7 +496,6 @@ static switch_status_t switch_opus_init(switch_codec_t *codec, switch_codec_flag codec_fmtp_only_remote.private_info = &opus_codec_settings_remote; switch_opus_fmtp_parse(codec->fmtp_in, &codec_fmtp_only_remote); } - context->codec_settings = opus_codec_settings; /* If bitrate negotiation is allowed, verify whether remote is asking for a smaller maxaveragebitrate */ if (opus_prefs.maxaveragebitrate && @@ -619,8 +618,8 @@ static switch_status_t switch_opus_init(switch_codec_t *codec, switch_codec_flag opus_encoder_ctl(context->encoder_object, OPUS_SET_BITRATE(fec_bitrate)); /* will override the maxaveragebitrate set in opus.conf.xml */ opus_codec_settings.maxaveragebitrate = fec_bitrate; - context->control_state.keep_fec = opus_prefs.keep_fec ; } + context->control_state.keep_fec = opus_prefs.keep_fec; } } @@ -669,6 +668,7 @@ static switch_status_t switch_opus_init(switch_codec_t *codec, switch_codec_flag } } + context->codec_settings = opus_codec_settings; codec->private_info = context; return SWITCH_STATUS_SUCCESS; @@ -1164,6 +1164,21 @@ static switch_status_t switch_opus_control(switch_codec_t *codec, context->old_plpct, plpct); } } + if (opus_prefs.adjust_bitrate) { + /* make bitrate adjust the step , but keep it as a multiple of 400 (see OpusFAQ). + * usual RTCP interval is 5 seconds which is long time - the step should be bigger. */ + /* step's value should depend on packet loss too, to decrease more abrubtly + * at high packet loss. */ + int base_step = 400; /*bps*/ + int range = context->codec_settings.maxaveragebitrate - SWITCH_OPUS_MIN_BITRATE; + float steps = (float)((float)(range / 100) / base_step); + int br_step = (int)(round(steps) * base_step) * plpct; + if (globals.debug || context->debug) { + switch_log_printf(SWITCH_CHANNEL_LOG, + SWITCH_LOG_DEBUG, "Opus encoder: bitrate increase/decrease step now is: %d bps, range:%d\n", br_step, range); + } + context->control_state.increase_step = context->control_state.decrease_step = br_step; + } context->old_plpct = plpct; } break; @@ -1192,8 +1207,10 @@ static switch_status_t switch_opus_control(switch_codec_t *codec, int br_step = context->control_state.decrease_step?context->control_state.decrease_step:400; opus_encoder_ctl(context->encoder_object, OPUS_GET_BITRATE(¤t_bitrate)); if (current_bitrate > SWITCH_OPUS_MIN_BITRATE) { - if ((context->control_state.keep_fec) && (current_bitrate < SWITCH_OPUS_MIN_FEC_BITRATE)) { - opus_prefs.keep_fec = 0; /* no point to try to keep FEC enabled anymore, we're low on network bandwidth (that's why we ended up here) */ + if (context->control_state.keep_fec) { + /* no point to try to keep FEC enabled anymore, + * we're low on network bandwidth (that's why we ended up here) */ + opus_prefs.keep_fec = 0; } opus_encoder_ctl(context->encoder_object, OPUS_SET_BITRATE(current_bitrate-br_step)); if (globals.debug || context->debug) {