Skip to content

Commit

Permalink
Add internal support for @discordjs/opus to v11 (#3700)
Browse files Browse the repository at this point in the history
* Add internal support for @discordjs/opus

* Remove redundant try/catch

* fix: use setBitrate method in @discordjs/opus

* chore: tidy up opus imports

* fix: correct imports for DiscordJsOpusEngine

* chore: update docs to prefer @discordjs/opus

* chore: bump prism-media to 0.0.4 to allow ffmpeg-static
  • Loading branch information
amishshah committed Jan 17, 2020
1 parent 36c0496 commit 6ab4649
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Expand Up @@ -11,7 +11,7 @@ To get ready to work on the codebase, please do the following:

1. Fork & clone the repository, and make sure you're on the **master** branch
2. Run `npm install`
3. If you're working on voice, also run `npm install node-opus` or `npm install opusscript`
3. If you're working on voice, also run `npm install @discordjs/opus` or `npm install opusscript`
4. Code your heart out!
5. Run `npm test` to run ESLint and ensure any JSDoc changes are valid
6. [Submit a pull request](https://github.com/discordjs/discord.js/compare)
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -30,13 +30,13 @@ discord.js is a powerful [node.js](https://nodejs.org) module that allows you to
Ignore any warnings about unmet peer dependencies, as they're all optional.

Without voice support: `npm install discord.js`
With voice support ([node-opus](https://www.npmjs.com/package/node-opus)): `npm install discord.js node-opus`
With voice support ([@discordjs/opus](https://www.npmjs.com/package/@discordjs/opus)): `npm install discord.js @discordjs/opus`
With voice support ([opusscript](https://www.npmjs.com/package/opusscript)): `npm install discord.js opusscript`

### Audio engines
The preferred audio engine is node-opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose node-opus.
Using opusscript is only recommended for development environments where node-opus is tough to get working.
For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers.
The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus.
Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working.
For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers.

### Optional packages
- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the WebSocket when *not* using uws (`npm install bufferutil`)
Expand Down
10 changes: 5 additions & 5 deletions docs/general/faq.md
Expand Up @@ -8,16 +8,16 @@ Update to Node.js 6.0.0 or newer.

## How do I get voice working?
- Install FFMPEG.
- Install either the `node-opus` package or the `opusscript` package.
node-opus is greatly preferred, due to it having significantly better performance.
- Install either the `@discordjs/opus` package or the `opusscript` package.
@discordjs/opus is greatly preferred, due to it having significantly better performance.

## How do I install FFMPEG?
- **npm:** `npm install ffmpeg-binaries`
- **Ubuntu 16.04:** `sudo apt install ffmpeg`
- **Ubuntu 14.04:** `sudo apt-get install libav-tools`
- **Windows:** `npm install ffmpeg-binaries` or see the [FFMPEG section of AoDude's guide](https://github.com/bdistin/OhGodMusicBot/blob/master/README.md#download-ffmpeg).

## How do I set up node-opus?
- **Ubuntu:** Simply run `npm install node-opus`, and it's done. Congrats!
## How do I set up @discordjs/opus?
- **Ubuntu:** Simply run `npm install @discordjs/opus`, and it's done. Congrats!
- **Windows:** Run `npm install --global --production windows-build-tools` in an admin command prompt or PowerShell.
Then, running `npm install node-opus` in your bot's directory should successfully build it. Woo!
Then, running `npm install @discordjs/opus` in your bot's directory should successfully build it. Woo!
8 changes: 4 additions & 4 deletions docs/general/welcome.md
Expand Up @@ -38,13 +38,13 @@ discord.js is a powerful [Node.js](https://nodejs.org) module that allows you to
Ignore any warnings about unmet peer dependencies, as they're all optional.

Without voice support: `npm install discord.js`
With voice support ([node-opus](https://www.npmjs.com/package/node-opus)): `npm install discord.js node-opus`
With voice support ([@discordjs/opus](https://www.npmjs.com/package/@discordjs/opus)): `npm install discord.js @discordjs/opus`
With voice support ([opusscript](https://www.npmjs.com/package/opusscript)): `npm install discord.js opusscript`

### Audio engines
The preferred audio engine is node-opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose node-opus.
Using opusscript is only recommended for development environments where node-opus is tough to get working.
For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers.
The preferred audio engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus.
Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working.
For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers.

### Optional packages
- [bufferutil](https://www.npmjs.com/package/bufferutil) to greatly speed up the WebSocket when *not* using uws (`npm install bufferutil`)
Expand Down
8 changes: 4 additions & 4 deletions docs/topics/voice.md
Expand Up @@ -7,12 +7,12 @@ To get started, make sure you have:
* FFmpeg - `npm install ffmpeg-binaries`
* an opus encoder, choose one from below:
* `npm install opusscript`
* `npm install node-opus`
* `npm install @discordjs/opus`
* a good network connection

The preferred opus engine is node-opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose node-opus.
Using opusscript is only recommended for development environments where node-opus is tough to get working.
For production bots, using node-opus should be considered a necessity, especially if they're going to be running on multiple servers.
The preferred opus engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus.
Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working.
For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers.

## Joining a voice channel
The example below reacts to a message and joins the sender's voice channel, catching any errors. This is important
Expand Down
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -34,7 +34,7 @@
"runkitExampleFilename": "./docs/examples/ping.js",
"dependencies": {
"long": "^4.0.0",
"prism-media": "^0.0.3",
"prism-media": "^0.0.4",
"snekfetch": "^3.6.4",
"tweetnacl": "^1.0.0",
"ws": "^6.0.0"
Expand All @@ -44,6 +44,7 @@
"bufferutil": "^4.0.0",
"erlpack": "discordapp/erlpack",
"libsodium-wrappers": "^0.7.3",
"@discordjs/opus": "^0.1.0",
"node-opus": "^0.2.7",
"opusscript": "^0.0.6",
"sodium": "^2.0.3"
Expand All @@ -55,6 +56,9 @@
"erlpack": {
"optional": true
},
"@discordjs/opus": {
"optional": true
},
"node-opus": {
"optional": true
},
Expand Down Expand Up @@ -93,6 +97,7 @@
"prism-media": false,
"opusscript": false,
"node-opus": false,
"@discordjs/opus": false,
"tweetnacl": false,
"sodium": false,
"src/sharding/Shard.js": false,
Expand All @@ -101,6 +106,7 @@
"src/client/voice/dispatcher/StreamDispatcher.js": false,
"src/client/voice/opus/BaseOpusEngine.js": false,
"src/client/voice/opus/NodeOpusEngine.js": false,
"src/client/voice/opus/DiscordJsOpusEngine.js": false,
"src/client/voice/opus/OpusEngineList.js": false,
"src/client/voice/opus/OpusScriptEngine.js": false,
"src/client/voice/pcm/ConverterEngine.js": false,
Expand Down
34 changes: 34 additions & 0 deletions src/client/voice/opus/DiscordJsOpusEngine.js
@@ -0,0 +1,34 @@
const OpusEngine = require('./BaseOpusEngine');

class DiscordJsOpusEngine extends OpusEngine {
constructor(player) {
super(player);
const opus = require('@discordjs/opus');
this.encoder = new opus.OpusEncoder(this.samplingRate, this.channels);
super.init();
}

setBitrate(bitrate) {
this.encoder.setBitrate(Math.min(128, Math.max(16, bitrate)) * 1000);
}

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) {
super.encode(buffer);
return this.encoder.encode(buffer, 1920);
}

decode(buffer) {
super.decode(buffer);
return this.encoder.decode(buffer, 1920);
}
}

module.exports = DiscordJsOpusEngine;
8 changes: 1 addition & 7 deletions src/client/voice/opus/NodeOpusEngine.js
@@ -1,15 +1,9 @@
const OpusEngine = require('./BaseOpusEngine');

let opus;

class NodeOpusEngine extends OpusEngine {
constructor(player) {
super(player);
try {
opus = require('node-opus');
} catch (err) {
throw err;
}
const opus = require('node-opus');
this.encoder = new opus.OpusEncoder(this.samplingRate, this.channels);
super.init();
}
Expand Down
1 change: 1 addition & 0 deletions src/client/voice/opus/OpusEngineList.js
@@ -1,4 +1,5 @@
const list = [
require('./DiscordJsOpusEngine'),
require('./NodeOpusEngine'),
require('./OpusScriptEngine'),
];
Expand Down
8 changes: 1 addition & 7 deletions src/client/voice/opus/OpusScriptEngine.js
@@ -1,15 +1,9 @@
const OpusEngine = require('./BaseOpusEngine');

let OpusScript;

class OpusScriptEngine extends OpusEngine {
constructor(player) {
super(player);
try {
OpusScript = require('opusscript');
} catch (err) {
throw err;
}
const OpusScript = require('opusscript');
this.encoder = new OpusScript(this.samplingRate, this.channels);
super.init();
}
Expand Down

0 comments on commit 6ab4649

Please sign in to comment.