diff --git a/examples/basic_room.py b/examples/basic_room.py index 3859f755..7e898c30 100644 --- a/examples/basic_room.py +++ b/examples/basic_room.py @@ -3,10 +3,9 @@ from signal import SIGINT, SIGTERM from typing import Union -from livekit import rtc +from livekit import api, rtc -URL = "ws://localhost:7880" -TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY" # noqa +# ensure LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET are set async def main(room: rtc.Room) -> None: @@ -105,7 +104,8 @@ def on_connection_quality_changed( def on_track_subscription_failed( participant: rtc.RemoteParticipant, track_sid: str, error: str ): - logging.info("track subscription failed: %s %s", participant.identity, error) + logging.info("track subscription failed: %s %s", + participant.identity, error) @room.on("connection_state_changed") def on_connection_state_changed(state: rtc.ConnectionState): @@ -127,7 +127,15 @@ def on_reconnecting() -> None: def on_reconnected() -> None: logging.info("reconnected") - await room.connect(URL, TOKEN) + info = api.ConnectionInfo() + token = api.AccessToken(info.api_key, info.api_secret) \ + .with_identity("python-bot") \ + .with_name("Python Bot") \ + .with_grants(api.VideoGrants( + room_join=True, + room="my-room", + )).to_jwt() + await room.connect(info.websocket_url(), token) logging.info("connected to room %s", room.name) logging.info("participants: %s", room.participants) @@ -138,7 +146,8 @@ def on_reconnected() -> None: if __name__ == "__main__": logging.basicConfig( level=logging.INFO, - handlers=[logging.FileHandler("basic_room.log"), logging.StreamHandler()], + handlers=[logging.FileHandler( + "basic_room.log"), logging.StreamHandler()], ) loop = asyncio.get_event_loop() @@ -150,7 +159,8 @@ async def cleanup(): asyncio.ensure_future(main(room)) for signal in [SIGINT, SIGTERM]: - loop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup())) + loop.add_signal_handler( + signal, lambda: asyncio.ensure_future(cleanup())) try: loop.run_forever() diff --git a/examples/face_landmark/face_landmark.py b/examples/face_landmark/face_landmark.py index 4a209e74..649a5c6f 100644 --- a/examples/face_landmark/face_landmark.py +++ b/examples/face_landmark/face_landmark.py @@ -9,10 +9,9 @@ from mediapipe import solutions from mediapipe.framework.formats import landmark_pb2 -from livekit import rtc +from livekit import api, rtc -URL = "ws://localhost:7880" -TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE5MDY2MTMyODgsImlzcyI6IkFQSVRzRWZpZFpqclFvWSIsIm5hbWUiOiJuYXRpdmUiLCJuYmYiOjE2NzI2MTMyODgsInN1YiI6Im5hdGl2ZSIsInZpZGVvIjp7InJvb20iOiJ0ZXN0Iiwicm9vbUFkbWluIjp0cnVlLCJyb29tQ3JlYXRlIjp0cnVlLCJyb29tSm9pbiI6dHJ1ZSwicm9vbUxpc3QiOnRydWV9fQ.uSNIangMRu8jZD5mnRYoCHjcsQWCrJXgHCs0aNIgBFY" # noqa +# ensure LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET are set tasks = set() @@ -30,10 +29,38 @@ running_mode=VisionRunningMode.VIDEO, ) -# from https://github.com/googlesamples/mediapipe/blob/main/examples/face_landmarker/python/%5BMediaPipe_Python_Tasks%5D_Face_Landmarker.ipynb + +async def main(room: rtc.Room) -> None: + video_stream = None + + @room.on("track_subscribed") + def on_track_subscribed(track: rtc.Track, *_): + if track.kind == rtc.TrackKind.KIND_VIDEO: + nonlocal video_stream + if video_stream is not None: + # only process the first stream received + return + + print("subscribed to track: " + track.name) + video_stream = rtc.VideoStream(track) + task = asyncio.create_task(frame_loop(video_stream)) + tasks.add(task) + task.add_done_callback(tasks.remove) + + info = api.ConnectionInfo() + token = api.AccessToken(info.api_key, info.api_secret) \ + .with_identity("python-bot") \ + .with_name("Python Bot") \ + .with_grants(api.VideoGrants( + room_join=True, + room="my-room", + )).to_jwt() + await room.connect(info.websocket_url(), token) + print("connected to room: " + room.name) def draw_landmarks_on_image(rgb_image, detection_result): + # from https://github.com/googlesamples/mediapipe/blob/main/examples/face_landmarker/python/%5BMediaPipe_Python_Tasks%5D_Face_Landmarker.ipynb face_landmarks_list = detection_result.face_landmarks # Loop through the detected faces to visualize. @@ -97,7 +124,8 @@ async def frame_loop(video_stream: rtc.VideoStream) -> None: mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=arr) - detection_result = landmarker.detect_for_video(mp_image, frame.timestamp_us) + detection_result = landmarker.detect_for_video( + mp_image, frame.timestamp_us) draw_landmarks_on_image(arr, detection_result) @@ -111,31 +139,11 @@ async def frame_loop(video_stream: rtc.VideoStream) -> None: cv2.destroyAllWindows() -async def main(room: rtc.Room) -> None: - video_stream = None - - @room.on("track_subscribed") - def on_track_subscribed(track: rtc.Track, *_): - if track.kind == rtc.TrackKind.KIND_VIDEO: - nonlocal video_stream - if video_stream is not None: - # only process the first stream received - return - - print("subscribed to track: " + track.name) - video_stream = rtc.VideoStream(track) - task = asyncio.create_task(frame_loop(video_stream)) - tasks.add(task) - task.add_done_callback(tasks.remove) - - await room.connect(URL, TOKEN) - print("connected to room: " + room.name) - - if __name__ == "__main__": logging.basicConfig( level=logging.INFO, - handlers=[logging.FileHandler("face_landmark.log"), logging.StreamHandler()], + handlers=[logging.FileHandler( + "face_landmark.log"), logging.StreamHandler()], ) loop = asyncio.get_event_loop() @@ -147,7 +155,8 @@ async def cleanup(): asyncio.ensure_future(main(room)) for signal in [SIGINT, SIGTERM]: - loop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup())) + loop.add_signal_handler( + signal, lambda: asyncio.ensure_future(cleanup())) try: loop.run_forever()