Developer Referencewebrtc_utils
RESTRTCMonitor
Periodically re-fetches TURN credentials from a TURN REST API.
Fetches every period seconds and delivers the parsed config through the
on_rtc_config callback, which the consumer must assign before start().
Request parameters (protocol, TLS, username) travel in configurable HTTP
headers so custom REST endpoints can be matched without code changes.
Attributes
attributeperiod= periodattributeenabled= enabledattributestop_event= asyncio.Event()attributeturn_rest_uri= turn_rest_uriattributeturn_rest_username= turn_rest_username.replace(':', '-')attributeturn_rest_username_auth_header= turn_rest_username_auth_headerattributeturn_protocol= turn_protocolattributeturn_rest_protocol_header= turn_rest_protocol_headerattributeturn_tls= turn_tlsattributeturn_rest_tls_header= turn_rest_tls_headerattributeturn_api_key= turn_api_key if turn_api_key else Noneattributeon_rtc_configCallable[[List[str], List[str], bytes], Any]= lambda stun_servers, turn_servers, rtc_config: logger_rtcice.warning('unhandled on_rtc_config')Functions
func__init__(self, turn_rest_uri, turn_rest_username, turn_rest_username_auth_header, turn_protocol='udp', turn_rest_protocol_header='x-turn-protocol', turn_tls=False, turn_rest_tls_header='x-turn-tls', turn_api_key=None, period=60, enabled=True)Source Code
def __init__(
self,
turn_rest_uri: str,
turn_rest_username: str,
turn_rest_username_auth_header: str,
turn_protocol: str = 'udp',
turn_rest_protocol_header: str = 'x-turn-protocol',
turn_tls: bool = False,
turn_rest_tls_header: str = 'x-turn-tls',
turn_api_key: Optional[str] = None,
period: int = 60,
enabled: bool = True
):
self.period = period
self.enabled = enabled
self.stop_event = asyncio.Event()
self._task: Optional[asyncio.Task] = None
self.turn_rest_uri = turn_rest_uri
self.turn_rest_username = turn_rest_username.replace(":", "-")
self.turn_rest_username_auth_header = turn_rest_username_auth_header
self.turn_protocol = turn_protocol
self.turn_rest_protocol_header = turn_rest_protocol_header
self.turn_tls = turn_tls
self.turn_rest_tls_header = turn_rest_tls_header
self.turn_api_key = turn_api_key if turn_api_key else None
self.on_rtc_config: Callable[[List[str], List[str], bytes], Any] = lambda stun_servers, turn_servers, rtc_config: logger_rtcice.warning("unhandled on_rtc_config")paramselfparamturn_rest_uristrparamturn_rest_usernamestrparamturn_rest_username_auth_headerstrparamturn_protocolstr= 'udp'paramturn_rest_protocol_headerstr= 'x-turn-protocol'paramturn_tlsbool= Falseparamturn_rest_tls_headerstr= 'x-turn-tls'paramturn_api_keyOptional[str]= Noneparamperiodint= 60paramenabledbool= TrueReturns
Nonefuncstart(self) -> NoneStarts the periodic refresh task; no-op when disabled.
Source Code
def start(self) -> None:
"""Starts the periodic refresh task; no-op when disabled."""
if not self.enabled:
return
self.stop_event.clear()
self._task = asyncio.create_task(self._monitor_loop())
logger_rtcice.info("TURN REST RTC monitor started")paramselfReturns
Nonefunc_monitor_loop(self) -> NoneFetches and dispatches REST configs until stopped.
Per-iteration failures are logged and retried on the next period rather than killing the monitor.
Source Code
async def _monitor_loop(self) -> None:
"""Fetches and dispatches REST configs until stopped.
Per-iteration failures are logged and retried on the next period
rather than killing the monitor.
"""
try:
while not self.stop_event.is_set():
try:
stun_servers, turn_servers, rtc_config = await fetch_turn_rest(
self.turn_rest_uri,
self.turn_rest_username,
self.turn_rest_username_auth_header,
self.turn_protocol,
self.turn_rest_protocol_header,
self.turn_tls,
self.turn_rest_tls_header,
self.turn_api_key
)
await _dispatch_rtc_callback(self.on_rtc_config, stun_servers, turn_servers, rtc_config)
except Exception as e:
logger_rtcice.warning(f"could not fetch TURN REST config in periodic monitor: {e}")
try:
await asyncio.wait_for(self.stop_event.wait(), timeout=self.period)
except asyncio.TimeoutError:
pass
except asyncio.CancelledError:
pass
except Exception as e:
logger_rtcice.error(f"Error in TURN REST RTC monitor: {e}")
finally:
logger_rtcice.info("TURN REST RTC monitor stopped")paramselfReturns
Nonefuncstop(self) -> NoneSignals the loop to exit and waits for the task to finish.
Source Code
async def stop(self) -> None:
"""Signals the loop to exit and waits for the task to finish."""
self.stop_event.set()
if self._task:
await self._taskparamselfReturns
None