o
    iF                     @   s   d Z ddl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mZmZmZmZmZ ddlmZ G d	d
 d
eZdS )z8Vonage Audio Connector WebSocket serializer for Pipecat.    N)Optional)logger)KeypadEntry)create_stream_resampler)AudioRawFrameFrameInputAudioRawFrameInputDTMFFrameInterruptionFrameOutputTransportMessageFrame!OutputTransportMessageUrgentFrame
StartFrame)FrameSerializerc                       s   e Zd ZdZG dd dejZddee f fddZdefd	d
Z	de
deeB dB fddZdeeB de
dB fddZ  ZS )VonageFrameSerializera  Serializer for Vonage Video API Audio Connector WebSocket protocol.

    This serializer converts between Pipecat frames and the Vonage Audio Connector
    WebSocket streaming protocol.

    Note:
        Ref docs: https://developer.vonage.com/en/video/guides/audio-connector
    c                   @   s.   e Zd ZU dZdZeed< dZee ed< dS )z!VonageFrameSerializer.InputParamsa}  Configuration parameters for VonageFrameSerializer.

        Parameters:
            vonage_sample_rate: Sample rate used by Vonage, defaults to 16000 Hz.
                Common values: 8000, 16000, 24000 Hz.
            sample_rate: Optional override for pipeline input sample rate.
            ignore_rtvi_messages: Inherited from base FrameSerializer, defaults to True.
        i>  vonage_sample_rateNsample_rate)	__name__
__module____qualname____doc__r   int__annotations__r   r    r   r   N/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/serializers/vonage.pyInputParams'   s   
 	r   Nparamsc                    s8   t  |pt  | jj| _d| _t | _	t | _
dS )zkInitialize the VonageFrameSerializer.

        Args:
            params: Configuration parameters.
        r   N)super__init__r   r   _paramsr   _vonage_sample_rate_sample_rater   _input_resampler_output_resampler)selfr   	__class__r   r   r   4   s
   
zVonageFrameSerializer.__init__framec                    s   | j jp|j| _dS )zSets up the serializer with pipeline configuration.

        Args:
            frame: The StartFrame containing pipeline configuration.
        N)r   r   audio_in_sample_rater    )r#   r&   r   r   r   setupB   s   zVonageFrameSerializer.setupreturnc                    s   t |trddi}t|S t |tr2|j}| j||j| j	I dH }|du s.t
|dkr0dS |S t |ttfrF| |r@dS t|jS dS )aP  Serializes a Pipecat frame to Vonage WebSocket format.

        Handles conversion of various frame types to Vonage WebSocket messages.

        Args:
            frame: The Pipecat frame to serialize.

        Returns:
            Serialized data as string (JSON commands) or bytes (audio), or None if the frame isn't handled.
        actionclearNr   )
isinstancer
   jsondumpsr   audior"   resampler   r   lenr   r   should_ignore_framemessage)r#   r&   answerdataserialized_datar   r   r   	serializeJ   s"   





zVonageFrameSerializer.serializer5   c                    s  t |tr+|}| j|| j| jI dH }|du st|dkr!dS t|d| jd}|S zt	|}|
d}|dkrHtd|
d  W dS |d	krTtd
 W dS |dkrftd|
d  W dS |dkr|
dpw|
di 
d}|du rtd|  W dS t|}td|  ztt|W W S  ty   td|  Y W dS w td|  W dS  tjy   td|  Y dS w )a  Deserializes Vonage WebSocket data to Pipecat frames.

        Handles conversion of Vonage events to appropriate Pipecat frames.
        - Binary messages contain audio data (16-bit linear PCM)
        - Text messages contain JSON events (websocket:connected, websocket:cleared, dtmf, etc.)

        Args:
            data: The raw WebSocket data from Vonage.

        Returns:
            A Pipecat frame corresponding to the Vonage event, or None if unhandled.
        Nr      )r/   num_channelsr   eventzwebsocket:connectedz)Vonage WebSocket connected: content-type=zcontent-typezwebsocket:clearedzVonage audio buffer clearedzwebsocket:notifyzVonage notify event: payloadzwebsocket:dtmfdigitdtmfz(DTMF event received but no digit found: zReceived DTMF digit: zInvalid DTMF digit received: zVonage event: z*Failed to parse JSON message from Vonage: )r,   bytesr!   r0   r   r    r1   r   r-   loadsgetr   debugwarningstrr	   r   
ValueErrorJSONDecodeError)r#   r5   r;   deserialized_dataaudio_framer3   r:   r<   r   r   r   deserializen   sb   




z!VonageFrameSerializer.deserialize)N)r   r   r   r   r   r   r   r   r   r(   r   rC   r>   r7   rH   __classcell__r   r   r$   r   r      s    	"$r   )r   r-   typingr   logurur   pipecat.audio.dtmf.typesr   pipecat.audio.utilsr   pipecat.frames.framesr   r   r   r	   r
   r   r   r   #pipecat.serializers.base_serializerr   r   r   r   r   r   <module>   s   (
