screen-relay 0.1.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.
@@ -80,14 +80,13 @@ Server::Server(Config settings) : config(std::move(settings)), capture([this](En
80
80
  }) { flow.configure(config.bitrate_min, config.bitrate_start, config.bitrate_max, config.fps); }
81
81
  void Server::release() { if (input) input->releaseAll(); }
82
82
  void Server::end() {
83
- release();
84
- input->cancel();
83
+ if (holdsInput()) { release(); input->cancel(); }
85
84
  uint32_t old;
86
85
  { std::lock_guard<std::mutex> lock(mutex); old = generation; generation = 0; pending.clear(); pending_cursor.reset(); flow.reset(); failed = false; }
87
86
  last_write_cursor = false;
88
87
  buffered_stream_bytes = 0; stream_notsent_lowat = 0; flow_timer_due_us = 0;
89
88
  if (old) capture.endSession(old);
90
- lastSequence = 0; received.clear(); replies.clear(); control_ready = controller = false; viewer = nullptr;
89
+ lastSequence = 0; received.clear(); replies.clear(); control_ready = controller = viewOnly = false; viewer = nullptr;
91
90
  }
92
91
  bool Server::message() {
93
92
  const auto& b = received;
@@ -113,6 +112,7 @@ bool Server::message() {
113
112
  return true;
114
113
  }
115
114
  if (b[5] != 3 || b.size() != 36 || seq || gen != generation) return false;
115
+ if (viewOnly) return true;
116
116
  return input->apply(inputEvent(b.data()+20)) != InputStatus::invalid;
117
117
  }
118
118
  void Server::updateFlow() {
@@ -321,7 +321,7 @@ int inputResponse(lws* wsi, HttpResponse* response, Server* s) {
321
321
  const bool complete = response->input_length == response->input_expected;
322
322
  response->input_expected = 0;
323
323
  if (!complete) return -1;
324
- if (s->viewer) return errorResponse(wsi, response, HTTP_STATUS_CONFLICT, "busy");
324
+ if (s->holdsInput()) return errorResponse(wsi, response, HTTP_STATUS_CONFLICT, "busy");
325
325
  const size_t count = response->input_length / 16;
326
326
  if (s->input->pending() + count > max_queued_events) return errorResponse(wsi, response, HTTP_STATUS_CONFLICT, "busy");
327
327
  size_t index = 0;
@@ -363,7 +363,7 @@ int callback(lws* wsi, lws_callback_reasons reason, void* user, void* in, size_t
363
363
  return errorResponse(wsi, response, status, status == HTTP_STATUS_LENGTH_REQUIRED ? "length_required" :
364
364
  status == HTTP_STATUS_REQ_ENTITY_TOO_LARGE ? "too_large" : "invalid_input");
365
365
  }
366
- if (s->viewer) return errorResponse(wsi, response, HTTP_STATUS_CONFLICT, "busy");
366
+ if (s->holdsInput()) return errorResponse(wsi, response, HTTP_STATUS_CONFLICT, "busy");
367
367
  response->input_length = 0; response->input_expected = length; return 0;
368
368
  }
369
369
  if (path == "/metrics" || path == "/capabilities" || path == "/screenshot.png") {
@@ -452,25 +452,29 @@ int callback(lws* wsi, lws_callback_reasons reason, void* user, void* in, size_t
452
452
  case LWS_CALLBACK_CLOSED_HTTP: closeScreenshot(s, wsi, static_cast<HttpResponse*>(user)); s->forgetInput(wsi); return 0;
453
453
  case LWS_CALLBACK_HTTP_FILE_COMPLETION: return completeResponse(wsi, static_cast<HttpResponse*>(user));
454
454
  case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: {
455
- char uri[256] = {};
455
+ char uri[256] = {}, args[256] = {};
456
456
  if (lws_hdr_copy(wsi, uri, sizeof(uri), WSI_TOKEN_GET_URI) <= 0 || !user) return 1;
457
- const bool control = !std::strcmp(uri, "/control");
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;
458
461
  if (!localRequest(s->config, wsi) || !authenticated(s->config, wsi, true) ||
459
- (!control && std::strcmp(uri, "/stream")) ||
460
462
  s->viewer || s->nextGeneration == UINT32_MAX) return 1;
461
- static_cast<HttpResponse*>(user)->control = control;
463
+ auto* response = static_cast<HttpResponse*>(user);
464
+ response->control = control; response->view_only = viewOnly;
462
465
  return 0;
463
466
  }
464
467
  case LWS_CALLBACK_ESTABLISHED: {
465
468
  if (s->viewer || s->nextGeneration == UINT32_MAX) return -1;
466
- s->viewer = wsi; s->controller = static_cast<HttpResponse*>(user)->control;
469
+ auto* response = static_cast<HttpResponse*>(user);
470
+ s->viewer = wsi; s->controller = response->control; s->viewOnly = response->view_only;
467
471
  #ifdef __linux__
468
472
  if (!s->controller) configureStreamSocket(wsi);
469
473
  #endif
470
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; }
471
475
  s->last_write_cursor = false;
472
476
  s->lastSequence=0; s->received.clear();
473
- s->input->resetLocks();
477
+ if (!s->viewOnly) s->input->resetLocks();
474
478
  if (s->controller) { s->control_ready = true; lws_callback_on_writable(wsi); }
475
479
  else { s->capture.beginSession(s->generation); s->updateFlow(); }
476
480
  return 0;
@@ -23,7 +23,7 @@ struct HttpResponse {
23
23
  size_t length, input_length, input_expected;
24
24
  std::vector<uint8_t>* image;
25
25
  size_t offset;
26
- bool image_headers, screenshot_error, control, input_reply, input_headers, closing;
26
+ bool image_headers, screenshot_error, control, view_only, input_reply, input_headers, closing;
27
27
  unsigned reply_status;
28
28
  };
29
29
  /** One viewer session at a time; the capture thread and the lws service thread share it through mutex. */
@@ -51,7 +51,7 @@ struct Server {
51
51
  lws* viewer = nullptr;
52
52
  Screenshot screenshot;
53
53
  lws* screenshot_client = nullptr;
54
- bool controller = false, control_ready = false;
54
+ bool controller = false, viewOnly = false, control_ready = false;
55
55
  std::deque<std::shared_ptr<ControlReply>> replies;
56
56
  std::mutex reply_mutex;
57
57
  std::deque<std::shared_ptr<InputReply>> input_replies;
@@ -73,6 +73,7 @@ struct Server {
73
73
  explicit Server(Config settings);
74
74
  void release();
75
75
  void end();
76
+ bool holdsInput() const { return viewer && !viewOnly; }
76
77
  bool message();
77
78
  void updateFlow();
78
79
  bool timedOut();
@@ -31,6 +31,11 @@ unsigned inputLength(const std::string& text, uint64_t& length) {
31
31
  if (!unsignedNumber(text, length) || !length || length % 16) return HTTP_STATUS_BAD_REQUEST;
32
32
  return length > max_input_batch * 16 ? HTTP_STATUS_REQ_ENTITY_TOO_LARGE : 0;
33
33
  }
34
+ bool viewerTarget(const std::string& target, bool& control, bool& viewOnly) {
35
+ control = target == "/control";
36
+ viewOnly = target == "/stream?view=1";
37
+ return control || viewOnly || target == "/stream";
38
+ }
34
39
  bool frameMessage(std::vector<uint8_t>& out, size_t pre, uint32_t generation, uint32_t lastSequence, const EncodedFrame& f,
35
40
  uint16_t width, uint16_t height) {
36
41
  if (!f.sequence || f.sequence != lastSequence + 1 || !f.width || !f.height ||
@@ -15,6 +15,8 @@ void put(uint8_t* p, uint64_t value, unsigned count);
15
15
  InputEvent inputEvent(const uint8_t* p);
16
16
  /** Validates an /input Content-Length header and returns 0 or the HTTP error status. */
17
17
  unsigned inputLength(const std::string& text, uint64_t& length);
18
+ /** Classifies a /stream, /stream?view=1, or /control request target and reports false for any other target. */
19
+ bool viewerTarget(const std::string& target, bool& control, bool& viewOnly);
18
20
  /**
19
21
  * The builders below fill out with pre zero bytes of caller padding followed by one NSL1 message.
20
22
  * frameMessage and cursorMessage return false when the payload must not be sent.