o
    i0                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ d dl	m
Z
mZmZmZmZmZ d dlZddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/ ddl0m1Z1m2Z2 ddl3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z< ed Z=eG dd dZ>eG dd dZ?eG dd dZ@eG dd dZAeG d d! d!ZBG d"d# d#eCZDG d$d% d%ee= ZEdS )&    )annotationsN)	dataclassfield)CallableDictLiteralOptionalcastMapping   )EventEmitter)	FfiClient	FfiHandle)ffi_pb2)participant_pb2)room_pb2)	stats_pb2)DisconnectReason)ConnectionState)	TrackKind)RpcMethodInvocationEvent)BroadcastQueue)E2EEManagerE2EEOptions)LocalParticipantParticipantRemoteParticipant)RemoteAudioTrackRemoteVideoTrack)RemoteTrackPublicationTrackPublication)TranscriptionSegment)TextStreamReaderByteStreamReaderTextStreamHandlerByteStreamHandler) participant_connectedparticipant_disconnectedlocal_track_publishedlocal_track_unpublishedlocal_track_subscribedtrack_publishedtrack_unpublishedtrack_subscribedtrack_unsubscribedtrack_subscription_failedtrack_mutedtrack_unmutedactive_speakers_changedroom_metadata_changedparticipant_metadata_changedparticipant_name_changedparticipant_attributes_changedconnection_quality_changed%participant_encryption_status_changedparticipant_permissions_changeddata_receivedsip_dtmf_receivedtranscription_receivede2ee_state_changedconnection_state_changed	connecteddisconnectedreconnectingreconnectedroom_updatedmovedtoken_refreshedc                   @  sD   e Zd ZU ejjZded< 	 ejj	Z
ded< 	 eedZded< dS )	RtcConfigurationz%proto_room.IceTransportType.ValueTypeice_transport_typez-proto_room.ContinualGatheringPolicy.ValueTypecontinual_gathering_policy)default_factoryzlist[proto_room.IceServer]ice_serversN)__name__
__module____qualname__
proto_roomIceTransportTypeTRANSPORT_ALLrG   __annotations__ContinualGatheringPolicyGATHER_CONTINUALLYrH   r   listrJ    rU   rU   R/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/rtc/room.pyrF   T   s   
 

rF   c                   @  sl   e Zd ZU dZded< 	 dZded< dZded< 	 dZded	< 	 dZd
ed< 	 dZ	ded< 	 dZ
ded< dS )RoomOptionsTboolauto_subscribeFdynacastNzE2EEOptions | Nonee2ee
encryptionzRtcConfiguration | None
rtc_configzfloat | Noneconnect_timeoutzbool | Nonesingle_peer_connection)rK   rL   rM   rY   rQ   rZ   r[   r\   r]   r^   r_   rU   rU   rU   rV   rW   c   s   
 rW   c                   @  s8   e Zd ZU ded< 	 ded< 	 ded< 	 dZded	< dS )

DataPacketbytesdataz#proto_room.DataPacketKind.ValueTypekindRemoteParticipant | NoneparticipantNz
str | Nonetopic)rK   rL   rM   rQ   rf   rU   rU   rU   rV   r`   t   s   
 r`   c                   @  s.   e Zd ZU ded< 	 ded< 	 dZded< dS )SipDTMFintcodestrdigitNrd   re   )rK   rL   rM   rQ   re   rU   rU   rU   rV   rg      s   
 rg   c                   @  s   e Zd ZU ded< ded< dS )RtcStatszlist[proto_stats.RtcStats]publisher_statssubscriber_statsN)rK   rL   rM   rQ   rU   rU   rU   rV   rl      s   
 rl   c                   @  s   e Zd ZdddZdS )ConnectErrormessagerj   c                 C  s
   || _ d S N)rp   )selfrp   rU   rU   rV   __init__   s   
zConnectError.__init__N)rp   rj   )rK   rL   rM   rs   rU   rU   rU   rV   ro      s    ro   c                      s  e Zd Z	dudv fddZdwdd	ZedxddZedyddZedzddZed{ddZ	edxddZ
edxddZed|ddZed}ddZed}d d!Zed~d#d$Zedd&d'Zedd)d*Zedd+d,Zdd-d.Zdud fd4d5Ze fdd:d;Zdd=d>ZddBdCZddDdEZddGdHZddIdJZdwdKdLZdwdMdNZddQdRZddTdUZddYdZZ dd]d^Z!ddadbZ"dwdcddZ#dwdedfZ$ddidjZ%ddldmZ&ddqdrZ'dxdsdtZ(  Z)S )RoomNloop#Optional[asyncio.AbstractEventLoop]returnNonec                   s   t    d| _|pt | _ttj  | _	t
 | _t | _t | _i | _tj| _tjt  | _d| _i | _i | _i | _i | _d| _d| _dS )zInitializes a new Room instance.

        Parameters:
            loop (Optional[asyncio.AbstractEventLoop]): The event loop to use. If not provided, the default event loop is used.
        N)superrs   _ffi_handleasyncioget_event_loop_loopr   	proto_ffiFfiEvent_room_queuerN   RoomInfo_infoset_rpc_invocation_tasks_data_stream_tasks_remote_participantsr   CONN_DISCONNECTED_connection_stateFuturerj   _first_sid_future_local_participant_text_stream_readers_byte_stream_readers_text_stream_handlers_byte_stream_handlers_token_server_url)rr   ru   	__class__rU   rV   rs      s"   
	

zRoom.__init__c                 C  s"   | j d urtjj| j d S d S rq   )rz   r   instancequeueunsubscribe
_ffi_queuerr   rU   rU   rV   __del__   s   
zRoom.__del__rj   c                   s   | j jr	| j jS | jI dH S )zAsynchronously retrieves the session ID (SID) of the room.

        Returns:
            str: The session ID of the room.
        N)r   sidr   r   rU   rU   rV   r      s   zRoom.sidr   c                 C  s   | j du r	td| j S )zGets the local participant in the room.

        Returns:
            LocalParticipant: The local participant in the room.
        Nz1cannot access local participant before connecting)r   	Exceptionr   rU   rU   rV   local_participant   s   
zRoom.local_participantConnectionState.ValueTypec                 C     | j S )zGets the connection state of the room.

        Returns:
            ConnectionState: The connection state of the room.
        )r   r   rU   rU   rV   connection_state      zRoom.connection_stateMapping[str, RemoteParticipant]c                 C  r   )zGets the remote participants in the room.

        Returns:
            dict[str, RemoteParticipant]: A dictionary of remote participants indexed by their
            identity.
        )r   r   rU   rU   rV   remote_participants   s   zRoom.remote_participantsc                 C     | j jS )z\Gets the name of the room.

        Returns:
            str: The name of the room.
        )r   namer   rU   rU   rV   r         z	Room.namec                 C  r   )zqGets the metadata associated with the room.

        Returns:
            str: The metadata of the room.
        )r   metadatar   rU   rU   rV   r      r   zRoom.metadatar   c                 C  r   )zGets the end-to-end encryption (E2EE) manager for the room.

        Returns:
            E2EEManager: The E2EE manager instance.
        )_e2ee_managerr   rU   rU   rV   e2ee_manager   r   zRoom.e2ee_managerrh   c                 C  r   )zGets the number of participants in the room.
        This value is updated periodically, and is eventually consistent.

        Returns:
            int: The number of participants in the room.
        )r   num_participantsr   rU   rU   rV   r         zRoom.num_participantsc                 C  r   )zGets the number of publishers in the room.
        This value is updated periodically, and is eventually consistent.

        Returns:
            int: The number of publishers in the room.
        )r   num_publishersr   rU   rU   rV   r   	  r   zRoom.num_publishersdatetime.datetimec                 C  s   t j | jjd t jjS )zxTime when the room was created.

        Returns:
            datetime.datetime: The creation time of the room.
          )datetimefromtimestampr   creation_timetimezoneutcr   rU   rU   rV   r     s   zRoom.creation_timerX   c                 C  r   )zWhether the room is actively recording.

        Returns:
            bool: True if actively recording, False otherwise.
        )r   active_recordingr   rU   rU   rV   is_recording  r   zRoom.is_recordingfloatc                 C     t | jjS )zAmount of time to hold the room open after the last standard participant leaves.

        Returns:
            float: The departure timeout of the room.
        )r   r   departure_timeoutr   rU   rU   rV   r   '     zRoom.departure_timeoutc                 C  r   )zAmount of time to keep the room open if no participants join.

        Returns:
            float: The empty timeout of the room.
        )r   r   empty_timeoutr   rU   rU   rV   r   0  r   zRoom.empty_timeoutc                 C  s   | j duo
| jtjkS )z{Checks if the room is currently connected.

        Returns:
            bool: True if connected, False otherwise.
        N)rz   r   r   r   r   rU   rU   rV   isconnected9  s   

zRoom.isconnectedevent
EventTypescallbackOptional[Callable]r   c                   s   t  ||S )a  Registers an event handler for a specific event type.

        Parameters:
            event (EventTypes): The name of the event to listen for.
            callback (Callable): The function to call when the event occurs.

        Returns:
            Callable: The registered callback function.

        Available events:
            - **"participant_connected"**: Called when a new participant joins the room.
                - Arguments: `participant` (RemoteParticipant)
            - **"participant_disconnected"**: Called when a participant leaves the room.
                - Arguments: `participant` (RemoteParticipant)
            - **"local_track_published"**: Called when a local track is published.
                - Arguments: `publication` (LocalTrackPublication), `track` (Track)
            - **"local_track_unpublished"**: Called when a local track is unpublished.
                - Arguments: `publication` (LocalTrackPublication)
            - **"local_track_subscribed"**: Called when a local track is subscribed.
                - Arguments: `track` (Track)
            - **"track_published"**: Called when a remote participant publishes a track.
                - Arguments: `publication` (RemoteTrackPublication), `participant` (RemoteParticipant)
            - **"track_unpublished"**: Called when a remote participant unpublishes a track.
                - Arguments: `publication` (RemoteTrackPublication), `participant` (RemoteParticipant)
            - **"track_subscribed"**: Called when a track is subscribed.
                - Arguments: `track` (Track), `publication` (RemoteTrackPublication), `participant` (RemoteParticipant)
            - **"track_unsubscribed"**: Called when a track is unsubscribed.
                - Arguments: `track` (Track), `publication` (RemoteTrackPublication), `participant` (RemoteParticipant)
            - **"track_subscription_failed"**: Called when a track subscription fails.
                - Arguments: `participant` (RemoteParticipant), `track_sid` (str), `error` (str)
            - **"track_muted"**: Called when a track is muted.
                - Arguments: `participant` (Participant), `publication` (TrackPublication)
            - **"track_unmuted"**: Called when a track is unmuted.
                - Arguments: `participant` (Participant), `publication` (TrackPublication)
            - **"active_speakers_changed"**: Called when the list of active speakers changes.
                - Arguments: `speakers` (list[Participant])
            - **"room_metadata_changed"**: Called when the room's metadata is updated.
                - Arguments: `old_metadata` (str), `new_metadata` (str)
            - **"participant_metadata_changed"**: Called when a participant's metadata is updated.
                - Arguments: `participant` (Participant), `old_metadata` (str), `new_metadata` (str)
            - **"participant_name_changed"**: Called when a participant's name is changed.
                - Arguments: `participant` (Participant), `old_name` (str), `new_name` (str)
            - **"participant_attributes_changed"**: Called when a participant's attributes change.
                - Arguments: `changed_attributes` (dict), `participant` (Participant)
            - **"participant_encryption_status_changed"**: Called when a participant's encryption status changes.
                - Arguments `is_encrypted` (bool), `participant` (Participant)
            - **"connection_quality_changed"**: Called when a participant's connection quality changes.
                - Arguments: `participant` (Participant), `quality` (ConnectionQuality)
            - **"transcription_received"**: Called when a transcription is received.
                - Arguments: `segments` (list[TranscriptionSegment]), `participant` (Participant), `publication` (TrackPublication)
            - **"data_received"**: Called when data is received.
                - Arguments: `data_packet` (DataPacket)
            - **"sip_dtmf_received"**: Called when a SIP DTMF signal is received.
                - Arguments: `sip_dtmf` (SipDTMF)
            - **"e2ee_state_changed"**: Called when a participant's E2EE state changes.
                - Arguments: `participant` (Participant), `state` (EncryptionState)
            - **"connection_state_changed"**: Called when the room's connection state changes.
                - Arguments: `connection_state` (ConnectionState)
            - **"connected"**: Called when the room is successfully connected.
                - Arguments: None
            - **"disconnected"**: Called when the room is disconnected.
                - Arguments: `reason` (DisconnectReason)
            - **"reconnecting"**: Called when the room is attempting to reconnect.
                - Arguments: None
            - **"reconnected"**: Called when the room has successfully reconnected.
                - Arguments: None
            - **"room_updated"**: Called when any information about the room is updated.
                - Arguments: None
            - **"moved"**: Called when the participant has been moved to another room.
                - Arguments: None

        Example:
            ```python
            def on_participant_connected(participant):
                print(f"Participant connected: {participant.identity}")

            room.on("participant_connected", on_participant_connected)
            ```
        )ry   on)rr   r   r   r   rU   rV   r   D  s   PzRoom.onurltokenoptionsrW   c              	     s  || _ || _t }||j_||j_|j|jj_|j	|jj_	|j
dur.t|j
d |jj_|jdur9|j|jj_|jrttjdtdd |jj|jjj_|jjj|jjjj_|jjj|jjjj_|jjj|jjjj_|jjj|jjjj_|jr|jj|jjj_|jjj|jjjj_|jjj|jjjj_|jjj|jjjj_|jjj|jjjj_|jr|jj|jjj_|jj|jjj_|jjjj|jj tjj !| j"| _#tjj ! }ztj$| |% fddI dH }W tjj &| ntjj &| w |jj'rtjj &| j# t(|jj't)|jj*j+j,j-| _.t/| j.j,|jp|j| _0|jj*j+j1| _2t3j4| _5t6| j7|jj*j8| _9|jj*j:D ]}| ;|j<}|j=D ]}	t>|	}
|
|j?|
j@< qGq<| j"A| B | _CdS )a  Connects to a LiveKit room using the specified URL and token.

        Parameters:
            url (str): The WebSocket URL of the LiveKit server to connect to.
            token (str): The access token for authentication and authorization.
            options (RoomOptions, optional): Additional options for the room connection.

        Raises:
            ConnectError: If the connection fails.

        Example:
            ```python
            room = Room()

            # Listen for events before connecting to the room
            @room.on("participant_connected")
            def on_participant_connected(participant):
                print(f"Participant connected: {participant.identity}")

            await room.connect("ws://localhost:7880", "your_token")
            ```
        Nr   z:options.e2ee is deprecated, use options.encryption instead   )
stacklevelc                      | j j j jkS rq   )connectasync_ideresprU   rV   <lambda>      zRoom.connect.<locals>.<lambda>)Dr   r   r~   
FfiRequestr   r   r   rY   r   rZ   r^   rh   connect_timeout_msr_   r[   warningswarnDeprecationWarningencryption_typekey_provider_options
shared_keyratchet_saltfailure_toleranceratchet_window_sizer\   r]   rG   rH   rJ   extendr   r   r   	subscriber}   r   requestwait_forr   errorro   r   resultroomhandleidrz   r   r   infor   r   CONN_CONNECTEDr   r   r   r   r   participants_create_remote_participantre   publicationsr   _track_publicationsr   create_task_listen_task_task)rr   r   r   r   reqr   cbptrpowned_publication_infopublicationrU   r   rV   r     s   




 

zRoom.connectrl   c              	     s   |   s	tdt }| jj|j_tj	j
 }ztj	| | fddI d H }W tj	j
| ntj	j
| w |jjrGt|jjt|jjj}t|jjj}t||dS )Nzthe room isn't connectedc                   r   rq   )get_session_statsr   r   r   rU   rV   r     r   z$Room.get_rtc_stats.<locals>.<lambda>)rm   rn   )r   RuntimeErrorr~   r   rz   r   r   room_handler   r   r   r   r   r   r   r   rT   r   rm   rn   rl   )rr   r   r   r   rm   rn   rU   r   rV   get_rtc_stats  s"   
 zRoom.get_rtc_statsrf   handlerr%   c                 C  .   | j |}|d u r|| j |< d S td| )Nz.byte stream handler for topic '%s' already set)r   get
ValueErrorrr   rf   r   existing_handlerrU   rU   rV   register_byte_stream_handler*     z!Room.register_byte_stream_handlerc                 C      | j |r| j | d S d S rq   )r   r   poprr   rf   rU   rU   rV   unregister_byte_stream_handler1     z#Room.unregister_byte_stream_handlerr$   c                 C  r   )Nz.text stream handler for topic '%s' already set)r   r   r   r   rU   rU   rV   register_text_stream_handler5  r   z!Room.register_text_stream_handlerc                 C  r   rq   )r   r   r   r   rU   rU   rV   unregister_text_stream_handler<  r  z#Room.unregister_text_stream_handlerc              	     s   |   sdS |  I dH  |  I dH  t }| jj|j_t	j
j }zt	j
| | fddI dH  W t	j
j| nt	j
j| w | jI dH  t	j
j| j | jtjkrwtj| jj_tj| _| d| j | dtj dS dS )zDisconnects from the room.Nc                   r   rq   )
disconnectr   r   r   rU   rV   r   M  r   z!Room.disconnect.<locals>.<lambda>r>   r@   )r   _drain_rpc_invocation_tasks_drain_data_stream_tasksr~   r   rz   r   r  r   r   r   r   r   r   r   r   r   r   r   r   r   r   CLIENT_INITIATEDr   r   disconnect_reasonemit)rr   r   r   rU   r   rV   r  @  s(    zRoom.disconnectc              	     s   	 | j  I d H }|ddkr| |j n-|jj| jjkrE|j	dr'n-z| 
|j W n tyD   td|jd|j Y nw | j| | j I d H  q|  I d H  |  I d H  d S )NTrp   rpc_method_invocationeosz&error running user callback for %s: %s)r   r   
WhichOneof_on_rpc_method_invocationr
  
room_eventr   rz   r   HasField_on_room_eventr   logging	exceptionr   
put_nowaitjoinr  r  )rr   r   rU   rU   rV   r   \  s,   
	zRoom._listen_taskrpc_invocationr   c                 C  sn   | j d u rd S |j| j jjkr5| j| j |j|j|j	|j
|j|jd }| j| || jj d S d S )Ng     @@)r   local_participant_handlerz   r   r}   r   _handle_rpc_method_invocationinvocation_idmethod
request_idcaller_identitypayloadresponse_timeout_msr   addadd_done_callbackdiscard)rr   r  taskrU   rU   rV   r  x  s    

zRoom._on_rpc_method_invocationproto_room.RoomEventc           $   	   C  s	  | d}|dkr| |jj}| d| d S |dkr4|jj}| j|}|jj	|j
_	| d| d S |dkrN|jj}| jj| }|j}| d|| d S |dkrd|jj}| jj| }| d| d S |dkr|jj}| jj| }|jd  | d|j d S |dkr| j|jj }t|jj}||j|j< | d|| d S |dkr| j|jj }|j|jj}| d|| d S |d	kr
|jj}	|	j}
| j|jj }|j|
j }d
|_|
jtjkrt |	}||_!| d	||| d S |
jtj"krt#|	}||_!| d	||| d S d S |dkr2|j$j}| j| }|j|j$j }|j}d |_!d|_| d||| d S |dkrP|j%j}| j| }|j%j&}| d||j%j| d S |dkr|j'j}| (|}t)|t*sfJ |j|j'j }d
|j
_+|jrzd
|jj
_+| d|| d S |dkr|j,j}| (|}t)|t*sJ |j|j,j }d|j
_+|jrd|jj
_+| d|| d S |dkrg }|j-j.D ]}| (|}t)|t*sJ |/| q| d| d S |dkr| j0}|j1j0| j
_0| d|| j0 d S |dkr| j
js	| j2|j3j |j3j| j
_d S |dkr;|j4j}| (|}t)|t*s'J |j0}|j4j0|j
_0| d|||j0 d S |dkre|j5j}| (|}t)|t*sQJ |j6}|j5j6|j
_6| d|||j6 d S |dkr|j7j}|j7j8}t9dd |j7j:D }| (|}t)|t*sJ |j
j8;  |j
j8<dd |D  | d|| d S |dkr|j=j}| (|}| d||j=j> d S |dkr|j?j}| (|}t)|t*sJ |j
j@A|j?j@ | d||jB d S |dkr |jCj}| (|}| d||jCjD d S |dkr,|jE}dd |jFD }| (|j}d }|r"|jG|j}| d||| d S |dkr|jH}| d }|d!krz|jIjJ}|jJ}tKL|jMtKNtKjO|jP jQ}tR|}tS|jTjUV  tLtW| X|j}| d"tY||j||jIjZd# d S |d$krtLtW| X|j}| d%t[|j\j]|j\j^|d& d S d S |d'kr|j_j}|j_j`} | d'| (||  d S |d(kr|jaj`}!|!| _b| d(|! d S |d)kr| d)|jcjd d S |d*kr| d* d S |d+kr| d+ d S |d,kr | e|jfjg|jfj d S |d-krthi| j|jkjl}"| jmn|" |"o| jmjp d S |d.kr<thi| q|jrjs}"| jmn|" |"o| jmjp d S |d/krL|jt| _
| d/ d S |d0kr\|ju| _
| d0 d S |d1krw|jvjwD ]}#| (|#jx}|rs|#|_
qed S |d2kr|jyjz| _{| d2 d S d S )3Nrp   r&   r'   r(   r)   r*   r+   r,   r-   Tr.   Fr/   r0   r1   r2   r3   room_sid_changedr4   r5   r6   c                 s      | ]	}|j |jfV  qd S rq   keyvalue.0entryrU   rU   rV   	<genexpr>  s
    

z&Room._on_room_event.<locals>.<genexpr>c                 s  r$  rq   r%  r(  rU   rU   rV   r+    s    r8   r9   r7   r<   c              
   S  s,   g | ]}t |j|j|j|j|j|jd qS ))r   textfinal
start_timeend_timelanguage)r!   r   r,  r-  r.  r/  r0  )r)  srU   rU   rV   
<listcomp>6  s    	z'Room._on_room_event.<locals>.<listcomp>data_packet_receivedr'  userr:   )rb   rc   re   rf   sip_dtmfr;   )ri   rk   re   r=   r>   r@   rA   rB   stream_header_receivedstream_chunk_receivedstream_trailer_receivedrC   rD   participants_updatedrE   )|r  r   r&   r   r	  r'   participant_identityr   r   r  r   r(   	track_sidr   track_publicationstrackr)   publication_sidr*   _first_subscription
set_resultr+   r   r   r   r   r,   r-   _subscribedrc   r   
KIND_VIDEOr   _track
KIND_AUDIOr   r.   r/   r   r0   _retrieve_participant
isinstancer   mutedr1   r2   participant_identitiesappendr   r3   r   r#  r4   r5   r   r6   
attributesdictchanged_attributesclearupdater8   is_encryptedparticipant_permission_changed
permissionCopyFrompermissionsr7   qualityr<   segmentsr   r3  r4  rb   ctypesr	   data_ptrPOINTERc_bytedata_lencontentsra   r   r   r   disposer   _retrieve_remote_participantr`   rf   rg   r5  ri   rk   r=   stater>   r   r@   reason_handle_stream_headerr6  headerr{   r   _handle_stream_chunkr7  chunkr   r  r  r   _handle_stream_trailerr8  trailerrC   rD   r9  r   identityrE   r   r   )$rr   r   whichrparticipantrf  r   lpublicationltrackrpublicationowned_track_info
track_inforemote_video_trackremote_audio_trackrtrackr   re   r   speakersold_metadataold_namerJ  rL  transcriptionrU  partpubpacket	which_valowned_buffer_infobuffer_infonative_datarb   
e2ee_stater   r!  r   rU   rU   rV   r    s  





























	




	













zRoom._on_room_eventra  proto_room.DataStream.Headerr:  c                 C  s   | d}|dkr.| j|j}|d u rtd|j d S t|}|| j|j< ||| d S |dkrW| j	|j}|d u rFtd|j d S t
|}|| j|j< ||| d S td| d S )Ncontent_headertext_headerz:ignoring text stream with topic '%s', no callback attachedbyte_headerz:ignoring byte stream with topic '%s', no callback attachedz received unknown header type, %s)r  r   r   rf   r  r   r"   r   	stream_idr   r#   r   warning)rr   ra  r:  stream_typetext_stream_handlertext_readerbyte_stream_handlerbyte_readerrU   rU   rV   r`    s6   


zRoom._handle_stream_headerrc  proto_room.DataStream.Chunkc                   sR   | j |j}| j|j}|r||I d H  d S |r'||I d H  d S d S rq   )r   r   r  r   _on_chunk_update)rr   rc  r  file_readerrU   rU   rV   rb    s   zRoom._handle_stream_chunkre  proto_room.DataStream.Trailerc                   sn   | j |j}| j|j}|r"||I d H  | j |j d S |r5||I d H  | j|j d S d S rq   )r   r   r  r   _on_stream_closer   )rr   re  r  r  rU   rU   rV   rd    s   zRoom._handle_stream_trailerc                   <   | j r| j D ]}|  qtj| j ddiI d H  d S d S Nreturn_exceptionsT)r   cancelr{   gatherrr   r!  rU   rU   rV   r       

z Room._drain_rpc_invocation_tasksc                   r  r  )r   r  r{   r  r  rU   rU   rV   r    r  zRoom._drain_data_stream_tasksrf  Optional[RemoteParticipant]c                 C  s   | j |dS )z)Retrieve a remote participant by identityN)r   r   rr   rf  rU   rU   rV   r]    s   z!Room._retrieve_remote_participantOptional[Participant]c                 C  s    |r|| j jkr| j S | |S )z2Retrieve a local or remote participant by identity)r   rf  r]  r  rU   rU   rV   rE    s   
zRoom._retrieve_participant
owned_info"proto_participant.OwnedParticipantr   c                 C  s.   |j j| jv rtdt|}|| j|j< |S )Nzparticipant already exists)r   rf  r   r   r   )rr   r  re   rU   rU   rV   r     s
   zRoom._create_remote_participantc              
   C  sB   d}| j  r| j  }d| d| j d| j dt| j d	S )Nunknownzrtc.Room(sid=z, name=z, metadata=z, connection_state=))r   doner   r   r   r   Namer   )rr   r   rU   rU   rV   __repr__  s   

*zRoom.__repr__rq   )ru   rv   rw   rx   )rw   rx   )rw   rj   )rw   r   )rw   r   )rw   r   )rw   r   )rw   rh   )rw   r   )rw   rX   )rw   r   )r   r   r   r   rw   r   )r   rj   r   rj   r   rW   rw   rx   )rw   rl   )rf   rj   r   r%   )rf   rj   )rf   rj   r   r$   )r  r   )r   r"  )ra  r}  r:  rj   )rc  r  )re  r  )rf  rj   rw   r  )rf  rj   rw   r  )r  r  rw   r   )*rK   rL   rM   rs   r   propertyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r   r  r  r  r   r  r  r`  rb  rd  r  r  r]  rE  r   r  __classcell__rU   rU   r   rV   rt      sj    
			

R
|







  

 
	





rt   )F
__future__r   r   r{   rV  r  dataclassesr   r   typingr   r   r   r   r	   r
   r   event_emitterr   _ffi_clientr   r   _protor   r~   r   proto_participantr   rN   r   proto_stats_proto.participant_pb2r   _proto.room_pb2r   _proto.track_pb2r   _proto.rpc_pb2r   _utilsr   r[   r   r   re   r   r   r   r=  r   r   track_publicationr   r    rt  r!   data_streamr"   r#   r$   r%   r   rF   rW   r`   rg   rl   r   ro   rt   rU   rU   rU   rV   <module>   sP    $	