diff --git a/src/structures/session.ts b/src/structures/session.ts index 505df7e75..86e81d68b 100644 --- a/src/structures/session.ts +++ b/src/structures/session.ts @@ -853,6 +853,8 @@ export default abstract class Session { round.songStartedAt = Date.now(); } + await this.ensureConnectionReady(); + this.connection.play(stream, { inputArgs, encoderArgs: Object.entries(encoderArgs).flatMap((x) => [ @@ -901,13 +903,13 @@ export default abstract class Session { } } + this.stopGuessTimeout(); + if (clipAction === ClipAction.END_ROUND) { // The end round clip doesn't deal with round state, it just plays and ends return; } - this.stopGuessTimeout(); - if (this.isGameSession() && this.isClipMode()) { const clipGameRound = round as ClipGameRound; if (!round.finished) { @@ -1319,4 +1321,28 @@ export default abstract class Session { ); }); } + + private async ensureConnectionReady(): Promise { + if (!this.connection) { + throw new Error( + "Connection is unexpectedly null in ensureConnectionReady", + ); + } + + if (!this.connection.piper.encoding) { + return; + } + + logger.warn("Connection is unexpectedly in encoding state."); + await delay(1000); + + // if still encoding, force stop + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (this.connection.piper.encoding) { + logger.warn( + "Connection is still in encoding state after timeout, force stop.", + ); + this.connection.stopPlaying(); + } + } }