WebRTC Integration
Learn how to stream bitHuman avatars to LiveKit rooms using WebRTC
Streaming Avatars with WebRTC
This guide explains how to integrate bitHuman avatars with WebRTC using LiveKit. This integration enables you to stream interactive avatars to multiple remote viewers in real-time while controlling the avatar’s speech and behavior.
Introduction
WebRTC integration allows you to:
- Stream high-quality avatar animations to multiple viewers simultaneously
- Control avatar speech and expressions in real-time
- Create interactive experiences for virtual events, customer service, education, and more
Architecture Overview
The integration consists of three main components working together:
- bitHuman Runtime: Generates the avatar animations and audio
- LiveKit Server: Handles WebRTC streaming and room management
- Client Applications: View and interact with the avatar
Data Flow
The process works as follows:
- The bitHuman server initializes the runtime with your avatar model
- The server connects to a LiveKit room and publishes video and audio tracks
- A WebSocket server accepts audio input to control the avatar
- Audio data is processed by the bitHuman runtime to generate animations
- The animated avatar is streamed to the LiveKit room in real-time
- Viewers connect to the LiveKit room to see and interact with the avatar
Prerequisites
Before you begin, ensure you have:
- Python 3.10 or higher
- bitHuman Runtime API token
- LiveKit server (cloud or self-hosted)
- LiveKit API key and secret
Implementation Guide
Step 1: Install Dependencies
First, install the required Python packages:
Step 2: Set Up Environment Variables
Create a .env
file with your API credentials:
Step 3: Create the bitHuman Server
The bitHuman server (download example) handles:
- Initializing the bitHuman runtime
- Connecting to LiveKit
- Publishing video and audio tracks
- Accepting audio input via WebSocket
Key components of the server:
Step 4: Create the WebSocket Client
The WebSocket client (download example) sends audio data to control the avatar:
Step 5: Run the Server
Start the bitHuman server with your avatar model and room name:
Command-line options:
Option | Description | Default |
---|---|---|
--avatar-model | bitHuman avatar model to use | (required) |
--room | LiveKit room name | (required) |
--token | bitHuman runtime token | From .env |
--livekit-url | LiveKit server URL | From .env |
--api-key | LiveKit API key | From .env |
--api-secret | LiveKit API secret | From .env |
--identity | Identity in LiveKit room | ”bithuman-avatar” |
--ws-port | WebSocket server port | 8765 |
Step 6: Control the Avatar
Use the WebSocket client to send audio to the avatar:
Client options:
Option | Description | Default |
---|---|---|
--ws-url | WebSocket server URL | ”ws://localhost:8765” |
--sample-rate | Target sample rate for audio | 16000 |
--chunk-size | Size of audio chunks in milliseconds | 100 |
Step 7: View the Avatar
To view the avatar, you can:
- Use the LiveKit Playground
- Create a custom web client using LiveKit Web SDK
- Build a mobile app with LiveKit iOS/Android SDKs
Advanced Configuration
WebSocket Protocol
The WebSocket server accepts:
- Binary data: Raw audio bytes (16kHz, mono, int16 format)
- JSON commands:
LiveKit Room Settings
Customize video quality by modifying the TrackPublishOptions
:
Using LiveKit Data Streams Instead of WebSockets
For a more integrated approach, you can use LiveKit’s Data Streams feature instead of a separate WebSocket server:
Learn more about LiveKit Data Streams and Remote Method Calls.
Troubleshooting
Common Issues
Issue | Solution |
---|---|
Connection errors | Verify your LiveKit server URL is correct and accessible |
Authentication errors | Check your API key and secret |
Audio not playing | Ensure audio files are in a supported format (WAV recommended) |
WebSocket connection issues | Check if the WebSocket port is blocked by a firewall |
Logging
Both scripts use the loguru
library for logging. Adjust the log level for more detailed information: