Skip to content

Commit 6f322fe

Browse files
authored
remove confusing errors when accessing room states before connection (#269)
1 parent ee150d7 commit 6f322fe

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

livekit-rtc/livekit/rtc/room.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ def __init__(self, loop: Optional[asyncio.AbstractEventLoop] = None) -> None:
131131
self._room_queue = BroadcastQueue[proto_ffi.FfiEvent]()
132132
self._info = proto_room.RoomInfo()
133133

134-
self.remote_participants: Dict[str, RemoteParticipant] = {}
135-
self.connection_state = ConnectionState.CONN_DISCONNECTED
134+
self._remote_participants: Dict[str, RemoteParticipant] = {}
135+
self._connection_state = ConnectionState.CONN_DISCONNECTED
136136
self._first_sid_future = asyncio.Future[str]()
137+
self._local_participant: LocalParticipant | None = None
137138

138139
def __del__(self) -> None:
139140
if self._ffi_handle is not None:
@@ -151,6 +152,37 @@ async def sid(self) -> str:
151152

152153
return await self._first_sid_future
153154

155+
@property
156+
def local_participant(self) -> LocalParticipant:
157+
"""Gets the local participant in the room.
158+
159+
Returns:
160+
LocalParticipant: The local participant in the room.
161+
"""
162+
if self._local_participant is None:
163+
raise Exception("cannot access local participant before connecting")
164+
165+
return self._local_participant
166+
167+
@property
168+
def connection_state(self) -> ConnectionState.ValueType:
169+
"""Gets the connection state of the room.
170+
171+
Returns:
172+
ConnectionState: The connection state of the room.
173+
"""
174+
return self._connection_state
175+
176+
@property
177+
def remote_participants(self) -> dict[str, RemoteParticipant]:
178+
"""Gets the remote participants in the room.
179+
180+
Returns:
181+
dict[str, RemoteParticipant]: A dictionary of remote participants indexed by their
182+
identity.
183+
"""
184+
return self._remote_participants
185+
154186
@property
155187
def name(self) -> str:
156188
"""Gets the name of the room.
@@ -186,7 +218,7 @@ def isconnected(self) -> bool:
186218
"""
187219
return (
188220
self._ffi_handle is not None
189-
and self.connection_state != ConnectionState.CONN_DISCONNECTED
221+
and self._connection_state != ConnectionState.CONN_DISCONNECTED
190222
)
191223

192224
def on(self, event: EventTypes, callback: Optional[Callable] = None) -> Callable:
@@ -345,9 +377,9 @@ def on_participant_connected(participant):
345377
self._e2ee_manager = E2EEManager(self._ffi_handle.handle, options.e2ee)
346378

347379
self._info = cb.connect.room.info
348-
self.connection_state = ConnectionState.CONN_CONNECTED
380+
self._connection_state = ConnectionState.CONN_CONNECTED
349381

350-
self.local_participant = LocalParticipant(
382+
self._local_participant = LocalParticipant(
351383
self._room_queue, cb.connect.local_participant
352384
)
353385

@@ -413,7 +445,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
413445
self.emit("participant_connected", rparticipant)
414446
elif which == "participant_disconnected":
415447
identity = event.participant_disconnected.participant_identity
416-
rparticipant = self.remote_participants.pop(identity)
448+
rparticipant = self._remote_participants.pop(identity)
417449
self.emit("participant_disconnected", rparticipant)
418450
elif which == "local_track_published":
419451
sid = event.local_track_published.track_sid
@@ -430,14 +462,14 @@ def _on_room_event(self, event: proto_room.RoomEvent):
430462
lpublication._first_subscription.set_result(None)
431463
self.emit("local_track_subscribed", lpublication.track)
432464
elif which == "track_published":
433-
rparticipant = self.remote_participants[
465+
rparticipant = self._remote_participants[
434466
event.track_published.participant_identity
435467
]
436468
rpublication = RemoteTrackPublication(event.track_published.publication)
437469
rparticipant.track_publications[rpublication.sid] = rpublication
438470
self.emit("track_published", rpublication, rparticipant)
439471
elif which == "track_unpublished":
440-
rparticipant = self.remote_participants[
472+
rparticipant = self._remote_participants[
441473
event.track_unpublished.participant_identity
442474
]
443475
rpublication = rparticipant.track_publications.pop(
@@ -447,7 +479,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
447479
elif which == "track_subscribed":
448480
owned_track_info = event.track_subscribed.track
449481
track_info = owned_track_info.info
450-
rparticipant = self.remote_participants[
482+
rparticipant = self._remote_participants[
451483
event.track_subscribed.participant_identity
452484
]
453485
rpublication = rparticipant.track_publications[track_info.sid]
@@ -466,7 +498,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
466498
)
467499
elif which == "track_unsubscribed":
468500
identity = event.track_unsubscribed.participant_identity
469-
rparticipant = self.remote_participants[identity]
501+
rparticipant = self._remote_participants[identity]
470502
rpublication = rparticipant.track_publications[
471503
event.track_unsubscribed.track_sid
472504
]
@@ -476,7 +508,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
476508
self.emit("track_unsubscribed", track, rpublication, rparticipant)
477509
elif which == "track_subscription_failed":
478510
identity = event.track_subscription_failed.participant_identity
479-
rparticipant = self.remote_participants[identity]
511+
rparticipant = self._remote_participants[identity]
480512
error = event.track_subscription_failed.error
481513
self.emit(
482514
"track_subscription_failed",
@@ -636,7 +668,7 @@ def _on_room_event(self, event: proto_room.RoomEvent):
636668
)
637669
elif which == "connection_state_changed":
638670
connection_state = event.connection_state_changed.state
639-
self.connection_state = connection_state
671+
self._connection_state = connection_state
640672
self.emit("connection_state_changed", connection_state)
641673
elif which == "connected":
642674
self.emit("connected")
@@ -651,7 +683,7 @@ def _retrieve_remote_participant(
651683
self, identity: str
652684
) -> Optional[RemoteParticipant]:
653685
"""Retrieve a remote participant by identity"""
654-
return self.remote_participants.get(identity, None)
686+
return self._remote_participants.get(identity, None)
655687

656688
def _retrieve_participant(self, identity: str) -> Optional[Participant]:
657689
"""Retrieve a local or remote participant by identity"""
@@ -663,16 +695,16 @@ def _retrieve_participant(self, identity: str) -> Optional[Participant]:
663695
def _create_remote_participant(
664696
self, owned_info: proto_participant.OwnedParticipant
665697
) -> RemoteParticipant:
666-
if owned_info.info.identity in self.remote_participants:
698+
if owned_info.info.identity in self._remote_participants:
667699
raise Exception("participant already exists")
668700

669701
participant = RemoteParticipant(owned_info)
670-
self.remote_participants[participant.identity] = participant
702+
self._remote_participants[participant.identity] = participant
671703
return participant
672704

673705
def __repr__(self) -> str:
674706
sid = "unknown"
675707
if self._first_sid_future.done():
676708
sid = self._first_sid_future.result()
677709

678-
return f"rtc.Room(sid={sid}, name={self.name}, metadata={self.metadata}, connection_state={self.connection_state})"
710+
return f"rtc.Room(sid={sid}, name={self.name}, metadata={self.metadata}, connection_state={self._connection_state})"

0 commit comments

Comments
 (0)