can we send audio in opus codec to livekit??
# ask-ai
s
can we send audio in opus codec to livekit??
t
Thanks for reaching out, Rahul. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely! LiveKit does support the Opus codec for audio. You can definitely send audio using Opus, and it works smoothly across all our SDKs, servers, and tools. If you’d like, I’m here to help with any details or guide you through the implementation—just let me know! Sources: Sources/LiveKit/Protos/livekit_models.pb.swift | Track egress | LiveKit docs | pkg/rtc/wrappedreceiver.go | livekit-protocol/livekit/protocol/models.py | lib/livekit/proto/livekit_models_pb.rb
s
show me a sample code in jabvascript
t
Here is a sample JavaScript code snippet to send Opus audio to LiveKit:
Copy code
import { Room, LocalAudioTrack, AudioSource, AudioFrame } from '@livekit/rtc-node';
import { readFileSync } from 'fs';

// Connect to a LiveKit room
const room = new Room();
await room.connect('wss://your-livekit-server-url', 'your-join-token');

// Set up audio source and track
const sampleRate = 48000; // Opus commonly uses 48kHz
const channels = 1;
const source = new AudioSource(sampleRate, channels);
const track = LocalAudioTrack.createAudioTrack('audio', source);
await room.localParticipant.publishTrack(track);

// Read and send audio data (PCM format)
const audioData = readFileSync('/path/to/audio.pcm'); // Raw PCM Int16 16-bit samples
const buffer = new Int16Array(audioData.buffer);
await source.captureFrame(new AudioFrame(buffer, sampleRate, channels, buffer.length));

// Cleanup when done
await track.close();
await room.disconnect();
This code shows you how to connect to a LiveKit room, set up an audio source, and send PCM audio data, which LiveKit will automatically encode to Opus for you. If you have any questions or need a hand with anything else, please don’t hesitate to ask—I’m here to help! Sources: packages/livekit-rtc/README.md | pkg/sip/room.go | opus/opus.go | livekit-agents/livekit/agents/voice/room_io/_pre_connect_audio.py
s
no the client need to send opus audio
t
Thanks for reaching out! I wanted to share that LiveKit doesn’t currently support publishing pre-encoded Opus audio directly from the browser using the JavaScript SDK. But don’t worry, there are a few ways you can approach this: • Node.js Applications: You can use the LiveKit server SDK to publish pre-encoded Opus files, which works great if you’re working on the server side. • Custom Byte Stream: Another option is to create a custom solution to send Opus-encoded byte streams along with the right metadata (like setting the MIME type to
audio/opus
). • FFmpeg Integration: You might also consider using FFmpeg to encode your audio to Opus and then publish it to LiveKit via the CLI—this can be handy for testing or development. If any of these sound like something you’d like to explore further, just let me know! I’m here to help you through it. Sources: README.md | livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/stt.py | README.md | Track egress | LiveKit docs | WhatsApp Integration with LiveKit
s
so using Custom Byte Stream we can directly stream opus audio to room from a user??
t
Absolutely, LiveKit can definitely accept custom byte streams with Opus audio directly from you. Here’s how you can make that happen: • Send the Opus-encoded audio as a byte stream, making sure to include important metadata like
sampleRate
,
channels
, and the correct MIME type (for example,
audio/opus
). • Use helpful tools such as the LiveKit Go SDK or the GStreamer Publisher to programmatically send your Opus stream into a LiveKit room. • Just keep in mind that the stream should be raw Opus data—usually wrapped in an Ogg container—and not mixed into a video container format. If you’d like, I’m here to guide you through the implementation or answer any questions you might have! Sources: tests/change-sophie.opus | README.md | pkg/provider/opuslooper.go | README.md | Track egress | LiveKit docs
s
ok give a sample code in javascript
t
Here is a sample JavaScript code snippet to send a custom Opus byte stream to LiveKit:
Copy code
import { Room } from '@livekit/rtc-node';

// Connect to the room
const room = new Room();
await room.connect('wss://your-livekit-server-url', 'your-join-token');

const localParticipant = room.localParticipant;

// Open a byte stream writer
const writer = await localParticipant.streamBytes({
  name: 'custom-opus-stream',
  topic: 'audio/opus', // Custom topic to identify the stream
  mimeType: 'audio/opus', // Indicate Opus format
});

// Simulate sending Opus packets (replace this with actual Opus data source)
const opusPackets = getOpusPacketStream(); // Your Opus data source

for (const packet of opusPackets) {
  await writer.write(packet); // Send each Opus frame
}

// Close the stream when done
await writer.close();
Just a heads-up: you’ll want to replace
getOpusPacketStream()
with your actual Opus encoder output or file reader. If you run into any questions or need a hand with this, please don’t hesitate to reach out—I’m here to help! Sources: livekit-agents/livekit/agents/voice/room_io/_pre_connect_audio.py | Track egress | LiveKit docs | Sending files & bytes | LiveKit docs
s
but agent stt will understand it??
t
Thanks for reaching out! I wanted to share that the LiveKit agent STT doesn’t directly handle Opus audio streams. It actually needs the audio in PCM format (signed 16-bit little-endian) to work properly for transcription. So, if you’re working with Opus streams, you’ll want to decode them into PCM format first. Good news is, LiveKit offers an
AudioStreamDecoder
utility that can help with this conversion. It takes your Opus audio and turns it into the PCM format that STT requires. If you’d like, I’m here to walk you through how to set this up—just let me know! Sources: livekit-agents/livekit/agents/voice/agent_session.py | AI Agents Framework | livekit/livekit_composite | DeepWiki