Add docstrings to TelnyxFrameSerializer
This commit is contained in:
@@ -34,9 +34,40 @@ from pipecat.serializers.base_serializer import FrameSerializer, FrameSerializer
|
|||||||
|
|
||||||
|
|
||||||
class TelnyxFrameSerializer(FrameSerializer):
|
class TelnyxFrameSerializer(FrameSerializer):
|
||||||
|
"""Serializer for Telnyx WebSocket protocol.
|
||||||
|
|
||||||
|
This serializer handles converting between Pipecat frames and Telnyx's WebSocket
|
||||||
|
media streams protocol. It supports audio conversion, DTMF events, and automatic
|
||||||
|
call termination.
|
||||||
|
|
||||||
|
When auto_hang_up is enabled (default), the serializer will automatically terminate
|
||||||
|
the Telnyx call when an EndFrame or CancelFrame is processed, but requires Telnyx
|
||||||
|
credentials to be provided.
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
_stream_id: The Telnyx Stream ID.
|
||||||
|
_call_control_id: The associated Telnyx Call Control ID.
|
||||||
|
_api_key: Telnyx API key for API access.
|
||||||
|
_params: Configuration parameters.
|
||||||
|
_telnyx_sample_rate: Sample rate used by Telnyx (typically 8kHz).
|
||||||
|
_sample_rate: Input sample rate for the pipeline.
|
||||||
|
_resampler: Audio resampler for format conversion.
|
||||||
|
_hangup_attempted: Flag to track if hang-up has been attempted.
|
||||||
|
"""
|
||||||
|
|
||||||
class InputParams(BaseModel):
|
class InputParams(BaseModel):
|
||||||
telnyx_sample_rate: int = 8000 # Default Telnyx rate (8kHz)
|
"""Configuration parameters for TelnyxFrameSerializer.
|
||||||
sample_rate: Optional[int] = None # Pipeline input rate
|
|
||||||
|
Attributes:
|
||||||
|
telnyx_sample_rate: Sample rate used by Telnyx, defaults to 8000 Hz.
|
||||||
|
sample_rate: Optional override for pipeline input sample rate.
|
||||||
|
inbound_encoding: Audio encoding for data sent to Telnyx (e.g., "PCMU").
|
||||||
|
outbound_encoding: Audio encoding for data received from Telnyx (e.g., "PCMU").
|
||||||
|
auto_hang_up: Whether to automatically terminate call on EndFrame.
|
||||||
|
"""
|
||||||
|
|
||||||
|
telnyx_sample_rate: int = 8000
|
||||||
|
sample_rate: Optional[int] = None
|
||||||
inbound_encoding: str = "PCMU"
|
inbound_encoding: str = "PCMU"
|
||||||
outbound_encoding: str = "PCMU"
|
outbound_encoding: str = "PCMU"
|
||||||
auto_hang_up: bool = True
|
auto_hang_up: bool = True
|
||||||
@@ -56,8 +87,8 @@ class TelnyxFrameSerializer(FrameSerializer):
|
|||||||
stream_id: The Stream ID for Telnyx.
|
stream_id: The Stream ID for Telnyx.
|
||||||
outbound_encoding: The encoding type for outbound audio (e.g., "PCMU").
|
outbound_encoding: The encoding type for outbound audio (e.g., "PCMU").
|
||||||
inbound_encoding: The encoding type for inbound audio (e.g., "PCMU").
|
inbound_encoding: The encoding type for inbound audio (e.g., "PCMU").
|
||||||
call_control_id: The Call Control ID for the Telnyx call.
|
call_control_id: The Call Control ID for the Telnyx call (optional, but required for auto hang-up).
|
||||||
api_key: Your Telnyx API key.
|
api_key: Your Telnyx API key (required for auto hang-up).
|
||||||
params: Configuration parameters.
|
params: Configuration parameters.
|
||||||
"""
|
"""
|
||||||
self._stream_id = stream_id
|
self._stream_id = stream_id
|
||||||
@@ -75,12 +106,36 @@ class TelnyxFrameSerializer(FrameSerializer):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def type(self) -> FrameSerializerType:
|
def type(self) -> FrameSerializerType:
|
||||||
|
"""Gets the serializer type.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The serializer type, either TEXT or BINARY.
|
||||||
|
"""
|
||||||
return FrameSerializerType.TEXT
|
return FrameSerializerType.TEXT
|
||||||
|
|
||||||
async def setup(self, frame: StartFrame):
|
async def setup(self, frame: StartFrame):
|
||||||
|
"""Sets up the serializer with pipeline configuration.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: The StartFrame containing pipeline configuration.
|
||||||
|
"""
|
||||||
self._sample_rate = self._params.sample_rate or frame.audio_in_sample_rate
|
self._sample_rate = self._params.sample_rate or frame.audio_in_sample_rate
|
||||||
|
|
||||||
async def serialize(self, frame: Frame) -> str | bytes | None:
|
async def serialize(self, frame: Frame) -> str | bytes | None:
|
||||||
|
"""Serializes a Pipecat frame to Telnyx WebSocket format.
|
||||||
|
|
||||||
|
Handles conversion of various frame types to Telnyx WebSocket messages.
|
||||||
|
For EndFrames and CancelFrames, initiates call termination if auto_hang_up is enabled.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: The Pipecat frame to serialize.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Serialized data as string or bytes, or None if the frame isn't handled.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If an unsupported encoding is specified.
|
||||||
|
"""
|
||||||
if (
|
if (
|
||||||
self._params.auto_hang_up
|
self._params.auto_hang_up
|
||||||
and not self._hangup_attempted
|
and not self._hangup_attempted
|
||||||
@@ -153,6 +208,20 @@ class TelnyxFrameSerializer(FrameSerializer):
|
|||||||
logger.exception(f"Failed to hang up Telnyx call: {e}")
|
logger.exception(f"Failed to hang up Telnyx call: {e}")
|
||||||
|
|
||||||
async def deserialize(self, data: str | bytes) -> Frame | None:
|
async def deserialize(self, data: str | bytes) -> Frame | None:
|
||||||
|
"""Deserializes Telnyx WebSocket data to Pipecat frames.
|
||||||
|
|
||||||
|
Handles conversion of Telnyx media events to appropriate Pipecat frames,
|
||||||
|
including audio data and DTMF keypresses.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data: The raw WebSocket data from Telnyx.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A Pipecat frame corresponding to the Telnyx event, or None if unhandled.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If an unsupported encoding is specified.
|
||||||
|
"""
|
||||||
message = json.loads(data)
|
message = json.loads(data)
|
||||||
|
|
||||||
if message["event"] == "media":
|
if message["event"] == "media":
|
||||||
|
|||||||
Reference in New Issue
Block a user