o
    i                     @   s&   d dl Z d dlmZ G dd dZdS )    N)Optionalc                   @   s^   e Zd ZdZddefddZddd	Zdefd
dZdefddZ	dddZ
defddZdS )SmallWebRTCSessionManageraY  
    Manages the waiting logic between PipecatSession and SmallWebRTCSession.

    This class provides:
      - A mechanism to wait for a WebRTC connection within a timeout period.
      - Automatic cancellation if no connection is established in time.
      - A way to complete the waiting future once the session has successfully started.
    x   timeout_secondsc                 C   s   d | _ d | _|| _d S )N)_pending_future_timeout_task_timeout_seconds)selfr    r
   \/home/ubuntu/.local/lib/python3.10/site-packages/pipecatcloud/smallwebrtc/session_manager.py__init__   s   
z"SmallWebRTCSessionManager.__init__returnNc                    s^    j dur
tdt  _  fdd}t|  _z j I dH  W    dS    w )a  
        Creates and waits for a future to be completed when WebRTC connection arrives.

        Raises:
            TimeoutError: If WebRTC connection is not received within the timeout period
            RuntimeError: If another waiting operation is already in progress
        Nz%Already waiting for WebRTC connectionc                      sJ   t  jI d H   jr! j s# jtd j d d S d S d S )Nz&WebRTC connection not received within z seconds)asynciosleepr   r   doneset_exceptionTimeoutErrorr
   r	   r
   r   timeout_handler!   s   zBSmallWebRTCSessionManager.wait_for_webrtc.<locals>.timeout_handler)r   RuntimeErrorr   Futurecreate_taskr   _cleanup)r	   r   r
   r   r   wait_for_webrtc   s   


z)SmallWebRTCSessionManager.wait_for_webrtcc                 C   s,   | j r| j  s|   | j d dS dS )z
        Completes the waiting future.

        Returns:
            bool: True if the future was found and completed, False otherwise
        TF)r   r   cancel_timeout
set_resultr   r
   r
   r   complete_session2   s
   z*SmallWebRTCSessionManager.complete_sessionc                 C   s"   | j r| j  s| j   dS dS )z
        Cancels the timeout handler without completing the future.

        Returns:
            bool: True if the timeout task was found and cancelled, False otherwise
        TF)r   r   cancelr   r
   r
   r   r   A   s   
z(SmallWebRTCSessionManager.cancel_timeoutc                 C   s   d| _ d| _dS )zCleans up all resources.N)r   r   r   r
   r
   r   r   M   s   
z"SmallWebRTCSessionManager._cleanupc                 C   s   | j duo
| j   S )z
        Checks if currently waiting for WebRTC connection.

        Returns:
            bool: True if waiting for connection, False otherwise
        N)r   r   r   r
   r
   r   
is_waitingR   s   z$SmallWebRTCSessionManager.is_waiting)r   )r   N)__name__
__module____qualname____doc__intr   r   boolr   r   r   r   r
   r
   r
   r   r      s    	

r   )r   typingr   r   r
   r
   r
   r   <module>   s    