From 8716702b595c59744c57b814ddfc8aca3404b967 Mon Sep 17 00:00:00 2001 From: aemino Date: Sun, 2 Apr 2017 10:43:46 -0700 Subject: [PATCH] Add FEC and PLP support for Opus encoding (#1264) * Add FEC and PLP support for Opus encoding * Silently fail if the Opus engine doesn't support CTL * Fixed inversed max/min functions --- src/client/voice/opus/BaseOpusEngine.js | 17 +++++++++++++++++ src/client/voice/opus/NodeOpusEngine.js | 9 +++++++++ src/client/voice/opus/OpusScriptEngine.js | 9 +++++++++ 3 files changed, 35 insertions(+) diff --git a/src/client/voice/opus/BaseOpusEngine.js b/src/client/voice/opus/BaseOpusEngine.js index 3262ce549..d0c13aae3 100644 --- a/src/client/voice/opus/BaseOpusEngine.js +++ b/src/client/voice/opus/BaseOpusEngine.js @@ -1,6 +1,23 @@ class BaseOpus { constructor(player) { this.player = player; + + this.ctl = { + FEC: 4012, + PLP: 4014, + }; + } + + init() { + try { + // enable FEC (forward error correction) + this.setFEC(true); + + // set PLP (expected packet loss percentage) to 15% + this.setPLP(0.15); + } catch (err) { + // Opus engine likely has no support for libopus CTL + } } encode(buffer) { diff --git a/src/client/voice/opus/NodeOpusEngine.js b/src/client/voice/opus/NodeOpusEngine.js index 10f287b9d..72f2a818b 100644 --- a/src/client/voice/opus/NodeOpusEngine.js +++ b/src/client/voice/opus/NodeOpusEngine.js @@ -11,6 +11,15 @@ class NodeOpusEngine extends OpusEngine { throw err; } this.encoder = new opus.OpusEncoder(48000, 2); + super.init(); + } + + setFEC(enabled) { + this.encoder.applyEncoderCTL(this.ctl.FEC, enabled ? 1 : 0); + } + + setPLP(percent) { + this.encoder.applyEncoderCTL(this.ctl.PLP, Math.min(100, Math.max(0, percent * 100))); } encode(buffer) { diff --git a/src/client/voice/opus/OpusScriptEngine.js b/src/client/voice/opus/OpusScriptEngine.js index c902e790c..81ca6206b 100644 --- a/src/client/voice/opus/OpusScriptEngine.js +++ b/src/client/voice/opus/OpusScriptEngine.js @@ -11,6 +11,15 @@ class OpusScriptEngine extends OpusEngine { throw err; } this.encoder = new OpusScript(48000, 2); + super.init(); + } + + setFEC(enabled) { + this.encoder.encoderCTL(this.ctl.FEC, enabled ? 1 : 0); + } + + setPLP(percent) { + this.encoder.encoderCTL(this.ctl.PLP, Math.min(100, Math.max(0, percent * 100))); } encode(buffer) {