Skip to content

Commit

Permalink
Rearrange stuff to remove reinit hack
Browse files Browse the repository at this point in the history
  • Loading branch information
longnguyen2004 committed Jan 24, 2025
1 parent b5f6534 commit 0716acf
Showing 1 changed file with 17 additions and 31 deletions.
48 changes: 17 additions & 31 deletions src/media/BaseMediaStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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: {
Expand All @@ -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
}
Expand Down

0 comments on commit 0716acf

Please sign in to comment.