rtci-interceptor 0.5.0

Sans-I/O RTP/RTCP interceptor pipeline for WebRTC: RTCP reports, NACK, TWCC, congestion control, pacing, FEC
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
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
//! Smooths outgoing packets to a target rate.

use super::pacer::Pacer as LeakyBucket;
use crate::Interceptor;
use crate::StreamInfo;
use crate::cc::probe::{ProbeFill, ProbePacket, ProbeRequest};
use crate::keyframe::LocalKeyFrameRequest;
use crate::{Attribute, AttributedPacket, Packet, TaggedPacket};
use rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication;
use sansio::Protocol;
use shared::TransportContext;
use shared::error::Error;
use shared::marshal::MarshalSize;
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// Rate used when none is configured: 1 Mb/s.
pub const DEFAULT_BITRATE: f64 = 1_000_000.0;

/// Packets held before the queue overflows (see [`QueueOverflow`]).
pub const DEFAULT_QUEUE_LIMIT: usize = 4096;

/// How long a packet may wait in the queue by default (libwebrtc's
/// `kMaxExpectedQueueLength`, 2 s); what happens to one that would wait longer is
/// [`QueueOverflow`]'s.
pub const DEFAULT_QUEUE_TIME_LIMIT: Duration = Duration::from_secs(2);

/// What the pacer does with media the target has no room for: a packet that has waited the
/// queue time limit, or the oldest one when a packet arrives at a full queue. It happens only
/// when the application writes faster than the congestion controller's target for longer
/// than the limit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum QueueOverflow {
    /// The media is discarded here, whole frames at a time (see [`PacerDiscard`]; counted by
    /// [`PacerInterceptor::dropped`] and [`PacerInterceptor::discarded_frames`]): nothing
    /// leaves above the target. The congestion controller's target is what the path carries (RFC
    /// 8836 section 2; RFC 8083 section 4 stops a flow that does not keep to it), so a sender
    /// that put the excess on the path anyway would fill the bottleneck's queue, lose there
    /// and drive the estimate to its floor: a 2.4 Mbit/s source on a 600 kbit/s link
    /// (`tests/gcc_rate_limited_link_rtc2rtc.rs`, and netem in
    /// `testenv_impairment::gcc_follows_a_rate_limited_link`) held a target of 500 to 590
    /// kbit/s this way, and fell to 100 kbit/s with [`QueueOverflow::Drain`]. The receiver
    /// counts the dropped packets as lost, which they are; the NACK responder does not resend
    /// them, and the application is asked for a key frame (see [`PacerDiscard`]).
    #[default]
    Drop,
    /// libwebrtc's pacer (`drain_large_queues`, `MaybeUpdateMediaRateDueToLongQueue`): the
    /// pacing rate is raised above the target as far as it takes to drain the queue before
    /// its oldest packet has waited the limit, a packet that has waited it leaves whatever the
    /// budget says, and a full queue releases its oldest over the budget (counted by
    /// [`PacerInterceptor::forced`]). Nothing the application wrote is lost here, and a
    /// source that keeps above the target puts all of it on the path. For an encoder that
    /// follows the target, as libwebrtc's does.
    Drain,
}

/// What the pacer does with what it holds when the path's capacity collapses: the target has
/// fallen to half or less of what it was when the queued media was written or within the
/// queue time bound, and the queue would now take half the queue time bound or more to leave (see "A capacity collapse" on
/// [`PacerInterceptor`]). It applies with [`QueueOverflow::Drop`]; [`QueueOverflow::Drain`]
/// never discards.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Backlog {
    /// The queue is given up: the media and retransmissions waiting are discarded
    /// ([`PacerDiscard`], [`RetransmissionsLeft`]), the NACK responder is told to forget what
    /// was sent before ([`PacerFlush`]) and the application is asked for a key frame of every
    /// video stream. The receiver's next picture is a current one, a key frame sized for the
    /// new rate, in place of seconds of frames sized for the old one delivered in order.
    /// RFC 8834 section 6.1: "It is not appropriate to blindly retransmit RTP packets in
    /// response to a NACK. The importance of lost packets and the likelihood of them arriving
    /// in time to be useful need to be considered"; RFC 4588 section 7: the sender "MAY
    /// selectively retransmit only the packets that it deems important".
    #[default]
    Flush,
    /// The source is paused and everything queued is delivered in order, with every
    /// retransmission asked for ("Pausing the source" on [`PacerInterceptor`]): for a receiver
    /// that plays strictly in order and cannot resume at a key frame. After a sharp drop the
    /// picture such a receiver shows is seconds old.
    ///
    /// "Keep and repair everything" is one choice for a whole chain: the default interceptor
    /// set (`DefaultInterceptorOptions::pacer_backlog`) also switches off the NACK
    /// responder's own rules that give repairs up
    /// ([`NackResponderBuilder::with_max_repair_age`](crate::NackResponderBuilder::with_max_repair_age),
    /// [`with_repair_backlog_bound`](crate::NackResponderBuilder::with_repair_backlog_bound)).
    /// A chain built by hand sets both to `None` on its responder.
    Keep,
}

/// Builder for [`PacerInterceptor`].
///
/// # Example
///
/// ```
/// use rtci_interceptor::{Slot, PacerBuilder, Registry};
///
/// let chain = Registry::new()
///     .with(Slot::Pacer, PacerBuilder::new().with_target_bitrate(2_000_000.0).build())
///     .build();
/// ```
pub struct PacerBuilder {
    bitrate: f64,
    burst_bits: Option<f64>,
    queue_limit: usize,
    queue_time_limit: Option<Duration>,
    queue_overflow: QueueOverflow,
    backlog: Backlog,
    immediate_release: bool,
    audio_in_share: bool,
}

impl Default for PacerBuilder {
    fn default() -> Self {
        Self {
            bitrate: DEFAULT_BITRATE,
            burst_bits: None,
            queue_limit: DEFAULT_QUEUE_LIMIT,
            queue_time_limit: Some(DEFAULT_QUEUE_TIME_LIMIT),
            queue_overflow: QueueOverflow::default(),
            backlog: Backlog::default(),
            immediate_release: true,
            audio_in_share: true,
        }
    }
}

impl PacerBuilder {
    /// Create a builder with the default rate and queue limit.
    pub fn new() -> Self {
        Self::default()
    }

    /// The rate to pace at, in bits per second.
    pub fn with_target_bitrate(mut self, bits_per_second: f64) -> Self {
        self.bitrate = bits_per_second;
        self
    }

    /// How much may be released at once, in bits.
    ///
    /// Larger bursts smooth less but cost fewer wake-ups.
    pub fn with_burst_bits(mut self, burst_bits: f64) -> Self {
        self.burst_bits = Some(burst_bits);
        self
    }

    /// How many packets may be queued. When a packet arrives at a full queue, the oldest one
    /// is dropped or leaves over the budget, as [`Self::with_queue_overflow`] says.
    pub fn with_queue_limit(mut self, queue_limit: usize) -> Self {
        self.queue_limit = queue_limit;
        self
    }

    /// How long a packet may wait in the queue ([`DEFAULT_QUEUE_TIME_LIMIT`] by default,
    /// `None` for no bound). When the application writes faster than the congestion
    /// controller's target, the queue grows; a packet that would wait longer than this is
    /// dropped or drained above the target, as [`Self::with_queue_overflow`] says. The
    /// application learns that its source is above the target from the target itself
    /// (`TargetBitrateChanged`, `RTCRtpSender::target_bitrates`), from
    /// [`PacerInterceptor::queue_time`], and, once media had to be discarded, from
    /// [`PacerDiscard`] (`RTCPeerConnectionEvent::OnPacerDiscard` in the peer connection) and
    /// the key-frame request that goes with it.
    pub fn with_queue_time_limit(mut self, limit: Option<Duration>) -> Self {
        self.queue_time_limit = limit;
        self
    }

    /// What happens to media the target has no room for ([`QueueOverflow::Drop`] by default).
    pub fn with_queue_overflow(mut self, overflow: QueueOverflow) -> Self {
        self.queue_overflow = overflow;
        self
    }

    /// What happens to the queue when the path's capacity collapses ([`Backlog::Flush`] by
    /// default).
    pub fn with_backlog(mut self, backlog: Backlog) -> Self {
        self.backlog = backlog;
        self
    }

    /// Whether a packet the budget already covers leaves when it is written, with nothing
    /// queued ahead of it. On by default.
    ///
    /// The pacer exists to keep a sender from putting more than its target rate times the
    /// burst time on the path at once (draft-ietf-rmcat-gcc-02 section 4: a group of packets
    /// of that size every `burst_time`, 5 ms recommended). The budget is that bound: it never
    /// holds more than one burst and refills at the target rate. A packet inside it adds
    /// nothing to what the bound allows, and holding it until the next wake-up only adds
    /// delay the sender caused itself, which RFC 8836 section 2 (requirement 1) asks a
    /// congestion controller for interactive media to keep as low as possible; no RFC asks
    /// for the wait. `false` restores it: every packet is queued and leaves from the next
    /// timeout, so departures happen only on the driver's clock.
    pub fn with_immediate_release(mut self, immediate_release: bool) -> Self {
        self.immediate_release = immediate_release;
        self
    }

    /// Whether audio takes part in the share by priority (`true`, the default; see "Order of
    /// service" on [`PacerInterceptor`]). RFC 8835 section 4.1 gives each stream at each level
    /// of priority about twice the capacity of the level below, "independent of the media
    /// type": with it on, an audio stream that writes above its share while other streams
    /// wait is held to its share (only those of its packets that would take more than it wait,
    /// the rest leave as they are written); audio within its share is never delayed. `false`
    /// is libwebrtc's pacer (`modules/pacing/pacing_controller.cc`: audio is not paced unless
    /// the `WebRTC-Pacer-BlockAudio` field trial is on, and leaves ahead of every other packet,
    /// `prioritized_packet_queue.cc`): every audio packet leaves when it is written, whatever
    /// it takes from the other streams.
    pub fn with_audio_in_share(mut self, audio_in_share: bool) -> Self {
        self.audio_in_share = audio_in_share;
        self
    }

    /// Build the interceptor.
    pub fn build(self) -> PacerInterceptor {
        let bucket = match self.burst_bits {
            Some(burst_bits) => LeakyBucket::new(self.bitrate).with_burst_bits(burst_bits),
            None => LeakyBucket::new(self.bitrate),
        };
        let mut pacer = PacerInterceptor::new(bucket, self.queue_limit);
        pacer.immediate_release = self.immediate_release;
        pacer.queue_time_limit = self.queue_time_limit;
        pacer.queue_overflow = self.queue_overflow;
        pacer.backlog = self.backlog;
        pacer.audio_in_share = self.audio_in_share;
        pacer
    }
}

/// Releases queued packets at a target rate rather than as fast as they arrive.
///
/// # Where this belongs in the chain
///
/// **Close to the wire, with every generator on the application side of it.** Everything after it
/// on the write path observes the *release* instant, so a send history goes there and records what
/// actually left; placed the other way round it would record the enqueue instant and charge this
/// queueing delay to the network.
///
/// Retransmissions, FEC repair packets and generated RTCP are all produced further out and reach
/// the pacer on the belt, so they are metered along with everything else. Under the nested chain
/// they bypassed it entirely.
///
/// # Differences from upstream
///
/// - **No ticker and no goroutine.** Upstream runs a loop on a 5 ms `time.Ticker`; here the
///   budget is a pure function of the instants handed in, so a release schedule is reproducible
///   rather than merely eventually-correct.
/// - **Idle means idle.** `poll_timeout` returns `None` with nothing queued, so an idle
///   connection does not wake the whole chain at the pacing interval.
/// - **The deadline is when the head can afford to go**, not `now + interval`, so it always
///   advances — a deadline at or before the `now` just handed to `handle_timeout` is the
///   webrtc#862 busy-loop.
///
/// # Order of service
///
/// 1. **Audio** (the packets of a stream bound with an `audio/` codec) leaves the moment it is
///    written, charged to the budget, which the packets behind it then wait out. A voice that
///    waits behind a video backlog is the call breaking up. **Unless it is above its share**
///    (RFC 8835 section 4.1: the share by priority is "independent of the media type"): while
///    other streams have packets waiting, an audio stream is a flow of the share by priority
///    (4, below) with the weight [`PacerPriorities`] gives it, and an audio packet whose start
///    tag is past the lowest start tag of the streams waiting waits (in a queue of its own,
///    each audio stream in the order written) until its turn and the budget come. An audio
///    stream within its share is never past them (its tags lag the virtual time the waiting
///    streams' service moves), so ordinary audio still leaves the moment it is written; one
///    that writes more than its share has that much of it held, and the others get theirs.
///    With nothing else waiting, nothing is held (each stream sends all it has). Held audio
///    leaves as the budget allows once nothing else waits, is dropped (with a [`PacerDiscard`],
///    no key-frame request) once it has waited the queue time bound, and leaves at once at a
///    flush. [`PacerBuilder::with_audio_in_share`] `false` never holds audio (libwebrtc's form).
/// 2. **Audio retransmissions** leave the same way: an audio stream's repair flows (its RTX
///    and FEC SSRCs, from [`Interceptor::bind_local_stream`]) count as audio, and as one flow
///    with their stream in the share, so an audio retransmission never waits behind video
///    either unless its stream is above its share.
/// 3. **Video retransmissions** (`Attribute::Retransmission` on any other stream) wait in the
///    media queue ahead of all new media (libwebrtc's `prioritized_packet_queue.cc`: "Send
///    retransmissions before new media"), a stream's oldest packet first (by the sequence
///    number repaired, whenever each was written: a receiver that plays in order waits for
///    its oldest gap). One whose packet
///    already has a retransmission waiting is not queued again
///    ([`PacerInterceptor::duplicate_retransmissions`]), and each that leaves or is dropped is
///    reported application-ward ([`RetransmissionsLeft`]), so the NACK responder does not make
///    another while one waits.
/// 4. **Video and FEC** (every other RTP packet that is not padding) wait behind them. Each
///    stream's packets leave in the order written; **among the streams with packets waiting the
///    payload octets are shared by priority** (RFC 8835 section 4.1: "each stream at each level
///    of priority is being given approximately twice the transmission capacity (measured in
///    payload bytes) of the level below"), by start-time fair queuing: each stream weighs what
///    [`PacerPriorities`] gives it (very-low 1, low 2, medium 4, high 8; a stream it does not
///    name weighs as `low`, the W3C default), and a bound stream's FEC and RTX streams are one
///    flow with it. A retransmission keeps its place ahead of new media (3), and its payload is
///    charged to its stream's share, so the repairs of a stream change the order, not the
///    shares. Streams of one priority share equally; with one stream waiting, its packets leave
///    in the order written, as before. Only packets that wait are shared out: what leaves the
///    moment it is written (nothing waiting, budget to spare) is not counted, and the shares
///    start afresh each time the queue holds no new media and no audio is held. Audio (1, 2)
///    takes part only while other streams wait (see 1).
///    The **queue time bound** (2 s by default,
///    [`PacerBuilder::with_queue_time_limit`]) and the queue limit are this queue's (3 and 4):
///    by default ([`QueueOverflow::Drop`]) what has waited the bound, or the oldest packet when
///    the queue is full, is discarded, so nothing leaves above the target (a retransmission
///    one packet at a time, new media whole frames at a time: see "Discarding media" below);
///    [`QueueOverflow::Drain`] raises the pacing rate above the target instead (libwebrtc's
///    `MaybeUpdateMediaRateDueToLongQueue`) and releases those packets over the budget.
/// 5. **Padding** (padding-only packets, RFC 3550 section 5.1: the padding bit and no payload,
///    which the NACK responder fills a probe cluster with) waits in a queue of its own and leaves
///    only when no media waits. A probe cluster is written as a burst of padding the moment it is
///    asked for; queued in line with the media it would hold every packet the application writes
///    during the cluster behind all of it (140 to 180 ms for a 6000-octet cluster at 200 kbit/s
///    in `early_media_rtc2rtc`). Media written during a cluster leaves first, at the cluster's
///    rate and with its mark, so it fills the cluster in place of padding, as in libwebrtc's
///    pacer (padding has the lowest priority of its packet queue).
///
/// Padding is outside the queue time bound: it never counts toward raising the pacing rate
/// (with [`QueueOverflow::Drain`]), is never dropped as overdue nor released over the budget,
/// and so never goes ahead of media or past the rate the congestion controller set.
///
/// **A cluster's padding belongs to the cluster.** The pacer tells the responder how much
/// padding a cluster needs beyond the media already queued ([`ProbeFill`]), so a cluster asked
/// while a backlog waits (the pause) is made of that backlog and no padding is written for
/// it. Padding written for a cluster that could not leave in it (media written meanwhile took
/// its place, or the cluster timed out) is **discarded when the cluster ends**, counted by
/// [`PacerInterceptor::padding_discarded`]: outside its cluster it measures nothing, and sent
/// later it is worse than nothing. Each padding packet holds the sequence number of the RTX
/// stream it was given when it was made; kept until budget was spare (seconds, while media
/// waited) it left far behind the stream's newest number, where a receiver's replay
/// protection (RFC 3711 section 3.3.2) and its feedback window no longer take it, inside
/// whatever cluster was being sent then: that cluster's packets were never all reported and
/// the probe gave no result (every cluster after a capacity drop in
/// `tests/capacity_cap_video_rtc2rtc.rs`, 191 stale padding packets queued, and an estimate
/// that stayed at its floor for want of a probe result). Discarding leaves a hole in the RTX
/// stream's sequence numbers, which the receiver's reports count as lost (RFC 3550 appendix
/// A.3); the sender's own readers of those reports take the numbers that never left out of
/// the count (`NeverSent`, in the circuit breaker and the receiver-report fallback of the
/// congestion controller), as for media the pacer discards.
///
/// Padding written while no cluster is being sent (an application's own) keeps its place
/// until budget is spare, however long that takes: it is sent late, not dropped. A padding
/// queue that reaches the queue limit (4096 packets) discards the padding that arrives.
///
/// # Pausing the source
///
/// When the media queued would take half the queue time bound or more to leave at the target
/// ([`PacerInterceptor::expected_queue_time`]; 1 s of the default 2 s), the pacer tells the
/// application to **stop producing**: every [`Attribute::TargetBitrateChanged`] it passes
/// application-ward carries **zero** (so `RTCRtpSender::target_bitrates` and the stats read
/// zero), with [`PacerPause`] carrying the congestion controller's target for the interceptors
/// that need the path's rate, and it announces the change at once on a carrier of its own. When
/// the queue is back under a twentieth of the bound (100 ms), it announces the real target
/// again. libwebrtc does the same: its control handler gives the encoder a target of zero while
/// the pacer's expected queue time exceeds 2 s
/// (`modules/congestion_controller/rtp/control_handler.cc`, `pacing_controller.h`: "the packet
/// producers should wait (eg drop frames rather than encoding them)"). Pausing at half the
/// bound, not at the bound, leaves a source that stops when told the time to notice (a frame
/// interval, a read of its target) and room for the target to fall once more.
///
/// **What a source that follows the pause wrote is never discarded for its age**: the media
/// written before the pause began (and within 250 ms of it, the time to notice) stays queued
/// until it leaves, however long that takes. At a sudden drop of the path's capacity that can be
/// seconds: the target may fall to a tenth of what the queued media was written for, and the
/// retransmissions of what the path lost at the drop leave ahead of it (order of service 3),
/// which is also the order a receiver needs them in. Nothing new joins the queue meanwhile, so
/// it is bounded by what was there when the pause began; discarding it would leave a gap in the
/// stream that costs the receiver more than the wait (a receiver that waits for missing packets
/// by count, as `rtci-media`'s `SampleBuilder` does, sat 9 s on such a gap in
/// `tests/capacity_cap_video_rtc2rtc.rs`). The queue time bound and the discard below are for
/// media written into a pause: a source that does not follow it.
///
/// **What that costs, and why it is not the default at a collapse.** This paragraph and the
/// one before describe a queue kept through a sudden drop: [`Backlog::Keep`], and by default
/// a long queue whose target did not collapse. By default a collapse flushes the queue
/// instead (next section; `docs/capacity-collapse.md`). After a sudden drop the picture a
/// receiver shows can be seconds old while those retransmissions leave: in that test, which
/// runs [`Backlog::Keep`] (2.5 Mbit/s into a path that drops to
/// 300 kbit/s) the receiver never went 2 s without a usable frame (0.75 to 1.13 s), but the
/// oldest picture it showed was 4.14 to 6.58 s old, against 2.90 to 3.20 s in a chain without
/// a pacer. The 55 to 62 video packets the path dropped are repaired inside an estimate of 100
/// to 250 kbit/s of which the audio takes 76, under every retransmission rule (59 to 80
/// retransmissions; a request for a packet whose retransmission the feedback reports received
/// is not answered, see the NACK responder). Dropping the repairs and asking for a key frame
/// instead is worse for a receiver that plays in order and waits for a gap by packet count
/// (measured: frozen for 8 s, as it holds the gap for its whole packet window): such a
/// receiver is what [`Backlog::Keep`] is for.
///
/// **A target of zero means: produce nothing until the next target.** An encoder skips (does
/// not encode) the frames captured meanwhile; it does not need a key frame to resume, because
/// nothing it encoded was lost: the next frame it encodes refers to the last one it sent, which
/// is still queued here and reaches the receiver. libwebrtc resumes the same way: it encodes the
/// last frame captured during the pause, or asks the capturer for a fresh one
/// (`video_stream_encoder.cc`, `OnBitrateUpdated`: "A pending stored frame can be processed",
/// else `RequestRefreshFrame`), and forces no key frame. Audio is not queued here and can carry
/// on; the target is the transport's. Media the application writes anyway is queued and, past
/// the bound, discarded as below, with a key-frame request: that, not the resume, is when a key
/// frame is needed.
///
/// # A capacity collapse
///
/// By default ([`Backlog::Flush`]) the pause above is for a queue that is long at a steady
/// target: a key frame larger than a second of the target, a source that is slow to follow.
/// When the queue is long because **the target collapsed** (a pause would begin or is in
/// force, and the target is now half or less of the highest target in force since the oldest
/// queued new media was written or in the last queue time bound, two seconds by default,
/// whichever reaches further back), the pacer does not pause, or ends the pause. The queue
/// may hold new media, retransmissions or both: the repairs asked for a second after a drop,
/// behind a deep queue on the path, are of media written for the old rate as well. It
/// **flushes**: every queued packet is
/// discarded (new media and FEC through [`PacerDiscard`], whole frames, as below; queued
/// retransmissions through [`RetransmissionsLeft`]), a [`PacerFlush`] tells the NACK responder
/// to forget everything sent before on the video streams, so that later NACKs for it are
/// answered by nothing, and the application is asked for a key frame of every video stream
/// (the Picture Loss Indication described below). The source is not paused: the next frame it
/// writes, at the target it now reads, is the key frame. Audio is never queued and is not
/// touched; padding stays.
///
/// What was queued was sized for a rate the path no longer carries: 0.4 s of 2.5 Mbit/s video
/// and the retransmissions of what the path dropped take 8 s to leave at 100 to 250 kbit/s
/// (`tests/capacity_instrument.rs`: the picture on the screen was 9 s old in the median),
/// against a key frame of a few packets. RFC 8834 section 6.1 asks a sender to consider "the
/// likelihood of [retransmissions] arriving in time to be useful"; RFC 8836 section 2 gives
/// interactive media "no more delay than hundreds of milliseconds end-to-end"; RFC 8298
/// section 3 lets a sender whose queues grow rapidly discard encoded frames "to ensure that
/// the RTP queues are drained quickly". A decoder refresh point "completely resets the
/// decoder" (RFC 5104 section 2.2): nothing sent before it is needed after it.
///
/// A flush is not repeated for what was written after it: no target from before a flush
/// counts again, and the key frame the application writes in answer was written at the fallen
/// target, so a queue it fills is a pause, not a collapse, and is delivered. The target has to
/// halve again, from what it was at the flush, for another one: at most log2(highest /
/// lowest target) flushes in a row, four from 2.5 Mbit/s to a 100 kbit/s floor. The receiver
/// sees a gap in the sequence numbers (the application numbered the discarded packets) and
/// its NACKs for it go unanswered; one that waits for missing packets has to bound that wait
/// in time or resume at the key frame (`docs/capacity-collapse.md`).
/// [`Backlog::Keep`] restores the pause for every long queue.
///
/// # Discarding media
///
/// With [`QueueOverflow::Drop`], new media the target has no room for is discarded **whole
/// frames at a time**. A frame is the packets of one stream (SSRC) with one RTP timestamp (RFC
/// 3550 section 5.1: the packets of one frame share it; the marker bit ends it). A frame with a
/// packet missing is worth nothing to a decoder, and so is every later frame that refers to it,
/// up to the next key frame; a frame that waited two seconds is stale anyway. So when the
/// oldest new-media packet has waited the bound (or is the oldest of a full queue), the pacer
/// discards every queued packet of its stream that is not a retransmission, oldest frame first,
/// and the packets of the newest discarded frame the application writes after that. Nothing
/// marks a key frame on this path (the application writes RTP packets, and only the codec's
/// payload says which frame is a key frame), so no queued frame of the stream is kept as one:
/// the key frame the application writes in answer to the request below is the next frame the
/// stream sends, not one that waits behind the stale ones. Audio, retransmissions and padding
/// are never discarded this way (audio is never queued, a retransmission is dropped alone,
/// padding is sent late).
///
/// For every discard the pacer emits on its **read** leg (the application-ward direction, the
/// only one that reaches the NACK responder application-ward of it) an empty RTCP carrier with
/// [`PacerDiscard`]: the NACK responder removes those packets from its retransmission buffer, so
/// a NACK for them is answered by nothing (RFC 4588 section 7: the sender "MAY selectively
/// retransmit only the packets that it deems important"), and the peer connection hands the
/// discard to the application (`RTCPeerConnectionEvent::OnPacerDiscard`). For a media stream
/// (not a repair flow) it also emits a Picture Loss Indication (RFC 4585 section 6.3.1) for the
/// stream, sender SSRC 0, marked [`Attribute::DeliverToApplication`]: the request a receiver
/// that lost its picture sends, so an application that answers PLI with a key frame
/// (`RTCMessage::RtcpPacket`, `RTCPeerConnectionEvent::OnKeyFrameRequest`) answers this too, at
/// the target the frames it writes now are sized for. The request carries
/// [`LocalKeyFrameRequest`] (`PacerDiscard`, or `PacerFlush` for the requests of a flush), so it
/// can be told from a remote one (`RTCPeerConnectionEvent::OnKeyFrameRequestOrigin`).
///
/// libwebrtc does not discard in its pacer: when the expected queue time passes 2 s it pauses
/// the encoder (target zero to the encoder, `modules/congestion_controller/rtp/control_handler.cc`)
/// and drains the queue above the target ([`QueueOverflow::Drain`]); its frames are dropped
/// before they are numbered. Here the application numbers its packets, so a discarded frame
/// leaves a gap in the stream's sequence numbers that the receiver sees as loss.
pub struct PacerInterceptor {
    pacer: LeakyBucket,
    /// The rate the bucket was set to before the queue time limit raised it, while it is
    /// raised: the target to come back to (the congestion controller's, or one set through
    /// [`Self::pacer_mut`]).
    raised_from: Option<f64>,
    /// The congestion controller's minimum that rode with its last target
    /// ([`MinimumTarget`](crate::MinimumTarget)), to ride with the target this pacer announces
    /// itself when a pause begins or ends.
    minimum: Option<crate::MinimumTarget>,
    queue: VecDeque<TaggedPacket>,
    /// Padding-only packets, released only while `queue` is empty (see the order of service),
    /// each with the probe cluster it was written for (the one being sent when it arrived).
    padding: VecDeque<(Option<u32>, TaggedPacket)>,
    /// Padding discarded: left over when its cluster ended, or refused by a full queue.
    padding_discarded: u64,
    /// When the cluster that ended last was asked for: its padding is written at that instant
    /// and, arriving after the cluster ended, is not queued.
    ended_cluster_asked: Option<Instant>,
    /// The bits waiting in `queue` (padding not counted).
    queued_bits: f64,
    queue_limit: usize,
    queue_time_limit: Option<Duration>,
    queue_overflow: QueueOverflow,
    backlog: Backlog,
    /// The targets set and when, oldest first, back to the one in force when the oldest queued
    /// new media was written: what that media was written for (see "A capacity collapse").
    targets: VecDeque<(Instant, f64)>,
    /// How many times the queue was flushed at a collapse.
    flushes: u64,
    /// The media SSRCs of the bound streams other than audio.
    video: HashSet<u32>,
    /// The SSRCs of the audio streams: their packets are not queued behind others.
    audio: HashSet<u32>,
    /// The flows the circuit breaker has ceased ([`CircuitBreakerNotice`]): their packets do
    /// not leave, and are not charged to the budget.
    ceased: HashSet<u32>,
    /// The streams that may not send ([`StreamsNotSending`]): their packets are dropped.
    not_sending: HashSet<u32>,
    /// Packets dropped because their stream may not send.
    not_sending_dropped: u64,
    /// Whether a packet inside the budget leaves at once when nothing is queued ahead of it.
    immediate_release: bool,
    /// The probe cluster being sent, if one is (see [`ProbeRequest`]).
    probe: Option<ActiveProbe>,
    /// Packets released over the budget ([`QueueOverflow::Drain`]).
    forced: u64,
    /// Media packets dropped for want of room in the target ([`QueueOverflow::Drop`]).
    dropped: u64,
    /// Frames of new media discarded whole ([`QueueOverflow::Drop`]).
    discarded_frames: u64,
    /// The repair flows (RTX, FEC SSRCs) of the bound streams other than audio: a discard on
    /// one asks for no key frame.
    repair_flows: HashSet<u32>,
    /// The RFC 8627 FlexFEC repair flows among them: a retransmission there (R=1, F=0) carries
    /// the whole source packet, not an RTX original sequence number.
    flexfec_flows: HashSet<u32>,
    /// (repair SSRC, media SSRC) of the bound streams: a repair flow is one flow with its
    /// stream in the share by priority. A few entries, searched in order.
    repaired_by: Vec<(u32, u32)>,
    /// Whether audio takes part in the share by priority ([`PacerBuilder::with_audio_in_share`]).
    audio_in_share: bool,
    /// Audio packets above their stream's share, waiting for their turn (see the order of
    /// service, 1), oldest first. Not in `queue`, `queued_bits` or the pause: what a source
    /// above its share writes is the source's to bring down, and audio is never flushed.
    held_audio: VecDeque<TaggedPacket>,
    /// The streams' weights ([`PacerPriorities`]), by media SSRC.
    priorities: Vec<(u32, f64)>,
    /// The flows of the share by priority since the queue last held no new media (see the
    /// order of service): one entry per stream that has waited or been repaired since. Cleared,
    /// not freed, when a new share starts, so a steady run allocates nothing.
    flows: Vec<FairFlow>,
    /// Start-time fair queuing's virtual time: the start tag of the new media served last.
    virtual_time: f64,
    /// The RTP timestamp of the newest frame discarded on each stream: its packets written
    /// later are discarded too.
    discarded_frame: HashMap<u32, u32>,
    /// Whether the application is told to pause (see "Pausing the source").
    paused: bool,
    /// When the pause in force, or the last one, began.
    pause_began: Option<Instant>,
    /// How many times a pause began.
    pauses: u64,
    /// Retransmissions not queued because the same packet's retransmission already waited.
    duplicate_retransmissions: u64,
    /// The retransmissions released and dropped since the last [`RetransmissionsLeft`].
    departed: Vec<(u32, u16)>,
    retransmissions_dropped: Vec<(u32, u16)>,
    /// Packets the budget has released, waiting to join the belt.
    released: VecDeque<TaggedPacket>,
    /// Inbound packets ready for the next interceptor.
    read_queue: VecDeque<TaggedPacket>,
    /// Outbound packets ready for the next interceptor: what passed through, plus
    /// anything this one generated.
    write_queue: VecDeque<TaggedPacket>,
}

impl PacerInterceptor {
    fn new(pacer: LeakyBucket, queue_limit: usize) -> Self {
        Self {
            read_queue: VecDeque::new(),
            write_queue: VecDeque::new(),
            released: VecDeque::new(),
            raised_from: None,
            minimum: None,
            pacer,
            queue: VecDeque::new(),
            padding: VecDeque::new(),
            padding_discarded: 0,
            ended_cluster_asked: None,
            queued_bits: 0.0,
            queue_limit: queue_limit.max(1),
            queue_time_limit: Some(DEFAULT_QUEUE_TIME_LIMIT),
            queue_overflow: QueueOverflow::default(),
            backlog: Backlog::default(),
            targets: VecDeque::new(),
            flushes: 0,
            video: HashSet::new(),
            audio: HashSet::new(),
            ceased: HashSet::new(),
            not_sending: HashSet::new(),
            not_sending_dropped: 0,
            immediate_release: true,
            probe: None,
            forced: 0,
            dropped: 0,
            discarded_frames: 0,
            repair_flows: HashSet::new(),
            flexfec_flows: HashSet::new(),
            repaired_by: Vec::new(),
            audio_in_share: true,
            held_audio: VecDeque::new(),
            priorities: Vec::new(),
            flows: Vec::new(),
            virtual_time: 0.0,
            discarded_frame: HashMap::new(),
            paused: false,
            pause_began: None,
            pauses: 0,
            duplicate_retransmissions: 0,
            departed: Vec::new(),
            retransmissions_dropped: Vec::new(),
        }
    }

    /// The pacing bucket, for a bandwidth estimator to drive.
    pub fn pacer(&self) -> &LeakyBucket {
        &self.pacer
    }

    /// The pacing bucket, mutably — this is where `set_target_bitrate` is reached.
    pub fn pacer_mut(&mut self) -> &mut LeakyBucket {
        &mut self.pacer
    }

    /// How many packets are waiting to be released, padding and held audio included.
    pub fn queued(&self) -> usize {
        self.queue.len() + self.padding.len() + self.held_audio.len()
    }

    /// How many padding-only packets were discarded: left over when the probe cluster they
    /// were written for ended (see "A cluster's padding belongs to the cluster" on
    /// [`PacerInterceptor`]), or refused because the padding queue was full.
    pub fn padding_discarded(&self) -> u64 {
        self.padding_discarded
    }

    /// How many packets were dropped because their stream may not send
    /// ([`StreamsNotSending`]): held when its direction changed, or written after.
    pub fn not_sending_dropped(&self) -> u64 {
        self.not_sending_dropped
    }

    /// How many packets left over the budget, overdue or to make room in a full queue
    /// ([`QueueOverflow::Drain`]).
    pub fn forced(&self) -> u64 {
        self.forced
    }

    /// How many media packets were dropped, overdue or to make room in a full queue
    /// ([`QueueOverflow::Drop`]): the packets of the discarded frames and the retransmissions
    /// dropped.
    pub fn dropped(&self) -> u64 {
        self.dropped
    }

    /// How many frames of new media were discarded whole ([`QueueOverflow::Drop`], see
    /// "Discarding media" on [`PacerInterceptor`]).
    pub fn discarded_frames(&self) -> u64 {
        self.discarded_frames
    }

    /// How many times the application was told to pause (see "Pausing the source" on
    /// [`PacerInterceptor`]).
    pub fn pauses(&self) -> u64 {
        self.pauses
    }

    /// How many times the queue was flushed at a collapse of the target (see "A capacity
    /// collapse" on [`PacerInterceptor`]).
    pub fn flushes(&self) -> u64 {
        self.flushes
    }

    /// Whether the application is told to pause now.
    pub fn paused(&self) -> bool {
        self.paused
    }

    /// How many retransmissions were not queued because a retransmission of the same packet
    /// already waited in the queue.
    pub fn duplicate_retransmissions(&self) -> u64 {
        self.duplicate_retransmissions
    }

    /// How long the media queued now takes to leave at the target: queued bits over the
    /// target (libwebrtc's `ExpectedQueueTime`). Padding is not counted.
    pub fn expected_queue_time(&self) -> Duration {
        if self.queued_bits <= 0.0 {
            return Duration::ZERO;
        }
        let target = self.target();
        if target <= 0.0 {
            return Duration::MAX;
        }
        Duration::try_from_secs_f64(self.queued_bits / target).unwrap_or(Duration::MAX)
    }

    /// How long the oldest queued media packet has waited at `now`, zero with none queued
    /// (padding is not counted: the queue time bound is the media's).
    pub fn queue_time(&self, now: Instant) -> Duration {
        self.queue.front().map_or(Duration::ZERO, |head| {
            now.saturating_duration_since(head.now)
        })
    }

    /// When the oldest queued new media (not a retransmission) was written.
    fn oldest_new_media(&self) -> Option<Instant> {
        self.queue
            .iter()
            .find(|queued| !queued.message.has(&Attribute::Retransmission))
            .map(|oldest| oldest.now)
    }

    /// The rate to pace at now: the target, or more when the queue would otherwise not drain
    /// before its oldest packet has waited the queue time limit. With `q` bits queued and the
    /// oldest packet having waited `w`, the rate is max(target, q / (limit - w)); once `w`
    /// reaches the limit, everything queued may leave.
    fn pacing_rate(&self, now: Instant) -> f64 {
        let target = self.target();
        if self.queue_overflow != QueueOverflow::Drain {
            return target;
        }
        let Some(limit) = self.queue_time_limit else {
            return target;
        };
        if self.queue.is_empty() {
            return target;
        }
        let remaining = limit.saturating_sub(self.queue_time(now)).as_secs_f64();
        let needed = if remaining > 0.001 {
            self.queued_bits / remaining
        } else {
            // Overdue: everything may leave (`handle_timeout` releases it).
            1e12
        };
        target.max(needed)
    }

    /// The target rate: what the bucket paces at when the queue is short.
    fn target(&self) -> f64 {
        self.raised_from
            .unwrap_or_else(|| self.pacer.target_bitrate())
    }

    /// Paces at [`Self::pacing_rate`] from `now`: above the target while the queue needs it,
    /// back at the target once it does not.
    fn follow_queue(&mut self, now: Instant) {
        let target = self.target();
        let rate = self.pacing_rate(now);
        if rate > target {
            self.raised_from = Some(target);
            if rate != self.pacer.target_bitrate() {
                self.pacer.set_target_bitrate(rate);
            }
        } else if let Some(target) = self.raised_from.take() {
            self.pacer.set_target_bitrate(target);
        }
    }

    /// Whether `packet` is padding only: an RTP packet with the padding bit and no payload.
    fn is_padding(packet: &TaggedPacket) -> bool {
        matches!(&packet.message.packet, Packet::Rtp(rtp) if rtp.header.padding && rtp.payload.is_empty())
    }

    fn is_audio(&self, packet: &TaggedPacket) -> bool {
        matches!(&packet.message.packet, Packet::Rtp(rtp) if self.audio.contains(&rtp.header.ssrc))
    }

    /// The size of a packet on the wire, in bits — the unit the budget is kept in.
    fn bits_of(packet: &TaggedPacket) -> f64 {
        match &packet.message.packet {
            Packet::Rtp(rtp) => (rtp.marshal_size() * 8) as f64,
            Packet::Rtcp(_) => 0.0,
        }
    }

    /// The flow of the stream `ssrc` in the share by priority: the media stream a bound repair
    /// flow belongs to, otherwise the stream itself.
    fn flow_key(&self, ssrc: u32) -> u32 {
        self.repaired_by
            .iter()
            .find(|(repair, _)| *repair == ssrc)
            .map_or(ssrc, |(_, media)| *media)
    }

    /// The index in `flows` of the RTP packet's flow, if it has one yet.
    fn flow_of(&self, packet: &TaggedPacket) -> Option<usize> {
        let Packet::Rtp(rtp) = &packet.message.packet else {
            return None;
        };
        let key = self.flow_key(rtp.header.ssrc);
        self.flows.iter().position(|flow| flow.ssrc == key)
    }

    /// The index in `flows` of the flow `key`, entered (with no share used) if it was not.
    fn flow_index(&mut self, key: u32) -> usize {
        if let Some(index) = self.flows.iter().position(|flow| flow.ssrc == key) {
            return index;
        }
        let weight = self.weight_for(key);
        let audio = self.audio.contains(&key);
        self.flows.push(FairFlow {
            ssrc: key,
            queued: 0,
            weight,
            finish: 0.0,
            audio,
        });
        self.flows.len() - 1
    }

    /// The weight of the stream `key` in the share ([`PacerPriorities`]; `low`'s when it is
    /// not named, or named with a weight that is not a positive number).
    fn weight_for(&self, key: u32) -> f64 {
        self.priorities
            .iter()
            .find(|(ssrc, _)| *ssrc == key)
            .map(|(_, weight)| *weight)
            .filter(|weight| weight.is_finite() && *weight > 0.0)
            .unwrap_or(DEFAULT_STREAM_WEIGHT)
    }

    /// New media joins the queue: counted to its flow. With no new media waiting before it, a
    /// new share starts (nothing that left before counts in it).
    fn count_queued(&mut self, packet: &TaggedPacket) {
        let Packet::Rtp(rtp) = &packet.message.packet else {
            return;
        };
        if (self.queue.is_empty() && self.held_audio.is_empty())
            || self.flows.iter().all(|flow| flow.queued == 0)
        {
            self.flows.clear();
            self.virtual_time = 0.0;
        }
        let key = self.flow_key(rtp.header.ssrc);
        let index = self.flow_index(key);
        self.flows[index].queued += 1;
    }

    /// Queued new media leaves the queue without being sent (discarded or dropped).
    fn uncount(&mut self, packet: &TaggedPacket) {
        if packet.message.has(&Attribute::Retransmission) {
            return;
        }
        if let Some(index) = self.flow_of(packet) {
            let flow = &mut self.flows[index];
            flow.queued = flow.queued.saturating_sub(1);
        }
    }

    /// A queued packet leaves: its payload octets are charged to its flow's share (start-time
    /// fair queuing: its start tag is the later of the flow's finish tag and the virtual time,
    /// its finish tag the start plus the octets over the weight). New media also moves the
    /// virtual time to its start tag; a retransmission does not (it went ahead of its turn,
    /// and the other streams are not to lose theirs for it).
    fn charge(&mut self, packet: &TaggedPacket) {
        let Packet::Rtp(rtp) = &packet.message.packet else {
            return;
        };
        let octets = rtp.payload.len() as f64;
        let repair = packet.message.has(&Attribute::Retransmission);
        let key = self.flow_key(rtp.header.ssrc);
        let index = self.flow_index(key);
        let virtual_time = self.virtual_time;
        let flow = &mut self.flows[index];
        let start = flow.finish.max(virtual_time);
        flow.finish = start + octets / flow.weight;
        if !repair {
            flow.queued = flow.queued.saturating_sub(1);
            self.virtual_time = start;
        }
    }

    /// The index in the queue of the packet to leave next when the budget allows (see the
    /// order of service): the front while it is a retransmission (they wait at the front) or
    /// while one stream has new media waiting; otherwise the oldest packet of the stream
    /// whose start tag is lowest, the older stream's on a tie. Allocates nothing: the flows
    /// are a few entries, and the queue is searched only as far as that stream's oldest
    /// packet.
    fn next_index(&self) -> usize {
        if self.flows.len() < 2 {
            return 0;
        }
        let Some(front) = self.queue.front() else {
            return 0;
        };
        if front.message.has(&Attribute::Retransmission) {
            return 0;
        }
        let mut waiting = 0;
        let mut lowest = f64::INFINITY;
        // Held audio is not in this queue (see `audio_turn`).
        for flow in self.flows.iter().filter(|flow| !flow.audio) {
            if flow.queued > 0 {
                waiting += 1;
                lowest = lowest.min(flow.start(self.virtual_time));
            }
        }
        if waiting < 2 {
            return 0;
        }
        self.queue
            .iter()
            .position(|packet| {
                self.flow_of(packet).is_some_and(|index| {
                    let flow = &self.flows[index];
                    !flow.audio && flow.queued > 0 && flow.start(self.virtual_time) == lowest
                })
            })
            .unwrap_or(0)
    }

    /// Whether the audio packet `packet` waits for its turn (see the order of service, 1): a
    /// packet of its stream is held already (each stream leaves in the order written), or
    /// other streams have packets waiting and its start tag is past the lowest of theirs.
    /// Nothing waits while nothing else does.
    fn audio_waits(&self, packet: &TaggedPacket) -> bool {
        let Packet::Rtp(rtp) = &packet.message.packet else {
            return false;
        };
        let key = self.flow_key(rtp.header.ssrc);
        let virtual_time = self.virtual_time;
        let mut own = virtual_time;
        let mut lowest = f64::INFINITY;
        for flow in &self.flows {
            if flow.ssrc == key {
                if flow.queued > 0 {
                    return true;
                }
                own = flow.start(virtual_time);
            } else if flow.queued > 0 {
                lowest = lowest.min(flow.start(virtual_time));
            }
        }
        own > lowest
    }

    /// An audio packet leaves (at once, or held until its turn: `held`): while other streams
    /// wait, or when it was held, its payload octets are charged to its stream's share and the
    /// virtual time moves to its start tag, as for new media (see [`Self::charge`]). With
    /// nothing waiting it is not counted (nothing is shared out).
    fn charge_audio(&mut self, packet: &TaggedPacket, held: bool) {
        if !held && self.flows.iter().all(|flow| flow.queued == 0) {
            return;
        }
        let Packet::Rtp(rtp) = &packet.message.packet else {
            return;
        };
        let octets = rtp.payload.len() as f64;
        let key = self.flow_key(rtp.header.ssrc);
        let index = self.flow_index(key);
        let virtual_time = self.virtual_time;
        let flow = &mut self.flows[index];
        let start = flow.start(virtual_time);
        flow.finish = start + octets / flow.weight;
        if held {
            flow.queued = flow.queued.saturating_sub(1);
        }
        self.virtual_time = start;
    }

    /// Holds an audio packet above its stream's share until its turn (see the order of
    /// service, 1). A full hold (the queue limit) lets its oldest packet go first.
    #[cold]
    #[inline(never)]
    fn hold_audio(&mut self, msg: TaggedPacket) {
        if self.held_audio.len() >= self.queue_limit
            && let Some(oldest) = self.held_audio.pop_front()
        {
            let now = msg.now;
            self.unhold(&oldest);
            self.discard_audio(now, vec![oldest]);
        }
        if let Packet::Rtp(rtp) = &msg.message.packet {
            let key = self.flow_key(rtp.header.ssrc);
            let index = self.flow_index(key);
            self.flows[index].queued += 1;
        }
        self.held_audio.push_back(msg);
    }

    /// A held audio packet leaves the hold without being sent.
    fn unhold(&mut self, packet: &TaggedPacket) {
        if let Some(index) = self.flow_of(packet) {
            let flow = &mut self.flows[index];
            flow.queued = flow.queued.saturating_sub(1);
        }
    }

    /// Discards held audio (`packets`, oldest first) with a [`PacerDiscard`] per stream and no
    /// key-frame request.
    #[cold]
    #[inline(never)]
    fn discard_audio(&mut self, now: Instant, packets: Vec<TaggedPacket>) {
        let mut streams: Vec<(u32, Vec<TaggedPacket>)> = Vec::new();
        for packet in packets {
            let Packet::Rtp(rtp) = &packet.message.packet else {
                continue;
            };
            let ssrc = rtp.header.ssrc;
            match streams.iter_mut().find(|(stream, _)| *stream == ssrc) {
                Some((_, held)) => held.push(packet),
                None => streams.push((ssrc, vec![packet])),
            }
        }
        for (ssrc, packets) in &streams {
            self.discard(now, *ssrc, packets, LocalKeyFrameRequest::PacerDiscard);
        }
    }

    /// Drops the held audio that has waited the queue time bound: a voice that late is worth
    /// nothing, and a source above its share would otherwise hold more and more.
    fn drop_overdue_audio(&mut self, now: Instant) {
        let Some(limit) = self.queue_time_limit else {
            return;
        };
        let mut overdue = Vec::new();
        while self
            .held_audio
            .front()
            .is_some_and(|held| now.saturating_duration_since(held.now) >= limit)
        {
            if let Some(packet) = self.held_audio.pop_front() {
                self.unhold(&packet);
                overdue.push(packet);
            }
        }
        if !overdue.is_empty() {
            self.discard_audio(now, overdue);
        }
    }

    /// Lets every held audio packet go at `now`, over the budget if need be: at a flush, which
    /// leaves nothing for audio to wait behind.
    fn release_held_audio(&mut self, now: Instant) {
        for mut packet in std::mem::take(&mut self.held_audio) {
            let bits = Self::bits_of(&packet);
            if !self.try_spend(bits) {
                self.pacer.consume(bits);
            }
            packet.now = now;
            self.release(packet, bits);
        }
    }

    /// The index in `held_audio` of the packet to leave next, when it is held audio's turn: the
    /// lowest start tag of the held streams is not past the lowest of the streams waiting in
    /// the queue (or none waits there). The oldest packet of that stream.
    fn audio_turn(&self) -> Option<usize> {
        if self.held_audio.is_empty() {
            return None;
        }
        let virtual_time = self.virtual_time;
        let (mut audio, mut other) = (f64::INFINITY, f64::INFINITY);
        for flow in self.flows.iter().filter(|flow| flow.queued > 0) {
            let start = flow.start(virtual_time);
            if flow.audio {
                audio = audio.min(start);
            } else {
                other = other.min(start);
            }
        }
        if audio > other {
            return None;
        }
        self.held_audio
            .iter()
            .position(|packet| {
                self.flow_of(packet).is_some_and(|index| {
                    let flow = &self.flows[index];
                    flow.audio && flow.queued > 0 && flow.start(virtual_time) == audio
                })
            })
            .or(Some(0))
    }

    /// The bucket releases are metered through now: the probe cluster's while one is being
    /// sent, the target rate's otherwise.
    fn bucket(&self) -> &LeakyBucket {
        self.probe
            .as_ref()
            .map_or(&self.pacer, |probe| &probe.bucket)
    }

    /// The instant the next packet can be released: the head of the media queue, or of the
    /// padding while no media waits (padding has no queue time bound to wake for).
    fn next_release(&self) -> Option<Instant> {
        let media = self.next_queue_release();
        if self.held_audio.is_empty() {
            return media;
        }
        // Held audio: its budget when it is its turn, and the queue time bound of the oldest.
        let by_budget = self
            .audio_turn()
            .and_then(|index| self.held_audio.get(index))
            .and_then(|packet| self.bucket().releasable_at(Self::bits_of(packet)));
        let by_limit = self
            .queue_time_limit
            .zip(self.held_audio.front())
            .map(|(limit, oldest)| oldest.now + limit);
        [media, by_budget, by_limit].into_iter().flatten().min()
    }

    /// [`Self::next_release`] of the media queue and the padding.
    fn next_queue_release(&self) -> Option<Instant> {
        let Some(head) = self.queue.front() else {
            let (_, head) = self.padding.front()?;
            return self.bucket().releasable_at(Self::bits_of(head));
        };
        // The budget is waited for by the packet whose turn it is (the share by priority), the
        // queue time bound by the oldest.
        let next = self.queue.get(self.next_index()).unwrap_or(head);
        let by_budget = self.bucket().releasable_at(Self::bits_of(next)).map(|at| {
            // While the rate is raised it moves with every call, and the instant computed at
            // one rate can come out a hair short of the budget at the next: a microsecond
            // later it is not, and the deadline advances (webrtc#862).
            if self.raised_from.is_some() {
                at + Duration::from_micros(1)
            } else {
                at
            }
        });
        // The rate rises as the oldest packet ages: wake by the time it reaches the limit
        // (never for a packet the bound does not apply to: that deadline would stay in the
        // past and a driver would spin on it).
        let by_limit = self
            .queue_time_limit
            .filter(|_| self.queue_overflow == QueueOverflow::Drain || self.ages_out(head))
            .map(|limit| head.now + limit);
        match (by_budget, by_limit) {
            (Some(a), Some(b)) => Some(a.min(b)),
            (a, b) => a.or(b),
        }
    }

    /// Whether `bits` may leave now, and if so, spends them.
    fn try_spend(&mut self, bits: f64) -> bool {
        match self.probe.as_mut() {
            Some(probe) => {
                if !probe.bucket.can_release(bits) {
                    return false;
                }
                probe.bucket.consume(bits);
                true
            }
            None => {
                if !self.pacer.can_release(bits) {
                    return false;
                }
                self.pacer.consume(bits);
                true
            }
        }
    }

    /// Brings the budgets up to `now`, and ends a probe cluster that could not be filled.
    fn refill(&mut self, now: Instant) {
        self.pacer.refill(now);
        if let Some(probe) = self.probe.as_mut() {
            probe.bucket.refill(now);
            if now.saturating_duration_since(probe.started) > PROBE_CLUSTER_TIMEOUT {
                self.end_probe();
            }
        }
    }

    /// The probe cluster being sent is over (sent in full, timed out, or replaced by the next
    /// one): the padding written for it that did not leave in it is discarded (see "A
    /// cluster's padding belongs to the cluster").
    fn end_probe(&mut self) {
        let Some(probe) = self.probe.take() else {
            return;
        };
        self.ended_cluster_asked = Some(probe.started);
        let before = self.padding.len();
        self.padding
            .retain(|(cluster, _)| *cluster != Some(probe.cluster));
        self.padding_discarded += (before - self.padding.len()) as u64;
    }

    /// A packet leaves at `now`: marked as part of the probe cluster while one is being sent.
    fn release(&mut self, mut packet: TaggedPacket, bits: f64) {
        if packet.message.has(&Attribute::Retransmission)
            && !Self::is_padding(&packet)
            && let Some(key) = self.retransmission_key(&packet)
        {
            self.departed.push(key);
        }
        if let Some(probe) = self.probe.as_mut() {
            packet.message.add(Attribute::Custom(Arc::new(ProbePacket {
                cluster: probe.cluster,
            })));
            probe.remaining_bits -= bits;
            if probe.remaining_bits <= 0.0 {
                self.end_probe();
            }
        }
        self.released.push_back(packet);
    }

    /// The packet a retransmission repairs, as (SSRC on the wire, sequence number repaired): on
    /// an RTX stream the original sequence number ahead of the payload (RFC 4588 section 4),
    /// otherwise (a retransmission under the media SSRC) its own sequence number.
    fn retransmission_key(&self, packet: &TaggedPacket) -> Option<(u32, u16)> {
        let Packet::Rtp(rtp) = &packet.message.packet else {
            return None;
        };
        let ssrc = rtp.header.ssrc;
        // A FlexFEC retransmission (RFC 8627 section 4.2.2.3): its payload is the source
        // packet, whose SSRC and sequence number name it.
        if self.flexfec_flows.contains(&ssrc) {
            let source = rtp.payload.get(..12)?;
            return Some((
                u32::from_be_bytes([source[8], source[9], source[10], source[11]]),
                u16::from_be_bytes([source[2], source[3]]),
            ));
        }
        if self.repair_flows.contains(&ssrc) {
            let osn = rtp.payload.get(..2)?;
            return Some((ssrc, u16::from_be_bytes([osn[0], osn[1]])));
        }
        Some((ssrc, rtp.header.sequence_number))
    }

    /// Whether a retransmission of the packet `packet` repairs already waits in the queue
    /// (retransmissions wait at its front).
    fn retransmission_waits(&self, packet: &TaggedPacket) -> bool {
        let Some(key) = self.retransmission_key(packet) else {
            return false;
        };
        self.queue
            .iter()
            .take_while(|queued| queued.message.has(&Attribute::Retransmission))
            .any(|queued| self.retransmission_key(queued) == Some(key))
    }

    /// Records a retransmission dropped from the queue, for [`RetransmissionsLeft`].
    fn retransmission_dropped(&mut self, packet: &TaggedPacket) {
        if let Some(key) = self.retransmission_key(packet) {
            self.retransmissions_dropped.push(key);
        }
    }

    /// Whether the pause should begin or end now (see "Pausing the source"); `true` when it
    /// changed.
    fn update_pause(&mut self, now: Instant) -> bool {
        if self.collapsed(now) {
            // Not a queue to wait out: the rate it was written for is gone. Also when the
            // target goes on falling during a pause that began before it had halved.
            self.flush(now);
            // A pause in force ends with its queue.
            return std::mem::replace(&mut self.paused, false);
        }
        let expected = self.expected_queue_time();
        let change = match self.queue_time_limit {
            Some(limit) if !self.paused => expected >= limit.mul_f64(PAUSE_AT),
            Some(limit) => expected <= limit.mul_f64(RESUME_AT),
            None => self.paused,
        };
        if change {
            self.paused = !self.paused;
            if self.paused {
                self.pauses += 1;
                self.pause_began = Some(now);
            }
        }
        change
    }

    /// Records the target set at `now`, and drops the targets too old to count.
    fn note_target(&mut self, now: Instant, bits_per_second: f64) {
        if self.targets.back().map(|(_, target)| *target) != Some(bits_per_second) {
            self.targets.push_back((now, bits_per_second));
        }
        self.forget_targets(now);
    }

    /// The instant from which the targets count toward a collapse at `now`: when the oldest
    /// queued new media was written, or the queue time bound ago, whichever is earlier. What
    /// waits here was written since, and so was what a retransmission queued now repairs,
    /// as far as it is still worth sending (media that waited the bound is discarded as
    /// stale). No target set before the last flush is kept ([`Self::flush`]), so the
    /// horizon never reaches behind one.
    fn collapse_horizon(&self, now: Instant) -> Option<Instant> {
        let oldest = self.oldest_new_media();
        let look_back = self
            .queue_time_limit
            .and_then(|limit| now.checked_sub(limit));
        match (oldest, look_back) {
            (Some(oldest), Some(look_back)) => Some(oldest.min(look_back)),
            (oldest, look_back) => oldest.or(look_back),
        }
    }

    /// Drops the targets replaced before [`Self::collapse_horizon`]; all but the last when
    /// none counts.
    fn forget_targets(&mut self, now: Instant) {
        let horizon = self.collapse_horizon(now);
        while self.targets.len() > 1 && horizon.is_none_or(|from| self.targets[1].0 <= from) {
            self.targets.pop_front();
        }
        // Far more changes than a queue's lifetime holds: the oldest are let go (the collapse
        // is then judged against a later, lower target, which errs towards the pause).
        while self.targets.len() > MAX_TARGETS_KEPT {
            self.targets.pop_front();
        }
    }

    /// Whether the queue is long because the target collapsed: what waits (new media or
    /// retransmissions) would take the pause mark or more to leave, whether a pause is in
    /// force or about to begin, and the target is at most [`COLLAPSE_AT`] of the highest in force since
    /// [`Self::collapse_horizon`] (see "A capacity collapse").
    fn collapsed(&self, now: Instant) -> bool {
        if self.backlog != Backlog::Flush || self.queue_overflow != QueueOverflow::Drop {
            return false;
        }
        let Some(limit) = self.queue_time_limit else {
            return false;
        };
        if self.queue.is_empty() || self.expected_queue_time() < limit.mul_f64(PAUSE_AT) {
            return false;
        }
        let Some(horizon) = self.collapse_horizon(now) else {
            return false;
        };
        let mut written_for: f64 = 0.0;
        for (index, (_, target)) in self.targets.iter().enumerate() {
            // In force at the horizon (the last set at or before it), or since.
            let superseded = self
                .targets
                .get(index + 1)
                .is_some_and(|(next, _)| *next <= horizon);
            if !superseded {
                written_for = written_for.max(*target);
            }
        }
        written_for > 0.0 && self.target() <= COLLAPSE_AT * written_for
    }

    /// Gives the queue up at a collapse: retransmissions dropped, media discarded stream by
    /// stream, the NACK responder told to forget what was sent before ([`PacerFlush`]), and
    /// a key frame asked of every video stream.
    fn flush(&mut self, now: Instant) {
        let mut streams: Vec<(u32, Vec<TaggedPacket>)> = Vec::new();
        for packet in std::mem::take(&mut self.queue) {
            if packet.message.has(&Attribute::Retransmission) {
                self.retransmission_dropped(&packet);
                self.dropped += 1;
            } else if let Packet::Rtp(rtp) = &packet.message.packet {
                let ssrc = rtp.header.ssrc;
                match streams.iter_mut().find(|(stream, _)| *stream == ssrc) {
                    Some((_, packets)) => packets.push(packet),
                    None => streams.push((ssrc, vec![packet])),
                }
            }
        }
        self.queued_bits = 0.0;
        // Audio is not flushed: nothing is left for held audio to wait behind.
        self.release_held_audio(now);
        self.flows.clear();
        self.virtual_time = 0.0;
        self.flushes += 1;
        let mut asked: HashSet<u32> = HashSet::new();
        for (ssrc, packets) in &streams {
            // `PacerDiscard`, and for a media stream the key-frame request.
            self.discard(now, *ssrc, packets, LocalKeyFrameRequest::PacerFlush);
            asked.insert(*ssrc);
        }
        let mut ssrcs: Vec<u32> = self.video.iter().copied().collect();
        ssrcs.sort_unstable();
        // A video stream with nothing queued lost its repairs all the same.
        for ssrc in ssrcs.iter().filter(|ssrc| !asked.contains(ssrc)) {
            self.read_queue.push_back(TaggedPacket {
                now,
                transport: TransportContext::default(),
                message: AttributedPacket::new(Packet::Rtcp(vec![Box::new(
                    PictureLossIndication {
                        sender_ssrc: 0,
                        media_ssrc: *ssrc,
                    },
                )]))
                .with(Attribute::DeliverToApplication)
                .with(Attribute::Custom(Arc::new(
                    LocalKeyFrameRequest::PacerFlush,
                ))),
            });
        }
        self.read_queue.push_back(TaggedPacket {
            now,
            transport: TransportContext::default(),
            message: AttributedPacket::new(Packet::Rtcp(vec![]))
                .with(Attribute::Custom(Arc::new(PacerFlush { ssrcs }))),
        });
        // What is written from here on is written for the target now: no earlier target
        // counts again, so the key frame that answers this flush, however long it takes to
        // leave, is a pause and not another collapse.
        let target = self.target();
        self.targets.clear();
        self.targets.push_back((now, target));
    }

    /// Whether new media written at `written` is what a source that follows the pause left
    /// behind: written before the pause in force (or the last one) began, or within
    /// [`PAUSE_GRACE`] of that. It is never discarded for its age (see "Pausing the source").
    fn written_before_pause(&self, written: Instant) -> bool {
        self.pause_began
            .is_some_and(|began| written <= began + PAUSE_GRACE)
    }

    /// Whether the queued `packet` is discarded once it has waited the queue time bound
    /// ([`QueueOverflow::Drop`]): a retransmission, and new media a pausing source would not
    /// have written.
    fn ages_out(&self, packet: &TaggedPacket) -> bool {
        packet.message.has(&Attribute::Retransmission) || !self.written_before_pause(packet.now)
    }

    /// The target as the application is to read it: zero while paused, with [`PacerPause`]
    /// carrying the congestion controller's.
    fn tag_target(&self, message: &mut AttributedPacket) {
        if !self.paused {
            return;
        }
        let mut target = None;
        for attribute in &mut message.attributes {
            if let Attribute::TargetBitrateChanged { bits_per_second } = attribute {
                target = Some(*bits_per_second);
                *bits_per_second = 0.0;
            }
        }
        if let Some(target_bits_per_second) = target {
            message.add(Attribute::Custom(Arc::new(PacerPause {
                target_bits_per_second,
            })));
        }
    }

    /// After a write or a timeout: a pause that began or ended goes application-ward as the
    /// target, and the retransmissions that left or were dropped as [`RetransmissionsLeft`].
    fn tell_application_ward(&mut self, now: Instant) {
        if self.update_pause(now) {
            let mut message =
                AttributedPacket::new(Packet::Rtcp(vec![])).with(Attribute::TargetBitrateChanged {
                    bits_per_second: self.target(),
                });
            // The controller's target said again, so with the minimum the controller gave it:
            // a reader that takes the minimum from each target (the NACK responder, which never
            // tells the original media less than it) would otherwise read "no minimum" here,
            // and lower the target by its whole retransmission reserve when a pause ends.
            if let Some(minimum) = self.minimum {
                message.add(Attribute::Custom(Arc::new(minimum)));
            }
            self.tag_target(&mut message);
            self.read_queue.push_back(TaggedPacket {
                now,
                transport: TransportContext::default(),
                message,
            });
        }
        if !self.departed.is_empty() || !self.retransmissions_dropped.is_empty() {
            let notice = RetransmissionsLeft {
                departed: std::mem::take(&mut self.departed),
                dropped: std::mem::take(&mut self.retransmissions_dropped),
            };
            self.read_queue.push_back(TaggedPacket {
                now,
                transport: TransportContext::default(),
                message: AttributedPacket::new(Packet::Rtcp(vec![]))
                    .with(Attribute::Custom(Arc::new(notice))),
            });
        }
    }

    /// Applies the [`StreamsNotSending`] notice `packet` carries, if any (see
    /// [`Self::stop_streams`]); whether it did. Out of line: the search calls through each
    /// custom attribute's vtable, and inlined in `enqueue` it kept registers spilled on the
    /// path of every packet.
    #[cold]
    #[inline(never)]
    fn apply_notice(&mut self, packet: &TaggedPacket) -> bool {
        // The streams' priorities (the peer connection's, on an RTCP carrier: an RTP packet
        // with attributes is not searched for them): they weigh in the shares from now on.
        if matches!(packet.message.packet, Packet::Rtcp(_))
            && let Some(priorities) = packet.message.custom::<PacerPriorities>()
        {
            self.set_priorities(&priorities.weights);
        }
        match packet.message.custom::<StreamsNotSending>() {
            Some(notice) => {
                self.stop_streams(packet.now, &notice.ssrcs);
                true
            }
            None => false,
        }
    }

    /// The streams' weights in the share by priority from now on ([`PacerPriorities`]),
    /// replacing those given before; a flow already in the share takes its new weight for what
    /// it sends from now on.
    #[cold]
    #[inline(never)]
    fn set_priorities(&mut self, weights: &[(u32, f64)]) {
        self.priorities.clear();
        self.priorities.extend_from_slice(weights);
        for index in 0..self.flows.len() {
            self.flows[index].weight = self.weight_for(self.flows[index].ssrc);
        }
    }

    /// Whether `packet` is RTP of a stream that may not send ([`StreamsNotSending`]).
    fn is_not_sending(&self, packet: &TaggedPacket) -> bool {
        matches!(&packet.message.packet, Packet::Rtp(rtp) if self.not_sending.contains(&rtp.header.ssrc))
    }

    /// Counts a packet of a stream that may not send as dropped (a retransmission for
    /// [`RetransmissionsLeft`] too).
    #[cold]
    #[inline(never)]
    fn drop_not_sending(&mut self, packet: &TaggedPacket) {
        if packet.message.has(&Attribute::Retransmission) && !Self::is_padding(packet) {
            self.retransmission_dropped(packet);
        }
        self.not_sending_dropped += 1;
    }

    /// The streams `ssrcs` may not send from `now` on (replacing those that could not
    /// before): everything of them that waits here (queued media and retransmissions,
    /// padding, and what was released for the next interceptor but has not gone to it) is
    /// dropped, without a key-frame request or a [`PacerDiscard`]: nothing is asked of a
    /// source that is not to send.
    ///
    /// Out of line and cold on purpose: `enqueue` looks for the notice on every packet written
    /// and finds it once per applied answer. Inlined there, this body made `handle_write`'s
    /// frame twice the size and the `Interceptor/Pacer/write+poll` benchmark slower
    /// (docs/performance.md, "pacer-4").
    #[cold]
    #[inline(never)]
    fn stop_streams(&mut self, now: Instant, ssrcs: &[u32]) {
        self.not_sending = ssrcs.iter().copied().collect();
        if self.not_sending.is_empty() {
            return;
        }
        let mut kept = VecDeque::with_capacity(self.queue.len());
        for packet in std::mem::take(&mut self.queue) {
            if self.is_not_sending(&packet) {
                self.queued_bits = (self.queued_bits - Self::bits_of(&packet)).max(0.0);
                self.uncount(&packet);
                self.drop_not_sending(&packet);
            } else {
                kept.push_back(packet);
            }
        }
        self.queue = kept;
        if self.queue.is_empty() {
            self.queued_bits = 0.0;
        }
        for packet in std::mem::take(&mut self.held_audio) {
            if self.is_not_sending(&packet) {
                self.unhold(&packet);
                self.drop_not_sending(&packet);
            } else {
                self.held_audio.push_back(packet);
            }
        }
        let mut padding = VecDeque::with_capacity(self.padding.len());
        for (cluster, packet) in std::mem::take(&mut self.padding) {
            if self.is_not_sending(&packet) {
                self.padding_discarded += 1;
                self.not_sending_dropped += 1;
            } else {
                padding.push_back((cluster, packet));
            }
        }
        self.padding = padding;
        let mut released = VecDeque::with_capacity(self.released.len());
        for packet in std::mem::take(&mut self.released) {
            if self.is_not_sending(&packet) {
                self.drop_not_sending(&packet);
            } else {
                released.push_back(packet);
            }
        }
        self.released = released;
        for ssrc in ssrcs {
            self.discarded_frame.remove(ssrc);
        }
        self.follow_queue(now);
    }

    /// Whether `packet` is new media (not a retransmission) of the stream `ssrc`.
    fn is_new_media_of(packet: &TaggedPacket, ssrc: u32) -> bool {
        matches!(&packet.message.packet, Packet::Rtp(rtp) if rtp.header.ssrc == ssrc)
            && !packet.message.has(&Attribute::Retransmission)
    }

    /// The stream of `packet` when it belongs to the frame discarded last on that stream; a
    /// packet of a later frame ends that frame's discard.
    fn continues_discard(&mut self, packet: &TaggedPacket) -> Option<u32> {
        let Packet::Rtp(rtp) = &packet.message.packet else {
            return None;
        };
        let ssrc = rtp.header.ssrc;
        let discarded = *self.discarded_frame.get(&ssrc)?;
        if discarded == rtp.header.timestamp {
            return Some(ssrc);
        }
        self.discarded_frame.remove(&ssrc);
        None
    }

    /// Discards the frame of the queued new-media packet at `index` and every other queued
    /// frame of its stream (see "Discarding media" on [`PacerInterceptor`]).
    fn discard_stream_of(&mut self, now: Instant, index: usize) {
        let Some(Packet::Rtp(rtp)) = self.queue.get(index).map(|packet| &packet.message.packet)
        else {
            return;
        };
        let ssrc = rtp.header.ssrc;
        let mut discarded = Vec::new();
        let mut kept = VecDeque::with_capacity(self.queue.len());
        for packet in self.queue.drain(..) {
            if Self::is_new_media_of(&packet, ssrc) {
                discarded.push(packet);
            } else {
                kept.push_back(packet);
            }
        }
        self.queue = kept;
        for packet in &discarded {
            self.uncount(packet);
        }
        let bits: f64 = discarded.iter().map(Self::bits_of).sum();
        self.queued_bits = (self.queued_bits - bits).max(0.0);
        self.discard(now, ssrc, &discarded, LocalKeyFrameRequest::PacerDiscard);
    }

    /// Counts the discard of `packets` (new media of the stream `ssrc`, oldest first) and tells
    /// the chain application-ward: the NACK responder and the application through
    /// [`PacerDiscard`], and the application a key frame is needed (the request carrying
    /// `why`, [`LocalKeyFrameRequest`]).
    fn discard(
        &mut self,
        now: Instant,
        ssrc: u32,
        packets: &[TaggedPacket],
        why: LocalKeyFrameRequest,
    ) {
        let mut sequence_numbers = Vec::with_capacity(packets.len());
        let mut octets = 0usize;
        let mut frames = 0u32;
        let mut newest = self.discarded_frame.get(&ssrc).copied();
        for packet in packets {
            if let Packet::Rtp(rtp) = &packet.message.packet {
                sequence_numbers.push(rtp.header.sequence_number);
                octets += rtp.marshal_size();
                if newest != Some(rtp.header.timestamp) {
                    frames += 1;
                    newest = Some(rtp.header.timestamp);
                }
            }
        }
        if sequence_numbers.is_empty() {
            return;
        }
        if let Some(timestamp) = newest {
            self.discarded_frame.insert(ssrc, timestamp);
        }
        self.dropped += sequence_numbers.len() as u64;
        self.discarded_frames += u64::from(frames);
        let target_bitrate = self.target();
        self.read_queue.push_back(TaggedPacket {
            now,
            transport: TransportContext::default(),
            message: AttributedPacket::new(Packet::Rtcp(vec![])).with(Attribute::Custom(Arc::new(
                PacerDiscard {
                    ssrc,
                    sequence_numbers,
                    frames,
                    octets,
                    target_bitrate,
                },
            ))),
        });
        // A new frame lost (not only the rest of one already discarded): the stream needs a
        // key frame, asked for the way a receiver asks, so the application's PLI handling
        // answers it.
        if frames > 0 && !self.repair_flows.contains(&ssrc) && !self.audio.contains(&ssrc) {
            self.read_queue.push_back(TaggedPacket {
                now,
                transport: TransportContext::default(),
                message: AttributedPacket::new(Packet::Rtcp(vec![Box::new(
                    PictureLossIndication {
                        sender_ssrc: 0,
                        media_ssrc: ssrc,
                    },
                )]))
                .with(Attribute::DeliverToApplication)
                .with(Attribute::Custom(Arc::new(why))),
            });
        }
    }
}

/// Media the pacer discarded for want of room in the target ([`QueueOverflow::Drop`]): whole
/// frames of one stream (see "Discarding media" on [`PacerInterceptor`]).
///
/// The pacer sends it application-ward, on an empty RTCP carrier on its read leg, at the
/// instant of the discard. The NACK responder removes the packets named here from its
/// retransmission buffer (a NACK for them is answered by nothing) and from the original
/// packets it counts against the target (they never used the link); the peer connection
/// hands it to the application as `RTCPeerConnectionEvent::OnPacerDiscard`. It means **the
/// source is above the target**: what the application writes has to come down to the target
/// it is given (`Attribute::TargetBitrateChanged`, `RTCRtpSender::target_bitrates`). Read with
/// [`AttributedPacket::custom`](crate::AttributedPacket::custom).
#[derive(Debug, Clone, PartialEq)]
pub struct PacerDiscard {
    /// The stream the packets were of.
    pub ssrc: u32,
    /// The sequence numbers of the packets discarded, oldest first.
    pub sequence_numbers: Vec<u16>,
    /// How many frames were discarded (0 when these packets are the rest of a frame already
    /// discarded).
    pub frames: u32,
    /// The octets of the packets discarded (RTP header and payload).
    pub octets: usize,
    /// The pacer's target at the discard, bits per second.
    pub target_bitrate: f64,
}

/// The pacer gave its queue up at a collapse of the target ([`Backlog::Flush`], see "A capacity
/// collapse" on [`PacerInterceptor`]): sent application-ward on an empty RTCP carrier, after
/// the [`PacerDiscard`] of each stream that had media queued. The NACK responder forgets
/// everything sent before it on the streams named (their retransmission buffers, the requests
/// waiting): a NACK for any of it is answered by nothing, and the stream goes on from the key
/// frame the application is asked for. Read with
/// [`AttributedPacket::custom`](crate::AttributedPacket::custom).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PacerFlush {
    /// The media SSRCs of the video streams flushed (every bound stream that is not audio).
    pub ssrcs: Vec<u32>,
}

/// The local streams that may not send media now: the SSRCs (media, RTX and FEC) of the
/// senders whose negotiated direction does not send (`recvonly`, `inactive`). RFC 8866
/// section 6.7.4: "No media is sent over an inactive media stream", and section 6.7.1: a
/// receive-only stream is in "receive-only mode"; RFC 3264 section 5.1. W3C webrtc-pc 4.4.1.6:
/// an answer sets `[[CurrentDirection]]`, and a sender sends only while it is "sendrecv" or
/// "sendonly". RTCP is not affected (RFC 8866 section 6.7: it "MUST still" be sent).
///
/// The peer connection writes it into the chain each time an answer is applied, as an
/// [`Attribute::Custom`] on an empty RTCP packet ([`StreamsNotSending::into_packet`]); each
/// replaces the one before. The [`PacerInterceptor`] drops what it holds of those streams at
/// that instant (media, retransmissions, padding) and every packet of them written later; the
/// [`NackResponderInterceptor`](crate::NackResponderInterceptor) forgets what it sent of them,
/// answers no NACK for them and pads no probe on their RTX streams. Read with
/// [`AttributedPacket::custom`](crate::AttributedPacket::custom).
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct StreamsNotSending {
    /// The SSRCs of the streams, their RTX and FEC streams included.
    pub ssrcs: Vec<u32>,
}

impl StreamsNotSending {
    /// The packet that carries this into the chain when written at `now`.
    pub fn into_packet(self, now: Instant) -> TaggedPacket {
        TaggedPacket {
            now,
            transport: TransportContext::default(),
            message: AttributedPacket::new(Packet::Rtcp(vec![]))
                .with(Attribute::Custom(Arc::new(self))),
        }
    }
}

/// A collapse is a target at most this share of the highest in force since the queued media
/// was written (or within the queue time bound, for what its retransmissions repair): half.
/// One decrease of the delay-based controller is 0.85 and the loss-based controller cuts by
/// at most half a report, so a target that halved within the life of the queue (two seconds
/// at most) is a path that lost its capacity, not a controller adapting; and a queue a
/// flush's own key frame fills was written at the fallen target and is never a collapse.
const COLLAPSE_AT: f64 = 0.5;

/// How many target changes are kept to say what queued media was written for.
const MAX_TARGETS_KEPT: usize = 1024;

/// The pause begins when the expected queue time reaches this share of the queue time bound:
/// half, so a source that stops when told (within a frame or a read of its target) never lets
/// its packets wait the bound, even when the target falls by half once more meanwhile.
const PAUSE_AT: f64 = 0.5;

/// The pause ends when the expected queue time is back under this share of the bound (100 ms
/// of the default 2 s): what then waits is the delay the call carries on with, so it is kept
/// short; the gap between the two shares keeps one frame from switching it on and off.
const RESUME_AT: f64 = 0.05;

/// How long after a pause began media still counts as written by a source that follows it:
/// the time such a source takes to notice (a read of its target, the frame being encoded).
const PAUSE_GRACE: Duration = Duration::from_millis(250);

/// Rides a target of zero ([`Attribute::TargetBitrateChanged`]) that the pacer hands
/// application-ward while it tells the source to pause (see "Pausing the source" on
/// [`PacerInterceptor`]): the congestion controller's target, for what application-ward of the
/// pacer needs the rate the path carries (the NACK responder's retransmission rate limit). Read
/// with [`AttributedPacket::custom`](crate::AttributedPacket::custom).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PacerPause {
    /// The congestion controller's target, bits per second.
    pub target_bits_per_second: f64,
}

/// The weight of a stream no [`PacerPriorities`] names: `low`'s (2), the W3C default priority.
const DEFAULT_STREAM_WEIGHT: f64 = 2.0;

/// One flow (a stream with its repair flows) in the pacer's share by priority.
#[derive(Debug, Clone, Copy)]
struct FairFlow {
    /// The media SSRC.
    ssrc: u32,
    /// Its new-media packets waiting in the queue.
    queued: u32,
    weight: f64,
    /// The finish tag of the last of its packets served: payload octets over the weight, in
    /// the share's virtual time.
    finish: f64,
    /// An audio stream: its waiting packets are held audio, not in the queue.
    audio: bool,
}

impl FairFlow {
    /// The start tag of its next packet at the virtual time `virtual_time`.
    fn start(&self, virtual_time: f64) -> f64 {
        self.finish.max(virtual_time)
    }
}

/// The streams' weights in the pacer's share by priority (see "Order of service" on
/// [`PacerInterceptor`]): each media SSRC with the weight of its encoding's priority, each
/// level twice the level below (RFC 8835 section 4.1; the peer connection gives very-low 1,
/// low 2, medium 4, high 8). A bound stream's RTX and FEC streams weigh with it; a stream not
/// named weighs 2 (`low`, the W3C default). Each replaces the weights given before.
///
/// The peer connection writes it into the chain, as an [`Attribute::Custom`] on an empty RTCP
/// packet ([`PacerPriorities::into_packet`]), each time a description is applied and after
/// `setParameters`. The pacer takes the weights and passes the carrier on, which reaches the
/// end of the chain and is dropped there. Read with
/// [`AttributedPacket::custom`](crate::AttributedPacket::custom).
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct PacerPriorities {
    /// (media SSRC, weight); a weight that is not a positive number counts as 2.
    pub weights: Vec<(u32, f64)>,
}

impl PacerPriorities {
    /// The weights `weights`, as (media SSRC, weight).
    pub fn new(weights: Vec<(u32, f64)>) -> Self {
        Self { weights }
    }

    /// The packet that carries this into the chain when written at `now`.
    pub fn into_packet(self, now: Instant) -> TaggedPacket {
        TaggedPacket {
            now,
            transport: TransportContext::default(),
            message: AttributedPacket::new(Packet::Rtcp(vec![]))
                .with(Attribute::Custom(Arc::new(self))),
        }
    }
}

/// The retransmissions that left the pacer, or were dropped there, since the last notice: each
/// as (SSRC on the wire, sequence number of the packet it repairs; the original sequence
/// number on an RTX stream). The pacer sends it application-ward on an empty RTCP carrier; the
/// NACK responder treats a packet as on its way while its retransmission waits in the pacer
/// and counts the resend interval from when it left, as libwebrtc's packet history does
/// (`modules/rtp_rtcp/source/rtp_packet_history.cc`: a request for a packet "already in pacer
/// queue" is ignored; the send time is set when it is sent). Read with
/// [`AttributedPacket::custom`](crate::AttributedPacket::custom).
#[derive(Debug, Clone, PartialEq, Default)]
pub struct RetransmissionsLeft {
    /// Released toward the wire.
    pub departed: Vec<(u32, u16)>,
    /// Dropped (waited the queue time bound, or the oldest of a full queue).
    pub dropped: Vec<(u32, u16)>,
}

/// How long a probe cluster is held open for packets to fill it. A cluster that takes longer
/// than this to leave is not a burst any more and measures nothing.
const PROBE_CLUSTER_TIMEOUT: Duration = Duration::from_millis(250);

/// A probe cluster on its way out.
struct ActiveProbe {
    cluster: u32,
    /// Meters the cluster's packets at the probe rate, one packet at a time.
    bucket: LeakyBucket,
    remaining_bits: f64,
    started: Instant,
}

impl Protocol<TaggedPacket, TaggedPacket, ()> for PacerInterceptor {
    type Rout = TaggedPacket;
    type Wout = TaggedPacket;
    type Eout = ();
    type Error = Error;
    type Time = Instant;

    fn handle_read(&mut self, mut msg: TaggedPacket) -> Result<(), Self::Error> {
        // Follow the congestion controller's estimate.
        //
        // It arrives on the **read** leg because that is the only one it can cross on. The
        // controller is wire-most, so on the write leg it is the last interceptor to see a packet
        // and anything it attached would already have gone past here. On the read leg it is the
        // first, and this is downstream of it — so the feedback packet that produced the estimate
        // carries it here on its way to the application.
        //
        // Observed, not consumed: the packet carries on with the attribute attached, so anything
        // further along reads the same number.
        if let Some(Attribute::TargetBitrateChanged { bits_per_second }) =
            msg.message.get(&Attribute::TargetBitrateChanged {
                bits_per_second: 0.0,
            })
        {
            match self.raised_from.as_mut() {
                // Raised to drain the queue: the new target is what it comes back to.
                Some(target) => *target = *bits_per_second,
                None => self.pacer.set_target_bitrate(*bits_per_second),
            }
            self.minimum = msg.message.custom::<crate::MinimumTarget>().copied();
            let target = *bits_per_second;
            self.note_target(msg.now, target);
            // A new rate changes how long the queue takes to leave: the pause may begin or
            // end, and the application reads it on this packet.
            self.update_pause(msg.now);
            self.tag_target(&mut msg.message);
        }

        // A flow the circuit breaker ceased or restarted (it sits wire-ward of here).
        if let Some(notice) = msg.message.custom::<crate::CircuitBreakerNotice>() {
            if notice.ceased.is_some() {
                self.ceased.insert(notice.ssrc);
            } else {
                self.ceased.remove(&notice.ssrc);
            }
        }

        // A probe cluster the congestion controller asks for: the next packets leave at its
        // rate, on top of the target's budget, and carry its mark. Observed, not consumed:
        // the NACK responder above reads the same request and fills the cluster.
        if let Some(request) = msg.message.custom::<ProbeRequest>().copied() {
            // A cluster still being sent is over: the new one takes its place.
            self.end_probe();
            let mut bucket = LeakyBucket::new(request.bits_per_second)
                .with_burst_bits(crate::pacing::pacer::MIN_BURST_BITS);
            bucket.refill(msg.now);
            self.probe = Some(ActiveProbe {
                cluster: request.cluster,
                bucket,
                remaining_bits: request.octets as f64 * 8.0,
                started: msg.now,
            });
            // What waits in the media queue leaves first and fills the cluster: the
            // responder is asked for the rest only.
            let queued_octets = (self.queued_bits.max(0.0) / 8.0) as usize;
            msg.message.add(Attribute::Custom(Arc::new(ProbeFill {
                cluster: request.cluster,
                octets: request.octets.saturating_sub(queued_octets),
            })));
        }

        self.read_queue.push_back(msg);
        Ok(())
    }

    fn poll_read(&mut self) -> Option<Self::Rout> {
        self.read_queue.pop_front()
    }

    fn handle_write(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
        let now = msg.now;
        let rtp = matches!(msg.message.packet, Packet::Rtp(_));
        // Nothing here borrows `msg` before it moves into `enqueue`: a borrow (the search for
        // the `StreamsNotSending` notice was one) makes the move a copy of every packet
        // written (224 bytes on x86-64). `enqueue` looks for the notice and says whether it
        // applied one.
        let not_sending = self.enqueue(msg)?;
        if rtp || not_sending {
            self.tell_application_ward(now);
        }
        Ok(())
    }

    fn poll_write(&mut self) -> Option<TaggedPacket> {
        // Unpaced traffic goes first — RTCP is only useful while it is fresh, and holding it
        // behind the budget would be pacing it after all.
        self.write_queue
            .pop_front()
            .or_else(|| self.released.pop_front())
    }

    fn handle_timeout(&mut self, now: Instant) -> Result<(), Error> {
        self.release_due(now);
        self.tell_application_ward(now);
        Ok(())
    }

    fn poll_timeout(&mut self) -> Option<Instant> {
        self.next_release()
    }
}

impl PacerInterceptor {
    /// A packet written: released, queued, or discarded; whether it carried a
    /// [`StreamsNotSending`] notice (applied first, see [`Self::stop_streams`]).
    fn enqueue(&mut self, msg: TaggedPacket) -> Result<bool, Error> {
        // No estimate is read here. It was, once — but the congestion controller sits wire-*ward*
        // of the pacer, so on the write leg it never sees a packet before this interceptor does and
        // the branch had no possible producer. If an application-set target is ever wanted it comes
        // back with its own `RTCEvent` variant, and a test.

        // The streams that may not send from now on: what waits of them goes.
        let not_sending = !msg.message.attributes.is_empty() && self.apply_notice(&msg);

        // RTCP is control traffic and mostly time-sensitive — feedback is only useful while it is
        // fresh — so it is not paced.
        if matches!(msg.message.packet, Packet::Rtcp(_)) {
            self.write_queue.push_back(msg);
            return Ok(not_sending);
        }

        // A stream that may not send (`StreamsNotSending`): written after its direction
        // changed (a retransmission, padding, FEC), it does not leave.
        if self.is_not_sending(&msg) {
            self.drop_not_sending(&msg);
            return Ok(not_sending);
        }

        // A flow the circuit breaker ceased (RFC 8083 section 4.5): its packets are dropped
        // at the breaker, below. They pass here unmetered: charged to the budget, a voice the
        // application keeps writing took 76 of a 100 kbit/s target that the video then
        // could not use (`tests/capacity_cap_video_rtc2rtc.rs`'s path), for packets that
        // never left.
        if matches!(&msg.message.packet, Packet::Rtp(rtp) if self.ceased.contains(&rtp.header.ssrc))
        {
            self.released.push_back(msg);
            return Ok(not_sending);
        }

        // Keeping the budget current at enqueue time is what lets `poll_timeout` compute a
        // deadline that is never in the past, even before the first `handle_timeout`.
        self.refill(msg.now);
        self.follow_queue(msg.now);

        // Audio is never queued behind video: its packets are small and few, and a voice that
        // waits behind a video backlog is the call breaking up. It leaves at once and is
        // charged to the budget, which the packets behind it then wait out.
        // Unless it is above its share while other streams wait (RFC 8835 section 4.1): then
        // it waits for its turn.
        if self.is_audio(&msg) {
            if self.audio_in_share {
                if self.audio_waits(&msg) {
                    self.hold_audio(msg);
                    return Ok(not_sending);
                }
                self.charge_audio(&msg, false);
            }
            let bits = Self::bits_of(&msg);
            if !self.try_spend(bits) {
                self.pacer.consume(bits);
            }
            self.release(msg, bits);
            return Ok(not_sending);
        }

        // Padding waits behind every other packet, and nothing waits behind it.
        let padding = Self::is_padding(&msg);
        // Written for a cluster that has ended already (filled by media in the instant it
        // was asked for): it would only leave late.
        if padding && self.probe.is_none() && self.ended_cluster_asked == Some(msg.now) {
            self.padding_discarded += 1;
            return Ok(not_sending);
        }

        // The rest of a frame already discarded is worth nothing either.
        let new_media = !padding && !msg.message.has(&Attribute::Retransmission);
        if new_media && let Some(ssrc) = self.continues_discard(&msg) {
            self.discard(
                msg.now,
                ssrc,
                std::slice::from_ref(&msg),
                LocalKeyFrameRequest::PacerDiscard,
            );
            return Ok(not_sending);
        }

        // Inside the budget and first in line: it leaves now. The budget is the bound on what
        // may enter the path at once, so nothing is gained by holding the packet for a timer.
        // Queued padding is not in line ahead of media.
        if self.immediate_release
            && self.queue.is_empty()
            && self.held_audio.is_empty()
            && (!padding || self.padding.is_empty())
        {
            let bits = Self::bits_of(&msg);
            if self.try_spend(bits) {
                self.release(msg, bits);
                return Ok(not_sending);
            }
        }

        if padding {
            // Never over the budget and never counted toward the queue time bound (see the
            // order of service); at the limit the arrival is discarded.
            if self.padding.len() >= self.queue_limit {
                self.padding_discarded += 1;
            } else {
                let cluster = self.probe.as_ref().map(|probe| probe.cluster);
                self.padding.push_back((cluster, msg));
            }
            return Ok(not_sending);
        }

        // A retransmission of a packet whose retransmission already waits adds nothing but a
        // second copy (libwebrtc's packet history ignores a request for a packet "already in
        // pacer queue", `rtp_packet_history.cc`).
        if !new_media && self.retransmission_waits(&msg) {
            self.duplicate_retransmissions += 1;
            return Ok(not_sending);
        }

        if self.queue.len() >= self.queue_limit {
            // Room for the arrival: the oldest goes (the newest is the one still worth
            // sending). Discarded by default, a retransmission alone, new media with its
            // stream's queued frames; with `Drain` it leaves now, over the budget.
            let retransmission = self
                .queue
                .front()
                .is_some_and(|oldest| oldest.message.has(&Attribute::Retransmission));
            match self.queue_overflow {
                QueueOverflow::Drop if !retransmission => {
                    self.discard_stream_of(msg.now, 0);
                    if new_media && let Some(ssrc) = self.continues_discard(&msg) {
                        self.discard(
                            msg.now,
                            ssrc,
                            std::slice::from_ref(&msg),
                            LocalKeyFrameRequest::PacerDiscard,
                        );
                        return Ok(not_sending);
                    }
                }
                _ => {
                    if let Some(mut oldest) = self.queue.pop_front() {
                        let bits = Self::bits_of(&oldest);
                        self.queued_bits -= bits;
                        if self.queue_overflow == QueueOverflow::Drain {
                            self.charge(&oldest);
                            self.pacer.consume(bits);
                            oldest.now = msg.now;
                            self.forced += 1;
                            self.release(oldest, bits);
                        } else {
                            self.retransmission_dropped(&oldest);
                            self.dropped += 1;
                        }
                    }
                }
            }
        }

        // Held: it leaves later, from `poll_write`, when the budget allows. A retransmission
        // goes ahead of the new media waiting, behind the retransmissions before it: a repair
        // the NACK responder has already held to the congestion controller's target is worth
        // most early, and waiting behind seconds of new video it may outlive its use or be
        // dropped as overdue. libwebrtc's pacer orders its queue the same way
        // (`modules/pacing/prioritized_packet_queue.cc`, `GetPriorityForType`: "Send
        // retransmissions before new media").
        let now = msg.now;
        self.queued_bits += Self::bits_of(&msg);
        if msg.message.has(&Attribute::Retransmission) {
            let end = self
                .queue
                .iter()
                .position(|queued| !queued.message.has(&Attribute::Retransmission))
                .unwrap_or(self.queue.len());
            // Among the retransmissions of one stream the oldest packet goes first, whenever
            // it was asked for: a receiver that plays in order waits for the oldest gap, and
            // a repair that was itself lost and is made again would otherwise wait behind
            // every newer one.
            let at = self
                .retransmission_key(&msg)
                .map_or(end, |(ssrc, repaired)| {
                    self.queue
                        .iter()
                        .take(end)
                        .position(|queued| {
                            self.retransmission_key(queued)
                                .is_some_and(|(other, waiting)| {
                                    other == ssrc
                                        && waiting != repaired
                                        && waiting.wrapping_sub(repaired) < 0x8000
                                })
                        })
                        .unwrap_or(end)
                });
            self.queue.insert(at, msg);
            if at == 0 {
                // First in line now: it may leave at once if the budget allows.
                self.release_due(now);
            }
            self.follow_queue(now);
            return Ok(not_sending);
        }
        self.count_queued(&msg);
        self.queue.push_back(msg);
        self.follow_queue(now);
        Ok(not_sending)
    }

    /// Releases what the budget allows at `now`, and drops what waited the bound.
    fn release_due(&mut self, now: Instant) {
        self.refill(now);
        self.follow_queue(now);
        if !self.held_audio.is_empty() {
            self.drop_overdue_audio(now);
        }

        loop {
            // Held audio whose turn it is (and no overdue head to deal with first).
            let head_overdue = self.queue.front().is_some_and(|head| {
                self.queue_time_limit
                    .is_some_and(|limit| now.saturating_duration_since(head.now) >= limit)
                    && (self.queue_overflow == QueueOverflow::Drain || self.ages_out(head))
            });
            if !head_overdue && let Some(index) = self.audio_turn() {
                let bits = self.held_audio.get(index).map_or(0.0, Self::bits_of);
                if !self.try_spend(bits) {
                    break;
                }
                let mut packet = self
                    .held_audio
                    .remove(index)
                    .expect("index within the hold");
                self.charge_audio(&packet, true);
                packet.now = now;
                self.release(packet, bits);
                continue;
            }
            let Some(head) = self.queue.front() else {
                break;
            };
            let mut bits = Self::bits_of(head);
            // Past the queue time limit the head is dropped (`Drop`) or leaves whatever the
            // budget says (`Drain`).
            let overdue = head_overdue;
            // The packet that leaves: the overdue head, or the one whose turn it is in the
            // share by priority (the head unless several streams wait).
            let mut index = 0;
            if overdue {
                if self.queue_overflow == QueueOverflow::Drop {
                    // A retransmission is dropped alone; new media with every queued frame
                    // of its stream (see "Discarding media").
                    if head.message.has(&Attribute::Retransmission) {
                        if let Some(dropped) = self.queue.pop_front() {
                            self.retransmission_dropped(&dropped);
                        }
                        self.queued_bits = (self.queued_bits - bits).max(0.0);
                        self.dropped += 1;
                    } else {
                        self.discard_stream_of(now, 0);
                    }
                    continue;
                }
                self.pacer.consume(bits);
                self.forced += 1;
            } else {
                index = self.next_index();
                if index != 0 {
                    bits = self.queue.get(index).map_or(bits, Self::bits_of);
                }
                if !self.try_spend(bits) {
                    break;
                }
            }

            let mut packet = self.queue.remove(index).expect("index within the queue");
            self.charge(&packet);
            self.queued_bits = (self.queued_bits - bits).max(0.0);
            // Rule 3: the packet departs now. Anything below recording departure — the send
            // history congestion control reads — must see the release instant, not the enqueue
            // instant, or this queueing delay is charged to the network.
            packet.now = now;
            // Queued for `poll_write`, which puts it back on the belt so it traverses every
            // interceptor ahead — numbering, and the send history reading the release instant just set.
            self.release(packet, bits);
        }
        if self.queue.is_empty() {
            self.queued_bits = 0.0;
        }
        if self.queue.is_empty() && self.held_audio.is_empty() {
            // No media waits: padding leaves from what the budget has left, never over it.
            while let Some((_, head)) = self.padding.front() {
                let bits = Self::bits_of(head);
                if !self.try_spend(bits) {
                    break;
                }
                let (_, mut packet) = self.padding.pop_front().expect("front just checked");
                packet.now = now;
                self.release(packet, bits);
            }
        }
        self.follow_queue(now);
    }
}

impl Interceptor for PacerInterceptor {
    /// Audio streams (and their repair flows) are not queued behind others. The repair flows of
    /// the other streams are remembered: a discard on one asks for no key frame.
    fn bind_local_stream(&mut self, info: &StreamInfo) {
        if info.mime_type.to_ascii_lowercase().starts_with("audio/") {
            self.audio.extend(crate::twcc::flow_ssrcs(info));
            // One flow with its stream in the share by priority.
            for repair in info.ssrc_rtx.into_iter().chain(info.ssrc_fec) {
                self.repaired_by.retain(|(ssrc, _)| *ssrc != repair);
                self.repaired_by.push((repair, info.ssrc));
            }
        } else {
            self.video.insert(info.ssrc);
            self.repair_flows
                .extend(info.ssrc_rtx.into_iter().chain(info.ssrc_fec));
            for repair in info.ssrc_rtx.into_iter().chain(info.ssrc_fec) {
                self.repaired_by.retain(|(ssrc, _)| *ssrc != repair);
                self.repaired_by.push((repair, info.ssrc));
            }
            if info.fec_is_flexfec() {
                self.flexfec_flows.extend(info.ssrc_fec);
            }
        }
    }

    fn unbind_local_stream(&mut self, info: &StreamInfo) {
        self.video.remove(&info.ssrc);
        for ssrc in crate::twcc::flow_ssrcs(info) {
            self.audio.remove(&ssrc);
            self.discarded_frame.remove(&ssrc);
            self.ceased.remove(&ssrc);
        }
        for ssrc in info.ssrc_rtx.into_iter().chain(info.ssrc_fec) {
            self.repair_flows.remove(&ssrc);
            self.flexfec_flows.remove(&ssrc);
            self.repaired_by.retain(|(repair, _)| *repair != ssrc);
        }
    }

    fn bind_remote_stream(&mut self, _info: &StreamInfo) {}

    fn unbind_remote_stream(&mut self, _info: &StreamInfo) {}
}