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:
BaseModelMessage 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:
BaseModelMessage 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:
BaseModelMessage 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:
objectUnion 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:
objectWrapper 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.
- class pipecat.transports.smallwebrtc.connection.SmallWebRTCConnection(ice_servers: list[str] | list[RTCIceServer] | None = None, connection_timeout_secs: int = 60)[source]
Bases:
BaseObjectWebRTC 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 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.
- 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_enabledisTrue. It is set toFalsewhen 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_SIZEentries. Messages that arrive when the queue is full are discarded with a warning.
- Parameters:
message – The message to send (will be JSON serialized).