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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
//! `tvpipe share`: the page server and the input it feeds (protocol in
//! docs/share-protocol.md).
//!
//! [`ShareServer`] serves the share page, `capture.js` and the page's
//! fonts on 127.0.0.1 and accepts the page's WebSocket at `/ws`. The page
//! and the socket need the session's random token (`?t=`), every request
//! a loopback `Host` (no DNS rebinding). Several pages may connect (a
//! reload, a second tab); the first that starts a capture becomes the
//! source, later ones are told `busy`. The session then sends the page its encode `plan` and turns
//! the server into a [`ShareInput`], which feeds the page's records into
//! the encoded-SLRW state (`media::input::EncodedState`): passthrough
//! H.264, PCM for AirPlay, `aacenc` AAC for Cast. Keyframe requests go
//! back to the page as `keyframe` messages. The input ends when the share
//! ends or the page goes away.
//!
//! With [`ShareServer::set_rejoin`] (the headless browser, W13) a source
//! page that goes away mid-stream does not end the input: tvpipe's
//! browser re-shares after a navigation, a reload or a crash, and the
//! newest page that captures becomes the source and gets the same plan.
//! The stream goes on with a gap; the page's clock is wall-clock time, so
//! the timeline stays continuous. [`ShareHandle::fail`] ends it instead
//! (the browser cannot be brought back).

use std::net::{SocketAddr, TcpStream};
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, SyncSender};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::time::{Duration, Instant};

use crate::browser::access::{Access, Via};
use crate::browser::fonts;
use crate::browser::protocol::{self, CaptureInfo, PageConfig, PageMsg, Remote, SharePlan};
use crate::codec::json::Value;
use crate::error::{Error, ErrorKind, Result};
use crate::media::h264;
use crate::media::input::{
    AudioOut, EncodedState, Input, Stop, KEYFRAME_REQUEST_INTERVAL, STOP_POLL,
};
use crate::media::slrw::{self, Kind, Packet};
use crate::net::http::Request;
use crate::net::httpd::{self, Reply};
use crate::net::tunnel::{self, Tunnel};
use crate::net::websocket::{self, close_code, Message, Role, Sender};
use crate::opts::{RawAudioSpec, RawVideoSpec};
use crate::types::StreamInfo;
use crate::types::{AudioFamily, EncodedFrame};

pub const PAGE_HTML: &str = include_str!("share.html");
/// The page `tvpipe screen`'s browser shows: capture.js and a line of
/// text; tvpipe starts the capture in it (`browser::screen`).
pub const SCREEN_HTML: &str = include_str!("screen.html");
/// The capture script: self-contained, also injected into pages by the
/// headless-browser mode (W13).
pub const CAPTURE_JS: &str = include_str!("capture.js");

/// Records queued between the page's socket and the session.
const EVENT_QUEUE: usize = 256;
/// How often a page connection checks for shutdown.
const CLIENT_POLL: Duration = Duration::from_millis(200);
/// How long the page may take from the plan to its first keyframe.
const FIRST_KEYFRAME_TIMEOUT: Duration = Duration::from_secs(15);
const LAG_REPORT: Duration = Duration::from_secs(5);
/// Video records left out of the final lag summary (encoder warm-up).
const LAG_WARMUP: u64 = 30;

/// What happened on the source page's socket.
enum Event {
    Packet(Packet),
    Stats(Value),
    Ended(String),
    Failed(Error),
    /// Rejoin mode: the source page went away (why); another may come.
    Detached(String),
    /// Rejoin mode: page `id` captured and is the source now.
    Attached(u64),
}

#[derive(Default)]
struct State {
    next_id: u64,
    clients: Vec<(u64, Sender<TcpStream>)>,
    /// The `config` message, once the session knows it.
    config: Option<String>,
    /// The last `status` message, for pages that connect later.
    status: Option<String>,
    /// The page whose capture is the source.
    active: Option<u64>,
    capture: Option<CaptureInfo>,
    /// The active page's clock at its `hello`, and when that arrived.
    clock: PageClock,
    agent: String,
    /// The plan went out: records flow to the input.
    streaming: bool,
    shutdown: bool,
    /// A source page that goes away may be replaced (see the module docs).
    rejoin: bool,
    /// Why the source cannot come back ([`ShareHandle::fail`]).
    failed: Option<(ErrorKind, String)>,
    /// The source is gone for good, without an error ([`ShareHandle::end`]).
    ended: bool,
}

struct Shared {
    access: Access,
    state: Mutex<State>,
    cv: Condvar,
    events: SyncSender<Event>,
}

impl Shared {
    fn lock(&self) -> MutexGuard<'_, State> {
        self.state.lock().unwrap_or_else(|e| e.into_inner())
    }

    fn broadcast(&self, st: &State, text: &str) {
        for (_, s) in &st.clients {
            let _ = s.send_text(text);
        }
    }

    fn handle(self: &Arc<Self>, req: &Request) -> Reply {
        let (via, path, token_ok) = match self.access.check(req, &["/capture.js", "/favicon.ico"]) {
            Ok(c) => (c.via, c.path, c.token_ok),
            Err(r) => return Reply::Response(r),
        };
        match path {
            "/capture.js" => Reply::Response(httpd::response(
                200,
                "OK",
                "text/javascript; charset=utf-8",
                CAPTURE_JS.as_bytes().to_vec(),
            )),
            "/favicon.ico" => Reply::Response(httpd::response(
                204,
                "No Content",
                "image/x-icon",
                Vec::new(),
            )),
            p if p.starts_with("/fonts/") => fonts::reply(p),
            _ if !token_ok => Reply::Response(httpd::error_response(
                403,
                "Forbidden (open the URL tvpipe printed)",
            )),
            "/" => Reply::Response(httpd::response(
                200,
                "OK",
                "text/html; charset=utf-8",
                PAGE_HTML.as_bytes().to_vec(),
            )),
            // tvpipe's own capture of this machine's screen: never over a
            // tunnel.
            "/screen" if via == Via::Local => Reply::Response(httpd::response(
                200,
                "OK",
                "text/html; charset=utf-8",
                SCREEN_HTML.as_bytes().to_vec(),
            )),
            // Over the tunnel only the page's own origin (here any page
            // may connect: `share URL` injects the script into any site).
            "/ws"
                if via == Via::Public && !self.access.origin_ok(via, req.headers.get("origin")) =>
            {
                Reply::Response(httpd::error_response(403, "Forbidden origin"))
            }
            "/ws" if websocket::is_upgrade(req) => match websocket::server_response(req) {
                Ok(resp) => {
                    let me = self.clone();
                    // Over the tunnel: who it is, for the confirmation.
                    let remote = (via == Via::Public)
                        .then(|| self.access.remote_peer(httpd::current_peer()));
                    Reply::Upgrade(
                        resp,
                        Box::new(move |tcp, rest| me.client(tcp, rest, remote)),
                    )
                }
                Err(e) => {
                    debug!("share: bad upgrade: {e}");
                    let mut r = if e.kind() == ErrorKind::Unsupported {
                        httpd::error_response(426, "Upgrade Required")
                    } else {
                        httpd::error_response(400, "Bad Request")
                    };
                    r.headers.add("Sec-WebSocket-Version", "13");
                    Reply::Response(r)
                }
            },
            _ => Reply::Response(httpd::error_response(404, "Not Found")),
        }
    }

    /// One page connection, on its own thread until it closes.
    /// `remote`: it came over the tunnel (with the visitor's address if
    /// known).
    fn client(self: Arc<Self>, tcp: TcpStream, rest: Vec<u8>, remote: Option<Option<String>>) {
        if tcp.set_read_timeout(Some(CLIENT_POLL)).is_err() {
            return;
        }
        let limits = websocket::Limits {
            max_message: protocol::MAX_MESSAGE,
        };
        let Ok((mut rx, tx)) = websocket::pair(tcp, rest, Role::Server, limits) else {
            return;
        };
        let id = {
            let mut st = self.lock();
            st.next_id += 1;
            let id = st.next_id;
            st.clients.push((id, tx.clone()));
            for m in [&st.config, &st.status].into_iter().flatten() {
                let _ = tx.send_text(m);
            }
            id
        };
        debug!(
            "share: page {id} connected{}",
            if remote.is_some() {
                " over the tunnel"
            } else {
                ""
            }
        );
        let mut clock = None;
        let mut page_agent = String::new();
        let why = loop {
            if self.lock().shutdown {
                break "tvpipe stopped".to_owned();
            }
            match rx.read_message() {
                Err(e) if e.kind() == ErrorKind::Timeout => {}
                Err(e) => break format!("the page's connection failed: {e}"),
                Ok(Message::Close(_)) => break "the page closed".to_owned(),
                Ok(Message::Text(t)) => match protocol::parse_page(&t) {
                    Ok(PageMsg::Hello {
                        version,
                        clock_us,
                        agent,
                    }) => {
                        debug!("share: page {id}: {agent} (protocol {version})");
                        if version != protocol::VERSION {
                            warn!(
                                "share: the page speaks protocol {version}, tvpipe {}",
                                protocol::VERSION
                            );
                        }
                        clock = Some((Instant::now(), clock_us));
                        page_agent.clone_from(&agent);
                        self.lock().agent = agent;
                    }
                    Ok(PageMsg::Capture(mut info)) => {
                        info.remote = remote.clone().map(|peer| Remote {
                            peer,
                            agent: page_agent.clone(),
                        });
                        let mut st = self.lock();
                        if st.rejoin && st.active != Some(id) {
                            // The newest capture is the source (the old
                            // page is navigating away).
                            info!(
                                "share: page {id} captures {}x{}{}",
                                info.width,
                                info.height,
                                if info.audio { " with audio" } else { "" }
                            );
                            st.active = Some(id);
                            st.capture = Some(info);
                            st.clock = clock;
                            self.cv.notify_all();
                            if st.streaming {
                                drop(st);
                                if self.events.send(Event::Attached(id)).is_err() {
                                    break "tvpipe stopped".to_owned();
                                }
                            }
                        } else if st.active.is_none() && !st.streaming {
                            info!(
                                "share: capturing {} {}x{}{}{}",
                                info.surface.as_deref().unwrap_or("a surface"),
                                info.width,
                                info.height,
                                info.fps.map(|f| format!("@{f:.0}")).unwrap_or_default(),
                                if info.audio { " with audio" } else { "" }
                            );
                            st.active = Some(id);
                            st.capture = Some(info);
                            st.clock = clock;
                            self.cv.notify_all();
                        } else if st.active != Some(id) {
                            drop(st);
                            let _ = tx.send_text(&protocol::busy());
                            let _ = tx.close(close_code::POLICY, "another page is sharing");
                            break "busy".to_owned();
                        }
                    }
                    Ok(PageMsg::Stats(v)) => {
                        if self.is_source(id) {
                            let _ = self.events.try_send(Event::Stats(v));
                        }
                    }
                    Ok(PageMsg::Ended(reason)) => {
                        if self.is_source(id) {
                            break reason;
                        }
                    }
                    Ok(PageMsg::Log { level, message }) => match level.as_str() {
                        "error" | "warn" => warn!("share page: {message}"),
                        _ => debug!("share page: {message}"),
                    },
                    Ok(PageMsg::Unknown(t)) => trace!("share: page {id}: unknown message {t:?}"),
                    Err(e) => debug!("share: page {id}: {e}"),
                },
                Ok(Message::Binary(b)) => {
                    let streaming = {
                        let st = self.lock();
                        st.streaming && st.active == Some(id)
                    };
                    if !streaming {
                        continue;
                    }
                    let ev = match protocol::parse_record(&b) {
                        Ok(p) => Event::Packet(p),
                        Err(e) => {
                            let _ = tx.close(close_code::UNSUPPORTED, e.message());
                            let _ = self.events.send(Event::Failed(e));
                            break "bad record".to_owned();
                        }
                    };
                    // Blocks while the session is behind: backpressure
                    // into the socket, where the page sees bufferedAmount
                    // grow and drops frames.
                    if self.events.send(ev).is_err() {
                        break "tvpipe stopped".to_owned();
                    }
                }
            }
        };
        debug!("share: page {id}: {why}");
        let mut st = self.lock();
        st.clients.retain(|(c, _)| *c != id);
        if st.active == Some(id) {
            if st.streaming && st.rejoin {
                st.active = None;
                st.capture = None;
                let _ = self.events.try_send(Event::Detached(why));
            } else if st.streaming {
                let _ = self.events.try_send(Event::Ended(why));
            } else {
                // Not started yet: another capture may take its place.
                st.active = None;
                st.capture = None;
                self.cv.notify_all();
            }
        }
        drop(st);
        let _ = tx.close(close_code::NORMAL, "");
        tx.with_stream(|s| {
            let _ = s.shutdown(std::net::Shutdown::Both);
        });
    }

    fn is_source(&self, id: u64) -> bool {
        self.lock().active == Some(id)
    }
}

/// The share page server, until a capture starts streaming.
pub struct ShareServer {
    http: Option<httpd::Server>,
    shared: Arc<Shared>,
    events: Receiver<Event>,
    url: String,
    end_reason: String,
    /// `--tunnel` ([`ShareServer::expose`]).
    tunnel: Option<Box<dyn Tunnel>>,
}

impl ShareServer {
    /// Listens on 127.0.0.1 (`port` 0: any free port).
    pub fn bind(port: u16) -> Result<ShareServer> {
        let token = crate::util::hex::encode(&crate::crypto::ring::random_array::<16>()?);
        // The port is only known after binding; the handler reads it from
        // `Shared`, which is built around a placeholder first.
        let listener_addr: SocketAddr = (std::net::Ipv4Addr::LOCALHOST, port).into();
        let (tx, events) = mpsc::sync_channel(EVENT_QUEUE);
        let cell: Arc<Mutex<Option<Arc<Shared>>>> = Arc::new(Mutex::new(None));
        let c = cell.clone();
        let handler: httpd::Handler = Arc::new(move |req: &Request| {
            let shared = c.lock().unwrap_or_else(|e| e.into_inner()).clone();
            match shared {
                Some(s) => s.handle(req),
                None => Reply::Response(httpd::error_response(503, "Service Unavailable")),
            }
        });
        let http = httpd::Server::bind(listener_addr, handler)?;
        let port = http.local_addr().port();
        let shared = Arc::new(Shared {
            access: Access::new("share", port, token.clone()),
            state: Mutex::new(State::default()),
            cv: Condvar::new(),
            events: tx,
        });
        *cell.lock().unwrap_or_else(|e| e.into_inner()) = Some(shared.clone());
        Ok(ShareServer {
            http: Some(http),
            shared,
            events,
            url: format!("http://127.0.0.1:{port}/?t={token}"),
            end_reason: "tvpipe stopped".into(),
            tunnel: None,
        })
    }

    /// Also serves the page on a public URL (docs/tunnels.md) that lets
    /// people in for `ttl`, and returns it: anyone who has it can ask to
    /// share to the receivers.
    pub fn expose(
        &mut self,
        provider: tunnel::Provider,
        ttl: Option<Duration>,
        stop: &dyn Fn() -> bool,
    ) -> Result<String> {
        let t = tunnel::start(self.shared.access.port(), provider, stop)?;
        let url = self.publish(t.host(), ttl, t.peer_lookup())?;
        self.tunnel = Some(t);
        Ok(url)
    }

    /// Accepts `host` as the page's public host (what [`expose`]
    /// does once the tunnel is up; tests call it directly).
    ///
    /// [`expose`]: Self::expose
    pub fn publish(
        &self,
        host: &str,
        ttl: Option<Duration>,
        peers: Option<tunnel::PeerLookup>,
    ) -> Result<String> {
        let token = self.shared.access.publish(host, ttl, peers)?;
        Ok(format!("https://{host}/?t={token}"))
    }

    /// Whether the capture [`wait_capture`](Self::wait_capture) returned
    /// is still there (its page did not go away).
    pub fn capture_pending(&self) -> bool {
        self.shared.lock().capture.is_some()
    }

    /// Turns the capture [`wait_capture`](Self::wait_capture) returned
    /// down: its page is told `reason` and closed, and the next capture
    /// may take its place.
    pub fn decline(&self, reason: &str) {
        let mut st = self.shared.lock();
        if st.streaming {
            return;
        }
        if let Some(id) = st.active.take() {
            st.capture = None;
            if let Some((_, s)) = st.clients.iter().find(|(c, _)| *c == id) {
                let _ = s.send_text(&protocol::end(reason));
                let _ = s.close(close_code::POLICY, reason);
            }
        }
    }

    /// Keep streaming when the source page goes away, until another page
    /// captures (see the module docs).
    pub fn set_rejoin(&self, on: bool) {
        self.shared.lock().rejoin = on;
    }

    /// A handle that can end the session from another thread.
    pub fn handle(&self) -> ShareHandle {
        ShareHandle(self.shared.clone())
    }

    /// Why [`ShareHandle::fail`] ended the session, if it did.
    pub fn failure(&self) -> Option<Error> {
        self.shared
            .lock()
            .failed
            .as_ref()
            .map(|(kind, msg)| Error::new(*kind, msg.clone()))
    }

    /// The page's URL (with the token).
    pub fn url(&self) -> &str {
        &self.url
    }

    /// The WebSocket URL a capture script connects to.
    pub fn ws_url(&self) -> String {
        format!(
            "ws://127.0.0.1:{}/ws?t={}",
            self.shared.access.port(),
            self.shared.access.token()
        )
    }

    /// The page `tvpipe screen`'s browser opens (with the token).
    pub fn screen_url(&self) -> String {
        format!(
            "http://127.0.0.1:{}/screen?t={}",
            self.shared.access.port(),
            self.shared.access.token()
        )
    }

    /// Tells pages what to capture (sent now and to every later page).
    pub fn configure(&self, config: &PageConfig) {
        let text = config.to_json();
        let mut st = self.shared.lock();
        self.shared.broadcast(&st, &text);
        st.config = Some(text);
    }

    /// Shows `state`/`message` on every page.
    pub fn set_status(&self, state: &str, message: &str, receivers: &[String]) {
        let text = protocol::status(state, message, receivers);
        let mut st = self.shared.lock();
        self.shared.broadcast(&st, &text);
        st.status = Some(text);
    }

    /// What pages see when the server goes away (default: "tvpipe
    /// stopped").
    pub fn set_end_reason(&mut self, reason: &str) {
        self.end_reason = reason.to_owned();
    }

    /// Waits until a page starts a capture; `None` on a stop request (or
    /// a [`failure`](Self::failure)).
    pub fn wait_capture(&self, stop: &Stop) -> Option<CaptureInfo> {
        let mut st = self.shared.lock();
        loop {
            if stop.requested() || st.failed.is_some() || st.ended {
                return None;
            }
            if let Some(c) = &st.capture {
                return Some(c.clone());
            }
            st = self
                .shared
                .cv
                .wait_timeout(st, STOP_POLL)
                .unwrap_or_else(|e| e.into_inner())
                .0;
        }
    }

    /// Sends the plan to the source page and waits for its first
    /// keyframe. On failure the pages are told why.
    pub fn start(
        mut self,
        plan: &SharePlan,
        opts: ShareInputOptions,
        stop: Stop,
    ) -> Result<ShareInput> {
        let claimed = self.claim().and_then(|(sender, clock, agent)| {
            sender.send_text(&plan.to_json())?;
            Ok((sender, clock, agent))
        });
        let (sender, clock, agent) = match claimed {
            Ok(v) => v,
            Err(e) => {
                self.set_end_reason(&e.to_string());
                return Err(e);
            }
        };
        let video = RawVideoSpec {
            size: crate::opts::Size {
                width: plan.width,
                height: plan.height,
            },
            fps: opts.fps,
            pix_fmt: crate::media::slrw::ENCODED_PIX_FMT.into(),
        };
        let st = match EncodedState::new(video, opts.audio, &opts.audio_families, None) {
            Ok(st) => st,
            Err(e) => {
                self.set_end_reason(&e.to_string());
                return Err(e);
            }
        };
        let mut input = ShareInput {
            server: self,
            plan: plan.clone(),
            sender,
            st,
            info: StreamInfo {
                video: crate::types::VideoInfo {
                    codec: crate::types::VideoCodec::H264,
                    width: 0,
                    height: 0,
                    fps: opts.fps,
                    avcc: Vec::new(),
                },
                audio: Vec::new(),
            },
            stop,
            last_key_request: None,
            length_size: 4,
            clock,
            lag: Lag::default(),
            page_stats: None,
            sound: Sound::default(),
            agent,
            last_video: None,
            gap_since: None,
        };
        match input.first_keyframe() {
            Ok(()) => Ok(input),
            Err(e) => {
                input.server.set_end_reason(&e.to_string());
                Err(e)
            }
        }
    }

    /// Marks the source page as streaming: its sender, clock and agent.
    fn claim(&self) -> Result<(Sender<TcpStream>, PageClock, String)> {
        let mut st = self.shared.lock();
        let active = st
            .active
            .ok_or_else(|| Error::closed("share: the capture ended before streaming started"))?;
        let sender = st
            .clients
            .iter()
            .find(|(id, _)| *id == active)
            .map(|(_, s)| s.clone())
            .ok_or_else(|| Error::closed("share: the page went away"))?;
        st.streaming = true;
        Ok((sender, st.clock, st.agent.clone()))
    }
}

impl Drop for ShareServer {
    fn drop(&mut self) {
        let end = protocol::end(&self.end_reason);
        {
            let mut st = self.shared.lock();
            st.shutdown = true;
            for (_, s) in &st.clients {
                let _ = s.send_text(&end);
                let _ = s.close(close_code::GOING_AWAY, &self.end_reason);
            }
            self.shared.cv.notify_all();
        }
        if let Some(mut h) = self.http.take() {
            h.shutdown();
        }
    }
}

/// Whether the page's sound was ever more than silence: a capture whose
/// sound stays digital silence is noted once (on macOS that is how a
/// browser without the "System Audio Recording" permission sounds).
#[derive(Debug, Default)]
struct Sound {
    /// Stats reports with sound flowing, all of it silence.
    silent: u32,
    heard: bool,
    noted: bool,
}

/// Silent stats reports (2 s each) before the note.
const SILENT_REPORTS: u32 = 5;

impl Sound {
    /// Takes a page `stats` message; `true` when the note goes out now.
    fn stats(&mut self, v: &Value) -> bool {
        let flowing = v.get("audioKbps").and_then(Value::as_f64).unwrap_or(0.0) > 0.0;
        let Some(peak) = v.get("audioPeak").and_then(Value::as_f64) else {
            return false;
        };
        if !flowing || self.heard || self.noted {
            return false;
        }
        if peak > 0.0 {
            self.heard = true;
            return false;
        }
        self.silent += 1;
        if self.silent < SILENT_REPORTS {
            return false;
        }
        self.noted = true;
        info!(
            "share: the captured sound has been silent for {} s (nothing playing?{})",
            SILENT_REPORTS * 2,
            if cfg!(target_os = "macos") {
                " On macOS the browser needs the System Audio Recording permission: \
                 System Settings > Privacy & Security > Screen & System Audio Recording"
            } else {
                ""
            }
        );
        true
    }
}

/// A page's clock (µs) and when we read it.
type PageClock = Option<(Instant, u64)>;

/// Ends a share from another thread (the headless browser's keeper).
#[derive(Clone)]
pub struct ShareHandle(Arc<Shared>);

impl ShareHandle {
    /// The source cannot come back: the input fails with `e` (or the
    /// wait for a capture ends; see [`ShareServer::failure`]).
    pub fn fail(&self, e: Error) {
        let mut st = self.0.lock();
        if st.failed.is_none() {
            st.failed = Some((e.kind(), e.message().to_owned()));
        }
        self.0.cv.notify_all();
        drop(st);
        let _ = self.0.events.try_send(Event::Failed(e));
    }

    /// The source is gone for good (the user closed the browser): the
    /// input ends like a file, or the wait for a capture ends.
    pub fn end(&self, reason: &str) {
        let mut st = self.0.lock();
        st.ended = true;
        self.0.cv.notify_all();
        drop(st);
        let _ = self.0.events.try_send(Event::Ended(reason.to_owned()));
    }

    /// The WebSocket URL a capture script connects to.
    pub fn ws_url(&self) -> String {
        format!(
            "ws://127.0.0.1:{}/ws?t={}",
            self.0.access.port(),
            self.0.access.token()
        )
    }

    /// Whether pages get their `config` at once (the session knows what
    /// to capture).
    pub fn configured(&self) -> bool {
        self.0.lock().config.is_some()
    }
}

/// The input side of [`ShareServer::start`].
pub struct ShareInputOptions {
    /// Frame rate for timing when the SPS has none.
    pub fps: crate::util::ratio::Ratio,
    /// The PCM the page sends (s16le), if any.
    pub audio: Option<RawAudioSpec>,
    pub audio_families: Vec<(AudioFamily, AudioOut)>,
}

/// Capture-to-tvpipe delay of video records (page clock vs. arrival).
#[derive(Default)]
struct Lag {
    since: Option<Instant>,
    n: u64,
    sum_ms: f64,
    max_ms: f64,
    seen: u64,
    total_n: u64,
    total_sum_ms: f64,
    total_max_ms: f64,
}

impl Lag {
    fn add(&mut self, ms: f64) {
        let now = Instant::now();
        let since = *self.since.get_or_insert(now);
        self.n += 1;
        self.sum_ms += ms;
        self.max_ms = self.max_ms.max(ms);
        self.seen += 1;
        // The totals leave out the encoder's warm-up.
        if self.seen > LAG_WARMUP {
            self.total_n += 1;
            self.total_sum_ms += ms;
            self.total_max_ms = self.total_max_ms.max(ms);
        }
        if now.duration_since(since) >= LAG_REPORT {
            debug!(
                "share: {:.1} fps from the page, capture to tvpipe avg {:.1} ms, max {:.1} ms",
                self.n as f64 / now.duration_since(since).as_secs_f64(),
                self.sum_ms / self.n as f64,
                self.max_ms
            );
            *self = Lag {
                since: Some(now),
                seen: self.seen,
                total_n: self.total_n,
                total_sum_ms: self.total_sum_ms,
                total_max_ms: self.total_max_ms,
                ..Lag::default()
            };
        }
    }
}

/// H.264 + PCM from the share page.
pub struct ShareInput {
    server: ShareServer,
    /// What every source page gets (rejoin mode: again for each).
    plan: SharePlan,
    sender: Sender<TcpStream>,
    st: EncodedState,
    info: StreamInfo,
    stop: Stop,
    last_key_request: Option<Instant>,
    /// The page's AVCC length size (from its avcC).
    length_size: usize,
    clock: PageClock,
    lag: Lag,
    page_stats: Option<Value>,
    sound: Sound,
    agent: String,
    /// When the last video record came, and since when there is none
    /// (rejoin mode: the page went away).
    last_video: Option<Instant>,
    gap_since: Option<Instant>,
}

impl ShareInput {
    fn first_keyframe(&mut self) -> Result<()> {
        let deadline = Instant::now() + FIRST_KEYFRAME_TIMEOUT;
        while !self.st.started() {
            if self.st.ended() {
                return Err(Error::closed(
                    "share: the capture ended before the first keyframe",
                ));
            }
            if Instant::now() >= deadline {
                return Err(Error::timeout(format!(
                    "share: no keyframe from the page within {} s (can the browser encode H.264?)",
                    FIRST_KEYFRAME_TIMEOUT.as_secs()
                )));
            }
            if !self.pump()? {
                return Err(Error::closed("share: stopped before the first keyframe"));
            }
        }
        self.info = self.st.stream_info()?;
        if let Some(sar) = square_pixels(&self.info.video.avcc) {
            warn!(
                "share: the browser's H.264 says pixels are {}:{}, not square; \
                 receivers may stretch the picture",
                sar.0, sar.1
            );
        }
        Ok(())
    }

    /// Waits up to [`STOP_POLL`] for one event and handles it; `false` on
    /// a stop request.
    fn pump(&mut self) -> Result<bool> {
        if self.stop.requested() {
            return Ok(false);
        }
        match self.server.events.recv_timeout(STOP_POLL) {
            Ok(Event::Packet(p)) => self.packet(p)?,
            Ok(Event::Stats(v)) => {
                trace!("share: page stats {v}");
                self.sound.stats(&v);
                self.page_stats = Some(v);
            }
            Ok(Event::Ended(reason)) => {
                info!("share: {reason}");
                self.server.set_end_reason("the share ended");
                self.st.end();
            }
            Ok(Event::Failed(e)) => return Err(e),
            Ok(Event::Detached(why)) => {
                info!("share: the page stopped sharing ({why}); waiting for it to share again");
                self.gap_since = self.last_video.or(Some(Instant::now()));
            }
            Ok(Event::Attached(id)) => self.attach(id)?,
            Err(RecvTimeoutError::Timeout) => {
                if let Some(e) = self.server.failure() {
                    return Err(e);
                }
            }
            Err(RecvTimeoutError::Disconnected) => self.st.end(),
        }
        Ok(true)
    }

    /// Rejoin mode: page `id` captured; it gets the plan and is the
    /// source from now on.
    fn attach(&mut self, id: u64) -> Result<()> {
        let (sender, clock, agent, capture) = {
            let st = self.server.shared.lock();
            if st.active != Some(id) {
                // Already replaced by a newer page (its own event follows).
                return Ok(());
            }
            let Some(sender) = st
                .clients
                .iter()
                .find(|(c, _)| *c == id)
                .map(|(_, s)| s.clone())
            else {
                return Ok(());
            };
            (sender, st.clock, st.agent.clone(), st.capture.clone())
        };
        let mut plan = self.plan.clone();
        if let Some(c) = &capture {
            plan.content = crate::browser::layout::place(
                (c.width.max(1), c.height.max(1)),
                (plan.width, plan.height),
            );
        }
        if let Err(e) = sender.send_text(&plan.to_json()) {
            debug!("share: page {id} went away before its plan: {e}");
            return Ok(());
        }
        info!("share: page {id} shares again");
        self.sender = sender;
        self.clock = clock;
        self.agent = agent;
        // Its avcC says (it comes before its first chunk).
        self.length_size = 4;
        Ok(())
    }

    fn packet(&mut self, mut p: Packet) -> Result<()> {
        match p.kind {
            // Audio from before the first keyframe (the encoder starting
            // up) would only make the session's clock start that late.
            Kind::Audio if !self.st.started() => return Ok(()),
            Kind::H264Config if p.payload.first() == Some(&slrw::CONFIG_AVCC) => {
                debug!("share: avcC {}", crate::util::hex::encode(&p.payload[1..]));
                let cfg = h264::AvcConfig::parse(&p.payload[1..])?;
                self.length_size = cfg.length_size as usize;
            }
            Kind::H264 => {
                let now = Instant::now();
                if let Some(since) = self.gap_since.take() {
                    info!(
                        "share: video again after a {} ms gap",
                        now.duration_since(since).as_millis()
                    );
                }
                self.last_video = Some(now);
                if let Some((at, page_us)) = self.clock {
                    let page_now = page_us as f64 + at.elapsed().as_secs_f64() * 1e6;
                    self.lag.add((page_now - p.pts_us as f64) / 1000.0);
                }
                let annexb = p.payload[0] & slrw::H264_ANNEX_B != 0;
                if !annexb && self.length_size != 4 {
                    p.payload = reframe(&p.payload, self.length_size)?;
                }
            }
            _ => {}
        }
        self.st.handle(p)
    }
}

/// The sample aspect ratio of an avcC's SPS when it is not square
/// (`None`: square or unspecified, which receivers treat as square).
fn square_pixels(avcc: &[u8]) -> Option<(u32, u32)> {
    let cfg = h264::AvcConfig::parse(avcc).ok()?;
    let sps = h264::parse_sps(cfg.sps.first()?).ok()?;
    sps.sar.filter(|&(w, h)| w != h)
}

/// An `h264` record payload (flags + AVCC with `n`-byte lengths) with
/// 4-byte lengths.
fn reframe(payload: &[u8], n: usize) -> Result<Vec<u8>> {
    let mut out = Vec::with_capacity(payload.len() + 16);
    out.push(payload[0]);
    for nal in h264::avcc_nals(&payload[1..], n) {
        let nal = nal?;
        out.extend_from_slice(&(nal.len() as u32).to_be_bytes());
        out.extend_from_slice(nal);
    }
    Ok(out)
}

impl Input for ShareInput {
    fn info(&self) -> &StreamInfo {
        &self.info
    }

    fn next_frame(&mut self) -> Result<Option<EncodedFrame>> {
        loop {
            if let Some(f) = self.st.pop() {
                return Ok(Some(f));
            }
            if self.st.ended() || !self.pump()? {
                return Ok(None);
            }
        }
    }

    fn request_keyframe(&mut self) -> bool {
        let now = Instant::now();
        if self
            .last_key_request
            .is_some_and(|l| now.duration_since(l) < KEYFRAME_REQUEST_INTERVAL)
        {
            return true;
        }
        self.last_key_request = Some(now);
        debug!("share: asking the page for a keyframe");
        self.sender.send_text(&protocol::keyframe()).is_ok()
    }

    fn describe(&self) -> String {
        let agent = browser_name(&self.agent);
        format!(
            "shared {}",
            if agent.is_empty() {
                "browser capture".into()
            } else {
                format!("capture from {agent}")
            }
        )
    }

    fn finish(self: Box<Self>, _clean_eof: bool) -> Result<()> {
        let l = &self.lag;
        if l.total_n > 0 {
            info!(
                "share: {} video frames; capture to tvpipe avg {:.1} ms, max {:.1} ms \
                 (after the first {LAG_WARMUP})",
                l.seen,
                l.total_sum_ms / l.total_n as f64,
                l.total_max_ms
            );
        }
        if let Some(s) = &self.page_stats {
            debug!("share: last page stats {s}");
        }
        // Dropping the server tells the pages and stops listening.
        Ok(())
    }
}

/// "Chrome/131", "Safari/18.1", "Firefox/133" from a user agent.
pub(crate) fn browser_name(agent: &str) -> String {
    let find = |name: &str| {
        agent.split_whitespace().find_map(|t| {
            t.strip_prefix(name)
                .and_then(|v| v.strip_prefix('/'))
                .map(|v| format!("{name} {}", v.split('.').next().unwrap_or(v)))
        })
    };
    find("Firefox")
        .or_else(|| find("Edg"))
        .or_else(|| find("Chrome"))
        .or_else(|| {
            find("Version")
                .filter(|_| agent.contains("Safari"))
                .map(|v| v.replace("Version", "Safari"))
        })
        .unwrap_or_default()
}

#[cfg(test)]
pub(crate) mod tests;