Skip to content

Commit

Permalink
Simplify timing code (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
longnguyen2004 authored Jan 29, 2025
1 parent 8348cd3 commit 1e5b918
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/media/BaseMediaStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export class BaseMediaStream extends Writable {
throw new Error("Not implemented");
}
async _write(frame: Packet, _: BufferEncoding, callback: (error?: Error | null) => void) {
const start_write = performance.now();
await this._waitForOtherStream();

const { data, ptshi, pts, durationhi, duration, time_base_num, time_base_den } = frame;
Expand Down Expand Up @@ -114,23 +113,22 @@ export class BaseMediaStream extends Writable {
}, `Frame takes too long to send (${(ratio * 100).toFixed(2)}% frametime)`)
}

const end_write = performance.now();
this._startTime ??= start_write;
this._startTime ??= start_sendFrame;
this._startPts ??= this._pts;
if (this._noSleep)
const sleep = Math.max(
0, this._pts - this._startPts + frametime - (end_sendFrame - this._startTime)
);
if (this._noSleep || sleep === 0)
{
callback(null);
}
else
{
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: end_write,
time: end_sendFrame,
startTime: this._startTime,
frametime
}
Expand Down

0 comments on commit 1e5b918

Please sign in to comment.