feat: introduce livekit-a2a-relay and video optimizations - #1199
Closed
mjpvl-ai wants to merge 5 commits into
Closed
feat: introduce livekit-a2a-relay and video optimizations#1199mjpvl-ai wants to merge 5 commits into
mjpvl-ai wants to merge 5 commits into
Conversation
mjpvl-ai
requested review from
1egoman,
MaxHeimbrock,
alan-george-lk,
cloudwebrtc,
ladvoc,
lukasIO,
pblazej,
stephen-derosa and
xianshijing-lk
as code owners
June 29, 2026 19:15
1egoman
removed their request for review
June 29, 2026 19:27
Contributor
|
Thanks for taking the time to put this together. After reviewing the PR, we’re going to close it in its current form. The proposed changes are quite large in scope and include several breaking changes. Given the direction of the work, we think the crate you added would be better maintained as its own open-source project. If there are smaller changes needed here to better support that project, we’d be happy to review those as separate, well-scoped PRs. In general, smaller PRs that focus on one specific bug fixes or enhancements are much easier for us to review and merge. If you’d like to discuss further, our developer community is a good place to continue the conversation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🎭 Introduce
livekit-a2a-relay: Bridging LiveKit WebRTC and the A2A ProtocolThis Pull Request introduces the
livekit-a2a-relaycrate: a high-performance, drop-in, ultra-low latency bridging layer that seamlessly connects LiveKit real-time WebRTC audio tracks to any Agent-to-Agent (A2A) compliant HTTP/SSE endpoint.By decoupling real-time audio transport and conversation flow control, this package enables developers to focus purely on agent intelligence without worrying about the underlying WebRTC FFI thread boundaries, clock drift, or interruption logic.
🏗️ Architecture & Data Flow
The crate implements a thread-isolated Actor model to ensure that high-frequency WebRTC audio I/O is never blocked by async HTTP requests or heavy processing tasks (like Whisper STT or Piper TTS).
flowchart TD subgraph LK ["LiveKit WebRTC Space"] Room["LiveKit Room"] Track["LocalAudioTrack"] Source["NativeAudioSource"] end subgraph ActorCore ["RelayActor Event Loop (Tokio Task)"] Actor["RelayActor Loop"] VAD["EnergyVad (VAD)"] Jitter["AudioJitterBuffer (Ring Buffer)"] end subgraph A2A ["A2A Protocol & Transport"] Turn["TurnManager (Atomic Counter)"] Client["OfficialA2aClient / Custom A2aClient"] end subgraph Backend ["Remote or Local A2A Agent"] Agent["A2A Mock Agent (Axum)"] end %% Audio Ingestion & VAD Flow Room -->|Incoming PCM| Actor Actor -->|Process Samples| VAD VAD -->|Speech Detected - Interruption| Turn Turn -->|Advance Turn ID| Actor Actor -->|Cancel Active Turn| Client Actor -->|Accumulated PCM| Client %% Audio Outflow & Playback Flow Client <-->|POST /message:stream - SSE| Agent Agent -->|Audio Chunks| Client Client -->|Filter by Turn ID| Actor Actor -->|Push Valid Frames| Jitter Jitter -->|Pop 10ms ticks + Comfort Noise| Actor Actor -->|Write Frame| Source Source -->|Publish Audio| Track Track --> Room🛠️ Key Components & Responsibilities
RelayActortokio::select!for predictable event prioritization.TurnManagerAtomicU64indices. Thread-safe and lock-free.AudioJitterBufferVecDequering-buffer. Supports configurable target depth and hard ceiling.EnergyVadA2aClient💫 Turn Lifecycle & Interruption Handling
One of the most complex challenges in real-time conversational AI is user interruption. The diagram below demonstrates how the
TurnManagerandRelayActorwork together to instantly halt agent output:sequenceDiagram autonumber actor User as User (WebRTC) participant Relay as RelayActor participant Turn as TurnManager participant Client as A2aClient (Official) participant Agent as A2A Agent Note over Agent: Speaking (Turn N) Agent->>Client: Send SSE Audio chunk (Turn N) Client->>Relay: Forward audio chunk (Turn N) Relay->>Relay: Push to JitterBuffer & play User->>Relay: Speaks (Interrupts Agent) Relay->>Relay: Run EnergyVad -> Active! Note over Relay,Turn: Interruption Sequence Started Relay->>Turn: Bump turn index (next_turn) Turn-->>Relay: Current Turn = N + 1 Relay->>Relay: Clear NativeAudioSource playback buffer Relay->>Relay: Clear AudioJitterBuffer Relay->>Client: cancel_turn(Turn N) Client->>Agent: HTTP POST Cancel/Reset (Turn N) Note over Agent: Stop generating/sending Turn N Agent->>Client: Late/Stale Audio chunk (Turn N) Client->>Relay: Forward audio chunk (Turn N) Relay->>Turn: is_valid(Turn N)? Turn-->>Relay: False (Current is N + 1) Note over Relay: 🚫 Discard stale frame immediately (No sound plays)🏃 Testing & Local ONNX Verification
To verify that the system runs flawlessly under local hardware constraints, a complete STT (Whisper) and TTS (Piper) pipeline was tested end-to-end:
📥 1. Pre-trained Model Fetching
Models are fetched and stored locally in the workspace directory using the provided setup script:
# Fetch Whisper small ASR model and Piper medium TTS model ./scripts/download_onnx_models.sh --stt small --tts medium🤖 2. Launching the A2A Mock Agent
The lightweight A2A compliant Axum mock agent runs locally on port
8000:🎭 3. Running the A2A WebRTC Relay
The relay example connects to a local LiveKit server (dev mode on
:7880) and bridges incoming audio to the mock text agent using local ONNX STT/TTS:🧪 4. Automated E2E Pipeline Validation
Running the automated test pipeline confirms complete protocol adherence:
Test Execution Output:
🔒 Code Quality & Compliance
unsafeBlocks: The entire crate uses 100% safe Rust.cargo fmt.