Subject: Help with Custom Audio Publishing in `@li...
# server-api
a
Subject: Help with Custom Audio Publishing in
@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):
Copy code
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
}.
r
You want to make your own and not use the Agent library?
a
I started with the agent library but for nodejs and I kept running to configuration issues. I wish I documented it so I could update you guys… anyways I decided to move to orchestrating myself… the audioSource is throwing an error please could you ask your team to check it? What’s the function available for sending raw data streams in your current implementation? I could try making changes as I would want to see it work and raise a PR for that??
r
Your best bet is to use the Python SDK for that.
a
For the orchestration part manually? Has anyone implemented it using node.js? There might be some examples of their approach?
@refined-appointment-81829 thanks for the prompt response. will go ahead and use the python SDK
👍 1