Add optional streamOptions to playStream() functions (#663)

* Adding playStream options

Added optional volume and seek properties

* fixed code formatting

Edited my code styling to match with the rest of d.js

* Added Default Variable

* Added Documentation

Added documentation for the streamOptions parameter
Added a default value for seek in FFmpegConverterEngine
Removed redundantcy

* Fixed docs for voiceConnection

* Change _updateMember's notSame check from && to || (#665)

* Fixed git merge fail

* Built Docs. Again.........

* Hopefully finally fixed
This commit is contained in:
Perry Berman
2016-09-08 19:49:52 -07:00
committed by Schuyler Cebulskie
parent 4be7a1bfd1
commit f7a542f12b
7 changed files with 33 additions and 21 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@@ -203,6 +203,7 @@ class VoiceConnection extends EventEmitter {
/**
* Play the given file in the voice connection
* @param {string} file The path to the file
* @param {Object} [options] Optional streamOptions object. Currently accepts seek and volume properties.
* @returns {StreamDispatcher}
* @example
* // play files natively
@@ -212,36 +213,42 @@ class VoiceConnection extends EventEmitter {
* })
* .catch(console.log);
*/
playFile(file) {
return this.player.playFile(file);
playFile(file, { seek = 0, volume = 1 }) {
const options = { seek: seek, volume: volume };
return this.player.playFile(file, options);
}
/**
* Plays and converts an audio stream in the voice connection
* @param {ReadableStream} stream The audio stream to play
* @param {Object} [options] Optional streamOptions object. Currently accepts seek and volume properties.
* @returns {StreamDispatcher}
* @example
* // play streams using ytdl-core
* const ytdl = require('ytdl-core');
* const streamOptions = {seek: 0, volume: 1};
* voiceChannel.join()
* .then(connection => {
* const stream = ytdl('https://www.youtube.com/watch?v=XAWgeLF9EVQ', {filter : 'audioonly'});
* const dispatcher = connection.playStream(stream);
* const dispatcher = connection.playStream(stream, streamOptions);
* })
* .catch(console.log);
*/
playStream(stream) {
return this.player.playStream(stream);
playStream(stream, { seek = 0, volume = 1 }) {
const options = { seek: seek, volume: volume };
return this.player.playStream(stream, options);
}
/**
* Plays a stream of 16-bit signed stereo PCM at 48KHz.
* @param {ReadableStream} stream The audio stream to play.
* @param {Object} [options] Optional streamOptions object. Currently accepts seek and volume properties.
* @returns {StreamDispatcher}
*/
playConvertedStream(stream) {
playConvertedStream(stream, { seek = 0, volume = 1 }) {
const options = { seek: seek, volume: volume };
this._shutdown();
const dispatcher = this.player.playPCMStream(stream);
const dispatcher = this.player.playPCMStream(stream, options);
return dispatcher;
}

View File

@@ -18,7 +18,7 @@ nonce.fill(0);
* @extends {EventEmitter}
*/
class StreamDispatcher extends EventEmitter {
constructor(player, stream, sd) {
constructor(player, stream, sd, streamOptions) {
super();
this.player = player;
this.stream = stream;
@@ -30,7 +30,7 @@ class StreamDispatcher extends EventEmitter {
};
this._startStreaming();
this._triggered = false;
this._volume = 1;
this._volume = streamOptions.volume;
}
/**

View File

@@ -12,7 +12,7 @@ class FfmpegConverterEngine extends ConverterEngine {
this.emit('error', err);
}
createConvertStream() {
createConvertStream(seek = 0) {
super.createConvertStream();
const encoder = ChildProcess.spawn(this.command, [
'-analyzeduration', '0',
@@ -20,7 +20,7 @@ class FfmpegConverterEngine extends ConverterEngine {
'-i', '-',
'-f', 's16le',
'-ar', '48000',
'-ss', '0',
'-ss', seek.toString(),
'pipe:1',
], { stdio: ['pipe', 'pipe', 'ignore'] });
encoder.on('error', e => this.handleError(encoder, e));

View File

@@ -25,8 +25,9 @@ class VoiceConnectionPlayer extends EventEmitter {
};
}
convertStream(stream) {
const encoder = this.converterEngine.createConvertStream();
convertStream(stream, { seek = 0, volume = 1 }) {
const options = { seek: seek, volume: volume };
const encoder = this.converterEngine.createConvertStream(options.seek);
const pipe = stream.pipe(encoder.stdin);
pipe.on('unpipe', () => pipe.destroy());
this.processMap.set(encoder.stdout, {
@@ -88,11 +89,13 @@ class VoiceConnectionPlayer extends EventEmitter {
});
}
playPCMStream(pcmStream) {
const dispatcher = new StreamDispatcher(this, pcmStream, this._streamingData);
playPCMStream(pcmStream, { seek = 0, volume = 1 }) {
const options = { seek: seek, volume: volume };
const dispatcher = new StreamDispatcher(this, pcmStream, this._streamingData, options);
dispatcher.on('speaking', value => this.setSpeaking(value));
dispatcher.on('end', () => this.killStream(pcmStream));
dispatcher.on('error', () => this.killStream(pcmStream));
dispatcher.setVolume(options.volume);
this.dispatcher = dispatcher;
return dispatcher;
}

View File

@@ -2,14 +2,16 @@ const BasePlayer = require('./BasePlayer');
const fs = require('fs');
class DefaultPlayer extends BasePlayer {
playFile(file) {
return this.playStream(fs.createReadStream(file));
playFile(file, { seek = 0, volume = 1 }) {
const options = { seek: seek, volume: volume };
return this.playStream(fs.createReadStream(file), options);
}
playStream(stream) {
playStream(stream, { seek = 0, volume = 1 }) {
this._shutdown();
const pcmStream = this.convertStream(stream);
const dispatcher = this.playPCMStream(pcmStream);
const options = { seek: seek, volume: volume };
const pcmStream = this.convertStream(stream, options);
const dispatcher = this.playPCMStream(pcmStream, options);
return dispatcher;
}
}