diff --git a/README.md b/README.md index b33d79ed..47b52027 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ - - - - - The LiveKit icon, the name of the repository and some sample code in the background. - + + + + + The LiveKit icon, the name of the repository and some sample code in the background. + [![pypi-v](https://img.shields.io/pypi/v/livekit.svg)](https://pypi.org/project/livekit/) @@ -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 ``` @@ -24,15 +25,25 @@ $ 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) @@ -40,9 +51,8 @@ async def main(): 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() diff --git a/examples/e2ee.py b/examples/e2ee.py index eb6f1003..c5cbdcae 100644 --- a/examples/e2ee.py +++ b/examples/e2ee.py @@ -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, diff --git a/examples/face_landmark/face_landmark.py b/examples/face_landmark/face_landmark.py index fa0cec1f..886230da 100644 --- a/examples/face_landmark/face_landmark.py +++ b/examples/face_landmark/face_landmark.py @@ -1,6 +1,6 @@ import asyncio import os -from queue import Queue +import signal import cv2 import mediapipe as mp @@ -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 @@ -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)