screen-relay 0.0.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/LICENSE +201 -0
  2. package/Makefile +165 -0
  3. package/README.md +112 -1
  4. package/THIRD-PARTY-NOTICES.md +75 -0
  5. package/dist/cli/client.d.ts +28 -0
  6. package/dist/cli/commands.d.ts +52 -0
  7. package/dist/cli/config.d.ts +32 -0
  8. package/dist/cli/format.d.ts +15 -0
  9. package/dist/cli/help.d.ts +5 -0
  10. package/dist/cli/host/common.d.ts +23 -0
  11. package/dist/cli/host/doctor.d.ts +2 -0
  12. package/dist/cli/host/packages.d.ts +13 -0
  13. package/dist/cli/host/session_commands.d.ts +8 -0
  14. package/dist/cli/host/setup.d.ts +22 -0
  15. package/dist/cli/host/state.d.ts +23 -0
  16. package/dist/cli/host/supervisor.d.ts +94 -0
  17. package/dist/cli/host/watcher.d.ts +4 -0
  18. package/dist/cli/host/windows.d.ts +82 -0
  19. package/dist/cli/main.d.ts +3 -0
  20. package/dist/cli/mode.d.ts +14 -0
  21. package/dist/cli/output.d.ts +24 -0
  22. package/dist/cli/parser.d.ts +6 -0
  23. package/dist/cli/relay.mjs +12631 -0
  24. package/dist/cli/skills.d.ts +21 -0
  25. package/dist/cli/theme.d.ts +14 -0
  26. package/dist/protocol/errors.d.ts +155 -0
  27. package/dist/protocol/index.d.ts +119 -0
  28. package/dist/protocol/index.js +418 -0
  29. package/dist/sdk/index.d.ts +78 -0
  30. package/dist/sdk/index.js +834 -0
  31. package/dist/sdk/transport.d.ts +20 -0
  32. package/dist/viewer/index.html +96 -0
  33. package/dist/viewer/viewer.js +1079 -0
  34. package/package.json +47 -7
  35. package/packages.tsv +38 -0
  36. package/skills/core/SKILL.md +171 -0
  37. package/skills/core/references/cli.md +119 -0
  38. package/skills/core/references/config.md +48 -0
  39. package/skills/core/references/macos.md +50 -0
  40. package/skills/core/references/sdk.md +69 -0
  41. package/skills/core/references/troubleshooting.md +33 -0
  42. package/skills/core/references/wayland.md +47 -0
  43. package/skills/screen-relay/SKILL.md +24 -0
  44. package/skills/vercel-sandbox/SKILL.md +68 -0
  45. package/src/engine/backends/linux/keycodes.hpp +35 -0
  46. package/src/engine/backends/linux/wayland/capture.cpp +298 -0
  47. package/src/engine/backends/linux/wayland/input.cpp +559 -0
  48. package/src/engine/backends/linux/wayland/keymap.cpp +118 -0
  49. package/src/engine/backends/linux/wayland/keymap.hpp +54 -0
  50. package/src/engine/backends/linux/wayland/protocols/virtual-keyboard-unstable-v1.xml +112 -0
  51. package/src/engine/backends/linux/wayland/protocols/wlr-screencopy-unstable-v1.xml +234 -0
  52. package/src/engine/backends/linux/wayland/protocols/wlr-virtual-pointer-unstable-v1.xml +152 -0
  53. package/src/engine/backends/linux/wayland/wayland.cpp +166 -0
  54. package/src/engine/backends/linux/wayland/wayland.hpp +60 -0
  55. package/src/engine/backends/linux/x11/capture.cpp +206 -0
  56. package/src/engine/backends/linux/x11/input.cpp +266 -0
  57. package/src/engine/backends/macos/capture.mm +367 -0
  58. package/src/engine/backends/macos/display.hpp +20 -0
  59. package/src/engine/backends/macos/display.mm +84 -0
  60. package/src/engine/backends/macos/input.mm +287 -0
  61. package/src/engine/backends/macos/keys.hpp +34 -0
  62. package/src/engine/backends/windows/capture.cpp +393 -0
  63. package/src/engine/backends/windows/input.cpp +274 -0
  64. package/src/engine/backends/windows/output.cpp +65 -0
  65. package/src/engine/backends/windows/output.hpp +26 -0
  66. package/src/engine/core/capture-bucket.hpp +40 -0
  67. package/src/engine/core/capture-source.hpp +59 -0
  68. package/src/engine/core/capture.cpp +565 -0
  69. package/src/engine/core/capture.hpp +58 -0
  70. package/src/engine/core/color.cpp +176 -0
  71. package/src/engine/core/color.hpp +16 -0
  72. package/src/engine/core/cpu-budget.hpp +196 -0
  73. package/src/engine/core/flow.hpp +317 -0
  74. package/src/engine/core/input.cpp +98 -0
  75. package/src/engine/core/input.hpp +115 -0
  76. package/src/engine/core/wait.cpp +125 -0
  77. package/src/engine/core/wait.hpp +41 -0
  78. package/src/engine/server/backend.cpp +65 -0
  79. package/src/engine/server/backend.hpp +18 -0
  80. package/src/engine/server/config.cpp +257 -0
  81. package/src/engine/server/config.hpp +24 -0
  82. package/src/engine/server/main.cpp +82 -0
  83. package/src/engine/server/metrics.cpp +47 -0
  84. package/src/engine/server/metrics.hpp +10 -0
  85. package/src/engine/server/screenshot.cpp +108 -0
  86. package/src/engine/server/screenshot.hpp +31 -0
  87. package/src/engine/server/server.cpp +535 -0
  88. package/src/engine/server/server.hpp +89 -0
  89. package/src/engine/server/wire.cpp +70 -0
  90. package/src/engine/server/wire.hpp +29 -0
  91. package/index.js +0 -2
@@ -0,0 +1,317 @@
1
+ #pragma once
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <cstddef>
6
+ #include <cstdint>
7
+ #include <deque>
8
+
9
+ namespace relay {
10
+ // All times are server steady-clock microseconds; service thread owns this state.
11
+ struct FlowControl {
12
+ static constexpr size_t max_bytes = 8 * 1024 * 1024;
13
+ static constexpr uint64_t pause_age_us = 250000;
14
+ static constexpr uint64_t timeout_us = 3000000;
15
+ static constexpr size_t max_frames = timeout_us * 120 / 1000000;
16
+ static constexpr uint64_t base_window_us = 10000000;
17
+ static constexpr uint64_t viewer_hold_limit_us = 100000;
18
+ enum class Pressure { clear, building, congested, paused };
19
+ static constexpr uint64_t delivery_window_us = 1000000;
20
+ static constexpr uint64_t bitrate_smoothing_us = 500000;
21
+ static constexpr uint64_t sample_interval_us = 10000, idle_sample_interval_us = 100000;
22
+ struct TransportSample {
23
+ uint64_t min_rtt_us = 0, rtt_us = 0, delivery_rate_bps = 0;
24
+ uint64_t notsent_bytes = 0, bytes_acked = 0, application_queued_bytes = 0;
25
+ uint32_t snd_cwnd = 0, total_retrans = 0;
26
+ bool app_limited = false;
27
+ };
28
+ struct DeliverySample { uint64_t time_us, rate_bps; };
29
+ /** Sliding-window extreme of timestamped values: keep a span, read the smallest (or largest) value over any shorter trailing window. */
30
+ struct WindowExtreme {
31
+ struct Sample { uint64_t time_us, value; };
32
+ std::deque<Sample> samples;
33
+ bool smallest = true;
34
+ void push(uint64_t now, uint64_t value, uint64_t span_us) {
35
+ while (!samples.empty() && now - std::min(now, samples.front().time_us) >= span_us) samples.pop_front();
36
+ while (!samples.empty() && (smallest ? samples.back().value >= value : samples.back().value <= value)) samples.pop_back();
37
+ samples.push_back({now, value});
38
+ }
39
+ bool empty() const { return samples.empty(); }
40
+ uint64_t overall(uint64_t none) const { return samples.empty() ? none : samples.front().value; }
41
+ /** Makes later reads over a window ignore anything older than `window_us` before `now`, even if a longer window is read later. */
42
+ void forget(uint64_t now, uint64_t window_us) { forgotten_us = std::max(forgotten_us, now - std::min(now, window_us)); }
43
+ uint64_t over(uint64_t now, uint64_t window_us, uint64_t none, uint64_t floor_us = 0) const {
44
+ for (const auto& sample : samples) {
45
+ if (sample.time_us > std::max(forgotten_us, floor_us) && now - std::min(now, sample.time_us) < window_us) return sample.value;
46
+ }
47
+ return none;
48
+ }
49
+ uint64_t forgotten_us = 0;
50
+ };
51
+ WindowExtreme depth, network, peak{{}, false}, held{{}, false};
52
+ /** Age of the oldest unacknowledged frame, sampled on service ticks; lets a stall be seen while no acknowledgements arrive. */
53
+ WindowExtreme waiting;
54
+ TransportSample transport;
55
+ bool transport_available = false;
56
+ uint64_t delivery_rate_bps = 0, last_transport_update_us = 0;
57
+ std::deque<DeliverySample> delivery_samples;
58
+ uint64_t min_bitrate = 1000000, initial_bitrate = 8000000, max_bitrate = 40000000;
59
+ struct Frame { uint32_t sequence; size_t bytes; uint64_t captured_us; bool sent = false; uint64_t sent_us = 0; };
60
+ std::deque<Frame> frames;
61
+ size_t bytes = 0;
62
+ /** Largest frame this generation; capture pauses while the byte budget left could not hold one, since enqueue rejects a frame that does not fit and the server then closes the stream. */
63
+ size_t largest_frame = 0;
64
+ uint32_t last_encoded = 0;
65
+ uint64_t acknowledged = 0, encoded = 0, last_progress_us = 0;
66
+ uint64_t target_bitrate = initial_bitrate;
67
+ /** Smoothed send-to-acknowledgement time; reported by /metrics only. */
68
+ double ack_rtt_us = 0;
69
+ unsigned window = 1;
70
+ uint64_t sample_start_us = 0, sample_bytes = 0, sample_acks = 0, previous_sent_us = 0;
71
+ uint64_t last_decrease_us = 0, last_increase_us = 0;
72
+ /** Rate measured over the last busy sample; reported by /metrics, and caps a cut. */
73
+ double goodput_bps = 0;
74
+ unsigned frame_rate = 60;
75
+
76
+ /** Applies the configured bitrate limits and starts at the configured initial rate. */
77
+ void configure(uint64_t minimum, uint64_t initial, uint64_t maximum, unsigned fps = 60) {
78
+ min_bitrate = minimum; initial_bitrate = initial; max_bitrate = maximum; target_bitrate = initial; frame_rate = std::max(1u, fps);
79
+ }
80
+ /** Restores the idle transport state while keeping the configured bitrate limits. */
81
+ void reset() {
82
+ const uint64_t minimum = min_bitrate, initial = initial_bitrate, maximum = max_bitrate;
83
+ const unsigned fps = frame_rate;
84
+ *this = FlowControl{};
85
+ configure(minimum, initial, maximum, fps);
86
+ }
87
+ /** Reads the viewer wait from an acknowledgement payload: 4 bytes of big-endian microseconds, or zero when absent or malformed. */
88
+ static uint64_t ackWaitUs(const uint8_t* payload, size_t size) {
89
+ return size == 4 ? (uint64_t(payload[0]) << 24) | (uint64_t(payload[1]) << 16) | (uint64_t(payload[2]) << 8) | payload[3] : 0;
90
+ }
91
+ uint64_t oldestAge(uint64_t now) const {
92
+ return frames.empty() ? 0 : now - std::min(now, frames.front().captured_us);
93
+ }
94
+ uint64_t frameIntervalUs() const { return (1000000ULL + frame_rate - 1) / frame_rate; }
95
+ uint64_t serviceDeadlineUs(uint64_t now, uint64_t pending) const {
96
+ const uint64_t next = now + (frames.empty() ? idle_sample_interval_us : sample_interval_us);
97
+ return pending ? std::min(pending, next) : next;
98
+ }
99
+ /** Capture-to-acknowledgement time of an uncongested frame: the network base plus the viewer's wait, so a hold the viewer adds is not read as queue; zero when unmeasured. A queue that stands for the whole window becomes the new base because nothing drains it; the pause and the 3 s timeout bound that. */
100
+ uint64_t baseDepthUs() const { return networkBaseDepthUs() + viewerWaitUs(); }
101
+ /** Largest capture-to-acknowledgement time over the last 10 s; sizes the frame window. */
102
+ uint64_t peakDepthUs() const { return peak.overall(0); }
103
+ /** Smallest network depth (capture-to-acknowledgement minus viewer wait) over the last 10 s; zero when unmeasured. */
104
+ uint64_t networkBaseDepthUs() const { return network.overall(0); }
105
+ /** Span over which the smallest queueing delay is taken: one base round trip, at least two frame intervals. */
106
+ uint64_t standingWindowUs() const { return std::max(baseDepthUs(), 2 * frameIntervalUs()); }
107
+ /** Standing queue at which capture stops: the base plus two frame intervals, or 250 ms before any acknowledgement. This mirrors the TCP-statistics rule of twice the round trip plus two frame intervals. */
108
+ uint64_t pauseQueueUs() const {
109
+ return depth.empty() ? pause_age_us : baseDepthUs() + 2 * frameIntervalUs();
110
+ }
111
+ /** Oldest-frame age at which capture pauses, as /metrics reports it. With TCP statistics it also gates admission; without, it is the base depth plus the pause queue. */
112
+ uint64_t admissionAgeUs() const {
113
+ return transport_available ? transportAgeLimitUs() : baseDepthUs() + pauseQueueUs();
114
+ }
115
+ /** Longest wait a viewer reported for a frame in the last 10 s, up to the 100 ms a viewer may hold one. A longer wait is a viewer falling behind and must not widen the limits. */
116
+ uint64_t viewerWaitUs() const { return std::min(viewer_hold_limit_us, held.overall(0)); }
117
+ /** Oldest-frame age at which capture stops with TCP statistics: twice the minimum round trip plus two frame intervals plus the viewer's wait, capped at 3 s, or 250 ms before a round trip is known. */
118
+ uint64_t transportAgeLimitUs() const {
119
+ if (!transport.min_rtt_us) return pause_age_us;
120
+ return std::min(timeout_us, 2 * std::min(transport.min_rtt_us, timeout_us) + 2 * frameIntervalUs() + viewerWaitUs());
121
+ }
122
+ /** Queueing delay present for the whole recent window: the smallest oldest-frame age or acknowledgement time in it, above the given base. */
123
+ uint64_t standingAbove(uint64_t now, const WindowExtreme& acknowledged_depths, uint64_t base) const {
124
+ if (frames.empty()) return 0;
125
+ const uint64_t window = standingWindowUs();
126
+ const uint64_t lowest = std::min({oldestAge(now), acknowledged_depths.over(now, window, ~0ULL),
127
+ waiting.over(now, window, ~0ULL, acknowledged_depths.forgotten_us)});
128
+ return lowest > base ? lowest - base : 0;
129
+ }
130
+ /** The total standing queue, viewer wait included. */
131
+ uint64_t standingQueueUs(uint64_t now) const { return standingAbove(now, depth, baseDepthUs()); }
132
+ /** The part of the standing queue that is in the network, with the time frames wait inside the viewer taken out. */
133
+ uint64_t standingNetworkQueueUs(uint64_t now) const { return standingAbove(now, network, networkBaseDepthUs()); }
134
+ /** Classifies a standing queue against the pause limit: clear below a quarter of it, building below half, congested below the limit, paused at it. */
135
+ Pressure levelOf(uint64_t queue) const {
136
+ const uint64_t limit = pauseQueueUs();
137
+ if (queue >= limit) return Pressure::paused;
138
+ if (queue >= limit / 2) return Pressure::congested;
139
+ return queue >= limit / 4 ? Pressure::building : Pressure::clear;
140
+ }
141
+ /** Bitrate decisions without TCP statistics: the standing network queue against the same limit as the pause. */
142
+ Pressure networkPressure(uint64_t now) const { return levelOf(standingNetworkQueueUs(now)); }
143
+ unsigned frameLimit() const {
144
+ const uint64_t count = (timeout_us * frame_rate + 999999) / 1000000;
145
+ return unsigned(std::clamp<uint64_t>(count, 2, max_frames));
146
+ }
147
+ uint64_t notsentLowat() const {
148
+ const uint64_t denominator = uint64_t(frame_rate) * 4;
149
+ return std::max<uint64_t>(16 * 1024, (target_bitrate + denominator - 1) / denominator);
150
+ }
151
+ uint64_t senderQueueUs() const {
152
+ double queued = double(transport.notsent_bytes) + double(transport.application_queued_bytes);
153
+ for (const auto& frame : frames) if (!frame.sent) queued += double(frame.bytes);
154
+ const uint64_t rate = delivery_rate_bps ? delivery_rate_bps : std::max<uint64_t>(1, target_bitrate);
155
+ return uint64_t(std::ceil(queued * 8000000.0 / double(rate)));
156
+ }
157
+ static uint64_t headroomBitrate(uint64_t target, uint64_t maximum, uint64_t elapsed_us,
158
+ uint64_t queued_us, uint64_t interval_us, uint64_t oldest_us, uint64_t age_limit_us) {
159
+ if (queued_us >= (interval_us + 1) / 2 || oldest_us >= age_limit_us) return target;
160
+ return uint64_t(std::round(std::min(double(maximum), double(target) *
161
+ std::pow(1.25, double(elapsed_us) / 1000000.0))));
162
+ }
163
+ uint64_t frameBitrate() const {
164
+ if (!transport_available) return target_bitrate;
165
+ const uint64_t interval = frameIntervalUs();
166
+ const uint64_t queued = std::clamp(senderQueueUs(), interval, 3 * interval);
167
+ return uint64_t(double(target_bitrate) *
168
+ (1.0 - 0.75 * double(queued - interval) / double(2 * interval)));
169
+ }
170
+ bool pausedWithTransport(uint64_t now) const {
171
+ return oldestAge(now) >= transportAgeLimitUs() || frames.size() >= window || senderQueueUs() >= 3 * frameIntervalUs();
172
+ }
173
+ bool pausedWithoutTransport(uint64_t now) const {
174
+ return levelOf(standingQueueUs(now)) == Pressure::paused;
175
+ }
176
+ bool paused(uint64_t now) const {
177
+ return bytes + largest_frame > max_bytes || (transport_available ? pausedWithTransport(now) : pausedWithoutTransport(now));
178
+ }
179
+ bool timedOut(uint64_t now) const {
180
+ return !frames.empty() && (now - std::min(now, last_progress_us) >= timeout_us ||
181
+ oldestAge(now) >= timeout_us);
182
+ }
183
+ bool enqueue(uint32_t seq, size_t size, uint64_t captured, uint64_t now) {
184
+ if (!seq || seq != last_encoded + 1 || frames.size() >= frameLimit() ||
185
+ !size || size > max_bytes || size > max_bytes - bytes) return false;
186
+ if (frames.empty()) last_progress_us = now;
187
+ frames.push_back({seq, size, captured}); bytes += size; largest_frame = std::max(largest_frame, size); last_encoded = seq; ++encoded;
188
+ return true;
189
+ }
190
+ bool sent(uint32_t seq, uint64_t now) {
191
+ for (auto& frame : frames) {
192
+ if (frame.sent) continue;
193
+ if (frame.sequence != seq) return false;
194
+ frame.sent = true; frame.sent_us = now; return true;
195
+ }
196
+ return false;
197
+ }
198
+ /** Credits the oldest frame. The viewer's wait is untrusted: one longer than the frame's whole capture-to-acknowledgement time counts as zero. */
199
+ bool ack(uint32_t seq, uint64_t now, uint64_t viewer_wait_us = 0) {
200
+ if (frames.empty() || !frames.front().sent || frames.front().sequence != seq) return false;
201
+ const auto frame = frames.front();
202
+ const double rtt = double(now - std::min(now, frame.sent_us));
203
+ ack_rtt_us = acknowledged ? ack_rtt_us * 0.875 + rtt * 0.125 : rtt;
204
+ recordDepth(frame.captured_us, now, viewer_wait_us);
205
+ // Sample continuous demand only: idle gaps and tiny updates are not capacity evidence.
206
+ if (!transport_available) {
207
+ if (previous_sent_us && frame.sent_us - previous_sent_us > 3 * frameIntervalUs()) {
208
+ sample_start_us = sample_bytes = sample_acks = 0;
209
+ }
210
+ if (!sample_start_us) sample_start_us = frame.sent_us;
211
+ previous_sent_us = frame.sent_us;
212
+ sample_bytes += frame.bytes; ++sample_acks;
213
+ }
214
+ bytes -= frame.bytes; frames.pop_front(); ++acknowledged; last_progress_us = now;
215
+ update(now);
216
+ return true;
217
+ }
218
+ void recordDepth(uint64_t captured, uint64_t now, uint64_t viewer_wait_us) {
219
+ const uint64_t total = now - std::min(now, captured);
220
+ const uint64_t wait = viewer_wait_us <= total ? viewer_wait_us : 0;
221
+ const uint64_t kept_before = transport_available ? 2 * frameIntervalUs() : standingWindowUs();
222
+ network.forget(now, kept_before);
223
+ depth.push(now, total, base_window_us);
224
+ peak.push(now, total, base_window_us);
225
+ held.push(now, wait, base_window_us);
226
+ network.push(now, total - wait, base_window_us);
227
+ depth.forget(now, transport_available ? 2 * frameIntervalUs() : standingWindowUs());
228
+ }
229
+ void expireDelivery(uint64_t now) {
230
+ while (!delivery_samples.empty() &&
231
+ now - std::min(now, delivery_samples.front().time_us) >= delivery_window_us) {
232
+ delivery_samples.pop_front();
233
+ }
234
+ delivery_rate_bps = delivery_samples.empty() ? 0 : delivery_samples.front().rate_bps;
235
+ }
236
+ void sampleTransport(const TransportSample& sample, uint64_t now) {
237
+ const bool progress = !transport_available || sample.bytes_acked > transport.bytes_acked;
238
+ if (!transport_available) last_transport_update_us = now;
239
+ transport = sample; transport_available = true;
240
+ expireDelivery(now);
241
+ if (progress && !sample.app_limited && sample.delivery_rate_bps) {
242
+ if (delivery_samples.empty()) last_transport_update_us = now;
243
+ while (!delivery_samples.empty() && delivery_samples.back().rate_bps <= sample.delivery_rate_bps) {
244
+ delivery_samples.pop_back();
245
+ }
246
+ delivery_samples.push_back({now, sample.delivery_rate_bps});
247
+ }
248
+ update(now);
249
+ }
250
+ void clearTransport() {
251
+ if (!transport_available) return;
252
+ transport = {}; transport_available = false; delivery_rate_bps = last_transport_update_us = 0;
253
+ delivery_samples.clear();
254
+ sample_start_us = sample_bytes = sample_acks = previous_sent_us = 0;
255
+ goodput_bps = 0;
256
+ updateWindow();
257
+ }
258
+ void windowWithTransport() {
259
+ const uint64_t count = transport.min_rtt_us ?
260
+ ((2 * std::min(transport.min_rtt_us, timeout_us) + viewerWaitUs()) * frame_rate + 999999) / 1000000 + 2 :
261
+ (pause_age_us * frame_rate + 999999) / 1000000;
262
+ window = unsigned(std::clamp<uint64_t>(count, 2, frameLimit()));
263
+ }
264
+ void windowWithoutTransport() {
265
+ window = acknowledged ? unsigned(std::clamp(
266
+ std::ceil(double(peakDepthUs()) * frame_rate / 1000000.0) + 2.0, 2.0, double(frameLimit()))) : 1;
267
+ }
268
+ void updateWindow() { transport_available ? windowWithTransport() : windowWithoutTransport(); }
269
+ void bitrateWithTransport(uint64_t now) {
270
+ expireDelivery(now);
271
+ const uint64_t elapsed = now - std::min(now, last_transport_update_us);
272
+ last_transport_update_us = now;
273
+ const uint64_t probe = transport.app_limited || !delivery_rate_bps ?
274
+ headroomBitrate(target_bitrate, max_bitrate, elapsed, senderQueueUs(), frameIntervalUs(),
275
+ oldestAge(now), transportAgeLimitUs()) : 0;
276
+ if (delivery_rate_bps) {
277
+ const double desired = std::clamp(double(delivery_rate_bps) * 0.85,
278
+ double(min_bitrate), double(max_bitrate));
279
+ if (!transport.app_limited || desired > double(target_bitrate)) {
280
+ const double weight = 1.0 - std::exp(-double(elapsed) / double(bitrate_smoothing_us));
281
+ target_bitrate = std::clamp<uint64_t>(uint64_t(std::round(double(target_bitrate) +
282
+ (desired - double(target_bitrate)) * weight)), min_bitrate, max_bitrate);
283
+ }
284
+ }
285
+ target_bitrate = std::max(target_bitrate, probe);
286
+ }
287
+ void bitrateWithoutTransport(uint64_t now) {
288
+ if (!frames.empty()) {
289
+ waiting.push(now, oldestAge(now), base_window_us);
290
+ depth.forget(now, standingWindowUs());
291
+ network.forget(now, standingWindowUs());
292
+ }
293
+ const bool busy = sample_start_us && sample_acks >= 8 && sample_bytes >= 65536;
294
+ const uint64_t interval = sample_start_us ? now - std::min(now, sample_start_us) : 0;
295
+ if (busy && interval >= 500000) goodput_bps = double(sample_bytes) * 8000000.0 / interval;
296
+ const Pressure state = networkPressure(now);
297
+ if (frames.size() >= 2 && bytes >= std::min<uint64_t>(target_bitrate / 80, 65536) &&
298
+ state >= Pressure::congested &&
299
+ now - last_decrease_us >= 500000) {
300
+ uint64_t next = target_bitrate * 3 / 4;
301
+ if (busy && interval >= 500000) next = std::min(next, uint64_t(goodput_bps * 0.9));
302
+ target_bitrate = std::max<uint64_t>(min_bitrate, next); last_decrease_us = now;
303
+ last_increase_us = now;
304
+ } else if (busy && interval >= 1000000 && state == Pressure::clear &&
305
+ now - last_increase_us >= 1000000 && now - last_decrease_us >= 2000000) {
306
+ // CRF can emit far below its cap; prompt ACKs of sustained demand permit a probe.
307
+ target_bitrate = std::min<uint64_t>(max_bitrate, target_bitrate + target_bitrate / 10);
308
+ last_increase_us = now;
309
+ }
310
+ if (interval >= 1000000) { sample_start_us = now; sample_bytes = sample_acks = 0; }
311
+ }
312
+ void update(uint64_t now) {
313
+ updateWindow();
314
+ transport_available ? bitrateWithTransport(now) : bitrateWithoutTransport(now);
315
+ }
316
+ };
317
+ } // namespace relay
@@ -0,0 +1,98 @@
1
+ #include "input.hpp"
2
+
3
+ namespace relay {
4
+ namespace {
5
+ constexpr int pixelCoordinate(uint16_t normalized, uint16_t extent) {
6
+ return int((uint64_t(normalized) * (extent - 1) + 32767) / 65535);
7
+ }
8
+ static_assert(pixelCoordinate(0, 1280) == 0 && pixelCoordinate(65535, 1280) == 1279 &&
9
+ pixelCoordinate(10248, 1280) == 200 && pixelCoordinate(40992, 1280) == 800 &&
10
+ pixelCoordinate(65535, 720) == 719 && pixelCoordinate(36459, 720) == 400);
11
+ bool counted(InputJob::Kind kind) {
12
+ return kind == InputJob::Kind::key || kind == InputJob::Kind::text || kind == InputJob::Kind::button ||
13
+ kind == InputJob::Kind::motion || kind == InputJob::Kind::scroll;
14
+ }
15
+ bool droppable(InputJob::Kind kind) {
16
+ return kind == InputJob::Kind::text || kind == InputJob::Kind::motion || kind == InputJob::Kind::scroll;
17
+ }
18
+ }
19
+ void InputQueue::post(InputJob job) {
20
+ Entry entry{std::move(job)};
21
+ if (entry.job.kind == InputJob::Kind::flush) {
22
+ if (open) { entry.unit = open; open = 0; }
23
+ } else if (counted(entry.job.kind)) {
24
+ if (!open) { open = ++next; ++records; }
25
+ entry.unit = open;
26
+ }
27
+ queue.push_back(std::move(entry));
28
+ }
29
+ bool InputQueue::empty() const { return queue.empty(); }
30
+ InputJob InputQueue::take() {
31
+ InputJob job = std::move(queue.front().job);
32
+ const uint64_t unit = queue.front().unit;
33
+ queue.pop_front();
34
+ if (job.kind == InputJob::Kind::flush && unit) --records;
35
+ return job;
36
+ }
37
+ size_t InputQueue::pending() const { return records; }
38
+ void InputQueue::cancel() {
39
+ size_t dropped = 0;
40
+ uint64_t seen = 0;
41
+ for (auto entry = queue.begin(); entry != queue.end();) {
42
+ if (entry->job.kind == InputJob::Kind::fence) {
43
+ entry->job.dropped += dropped;
44
+ dropped = 0; seen = 0;
45
+ ++entry;
46
+ continue;
47
+ }
48
+ if (!droppable(entry->job.kind)) { ++entry; continue; }
49
+ if (entry->unit != seen) { seen = entry->unit; ++dropped; }
50
+ entry = queue.erase(entry);
51
+ }
52
+ }
53
+ InputController::InputController(std::unique_ptr<InputBackend> source) : backend(std::move(source)) {}
54
+ InputController::~InputController() { releaseAll(); }
55
+ std::pair<uint16_t, uint16_t> InputController::dimensions() const { return backend->dimensions(); }
56
+ const char* InputController::name() const { return backend->name(); }
57
+ void InputController::releaseAll() {
58
+ for (const auto key : keys) backend->releaseKey(key);
59
+ for (const auto button : buttons) backend->releaseButton(button);
60
+ keys.clear(); buttons.clear(); backend->flush();
61
+ }
62
+ void InputController::resetLocks() { backend->resetLocks(); backend->flush(); }
63
+ void InputController::fence(std::shared_ptr<InputFence> mark) { backend->fence(std::move(mark)); }
64
+ size_t InputController::pending() const { return backend->pending(); }
65
+ void InputController::cancel() { backend->cancel(); }
66
+ InputStatus InputController::apply(const InputEvent& input) {
67
+ const unsigned event = input.type, code = input.code;
68
+ const int64_t value = input.value;
69
+ if (input.reserved || (input.buttons & ~31u) || (input.modifiers & ~255u)) return InputStatus::invalid;
70
+ if (event == 7) {
71
+ if (code || value || input.modifiers || input.buttons) return InputStatus::invalid;
72
+ releaseAll(); return InputStatus::ok;
73
+ }
74
+ if (event == 8) {
75
+ if (code || input.modifiers || input.buttons || value < 1 || value > 0x10FFFF ||
76
+ (value >= 0xD800 && value <= 0xDFFF)) return InputStatus::invalid;
77
+ if (!backend->typeText(uint32_t(value))) return InputStatus::unsupported;
78
+ backend->flush(); return InputStatus::ok;
79
+ }
80
+ if (event < 1 || event > 6 || (event != 6 && value)) return InputStatus::invalid;
81
+ if (event <= 2) {
82
+ if (code > 231) return InputStatus::invalid;
83
+ const auto key = backend->nativeKey(uint16_t(code));
84
+ if (!key) return InputStatus::unsupported;
85
+ if (event == 1) { if (keys.insert(*key).second) backend->pressKey(*key); }
86
+ else if (keys.erase(*key)) backend->releaseKey(*key);
87
+ } else {
88
+ if ((event == 3 && code) || ((event == 4 || event == 5) && (code < 1 || code > 3)) ||
89
+ (event == 6 && (code > 1 || value < -1200 || value > 1200 || value % 120))) return InputStatus::invalid;
90
+ const auto size = backend->dimensions();
91
+ backend->move(pixelCoordinate(input.x, size.first), pixelCoordinate(input.y, size.second));
92
+ if (event == 4) { if (buttons.insert(code).second) backend->pressButton(code); }
93
+ if (event == 5 && buttons.erase(code)) backend->releaseButton(code);
94
+ if (event == 6) backend->scroll(code, int(value / 120));
95
+ }
96
+ backend->flush(); return InputStatus::ok;
97
+ }
98
+ }
@@ -0,0 +1,115 @@
1
+ #pragma once
2
+ #include <cstddef>
3
+ #include <cstdint>
4
+ #include <deque>
5
+ #include <memory>
6
+ #include <optional>
7
+ #include <set>
8
+ #include <utility>
9
+
10
+ namespace relay {
11
+ enum class InputStatus : uint8_t { ok = 0, invalid = 1, unsupported = 2 };
12
+ struct InputEvent {
13
+ uint8_t type = 0, reserved = 0;
14
+ uint16_t code = 0;
15
+ int32_t value = 0;
16
+ uint16_t x = 0, y = 0, buttons = 0, modifiers = 0;
17
+ };
18
+ /// Platform key identifier returned by a backend, tracked by InputController.
19
+ using NativeKey = uint32_t;
20
+ /// Completion notice for posted input, called by the backend on its own thread.
21
+ class InputFence {
22
+ public:
23
+ virtual ~InputFence() = default;
24
+ /// Runs once after every action posted before the fence, reporting whether all of them applied and how many records were dropped.
25
+ virtual void complete(bool applied, size_t dropped) = 0;
26
+ };
27
+ /// One queued backend action; the actions posted for a single protocol record form one queue unit.
28
+ struct InputJob {
29
+ enum class Kind : uint8_t { key, text, button, motion, scroll, locks, flush, fence } kind = Kind::flush;
30
+ uint32_t code = 0, codepoint = 0;
31
+ unsigned number = 0;
32
+ int x = 0, y = 0, notches = 0;
33
+ bool press = false;
34
+ size_t dropped = 0;
35
+ std::shared_ptr<InputFence> fence;
36
+ InputJob() = default;
37
+ explicit InputJob(Kind value) : kind(value) {}
38
+ /// Completes the fence after the work before it reached the platform, reporting the records dropped from before it.
39
+ void complete() const { if (fence) fence->complete(dropped == 0, dropped); }
40
+ };
41
+ /// Backend work queue that counts one protocol record as one unit however many actions it posts; not thread safe.
42
+ class InputQueue {
43
+ public:
44
+ void post(InputJob job);
45
+ bool empty() const;
46
+ /// Removes the next action, closing the record unit whose trailing flush it applies.
47
+ InputJob take();
48
+ /// Records queued, one per protocol record.
49
+ size_t pending() const;
50
+ /// Drops queued text and moves as not applied, leaving the fences that covered them for the worker to complete.
51
+ void cancel();
52
+
53
+ private:
54
+ struct Entry {
55
+ InputJob job;
56
+ uint64_t unit = 0;
57
+ };
58
+ std::deque<Entry> queue;
59
+ uint64_t next = 0, open = 0;
60
+ size_t records = 0;
61
+ };
62
+ /// Platform primitives only; protocol rules, coordinate mapping and held input live in InputController.
63
+ class InputBackend {
64
+ public:
65
+ virtual ~InputBackend() = default;
66
+ /// Native key for a HID usage, or nothing when the backend cannot produce one.
67
+ virtual std::optional<NativeKey> nativeKey(uint16_t usage) const = 0;
68
+ virtual void pressKey(NativeKey key) = 0;
69
+ virtual void releaseKey(NativeKey key) = 0;
70
+ /// Types one Unicode scalar value without changing held keys; false when the platform cannot produce it.
71
+ virtual bool typeText(uint32_t codepoint) = 0;
72
+ virtual void pressButton(unsigned button) = 0;
73
+ virtual void releaseButton(unsigned button) = 0;
74
+ virtual void move(int x, int y) = 0;
75
+ /// Scrolls notches on axis 0 (vertical) or 1 (horizontal); positive means down or right.
76
+ virtual void scroll(unsigned axis, int notches) = 0;
77
+ /// Turns lock keys off, currently Caps Lock.
78
+ virtual void resetLocks() = 0;
79
+ /// Completes mark after every event posted so far, without waiting for it.
80
+ virtual void fence(std::shared_ptr<InputFence> mark) = 0;
81
+ /// Records posted but not yet applied, so a caller can refuse a batch before it queues behind them.
82
+ virtual size_t pending() const = 0;
83
+ /// Drops queued text and moves as not applied, keeping key and button releases and counting each dropped record once.
84
+ virtual void cancel() = 0;
85
+ virtual void flush() = 0;
86
+ virtual std::pair<uint16_t, uint16_t> dimensions() const = 0;
87
+ virtual const char* name() const = 0;
88
+ };
89
+ /// Records allowed to wait in a backend queue; a batch that would exceed it is refused before any of it is queued.
90
+ constexpr size_t max_queued_events = 1024;
91
+ /** Validates protocol events, maps pointer coordinates and dedupes held input for one backend. */
92
+ class InputController {
93
+ public:
94
+ explicit InputController(std::unique_ptr<InputBackend> source);
95
+ ~InputController();
96
+ InputController(const InputController&) = delete;
97
+ InputController& operator=(const InputController&) = delete;
98
+ InputStatus apply(const InputEvent& event);
99
+ void releaseAll();
100
+ void resetLocks();
101
+ /// Completes mark once every event applied so far has reached the backend.
102
+ void fence(std::shared_ptr<InputFence> mark);
103
+ /// Records posted but not yet applied.
104
+ size_t pending() const;
105
+ /// Drops queued text and moves, so their fences complete as not applied.
106
+ void cancel();
107
+ std::pair<uint16_t, uint16_t> dimensions() const;
108
+ const char* name() const;
109
+
110
+ private:
111
+ std::unique_ptr<InputBackend> backend;
112
+ std::set<NativeKey> keys;
113
+ std::set<unsigned> buttons;
114
+ };
115
+ } // namespace relay
@@ -0,0 +1,125 @@
1
+ #include "wait.hpp"
2
+ #include <cerrno>
3
+ #include <ctime>
4
+ #include <stdexcept>
5
+
6
+ #ifdef _WIN32
7
+ #include <windows.h>
8
+ #elif defined(__linux__)
9
+ #include <poll.h>
10
+ #include <sys/eventfd.h>
11
+ #include <unistd.h>
12
+ #else
13
+ #include <climits>
14
+ #include <fcntl.h>
15
+ #include <poll.h>
16
+ #include <unistd.h>
17
+ #endif
18
+
19
+ namespace relay {
20
+ #ifdef _WIN32
21
+ Wake::Wake() : handle_(CreateEventW(nullptr, TRUE, FALSE, nullptr)) {}
22
+ Wake::~Wake() { if (handle_) CloseHandle(handle_); }
23
+ bool Wake::valid() const { return handle_ != nullptr; }
24
+ void Wake::signal() { if (handle_) SetEvent(handle_); }
25
+ bool Wake::consume() {
26
+ if (!handle_ || WaitForSingleObject(handle_, 0) != WAIT_OBJECT_0) return false;
27
+ ResetEvent(handle_);
28
+ return true;
29
+ }
30
+ WaitReady wait(WaitHandle source, Wake& wake, int64_t timeout_ns) {
31
+ thread_local HANDLE timer = CreateWaitableTimerExW(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
32
+ TIMER_ALL_ACCESS);
33
+ HANDLE handles[3] = {source, wake.handle(), timer};
34
+ DWORD count = 2, limit = INFINITE;
35
+ if (timeout_ns >= 0) {
36
+ if (timer) {
37
+ LARGE_INTEGER due;
38
+ due.QuadPart = -(timeout_ns / 100);
39
+ if (!SetWaitableTimer(timer, &due, 0, nullptr, nullptr, FALSE)) throw std::runtime_error("capture timer failed");
40
+ count = 3;
41
+ } else {
42
+ limit = DWORD((timeout_ns + 999999) / 1000000);
43
+ }
44
+ }
45
+ const DWORD result = WaitForMultipleObjects(count, handles, FALSE, limit);
46
+ if (result == WAIT_FAILED) throw std::runtime_error("capture wait failed");
47
+ WaitReady ready;
48
+ ready.source = WaitForSingleObject(source, 0) == WAIT_OBJECT_0;
49
+ ready.wake = result == WAIT_OBJECT_0 + 1;
50
+ return ready;
51
+ }
52
+ #elif defined(__linux__)
53
+ Wake::Wake() : handle_(eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK)) {}
54
+ Wake::~Wake() { if (handle_ >= 0) close(handle_); }
55
+ bool Wake::valid() const { return handle_ >= 0; }
56
+ void Wake::signal() {
57
+ const uint64_t one = 1;
58
+ if (handle_ >= 0) { const auto result = write(handle_, &one, sizeof(one)); (void)result; }
59
+ }
60
+ bool Wake::consume() {
61
+ uint64_t value;
62
+ return handle_ >= 0 && read(handle_, &value, sizeof(value)) == sizeof(value);
63
+ }
64
+ WaitReady wait(WaitHandle source, Wake& wake, int64_t timeout_ns) {
65
+ timespec timeout{};
66
+ const timespec* timeout_ptr = nullptr;
67
+ if (timeout_ns >= 0) {
68
+ timeout = {static_cast<time_t>(timeout_ns / 1000000000), static_cast<long>(timeout_ns % 1000000000)};
69
+ timeout_ptr = &timeout;
70
+ }
71
+ pollfd descriptors[2] = {{source, POLLIN, 0}, {wake.handle(), POLLIN, 0}};
72
+ if (ppoll(descriptors, 2, timeout_ptr, nullptr) < 0) {
73
+ if (errno == EINTR) return {};
74
+ throw std::runtime_error("capture ppoll failed");
75
+ }
76
+ if (descriptors[0].revents & (POLLERR | POLLHUP | POLLNVAL)) throw std::runtime_error("capture source connection closed");
77
+ return {bool(descriptors[0].revents & POLLIN), bool(descriptors[1].revents & POLLIN)};
78
+ }
79
+ #else
80
+ Wake::Wake() : handle_(-1) {
81
+ int descriptors[2] = {-1, -1};
82
+ if (pipe(descriptors) != 0) return;
83
+ for (const int fd : descriptors) {
84
+ fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
85
+ fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
86
+ }
87
+ handle_ = descriptors[0];
88
+ write_handle_ = descriptors[1];
89
+ }
90
+ Wake::~Wake() {
91
+ if (handle_ >= 0) close(handle_);
92
+ if (write_handle_ >= 0) close(write_handle_);
93
+ }
94
+ bool Wake::valid() const { return handle_ >= 0 && write_handle_ >= 0; }
95
+ void Wake::signal() {
96
+ const char one = 1;
97
+ if (write_handle_ >= 0) { const auto result = write(write_handle_, &one, sizeof(one)); (void)result; }
98
+ }
99
+ bool Wake::consume() {
100
+ if (handle_ < 0) return false;
101
+ char buffer[64];
102
+ bool any = false;
103
+ for (;;) {
104
+ const auto count = read(handle_, buffer, sizeof(buffer));
105
+ if (count > 0) { any = true; continue; }
106
+ if (count < 0 && errno == EINTR) continue;
107
+ return any;
108
+ }
109
+ }
110
+ WaitReady wait(WaitHandle source, Wake& wake, int64_t timeout_ns) {
111
+ int timeout_ms = -1;
112
+ if (timeout_ns >= 0) {
113
+ const int64_t milliseconds = timeout_ns / 1000000 + (timeout_ns % 1000000 != 0 ? 1 : 0);
114
+ timeout_ms = milliseconds > INT_MAX ? INT_MAX : static_cast<int>(milliseconds);
115
+ }
116
+ pollfd descriptors[2] = {{source, POLLIN, 0}, {wake.handle(), POLLIN, 0}};
117
+ if (poll(descriptors, 2, timeout_ms) < 0) {
118
+ if (errno == EINTR) return {};
119
+ throw std::runtime_error("capture poll failed");
120
+ }
121
+ if (descriptors[0].revents & (POLLERR | POLLHUP | POLLNVAL)) throw std::runtime_error("capture source connection closed");
122
+ return {bool(descriptors[0].revents & POLLIN), bool(descriptors[1].revents & POLLIN)};
123
+ }
124
+ #endif
125
+ }