Skip to content

Commit

Permalink
Merge pull request #69 from longnguyen2004/nosleep
Browse files Browse the repository at this point in the history
  • Loading branch information
dank074 authored Feb 28, 2024
2 parents 3094aba + dfb0ba5 commit 8510166
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/media/AudioStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ class AudioStream extends Writable {
public count: number;
public sleepTime: number;
public startTime?: number;

constructor(udp: MediaUdp) {
private noSleep: boolean;

constructor(udp: MediaUdp, noSleep = false) {
super();
this.udp = udp;
this.count = 0;
this.sleepTime = 20;
this.noSleep = noSleep;
}

_write(chunk: any, _: BufferEncoding, callback: (error?: Error | null) => void) {
Expand All @@ -22,9 +24,17 @@ class AudioStream extends Writable {
this.udp.sendAudioFrame(chunk);

const next = ((this.count + 1) * this.sleepTime) - (performance.now() - this.startTime);
setTimeout(() => {

if (this.noSleep)
{
callback();
}, next);
}
else
{
setTimeout(() => {
callback();
}, next);
}
}
}

Expand Down
17 changes: 13 additions & 4 deletions src/media/VideoStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export class VideoStream extends Writable {
public count: number;
public sleepTime: number;
public startTime?: number;

constructor(udp: MediaUdp, fps: number = 30) {
private noSleep: boolean;

constructor(udp: MediaUdp, fps: number = 30, noSleep = false) {
super();
this.udp = udp;
this.count = 0;
this.sleepTime = 1000 / fps;
this.noSleep = noSleep;
}

public setSleepTime(time: number) {
Expand All @@ -27,8 +29,15 @@ export class VideoStream extends Writable {

const next = ( (this.count + 1) * this.sleepTime) - (performance.now() - this.startTime);

setTimeout(() => {
if (this.noSleep)
{
callback();
}, next);
}
else
{
setTimeout(() => {
callback();
}, next);
}
}
}

0 comments on commit 8510166

Please sign in to comment.