diff --git a/examples/publish-wav/index.ts b/examples/publish-wav/index.ts index 32af721a..3db75df4 100644 --- a/examples/publish-wav/index.ts +++ b/examples/publish-wav/index.ts @@ -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); @@ -61,6 +60,7 @@ while (written < dataSize) { Math.trunc(frameSize / channels), ); await source.captureFrame(frame); + await source.waitForPlayout(); written += frameSize; } diff --git a/packages/livekit-rtc/src/audio_source.ts b/packages/livekit-rtc/src/audio_source.ts index e66481d5..33224f22 100644 --- a/packages/livekit-rtc/src/audio_source.ts +++ b/packages/livekit-rtc/src/audio_source.ts @@ -97,14 +97,15 @@ 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) { clearTimeout(this.timeout); } - this.timeout = setTimeout(this.releaseQueue.put, this.currentQueueSize); + + // remove 50ms to account for processing time + // (e.g. using wait_for_playout for very small chunks) + this.timeout = setTimeout(this.releaseQueue.put, this.currentQueueSize - 50); const req = new CaptureAudioFrameRequest({ sourceHandle: this.ffiHandle.handle,