Hey, In rtc-node, how does one work with `audioSo...
# agents
r
Hey, In rtc-node, how does one work with
audioSource.waitForPlayout()
? I have the following example code to demonstrate the issue I am facing:
Copy code
// capture many frames ~100
await audioSource.waitForPlayout();
audioSource.clearQueue();
Since I am awaiting playout first, I would expect that
audioSource.clearQueue();
does nothing - however the
waitForPlayout
promise resolve before audio has fully played out in the livekit playground (1-2 seconds early).
audioSource.clearQueue();
stops playout on the client instantly resulting in the last few seconds of audio getting lost. How can I wait for audio to finish playing in the playground.
l
I don't think
waitForPlayout()
accounts for network latency, or audio buffering at the client end. You might need to add a couple of seconds buffer before calling
clearQueue()
or perhaps use https://docs.livekit.io/reference/python/v1/livekit/rtc/audio_source.html#livekit.rtc.audio_source.AudioSource.queued_duration to check if there is still queued audio.
r
Hey @lively-winter-88668, I have gotten somewhat closer to the root cause. In the code I shared above I did not share something because I thought it was irrelevant but having debugged further, it seems like it is the cause of the issue. Earlier in my code I call
audioSource.clearQueue();
before capturing the next set of audio frames (to make sure nothing else is being played out). It seems like this initial clearing of the queue, results in the
waitForPlayout()
not working properly.
Copy code
audioSource.clearQueue(); <--- this lines is causing the issue

// capture around 100 frames
audioSource.captureFrame(frame)

await audioSource.waitForPlayout();
console.log("done")
The "done" log gets logged before audio finishes only if I clear the queue before hand. Without the initial clearQueue, the "done" gets logged at the right time. Even if I add a delay (1 second) after clearing the queue to ensure the clear queue request has propagated, the done log still comes too early. How should I be clearing the queue to make it work?
l
I'll be honest, I'm a bit out of my depth with using audio capture within an Agent. I asked DeepWiki (here, then scroll all the way to the bottom) and it suggested a race condition, with a plausible explanation... it also suggested https://github.com/livekit/agents-js/blob/main/agents/src/voice/room_io/_output.ts as an example implementation. I hope that helps, if not, then I'd need to be able to reproduce this in a minimal example.