Skip to content

Commit

Permalink
fixes #309 example in readme and makes a more complete example (#310)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Wilson <[email protected]>
  • Loading branch information
yepher and Chris Wilson authored Nov 28, 2024
1 parent 01a6c6b commit 99f79c3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ $ pip install livekit

### Connecting to a room

see [room_example](examples/room_example.py) for full example

```python
from livekit import rtc

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

async def receive_frames(stream: rtc.VideoStream):
async for frame in video_stream:
async for frame in stream:
# received a video frame from the track, process it here
pass

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

# 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.track_publications.items():
print("track publication: %s", publication.sid)
for identity, participant in room.remote_participants.items():
print(f"identity: {identity}")
print(f"participant: {participant}")
for tid, publication in participant.track_publications.items():
print(f"\ttrack id: {publication}")
```

### Sending and receiving chat
Expand Down
65 changes: 65 additions & 0 deletions examples/room_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import logging
import asyncio
from livekit import rtc

# Set the following environment variables with your own values
TOKEN = os.environ.get("LIVEKIT_TOKEN")
URL = os.environ.get("LIVEKIT_URL")

async def main():
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

Check failure on line 12 in examples/room_example.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F841)

examples/room_example.py:12:5: F841 Local variable `logger` is assigned to but never used
room = rtc.Room()

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

async def receive_frames(stream: rtc.VideoStream):
async for frame in stream:
# received a video frame from the track, process it here
pass

# track_subscribed is emitted whenever the local participant is subscribed to a new track
@room.on("track_subscribed")
def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant):
logging.info("track subscribed: %s", publication.sid)
if track.kind == rtc.TrackKind.KIND_VIDEO:
video_stream = rtc.VideoStream(track)
asyncio.ensure_future(receive_frames(video_stream))

# 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)

for identity, participant in room.remote_participants.items():
print(f"identity: {identity}")
print(f"participant: {participant}")
# Now participant is the RemoteParticipant object, not a tuple
print(f"participant sid: {participant.sid}")
print(f"participant identity: {participant.identity}")
print(f"participant name: {participant.name}")
print(f"participant kind: {participant.kind}")
print(f"participant track publications: {participant.track_publications}")
for tid, publication in participant.track_publications.items():
print(f"\ttrack id: {tid}")
print(f"\t\ttrack publication: {publication}")
print(f"\t\ttrack kind: {publication.kind}")
print(f"\t\ttrack name: {publication.name}")
print(f"\t\ttrack source: {publication.source}")

print(f"participant metadata: {participant.metadata}")



if __name__ == "__main__":
# exit if token and url are not set
if not TOKEN or not URL:
print("TOKEN and URL are required environment variables")
exit(1)

asyncio.run(main())

0 comments on commit 99f79c3

Please sign in to comment.