From 0716acf73c1444ea4cf024185c037eacd919e631 Mon Sep 17 00:00:00 2001 From: Long Nguyen Date: Sat, 25 Jan 2025 01:47:17 +0700 Subject: [PATCH] Rearrange stuff to remove reinit hack --- src/media/BaseMediaStream.ts | 48 +++++++++++++----------------------- 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/src/media/BaseMediaStream.ts b/src/media/BaseMediaStream.ts index 85570ec..cb5e014 100644 --- a/src/media/BaseMediaStream.ts +++ b/src/media/BaseMediaStream.ts @@ -12,7 +12,6 @@ export class BaseMediaStream extends Writable { private _loggerSleep: Log; private _noSleep: boolean; - private _reinitSleep = false; private _startTime?: number; private _startPts?: number; private _sync = true; @@ -40,22 +39,9 @@ export class BaseMediaStream extends Writable { return this._noSleep; } set noSleep(val: boolean) { - if (val) - { - this._noSleep = true; - this._reinitSleep = false; - } - else - { - /** - * If _noSleep is set to true while _write() is still executing - * (for example when waiting on _waitForOtherStream()), it can - * cause problems with the timeout calculation code below. So - * instead of reinit-ing the sleep mechanism here, we let _write() - * do it instead. - */ - this._reinitSleep = true; - } + this._noSleep = val; + if (!val) + this._startPts = this._startTime = undefined; } get pts(): number | undefined { return this._pts; @@ -94,30 +80,24 @@ export class BaseMediaStream extends Writable { throw new Error("Not implemented"); } async _write(frame: Packet, _: BufferEncoding, callback: (error?: Error | null) => void) { - // See above on why this is here - if (this._reinitSleep) - { - this._noSleep = this._reinitSleep = false; - this._startPts = this._startTime = undefined; - } - if (this._startTime === undefined) - this._startTime = performance.now(); + const start_write = performance.now(); await this._waitForOtherStream(); const { data, ptshi, pts, durationhi, duration, time_base_num, time_base_den } = frame; // biome-ignore lint/style/noNonNullAssertion: this will never happen with our media stream const frametime = combineLoHi(durationhi!, duration!) / time_base_den! * time_base_num! * 1000; - const start = performance.now(); + const start_sendFrame = performance.now(); await this._sendFrame(Buffer.from(data), frametime); - const end = performance.now(); + const end_sendFrame = performance.now(); + // biome-ignore lint/style/noNonNullAssertion: this will never happen with our media stream this._pts = combineLoHi(ptshi!, pts!) / time_base_den! * time_base_num! * 1000; if (this._startPts === undefined) this._startPts = this._pts; this.emit("pts", this._pts); - const sendTime = end - start; + const sendTime = end_sendFrame - start_sendFrame; const ratio = sendTime / frametime; this._loggerSend.debug({ stats: { @@ -135,19 +115,25 @@ export class BaseMediaStream extends Writable { frametime }, `Frame takes too long to send (${(ratio * 100).toFixed(2)}% frametime)`) } - const now = performance.now(); + + const end_write = performance.now(); + if (this._startTime === undefined) + this._startTime = start_write; + if (this._noSleep) { callback(null); } else { - const sleep = Math.max(0, this._pts - this._startPts + frametime - (now - this._startTime)); + const sleep = Math.max( + 0, this._pts - this._startPts + frametime - (end_write - this._startTime) + ); this._loggerSleep.debug({ stats: { pts: this._pts, startPts: this._startPts, - time: now, + time: end_write, startTime: this._startTime, frametime }