tvpipe 0.3.0

Stream a program's frames and sound, files, web pages or the screen to Apple TVs, Chromecasts and browser tabs with low latency, in process
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Surfaces (docs/playshare-design.md §17): a page encodes a canvas with
//! WebCodecs and sends the H.264 and PCM as SLRW records over
//! `/_tvpipe/surface?id=N`; tvpipe streams them to the surface's
//! receivers on the program's-own-H.264 path (no browser of tvpipe's in
//! between). Keyframe requests go back to the page.
//!
//! Page → tvpipe: binary messages, one SLRW record each (`u8 kind | u64le
//! pts_us | u32le len | payload`; kinds 4 config, 3 H.264, 2 PCM, 0 end;
//! `pts_us` on the wall clock, `performance.timeOrigin + now()`).
//! tvpipe → page: `{"t":"status",…}` (the surface's state and receivers),
//! `{"t":"keyframe"}`, `{"t":"stats","fps","kbps","lagMs","lagMaxMs","dropped"}`
//! once a second while frames come.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

use crate::codec::json::Value;
use crate::error::{Error, Result};
use crate::net::websocket::Message;

use super::backend::{SurfaceSink, SurfaceSpec};
use super::conn::{self, Next, Out};
use super::hub::Hub;

/// The largest record (a 4K keyframe fits).
const MAX_RECORD: usize = 8 << 20;
/// Surfaces at once.
pub const MAX_SURFACES: usize = 8;

#[derive(Default)]
struct Stats {
    /// A video frame was queued: sound may follow (before it, the stream
    /// is not open and PCM would wait in its queue, stale).
    video_on: bool,
    /// The first record: its wall-clock pts and where that is on the
    /// stream's clock. Later records keep their distance to it.
    origin: Option<(u64, Duration)>,
    frames: u64,
    dropped: u64,
    bytes: u64,
    lag_sum_us: i64,
    lag_max_us: i64,
    since: Option<Instant>,
}

/// One surface.
pub(super) struct Entry {
    /// `None` once stopped (a socket may still hold the entry).
    sink: Arc<Mutex<Option<Box<dyn SurfaceSink>>>>,
    stats: Arc<Mutex<Stats>>,
    out: Option<Out>,
    last: Value,
}

/// The surfaces of a run.
#[derive(Default)]
pub(super) struct Surfaces {
    next: u32,
    map: HashMap<u32, Entry>,
}

fn wall_us() -> i64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_micros() as i64)
        .unwrap_or(0)
}

fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    m.lock().unwrap_or_else(|e| e.into_inner())
}

impl Hub {
    fn surfaces(&self) -> std::sync::MutexGuard<'_, Surfaces> {
        lock(&self.surfaces)
    }

    /// `surface.open`.
    pub(super) fn surface_open(&self, params: &Value, selection: Vec<String>) -> Result<Value> {
        let num = |k: &str, lo: u32, hi: u32, default: Option<u32>| -> Result<u32> {
            match params.get(k) {
                None | Some(Value::Null) => {
                    default.ok_or_else(|| Error::usage(format!("surface.open: {k}")))
                }
                Some(v) => v
                    .as_u32()
                    .filter(|n| (lo..=hi).contains(n))
                    .ok_or_else(|| Error::usage(format!("surface.open: {k} is {lo}-{hi}"))),
            }
        };
        let width = num("width", 16, 3840, None)? & !1;
        let height = num("height", 16, 2160, None)? & !1;
        let fps = num("fps", 1, 120, Some(30))?;
        let bitrate = params
            .get("bitrate")
            .and_then(Value::as_u32)
            .map(|b| b / 1000);
        let audio = match params.get("audio") {
            Some(Value::Bool(true)) => Some((48_000, 2)),
            Some(a) if a.as_object().is_some() => Some((
                num_in(a, "sampleRate", 8_000, 192_000)?,
                num_in(a, "channels", 1, 2)? as u8,
            )),
            _ => None,
        };
        let to: Vec<String> = match params.get("to").and_then(Value::as_array) {
            Some(list) => list
                .iter()
                .filter_map(Value::as_str)
                .map(str::to_owned)
                .collect(),
            None => selection,
        };
        if to.is_empty() {
            return Err(Error::usage("no receivers chosen").with_code("no-receivers"));
        }
        if self.surfaces().map.len() >= MAX_SURFACES {
            return Err(Error::usage("too many surfaces").with_code("too-many"));
        }
        let spec = SurfaceSpec {
            width,
            height,
            fps,
            audio,
            bitrate_kbps: bitrate,
        };
        info!(
            "playshare: a {width}x{height}@{fps} surface to {}",
            to.join(", ")
        );
        let sink = self.backend.surface(&to, spec)?;
        let mut s = self.surfaces();
        s.next += 1;
        let id = s.next;
        s.map.insert(
            id,
            Entry {
                sink: Arc::new(Mutex::new(Some(sink))),
                stats: Arc::default(),
                out: None,
                last: Value::Null,
            },
        );
        Ok(Value::object()
            .with("id", id)
            .with("path", format!("/_tvpipe/surface?id={id}"))
            .with("width", width)
            .with("height", height)
            .with("fps", fps)
            .with(
                "audio",
                match audio {
                    Some((r, c)) => Value::object()
                        .with("sampleRate", r)
                        .with("channels", u32::from(c)),
                    None => Value::Null,
                },
            ))
    }

    /// `surface.close`.
    pub(super) fn surface_close(&self, id: u32) -> Result<Value> {
        let e = self
            .surfaces()
            .map
            .remove(&id)
            .ok_or_else(|| Error::usage(format!("no surface {id}")))?;
        self.stop_entry(id, e);
        Ok(Value::object())
    }

    fn stop_entry(&self, id: u32, e: Entry) {
        if let Some(o) = &e.out {
            o.text(Value::object().with("t", "end").to_json());
            o.close(1000, "closed");
        }
        info!("playshare: surface {id} stopped");
        let sink = lock(&e.sink).take();
        if let Some(sink) = sink {
            sink.stop();
        }
        self.event(
            "surface",
            Value::object().with("id", id).with("state", "stopped"),
        );
    }

    /// Every surface (the end).
    pub(super) fn surfaces_stop_all(&self) {
        let all: Vec<(u32, Entry)> = self.surfaces().map.drain().collect();
        for (id, e) in all {
            self.stop_entry(id, e);
        }
    }

    /// Whether `id` is an open surface.
    pub(super) fn surface_exists(&self, id: u32) -> bool {
        self.surfaces().map.contains_key(&id)
    }

    /// Keyframe requests and status changes, to the pages and as events.
    pub(super) fn surfaces_tick(&self) {
        let mut events = Vec::new();
        let mut s = self.surfaces();
        for (id, e) in s.map.iter_mut() {
            let (status, key) = match lock(&e.sink).as_mut() {
                Some(sink) => (sink.status(), sink.keyframe_wanted()),
                None => continue,
            };
            if let Some(o) = &e.out {
                if key {
                    o.text(Value::object().with("t", "keyframe").to_json());
                }
            }
            if status != e.last {
                e.last = status.clone();
                if let Some(o) = &e.out {
                    o.text(status.clone().with("t", "status").to_json());
                }
                events.push(status.with("id", *id));
            }
            // Stats once a second.
            let mut st = lock(&e.stats);
            if let Some(since) = st.since {
                let dt = since.elapsed();
                if dt >= Duration::from_secs(1) {
                    let secs = dt.as_secs_f64();
                    let n = st.frames.max(1) as f64;
                    let v = Value::object()
                        .with("t", "stats")
                        .with("fps", (st.frames as f64 / secs * 10.0).round() / 10.0)
                        .with("kbps", (st.bytes as f64 * 8.0 / 1000.0 / secs).round())
                        .with("lagMs", (st.lag_sum_us as f64 / n / 100.0).round() / 10.0)
                        .with("lagMaxMs", (st.lag_max_us as f64 / 100.0).round() / 10.0)
                        .with("dropped", st.dropped);
                    debug!("playshare: surface {id}: {v}");
                    if let Some(o) = &e.out {
                        o.text(v.to_json());
                    }
                    let (origin, video_on) = (st.origin, st.video_on);
                    *st = Stats {
                        origin,
                        video_on,
                        since: Some(Instant::now()),
                        ..Stats::default()
                    };
                }
            }
        }
        drop(s);
        for e in events {
            self.event("surface", e);
        }
    }

    /// Runs a surface's socket until it ends.
    pub(super) fn run_surface(
        self: &Arc<Hub>,
        tcp: std::net::TcpStream,
        leftover: Vec<u8>,
        id: u32,
    ) {
        let Some((sink, stats)) = self
            .surfaces()
            .map
            .get(&id)
            .map(|e| (e.sink.clone(), e.stats.clone()))
        else {
            return;
        };
        let opts = conn::Options {
            name: "surface",
            max_message: MAX_RECORD,
            first_message: None,
            mark: None,
            limit: None,
        };
        let hub = self.clone();
        conn::run(
            tcp,
            leftover,
            opts,
            |out| {
                let mut s = hub.surfaces();
                if let Some(e) = s.map.get_mut(&id) {
                    if let Some(old) = e.out.replace(out.clone()) {
                        old.close(1000, "replaced");
                    }
                    e.last = Value::Null; // the new page gets the status
                }
            },
            |msg| match msg {
                Message::Binary(b) => match record(&sink, &stats, &b) {
                    Ok(()) => Next::Continue,
                    Err(e) => {
                        warn!("playshare: surface {id}: {e}");
                        Next::Close(1002, "bad record".into())
                    }
                },
                Message::Text(t) => {
                    if let Ok(v) = Value::parse(&t) {
                        if v.get("t").and_then(Value::as_str) == Some("log") {
                            info!(
                                "playshare: surface {id}: {} in {}",
                                v.get("codec").and_then(Value::as_str).unwrap_or("?"),
                                v.get("agent").and_then(Value::as_str).unwrap_or("?")
                            );
                        }
                    }
                    Next::Continue
                }
                Message::Close(_) => Next::Continue,
            },
        );
        let mut s = self.surfaces();
        if let Some(e) = s.map.get_mut(&id) {
            e.out = None;
        }
    }
}

fn num_in(v: &Value, k: &str, lo: u32, hi: u32) -> Result<u32> {
    v.get(k)
        .and_then(Value::as_u32)
        .filter(|n| (lo..=hi).contains(n))
        .ok_or_else(|| Error::usage(format!("surface.open: audio.{k} is {lo}-{hi}")))
}

/// One SLRW record from the page.
fn record(
    sink: &Mutex<Option<Box<dyn SurfaceSink>>>,
    stats: &Mutex<Stats>,
    b: &[u8],
) -> Result<()> {
    let mut guard = lock(sink);
    let Some(sink) = guard.as_mut() else {
        return Err(Error::closed("surface: closed"));
    };
    if b.len() < 13 {
        return Err(Error::malformed("surface: a short record"));
    }
    let kind = b[0];
    let pts = u64::from_le_bytes(b[1..9].try_into().unwrap_or([0; 8]));
    let len = u32::from_le_bytes(b[9..13].try_into().unwrap_or([0; 4])) as usize;
    let payload = &b[13..];
    if len != payload.len() {
        return Err(Error::malformed("surface: the record's length is wrong"));
    }
    // Onto the stream's clock, as a game in process stamps its frames:
    // the first record at now on that clock less how long ago the page
    // took it (its pts is the page's wall clock), the others at their
    // distance from it.
    let rel = {
        let mut st = lock(stats);
        let (p0, r0) = *st.origin.get_or_insert_with(|| {
            let age = Duration::from_micros(u64::try_from(wall_us() - pts as i64).unwrap_or(0));
            (pts, sink.elapsed().saturating_sub(age))
        });
        if pts >= p0 {
            r0 + Duration::from_micros(pts - p0)
        } else {
            r0.saturating_sub(Duration::from_micros(p0 - pts))
        }
    };
    match kind {
        4 => {
            let (&format, data) = payload
                .split_first()
                .ok_or_else(|| Error::malformed("surface: an empty config"))?;
            sink.config(data, format == 1)
        }
        3 => {
            let (&flags, au) = payload
                .split_first()
                .ok_or_else(|| Error::malformed("surface: an empty frame"))?;
            let queued = sink.video(au, rel, flags & 1 != 0, flags & 2 != 0)?;
            let mut st = lock(stats);
            st.since.get_or_insert_with(Instant::now);
            if queued {
                st.video_on = true;
                st.frames += 1;
                st.bytes += au.len() as u64;
                let lag = wall_us() - pts as i64;
                st.lag_sum_us += lag;
                st.lag_max_us = st.lag_max_us.max(lag);
            } else {
                st.dropped += 1;
            }
            Ok(())
        }
        2 => {
            if payload.len() % 2 != 0 {
                return Err(Error::malformed("surface: odd PCM"));
            }
            if !lock(stats).video_on {
                return Ok(());
            }
            let pcm: Vec<i16> = payload
                .chunks_exact(2)
                .map(|c| i16::from_le_bytes([c[0], c[1]]))
                .collect();
            sink.audio(&pcm, rel)
        }
        0 => Ok(()),
        k => Err(Error::malformed(format!("surface: record kind {k}"))),
    }
}