Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!--BEGIN_BANNER_IMAGE-->
<picture>
<source media="(prefers-color-scheme: dark)" srcset="/.github/banner_dark.png">
<source media="(prefers-color-scheme: light)" srcset="/.github/banner_light.png">
<img style="width:100%;" alt="The LiveKit icon, the name of the repository and some sample code in the background." src="https://raw.githubusercontent.com/livekit/client-sdk-python/main/.github/banner_light.png">
</picture>
<!--BEGIN_BANNER_IMAGE-->
<picture>
<source media="(prefers-color-scheme: dark)" srcset="/.github/banner_dark.png">
<source media="(prefers-color-scheme: light)" srcset="/.github/banner_light.png">
<img style="width:100%;" alt="The LiveKit icon, the name of the repository and some sample code in the background." src="https://raw.githubusercontent.com/livekit/client-sdk-python/main/.github/banner_light.png">
</picture>
<!--END_BANNER_IMAGE-->

[![pypi-v](https://img.shields.io/pypi/v/livekit.svg)](https://pypi.org/project/livekit/)
Expand All @@ -15,6 +15,7 @@ The Livekit Python Client provides a convenient interface for integrating Liveki
Official LiveKit documentation: https://docs.livekit.io/

## Installation

```shell
$ pip install livekit
```
Expand All @@ -24,25 +25,34 @@ $ pip install livekit
```python
async def main():
room = livekit.Room()
# By default, autosubscribe is enabled. The participant will be subscribed to
# all published tracks in the room
await room.connect(URL, TOKEN)
logging.info("connected to room %s", room.name)

# participants and tracks that are already available in the room
# participant_connected and track_published events will *not* be emitted for them
for participant in room.participants.items():
for publication in participant.tracks.items():
print("track publication: %s", publication.sid)

@room.on("participant_connected")
def on_participant_connected(participant: livekit.RemoteParticipant):
logging.info(
"participant connected: %s %s", participant.sid, participant.identity)

video_stream = None

# track_subscribed is emitted whenever the local participant is subscribed to a new track
@room.on("track_subscribed")
def on_track_subscribed(track: livekit.Track, publication: livekit.RemoteTrackPublication, participant: livekit.RemoteParticipant):
logging.info("track subscribed: %s", publication.sid)
if track.kind == livekit.TrackKind.KIND_VIDEO:
nonlocal video_stream
video_stream = livekit.VideoStream(track)

@video_stream.on("frame_received")
def on_video_frame(frame: livekit.VideoFrame):
# received a video frame from the track
async for frame in video_stream:
# received a video frame from the track, process it here
pass

await room.run()
Expand Down
2 changes: 1 addition & 1 deletion examples/e2ee.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def on_e2ee_state_changed(participant: livekit.Participant,
logging.info("connecting to %s", URL)
try:
e2ee_options = livekit.E2EEOptions()
e2ee_options.key_provider_options.shared_key = b"abcdef" # this is our e2ee key
e2ee_options.key_provider_options.shared_key = b"livekitrocks" # this is our e2ee key

await room.connect(URL, TOKEN, options=livekit.RoomOptions(
auto_subscribe=True,
Expand Down
20 changes: 18 additions & 2 deletions examples/face_landmark/face_landmark.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import os
from queue import Queue
import signal

import cv2
import mediapipe as mp
Expand Down Expand Up @@ -104,7 +104,19 @@ async def frame_loop(video_stream: livekit.VideoStream) -> None:

async def main() -> None:
room = livekit.Room()
await room.connect(URL, TOKEN)

loop = asyncio.get_event_loop()
loop.add_signal_handler(
signal.SIGINT, lambda: exit(0))

await room.connect(URL, TOKEN, livekit.RoomOptions(
# Unncomment below to enable E2EE
# e2ee=livekit.E2EEOptions(
# key_provider_options=livekit.KeyProviderOptions(
# shared_key=b"livekitrocks"
# )
# ),
))
print("connected to room: " + room.name)

video_stream = None
Expand All @@ -113,6 +125,10 @@ async def main() -> None:
def on_track_subscribed(track: livekit.Track, *_):
if track.kind == livekit.TrackKind.KIND_VIDEO:
nonlocal video_stream
# only process the first stream received
if video_stream is not None:
return
print("subscribed to track: " + track.name)
video_stream = livekit.VideoStream(track)
task = asyncio.create_task(frame_loop(video_stream))
tasks.add(task)
Expand Down