
    qiVj              	      X   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZ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'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl,m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7  G d de8      Z9 G d de8      Z: G d de8      Z; G d de8      Z< G d de8      Z= G d d e      Z>e
d!ge	eee?      ee?   f   f   Z@ ed"e
e2ge	eee?      ee?   f   f   #      ZA G d$ d%e>      ZB G d& d'e>      ZCy)(    )annotationsN)	ListUnionCallableDict	AwaitableOptionalMappingcastTypeVar)abstractmethodABC   )	FfiClient	FfiHandle)ffi_pb2)participant_pb2)TrackPublishOptions)TranscriptionSegment)ParticipantTrackPermission)BroadcastQueue)
LocalTrack)LocalTrackPublicationRemoteTrackPublicationTrackPublication)Transcription)RpcError)"RpcMethodInvocationResponseRequest)logger)RpcInvocationData)TextStreamWriterByteStreamWriterByteStreamInfoSTREAM_CHUNK_SIZEc                      e Zd ZddZy)PublishTrackErrorc                    || _         y Nmessageselfr*   s     I/opt/pipecat/venv/lib/python3.12/site-packages/livekit/rtc/participant.py__init__zPublishTrackError.__init__;   	        Nr*   strreturnNone__name__
__module____qualname__r.    r0   r-   r&   r&   :       r0   r&   c                      e Zd ZddZy)UnpublishTrackErrorc                    || _         y r(   r)   r+   s     r-   r.   zUnpublishTrackError.__init__@   r/   r0   Nr1   r5   r9   r0   r-   r<   r<   ?   r:   r0   r<   c                      e Zd ZddZy)PublishDataErrorc                    || _         y r(   r)   r+   s     r-   r.   zPublishDataError.__init__E   r/   r0   Nr1   r5   r9   r0   r-   r?   r?   D   r:   r0   r?   c                      e Zd ZddZy)PublishDTMFErrorc                    || _         y r(   r)   r+   s     r-   r.   zPublishDTMFError.__init__J   r/   r0   Nr1   r5   r9   r0   r-   rB   rB   I   r:   r0   rB   c                      e Zd ZddZy)PublishTranscriptionErrorc                    || _         y r(   r)   r+   s     r-   r.   z"PublishTranscriptionError.__init__O   r/   r0   Nr1   r5   r9   r0   r-   rE   rE   N   r:   r0   rE   c                      e Zd ZddZeedd              Zedd       Zedd       Zedd       Z	edd       Z
edd       Zedd       Zedd	       Ze	 	 dd
       Zy)Participantc                n    |j                   | _        t        |j                  j                        | _        y r(   )info_infor   handleid_ffi_handle)r,   
owned_infos     r-   r.   zParticipant.__init__T   s&    __
$Z%6%6%9%9:r0   c                     y)U
        A dictionary of track publications associated with the participant.
        Nr9   r,   s    r-   track_publicationszParticipant.track_publicationsX   s     	r0   c                .    | j                   j                  S r(   )rK   sidrR   s    r-   rU   zParticipant.sid`   s    zz~~r0   c                .    | j                   j                  S r(   )rK   namerR   s    r-   rW   zParticipant.named   s    zzr0   c                .    | j                   j                  S r(   )rK   identityrR   s    r-   rY   zParticipant.identityh       zz"""r0   c                .    | j                   j                  S r(   )rK   metadatarR   s    r-   r\   zParticipant.metadatal   rZ   r0   c                @    t        | j                  j                        S )z2Custom attributes associated with the participant.)dictrK   
attributesrR   s    r-   r_   zParticipant.attributesp   s     DJJ))**r0   c                .    | j                   j                  S )zLParticipant's kind (e.g., regular participant, ingress, egress, sip, agent).)rK   kindrR   s    r-   ra   zParticipant.kindu   s     zzr0   c                .    | j                   j                  S )z.The participant's permissions within the room.)rK   
permissionrR   s    r-   permissionszParticipant.permissionsz   s     zz$$$r0   c                    | j                   j                  t        j                  j                  k(  ry| j                   j                  S )a  Reason for the participant's disconnection.

        Returns one of DisconnectReasons or None if the participant isn't disconnected. Common reasons are:
        - CLIENT_INITIATED - the client initiated the disconnect
        - DUPLICATE_IDENTITY - another participant with the same identity has joined the room
        - SERVER_SHUTDOWN - the server instance is shutting down
        - PARTICIPANT_REMOVED - RoomService.RemoveParticipant was called
        - ROOM_DELETED - RoomService.DeleteRoom was called
        - STATE_MISMATCH - the client is attempting to resume a session, but server is not aware of it
        - JOIN_FAILURE - client was unable to connect fully

        When dialing a participant via SIP, you may see the following reasons:
        - USER_UNAVAILABLE - SIP callee did not respond in time
        - USER_REJECTED - SIP callee rejected the call (busy)
        - SIP_TRUNK_FAILURE - SIP protocol failure or unexpected response
        N)rK   disconnect_reasonproto_participantDisconnectReasonUNKNOWN_REASONrR   s    r-   rf   zParticipant.disconnect_reason   s6    ( ::''+<+M+M+\+\\zz+++r0   NrO   "proto_participant.OwnedParticipantr3   r4   )r3   zMapping[str, TrackPublication]r3   r2   )r3   dict[str, str])r3   z+proto_participant.ParticipantKind.ValueType)r3   z'proto_participant.ParticipantPermission)r3   z6Optional[proto_participant.DisconnectReason.ValueType])r6   r7   r8   r.   propertyr   rS   rU   rW   rY   r\   r_   ra   rd   rf   r9   r0   r-   rH   rH   S   s    ;        # # # # + +   % % ,	?, ,r0   rH   r    F)boundc                      e Zd ZdZ	 	 	 	 	 	 d! fdZed"d       Zdg dd	 	 	 	 	 	 	 	 	 d#dZd$dZd%d	Z	d
d	 	 	 	 	 	 	 	 	 d&dZ
	 d'	 	 	 	 	 d(dZd)dZd
d	 	 	 	 	 d*dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d+dZd,dZd-dZd.dZd
dd
d
d
d
d
d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d/dZd
dd
d
d	 	 	 	 	 	 	 	 	 d0dZd
dd
d
d
dd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d1dZdd
d
d
d	 	 	 	 	 	 	 	 	 	 	 d2dZ e       f	 	 	 	 	 d3dZd4dZd5d Z xZS )6LocalParticipantz+Represents the local participant in a room.c                N    t         |   |       || _        i | _        i | _        y r(   )superr.   _room_queue_track_publications_rpc_handlers)r,   
room_queuerO   	__class__s      r-   r.   zLocalParticipant.__init__   s*    
 	$%EG 46r0   c                    | j                   S rQ   rv   rR   s    r-   rS   z#LocalParticipant.track_publications       
 '''r0   T )reliabledestination_identitiestopicc                 
K   t        |t              r|j                  d      }t        |      }t	        j
                  |z  | }t        j                         }| j                  j                  |j                  _        t	        j                  |      |j                  _        ||j                  _        ||j                  _        ||j                  _        |j                  j"                  j%                  |       t&        j(                  j*                  j-                         }	 t&        j(                  j/                  |      
|j1                  
fd       d{   }	t&        j(                  j*                  j3                  |       |	j                  j4                  rt7        |	j                  j4                        y7 c# t&        j(                  j*                  j3                  |       w xY ww)a  
        Publish arbitrary data to the room.

        Args:
            payload (Union[bytes, str]): The data to publish.
            reliable (bool, optional): Whether to send reliably or not. Defaults to True.
            destination_identities (List[str], optional): List of participant identities to send to. Defaults to [].
            topic (str, optional): The topic under which to publish the data. Defaults to "".

        Raises:
            PublishDataError: If there is an error in publishing data.
        zutf-8c                ^    | j                   j                  j                   j                  k(  S r(   )publish_dataasync_ideresps    r-   <lambda>z/LocalParticipant.publish_data.<locals>.<lambda>   s"    !..11T5F5F5O5OO r0   N)
isinstancer2   encodelenctypesc_byte	proto_ffi
FfiRequestrN   rL   r   local_participant_handle	addressofdata_ptrdata_lenr   r   r   extendr   instancequeue	subscriberequestwait_forunsubscribeerrorr?   )r,   payloadr   r   r   r   cdatareqr   cbr   s             @r-   r   zLocalParticipant.publish_data   ss    ( gs#nnW-Gw<)G4""$484D4D4K4K1$*$4$4U$;!$,!$,!!&//667MN""((224	8%%--c2D+0>>O, &B $$007??  "2??#8#899 !& $$007s1   DG-#6F? F=F? AG-=F? ?+G**G-c                 K   t        j                         }| j                  j                  |j                  _        ||j                  _        ||j                  _        t        j                  j                  j                         }	 t        j                  j                  |      |j                  fd       d{   }t        j                  j                  j                  |       |j                  j                  rt!        |j                  j                        y7 c# t        j                  j                  j                  |       w xY ww)z
        Publish SIP DTMF message.

        Args:
            code (int): DTMF code.
            digit (str): DTMF digit.

        Raises:
            PublishDTMFError: If there is an error in publishing SIP DTMF message.
        c                ^    | j                   j                  j                   j                  k(  S r(   )publish_sip_dtmfr   r   s    r-   r   z/LocalParticipant.publish_dtmf.<locals>.<lambda>   s$    !,,559N9N9W9WW r0   N)r   r   rN   rL   r   r   codedigitr   r   r   r   r   r   r   r   rB   )r,   r   r   r   r   r   r   s         @r-   publish_dtmfzLocalParticipant.publish_dtmf   s     ""$8<8H8H8O8O5$(!%*"""((224	8%%--c2D+0>>W, &B $$007$$"2#6#6#<#<== %& $$007s1   BE6D$ >D"?D$ AE"D$ $+EEc                  K   t        j                         }|j                  D cg c]O  }t        |j                  |j
                  |j                  |j                  |j                  |j                        Q }}| j                  j                  |j                  _        |j                  |j                  _        |j                  j                  j                  |       |j                   |j                  _        t$        j&                  j(                  j+                         }	 t$        j&                  j-                  |      |j/                  fd       d{   }t$        j&                  j(                  j1                  |       |j                  j2                  rt5        |j                  j2                        yc c}w 7 h# t$        j&                  j(                  j1                  |       w xY ww)a   
        Publish transcription data to the room.

        Args:
            transcription (Transcription): The transcription data to publish.

        Raises:
            PublishTranscriptionError: If there is an error in publishing transcription.
        )rM   text
start_timeend_timefinallanguagec                ^    | j                   j                  j                   j                  k(  S r(   )publish_transcriptionr   r   s    r-   r   z8LocalParticipant.publish_transcription.<locals>.<lambda>  s$    !11::d>X>X>a>aa r0   N)r   r   segmentsProtoTranscriptionSegmentrM   r   r   r   r   r   rN   rL   r   r   participant_identityr   	track_sidtrack_idr   r   r   r   r   r   r   r   rE   )r,   transcriptionr   sproto_segmentsr   r   r   s          @r-   r   z&LocalParticipant.publish_transcription   s     ""$ #++

  &44VV<<gg

 

 >B=M=M=T=T!!:9F9[9[!!6!!**11.A-:-D-D!!*""((224	8%%--c2D+0>>a, &B $$007##))+B,D,D,J,JKK *5

(& $$007s=   #G4AF?:B*G4%6G GG  A$G4G +G11G4N)response_timeoutc               P  K   t        j                         }| j                  j                  |j                  _        ||j                  _        ||j                  _        ||j                  _        |t        |dz        |j                  _
        t        j                  j                  j                         }	 t        j                  j                  |      |j!                  fd       d{   }t        j                  j                  j#                  |       |j                  j%                  d      r)t'        j(                  |j                  j*                        |j                  j                  S 7 # t        j                  j                  j#                  |       w xY ww)a  
        Initiate an RPC call to a remote participant.

        Args:
            destination_identity (str): The `identity` of the destination participant
            method (str): The method name to call
            payload (str): The method payload
            response_timeout (Optional[float]): Timeout for receiving a response after initial connection

        Returns:
            str: The response payload

        Raises:
            RpcError: On failure. Details in `message`.
        Ni  c                ^    | j                   j                  j                   j                  k(  S r(   )perform_rpcr   r   s    r-   r   z.LocalParticipant.perform_rpc.<locals>.<lambda>F  s"    0F0F$JZJZJcJc0c r0   r   )r   r   rN   rL   r   r   destination_identitymethodr   intresponse_timeout_msr   r   r   r   r   r   r   HasFieldr   _from_protor   )	r,   r   r   r   r   r   r   r   r   s	           @r-   r   zLocalParticipant.perform_rpc$  s/    . ""$373C3C3J3J0/C,!'")'256F6M2NCOO/""((224	8%%--c2D~~&cddB$$007>>""7+&&r~~';';<<~~%%% e$$007s1   B4F&86E8 .E6/E8 3BF&6E8 8+F##F&c                ,     d fd}| ||      S |S )a  
        Establishes the participant as a receiver for calls of the specified RPC method.
        Can be used either as a decorator or a regular method.

        The handler will receive one argument of type `RpcInvocationData` and should return a string response which will be forwarded back to the caller.

        The handler may be synchronous or asynchronous.

        If unable to respond within `response_timeout`, the caller will hang up and receive an error on their side.

        You may raise errors of type `RpcError` in the handler, and they will be forwarded to the caller.

        Other errors raised in your handler will be caught and forwarded to the caller as "1500 Application Error".

        Args:
            method_name (str): The name of the indicated RPC method.
            handler (Optional[Callable]): Handler to be invoked whenever an RPC request for this method is received.  Omit this argument to use the decorator syntax.

        Returns:
            None (when used as a decorator it returns the decorator function)

        Example:
            # As a decorator:
            @room.local_participant.register_rpc_method("greet")
            async def greet_handler(data: RpcInvocationData) -> str:
                print(f"Received greeting from {data.caller_identity}: {data.payload}")
                return f"Hello, {data.caller_identity}!"

            # As a regular method:
            async def greet_handler(data: RpcInvocationData) -> str:
                print(f"Received greeting from {data.caller_identity}: {data.payload}")
                return f"Hello, {data.caller_identity}!"

            room.local_participant.register_rpc_method('greet', greet_handler)
        c                    | j                   <   t        j                         }j                  j                  |j
                  _        |j
                  _        t        j                  j                  |       | S r(   )rw   r   r   rN   rL   register_rpc_methodr   r   r   r   r   )handler_funcr   method_namer,   s     r-   registerz6LocalParticipant.register_rpc_method.<locals>.registerx  sc    .:D{+&&(C?C?O?O?V?VC##<-8C##*&&s+r0   )r   ro   r3   ro   r9   )r,   r   handlerr   s   ``  r-   r   z$LocalParticipant.register_rpc_methodO  s!    R	  G$$Or0   c                   | j                   j                  |d       t        j                         }| j                  j
                  |j                  _        ||j                  _        t        j                  j                  |       y)z
        Unregisters a previously registered RPC method.

        Args:
            method (str): The name of the RPC method to unregister
        N)rw   popr   r   rN   rL   unregister_rpc_methodr   r   r   r   r   )r,   r   r   s      r-   r   z&LocalParticipant.unregister_rpc_method  sd     	vt,""$=A=M=M=T=T!!:+1!!(""3'r0   )participant_permissionsc               (   |g }t        j                         }| j                  j                  |j                  _        ||j                  _        |j                  j                  j                  |       t        j                  j                  |       y)a  
        Set the track subscription permissions for the local participant.

        Args:
            allow_all_participants (bool): Whether to allow all participants to subscribe to this participant's tracks.
            participant_permissions (List[ParticipantTrackPermission]): Participant-specific track subscription permissions, ignored if `allow_all_participants` is True.
        N)r   r   rN   rL   "set_track_subscription_permissionsr   all_participants_allowedrd   r   r   r   r   )r,   allow_all_participantsr   r   s       r-   r   z3LocalParticipant.set_track_subscription_permissions  su     #*&(#""$JNJZJZJaJa..GJ`..G..::AABYZ""3'r0   c                  K   d }d }t        ||||      }	| j                  j                  |      }
|
s.t        j                  t        j
                  j                        }nZ	 t        j                  |
      r'	 t        j                   |
|	      |       d {   }nt        t        t             |
|	            }t+        j,                  t/        | j0                  j2                  ||r|j5                         nd |            }t6        j8                  j;                  |      }|j<                  j>                  r/|j<                  j>                  }t%        j>                  d|        y y 7 # t        j                  $ r. t        j                  t        j
                  j                        t        j                  $ r. t        j                  t        j
                  j                        w xY w# t        $ r}|}Y d }~Dd }~wt"        $ rJ t%        j&                  d| d       t        j                  t        j
                  j(                        }Y w xY ww)N)timeoutz+Uncaught error returned by RPC handler for z'. Returning APPLICATION_ERROR instead. )r   invocation_idr   r   )rpc_method_invocation_responsez.error sending rpc method invocation response: ) r    rw   getr   	_built_in	ErrorCodeUNSUPPORTED_METHODasyncioiscoroutinefunctionr   TimeoutErrorRESPONSE_TIMEOUTCancelledErrorRECIPIENT_DISCONNECTEDr   r	   r2   	Exceptionr   	exceptionAPPLICATION_ERRORr   r   r   rN   rL   	_to_protor   r   r   r   r   )r,   r   r   
request_idcaller_identityr   r   response_errorresponse_payloadparamsr   r   r   reserrs                  r-   _handle_rpc_method_invocationz.LocalParticipant._handle_rpc_method_invocation  s     .2*.":IYZ$$((0%//0B0B0U0UVNZ..w7\181A1A#FO5E2 ,( (,HSM76?'K$ ""+M)-)9)9)@)@+4Bn..0(	,
   ((---3344::CLLI#OP 4;, #// V&001C1C1T1TUU"11 \&001C1C1Z1Z[[\  '!& Z  A& J< < "*!3!3H4F4F4X4X!YZsi   AI!G- 7 E* E(E* G- :B.I(E* *B G**G- -	I6G>8I>AIIIIc                  K   t        j                         }| j                  j                  |j                  _        ||j                  _        t        j                  j                  j                         }	 t        j                  j                  |      |j                  fd       d{    t        j                  j                  j                  |       y7 .# t        j                  j                  j                  |       w xY ww)z
        Set the metadata for the local participant.

        Note: this requires `canUpdateOwnMetadata` permission.

        Args:
            metadata (str): The new metadata.
        c                ^    | j                   j                  j                   j                  k(  S r(   )set_local_metadatar   r   s    r-   r   z/LocalParticipant.set_metadata.<locals>.<lambda>  s$    !..774;R;R;[;[[ r0   N)r   r   rN   rL   r   r   r\   r   r   r   r   r   r   r   )r,   r\   r   r   r   s       @r-   set_metadatazLocalParticipant.set_metadata  s      ""$:>:J:J:Q:Q7*2'""((224	8%%--c2D..[   $$007	 $$0070   A3D76C -C.C 2*DC +D		Dc                  K   t        j                         }| j                  j                  |j                  _        ||j                  _        t        j                  j                  j                         }	 t        j                  j                  |      |j                  fd       d{    t        j                  j                  j                  |       y7 .# t        j                  j                  j                  |       w xY ww)z
        Set the name for the local participant.

        Note: this requires `canUpdateOwnMetadata` permission.

        Args:
            name (str): The new name.
        c                ^    | j                   j                  j                   j                  k(  S r(   )set_local_namer   r   s    r-   r   z+LocalParticipant.set_name.<locals>.<lambda>  s$    !**33t7J7J7S7SS r0   N)r   r   rN   rL   r   r   rW   r   r   r   r   r   r   r   )r,   rW   r   r   r   s       @r-   set_namezLocalParticipant.set_name  s      ""$6:6F6F6M6M3"&""((224	8%%--c2D..S   $$007	 $$007r   c                *  K   t        j                         }| j                  j                  |j                  _        |j                  j                  D ci c]  }|j                  |j                   }}|j                  |       |j                         D ]7  \  }}|j                  j                  j                         }||_        ||_        9 t        j                  j                  j                         }	 t        j                  j!                  |      |j#                  fd       d{    t        j                  j                  j%                  |       yc c}w 7 3# t        j                  j                  j%                  |       w xY ww)z
        Set custom attributes for the local participant.

        Note: this requires `canUpdateOwnMetadata` permission.

        Args:
            attributes (dict[str, str]): A dictionary of attributes to set.
        c                ^    | j                   j                  j                   j                  k(  S r(   )set_local_attributesr   r   s    r-   r   z1LocalParticipant.set_attributes.<locals>.<lambda>&  s$    !0099T=V=V=_=__ r0   N)r   r   rN   rL   r   r   r_   keyvalueupdateitemsaddr   r   r   r   r   r   r   )	r,   r_   r   entryexisting_attributesr   r   r   r   s	           @r-   set_attributeszLocalParticipant.set_attributes  sL     ""$<@<L<L<S<S  9030H0H0S0S
',EIIu{{"
 
 	"":.-335 	 JC,,77;;=EEIEK	 
 ""((224	8%%--c2D.._   $$007#
 $$007s=   AFE3BF96E% /E#0E% 4/F#E% %+FF)r   r   r_   	stream_idreply_to_id
total_sizesender_identityc          
     h   K   t        | |||||||      }|j                          d{    |S 7 w)z
        Returns a TextStreamWriter that allows to write individual chunks of text to a text stream.
        In most cases where you want to simply send a text message use send_text() instead.
        )r   r_   r   r   r   r   r  N)r!   _send_header)	r,   r   r   r_   r   r   r   r  writers	            r-   stream_textzLocalParticipant.stream_text+  sH      "!##9!+	
 !!### 	$   '202)r   r   r_   r   c                  K   t        |j                               }| j                  |||||       d {   }|j                  |       d {    |j	                          d {    |j
                  S 7 A7 *7 w)N)r   r   r_   r   r   )r   r   r  writeacloserJ   )r,   r   r   r   r_   r   r   r  s           r-   	send_textzLocalParticipant.send_textI  s}      '
''#9!#! ( 
 
 ll4   mmo{{
 	!s3   3A=A7A=A9A=&A;'A=9A=;A=application/octet-stream)r   	mime_typer_   r   r   r   c          
     h   K   t        | |||||||      }|j                          d{    |S 7 w)z
        Returns a ByteStreamWriter that allows to write individual chunks of bytes to a byte stream.
        In cases where you want to simply send a file from the file system use send_file() instead.
        )rW   r_   r   r   r  r   r   N)r"   r  )	r,   rW   r   r  r_   r   r   r   r  s	            r-   stream_byteszLocalParticipant.stream_bytes`  sH      "!!#9	
 !!### 	$r  )r   r   r_   r   c          	       K   t         j                  j                  |      }t         j                  j                  |      }t	        j
                  |      \  }}	|d}| j                  |||||||       d {   }
t        j                  |d      4 d {   }|j                  t               d {   x}r9|
j                  |       d {    |j                  t               d {   x}r9d d d       d {    |
j                          d {    |
j                  S 7 7 7 w7 ^7 C7 2# 1 d {  7  sw Y   BxY w7 1w)Nr  )rW   r   r  r_   r   r   r   rb)ospathgetsizebasename	mimetypes
guess_typer  aiofilesopenreadr$   r  r	  rJ   )r,   	file_pathr   r   r_   r   	file_size	file_namer  _r  fbytess                r-   	send_filezLocalParticipant.send_file~  s6     GGOOI.	GG$$Y/	 ++I6	12I)-):): !#9 *; *
 $
 ==D1 	* 	*Q!"(9!:::%:ll5))) "#(9!:::%:	* 	* mmo{{$
	*:) ;	* 	* 	* 	* 	s   A6E8D(9ED*ED44D,5D4D.D4,D0-D44E?D2 EE	E*E,D4.D40D42E4E:D=;EEc                z  K   t        j                         }|j                  j                  |j                  _        | j                  j                  |j                  _        |j                  j                  j                  |       | j                  j                         }	 t        j                  j                  |      |j                  fd       d{   }|j                  j                  rt!        |j                  j                        t#        |j                  j$                        }||_        |j(                  |j*                  _        || j,                  |j(                  <   |j/                          || j                  j1                  |       S 7 # | j                  j1                  |       w xY ww)a  
        Publish a local track to the room.

        Args:
            track (LocalTrack): The track to publish.
            options (TrackPublishOptions, optional): Options for publishing the track.

        Returns:
            LocalTrackPublication: The publication of the published track.

        Raises:
            PublishTrackError: If there is an error in publishing the track.
        c                ^    | j                   j                  j                   j                  k(  S r(   )publish_trackr   r   s    r-   r   z0LocalParticipant.publish_track.<locals>.<lambda>  s"    !//22d6H6H6Q6QQ r0   N)r   r   rN   rL   r#  track_handler   optionsCopyFromru   r   r   r   r   r   r   r&   r   publication_trackrU   rK   rv   	task_doner   )r,   trackr%  r   r   r   track_publicationr   s          @r-   r#  zLocalParticipant.publish_track  sW      ""$).):):)A)A&595E5E5L5L2!!**73  **,	0%%--c2D+0>>Q, &B %%'(8(8(>(>?? 5b6F6F6R6R S',$/33EKKO>OD$$%6%:%:;OO$((/& ((/s1   BF;"6F FB$F =F;F F88F;c                  K   t        j                         }| j                  j                  |j                  _        ||j                  _        d|j                  _        | j                  j                         }	 t        j                  j                  |      |j                  fd       d{   }|j                  j                  rt        |j                  j                        | j                   j#                  |      }d|_        |j'                          | j                  j)                  |       y7 # | j                  j)                  |       w xY ww)z
        Unpublish a track from the room.

        Args:
            track_sid (str): The SID of the track to unpublish.

        Raises:
            UnpublishTrackError: If there is an error in unpublishing the track.
        Tc                ^    | j                   j                  j                   j                  k(  S r(   )unpublish_trackr   r   s    r-   r   z2LocalParticipant.unpublish_track.<locals>.<lambda>  s$    !++448L8L8U8UU r0   N)r   r   rN   rL   r.  r   r   stop_on_unpublishru   r   r   r   r   r   r   r<   rv   r   r(  r)  r   )r,   r   r   r   r   r'  r   s         @r-   r.  z LocalParticipant.unpublish_track  s     ""$7;7G7G7N7N4(1%04-  **,	0%%--c2D+0>>U, &B !!'')"*<*<*B*BCC2266yAK!%KOO((/& ((/s1   A6E:6D: 0D81A+D: E8D: :EEc                V    d| j                    d| j                   d| j                   dS )Nzrtc.LocalParticipant(sid=, identity=, name=)rU   rY   rW   rR   s    r-   __repr__zLocalParticipant.__repr__  s.    *488*KgVZV_V_U``abbr0   )rx   z"BroadcastQueue[proto_ffi.FfiEvent]rO   rk   r3   r4   )r3   z#Mapping[str, LocalTrackPublication])
r   zUnion[bytes, str]r   boolr   z	List[str]r   r2   r3   r4   )r   r   r   r2   r3   r4   )r   r   r3   r4   )
r   r2   r   r2   r   r2   r   zOptional[float]r3   r2   r(   )r   r2   r   zOptional[F]r3   zUnion[F, Callable[[F], F]])r   r2   r3   r4   )r   r6  r   z*Optional[List[ParticipantTrackPermission]]r3   r4   )r   r   r   r2   r   r2   r   r2   r   r2   r   floatr3   r4   )r\   r2   r3   r4   )rW   r2   r3   r4   )r_   rm   r3   r4   )r   Optional[List[str]]r   r2   r_   Optional[Dict[str, str]]r   
str | Noner   r:  r   
int | Noner  r:  r3   r!   )
r   r2   r   r8  r   r2   r_   r9  r   r:  )rW   r2   r   r;  r  r2   r_   r9  r   r:  r   r8  r   r2   r3   r"   )r  r2   r   r2   r   r8  r_   r9  r   r:  r3   r#   )r*  r   r%  r   r3   r   )r   r2   r3   r4   rl   )r6   r7   r8   __doc__r.   rn   rS   r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r   r   r#  r.  r5  __classcell__ry   s   @r-   rr   rr      s   5767 77 
	7 ( ( ,.,:",: 	,:
 !*,: ,: 
,:\>8&L\ -1)& ")& 	)&
 )& *)& 
)&\  $44 4 
$	4l(& OS	( !%( "L	(
 
(,5Q5Q 5Q 	5Q
 5Q 5Q  5Q 
5Qn8,8,8B 7;/3 $"&!%&* !4 	
 -     $ 
D 7;/3"& !4	
  -  6 "&3/3 $6: 	
  -  !4  
D 6:/3 $ 	
 !4 -  
B AT@U'0'0*='0	'0R0>cr0   rr   c                  :     e Zd Zd fdZedd       ZddZ xZS )RemoteParticipantc                2    t         |   |       i | _        y r(   )rt   r.   rv   )r,   rO   ry   s     r-   r.   zRemoteParticipant.__init__  s    $FH r0   c                    | j                   S r{   r|   rR   s    r-   rS   z$RemoteParticipant.track_publications  r}   r0   c                V    d| j                    d| j                   d| j                   dS )Nzrtc.RemoteParticipant(sid=r1  r2  r3  r4  rR   s    r-   r5  zRemoteParticipant.__repr__  s.    +DHH:[wW[W`W`Vaabccr0   rj   )r3   z$Mapping[str, RemoteTrackPublication]rl   )r6   r7   r8   r.   rn   rS   r5  r=  r>  s   @r-   r@  r@    s#    I ( (dr0   r@  )D
__future__r   r   r   r  r  r  typingr   r   r   r   r   r	   r
   r   r   abcr   r   _ffi_clientr   r   _protor   r   r   rg   _proto.room_pb2r   r   r   _proto.track_pb2r   _utilsr   r*  r   r+  r   r   r   r   r   rpcr   _proto.rpc_pb2r   logr   r    data_streamr!   r"   r#   r$   r   r&   r<   r?   rB   rE   rH   r2   
RpcHandlerro   rr   r@  r9   r0   r-   <module>rQ     s6   #   	   [ [ [ # - ( 8 #  
 )  >  " 	 
) 
y 
y 
	 
B,# B,J *+U9Xc]3KXVY]3Z-[[\
x*+U9Xc]3KXVY]3Z-[[\
I	c{ I	cXd dr0   