Reset what is playing

This commit is contained in:
Amish Shah
2016-12-29 21:51:23 +00:00
parent 72a99f9582
commit ad18b05d66
2 changed files with 21 additions and 3 deletions

View File

@@ -16,6 +16,7 @@ class VoiceBroadcast extends EventEmitter {
this.dispatchers = []; this.dispatchers = [];
this.prism = new Prism(); this.prism = new Prism();
this.currentTranscoder = null; this.currentTranscoder = null;
this.tickInterval = null;
} }
get _playableStream() { get _playableStream() {
@@ -24,7 +25,13 @@ class VoiceBroadcast extends EventEmitter {
} }
registerDispatcher(dispatcher) { registerDispatcher(dispatcher) {
if (!this.dispatchers.includes(dispatcher)) this.dispatchers.push(dispatcher); if (!this.dispatchers.includes(dispatcher)) {
this.dispatchers.push(dispatcher);
dispatcher.once('end', () => {
const ind = this.dispatchers.indexOf(dispatcher);
if (ind > -1) this.dispatchers.splice(ind, 1);
});
}
} }
killCurrentTranscoder() { killCurrentTranscoder() {
@@ -71,6 +78,11 @@ class VoiceBroadcast extends EventEmitter {
} }
_startPlaying() { _startPlaying() {
if (this.tickInterval) clearInterval(this.tickInterval);
this.tickInterval = this.client.setInterval(this.tick.bind(this), 20);
}
tick() {
if (!this._playableStream) return; if (!this._playableStream) return;
const stream = this._playableStream; const stream = this._playableStream;
const buffer = stream.read(1920 * 2); const buffer = stream.read(1920 * 2);
@@ -78,7 +90,13 @@ class VoiceBroadcast extends EventEmitter {
for (const dispatcher of this.dispatchers) { for (const dispatcher of this.dispatchers) {
setImmediate(() => dispatcher.process(buffer, true)); setImmediate(() => dispatcher.process(buffer, true));
} }
setTimeout(this._startPlaying.bind(this), 20); }
end() {
this.killCurrentTranscoder();
for (const dispatcher of this.dispatchers) {
dispatcher.destroy('end', 'broadcast ended');
}
} }
} }

View File

@@ -177,7 +177,7 @@ class StreamDispatcher extends EventEmitter {
const data = this.streamingData; const data = this.streamingData;
if (data.missed >= 5) { if (data.missed >= 5 && !controlled) {
this.destroy('end', 'Stream is not generating quickly enough.'); this.destroy('end', 'Stream is not generating quickly enough.');
return; return;
} }