Skip to content

Commit 72b31b9

Browse files
committed
implement wrapper
1 parent 371d05b commit 72b31b9

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

examples/basic_room.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ def on_reconnecting() -> None:
117117
def on_reconnected() -> None:
118118
logging.info("reconnected")
119119

120+
@room.on("room_updated")
121+
def on_room_updated() -> None:
122+
logging.info(f"room updated, participants: {room.num_participants}")
123+
120124
token = (
121125
api.AccessToken()
122126
.with_identity("python-bot")
@@ -130,8 +134,8 @@ def on_reconnected() -> None:
130134
.to_jwt()
131135
)
132136
await room.connect(os.getenv("LIVEKIT_URL"), token)
133-
logging.info("connected to room %s", room.name)
134-
logging.info("participants: %s", room.remote_participants)
137+
logging.info(f"connected to room {room.name}, created {room.creation_time}")
138+
logging.info(f"participants: {room.remote_participants}")
135139

136140
await asyncio.sleep(2)
137141
await room.local_participant.publish_data("hello world")

livekit-rtc/livekit/rtc/room.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
from __future__ import annotations
1515

16+
import datetime
1617
import asyncio
1718
import ctypes
1819
import logging
@@ -70,6 +71,8 @@
7071
"disconnected",
7172
"reconnecting",
7273
"reconnected",
74+
"room_updated",
75+
"moved",
7376
]
7477

7578

@@ -234,6 +237,60 @@ def e2ee_manager(self) -> E2EEManager:
234237
E2EEManager: The E2EE manager instance.
235238
"""
236239
return self._e2ee_manager
240+
241+
@property
242+
def num_participants(self) -> int:
243+
"""Gets the number of participants in the room.
244+
245+
Returns:
246+
int: The number of participants in the room.
247+
"""
248+
return self._info.num_participants
249+
250+
@property
251+
def num_publishers(self) -> int:
252+
"""Gets the number of publishers in the room.
253+
254+
Returns:
255+
int: The number of publishers in the room.
256+
"""
257+
return self._info.num_publishers
258+
259+
@property
260+
def creation_time(self) -> datetime.datetime:
261+
"""Time when the room was created.
262+
263+
Returns:
264+
datetime.datetime: The creation time of the room.
265+
"""
266+
return datetime.datetime.fromtimestamp(self._info.creation_time / 1000, datetime.timezone.utc)
267+
268+
@property
269+
def is_recording(self) -> bool:
270+
"""Whether the room is actively recording.
271+
272+
Returns:
273+
bool: True if actively recording, False otherwise.
274+
"""
275+
return self._info.active_recording
276+
277+
@property
278+
def departure_timeout(self) -> float:
279+
"""Amount of time to hold the room open after the last standard participant leaves.
280+
281+
Returns:
282+
float: The departure timeout of the room.
283+
"""
284+
return float(self._info.departure_timeout)
285+
286+
@property
287+
def empty_timeout(self) -> float:
288+
"""Amount of time to keep the room open if no participants join.
289+
290+
Returns:
291+
float: The empty timeout of the room.
292+
"""
293+
return float(self._info.empty_timeout)
237294

238295
def isconnected(self) -> bool:
239296
"""Checks if the room is currently connected.
@@ -311,6 +368,10 @@ def on(self, event: EventTypes, callback: Optional[Callable] = None) -> Callable
311368
- Arguments: None
312369
- **"reconnected"**: Called when the room has successfully reconnected.
313370
- Arguments: None
371+
- **"room_updated"**: Called when any information about the room is updated.
372+
- Arguments: None
373+
- **"moved"**: Called when the participant has been moved to another room.
374+
- Arguments: None
314375
315376
Example:
316377
```python
@@ -771,7 +832,18 @@ def _on_room_event(self, event: proto_room.RoomEvent):
771832
task.add_done_callback(self._data_stream_tasks.discard)
772833

773834
elif which == "room_updated":
774-
print(f"received room update: {event.room_updated}")
835+
self._info = event.room_updated
836+
self.emit("room_updated")
837+
838+
elif which == "moved":
839+
self._info = event.moved
840+
self.emit("moved")
841+
842+
elif which == "participants_updated":
843+
for info in event.participants_updated.participants:
844+
participant = self._retrieve_participant(info.identity)
845+
if participant:
846+
participant._info = info
775847

776848
def _handle_stream_header(
777849
self, header: proto_room.DataStream.Header, participant_identity: str

0 commit comments

Comments
 (0)