Files
ai-video-fullstack/backend/services/webrtc_ice.py
Xin Wang 35d24acf40 Refactor backend configuration management and update environment settings
- Replace the `config.py` module with a new `settings.py` to streamline environment variable management, focusing on database, CORS, and TURN settings.
- Update references throughout the backend codebase to use the new `settings` module instead of the deprecated `config`.
- Modify the `.env.example` file to reflect the new configuration approach, indicating that model provider credentials should be maintained separately.
- Enhance the `AssistantConfig` model to clarify the source of runtime connection information, ensuring it is injected from model resources rather than relying on defaults from the environment.
- Introduce new user scripts for audio and video management in the Tampermonkey environment, enhancing WebRTC capabilities.
2026-07-09 21:40:29 +08:00

61 lines
1.8 KiB
Python

"""WebRTC ICE server config: STUN + optional TURN for cross-network voice preview."""
from __future__ import annotations
import base64
import hashlib
import hmac
import time
import settings
STUN_URL = "stun:stun.l.google.com:19302"
def _turn_credentials() -> tuple[str, str] | None:
"""Return (username, credential) for TURN, or None when TURN is not configured."""
if not settings.TURN_URLS:
return None
if settings.TURN_SECRET:
expiry = int(time.time()) + settings.TURN_CREDENTIAL_TTL
username = f"{expiry}:ai-video"
credential = base64.b64encode(
hmac.new(
settings.TURN_SECRET.encode("utf-8"),
username.encode("utf-8"),
hashlib.sha1,
).digest()
).decode("utf-8")
return username, credential
if settings.TURN_USERNAME and settings.TURN_PASSWORD:
return settings.TURN_USERNAME, settings.TURN_PASSWORD
return None
def client_ice_servers() -> list[dict]:
"""ICE servers for browser RTCPeerConnection (JSON-serializable)."""
servers: list[dict] = [{"urls": STUN_URL}]
creds = _turn_credentials()
if not creds:
return servers
username, credential = creds
for url in settings.TURN_URLS:
servers.append({"urls": url, "username": username, "credential": credential})
return servers
def aiortc_ice_servers() -> list:
"""ICE servers for backend SmallWebRTCConnection / aiortc."""
from aiortc import RTCIceServer
servers = [RTCIceServer(urls=STUN_URL)]
creds = _turn_credentials()
if not creds:
return servers
username, credential = creds
for url in settings.TURN_URLS:
servers.append(
RTCIceServer(urls=url, username=username, credential=credential)
)
return servers