connection

Small WebRTC connection implementation for Pipecat.

This module provides a WebRTC connection implementation using aiortc, with support for audio/video tracks, data channels, and signaling for real-time communication applications.

class pipecat.transports.smallwebrtc.connection.TrackStatusMessage(*, type: Literal['trackStatus'], receiver_index: int, enabled: bool)[source]

Bases: BaseModel

Message for updating track enabled/disabled status.

Parameters:
  • type – Message type identifier.

  • receiver_index – Index of the track receiver to update.

  • enabled – Whether the track should be enabled or disabled.

type: Literal['trackStatus']
receiver_index: int
enabled: bool
class pipecat.transports.smallwebrtc.connection.RenegotiateMessage(*, type: Literal['renegotiate'] = 'renegotiate')[source]

Bases: BaseModel

Message requesting WebRTC renegotiation.

Parameters:

type – Message type identifier for renegotiation requests.

type: Literal['renegotiate']
class pipecat.transports.smallwebrtc.connection.PeerLeftMessage(*, type: Literal['peerLeft'] = 'peerLeft')[source]

Bases: BaseModel

Message indicating a peer has left the connection.

Parameters:

type – Message type identifier for peer departure.

type: Literal['peerLeft']
class pipecat.transports.smallwebrtc.connection.SignallingMessage[source]

Bases: object

Union types for signaling message handling.

Parameters:
  • Inbound – Types of messages that can be received from peers.

  • outbound – Types of messages that can be sent to peers.

Inbound

alias of TrackStatusMessage

outbound

alias of RenegotiateMessage

class pipecat.transports.smallwebrtc.connection.SmallWebRTCTrack(receiver)[source]

Bases: object

Wrapper for WebRTC media tracks with enabled/disabled state management.

Provides additional functionality on top of aiortc MediaStreamTrack including enable/disable control and frame discarding for audio and video streams.

__init__(receiver)[source]

Initialize the WebRTC track wrapper.

Parameters:

receiver – The RemoteStreamTrack receiver instance.

set_enabled(enabled: bool) None[source]

Enable or disable the track.

Parameters:

enabled – Whether the track should be enabled for receiving frames.

is_enabled() bool[source]

Check if the track is currently enabled.

Returns:

True if the track is enabled for receiving frames.

async discard_old_frames()[source]

Discard old frames from the track queue to reduce latency.

async recv() Frame | None[source]

Receive the next frame from the track.

Enables the internal receiving state and starts idle watcher.

Returns:

The next frame, except for video tracks, where it returns the frame only if the track is enabled, otherwise, returns None.

stop()[source]

Stop receiving frames from the track.

class pipecat.transports.smallwebrtc.connection.SmallWebRTCConnection(ice_servers: list[str] | list[RTCIceServer] | None = None, connection_timeout_secs: int = 60)[source]

Bases: BaseObject

WebRTC connection implementation using aiortc.

Provides WebRTC peer connection functionality including ICE server configuration, track management, data channel communication, and connection state handling for real-time audio/video communication.

__init__(ice_servers: list[str] | list[RTCIceServer] | None = None, connection_timeout_secs: int = 60)[source]

Initialize the WebRTC connection.

Parameters:
  • ice_servers – List of ICE servers as URLs or IceServer objects.

  • connection_timeout_secs – Timeout in seconds for connecting to the peer.

Raises:

TypeError – If ice_servers contains mixed types or unsupported types.

ice_servers: list[RTCIceServer]
property pc: RTCPeerConnection

Get the underlying RTCPeerConnection.

Returns:

The aiortc RTCPeerConnection instance.

property pc_id: str

Get the peer connection identifier.

Returns:

The unique identifier for this peer connection.

async initialize(sdp: str, type: str)[source]

Initialize the connection with an SDP offer.

Parameters:
  • sdp – The SDP offer string.

  • type – The SDP type (usually “offer”).

async connect()[source]

Connect the WebRTC peer connection and handle initial setup.

async renegotiate(sdp: str, type: str, restart_pc: bool = False)[source]

Renegotiate the WebRTC connection with new parameters.

Parameters:
  • sdp – The new SDP offer string.

  • type – The SDP type (usually “offer”).

  • restart_pc – Whether to restart the peer connection entirely.

force_transceivers_to_send_recv()[source]

Force all transceivers to bidirectional send/receive mode.

replace_audio_track(track)[source]

Replace the audio track in the first transceiver.

Parameters:

track – The new audio track to use for sending.

replace_video_track(track)[source]

Replace the video track in the second transceiver.

Parameters:

track – The new video track to use for sending.

replace_screen_video_track(track)[source]

Replace the screen video track in the second transceiver.

Parameters:

track – The new screen video track to use for sending.

async disconnect()[source]

Disconnect from the WebRTC peer connection.

get_answer()[source]

Get the SDP answer for the current connection.

Returns:

Dictionary containing SDP answer, type, and peer connection ID, or None if no answer is available.

is_connected() bool[source]

Check if the WebRTC connection is currently active.

Returns:

True if the connection is active and receiving data.

audio_input_track()[source]

Get the audio input track wrapper.

Returns:

SmallWebRTCTrack wrapper for the audio track, or None if unavailable.

video_input_track()[source]

Get the video input track wrapper.

Returns:

SmallWebRTCTrack wrapper for the video track, or None if unavailable.

screen_video_input_track()[source]

Get the screen video input track wrapper.

Returns:

SmallWebRTCTrack wrapper for the screen video track, or None if unavailable.

send_app_message(message: Any)[source]

Send an application message through the data channel.

If the data channel is open the message is sent immediately. Otherwise, the message is placed in an in-memory queue so it can be flushed once the channel opens, subject to the following constraints:

  • Queueing is only attempted when _data_channel_enabled is True. It is set to False when the data-channel open timeout fires (see _start_data_channel_timeout()), after which messages are silently discarded.

  • The queue will not grow beyond MAX_MESSAGE_QUEUE_SIZE entries. Messages that arrive when the queue is full are discarded with a warning.

Parameters:

message – The message to send (will be JSON serialized).

ask_to_renegotiate()[source]

Request renegotiation of the WebRTC connection.

async add_ice_candidate(candidate)[source]

Handle incoming ICE candidates.