| 1 |
"""Client library for RTSPtoWebRTC server.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import logging |
| 6 |
from collections.abc import Mapping |
| 7 |
from typing import Any |
| 8 |
|
| 9 |
import aiohttp |
| 10 |
|
| 11 |
from . import diagnostics |
| 12 |
from .diagnostics import DISCOVERY_DIAGNOSTICS as DIAGNOSTICS |
| 13 |
from .exceptions import ClientError |
| 14 |
from .interface import WebRTCClientInterface |
| 15 |
from .web_client import WebClient |
| 16 |
from .webrtc_client import WebRTCClient |
| 17 |
|
| 18 |
_LOGGER = logging.getLogger(__name__) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
Client = WebRTCClient |
| 23 |
|
| 24 |
|
| 25 |
async def get_adaptive_client( |
| 26 |
websession: aiohttp.ClientSession, server_url: str | None = None |
| 27 |
) -> WebRTCClientInterface: |
| 28 |
"""Initialize Client that can auto-detect the appropriate client for the server.""" |
| 29 |
web_client = WebClient(websession, server_url) |
| 30 |
webrtc_client = WebRTCClient(websession, server_url) |
| 31 |
|
| 32 |
DIAGNOSTICS.increment("attempt") |
| 33 |
web_heartbeat = web_client.heartbeat() |
| 34 |
webrtc_heartbeat = webrtc_client.heartbeat() |
| 35 |
|
| 36 |
client: WebRTCClientInterface | None = None |
| 37 |
web_err: ClientError | None = None |
| 38 |
try: |
| 39 |
await web_heartbeat |
| 40 |
except ClientError as err: |
| 41 |
DIAGNOSTICS.increment("web.failure") |
| 42 |
_LOGGER.debug("Discovery of RTSPtoWeb server failed: %s", str(err)) |
| 43 |
web_err = err |
| 44 |
else: |
| 45 |
DIAGNOSTICS.increment("web.success") |
| 46 |
client = web_client |
| 47 |
try: |
| 48 |
await webrtc_heartbeat |
| 49 |
except ClientError as err: |
| 50 |
DIAGNOSTICS.increment("webrtc.failure") |
| 51 |
_LOGGER.debug("Discovery of RTSPtoWebRTC server failed: %s", str(err)) |
| 52 |
if not client: |
| 53 |
assert web_err |
| 54 |
raise web_err |
| 55 |
else: |
| 56 |
DIAGNOSTICS.increment("webrtc.success") |
| 57 |
if not client: |
| 58 |
client = webrtc_client |
| 59 |
|
| 60 |
return client |
| 61 |
|
| 62 |
|
| 63 |
def get_diagnostics() -> Mapping[str, Any]: |
| 64 |
"""Return client library diagnostic debug information.""" |
| 65 |
return diagnostics.get_diagnostics() |