|
| 1 | +import os |
| 2 | +import logging |
| 3 | +import asyncio |
| 4 | +from livekit import rtc |
| 5 | + |
| 6 | +# Set the following environment variables with your own values |
| 7 | +TOKEN = os.environ.get("LIVEKIT_TOKEN") |
| 8 | +URL = os.environ.get("LIVEKIT_URL") |
| 9 | + |
| 10 | +async def main(): |
| 11 | + logging.basicConfig(level=logging.INFO) |
| 12 | + logger = logging.getLogger(__name__) |
| 13 | + room = rtc.Room() |
| 14 | + |
| 15 | + @room.on("participant_connected") |
| 16 | + def on_participant_connected(participant: rtc.RemoteParticipant): |
| 17 | + logging.info( |
| 18 | + "participant connected: %s %s", participant.sid, participant.identity) |
| 19 | + |
| 20 | + async def receive_frames(stream: rtc.VideoStream): |
| 21 | + async for frame in stream: |
| 22 | + # received a video frame from the track, process it here |
| 23 | + pass |
| 24 | + |
| 25 | + # track_subscribed is emitted whenever the local participant is subscribed to a new track |
| 26 | + @room.on("track_subscribed") |
| 27 | + def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant): |
| 28 | + logging.info("track subscribed: %s", publication.sid) |
| 29 | + if track.kind == rtc.TrackKind.KIND_VIDEO: |
| 30 | + video_stream = rtc.VideoStream(track) |
| 31 | + asyncio.ensure_future(receive_frames(video_stream)) |
| 32 | + |
| 33 | + # By default, autosubscribe is enabled. The participant will be subscribed to |
| 34 | + # all published tracks in the room |
| 35 | + await room.connect(URL, TOKEN) |
| 36 | + logging.info("connected to room %s", room.name) |
| 37 | + |
| 38 | + for identity, participant in room.remote_participants.items(): |
| 39 | + print(f"identity: {identity}") |
| 40 | + print(f"participant: {participant}") |
| 41 | + # Now participant is the RemoteParticipant object, not a tuple |
| 42 | + print(f"participant sid: {participant.sid}") |
| 43 | + print(f"participant identity: {participant.identity}") |
| 44 | + print(f"participant name: {participant.name}") |
| 45 | + print(f"participant kind: {participant.kind}") |
| 46 | + print(f"participant track publications: {participant.track_publications}") |
| 47 | + for tid, publication in participant.track_publications.items(): |
| 48 | + print(f"\ttrack id: {tid}") |
| 49 | + print(f"\t\ttrack publication: {publication}") |
| 50 | + print(f"\t\ttrack kind: {publication.kind}") |
| 51 | + print(f"\t\ttrack name: {publication.name}") |
| 52 | + print(f"\t\ttrack source: {publication.source}") |
| 53 | + |
| 54 | + print(f"participant metadata: {participant.metadata}") |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + # exit if token and url are not set |
| 60 | + if not TOKEN or not URL: |
| 61 | + print("TOKEN and URL are required environment variables") |
| 62 | + exit(1) |
| 63 | + |
| 64 | + asyncio.run(main()) |
| 65 | + |
0 commit comments