Skip to content

Commit 99f79c3

Browse files
yepherChris Wilson
andauthored
fixes #309 example in readme and makes a more complete example (#310)
Co-authored-by: Chris Wilson <[email protected]>
1 parent 01a6c6b commit 99f79c3

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ $ pip install livekit
7474

7575
### Connecting to a room
7676

77+
see [room_example](examples/room_example.py) for full example
78+
7779
```python
7880
from livekit import rtc
7981

@@ -86,7 +88,7 @@ async def main():
8688
"participant connected: %s %s", participant.sid, participant.identity)
8789

8890
async def receive_frames(stream: rtc.VideoStream):
89-
async for frame in video_stream:
91+
async for frame in stream:
9092
# received a video frame from the track, process it here
9193
pass
9294

@@ -105,9 +107,11 @@ async def main():
105107

106108
# participants and tracks that are already available in the room
107109
# participant_connected and track_published events will *not* be emitted for them
108-
for participant in room.participants.items():
109-
for publication in participant.track_publications.items():
110-
print("track publication: %s", publication.sid)
110+
for identity, participant in room.remote_participants.items():
111+
print(f"identity: {identity}")
112+
print(f"participant: {participant}")
113+
for tid, publication in participant.track_publications.items():
114+
print(f"\ttrack id: {publication}")
111115
```
112116

113117
### Sending and receiving chat

examples/room_example.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)