diff --git a/src/anam/types.py b/src/anam/types.py index 6ab936d..47c8630 100644 --- a/src/anam/types.py +++ b/src/anam/types.py @@ -231,6 +231,24 @@ class AgentAudioInputPayload: sequence_number: int +def _reorder_ice_server_urls(ice_servers: list[dict[str, Any]]) -> list[dict[str, Any]]: + """Reorder URLs to prioritize TURNS (TURN over TLS) as aiortc only uses the first TURN URI.""" + + def _turns_first(u): + return 0 if str(u).lower().startswith("turns:") else 1 + + return [ + { + **s, + "urls": sorted( + [s["urls"]] if isinstance(s.get("urls"), str) else s.get("urls", []), + key=_turns_first, + ), + } + for s in ice_servers + ] + + @dataclass class SessionInfo: """Information about an active streaming session. @@ -257,5 +275,5 @@ def from_api_response(cls, data: dict[str, Any]) -> "SessionInfo": signalling_endpoint=data["signallingEndpoint"], heartbeat_interval_seconds=client_config.get("heartbeatIntervalSeconds", 5), max_reconnection_attempts=client_config.get("maxWsReconnectionAttempts", 5), - ice_servers=client_config.get("iceServers", []), + ice_servers=_reorder_ice_server_urls(client_config.get("iceServers") or []), ) diff --git a/uv.lock b/uv.lock index 1200a81..89f9fe8 100644 --- a/uv.lock +++ b/uv.lock @@ -188,7 +188,7 @@ wheels = [ [[package]] name = "anam" -version = "0.2.0" +version = "0.3.0" source = { editable = "." } dependencies = [ { name = "aiohttp" },