Skip to content

Commit 80a1496

Browse files
committed
Providing the proxy routes.
1 parent f6bfa81 commit 80a1496

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

p2p-webrtc/video-transform/server/server.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
import sys
99
import uuid
1010
from contextlib import asynccontextmanager
11+
from http import HTTPMethod
1112
from typing import Any, Dict, List, Optional, TypedDict
1213

1314
import uvicorn
1415
from bot import run_bot
1516
from dotenv import load_dotenv
16-
from fastapi import BackgroundTasks, FastAPI, Request
17+
from fastapi import BackgroundTasks, FastAPI, Request, Response
1718
from fastapi.responses import RedirectResponse
1819
from loguru import logger
1920
from pipecat.transports.smallwebrtc.connection import IceServer
2021
from pipecat.transports.smallwebrtc.request_handler import (
22+
IceCandidate,
2123
SmallWebRTCPatchRequest,
2224
SmallWebRTCRequest,
2325
SmallWebRTCRequestHandler,
@@ -93,10 +95,50 @@ class StartBotResult(TypedDict, total=False):
9395

9496
result: StartBotResult = {"sessionId": session_id}
9597
if request_data.get("enableDefaultIceServers"):
96-
result["iceConfig"] = IceConfig(iceServers=[IceServer(urls="stun:stun.l.google.com:19302")])
98+
result["iceConfig"] = IceConfig(
99+
iceServers=[IceServer(urls=["stun:stun.l.google.com:19302"])]
100+
)
97101

98102
return result
99103

104+
@app.api_route(
105+
"/sessions/{session_id}/{path:path}",
106+
methods=["GET", "POST", "PUT", "PATCH", "DELETE"],
107+
)
108+
async def proxy_request(
109+
session_id: str, path: str, request: Request, background_tasks: BackgroundTasks
110+
):
111+
"""Mimic Pipecat Cloud's proxy."""
112+
active_session = active_sessions.get(session_id)
113+
if active_session is None:
114+
return Response(content="Invalid or not-yet-ready session_id", status_code=404)
115+
116+
if path.endswith("api/offer"):
117+
# Parse the request body and convert to SmallWebRTCRequest
118+
try:
119+
request_data = await request.json()
120+
if request.method == HTTPMethod.POST.value:
121+
webrtc_request = SmallWebRTCRequest(
122+
sdp=request_data["sdp"],
123+
type=request_data["type"],
124+
pc_id=request_data.get("pc_id"),
125+
restart_pc=request_data.get("restart_pc"),
126+
request_data=request_data,
127+
)
128+
return await offer(webrtc_request, background_tasks)
129+
elif request.method == HTTPMethod.PATCH.value:
130+
patch_request = SmallWebRTCPatchRequest(
131+
pc_id=request_data["pc_id"],
132+
candidates=[IceCandidate(**c) for c in request_data.get("candidates", [])],
133+
)
134+
return await ice_candidate(patch_request)
135+
except Exception as e:
136+
logger.error(f"Failed to parse WebRTC request: {e}")
137+
return Response(content="Invalid WebRTC request", status_code=400)
138+
139+
logger.info(f"Received request for path: {path}")
140+
return Response(status_code=200)
141+
100142

101143
@asynccontextmanager
102144
async def lifespan(app: FastAPI):

0 commit comments

Comments
 (0)