livekit / python-sdks

LiveKit real-time and server SDKs for Python

Home Page:https://docs.livekit.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

participant_connected not work.

see2023 opened this issue · comments

Server: 1.5.2
python: livekit 0.8.0
Can't see participant connected.

But from the logs in the package, it looks normal:

...python3.10/site-packages/livekit/rtc/_ffi_client.py:138] - livekit::room:633:livekit::room - new participant: ...

server log shows them in the same room:

2024-02-06T22:24:28.456+0800    DEBUG   livekit.sub     sfu/downtrack.go:1948   forwarded key frame     {"room": "my-room", "roomID": "RM_CyQu4fioWqKw", "participant": "user1", "pID": "PA_111", "remote": false, "trackID": "TR_111", "relayed": false, "layer": 2, "rtpsn": 42511, "rtpts": 513058783}
2024-02-06T22:26:51.431+0800    DEBUG   livekit.pub     connectionquality/scorer.go:502 updating maxPPS {"room": "my-room", "roomID": "RM_CyQu4fioWqKw", "participant": "user2", "pID": "PA_222", "remote": false, "trackID": "TR_222", "relayed": false, "mime": "audio/red", "direction": "up", "expected": 165, "duration": 5.000402637, "pps": 32.99734280977662}

Is there something wrong with the code? Thanks for the help.

from livekit import rtc
import logging
import asyncio

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s [in %(pathname)s:%(lineno)d] - %(message)s',
)

URL = ''
TOKEN = ''

async def main():
    room = rtc.Room()

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

    # 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)
    
    input("press enter to exit")
    await room.disconnect()


if __name__ == '__main__':
    asyncio.run(main())

Hey, you are blocking the asyncio event loop when calling the input method.
Use another async strategy like a signal handler