-
Notifications
You must be signed in to change notification settings - Fork 52
Adding support for trickle ice. #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
filipi87
wants to merge
3
commits into
main
Choose a base branch
from
filipi/trickle_ice
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−134
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,61 +5,51 @@ | |
| # | ||
|
|
||
| import argparse | ||
| import asyncio | ||
| import sys | ||
| from contextlib import asynccontextmanager | ||
| from typing import Dict | ||
|
|
||
| import uvicorn | ||
| from bot import run_bot | ||
| from dotenv import load_dotenv | ||
| from fastapi import BackgroundTasks, FastAPI | ||
| from fastapi import BackgroundTasks, FastAPI, Request | ||
| from fastapi.responses import FileResponse | ||
| from loguru import logger | ||
| from pipecat.transports.smallwebrtc.connection import IceServer, SmallWebRTCConnection | ||
| from pipecat.transports.smallwebrtc.request_handler import ( | ||
| SmallWebRTCPatchRequest, | ||
| SmallWebRTCRequest, | ||
| SmallWebRTCRequestHandler, | ||
| ) | ||
|
|
||
| # Load environment variables | ||
| load_dotenv(override=True) | ||
|
|
||
| app = FastAPI() | ||
|
|
||
| # Store connections by pc_id | ||
| pcs_map: Dict[str, SmallWebRTCConnection] = {} | ||
|
|
||
| # TODO: need to configure a TURN server if using inside Docker on Mac. | ||
| ice_servers = [ | ||
| IceServer( | ||
| urls="stun:stun.l.google.com:19302", | ||
| ) | ||
| ] | ||
| # Initialize the SmallWebRTC request handler | ||
| small_webrtc_handler: SmallWebRTCRequestHandler = SmallWebRTCRequestHandler() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is much cleaner using the nice encapsulation you've built into |
||
|
|
||
|
|
||
| @app.post("/api/offer") | ||
| async def offer(request: dict, background_tasks: BackgroundTasks): | ||
| pc_id = request.get("pc_id") | ||
|
|
||
| if pc_id and pc_id in pcs_map: | ||
| pipecat_connection = pcs_map[pc_id] | ||
| logger.info(f"Reusing existing connection for pc_id: {pc_id}") | ||
| await pipecat_connection.renegotiate( | ||
| sdp=request["sdp"], type=request["type"], restart_pc=request.get("restart_pc", False) | ||
| ) | ||
| else: | ||
| pipecat_connection = SmallWebRTCConnection(ice_servers) | ||
| await pipecat_connection.initialize(sdp=request["sdp"], type=request["type"]) | ||
| async def offer(request: SmallWebRTCRequest, background_tasks: BackgroundTasks): | ||
| """Handle WebRTC offer requests via SmallWebRTCRequestHandler.""" | ||
|
|
||
| @pipecat_connection.event_handler("closed") | ||
| async def handle_disconnected(webrtc_connection: SmallWebRTCConnection): | ||
| logger.info(f"Discarding peer connection for pc_id: {webrtc_connection.pc_id}") | ||
| pcs_map.pop(webrtc_connection.pc_id, None) | ||
| # Prepare runner arguments with the callback to run your bot | ||
| async def webrtc_connection_callback(connection): | ||
| background_tasks.add_task(run_bot, connection) | ||
|
|
||
| background_tasks.add_task(run_bot, pipecat_connection) | ||
| # Delegate handling to SmallWebRTCRequestHandler | ||
| answer = await small_webrtc_handler.handle_web_request( | ||
| request=request, | ||
| webrtc_connection_callback=webrtc_connection_callback, | ||
| ) | ||
| return answer | ||
|
|
||
| answer = pipecat_connection.get_answer() | ||
| # Updating the peer connection inside the map | ||
| pcs_map[answer["pc_id"]] = pipecat_connection | ||
|
|
||
| return answer | ||
| @app.patch("/api/offer") | ||
| async def ice_candidate(request: SmallWebRTCPatchRequest): | ||
| logger.debug(f"Received patch request: {request}") | ||
| await small_webrtc_handler.handle_patch_request(request) | ||
| return {"status": "success"} | ||
|
|
||
|
|
||
| @app.get("/") | ||
|
|
@@ -70,9 +60,7 @@ async def serve_index(): | |
| @asynccontextmanager | ||
| async def lifespan(app: FastAPI): | ||
| yield # Run app | ||
| coros = [pc.disconnect() for pc in pcs_map.values()] | ||
| await asyncio.gather(*coros) | ||
| pcs_map.clear() | ||
| await small_webrtc_handler.close() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.