From 0921484ef6775691f0b5fcd17623f6b08837c0a9 Mon Sep 17 00:00:00 2001 From: meew0 Date: Sun, 27 Dec 2015 17:25:04 +0100 Subject: [PATCH] Add opus sanity check --- src/Voice/AudioEncoder.js | 16 ++++++++++++++++ src/Voice/VoiceConnection.js | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Voice/AudioEncoder.js b/src/Voice/AudioEncoder.js index 588934c39..95a16f3c3 100644 --- a/src/Voice/AudioEncoder.js +++ b/src/Voice/AudioEncoder.js @@ -15,6 +15,22 @@ export default class AudioEncoder { this.opus = new opus.OpusEncoder(48000, 2); } this.choice = false; + this.sanityCheckPassed = undefined; + } + + sanityCheck() { + var _opus = this.opus; + var encodeZeroes = function() { + try { + var zeroes = new Buffer(1920); + zeroes.fill(0); + return _opus.encode(zeroes, 1920).readUIntBE(0, 3); + } catch(err) { + return false; + } + }; + if(this.sanityCheckPassed === undefined) this.sanityCheckPassed = (encodeZeroes() === 16056318); + return this.sanityCheckPassed; } opusBuffer(buffer) { diff --git a/src/Voice/VoiceConnection.js b/src/Voice/VoiceConnection.js index c1c4c1528..4c8283c40 100644 --- a/src/Voice/VoiceConnection.js +++ b/src/Voice/VoiceConnection.js @@ -189,6 +189,14 @@ export default class VoiceConnection extends EventEmitter { self.client.emit("debug", "Tried to use node-opus, but opus not available - install it!"); return; } + + if (!self.encoder.sanityCheck()) { + self.playing = false; + self.emit("error", "Opus sanity check failed!"); + self.client.emit("debug", "Opus sanity check failed - opus is installed but not correctly! Please reinstall opus and make sure it's installed correctly."); + return; + } + var buffer = self.encoder.opusBuffer(rawbuffer); var packet = new VoicePacket(buffer, sequence, timestamp, self.vWSData.ssrc); return self.sendPacket(packet, callback);