Refactoring SmallWebRTCRequestHandler.

This commit is contained in:
Filipi Fuchter
2025-09-15 09:58:44 -03:00
parent 95f72f6dce
commit 11d0c3d46d
2 changed files with 2 additions and 18 deletions

View File

@@ -208,8 +208,6 @@ def _setup_webrtc_routes(app: FastAPI, esp32_mode: bool = False, host: str = "lo
@app.post("/api/offer") @app.post("/api/offer")
async def offer(request: SmallWebRTCRequest, background_tasks: BackgroundTasks): async def offer(request: SmallWebRTCRequest, background_tasks: BackgroundTasks):
"""Handle WebRTC offer requests via SmallWebRTCRequestHandler.""" """Handle WebRTC offer requests via SmallWebRTCRequestHandler."""
# Create a future to receive the answer from the handler
pending_answer: Future = Future()
# Prepare runner arguments with the callback to run your bot # Prepare runner arguments with the callback to run your bot
async def webrtc_connection_callback(connection): async def webrtc_connection_callback(connection):
@@ -218,14 +216,10 @@ def _setup_webrtc_routes(app: FastAPI, esp32_mode: bool = False, host: str = "lo
background_tasks.add_task(bot_module.bot, runner_args) background_tasks.add_task(bot_module.bot, runner_args)
# Delegate handling to SmallWebRTCRequestHandler # Delegate handling to SmallWebRTCRequestHandler
await small_webrtc_handler.handle_web_request( answer = await small_webrtc_handler.handle_web_request(
request=request, request=request,
pending_answer=pending_answer,
webrtc_connection_callback=webrtc_connection_callback, webrtc_connection_callback=webrtc_connection_callback,
) )
# Wait for the handler to resolve the answer
answer = await pending_answer
return answer return answer
@asynccontextmanager @asynccontextmanager

View File

@@ -72,7 +72,6 @@ class SmallWebRTCRequestHandler:
async def handle_web_request( async def handle_web_request(
self, self,
request: SmallWebRTCRequest, request: SmallWebRTCRequest,
pending_answer: asyncio.Future,
webrtc_connection_callback: Callable[[Any], Awaitable[None]], webrtc_connection_callback: Callable[[Any], Awaitable[None]],
) -> None: ) -> None:
"""Handle a SmallWebRTC request and resolve the pending answer. """Handle a SmallWebRTC request and resolve the pending answer.
@@ -82,13 +81,10 @@ class SmallWebRTCRequestHandler:
- Otherwise, create a new `SmallWebRTCConnection`. - Otherwise, create a new `SmallWebRTCConnection`.
- Invoke the provided callback with the connection. - Invoke the provided callback with the connection.
- Manage ESP32-specific munging if enabled. - Manage ESP32-specific munging if enabled.
- Resolve or reject the `pending_answer` future in the runner arguments.
Args: Args:
request (SmallWebRTCRequest): The incoming WebRTC request, containing request (SmallWebRTCRequest): The incoming WebRTC request, containing
SDP, type, and optionally a `pc_id`. SDP, type, and optionally a `pc_id`.
pending_answer (asyncio.Future): A future that will be resolved with the
SDP answer once the request is processed.
webrtc_connection_callback (Callable[[Any], Awaitable[None]]): An webrtc_connection_callback (Callable[[Any], Awaitable[None]]): An
asynchronous callback function that is invoked with the WebRTC connection. asynchronous callback function that is invoked with the WebRTC connection.
@@ -136,16 +132,10 @@ class SmallWebRTCRequestHandler:
self._pcs_map[answer["pc_id"]] = pipecat_connection self._pcs_map[answer["pc_id"]] = pipecat_connection
# Resolve the pending answer future return answer
if not pending_answer.done():
pending_answer.set_result(answer)
except Exception as e: except Exception as e:
logger.error(f"Error processing SmallWebRTC request: {e}") logger.error(f"Error processing SmallWebRTC request: {e}")
logger.debug(f"SmallWebRTC request details: {request}") logger.debug(f"SmallWebRTC request details: {request}")
if not pending_answer.done():
pending_answer.set_exception(e)
raise raise
async def close(self): async def close(self):