|
| 1 | +# LiveKit Python SDK Examples |
| 2 | + |
| 3 | +This directory contains examples demonstrating various features of the LiveKit Python SDK. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Set the following environment variables: |
| 8 | + |
| 9 | +```bash |
| 10 | +export LIVEKIT_URL=ws://localhost:7880 |
| 11 | +export LIVEKIT_API_KEY=devkey |
| 12 | +export LIVEKIT_API_SECRET=secret |
| 13 | +``` |
| 14 | + |
| 15 | +## Examples Overview |
| 16 | + |
| 17 | +| Example | Description | |
| 18 | +|---------|-------------| |
| 19 | +| [basic_room.py](#basic_roompy) | Room connection with PlatformAudio and synthetic audio modes | |
| 20 | +| [room_example.py](#room_examplepy) | Basic room connection and event handling | |
| 21 | +| [api.py](#apipy) | Room management via LiveKit API | |
| 22 | +| [e2ee.py](#e2eepy) | End-to-end encryption demo | |
| 23 | +| [rpc.py](#rpcpy) | Remote Procedure Call between participants | |
| 24 | +| [multiple_connections.py](#multiple_connectionspy) | Sequential connections for sync frameworks | |
| 25 | +| [participant_attributes.py](#participant_attributespy) | Dynamic participant attributes | |
| 26 | +| [publish_wave.py](#publish_wavepy) | Publish sine wave audio | |
| 27 | +| [publish_hue.py](#publish_huepy) | Publish color-cycling video | |
| 28 | +| [play_audio_stream.py](#play_audio_streampy) | Play received audio with sounddevice | |
| 29 | +| [webhook.py](#webhookpy) | Webhook event handling | |
| 30 | +| [agent_dispatch.py](#agent_dispatchpy) | Manual agent dispatch | |
| 31 | + |
| 32 | +### Subdirectories |
| 33 | + |
| 34 | +| Directory | Description | |
| 35 | +|-----------|-------------| |
| 36 | +| [face_landmark/](face_landmark/) | Facial landmark detection using MediaPipe | |
| 37 | +| [video-stream/](video-stream/) | Video/audio sync with AVSynchronizer | |
| 38 | +| [data_tracks/](data_tracks/) | Data channel examples | |
| 39 | +| [data-streams/](data-streams/) | Data streaming examples | |
| 40 | +| [local_audio/](local_audio/) | Local audio capture examples | |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## basic_room.py |
| 45 | + |
| 46 | +The most comprehensive example, demonstrating room connection with audio capabilities. It showcases two audio capture modes that can be used independently or combined. |
| 47 | + |
| 48 | +### Quick Start |
| 49 | + |
| 50 | +```bash |
| 51 | +# List available audio devices |
| 52 | +python basic_room.py --list-devices |
| 53 | + |
| 54 | +# Connect with PlatformAudio (recommended) |
| 55 | +python basic_room.py --platform-audio --room my-room |
| 56 | + |
| 57 | +# Connect with specific devices |
| 58 | +python basic_room.py --platform-audio --mic-id "device-guid" --speaker-id "device-guid" |
| 59 | + |
| 60 | +# Publish a WAV file |
| 61 | +python basic_room.py --file audio.wav --room my-room |
| 62 | + |
| 63 | +# Mix microphone + WAV file (both modes together) |
| 64 | +python basic_room.py --platform-audio --file background.wav --room my-room |
| 65 | +``` |
| 66 | + |
| 67 | +### Audio Modes |
| 68 | + |
| 69 | +#### PlatformAudio Mode (`--platform-audio`) |
| 70 | + |
| 71 | +Uses WebRTC's Audio Device Module (ADM) for microphone capture. **Recommended for most applications.** |
| 72 | + |
| 73 | +**Features:** |
| 74 | +- Built-in voice processing: |
| 75 | + - **Echo Cancellation (AEC)** - removes echo from speaker playback |
| 76 | + - **Noise Suppression (NS)** - reduces background noise |
| 77 | + - **Auto Gain Control (AGC)** - normalizes audio levels |
| 78 | +- Hardware-accelerated processing on supported platforms (e.g., iOS VPIO) |
| 79 | +- Automatic speaker playout for received audio |
| 80 | +- Device enumeration and selection |
| 81 | +- No external audio libraries required |
| 82 | + |
| 83 | +**Limitations:** |
| 84 | +- No direct access to raw audio frames (ADM sends directly to WebRTC) |
| 85 | +- Cannot apply custom audio processing before publishing |
| 86 | + |
| 87 | +```python |
| 88 | +platform_audio = rtc.PlatformAudio() |
| 89 | +source = platform_audio.create_audio_source( |
| 90 | + rtc.PlatformAudioOptions( |
| 91 | + echo_cancellation=True, |
| 92 | + noise_suppression=True, |
| 93 | + auto_gain_control=True, |
| 94 | + ) |
| 95 | +) |
| 96 | +track = rtc.LocalAudioTrack.create_audio_track("microphone", source) |
| 97 | +``` |
| 98 | + |
| 99 | +#### Synthetic Mode (Default) |
| 100 | + |
| 101 | +Manual control over audio frames via `AudioSource.capture_frame()`. Use this for custom audio processing or programmatic audio generation. |
| 102 | + |
| 103 | +**Features:** |
| 104 | +- Full control over audio data |
| 105 | +- Access to raw audio frames for custom processing |
| 106 | +- Generate synthetic audio (files, TTS, audio synthesis) |
| 107 | +- Apply custom filters, effects, or ML models |
| 108 | + |
| 109 | +**Limitations:** |
| 110 | +- No built-in AEC/NS/AGC - implement yourself or use `AudioProcessingModule` |
| 111 | +- Must handle speaker playout manually via `AudioStream` |
| 112 | +- Requires external audio libraries for mic capture (sounddevice, pyaudio) |
| 113 | + |
| 114 | +```python |
| 115 | +source = rtc.AudioSource(sample_rate=48000, num_channels=1) |
| 116 | +track = rtc.LocalAudioTrack.create_audio_track("audio", source) |
| 117 | + |
| 118 | +frame = rtc.AudioFrame(data=audio_bytes, sample_rate=48000, ...) |
| 119 | +await source.capture_frame(frame) |
| 120 | +``` |
| 121 | + |
| 122 | +#### Mixing Both Modes |
| 123 | + |
| 124 | +PlatformAudio and Synthetic modes can run simultaneously. The `--file` option demonstrates this by publishing a WAV file (synthetic) alongside microphone capture (PlatformAudio). |
| 125 | + |
| 126 | +```bash |
| 127 | +python basic_room.py --platform-audio --file music.wav --room my-room |
| 128 | +``` |
| 129 | + |
| 130 | +This creates two audio tracks: |
| 131 | +1. **Microphone** - via PlatformAudio with voice processing |
| 132 | +2. **File** - via synthetic mode from WAV |
| 133 | + |
| 134 | +Use cases: background music while speaking, sound effects, mixing pre-recorded audio with live input. |
| 135 | + |
| 136 | +### Command Line Options |
| 137 | + |
| 138 | +| Option | Description | |
| 139 | +|--------|-------------| |
| 140 | +| `--list-devices` | List available audio devices and exit | |
| 141 | +| `--platform-audio` | Use PlatformAudio for microphone (recommended) | |
| 142 | +| `--file WAV_PATH` | Publish audio from WAV file (synthetic mode) | |
| 143 | +| `--room NAME` | Room name (default: my-room) | |
| 144 | +| `--mic-id ID` | Select microphone by device ID | |
| 145 | +| `--speaker-id ID` | Select speaker by device ID | |
| 146 | + |
| 147 | +### When to Use Each Mode |
| 148 | + |
| 149 | +| Use Case | Mode | |
| 150 | +|----------|------| |
| 151 | +| Voice/video calls | PlatformAudio | |
| 152 | +| Conferencing apps | PlatformAudio | |
| 153 | +| Playing audio files | Synthetic | |
| 154 | +| Text-to-speech | Synthetic | |
| 155 | +| Custom audio processing | Synthetic | |
| 156 | +| ML audio effects | Synthetic | |
| 157 | +| Background music + voice | Both | |
| 158 | + |
| 159 | +--- |
| 160 | + |
| 161 | +## room_example.py |
| 162 | + |
| 163 | +Basic room connection demonstrating event handling and participant tracking. |
| 164 | + |
| 165 | +```bash |
| 166 | +python room_example.py |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## api.py |
| 172 | + |
| 173 | +Room management using the LiveKit API (create rooms, list rooms). |
| 174 | + |
| 175 | +```bash |
| 176 | +python api.py |
| 177 | +``` |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## e2ee.py |
| 182 | + |
| 183 | +End-to-end encryption with a rotating 3D cube visualization. |
| 184 | + |
| 185 | +```bash |
| 186 | +python e2ee.py |
| 187 | +``` |
| 188 | + |
| 189 | +--- |
| 190 | + |
| 191 | +## rpc.py |
| 192 | + |
| 193 | +Remote Procedure Call (RPC) between participants - greetings, math operations, timeout handling. |
| 194 | + |
| 195 | +```bash |
| 196 | +python rpc.py |
| 197 | +``` |
| 198 | + |
| 199 | +--- |
| 200 | + |
| 201 | +## multiple_connections.py |
| 202 | + |
| 203 | +Sequential room connections in a single thread. Useful for Django/Flask integration. |
| 204 | + |
| 205 | +```bash |
| 206 | +python multiple_connections.py |
| 207 | +``` |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## participant_attributes.py |
| 212 | + |
| 213 | +Set, update, and delete participant attributes dynamically. |
| 214 | + |
| 215 | +```bash |
| 216 | +python participant_attributes.py |
| 217 | +``` |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +## publish_wave.py |
| 222 | + |
| 223 | +Publish a sine wave audio track at a specified frequency. |
| 224 | + |
| 225 | +```bash |
| 226 | +python publish_wave.py |
| 227 | +``` |
| 228 | + |
| 229 | +--- |
| 230 | + |
| 231 | +## publish_hue.py |
| 232 | + |
| 233 | +Publish a color-cycling animated video track. |
| 234 | + |
| 235 | +```bash |
| 236 | +python publish_hue.py |
| 237 | +``` |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +## play_audio_stream.py |
| 242 | + |
| 243 | +Play incoming audio from remote participants using sounddevice. |
| 244 | + |
| 245 | +```bash |
| 246 | +pip install sounddevice |
| 247 | +python play_audio_stream.py |
| 248 | +``` |
| 249 | + |
| 250 | +--- |
| 251 | + |
| 252 | +## webhook.py |
| 253 | + |
| 254 | +Handle LiveKit webhook events using aiohttp. |
| 255 | + |
| 256 | +```bash |
| 257 | +python webhook.py |
| 258 | +``` |
| 259 | + |
| 260 | +--- |
| 261 | + |
| 262 | +## agent_dispatch.py |
| 263 | + |
| 264 | +Manually dispatch agents to rooms (instead of automatic dispatch). |
| 265 | + |
| 266 | +```bash |
| 267 | +python agent_dispatch.py |
| 268 | +``` |
0 commit comments