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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
//! The share server and input against a fake page written in Rust: it
//! speaks the page's side of the protocol over a real loopback WebSocket
//! and encodes with the built-in H.264 encoder (no browser, no ffmpeg).

use std::io::{Read, Write};
use std::sync::{Arc, Condvar, Mutex};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

use super::*;
use crate::codec::json::Value;
use crate::media::convert::Yuv420Frame;
use crate::media::h264enc;
use crate::net::websocket::{self, Message};
use crate::types::TrackKind;
use crate::util::ratio::Ratio;

/// What the fake page does.
#[derive(Clone)]
pub(crate) struct PageScript {
    pub frames: usize,
    pub fps: u32,
    pub capture: (u32, u32),
    pub audio: bool,
    /// Send `ended` after the frames (else just close the socket).
    pub send_ended: bool,
    /// Replace the first video record with this message.
    pub bad_record: Option<Vec<u8>>,
    /// Come as a browser over the tunnel would: this `Host`, and
    /// `https://HOST` as `Origin`.
    pub public_host: Option<String>,
}

impl Default for PageScript {
    fn default() -> PageScript {
        PageScript {
            frames: 30,
            fps: 30,
            capture: (640, 360),
            audio: true,
            send_ended: true,
            bad_record: None,
            public_host: None,
        }
    }
}

/// What the fake page saw.
#[derive(Default, Debug, Clone)]
pub(crate) struct PageSeen {
    pub config: Option<Value>,
    pub plan: Option<Value>,
    pub statuses: Vec<String>,
    pub keyframe_requests: usize,
    pub busy: bool,
    pub end: Option<String>,
    pub closed: bool,
    /// IDRs the page made because tvpipe asked.
    pub requested_idrs: usize,
    pub frames_sent: usize,
}

type Seen = Arc<(Mutex<PageSeen>, Condvar)>;

fn wait_for(seen: &Seen, timeout: Duration, f: impl Fn(&PageSeen) -> bool) -> bool {
    let (m, cv) = &**seen;
    let deadline = Instant::now() + timeout;
    let mut g = m.lock().unwrap();
    while !f(&g) {
        let left = deadline.saturating_duration_since(Instant::now());
        if left.is_zero() {
            return false;
        }
        g = cv.wait_timeout(g, left).unwrap().0;
    }
    true
}

/// A fake page: connects to `ws_url`, captures, waits for the plan and
/// streams `script.frames` frames in real time.
pub(crate) fn spawn_page(ws_url: String, script: PageScript) -> JoinHandle<PageSeen> {
    std::thread::spawn(move || run_page(&ws_url, script))
}

fn run_page(ws_url: &str, script: PageScript) -> PageSeen {
    let seen: Seen = Arc::default();
    let (mut rx, tx) = match &script.public_host {
        None => websocket::connect(
            ws_url,
            Duration::from_secs(10),
            websocket::Limits::default(),
        )
        .expect("page connect"),
        Some(host) => {
            let u = websocket::WsUrl::parse(ws_url).unwrap();
            let tcp = TcpStream::connect((u.host.as_str(), u.port)).unwrap();
            let url = websocket::WsUrl {
                host: host.clone(),
                port: 80,
                path: u.path,
            };
            let origin = format!("https://{host}");
            let (tcp, rest) =
                websocket::client_handshake(tcp, &url, &[("Origin", origin.as_str())]).unwrap();
            websocket::pair(
                tcp,
                rest,
                websocket::Role::Client,
                websocket::Limits::default(),
            )
            .unwrap()
        }
    };
    let s2 = seen.clone();
    let reader = std::thread::spawn(move || loop {
        let msg = rx.read_message();
        let (m, cv) = &*s2;
        let mut g = m.lock().unwrap();
        match msg {
            Ok(Message::Text(t)) => {
                let v = Value::parse(&t).unwrap();
                match v.get("type").and_then(Value::as_str).unwrap_or("") {
                    "config" => g.config = Some(v.clone()),
                    "plan" => g.plan = Some(v.clone()),
                    "status" => g
                        .statuses
                        .push(v.get("state").unwrap().as_str().unwrap().into()),
                    "keyframe" => g.keyframe_requests += 1,
                    "busy" => g.busy = true,
                    "end" => g.end = Some(v.get("reason").unwrap().as_str().unwrap().into()),
                    _ => {}
                }
            }
            Ok(Message::Binary(_)) => {}
            Ok(Message::Close(_)) | Err(_) => {
                g.closed = true;
                cv.notify_all();
                return;
            }
        }
        cv.notify_all();
    });
    // One clock for every fake page, like the real pages' wall clock
    // (timeOrigin + now()).
    static BASE: std::sync::OnceLock<Instant> = std::sync::OnceLock::new();
    let t0 = *BASE.get_or_init(Instant::now);
    let clock = move || 5_000_000 + t0.elapsed().as_micros() as u64;
    let hello = Value::object()
        .with("type", "hello")
        .with("version", 1)
        .with("clock", clock())
        .with("agent", "Mozilla/5.0 Chrome/131.0.0.0 Safari/537.36");
    tx.send_text(&hello.to_json()).unwrap();
    assert!(wait_for(&seen, Duration::from_secs(20), |s| s
        .config
        .is_some()
        || s.closed));
    let capture = Value::object()
        .with("type", "capture")
        .with("width", script.capture.0)
        .with("height", script.capture.1)
        .with("fps", script.fps)
        .with("audio", script.audio)
        .with("surface", "browser");
    let _ = tx.send_text(&capture.to_json());
    if !wait_for(&seen, Duration::from_secs(30), |s| {
        s.plan.is_some() || s.busy || s.closed
    }) {
        panic!("no plan");
    }
    let plan = seen.0.lock().unwrap().plan.clone();
    if let Some(plan) = plan {
        stream(&tx, &seen, &plan, &script, &clock);
    }
    if script.send_ended {
        let _ = tx.send_text(r#"{"type":"ended","reason":"the fake page is done"}"#);
    }
    let _ = tx.close(close_code::NORMAL, "");
    // tvpipe closes its end once the session is over.
    let _ = wait_for(&seen, Duration::from_secs(20), |s| s.closed);
    tx.with_stream(|s| {
        let _ = s.shutdown(std::net::Shutdown::Both);
    });
    let _ = reader.join();
    let out = seen.0.lock().unwrap().clone();
    out
}

fn stream(
    tx: &websocket::Sender<TcpStream>,
    seen: &Seen,
    plan: &Value,
    script: &PageScript,
    clock: &dyn Fn() -> u64,
) {
    let w = plan.get("width").unwrap().as_u32().unwrap();
    let h = plan.get("height").unwrap().as_u32().unwrap();
    let key_interval = plan.get("keyInterval").unwrap().as_u32().unwrap().max(1) as usize;
    let audio = plan.get("audio").filter(|a| !a.is_null()).map(|a| {
        (
            a.get("sampleRate").unwrap().as_u32().unwrap(),
            a.get("channels").unwrap().as_u32().unwrap(),
        )
    });
    let mut enc = h264enc::Encoder::new(h264enc::Config {
        width: w,
        height: h,
        fps: Ratio::integer(script.fps),
        bitrate_kbps: 500,
        gop: 0,
        threads: 1,
        ..h264enc::Config::default()
    })
    .unwrap();
    let mut cfg = vec![slrw::CONFIG_AVCC];
    cfg.extend_from_slice(&enc.avcc());
    tx.send_binary(&protocol::record(Kind::H264Config, clock(), &cfg))
        .unwrap();
    let start = Instant::now();
    let mut answered = 0;
    let mut since_key = usize::MAX - 1;
    for i in 0..script.frames {
        let due = start + Duration::from_micros(i as u64 * 1_000_000 / script.fps as u64);
        std::thread::sleep(due.saturating_duration_since(Instant::now()));
        let pts = clock();
        let requests = seen.0.lock().unwrap().keyframe_requests;
        let requested = requests > answered;
        answered = requests;
        let mut f = Yuv420Frame::new(w, h);
        for (k, y) in f.y.iter_mut().enumerate() {
            *y = ((k as u32 / 4 + i as u32 * 3) % 200 + 16) as u8;
        }
        let out = enc
            .encode(&f, requested || since_key + 1 >= key_interval)
            .unwrap();
        since_key = if out.keyframe { 0 } else { since_key + 1 };
        if requested && out.keyframe {
            seen.0.lock().unwrap().requested_idrs += 1;
        }
        let mut payload = vec![if out.keyframe { slrw::H264_KEYFRAME } else { 0 }];
        payload.extend_from_slice(&out.data);
        let msg = match (&script.bad_record, i) {
            (Some(bad), 1) => bad.clone(),
            _ => protocol::record(Kind::H264, pts, &payload),
        };
        if tx.send_binary(&msg).is_err() {
            return;
        }
        seen.0.lock().unwrap().frames_sent += 1;
        if let Some((rate, ch)) = audio {
            let n = (rate / script.fps) as usize;
            let pcm: Vec<u8> = (0..n * ch as usize)
                .flat_map(|k| (((k as f64 * 0.05).sin() * 4000.0) as i16).to_le_bytes())
                .collect();
            let _ = tx.send_binary(&protocol::record(Kind::Audio, pts, &pcm));
        }
    }
}

fn never() -> crate::util::stop::Stop {
    crate::util::stop::Stop::new()
}

fn config(receivers: &[&str]) -> PageConfig {
    PageConfig {
        fps: 30.0,
        audio: true,
        sample_rate: 48_000,
        channels: 2,
        receivers: receivers.iter().map(|s| s.to_string()).collect(),
    }
}

fn plan(w: u32, h: u32, key_interval: u32, audio: bool) -> SharePlan {
    SharePlan {
        width: w,
        height: h,
        fps: 30.0,
        bitrate_bps: 1_000_000,
        key_interval,
        audio: audio.then_some((48_000, 2)),
        content: crate::browser::layout::Rect {
            x: 0,
            y: 0,
            width: w,
            height: h,
        },
    }
}

fn input_opts(audio: bool) -> ShareInputOptions {
    ShareInputOptions {
        fps: Ratio::integer(30),
        audio: audio.then(|| "48000:2:s16le".parse().unwrap()),
        audio_families: if audio {
            vec![(AudioFamily::AirPlay, AudioOut::Pcm { frame_samples: 352 })]
        } else {
            Vec::new()
        },
    }
}

fn drain(input: &mut ShareInput) -> Vec<EncodedFrame> {
    let mut out = Vec::new();
    while let Some(f) = input.next_frame().unwrap() {
        out.push(f);
    }
    out
}

#[test]
fn streams_video_and_pcm_until_the_share_ends() {
    let server = ShareServer::bind(0).unwrap();
    server.configure(&config(&["TV"]));
    let page = spawn_page(server.ws_url(), PageScript::default());
    let cap = server.wait_capture(&never()).unwrap();
    assert_eq!((cap.width, cap.height, cap.audio), (640, 360, true));
    assert_eq!(cap.surface.as_deref(), Some("browser"));
    let mut input = server
        .start(&plan(320, 180, 15, true), input_opts(true), never())
        .unwrap();
    let info = input.info().clone();
    assert_eq!((info.video.width, info.video.height), (320, 180));
    assert_eq!(info.audio.len(), 1);
    assert!(
        input.describe().contains("Chrome 131"),
        "{}",
        input.describe()
    );
    let frames = drain(&mut input);
    let video: Vec<_> = frames
        .iter()
        .filter(|f| f.track == TrackKind::Video)
        .collect();
    let audio = frames.len() - video.len();
    assert_eq!(video.len(), 30);
    assert!(video[0].keyframe && video[15].keyframe && !video[1].keyframe);
    assert!(video.windows(2).all(|w| w[0].pts < w[1].pts));
    // 30 frames of 1600 samples = 48000 samples in 352-sample frames.
    assert!((130..=137).contains(&audio), "{audio}");
    Box::new(input).finish(true).unwrap();
    let seen = page.join().unwrap();
    assert_eq!(seen.frames_sent, 30);
    assert_eq!(
        seen.config.unwrap().get("sampleRate").unwrap().as_u32(),
        Some(48_000)
    );
    assert_eq!(seen.plan.unwrap().get("width").unwrap().as_u32(), Some(320));
    // The page ended the share itself: it gets no `end`, just the close.
    assert!(seen.closed && seen.end.is_none());
}

#[test]
fn keyframes_on_request() {
    let server = ShareServer::bind(0).unwrap();
    server.configure(&config(&[]));
    let page = spawn_page(
        server.ws_url(),
        PageScript {
            frames: 60,
            audio: false,
            ..PageScript::default()
        },
    );
    server.wait_capture(&never()).unwrap();
    // No periodic IDRs: every keyframe after the first was asked for.
    let mut input = server
        .start(&plan(160, 96, 100_000, false), input_opts(false), never())
        .unwrap();
    let mut keys = 0;
    let mut n = 0;
    let mut asked_at = None;
    while let Some(f) = input.next_frame().unwrap() {
        n += 1;
        keys += f.keyframe as usize;
        if n == 10 {
            assert!(input.request_keyframe());
            // Coalesced: a second request right away is not sent.
            assert!(input.request_keyframe());
            asked_at = Some(n);
        }
    }
    assert!(asked_at.is_some());
    assert_eq!(keys, 2, "the first IDR and the requested one");
    drop(input);
    let seen = page.join().unwrap();
    assert_eq!(seen.keyframe_requests, 1);
    assert_eq!(seen.requested_idrs, 1);
}

#[test]
fn a_second_page_is_busy_and_a_closed_page_ends_the_input() {
    let server = ShareServer::bind(0).unwrap();
    server.configure(&config(&[]));
    let first = spawn_page(
        server.ws_url(),
        PageScript {
            frames: 45,
            audio: false,
            send_ended: false,
            ..PageScript::default()
        },
    );
    server.wait_capture(&never()).unwrap();
    let second = spawn_page(server.ws_url(), PageScript::default());
    let seen2 = second.join().unwrap();
    assert!(seen2.busy && seen2.plan.is_none() && seen2.closed);
    let mut input = server
        .start(&plan(160, 96, 30, false), input_opts(false), never())
        .unwrap();
    // The page closes its socket without `ended`: the input ends.
    let frames = drain(&mut input);
    assert_eq!(frames.len(), 45);
    drop(input);
    let seen = first.join().unwrap();
    assert!(seen.closed);
}

#[test]
fn a_bad_record_fails_the_input() {
    let server = ShareServer::bind(0).unwrap();
    server.configure(&config(&[]));
    let page = spawn_page(
        server.ws_url(),
        PageScript {
            frames: 10,
            audio: false,
            bad_record: Some(protocol::record(Kind::H264, 1, &[0, 0, 0, 0, 9, 1])),
            ..PageScript::default()
        },
    );
    server.wait_capture(&never()).unwrap();
    let mut input = server
        .start(&plan(160, 96, 30, false), input_opts(false), never())
        .unwrap();
    let mut err = None;
    for _ in 0..20 {
        match input.next_frame() {
            Ok(Some(_)) => {}
            Ok(None) => break,
            Err(e) => {
                err = Some(e);
                break;
            }
        }
    }
    assert!(err.is_some(), "a truncated AVCC unit must fail the stream");
    drop(input);
    page.join().unwrap();
}

#[test]
fn ended_before_the_plan_lets_another_capture_take_over() {
    let server = ShareServer::bind(0).unwrap();
    server.configure(&config(&[]));
    let url = server.ws_url();
    // A page that captures, then stops before tvpipe sends a plan.
    let (mut rx, tx) =
        websocket::connect(&url, Duration::from_secs(10), websocket::Limits::default()).unwrap();
    tx.send_text(r#"{"type":"hello","version":1,"clock":1}"#)
        .unwrap();
    tx.send_text(r#"{"type":"capture","width":320,"height":200,"audio":false}"#)
        .unwrap();
    assert_eq!(server.wait_capture(&never()).unwrap().height, 200);
    tx.send_text(r#"{"type":"ended","reason":"changed my mind"}"#)
        .unwrap();
    // The server closes that connection; read until it does.
    while !matches!(rx.read_message(), Ok(Message::Close(_)) | Err(_)) {}
    let deadline = Instant::now() + Duration::from_secs(10);
    while server.shared.lock().capture.is_some() {
        assert!(Instant::now() < deadline);
        std::thread::sleep(Duration::from_millis(10));
    }
    let page = spawn_page(
        url,
        PageScript {
            frames: 5,
            audio: false,
            capture: (800, 600),
            ..PageScript::default()
        },
    );
    assert_eq!(server.wait_capture(&never()).unwrap().height, 600);
    let mut input = server
        .start(&plan(160, 120, 30, false), input_opts(false), never())
        .unwrap();
    assert_eq!(drain(&mut input).len(), 5);
    drop(input);
    page.join().unwrap();
}

#[test]
fn rejoin_mode_keeps_streaming_across_pages() {
    let server = ShareServer::bind(0).unwrap();
    server.set_rejoin(true);
    server.configure(&config(&[]));
    let handle = server.handle();
    assert_eq!(handle.ws_url(), server.ws_url());
    let url = server.ws_url();
    let script = PageScript {
        frames: 20,
        audio: true,
        send_ended: false,
        ..PageScript::default()
    };
    let first = spawn_page(url.clone(), script.clone());
    server.wait_capture(&never()).unwrap();
    let mut input = server
        .start(&plan(160, 96, 10, true), input_opts(true), never())
        .unwrap();
    let mut frames = Vec::new();
    // The first page streams and closes (a navigation): the input waits.
    let mut second = None;
    let deadline = Instant::now() + Duration::from_secs(30);
    let mut video = 0;
    while video < 40 {
        assert!(Instant::now() < deadline, "only {video} video frames");
        let f = input.next_frame().unwrap().expect("the input ended");
        if f.track == TrackKind::Video {
            video += 1;
        }
        frames.push(f);
        if video == 20 && second.is_none() {
            // A new document of the same tab shares again, with another
            // capture size: its plan is the same frame, its content placed.
            second = Some(spawn_page(
                url.clone(),
                PageScript {
                    capture: (320, 240),
                    ..script.clone()
                },
            ));
        }
    }
    handle.end("the browser was closed");
    let rest = drain(&mut input);
    frames.extend(rest);
    let video: Vec<_> = frames
        .iter()
        .filter(|f| f.track == TrackKind::Video)
        .collect();
    assert_eq!(video.len(), 40);
    assert!(
        video[0].keyframe && video[20].keyframe,
        "each page starts with an IDR"
    );
    assert!(
        video.windows(2).all(|w| w[0].pts < w[1].pts),
        "one timeline"
    );
    assert!(frames.iter().any(|f| f.track != TrackKind::Video));
    drop(input);
    let seen1 = first.join().unwrap();
    let seen2 = second.unwrap().join().unwrap();
    assert!(!seen1.busy && !seen2.busy);
    let p2 = seen2.plan.unwrap();
    assert_eq!(p2.get("width").unwrap().as_u32(), Some(160));
    let content = p2.get("content").unwrap();
    // 320x240 in 160x96: pillarboxed.
    assert_eq!(content.get("height").unwrap().as_u32(), Some(96));
    assert_eq!(content.get("width").unwrap().as_u32(), Some(128));
}

#[test]
fn a_failed_source_ends_the_wait_and_the_input() {
    let server = ShareServer::bind(0).unwrap();
    server.set_rejoin(true);
    server.configure(&config(&[]));
    let handle = server.handle();
    let h = handle.clone();
    let t = std::thread::spawn(move || {
        std::thread::sleep(Duration::from_millis(100));
        h.fail(Error::process("the browser died 4 times"));
    });
    assert!(server.wait_capture(&never()).is_none());
    t.join().unwrap();
    let e = server.failure().unwrap();
    assert_eq!(e.kind(), ErrorKind::Process);
    assert!(e.to_string().contains("died 4 times"));

    // Mid-stream: the input fails with it.
    let server = ShareServer::bind(0).unwrap();
    server.set_rejoin(true);
    server.configure(&config(&[]));
    let handle = server.handle();
    let page = spawn_page(
        server.ws_url(),
        PageScript {
            frames: 10,
            audio: false,
            send_ended: false,
            ..PageScript::default()
        },
    );
    server.wait_capture(&never()).unwrap();
    let mut input = server
        .start(&plan(160, 96, 30, false), input_opts(false), never())
        .unwrap();
    let mut n = 0;
    let err = loop {
        match input.next_frame() {
            Ok(Some(_)) => {
                n += 1;
                if n == 10 {
                    handle.fail(Error::process("gave up"));
                }
            }
            Ok(None) => panic!("ended instead of failing"),
            Err(e) => break e,
        }
    };
    assert!(err.to_string().contains("gave up"), "{err}");
    drop(input);
    page.join().unwrap();
}

fn http_get(addr: SocketAddr, target: &str, host: &str) -> (u16, String) {
    let mut s = TcpStream::connect(addr).unwrap();
    s.set_read_timeout(Some(Duration::from_secs(10))).unwrap();
    write!(
        s,
        "GET {target} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n\r\n"
    )
    .unwrap();
    let mut out = String::new();
    let _ = s.read_to_string(&mut out);
    let status = out.get(9..12).and_then(|c| c.parse().ok()).unwrap_or(0);
    (status, out)
}

#[test]
fn page_server_checks_token_and_host() {
    let server = ShareServer::bind(0).unwrap();
    let url = server.url().to_owned();
    let port: u16 = url
        .trim_start_matches("http://127.0.0.1:")
        .split('/')
        .next()
        .unwrap()
        .parse()
        .unwrap();
    let addr: SocketAddr = ([127, 0, 0, 1], port).into();
    let host = format!("127.0.0.1:{port}");
    let query = &url[url.find('?').unwrap()..];
    let (st, body) = http_get(addr, &format!("/{query}"), &host);
    assert_eq!(st, 200);
    assert!(body.contains("tvpipe share") && body.contains("/capture.js"));
    let (st, body) = http_get(addr, "/capture.js", &format!("localhost:{port}"));
    assert_eq!(st, 200);
    assert!(body.contains("tvpipeCapture"));
    assert!(body.contains("Content-Type: text/javascript"));
    // No token, a wrong token, a foreign Host (DNS rebinding).
    assert_eq!(http_get(addr, "/", &host).0, 403);
    assert_eq!(http_get(addr, "/?t=00", &host).0, 403);
    assert_eq!(
        http_get(addr, &format!("/{query}"), "evil.example:80").0,
        421
    );
    assert_eq!(http_get(addr, &format!("/ws{query}"), &host).0, 404);
    assert_eq!(http_get(addr, "/nothing", &host).0, 403);
    // The WebSocket needs the token too.
    let bad = format!("ws://127.0.0.1:{port}/ws?t=1234");
    assert!(
        websocket::connect(&bad, Duration::from_secs(10), websocket::Limits::default()).is_err()
    );
    assert!(websocket::connect(
        &server.ws_url(),
        Duration::from_secs(10),
        websocket::Limits::default()
    )
    .is_ok());
}

#[test]
fn browser_names() {
    let chrome = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
    let safari = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15";
    let firefox =
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:133.0) Gecko/20100101 Firefox/133.0";
    let edge = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0";
    assert_eq!(browser_name(chrome), "Chrome 131");
    assert_eq!(browser_name(safari), "Safari 18");
    assert_eq!(browser_name(firefox), "Firefox 133");
    assert_eq!(browser_name(edge), "Edg 131");
    assert_eq!(browser_name("curl/8"), "");
}

#[test]
fn square_pixel_check() {
    // The built-in encoder writes no aspect ratio: fine.
    let enc = h264enc::Encoder::new(h264enc::Config {
        width: 64,
        height: 48,
        ..h264enc::Config::default()
    })
    .unwrap();
    assert_eq!(square_pixels(&enc.avcc()), None);
    // An avcC around x264's SPS with SAR 4:3 (see h264's KATs) is flagged.
    let sps =
        crate::util::hex::decode("6764000bacd942847e5c3840000003004000000f03c50a6580").unwrap();
    let cfg = h264::AvcConfig::from_parameter_sets(
        vec![sps],
        vec![vec![0x68, 0xeb, 0xe3, 0xcb, 0x22, 0xc0]],
    )
    .unwrap()
    .to_bytes()
    .unwrap();
    assert_eq!(square_pixels(&cfg), Some((4, 3)));
}

#[test]
fn reframes_short_lengths() {
    let p = [1u8, 0, 3, 0x65, 0xaa, 0xbb, 0, 1, 0x41];
    assert_eq!(
        reframe(&p, 2).unwrap(),
        [1, 0, 0, 0, 3, 0x65, 0xaa, 0xbb, 0, 0, 0, 1, 0x41]
    );
    assert!(reframe(&[1, 0, 9, 0x65], 2).is_err());
}

#[test]
fn silent_sound_is_noted_once() {
    let stats = |kbps: f64, peak: Option<f64>| {
        let v = Value::object().with("audioKbps", kbps);
        match peak {
            Some(p) => v.with("audioPeak", p),
            None => v,
        }
    };
    let mut s = Sound::default();
    for _ in 0..SILENT_REPORTS - 1 {
        assert!(!s.stats(&stats(1536.0, Some(0.0))));
    }
    // No sound flowing, or an older page without the field: not counted.
    assert!(!s.stats(&stats(0.0, Some(0.0))));
    assert!(!s.stats(&stats(1536.0, None)));
    assert!(s.stats(&stats(1536.0, Some(0.0))));
    assert!(!s.stats(&stats(1536.0, Some(0.0))));

    // Sound heard once: silence later is fine.
    let mut s = Sound::default();
    assert!(!s.stats(&stats(1536.0, Some(0.25))));
    for _ in 0..2 * SILENT_REPORTS {
        assert!(!s.stats(&stats(1536.0, Some(0.0))));
    }
}