
    /j$                        S SK Jr  S SKJr  S SKJrJr  SSKJrJ	r	  SSK
Jr  SSK
Jr   " S S	\5      r " S
 S\5      r\ " S S5      5       r\ " S S5      5       r " S S5      r " S S5      r " S S5      rg)    )annotations)	dataclass)AsyncIteratorOptional   )	FfiClient	FfiHandle)ffi_pb2)data_track_pb2c                  "    \ rS rSrSrSS jrSrg)SubscribeDataTrackError   z9An error that can occur when subscribing to a data track.c                    Xl         g Nmessageselfr   s     G/app/agent/.venv/lib/python3.13/site-packages/livekit/rtc/data_track.py__init__ SubscribeDataTrackError.__init__           r   Nr   strreturnNone__name__
__module____qualname____firstlineno____doc__r   __static_attributes__ r   r   r   r      s
    Cr   r   c                  "    \ rS rSrSrSS jrSrg)PushFrameError    zFrame could not be pushed to a data track.

Pushing a frame can fail for several reasons:

- The track has been unpublished by the local participant or SFU
- The room is no longer connected
- Frames are being pushed too fast
c                    Xl         g r   r   r   s     r   r   PushFrameError.__init__*   r   r   r   Nr   r   r%   r   r   r'   r'       s    r   r'   c                  <    \ rS rSr% SrS\S'    S\S'    S\S'   Srg	)
DataTrackInfo.   z)Information about a published data track.r   sidnamebool	uses_e2eer%   N)r   r    r!   r"   r#   __annotations__r$   r%   r   r   r,   r,   .   s!    3	H I6OLr   r,   c                  4    \ rS rSr% SrS\S'    SrS\S'   Srg)	DataTrackFrame@   zQA frame published on a data track, consisting of a payload and optional metadata.bytespayloadNOptional[int]user_timestampr%   )r   r    r!   r"   r#   r2   r9   r$   r%   r   r   r4   r4   @   s    [N$(NM(;r   r4   c                  ^    \ rS rSrSrSS jr\SS j5       rSS jrSS jr	SS jr
SS jrS	rg
)LocalDataTrackK   z.Data track published by the local participant.c                    [        UR                  R                  UR                  R                  UR                  R                  S9U l        [        UR                  R                  5      U l	        g N)r.   r/   r1   )
r,   infor.   r/   r1   _infor	   handleid_ffi_handler   
owned_infos     r   r   LocalDataTrack.__init__N   sP    "##%% oo//


 %Z%6%6%9%9:r   c                    U R                   $ z!Information about the data track.r@   r   s    r   r?   LocalDataTrack.infoV        zzr   c                   [         R                  " [        UR                  5      S9nUR                  b  UR                  Ul        [
        R                  " 5       nU R                  R                  UR                  l
        UR                  R                  R                  U5        [        R                  R                  U5      nUR                  R!                  S5      (       a)  [#        UR                  R$                  R&                  5      eg)zTry pushing a frame to subscribers of the track.

See :class:`DataTrackFrame` for how to construct a frame and attach metadata.

Args:
    frame: The data track frame to send.

Raises:
    PushFrameError: If the push fails.
)r7   Nerror)proto_data_trackr4   r6   r7   r9   	proto_ffi
FfiRequestrC   rA   local_data_track_try_pushtrack_handleframeCopyFromr   instancerequestHasFieldr'   rN   r   )r   rT   proto_framereqresps        r   try_pushLocalDataTrack.try_push[   s     '55eEMM>RS+).)=)=K&""$595E5E5L5L%%2%%++44[A!!))#.))227;; !?!?!E!E!M!MNN <r   c                    [         R                  " 5       nU R                  R                  UR                  l        [        R                  R                  U5      nUR                  R                  $ z,Whether or not the track is still published.)
rP   rQ   rC   rA   local_data_track_is_publishedrS   r   rV   rW   is_publishedr   rZ   r[   s      r   ra   LocalDataTrack.is_publishedr   sR    ""$9=9I9I9P9P))6!!))#.11>>>r   c                   #    [         R                  " 5       nU R                  R                  UR                  l        [        R                  R                  U5        g7f)zUnpublishes the track.N)	rP   rQ   rC   rA   local_data_track_unpublishrS   r   rV   rW   r   rZ   s     r   	unpublishLocalDataTrack.unpublishz   sA     ""$6:6F6F6M6M&&3""3's   AAc                d    SU R                   R                   SU R                   R                   S3$ )Nzrtc.LocalDataTrack(sid=, name=))r@   r.   r/   rJ   s    r   __repr__LocalDataTrack.__repr__   s*    ((8

?PPQRRr   )rC   r@   N)rE   z$proto_data_track.OwnedLocalDataTrackr   r   r   r,   )rT   r4   r   r   r   r0   r   r   r   r   )r   r    r!   r"   r#   r   propertyr?   r\   ra   rg   rl   r$   r%   r   r   r;   r;   K   s4    8;  O.?(Sr   r;   c                  p    \ rS rSrSrSS jr\SS j5       r\SS j5       rSS.SS jjr	SS	 jr
SS
 jrSrg)RemoteDataTrack   z-Data track published by a remote participant.c                   [        UR                  R                  UR                  R                  UR                  R                  S9U l        [        UR                  R                  5      U l	        UR                  U l        g r>   )r,   r?   r.   r/   r1   r@   r	   rA   rB   rC   publisher_identity_publisher_identityrD   s     r   r   RemoteDataTrack.__init__   s^    "##%% oo//


 %Z%6%6%9%9:#-#@#@ r   c                    U R                   $ rH   rI   rJ   s    r   r?   RemoteDataTrack.info   rL   r   c                    U R                   $ )z4Identity of the participant who published the track.)rx   rJ   s    r   rw   "RemoteDataTrack.publisher_identity   s     '''r   N)buffer_sizec               x   [         R                  " 5       nUb  Xl        [        R                  " 5       nU R
                  R                  UR                  l        UR                  R                  R                  U5        [        R                  R                  U5      n[        UR                  R                  5      $ )a  Subscribes to the data track to receive frames.

Args:
    buffer_size: Maximum number of received frames to buffer internally.
        When ``None``, the default buffer size is used.
        Zero is not a valid buffer size; if a value of zero is provided, it will be clamped to one.

Returns a :class:`DataTrackStream` that yields
:class:`DataTrackFrame` instances as they arrive. If the
subscription encounters an error, it is raised as
:class:`SubscribeDataTrackError` when iteration ends.
)rO   DataTrackSubscribeOptionsr~   rP   rQ   rC   rA   subscribe_data_trackrS   optionsrU   r   rV   rW   DataTrackStreamstream)r   r~   optsrZ   r[   s        r   	subscribeRemoteDataTrack.subscribe   s      99;"*""$040@0@0G0G  -  ((11$7!!))#.t88??@@r   c                    [         R                  " 5       nU R                  R                  UR                  l        [        R                  R                  U5      nUR                  R                  $ r_   )
rP   rQ   rC   rA   remote_data_track_is_publishedrS   r   rV   rW   ra   rb   s      r   ra   RemoteDataTrack.is_published   sR    ""$:>:J:J:Q:Q**7!!))#.22???r   c                ~    SU R                   R                   SU R                   R                   SU R                   S3$ )Nzrtc.RemoteDataTrack(sid=rj   z, publisher_identity=rk   )r@   r.   r/   rx   rJ   s    r   rl   RemoteDataTrack.__repr__   s?    &tzz~~&6gdjjoo=N O""&":":!;1>	
r   )rC   r@   rx   )rE   z%proto_data_track.OwnedRemoteDataTrackr   r   rn   rq   )r~   r8   r   r   ro   )r   r    r!   r"   r#   r   rr   r?   rw   r   ra   rl   r$   r%   r   r   rt   rt      sI    7A   ( ( 9= A0@
r   rt   c                  h    \ rS rSrSrSS jrSS jrSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrSrg)r      a  An active subscription to a remote data track.

Use as an async iterator to receive frames::

    stream = remote_track.subscribe()
    async for frame in stream:
        process(frame.payload)

Dropping or closing the stream unsubscribes from the track.

If subscribing to the track fails, :class:`SubscribeDataTrackError`
is raised when iteration ends instead of a normal ``StopAsyncIteration``.
c                   ^ [        UR                  R                  5      U l        UR                  R                  m[        R
                  R                  R                  U4S jS9U l        SU l	        g )Nc                l   > U R                  S5      S:H  =(       a    U R                  R                  T:H  $ )Nr   data_track_stream_event)
WhichOneofr   stream_handle)e	handle_ids    r   <lambda>*DataTrackStream.__init__.<locals>.<lambda>   s5    Y'+DD I--;;yHIr   )	filter_fnF)
r	   rA   rB   rC   r   rV   queuer   _queue_closed)r   rE   r   s     @r   r   DataTrackStream.__init__   s^    $Z%6%6%9%9:%%((	((..88 9 
 r   c                `   #     U R                  5       I Sh  vN $  N! [         a     gf = f7f)z9Read a single frame, or ``None`` if the stream has ended.N)	__anext__StopAsyncIterationrJ   s    r   readDataTrackStream.read   s.     	))))! 		s(   .  . 
+.+.c                    U $ r   r%   rJ   s    r   	__aiter__DataTrackStream.__aiter__   s    r   c                d  #    U R                   (       a  [        eU R                  5         U R                  R	                  5       I S h  vN nUR
                  nUR                  S5      nUS:X  aN  UR                  R                  nS nUR                  S5      (       a  UR                  n[        UR                  US9$ US:X  aU  U R                  5         UR                  R                  S5      (       a  [        UR                  R                   5      e[        eU R                  5         [        e N7f)Ndetailframe_receivedr9   )r7   r9   eosrN   )r   r   _send_read_requestr   getr   r   r   rT   rX   r9   r4   r7   _closer   r   rN   )r   eventstream_eventr   rY   user_tss         r   r   DataTrackStream.__anext__   s     <<$$!*.++//*;$;44((2%%&55;;K%)G##$455%44!#++&  u_KKM((11-l.>.>.D.DEE$$KKM$$) %<s   AD0D.C'D0c                    [         R                  " 5       nU R                  R                  UR                  l        [        R                  R                  U5        g r   )	rP   rQ   rC   rA   data_track_stream_readr   r   rV   rW   rf   s     r   r   "DataTrackStream._send_read_request  s>    ""$373C3C3J3J""0""3'r   c                    U R                   (       d;  SU l         [        R                  R                  R	                  U R
                  5        g g )NT)r   r   rV   r   unsubscriber   rJ   s    r   r   DataTrackStream._close  s3    ||DL$$00= r   c                X    U R                  5         U R                  R                  5         g)z2Explicitly close the subscription and unsubscribe.N)r   rC   disposerJ   s    r   closeDataTrackStream.close  s      "r   c                ,   #    U R                  5         g 7fr   )r   rJ   s    r   acloseDataTrackStream.aclose  s     

s   )r   rC   r   N)rE   z%proto_data_track.OwnedDataTrackStreamr   r   )r   zOptional[DataTrackFrame])r   zAsyncIterator[DataTrackFrame])r   r4   rp   )r   r    r!   r"   r#   r   r   r   r   r   r   r   r   r$   r%   r   r   r   r      s/    
%6(
>
#
r   r   N)
__future__r   dataclassesr   typingr   r   _ffi_clientr   r	   _protor
   rP   r   rO   	Exceptionr   r'   r,   r4   r;   rt   r   r%   r   r   <module>r      s    # ! * - ( 6i Y  M M M" < < <6S 6Sr:
 :
zP Pr   