adorable-hairdresser-72064
04/11/2025, 3:05 PM@livekit/rtc-node v0.13.10
Message:
Hi LiveKit team and community!
We're building a server-side agent using @livekit/rtc-node (v0.13.10) and need to publish custom audio (generated TTS) from our Node.js application into a room.
We're following the pattern of creating a custom AudioSource and then linking it to a LocalAudioTrack for publishing. However, we're encountering issues during the initialization phase.
Problem:
When we try to create the LocalAudioTrack (using either new LocalAudioTrack(name, source) or the static LocalAudioTrack.createAudioTrack(name, source) method) immediately after creating the AudioSource instance (new AudioSource(rate, channels)), we get a TypeError: Cannot read properties of undefined (reading 'id') originating from the base Track constructor.
Analysis:
Our understanding is that the AudioSource constructor might be returning before the underlying asynchronous FFI call to create the native source (and get its handle ID) has completed. This leads to the AudioSource instance having an uninitialized ffiHandle when it's passed to the LocalAudioTrack constructor/factory, causing the TypeError.
Question:
What is the correct and recommended way in @livekit/rtc-node v0.13.10 to reliably:
1. Create a custom AudioSource.
2. Ensure its underlying native handle/resources are fully initialized.
3. Create a LocalAudioTrack linked to this initialized AudioSource.
4. Publish the LocalAudioTrack.
Is there an asynchronous initialization pattern required for AudioSource (like a static factory method or an init() call), or a specific high-level function we should be using for publishing custom audio sources?
We've looked at the examples showing LocalAudioTrack.createAudioTrack(name, source), but it seems to hit this timing issue with the AudioSource constructor in our setup.
Any guidance or examples for this specific version (v0.13.10) would be greatly appreciated! Thanks!
Code Snippet (Illustrative):
import { AudioSource, LocalAudioTrack, TrackSource, /* ... */ } from "@livekit/rtc-node"; // v0.13.10
// Inside async function after room connection
try {
// 1. Create AudioSource (using the current constructor)
const audioSource = new AudioSource(16000, 1);
// PROBLEM: We suspect audioSource.ffiHandle.id is not ready yet
// 2. Attempt to create track (fails here)
const audioTrack = LocalAudioTrack.createAudioTrack('bot-mic', audioSource);
// 3. Publish (never reached)
// await room.localParticipant.publishTrack(audioTrack, { /* ... */ });
} catch (error) {
console.error("Error:", error); // Logs the TypeError
}.refined-appointment-81829
04/11/2025, 5:36 PMadorable-hairdresser-72064
04/12/2025, 1:54 AMrefined-appointment-81829
04/12/2025, 2:03 AMadorable-hairdresser-72064
04/12/2025, 2:30 AMadorable-hairdresser-72064
04/12/2025, 4:43 AM