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,31 @@
1
+ #pragma once
2
+
3
+ #include <condition_variable>
4
+ #include <cstdint>
5
+ #include <mutex>
6
+ #include <string>
7
+ #include <thread>
8
+ #include <vector>
9
+
10
+ struct lws_context;
11
+
12
+ namespace relay {
13
+ // One job/result globally. The worker owns its capture source and PNG codec.
14
+ class Screenshot {
15
+ public:
16
+ ~Screenshot() { stop(); }
17
+ void start(lws_context* context);
18
+ bool request();
19
+ bool take(std::vector<uint8_t>& bytes, std::string& error);
20
+ void finish();
21
+ void stop();
22
+ private:
23
+ std::mutex mutex_;
24
+ std::condition_variable ready_;
25
+ std::thread worker_;
26
+ bool busy_ = false, requested_ = false, complete_ = false, stopping_ = false;
27
+ std::vector<uint8_t> bytes_;
28
+ std::string error_;
29
+ void run(lws_context* context);
30
+ };
31
+ } // namespace relay
@@ -0,0 +1,535 @@
1
+ #include "server.hpp"
2
+ #include "backend.hpp"
3
+ #include "core/capture-source.hpp"
4
+ #include "metrics.hpp"
5
+ #include <algorithm>
6
+ #include <chrono>
7
+ #include <cstddef>
8
+ #include <cstring>
9
+ #include <iostream>
10
+ #include <limits>
11
+ #ifdef __linux__
12
+ #include <sys/socket.h>
13
+ #include <netinet/in.h>
14
+ #include <linux/tcp.h>
15
+ #endif
16
+
17
+ namespace relay {
18
+ namespace {
19
+ using Clock = std::chrono::steady_clock;
20
+ uint64_t nowUs() {
21
+ return std::chrono::duration_cast<std::chrono::microseconds>(Clock::now().time_since_epoch()).count();
22
+ }
23
+ /// Wakes the service thread once one deferred reply can be written.
24
+ class ReplyFence final : public InputFence {
25
+ public:
26
+ ReplyFence(std::shared_ptr<Server::InputWake> wake, std::function<void(bool, size_t)> mark)
27
+ : wake(std::move(wake)), mark(std::move(mark)) {}
28
+ void complete(bool applied, size_t dropped) override {
29
+ mark(applied, dropped);
30
+ wake->cancel();
31
+ }
32
+ private:
33
+ std::shared_ptr<Server::InputWake> wake;
34
+ std::function<void(bool, size_t)> mark;
35
+ };
36
+ #ifdef __linux__
37
+ void configureStreamSocket(lws* wsi) {
38
+ const int fd = lws_get_socket_fd(wsi);
39
+ if (fd < 0) return;
40
+ const int enabled = 1;
41
+ (void)setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &enabled, sizeof(enabled));
42
+ }
43
+ std::optional<FlowControl::TransportSample> streamTransportSample(lws* wsi, size_t buffered_bytes) {
44
+ const int fd = lws_get_socket_fd(wsi);
45
+ if (fd < 0) return std::nullopt;
46
+ tcp_info info{};
47
+ socklen_t length = sizeof(info);
48
+ if (getsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, &length) ||
49
+ length < offsetof(tcp_info, tcpi_delivery_rate) + sizeof(info.tcpi_delivery_rate) ||
50
+ !info.tcpi_min_rtt || info.tcpi_min_rtt == std::numeric_limits<uint32_t>::max()) return std::nullopt;
51
+ FlowControl::TransportSample sample;
52
+ sample.min_rtt_us = info.tcpi_min_rtt;
53
+ sample.rtt_us = info.tcpi_rtt;
54
+ sample.delivery_rate_bps = std::min<uint64_t>(info.tcpi_delivery_rate, std::numeric_limits<uint64_t>::max() / 8) * 8;
55
+ sample.notsent_bytes = info.tcpi_notsent_bytes;
56
+ sample.snd_cwnd = info.tcpi_snd_cwnd;
57
+ sample.total_retrans = info.tcpi_total_retrans;
58
+ sample.app_limited = info.tcpi_delivery_rate_app_limited;
59
+ sample.bytes_acked = info.tcpi_bytes_acked;
60
+ sample.application_queued_bytes = buffered_bytes;
61
+ return sample;
62
+ }
63
+ #endif
64
+ } // namespace
65
+ Server::Server(Config settings) : config(std::move(settings)), capture([this](EncodedFrame&& frame) {
66
+ std::lock_guard<std::mutex> lock(mutex);
67
+ if (!context || !generation || frame.generation != generation) return false;
68
+ if (!flow.enqueue(frame.sequence, frame.annex_b.size(), frame.pts_us, nowUs())) {
69
+ failed = true; lws_cancel_service(context); return false;
70
+ }
71
+ pending.push_back(std::move(frame));
72
+ lws_cancel_service(context);
73
+ return true;
74
+ }, CaptureSettings{config.fps, config.bitrate_min, config.bitrate_start, config.bitrate_max}, backendFor(config.backend).capture,
75
+ [this](uint32_t gen, CursorImage&& cursor) {
76
+ std::lock_guard<std::mutex> lock(mutex);
77
+ if (!context || !generation || gen != generation) return;
78
+ pending_cursor = std::move(cursor);
79
+ lws_cancel_service(context);
80
+ }) { flow.configure(config.bitrate_min, config.bitrate_start, config.bitrate_max, config.fps); }
81
+ void Server::release() { if (input) input->releaseAll(); }
82
+ void Server::end() {
83
+ if (holdsInput()) { release(); input->cancel(); }
84
+ uint32_t old;
85
+ { std::lock_guard<std::mutex> lock(mutex); old = generation; generation = 0; pending.clear(); pending_cursor.reset(); flow.reset(); failed = false; }
86
+ last_write_cursor = false;
87
+ buffered_stream_bytes = 0; stream_notsent_lowat = 0; flow_timer_due_us = 0;
88
+ if (old) capture.endSession(old);
89
+ lastSequence = 0; received.clear(); replies.clear(); control_ready = controller = viewOnly = false; viewer = nullptr;
90
+ }
91
+ bool Server::message() {
92
+ const auto& b = received;
93
+ if (b.size() < 20 || std::memcmp(b.data(), "NSL1", 4) || b[4] != 1 || u16(b.data()+6) || u32(b.data()+16) != b.size()-20) return false;
94
+ const auto gen = u32(b.data()+8), seq = u32(b.data()+12);
95
+ if (!gen) return false;
96
+ if (controller) {
97
+ if (b[5] != 3 || b.size() != 36 || !seq || gen != generation || replies.size() >= 32) return false;
98
+ auto reply = std::make_shared<ControlReply>();
99
+ reply->seq = seq; reply->status = input->apply(inputEvent(b.data()+20));
100
+ replies.push_back(reply);
101
+ input->fence(std::make_shared<ReplyFence>(wake, [reply](bool, size_t) { reply->ready.store(true, std::memory_order_release); }));
102
+ lws_callback_on_writable(viewer); return true;
103
+ }
104
+ if (b[5] == 2) {
105
+ if (b.size() != 20 && b.size() != 24) return false;
106
+ const uint64_t wait = FlowControl::ackWaitUs(b.data()+20, b.size()-20);
107
+ bool accepted = false;
108
+ { std::lock_guard<std::mutex> lock(mutex);
109
+ if (gen == generation && seq) accepted = flow.ack(seq, nowUs(), wait);
110
+ }
111
+ if (accepted) { capture.acknowledge(gen, seq); updateFlow(); }
112
+ return true;
113
+ }
114
+ if (b[5] != 3 || b.size() != 36 || seq || gen != generation) return false;
115
+ if (viewOnly) return true;
116
+ return input->apply(inputEvent(b.data()+20)) != InputStatus::invalid;
117
+ }
118
+ void Server::updateFlow() {
119
+ if (viewer && !lws_partial_buffered(viewer)) buffered_stream_bytes = 0;
120
+ #ifdef __linux__
121
+ const auto sample = viewer && !controller && !config.proxy ? streamTransportSample(viewer, buffered_stream_bytes) : std::nullopt;
122
+ uint32_t notsent_lowat;
123
+ #endif
124
+ uint32_t gen; unsigned window; uint64_t bitrate, admit_until, service_deadline; bool paused, inflight;
125
+ { std::lock_guard<std::mutex> lock(mutex);
126
+ const auto now = nowUs();
127
+ #ifdef __linux__
128
+ if (sample) flow.sampleTransport(*sample, now); else flow.clearTransport();
129
+ #endif
130
+ flow.update(now);
131
+ gen = generation; window = flow.window; bitrate = flow.frameBitrate(); paused = flow.paused(now);
132
+ inflight = !flow.frames.empty();
133
+ admit_until = inflight && flow.transport_available ? flow.frames.front().captured_us + flow.transportAgeLimitUs() : 0;
134
+ #ifdef __linux__
135
+ notsent_lowat = flow.notsentLowat();
136
+ #endif
137
+ service_deadline = flow.serviceDeadlineUs(now, flow_timer_due_us);
138
+ }
139
+ #ifdef __linux__
140
+ if (viewer && !controller && notsent_lowat != stream_notsent_lowat) {
141
+ const int fd = lws_get_socket_fd(viewer);
142
+ if (fd >= 0) (void)setsockopt(fd, IPPROTO_TCP, TCP_NOTSENT_LOWAT, &notsent_lowat, sizeof(notsent_lowat));
143
+ stream_notsent_lowat = notsent_lowat;
144
+ }
145
+ #endif
146
+ if (viewer && !controller && service_deadline != flow_timer_due_us) {
147
+ const uint64_t now = nowUs();
148
+ lws_set_timer_usecs(viewer, service_deadline - std::min(service_deadline, now));
149
+ flow_timer_due_us = service_deadline;
150
+ }
151
+ // Never hold the transport mutex while calling the capture API. Publication takes it later.
152
+ capture.setFlowControl(gen, window, bitrate, paused, admit_until);
153
+ }
154
+ bool Server::timedOut() {
155
+ std::lock_guard<std::mutex> lock(mutex);
156
+ return failed || flow.timedOut(nowUs());
157
+ }
158
+ std::string Server::metrics() {
159
+ std::lock_guard<std::mutex> lock(mutex);
160
+ return ",\"inflight_frames\":" + std::to_string(flow.frames.size()) +
161
+ ",\"inflight_bytes\":" + std::to_string(flow.bytes) +
162
+ ",\"window_frames\":" + std::to_string(flow.window) +
163
+ ",\"target_bitrate_bps\":" + std::to_string(flow.target_bitrate) +
164
+ ",\"min_rtt_ms\":" + std::to_string(flow.transport.min_rtt_us / 1000.0) +
165
+ ",\"delivery_rate_bps\":" + std::to_string(flow.delivery_rate_bps) +
166
+ ",\"notsent_bytes\":" + std::to_string(flow.transport.notsent_bytes) +
167
+ ",\"sender_queue_ms\":" + std::to_string(flow.senderQueueUs() / 1000.0) +
168
+ ",\"retrans\":" + std::to_string(flow.transport.total_retrans) +
169
+ ",\"ack_rtt_ms\":" + std::to_string(flow.ack_rtt_us / 1000.0) +
170
+ ",\"oldest_frame_ms\":" + std::to_string(flow.oldestAge(nowUs()) / 1000.0) +
171
+ ",\"frames_acked\":" + std::to_string(flow.acknowledged) +
172
+ ",\"frames_encoded\":" + std::to_string(flow.encoded) +
173
+ ",\"observed_busy_goodput_bps\":" + std::to_string(flow.goodput_bps) +
174
+ ",\"max_inflight_frames\":" + std::to_string(flow.frameLimit()) +
175
+ ",\"max_inflight_bytes\":" + std::to_string(FlowControl::max_bytes) +
176
+ ",\"max_frame_bytes\":" + std::to_string(FlowControl::max_bytes) +
177
+ ",\"admission_age_ms\":" + std::to_string(flow.admissionAgeUs() / 1000.0) +
178
+ ",\"no_ack_timeout_ms\":" + std::to_string(FlowControl::timeout_us / 1000) +
179
+ ",\"max_frame_age_ms\":" + std::to_string(FlowControl::timeout_us / 1000);
180
+ }
181
+ int Server::writeControl(lws* wsi) {
182
+ if (!control_ready && (replies.empty() || !replies.front()->ready.load(std::memory_order_acquire))) return 0;
183
+ std::vector<uint8_t> wire;
184
+ if (control_ready) controlReady(wire, LWS_PRE, generation, width, height);
185
+ else inputReply(wire, LWS_PRE, generation, replies.front()->seq, replies.front()->status);
186
+ const size_t size = wire.size() - LWS_PRE;
187
+ if (lws_write(wsi, wire.data() + LWS_PRE, size, LWS_WRITE_BINARY) != int(size)) return -1;
188
+ if (control_ready) control_ready = false; else replies.pop_front();
189
+ if (!replies.empty() && replies.front()->ready.load(std::memory_order_acquire)) lws_callback_on_writable(wsi);
190
+ return 0;
191
+ }
192
+ void Server::InputWake::set(lws_context* next) {
193
+ std::lock_guard<std::mutex> lock(mutex);
194
+ context = next;
195
+ }
196
+ void Server::InputWake::cancel() {
197
+ std::lock_guard<std::mutex> lock(mutex);
198
+ if (context) lws_cancel_service(context);
199
+ }
200
+ int Server::deferInput(lws* wsi, InputStatus status, size_t applied, size_t posted) {
201
+ auto reply = std::make_shared<InputReply>();
202
+ reply->wsi = wsi;
203
+ if (status == InputStatus::ok) {
204
+ reply->status = HTTP_STATUS_OK;
205
+ reply->body = "{\"applied\":" + std::to_string(applied) + "}";
206
+ } else {
207
+ const bool invalid = status == InputStatus::invalid;
208
+ reply->status = invalid ? HTTP_STATUS_BAD_REQUEST : 422;
209
+ reply->body = std::string("{\"error\":\"") + (invalid ? "invalid_input" : "unsupported_input") +
210
+ "\",\"index\":" + std::to_string(applied - 1) + "}";
211
+ }
212
+ { std::lock_guard<std::mutex> lock(reply_mutex); input_replies.push_back(reply); }
213
+ input->fence(std::make_shared<ReplyFence>(wake, [reply, posted](bool complete, size_t dropped) {
214
+ if (!complete) {
215
+ reply->status = HTTP_STATUS_CONFLICT;
216
+ reply->body = "{\"error\":\"busy\",\"applied\":" + std::to_string(posted - dropped) + "}";
217
+ }
218
+ reply->ready.store(true, std::memory_order_release);
219
+ }));
220
+ return 0;
221
+ }
222
+ std::vector<std::shared_ptr<Server::InputReply>> Server::readyInputs() {
223
+ std::vector<std::shared_ptr<InputReply>> ready;
224
+ std::lock_guard<std::mutex> lock(reply_mutex);
225
+ for (auto reply = input_replies.begin(); reply != input_replies.end();) {
226
+ if (!(*reply)->ready.load(std::memory_order_acquire)) { ++reply; continue; }
227
+ ready.push_back(*reply); reply = input_replies.erase(reply);
228
+ }
229
+ return ready;
230
+ }
231
+ void Server::forgetInput(lws* wsi) {
232
+ std::lock_guard<std::mutex> lock(reply_mutex);
233
+ input_replies.erase(std::remove_if(input_replies.begin(), input_replies.end(),
234
+ [wsi](const std::shared_ptr<InputReply>& reply) { return reply->wsi == wsi; }), input_replies.end());
235
+ }
236
+ int Server::write(lws* wsi) {
237
+ updateFlow();
238
+ std::optional<EncodedFrame> frame;
239
+ std::optional<CursorImage> cursor;
240
+ { std::lock_guard<std::mutex> lock(mutex);
241
+ if (failed) return -1;
242
+ // Start with video to establish the generation; then share writes fairly.
243
+ if (lastSequence && pending_cursor && (!last_write_cursor || pending.empty())) {
244
+ cursor = std::move(pending_cursor); pending_cursor.reset();
245
+ } else {
246
+ if (pending.empty()) return 0;
247
+ frame.emplace(std::move(pending.front())); pending.pop_front();
248
+ }
249
+ }
250
+ std::vector<uint8_t> wire;
251
+ if (cursor) {
252
+ if (!cursorMessage(wire, LWS_PRE, generation, *cursor)) return -1;
253
+ const size_t size = wire.size() - LWS_PRE;
254
+ if (lws_write(wsi, wire.data() + LWS_PRE, size, LWS_WRITE_BINARY) != int(size)) return -1;
255
+ buffered_stream_bytes = lws_partial_buffered(wsi) ? size : 0;
256
+ last_write_cursor = true;
257
+ updateFlow();
258
+ { std::lock_guard<std::mutex> lock(mutex);
259
+ if (!pending.empty() || pending_cursor) lws_callback_on_writable(wsi);
260
+ }
261
+ return 0;
262
+ }
263
+ auto& f = *frame;
264
+ if (f.generation != generation) return 0;
265
+ if (!frameMessage(wire, LWS_PRE, generation, lastSequence, f, width, height)) return -1;
266
+ const size_t size = wire.size() - LWS_PRE;
267
+ if (lws_write(wsi, wire.data() + LWS_PRE, size, LWS_WRITE_BINARY) != int(size)) return -1;
268
+ buffered_stream_bytes = lws_partial_buffered(wsi) ? size : 0;
269
+ width = f.width; height = f.height; lastSequence = f.sequence;
270
+ last_write_cursor = false;
271
+ { std::lock_guard<std::mutex> lock(mutex); if (!flow.sent(f.sequence, nowUs())) return -1; }
272
+ updateFlow();
273
+ { std::lock_guard<std::mutex> lock(mutex); if (!pending.empty() || pending_cursor) lws_callback_on_writable(wsi); }
274
+ return 0;
275
+ }
276
+ namespace {
277
+ int responseHeaders(lws* wsi, unsigned status, const char* mime, size_t length) {
278
+ unsigned char buffer[LWS_PRE+512];
279
+ unsigned char* start = buffer+LWS_PRE;
280
+ unsigned char* next = start;
281
+ unsigned char* end = buffer+sizeof(buffer);
282
+ if (lws_add_http_common_headers(wsi, status, mime, length, &next, end) ||
283
+ lws_add_http_header_by_name(wsi, reinterpret_cast<const unsigned char*>("cache-control:"),
284
+ reinterpret_cast<const unsigned char*>("no-store"), 8, &next, end) ||
285
+ lws_add_http_header_by_token(wsi, WSI_TOKEN_CONNECTION,
286
+ reinterpret_cast<const unsigned char*>("close"), 5, &next, end) ||
287
+ lws_finalize_http_header(wsi, &next, end) ||
288
+ lws_write(wsi, start, size_t(next-start), LWS_WRITE_HTTP_HEADERS) != next-start) return -1;
289
+ lws_callback_on_writable(wsi); return 0;
290
+ }
291
+ /// Closes the HTTP connection once its response is flushed, since libwebsockets 4.1 spins on an idle
292
+ /// keep-alive connection that the peer has half-closed.
293
+ int completeResponse(lws* wsi, HttpResponse* response) {
294
+ if (!lws_partial_buffered(wsi)) return -1;
295
+ if (response) response->closing = true;
296
+ lws_callback_on_writable(wsi); return 0;
297
+ }
298
+ int jsonResponse(lws* wsi, HttpResponse* response, unsigned status, const std::string& json) {
299
+ if (!response || json.size() > sizeof(response->body)) return -1;
300
+ std::memcpy(response->body, json.data(), json.size()); response->length = json.size();
301
+ return responseHeaders(wsi, status, "application/json", response->length);
302
+ }
303
+ int errorResponse(lws* wsi, HttpResponse* response, unsigned status, const char* error) {
304
+ return jsonResponse(wsi, response, status, "{\"error\":\"" + std::string(error) + "\"}");
305
+ }
306
+ constexpr char fileCloseHeaders[] = "connection: close\r\n";
307
+ constexpr char viewerHtmlHeaders[] =
308
+ "connection: close\r\n"
309
+ "content-security-policy: frame-ancestors 'none'\r\n"
310
+ "x-frame-options: DENY\r\n";
311
+ void closeScreenshot(Server* server, lws* wsi, HttpResponse* response) {
312
+ if (server->screenshot_client == wsi) {
313
+ server->screenshot_client = nullptr;
314
+ lws_set_timer_usecs(wsi, LWS_SET_TIMER_USEC_CANCEL);
315
+ // During capture the slot stays busy until its worker result is safely discarded.
316
+ if (response && response->image) server->screenshot.finish();
317
+ }
318
+ if (response) { delete response->image; response->image = nullptr; response->offset = 0; }
319
+ }
320
+ int inputResponse(lws* wsi, HttpResponse* response, Server* s) {
321
+ const bool complete = response->input_length == response->input_expected;
322
+ response->input_expected = 0;
323
+ if (!complete) return -1;
324
+ if (s->holdsInput()) return errorResponse(wsi, response, HTTP_STATUS_CONFLICT, "busy");
325
+ const size_t count = response->input_length / 16;
326
+ if (s->input->pending() + count > max_queued_events) return errorResponse(wsi, response, HTTP_STATUS_CONFLICT, "busy");
327
+ size_t index = 0;
328
+ auto status = InputStatus::ok;
329
+ for (; index < count && status == InputStatus::ok; ++index) status = s->input->apply(inputEvent(response->input + index * 16));
330
+ s->release();
331
+ return s->deferInput(wsi, status, index, status == InputStatus::ok ? index : index - 1);
332
+ }
333
+ int metricsResponse(lws* wsi, HttpResponse* response, Server* server) {
334
+ if (!response) return -1;
335
+ std::string json;
336
+ const bool available = cgroupMetrics(json);
337
+ if (!available) json = "{\"resources_available\":false,\"usage_usec\":null,\"memory_current\":null,\"inactive_file\":null}";
338
+ json.pop_back(); json += server->metrics() + "}";
339
+ if (json.size() > sizeof(response->body)) return -1;
340
+ std::memcpy(response->body, json.data(), json.size());
341
+ response->length = json.size();
342
+ return responseHeaders(wsi, HTTP_STATUS_OK,
343
+ "application/json", response->length);
344
+ }
345
+ } // namespace
346
+ int callback(lws* wsi, lws_callback_reasons reason, void* user, void* in, size_t len) {
347
+ auto* s = static_cast<Server*>(lws_context_user(lws_get_context(wsi)));
348
+ if (!s) return 0;
349
+ switch (reason) {
350
+ case LWS_CALLBACK_HTTP: {
351
+ const std::string path(static_cast<const char*>(in), len);
352
+ auto* response = static_cast<HttpResponse*>(user);
353
+ if (response) { response->input_reply = false; response->input_headers = false; response->reply_status = 0; }
354
+ if (path == "/input") {
355
+ if (!response) return -1;
356
+ if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) <= 0) {
357
+ return errorResponse(wsi, response, HTTP_STATUS_METHOD_NOT_ALLOWED, "method_not_allowed");
358
+ }
359
+ if (!localRequest(s->config, wsi)) return errorResponse(wsi, response, HTTP_STATUS_FORBIDDEN, "forbidden");
360
+ if (!authenticated(s->config, wsi)) return errorResponse(wsi, response, HTTP_STATUS_UNAUTHORIZED, "unauthorized");
361
+ uint64_t length = 0;
362
+ if (const auto status = inputLength(header(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH), length)) {
363
+ return errorResponse(wsi, response, status, status == HTTP_STATUS_LENGTH_REQUIRED ? "length_required" :
364
+ status == HTTP_STATUS_REQ_ENTITY_TOO_LARGE ? "too_large" : "invalid_input");
365
+ }
366
+ if (s->holdsInput()) return errorResponse(wsi, response, HTTP_STATUS_CONFLICT, "busy");
367
+ response->input_length = 0; response->input_expected = length; return 0;
368
+ }
369
+ if (path == "/metrics" || path == "/capabilities" || path == "/screenshot.png") {
370
+ if (lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) <= 0) {
371
+ return errorResponse(wsi, response, HTTP_STATUS_METHOD_NOT_ALLOWED, "method_not_allowed");
372
+ }
373
+ if (!localRequest(s->config, wsi)) return errorResponse(wsi, response, HTTP_STATUS_FORBIDDEN, "forbidden");
374
+ if (!authenticated(s->config, wsi)) return errorResponse(wsi, response, HTTP_STATUS_UNAUTHORIZED, "unauthorized");
375
+ if (path == "/metrics") return metricsResponse(wsi, response, s);
376
+ if (path == "/screenshot.png") {
377
+ if (s->screenshot_client || !s->screenshot.request()) {
378
+ return errorResponse(wsi, response, HTTP_STATUS_SERVICE_UNAVAILABLE, "busy");
379
+ }
380
+ s->screenshot_client = wsi; response->image_headers = false; response->offset = 0;
381
+ lws_set_timer_usecs(wsi, 5000000); return 0;
382
+ }
383
+ const std::string backend = s->input->name();
384
+ const std::string json = "{\"version\":1,\"backend\":\"" + backend +
385
+ "\",\"width\":" + std::to_string(s->width) + ",\"height\":" + std::to_string(s->height) +
386
+ ",\"fps\":" + std::to_string(s->config.fps) +
387
+ ",\"input\":{\"keyboard\":true,\"pointer\":true,\"buttons\":true,\"wheel\":true}," +
388
+ "\"screenshot\":\"/screenshot.png\",\"control\":\"/control\",\"wayland\":" +
389
+ (backend == "wayland" ? "true" : "false") +
390
+ ",\"max_input_pending\":32,\"cursor\":" + (s->capture.stats().cursor_supported ? "true" : "false") +
391
+ ",\"input_endpoint\":\"/input\",\"max_input_batch\":" + std::to_string(max_input_batch) +
392
+ ",\"audio\":false,\"clipboard\":false,\"resize\":false,\"ime\":false}";
393
+ return jsonResponse(wsi, response, HTTP_STATUS_OK, json);
394
+ }
395
+ std::string file; const char* mime = nullptr; const char* headers = nullptr; int headerCount = 0;
396
+ if (path == "/" || path == "/index.html") {
397
+ file=s->config.viewer+"/index.html"; mime="text/html";
398
+ headers=viewerHtmlHeaders; headerCount=int(sizeof(viewerHtmlHeaders)-1);
399
+ }
400
+ if (path == "/viewer.js") {
401
+ file=s->config.viewer+"/viewer.js"; mime="application/javascript";
402
+ headers=fileCloseHeaders; headerCount=int(sizeof(fileCloseHeaders)-1);
403
+ }
404
+ if (file.empty()) return errorResponse(wsi, response, HTTP_STATUS_NOT_FOUND, "not_found");
405
+ return lws_serve_http_file(wsi, file.c_str(), mime, headers, headerCount) < 0 ? -1 : 0;
406
+ }
407
+ case LWS_CALLBACK_HTTP_WRITEABLE: {
408
+ auto* response = static_cast<HttpResponse*>(user);
409
+ if (!response) return -1;
410
+ if (response->closing) return completeResponse(wsi, response);
411
+ if (response->input_reply) {
412
+ if (!response->input_headers) {
413
+ response->input_headers = true;
414
+ return responseHeaders(wsi, response->reply_status, "application/json", response->length);
415
+ }
416
+ response->input_reply = false;
417
+ }
418
+ if (response->image) {
419
+ if (!response->image_headers) {
420
+ response->image_headers = true;
421
+ return responseHeaders(wsi, response->screenshot_error ? HTTP_STATUS_SERVICE_UNAVAILABLE : HTTP_STATUS_OK,
422
+ response->screenshot_error ? "application/json" : "image/png", response->image->size());
423
+ }
424
+ unsigned char chunk[LWS_PRE + 16384];
425
+ const size_t count = std::min<size_t>(16384, response->image->size() - response->offset);
426
+ const bool final = response->offset + count == response->image->size();
427
+ std::memcpy(chunk+LWS_PRE, response->image->data()+response->offset, count);
428
+ if (lws_write(wsi, chunk+LWS_PRE, count, final ? LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP) != int(count)) return -1;
429
+ response->offset += count;
430
+ if (!final) { lws_callback_on_writable(wsi); return 0; }
431
+ closeScreenshot(s, wsi, response); return completeResponse(wsi, response);
432
+ }
433
+ if (!response->length) return 0;
434
+ unsigned char buffer[LWS_PRE+2048];
435
+ std::memcpy(buffer+LWS_PRE, response->body, response->length);
436
+ const size_t count = response->length;
437
+ response->length = 0;
438
+ if (lws_write(wsi, buffer+LWS_PRE, count, LWS_WRITE_HTTP_FINAL) != int(count)) return -1;
439
+ return completeResponse(wsi, response);
440
+ }
441
+ case LWS_CALLBACK_HTTP_BODY: {
442
+ auto* response = static_cast<HttpResponse*>(user);
443
+ if (!response || !response->input_expected) return 0;
444
+ if (len > response->input_expected - response->input_length) return -1;
445
+ std::memcpy(response->input + response->input_length, in, len); response->input_length += len;
446
+ return 0;
447
+ }
448
+ case LWS_CALLBACK_HTTP_BODY_COMPLETION: {
449
+ auto* response = static_cast<HttpResponse*>(user);
450
+ return response && response->input_expected ? inputResponse(wsi, response, s) : 0;
451
+ }
452
+ case LWS_CALLBACK_CLOSED_HTTP: closeScreenshot(s, wsi, static_cast<HttpResponse*>(user)); s->forgetInput(wsi); return 0;
453
+ case LWS_CALLBACK_HTTP_FILE_COMPLETION: return completeResponse(wsi, static_cast<HttpResponse*>(user));
454
+ case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: {
455
+ char uri[256] = {}, args[256] = {};
456
+ if (lws_hdr_copy(wsi, uri, sizeof(uri), WSI_TOKEN_GET_URI) <= 0 || !user) return 1;
457
+ std::string target(uri);
458
+ if (lws_hdr_copy(wsi, args, sizeof(args), WSI_TOKEN_HTTP_URI_ARGS) > 0) { target += '?'; target += args; }
459
+ bool control = false, viewOnly = false;
460
+ if (!viewerTarget(target, control, viewOnly)) return 1;
461
+ if (!localRequest(s->config, wsi) || !authenticated(s->config, wsi, true) ||
462
+ s->viewer || s->nextGeneration == UINT32_MAX) return 1;
463
+ auto* response = static_cast<HttpResponse*>(user);
464
+ response->control = control; response->view_only = viewOnly;
465
+ return 0;
466
+ }
467
+ case LWS_CALLBACK_ESTABLISHED: {
468
+ if (s->viewer || s->nextGeneration == UINT32_MAX) return -1;
469
+ auto* response = static_cast<HttpResponse*>(user);
470
+ s->viewer = wsi; s->controller = response->control; s->viewOnly = response->view_only;
471
+ #ifdef __linux__
472
+ if (!s->controller) configureStreamSocket(wsi);
473
+ #endif
474
+ { std::lock_guard<std::mutex> lock(s->mutex); s->generation = ++s->nextGeneration; s->pending.clear(); s->pending_cursor.reset(); s->flow.reset(); s->failed=false; }
475
+ s->last_write_cursor = false;
476
+ s->lastSequence=0; s->received.clear();
477
+ if (!s->viewOnly) s->input->resetLocks();
478
+ if (s->controller) { s->control_ready = true; lws_callback_on_writable(wsi); }
479
+ else { s->capture.beginSession(s->generation); s->updateFlow(); }
480
+ return 0;
481
+ }
482
+ case LWS_CALLBACK_RECEIVE:
483
+ if (s->viewer != wsi || !lws_frame_is_binary(wsi) || len > 65536-s->received.size()) return -1;
484
+ s->received.insert(s->received.end(), static_cast<uint8_t*>(in), static_cast<uint8_t*>(in)+len);
485
+ if (s->received.size() > 36) return -1;
486
+ if (lws_is_final_fragment(wsi) && !lws_remaining_packet_payload(wsi)) {
487
+ if (!s->message()) return -1;
488
+ s->received.clear();
489
+ }
490
+ return 0;
491
+ case LWS_CALLBACK_SERVER_WRITEABLE: return s->viewer == wsi ? (s->controller ? s->writeControl(wsi) : s->write(wsi)) : -1;
492
+ case LWS_CALLBACK_EVENT_WAIT_CANCELLED: {
493
+ if (s->viewer) {
494
+ if (!s->controller) s->updateFlow();
495
+ lws_callback_on_writable(s->viewer);
496
+ }
497
+ for (const auto& reply : s->readyInputs()) {
498
+ auto* response = reply->wsi ? static_cast<HttpResponse*>(lws_wsi_user(reply->wsi)) : nullptr;
499
+ if (!response) continue;
500
+ std::memcpy(response->body, reply->body.data(), reply->body.size());
501
+ response->length = reply->body.size();
502
+ response->reply_status = reply->status;
503
+ response->input_reply = true; response->input_headers = false;
504
+ lws_callback_on_writable(reply->wsi);
505
+ }
506
+ std::vector<uint8_t> image; std::string error;
507
+ if (s->screenshot.take(image, error)) {
508
+ if (!s->screenshot_client) { s->screenshot.finish(); return 0; }
509
+ auto* response = static_cast<HttpResponse*>(lws_wsi_user(s->screenshot_client));
510
+ if (!response) { s->screenshot_client = nullptr; s->screenshot.finish(); return 0; }
511
+ response->screenshot_error = !error.empty();
512
+ if (!error.empty()) {
513
+ std::cerr << "Screenshot failed: " << error << '\n';
514
+ const std::string body = "{\"error\":\"screenshot_unavailable\"}";
515
+ image.assign(body.begin(), body.end());
516
+ }
517
+ response->image = new std::vector<uint8_t>(std::move(image));
518
+ lws_callback_on_writable(s->screenshot_client);
519
+ }
520
+ return 0;
521
+ }
522
+ case LWS_CALLBACK_TIMER:
523
+ if (s->screenshot_client == wsi) { closeScreenshot(s, wsi, static_cast<HttpResponse*>(user)); return -1; }
524
+ if (s->viewer != wsi) return -1;
525
+ s->flow_timer_due_us = 0;
526
+ if (s->timedOut()) {
527
+ std::cerr << "Viewer flow failure, 3s frame age, or no ACK progress for 3s\n"; s->end(); return -1;
528
+ }
529
+ s->updateFlow();
530
+ return 0;
531
+ case LWS_CALLBACK_CLOSED: if (s->viewer == wsi) s->end(); return 0;
532
+ default: return 0;
533
+ }
534
+ }
535
+ } // namespace relay
@@ -0,0 +1,89 @@
1
+ #pragma once
2
+
3
+ #include "config.hpp"
4
+ #include "core/capture.hpp"
5
+ #include "core/flow.hpp"
6
+ #include "core/input.hpp"
7
+ #include "screenshot.hpp"
8
+ #include "wire.hpp"
9
+ #include <libwebsockets.h>
10
+ #include <atomic>
11
+ #include <deque>
12
+ #include <functional>
13
+ #include <memory>
14
+ #include <mutex>
15
+ #include <optional>
16
+ #include <string>
17
+ #include <utility>
18
+ #include <vector>
19
+
20
+ namespace relay {
21
+ struct HttpResponse {
22
+ unsigned char body[2048], input[max_input_batch * 16];
23
+ size_t length, input_length, input_expected;
24
+ std::vector<uint8_t>* image;
25
+ size_t offset;
26
+ bool image_headers, screenshot_error, control, view_only, input_reply, input_headers, closing;
27
+ unsigned reply_status;
28
+ };
29
+ /** One viewer session at a time; the capture thread and the lws service thread share it through mutex. */
30
+ struct Server {
31
+ struct InputReply {
32
+ lws* wsi = nullptr;
33
+ unsigned status = 0;
34
+ std::string body;
35
+ std::atomic<bool> ready{false};
36
+ };
37
+ struct ControlReply {
38
+ uint32_t seq = 0;
39
+ InputStatus status = InputStatus::ok;
40
+ std::atomic<bool> ready{false};
41
+ };
42
+ /// Context the input thread may wake; it is set and cleared under this mutex so a fence never wakes a destroyed context.
43
+ struct InputWake {
44
+ void set(lws_context* next);
45
+ void cancel();
46
+ std::mutex mutex;
47
+ lws_context* context = nullptr;
48
+ };
49
+ Config config;
50
+ lws_context* context = nullptr;
51
+ lws* viewer = nullptr;
52
+ Screenshot screenshot;
53
+ lws* screenshot_client = nullptr;
54
+ bool controller = false, viewOnly = false, control_ready = false;
55
+ std::deque<std::shared_ptr<ControlReply>> replies;
56
+ std::mutex reply_mutex;
57
+ std::deque<std::shared_ptr<InputReply>> input_replies;
58
+ std::mutex mutex;
59
+ std::shared_ptr<InputWake> wake = std::make_shared<InputWake>();
60
+ std::unique_ptr<InputController> input;
61
+ std::deque<EncodedFrame> pending;
62
+ std::optional<CursorImage> pending_cursor;
63
+ bool last_write_cursor = false;
64
+ size_t buffered_stream_bytes = 0;
65
+ uint32_t stream_notsent_lowat = 0;
66
+ uint64_t flow_timer_due_us = 0;
67
+ FlowControl flow;
68
+ CaptureEncoder capture;
69
+ uint32_t generation = 0, lastSequence = 0, nextGeneration = 0;
70
+ uint16_t width = 0, height = 0;
71
+ bool failed = false;
72
+ std::vector<uint8_t> received;
73
+ explicit Server(Config settings);
74
+ void release();
75
+ void end();
76
+ bool holdsInput() const { return viewer && !viewOnly; }
77
+ bool message();
78
+ void updateFlow();
79
+ bool timedOut();
80
+ std::string metrics();
81
+ /// Holds the HTTP response until the backend applied or dropped the batch; posted counts records that reached it.
82
+ int deferInput(lws* wsi, InputStatus status, size_t applied, size_t posted);
83
+ std::vector<std::shared_ptr<InputReply>> readyInputs();
84
+ void forgetInput(lws* wsi);
85
+ int writeControl(lws* wsi);
86
+ int write(lws* wsi);
87
+ };
88
+ int callback(lws* wsi, lws_callback_reasons reason, void* user, void* in, size_t len);
89
+ } // namespace relay