Skip to content

Commit

Permalink
fix 50ms and update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nbsp committed Sep 19, 2024
1 parent 7f01628 commit 0e7a59c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions examples/publish-wav/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ const track = LocalAudioTrack.createAudioTrack('audio', source);
const options = new TrackPublishOptions();
const buffer = new Int16Array(sample.buffer);
options.source = TrackSource.SOURCE_MICROPHONE;
await room.localParticipant.publishTrack(track, options);
await new Promise((resolve) => setTimeout(resolve, 1000)); // wait a bit so the start doesn't cut off
await room.localParticipant.publishTrack(track, options).then((pub) => pub.waitForSubscription());

let written = 44; // start of WAVE data stream
const FRAME_DURATION = 1; // write 1s of audio at a time
const numSamples = sampleRate / FRAME_DURATION;
const numSamples = sampleRate * FRAME_DURATION;
while (written < dataSize) {
const available = dataSize - written;
const frameSize = Math.min(numSamples, available);
Expand All @@ -61,6 +60,7 @@ while (written < dataSize) {
Math.trunc(frameSize / channels),
);
await source.captureFrame(frame);
await source.waitForPlayout();

written += frameSize;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/livekit-rtc/src/audio_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export class AudioSource {
this.queueSize = queueSize;

this.lastCapture = 0;
this.currentQueueSize = 0;

// remove 50ms to account for processing time
// (e.g. using wait_for_playout for very small chunks)
this.currentQueueSize = -50;

const req = new NewAudioSourceRequest({
type: AudioSourceType.AUDIO_SOURCE_NATIVE,
Expand Down Expand Up @@ -97,8 +100,6 @@ export class AudioSource {
const elapsed = this.lastCapture === 0 ? 0 : now - this.lastCapture;
this.currentQueueSize += (frame.samplesPerChannel / frame.sampleRate - elapsed) * 1000;

// remove 50ms to account for processing time (e.g. using wait_for_playout for very small chunks)
this.currentQueueSize -= 50;
this.lastCapture = now;

if (this.timeout) {
Expand Down

0 comments on commit 0e7a59c

Please sign in to comment.