Skip to content

Commit c8ca2eb

Browse files
committed
adding to readme for MediaDevices usage
1 parent 58483ac commit c8ca2eb

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,98 @@ except Exception as e:
173173

174174
You may find it useful to adjust the `response_timeout` parameter, which indicates the amount of time you will wait for a response. We recommend keeping this value as low as possible while still satisfying the constraints of your application.
175175

176+
## Using local media devices
177+
178+
The `MediaDevices` class provides a high-level interface for working with local audio input (microphone) and output (speakers) devices. It's built on top of the `sounddevice` library and integrates seamlessly with LiveKit's audio processing features. In order to use `MediaDevices`, you must have the `sounddevice` library installed in your local Python environment, if it's not available, `MediaDevices` will not work.
179+
180+
### Capturing microphone input
181+
182+
```python
183+
from livekit import rtc
184+
185+
# Create a MediaDevices instance
186+
devices = rtc.MediaDevices()
187+
188+
# Open the default microphone with audio processing enabled
189+
mic = devices.open_input(
190+
enable_aec=True, # Acoustic Echo Cancellation
191+
noise_suppression=True, # Noise suppression
192+
high_pass_filter=True, # High-pass filter
193+
auto_gain_control=True # Automatic gain control
194+
)
195+
196+
# Use the audio source to create a track and publish it
197+
track = rtc.LocalAudioTrack.create_audio_track("microphone", mic.source)
198+
await room.local_participant.publish_track(track)
199+
200+
# Clean up when done
201+
await mic.aclose()
202+
```
203+
204+
### Playing audio to speakers
205+
206+
```python
207+
# Open the default output device
208+
player = devices.open_output()
209+
210+
# Add remote audio tracks to the player (typically in a track_subscribed handler)
211+
@room.on("track_subscribed")
212+
def on_track_subscribed(track: rtc.Track, publication, participant):
213+
if track.kind == rtc.TrackKind.KIND_AUDIO:
214+
player.add_track(track)
215+
216+
# Start playback (mixes all added tracks)
217+
await player.start()
218+
219+
# Clean up when done
220+
await player.aclose()
221+
```
222+
223+
### Full duplex audio (microphone + speakers)
224+
225+
For full duplex audio with echo cancellation, open the input device first (with AEC enabled), then open the output device. The output player will automatically feed the APM's reverse stream for effective echo cancellation:
226+
227+
```python
228+
devices = rtc.MediaDevices()
229+
230+
# Open microphone with AEC
231+
mic = devices.open_input(enable_aec=True)
232+
233+
# Open speakers - automatically uses the mic's APM for echo cancellation
234+
player = devices.open_output()
235+
236+
# Publish microphone
237+
track = rtc.LocalAudioTrack.create_audio_track("mic", mic.source)
238+
await room.local_participant.publish_track(track)
239+
240+
# Add remote tracks and start playback
241+
player.add_track(remote_audio_track)
242+
await player.start()
243+
```
244+
245+
### Listing available devices
246+
247+
```python
248+
devices = rtc.MediaDevices()
249+
250+
# List input devices
251+
input_devices = devices.list_input_devices()
252+
for device in input_devices:
253+
print(f"{device['index']}: {device['name']}")
254+
255+
# List output devices
256+
output_devices = devices.list_output_devices()
257+
for device in output_devices:
258+
print(f"{device['index']}: {device['name']}")
259+
260+
# Get default device indices
261+
default_input = devices.default_input_device()
262+
default_output = devices.default_output_device()
263+
```
264+
265+
See [publish_mic.py](examples/local_audio/publish_mic.py) and [full_duplex.py](examples/local_audio/full_duplex.py) for complete examples.
266+
267+
176268
#### Errors
177269

178270
LiveKit is a dynamic realtime environment and calls can fail for various reasons.

0 commit comments

Comments
 (0)