Live view over WebRTC (openpanel L1, server and browser)
A device reporting a `live` port streams its camera to the browser's own
RTCPeerConnection. The flows socket carries the signalling as text, so it
crosses the portal: `live` messages on a person's socket, `signal` on a
device's (start, sdp, candidate, bye, busy). The server routes by session,
holds a device to two viewers, ends a session when either socket closes or
no answer comes within 10 s, and gives each socket an outbox of its own
that ends a session rather than drop a message. TURN credentials are
minted per session in coturn's REST scheme (STUN_URLS, TURN_URLS,
TURN_SECRET), the viewer's with the device's offer; GET /devices/{id}/ice
hands them out too. The tunnel keeps a stream's early frames instead of
dropping them past eight and forgets them when the stream or link goes.
A media tile on a device's camera, and the Devices tab's preview, play
live when tapped, pause frames meanwhile, and fall back to frames with a
short note on busy, failure or no picture within 10 s. coturn is an
optional compose service behind the `turn` profile.
This commit is contained in:
@@ -71,6 +71,7 @@ from fluksio.flow.devices import (
|
||||
read_report,
|
||||
)
|
||||
from fluksio.flow.events import event_bus
|
||||
from fluksio.flow.live import ICE_TTL_S, ice_servers
|
||||
from fluksio.flow.state import CONNECT_TIMEOUT_S, SOCKET_TIMEOUT_S
|
||||
from fluksio.flow.store import StaleVersion
|
||||
from fluksio.models import Message, User
|
||||
@@ -387,6 +388,20 @@ class DevicesPublic(BaseModel):
|
||||
frontend_host: str = ""
|
||||
|
||||
|
||||
class IceServer(BaseModel):
|
||||
"""One STUN or TURN server, as `RTCPeerConnection` takes it."""
|
||||
|
||||
urls: list[str]
|
||||
username: str | None = None
|
||||
credential: str | None = None
|
||||
|
||||
|
||||
class IceServers(BaseModel):
|
||||
ice_servers: list[IceServer] = Field(default_factory=list)
|
||||
#: Seconds the TURN credentials stay good.
|
||||
ttl: int = 0
|
||||
|
||||
|
||||
def _describe(request: Request) -> str:
|
||||
"""A line naming the device behind a pairing request.
|
||||
|
||||
@@ -665,6 +680,23 @@ async def read_device(device_id: str, store: DeviceStoreDep) -> Any:
|
||||
return await run_in_threadpool(_document, device, store)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/{device_id}/ice",
|
||||
response_model=IceServers,
|
||||
response_model_exclude_none=True,
|
||||
)
|
||||
async def read_ice(device_id: str, store: DeviceStoreDep, user: CurrentUser) -> Any:
|
||||
"""Where a browser looks for a way to this device's live view.
|
||||
|
||||
STUN and TURN servers, the TURN ones with credentials of the caller's own
|
||||
that expire. Empty when none are set, which is enough on one network.
|
||||
"""
|
||||
if await run_in_threadpool(store.find, device_id) is None:
|
||||
raise _not_found(device_id)
|
||||
servers = [IceServer(**server) for server in ice_servers(str(user.id))]
|
||||
return IceServers(ice_servers=servers, ttl=ICE_TTL_S)
|
||||
|
||||
|
||||
panels_router.add_api_route(
|
||||
"/{device_id}",
|
||||
read_device,
|
||||
|
||||
Reference in New Issue
Block a user