Skip to main content

Vonage Audio Connector Server SDK

Project description

Vonage Audio Connector Server SDK for Python

This SDK aims to provide a simple event-driven interface for creating WebSocket servers, that can handle multiple Vonage AudioConnector connections and support JSON text messages and binary audio samples. Main objective is to make it easy for customers to be able to build applications that process real-time audio from Vonage Video API sessions.

Usage

To use the audio connector server sdk to create your own websocket server, access the feature through video by using start_audio_connector_server.

from audio_connector_sdk.vonage_video.models.audio_connector import AudioConnectorServerConfig
from audio_connector_sdk.vonage_video import Video

async def main() -> None:
    video = Video()

    # Define the server configuration
    """Args:
            host (str): The host for server.
            port (int): The port for server.
            ssl (Optional[SSLContext]): The SSL context for secure connections.
            on_start: Optional[Callable[[], Awaitable[None]]] = None: A optional Coroutine to be called once the server has started.
            on_stop: Optional[Callable[[], Awaitable[None]]] = None: A optional Coroutine to be called once the server has stopped.
            on_connect: Optional[Callable[[], Awaitable[None]]] = None: A optional Coroutine to be called when a new client connection is accepted.
    """
    config = AudioConnectorServerConfig(
        host="localhost",
        port=8765,
        ssl=ssl_context,
        on_start=on_start,
        on_stop=on_stop,
        on_connect=on_connect
    )

    # Start the audio connector server
    server_handle = await video.start_audio_connector_server(config)

    """start_audio_connector_server returns the AudioConnectorServerHandle:
        host (str): The host for server.
        port (int): The port for server.
        running (bool): Whether the server is running.
        stop(): A awaitable coroutine to stop the server.
        get_address() -> str: A coroutine to get the current address of server
    """

You need to provide the async coroutines using the websocket server and interact with the connections.

async def on_message(message):
    print("Received message")
    return message

async def on_disconnect():
    print("Client disconnected.")

async def on_error(error):
    print(f"Error occurred: {error}")

To send and receive messages from the connection, you need to set the handlers for the connection and use send_json_packet or send_audio_buffer to send packets.

async def on_connect(client):

    client.set_handler(
            on_message=on_message,
            on_disconnect=on_disconnect,
            on_error=on_error
    )
        """
        Register per-connection event handlers.

        :param on_message: Coroutine to be called when a message is received.
        :param on_disconnect: Coroutine to be called when the connection closes.
        :param on_error: Coroutine to be called when an error occurs.
        """

    await client.send_json_packet(string_json)
        """
        Send a JSON-encoded message to the connected WebSocket client.
        Args:
            :param data: The data to send, which will be converted to a JSON string.
        """

    await client.send_audio_buffer(audio_data, bytes_per_sample=640, frames_ms=20, pad_last_frame=True, flush_buffer=False,)
        """
        Send a PCM audio stream to the connected WebSocket client.

        Args:
            :param audio_data: The PCM audio data to send as bytes.
            :param bytes_per_sample: Number of bytes in each audio frame. For Opentok/Vonage, use 640 or 320 bytes depending on the `audio_rate` configured in the audio connector API.
            :param frames_ms: The duration of each frame in milliseconds. It can be either 10 or 20.
            :param pad_last_frame: If True, pads the last frame with zeros if it is shorter than bytes_per_sample.
            :param flush_buffer: If True, flushes any buffered packets before sending new data.

        Note: The raw byte buffer provided in audio_data is partitioned into fixed-size frames (typically 640 bytes, or 320 bytes as configured).
        The permissible number of frames for immediate dispatch is determined by the active frame window (configured via set_buffer_timing, based on active_seconds).
            - If the total frame count is within this active limit, frames are sent directly, one by one.
            - If it exceeds the limit, frames are queued and delivered asynchronously by a background sender task to reduce load on the SDK and application.

        Optional behaviors include padding the final partial frame and flushing any previously queued frames before sending.
        """

    client.info()
        """
        Retrieve metadata about the connection extracted from request headers.

        Returns:
            AudioConnectorConnectionData():
            Metadata about the connection extracted from request headers.
            Fields:
                sessionId (str): The session ID from the headers (if present).
                conferenceId (str): The conference ID from the headers (if present).
                connectionId (str): The conference ID from the headers (if present).
                uri (str): The URL path of the WebSocket connection.
                customHeaders (dict): All headers sent with the connection request.
                id (str): The unique identifier for the connection.
        """

    client.flush_buffer()
        """
        Flush any buffered packets to the WebSocket client.
        """

    client.disconnect():
        """
        Close the WebSocket connection.
        This method can be called to gracefully close the connection.
        """

    client.set_buffer_timing(active_seconds=7.5)
        """
        Update pacing parameters for the audio frame sender.

        :param active_seconds: Duration (seconds) to actively send frames per cycle.
        """

Sample App

A sample app on how to use the sdk. For more details, see the SAMPLES.

Unit Tests

To run the unit tests, follow these steps:

python -m pytest tests/unit/test_audio_connector_server.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vonage_audio_connector_server-0.1.1.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vonage_audio_connector_server-0.1.1-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file vonage_audio_connector_server-0.1.1.tar.gz.

File metadata

File hashes

Hashes for vonage_audio_connector_server-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a7f2da12b5f7ac5abfdf22e58ffca7890b215ff43d1261f5e6c75551fa4bf82a
MD5 cbe693ff38b3cbb079b9fb0d1c9545b0
BLAKE2b-256 1dcee996db2152a48e684bb7fd84416c42baf0c301eb9926e54487b1f9b1f858

See more details on using hashes here.

File details

Details for the file vonage_audio_connector_server-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for vonage_audio_connector_server-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d2796c493a772b26fd51fc6c1d6e7eaacee6284058f650ec74c31cdc89406dc
MD5 18a8a7e5c58e58dc689eceb2db9cb4be
BLAKE2b-256 6fba1290da6d8be5f5a60e80e285d9507448d12368f8026602caae4ebae7cbaa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page