Skip to content

LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.

Remote desktop video: a keyframe request re-encodes the current frame, one flush frame after a change, x264 ultrafast

Federico Pereirae2644086c664
diff --git a/server/src/media/helper/lm_stream.c b/server/src/media/helper/lm_stream.c
index 5f008f2..63c22fc 100644
--- a/server/src/media/helper/lm_stream.c
+++ b/server/src/media/helper/lm_stream.c
@@ -123,7 +123,7 @@ static AVCodecContext *open_sw(Vid *e, const char *name, int fps) {
c->thread_count = 2;
c->rc_max_rate = (int64_t)e->kbps * 1000;
c->rc_buffer_size = (int)(c->rc_max_rate / 3); /* about 1/3 s: low latency */
- av_dict_set(&o, "preset", "veryfast", 0);
+ av_dict_set(&o, "preset", "ultrafast", 0);
av_dict_set(&o, "tune", "zerolatency", 0);
av_dict_set(&o, "profile", "baseline", 0);
av_dict_set(&o, "crf", "22", 0);
diff --git a/server/src/media/mstream.c b/server/src/media/mstream.c
index 2d7671d..e3ba9fb 100644
--- a/server/src/media/mstream.c
+++ b/server/src/media/mstream.c
@@ -26,8 +26,8 @@ typedef struct {
int id; /* the MediaSrc of the helper (0: not running) */
int viewers, audio;
unsigned kbps, sent_kbps;
- int w, h, pending, want_key;
- double t0, last_feed, retry_at;
+ int w, h, pending, want_key, flushing;
+ double t0, last_feed, retry_at, flush_at;
uint8_t *buf;
size_t len, cap, total;
long frames, enc100;
@@ -174,8 +174,12 @@ void mstream_want(int codec, int viewers, int audio, unsigned kbps) {
if (!e->id) start(e, codec);
}
+/* A keyframe now, even if the screen doesn't change: frames are only encoded when something changes,
+ * and a viewer that lost one (or just joined) would otherwise wait for the next change. */
void mstream_keyframe(int codec) {
- if (codec >= 1 && codec < MS_CODECS) E[codec].want_key = 1;
+ if (codec < 1 || codec >= MS_CODECS) return;
+ E[codec].want_key = 1;
+ E[codec].pending = 1;
}
static void feed(Enc *e, int codec) {
@@ -185,8 +189,11 @@ static void feed(Enc *e, int codec) {
start(e, codec);
return;
}
- if (!e->pending || !fpx) return;
double t = now_ms();
+ /* one more (tiny, unchanged) frame shortly after the last change: browsers paint a lone frame late,
+ * often only when the next one arrives */
+ if (!e->pending && e->flush_at && t >= e->flush_at) { e->pending = 1; e->flush_at = 0; e->flushing = 1; }
+ if (!e->pending || !fpx) return;
if (t - e->last_feed < 1000.0 / FPS - 1) return;
int slot = -1;
for (int i = 0; i < s->in_slots; i++) if (!s->in_busy[i]) { slot = i; break; }
@@ -199,6 +206,8 @@ static void feed(Enc *e, int codec) {
s->in_busy[slot] = 1;
e->last_feed = t;
e->pending = 0;
+ e->flush_at = e->flushing ? 0 : t + 70;
+ e->flushing = 0;
}
void mstream_frame(const uint32_t *px, int stride, int w, int h) {
@@ -224,7 +233,8 @@ double mstream_timeout(void) {
for (int c = 1; c < MS_CODECS; c++) {
Enc *e = &E[c];
if (!e->viewers) continue;
- double d = !e->id ? (e->retry_at > t ? e->retry_at - t : 200) : e->pending ? 1000.0 / FPS - (t - e->last_feed) : -1;
+ double d = !e->id ? (e->retry_at > t ? e->retry_at - t : 200) : e->pending ? 1000.0 / FPS - (t - e->last_feed)
+ : e->flush_at ? e->flush_at - t : -1;
if (d >= 0 && d < 4) d = 4;
if (d >= 0 && (best < 0 || d < best)) best = d;
}
diff --git a/server/src/remote/client.html b/server/src/remote/client.html
index 5f76cdd..a854ccc 100644
--- a/server/src/remote/client.html
+++ b/server/src/remote/client.html
@@ -125,6 +125,9 @@ function startRtc(ice) {
tiles.binaryType = 'arraybuffer';
tiles.onmessage = (e) => onTile(new Uint8Array(e.data));
pc.ontrack = (e) => {
+ // a desktop wants the newest frame now, not a smooth buffer
+ try { e.receiver.jitterBufferTarget = 0; } catch (x) {}
+ try { e.receiver.playoutDelayHint = 0; } catch (x) {}
const ms = e.streams[0] || new MediaStream([e.track]);
if (vid.srcObject !== ms) vid.srcObject = ms;
vid.play().catch(() => {});
diff --git a/server/src/remote/remote_admin.c b/server/src/remote/remote_admin.c
index ed836cf..5e0c1ae 100644
--- a/server/src/remote/remote_admin.c
+++ b/server/src/remote/remote_admin.c
@@ -9,12 +9,17 @@
* remote-allow add|del <cidr> the "only these addresses" list (net=list)
* remote-member <u>\n1|0 adds or removes a user from the lordos-remote group
* remote-kick <pid> cuts a connection (only a pid from lord-sessiond's list)
+ * remote-ice <lines> the video path (docs/REMOTE.md, "Video (WebRTC)"): video=on|off,
+ * stun=<host:port>[,…] (up to 4), turn=<host:port> (empty: none),
+ * turn_user=<name>, turn_pass=<password> (kept in turn.key, root 0600;
+ * never written to the config, the logs or an answer)
*
* The remote desktop is part of lord-sessiond (lordos.service, docs/SESSIONS.md): these
* actions only change /etc/lordos/remote/config and notify it with SIGHUP. */
#include "remote_admin.h"
#include "ipnet.h"
#include "lockout.h"
+#include "rtcwire.h"
#include <errno.h>
#include <fcntl.h>
@@ -31,6 +36,7 @@
#define CONF_DIR "/etc/lordos/remote"
#define RD_ACCESS_PATH CONF_DIR "/access"
+#define RD_TURN_KEY_PATH CONF_DIR "/turn.key"
static int write_file(const char *path, const char *text, mode_t mode) {
char tmp[PATH_MAX];
@@ -43,16 +49,28 @@ static int write_file(const char *path, const char *text, mode_t mode) {
return ok && !rename(tmp, path) ? 0 : -1;
}
-typedef struct { int enabled, port, group_only, insecure_dev, net; } Conf;
+typedef struct {
+ int enabled, port, group_only, insecure_dev, net;
+ int video, nstun, turn_key;
+ char stun[4][96], turn[96], turn_user[129];
+} Conf;
static void conf_read(Conf *c) {
c->port = 5673;
c->enabled = c->group_only = c->insecure_dev = 0;
c->net = RS_NET_LAN;
+ c->video = 1;
+ c->nstun = c->turn_key = 0;
+ c->turn[0] = c->turn_user[0] = 0;
FILE *f = fopen(CONF_DIR "/config", "r");
- char line[128], s[32];
+ char line[256], s[32], h[160];
int v;
while (f && fgets(line, sizeof line, f)) {
+ if (sscanf(line, "video=%d", &v) == 1) { c->video = v != 0; continue; }
+ if (sscanf(line, "turn_key=%d", &v) == 1) { c->turn_key = v == 1; continue; }
+ if (sscanf(line, "stun=%159s", h) == 1) { if (rw_ice_server_valid(h) && c->nstun < 4) snprintf(c->stun[c->nstun++], 96, "%.95s", h); continue; }
+ if (sscanf(line, "turn=%159s", h) == 1) { if (rw_ice_server_valid(h)) snprintf(c->turn, sizeof c->turn, "%.95s", h); continue; }
+ if (sscanf(line, "turn_user=%159s", h) == 1) { if (rw_turn_user_valid(h)) snprintf(c->turn_user, sizeof c->turn_user, "%.128s", h); continue; }
if (sscanf(line, "enabled=%d", &v) == 1) c->enabled = v == 1;
else if (sscanf(line, "port=%d", &v) == 1 && v >= 1024 && v < 65536) c->port = v;
else if (sscanf(line, "allow=%31s", s) == 1) c->group_only = !strcmp(s, "group");
@@ -65,9 +83,11 @@ static void conf_read(Conf *c) {
static int conf_write(const Conf *c) {
mkdir("/etc/lordos", 0755);
mkdir(CONF_DIR, 0755);
- char text[256];
- snprintf(text, sizeof text, "# LordOS · remote desktop (Settings → Sharing). See docs/REMOTE.md\nenabled=%d\nport=%d\nallow=%s\nnet=%s\ninsecure_dev=%d\n",
- c->enabled, c->port, c->group_only ? "group" : "all", rs_net_name(c->net), c->insecure_dev);
+ char text[1400];
+ int n = snprintf(text, sizeof text, "# LordOS · remote desktop (Settings → Sharing). See docs/REMOTE.md\nenabled=%d\nport=%d\nallow=%s\nnet=%s\ninsecure_dev=%d\nvideo=%d\n",
+ c->enabled, c->port, c->group_only ? "group" : "all", rs_net_name(c->net), c->insecure_dev, c->video);
+ for (int i = 0; i < c->nstun; i++) n += snprintf(text + n, sizeof text - (size_t)n, "stun=%s\n", c->stun[i]);
+ if (c->turn[0]) n += snprintf(text + n, sizeof text - (size_t)n, "turn=%s\nturn_user=%s\nturn_key=%d\n", c->turn, c->turn_user, c->turn_key);
return write_file(CONF_DIR "/config", text, 0644);
}
@@ -323,3 +343,59 @@ void remote_act_failures(const AdminReq *r, const char *arg, size_t len) {
}
admin_ok("remote.failures", "count", lerr_int(rows), NULL);
}
+
+/* remote-ice: the video path's settings (Settings → Sharing → Video). Every line is checked before
+ * anything is written; the TURN password goes only to turn.key (root 0600), which lord-sessiond reads
+ * for each video connection and hands to the bridge in memory. */
+void remote_act_ice(const AdminReq *r, const char *arg, size_t len) {
+ (void)r;
+ if (len > 2048) { admin_err("admin.invalidArgument", NULL); return; }
+ Conf c;
+ conf_read(&c);
+ char buf[2050], pass[130] = "";
+ int set_turn = 0, set_pass = 0;
+ snprintf(buf, sizeof buf, "%.*s", (int)len, arg);
+ for (char *save = NULL, *line = strtok_r(buf, "\n", &save); line; line = strtok_r(NULL, "\n", &save)) {
+ if (!strcmp(line, "video=on") || !strcmp(line, "video=off")) c.video = !strcmp(line, "video=on");
+ else if (!strncmp(line, "stun=", 5)) {
+ c.nstun = 0;
+ char list[600];
+ snprintf(list, sizeof list, "%s", line + 5);
+ for (char *s2 = NULL, *one = strtok_r(list, ",", &s2); one; one = strtok_r(NULL, ",", &s2)) {
+ if (c.nstun >= 4 || !rw_ice_server_valid(one)) { admin_err("remote.badIceServer", NULL); goto out; }
+ snprintf(c.stun[c.nstun++], 96, "%.95s", one);
+ }
+ } else if (!strncmp(line, "turn=", 5)) {
+ if (line[5] && !rw_ice_server_valid(line + 5)) { admin_err("remote.badIceServer", NULL); goto out; }
+ snprintf(c.turn, sizeof c.turn, "%.95s", line + 5);
+ set_turn = 1;
+ } else if (!strncmp(line, "turn_user=", 10)) {
+ if (!rw_turn_user_valid(line + 10)) { admin_err("remote.badTurnUser", NULL); goto out; }
+ snprintf(c.turn_user, sizeof c.turn_user, "%.128s", line + 10);
+ } else if (!strncmp(line, "turn_pass=", 10)) {
+ if (!rw_turn_pass_valid(line + 10)) { admin_err("remote.badTurnPassword", NULL); goto out; }
+ snprintf(pass, sizeof pass, "%s", line + 10);
+ set_pass = 1;
+ } else { admin_err("admin.invalidArgument", NULL); goto out; }
+ }
+ if (set_turn && !c.turn[0]) { c.turn_user[0] = 0; c.turn_key = 0; set_pass = 0; unlink(RD_TURN_KEY_PATH); }
+ if (c.turn[0] && !c.turn_user[0]) { admin_err("remote.badTurnUser", NULL); goto out; }
+ if (set_pass) {
+ if (!c.turn[0]) { admin_err("admin.invalidArgument", NULL); goto out; }
+ mkdir("/etc/lordos", 0755);
+ mkdir(CONF_DIR, 0755);
+ char line[140];
+ snprintf(line, sizeof line, "%s\n", pass);
+ int w = write_file(RD_TURN_KEY_PATH, line, 0600);
+ explicit_bzero(line, sizeof line);
+ if (w || chmod(RD_TURN_KEY_PATH, 0600)) { admin_err("remote.configFailed", NULL); goto out; }
+ c.turn_key = 1;
+ }
+ if (conf_write(&c)) { admin_err("remote.configFailed", NULL); goto out; }
+ pid_t pid = sessiond_pid();
+ if (pid > 0) kill(pid, SIGHUP);
+ admin_ok("remote.saved", NULL);
+out:
+ explicit_bzero(pass, sizeof pass);
+ explicit_bzero(buf, sizeof buf);
+}
diff --git a/server/src/remote/remote_admin.h b/server/src/remote/remote_admin.h
index 044cd78..a39ce43 100644
--- a/server/src/remote/remote_admin.h
+++ b/server/src/remote/remote_admin.h
@@ -13,6 +13,7 @@ void remote_act_block(const AdminReq *r, const char *arg, size_t len);
void remote_act_unblock(const AdminReq *r, const char *arg, size_t len);
void remote_act_allow(const AdminReq *r, const char *arg, size_t len);
void remote_act_failures(const AdminReq *r, const char *arg, size_t len);
+void remote_act_ice(const AdminReq *r, const char *arg, size_t len);
#define REMOTE_ADMIN_ACTIONS \
{ "remote-enable", remote_act_enable, A_FORK }, \
@@ -23,6 +24,7 @@ void remote_act_failures(const AdminReq *r, const char *arg, size_t len);
{ "remote-block", remote_act_block, 0 }, \
{ "remote-unblock", remote_act_unblock, 0 }, \
{ "remote-allow", remote_act_allow, 0 }, \
- { "remote-failures", remote_act_failures, 0 },
+ { "remote-failures", remote_act_failures, 0 }, \
+ { "remote-ice", remote_act_ice, 0 },
#endif