rtci-host 0.3.0

The host I/O around the sans-I/O rtci WebRTC engine: sockets, timers, STUN/TURN clients, ICE-TCP, mDNS and media, behind a permission gate and a socket factory
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
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
// Copyright 2026 Brainwires. MIT OR Apache-2.0.

//! Media: transceivers, senders and receivers over the engine's RTP stack
//! (SRTP, RTCP reports, NACK, TWCC), and the codec capabilities.
//!
//! There is no capture device and no codec here. The standard surface
//! (`addTransceiver`, `addTrack`, `getTransceivers`, `ontrack`, parameters,
//! capabilities, codec preferences, per-sender / per-receiver stats) works
//! without one; what goes out on a sender is what the program writes through
//! the `Deno.webrtc` extension (encoded frames, packetized here, or finished
//! RTP packets), and what a receiver gets is delivered as RTP packets to a
//! `Deno.webrtc.readRtp()` reader.
//!
//! The engine names transceivers by their index, which a removal (a rollback,
//! or an answer rejecting a stopped transceiver's section) shifts;
//! [`Core::keys`](crate::peer::Core) maps each index to a stable key that
//! JavaScript uses instead.

use std::collections::HashMap;
use std::collections::VecDeque;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;
use std::sync::atomic::Ordering;
use std::time::Instant;

use bytes::Bytes;
use rtci::interceptor::Attribute;
use rtci::interceptor::AttributedPacket;
use rtci::interceptor::CONGESTION_CONTROL_DEFAULT_PRUNE_HORIZON;
use rtci::interceptor::GCC_INITIAL_BITRATE;
use rtci::interceptor::Gcc;
use rtci::interceptor::Interceptor;
use rtci::interceptor::PacerBuilder;
use rtci::interceptor::PacerInterceptor;
use rtci::interceptor::Packet;
use rtci::interceptor::Registry;
use rtci::interceptor::Slot;
use rtci::interceptor::StreamInfo;
use rtci::interceptor::TaggedPacket;
use rtci::media_stream::MediaStreamTrack;
use rtci::peer_connection::RTCPeerConnectionBuilder;
use rtci::peer_connection::configuration::interceptor_registry::CongestionFeedback;
use rtci::peer_connection::configuration::interceptor_registry::DefaultInterceptorOptions;
use rtci::peer_connection::configuration::interceptor_registry::configure_congestion_control;
use rtci::peer_connection::configuration::interceptor_registry::configure_congestion_control_feedback;
use rtci::peer_connection::configuration::interceptor_registry::register_default_interceptors_with_options;
use rtci::peer_connection::configuration::media_engine::MediaEngine;
use rtci::peer_connection::cryptex::RTCRtpHeaderEncryptionPolicy;
use rtci::rtcp::payload_feedbacks::full_intra_request::FullIntraRequest;
use rtci::rtcp::payload_feedbacks::picture_loss_indication::PictureLossIndication;
use rtci::rtcp::transport_feedbacks::cc_feedback_report::CcFeedbackReport;
use rtci::rtcp::transport_feedbacks::transport_layer_cc::TransportLayerCc;
use rtci::rtp::packetizer::Packetizer;
use rtci::rtp_transceiver::RTCRtpTransceiverDirection;
use rtci::rtp_transceiver::RTCRtpTransceiverInit;
use rtci::rtp_transceiver::rtp_sender::RTCP_FB_ACK_CCFB;
use rtci::rtp_transceiver::rtp_sender::RTCRtpCodec;
use rtci::rtp_transceiver::rtp_sender::RTCRtpCodecParameters;
use rtci::rtp_transceiver::rtp_sender::RTCRtpCodingParameters;
use rtci::rtp_transceiver::rtp_sender::RTCRtpEncodingParameters;
use rtci::rtp_transceiver::rtp_sender::RTCRtpHeaderExtensionCapability;
use rtci::rtp_transceiver::rtp_sender::RTCRtpHeaderExtensionEncryption;
use rtci::rtp_transceiver::rtp_sender::RTCRtpHeaderExtensionToNegotiate;
use rtci::rtp_transceiver::rtp_sender::RTCRtpSendParameters;
use rtci::rtp_transceiver::rtp_sender::RtpCodecKind;
use rtci::rtp_transceiver::rtp_sender::TYPE_RTCP_FB_ACK;
use rtci::sansio::Protocol;
use rtci::shared::marshal::MarshalSize;
use rtci::shared::marshal::Unmarshal;
use serde::Deserialize;
use serde::Serialize;

use crate::error::Error;
use crate::error::ErrorKind;
use crate::peer::Core;

/// The MTU RTP packets are cut to (what browsers use; leaves room for SRTP,
/// UDP, IP and a TURN header), header extensions included: a sender's
/// payloads are smaller by the octets the engine adds
/// (`RTCRtpSender::max_packet_expansion`, RFC 7941 section 4.2.2), and by
/// more once a path reported a smaller MTU ([`rtp_mtu`]).
const RTP_MTU: usize = 1200;

/// The most an SRTP packet adds to the RTP packet it protects: the
/// authentication tag of the largest profile browsers negotiate
/// (AEAD_AES_128_GCM / AEAD_AES_256_GCM, 16 octets; RFC 7714 section 15).
const SRTP_MAX_OVERHEAD: usize = 16;

/// The smallest MTU [`rtp_mtu`] cuts to: a payload of a few hundred octets
/// still packetizes every codec, where a path report below the IPv4 minimum
/// would leave none.
const RTP_MTU_FLOOR: usize = 256;

/// The size a sender's RTP packets are cut to by its packetizer, header
/// included and the engine's extensions left out: [`RTP_MTU`], or what a
/// datagram of `datagram_limit` octets (the path's, once reported) carries
/// once SRTP has its tag, less `expansion`
/// (`RTCRtpSender::max_packet_expansion`), so the RTP packet on the wire,
/// extensions included, stays within both.
fn rtp_mtu(datagram_limit: Option<usize>, expansion: usize) -> usize {
  let mtu = datagram_limit.map_or(RTP_MTU, |limit| {
    RTP_MTU.min(limit.saturating_sub(SRTP_MAX_OVERHEAD))
  });
  mtu.saturating_sub(expansion).max(RTP_MTU_FLOOR)
}

/// The engine's media configuration: every codec browsers negotiate (Opus,
/// G.722, PCMU/PCMA, VP8, VP9, H.264, AV1, H.265, RTX), the default
/// interceptors (NACK, RTCP sender/receiver reports, TWCC feedback,
/// simulcast header extensions), send-side congestion control (GCC over
/// TWCC, with a pacer) and RFC 8888 congestion control feedback (`ack
/// ccfb`), which carries ECN.
pub fn media_stack() -> Result<(MediaEngine, Registry), Error> {
  media_stack_counting(Arc::default())
}

/// [`media_stack`], with the pacer counting the packets its full queue
/// refuses into `refusals` (a connection's `getStats()` reports them).
pub(crate) fn media_stack_counting(
  refusals: Arc<PacerRefusals>,
) -> Result<(MediaEngine, Registry), Error> {
  let mut media_engine = MediaEngine::default();
  media_engine
    .register_default_codecs()
    .map_err(crate::peer::rtc_err)?;
  // The defaults register RFC 4733 telephone events (RFC 7874 section 3),
  // at Opus' clock and at 8 kHz, for RTCDTMFSender: a second registration
  // here would put another format on a payload type the video RTX formats
  // use (RFC 9143 section 9.1.1 refuses one payload type with two
  // configurations in a BUNDLE group).
  // The defaults include the fork's key-frame request interceptor, which
  // delivers inbound PLI and FIR to the application (here, an
  // `EncodedMediaTrack`'s `keyframerequest`): upstream ends them at the
  // chain's terminus.
  // The audio level extensions (RFC 6464, RFC 6465) are offered in the
  // clear only, as Chromium, WebKit and Firefox offer them (and as the 0.22
  // engine did; rtci's `Chrome` profile value of
  // `audio_level_encryption`). The default, best effort, offers each also in
  // its RFC 6904 encrypted form and answers an offer that has both with the
  // encrypted one and the clear one `a=extmap:<id>/inactive`: that answer's
  // first "inactive" is then an extmap line's, not the section's direction
  // (WPT `protocol/direction.html` changes the first one to make an invalid
  // answer), and every audio section carries two mappings per extension.
  // Answering, the engine still takes the encrypted form an offer has (RFC
  // 6904 section 4.1). RFC 9335 cryptex (`rtpHeaderEncryptionPolicy`)
  // encrypts every extension and is not affected.
  let mut options =
    DefaultInterceptorOptions::for_profile(media_engine.behaviour_profile());
  options.audio_level_encryption = RTCRtpHeaderExtensionEncryption::Clear;
  let (registry, _handles) = register_default_interceptors_with_options(
    Registry::new(),
    &mut media_engine,
    &options,
  )
  .map_err(crate::peer::rtc_err)?;
  // Send-side congestion control: transport-wide sequence numbers on what
  // leaves, GCC over the TWCC feedback that comes back, and a pacer at its
  // estimate. The defaults already negotiate TWCC (`transport-cc` feedback and
  // the header extension, deduplicated here) and record arrivals for the
  // remote side; this takes the receiver's slot with the same recorder. The
  // pacer only queues (bounded), and the driver releases from it at the
  // engine's `poll_timeout`, so writes never wait on it. It paces at
  // PACING_FACTOR times the estimate, and only while the remote's congestion
  // feedback is in use: see [`FactorPacer`] for when it lets packets straight
  // through instead, what the stats report then, and how a packet its full
  // queue refuses is counted.
  let registry = configure_congestion_control(
    registry,
    Gcc::default(),
    CongestionFeedback::Twcc,
    &mut media_engine,
  )
  .map_err(crate::peer::rtc_err)?
  .with(Slot::Pacer, FactorPacer::new(refusals));
  // RFC 8888 feedback beside TWCC: `a=rtcp-fb:<pt> ack ccfb` on every codec
  // and the recorder that reports each received packet with the ECN
  // codepoint it arrived with. A stream sends ECT(1) (L4S) only once `ack
  // ccfb` is negotiated, since an L4S sender needs CE reported back; a peer
  // that doesn't answer it (browsers, unless Chrome's field trial is on)
  // negotiates without it and keeps TWCC alone. Where both are negotiated
  // the send history keeps one record per packet, so a packet both report
  // reaches the estimator once.
  let registry =
    configure_congestion_control_feedback(registry, &mut media_engine);
  register_header_extensions(&mut media_engine)?;
  Ok((media_engine, registry))
}

/// How much faster than the congestion controller's estimate the pacer lets
/// packets out: libwebrtc's pacing factor (`kDefaultPaceMultiplier`, 2.5).
/// The pacer smooths bursts (a key frame's packets); it is not what holds a
/// sender to the estimate, which is the encoder's job in a browser.
pub const PACING_FACTOR: f64 = 2.5;

/// How long the pacer keeps pacing without congestion feedback: from the
/// first packet of a stream that negotiated it, and from each report after
/// that. It is the congestion controller's own horizon
/// (`DEFAULT_PRUNE_HORIZON`, 2 s): past it the send history has written off
/// every packet the remote had not reported, so no report can still bring
/// the estimate up to date, and holding packets at it would cap the
/// connection at a number nothing measures. Remotes send TWCC every 50 to
/// 250 ms (libwebrtc) and RFC 8888 reports about once per round trip, so
/// this is eight reports or more: a few lost ones never trip it.
pub const FEEDBACK_TIMEOUT: std::time::Duration =
  CONGESTION_CONTROL_DEFAULT_PRUNE_HORIZON;

/// The longest the pacer lets its queue grow, in the time it takes to drain:
/// past it the pacer releases faster than the estimate, at what drains the
/// queue in this time (libwebrtc's pacer queue time limit,
/// `kMaxExpectedQueueLength`, 2 s). A source steadily above the estimate is
/// then delayed by this much at most, rather than by however long its
/// backlog takes at the estimate (minutes, at a low one).
pub const QUEUE_TIME_LIMIT: std::time::Duration =
  std::time::Duration::from_secs(2);

/// The engine pacer's rate while [`FactorPacer`] is not pacing: high enough
/// that what it still holds from before (packets queued while it was) goes
/// at the next wake-up.
const UNPACED_BITRATE: f64 = 1e9;

/// The packets (and their bytes) a connection's pacer refused because its
/// queue was full. Shared with the connection (`MediaState`), whose
/// `getStats()` adds them to the selected candidate pair's
/// `packetsDiscardedOnSend` / `bytesDiscardedOnSend`.
#[derive(Default, Debug)]
pub struct PacerRefusals {
  packets: AtomicU64,
  bytes: AtomicU64,
}

impl PacerRefusals {
  /// The packets refused so far, and their bytes.
  pub fn get(&self) -> (u64, u64) {
    (
      self.packets.load(Ordering::Relaxed),
      self.bytes.load(Ordering::Relaxed),
    )
  }

  pub(crate) fn add(&self, bytes: usize) {
    self.packets.fetch_add(1, Ordering::Relaxed);
    self.bytes.fetch_add(bytes as u64, Ordering::Relaxed);
  }
}

/// The pacer slot: the engine's pacer at [`PACING_FACTOR`] times the
/// congestion controller's estimate, while the remote's congestion feedback
/// is in use, and a pass-through otherwise.
///
/// - **Which packets.** Those of a local video stream (its RTX and FEC
///   SSRCs included) that negotiated feedback: the transport-wide sequence
///   number extension (TWCC) or `ack ccfb` (RFC 8888). A stream that
///   negotiated neither is never paced: the remote will not report on it, so
///   the estimate never moves and pacing would hold it at the initial rate
///   (GCC's 300 kb/s, so 750 kb/s here) for the life of the connection.
///   Audio is never paced either: its rate is small and steady, and behind a
///   video key frame in one queue it would arrive late (libwebrtc sends it
///   ahead of video). The congestion controller still counts it.
/// - **How fast.** At [`PACING_FACTOR`] times the estimate, or faster when
///   the queue would take longer than [`QUEUE_TIME_LIMIT`] to drain at that:
///   then at what drains it in that time.
/// - **When.** While reports keep coming: TWCC or RFC 8888 feedback (or an
///   estimate the controller made from one) went past within
///   [`FEEDBACK_TIMEOUT`]. Before the first report the window runs from
///   the first packet of a negotiated stream, at the initial rate, as a
///   browser starts; a remote that negotiated feedback but sends none, or
///   whose reports stop, gets pass-through once it passes, and pacing at the
///   estimate again with its next report. What was queued then goes at
///   once, and later packets of a stream wait behind it, so a stream's
///   packets keep their order.
/// - **Stats.** The estimate reaches `availableOutgoingBitrate` and
///   `targetBitrate` only while it paces: from the first report (the
///   initial rate until the controller moves it) to when reports stop, and
///   again from the next one. Without feedback `availableOutgoingBitrate` is
///   absent and `targetBitrate` is 0, as before any estimate: nothing is
///   estimated, and nothing but the source and the path limits the rate.
/// - **A full queue.** The engine pacer refuses a packet past its limit, as
///   a congested path would drop it, without an error to its caller (the
///   write reaches it later, from the driver). Each refusal is counted
///   ([`PacerRefusals`]) and logged once per run of them.
///
/// The engine paces at the estimate itself (it adds no headroom of its own)
/// and hands the same number to the stats, so the estimate stays what the
/// stats report; only the rate this pacer releases at is scaled.
pub(crate) struct FactorPacer {
  pacer: PacerInterceptor,
  /// The SSRCs (primary, RTX, FEC) of each bound local stream it paces
  /// ([`paces`](Self::paces)), by its primary SSRC.
  negotiated: HashMap<u32, Vec<u32>>,
  /// The controller's latest estimate (GCC's initial one before any).
  estimate: f64,
  /// When the last report (or estimate) came by.
  last_feedback: Option<Instant>,
  /// Before any report: when a negotiated stream's first packet went out.
  waiting_since: Option<Instant>,
  /// Whether packets of negotiated streams are paced now.
  paced: bool,
  /// What the engine pacer holds, per SSRC in the order written: each
  /// packet's sequence number, when it was written, and its bits. Per SSRC
  /// because the engine pacer (rtci 0.4) shares its queue among the streams
  /// by priority (RFC 8835 section 4.1): packets of different streams leave
  /// interleaved, not in the order written, while each stream's leave in
  /// its own order (a retransmission without RTX goes ahead of new media,
  /// so a released packet is looked up by its sequence number).
  held: HashMap<u32, VecDeque<(u16, Instant, f64)>>,
  /// The bits in `held`.
  held_bits: f64,
  /// The latest instant handed in.
  now: Option<Instant>,
  /// Whether the stats hold an estimate this pacer let through.
  estimate_reported: bool,
  /// Packets let straight through, behind what the engine pacer releases.
  passed: VecDeque<TaggedPacket>,
  /// Attribute carriers for the stats (an estimate withdrawn).
  carriers: VecDeque<TaggedPacket>,
  refusals: Arc<PacerRefusals>,
  /// In a run of refusals (logged at its first).
  refusing: bool,
}

impl FactorPacer {
  pub(crate) fn new(refusals: Arc<PacerRefusals>) -> Self {
    Self::with_pacer(
      PacerBuilder::new()
        .with_target_bitrate(GCC_INITIAL_BITRATE * PACING_FACTOR)
        // The queue's time limit is this pacer's (`rate`: drained faster
        // past QUEUE_TIME_LIMIT, never discarded for its age), not the
        // engine pacer's default, which drops what waited 2 s.
        .with_queue_time_limit(None)
        .build(),
      refusals,
    )
  }

  pub(crate) fn with_pacer(
    pacer: PacerInterceptor,
    refusals: Arc<PacerRefusals>,
  ) -> Self {
    Self {
      pacer,
      negotiated: HashMap::new(),
      estimate: GCC_INITIAL_BITRATE,
      last_feedback: None,
      waiting_since: None,
      paced: true,
      held: HashMap::new(),
      held_bits: 0.0,
      now: None,
      estimate_reported: false,
      passed: VecDeque::new(),
      carriers: VecDeque::new(),
      refusals,
      refusing: false,
    }
  }

  /// The estimate a packet on the read leg carries (the congestion
  /// controller attaches it to the feedback that produced it).
  fn estimate(msg: &TaggedPacket) -> Option<f64> {
    match msg.message.get(&Attribute::TargetBitrateChanged {
      bits_per_second: 0.0,
    }) {
      Some(Attribute::TargetBitrateChanged { bits_per_second }) => {
        Some(*bits_per_second)
      }
      _ => None,
    }
  }

  /// Whether an inbound packet is congestion feedback: a TWCC or an RFC
  /// 8888 report.
  fn is_feedback(msg: &TaggedPacket) -> bool {
    let Packet::Rtcp(packets) = &msg.message.packet else {
      return false;
    };
    packets.iter().any(|p| {
      let p = p.as_any();
      p.is::<TransportLayerCc>() || p.is::<CcFeedbackReport>()
    })
  }

  /// Whether a stream is paced: video that negotiated feedback the remote
  /// reports it with.
  fn paces(info: &StreamInfo) -> bool {
    !info.mime_type.to_ascii_lowercase().starts_with("audio/")
      && Self::negotiates_feedback(info)
  }

  /// Whether a stream negotiated feedback the remote reports it with.
  fn negotiates_feedback(info: &StreamInfo) -> bool {
    info
      .rtp_header_extensions
      .iter()
      .any(|e| e.uri == rtci::sdp::extmap::TRANSPORT_CC_URI)
      || info
        .rtcp_feedback
        .iter()
        .any(|f| f.typ == TYPE_RTCP_FB_ACK && f.parameter == RTCP_FB_ACK_CCFB)
  }

  fn is_negotiated(&self, ssrc: u32) -> bool {
    self.negotiated.values().any(|ssrcs| ssrcs.contains(&ssrc))
  }

  /// Whether negotiated streams are to be paced at `now`.
  fn pacing(&self, now: Instant) -> bool {
    if self.negotiated.is_empty() {
      return false;
    }
    match self.last_feedback.or(self.waiting_since) {
      Some(since) => now.saturating_duration_since(since) < FEEDBACK_TIMEOUT,
      None => true,
    }
  }

  /// The rate the engine pacer releases at now: the scaled estimate, or,
  /// when that is slower, what lets the queue out by the time its oldest
  /// packet has waited [`QUEUE_TIME_LIMIT`] (libwebrtc's rule: the time left
  /// is the limit less that wait, and at least a millisecond).
  fn rate(&self) -> f64 {
    if !self.paced {
      return UNPACED_BITRATE;
    }
    let paced = self.estimate * PACING_FACTOR;
    let oldest = self
      .held
      .values()
      .filter_map(|queue| queue.front().map(|&(_, written, _)| written))
      .min();
    let (Some(oldest), Some(now)) = (oldest, self.now) else {
      return paced;
    };
    let left = QUEUE_TIME_LIMIT
      .saturating_sub(now.saturating_duration_since(oldest))
      .max(std::time::Duration::from_millis(1));
    paced.max(self.held_bits / left.as_secs_f64())
  }

  /// Records the latest instant handed in.
  fn observe(&mut self, now: Instant) {
    if self.now.is_none_or(|last| now > last) {
      self.now = Some(now);
    }
  }

  /// Hands the engine pacer [`rate`](Self::rate).
  fn apply_rate(&mut self) {
    let rate = self.rate();
    self.pacer.pacer_mut().set_target_bitrate(rate);
  }

  /// Starts or stops pacing as feedback came or stopped.
  fn update(&mut self, now: Instant) {
    self.observe(now);
    let paced = self.pacing(now);
    if paced == self.paced {
      return;
    }
    self.paced = paced;
    self.apply_rate();
    if !paced && self.estimate_reported {
      // The stats stop reporting an estimate nothing is paced at.
      self.estimate_reported = false;
      let mut message = AttributedPacket::new(Packet::Rtcp(Vec::new()));
      message.add(Attribute::TargetBitrateChanged {
        bits_per_second: 0.0,
      });
      self.carriers.push_back(TaggedPacket {
        now,
        transport: Default::default(),
        message,
      });
    }
  }

  /// A packet the engine pacer released: its entry leaves `held`. One that
  /// was never held (padding the pacer made) changes nothing.
  fn released(&mut self, ssrc: u32, sequence_number: u16) {
    let Some(queue) = self.held.get_mut(&ssrc) else {
      return;
    };
    let Some(at) = queue.iter().position(|&(seq, _, _)| seq == sequence_number)
    else {
      return;
    };
    let Some((_, _, bits)) = queue.remove(at) else {
      return;
    };
    if queue.is_empty() {
      self.held.remove(&ssrc);
    }
    self.held_bits = if self.held.is_empty() {
      0.0
    } else {
      (self.held_bits - bits).max(0.0)
    };
  }

  /// The engine pacer's target rate.
  #[cfg(test)]
  pub(crate) fn target_bitrate(&self) -> f64 {
    self.pacer.pacer().target_bitrate()
  }

  /// The bits [`held`](Self::held) counts.
  #[cfg(test)]
  pub(crate) fn held_bits(&self) -> f64 {
    self.held_bits
  }
}

impl rtci::sansio::Protocol<TaggedPacket, TaggedPacket, ()> for FactorPacer {
  type Rout = TaggedPacket;
  type Wout = TaggedPacket;
  type Eout = ();
  type Error = rtci::shared::error::Error;
  type Time = Instant;

  fn handle_read(&mut self, mut msg: TaggedPacket) -> Result<(), Self::Error> {
    let estimate = Self::estimate(&msg);
    let feedback = estimate.is_some() || Self::is_feedback(&msg);
    if let Some(bits_per_second) = estimate {
      self.estimate = bits_per_second;
    }
    if feedback {
      self.last_feedback = Some(msg.now);
    }
    self.update(msg.now);
    if feedback && self.paced {
      if estimate.is_none() && !self.estimate_reported {
        // Pacing (again) at an estimate the stats don't hold: the first
        // report, or the first since reports stopped.
        msg.message.add(Attribute::TargetBitrateChanged {
          bits_per_second: self.estimate,
        });
      }
      self.estimate_reported = true;
    }
    if estimate.is_some() && !self.paced {
      // Not pacing (no congestion feedback negotiated, or none for
      // FEEDBACK_TIMEOUT): the stats report no estimate, even one the
      // controller attached without congestion feedback (rtci 0.2 does, on a
      // connection that negotiated none).
      msg
        .message
        .attributes
        .retain(|a| !matches!(a, Attribute::TargetBitrateChanged { .. }));
    }
    // The engine's pacer follows an estimate going past; the scaled rate
    // (or the pass-through's) replaces what it set.
    self.pacer.handle_read(msg)?;
    self.apply_rate();
    Ok(())
  }

  fn poll_read(&mut self) -> Option<TaggedPacket> {
    self.pacer.poll_read().or_else(|| self.carriers.pop_front())
  }

  fn handle_write(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
    let Packet::Rtp(rtp) = &msg.message.packet else {
      // RTCP: the engine's pacer passes it straight on (and takes the
      // carriers meant for it, the streams' priorities among them).
      return self.pacer.handle_write(msg);
    };
    let bytes = rtp.marshal_size();
    let (ssrc, sequence_number) = (rtp.header.ssrc, rtp.header.sequence_number);
    if !self.is_negotiated(rtp.header.ssrc) {
      self.passed.push_back(msg);
      return Ok(());
    }
    if self.last_feedback.is_none() && self.waiting_since.is_none() {
      self.waiting_since = Some(msg.now);
    }
    self.update(msg.now);
    if !self.paced && self.pacer.queued() == 0 {
      self.passed.push_back(msg);
      return Ok(());
    }
    let refused = self.pacer.dropped();
    self.pacer.handle_write(msg)?;
    if self.pacer.dropped() > refused {
      self.refusals.add(bytes);
      if !self.refusing {
        self.refusing = true;
        log::warn!(
          "webrtc: the pacer's queue is full ({} packets); refusing RTP until it drains",
          self.pacer.queued()
        );
      }
    } else {
      self.refusing = false;
      let bits = (bytes * 8) as f64;
      self.held.entry(ssrc).or_default().push_back((
        sequence_number,
        self.now.unwrap_or_else(Instant::now),
        bits,
      ));
      self.held_bits += bits;
      self.apply_rate();
    }
    Ok(())
  }

  fn poll_write(&mut self) -> Option<TaggedPacket> {
    // What the engine pacer releases was written before anything still let
    // through here.
    if let Some(out) = self.pacer.poll_write() {
      if let Packet::Rtp(rtp) = &out.message.packet {
        self.released(rtp.header.ssrc, rtp.header.sequence_number);
        self.apply_rate();
      }
      return Some(out);
    }
    self.passed.pop_front()
  }

  fn handle_event(&mut self, evt: ()) -> Result<(), Self::Error> {
    self.pacer.handle_event(evt)
  }

  fn poll_event(&mut self) -> Option<()> {
    self.pacer.poll_event()
  }

  fn handle_timeout(&mut self, now: Instant) -> Result<(), Self::Error> {
    self.update(now);
    self.apply_rate();
    self.pacer.handle_timeout(now)
  }

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

  fn close(&mut self) -> Result<(), Self::Error> {
    self.pacer.close()
  }
}

impl Interceptor for FactorPacer {
  fn bind_local_stream(&mut self, info: &StreamInfo) {
    if Self::paces(info) {
      let ssrcs = std::iter::once(info.ssrc)
        .chain(info.ssrc_rtx)
        .chain(info.ssrc_fec)
        .collect();
      self.negotiated.insert(info.ssrc, ssrcs);
    }
    self.pacer.bind_local_stream(info);
  }

  fn unbind_local_stream(&mut self, info: &StreamInfo) {
    self.negotiated.remove(&info.ssrc);
    self.pacer.unbind_local_stream(info);
  }

  fn bind_remote_stream(&mut self, info: &StreamInfo) {
    self.pacer.bind_remote_stream(info);
  }

  fn unbind_remote_stream(&mut self, info: &StreamInfo) {
    self.pacer.unbind_remote_stream(info);
  }
}

/// The RTP header extensions RFC 8834 section 5.2 has a WebRTC endpoint
/// support, on top of the ones the default interceptors register (the
/// simulcast `mid` / `rid` / `repaired-rid` for video, transport-wide
/// congestion control for both): `mid` for audio too (5.2.4, mandatory with
/// BUNDLE), the client-to-mixer audio level (5.2.2, RFC 6464) and the
/// coordination of video orientation (5.2.5), and absolute capture time
/// (abs-capture-time; RTCRtpTransceiver's header extension list starts it
/// "stopped", so an offer carries it only once the application asks, while an
/// answer takes it when offered). Registered after the defaults, so the ids
/// those have stay as they were. A sender writes the audio level and the
/// capture time (`header_extensions.rs`); the engine reads them from
/// received packets as any other extension.
fn register_header_extensions(
  media_engine: &mut MediaEngine,
) -> Result<(), Error> {
  for (uri, kind) in [
    (rtci::sdp::extmap::SDES_MID_URI, RtpCodecKind::Audio),
    (rtci::sdp::extmap::AUDIO_LEVEL_URI, RtpCodecKind::Audio),
    (
      rtci::sdp::extmap::VIDEO_ORIENTATION_URI,
      RtpCodecKind::Video,
    ),
    (
      crate::header_extensions::ABS_CAPTURE_TIME_URI,
      RtpCodecKind::Audio,
    ),
    (
      crate::header_extensions::ABS_CAPTURE_TIME_URI,
      RtpCodecKind::Video,
    ),
  ] {
    media_engine
      .register_header_extension(
        RTCRtpHeaderExtensionCapability {
          uri: uri.to_string(),
        },
        kind,
        None,
      )
      .map_err(crate::peer::rtc_err)?;
  }
  Ok(())
}

pub fn parse_kind(kind: &str) -> Result<RtpCodecKind, Error> {
  match kind {
    "audio" => Ok(RtpCodecKind::Audio),
    "video" => Ok(RtpCodecKind::Video),
    other => Err(Error::new(
      ErrorKind::Type,
      format!("'{other}' is not a valid media kind"),
    )),
  }
}

fn kind_str(kind: RtpCodecKind) -> &'static str {
  match kind {
    RtpCodecKind::Audio => "audio",
    RtpCodecKind::Video => "video",
    _ => "",
  }
}

pub fn parse_direction(
  direction: &str,
) -> Result<RTCRtpTransceiverDirection, Error> {
  match RTCRtpTransceiverDirection::from(direction) {
    RTCRtpTransceiverDirection::Unspecified => Err(Error::new(
      ErrorKind::Type,
      format!("'{direction}' is not a valid RTCRtpTransceiverDirection"),
    )),
    d => Ok(d),
  }
}

fn direction_str(direction: RTCRtpTransceiverDirection) -> Option<String> {
  match direction {
    RTCRtpTransceiverDirection::Unspecified => None,
    d => Some(d.to_string()),
  }
}

/// `RTCRtpCodec` as JavaScript sees it: a dictionary, so a member the codec
/// doesn't have (a video codec's `channels`, an absent fmtp line) is left
/// out rather than `null`.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CodecArg {
  pub mime_type: String,
  pub clock_rate: u32,
  #[serde(default, skip_serializing_if = "Option::is_none")]
  pub channels: Option<u16>,
  #[serde(default, skip_serializing_if = "Option::is_none")]
  pub sdp_fmtp_line: Option<String>,
}

impl CodecArg {
  fn from_codec(codec: &RTCRtpCodec) -> Self {
    let audio = codec.mime_type.to_ascii_lowercase().starts_with("audio/");
    Self {
      mime_type: codec.mime_type.clone(),
      clock_rate: codec.clock_rate,
      // An audio codec without a channel count is mono (RFC 4566's
      // default for the rtpmap encoding parameters).
      channels: if codec.channels > 0 {
        Some(codec.channels)
      } else {
        audio.then_some(1)
      },
      sdp_fmtp_line: (!codec.sdp_fmtp_line.is_empty())
        .then(|| codec.sdp_fmtp_line.clone()),
    }
  }

  fn to_codec(&self) -> RTCRtpCodec {
    let audio = self.mime_type.to_ascii_lowercase().starts_with("audio/");
    RTCRtpCodec {
      mime_type: self.mime_type.clone(),
      clock_rate: self.clock_rate,
      // A mono audio codec is the engine's 0 (no encoding parameter in its
      // rtpmap), the reverse of `from_codec`: the engine matches a codec
      // dictionary's channels exactly (W3C webrtc "codec dictionary
      // match"), so `channels: 1`, which getCapabilities() and
      // getParameters() report for PCMU, PCMA, G722, CN and
      // telephone-event, would match none of its codecs.
      channels: match self.channels {
        Some(1) if audio => 0,
        channels => channels.unwrap_or(0),
      },
      sdp_fmtp_line: self.sdp_fmtp_line.clone().unwrap_or_default(),
      rtcp_feedback: vec![],
    }
  }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Capabilities {
  pub(crate) codecs: Vec<CodecArg>,
  header_extensions: Vec<HeaderExtensionCapability>,
}

#[derive(Serialize)]
pub struct HeaderExtensionCapability {
  uri: String,
}

/// `RTCRtpSender.getCapabilities(kind)` / `RTCRtpReceiver.getCapabilities`:
/// what the media stack offers, read from the offer of a connection that is
/// never used for anything else (the engine's codec lists are internal).
/// RTX is listed once, without the `apt` that ties each RTX payload type to
/// its primary, as browsers list it.
pub fn capabilities(
  kind: &str,
  _sender: bool,
) -> Result<Option<Capabilities>, Error> {
  let Ok(kind_enum) = parse_kind(kind) else {
    return Ok(None);
  };
  let (media_engine, registry) = media_stack()?;
  let mut pc = RTCPeerConnectionBuilder::new()
    .with_media_engine(media_engine)
    .with_interceptor_registry(registry)
    .build(Instant::now())
    .map_err(crate::peer::rtc_err)?;
  let index = pc
    .add_transceiver_from_kind(
      kind_enum,
      Some(RTCRtpTransceiverInit {
        direction: RTCRtpTransceiverDirection::Sendrecv,
        streams: vec![],
        send_encodings: vec![RTCRtpEncodingParameters::default()],
      }),
    )
    .map_err(crate::peer::rtc_err)?;
  let offer = pc.create_offer(None).map_err(crate::peer::rtc_err)?;
  // The codecs come from the engine's registry (its own
  // `RTCRtpSender.getCapabilities`, W3C webrtc-pc 5.2: what this side can
  // send and receive), not read back from an offer. rtci 0.2.0 wrote every
  // H.264 High format of an offer with level 5.0's fmtp line, so an offer
  // read back merged them (WPT `protocol/codecs-filtered-by-direction`);
  // since 0.2.1 each payload type carries its own fmtp (rtci jsep-83), and
  // the registry stays the direct source. The offer gives the header
  // extensions.
  let registered = pc
    .rtp_sender(index.into())
    .and_then(|sender| sender.get_capabilities(kind_enum));
  let _ = pc.close();
  let mut caps = capabilities_from_sdp(kind, &offer.sdp);
  if let Some(registered) = registered {
    caps.codecs = codecs_from_registry(&registered.codecs);
  }
  Ok(Some(caps))
}

/// The engine's registered codecs as `getCapabilities()` lists them, each
/// once: RTX without the `apt` that ties it to one primary (the engine's
/// list already has none), every other format with its parameters, as
/// [`capabilities_from_sdp`] lists them.
fn codecs_from_registry(codecs: &[RTCRtpCodec]) -> Vec<CodecArg> {
  let mut out: Vec<CodecArg> = Vec::new();
  for codec in codecs {
    let mut arg = CodecArg::from_codec(codec);
    if arg.mime_type.to_ascii_lowercase().ends_with("/rtx") {
      arg.sdp_fmtp_line = None;
    }
    let duplicate = out.iter().any(|c| {
      c.mime_type.eq_ignore_ascii_case(&arg.mime_type)
        && c.clock_rate == arg.clock_rate
        && c.channels == arg.channels
        && c.sdp_fmtp_line == arg.sdp_fmtp_line
    });
    if !duplicate {
      out.push(arg);
    }
  }
  out
}

/// The codecs (`a=rtpmap` + `a=fmtp`) and header extensions (`a=extmap`)
/// of an SDP's media sections of `kind`.
pub fn capabilities_from_sdp(kind: &str, sdp: &str) -> Capabilities {
  let mut codecs: Vec<(String, CodecArg)> = Vec::new();
  let mut fmtp: HashMap<String, String> = HashMap::new();
  let mut header_extensions: Vec<(u16, HeaderExtensionCapability)> = Vec::new();
  let mut in_kind = false;
  for line in sdp.lines() {
    let line = line.trim_end();
    if let Some(m) = line.strip_prefix("m=") {
      in_kind = m.split_ascii_whitespace().next() == Some(kind);
      continue;
    }
    if !in_kind {
      continue;
    }
    if let Some(value) = line.strip_prefix("a=rtpmap:") {
      let Some((pt, rest)) = value.split_once(' ') else {
        continue;
      };
      let mut parts = rest.split('/');
      let name = parts.next().unwrap_or_default();
      let clock_rate = parts.next().and_then(|c| c.parse().ok()).unwrap_or(0);
      let channels = parts
        .next()
        .and_then(|c| c.parse().ok())
        .or((kind == "audio").then_some(1));
      codecs.push((
        pt.to_string(),
        CodecArg {
          mime_type: format!("{kind}/{name}"),
          clock_rate,
          channels,
          sdp_fmtp_line: None,
        },
      ));
    } else if let Some(value) = line.strip_prefix("a=fmtp:") {
      if let Some((pt, params)) = value.split_once(' ') {
        fmtp.insert(pt.to_string(), params.to_string());
      }
    } else if let Some(value) = line.strip_prefix("a=extmap:")
      && let Some(uri) = extmap_uri(value)
    {
      // By id: the engine writes the lines in no particular order.
      let id = value
        .split(|c: char| c == '/' || c.is_ascii_whitespace())
        .next()
        .and_then(|id| id.parse::<u16>().ok())
        .unwrap_or(u16::MAX);
      header_extensions.push((
        id,
        HeaderExtensionCapability {
          uri: uri.to_string(),
        },
      ));
    }
  }
  header_extensions.sort_by_key(|(id, _)| *id);
  // An extension offered both in the clear and encrypted (RFC 6904) is one
  // capability.
  let mut unique: Vec<HeaderExtensionCapability> = Vec::new();
  for (_, h) in header_extensions {
    if !unique.iter().any(|u| u.uri == h.uri) {
      unique.push(h);
    }
  }
  let header_extensions = unique;
  let mut out: Vec<CodecArg> = Vec::new();
  for (pt, mut codec) in codecs {
    // RTX is listed without the `apt` that ties its payload type to one
    // primary. Every other format keeps its parameters, an audio RED's
    // redundancy list (`111/111`) included, as browsers list it and as the
    // engine registered it: setCodecPreferences() matches them exactly.
    if !codec.mime_type.to_ascii_lowercase().ends_with("/rtx") {
      codec.sdp_fmtp_line = fmtp.get(&pt).cloned();
    }
    let duplicate = out.iter().any(|c| {
      c.mime_type.eq_ignore_ascii_case(&codec.mime_type)
        && c.clock_rate == codec.clock_rate
        && c.channels == codec.channels
        && c.sdp_fmtp_line == codec.sdp_fmtp_line
    });
    if !duplicate {
      out.push(codec);
    }
  }
  Capabilities {
    codecs: out,
    header_extensions,
  }
}

/// The RFC 6904 URI that marks an `a=extmap` line as the encrypted form of
/// the extension it names next.
const ENCRYPT_URI: &str = "urn:ietf:params:rtp-hdrext:encrypt";

/// The extension URI of an `a=extmap` value (`<id>[/<direction>] <uri>
/// [attributes]`); of the encrypted form (`<id>
/// urn:ietf:params:rtp-hdrext:encrypt <uri> [attributes]`, RFC 6904 section
/// 4, which rtci 0.2 offers for the audio level extensions beside the clear
/// one), the URI it encrypts.
pub(crate) fn extmap_uri(value: &str) -> Option<&str> {
  let mut tokens = value.split_ascii_whitespace().skip(1);
  match tokens.next()? {
    ENCRYPT_URI => tokens.next(),
    uri => Some(uri),
  }
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TrackArg {
  pub id: String,
  pub label: String,
}

#[derive(Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EncodingArg {
  #[serde(default)]
  pub rid: Option<String>,
  #[serde(default = "yes")]
  pub active: bool,
}

fn yes() -> bool {
  true
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TransceiverArg {
  pub kind: String,
  pub track: Option<TrackArg>,
  pub direction: String,
  pub streams: Vec<String>,
  pub send_encodings: Vec<EncodingArg>,
  /// The `Deno.webrtc.EncodedMediaTrack` codec, which becomes the sender's
  /// preferred codec.
  pub codec: Option<CodecArg>,
  /// `addTrack` made the transceiver (not `addTransceiver`): a remote
  /// offer's new media section of the same kind may take it.
  #[serde(default)]
  pub add_track: bool,
}

/// The stream ids of a sender's `a=msid` lines: the `MediaStream`s it is in,
/// or `-` for none (RFC 9429 section 5.2.1).
fn msid_streams(streams: Vec<String>) -> Vec<String> {
  if streams.is_empty() {
    vec!["-".to_string()]
  } else {
    streams
  }
}

/// One m-section of the remote description, by mid.
#[derive(Serialize, Clone, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct RemoteSection {
  /// The remote side's direction attribute (`sendrecv` / `sendonly` /
  /// `recvonly` / `inactive`).
  pub direction: String,
  /// The `a=msid` stream ids (`-` dropped).
  pub streams: Vec<String>,
  /// Port 0 without `a=bundle-only`: the section was rejected.
  pub rejected: bool,
  pub kind: String,
  /// The layers of the section's `a=simulcast` `recv` list (RFC 8853): the
  /// first of each layer's alternatives, without the paused marker `~`,
  /// each once, in the list's order.
  pub simulcast_recv: Vec<String>,
}

/// A transceiver as JavaScript reconciles it after every operation.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TransceiverInfo {
  pub key: u32,
  pub kind: String,
  pub mid: Option<String>,
  pub direction: Option<String>,
  pub current_direction: Option<String>,
  pub remote: Option<RemoteSection>,
}

/// The id of the stream a remote track with no `a=msid` is in.
const DEFAULT_STREAM_ID: &str = "default";

/// The m-sections of an SDP, keyed by mid.
pub fn sections(sdp: &str) -> HashMap<String, RemoteSection> {
  let mut out = HashMap::new();
  let mut current: Option<(Option<String>, RemoteSection)> = None;
  // Whether the current section has an `a=msid` line at all.
  let mut has_msid = false;
  let mut flush = |current: &mut Option<(Option<String>, RemoteSection)>,
                   has_msid: bool| {
    if let Some((Some(mid), mut section)) = current.take() {
      // A media section with no `a=msid` at all (an endpoint that predates
      // it): its track is in one default stream (RFC 9429 section 5.10; the
      // id `-` means "no stream" and stays none).
      if !has_msid && (section.kind == "audio" || section.kind == "video") {
        section.streams.push(DEFAULT_STREAM_ID.to_string());
      }
      out.insert(mid, section);
    }
  };
  for line in sdp.lines() {
    let line = line.trim_end();
    if let Some(m) = line.strip_prefix("m=") {
      flush(&mut current, has_msid);
      has_msid = false;
      let mut fields = m.split_ascii_whitespace();
      let kind = fields.next().unwrap_or_default().to_string();
      let port = fields.next().unwrap_or_default();
      current = Some((
        None,
        RemoteSection {
          direction: "sendrecv".to_string(),
          streams: vec![],
          rejected: port == "0",
          kind,
          simulcast_recv: vec![],
        },
      ));
      continue;
    }
    let Some((mid, section)) = current.as_mut() else {
      continue;
    };
    let Some(attr) = line.strip_prefix("a=") else {
      continue;
    };
    if let Some(value) = attr.strip_prefix("mid:") {
      *mid = Some(value.trim().to_string());
    } else if let Some(value) = attr.strip_prefix("msid:") {
      has_msid = true;
      if let Some(stream) = value.split_ascii_whitespace().next()
        && stream != "-"
        && !section.streams.iter().any(|s| s == stream)
      {
        section.streams.push(stream.to_string());
      }
    } else if let Some(value) = attr.strip_prefix("simulcast:") {
      section.simulcast_recv = simulcast_recv_layers(value);
    } else if matches!(attr, "sendrecv" | "sendonly" | "recvonly" | "inactive")
    {
      section.direction = attr.to_string();
    } else if attr == "bundle-only" {
      // RFC 8843 section 6 / RFC 9429 section 5.10: port zero with
      // `a=bundle-only` is a section reached through its BUNDLE group, not a
      // rejected one.
      section.rejected = false;
    }
  }
  flush(&mut current, has_msid);
  out
}

/// The layers of an `a=simulcast` value's `recv` list (`send 1;2 recv
/// a,b;~c`): the first alternative of each, `~` dropped, each once.
fn simulcast_recv_layers(value: &str) -> Vec<String> {
  let mut out: Vec<String> = Vec::new();
  let mut fields = value.split_ascii_whitespace();
  while let (Some(direction), Some(list)) = (fields.next(), fields.next()) {
    if !direction.eq_ignore_ascii_case("recv") {
      continue;
    }
    for layer in list.split(';') {
      let first = layer.split(',').next().unwrap_or_default();
      let rid = first.strip_prefix('~').unwrap_or(first);
      if !rid.is_empty() && !out.iter().any(|r| r == rid) {
        out.push(rid.to_string());
      }
    }
  }
  out
}

/// The codec preferences an `EncodedMediaTrack` gives its transceiver: its
/// codec, then (for audio) the telephone-event formats `RTCDTMFSender`
/// needs, and (for video) retransmission, which a preference list leaves out
/// unless it lists it (W3C `setCodecPreferences`). The engine offers no FEC
/// format by default, and a preference it doesn't offer is refused.
pub fn track_preferences(codec: &CodecArg) -> Vec<CodecArg> {
  let mut out = vec![codec.clone()];
  if codec.mime_type.to_ascii_lowercase().starts_with("video/") {
    out.push(CodecArg {
      mime_type: "video/rtx".to_string(),
      clock_rate: 90000,
      channels: None,
      sdp_fmtp_line: None,
    });
  }
  if codec.mime_type.to_ascii_lowercase().starts_with("audio/") {
    for clock_rate in [48000, 8000] {
      out.push(CodecArg {
        mime_type: "audio/telephone-event".to_string(),
        clock_rate,
        channels: None,
        sdp_fmtp_line: Some("0-15".to_string()),
      });
    }
  }
  out
}

/// What a sender's frames are cut into RTP packets with, per SSRC.
pub struct SenderPacketizer {
  ssrc: u32,
  payload_type: u8,
  mime_type: String,
  clock_rate: u32,
  /// The next frame's RTP timestamp.
  timestamp: u32,
  /// The size the packetizer cuts to ([`rtp_mtu`]).
  mtu: usize,
  /// The packetizer's sequence numbers, kept when only the size changes,
  /// so the SSRC's sequence goes on.
  sequencer: Box<dyn rtci::rtp::sequence::Sequencer>,
  packetizer: Box<dyn Packetizer>,
}

/// What an encoded frame's metadata names about its sender.
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SenderFrameInfo {
  pub synchronization_source: u32,
  pub payload_type: u8,
  pub mime_type: String,
  pub rtp_timestamp: u32,
}

/// A fake capture device's frame, for an encoded transform.
///
/// `P` is how the frame's bytes are held ([`crate::Payload`] from the
/// engine); see [`FakeFrame::map_payload`].
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FakeFrame<P = crate::peer::Payload> {
  pub data: P,
  pub duration_ms: f64,
  pub key_frame: bool,
  pub width_height: Option<(u16, u16)>,
  pub info: SenderFrameInfo,
  /// The RFC 6464 level (-dBov) measured from the frame's PCM, as the sender
  /// writes it in the audio level header extension
  /// (`header_extensions::audio_level`); `None` where the PCM isn't at hand
  /// (and for video).
  pub audio_level: Option<u8>,
}

impl<P> FakeFrame<P> {
  /// The same frame with its bytes converted by `f`.
  pub fn map_payload<Q>(self, f: impl FnOnce(P) -> Q) -> FakeFrame<Q> {
    FakeFrame {
      data: f(self.data),
      duration_ms: self.duration_ms,
      key_frame: self.key_frame,
      width_height: self.width_height,
      info: self.info,
      audio_level: self.audio_level,
    }
  }
}

/// Whether an encoded video frame is a key frame (audio frames are not
/// typed): from its bitstream.
pub fn is_key_frame(mime_type: &str, data: &[u8]) -> bool {
  match mime_type.to_ascii_lowercase().as_str() {
    // RFC 6386 9.1: bit 0 of the frame tag is 0 on a key frame.
    "video/vp8" => data.first().is_some_and(|b| b & 1 == 0),
    // VP9 uncompressed header: frame_marker (2), profile (2, a reserved
    // zero bit for profile 3), show_existing_frame, then frame_type (0 =
    // key).
    "video/vp9" => data.first().is_some_and(|b| {
      let profile = ((b >> 5) & 1) | (((b >> 4) & 1) << 1);
      let shift = if profile == 3 { 1 } else { 0 };
      (b >> (3 - shift)) & 1 == 0 && (b >> (2 - shift)) & 1 == 0
    }),
    "video/h264" => {
      nal_units(data).any(|n| n.first().is_some_and(|b| b & 0x1f == 5))
    }
    "video/h265" => nal_units(data).any(|n| {
      n.first()
        .is_some_and(|b| (16..=23).contains(&((b >> 1) & 0x3f)))
    }),
    "video/av1" => {
      // A sequence header OBU (type 1) starts every key frame's temporal
      // unit.
      data.iter().take(16).any(|b| (b >> 3) & 0xf == 1)
    }
    _ => false,
  }
}

/// The NAL units of an Annex B stream.
fn nal_units(data: &[u8]) -> impl Iterator<Item = &[u8]> {
  let mut starts = Vec::new();
  let mut i = 0;
  while i + 3 <= data.len() {
    if data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1 {
      starts.push(i + 3);
      i += 3;
    } else {
      i += 1;
    }
  }
  let ends: Vec<usize> = starts
    .iter()
    .skip(1)
    .map(|s| s - 3)
    .chain(std::iter::once(data.len()))
    .collect();
  starts
    .into_iter()
    .zip(ends)
    .map(move |(a, b)| &data[a..b.max(a)])
}

/// A key frame's size, where its bitstream says it simply (VP8).
pub fn frame_size(mime_type: &str, data: &[u8]) -> Option<(u16, u16)> {
  if !mime_type.eq_ignore_ascii_case("video/vp8")
    || data.len() < 10
    || data[0] & 1 != 0
    || data[3..6] != [0x9d, 0x01, 0x2a]
  {
    return None;
  }
  let w = u16::from_le_bytes([data[6], data[7]]) & 0x3fff;
  let h = u16::from_le_bytes([data[8], data[9]]) & 0x3fff;
  Some((w, h))
}

/// A format no encoder produces frames in: one that repairs or protects
/// another's packets (RTX, RED, ULPFEC, FlexFEC; the engine's
/// `is_repair_codec`), comfort noise or telephone events.
fn is_auxiliary_mime(mime: &str) -> bool {
  let mime = mime.to_ascii_lowercase();
  let subtype = mime.split_once('/').map_or(mime.as_str(), |(_, s)| s);
  matches!(subtype, "rtx" | "ulpfec" | "red" | "cn" | "telephone-event")
    || subtype.starts_with("flexfec")
}

/// One parameter of an `a=fmtp` line (`name=value;...`), lowercased.
fn fmtp_param(fmtp: &str, name: &str) -> Option<String> {
  fmtp.split(';').find_map(|p| {
    let (k, v) = p.split_once('=')?;
    k.trim()
      .eq_ignore_ascii_case(name)
      .then(|| v.trim().to_ascii_lowercase())
  })
}

/// The negotiated format that is `wanted`: same name, clock rate and
/// channels, the one with the same `a=fmtp` first, else any whose
/// parameters describe the same bitstream (H.264's packetization mode,
/// VP9's profile; the rest, such as a level, the receiver's limit).
fn negotiated_format<'a>(
  codecs: &'a [RTCRtpCodecParameters],
  wanted: &RTCRtpCodec,
) -> Option<&'a RTCRtpCodecParameters> {
  let same = |c: &&RTCRtpCodecParameters| {
    c.rtp_codec
      .mime_type
      .eq_ignore_ascii_case(&wanted.mime_type)
      && c.rtp_codec.clock_rate == wanted.clock_rate
      && c.rtp_codec.channels == wanted.channels
  };
  let mime = wanted.mime_type.to_ascii_lowercase();
  let bitstream: &[(&str, &str)] = match mime.as_str() {
    "video/h264" | "video/h265" => &[("packetization-mode", "0")],
    "video/vp9" => &[("profile-id", "0")],
    "video/av1" => &[("profile", "0")],
    _ => &[],
  };
  codecs
    .iter()
    .filter(same)
    .find(|c| c.rtp_codec.sdp_fmtp_line.trim() == wanted.sdp_fmtp_line.trim())
    .or_else(|| {
      codecs.iter().filter(same).find(|c| {
        bitstream.iter().all(|(name, default)| {
          fmtp_param(&c.rtp_codec.sdp_fmtp_line, name)
            .unwrap_or_else(|| default.to_string())
            == fmtp_param(&wanted.sdp_fmtp_line, name)
              .unwrap_or_else(|| default.to_string())
        })
      })
    })
}

/// The negotiated format a sender's frames go out in, with the payload type
/// the remote description gave it, from the engine sender's parameters as
/// the last applied description left them (W3C webrtc-pc 5.2: the codecs
/// negotiated for sending, in the remote m= line's order of preference):
/// - a sender fed by an `EncodedMediaTrack` sends its codec, the one its
///   bytes are already in: the negotiated format that is it, or none, and
///   then nothing is sent (another format's payload type on its bytes
///   would have the receiver decode them as that format);
/// - else the codec the first encoding names (`RTCRtpEncodingParameters`
///   `codec`, which the engine removes when an answer does not negotiate
///   it, webrtc-pc 4.4.1.6);
/// - else the first media format of the list (not a repair format, comfort
///   noise or telephone events): an answer that leads with H.264 has H.264
///   sent, whatever the local preferences or offer led with.
///
/// The engine's `write_rtp` rewrites a packet whose payload type is not
/// negotiated to its encoding's format, so a packet cut for the wrong format
/// would leave labelled as another: what is cut is chosen here, and always
/// carries a negotiated payload type.
pub(crate) fn sending_codec(
  params: &RTCRtpSendParameters,
  track_codec: Option<&CodecArg>,
) -> Option<RTCRtpCodecParameters> {
  let codecs: Vec<RTCRtpCodecParameters> = params
    .rtp_parameters
    .codecs
    .iter()
    .filter(|c| !is_auxiliary_mime(&c.rtp_codec.mime_type))
    .cloned()
    .collect();
  if let Some(track) = track_codec {
    return negotiated_format(&codecs, &track.to_codec()).cloned();
  }
  let named = params
    .encodings
    .first()
    .map(|e| &e.codec)
    .filter(|c| !c.mime_type.is_empty());
  match named {
    Some(codec) => negotiated_format(&codecs, codec).cloned(),
    None => codecs.into_iter().next(),
  }
}

fn payloader_for(
  mime: &str,
) -> Option<Box<dyn rtci::rtp::packetizer::Payloader>> {
  use rtci::rtp::codec;
  let mime = mime.to_ascii_lowercase();
  Some(match mime.as_str() {
    "audio/opus" => Box::<codec::opus::OpusPayloader>::default(),
    "audio/g722" | "audio/pcmu" | "audio/pcma" => {
      Box::<codec::g7xx::G7xxPayloader>::default()
    }
    "video/vp8" => Box::<codec::vp8::Vp8Payloader>::default(),
    "video/vp9" => Box::<codec::vp9::Vp9Payloader>::default(),
    "video/h264" => Box::<codec::h264::H264Payloader>::default(),
    "video/h265" => Box::<codec::h265::HevcPayloader>::default(),
    "video/av1" => Box::<codec::av1::Av1Payloader>::default(),
    _ => return None,
  })
}

/// RTP packets in, per transceiver key, while a reader wants them.
#[derive(Default)]
pub struct MediaState {
  /// Engine transceiver index -> stable key.
  pub keys: Vec<u32>,
  pub next_key: u32,
  /// Kinds of the transceivers, by key.
  pub kinds: HashMap<u32, RtpCodecKind>,
  /// Engine track id -> transceiver key (receivers' and senders' tracks).
  pub track_keys: HashMap<String, u32>,
  /// Keys whose receiver's RTP is read from JavaScript.
  pub reading: std::collections::HashSet<u32>,
  /// Keys whose sender's track is `null` or that a `setParameters` made
  /// inactive: what is written to them is dropped.
  pub paused: std::collections::HashSet<u32>,
  pub packetizers: HashMap<u32, SenderPacketizer>,
  /// The codec of the `EncodedMediaTrack` each sender is fed by, which the
  /// bytes of its `writeFrame` / `writeRtp` are in ([`sending_codec`]).
  pub track_codecs: HashMap<u32, CodecArg>,
  /// Keys whose receiver delivered its first packet (`unmute` was sent).
  pub flowing: std::collections::HashSet<u32>,
  /// The generation JavaScript gave each receiver's last mute
  /// (`op_webrtc_receiver_muted`); a `trackunmute` carries it, so one
  /// queued before a newer mute is told apart.
  pub unmute_generation: HashMap<u32, u32>,
  /// RFC 4733 state per sender: the next sequence number and timestamp.
  pub dtmf: HashMap<u32, (u16, u32)>,
  /// The SSRCs and CSRCs each receiver got packets from, for
  /// `getSynchronizationSources()` / `getContributingSources()`.
  pub sources: HashMap<u32, Vec<Source>>,
  /// Where each sender fed by a fake capture device is in its clip.
  pub fake: HashMap<u32, crate::fake_media::Cursor>,
  /// The encoded video frames each outbound RTP stream (by SSRC) took and
  /// sent, which the engine counts only when told (webrtc-stats encoder
  /// members: the encoder is upstream of the sender here).
  pub sent_video: HashMap<u32, SentVideo>,
  /// The complete video frames each inbound RTP stream (by SSRC) brought.
  pub received_video: HashMap<u32, ReceivedVideo>,
  /// What this connection's pacer refused ([`FactorPacer`]).
  pub pacer_refusals: Arc<PacerRefusals>,
  /// When each inbound stream (by SSRC) last asked its sender for a key
  /// frame after a loss ([`KEY_FRAME_REQUEST_INTERVAL`]).
  pub loss_key_frame_requests: HashMap<u32, Instant>,
  /// The largest datagram (UDP payload) the connection's path carries, the
  /// smallest the engine reported (`OnPathMtuExceeded`); `None` until a
  /// report. Senders' packets are cut for it ([`rtp_mtu`]).
  pub path_datagram_limit: Option<usize>,
}

impl MediaState {
  /// A path carries datagrams of at most `max_datagram` octets: never
  /// raised (RFC 8201 section 4), as the engine's own estimate.
  pub(crate) fn limit_datagrams(&mut self, max_datagram: usize) {
    self.path_datagram_limit = Some(
      self
        .path_datagram_limit
        .map_or(max_datagram, |limit| limit.min(max_datagram)),
    );
  }
}

/// How often a receiver whose stream lost a frame asks the sender for a key
/// frame again while no key frame came: once per SSRC per this interval, as
/// libwebrtc paces its repeated requests. Each delta frame after the loss
/// would otherwise send one.
pub const KEY_FRAME_REQUEST_INTERVAL: std::time::Duration =
  std::time::Duration::from_millis(500);

/// What an outbound video stream's frames were, as they reached its sender:
/// encoded frames, from a fake capture device's clip, an
/// `EncodedMediaTrack` or an encoded transform. Only what the frames
/// themselves show is counted (their number, key frames from the bitstream,
/// their coded size, their byte size); the encoder's own measures (QP,
/// encode time, quality limitation) are not known here and stay absent.
#[derive(Debug, Default)]
pub struct SentVideo {
  pub frames_encoded: u32,
  pub key_frames_encoded: u32,
  pub frames_sent: u32,
  pub huge_frames_sent: u32,
  /// Bytes of the frames sent, for their average size.
  pub bytes_sent: u64,
  /// The coded size of the last frame that named one.
  pub size: Option<(u32, u32)>,
  /// When each frame of the last second reached the sender.
  pub recent: std::collections::VecDeque<Instant>,
}

/// webrtc-stats `hugeFramesSent`: a frame at least 2.5 times the average
/// size of the frames sent before it.
const HUGE_FRAME_FACTOR: f64 = 2.5;

impl SentVideo {
  /// Frames that reached the sender in the second up to `now`
  /// (`framesPerSecond`).
  pub fn frames_per_second(&mut self, now: Instant) -> f64 {
    while self.recent.front().is_some_and(|t| {
      now.saturating_duration_since(*t) > std::time::Duration::from_secs(1)
    }) {
      self.recent.pop_front();
    }
    self.recent.len() as f64
  }

  /// One encoded frame reached the sender at `now`; `sent` when its packets
  /// went out.
  pub(crate) fn frame(
    &mut self,
    mime_type: &str,
    data: &[u8],
    sent: bool,
    now: Instant,
  ) {
    self.frames_encoded = self.frames_encoded.saturating_add(1);
    if is_key_frame(mime_type, data) {
      self.key_frames_encoded = self.key_frames_encoded.saturating_add(1);
    }
    if let Some(size) = crate::media_sink::frame_coded_size(mime_type, data) {
      self.size = Some(size);
    }
    self.recent.push_back(now);
    self.frames_per_second(now);
    if !sent {
      return;
    }
    if self.frames_sent > 0 {
      let average = self.bytes_sent as f64 / self.frames_sent as f64;
      if data.len() as f64 >= HUGE_FRAME_FACTOR * average {
        self.huge_frames_sent = self.huge_frames_sent.saturating_add(1);
      }
    }
    self.frames_sent = self.frames_sent.saturating_add(1);
    self.bytes_sent = self.bytes_sent.saturating_add(data.len() as u64);
  }

  fn update(&mut self, now: Instant) -> rtci::statistics::EncoderStatsUpdate {
    rtci::statistics::EncoderStatsUpdate {
      frames_encoded: Some(self.frames_encoded),
      key_frames_encoded: Some(self.key_frames_encoded),
      frames_sent: Some(self.frames_sent),
      huge_frames_sent: Some(self.huge_frames_sent),
      frame_width: self.size.map(|(w, _)| w),
      frame_height: self.size.map(|(_, h)| h),
      frames_per_second: Some(self.frames_per_second(now)),
      ..Default::default()
    }
  }
}

/// The frames of an inbound video stream as its packets arrive: a frame is
/// complete (webrtc-stats `framesReceived`) when its packets, from the one
/// after the previous frame's last (RTP marker) packet through its own
/// marker packet, all arrived in sequence. There is no jitter buffer, so a
/// frame with a packet missing or out of order is not counted, as the frame
/// assemblers (`encoded_frames.rs`) drop it; the first frame, whose start
/// can't be told, is not counted either.
#[derive(Debug, Default)]
pub struct ReceivedVideo {
  pub frames_received: u32,
  /// The RTP timestamp of the frame in the making.
  timestamp: Option<u32>,
  /// The sequence number the next packet should have.
  next: Option<u16>,
  /// Whether the frame in the making lost a packet (or its start).
  broken: bool,
  /// Whether the last packet ended a frame.
  ended: bool,
}

impl ReceivedVideo {
  /// A received packet without a payload: padding, or a ULPFEC packet of a
  /// RED stream (the engine delivers it emptied). It carries no frame but
  /// takes a sequence number of the stream, so the next packet is still in
  /// sequence after it; one that comes after a gap leaves the next frame
  /// uncounted, as its start may be what was lost.
  pub fn empty(&mut self, header: &rtci::rtp::Header) {
    if self.next != Some(header.sequence_number) {
      self.broken = true;
      self.ended = false;
    }
    self.next = Some(header.sequence_number.wrapping_add(1));
  }

  /// A received packet with a payload; returns whether it completed a frame.
  pub fn packet(&mut self, header: &rtci::rtp::Header) -> bool {
    let in_sequence = self.next == Some(header.sequence_number);
    if self.timestamp != Some(header.timestamp) {
      // A new frame starts whole only right after a frame's last packet.
      self.timestamp = Some(header.timestamp);
      self.broken = !(self.ended && in_sequence);
    } else if !in_sequence {
      self.broken = true;
    }
    self.next = Some(header.sequence_number.wrapping_add(1));
    self.ended = header.marker;
    if !header.marker {
      return false;
    }
    self.timestamp = None;
    if self.broken {
      return false;
    }
    self.frames_received = self.frames_received.saturating_add(1);
    true
  }
}

/// At most this many inbound video streams are followed per connection.
const MAX_RECEIVED_VIDEO_STREAMS: usize = 64;

/// A synchronization or contributing source a receiver heard from.
#[derive(Clone, Debug)]
pub struct Source {
  pub source: u32,
  pub contributing: bool,
  /// When its last packet arrived, in ms since the epoch.
  pub timestamp: f64,
  pub rtp_timestamp: u32,
}

/// Sources older than this are not reported (the spec's 10 seconds).
const SOURCE_LIFETIME_MS: f64 = 10_000.0;

fn epoch_ms() -> f64 {
  std::time::SystemTime::now()
    .duration_since(std::time::UNIX_EPOCH)
    .map(|d| d.as_secs_f64() * 1000.0)
    .unwrap_or(0.0)
}

impl MediaState {
  /// Records the sources of a received RTP packet.
  pub fn saw_rtp(&mut self, key: u32, header: &rtci::rtp::Header) {
    let now = epoch_ms();
    let list = self.sources.entry(key).or_default();
    let mut upsert = |source: u32, contributing: bool| {
      match list
        .iter_mut()
        .find(|s| s.source == source && s.contributing == contributing)
      {
        Some(s) => {
          s.timestamp = now;
          s.rtp_timestamp = header.timestamp;
        }
        None => {
          // Bounded: a stream can't carry more than 15 CSRCs a packet,
          // and stale ones are dropped below.
          list.push(Source {
            source,
            contributing,
            timestamp: now,
            rtp_timestamp: header.timestamp,
          });
        }
      }
    };
    upsert(header.ssrc, false);
    for csrc in &header.csrc {
      upsert(*csrc, true);
    }
    list.retain(|s| now - s.timestamp <= SOURCE_LIFETIME_MS);
  }
}

impl Core {
  /// A received RTP packet of a video receiver, for its stream's
  /// `framesReceived` (padding-only packets carry no frame).
  pub(crate) fn count_received_frame(
    &mut self,
    key: u32,
    packet: &rtci::rtp::Packet,
  ) {
    if self.media.kinds.get(&key) != Some(&RtpCodecKind::Video) {
      return;
    }
    let ssrc = packet.header.ssrc;
    let streams = &mut self.media.received_video;
    if !streams.contains_key(&ssrc)
      && streams.len() >= MAX_RECEIVED_VIDEO_STREAMS
    {
      return;
    }
    let video = streams.entry(ssrc).or_default();
    if packet.payload.is_empty() {
      video.empty(&packet.header);
      return;
    }
    if video.packet(&packet.header) {
      let frames_received = video.frames_received;
      self.pc.update_decoder_stats(
        ssrc,
        rtci::statistics::DecoderStatsUpdate {
          frames_received: Some(frames_received),
          ..Default::default()
        },
      );
    }
  }

  /// Brings the members measured over a window (`framesPerSecond`) up to
  /// `now` before a report.
  pub(crate) fn refresh_media_stats(&mut self, now: Instant) {
    for (ssrc, video) in &mut self.media.sent_video {
      let frames_per_second = video.frames_per_second(now);
      self.pc.update_encoder_stats(
        *ssrc,
        rtci::statistics::EncoderStatsUpdate {
          frames_per_second: Some(frames_per_second),
          ..Default::default()
        },
      );
    }
  }
}

impl MediaState {
  fn new_key(&mut self) -> u32 {
    self.next_key += 1;
    self.next_key
  }
}

impl Core {
  /// Gives every engine transceiver a key (new ones are appended by the
  /// engine).
  pub(crate) fn sync_transceiver_keys(&mut self) {
    let count = self.pc.get_transceivers().count();
    while self.media.keys.len() < count {
      let key = self.media.new_key();
      let index = self.media.keys.len();
      self.media.keys.push(key);
      let kind = self
        .pc
        .rtp_receiver(index.into())
        .map(|r| r.track().kind())
        .filter(|k| *k != RtpCodecKind::Unspecified)
        .or_else(|| {
          self
            .pc
            .rtp_sender(index.into())
            .and_then(|s| s.track().map(|t| t.kind()))
        });
      if let Some(kind) = kind.filter(|k| *k != RtpCodecKind::Unspecified) {
        self.media.kinds.insert(key, kind);
      }
    }
  }

  /// After a description (or a rollback): drops the keys of the transceivers
  /// the engine removed from its set, which moves the later ones down (a
  /// rollback undoing the remote offer that created them, RFC 9429 section
  /// 5.7; an answer leaving a stopped transceiver's m= section rejected, W3C
  /// webrtc-pc 4.4.1.6), then keys the ones it added. The engine reports
  /// each removal by the index the transceiver had then, in removal order,
  /// so removing them in that order keeps the keys in step. An index past
  /// the keys is a transceiver the same call added and removed again.
  pub(crate) fn sync_removed_transceivers(&mut self) {
    for index in self.pc.take_removed_transceivers() {
      if index >= self.media.keys.len() {
        continue;
      }
      let key = self.media.keys.remove(index);
      self.forget_transceiver(key);
    }
    self.sync_transceiver_keys();
  }

  /// Per-key state of a transceiver that left the set.
  fn forget_transceiver(&mut self, key: u32) {
    let media = &mut self.media;
    media.kinds.remove(&key);
    media.track_keys.retain(|_, k| *k != key);
    media.reading.remove(&key);
    media.paused.remove(&key);
    media.packetizers.remove(&key);
    media.track_codecs.remove(&key);
    media.flowing.remove(&key);
    media.dtmf.remove(&key);
    media.sources.remove(&key);
    media.fake.remove(&key);
  }

  pub(crate) fn index_of(&self, key: u32) -> Result<usize, Error> {
    self
      .media
      .keys
      .iter()
      .position(|k| *k == key)
      .ok_or_else(|| {
        Error::new(
          ErrorKind::InvalidState,
          "The transceiver is gone".to_string(),
        )
      })
  }

  pub(crate) fn add_transceiver(
    &mut self,
    arg: TransceiverArg,
  ) -> Result<u32, Error> {
    let kind = parse_kind(&arg.kind)?;
    let direction = parse_direction(&arg.direction)?;
    // Every W3C transceiver has a sender and a receiver: the engine's are
    // made by creating it sendrecv (a sender needs an encoding there), then
    // the asked-for direction is set.
    let mut encodings: Vec<RTCRtpEncodingParameters> = arg
      .send_encodings
      .iter()
      .map(|e| RTCRtpEncodingParameters {
        rtp_coding_parameters: RTCRtpCodingParameters {
          rid: e.rid.clone().unwrap_or_default(),
          ..Default::default()
        },
        active: e.active,
        codec: arg
          .codec
          .as_ref()
          .map(CodecArg::to_codec)
          .unwrap_or_default(),
        ..Default::default()
      })
      .collect();
    if encodings.is_empty() {
      encodings.push(RTCRtpEncodingParameters {
        active: true,
        codec: arg
          .codec
          .as_ref()
          .map(CodecArg::to_codec)
          .unwrap_or_default(),
        ..Default::default()
      });
    }
    let init = RTCRtpTransceiverInit {
      direction: RTCRtpTransceiverDirection::Sendrecv,
      streams: arg.streams.clone(),
      send_encodings: encodings.clone(),
    };
    let index = match &arg.track {
      Some(track) => {
        let stream = msid_streams(arg.streams.clone()).remove(0);
        let engine_track = MediaStreamTrack::new(
          stream,
          track.id.clone(),
          track.label.clone(),
          kind,
          encodings,
        );
        self
          .pc
          .add_transceiver_from_track(engine_track, Some(init))
          .map_err(crate::peer::rtc_err)?
      }
      None => self
        .pc
        .add_transceiver_from_kind(kind, Some(init))
        .map_err(crate::peer::rtc_err)?,
    };
    self.sync_transceiver_keys();
    let key = self.media.keys[index];
    self.media.kinds.insert(key, kind);
    if let Some(mut sender) = self.pc.rtp_sender(index.into()) {
      sender
        .set_streams(msid_streams(arg.streams))
        .map_err(crate::peer::rtc_err)?;
      // A transceiver made from a kind has no engine track (W3C null).
      if let Some(track) = sender.track() {
        let id = track.track_id().to_string();
        self.media.track_keys.insert(id, key);
      }
    }
    // RFC 9429 section 5.10: a remote offer's new media section takes a
    // transceiver `addTrack` created, never one `addTransceiver` did.
    if !arg.add_track
      && let Some(mut t) = self.pc.rtp_transceiver(index)
    {
      t.set_associable_with_remote_offer(false);
    }
    if arg.track.is_none() {
      self.media.paused.insert(key);
    }
    if let Some(codec) = &arg.codec {
      self.media.track_codecs.insert(key, codec.clone());
      let _ =
        self.set_codec_preferences_inner(index, &track_preferences(codec));
    }
    if direction != RTCRtpTransceiverDirection::Sendrecv
      && let Some(mut t) = self.pc.rtp_transceiver(index)
    {
      t.set_direction(direction).map_err(crate::peer::rtc_err)?;
    }
    Ok(key)
  }

  /// `addTrack` onto a transceiver JavaScript chose for reuse: the sender
  /// takes the track (its id and streams go in the msid) and starts sending.
  /// A transceiver a remote offer created has no engine sender until now; the
  /// engine hands out a sender only while the direction sends, so the
  /// direction gains `send` first. One `addTransceiver` created stays out of a
  /// remote offer's new m-sections (WPT checkAddTransceiverThenAddTrackPairs:
  /// browsers keep it unassociable), and `addTrack` keeps a transceiver a
  /// rolled-back remote offer created (W3C 4.4.1.6).
  pub(crate) fn attach_track(
    &mut self,
    key: u32,
    track: TrackArg,
    streams: Vec<String>,
  ) -> Result<(), Error> {
    let index = self.index_of(key)?;
    let kind = self.media.kinds.get(&key).copied().unwrap_or_default();
    let streams = msid_streams(streams);
    let stream = streams[0].clone();
    if let Some(mut t) = self.pc.rtp_transceiver(index) {
      let has_recv = t.direction().has_recv();
      t.set_direction(RTCRtpTransceiverDirection::from_send_recv(
        true, has_recv,
      ))
      .map_err(crate::peer::rtc_err)?;
      t.keep_on_rollback();
    }
    let has_sender = self
      .pc
      .rtp_transceiver(index)
      .is_some_and(|t| t.sender().is_some());
    if !has_sender {
      self
        .pc
        .add_track_to_transceiver(
          index,
          MediaStreamTrack::new(
            stream,
            track.id.clone(),
            track.label,
            kind,
            vec![RTCRtpEncodingParameters {
              active: true,
              ..Default::default()
            }],
          ),
        )
        .map_err(crate::peer::rtc_err)?;
      if let Some(mut sender) = self.pc.rtp_sender(index.into()) {
        sender.set_streams(streams).map_err(crate::peer::rtc_err)?;
      }
      self.media.track_keys.insert(track.id, key);
      self.media.paused.remove(&key);
      return Ok(());
    }
    let Some(mut sender) = self.pc.rtp_sender(index.into()) else {
      return Err(Error::new(
        ErrorKind::InvalidState,
        "The transceiver has no sender".to_string(),
      ));
    };
    // A sender made from a kind has no engine track: the new one takes one
    // active encoding, unless the sender has encodings of its own (which
    // the engine keeps, with their SSRCs).
    let codings = sender.track().map_or_else(
      || {
        vec![RTCRtpEncodingParameters {
          active: true,
          ..Default::default()
        }]
      },
      |t| t.codings().to_vec(),
    );
    let old = sender.track().map(|t| t.track_id().to_string());
    sender
      .replace_track(Some(MediaStreamTrack::new(
        stream,
        track.id.clone(),
        track.label,
        kind,
        codings,
      )))
      .map_err(crate::peer::rtc_err)?;
    sender.set_streams(streams).map_err(crate::peer::rtc_err)?;
    if let Some(old) = old {
      self.media.track_keys.remove(&old);
    }
    self.media.track_keys.insert(track.id, key);
    self.media.paused.remove(&key);
    Ok(())
  }

  /// A transceiver a remote offer created has no engine sender. Made to send
  /// (a sending direction, or `replaceTrack` with a track) without `addTrack`,
  /// it gets one: SSRCs in the answer, an outbound stream. A rollback still
  /// removes it (only `addTrack` keeps it).
  fn ensure_sender(&mut self, key: u32) {
    if let Ok(index) = self.index_of(key) {
      let _ = self.pc.ensure_transceiver_sender(index);
    }
  }

  pub(crate) fn set_sender_paused(&mut self, key: u32, paused: bool) {
    if paused {
      self.media.paused.insert(key);
    } else {
      self.ensure_sender(key);
      self.media.paused.remove(&key);
    }
  }

  /// The sender's track changed: `codec` is the new track's when it is an
  /// `EncodedMediaTrack` (what its frames are in), `None` for any other
  /// track or none.
  pub(crate) fn set_sender_track_codec(
    &mut self,
    key: u32,
    codec: Option<CodecArg>,
  ) {
    match codec {
      Some(codec) => {
        self.media.track_codecs.insert(key, codec);
      }
      None => {
        self.media.track_codecs.remove(&key);
      }
    }
  }

  pub(crate) fn set_streams(
    &mut self,
    key: u32,
    streams: Vec<String>,
  ) -> Result<(), Error> {
    let index = self.index_of(key)?;
    if let Some(mut sender) = self.pc.rtp_sender(index.into()) {
      sender
        .set_streams(msid_streams(streams))
        .map_err(crate::peer::rtc_err)?;
    }
    Ok(())
  }

  pub(crate) fn set_direction(
    &mut self,
    key: u32,
    direction: &str,
  ) -> Result<(), Error> {
    let direction = parse_direction(direction)?;
    let index = self.index_of(key)?;
    if direction.has_send() {
      self.ensure_sender(key);
    }
    if let Some(mut t) = self.pc.rtp_transceiver(index) {
      t.set_direction(direction).map_err(crate::peer::rtc_err)?;
    }
    Ok(())
  }

  pub(crate) fn stop_transceiver(&mut self, key: u32) -> Result<(), Error> {
    let index = self.index_of(key)?;
    self.media.paused.insert(key);
    self.media.reading.remove(&key);
    if let Some(mut t) = self.pc.rtp_transceiver(index) {
      t.stop().map_err(crate::peer::rtc_err)?;
    }
    Ok(())
  }

  fn set_codec_preferences_inner(
    &mut self,
    index: usize,
    codecs: &[CodecArg],
  ) -> Result<(), Error> {
    let kind = self
      .media
      .kinds
      .get(&self.media.keys[index])
      .copied()
      .unwrap_or_default();
    let params: Vec<RTCRtpCodecParameters> = codecs
      .iter()
      .map(|c| RTCRtpCodecParameters {
        rtp_codec: c.to_codec(),
        payload_type: 0,
      })
      .collect();
    if let Some(mut t) = self.pc.rtp_transceiver(index) {
      t.set_codec_preferences(params).map_err(|e| {
        Error::new(
          ErrorKind::InvalidModification,
          format!("setCodecPreferences: {e} ({} codec)", kind_str(kind)),
        )
      })?;
    }
    Ok(())
  }

  pub(crate) fn set_codec_preferences(
    &mut self,
    key: u32,
    codecs: Vec<CodecArg>,
  ) -> Result<(), Error> {
    let index = self.index_of(key)?;
    self.set_codec_preferences_inner(index, &codecs)
  }

  /// `setHeaderExtensionsToNegotiate()` and its effect on the next offer or
  /// answer (webrtc-extensions): the URIs this transceiver's media section
  /// may carry. JavaScript hands the list over before each offer and answer.
  pub(crate) fn set_header_extensions_to_negotiate(
    &mut self,
    key: u32,
    uris: Vec<String>,
  ) -> Result<(), Error> {
    let index = self.index_of(key)?;
    let Some(mut t) = self.pc.rtp_transceiver(index) else {
      return Ok(());
    };
    // The engine takes the whole list with a direction per extension: the
    // ones JavaScript names are negotiated (an extension stopped before
    // comes back "sendrecv"), the others "stopped".
    let list: Vec<RTCRtpHeaderExtensionToNegotiate> = t
      .get_header_extensions_to_negotiate()
      .into_iter()
      .map(|mut e| {
        if !uris.contains(&e.uri) {
          e.direction = RTCRtpTransceiverDirection::Stopped;
        } else if e.direction == RTCRtpTransceiverDirection::Stopped {
          e.direction = RTCRtpTransceiverDirection::Sendrecv;
        }
        e
      })
      .collect();
    // JavaScript refuses to stop an extension every WebRTC endpoint must
    // implement (webrtc-extensions; RFC 8834 section 5.2), as the engine
    // does: what reaches here is what the engine takes.
    t.set_header_extensions_to_negotiate(list)
      .map_err(|e| Error::new(ErrorKind::InvalidModification, e.to_string()))?;
    Ok(())
  }

  /// `RTCConfiguration.rtpHeaderEncryptionPolicy` and
  /// `alwaysNegotiateDataChannels` (webrtc-extensions).
  pub(crate) fn set_negotiation_options(
    &mut self,
    rtp_header_encryption_policy: &str,
    always_negotiate_data_channels: bool,
  ) -> Result<(), Error> {
    let policy = match rtp_header_encryption_policy {
      "disable" => RTCRtpHeaderEncryptionPolicy::Disable,
      "negotiate" => RTCRtpHeaderEncryptionPolicy::Negotiate,
      "require" => RTCRtpHeaderEncryptionPolicy::Require,
      other => {
        return Err(Error::new(
          ErrorKind::Type,
          format!("'{other}' is not a valid RTCRtpHeaderEncryptionPolicy"),
        ));
      }
    };
    self
      .pc
      .set_rtp_header_encryption_policy(policy)
      .map_err(crate::peer::rtc_err)?;
    self
      .pc
      .set_always_negotiate_data_channels(always_negotiate_data_channels)
      .map_err(crate::peer::rtc_err)?;
    Ok(())
  }

  pub(crate) fn transceivers(&mut self) -> Vec<TransceiverInfo> {
    self.sync_transceiver_keys();
    let remote = self
      .pc
      .remote_description()
      .map(|d| sections(&d.sdp))
      .unwrap_or_default();
    let mut out = Vec::with_capacity(self.media.keys.len());
    for (index, key) in self.media.keys.clone().into_iter().enumerate() {
      let Some(t) = self.pc.rtp_transceiver(index) else {
        continue;
      };
      let mid = t.mid().clone();
      let direction = direction_str(t.direction());
      let current_direction = t.current_direction().and_then(direction_str);
      let section = mid.as_ref().and_then(|m| remote.get(m)).cloned();
      // The remote side doesn't send on this section (now): its track is
      // muted, and the next RTP that arrives unmutes it again.
      if section.as_ref().is_some_and(|s| {
        s.rejected || !matches!(s.direction.as_str(), "sendrecv" | "sendonly")
      }) {
        self.media.flowing.remove(&key);
      }
      let kind = match self.media.kinds.get(&key) {
        Some(kind) => kind_str(*kind).to_string(),
        None => {
          let kind = section
            .as_ref()
            .and_then(|s| parse_kind(&s.kind).ok())
            .unwrap_or_default();
          if kind != RtpCodecKind::Unspecified {
            self.media.kinds.insert(key, kind);
          }
          kind_str(kind).to_string()
        }
      };
      // A receiver's engine track id reaches the RTP it delivers.
      if let Some(r) = self.pc.rtp_receiver(index.into()) {
        let id = r.track().track_id().to_string();
        if !id.is_empty() {
          self.media.track_keys.insert(id, key);
        }
      }
      out.push(TransceiverInfo {
        key,
        kind,
        mid,
        direction,
        current_direction,
        remote: section,
      });
    }
    out
  }

  /// The codecs of the transceiver's m-section in the current local
  /// (`local`) or remote description, in the section's order.
  fn negotiated_section_codecs(
    &mut self,
    index: usize,
    local: bool,
  ) -> Option<(String, Vec<(u8, CodecArg)>)> {
    let mid = self.pc.rtp_transceiver(index)?.mid().clone()?;
    let sdp = if local {
      self
        .pc
        .current_local_description()
        .or(self.pc.pending_local_description())
        .map(|d| d.sdp)
    } else {
      self
        .pc
        .current_remote_description()
        .or(self.pc.remote_description())
        .map(|d| d.sdp.clone())
    }?;
    section_codecs(&sdp, &mid)
  }

  pub(crate) fn sender_parameters(
    &mut self,
    key: u32,
  ) -> Result<serde_json::Value, Error> {
    let index = self.index_of(key)?;
    // webrtc-pc 5.2 getParameters(): the codecs negotiated for sending. The
    // set is the engine's own (its sender's codecs: the formats of the
    // remote description this side can send, since rtci 0.3 also those a
    // remote answer added beyond the offer, RFC 9429 5.11, DENEXT.md item
    // 572); each is written as the remote description has it (its payload
    // type and fmtp line, RFC 9429 5.3.1), in the remote m= line's order of
    // preference.
    let remote_section = self.negotiated_section_codecs(index, false);
    let remote_codecs = remote_section
      .clone()
      .map(|(kind, codecs)| sendable_codecs(&kind, codecs));
    let Some(mut sender) = self.pc.rtp_sender(index.into()) else {
      // A transceiver a remote offer made has no engine sender until it
      // sends; what it may send is negotiated all the same.
      return Ok(match remote_codecs {
        Some(codecs) => serde_json::json!({
          "codecs": codecs
            .iter()
            .map(|(pt, c)| section_codec_json(*pt, c))
            .collect::<Vec<_>>(),
          "headerExtensions": [],
          "encodings": [],
        }),
        None => serde_json::Value::Null,
      });
    };
    let params = sender.get_parameters().clone();
    let codecs = match remote_codecs {
      // The engine holds no send codecs for a transceiver negotiated not
      // to send; getParameters() lists what the remote side would take all
      // the same (WPT RTCRtpParameters-codecs "should have codecs after
      // negotiation", a recvonly answerer).
      Some(codecs) if params.rtp_parameters.codecs.is_empty() => codecs
        .iter()
        .map(|(pt, c)| section_codec_json(*pt, c))
        .collect(),
      Some(_) => negotiated_send_codecs(
        &params.rtp_parameters.codecs,
        remote_section
          .as_ref()
          .map_or(&[][..], |(_, c)| c.as_slice()),
      ),
      None => params
        .rtp_parameters
        .codecs
        .iter()
        .map(codec_params_json)
        .collect(),
    };
    Ok(serde_json::json!({
      "codecs": codecs,
      "headerExtensions": params.rtp_parameters.header_extensions.iter().map(|h| serde_json::json!({
        "uri": h.uri, "id": h.id, "encrypted": h.encrypted,
      })).collect::<Vec<_>>(),
      "encodings": params.encodings.iter().map(|e| serde_json::json!({
        "rid": e.rtp_coding_parameters.rid,
        "ssrc": e.rtp_coding_parameters.ssrc,
      })).collect::<Vec<_>>(),
    }))
  }

  pub(crate) fn receiver_parameters(
    &mut self,
    key: u32,
  ) -> Result<serde_json::Value, Error> {
    let index = self.index_of(key)?;
    // webrtc-pc 5.3 getParameters(): the codecs the receiver is prepared to
    // receive, which are the local description's for its m-section.
    let local_codecs = self.negotiated_section_codecs(index, true);
    let Some(mut receiver) = self.pc.rtp_receiver(index.into()) else {
      return Ok(serde_json::Value::Null);
    };
    let params = receiver.get_parameters().clone();
    let codecs: Vec<serde_json::Value> = match local_codecs {
      Some((_, codecs)) => codecs
        .iter()
        .map(|(pt, c)| section_codec_json(*pt, c))
        .collect(),
      None => params
        .rtp_parameters
        .codecs
        .iter()
        .map(codec_params_json)
        .collect(),
    };
    Ok(serde_json::json!({
      "codecs": codecs,
      "headerExtensions": params.rtp_parameters.header_extensions.iter().map(|h| serde_json::json!({
        "uri": h.uri, "id": h.id, "encrypted": h.encrypted,
      })).collect::<Vec<_>>(),
    }))
  }

  /// `getSynchronizationSources()` / `getContributingSources()`: the
  /// sources heard in the last 10 seconds, most recent first, with
  /// timestamps on the wall clock as the spec has them (ms since the epoch).
  pub(crate) fn receiver_sources(
    &mut self,
    key: u32,
    contributing: bool,
  ) -> Result<Vec<serde_json::Value>, Error> {
    self.index_of(key)?;
    let now = epoch_ms();
    let mut list: Vec<Source> = self
      .media
      .sources
      .get(&key)
      .map(|l| {
        l.iter()
          .filter(|s| {
            s.contributing == contributing
              && now - s.timestamp <= SOURCE_LIFETIME_MS
          })
          .cloned()
          .collect()
      })
      .unwrap_or_default();
    list.sort_by(|a, b| b.timestamp.total_cmp(&a.timestamp));
    Ok(
      list
        .into_iter()
        .map(|s| {
          serde_json::json!({
            "timestamp": s.timestamp,
            "source": s.source,
            "rtpTimestamp": s.rtp_timestamp,
          })
        })
        .collect(),
    )
  }

  /// `Deno.webrtc` `writeRtp`: a finished RTP packet; the SSRC becomes the
  /// sender's (its first encoding's) and an unknown payload type the
  /// negotiated one.
  pub(crate) fn write_rtp(
    &mut self,
    key: u32,
    data: &[u8],
  ) -> Result<bool, Error> {
    if self.media.paused.contains(&key) {
      return Ok(false);
    }
    let index = self.index_of(key)?;
    let mut buf = data;
    let mut packet =
      rtci::rtp::packet::Packet::unmarshal(&mut buf).map_err(|e| {
        Error::new(ErrorKind::Type, format!("Not an RTP packet: {e}"))
      })?;
    let Some(mut sender) = self.pc.rtp_sender(index.into()) else {
      return Ok(false);
    };
    let Some(ssrc) = sender.ssrcs().next() else {
      return Ok(false);
    };
    packet.header.ssrc = ssrc;
    // The packet's payload is in the track's codec: one with a payload type
    // that isn't negotiated goes out on that codec's, and none goes out
    // while it isn't negotiated (the engine would rewrite the payload type
    // to another format's, [`sending_codec`]).
    let params = sender.get_parameters();
    let negotiated = params
      .rtp_parameters
      .codecs
      .iter()
      .any(|c| c.payload_type == packet.header.payload_type);
    if !negotiated {
      match sending_codec(params, self.media.track_codecs.get(&key)) {
        Some(codec) => packet.header.payload_type = codec.payload_type,
        None => return Ok(false),
      }
    }
    match sender.write_rtp(Instant::now(), packet) {
      Ok(()) => Ok(true),
      // Not negotiated (yet): dropped, as a sender that isn't sending
      // drops what its track produces.
      Err(_) => Ok(false),
    }
  }

  /// `Deno.webrtc` `writeFrame`: one encoded frame, cut into RTP packets
  /// for the negotiated codec; `samples` advances the RTP timestamp.
  pub(crate) fn write_frame(
    &mut self,
    key: u32,
    data: &[u8],
    duration_ms: f64,
  ) -> Result<bool, Error> {
    self.write_frame_at(key, data, duration_ms, None)
  }

  /// The packetizer of the format the sender sends ([`sending_codec`]),
  /// read from the engine sender for every frame, so each applied
  /// description that changes it (an answer that leads with another codec, a
  /// renegotiation) switches it, and made anew when the format, its payload
  /// type or the SSRC changed; `None` before negotiation, while the sender
  /// is paused, or for an `EncodedMediaTrack` whose codec was not
  /// negotiated.
  fn sender_packetizer(
    &mut self,
    key: u32,
  ) -> Result<Option<&mut SenderPacketizer>, Error> {
    if self.media.paused.contains(&key) {
      return Ok(None);
    }
    let index = self.index_of(key)?;
    let now = Instant::now();
    let Some(mut sender) = self.pc.rtp_sender(index.into()) else {
      return Ok(None);
    };
    let Some(ssrc) = sender.ssrcs().next() else {
      return Ok(None);
    };
    let Some(codec) =
      sending_codec(sender.get_parameters(), self.media.track_codecs.get(&key))
    else {
      // Nothing that was cut for an earlier format is kept.
      self.media.packetizers.remove(&key);
      return Ok(None);
    };
    // What the engine adds to each packet changes with a negotiation (the
    // extensions, the mid, the rids, RTX), and the size with a path report.
    let mtu = rtp_mtu(
      self.media.path_datagram_limit,
      sender.max_packet_expansion(),
    );
    // `Some(kept)`: a new packetizer is needed; `kept` is the sequence and
    // timestamp to go on with when only the size changed.
    let renew = match self.media.packetizers.get(&key) {
      Some(p)
        if p.ssrc == ssrc
          && p.payload_type == codec.payload_type
          && p.mime_type.eq_ignore_ascii_case(&codec.rtp_codec.mime_type) =>
      {
        (p.mtu != mtu).then(|| Some((p.sequencer.clone_to(), p.timestamp)))
      }
      _ => Some(None),
    };
    if let Some(kept) = renew {
      let Some(payloader) = payloader_for(&codec.rtp_codec.mime_type) else {
        return Err(Error::new(
          ErrorKind::NotSupported,
          format!(
            "writeFrame can't packetize {}; write RTP packets instead",
            codec.rtp_codec.mime_type
          ),
        ));
      };
      let (sequencer, timestamp) = kept.unwrap_or_else(|| {
        let mut start = [0u8; 4];
        let _ = aws_lc_rs::rand::fill(&mut start);
        (
          Box::new(rtci::rtp::sequence::new_random_sequencer()),
          u32::from_le_bytes(start),
        )
      });
      let packetizer = rtci::rtp::packetizer::new_packetizer(
        now,
        mtu,
        codec.payload_type,
        ssrc,
        payloader,
        sequencer.clone_to(),
        codec.rtp_codec.clock_rate,
      );
      self.media.packetizers.insert(
        key,
        SenderPacketizer {
          ssrc,
          payload_type: codec.payload_type,
          mime_type: codec.rtp_codec.mime_type.clone(),
          clock_rate: codec.rtp_codec.clock_rate,
          timestamp,
          mtu,
          sequencer,
          packetizer: Box::new(packetizer),
        },
      );
    }
    Ok(self.media.packetizers.get_mut(&key))
  }

  /// Takes the RTP timestamp of a sender's next frame of `duration_ms`
  /// (an encoded transform's frame, written later with
  /// [`write_frame_at`](Self::write_frame_at)), with what its metadata
  /// names: `None` before negotiation.
  pub(crate) fn reserve_frame(
    &mut self,
    key: u32,
    duration_ms: f64,
  ) -> Result<Option<SenderFrameInfo>, Error> {
    let Some(p) = self.sender_packetizer(key)? else {
      return Ok(None);
    };
    let samples = ((duration_ms / 1000.0) * p.clock_rate as f64).round() as u32;
    let rtp_timestamp = p.timestamp;
    p.timestamp = p.timestamp.wrapping_add(samples);
    Ok(Some(SenderFrameInfo {
      synchronization_source: p.ssrc,
      payload_type: p.payload_type,
      mime_type: p.mime_type.clone(),
      rtp_timestamp,
    }))
  }

  /// Writes one encoded frame on a sender, cut into RTP packets: at the next
  /// timestamp, or at `rtp_timestamp` (one [`reserve_frame`](Self::reserve_frame)
  /// took). Returns whether it went out.
  pub(crate) fn write_frame_at(
    &mut self,
    key: u32,
    data: &[u8],
    duration_ms: f64,
    rtp_timestamp: Option<u32>,
  ) -> Result<bool, Error> {
    let index = self.index_of(key)?;
    let now = Instant::now();
    let Some(p) = self.sender_packetizer(key)? else {
      return Ok(false);
    };
    let samples = ((duration_ms / 1000.0) * p.clock_rate as f64).round() as u32;
    let timestamp = match rtp_timestamp {
      Some(t) => t,
      None => {
        let t = p.timestamp;
        p.timestamp = p.timestamp.wrapping_add(samples);
        t
      }
    };
    let mime_type = p.mime_type.clone();
    let ssrc = p.ssrc;
    let mut packets = p
      .packetizer
      .packetize(now, &Bytes::copy_from_slice(data), samples)
      .map_err(|e| {
        Error::new(ErrorKind::Type, format!("Could not packetize: {e}"))
      })?;
    // The timestamps are ours, so a frame's metadata can name its own.
    for packet in &mut packets {
      packet.header.timestamp = timestamp;
    }
    let Some(mut sender) = self.pc.rtp_sender(index.into()) else {
      return Ok(false);
    };
    // wpt100:hdrext — the negotiated audio level (RFC 6464) on each packet
    // and the absolute capture time on the frame's first one.
    let mut extension_id = |uri: &str| {
      sender
        .get_parameters()
        .rtp_parameters
        .header_extensions
        .iter()
        .find(|h| h.uri == uri)
        .and_then(|h| u8::try_from(h.id).ok())
    };
    let level_id = extension_id(rtci::sdp::extmap::AUDIO_LEVEL_URI);
    let capture_id =
      extension_id(crate::header_extensions::ABS_CAPTURE_TIME_URI);
    if let Some(id) = level_id
      && let Some(level) =
        crate::header_extensions::audio_level(&mime_type, data)
    {
      let payload = crate::header_extensions::audio_level_payload(level);
      for packet in &mut packets {
        let _ = packet.header.set_extension(id, payload.clone());
      }
    }
    if let Some(id) = capture_id
      && let Some(first) = packets.first_mut()
      && let Some(payload) = crate::header_extensions::abs_capture_time_payload(
        std::time::SystemTime::now(),
      )
    {
      let _ = first.header.set_extension(id, payload);
    }
    let mut sent = false;
    for packet in packets {
      sent |= sender.write_rtp(now, packet).is_ok();
    }
    // A video frame that reached the sender counts in its stream's encoder
    // stats, and in `framesSent` when it went out.
    if mime_type.to_ascii_lowercase().starts_with("video/") {
      let video = self.media.sent_video.entry(ssrc).or_default();
      video.frame(&mime_type, data, sent, now);
      let update = video.update(now);
      self.pc.update_encoder_stats(ssrc, update);
    }
    Ok(sent)
  }

  /// A fake capture device's next frame for a sender (one with an encoded
  /// transform, which writes it later), in the codec it negotiated: its
  /// bytes, duration and whether it is a key frame, and the frame's
  /// metadata. `None` before negotiation or for a codec there is no clip of.
  pub(crate) fn fake_frame(
    &mut self,
    key: u32,
    stereo: bool,
  ) -> Result<Option<FakeFrame>, Error> {
    let Some(mime) = self.sender_packetizer(key)?.map(|p| p.mime_type.clone())
    else {
      return Ok(None);
    };
    let cursor = self.media.fake.entry(key).or_default();
    let Some((data, duration_ms)) = cursor.advance(&mime, stereo) else {
      return Ok(None);
    };
    let Some(info) = self.reserve_frame(key, duration_ms)? else {
      return Ok(None);
    };
    Ok(Some(FakeFrame {
      data: crate::peer::Payload::new(data.to_vec()),
      duration_ms,
      key_frame: is_key_frame(&mime, data),
      width_height: frame_size(&mime, data),
      info,
      audio_level: crate::header_extensions::audio_level(&mime, data),
    }))
  }

  /// A fake capture device's next frame (`fake_media.rs`) on a sender, in
  /// the codec it negotiated; `stereo` takes Opus' two-channel clip.
  /// Returns whether it went out (false before negotiation, or for a codec
  /// there is no clip of).
  pub(crate) fn write_fake(
    &mut self,
    key: u32,
    stereo: bool,
  ) -> Result<bool, Error> {
    if self.media.paused.contains(&key) {
      return Ok(false);
    }
    let index = self.index_of(key)?;
    let Some(mut sender) = self.pc.rtp_sender(index.into()) else {
      return Ok(false);
    };
    // The device encodes in the format the sender sends.
    let Some(mime) =
      sending_codec(sender.get_parameters(), self.media.track_codecs.get(&key))
        .map(|c| c.rtp_codec.mime_type)
    else {
      return Ok(false);
    };
    let cursor = self.media.fake.entry(key).or_default();
    let Some((frame, duration)) = cursor.advance(&mime, stereo) else {
      return Ok(false);
    };
    self.write_frame(key, frame, duration)
  }

  /// `RTCDTMFSender`: one RFC 4733 event (0-9 → 0-9, `*` 10, `#` 11, A-D
  /// 12-15) lasting `duration_ms`: a start packet (marker set) and the end
  /// packet three times, on the negotiated telephone-event payload type at
  /// the clock of the sender's audio codec. Returns whether it went out.
  pub(crate) fn send_dtmf(
    &mut self,
    key: u32,
    event: u8,
    duration_ms: u32,
  ) -> Result<bool, Error> {
    let index = self.index_of(key)?;
    let Some(mut sender) = self.pc.rtp_sender(index.into()) else {
      return Ok(false);
    };
    let Some(ssrc) = sender.ssrcs().next() else {
      return Ok(false);
    };
    let params = sender.get_parameters().clone();
    let codecs = &params.rtp_parameters.codecs;
    let primary_rate = codecs
      .iter()
      .find(|c| {
        let m = c.rtp_codec.mime_type.to_ascii_lowercase();
        !m.ends_with("/telephone-event")
          && !m.ends_with("/cn")
          && !m.ends_with("/red")
          && !m.ends_with("/rtx")
      })
      .map(|c| c.rtp_codec.clock_rate);
    let events: Vec<&RTCRtpCodecParameters> = codecs
      .iter()
      .filter(|c| {
        c.rtp_codec
          .mime_type
          .eq_ignore_ascii_case("audio/telephone-event")
      })
      .collect();
    let Some(codec) = events
      .iter()
      .find(|c| Some(c.rtp_codec.clock_rate) == primary_rate)
      .or_else(|| events.first())
    else {
      return Ok(false);
    };
    let (payload_type, clock_rate) =
      (codec.payload_type, codec.rtp_codec.clock_rate);
    let entry = self.media.dtmf.entry(key).or_insert_with(|| {
      let mut seed = [0u8; 6];
      let _ = aws_lc_rs::rand::fill(&mut seed);
      (
        u16::from_le_bytes([seed[0], seed[1]]),
        u32::from_le_bytes([seed[2], seed[3], seed[4], seed[5]]),
      )
    });
    let (mut seq, timestamp) = *entry;
    let samples = (u64::from(duration_ms) * u64::from(clock_rate) / 1000)
      .min(u64::from(u16::MAX)) as u16;
    let now = Instant::now();
    let packet = |seq: u16, end: bool, marker: bool, duration: u16| {
      let flags = if end { 0x80 } else { 0x00 } | 10; // volume -10 dBm0
      let d = duration.to_be_bytes();
      rtci::rtp::packet::Packet {
        header: rtci::rtp::Header {
          version: 2,
          marker,
          payload_type,
          sequence_number: seq,
          timestamp,
          ssrc,
          ..Default::default()
        },
        payload: Bytes::copy_from_slice(&[event, flags, d[0], d[1]]),
      }
    };
    let mut sent = false;
    let Some(mut sender) = self.pc.rtp_sender(index.into()) else {
      return Ok(false);
    };
    sent |= sender.write_rtp(now, packet(seq, false, true, 0)).is_ok();
    for _ in 0..3 {
      seq = seq.wrapping_add(1);
      sent |= sender
        .write_rtp(now, packet(seq, true, false, samples))
        .is_ok();
    }
    *self.media.dtmf.get_mut(&key).unwrap() = (
      seq.wrapping_add(1),
      timestamp.wrapping_add(u32::from(samples)),
    );
    Ok(sent)
  }

  /// `Deno.webrtc` `requestKeyFrame`: a PLI to the remote sender.
  pub(crate) fn request_key_frame(&mut self, key: u32) -> Result<bool, Error> {
    let index = self.index_of(key)?;
    let Some(mut receiver) = self.pc.rtp_receiver(index.into()) else {
      return Ok(false);
    };
    let ssrcs: Vec<u32> = receiver.track().ssrcs().collect();
    let mut sent = false;
    for media_ssrc in ssrcs {
      sent |= write_pli(&mut receiver, media_ssrc, Instant::now());
    }
    Ok(sent)
  }

  /// A receiver's frame assembler lost a frame of the stream `media_ssrc`
  /// and has seen no key frame since: a PLI to the remote sender (RFC 4585
  /// section 6.3.1), at most once per [`KEY_FRAME_REQUEST_INTERVAL`] per
  /// stream. Returns whether one was sent.
  pub(crate) fn request_key_frame_after_loss(
    &mut self,
    key: u32,
    media_ssrc: u32,
    now: Instant,
  ) -> bool {
    let requests = &mut self.media.loss_key_frame_requests;
    if requests.get(&media_ssrc).is_some_and(|last| {
      now.saturating_duration_since(*last) < KEY_FRAME_REQUEST_INTERVAL
    }) {
      return false;
    }
    // Bounded: only streams that asked within the interval are kept.
    requests.retain(|_, last| {
      now.saturating_duration_since(*last) < KEY_FRAME_REQUEST_INTERVAL
    });
    let Ok(index) = self.index_of(key) else {
      return false;
    };
    let Some(mut receiver) = self.pc.rtp_receiver(index.into()) else {
      return false;
    };
    let sent = write_pli(&mut receiver, media_ssrc, now);
    if sent {
      self.media.loss_key_frame_requests.insert(media_ssrc, now);
      self.wrote_rtcp = true;
    }
    sent
  }

  pub(crate) fn key_for_track(&mut self, track_id: &str) -> Option<u32> {
    if let Some(key) = self.media.track_keys.get(track_id) {
      return Some(*key);
    }
    for (index, key) in self.media.keys.clone().into_iter().enumerate() {
      if let Some(r) = self.pc.rtp_receiver(index.into())
        && r.track().track_id() == track_id
      {
        self.media.track_keys.insert(track_id.to_string(), key);
        return Some(key);
      }
    }
    None
  }

  pub(crate) fn key_for_receiver_index(&self, index: usize) -> Option<u32> {
    self.media.keys.get(index).copied()
  }
}

/// A PLI for the stream `media_ssrc`, written by its receiver. The sender
/// SSRC is 0, as the engine's own requests have it.
fn write_pli(
  receiver: &mut rtci::rtp_transceiver::rtp_receiver::RTCRtpReceiver<'_>,
  media_ssrc: u32,
  now: Instant,
) -> bool {
  receiver
    .write_rtcp(
      now,
      vec![Box::new(PictureLossIndication {
        sender_ssrc: 0,
        media_ssrc,
      })],
    )
    .is_ok()
}

/// Whether an RTCP compound asks for a key frame (PLI or FIR).
pub fn wants_key_frame(packets: &[Box<dyn rtci::rtcp::Packet>]) -> bool {
  packets.iter().any(|p| {
    p.as_any().downcast_ref::<PictureLossIndication>().is_some()
      || p.as_any().downcast_ref::<FullIntraRequest>().is_some()
  })
}

fn section_codec_json(payload_type: u8, c: &CodecArg) -> serde_json::Value {
  let mut v = serde_json::to_value(c).unwrap_or(serde_json::Value::Null);
  if let Some(obj) = v.as_object_mut() {
    obj.insert("payloadType".to_string(), payload_type.into());
  }
  v
}

/// RFC 3551's static audio payload types, which may come without an
/// `a=rtpmap` line.
fn static_audio_codec(payload_type: u8) -> Option<(&'static str, u32)> {
  match payload_type {
    0 => Some(("PCMU", 8000)),
    3 => Some(("GSM", 8000)),
    4 => Some(("G723", 8000)),
    8 => Some(("PCMA", 8000)),
    9 => Some(("G722", 8000)),
    13 => Some(("CN", 8000)),
    18 => Some(("G729", 8000)),
    _ => None,
  }
}

/// The media kind and codecs (payload type, `a=rtpmap`, `a=fmtp`) of the
/// m-section of `sdp` whose mid is `mid`, in the m= line's format order. A
/// dynamic payload type with no `a=rtpmap` is left out.
pub fn section_codecs(
  sdp: &str,
  mid: &str,
) -> Option<(String, Vec<(u8, CodecArg)>)> {
  struct Section {
    kind: String,
    formats: Vec<String>,
    rtpmap: HashMap<String, String>,
    fmtp: HashMap<String, String>,
    mid: Option<String>,
  }
  let mut sections: Vec<Section> = Vec::new();
  for line in sdp.lines() {
    let line = line.trim_end();
    if let Some(m) = line.strip_prefix("m=") {
      let mut fields = m.split_ascii_whitespace();
      let kind = fields.next().unwrap_or_default().to_string();
      let formats = fields.skip(2).map(str::to_string).collect();
      sections.push(Section {
        kind,
        formats,
        rtpmap: HashMap::new(),
        fmtp: HashMap::new(),
        mid: None,
      });
      continue;
    }
    let Some(section) = sections.last_mut() else {
      continue;
    };
    if let Some(value) = line.strip_prefix("a=mid:") {
      section.mid = Some(value.trim().to_string());
    } else if let Some(value) = line.strip_prefix("a=rtpmap:") {
      if let Some((pt, rest)) = value.split_once(' ') {
        section
          .rtpmap
          .entry(pt.to_string())
          .or_insert_with(|| rest.trim().to_string());
      }
    } else if let Some(value) = line.strip_prefix("a=fmtp:")
      && let Some((pt, params)) = value.split_once(' ')
    {
      section
        .fmtp
        .entry(pt.to_string())
        .or_insert_with(|| params.trim().to_string());
    }
  }
  let section = sections
    .into_iter()
    .find(|s| s.mid.as_deref() == Some(mid))?;
  let audio = section.kind == "audio";
  let mut out: Vec<(u8, CodecArg)> = Vec::new();
  for format in &section.formats {
    let Ok(pt) = format.parse::<u8>() else {
      continue;
    };
    if out.iter().any(|(p, _)| *p == pt) {
      continue;
    }
    let (name, clock_rate, channels) = match section.rtpmap.get(format) {
      Some(map) => {
        let mut parts = map.split('/');
        let name = parts.next().unwrap_or_default().to_string();
        let clock_rate = parts.next().and_then(|c| c.parse().ok()).unwrap_or(0);
        let channels = parts.next().and_then(|c| c.parse().ok());
        (name, clock_rate, channels)
      }
      None => match static_audio_codec(pt).filter(|_| audio) {
        Some((name, clock_rate)) => (name.to_string(), clock_rate, None),
        None => continue,
      },
    };
    out.push((
      pt,
      CodecArg {
        mime_type: format!("{}/{name}", section.kind),
        clock_rate,
        // An audio codec without a channel count is mono (RFC 4566).
        channels: if audio { channels.or(Some(1)) } else { None },
        sdp_fmtp_line: section.fmtp.get(format).cloned(),
      },
    ));
  }
  Some((section.kind, out))
}

/// The send codecs this stack implements for `kind` (computed once).
fn supported_codecs(kind: &str) -> &'static [CodecArg] {
  static AUDIO: std::sync::OnceLock<Vec<CodecArg>> = std::sync::OnceLock::new();
  static VIDEO: std::sync::OnceLock<Vec<CodecArg>> = std::sync::OnceLock::new();
  let cell = match kind {
    "audio" => &AUDIO,
    "video" => &VIDEO,
    _ => return &[],
  };
  cell.get_or_init(|| {
    capabilities(kind, true)
      .ok()
      .flatten()
      .map(|c| c.codecs)
      .unwrap_or_default()
  })
}

fn is_rtx(c: &CodecArg) -> bool {
  c.mime_type.to_ascii_lowercase().ends_with("/rtx")
}

/// The `apt` (RFC 4588) of a retransmission format's fmtp line.
fn rtx_apt(c: &CodecArg) -> Option<u8> {
  c.sdp_fmtp_line.as_deref()?.split(';').find_map(|p| {
    p.trim()
      .strip_prefix("apt=")
      .and_then(|v| v.trim().parse().ok())
  })
}

/// Of a remote m-section's codecs, those this stack can send (same name,
/// clock rate and, for audio, channels as one it implements); a
/// retransmission format only with the primary it repairs.
fn sendable_codecs(
  kind: &str,
  codecs: Vec<(u8, CodecArg)>,
) -> Vec<(u8, CodecArg)> {
  let supported = supported_codecs(kind);
  let known = |c: &CodecArg| {
    supported.iter().any(|s| {
      s.mime_type.eq_ignore_ascii_case(&c.mime_type)
        && s.clock_rate == c.clock_rate
        && (kind != "audio" || s.channels == c.channels)
    })
  };
  let primaries: Vec<u8> = codecs
    .iter()
    .filter(|(_, c)| !is_rtx(c) && known(c))
    .map(|(pt, _)| *pt)
    .collect();
  codecs
    .into_iter()
    .filter(|(pt, c)| {
      if is_rtx(c) {
        known(c) && rtx_apt(c).is_some_and(|apt| primaries.contains(&apt))
      } else {
        primaries.contains(pt)
      }
    })
    .collect()
}

/// The engine sender's codecs (`engine`, in the remote description's
/// payload types) as `getParameters()` lists them: each written as the
/// remote m-section (`section`) has it, in that section's order, and any the
/// section does not name after them as the engine holds them.
fn negotiated_send_codecs(
  engine: &[RTCRtpCodecParameters],
  section: &[(u8, CodecArg)],
) -> Vec<serde_json::Value> {
  let in_engine = |pt: u8, c: &CodecArg| {
    engine.iter().any(|e| {
      e.payload_type == pt
        && e.rtp_codec.mime_type.eq_ignore_ascii_case(&c.mime_type)
    })
  };
  let mut out: Vec<serde_json::Value> = section
    .iter()
    .filter(|(pt, c)| in_engine(*pt, c))
    .map(|(pt, c)| section_codec_json(*pt, c))
    .collect();
  for e in engine {
    let listed = section.iter().any(|(pt, c)| {
      *pt == e.payload_type
        && e.rtp_codec.mime_type.eq_ignore_ascii_case(&c.mime_type)
    });
    if !listed {
      out.push(codec_params_json(e));
    }
  }
  out
}

fn codec_params_json(c: &RTCRtpCodecParameters) -> serde_json::Value {
  let mut v = serde_json::to_value(CodecArg::from_codec(&c.rtp_codec))
    .unwrap_or(serde_json::Value::Null);
  if let Some(obj) = v.as_object_mut() {
    obj.insert("payloadType".to_string(), c.payload_type.into());
  }
  v
}

#[cfg(test)]
mod rtp_mtu_tests {
  use super::*;

  // RFC 7941 section 4.2.2: payloads are cut so that the RTP packet, with
  // the extensions the engine adds, stays within the MTU; and within what
  // a reported path carries once SRTP has its tag.
  #[test]
  fn packets_are_cut_for_the_extensions_and_the_path() {
    assert_eq!(rtp_mtu(None, 0), RTP_MTU);
    assert_eq!(rtp_mtu(None, 18), RTP_MTU - 18);
    // A path larger than the default changes nothing.
    assert_eq!(rtp_mtu(Some(1452), 18), RTP_MTU - 18);
    // IPv6's minimum (1280 less IPv6 and UDP: 1232-byte datagrams) still
    // carries the default once SRTP has its tag (1216 >= 1200).
    assert_eq!(rtp_mtu(Some(1232), 18), RTP_MTU - 18);
    // A path just under the default plus the tag cuts below it.
    assert_eq!(rtp_mtu(Some(1200), 18), 1200 - SRTP_MAX_OVERHEAD - 18);
    // IPv4's minimum (576 less IPv4 and UDP).
    assert_eq!(rtp_mtu(Some(548), 18), 548 - SRTP_MAX_OVERHEAD - 18);
    // Never below the floor.
    assert_eq!(rtp_mtu(Some(200), 18), RTP_MTU_FLOOR);
  }

  // A path's estimate only ever goes down (RFC 8201 section 4).
  #[test]
  fn a_path_report_never_raises_the_limit() {
    let mut media = MediaState::default();
    assert_eq!(media.path_datagram_limit, None);
    media.limit_datagrams(1232);
    assert_eq!(media.path_datagram_limit, Some(1232));
    media.limit_datagrams(1400);
    assert_eq!(media.path_datagram_limit, Some(1232));
    media.limit_datagrams(548);
    assert_eq!(media.path_datagram_limit, Some(548));
  }
}

#[cfg(test)]
mod factor_pacer_tests {
  use std::sync::Arc;
  use std::time::Duration;
  use std::time::Instant;

  use bytes::Bytes;
  use rtci::interceptor::Attribute;
  use rtci::interceptor::AttributedPacket;
  use rtci::interceptor::GCC_INITIAL_BITRATE;
  use rtci::interceptor::Interceptor;
  use rtci::interceptor::PacerBuilder;
  use rtci::interceptor::Packet;
  use rtci::interceptor::RTCPFeedback;
  use rtci::interceptor::RTPHeaderExtension;
  use rtci::interceptor::StreamInfo;
  use rtci::interceptor::TaggedPacket;
  use rtci::rtcp::transport_feedbacks::cc_feedback_report::CcFeedbackReport;
  use rtci::rtcp::transport_feedbacks::transport_layer_cc::TransportLayerCc;
  use rtci::sansio::Protocol;
  use rtci::shared::TransportContext;
  use rtci::shared::marshal::MarshalSize;

  use super::FEEDBACK_TIMEOUT;
  use super::FactorPacer;
  use super::PACING_FACTOR;
  use super::PacerRefusals;
  use super::QUEUE_TIME_LIMIT;

  const SSRC: u32 = 1234;

  fn rtcp(
    now: Instant,
    packet: Box<dyn rtci::rtcp::Packet>,
    estimate: Option<f64>,
  ) -> TaggedPacket {
    let mut message = AttributedPacket::new(Packet::Rtcp(vec![packet]));
    if let Some(bits_per_second) = estimate {
      message.add(Attribute::TargetBitrateChanged { bits_per_second });
    }
    TaggedPacket {
      now,
      transport: TransportContext::default(),
      message,
    }
  }

  /// A receiver report: not congestion feedback, but the estimate the
  /// controller attaches makes it one.
  fn feedback(estimate: Option<f64>) -> TaggedPacket {
    rtcp(
      Instant::now(),
      Box::new(rtci::rtcp::receiver_report::ReceiverReport::default()),
      estimate,
    )
  }

  fn twcc(now: Instant, estimate: Option<f64>) -> TaggedPacket {
    rtcp(now, Box::new(TransportLayerCc::default()), estimate)
  }

  fn ccfb(now: Instant) -> TaggedPacket {
    rtcp(now, Box::new(CcFeedbackReport::default()), None)
  }

  fn rtp(now: Instant, ssrc: u32, seq: u16) -> TaggedPacket {
    TaggedPacket {
      now,
      transport: TransportContext::default(),
      message: AttributedPacket::new(Packet::Rtp(rtci::rtp::packet::Packet {
        header: rtci::rtp::Header {
          version: 2,
          sequence_number: seq,
          ssrc,
          ..Default::default()
        },
        payload: Bytes::from(vec![0u8; 1000]),
      })),
    }
  }

  /// A stream that negotiated the transport-wide sequence number (TWCC).
  fn twcc_stream(ssrc: u32) -> StreamInfo {
    StreamInfo {
      ssrc,
      rtp_header_extensions: vec![RTPHeaderExtension {
        uri: rtci::sdp::extmap::TRANSPORT_CC_URI.to_string(),
        id: 5,
      }],
      ..Default::default()
    }
  }

  /// A stream that negotiated RFC 8888 feedback (`ack ccfb`) only.
  fn ccfb_stream(ssrc: u32) -> StreamInfo {
    StreamInfo {
      ssrc,
      rtcp_feedback: vec![RTCPFeedback {
        typ: "ack".to_string(),
        parameter: "ccfb".to_string(),
      }],
      ..Default::default()
    }
  }

  /// What leaves a paced stream at once: the engine pacer starts with one
  /// burst in its bucket and refills it to one burst (draft-ietf-rmcat-gcc-02
  /// section 4: the rate times 5 ms, at least one full-sized packet), which
  /// at the initial rate is one of these packets.
  const BURST: usize = 1;

  fn pacer() -> FactorPacer {
    FactorPacer::new(Arc::default())
  }

  /// Writes `count` RTP packets at `now` and returns how many came straight
  /// out (unpaced), without a timeout releasing them.
  fn write(p: &mut FactorPacer, now: Instant, ssrc: u32, count: u16) -> usize {
    for seq in 0..count {
      p.handle_write(rtp(now, ssrc, seq)).unwrap();
    }
    drain(p)
  }

  fn drain(p: &mut FactorPacer) -> usize {
    std::iter::from_fn(|| p.poll_write()).count()
  }

  /// The estimates the packets read through it carry to the stats.
  fn reported(p: &mut FactorPacer) -> Vec<f64> {
    std::iter::from_fn(|| p.poll_read())
      .filter_map(|out| FactorPacer::estimate(&out))
      .collect()
  }

  // A congestion controller estimate paces at PACING_FACTOR times it, and the
  // feedback that carried it goes on with the estimate itself (what the stats
  // report).
  #[test]
  fn paces_at_the_factor_times_the_estimate() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    assert_eq!(p.target_bitrate(), GCC_INITIAL_BITRATE * PACING_FACTOR);
    p.handle_read(feedback(Some(400_000.0))).unwrap();
    assert_eq!(p.target_bitrate(), 400_000.0 * PACING_FACTOR);
    assert_eq!(reported(&mut p), vec![400_000.0]);
  }

  // Feedback without an estimate leaves the rate alone.
  #[test]
  fn keeps_the_rate_without_an_estimate() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    p.handle_read(feedback(Some(500_000.0))).unwrap();
    p.handle_read(feedback(None)).unwrap();
    assert_eq!(p.target_bitrate(), 500_000.0 * PACING_FACTOR);
  }

  // A stream that negotiated neither TWCC nor `ack ccfb` is not paced: the
  // remote never reports on it, so the estimate would never leave its
  // initial value (PR-02: every such connection was held near 750 kb/s).
  // Its packets come straight out, however fast they are written.
  #[test]
  fn passes_a_stream_without_feedback_through() {
    let mut p = pacer();
    p.bind_local_stream(&StreamInfo {
      ssrc: SSRC,
      ..Default::default()
    });
    let now = Instant::now();
    assert_eq!(write(&mut p, now, SSRC, 500), 500);
    // Seconds later, still nothing held.
    assert_eq!(write(&mut p, now + Duration::from_secs(10), SSRC, 500), 500);
    assert_eq!(p.refusals.get(), (0, 0));
    // No estimate reaches the stats.
    assert!(reported(&mut p).is_empty());
  }

  // An unbound SSRC (no stream negotiated anything) passes through too.
  #[test]
  fn passes_an_unbound_stream_through() {
    let mut p = pacer();
    assert_eq!(write(&mut p, Instant::now(), SSRC, 100), 100);
  }

  // A stream that negotiated TWCC is paced from its first packet, at the
  // initial rate: the packets wait for the pacer's budget.
  #[test]
  fn paces_a_stream_that_negotiated_twcc() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    let now = Instant::now();
    assert!(write(&mut p, now, SSRC, 100) <= BURST);
    p.handle_timeout(now).unwrap();
    let released = drain(&mut p);
    assert!(released < 100, "released {released}");
  }

  // RFC 8888 feedback (`ack ccfb`) counts as congestion feedback too.
  #[test]
  fn paces_a_stream_that_negotiated_ccfb() {
    let mut p = pacer();
    p.bind_local_stream(&ccfb_stream(SSRC));
    let now = Instant::now();
    assert!(write(&mut p, now, SSRC, 10) <= BURST);
    // An RFC 8888 report keeps it paced past the feedback timeout.
    let later = now + FEEDBACK_TIMEOUT - Duration::from_millis(100);
    p.handle_read(ccfb(later)).unwrap();
    assert!(
      write(&mut p, later + Duration::from_millis(500), SSRC, 10) <= BURST
    );
  }

  // Only the negotiated stream is paced: another stream of the same
  // connection that negotiated nothing passes beside it.
  #[test]
  fn paces_only_the_negotiated_streams() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    p.bind_local_stream(&StreamInfo {
      ssrc: SSRC + 1,
      ..Default::default()
    });
    let now = Instant::now();
    assert!(write(&mut p, now, SSRC, 10) <= BURST);
    assert_eq!(write(&mut p, now, SSRC + 1, 10), 10);
  }

  // While reports keep coming, it keeps pacing, however long the call.
  #[test]
  fn keeps_pacing_while_feedback_arrives() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    let start = Instant::now();
    for i in 0..20 {
      let now = start + Duration::from_millis(250) * i;
      p.handle_read(twcc(now, None)).unwrap();
      assert!(write(&mut p, now, SSRC, 5) <= BURST, "at {i}");
      p.handle_timeout(now).unwrap();
      drain(&mut p);
    }
  }

  // Negotiated, but no report within FEEDBACK_TIMEOUT of the first packet:
  // what was held goes at the next wake-up and later packets pass straight
  // through; the next report resumes pacing at its estimate.
  #[test]
  fn falls_back_without_feedback_then_resumes() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    let start = Instant::now();
    let first = write(&mut p, start, SSRC, 100);
    assert!(first <= BURST);
    let before = start + FEEDBACK_TIMEOUT - Duration::from_millis(1);
    let second = write(&mut p, before, SSRC, 1);
    assert!(second <= BURST, "still paced");
    let after = start + FEEDBACK_TIMEOUT;
    p.handle_timeout(after).unwrap();
    assert_eq!(
      drain(&mut p),
      101 - first - second,
      "the held packets all go"
    );
    assert_eq!(write(&mut p, after, SSRC, 200), 200);
    // A report: paced again, at its estimate.
    let resumed = after + Duration::from_secs(5);
    p.handle_read(twcc(resumed, Some(400_000.0))).unwrap();
    assert_eq!(p.target_bitrate(), 400_000.0 * PACING_FACTOR);
    let third = write(&mut p, resumed, SSRC, 10);
    assert!(third <= BURST);
    // And reports that stop after that fall back the same way.
    let stopped = resumed + FEEDBACK_TIMEOUT;
    p.handle_timeout(stopped).unwrap();
    assert_eq!(drain(&mut p), 10 - third);
    assert_eq!(write(&mut p, stopped, SSRC, 10), 10);
  }

  // rtci 0.4's pacer interleaves the streams waiting in it (by priority,
  // RFC 8835 section 4.1) instead of releasing them in the order written:
  // what this pacer counts as held stays the bits of the packets still in
  // it, whichever stream's leave first, so the backlog rate is right.
  #[test]
  fn counts_what_it_holds_when_streams_interleave() {
    const OTHER: u32 = 5678;
    fn sized(now: Instant, ssrc: u32, seq: u16, len: usize) -> TaggedPacket {
      let mut packet = rtp(now, ssrc, seq);
      if let Packet::Rtp(rtp) = &mut packet.message.packet {
        rtp.payload = Bytes::from(vec![0u8; len]);
      }
      packet
    }
    let bits = |ssrc: u32, seq: u16| {
      let len = if ssrc == SSRC { 1000 } else { 300 };
      match &sized(Instant::now(), ssrc, seq, len).message.packet {
        Packet::Rtp(rtp) => (rtp.marshal_size() * 8) as f64,
        _ => unreachable!("an RTP packet"),
      }
    };
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    p.bind_local_stream(&twcc_stream(OTHER));
    let start = Instant::now();
    let mut waiting: Vec<(u32, u16)> = Vec::new();
    for seq in 0..40 {
      p.handle_write(sized(start, SSRC, seq, 1000)).unwrap();
      waiting.push((SSRC, seq));
    }
    for seq in 0..40 {
      p.handle_write(sized(start, OTHER, seq, 300)).unwrap();
      waiting.push((OTHER, seq));
    }
    let mut now = start;
    let mut order = Vec::new();
    while !waiting.is_empty() {
      p.handle_read(twcc(now, None)).unwrap();
      p.handle_timeout(now).unwrap();
      while let Some(out) = p.poll_write() {
        if let Packet::Rtp(rtp) = out.message.packet {
          let id = (rtp.header.ssrc, rtp.header.sequence_number);
          waiting.retain(|w| *w != id);
          order.push(rtp.header.ssrc);
        }
      }
      let expected: f64 = waiting.iter().map(|&(s, q)| bits(s, q)).sum();
      assert!(
        (p.held_bits() - expected).abs() < 1.0,
        "holds {} bits, {} wait",
        p.held_bits(),
        expected
      );
      if waiting.is_empty() {
        break;
      }
      now = p.poll_timeout().expect("a release is due").max(now);
    }
    assert_eq!(p.held_bits(), 0.0);
    // Each stream left in its own order; the other stream started before
    // the first had gone (they shared the path).
    let first_other = order.iter().position(|s| *s == OTHER).unwrap();
    let last_first = order.iter().rposition(|s| *s == SSRC).unwrap();
    assert!(first_other < last_first, "interleaved: {order:?}");
  }

  // A packet written while the held ones haven't gone yet waits behind them,
  // so the stream keeps its order.
  #[test]
  fn keeps_a_streams_order_when_falling_back() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    let start = Instant::now();
    let first = write(&mut p, start, SSRC, 50);
    assert!(first <= BURST);
    let after = start + FEEDBACK_TIMEOUT;
    p.handle_write(rtp(after, SSRC, 50)).unwrap();
    p.handle_timeout(after).unwrap();
    let order: Vec<u16> = std::iter::from_fn(|| p.poll_write())
      .map(|out| match out.message.packet {
        Packet::Rtp(rtp) => rtp.header.sequence_number,
        _ => unreachable!("only RTP was written"),
      })
      .collect();
    assert_eq!(order, (first as u16..=50).collect::<Vec<_>>());
  }

  // The stats hold an estimate only while it paces at one: the initial
  // estimate with the first report, the controller's after that, none once
  // reports stop (`availableOutgoingBitrate` absent, `targetBitrate` 0), and
  // the last one again with the next report.
  #[test]
  fn reports_an_estimate_only_while_pacing() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    let start = Instant::now();
    write(&mut p, start, SSRC, 1);
    assert!(reported(&mut p).is_empty(), "nothing before a report");
    let first = start + Duration::from_millis(100);
    p.handle_read(twcc(first, None)).unwrap();
    assert_eq!(reported(&mut p), vec![GCC_INITIAL_BITRATE]);
    p.handle_read(twcc(first, Some(500_000.0))).unwrap();
    assert_eq!(reported(&mut p), vec![500_000.0]);
    p.handle_read(twcc(first, None)).unwrap();
    assert!(reported(&mut p).is_empty(), "unchanged: nothing new");
    p.handle_timeout(first + FEEDBACK_TIMEOUT).unwrap();
    let carrier = p.poll_read().expect("the estimate is withdrawn");
    assert!(
      matches!(&carrier.message.packet, Packet::Rtcp(packets) if packets.is_empty())
    );
    assert_eq!(FactorPacer::estimate(&carrier), Some(0.0));
    assert!(p.poll_read().is_none());
    let back = first + FEEDBACK_TIMEOUT * 2;
    p.handle_read(twcc(back, None)).unwrap();
    assert_eq!(reported(&mut p), vec![500_000.0]);
  }

  // A packet the full queue refuses is counted, with its bytes, for the
  // candidate pair's `packetsDiscardedOnSend` / `bytesDiscardedOnSend`.
  #[test]
  fn counts_what_a_full_queue_refuses() {
    let refusals = Arc::new(PacerRefusals::default());
    let mut p = FactorPacer::with_pacer(
      PacerBuilder::new()
        .with_target_bitrate(GCC_INITIAL_BITRATE * PACING_FACTOR)
        .with_queue_time_limit(None)
        .with_queue_limit(4)
        .build(),
      refusals.clone(),
    );
    p.bind_local_stream(&twcc_stream(SSRC));
    let now = Instant::now();
    let released = write(&mut p, now, SSRC, 10);
    assert!(released <= BURST);
    let size = match &rtp(now, SSRC, 0).message.packet {
      Packet::Rtp(rtp) => rtp.marshal_size() as u64,
      _ => unreachable!("an RTP packet"),
    };
    // Ten written: what left at once, four queued, the rest refused.
    let refused = (10 - released - 4) as u64;
    assert_eq!(refusals.get(), (refused, refused * size));
  }

  // A backlog drains within QUEUE_TIME_LIMIT whatever the estimate: 1,000
  // packets (about 8 Mb) at the initial 750 kb/s would take over 10 s; the
  // pacer releases them faster instead, while feedback keeps it pacing.
  #[test]
  fn drains_a_backlog_within_the_queue_time_limit() {
    let mut p = pacer();
    p.bind_local_stream(&twcc_stream(SSRC));
    let start = Instant::now();
    let mut released = write(&mut p, start, SSRC, 1000);
    assert!(released <= BURST);
    let mut now = start;
    while released < 1000 {
      p.handle_read(twcc(now, None)).unwrap();
      p.handle_timeout(now).unwrap();
      released += drain(&mut p);
      if released == 1000 {
        break;
      }
      now = p.poll_timeout().expect("a release is due").max(now);
      assert!(
        now <= start + QUEUE_TIME_LIMIT + Duration::from_millis(100),
        "{released} of 1000 released after {:?}",
        now - start
      );
    }
    // Drained, it paces at the estimate again.
    assert_eq!(p.target_bitrate(), GCC_INITIAL_BITRATE * PACING_FACTOR);
  }

  // Audio is never paced, even where it negotiated feedback: it doesn't wait
  // behind video in the queue.
  #[test]
  fn does_not_pace_audio() {
    let mut p = pacer();
    p.bind_local_stream(&StreamInfo {
      mime_type: "audio/opus".to_string(),
      ..twcc_stream(SSRC)
    });
    p.bind_local_stream(&twcc_stream(SSRC + 1));
    let now = Instant::now();
    assert!(write(&mut p, now, SSRC + 1, 100) <= BURST);
    assert_eq!(write(&mut p, now, SSRC, 50), 50);
  }
}