o
    i                     @  s   d dl mZ d dlZd dlmZ d dlmZ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mZ ddlmZ ddlmZ ddlmZ eG dd dZG dd dZdS )    )annotationsN)	dataclass)AnyAsyncIteratorOptional   )	FfiClient	FfiHandle)ffi_pb2)video_frame_pb2)TrackSource)	RingQueuetask_done_logger)Participant)Track)
VideoFramec                   @  s&   e Zd ZU ded< ded< ded< dS )VideoFrameEventr   frameinttimestamp_uszproto_video_frame.VideoRotationrotationN)__name__
__module____qualname____annotations__ r   r   Z/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/rtc/video_stream.pyr      s   
 r   c                   @  s   e Zd ZdZ			d/d0ddZeddddd1ddZeddddd2ddZd3ddZd4ddZ	d5ddZ
d3d d!Zd3d"d#Zd6d'd(Zd7d*d+Zd8d-d.ZdS )9VideoStreamzDVideoStream is a stream of video frames received from a RemoteTrack.Nr   trackr   loop#Optional[asyncio.AbstractEventLoop]capacityr   format5Optional[proto_video_frame.VideoBufferType.ValueType]returnNonec                 K  s   |pt  | _tjjj| jdd d| _t|| _	|| _
|| _|| _|| _d }d|v r7| j|d |d d}n|  }t|jj| _|j| _| j|  | _| jt d S )Nc                 S  s   |  ddkS )Nmessagevideo_stream_event)
WhichOneof)er   r   r   <lambda>6   s    z&VideoStream.__init__.<locals>.<lambda>)	filter_fnparticipanttrack_source)r,   r-   )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   )selfr   r   r!   r"   kwargsstreamr   r   r   __init__)   s(   
zVideoStream.__init__)r   r"   r!   r,   r   r-   TrackSource.ValueTypec                C  s   t |||||d dS )N)r,   r-   r   r!   r"   r   r   )clsr,   r-   r   r"   r!   r   r   r   from_participantK   s   
zVideoStream.from_participantc                C  s   t ||||dS )N)r   r   r!   r"   rI   )rJ   r   r   r"   r!   r   r   r   
from_track^   s   	zVideoStream.from_trackc                 C  s   t jj| j d S N)r   r1   r2   unsubscriber4   rD   r   r   r   __del__n   s   zVideoStream.__del__r   c                 C  s^   | j d usJ t }|j}| j jj|_tjj	|_
| jd ur"| j|_d|_tj|}|jjS NT)r6   	proto_ffi
FfiRequestnew_video_streamr=   r;   track_handleproto_video_frameVideoStreamTypeVIDEO_STREAM_NATIVEtyper7   r"   normalize_strider   r1   requestrF   )rD   reqrT   respr   r   r   r:   q   s   

z VideoStream._create_owned_streamc                 C  sT   t  }|j}|jj|_tjj|_	||_
d|_| jd ur | j|_tj|}|jjS rQ   )rR   rS   video_stream_from_participantr=   r;   participant_handlerV   rW   rX   rY   r-   rZ   r7   r"   r   r1   r[   rF   )rD   r,   r-   r\   r^   r]   r   r   r   r9   }   s   


z1VideoStream._create_owned_stream_from_participantc                   s   	 | j | jI d H }|j}|dr/|jj}t|}t	||jj
|jjd}| j| n|dr5nqtjj| j  d S )NTframe_received)r   r   r   eos)r4   wait_for	_is_eventr'   HasFieldr`   bufferr   _from_owned_infor   r   r   r5   putr   r1   r2   rN   )rD   eventvideo_eventowned_buffer_infor   r   r   r   rA      s"   


zVideoStream._runc                   s   | j   | jI d H  d S rM   )r=   disposerB   rO   r   r   r   aclose   s   
zVideoStream.acloser)   proto_ffi.FfiEventboolc                 C  s   |j j| jjkS rM   )r'   stream_handler=   r;   )rD   r)   r   r   r   rc      s   zVideoStream._is_eventAsyncIterator[VideoFrameEvent]c                 C  s   | S rM   r   rO   r   r   r   	__aiter__   s   zVideoStream.__aiter__r   c                   s0   | j  rt| j I d H }|d u rt|S rM   )rB   doneStopAsyncIterationr5   get)rD   itemr   r   r   	__anext__   s   
zVideoStream.__anext__)Nr   N)
r   r   r   r    r!   r   r"   r#   r$   r%   )r,   r   r-   rH   r   r    r"   r#   r!   r   r$   r   )
r   r   r   r    r"   r#   r!   r   r$   r   )r$   r%   )r$   r   )r,   r   r-   rH   r$   r   )r)   rm   r$   rn   )r$   rp   )r$   r   )r   r   r   __doc__rG   classmethodrK   rL   rP   r:   r9   rA   rl   rc   rq   rv   r   r   r   r   r   &   s0    "






r   )
__future__r   r.   dataclassesr   typingr   r   r   _ffi_clientr   r	   _protor
   rR   r   rV   _proto.track_pb2r   _utilsr   r   r,   r   r   r   video_framer   r   r   r   r   r   r   <module>   s   