Add WebRTC TURN configuration and related enhancements

- Introduce a new `.env.example` file for environment variable setup, including `PUBLIC_IP`, `TURN_SECRET`, and `TURN_URLS` for WebRTC TURN server configuration.
- Update `docker-compose.yaml` to support TURN server deployment with necessary environment variables and commands.
- Enhance backend configuration and routes to include WebRTC ICE server settings, allowing for STUN/TURN server integration.
- Implement a new service for managing WebRTC ICE server configurations, providing credentials for TURN when configured.
- Modify frontend API to fetch ICE server configurations dynamically, improving support for cross-network voice preview.
This commit is contained in:
Xin Wang
2026-07-06 17:45:53 +08:00
parent d6b04d71a0
commit e857828fe5
9 changed files with 121 additions and 16 deletions

View File

@@ -17,17 +17,17 @@ from models import AssistantConfig, SignalingOffer
from services.config_resolver import resolve_runtime_config
from starlette.websockets import WebSocketDisconnect, WebSocketState
# 注意:pipecat / aiortc 都是重依赖(语音才用),改成函数内"惰性导入",
# 这样不装 pipecat 也能启动后端、验证 CRUD。语音真正用到时才加载。
from services.webrtc_ice import aiortc_ice_servers, client_ice_servers
router = APIRouter()
# 注意:pipecat 是重依赖(语音才用),在 _handle_offer 等处惰性导入。
router = APIRouter(tags=["voice"])
def _ice_servers():
from aiortc import RTCIceServer
# 本地只用 STUN;公网部署在此追加 TURN(参考 dograh get_ice_servers)
return [RTCIceServer(urls="stun:stun.l.google.com:19302")]
@router.get("/api/webrtc/ice-servers")
async def ice_servers():
"""Browser fetches STUN/TURN config (with ephemeral TURN creds when configured)."""
return {"iceServers": client_ice_servers()}
@router.websocket("/ws/voice")
@@ -87,7 +87,7 @@ async def _handle_offer(websocket, payload, peers):
await pc.renegotiate(sdp=offer.sdp, type=offer.type, restart_pc=False)
else:
cfg = await _resolve_config(offer) # 解析放在建连前,配置错就别建连
pc = SmallWebRTCConnection(ice_servers=_ice_servers())
pc = SmallWebRTCConnection(ice_servers=aiortc_ice_servers())
if pc_id:
pc._pc_id = pc_id
await pc.initialize(sdp=offer.sdp, type=offer.type)