
    /j                        S SK Jr  S SKrS SKJr  S SKJrJrJr  SSK	J
r
Jr  SSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSKJr  \ " S S5      5       r " S S5      rg)    )annotationsN)	dataclass)AnyAsyncIteratorOptional   )	FfiClient	FfiHandle)ffi_pb2)video_frame_pb2)TrackSource)	RingQueuetask_done_logger)Participant)Track)
VideoFramec                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg	)
VideoFrameEvent   r   frameinttimestamp_uszproto_video_frame.VideoRotationrotation N)__name__
__module____qualname____firstlineno____annotations____static_attributes__r       I/app/agent/.venv/lib/python3.13/site-packages/livekit/rtc/video_stream.pyr   r      s    --r!   r   c                     \ rS rSrSr   S         SS jjr\SSSS.           SS jj5       r\SSSS.         SS jj5       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)VideoStream&   zDVideoStream is a stream of video frames received from a RemoteTrack.Nr   c                l   U=(       d    [         R                  " 5       U l        [        R                  R
                  R                  U R                  S S9U l        [        U5      U l	        Xl
        X@l        X0l        X@l        S nSU;   a  U R                  US   US   S9nOU R                  5       n[        UR                   R"                  5      U l        UR&                  U l        U R                  R+                  U R-                  5       5      U l        U R.                  R1                  [2        5        g )Nc                *    U R                  S5      S:H  $ )Nmessagevideo_stream_event)
WhichOneof)es    r"   <lambda>&VideoStream.__init__.<locals>.<lambda>6   s    Y 7;O Or!   )	filter_fnparticipanttrack_source)r/   r0   )asyncioget_event_loop_loopr	   instancequeue	subscribe
_ffi_queuer   _queue_track_format	_capacity%_create_owned_stream_from_participant_create_owned_streamr
   handleid_ffi_handleinfo_infocreate_task_run_taskadd_done_callbackr   )selftrackloopcapacityformatkwargsstreams          r"   __init__VideoStream.__init__)   s     5W335
 $,,22<<JJO = 
 :C89L$)!F"??"=1~@V @ F ..0F$V]]%5%56[[
ZZ++DIIK8


$$%56r!   )rI   rK   rJ   c          	         [        UUUUUS S9$ )N)r/   r0   rI   rJ   rK   rH   r$   )clsr/   r0   rI   rK   rJ   s         r"   from_participantVideoStream.from_participantK   s#     #%
 	
r!   c                   [        UUUUS9$ )N)rH   rI   rJ   rK   rQ   )rR   rH   rI   rK   rJ   s        r"   
from_trackVideoStream.from_track^   s     	
 	
r!   c                j    [         R                  R                  R                  U R                  5        g N)r	   r4   r5   unsubscriber7   rG   s    r"   __del__VideoStream.__del__n   s       ,,T__=r!   c                   U R                   c   e[        R                  " 5       nUR                  nU R                   R                  R
                  Ul        [        R                  R                  Ul
        U R                  b  U R                  Ul        SUl        [        R                  R!                  U5      nUR                  R"                  $ NT)r9   	proto_ffi
FfiRequestnew_video_streamr@   r>   track_handleproto_video_frameVideoStreamTypeVIDEO_STREAM_NATIVEtyper:   rK   normalize_strider	   r4   requestrM   )rG   reqrb   resps       r"   r=    VideoStream._create_owned_streamq   s    {{&&&""$//(,(?(?(F(F% 1 A A U U<<#&*ll#,0)!!))#.$$+++r!   c                x   [         R                  " 5       nUR                  nUR                  R                  Ul        [        R                  R                  Ul	        X$l
        SUl        U R                  b  U R                  Ul        [        R                  R!                  U5      nUR                  R"                  $ r_   )r`   ra   video_stream_from_participantr@   r>   participant_handlerd   re   rf   rg   r0   rh   r:   rK   r	   r4   ri   rM   )rG   r/   r0   rj   rn   rk   s         r"   r<   1VideoStream._create_owned_stream_from_participant}   s     ""$(+(I(I%;F;R;R;Y;Y%8->-N-N-b-b%*5A29=%6<<#37<<)0!!))#.11888r!   c                >  #     U R                   R                  U R                  5      I S h  vN nUR                  nUR	                  S5      (       a{  UR
                  R                  n[        R                  " U5      n[        UUR
                  R                  UR
                  R                  S9nU R                  R                  U5        OUR	                  S5      (       a  OM  [        R                  R                   R#                  U R                   5        g  N7f)Nframe_received)r   r   r   eos)r7   wait_for	_is_eventr)   HasFieldrr   bufferr   _from_owned_infor   r   r   r8   putr	   r4   r5   rZ   )rG   eventvideo_eventowned_buffer_infor   s        r"   rD   VideoStream._run   s     //224>>BBE22K##$455$/$>$>$E$E!"334EF'!,!;!;!H!H(77@@ &%%e,,! $ 	  ,,T__=# Cs   *DDC/Dc                l   #    U R                   R                  5         U R                  I S h  vN   g  N7frY   )r@   disposerE   r[   s    r"   acloseVideoStream.aclose   s$       "jjs   *424c                \    UR                   R                  U R                  R                  :H  $ rY   )r)   stream_handler@   r>   )rG   r+   s     r"   ru   VideoStream._is_event   s%    ##11T5E5E5L5LLLr!   c                    U $ rY   r   r[   s    r"   	__aiter__VideoStream.__aiter__   s    r!   c                   #    U R                   R                  5       (       a  [        eU R                  R	                  5       I S h  vN nUc  [        eU$  N7frY   )rE   doneStopAsyncIterationr8   get)rG   items     r"   	__anext__VideoStream.__anext__   sD     ::??$$[[__&&<$$	 's   AAAA)	r;   r@   r7   r:   rB   r3   r8   rE   r9   )Nr   N)
rH   r   rI   #Optional[asyncio.AbstractEventLoop]rJ   r   rK   5Optional[proto_video_frame.VideoBufferType.ValueType]returnNone)r/   r   r0   TrackSource.ValueTyperI   r   rK   r   rJ   r   r   r$   )
rH   r   rI   r   rK   r   rJ   r   r   r$   )r   r   )r   r   )r/   r   r0   r   r   r   )r+   zproto_ffi.FfiEventr   bool)r   zAsyncIterator[VideoFrameEvent])r   r   )r   r   r   r   __doc__rN   classmethodrS   rV   r\   r=   r<   rD   r   ru   r   r   r    r   r!   r"   r$   r$   &   s6   N
 59HL 7 7 2 7 	 7
 F 7 
 7D  59HL
 !
 ,	

 2
 F
 
 

 
$ 
 59HL
 
 2	

 F
 
 

 
>
,9&96K9	9>*Mr!   r$   )
__future__r   r1   dataclassesr   typingr   r   r   _ffi_clientr	   r
   _protor   r`   r   rd   _proto.track_pb2r   _utilsr   r   r/   r   rH   r   video_framer   r   r$   r   r!   r"   <module>r      sS    #  ! / / - ( 8 ) / $  # . . .L Lr!   