|
| 1 | +import argparse |
| 2 | +import asyncio |
| 3 | +import logging |
| 4 | +import signal |
| 5 | + |
| 6 | +from livekit import rtc |
| 7 | + |
| 8 | + |
| 9 | +async def main() -> None: |
| 10 | + parser = argparse.ArgumentParser() |
| 11 | + parser.add_argument("--url", required=True, help="LiveKit server URL") |
| 12 | + parser.add_argument("--token", required=True, help="LiveKit access token") |
| 13 | + args = parser.parse_args() |
| 14 | + |
| 15 | + logging.basicConfig(level=logging.INFO) |
| 16 | + logger = logging.getLogger("debug-participant") |
| 17 | + |
| 18 | + room = rtc.Room() |
| 19 | + stop_event = asyncio.Event() |
| 20 | + |
| 21 | + @room.on("participant_connected") |
| 22 | + def on_participant_connected(participant: rtc.RemoteParticipant) -> None: |
| 23 | + logger.info( |
| 24 | + "participant connected: sid=%s identity=%s", |
| 25 | + participant.sid, |
| 26 | + participant.identity, |
| 27 | + ) |
| 28 | + |
| 29 | + @room.on("participant_disconnected") |
| 30 | + def on_participant_disconnected(participant: rtc.RemoteParticipant) -> None: |
| 31 | + logger.info( |
| 32 | + "participant disconnected: sid=%s identity=%s", |
| 33 | + participant.sid, |
| 34 | + participant.identity, |
| 35 | + ) |
| 36 | + |
| 37 | + @room.on("data_track_published") |
| 38 | + def on_data_track_published(track: rtc.RemoteDataTrack) -> None: |
| 39 | + logger.info( |
| 40 | + "data track published: sid=%s name=%s publisher=%s", |
| 41 | + track.info.sid, |
| 42 | + track.info.name, |
| 43 | + track.publisher_identity, |
| 44 | + ) |
| 45 | + |
| 46 | + @room.on("data_track_unpublished") |
| 47 | + def on_data_track_unpublished(track_sid: str) -> None: |
| 48 | + logger.info("data track unpublished: sid=%s", track_sid) |
| 49 | + |
| 50 | + loop = asyncio.get_running_loop() |
| 51 | + for sig in (signal.SIGINT, signal.SIGTERM): |
| 52 | + loop.add_signal_handler(sig, stop_event.set) |
| 53 | + |
| 54 | + await room.connect(args.url, args.token) |
| 55 | + logger.info("connected to room: %s", room.name) |
| 56 | + |
| 57 | + for participant in room.remote_participants.values(): |
| 58 | + logger.info( |
| 59 | + "participant already in room: sid=%s identity=%s", |
| 60 | + participant.sid, |
| 61 | + participant.identity, |
| 62 | + ) |
| 63 | + |
| 64 | + try: |
| 65 | + await stop_event.wait() |
| 66 | + finally: |
| 67 | + await room.disconnect() |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + asyncio.run(main()) |
0 commit comments