tvpipe 0.2.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! Golden vectors of the machine interface's events
//! (`tests/vectors/machine/events.txt`), the contract with the Node SDK
//! (docs/rust-sdk-design.md ยง7). `TVPIPE_BLESS=1 cargo test events::`
//! rewrites the file from [`examples`].

use super::*;
use crate::log::{Level, Record};
use crate::net::discover::{AirPlayService, CastService, DeviceInfo, Endpoint};

const FILE: &str = "machine/events.txt";

/// Which vector shows `e`: every variant needs one, so a new variant does
/// not compile until it has an example below.
fn variant(e: &Event) -> &'static str {
    match e {
        Event::Hello(_) => "hello",
        Event::Log(_) => "log",
        Event::Receiver(_) => "receiver",
        Event::Player(_) => "player",
        Event::Viewer(_) => "viewer",
        Event::Tunnel(_) => "tunnel",
        Event::SharePage(_) => "share-page",
        Event::Capture(_) => "capture",
        Event::Connected(_) => "connected",
        Event::Started(_) => "started",
        Event::Keyframe => "keyframe",
        Event::Stats(_) => "stats",
        Event::Devices(_) => "devices",
        Event::PinRequired(_) => "pin-required",
        Event::Paired(_) => "paired",
        Event::Unpaired(_) => "unpaired",
        Event::End(_) => "end",
        Event::Error(_) => "error",
    }
}

fn stats(name: &str, n: u64) -> ReceiverStats {
    ReceiverStats {
        name: name.into(),
        pushed: n,
        dropped: 1,
        dropped_full: 2,
        dropped_waiting: 3,
    }
}

fn s(v: &str) -> String {
    v.to_owned()
}

/// The examples, in file order.
fn examples() -> Vec<(&'static str, Event)> {
    let receiver = |state, protocol, error: Option<&str>, code: Option<&str>| {
        Event::Receiver(ReceiverEvent {
            name: s("Living Room"),
            protocol,
            state,
            error: error.map(s),
            code: code.map(s),
        })
    };
    let device = DeviceInfo {
        name: s("Living Room"),
        addresses: vec!["192.0.2.10".parse().unwrap()],
        model: Some(s("AppleTV14,1")),
        airplay: Some(AirPlayService {
            endpoint: Endpoint {
                instance: s("Living Room"),
                host: s("Living-Room.local"),
                port: 7000,
                address: s("192.0.2.10:7000"),
            },
            model: Some(s("AppleTV14,1")),
            srcvers: Some(s("860.7.1")),
            osvers: Some(s("26.1")),
            deviceid: Some(s("AA:BB:CC:00:00:0A")),
            features: Some(s("0x4A7FDFD5,0x3C177FDE")),
            flags: Some(s("0x644")),
            pk: Some(s("00".repeat(32).as_str())),
            mirroring: true,
            password: false,
            pin: true,
            paired: Some(false),
        }),
        raop: None,
        cast: None,
    };
    let speaker = DeviceInfo {
        name: s("Kitchen speaker"),
        addresses: vec!["192.0.2.11".parse().unwrap()],
        model: None,
        airplay: None,
        raop: None,
        cast: Some(CastService {
            endpoint: Endpoint {
                instance: s("Google-Nest-Mini-0123"),
                host: s("0123.local"),
                port: 8009,
                address: s("192.0.2.11:8009"),
            },
            friendly_name: Some(s("Kitchen speaker")),
            model: Some(s("Google Nest Mini")),
            id: Some(s("0123456789abcdef")),
            capabilities: Some(199_172),
            status: None,
            video: false,
            group: false,
        }),
    };
    vec![
        (
            "hello",
            Event::Hello(Hello::new(1, "0.1.2", "test-pattern")),
        ),
        (
            "log",
            Event::Log(Record {
                level: Level::Info,
                module: s("browser::player"),
                message: s("browser: open http://127.0.0.1:8765/?t=โ€ฆ to watch"),
            }),
        ),
        (
            "log at every level",
            Event::Log(Record {
                level: Level::Trace,
                module: s("cast::rtcp"),
                message: s("quoted \"text\" and a tab\t"),
            }),
        ),
        (
            "receiver connected",
            receiver(
                ReceiverState::Connected,
                Some(Protocol::AirPlay),
                None,
                None,
            ),
        ),
        (
            "receiver failed while connecting",
            receiver(
                ReceiverState::Failed,
                Some(Protocol::AirPlay),
                Some("Living Room requires pairing: run `tvpipe pair \"Living Room\"`"),
                Some("pairing-required"),
            ),
        ),
        (
            "receiver lost",
            receiver(
                ReceiverState::Lost,
                None,
                Some("cast: no RTCP for 5 s"),
                None,
            ),
        ),
        (
            "receiver reconnected",
            receiver(ReceiverState::Reconnected, None, None, None),
        ),
        (
            "receiver gave up",
            receiver(
                ReceiverState::GaveUp,
                None,
                Some("gave up reconnecting after 120 s: connect: connection refused"),
                None,
            ),
        ),
        (
            "receiver ended",
            receiver(ReceiverState::Ended, None, None, None),
        ),
        (
            "receiver stopped",
            receiver(ReceiverState::Stopped, None, None, None),
        ),
        (
            "player",
            Event::Player(PlayerEvent {
                name: s("browser"),
                url: s("http://127.0.0.1:8765/?t=0123456789abcdef0123456789abcdef"),
            }),
        ),
        (
            "viewer",
            Event::Viewer(ViewerEvent {
                name: s("browser"),
                tab: 1,
                state: ViewerState::Connected,
                public: false,
            }),
        ),
        (
            "viewer over the tunnel leaves",
            Event::Viewer(ViewerEvent {
                name: s("browser"),
                tab: 2,
                state: ViewerState::Disconnected,
                public: true,
            }),
        ),
        (
            "tunnel to the player",
            Event::Tunnel(TunnelEvent {
                page: TunnelPage::Player,
                name: Some(s("browser")),
                provider: s("cloudflare"),
                url: s("https://example.trycloudflare.com/?t=abc&delay=300"),
            }),
        ),
        (
            "tunnel to the share page",
            Event::Tunnel(TunnelEvent {
                page: TunnelPage::Share,
                name: None,
                provider: s("opentunnel"),
                url: s("https://abc.opentunnel.example/?t=def"),
            }),
        ),
        (
            "share-page",
            Event::SharePage(SharePageEvent {
                url: s("http://127.0.0.1:52345/?t=0123"),
            }),
        ),
        (
            "capture",
            Event::Capture(CaptureEvent {
                surface: Some(s("window")),
                label: Some(s("Slides")),
                width: 1920,
                height: 1080,
                audio: true,
            }),
        ),
        (
            "capture without details",
            Event::Capture(CaptureEvent {
                surface: None,
                label: None,
                width: 640,
                height: 480,
                audio: false,
            }),
        ),
        (
            "connected",
            Event::Connected(ConnectedEvent {
                receivers: vec![s("Living Room"), s("browser")],
            }),
        ),
        (
            "started",
            Event::Started(Started {
                route: Route::Builtin,
                video: VideoStarted {
                    codec: s("h264"),
                    width: 1280,
                    height: 720,
                    fps: 30.0,
                },
                audio: vec![
                    AudioStarted {
                        codec: s("pcm-s16le"),
                        sample_rate: 44_100,
                        channels: 2,
                    },
                    AudioStarted {
                        codec: s("aac-lc"),
                        sample_rate: 48_000,
                        channels: 2,
                    },
                ],
                latency_ms: 300,
            }),
        ),
        (
            "started with a fractional rate and no audio",
            Event::Started(Started {
                route: Route::Passthrough,
                video: VideoStarted {
                    codec: s("h264"),
                    width: 1920,
                    height: 1080,
                    fps: 29.97002997002997,
                },
                audio: vec![],
                latency_ms: 100,
            }),
        ),
        ("keyframe", Event::Keyframe),
        (
            "stats",
            Event::Stats(Stats {
                elapsed_ms: 2005,
                video_frames: 60,
                audio_frames: 273,
                receivers: vec![stats("Living Room", 333)],
            }),
        ),
        (
            "devices",
            Event::Devices(DevicesEvent {
                devices: vec![device, speaker],
            }),
        ),
        (
            "pin-required",
            Event::PinRequired(PinRequest {
                name: s("Living Room"),
                address: s("192.0.2.10:7000"),
            }),
        ),
        (
            "paired",
            Event::Paired(Paired {
                name: s("Living Room"),
                model: Some(s("AppleTV14,1")),
                file: Some(s("/home/user/.config/tvpipe/credentials.txt")),
            }),
        ),
        (
            "paired in memory",
            Event::Paired(Paired {
                name: s("Living Room"),
                model: None,
                file: None,
            }),
        ),
        (
            "unpaired",
            Event::Unpaired(Unpaired {
                name: s("Living Room"),
                id: s("5B3C1A0E-0000-4000-8000-000000000001"),
            }),
        ),
        (
            "end of a stream",
            Event::End(EndInfo {
                reason: EndReason::Eof,
                receivers: Some(vec![
                    ReceiverEndInfo {
                        stats: stats("Living Room", 333),
                        end: ReceiverEnd::Stopped,
                        error: None,
                    },
                    ReceiverEndInfo {
                        stats: stats("Office", 12),
                        end: ReceiverEnd::Failed,
                        error: Some(s("cast: control channel ended")),
                    },
                ]),
            }),
        ),
        (
            "end of a command",
            Event::End(EndInfo::new(EndReason::Done)),
        ),
        (
            "end after every receiver left",
            Event::End(EndInfo {
                reason: EndReason::ReceiversGone,
                receivers: Some(vec![ReceiverEndInfo {
                    stats: stats("Living Room", 5),
                    end: ReceiverEnd::Ended,
                    error: None,
                }]),
            }),
        ),
        (
            "error",
            Event::Error(ErrorInfo::from(
                &Error::auth("Living Room requires pairing")
                    .with_code("pairing-required")
                    .with_device("Living Room"),
            )),
        ),
        (
            "error without code",
            Event::Error(ErrorInfo::from(&Error::not_found("no such file"))),
        ),
    ]
}

fn render() -> String {
    let mut out = String::from(
        "# The machine interface's events (docs/machine.md) as tvpipe writes them:\n\
         # one block per event type and interesting case. Generated from\n\
         # crates/tvpipe/src/events/tests.rs (TVPIPE_BLESS=1 cargo test -p tvpipe events::);\n\
         # the Node SDK's tests read it too.\n",
    );
    for (name, e) in examples() {
        out.push_str(&format!(
            "\nname = {name}\ntype = {}\njson = {}\n",
            variant(&e),
            e.to_json()
        ));
    }
    out
}

#[test]
fn golden_vectors() {
    let path = crate::util::vectors::path("tests/vectors").join(FILE);
    let want = render();
    if std::env::var_os("TVPIPE_BLESS").is_some() {
        std::fs::write(&path, &want).unwrap();
    }
    let have = std::fs::read_to_string(&path).unwrap_or_default();
    assert!(
        have == want,
        "{} is out of date: TVPIPE_BLESS=1 cargo test -p tvpipe events::",
        path.display()
    );
}

#[test]
fn every_variant_has_a_vector_and_round_trips() {
    let examples = examples();
    let mut types: Vec<&str> = examples.iter().map(|(_, e)| variant(e)).collect();
    types.dedup();
    assert_eq!(types.len(), 18, "{types:?}");
    for (name, e) in &examples {
        assert_eq!(e.type_name(), variant(e), "{name}");
        let back = Event::from_json(&e.to_json()).unwrap();
        assert_eq!(&back, e, "{name}");
    }
    for b in crate::util::vectors::load(FILE) {
        let e = Event::from_json(b.str("json")).unwrap();
        assert_eq!(e.type_name(), b.str("type"));
    }
}

#[test]
fn readers_ignore_unknown_fields_and_refuse_unknown_types() {
    let e = Event::from_json(r#"{"type":"keyframe","future":1}"#).unwrap();
    assert_eq!(e, Event::Keyframe);
    assert!(Event::from_json(r#"{"type":"teleport"}"#).is_err());
    assert!(Event::from_json(r#"{"type":"receiver","name":"x","state":"melted"}"#).is_err());
    assert!(Event::from_json("not json").is_err());
}