From 06c72f83d79e38b0421fdfe1d4b1bf8f5bf3b9c9 Mon Sep 17 00:00:00 2001 From: Jason Locklin Date: Fri, 31 Jul 2015 10:50:37 -0400 Subject: OPUS_APPLICATION_VOIP should be used See: http://opus-codec.org/docs/html_api-1.0.1/group__opus__encoder.html#gaa89264fd93c9da70362a0c9b96b9ca88 "VOIP" rather than "AUDIO": > gives best quality at a given bitrate for voice signals. It enhances the input signal by high-pass filtering and emphasizing formants and harmonics. Optionally it includes in-band forward error correction to protect against packet loss. Use this mode for typical VoIP applications. --- toxav/codec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toxav/codec.c b/toxav/codec.c index ae146ccf..71d24e87 100644 --- a/toxav/codec.c +++ b/toxav/codec.c @@ -294,7 +294,7 @@ static int init_audio_encoder(CSSession *cs) { int rc = OPUS_OK; cs->audio_encoder = opus_encoder_create(cs->audio_encoder_sample_rate, - cs->audio_encoder_channels, OPUS_APPLICATION_AUDIO, &rc); + cs->audio_encoder_channels, OPUS_APPLICATION_VOIP, &rc); if ( rc != OPUS_OK ) { LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(rc)); -- cgit v1.2.3 From bcb864af4ac51fb78cdac9df249949d6fbca937a Mon Sep 17 00:00:00 2001 From: Jason Locklin Date: Fri, 31 Jul 2015 11:01:32 -0400 Subject: Enable in-band FEC In-band FEC can be used with OPUS_APPLICATION_VOIP to improve Codec robustness to packet loss and corruption. It is disabled by default: http://opus-codec.org/docs/html_api-1.0.1/group__opus__encoderctls.html#ga5b67dc832aa46c1c2f35752c46380545 --- toxav/codec.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/toxav/codec.c b/toxav/codec.c index 71d24e87..7b76929c 100644 --- a/toxav/codec.c +++ b/toxav/codec.c @@ -303,6 +303,13 @@ static int init_audio_encoder(CSSession *cs) rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_encoder_bitrate)); + if ( rc != OPUS_OK ) { + LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc)); + return -1; + } + + rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_INBAND_FEC(1)); + if ( rc != OPUS_OK ) { LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc)); return -1; -- cgit v1.2.3 From ef086a5897ab2f3296ba777e7e1229384981313c Mon Sep 17 00:00:00 2001 From: Jason Locklin Date: Fri, 31 Jul 2015 11:08:51 -0400 Subject: Set packet loss percentage Make the Codec resistant to up to 10% packet loss (default 0) at the expense of some bandwidth. 10% is aggressive (1-5% should be typical for voip systems, but can be higher when users are on WiFi connections. This could also be adjusted on the fly, rather than hard-coded, with feedback from the receiving client. --- toxav/codec.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/toxav/codec.c b/toxav/codec.c index 7b76929c..9f705db4 100644 --- a/toxav/codec.c +++ b/toxav/codec.c @@ -315,6 +315,14 @@ static int init_audio_encoder(CSSession *cs) return -1; } + /* Make codec resistant to up to 10% packet loss */ + rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_PACKET_LOSS_PERC(10)); + + if ( rc != OPUS_OK ) { + LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc)); + return -1; + } + rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10)); if ( rc != OPUS_OK ) { -- cgit v1.2.3 From f05fdae055cb01ed03008d964485dfda16b2df18 Mon Sep 17 00:00:00 2001 From: Jason Locklin Date: Fri, 31 Jul 2015 11:11:32 -0400 Subject: added documentation --- toxav/codec.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/toxav/codec.c b/toxav/codec.c index 9f705db4..81bbede6 100644 --- a/toxav/codec.c +++ b/toxav/codec.c @@ -308,6 +308,7 @@ static int init_audio_encoder(CSSession *cs) return -1; } + /* Enable in-band forward error correction in codec */ rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_INBAND_FEC(1)); if ( rc != OPUS_OK ) { @@ -323,6 +324,7 @@ static int init_audio_encoder(CSSession *cs) return -1; } + /* Set algorithm to the highest complexity, maximizing compression */ rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10)); if ( rc != OPUS_OK ) { -- cgit v1.2.3