Adding support to define ice servers.
This commit is contained in:
@@ -21,6 +21,8 @@ app = FastAPI()
|
||||
# Store connections by pc_id
|
||||
pcs_map: Dict[str, SmallWebRTCConnection] = {}
|
||||
|
||||
ice_servers = ["stun:stun.l.google.com:19302"]
|
||||
|
||||
|
||||
@app.post("/api/offer")
|
||||
async def offer(request: dict, background_tasks: BackgroundTasks):
|
||||
@@ -33,7 +35,7 @@ async def offer(request: dict, background_tasks: BackgroundTasks):
|
||||
sdp=request["sdp"], type=request["type"], restart_pc=request.get("restart_pc", False)
|
||||
)
|
||||
else:
|
||||
pipecat_connection = SmallWebRTCConnection()
|
||||
pipecat_connection = SmallWebRTCConnection(ice_servers)
|
||||
await pipecat_connection.initialize(sdp=request["sdp"], type=request["type"])
|
||||
|
||||
@pipecat_connection.on("closed")
|
||||
|
||||
@@ -5,7 +5,7 @@ import uuid
|
||||
from enum import Enum
|
||||
from typing import Any, Optional
|
||||
|
||||
from aiortc import RTCPeerConnection, RTCSessionDescription
|
||||
from aiortc import RTCConfiguration, RTCIceServer, RTCPeerConnection, RTCSessionDescription
|
||||
from loguru import logger
|
||||
|
||||
from pipecat.utils.event_emitter import EventEmitter
|
||||
@@ -18,15 +18,21 @@ class SignallingMessage(Enum):
|
||||
|
||||
|
||||
class SmallWebRTCConnection(EventEmitter):
|
||||
def __init__(self):
|
||||
def __init__(self, ice_servers=None):
|
||||
super().__init__()
|
||||
if ice_servers:
|
||||
self.ice_servers = [RTCIceServer(urls=server) for server in ice_servers]
|
||||
else:
|
||||
self.ice_servers = []
|
||||
self._is_connecting = False
|
||||
self._initialize()
|
||||
|
||||
def _initialize(self):
|
||||
logger.info("Initializing new peer connection")
|
||||
rtc_config = RTCConfiguration(iceServers=self.ice_servers)
|
||||
|
||||
self.answer: Optional[RTCSessionDescription] = None
|
||||
self.pc = RTCPeerConnection()
|
||||
self.pc = RTCPeerConnection(rtc_config)
|
||||
self.pc_id = "PeerConnection(%s)" % uuid.uuid4()
|
||||
self._setup_listeners()
|
||||
self._tracks = set()
|
||||
|
||||
Reference in New Issue
Block a user