From 1cb5c11239214d30b174d5d5257d7442e5565727 Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Mon, 24 Feb 2025 19:51:43 +0200 Subject: [PATCH] pikvm/pikvm#1339: Pass ICE servers to the Web UI --- PKGBUILD | 2 +- kvmd/apps/janus/runner.py | 6 +++++- kvmd/apps/janus/stun.py | 2 ++ web/share/js/kvm/stream_janus.js | 22 ++++++++++++++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/PKGBUILD b/PKGBUILD index 4fc66a4a..95a92dba 100644 --- a/PKGBUILD +++ b/PKGBUILD @@ -96,7 +96,7 @@ depends=( certbot platform-io-access raspberrypi-utils - "ustreamer>=6.31" + "ustreamer>=6.33" # Systemd UDEV bug "systemd>=248.3-2" diff --git a/kvmd/apps/janus/runner.py b/kvmd/apps/janus/runner.py index 9e426021..f1a99489 100644 --- a/kvmd/apps/janus/runner.py +++ b/kvmd/apps/janus/runner.py @@ -21,6 +21,7 @@ class _Netcfg: nat_type: StunNatType = dataclasses.field(default=StunNatType.ERROR) src_ip: str = dataclasses.field(default="") ext_ip: str = dataclasses.field(default="") + stun_host: str = dataclasses.field(default="") stun_ip: str = dataclasses.field(default="") stun_port: int = dataclasses.field(default=0) @@ -172,7 +173,10 @@ class JanusRunner: # pylint: disable=too-many-instance-attributes part.format(**placeholders) for part in cmd ] - self.__janus_proc = await aioproc.run_process(cmd) + self.__janus_proc = await aioproc.run_process( + cmd=cmd, + env={"JANUS_USTREAMER_WEB_ICE_URL": f"stun:{netcfg.stun_host}:{netcfg.stun_port}"}, + ) get_logger(0).info("Started Janus pid=%d: %s", self.__janus_proc.pid, tools.cmdfmt(cmd)) async def __kill_janus_proc(self) -> None: diff --git a/kvmd/apps/janus/stun.py b/kvmd/apps/janus/stun.py index 41cd86e7..7026ec2b 100644 --- a/kvmd/apps/janus/stun.py +++ b/kvmd/apps/janus/stun.py @@ -30,6 +30,7 @@ class StunInfo: nat_type: StunNatType src_ip: str ext_ip: str + stun_host: str stun_ip: str stun_port: int @@ -102,6 +103,7 @@ class Stun: nat_type=nat_type, src_ip=src_ip, ext_ip=ext_ip, + stun_host=self.__host, stun_ip=self.__stun_ip, stun_port=self.__port, ) diff --git a/web/share/js/kvm/stream_janus.js b/web/share/js/kvm/stream_janus.js index 18a70005..a7c93ee9 100644 --- a/web/share/js/kvm/stream_janus.js +++ b/web/share/js/kvm/stream_janus.js @@ -49,6 +49,8 @@ export function JanusStreamer(__setActive, __setInactive, __setInfo, __orient, _ var __state = null; var __frames = 0; + var __ice = null; + /************************************************************************/ self.getOrientation = () => __orient; @@ -99,6 +101,7 @@ export function JanusStreamer(__setActive, __setInactive, __setInfo, __orient, _ "server": tools.makeWsUrl("janus/ws"), "ipv6": true, "destroyOnUnload": false, + "iceServers": () => __getIceServers(), "success": __attachJanus, "error": function(error) { __logError(error); @@ -109,6 +112,15 @@ export function JanusStreamer(__setActive, __setInactive, __setInfo, __orient, _ } }; + var __getIceServers = function() { + if (__ice !== null && __ice.url) { + __logInfo("Using the custom ICE Server got from uStreamer:", __ice); + return [{"urls": __ice.url}]; + } else { + return []; + } + }; + var __finishJanus = function() { if (__stop) { if (__retry_ensure_timeout !== null) { @@ -201,7 +213,8 @@ export function JanusStreamer(__setActive, __setInactive, __setInfo, __orient, _ "success": function(handle) { __handle = handle; __logInfo("uStreamer attached:", handle.getPlugin(), handle.getId()); - __sendWatch(); + __logInfo("Sending FEATURES ..."); + __handle.send({"message": {"request": "features"}}); }, "error": function(error) { @@ -232,7 +245,7 @@ export function JanusStreamer(__setActive, __setInactive, __setInfo, __orient, _ __stopRetryEmsgInterval(); if (msg.result) { - __logInfo("Got uStreamer result message:", msg.result.status); // starting, started, stopped + __logInfo("Got uStreamer result message:", msg.result); // starting, started, stopped if (msg.result.status === "started") { __setActive(); __setInfo(false, false, ""); @@ -242,6 +255,8 @@ export function JanusStreamer(__setActive, __setInactive, __setInfo, __orient, _ } else if (msg.result.status === "features") { tools.feature.setEnabled($("stream-audio"), msg.result.features.audio); tools.feature.setEnabled($("stream-mic"), msg.result.features.mic); + __ice = msg.result.features.ice; + __sendWatch(); } } else if (msg.error_code || msg.error) { __logError("Got uStreamer error message:", msg.error_code, "-", msg.error); @@ -367,8 +382,7 @@ export function JanusStreamer(__setActive, __setInactive, __setInfo, __orient, _ var __sendWatch = function() { if (__handle) { - __logInfo(`Sending WATCH(orient=${__orient}, audio=${__allow_audio}, mic=${__allow_mic}) + FEATURES ...`); - __handle.send({"message": {"request": "features"}}); + __logInfo(`Sending WATCH(orient=${__orient}, audio=${__allow_audio}, mic=${__allow_mic}) ...`); __handle.send({"message": {"request": "watch", "params": { "orientation": __orient, "audio": __allow_audio,