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
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
//! `MirrorSink`: the AirPlay screen-mirroring [`Sink`] (PLAN §3, §7.4).
//!
//! [`connect_mirror`] pair-verifies, runs FairPlay and the session setup
//! up to the audio SETUP ([`Session::establish`]), so the canvas and
//! audio format are known before ffmpeg starts. [`Sink::start`] runs the
//! video SETUP, connects the data channel and starts:
//!
//! - `airplay-video`: writes codec packets, encrypted frames and the 1 s
//!   heartbeat to the data connection (one writer, whole packets);
//! - `airplay-feedback`: `POST /feedback` every 2 s (re-anchors PTP);
//! - `airplay-data`: watches the data connection for the receiver ending
//!   the session;
//! - `airplay-rtcp`: answers retransmit requests on the audio control
//!   socket.
//!
//! [`Sink::push`] timestamps video and queues it for the writer; audio is
//! wrapped as verbatim ALAC, encrypted and sent at once (with a sync
//! packet first and then every second).
//!
//! Timing: the session's [`MediaClock`] (handed over by the fanout with
//! [`Sink::set_clock`], shared with the other receivers; ARCHITECTURE.md)
//! maps pts to capture instants; without one, a private clock is anchored
//! on the first frame and re-anchored when frames are more than
//! [`MAX_LATENESS`] late or [`MAX_EARLY`] early. The session timeline
//! (PTP via response headers, or NTP) maps those to receiver time.
//!
//! Sources: docs/airplay-mirroring.md §4.8, §4.11, §4.13, §6, §7.4,
//! §8.2–§8.8; PLAN §3, §5.3.

use std::collections::VecDeque;
use std::io::{Read, Write};
use std::net::{IpAddr, SocketAddr, TcpStream, UdpSocket};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

use crate::airplay::audio::{
    alac_verbatim, sync_ntp, sync_ptp, AudioCipher, Packetizer, Redundancy, ALAC_FRAME_SAMPLES,
    SAMPLE_RATE,
};
use crate::airplay::keys;
use crate::airplay::legacy_pair::{self, Connected};
use crate::airplay::mirror::{self, avcc_record, codec_header, frame_payload, VideoCipher};
use crate::airplay::pair::{self, SecureClient};
use crate::airplay::policy::{self, bit};
use crate::airplay::secure::SecureStream;
use crate::airplay::session::{
    lock_control, ControlStream, MirrorOptions, Session, SetupError, DEFAULT_TIMEOUT,
    STOP_TEARDOWN_TIMEOUT,
};
use crate::airplay::setup::AudioCodecId;
use crate::airplay::timing::{
    anchor_from_headers, fp_from_duration, fp_to_ns, ntp_clock, Timeline, NTP_EPOCH_OFFSET_FP,
};
use crate::error::{Error, Result};
use crate::media::clock::{SessionClock, SinkClock, Tolerance};
use crate::media::h264::{self, AvcConfig};
use crate::types::{
    AudioCodec, AudioFamily, AudioFormat, AudioInfo, Capabilities, EncodedFrame, Sink, SinkStatus,
    TrackKind, VideoCodec, VideoInfo, VIDEO_CLOCK_RATE,
};
use crate::util::ratio::Ratio;
use crate::util::time::ticks_to_duration;

/// Frames later than this re-anchor the media clock.
pub const MAX_LATENESS: Duration = Duration::from_millis(100);
/// Frames earlier than this re-anchor it too (a jump in the input).
pub const MAX_EARLY: Duration = Duration::from_secs(1);
pub const FEEDBACK_INTERVAL: Duration = Duration::from_secs(2);
pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(1);
pub const SYNC_INTERVAL: Duration = Duration::from_secs(1);
/// Write timeouts (§6.5).
const FRAME_WRITE_TIMEOUT: Duration = Duration::from_secs(2);
const CODEC_WRITE_TIMEOUT: Duration = Duration::from_secs(1);
const HEARTBEAT_WRITE_TIMEOUT: Duration = Duration::from_secs(5);
/// How long `stop` lets the writer send what is still queued.
const STOP_DRAIN: Duration = Duration::from_millis(500);
/// After TEARDOWN: how long `stop` waits for the sink's threads.
const STOP_JOIN: Duration = Duration::from_secs(1);
/// After a failed write: how long the writer waits for the reader to see
/// the receiver closing the connection (which makes it no failure).
const WRITE_FAILURE_GRACE: Duration = Duration::from_millis(250);
/// Video packets queued for the writer before frames are dropped.
const MAX_QUEUED: usize = 32;
const MAX_QUEUED_BYTES: usize = 8 << 20;
/// Capture instants further than this from the predicted ones mean the
/// media clock was re-anchored.
const CLOCK_SHIFT_TOLERANCE: Duration = Duration::from_millis(1);
/// Consecutive stale audio frames at start-up before giving up (§8.8).
const MAX_STALE_AT_START: u32 = 128;
/// Burst pacing: packets per window (§8.4).
const BURST_PACKETS: u32 = 12;
const BURST_WINDOW: Duration = Duration::from_millis(5);
/// Bitrate ceiling the reference uses (§6.6).
const MAX_BITRATE_KBPS: u32 = 12_000;

/// The production stream type.
pub type SecureMirrorSink = MirrorSink<SecureStream<TcpStream>>;

/// Connects to an AirPlay receiver for mirroring: pair-verify (stored
/// credentials, else transient), FairPlay, session setup. Falls back from
/// PTP to NTP on a fresh connection when PTP fails (§2.5).
pub fn connect_mirror(
    target: &pair::Target,
    opts: &MirrorOptions,
    storage: &crate::storage::Storage,
) -> Result<AnyMirrorSink> {
    let mut store = crate::airplay::credentials::Store::open(storage)?;
    let name = target.label();
    match legacy_pair::connect(target, &mut store)? {
        Connected::Hap(control) => {
            let mut first = Some(*control);
            connect_with(name, opts, || {
                let control = match first.take() {
                    Some(c) => c,
                    None => pair::connect(target, &store)?,
                };
                let secret = control.shared.secret().to_vec();
                let client: SecureClient = control.client;
                Ok((client, control.addr, secret, true))
            })
            .map(AnyMirrorSink::Hap)
        }
        Connected::Legacy {
            client,
            addr,
            secret,
        } => {
            warn!("airplay: {name}: legacy profile (untested on hardware)");
            let mut first = Some((client, addr, secret));
            connect_with(name, opts, || match first.take() {
                Some((client, addr, secret)) => Ok((client, addr, secret, false)),
                // The legacy profile never uses PTP, so there is no retry.
                None => Err(Error::protocol("airplay: legacy session cannot be retried")),
            })
            .map(AnyMirrorSink::Legacy)
        }
    }
}

/// A mirroring sink of either crypto profile.
pub enum AnyMirrorSink {
    /// Encrypted control channel (HAP pair-verify).
    Hap(SecureMirrorSink),
    /// Plaintext control channel (legacy raw pair-verify).
    Legacy(MirrorSink<TcpStream>),
}

macro_rules! any {
    ($self:ident, $s:ident => $e:expr) => {
        match $self {
            AnyMirrorSink::Hap($s) => $e,
            AnyMirrorSink::Legacy($s) => $e,
        }
    };
}

impl Sink for AnyMirrorSink {
    fn name(&self) -> &str {
        any!(self, s => s.name())
    }
    fn capabilities(&self) -> &Capabilities {
        any!(self, s => s.capabilities())
    }
    fn set_clock(&mut self, clock: SessionClock) {
        any!(self, s => s.set_clock(clock))
    }
    fn start(&mut self, video: &VideoInfo, audio: Option<&AudioInfo>) -> Result<()> {
        any!(self, s => s.start(video, audio))
    }
    fn push(&mut self, frame: &EncodedFrame) -> Result<SinkStatus> {
        any!(self, s => s.push(frame))
    }
    fn wants_keyframe(&self) -> bool {
        any!(self, s => s.wants_keyframe())
    }
    fn stop(&mut self) -> Result<()> {
        any!(self, s => s.stop())
    }
}

/// A fresh authenticated control connection: client, peer, shared
/// secret, HAP profile.
pub type Fresh<S> = (crate::net::rtsp::RtspClient<S>, SocketAddr, Vec<u8>, bool);

/// [`connect_mirror`] over any connector: establishes the session, and
/// when PTP fails tears it down and retries once with NTP on a new
/// connection (§2.5).
pub fn connect_with<S: ControlStream>(
    name: String,
    opts: &MirrorOptions,
    mut connect: impl FnMut() -> Result<Fresh<S>>,
) -> Result<MirrorSink<S>> {
    let mut opts = opts.clone();
    loop {
        let (client, peer, secret, hap) = connect()?;
        match Session::establish(client, peer, secret, hap, &opts) {
            Ok(s) => return Ok(MirrorSink::new(name, s, &opts)),
            Err(SetupError::PtpFailed(e)) if opts.ptp => {
                warn!("airplay: PTP setup failed ({e}); retrying with NTP");
                opts.ptp = false;
            }
            Err(e) => return Err(e.into_error()),
        }
    }
}

/// The private clock's rule (a sink used without a fanout).
const PRIVATE_CLOCK: Tolerance = Tolerance {
    late: MAX_LATENESS,
    early: MAX_EARLY,
};

/// Maps pts to capture instants (like the Cast sink): on the session's
/// clock when there is one, else on the private one (early frames keep
/// their future instants, frames far off re-anchor).
fn capture_instant(clock: &SinkClock, pts: u64, rate: u32, now: Instant) -> Instant {
    clock.instant(pts, rate, now)
}

// ---------------------------------------------------------------------------
// Video writer

enum Item {
    Codec(Vec<u8>),
    Frame {
        payload: Vec<u8>,
        ts: u64,
        idr: bool,
    },
}

impl Item {
    fn len(&self) -> usize {
        match self {
            Item::Codec(p) => p.len(),
            Item::Frame { payload, .. } => payload.len(),
        }
    }
}

#[derive(Default)]
struct Queue {
    items: VecDeque<Item>,
    bytes: usize,
    closed: bool,
    /// After `closed`: queued packets are written until then, the rest
    /// is dropped (a slow receiver must not hold up a stop).
    drain_until: Option<Instant>,
}

/// State shared between the sink and its threads.
struct Shared {
    queue: Mutex<Queue>,
    cond: Condvar,
    /// The receiver ended the session.
    finished: AtomicBool,
    /// A fatal error in a thread.
    failed: Mutex<Option<Error>>,
    /// Stopping: threads exit, errors are expected.
    stopping: AtomicBool,
    /// The first video frame went out (audio may start).
    first_frame_sent: AtomicBool,
    /// A frame was dropped; skip to the next keyframe.
    need_key: AtomicBool,
    /// When the receiver last answered `POST /feedback` (or the stream
    /// started).
    last_answer: Mutex<Instant>,
}

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

    fn fail(&self, e: Error) {
        if self.stopping.load(Ordering::Acquire) {
            debug!("airplay: after stop: {e}");
            return;
        }
        // `push` returns it; the session logs it (and may reconnect).
        debug!("airplay: {e}");
        let mut f = self.failed.lock().unwrap_or_else(|e| e.into_inner());
        f.get_or_insert(e);
    }

    fn take_failure(&self) -> Option<Error> {
        self.failed.lock().unwrap_or_else(|e| e.into_inner()).take()
    }

    fn answered(&self) {
        *self.last_answer.lock().unwrap_or_else(|e| e.into_inner()) = Instant::now();
    }

    fn silent_for(&self) -> Duration {
        self.last_answer
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .elapsed()
    }
}

fn write_packet(sock: &mut TcpStream, bytes: &[u8], timeout: Duration) -> Result<()> {
    let _ = sock.set_write_timeout(Some(timeout));
    sock.write_all(bytes)
        .map_err(|e| Error::io("airplay: video data write", e))
}

enum Next {
    Item(Item),
    Heartbeat,
    Done,
}

fn writer_loop(shared: &Shared, mut sock: TcpStream, mut cipher: VideoCipher, timeline: Timeline) {
    let mut next_heartbeat: Option<Instant> = None;
    let mut frames = 0u64;
    loop {
        let next = {
            let mut q = shared.queue();
            loop {
                if q.closed && q.drain_until.is_some_and(|t| Instant::now() >= t) {
                    if !q.items.is_empty() {
                        debug!(
                            "airplay: dropping {} queued video packets at stop",
                            q.items.len()
                        );
                    }
                    q.items.clear();
                    q.bytes = 0;
                }
                if let Some(it) = q.items.pop_front() {
                    q.bytes -= it.len();
                    break Next::Item(it);
                }
                if q.closed {
                    break Next::Done;
                }
                let now = Instant::now();
                match next_heartbeat {
                    Some(at) if now >= at => break Next::Heartbeat,
                    Some(at) => {
                        q = shared
                            .cond
                            .wait_timeout(q, at - now)
                            .map(|(g, _)| g)
                            .unwrap_or_else(|e| e.into_inner().0);
                    }
                    None => q = shared.cond.wait(q).unwrap_or_else(|e| e.into_inner()),
                }
            }
        };
        let r = match next {
            Next::Done => return,
            Next::Heartbeat => {
                next_heartbeat = Some(Instant::now() + HEARTBEAT_INTERVAL);
                trace!("airplay: heartbeat");
                write_packet(&mut sock, &mirror::heartbeat(), HEARTBEAT_WRITE_TIMEOUT)
            }
            Next::Item(Item::Codec(p)) => {
                debug!("airplay: codec packet ({} bytes)", p.len());
                write_packet(&mut sock, &p, CODEC_WRITE_TIMEOUT)
            }
            Next::Item(Item::Frame { payload, ts, idr }) => cipher
                .frame_packet(payload, ts, idr, timeline.timeline_id())
                .and_then(|pkt| {
                    write_packet(&mut sock, &pkt, FRAME_WRITE_TIMEOUT)?;
                    frames += 1;
                    if frames == 1 {
                        debug!("airplay: first video frame sent ({} bytes)", pkt.len());
                        shared.first_frame_sent.store(true, Ordering::Release);
                        next_heartbeat = Some(Instant::now() + HEARTBEAT_INTERVAL);
                    }
                    Ok(())
                }),
        };
        if let Err(e) = r {
            // The receiver closing the connection fails the write too;
            // that is the end of the session, not a loss, once the
            // reader sees it.
            let until = Instant::now() + WRITE_FAILURE_GRACE;
            while !shared.finished.load(Ordering::Acquire) && Instant::now() < until {
                std::thread::sleep(Duration::from_millis(5));
            }
            if !shared.finished.load(Ordering::Acquire) {
                shared.fail(e);
            }
            let mut q = shared.queue();
            q.items.clear();
            q.bytes = 0;
            q.closed = true;
            return;
        }
    }
}

// ---------------------------------------------------------------------------
// Video and audio state on the push side

struct VideoState {
    length_size: usize,
    sps: Vec<u8>,
    pps: Vec<u8>,
    width: u32,
    height: u32,
    need_codec: bool,
    last_ts: Option<u64>,
    lead: u64,
}

impl VideoState {
    fn new(video: &VideoInfo, lead_ms: u32) -> Result<VideoState> {
        if video.codec != VideoCodec::H264 {
            return Err(Error::unsupported(
                "airplay: only H.264 mirroring is implemented",
            ));
        }
        let cfg = AvcConfig::parse(&video.avcc)?;
        let sps = cfg
            .sps
            .first()
            .cloned()
            .ok_or_else(|| Error::malformed("airplay: avcC without SPS"))?;
        let pps = cfg
            .pps
            .first()
            .cloned()
            .ok_or_else(|| Error::malformed("airplay: avcC without PPS"))?;
        let mut v = VideoState {
            length_size: cfg.length_size as usize,
            sps: Vec::new(),
            pps,
            width: video.width,
            height: video.height,
            need_codec: true,
            last_ts: None,
            lead: fp_from_duration(Duration::from_millis(lead_ms as u64)),
        };
        v.set_sps(sps);
        Ok(v)
    }

    fn set_sps(&mut self, sps: Vec<u8>) {
        if let Ok(s) = h264::parse_sps(&sps) {
            if s.width > 0 && s.height > 0 {
                self.width = s.width;
                self.height = s.height;
            }
        }
        self.sps = sps;
    }

    /// §6.3: strictly increasing timestamps.
    fn timestamp(&mut self, t: u64) -> u64 {
        let ts = match self.last_ts {
            Some(prev) if t <= prev => prev + 1,
            _ => t,
        };
        self.last_ts = Some(ts);
        ts
    }
}

struct AudioState {
    channels: u8,
    packetizer: Arc<Mutex<Packetizer>>,
    data: UdpSocket,
    control: UdpSocket,
    rtcp: SocketAddr,
    lead: Duration,
    lead_samples: u32,
    started: bool,
    stale: u32,
    /// `rtp = pts + offset` (mod 2³²).
    offset: u32,
    last_pts: Option<u64>,
    /// The capture instant of the last frame (to see clock re-anchors).
    last_capture: Option<Instant>,
    last_sync: Option<Instant>,
    first_sync: bool,
    window: (Instant, u32),
    frames: u64,
}

fn lock_pk(p: &Mutex<Packetizer>) -> MutexGuard<'_, Packetizer> {
    p.lock().unwrap_or_else(|e| e.into_inner())
}

impl AudioState {
    fn pace(&mut self) {
        let now = Instant::now();
        if now.duration_since(self.window.0) >= BURST_WINDOW {
            self.window = (now, 0);
        }
        if self.window.1 >= BURST_PACKETS {
            let until = self.window.0 + BURST_WINDOW;
            if let Some(d) = until.checked_duration_since(now) {
                std::thread::sleep(d);
            }
            self.window = (Instant::now(), 0);
        }
        self.window.1 += 1;
    }

    fn send_sync(&mut self, timeline: &Timeline, rtp_now: u32, t: Instant) {
        let first = self.first_sync;
        self.first_sync = false;
        let play = rtp_now.wrapping_sub(self.lead_samples);
        let pkt = match timeline {
            Timeline::Ptp(c) => sync_ptp(first, play, fp_to_ns(c.at(t)), c.timeline()).to_vec(),
            Timeline::Ntp => sync_ntp(
                first,
                play,
                ntp_clock(t).wrapping_add(NTP_EPOCH_OFFSET_FP),
                rtp_now,
            )
            .to_vec(),
        };
        if first {
            debug!("airplay: first audio sync (rtp {rtp_now}, play {play})");
        }
        if let Err(e) = self.control.send_to(&pkt, self.rtcp) {
            debug!("airplay: sync packet: {e}");
        }
        self.last_sync = Some(Instant::now());
    }

    fn push(
        &mut self,
        frame: &EncodedFrame,
        clock: &SinkClock,
        timeline: &Timeline,
    ) -> Result<SinkStatus> {
        let now = Instant::now();
        let capture = capture_instant(clock, frame.pts, SAMPLE_RATE, now);
        if !self.started {
            // §8.8: drop frames that would arrive too late.
            if capture + self.lead <= now + Duration::from_millis(5) {
                self.stale += 1;
                if self.stale >= MAX_STALE_AT_START {
                    return Err(Error::protocol(
                        "airplay: audio frames keep arriving too late to play",
                    ));
                }
                return Ok(SinkStatus::Dropped);
            }
            self.started = true;
            self.offset = ALAC_FRAME_SAMPLES.wrapping_sub(frame.pts as u32);
        }
        let samples = (frame.data.len() / (2 * self.channels as usize)) as u32;
        if let (Some(last), Some(last_capture)) = (self.last_pts, self.last_capture) {
            if frame.pts < last {
                // A new segment: continue after the last frame (§8.5).
                let last_rtp = (last as u32).wrapping_add(self.offset);
                self.offset = last_rtp
                    .wrapping_add(ALAC_FRAME_SAMPLES)
                    .wrapping_sub(frame.pts as u32);
                self.first_sync = true;
                self.last_sync = None;
            } else {
                // The media clock re-anchored (the source stalled and the
                // frames arrive late): keep the RTP timeline on the same
                // sample<->time line by leaving a gap of the shift in the
                // RTP timestamps (§8.2: a real gap stays a gap). The
                // receiver keeps its first mapping; without the gap every
                // later packet would be late by the accumulated stalls.
                let predicted = last_capture + ticks_to_duration(frame.pts - last, SAMPLE_RATE);
                if capture > predicted + CLOCK_SHIFT_TOLERANCE {
                    let gap = ((capture - predicted).as_secs_f64() * SAMPLE_RATE as f64).round();
                    debug!("airplay: audio clock moved {gap} samples ahead; RTP gap");
                    self.offset = self.offset.wrapping_add(gap as u32);
                } else if capture + CLOCK_SHIFT_TOLERANCE < predicted {
                    // Moved back (a jump in the input): announce a new
                    // mapping (§8.5).
                    debug!("airplay: audio clock moved back; new sync mapping");
                    self.first_sync = true;
                    self.last_sync = None;
                }
            }
        }
        self.last_pts = Some(frame.pts);
        self.last_capture = Some(capture);
        let rtp = (frame.pts as u32).wrapping_add(self.offset);
        let alac = alac_verbatim(&frame.data, self.channels)?;
        let due = self
            .last_sync
            .is_none_or(|t| now.duration_since(t) >= SYNC_INTERVAL);
        if due {
            self.send_sync(timeline, rtp, capture);
        }
        let packets = lock_pk(&self.packetizer).frame(rtp, &alac)?;
        for p in packets {
            self.pace();
            if let Err(e) = self.data.send(&p) {
                debug!("airplay: audio send: {e}");
            }
        }
        self.frames += 1;
        if self.frames == 1 {
            debug!("airplay: first audio frame sent (rtp {rtp}, {samples} samples)");
        }
        Ok(SinkStatus::Accepted)
    }
}

// ---------------------------------------------------------------------------
// The sink

/// Debug statistics on frame timing, logged every few seconds: how late
/// each track arrives relative to a fixed pts schedule (anchored on the
/// first frame, never moved), and the playout headroom it is sent with.
#[derive(Default)]
struct Diag {
    origin: Option<(Instant, Duration)>,
    since: Option<Instant>,
    /// [video, audio]: (min, max) arrival lateness and min headroom, ms.
    late: [(i64, i64); 2],
    headroom: [i64; 2],
    count: [u64; 2],
}

const DIAG_INTERVAL: Duration = Duration::from_secs(5);

impl Diag {
    fn note(&mut self, track: usize, media: Duration, now: Instant, deadline: Instant) {
        let (t0, m0) = *self.origin.get_or_insert((now, media));
        let since = *self.since.get_or_insert(now);
        let ms = |a: Instant, b: Instant| -> i64 {
            if a >= b {
                (a - b).as_millis() as i64
            } else {
                -((b - a).as_millis() as i64)
            }
        };
        let due = if media >= m0 {
            t0 + (media - m0)
        } else {
            t0.checked_sub(m0 - media).unwrap_or(t0)
        };
        let late = ms(now, due);
        let head = ms(deadline, now);
        if self.count[track] == 0 {
            self.late[track] = (late, late);
            self.headroom[track] = head;
        } else {
            let l = &mut self.late[track];
            *l = (l.0.min(late), l.1.max(late));
            self.headroom[track] = self.headroom[track].min(head);
        }
        self.count[track] += 1;
        if now.duration_since(since) >= DIAG_INTERVAL {
            debug!(
                "airplay: timing: video {} frames late {}..{} ms headroom >= {} ms; \
                 audio {} frames late {}..{} ms headroom >= {} ms",
                self.count[0],
                self.late[0].0,
                self.late[0].1,
                self.headroom[0],
                self.count[1],
                self.late[1].0,
                self.late[1].1,
                self.headroom[1]
            );
            self.count = [0, 0];
            self.since = Some(now);
        }
    }
}

struct Media {
    diag: Diag,
    clock: SinkClock,
    video: VideoState,
    audio: Option<AudioState>,
    data_sock: TcpStream,
    threads: Vec<JoinHandle<()>>,
}

pub struct MirrorSink<S: ControlStream> {
    name: String,
    caps: Capabilities,
    session: Session<S>,
    shared: Arc<Shared>,
    media: Option<Media>,
    /// The session's media clock (from the fanout), if any.
    clock: Option<SessionClock>,
    /// [`MirrorOptions::feedback_timeout`].
    feedback_timeout: Duration,
    stopped: bool,
}

impl<S: ControlStream> MirrorSink<S> {
    pub fn new(name: String, session: Session<S>, opts: &MirrorOptions) -> MirrorSink<S> {
        let c = session.canvas;
        let fps = c.max_fps.unwrap_or(60).clamp(1, 60);
        let audio_formats = if opts.audio && session.audio.ports.is_some() {
            match session.audio.codec {
                AudioCodecId::Alac => vec![AudioFormat {
                    codec: AudioCodec::Alac,
                    sample_rate: SAMPLE_RATE,
                    channels: 2,
                }],
                AudioCodecId::AacEld => vec![AudioFormat {
                    codec: AudioCodec::AacEld,
                    sample_rate: SAMPLE_RATE,
                    channels: 2,
                }],
            }
        } else {
            Vec::new()
        };
        let caps = Capabilities {
            max_width: c.width,
            max_height: c.height,
            max_fps: Ratio::integer(fps),
            video_codecs: vec![VideoCodec::H264],
            audio_formats,
            audio_family: AudioFamily::AirPlay,
            max_bitrate_kbps: Some(MAX_BITRATE_KBPS),
            min_latency_ms: None,
            preferred_latency_ms: Some(session.leads.video_ms),
            // The canvas from SETUP is the screen's shape.
            display_aspect: crate::types::aspect_of(c.width, c.height)
                .or(Some(crate::types::DEFAULT_DISPLAY_ASPECT)),
        };
        info!(
            "airplay: {name}: session ready ({}, {}x{}@{fps}, audio {:?})",
            match session.timeline {
                Timeline::Ptp(_) => "PTP",
                Timeline::Ntp => "NTP",
            },
            c.width,
            c.height,
            caps.audio_formats.first().map(|f| f.codec)
        );
        MirrorSink {
            name,
            caps,
            session,
            shared: Arc::new(Shared {
                queue: Mutex::new(Queue::default()),
                cond: Condvar::new(),
                finished: AtomicBool::new(false),
                failed: Mutex::new(None),
                stopping: AtomicBool::new(false),
                first_frame_sent: AtomicBool::new(false),
                need_key: AtomicBool::new(false),
                last_answer: Mutex::new(Instant::now()),
            }),
            feedback_timeout: opts.feedback_timeout,
            media: None,
            clock: None,
            stopped: false,
        }
    }

    pub fn session(&self) -> &Session<S> {
        &self.session
    }

    fn audio_state(&self, audio: Option<&AudioInfo>) -> Result<Option<AudioState>> {
        let (Some(info), Some(ports)) = (audio, self.session.audio.ports) else {
            return Ok(None);
        };
        if info.codec != AudioCodec::PcmS16le || info.sample_rate != SAMPLE_RATE {
            return Err(Error::unsupported(format!(
                "airplay: audio must be 44.1 kHz s16le PCM (got {:?} at {} Hz)",
                info.codec, info.sample_rate
            )));
        }
        if self.session.audio.codec != AudioCodecId::Alac {
            return Err(Error::unsupported(
                "airplay: AAC-ELD audio is not implemented",
            ));
        }
        let ip = self.session.peer.ip();
        let cipher = match (&self.session.audio.key, &self.session.fairplay) {
            (Some(k), _) => AudioCipher::chacha(k),
            (None, Some(fp)) => AudioCipher::aes_cbc(&fp.master, &fp.eiv)?,
            (None, None) => AudioCipher::Clear,
        };
        let features = self.session.info.features;
        let redundancy = if policy::has(features, bit::RFC2198) {
            Redundancy::Rfc2198
        } else {
            Redundancy::Resend
        };
        let socks = &self.session.audio.sockets;
        let clone = |s: &UdpSocket| {
            s.try_clone()
                .map_err(|e| Error::io("airplay: clone UDP", e))
        };
        let data = clone(&socks.data)?;
        data.connect(SocketAddr::new(ip, ports.rtp))
            .map_err(|e| Error::io("airplay: audio connect", e))?;
        Ok(Some(AudioState {
            channels: info.channels,
            packetizer: Arc::new(Mutex::new(Packetizer::new(cipher, redundancy))),
            data,
            control: clone(&socks.control)?,
            rtcp: SocketAddr::new(ip, ports.rtcp),
            lead: Duration::from_millis(self.session.leads.audio_ms as u64),
            lead_samples: self.session.leads.audio_samples(),
            started: false,
            stale: 0,
            offset: 0,
            last_pts: None,
            last_capture: None,
            last_sync: None,
            first_sync: true,
            window: (Instant::now(), 0),
            frames: 0,
        }))
    }

    fn spawn(
        threads: &mut Vec<JoinHandle<()>>,
        name: &str,
        f: impl FnOnce() + Send + 'static,
    ) -> Result<()> {
        threads.push(
            std::thread::Builder::new()
                .name(name.into())
                .spawn(f)
                .map_err(|e| Error::io(format!("airplay: spawn {name}"), e))?,
        );
        Ok(())
    }

    fn start_inner(&mut self, video: &VideoInfo, audio: Option<&AudioInfo>) -> Result<()> {
        let vstate = VideoState::new(video, self.session.leads.video_ms)?;
        let astate = self.audio_state(audio)?;
        let data = self.session.start_video()?;
        let cipher = if self.session.hap {
            VideoCipher::chacha(&keys::video_key(
                &self.session.secret.0,
                self.session.video_id,
            )?)
        } else {
            // §5.2: K with FairPlay; without it the random `shk` we sent
            // (audio then goes in the clear). Untested on hardware.
            let shk = match (&self.session.fairplay, self.session.video_shk) {
                (Some(fp), _) => fp.master,
                (None, Some(shk)) => {
                    warn!("airplay: legacy profile without FairPlay (untested on any receiver)");
                    shk
                }
                (None, None) => return Err(Error::protocol("airplay: no video key")),
            };
            let (k, iv) = keys::legacy_video_key_iv(&shk, self.session.video_id);
            VideoCipher::aes_ctr(&k, &iv)?
        };
        let mut threads = Vec::new();
        let clone = |s: &TcpStream| {
            s.try_clone()
                .map_err(|e| Error::io("airplay: clone socket", e))
        };
        self.shared.answered();

        // Writer.
        {
            let shared = self.shared.clone();
            let sock = clone(&data)?;
            let timeline = self.session.timeline.clone();
            Self::spawn(&mut threads, "airplay-video", move || {
                writer_loop(&shared, sock, cipher, timeline)
            })?;
        }
        // Data connection reader: the receiver closing it (EOF, or a
        // reset when our frames were still unread) means it ended
        // mirroring (§4.14); a receiver that is gone (another address,
        // off the network) cannot close anything. Other errors (the
        // kernel giving up on retransmissions) mean it is lost.
        {
            let shared = self.shared.clone();
            let mut sock = clone(&data)?;
            Self::spawn(&mut threads, "airplay-data", move || {
                let mut buf = [0u8; 1024];
                let error = loop {
                    match sock.read(&mut buf) {
                        Ok(0) => break None,
                        Err(e) if e.kind() == std::io::ErrorKind::ConnectionReset => break None,
                        Ok(n) => trace!("airplay: {n} bytes on the video data connection"),
                        Err(e)
                            if matches!(
                                e.kind(),
                                std::io::ErrorKind::WouldBlock
                                    | std::io::ErrorKind::TimedOut
                                    | std::io::ErrorKind::Interrupted
                            ) => {}
                        Err(e) => break Some(e),
                    }
                };
                if shared.stopping.load(Ordering::Acquire) {
                    return;
                }
                match error {
                    None => {
                        info!("airplay: receiver closed the video connection");
                        shared.finished.store(true, Ordering::Release);
                    }
                    Some(e) => shared.fail(Error::io("airplay: video data connection", e)),
                }
            })?;
        }
        // Feedback (§4.13), first one immediately.
        {
            let shared = self.shared.clone();
            let control = self.session.control.clone();
            let timeline = self.session.timeline.clone();
            Self::spawn(&mut threads, "airplay-feedback", move || {
                feedback_loop(&shared, &control, &timeline)
            })?;
        }
        // Retransmits (§8.6).
        if let Some(a) = &astate {
            let shared = self.shared.clone();
            let sock = a
                .control
                .try_clone()
                .map_err(|e| Error::io("airplay: clone UDP", e))?;
            let pk = a.packetizer.clone();
            let ip = self.session.peer.ip();
            Self::spawn(&mut threads, "airplay-rtcp", move || {
                rtcp_loop(&shared, &sock, &pk, ip)
            })?;
        }
        debug!(
            "airplay: {}: streaming {}x{} video{}",
            self.name,
            vstate.width,
            vstate.height,
            if astate.is_some() {
                " and ALAC audio"
            } else {
                ""
            }
        );
        self.media = Some(Media {
            diag: Diag::default(),
            clock: self
                .clock
                .clone()
                .map_or_else(|| SinkClock::private(PRIVATE_CLOCK), SinkClock::shared),
            video: vstate,
            audio: astate,
            data_sock: data,
            threads,
        });
        Ok(())
    }

    fn queue_video(shared: &Shared, items: Vec<Item>, keyframe: bool) -> SinkStatus {
        let mut q = shared.queue();
        let bytes: usize = items.iter().map(Item::len).sum();
        if q.closed {
            return SinkStatus::Dropped;
        }
        if q.items.len() + items.len() > MAX_QUEUED || q.bytes + bytes > MAX_QUEUED_BYTES {
            if !shared.need_key.swap(true, Ordering::AcqRel) {
                debug!("airplay: video queue full; skipping to the next keyframe");
            }
            return SinkStatus::Dropped;
        }
        if keyframe {
            shared.need_key.store(false, Ordering::Release);
        }
        q.bytes += bytes;
        q.items.extend(items);
        drop(q);
        shared.cond.notify_all();
        SinkStatus::Accepted
    }

    fn push_video(&mut self, frame: &EncodedFrame) -> Result<SinkStatus> {
        let Some(m) = self.media.as_mut() else {
            return Err(Error::protocol("airplay: push before start"));
        };
        let now = Instant::now();
        let p = frame_payload(&frame.data, m.video.length_size)?;
        if p.idr {
            if let Some(sps) = p.sps.as_ref().filter(|s| **s != m.video.sps) {
                debug!("airplay: SPS changed");
                m.video.set_sps(sps.clone());
                m.video.need_codec = true;
            }
            if let Some(pps) = p.pps.as_ref().filter(|s| **s != m.video.pps) {
                debug!("airplay: PPS changed");
                m.video.pps = pps.clone();
                m.video.need_codec = true;
            }
        } else if m.video.need_codec || self.shared.need_key.load(Ordering::Acquire) {
            return Ok(SinkStatus::Dropped);
        }
        if p.data.is_empty() {
            return Ok(SinkStatus::Dropped);
        }
        let capture = capture_instant(&m.clock, frame.pts, VIDEO_CLOCK_RATE, now);
        m.diag.note(
            0,
            ticks_to_duration(frame.pts, VIDEO_CLOCK_RATE),
            now,
            capture + Duration::from_millis(self.session.leads.video_ms as u64),
        );
        let ts = m
            .video
            .timestamp(self.session.timeline.at(capture).wrapping_add(m.video.lead));
        let mut items = Vec::with_capacity(2);
        if p.idr && m.video.need_codec {
            let rec = avcc_record(&m.video.sps, &m.video.pps)?;
            let mut pkt = codec_header(rec.len(), ts, m.video.width, m.video.height)?.to_vec();
            pkt.extend_from_slice(&rec);
            items.push(Item::Codec(pkt));
        }
        items.push(Item::Frame {
            payload: p.data,
            ts,
            idr: p.idr,
        });
        let status = Self::queue_video(&self.shared, items, p.idr);
        if status == SinkStatus::Accepted && p.idr {
            m.video.need_codec = false;
            if self
                .session
                .signals
                .keyframe_requested
                .swap(false, Ordering::AcqRel)
            {
                debug!("airplay: keyframe request served");
            }
        }
        Ok(status)
    }

    fn push_audio(&mut self, frame: &EncodedFrame) -> Result<SinkStatus> {
        let Some(m) = self.media.as_mut() else {
            return Err(Error::protocol("airplay: push before start"));
        };
        let Some(a) = m.audio.as_mut() else {
            return Ok(SinkStatus::Dropped);
        };
        if !self.shared.first_frame_sent.load(Ordering::Acquire) {
            return Ok(SinkStatus::Dropped);
        }
        let now = Instant::now();
        let r = a.push(frame, &m.clock, &self.session.timeline);
        if let Some(capture) = m.clock.instant_for(frame.pts, SAMPLE_RATE) {
            m.diag.note(
                1,
                ticks_to_duration(frame.pts, SAMPLE_RATE),
                now,
                capture + a.lead,
            );
        }
        r
    }
}

fn feedback_loop<S: ControlStream>(
    shared: &Shared,
    control: &Mutex<crate::airplay::session::Control<S>>,
    timeline: &Timeline,
) {
    let mut next = Instant::now();
    loop {
        {
            let mut q = shared.queue();
            loop {
                if shared.stopping.load(Ordering::Acquire) || q.closed {
                    return;
                }
                let now = Instant::now();
                if now >= next {
                    break;
                }
                q = shared
                    .cond
                    .wait_timeout(q, next - now)
                    .map(|(g, _)| g)
                    .unwrap_or_else(|e| e.into_inner().0);
            }
        }
        next += FEEDBACK_INTERVAL;
        let mut c = lock_control(control);
        match c.request("POST", "/feedback", &[], None, DEFAULT_TIMEOUT) {
            Ok(r) => {
                let t1 = Instant::now();
                drop(c);
                shared.answered();
                if !r.is_success() {
                    debug!("airplay: /feedback answered {}", r.status);
                }
                if let (Timeline::Ptp(clock), Some(r1)) =
                    (timeline, anchor_from_headers(&r.headers))
                {
                    clock.reanchor(t1, r1);
                }
            }
            Err(e) if c.is_broken() => {
                drop(c);
                shared.fail(e);
                return;
            }
            Err(e) => warn!("airplay: /feedback: {e}"),
        }
    }
}

fn rtcp_loop(shared: &Shared, sock: &UdpSocket, pk: &Mutex<Packetizer>, peer: IpAddr) {
    let _ = sock.set_read_timeout(Some(Duration::from_millis(100)));
    let mut buf = [0u8; 2048];
    while !shared.stopping.load(Ordering::Acquire) {
        let (n, from) = match sock.recv_from(&mut buf) {
            Ok(v) => v,
            Err(_) => continue,
        };
        if from.ip() != peer {
            continue;
        }
        let replies = lock_pk(pk).retransmit(&buf[..n]);
        if replies.is_empty() {
            trace!("airplay: {n} bytes on the audio control socket");
            continue;
        }
        debug!("airplay: retransmitting {} audio packets", replies.len());
        for r in replies {
            let _ = sock.send_to(&r, from);
        }
    }
}

impl<S: ControlStream> Sink for MirrorSink<S> {
    fn name(&self) -> &str {
        &self.name
    }

    fn capabilities(&self) -> &Capabilities {
        &self.caps
    }

    fn set_clock(&mut self, clock: SessionClock) {
        self.clock = Some(clock);
    }

    fn start(&mut self, video: &VideoInfo, audio: Option<&AudioInfo>) -> Result<()> {
        if self.media.is_some() {
            return Err(Error::protocol("airplay: sink already started"));
        }
        self.start_inner(video, audio)
    }

    fn push(&mut self, frame: &EncodedFrame) -> Result<SinkStatus> {
        // The receiver ending the session wins over the errors closing
        // its connections causes.
        if self.shared.finished.load(Ordering::Acquire) {
            return Ok(SinkStatus::Finished);
        }
        if let Some(e) = self.shared.take_failure() {
            return Err(e);
        }
        if self.media.is_some() {
            let silent = self.shared.silent_for();
            if silent >= self.feedback_timeout {
                return Err(Error::timeout(format!(
                    "airplay: no answer to /feedback for {:.0} s",
                    silent.as_secs_f64()
                )));
            }
        }
        match frame.track {
            TrackKind::Video => self.push_video(frame),
            TrackKind::Audio(AudioFamily::AirPlay) => self.push_audio(frame),
            TrackKind::Audio(_) => Ok(SinkStatus::Dropped),
        }
    }

    fn wants_keyframe(&self) -> bool {
        // Before start, after a dropped frame, or when the receiver asked
        // (`forceKeyFrame`): the fanout then skips to the next IDR.
        self.media.is_none()
            || self.shared.need_key.load(Ordering::Acquire)
            || self
                .session
                .signals
                .keyframe_requested
                .load(Ordering::Acquire)
    }

    fn stop(&mut self) -> Result<()> {
        if self.stopped {
            return Ok(());
        }
        self.stopped = true;
        // Let the writer finish what is queued, then stop everything.
        {
            let mut q = self.shared.queue();
            q.closed = true;
            q.drain_until = Some(Instant::now() + STOP_DRAIN);
        }
        self.shared.cond.notify_all();
        let mut threads = Vec::new();
        if let Some(m) = self.media.as_mut() {
            // The writer drains and exits on `closed`.
            if let Some(w) = (!m.threads.is_empty()).then(|| m.threads.remove(0)) {
                let _ = w.join();
            }
            threads = std::mem::take(&mut m.threads);
        }
        self.shared.stopping.store(true, Ordering::Release);
        self.shared.cond.notify_all();
        let r = self.session.teardown(STOP_TEARDOWN_TIMEOUT);
        if let Some(m) = self.media.take() {
            let _ = m.data_sock.shutdown(std::net::Shutdown::Both);
            if let Some(a) = &m.audio {
                debug!("airplay: sent {} audio frames", a.frames);
            }
        }
        // A `/feedback` blocked on a receiver that stopped answering is
        // woken by the shutdown above on Linux and macOS, not on Windows
        // (`util::thread`): leave it to its own timeout.
        let deadline = Instant::now() + STOP_JOIN;
        for t in threads {
            crate::util::thread::join_until(t, deadline);
        }
        debug!("airplay: {}: stopped", self.name);
        r
    }
}

impl<S: ControlStream> Drop for MirrorSink<S> {
    fn drop(&mut self) {
        let _ = self.stop();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::airplay::fake_mirror::{Behaviour, FakeMirror, CLOCK_ID};
    use crate::airplay::policy::Leads;
    use crate::codec::bplist::Plist;
    use crate::net::rtsp::RtspClient;
    use crate::util::vectors;

    fn connect(fake: &FakeMirror, opts: &MirrorOptions) -> MirrorSink<TcpStream> {
        try_connect(fake, opts).unwrap()
    }

    fn try_connect(fake: &FakeMirror, opts: &MirrorOptions) -> Result<MirrorSink<TcpStream>> {
        connect_with("fake".into(), opts, || {
            let tcp = TcpStream::connect(fake.addr).unwrap();
            Ok((RtspClient::new(tcp), fake.addr, fake.secret.clone(), true))
        })
    }

    fn avcc() -> (Vec<u8>, Vec<u8>, Vec<u8>) {
        let b = vectors::load("airplay-mirror/video.txt")
            .into_iter()
            .find(|b| b.name() == "avcC record for codec packet")
            .unwrap();
        let (sps, pps) = (b.hex("sps"), b.hex("pps"));
        let mut rec = vec![1, sps[1], sps[2], sps[3], 0xff, 0xe1];
        rec.extend_from_slice(&(sps.len() as u16).to_be_bytes());
        rec.extend_from_slice(&sps);
        rec.push(1);
        rec.extend_from_slice(&(pps.len() as u16).to_be_bytes());
        rec.extend_from_slice(&pps);
        (rec, sps, pps)
    }

    fn nal(bytes: &[u8]) -> Vec<u8> {
        [&(bytes.len() as u32).to_be_bytes()[..], bytes].concat()
    }

    fn video_info() -> VideoInfo {
        VideoInfo {
            codec: VideoCodec::H264,
            width: 1920,
            height: 1080,
            fps: Ratio::integer(30),
            avcc: avcc().0,
        }
    }

    fn audio_info() -> AudioInfo {
        AudioInfo {
            codec: AudioCodec::PcmS16le,
            sample_rate: 44_100,
            channels: 2,
            frame_samples: 352,
            config: Vec::new(),
        }
    }

    fn video(i: u64, sps: &[u8], pps: &[u8]) -> EncodedFrame {
        let idr = i % 15 == 0;
        let data = if idr {
            [
                nal(&[0x09, 0xf0]),
                nal(sps),
                nal(pps),
                nal(&[0x65, 0x88, i as u8]),
            ]
            .concat()
        } else {
            [nal(&[0x09, 0xf0]), nal(&[0x41, 0x9a, i as u8])].concat()
        };
        EncodedFrame {
            track: TrackKind::Video,
            pts: i * 3000,
            keyframe: idr,
            data: data.into(),
        }
    }

    fn pcm(i: u64) -> Vec<u8> {
        (0..352 * 2)
            .flat_map(|s| ((s as i16).wrapping_mul(i as i16 + 1)).to_le_bytes())
            .collect()
    }

    fn audio(i: u64) -> EncodedFrame {
        EncodedFrame {
            track: TrackKind::Audio(AudioFamily::AirPlay),
            pts: i * 352,
            keyframe: true,
            data: pcm(i).into(),
        }
    }

    fn wait_for(what: &str, mut f: impl FnMut() -> bool) {
        let until = Instant::now() + Duration::from_secs(5);
        while !f() {
            assert!(Instant::now() < until, "timed out waiting for {what}");
            std::thread::sleep(Duration::from_millis(10));
        }
    }

    /// Streams 1.3 s of 30 fps video and audio to the fake receiver.
    fn stream(sink: &mut MirrorSink<TcpStream>, frames: u64) -> Vec<SinkStatus> {
        stream_with_stall(sink, frames, None).statuses
    }

    /// What `stream_with_stall` pushed and saw.
    struct Streamed {
        statuses: Vec<SinkStatus>,
        /// The most a push came after its due time (a loaded machine
        /// oversleeping). The audio pushed with video frame `i` is due
        /// with it, so the clock sees it up to [`FRAME`] later still.
        late: Duration,
        /// Per audio frame, right after pushing it: where the sink's
        /// clock put media time 0 (its origin), relative to the start of
        /// the schedule, in ms. Changes are the clock's re-anchors.
        origins_ms: Vec<f64>,
    }

    /// One video frame at 30 fps.
    const FRAME: Duration = Duration::from_micros(33_333);

    /// Like `stream`, but the source stalls for `stall.1` before frame
    /// `stall.0` (like ffmpeg lagging on a badly interleaved file): the
    /// frames after it arrive late and in a burst.
    fn stream_with_stall(
        sink: &mut MirrorSink<TcpStream>,
        frames: u64,
        stall: Option<(u64, Duration)>,
    ) -> Streamed {
        let (_, sps, pps) = avcc();
        let t0 = Instant::now();
        // The clock's origin, read through a pts far after the anchor.
        const PROBE: u64 = 3600 * VIDEO_CLOCK_RATE as u64;
        let origin_ms = |sink: &MirrorSink<TcpStream>| {
            let at = sink
                .media
                .as_ref()?
                .clock
                .instant_for(PROBE, VIDEO_CLOCK_RATE)?;
            let at = at.checked_sub(ticks_to_duration(PROBE, VIDEO_CLOCK_RATE))?;
            Some(match at.checked_duration_since(t0) {
                Some(d) => d.as_secs_f64() * 1000.0,
                None => -(t0 - at).as_secs_f64() * 1000.0,
            })
        };
        let mut s = Streamed {
            statuses: Vec::new(),
            late: Duration::ZERO,
            origins_ms: Vec::new(),
        };
        let mut a = 0u64;
        for i in 0..frames {
            let mut due = t0 + FRAME * i as u32;
            if let Some((at, d)) = stall {
                if i >= at {
                    due += d;
                }
            }
            if let Some(d) = due.checked_duration_since(Instant::now()) {
                std::thread::sleep(d);
            }
            s.late = s.late.max(Instant::now().saturating_duration_since(due));
            s.statuses.push(sink.push(&video(i, &sps, &pps)).unwrap());
            // Audio up to this video frame's time.
            while a * 352 * 90_000 / 44_100 <= i * 3000 {
                s.late = s.late.max(Instant::now().saturating_duration_since(due));
                s.statuses.push(sink.push(&audio(a)).unwrap());
                s.origins_ms.push(origin_ms(sink).unwrap_or(f64::NAN));
                a += 1;
            }
        }
        s
    }

    #[test]
    fn streams_to_fake_receiver() {
        let fake = FakeMirror::start(Behaviour::default());
        let opts = MirrorOptions::default();
        let mut sink = connect(&fake, &opts);
        let caps = sink.capabilities().clone();
        assert_eq!((caps.max_width, caps.max_height), (1280, 720));
        assert_eq!(caps.audio_formats.len(), 1);
        assert_eq!(caps.audio_family, AudioFamily::AirPlay);
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        let statuses = stream(&mut sink, 40);
        assert!(statuses.iter().all(|s| *s != SinkStatus::Finished));
        wait_for("audio and video", || {
            let r = fake.rec();
            r.video.iter().filter(|p| p.kind == 0).count() == 40 && !r.retransmits.is_empty()
        });
        sink.stop().unwrap();
        sink.stop().unwrap();

        let r = fake.rec();
        assert_eq!(
            r.requests,
            [
                "GET /info",
                &format!("SETUP rtsp://{}/{}", fake.addr, sink.session.audio_id),
                "GET /info",
                &format!("RECORD rtsp://{}/{}", fake.addr, sink.session.audio_id),
                &format!("SETUP rtsp://{}/{}", fake.addr, sink.session.audio_id),
                &format!("SETUP rtsp://{}/{}", fake.addr, sink.session.video_id),
                "POST /feedback",
            ]
            .iter()
            .map(|s| s.to_string())
            .chain(r.requests[7..].iter().cloned())
            .collect::<Vec<_>>()
        );
        assert_eq!(
            r.requests.last().unwrap(),
            &format!("TEARDOWN rtsp://{}/{}", fake.addr, sink.session.audio_id)
        );
        assert!(r.teardown);
        assert_eq!(r.event_replies, 1);
        assert!(r.setup_bodies[0].get("timingPeerInfo").is_some());

        // Video: codec packet, then IDR at the same timestamp; strictly
        // increasing timestamps; SPS/PPS/AUD stripped; heartbeat.
        let (_, sps, pps) = avcc();
        let v = &r.video;
        assert_eq!(v[0].kind, 1);
        assert_eq!(v[0].payload, avcc_record(&sps, &pps).unwrap());
        assert_eq!((v[1].kind, v[1].idr, v[1].ts), (0, true, v[0].ts));
        assert_eq!(v[1].payload, nal(&[0x65, 0x88, 0]));
        assert_eq!(v[1].timeline, CLOCK_ID);
        let frames: Vec<&crate::airplay::fake_mirror::VideoPacket> =
            v.iter().filter(|p| p.kind == 0).collect();
        assert!(frames.windows(2).all(|w| w[1].ts > w[0].ts));
        assert_eq!(frames[1].payload, nal(&[0x41, 0x9a, 1]));
        assert!(!frames[1].idr && frames[15].idr);
        // In-band SPS equal to the avcC one: no second codec packet.
        assert_eq!(v.iter().filter(|p| p.kind == 1).count(), 1);
        assert!(v.iter().any(|p| p.kind == 2), "heartbeat");
        // ~33 ms apart on the receiver's timeline (PTP anchored at the
        // SETUP headers: 1201707.448 s).
        let dt = frames[30].ts - frames[0].ts;
        let secs = dt as f64 / 4_294_967_296.0;
        assert!((0.9..1.2).contains(&secs), "{secs}");
        assert!(frames[0].ts >> 32 >= 1_201_707);

        // Audio: sequence from 1, RTP from 352, verbatim ALAC of our PCM.
        let a = &r.audio;
        assert!(a.len() > 20, "{}", a.len());
        assert_eq!((a[0].0, a[0].1), (1, 352));
        assert_eq!(a[1].1 - a[0].1, 352);
        assert_eq!(a[0].2.len(), 1416);
        // First sync: X bit, PTP form, rtp_play = rtp_now - 3748.
        assert_eq!(&r.sync[0][..4], &[0x90, 0xd7, 0, 4]);
        let play = u32::from_be_bytes(r.sync[0][4..8].try_into().unwrap());
        assert_eq!(play, 352u32.wrapping_sub(Leads::new(None).audio_samples()));
        assert_eq!(&r.sync[0][20..28], &CLOCK_ID.to_be_bytes());
        assert!(r.sync.len() >= 2 && r.sync[1][0] == 0x80);
        // Retransmit answers for seq 1 and 2 carry the stored PT 96 copies.
        assert_eq!(r.retransmits.len(), 2);
        for (i, p) in r.retransmits.iter().enumerate() {
            assert_eq!(&p[..4], &[0x80, 0xd6, 0, 9]);
            assert_eq!(p[5], 96);
            assert_eq!(u16::from_be_bytes([p[6], p[7]]), i as u16 + 1);
        }
    }

    #[test]
    fn audio_payload_is_verbatim_alac() {
        let fake = FakeMirror::start(Behaviour {
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        let late = stream_with_stall(&mut sink, 10, None).late;
        wait_for("audio", || fake.rec().audio.len() >= 5);
        sink.stop().unwrap();
        let r = fake.rec();
        // Audio before the first video frame is dropped, so find the first
        // sent frame among the pushed ones; the rest follow in order.
        let first = (0..20u64)
            .find(|&src| alac_verbatim(&pcm(src), 2).unwrap() == r.audio[0].2)
            .expect("ALAC payload of a pushed frame");
        assert_eq!(r.audio[0].1, 352);
        for (k, (seq, ts, payload)) in r.audio.iter().take(5).enumerate() {
            assert_eq!(*seq as usize, k + 1);
            assert_eq!(*payload, alac_verbatim(&pcm(first + k as u64), 2).unwrap());
            if k > 0 {
                // 352 apart, unless a push came so late (a loaded machine)
                // that the clock re-anchored: then the RTP time has a gap.
                let step = ts.wrapping_sub(r.audio[k - 1].1);
                if late < MAX_LATENESS / 2 {
                    assert_eq!(step, 352, "{late:?}");
                } else {
                    assert!((352..1 << 20).contains(&step), "{step} {late:?}");
                }
            }
        }
    }

    #[test]
    fn receiver_closing_data_finishes() {
        let fake = FakeMirror::start(Behaviour {
            close_after_packets: Some(3),
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), None).unwrap();
        let (_, sps, pps) = avcc();
        let mut end = None;
        for i in 0..200 {
            match sink.push(&video(i, &sps, &pps)) {
                Ok(SinkStatus::Finished) => {
                    end = Some(SinkStatus::Finished);
                    break;
                }
                Err(e) => panic!("the receiver ending mirroring is no failure: {e}"),
                _ => std::thread::sleep(Duration::from_millis(20)),
            }
        }
        assert_eq!(end, Some(SinkStatus::Finished));
        sink.stop().unwrap();
    }

    /// Pushes video until `push` says something other than Accepted or
    /// Dropped.
    fn push_until_end(sink: &mut MirrorSink<TcpStream>) -> Result<SinkStatus> {
        let (_, sps, pps) = avcc();
        let until = Instant::now() + Duration::from_secs(15);
        for i in 0.. {
            assert!(Instant::now() < until, "the sink never noticed");
            match sink.push(&video(i, &sps, &pps)) {
                Ok(SinkStatus::Accepted | SinkStatus::Dropped) => {}
                other => return other,
            }
            std::thread::sleep(Duration::from_millis(20));
        }
        unreachable!()
    }

    /// The receiver goes off the network: its connections break or go
    /// quiet without the data connection being closed. That is a
    /// failure (the session may reconnect), not the end of the session.
    #[test]
    fn vanished_receiver_fails_sink() {
        let fake = FakeMirror::start(Behaviour {
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), None).unwrap();
        stream(&mut sink, 5);
        fake.vanish();
        let e = push_until_end(&mut sink).unwrap_err();
        assert_ne!(e.kind(), crate::error::ErrorKind::Unsupported, "{e}");
        let t = Instant::now();
        let _ = sink.stop();
        assert!(t.elapsed() < Duration::from_secs(6), "{:?}", t.elapsed());
        // It serves the next connection as a new session.
        let mut again = connect(&fake, &MirrorOptions::default());
        again.start(&video_info(), None).unwrap();
        stream(&mut again, 5);
        wait_for("second session", || {
            fake.rec()
                .video
                .iter()
                .any(|p| p.session == 1 && p.kind == 0)
        });
        again.stop().unwrap();
    }

    /// `/feedback` goes unanswered: lost after the feedback timeout.
    #[test]
    fn unanswered_feedback_fails_sink() {
        let fake = FakeMirror::start(Behaviour {
            hang_feedback: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let opts = MirrorOptions {
            feedback_timeout: Duration::from_millis(400),
            ..MirrorOptions::default()
        };
        let mut sink = connect(&fake, &opts);
        sink.start(&video_info(), None).unwrap();
        let t0 = Instant::now();
        let e = push_until_end(&mut sink).unwrap_err();
        assert_eq!(e.kind(), crate::error::ErrorKind::Timeout, "{e}");
        assert!(t0.elapsed() >= Duration::from_millis(400));
        let _ = sink.stop();
    }

    #[test]
    fn receiver_keyframe_request() {
        let fake = FakeMirror::start(Behaviour {
            force_keyframe: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), None).unwrap();
        let (_, sps, pps) = avcc();
        assert!(sink.push(&video(0, &sps, &pps)).unwrap() == SinkStatus::Accepted);
        wait_for("forceKeyFrame", || sink.wants_keyframe());
        // A non-IDR leaves the request pending; the next IDR serves it.
        sink.push(&video(1, &sps, &pps)).unwrap();
        assert!(sink.wants_keyframe());
        assert_eq!(
            sink.push(&video(15, &sps, &pps)).unwrap(),
            SinkStatus::Accepted
        );
        assert!(!sink.wants_keyframe());
        sink.stop().unwrap();
    }

    #[test]
    fn ptp_without_anchor_falls_back_to_ntp() {
        let fake = FakeMirror::start(Behaviour {
            no_timing_headers: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        assert!(matches!(sink.session.timeline, Timeline::Ntp));
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        stream(&mut sink, 3);
        sink.stop().unwrap();
        let r = fake.rec();
        let protocols: Vec<&str> = r
            .setup_bodies
            .iter()
            .filter_map(|b| b.get("timingProtocol").and_then(Plist::as_str))
            .collect();
        assert_eq!(protocols, ["PTP", "NTP"]);
        // The failed PTP session was released before the retry.
        let first_teardown = r
            .requests
            .iter()
            .position(|q| q.starts_with("TEARDOWN"))
            .unwrap();
        let second_info = r
            .requests
            .iter()
            .skip(1)
            .position(|q| q == "GET /info")
            .unwrap()
            + 1;
        assert!(first_teardown < second_info, "{:?}", r.requests);
        assert!(r.video.iter().any(|p| p.kind == 0));
    }

    #[test]
    fn stalled_receiver_fails_the_sink() {
        let fake = FakeMirror::start(Behaviour {
            stall_data: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), None).unwrap();
        let (_, sps, pps) = avcc();
        let big = |i: u64| {
            let mut f = video(i, &sps, &pps);
            let mut d = f.data.to_vec();
            d.extend(nal(&vec![0x41; 256 * 1024]));
            f.data = d.into();
            f
        };
        let t0 = Instant::now();
        let mut dropped = 0;
        let err = loop {
            assert!(t0.elapsed() < Duration::from_secs(20), "never failed");
            match sink.push(&big(dropped % 15)) {
                Ok(SinkStatus::Dropped) => dropped += 1,
                Ok(_) => {}
                Err(e) => break e,
            }
            std::thread::sleep(Duration::from_millis(2));
        };
        assert!(dropped > 0, "queue never filled");
        assert!(sink.wants_keyframe());
        assert_eq!(err.kind(), crate::error::ErrorKind::Timeout, "{err}");
        let t = Instant::now();
        sink.stop().unwrap();
        assert!(t.elapsed() < Duration::from_secs(3));
    }

    #[test]
    fn legacy_profile_media() {
        let master = [0x25; 16];
        let eiv = [0xf0; 16];
        let fake = FakeMirror::start(Behaviour {
            legacy: Some((master, eiv)),
            nack_after: None,
            ..Behaviour::default()
        });
        let tcp = TcpStream::connect(fake.addr).unwrap();
        let opts = MirrorOptions::default();
        let mut session = Session::establish(
            RtspClient::new(tcp),
            fake.addr,
            fake.secret.clone(),
            false,
            &opts,
        )
        .map_err(SetupError::into_error)
        .unwrap();
        // NTP (PTP needs HAP); no ChaCha audio key.
        assert!(matches!(session.timeline, Timeline::Ntp));
        assert!(session.audio.key.is_none());
        // The fake does no FairPlay; give the session fixed keys.
        session.fairplay = Some(crate::airplay::session::FairPlayKeys {
            master,
            eiv,
            ekey: [0; 72],
        });
        let mut sink = MirrorSink::new("legacy".into(), session, &opts);
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        stream(&mut sink, 20);
        wait_for("media", || {
            let r = fake.rec();
            r.video.iter().filter(|p| p.kind == 0).count() == 20 && r.audio.len() > 5
        });
        sink.stop().unwrap();
        let r = fake.rec();
        assert_eq!(r.event_replies, 1, "plaintext event channel");
        let frames: Vec<_> = r.video.iter().filter(|p| p.kind == 0).collect();
        assert_eq!(frames[0].payload, nal(&[0x65, 0x88, 0]));
        assert_eq!(frames[19].payload, nal(&[0x41, 0x9a, 19]));
        assert_eq!(frames[0].timeline, 0);
        let first = (0..20u64)
            .find(|&src| alac_verbatim(&pcm(src), 2).unwrap() == r.audio[0].2)
            .expect("CBC audio decrypts to a pushed frame");
        assert_eq!(r.audio[1].2, alac_verbatim(&pcm(first + 1), 2).unwrap());
        // The legacy SETUP #1 carries the FairPlay root fields... only when
        // FairPlay ran during setup, which the fake cannot do.
        assert!(r.setup_bodies[0].get("ekey").is_none());
        assert_eq!(
            r.setup_bodies[0]
                .get("timingProtocol")
                .and_then(Plist::as_str),
            Some("NTP")
        );
    }

    /// Legacy without FairPlay (§5.2, untested on any receiver): random
    /// `shk` keys the AES-CTR video, audio goes in the clear, no ekey.
    #[test]
    fn legacy_without_fairplay() {
        let fake = FakeMirror::start(Behaviour {
            legacy: Some(([0; 16], [0; 16])),
            legacy_no_fairplay: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let tcp = TcpStream::connect(fake.addr).unwrap();
        let opts = MirrorOptions::default();
        let session = Session::establish(
            RtspClient::new(tcp),
            fake.addr,
            fake.secret.clone(),
            false,
            &opts,
        )
        .map_err(SetupError::into_error)
        .unwrap();
        assert!(session.fairplay.is_none());
        let mut sink = MirrorSink::new("legacy".into(), session, &opts);
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        stream(&mut sink, 10);
        wait_for("media", || {
            let r = fake.rec();
            r.video.iter().filter(|p| p.kind == 0).count() == 10 && r.audio.len() > 3
        });
        sink.stop().unwrap();
        let r = fake.rec();
        assert!(r.setup_bodies.iter().all(|b| b.get("ekey").is_none()));
        let frames: Vec<_> = r.video.iter().filter(|p| p.kind == 0).collect();
        assert_eq!(frames[0].payload, nal(&[0x65, 0x88, 0]));
        assert_eq!(frames[9].payload, nal(&[0x41, 0x9a, 9]));
        assert!((0..20u64).any(|src| alac_verbatim(&pcm(src), 2).unwrap() == r.audio[0].2));
    }

    /// A receiver offering FairPlay with the HAP profile: a build without
    /// FairPlay skips it (the §5.3 placeholders), a build with it tries
    /// `/fp-setup` (404 from the fake) and goes on without. Both stream.
    #[test]
    fn hap_receiver_offering_fairplay_streams_either_way() {
        let fake = FakeMirror::start(Behaviour {
            fairplay_bit: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let opts = MirrorOptions::default();
        let mut sink = connect(&fake, &opts);
        assert!(sink.session.fairplay.is_none());
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        stream(&mut sink, 5);
        wait_for("video", || {
            fake.rec().video.iter().filter(|p| p.kind == 0).count() == 5
        });
        sink.stop().unwrap();
        let tried = fake.rec().requests.iter().any(|r| r.contains("/fp-setup"));
        assert_eq!(tried, crate::airplay::session::FAIRPLAY_BUILT);
    }

    /// The legacy profile with feature 14 needs FairPlay: without it the
    /// session fails with `fairplay-required` before SETUP.
    #[test]
    fn legacy_receiver_needs_fairplay() {
        let fake = FakeMirror::start(Behaviour {
            legacy: Some(([0; 16], [0; 16])),
            legacy_no_fairplay: true,
            fairplay_bit: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let tcp = TcpStream::connect(fake.addr).unwrap();
        let r = Session::establish(
            RtspClient::new(tcp),
            fake.addr,
            fake.secret.clone(),
            false,
            &MirrorOptions::default(),
        )
        .map_err(SetupError::into_error);
        if crate::airplay::session::FAIRPLAY_BUILT {
            // /fp-setup is 404: legacy without FairPlay, as before.
            assert!(r.unwrap().fairplay.is_none());
        } else {
            let e = r.err().unwrap();
            assert_eq!(e.code(), Some("fairplay-required"), "{e}");
            assert!(e.to_string().contains("`fairplay` feature"), "{e}");
            assert!(fake.rec().setup_bodies.is_empty());
        }
    }

    /// A receiver that refuses the session after FairPlay was skipped:
    /// `fairplay-required` (with FairPlay built in, the refusal itself).
    #[test]
    fn refusal_without_fairplay_names_it() {
        let fake = FakeMirror::start(Behaviour {
            fairplay_bit: true,
            refuse_session_setup: Some(403),
            nack_after: None,
            ..Behaviour::default()
        });
        let tcp = TcpStream::connect(fake.addr).unwrap();
        let e = Session::establish(
            RtspClient::new(tcp),
            fake.addr,
            fake.secret.clone(),
            true,
            &MirrorOptions::default(),
        )
        .map_err(SetupError::into_error)
        .err()
        .unwrap();
        assert_eq!(
            e.code() == Some("fairplay-required"),
            !crate::airplay::session::FAIRPLAY_BUILT,
            "{e}"
        );
    }

    /// Regression (audio stopped on a QuickTime recording): when the
    /// source stalls and the media clock re-anchors, the RTP timestamps
    /// get the same gap, so every sync packet stays on one sample<->time
    /// line (the receiver keeps its first mapping and would otherwise
    /// count every later packet as late).
    #[test]
    fn source_stall_keeps_audio_mapping() {
        let fake = FakeMirror::start(Behaviour {
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        let stall = Duration::from_millis(400);
        let st = stream_with_stall(&mut sink, 84, Some((36, stall)));
        wait_for("sync after the stall", || fake.rec().sync.len() >= 3);
        sink.stop().unwrap();
        let r = fake.rec();
        let ms = |d: Duration| d.as_secs_f64() * 1000.0;
        let samples_ms = |n: u32| n as i32 as f64 * 1000.0 / 44_100.0;
        // The received packets by audio frame (the payload tells which).
        let index: std::collections::HashMap<Vec<u8>, usize> = (0..st.origins_ms.len())
            .map(|a| (alac_verbatim(&pcm(a as u64), 2).unwrap(), a))
            .collect();
        let got: Vec<(usize, u32)> = r
            .audio
            .iter()
            .map(|(_, rtp, payload)| (index[payload], *rtp))
            .collect();
        let (a0, rtp0) = got[0];
        let origin0 = st.origins_ms[a0];
        // RTP gaps (beyond 352 per frame) appear exactly where and by as
        // much as the clock re-anchored (moved its origin), so every
        // packet stays on the first sample<->time line.
        let mut gaps_ms = Vec::new();
        let mut last_gap = 0.0;
        for &(a, rtp) in &got {
            let gap = samples_ms(rtp.wrapping_sub(rtp0).wrapping_sub((a - a0) as u32 * 352));
            let moved = st.origins_ms[a] - origin0;
            assert!(
                (gap - moved).abs() < 0.1,
                "audio frame {a}: RTP gap {gap} ms, clock moved {moved} ms since frame {a0}"
            );
            if gap - last_gap > 0.1 {
                gaps_ms.push(gap - last_gap);
            }
            last_gap = gap;
        }
        // The re-anchors add up to the real stall plus lateness: the
        // origin starts at the first push (or at a re-anchor before audio
        // started) and ends at a frame after the stall, each at most
        // `late` plus the audio's lag behind video (one frame) after the
        // schedule. Each re-anchor is a frame more than MAX_LATENESS late.
        let (late_ms, stall_ms) = (ms(st.late + FRAME) + 5.0, ms(stall));
        let end = *st.origins_ms.last().unwrap();
        let what = format!(
            "gaps {gaps_ms:?} ms, {:?} late, origins {origin0} -> {end} ms",
            st.late
        );
        assert!((-1.0..=late_ms).contains(&origin0), "{what}");
        assert!(
            (stall_ms - 1.0..=stall_ms + late_ms).contains(&end),
            "{what}"
        );
        let total: f64 = gaps_ms.iter().sum();
        assert!(
            (stall_ms - origin0 - 1.0..=stall_ms + late_ms).contains(&total),
            "{what}"
        );
        assert!(
            gaps_ms.iter().all(|&g| g >= ms(MAX_LATENESS) - 1.0),
            "{what}"
        );
        // On an idle machine no push is MAX_LATENESS late: the stall is
        // the one re-anchor.
        if late_ms < ms(MAX_LATENESS) {
            assert_eq!(gaps_ms.len(), 1, "{what}");
        }
        // Every sync packet: (rtp_play, T ns). All on one line.
        let points: Vec<(u32, u64)> = r
            .sync
            .iter()
            .map(|p| {
                (
                    u32::from_be_bytes(p[4..8].try_into().unwrap()),
                    u64::from_be_bytes(p[8..16].try_into().unwrap()),
                )
            })
            .collect();
        let (r0, t0) = points[0];
        for &(rtp, t) in &points[1..] {
            let by_rtp = rtp.wrapping_sub(r0) as f64 / 44_100.0;
            let by_time = (t - t0) as f64 / 1e9;
            assert!(
                (by_rtp - by_time).abs() < 0.003,
                "sync mapping moved: {by_rtp} s of samples vs {by_time} s"
            );
        }
        // Only the first sync packet has the X bit.
        assert!(r.sync[1..].iter().all(|p| p[0] == 0x80));
    }

    /// UDP from another sender reaching the fake's audio ports (a sink of
    /// another test still sending to a port that was freed and reused) is
    /// ignored and counted; before, it failed authentication and killed
    /// the fake's audio thread (seen once in a loaded run).
    #[test]
    fn fake_receiver_ignores_stray_udp() {
        let fake = FakeMirror::start(Behaviour {
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        let a = sink.media.as_ref().unwrap().audio.as_ref().unwrap();
        let (data, rtcp) = (a.data.peer_addr().unwrap(), a.rtcp);
        let stray = UdpSocket::bind("127.0.0.1:0").unwrap();
        let mut junk = vec![0x80u8, 0x60, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0];
        junk.extend_from_slice(&[7; 40]);
        let send = || {
            stray.send_to(&junk, data).unwrap();
            stray.send_to(&junk, rtcp).unwrap();
        };
        // Before our sender's first packet, and between its packets.
        send();
        stream(&mut sink, 10);
        send();
        stream(&mut sink, 10);
        wait_for("audio, sync and four strays", || {
            let r = fake.rec();
            !r.audio.is_empty() && !r.sync.is_empty() && r.strays == 4
        });
        sink.stop().unwrap();
        let r = fake.rec();
        assert_eq!(r.strays, 4);
        // Only our sync packets (type 0x57, marker set) got through.
        assert!(r.sync.iter().all(|p| p[1] == 0xd7));
    }

    /// A receiver that stops answering must not make `stop` (Ctrl-C) wait
    /// for the 30 s request timeout of the pending `/feedback`.
    #[test]
    fn stop_is_bounded_when_the_receiver_hangs() {
        let fake = FakeMirror::start(Behaviour {
            hang_feedback: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        stream(&mut sink, 5);
        wait_for("feedback", || {
            fake.rec().requests.iter().any(|r| r == "POST /feedback")
        });
        let t = Instant::now();
        let _ = sink.stop();
        assert!(t.elapsed() < Duration::from_secs(6), "{:?}", t.elapsed());
    }

    #[test]
    fn eof_after_teardown_is_a_clean_stop() {
        let fake = FakeMirror::start(Behaviour {
            teardown_eof: true,
            nack_after: None,
            ..Behaviour::default()
        });
        let mut sink = connect(&fake, &MirrorOptions::default());
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        stream(&mut sink, 5);
        sink.stop().unwrap();
        assert!(fake.rec().teardown);
    }

    #[test]
    fn media_first_ordering() {
        let fake = FakeMirror::start(Behaviour {
            refuse_session_setup: Some(455),
            nack_after: None,
            ..Behaviour::default()
        });
        let opts = MirrorOptions::default();
        let mut sink = connect(&fake, &opts);
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        stream(&mut sink, 5);
        sink.stop().unwrap();
        let r = fake.rec();
        let audio_uri = format!("rtsp://{}/{}", fake.addr, sink.session.audio_id);
        assert_eq!(
            r.requests[..6],
            [
                "GET /info".to_string(),
                format!("SETUP {audio_uri}"),
                format!("SETUP {audio_uri}"),
                "GET /info".into(),
                format!("SETUP rtsp://{}/{}", fake.addr, sink.session.video_id),
                format!("RECORD {audio_uri}"),
            ]
        );
        // The media-first SETUP carries the session and the stream.
        let mf = &r.setup_bodies[1];
        assert!(mf.get("sessionUUID").is_some() && mf.get("streams").is_some());
        assert!(mf.get("updateSessionRequest").is_none());
        assert!(r.video.iter().any(|p| p.kind == 0));
    }

    #[test]
    fn ntp_session() {
        let fake = FakeMirror::start(Behaviour {
            nack_after: None,
            ..Behaviour::default()
        });
        let opts = MirrorOptions {
            ptp: false,
            ..MirrorOptions::default()
        };
        let mut sink = connect(&fake, &opts);
        assert!(matches!(sink.session.timeline, Timeline::Ntp));
        sink.start(&video_info(), Some(&audio_info())).unwrap();
        stream(&mut sink, 5);
        wait_for("sync", || !fake.rec().sync.is_empty());
        sink.stop().unwrap();
        let r = fake.rec();
        assert_eq!(
            r.setup_bodies[0]
                .get("timingProtocol")
                .and_then(Plist::as_str),
            Some("NTP")
        );
        assert!(r.setup_bodies[0].get("timingPort").is_some());
        assert_eq!(&r.sync[0][..2], &[0x90, 0xd4]);
        assert_eq!(r.sync[0].len(), 20);
        let f = r.video.iter().find(|p| p.kind == 0).unwrap();
        assert_eq!(f.timeline, 0);
    }
}