From dd402da9e56fe409995d1a114da7d560c09aa017 Mon Sep 17 00:00:00 2001 From: daniil5701133 Date: Tue, 25 Jun 2024 14:40:14 +0300 Subject: [PATCH] added handling streamSid after first wss connect fixx name --- examples/twilio-chatbot/bot.py | 6 ++++-- examples/twilio-chatbot/server.py | 9 ++++++++- src/pipecat/serializers/twilio.py | 9 +++------ src/pipecat/transports/network/fastapi_websocket.py | 4 ++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/examples/twilio-chatbot/bot.py b/examples/twilio-chatbot/bot.py index 8a368f830..7d523ca43 100644 --- a/examples/twilio-chatbot/bot.py +++ b/examples/twilio-chatbot/bot.py @@ -15,6 +15,7 @@ from pipecat.services.elevenlabs import ElevenLabsTTSService from pipecat.transports.network.fastapi_websocket import FastAPIWebsocketTransport, FastAPIWebsocketParams from pipecat.vad.silero import SileroVADAnalyzer +from pipecat.serializers.twilio import TwilioFrameSerializer from loguru import logger @@ -25,7 +26,7 @@ logger.add(sys.stderr, level="DEBUG") -async def run_bot(websocket_client): +async def run_bot(websocket_client, stream_sid): async with aiohttp.ClientSession() as session: transport = FastAPIWebsocketTransport( websocket=websocket_client, @@ -34,7 +35,8 @@ async def run_bot(websocket_client): add_wav_header=False, vad_enabled=True, vad_analyzer=SileroVADAnalyzer(), - vad_audio_passthrough=True + vad_audio_passthrough=True, + serializer=TwilioFrameSerializer(stream_sid) ) ) diff --git a/examples/twilio-chatbot/server.py b/examples/twilio-chatbot/server.py index 025f9dd17..f64e7f309 100644 --- a/examples/twilio-chatbot/server.py +++ b/examples/twilio-chatbot/server.py @@ -1,3 +1,5 @@ +import json + import uvicorn from fastapi import FastAPI, WebSocket @@ -26,8 +28,13 @@ async def start_call(): @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): await websocket.accept() + start_data = websocket.iter_text() + await start_data.__anext__() + call_data = json.loads(await start_data.__anext__()) + print(call_data, flush=True) + stream_sid = call_data['start']['streamSid'] print("WebSocket connection accepted") - await run_bot(websocket) + await run_bot(websocket, stream_sid) if __name__ == "__main__": diff --git a/src/pipecat/serializers/twilio.py b/src/pipecat/serializers/twilio.py index ea7a562aa..713f43a86 100644 --- a/src/pipecat/serializers/twilio.py +++ b/src/pipecat/serializers/twilio.py @@ -17,8 +17,8 @@ class TwilioFrameSerializer(FrameSerializer): AudioRawFrame: "audio", } - def __init__(self): - self._sid = None + def __init__(self, stream_sid: str): + self._stream_sid = stream_sid def serialize(self, frame: Frame) -> str | bytes | None: if not isinstance(frame, AudioRawFrame): @@ -30,7 +30,7 @@ def serialize(self, frame: Frame) -> str | bytes | None: payload = base64.b64encode(serialized_data).decode("utf-8") answer = { "event": "media", - "streamSid": self._sid, + "streamSid": self._stream_sid, "media": { "payload": payload } @@ -41,9 +41,6 @@ def serialize(self, frame: Frame) -> str | bytes | None: def deserialize(self, data: str | bytes) -> Frame | None: message = json.loads(data) - if not self._sid: - self._sid = message["streamSid"] if "streamSid" in message else None - if message["event"] != "media": return None else: diff --git a/src/pipecat/transports/network/fastapi_websocket.py b/src/pipecat/transports/network/fastapi_websocket.py index 0b3eb906f..cf71ee021 100644 --- a/src/pipecat/transports/network/fastapi_websocket.py +++ b/src/pipecat/transports/network/fastapi_websocket.py @@ -35,7 +35,7 @@ class FastAPIWebsocketParams(TransportParams): add_wav_header: bool = False audio_frame_size: int = 6400 # 200ms - serializer: FrameSerializer = TwilioFrameSerializer() + serializer: FrameSerializer class FastAPIWebsocketCallbacks(BaseModel): @@ -125,7 +125,7 @@ class FastAPIWebsocketTransport(BaseTransport): def __init__( self, websocket: WebSocket, - params: FastAPIWebsocketParams = FastAPIWebsocketParams(), + params: FastAPIWebsocketParams, input_name: str | None = None, output_name: str | None = None, loop: asyncio.AbstractEventLoop | None = None):