from livekit is it possible to get audio in opus c...
# ask-ai
s
from livekit is it possible to get audio in opus codec??
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 fully supports audio in the Opus codec. It’s actually one of the main audio codecs we use to make sure you get high-quality, low-latency sound. If you have any specific questions or want to know more about how it works, please don’t hesitate to ask—I’m here to help! Sources: Sources/LiveKit/Protos/livekit_models.pb.swift | livekit-protocol/livekit/protocol/models.py | lib/livekit/proto/livekit_models_pb.rb | pkg/rtc/wrappedreceiver.go | Track egress | LiveKit Docs
s
can you give me a sample code??
t
I'd love to share a helpful example with you on how to publish and subscribe to Opus audio tracks using the LiveKit Go SDK. Here's a sample code snippet that walks you through the process:
Copy code
package main

import (
    "github.com/livekit/server-sdk-go/v2"
    "github.com/livekit/server-sdk-go/v2/pkg/media"
    "github.com/pion/webrtc/v4"
)

func main() {
    // Connect to the room
    room, err := lksdk.ConnectToRoom("your-host", lksdk.ConnectInfo{
        APIKey:    "your-api-key",
        APISecret: "your-api-secret",
        RoomName:  "your-room-name",
        ParticipantIdentity: "your-identity",
    }, nil)
    if err != nil {
        panic(err)
    }

    // Publish Opus audio
    publishTrack, err := media.NewPCMLocalTrack(media.DefaultOpusSampleRate, 1, nil)
    if err != nil {
        panic(err)
    }
    defer publishTrack.Close()

    if _, err = room.LocalParticipant.PublishTrack(publishTrack, &lksdk.TrackPublicationOptions{
        Name: "OpusAudio",
    }); err != nil {
        panic(err)
    }

    // Subscribe to Opus audio
    room.Callbacks.OnTrackSubscribed = func(track *webrtc.TrackRemote, publication *lksdk.RemoteTrackPublication, participant *lksdk.RemoteParticipant) {
        if track.Codec().MimeType == webrtc.MimeTypeOpus {
            // Handle Opus track
            fmt.Println("Subscribed to Opus track")
        }
    }
}
This example guides you through connecting to a room, publishing an Opus audio track, and subscribing to those tracks. If you have any questions or would like me to walk you through more examples, please don’t hesitate to ask—I’m here to help! Sources: opus/opus.go | README.md | README.md