Skip to content

Commit

Permalink
Fix wrong audio stream, improve debugging (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrymxu committed Feb 8, 2021
1 parent 3b493a2 commit 118f719
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"wav": "^1.0.2",
"yargs-parser": "^20.0.0",
"youtube-search": "^1.1.4",
"ytdl-core": "^4.2.1",
"ytdl-core": "^4.4.5",
"ytpl": "^2.0.3",
"ytsr": "^3.0.0",
"zlib-sync": "^0.1.7"
Expand Down
7 changes: 7 additions & 0 deletions src/GlobalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Config} from './guild/Config';
import {FileUtils} from './utils/FileUtils';
import {DefaultConfig} from './guild/DefaultConfig';
import {Directory} from './Directory';
import {Utils} from './utils/Utils';

const guildContexts: Map<string, GuildContext> = new Map();
const client = new Client();
Expand Down Expand Up @@ -43,9 +44,11 @@ export namespace GlobalContext {

export function getMemoryUsage(): number[] {
return [
process.memoryUsage().heapUsed / 1000000,
process.memoryUsage().heapTotal / 1000000,
process.memoryUsage().external / 1000000,
process.memoryUsage().rss / 1000000,
process.memoryUsage().arrayBuffers,
];
}

Expand All @@ -59,4 +62,8 @@ export namespace GlobalContext {
}
return version;
}

export function getUptime(): string {
return Utils.convertSecondsToTimeString(process.uptime());
}
}
11 changes: 7 additions & 4 deletions src/commands/debug/Status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,19 @@ function getStatusResponse(context: GuildContext): string {
tableData3.push([GuildUtils.parseUserFromUserID(context, userID)!.username]);
});
response += `${TableGenerator.createTable(tableHeader3, tableData3)}\n`;
response += `Version: ${GlobalContext.getBotVersion()}`;
response += `Version: ${GlobalContext.getBotVersion()}\n`;
response += `Uptime: ${GlobalContext.getUptime()}`;
return response;
}

function getMemoryResponse(): string {
const tableHeaders = ['Type', 'Allocated (MBs)'];
const tableData: string[][] = [];
const memory = GlobalContext.getMemoryUsage();
tableData.push(['heapTotal', memory[0].toString()]);
tableData.push(['external', memory[1].toString()]);
tableData.push(['rss', memory[2].toString()]);
tableData.push(['heapUsed', memory[0].toString()]);
tableData.push(['heapTotal', memory[1].toString()]);
tableData.push(['external', memory[2].toString()]);
tableData.push(['rss', memory[3].toString()]);
tableData.push(['arrayBuffers', memory[4].toString()]);
return TableGenerator.createTable(tableHeaders, tableData);
}
8 changes: 7 additions & 1 deletion src/voice/ConnectionHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {CachingStream} from '../utils/CachingStream';
import SilenceInsertionHandler from './SilenceInsertionHandler';
import SilenceDetectingStream from '../utils/SilenceDetectingStream';
import {AudioUtils} from '../utils/AudioUtils';
import { Transform } from 'stream';

const USER_REJOIN_THRESHOLD = 15000;
const NO_USER_TIMEOUT = 60 * 1000;
Expand All @@ -24,6 +25,7 @@ export default class VoiceConnectionHandler {
private readonly isListeningToCommand: Map<string, boolean> = new Map();
private noUsersInVoiceChannelTimeout: NodeJS.Timeout | undefined;
private userJoinedFromBrowser: Set<string> = new Set();
private readonly opusDecoderStreamReferences: Map<string, Transform> = new Map();

private readonly silenceInsertionHandler = new SilenceInsertionHandler(this.voiceStreams);
private readonly mergeStream: MergingStream = new MergingStream(this.voiceStreams);
Expand Down Expand Up @@ -177,6 +179,8 @@ export default class VoiceConnectionHandler {
const timeout = setTimeout(() => {
Logger.i(VoiceConnectionHandler.name, `Removing ${user.tag} [${user.id}]`, this.context);
this.voiceStreams.delete(user.id);
this.opusDecoderStreamReferences.get(user.id)?.destroy();
this.opusDecoderStreamReferences.delete(user.id);
this.removedTimeouts.delete(user.id);
}, USER_REJOIN_THRESHOLD);
this.removedTimeouts.set(user.id, timeout);
Expand All @@ -197,9 +201,11 @@ export default class VoiceConnectionHandler {
console.log(`Error when decoding stream, unable to listen to ${user.tag}, Error: ${err}`);
opusStream.removeAllListeners();
opusStream.destroy();
decodedAudioStream.destroy();
this.userJoinedFromBrowser.add(user.id);
return;
});
this.opusDecoderStreamReferences.set(user.id, decodedAudioStream);

// Currently Broken, Need to manually decode in order to catch exception
// const audio = connection.receiver.createStream(user, {
Expand All @@ -208,7 +214,7 @@ export default class VoiceConnectionHandler {
// });
const previousStream = this.voiceStreams.get(user.id);
const recorderStream = previousStream || new RecordingStream(true);
opusStream.pipe(recorderStream, {end: false});
decodedAudioStream.pipe(recorderStream, {end: false});
this.voiceStreams.set(user.id, recorderStream);
const speechRecognizer = this.context.getVoiceDependencyProvider().getSpeechRecognizer();
const hotwordEngine = this.context.getVoiceDependencyProvider().getHotwordEngine();
Expand Down

0 comments on commit 118f719

Please sign in to comment.