@desklink/host 0.1.1 → 0.2.0

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.
@@ -89,6 +89,26 @@ pub enum SourceRequest {
89
89
  #[serde(default)]
90
90
  display: Option<String>,
91
91
  },
92
+ /// Video the consumer already encoded, fed access unit by access unit with
93
+ /// `session.feed`. The engine has no capture backend, no encoder and no
94
+ /// input backend for this source: it packetizes what it is handed and
95
+ /// forwards control messages back to the consumer, which owns the device.
96
+ Encoded {
97
+ codec: EncodedCodec,
98
+ /// The stream's own pixel size. The consumer is the only party that
99
+ /// knows it, and nothing here is scaled: the client aims at these
100
+ /// pixels and the consumer maps them to its device.
101
+ width: usize,
102
+ height: usize,
103
+ },
104
+ }
105
+
106
+ /// A codec the consumer may feed. Only H.264 is accepted today, because it is
107
+ /// what the device helpers emit and what the clients can already decode.
108
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
109
+ #[serde(rename_all = "lowercase")]
110
+ pub enum EncodedCodec {
111
+ H264,
92
112
  }
93
113
 
94
114
  #[derive(Debug, Deserialize)]
@@ -154,6 +174,44 @@ pub struct FrameParams {
154
174
  pub region: Option<[usize; 4]>,
155
175
  }
156
176
 
177
+ /// One access unit of a consumer-encoded stream.
178
+ ///
179
+ /// Base64 inside the same JSON line keeps one ordered channel and one fd: one
180
+ /// second of a 2 Mbit/s stream costs about 1 ms to decode here and 0.09 ms to
181
+ /// encode on the consumer's side, against the 5 % of a core that would have
182
+ /// justified a second fd (see `docs/PROTOCOL.md`, "Encoded sources").
183
+ #[derive(Debug, Deserialize)]
184
+ pub struct FeedParams {
185
+ pub session_id: String,
186
+ /// True for an IDR access unit. The first one also carries the SPS whose
187
+ /// profile-level-id the offer negotiates.
188
+ pub keyframe: bool,
189
+ pub data_b64: String,
190
+ }
191
+
192
+ /// The largest access unit this protocol accepts, once decoded. A 4K key frame
193
+ /// at a quality worth sending is a few hundred kilobytes; this refuses a request
194
+ /// that is not a picture rather than acting as a rate limit.
195
+ pub const MAX_ACCESS_UNIT_BYTES: usize = 8 * 1024 * 1024;
196
+
197
+ impl FeedParams {
198
+ /// The access unit's own bytes, or why this request is not one.
199
+ pub fn access_unit(&self) -> std::result::Result<Vec<u8>, String> {
200
+ let data = base64::Engine::decode(
201
+ &base64::engine::general_purpose::STANDARD,
202
+ self.data_b64.as_bytes(),
203
+ )
204
+ .map_err(|error| format!("data_b64 is not base64: {error}"))?;
205
+ if data.len() > MAX_ACCESS_UNIT_BYTES {
206
+ return Err(format!(
207
+ "an access unit is {} bytes; the limit is {MAX_ACCESS_UNIT_BYTES}",
208
+ data.len()
209
+ ));
210
+ }
211
+ Ok(data)
212
+ }
213
+ }
214
+
157
215
  #[derive(Debug, Deserialize)]
158
216
  pub struct SessionRef {
159
217
  pub session_id: String,
@@ -202,7 +260,7 @@ pub struct ClipboardParams {
202
260
  /// `seq` is the client's own ordering. The engine refuses a repeat or a lower
203
261
  /// value rather than re-applying an action, so a replayed frame cannot move the
204
262
  /// pointer twice.
205
- #[derive(Debug, Deserialize)]
263
+ #[derive(Debug, Deserialize, Serialize)]
206
264
  #[serde(tag = "kind", rename_all = "snake_case")]
207
265
  pub enum ControlMessage {
208
266
  Pointer {
@@ -274,7 +332,7 @@ impl ControlMessage {
274
332
  }
275
333
  }
276
334
 
277
- #[derive(Debug, Clone, Copy, Deserialize)]
335
+ #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
278
336
  #[serde(rename_all = "snake_case")]
279
337
  pub enum PointerPhase {
280
338
  Move,
@@ -355,6 +413,84 @@ mod tests {
355
413
  ));
356
414
  }
357
415
 
416
+ #[test]
417
+ fn an_encoded_source_names_its_codec_and_its_own_size() {
418
+ let request: SourceRequest = serde_json::from_value(serde_json::json!({
419
+ "kind": "encoded",
420
+ "codec": "h264",
421
+ "width": 486,
422
+ "height": 1080
423
+ }))
424
+ .unwrap();
425
+ assert!(matches!(
426
+ request,
427
+ SourceRequest::Encoded {
428
+ codec: EncodedCodec::H264,
429
+ width: 486,
430
+ height: 1080
431
+ }
432
+ ));
433
+ }
434
+
435
+ #[test]
436
+ fn an_unknown_encoded_codec_is_refused_rather_than_guessed() {
437
+ assert!(serde_json::from_value::<SourceRequest>(serde_json::json!({
438
+ "kind": "encoded",
439
+ "codec": "av1",
440
+ "width": 486,
441
+ "height": 1080
442
+ }))
443
+ .is_err());
444
+ }
445
+
446
+ #[test]
447
+ fn a_feed_request_is_one_access_unit_with_its_own_keyframe_flag() {
448
+ let params: FeedParams =
449
+ serde_json::from_str(r#"{"session_id":"s","keyframe":true,"data_b64":"AAAA"}"#)
450
+ .unwrap();
451
+ assert_eq!(params.session_id, "s");
452
+ assert!(params.keyframe, "a feed says whether its unit is an IDR");
453
+ assert_eq!(params.data_b64, "AAAA");
454
+ assert_eq!(params.access_unit().unwrap(), vec![0x00, 0x00, 0x00]);
455
+ }
456
+
457
+ #[test]
458
+ fn a_feed_request_that_is_not_base64_is_refused() {
459
+ let params = FeedParams {
460
+ session_id: String::from("s"),
461
+ keyframe: false,
462
+ data_b64: String::from("not base64!!"),
463
+ };
464
+ assert!(params.access_unit().is_err());
465
+ }
466
+
467
+ /// The reason this protocol carries bytes in base64 rather than adding a
468
+ /// second inherited fd: at 2 Mbit/s it costs a fraction of a per cent of a
469
+ /// core, against a whole second channel to keep ordered and bounded. The
470
+ /// bound below is deliberately loose — it is a sanity check on the order of
471
+ /// magnitude, not a benchmark, so it does not flake on a loaded machine.
472
+ #[test]
473
+ fn decoding_a_two_megabit_stream_is_nowhere_near_a_core() {
474
+ // One second of a 2 Mbit/s stream, in access units.
475
+ let unit: Vec<u8> = (0..8_000u32).map(|index| index as u8).collect();
476
+ let encoded = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &unit);
477
+ let params = FeedParams {
478
+ session_id: String::from("s"),
479
+ keyframe: false,
480
+ data_b64: encoded,
481
+ };
482
+ let started = std::time::Instant::now();
483
+ for _ in 0..(250_000 / unit.len()) {
484
+ assert_eq!(params.access_unit().unwrap().len(), unit.len());
485
+ }
486
+ let elapsed = started.elapsed();
487
+ assert!(
488
+ elapsed < std::time::Duration::from_millis(500),
489
+ "a second of 2 Mbit/s base64 took {elapsed:?}, which is far past 5 % of a core",
490
+ );
491
+ println!("a second of 2 Mbit/s stream decoded in {elapsed:?}");
492
+ }
493
+
358
494
  #[test]
359
495
  fn open_defaults_keep_the_desktops_own_pixels_up_to_4k() {
360
496
  let params: OpenParams =