Merge pull request #1587 from pipecat-ai/improving_ice_servers

Updated `SmallWebRTCConnection` to support `ice_servers` with credentials.
This commit is contained in:
Filipi da Silva Fuchter
2025-04-29 11:07:32 -03:00
committed by GitHub
6 changed files with 42 additions and 13 deletions

View File

@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Updated `SmallWebRTCConnection` to support `ice_servers` with credentials.
- Added `VADUserStartedSpeakingFrame` and `VADUserStoppedSpeakingFrame`, - Added `VADUserStartedSpeakingFrame` and `VADUserStoppedSpeakingFrame`,
indicating when the VAD detected the user to start and stop speaking. These indicating when the VAD detected the user to start and stop speaking. These
events are helpful when using smart turn detection, as the user's stop time events are helpful when using smart turn detection, as the user's stop time

View File

@@ -20,7 +20,7 @@ from fastapi.responses import RedirectResponse
from loguru import logger from loguru import logger
from pipecat_ai_small_webrtc_prebuilt.frontend import SmallWebRTCPrebuiltUI from pipecat_ai_small_webrtc_prebuilt.frontend import SmallWebRTCPrebuiltUI
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection from pipecat.transports.network.webrtc_connection import IceServer, SmallWebRTCConnection
# Load environment variables # Load environment variables
load_dotenv(override=True) load_dotenv(override=True)
@@ -30,7 +30,11 @@ app = FastAPI()
# Store connections by pc_id # Store connections by pc_id
pcs_map: Dict[str, SmallWebRTCConnection] = {} pcs_map: Dict[str, SmallWebRTCConnection] = {}
ice_servers = ["stun:stun.l.google.com:19302"] ice_servers = [
IceServer(
urls="stun:stun.l.google.com:19302",
)
]
# Mount the frontend at / # Mount the frontend at /
app.mount("/client", SmallWebRTCPrebuiltUI) app.mount("/client", SmallWebRTCPrebuiltUI)

View File

@@ -18,7 +18,7 @@ from fastapi.responses import RedirectResponse
from loguru import logger from loguru import logger
from pipecat_ai_small_webrtc_prebuilt.frontend import SmallWebRTCPrebuiltUI from pipecat_ai_small_webrtc_prebuilt.frontend import SmallWebRTCPrebuiltUI
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection from pipecat.transports.network.webrtc_connection import IceServer, SmallWebRTCConnection
# Load environment variables # Load environment variables
load_dotenv(override=True) load_dotenv(override=True)
@@ -28,7 +28,11 @@ app = FastAPI()
# Store connections by pc_id # Store connections by pc_id
pcs_map: Dict[str, SmallWebRTCConnection] = {} pcs_map: Dict[str, SmallWebRTCConnection] = {}
ice_servers = ["stun:stun.l.google.com:19302"] ice_servers = [
IceServer(
urls="stun:stun.l.google.com:19302",
)
]
# Mount the frontend at / # Mount the frontend at /
app.mount("/prebuilt", SmallWebRTCPrebuiltUI) app.mount("/prebuilt", SmallWebRTCPrebuiltUI)

View File

@@ -18,7 +18,7 @@ from fastapi.responses import RedirectResponse
from loguru import logger from loguru import logger
from pipecat_ai_small_webrtc_prebuilt.frontend import SmallWebRTCPrebuiltUI from pipecat_ai_small_webrtc_prebuilt.frontend import SmallWebRTCPrebuiltUI
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection from pipecat.transports.network.webrtc_connection import IceServer, SmallWebRTCConnection
# Load environment variables # Load environment variables
load_dotenv(override=True) load_dotenv(override=True)
@@ -28,7 +28,11 @@ app = FastAPI()
# Store connections by pc_id # Store connections by pc_id
pcs_map: Dict[str, SmallWebRTCConnection] = {} pcs_map: Dict[str, SmallWebRTCConnection] = {}
ice_servers = ["stun:stun.l.google.com:19302"] ice_servers = [
IceServer(
urls="stun:stun.l.google.com:19302",
)
]
# Mount the frontend at / # Mount the frontend at /
app.mount("/prebuilt", SmallWebRTCPrebuiltUI) app.mount("/prebuilt", SmallWebRTCPrebuiltUI)

View File

@@ -17,7 +17,7 @@ from fastapi import BackgroundTasks, FastAPI
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
from loguru import logger from loguru import logger
from pipecat.transports.network.webrtc_connection import SmallWebRTCConnection from pipecat.transports.network.webrtc_connection import IceServer, SmallWebRTCConnection
# Load environment variables # Load environment variables
load_dotenv(override=True) load_dotenv(override=True)
@@ -28,6 +28,13 @@ app = FastAPI()
pcs_map: Dict[str, SmallWebRTCConnection] = {} pcs_map: Dict[str, SmallWebRTCConnection] = {}
ice_servers = [
IceServer(
urls="stun:stun.l.google.com:19302",
)
]
@app.post("/api/offer") @app.post("/api/offer")
async def offer(request: dict, background_tasks: BackgroundTasks): async def offer(request: dict, background_tasks: BackgroundTasks):
pc_id = request.get("pc_id") pc_id = request.get("pc_id")
@@ -37,7 +44,7 @@ async def offer(request: dict, background_tasks: BackgroundTasks):
logger.info(f"Reusing existing connection for pc_id: {pc_id}") logger.info(f"Reusing existing connection for pc_id: {pc_id}")
await pipecat_connection.renegotiate(sdp=request["sdp"], type=request["type"]) await pipecat_connection.renegotiate(sdp=request["sdp"], type=request["type"])
else: else:
pipecat_connection = SmallWebRTCConnection() pipecat_connection = SmallWebRTCConnection(ice_servers)
await pipecat_connection.initialize(sdp=request["sdp"], type=request["type"]) await pipecat_connection.initialize(sdp=request["sdp"], type=request["type"])
@pipecat_connection.event_handler("closed") @pipecat_connection.event_handler("closed")

View File

@@ -7,7 +7,7 @@
import asyncio import asyncio
import json import json
import time import time
from typing import Any, Literal, Optional, Union from typing import Any, List, Literal, Optional, Union
from av.frame import Frame from av.frame import Frame
from loguru import logger from loguru import logger
@@ -87,13 +87,21 @@ class SmallWebRTCTrack:
return getattr(self._track, name) return getattr(self._track, name)
# Alias so we don't need to expose RTCIceServer
IceServer = RTCIceServer
class SmallWebRTCConnection(BaseObject): class SmallWebRTCConnection(BaseObject):
def __init__(self, ice_servers=None): def __init__(self, ice_servers: Optional[Union[List[str], List[IceServer]]] = None):
super().__init__() super().__init__()
if ice_servers: if not ice_servers:
self.ice_servers = [RTCIceServer(urls=server) for server in ice_servers] self.ice_servers: List[IceServer] = []
elif all(isinstance(s, IceServer) for s in ice_servers):
self.ice_servers = ice_servers
elif all(isinstance(s, str) for s in ice_servers):
self.ice_servers = [IceServer(urls=s) for s in ice_servers]
else: else:
self.ice_servers = [] raise TypeError("ice_servers must be either List[str] or List[RTCIceServer]")
self._connect_invoked = False self._connect_invoked = False
self._track_map = {} self._track_map = {}
self._track_getters = { self._track_getters = {