rtci 0.1.0

A sans-I/O WebRTC implementation: peer connections with ICE, DTLS-SRTP, SCTP data channels, RTP/RTCP and JSEP negotiation
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
//! Advanced configuration engine for WebRTC peer connections.
//!
//! The `SettingEngine` provides low-level control over WebRTC transport behavior,
//! timeouts, security settings, and network configuration. Unlike the standard
//! `RTCConfiguration` which focuses on standards-compliant WebRTC settings, the
//! `SettingEngine` allows for advanced customization and optimization for specific
//! deployment scenarios.
//!
//! # Key Configuration Areas
//!
//! - **ICE Timeouts**: Configure connection health monitoring and keepalive intervals
//! - **NAT Traversal**: Set up 1:1 NAT mappings for cloud deployments (e.g., AWS EC2)
//! - **DTLS Security**: Control certificate verification and DTLS role behavior
//! - **Replay Protection**: Configure anti-replay windows for DTLS, SRTP, and SRTCP
//! - **Network Types**: Restrict candidate gathering to specific network types
//!
//! # Examples
//!
//! ## Configuring ICE timeouts for unstable networks
//!
//! ```
//! use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
//! use std::time::Duration;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Increase timeouts for mobile or unstable networks
//! let setting_engine = SettingEngineBuilder::new()
//!     .with_ice_timeouts(
//!         Some(Duration::from_secs(10)),  // disconnected_timeout (default: 5s)
//!         Some(Duration::from_secs(30)),  // failed_timeout (default: 25s)
//!         Some(Duration::from_secs(3)),   // keep_alive_interval (default: 2s)
//!     )
//!     .build();
//!
//! // Use with RTCConfiguration
//! // let mut config = RTCConfiguration::default();
//! // config.setting_engine = Some(setting_engine);
//! # Ok(())
//! # }
//! ```
//!
//! ## Setting up 1:1 NAT for cloud deployments
//!
//! ```
//! use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
//! use rtci::peer_connection::transport::RTCIceCandidateType;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Configure for AWS EC2 instance with Elastic IP
//! // Private IP: 10.0.1.5, Public IP: 54.123.45.67
//! let setting_engine = SettingEngineBuilder::new()
//!     .with_nat_1to1_ips(
//!         vec!["54.123.45.67".to_string()],
//!         RTCIceCandidateType::Host, // Use public IP for host candidates
//!     )
//!     .build();
//!
//! // This tells ICE to advertise the public IP instead of the private one
//! # Ok(())
//! # }
//! ```
//!
//! ## Configuring replay protection for security-critical applications
//!
//! ```
//! use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Increase replay protection window sizes
//! let setting_engine = SettingEngineBuilder::new()
//!     .with_dtls_replay_protection_window(128)  // DTLS anti-replay
//!     .with_srtp_replay_protection_window(256)  // SRTP anti-replay
//!     .with_srtcp_replay_protection_window(128) // SRTCP anti-replay
//!     .build();
//!
//! // Larger windows protect against more packet reordering but use more memory
//! # Ok(())
//! # }
//! ```
//!
//! ## Restricting network types for controlled environments
//!
//! ```
//! use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
//! use ice::network_type::NetworkType;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Only gather IPv4 UDP candidates (no IPv6, no TCP)
//! let setting_engine = SettingEngineBuilder::new()
//!     .with_network_types(vec![NetworkType::Udp4])
//!     .build();
//!
//! // This reduces candidate gathering time and SDP size
//! # Ok(())
//! # }
//! ```
//!
//! # See Also
//!
//! - [`RTCConfiguration`](crate::peer_connection::configuration::RTCConfiguration) - Standard WebRTC configuration
//! - [`MediaEngine`](crate::peer_connection::configuration::media_engine::MediaEngine) - Codec registration
//! - [RFC 8445 - ICE](https://datatracker.ietf.org/doc/html/rfc8445)
//! - [RFC 8446 - TLS 1.3 (DTLS basis)](https://datatracker.ietf.org/doc/html/rfc8446)

#[cfg(test)]
mod setting_engine_test;

use std::net::IpAddr;
use std::sync::Arc;

use crypto::RTCCryptoProvider;
use dtls::cipher_suite::CipherSuiteId;
use dtls::extension::extension_use_srtp::SrtpProtectionProfile;
//TODO: use ice::agent::agent_config::{InterfaceFilterFn, IpFilterFn};
use ice::agent::remote_policy::RemoteAddressFilter;
use ice::mdns::MulticastDnsMode;
use ice::network_type::NetworkType;
use ice::{FailedNomination, NominationAcceptance, UnauthenticatedRequests};
//TODO: use ice::udp_network::UDPNetwork;
use crate::peer_connection::configuration::behaviour_profile::BehaviourProfile;
use crate::peer_connection::transport::dtls::role::RTCDtlsRole;
use crate::peer_connection::transport::ice::candidate_type::RTCIceCandidateType;
use std::time::Duration;

/// Equal to UDP MTU
pub(crate) const RECEIVE_MTU: usize = 1460;

/// ICE timeout configuration for connection health monitoring.
///
/// These timeouts control how ICE determines connection state transitions
/// and when to send keepalive packets. Adjust these for different network
/// conditions (mobile, satellite, etc.).
#[derive(Default, Clone)]
pub struct Timeout {
    /// Duration without network activity before ICE is considered disconnected.
    /// Default: 5 seconds.
    pub ice_disconnected_timeout: Option<Duration>,

    /// Duration without network activity before ICE is considered failed after disconnected.
    /// Default: 25 seconds.
    pub ice_failed_timeout: Option<Duration>,

    /// How often ICE sends keepalive packets when there's no media flow.
    /// Default: 2 seconds. If media is flowing, no keepalives are sent.
    pub ice_keepalive_interval: Option<Duration>,

    /// Controls how often ICE sends binding requests for a candidate pair.
    /// When combined with `ice_max_binding_requests`, controls how long ICE will attempt
    /// to connect a candidate.
    pub ice_check_interval: Option<Duration>,

    /// The max amount of binding requests ICE will send over a candidate pair for validation
    /// or nomination, if after max_binding_requests the candidate is yet to answer a binding
    /// request or a nomination we set the pair as failed.
    /// When combined with `ice_check_interval`, controls how long ICE will attempt
    /// to connect a candidate.
    pub ice_max_binding_requests: Option<u16>,

    /// Minimum wait time before accepting host candidates.
    pub ice_host_acceptance_min_wait: Option<Duration>,

    /// Minimum wait time before accepting server reflexive candidates.
    pub ice_srflx_acceptance_min_wait: Option<Duration>,

    /// Minimum wait time before accepting peer reflexive candidates.
    pub ice_prflx_acceptance_min_wait: Option<Duration>,

    /// Minimum wait time before accepting relay candidates.
    pub ice_relay_acceptance_min_wait: Option<Duration>,
}

/// Data-channel configuration for in-band channels.
#[derive(Clone, Default)]
pub struct DataChannel {
    /// Maximum time to wait for a peer's `DATA_CHANNEL_ACK` before failing an
    /// in-band data channel, counted from the moment the association came up on this side.
    /// `None` (the default) disables the timeout.
    ///
    /// RFC 8832 has none: the `DATA_CHANNEL_OPEN` and the `DATA_CHANNEL_ACK` are sent on a
    /// reliable, ordered stream, so they arrive while the association lives, and a channel
    /// whose association fails gets an error with it. A fixed timeout instead closed channels
    /// on a lossy path that was still up, where SCTP's retransmissions (which back off) or
    /// the peer's half of the SCTP handshake (a lost COOKIE ACK) took longer than it.
    pub dcep_handshake_timeout: Option<Duration>,
}

/// MulticastDNS configuration for mDNS.
#[derive(Clone)]
pub struct MulticastDNS {
    /// Duration without network activity before mDNS query is considered failed.
    /// Default: 10 seconds.
    pub timeout: Option<Duration>,
    /// Represents the different Multicast modes that ICE can run.
    pub mode: MulticastDnsMode,
    /// Controls the local name for this agent. If none is specified a random one will be generated.
    pub local_name: String,
    /// Control mDNS local IP address
    pub local_ip: Option<IpAddr>,
    /// See [`SettingEngineBuilder::with_multicast_dns_first_query_delay`]. `None`: on.
    pub first_query_delay: Option<bool>,
    /// See [`SettingEngineBuilder::with_multicast_dns_multi_question_delay`]. `None`: on.
    pub multi_question_delay: Option<bool>,
    /// See [`SettingEngineBuilder::with_multicast_dns_destination_reported`].
    pub destination_reported: bool,
    /// See [`SettingEngineBuilder::with_multicast_dns_unicast_response`].
    pub unicast_response: bool,
    /// See [`SettingEngineBuilder::with_multicast_dns_rate_limit`].
    pub rate_limit: Option<ice::mdns::MdnsRateLimit>,
}

impl Default for MulticastDNS {
    fn default() -> Self {
        Self {
            timeout: Some(Duration::from_secs(10)),
            // Safari and Chrome emit only mDNS ("<uuid>.local") host candidates
            // by default (to avoid leaking private IPs). With Disabled those are
            // silently dropped, leaving no usable remote candidates from such an
            // offer; QueryOnly resolves them via mDNS without publishing our own
            // host IPs as mDNS names. This matches the MulticastDnsMode enum's
            // own #[default].
            mode: MulticastDnsMode::QueryOnly,
            local_name: "".to_string(),
            local_ip: None,
            first_query_delay: None,
            multi_question_delay: None,
            destination_reported: false,
            unicast_response: false,
            rate_limit: None,
        }
    }
}

/// ICE candidate gathering and filtering configuration.
///
/// Controls which types of candidates are gathered, NAT mappings,
/// and custom network filtering.
#[derive(Default, Clone)]
pub struct Candidates {
    /// Enable ICE Lite mode (only respond to connectivity checks, don't initiate).
    pub ice_lite: bool,

    /// webrtc-extensions 7: propose each nomination to the application first
    /// (`OnIceCandidatePairNominate`, answered with
    /// `RTCPeerConnection::resolve_candidate_pair_nomination`), the cancelable
    /// `icecandidatepairnominate`. Off by default: the agent nominates at once.
    pub candidate_pair_proposals: bool,

    /// Restrict candidate gathering to specific network types (e.g., UDP4, UDP6, TCP4).
    pub ice_network_types: Vec<NetworkType>,
    //TODO: pub interface_filter: Arc<Option<InterfaceFilterFn>>,
    //TODO: pub ip_filter: Arc<Option<IpFilterFn>>,
    /// External IP addresses for 1:1 NAT mappings (e.g., AWS Elastic IP).
    pub nat_1to1_ips: Vec<String>,

    /// Candidate type to use for NAT 1:1 IPs (Host or Srflx).
    pub nat_1to1_ip_candidate_type: RTCIceCandidateType,
    /// Static ICE username fragment (ufrag) for reproducible sessions.
    pub username_fragment: String,

    /// Static ICE password for reproducible sessions.
    pub password: String,

    /// Whether to discard local candidates during ICE restart.
    pub discard_local_candidates_during_ice_restart: bool,

    /// Allow gathering loopback candidates (useful for some VM configurations).
    /// Note: This is non-standard per RFC 8445.
    ///
    /// Not read by this engine, which gathers no candidates (see the deprecated
    /// `SettingEngineBuilder::with_include_loopback_candidate`).
    pub include_loopback_candidate: bool,

    /// Lets connectivity checks go to loopback / link-local remote addresses from local
    /// candidates outside that scope. See
    /// `SettingEngineBuilder::with_remote_candidate_scopes`.
    pub allow_loopback_remote_candidates: bool,
    /// See `SettingEngineBuilder::with_remote_candidate_scopes`.
    pub allow_link_local_remote_candidates: bool,
    /// See `SettingEngineBuilder::with_remote_address_filter`.
    pub remote_address_filter: Option<RemoteAddressFilter>,
}

/// Where the ICE agent follows RFC 8445 / RFC 8489 by default and a browser (libwebrtc)
/// behaviour, or one of this fork's hardenings, can be chosen instead. Every default is the
/// RFC's.
#[derive(Default, Clone)]
pub struct IceBehaviour {
    /// See [`SettingEngineBuilder::with_ice_tie_breaker`].
    pub tie_breaker: Option<u64>,
    /// See [`SettingEngineBuilder::with_ice_nomination_acceptance`].
    pub nomination_acceptance: NominationAcceptance,
    /// See [`SettingEngineBuilder::with_ice_failed_nomination`].
    pub failed_nomination: FailedNomination,
    /// See [`SettingEngineBuilder::with_ice_nomination_on_consent_checks`].
    pub nominate_on_consent_checks: bool,
    /// See [`SettingEngineBuilder::with_ice_pacing`].
    pub pacing: Option<Duration>,
    /// See [`SettingEngineBuilder::with_ice_pacing_group`].
    pub pacing_group: Option<std::sync::Arc<ice::PacingGroup>>,
    /// See [`SettingEngineBuilder::with_ice_unauthenticated_requests`].
    pub unauthenticated_requests: UnauthenticatedRequests,
    /// See [`SettingEngineBuilder::with_ice_data_from_selected_pair_only`].
    pub data_from_selected_pair_only: bool,
    /// See [`SettingEngineBuilder::with_ice_data_before_nomination`].
    pub send_data_before_nomination: bool,
    /// See [`SettingEngineBuilder::with_ice_pairing_before_candidates_are_conveyed`].
    pub pair_before_conveyed: bool,
    /// See [`SettingEngineBuilder::with_ice_software`]: `None` until it is called (each
    /// stack's own SOFTWARE), then the value it was given.
    pub software: Option<Option<String>>,
}

/// Replay attack protection window sizes.
///
/// Larger windows provide better protection against packet reordering
/// but consume more memory. Set to 0 to disable replay protection (not recommended).
#[derive(Default, Copy, Clone)]
pub struct ReplayProtection {
    /// DTLS replay protection window size (in packets).
    pub dtls: usize,

    /// SRTP replay protection window size (in packets).
    pub srtp: usize,

    /// SRTCP replay protection window size (in packets).
    pub srtcp: usize,
}

/// Maximum message size for SCTP data channels.
///
/// Controls the maximum size of messages that can be sent through data channels.
/// Per [RFC 8841](https://datatracker.ietf.org/doc/html/rfc8841), the default is 64KB.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SctpMaxMessageSize {
    /// Fixed maximum message size in bytes.
    Bounded(u32),

    /// No practical limit (uses MAX_MESSAGE_SIZE internally).
    Unbounded,
}

impl SctpMaxMessageSize {
    /// Default message size per RFC 8841 (64KB).
    pub const DEFAULT_MESSAGE_SIZE: u32 = 65536;

    /// Maximum message size (256KB).
    pub const MAX_MESSAGE_SIZE: u32 = 262144;

    /// Returns the message size as `usize`.
    pub fn as_usize(&self) -> usize {
        match self {
            Self::Bounded(result) => (*result).min(Self::MAX_MESSAGE_SIZE) as usize,
            Self::Unbounded => Self::MAX_MESSAGE_SIZE as usize,
        }
    }
}

impl Default for SctpMaxMessageSize {
    fn default() -> Self {
        // https://datatracker.ietf.org/doc/html/rfc8841#section-6.1-4
        // > If the SDP "max-message-size" attribute is not present, the default value is 64K.
        Self::Bounded(Self::DEFAULT_MESSAGE_SIZE)
    }
}

/// Advanced configuration engine for fine-tuning WebRTC behavior.
///
/// `SettingEngine` provides granular control over transport-level settings that
/// are not exposed through the standard WebRTC API. Use this to optimize for
/// specific deployment scenarios, network conditions, or security requirements.
///
/// # Configuration Categories
///
/// - **Timeout**: ICE connection health monitoring and keepalive
/// - **TURN**: Allocation maintenance behavior
/// - **Candidates**: NAT traversal, network filtering, ICE credentials
/// - **Replay Protection**: Anti-replay window sizes for DTLS/SRTP/SRTCP
/// - **DTLS**: Certificate verification and role selection
/// - **Media Engine**: Codec registration behavior
/// - **SCTP**: Data channel message size limits
///
/// # Examples
///
/// ## Basic usage with RTCConfiguration
///
/// ```
/// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
/// use std::time::Duration;
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let setting_engine = SettingEngineBuilder::new()
///     // Configure timeouts
///     .with_ice_timeouts(
///         Some(Duration::from_secs(10)),
///         Some(Duration::from_secs(30)),
///         Some(Duration::from_secs(3)),
///     )
///     .build();
///
/// // Use with peer connection configuration
/// // let api = APIBuilder::new().with_setting_engine(setting_engine).build();
/// # Ok(())
/// # }
/// ```
///
/// # See Also
///
/// - [W3C WebRTC Spec](https://www.w3.org/TR/webrtc/)
/// - [RFC 8445 - ICE](https://datatracker.ietf.org/doc/html/rfc8445)
#[derive(Clone)]
pub struct SettingEngine {
    /// The profile the settings were last set to ([`SettingEngineBuilder::with_behaviour_profile`]);
    /// settings changed after it keep their own values.
    pub(crate) behaviour_profile: BehaviourProfile,
    pub(crate) crypto_provider: Option<Arc<dyn RTCCryptoProvider>>,
    pub(crate) timeout: Timeout,
    pub(crate) data_channel: DataChannel,
    pub(crate) turn_allocation_refresh_interval_cap: Option<Duration>,
    pub(crate) candidates: Candidates,
    pub(crate) ice: IceBehaviour,
    pub(crate) multicast_dns: MulticastDNS,
    pub(crate) replay_protection: ReplayProtection,
    /// Fingerprints at session level (`with_sdp_media_level_fingerprints(false)`); inverted
    /// so that the derived `Default` is media level.
    pub(crate) sdp_session_level_fingerprints: bool,
    pub(crate) disable_sdp_tls_id: bool,
    /// A remote description may ask for a new DTLS association without an ICE restart (RFC
    /// 8842 section 3.1 read without its section 6); off by default (RFC 9429 sections 5.8.3
    /// and 5.11).
    pub(crate) dtls_new_association_without_ice_restart: bool,
    /// A new DTLS association keeps the local candidates and may use the transport addresses
    /// of the one it replaces (`with_dtls_new_association_on_previous_addresses`); off by
    /// default (RFC 8842 sections 5.1 and 6).
    pub(crate) dtls_new_association_on_previous_addresses: bool,
    /// RFC 8830 section 3: a section that lists no `a=msid` any more ends its track.
    pub(crate) msid_removal_ends_track: bool,
    /// RFC 8830 section 3: a new msid-appdata in a section is a new track: the receiver's
    /// track ends and a new one takes its place.
    pub(crate) msid_appdata_change_renews_track: bool,
    /// RFC 8830 section 3: a remote track whose SSRCs have all left (BYE, timeout) ends.
    pub(crate) source_departure_ends_track: bool,
    /// RFC 9429 section 5.3.1: an answer rejects the section of a transceiver stopped after
    /// the offer arrived.
    pub(crate) stopping_transceiver_rejected_in_answer: bool,
    /// `use_srtp` only on an association that carries SRTP media (RFC 5764 section 4.1);
    /// inverted so that the derived `Default` is the browsers' form, the default.
    pub(crate) dtls_use_srtp_only_with_media: bool,
    pub(crate) accept_lowercase_fingerprints: bool,
    /// [`SettingEngineBuilder::with_bundled_fingerprints_agreeing`]: a remote description whose
    /// bundled sections carry fingerprints other than the tagged section's is refused.
    pub(crate) bundled_fingerprints_agree: bool,
    /// [`SettingEngineBuilder::with_cryptex_required_on_every_bundled_section`]: `a=cryptex`
    /// counts only at the session level or on every RTP section, the tagged one not standing
    /// for its group.
    pub(crate) cryptex_required_on_every_bundled_section: bool,
    /// Answers and subsequent offers repeat the BUNDLE attributes in every bundled m=
    /// section instead of carrying them in the tagged one only (RFC 9143 section 7.1.3); see
    /// [`SettingEngineBuilder::with_bundle_attributes_repeated`].
    /// Inverted so that the derived default is "repeated".
    pub(crate) bundle_attributes_tagged_only: bool,
    /// [`SettingEngineBuilder::with_max_bundle_as_must_bundle`].
    /// Inverted so that the derived default is "honoured".
    pub(crate) max_bundle_ignored: bool,
    /// [`SettingEngineBuilder::with_unique_transport_credentials`]: every transport of the
    /// connection gets ICE credentials of its own. The derived default `false` shares the first
    /// transport's.
    pub(crate) unique_transport_credentials: bool,
    /// [`SettingEngineBuilder::with_implicit_rollback`].
    /// Inverted so that the derived default is "implicit rollback on".
    pub(crate) explicit_rollback_only: bool,
    pub(crate) answering_dtls_role: RTCDtlsRole,
    pub(crate) disable_certificate_fingerprint_verification: bool,
    pub(crate) allow_insecure_verification_algorithm: bool,
    pub(crate) disable_media_engine_copy: bool,
    pub(crate) disable_media_engine_multiple_codecs: bool,
    /// [`SettingEngineBuilder::with_own_payload_types_in_answers`].
    pub(crate) own_payload_types_in_answers: bool,
    pub(crate) srtp_protection_profiles: Vec<SrtpProtectionProfile>,
    pub(crate) dtls_cipher_suites: Vec<CipherSuiteId>,
    pub(crate) receive_mtu: usize,
    pub(crate) mid_generator: Option<Arc<dyn Fn(isize) -> String + Send + Sync>>,
    /// Determines the max size of any message that may be sent through an SCTP transport.
    pub(crate) sctp_max_message_size: SctpMaxMessageSize,
    /// Overrides the SCTP receive-buffer size (the a_rwnd flow-control window), in bytes.
    /// `None` uses the rtci-sctp default (`INITIAL_RECV_BUF_SIZE`, 1 MiB).
    pub(crate) sctp_max_receive_buffer_size: Option<u32>,
    /// Overrides the outbound SCTP DATA-packet budget, converted to a payload
    /// budget via rtci-sctp's `max_payload_size_for_mtu`. `None` uses the rtci-sctp
    /// default (`INITIAL_MTU`, 1191 — the TURN-relayed IPv6 minimum-MTU budget).
    pub(crate) sctp_mtu: Option<u32>,
    /// SCTP path MTU discovery (RFC 8899 section 6.2.3): `Some` with the largest IP packet to
    /// probe for, `None` off. Unset is the default, on up to 1500 bytes.
    pub(crate) sctp_path_mtu_discovery: Option<Option<u32>>,
    /// SCTP streams share the bytes sent by weight (RFC 8260 section 3.6, RFC 8831 section
    /// 6.4). `None` is the default, on.
    pub(crate) sctp_weighted_fair_queueing: Option<bool>,
    /// SCTP user message interleaving (RFC 8260) is announced. `None` is the default, on.
    pub(crate) sctp_message_interleaving: Option<bool>,
    /// The SCTP zero checksum (RFC 9653) is announced as acceptable. `None` is the default,
    /// on.
    pub(crate) sctp_zero_checksum: Option<bool>,
    /// [`SettingEngineBuilder::with_sctp_max_streams`]. `None` is the default, 65535.
    pub(crate) sctp_max_streams: Option<u16>,
    /// [`SettingEngineBuilder::with_sctp_inbound_reassembly_limit`]. `None` is the default,
    /// 64 MiB.
    pub(crate) sctp_inbound_reassembly_limit: Option<usize>,
    /// [`SettingEngineBuilder::with_sctp_rto`]. `None` is RFC 9260's 1 / 1 / 60 seconds.
    pub(crate) sctp_rto: Option<sctp::RtoConfig>,
    /// [`SettingEngineBuilder::with_sctp_max_retransmissions`]. `None` is RFC 9260's 10.
    pub(crate) sctp_max_retransmissions: Option<usize>,
    /// A message over 16 KB is refused on an association without message interleaving
    /// (RFC 8831 section 6.6). `None` is the default, on.
    pub(crate) sctp_message_size_limit_without_interleaving: Option<bool>,
    /// [`SettingEngineBuilder::with_data_channel_max_buffer_size`].
    pub(crate) data_channel_max_buffer_size: Option<u32>,
    /// [`SettingEngineBuilder::with_data_channel_send_before_ack`].
    pub(crate) data_channel_send_before_ack: bool,
    /// [`SettingEngineBuilder::with_non_interactive_video`].
    pub(crate) non_interactive_video: bool,
    /// [`SettingEngineBuilder::with_srtp_key_lifetime`].
    pub(crate) srtp_key_lifetime: Option<srtp::keying::KeyLifetime>,
    /// [`SettingEngineBuilder::with_dtls_connection_id`].
    pub(crate) dtls_connection_id_length: Option<usize>,
    /// [`SettingEngineBuilder::with_dtls_alpn`]: `true` when it is turned off.
    pub(crate) dtls_alpn_off: bool,
    /// [`SettingEngineBuilder::with_srtp_automatic_rekeying`]: `true` when it is turned off.
    pub(crate) srtp_automatic_rekeying_off: bool,
    /// [`SettingEngineBuilder::with_srtp_rekeying_dtls_client_first`]: `true` when it is
    /// turned off (both sides ask at the lifetime warning).
    pub(crate) srtp_rekeying_both_at_warning: bool,
    pub(crate) ignore_rid_pause_for_recv: bool,
    /// [`SettingEngineBuilder::with_rid_pause_marks`].
    pub(crate) rid_pause_marks: bool,
    /// [`SettingEngineBuilder::with_rollback_content_ignored`].
    pub(crate) rollback_content_ignored: bool,
    /// [`SettingEngineBuilder::with_strict_sdp_attribute_grammar`].
    pub(crate) strict_sdp_attribute_grammar: bool,
    /// [`SettingEngineBuilder::with_w3c_simulcast_answer`].
    pub(crate) w3c_simulcast_answer: bool,
    /// [`SettingEngineBuilder::with_nominated_candidates_only`].
    pub(crate) nominated_candidates_only: bool,
    /// [`SettingEngineBuilder::with_msid_appdata`]`(false)`: `a=msid` lines without the
    /// track id (inverted so that the derived default writes it).
    pub(crate) msid_appdata_omitted: bool,
    /// [`SettingEngineBuilder::with_video_receive_limits`].
    pub(crate) video_receive_limits: Option<crate::peer_connection::sdp::RTCVideoResolutionRange>,
    /// [`SettingEngineBuilder::with_codec_video_receive_limits`], by MIME type.
    pub(crate) codec_video_receive_limits:
        Vec<(String, crate::peer_connection::sdp::RTCVideoResolutionRange)>,
    /// Whether `write_rtp` sends header extension elements whose id is not negotiated; see
    /// [`SettingEngineBuilder::with_unnegotiated_header_extensions`].
    pub(crate) unnegotiated_header_extensions: bool,
    /// [`SettingEngineBuilder::with_ssrc_per_clock_rate`]: a codec of another clock rate moves
    /// the stream to a new SSRC (RFC 7160 section 4.1). Off by default: one SSRC across clock
    /// rates, which every browser receiver follows.
    pub(crate) ssrc_per_clock_rate: bool,
    /// [`SettingEngineBuilder::with_audio_redundancy`]: when an audio stream that negotiated
    /// RED (RFC 2198) is sent in it.
    pub(crate) audio_redundancy: interceptor::AudioRedundancy,
    pub(crate) write_ssrc_attributes_for_simulcast: bool,
    /// The application's socket layer reports each received datagram's ECN codepoint and
    /// applies the one each sent datagram carries ([`SettingEngineBuilder::with_ecn`]).
    pub(crate) ecn: bool,
    /// How a stream starts marking ([`SettingEngineBuilder::with_ecn_initiation`]).
    pub(crate) ecn_initiation: interceptor::EcnInitiation,
    /// ECN is used on `ack ccfb` alone, without `a=ecn-capable-rtp`
    /// ([`SettingEngineBuilder::with_ecn_without_signalling`]).
    pub(crate) ecn_without_signalling: bool,
    /// The engine's own stream transports ([`SettingEngineBuilder::with_tcp_candidate_gathering`]
    /// and the methods after it).
    pub(crate) streams: crate::peer_connection::streams::StreamSettings,
    /// How the engine's TURN-over-TLS connections verify their server
    /// ([`SettingEngineBuilder::with_turn_tls`]).
    pub(crate) turn_tls: crate::peer_connection::turn_tls::TurnTlsConfig,
    /// Which ECT codepoint marked packets carry ([`SettingEngineBuilder::with_ecn_marking`]).
    pub(crate) ecn_marking: interceptor::EcnMarking,
    /// The connection runs without the RTCP session the builder otherwise installs
    /// ([`SettingEngineBuilder::with_rtcp_session_omitted`]); a departure from RFC 8834 4.1.
    pub(crate) rtcp_session_omitted: bool,
    /// Received RTCP is held to RFC 3550 Appendix A.2 and section 6.4.1
    /// ([`SettingEngineBuilder::with_strict_rtcp_validity`]).
    pub(crate) strict_rtcp_validity: bool,
    /// RTCP feedback the application writes is sent whether or not its section negotiated
    /// it ([`SettingEngineBuilder::with_unnegotiated_rtcp_feedback`]).
    pub(crate) unnegotiated_rtcp_feedback: bool,
    /// `a=ice-options` in each media section instead of once at session level
    /// ([`SettingEngineBuilder::with_ice_options_in_media_sections`]).
    pub(crate) ice_options_in_media_sections: bool,
    /// [`SettingEngineBuilder::with_default_ice_pacing_stated`]`(false)`: no `a=ice-pacing`
    /// line while Ta is the default (inverted so that the derived default states it).
    pub(crate) default_ice_pacing_omitted: bool,
    /// How the DTLS client's retransmission timer starts
    /// ([`SettingEngineBuilder::with_dtls_flight_interval`]).
    pub(crate) dtls_flight_interval: DtlsFlightInterval,
}

/// How the first DTLS retransmission timer of a peer connection's handshake is chosen
/// ([`SettingEngineBuilder::with_dtls_flight_interval`]). The timer doubles on each
/// retransmission either way (RFC 9147 section 5.8.2, RFC 6347 section 4.2.4.1).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum DtlsFlightInterval {
    /// RFC 9147 section 5.8.2 for DTLS-SRTP (the default): 400 ms, or, once ICE has measured
    /// the round trip time of the selected pair, 1.5 times that and never under 100 ms.
    #[default]
    Rfc9147,
    /// libwebrtc's `DtlsTransportInternalImpl::ConfigureHandshakeTimeout`
    /// (`p2p/dtls/dtls_transport.cc`, `ComputeRetransmissionTimeout`): twice ICE's round
    /// trip time, held between 50 ms and 3 s; 50 ms without a measured round trip time (the
    /// initial value `OpenSSLStreamAdapter` gives BoringSSL).
    Libwebrtc,
}

impl DtlsFlightInterval {
    /// The first retransmission timer for a handshake started with `round_trip_time`, ICE's
    /// measurement of the selected pair, if any.
    pub fn initial(self, round_trip_time: Option<Duration>) -> Duration {
        match (self, round_trip_time) {
            (Self::Rfc9147, None) => {
                crate::peer_connection::transport::dtls::DTLS_SRTP_FLIGHT_INTERVAL
            }
            (Self::Rfc9147, Some(rtt)) => (rtt + rtt / 2)
                .max(crate::peer_connection::transport::dtls::MINIMUM_FLIGHT_INTERVAL),
            (Self::Libwebrtc, None) => Duration::from_millis(50),
            (Self::Libwebrtc, Some(rtt)) => {
                (rtt * 2).clamp(Duration::from_millis(50), Duration::from_secs(3))
            }
        }
    }
}

/// The engine's own defaults, [`BehaviourProfile::Rfc`], whatever the build's default profile.
impl SettingEngine {
    pub(crate) fn rfc_base() -> Self {
        Self {
            behaviour_profile: BehaviourProfile::Rfc,
            crypto_provider: Default::default(),
            timeout: Default::default(),
            data_channel: Default::default(),
            turn_allocation_refresh_interval_cap: Default::default(),
            candidates: Default::default(),
            ice: Default::default(),
            multicast_dns: Default::default(),
            replay_protection: Default::default(),
            sdp_session_level_fingerprints: Default::default(),
            disable_sdp_tls_id: Default::default(),
            dtls_new_association_without_ice_restart: Default::default(),
            msid_removal_ends_track: Default::default(),
            stopping_transceiver_rejected_in_answer: Default::default(),
            dtls_use_srtp_only_with_media: Default::default(),
            accept_lowercase_fingerprints: Default::default(),
            bundle_attributes_tagged_only: Default::default(),
            max_bundle_ignored: Default::default(),
            unique_transport_credentials: Default::default(),
            explicit_rollback_only: Default::default(),
            answering_dtls_role: Default::default(),
            disable_certificate_fingerprint_verification: Default::default(),
            allow_insecure_verification_algorithm: Default::default(),
            disable_media_engine_copy: Default::default(),
            disable_media_engine_multiple_codecs: Default::default(),
            srtp_protection_profiles: Default::default(),
            dtls_cipher_suites: Default::default(),
            receive_mtu: Default::default(),
            mid_generator: Default::default(),
            sctp_max_message_size: Default::default(),
            sctp_max_receive_buffer_size: Default::default(),
            sctp_mtu: Default::default(),
            sctp_path_mtu_discovery: Default::default(),
            sctp_weighted_fair_queueing: Default::default(),
            sctp_message_interleaving: Default::default(),
            sctp_zero_checksum: Default::default(),
            sctp_max_streams: Default::default(),
            sctp_rto: Default::default(),
            sctp_max_retransmissions: Default::default(),
            sctp_message_size_limit_without_interleaving: Default::default(),
            data_channel_max_buffer_size: Default::default(),
            data_channel_send_before_ack: Default::default(),
            non_interactive_video: Default::default(),
            srtp_key_lifetime: Default::default(),
            srtp_automatic_rekeying_off: Default::default(),
            srtp_rekeying_both_at_warning: Default::default(),
            ignore_rid_pause_for_recv: Default::default(),
            rid_pause_marks: Default::default(),
            rollback_content_ignored: Default::default(),
            strict_sdp_attribute_grammar: Default::default(),
            w3c_simulcast_answer: Default::default(),
            nominated_candidates_only: Default::default(),
            msid_appdata_omitted: Default::default(),
            video_receive_limits: Default::default(),
            codec_video_receive_limits: Default::default(),
            unnegotiated_header_extensions: Default::default(),
            ssrc_per_clock_rate: Default::default(),
            // Train 11's fields, at the value the derive gave them.
            msid_appdata_change_renews_track: Default::default(),
            source_departure_ends_track: Default::default(),
            bundled_fingerprints_agree: Default::default(),
            cryptex_required_on_every_bundled_section: Default::default(),
            dtls_connection_id_length: Default::default(),
            dtls_alpn_off: Default::default(),
            write_ssrc_attributes_for_simulcast: Default::default(),
            ecn: Default::default(),
            ecn_initiation: Default::default(),
            ecn_without_signalling: Default::default(),
            streams: Default::default(),
            turn_tls: Default::default(),
            ecn_marking: Default::default(),
            rtcp_session_omitted: Default::default(),
            strict_rtcp_validity: Default::default(),
            unnegotiated_rtcp_feedback: Default::default(),
            ice_options_in_media_sections: Default::default(),
            default_ice_pacing_omitted: Default::default(),
            dtls_flight_interval: Default::default(),
            // Master's fields of the train-12 merge, at the value the derive gave them.
            own_payload_types_in_answers: Default::default(),
            audio_redundancy: Default::default(),
            // Train 13's field (sctp-15), at the value the derive gave it.
            sctp_inbound_reassembly_limit: Default::default(),
            // Train 13's field (mt-9), at the value the derive gave it.
            dtls_new_association_on_previous_addresses: Default::default(),
        }
    }

    /// The behaviour profile the settings were last set to: the build's default profile
    /// ([`BehaviourProfile::compiled_default`]) unless
    /// [`SettingEngineBuilder::with_behaviour_profile`] named another. Settings changed after it
    /// keep their own values.
    pub fn behaviour_profile(&self) -> BehaviourProfile {
        self.behaviour_profile
    }
}

/// Every setting at the default of [`BehaviourProfile::default()`]: the RFC defaults, or
/// Chrome's with the `chrome-defaults` cargo feature (see [`BehaviourProfile`]).
impl Default for SettingEngine {
    fn default() -> Self {
        SettingEngineBuilder(Self::rfc_base())
            .with_behaviour_profile(BehaviourProfile::default())
            .build()
    }
}

impl SettingEngine {
    /// The crypto provider configured on this engine, if any.
    ///
    /// `None` means no provider has been set, so building a peer connection will resolve the
    /// feature-selected built-in.
    ///
    /// Callers assembling additional components around a peer connection — an async wrapper's
    /// TURN client, for instance — read the provider from here and pass it to
    /// [`with_crypto_provider`](SettingEngineBuilder::with_crypto_provider) before building, so the whole
    /// connection provably shares one provider instead of resolving a second.
    pub fn crypto_provider(&self) -> Option<&Arc<dyn RTCCryptoProvider>> {
        self.crypto_provider.as_ref()
    }

    /// Set crypto provider on this engine.
    #[doc(hidden)]
    pub fn set_crypto_provider(&mut self, crypto_provider: Arc<dyn RTCCryptoProvider>) {
        self.crypto_provider = Some(crypto_provider);
    }

    /// Returns the multicast DNS configuration.
    pub fn multicast_dns(&self) -> &MulticastDNS {
        &self.multicast_dns
    }

    /// Returns the configured maximum interval between TURN allocation Refresh requests.
    ///
    /// `None` preserves the TURN client's default cadence of half the lifetime advertised by
    /// the server.
    pub fn turn_allocation_refresh_interval_cap(&self) -> Option<Duration> {
        self.turn_allocation_refresh_interval_cap
    }

    /// The SOFTWARE of the ICE agent's STUN messages, in the form of
    /// [`ice::agent::agent_config::AgentConfig::software`] (`None`: its default, empty: none);
    /// see [`SettingEngineBuilder::with_ice_software`].
    pub(crate) fn ice_agent_software(&self) -> Option<String> {
        self.ice
            .software
            .as_ref()
            .map(|software| software.clone().unwrap_or_default())
    }

    /// The SOFTWARE of the server-reflexive Binding requests the engine's gatherer sends (RFC
    /// 8489 section 14.14: a request SHOULD carry it), resolved: the ICE agent's default
    /// ([`ice::agent::DEFAULT_SOFTWARE`]) unless [`SettingEngineBuilder::with_ice_software`]
    /// says otherwise, and `None` (no attribute) for `with_ice_software(None)` or an empty text.
    pub(crate) fn srflx_software(&self) -> Option<String> {
        match &self.ice.software {
            None => Some(ice::agent::DEFAULT_SOFTWARE.to_owned()),
            Some(Some(software)) if !software.is_empty() => Some(software.clone()),
            Some(_) => None,
        }
    }

    /// The SOFTWARE of the requests of the TURN clients the engine runs, in the form of
    /// [`turn::client::ClientConfig::software`] (empty: none); see
    /// [`SettingEngineBuilder::with_ice_software`].
    pub(crate) fn turn_software(&self) -> String {
        match &self.ice.software {
            None => turn::client::DEFAULT_SOFTWARE.to_owned(),
            Some(software) => software.clone().unwrap_or_default(),
        }
    }

    /// Whether an ICE restart discards the local candidates gathered by the previous generation.
    ///
    /// Set through
    /// [`with_discard_local_candidates_during_ice_restart`](SettingEngineBuilder::with_discard_local_candidates_during_ice_restart),
    /// where the reasoning lives.
    ///
    /// Read by the layer that owns the sockets, because the two decisions are one decision.
    /// Discarding the old candidates is only necessary when the transport under them is being
    /// replaced, and replacing that transport is only safe when the candidates naming it are
    /// discarded — so an async wrapper treats this as "the sockets are replaced on restart" and
    /// rebinds them, rather than exposing a second switch the two halves could disagree on.
    pub fn discard_local_candidates_during_ice_restart(&self) -> bool {
        self.candidates.discard_local_candidates_during_ice_restart
    }

    /// The ICE credentials configured on this engine.
    ///
    /// Both empty (the default) means every peer connection generates fresh random
    /// credentials when its ICE agent is created.
    pub fn ice_credentials(&self) -> (&str, &str) {
        (
            self.candidates.username_fragment.as_str(),
            self.candidates.password.as_str(),
        )
    }

    /// Set ICE credentials on this engine.
    #[doc(hidden)]
    pub fn set_ice_credentials(&mut self, username_fragment: String, password: String) {
        self.candidates.username_fragment = username_fragment;
        self.candidates.password = password;
    }
}

/// Fluent registry for [`SettingEngine`].
///
/// Mirrors [`RTCConfigurationBuilder`](crate::peer_connection::configuration::RTCConfigurationBuilder),
/// so a peer connection is configured in one style throughout rather than switching between a
/// fluent registry and a sequence of `&mut self` setters.
///
/// [`build`](Self::build) is infallible: every setting is either always valid or normalised on
/// the way in, so there is no failure mode to report.
#[derive(Default)]
pub struct SettingEngineBuilder(SettingEngine);

impl SettingEngineBuilder {
    /// A registry with every setting at its default: that of [`BehaviourProfile::default()`],
    /// the RFC defaults unless the `chrome-defaults` cargo feature is on in the build.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets every setting a [`BehaviourProfile`] governs to the profile's value, as the table
    /// `docs/behaviour-profiles.md` lists them; settings the profile does not govern keep
    /// theirs. A setting changed after this call keeps the value it is given.
    ///
    /// The same profile goes to [`MediaEngine::for_profile`](crate::peer_connection::configuration::media_engine::MediaEngine::for_profile),
    /// from which
    /// [`register_default_interceptors`](crate::peer_connection::configuration::interceptor_registry::register_default_interceptors)
    /// takes it, so that the whole peer connection follows one profile.
    ///
    /// ```
    /// use rtci::peer_connection::configuration::BehaviourProfile;
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_behaviour_profile(BehaviourProfile::Chrome)
    ///     .build();
    /// assert_eq!(setting_engine.behaviour_profile(), BehaviourProfile::Chrome);
    /// ```
    pub fn with_behaviour_profile(self, profile: BehaviourProfile) -> Self {
        let mut builder =
            crate::peer_connection::configuration::behaviour_profile::apply_to_setting_engine(
                self, profile,
            );
        builder.0.behaviour_profile = profile;
        builder
    }

    /// Selects the cryptographic provider used by peer connections built with this setting engine.
    ///
    /// The provider is resolved once at peer-connection construction and shared with ICE, DTLS,
    /// SRTP, certificate, and fingerprint operations. Different peer connections may select
    /// different providers in the same process.
    pub fn with_crypto_provider(mut self, provider: Arc<dyn RTCCryptoProvider>) -> Self {
        self.0.crypto_provider = Some(provider);
        self
    }

    /// Overrides the default SRTP protection profiles.
    ///
    /// SRTP profiles define the encryption algorithms used for media streams.
    /// Only override this if you need specific security requirements or
    /// compatibility with non-standard implementations.
    ///
    /// # Parameters
    ///
    /// * `profiles` - List of SRTP protection profiles to use
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    /// use dtls::extension::extension_use_srtp::SrtpProtectionProfile;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Use specific SRTP profile
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_srtp_protection_profiles(vec![
    ///         SrtpProtectionProfile::Srtp_Aes128_Cm_Hmac_Sha1_80,
    ///     ])
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_srtp_protection_profiles(mut self, profiles: Vec<SrtpProtectionProfile>) -> Self {
        self.0.srtp_protection_profiles = profiles;
        self
    }

    /// Restricts the DTLS cipher suites offered during the handshake.
    ///
    /// An empty list (the default) uses the WebRTC default set: the `dtls` crate's built-in
    /// suites without AES-256-CBC-SHA (AES-128-GCM and ChaCha20-Poly1305; browsers negotiate
    /// GCM). List a CBC suite here to offer it. The default offers **both** ECDHE_ECDSA and
    /// ECDHE_RSA suites. That is deliberate — the local
    /// certificate is not known when the list is compiled — but it means a remote peer may
    /// select an ECDHE_RSA suite that an ECDSA certificate cannot satisfy, and the
    /// handshake then stalls with the connection stuck in `Connecting`.
    ///
    /// Certificates generated by this crate are ECDSA (P-256), so an application that does
    /// not supply its own RSA certificate can pin the ECDSA suites and remove that
    /// possibility:
    ///
    /// ```
    /// use dtls::cipher_suite::CipherSuiteId;
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_dtls_cipher_suites(vec![
    ///         CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_128_Gcm_Sha256,
    ///         CipherSuiteId::Tls_Ecdhe_Ecdsa_With_Aes_256_Cbc_Sha,
    ///         CipherSuiteId::Tls_Ecdhe_Ecdsa_With_ChaCha20_Poly1305_Sha256,
    ///     ])
    ///     .build();
    /// ```
    ///
    /// The order is a preference order. Every suite named must be one the `dtls` crate
    /// implements, or building the transport fails with `ErrInvalidCipherSuite`; a list
    /// that filters down to nothing usable fails with `ErrNoAvailableCipherSuites`.
    pub fn with_dtls_cipher_suites(mut self, cipher_suites: Vec<CipherSuiteId>) -> Self {
        self.0.dtls_cipher_suites = cipher_suites;
        self
    }

    /// Caps the interval between TURN allocation Refresh requests.
    ///
    /// By default, TURN allocations are refreshed at half the lifetime advertised by the server.
    /// A shorter cap can keep an otherwise idle client-to-server NAT mapping active. Values below
    /// one second are rounded up by the TURN client.
    pub fn with_turn_allocation_refresh_interval_cap(mut self, cap: Duration) -> Self {
        self.0.turn_allocation_refresh_interval_cap = Some(cap);
        self
    }

    /// Configures ICE timeout behavior for connection health monitoring.
    ///
    /// These timeouts control when ICE transitions between connection states
    /// and when keepalive packets are sent. Adjust these for different network
    /// conditions:
    /// - Increase for unstable networks (mobile, satellite)
    /// - Decrease for low-latency applications
    ///
    /// # Parameters
    ///
    /// * `disconnected_timeout` - Duration without activity before considered disconnected (default: 5s)
    /// * `failed_timeout` - Duration after disconnected before considered failed (default: 25s)
    /// * `keep_alive_interval` - How often to send keepalives when idle (default: 2s)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    /// use std::time::Duration;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Conservative settings for mobile networks
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_ice_timeouts(
    ///         Some(Duration::from_secs(10)),  // Longer before disconnected
    ///         Some(Duration::from_secs(40)),  // Longer before failed
    ///         Some(Duration::from_secs(5)),   // Less frequent keepalives
    ///     )
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    ///
    /// - [RFC 8445 §16 - Timers](https://datatracker.ietf.org/doc/html/rfc8445#section-16)
    pub fn with_ice_timeouts(
        mut self,
        disconnected_timeout: Option<Duration>,
        failed_timeout: Option<Duration>,
        keep_alive_interval: Option<Duration>,
    ) -> Self {
        self.0.timeout.ice_disconnected_timeout = disconnected_timeout;
        self.0.timeout.ice_failed_timeout = failed_timeout;
        self.0.timeout.ice_keepalive_interval = keep_alive_interval;
        self
    }

    /// Configures the DCEP handshake timeout for in-band data channels.
    ///
    /// Off by default (RFC 8832 has no such timeout; see [`DataChannel::dcep_handshake_timeout`]).
    /// A channel whose `DATA_CHANNEL_ACK` has not arrived in time gets an error and is closed.
    ///
    /// # Parameters
    ///
    /// * `timeout` - Maximum time to wait for the peer's `DATA_CHANNEL_ACK`,
    ///   or `None` to disable the timeout.
    pub fn with_dcep_handshake_timeout(mut self, timeout: Option<Duration>) -> Self {
        self.0.data_channel.dcep_handshake_timeout = timeout;
        self
    }

    /// Configures ICE connection attempt behavior, including number of connection attempts
    /// per candidate, and the amount of time between connection attempts.
    ///
    /// The default settings configure ICE to attempt to connect a candidate for up to 1.4 seconds.
    ///
    /// # Parameters
    ///
    /// * `check_interval` - The delay between each connection attempt (default: 200 ms)
    /// * `max_binding_requests` - Maximum number of connection attempts per candidate (default: 7)
    pub fn with_ice_connection_attempts(
        mut self,
        check_interval: Option<Duration>,
        max_binding_requests: Option<u16>,
    ) -> Self {
        self.0.timeout.ice_check_interval = check_interval;
        self.0.timeout.ice_max_binding_requests = max_binding_requests;
        self
    }

    /// Sets minimum wait time before accepting host candidates.
    ///
    /// # Parameters
    ///
    /// * `t` - Minimum wait duration, or `None` for immediate acceptance
    pub fn with_host_acceptance_min_wait(mut self, t: Option<Duration>) -> Self {
        self.0.timeout.ice_host_acceptance_min_wait = t;
        self
    }

    /// Sets minimum wait time before accepting server reflexive candidates.
    ///
    /// Server reflexive candidates are discovered through STUN servers.
    ///
    /// # Parameters
    ///
    /// * `t` - Minimum wait duration, or `None` for immediate acceptance
    pub fn with_srflx_acceptance_min_wait(mut self, t: Option<Duration>) -> Self {
        self.0.timeout.ice_srflx_acceptance_min_wait = t;
        self
    }

    /// Sets minimum wait time before accepting peer reflexive candidates.
    ///
    /// Peer reflexive candidates are discovered during connectivity checks.
    ///
    /// # Parameters
    ///
    /// * `t` - Minimum wait duration, or `None` for immediate acceptance
    pub fn with_prflx_acceptance_min_wait(mut self, t: Option<Duration>) -> Self {
        self.0.timeout.ice_prflx_acceptance_min_wait = t;
        self
    }

    /// Sets minimum wait time before accepting relay candidates.
    ///
    /// Relay candidates are provided by TURN servers.
    ///
    /// # Parameters
    ///
    /// * `t` - Minimum wait duration, or `None` for immediate acceptance
    pub fn with_relay_acceptance_min_wait(mut self, t: Option<Duration>) -> Self {
        self.0.timeout.ice_relay_acceptance_min_wait = t;
        self
    }

    /*todo:
    /// set_udp_network allows ICE traffic to come through Ephemeral or UDPMux.
    /// UDPMux drastically simplifying deployments where ports will need to be opened/forwarded.
    /// UDPMux should be started prior to creating PeerConnections.
    pub fn with_udp_network(mut self, udp_network: UDPNetwork) -> Self {
        self.0.udp_network = udp_network;
    }*/

    /// webrtc-extensions 7 `icecandidatepairnominate`, cancelable: with `true`, the
    /// controlling agent proposes the pair it picked to nominate
    /// (`RTCPeerConnectionEvent::OnIceCandidatePairNominate`) and nominates it only when the
    /// application accepts (`RTCPeerConnection::resolve_candidate_pair_nomination`); a
    /// declined pair is not nominated and checks go on. Off by default (an event nobody
    /// cancels).
    pub fn with_candidate_pair_proposals(mut self, proposals: bool) -> Self {
        self.0.candidates.candidate_pair_proposals = proposals;
        self
    }

    /// Configures ICE Lite mode.
    ///
    /// In ICE Lite mode, the agent only responds to connectivity checks
    /// but does not initiate them. This is typically used by servers that
    /// have public IP addresses and don't need full ICE functionality.
    ///
    /// # Parameters
    ///
    /// * `lite` - `true` to enable ICE Lite, `false` for full ICE
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Enable ICE Lite for a publicly accessible server
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_lite(true)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    ///
    /// - [RFC 8445 §2.7 - Lite Implementation](https://datatracker.ietf.org/doc/html/rfc8445#section-2.7)
    pub fn with_lite(mut self, lite: bool) -> Self {
        self.0.candidates.ice_lite = lite;
        self
    }

    /// Restricts candidate gathering to specific network types.
    ///
    /// This reduces the number of candidates gathered, which can speed up
    /// connection establishment and reduce SDP size. Useful when you know
    /// certain network types won't work in your deployment.
    ///
    /// # Parameters
    ///
    /// * `candidate_types` - List of allowed network types (e.g., UDP4, UDP6, TCP4)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    /// use ice::network_type::NetworkType;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Only use IPv4 UDP (most common case)
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_network_types(vec![NetworkType::Udp4])
    ///     .build();
    ///
    /// // Or allow both IPv4 and IPv6 UDP
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_network_types(vec![NetworkType::Udp4, NetworkType::Udp6])
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_network_types(mut self, candidate_types: Vec<NetworkType>) -> Self {
        self.0.candidates.ice_network_types = candidate_types;
        self
    }

    /*todo:
    /// set_interface_filter sets the filtering functions when gathering ICE candidates
    /// This can be used to exclude certain network interfaces from ICE. Which may be
    /// useful if you know a certain interface will never succeed, or if you wish to reduce
    /// the amount of information you wish to expose to the remote peer
    pub fn with_interface_filter(mut self, filter: InterfaceFilterFn) -> Self {
        self.0.candidates.interface_filter = Arc::new(Some(filter));
        self
    }


    /// set_ip_filter sets the filtering functions when gathering ICE candidates
    /// This can be used to exclude certain ip from ICE. Which may be
    /// useful if you know a certain ip will never succeed, or if you wish to reduce
    /// the amount of information you wish to expose to the remote peer
    pub fn with_ip_filter(mut self, filter: IpFilterFn) -> Self {
        self.0.candidates.ip_filter = Arc::new(Some(filter));
    }*/

    /// Configures 1:1 NAT IP mapping for cloud deployments.
    ///
    /// This is essential for WebRTC servers running on cloud instances (e.g., AWS EC2)
    /// that have a private IP address but are accessible via a public IP through 1:1 NAT.
    ///
    /// # Parameters
    ///
    /// * `ips` - List of external/public IP addresses
    /// * `candidate_type` - How to advertise the public IPs:
    ///   - `RTCIceCandidateType::Host`: Replace private IP with public IP (mDNS disabled)
    ///   - `RTCIceCandidateType::Srflx`: Add public IP as server reflexive candidate
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    /// use rtci::peer_connection::transport::RTCIceCandidateType;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // AWS EC2: Private IP 10.0.1.5, Elastic IP 54.123.45.67
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_nat_1to1_ips(
    ///         vec!["54.123.45.67".to_string()],
    ///         RTCIceCandidateType::Host,
    ///     )
    ///     .build();
    ///
    /// // Or use Srflx to keep private IP available
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_nat_1to1_ips(
    ///         vec!["54.123.45.67".to_string()],
    ///         RTCIceCandidateType::Srflx,
    ///     )
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Note
    ///
    /// - With `Host` type, the private IP is not advertised to the peer
    /// - With `Srflx` type, both private and public IPs are available
    /// - Cannot use STUN servers when using `Srflx` type
    /// - Cannot use with mDNS when using `Host` type
    ///
    /// # How it applies
    ///
    /// The sans-I/O engine gathers no candidates: the mapping is applied to the host
    /// candidates the application adds with `add_local_candidate`. With `Host` a mapped host
    /// candidate is announced on the public address (it stays bound to the local one); with
    /// `Srflx` a server-reflexive candidate on the public address is announced beside it. Each
    /// entry is `"<public ip>"` or `"<public ip>/<local ip>"`. An invalid list, `Host` with
    /// mDNS `QueryAndGather`, or a type the candidate policy excludes fails peer connection
    /// creation.
    pub fn with_nat_1to1_ips(
        mut self,
        ips: Vec<String>,
        candidate_type: RTCIceCandidateType,
    ) -> Self {
        self.0.candidates.nat_1to1_ips = ips;
        self.0.candidates.nat_1to1_ip_candidate_type = candidate_type;
        self
    }

    /// Sets the DTLS role to use when answering an offer.
    ///
    /// The DTLS role determines whether this peer acts as a DTLS client
    /// (initiating the handshake) or server (waiting for handshake). Normally
    /// this is negotiated automatically, but you can override it for debugging
    /// or compatibility with non-compliant implementations.
    ///
    /// # Parameters
    ///
    /// * `role` - DTLS role to use:
    ///   - `DTLSRole::Client`: Act as DTLS client, send ClientHello
    ///   - `DTLSRole::Server`: Act as DTLS server, wait for ClientHello
    ///
    /// # Returns
    ///
    /// * `Ok(())` on success
    /// * `Err(Error)` if role is not Client or Server
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    /// use rtci::peer_connection::transport::RTCDtlsRole;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Force this peer to always act as DTLS client when answering
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_answering_dtls_role(RTCDtlsRole::Client)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    ///
    /// - [RFC 8842 - DTLS for WebRTC](https://datatracker.ietf.org/doc/html/rfc8842)
    pub fn with_answering_dtls_role(mut self, role: RTCDtlsRole) -> Self {
        // Deliberately unvalidated. `Unspecified` and `Auto` are not errors here: both
        // consumers already handle them — `to_connection_role()` maps `Unspecified` to
        // `ConnectionRole::Unspecified`, which the answer path detects and replaces with
        // `DEFAULT_DTLS_ROLE_ANSWER`, and the role-selection `match` falls through to the
        // ICE-derived default. Rejecting them contradicted behaviour that already worked.
        self.0.answering_dtls_role = role;
        self
    }

    /// Sets the timeout for multicast DNS resolution.
    pub fn with_multicast_dns_timeout(mut self, timeout: Option<Duration>) -> Self {
        self.0.multicast_dns.timeout = timeout;
        self
    }

    /// set_ice_multicast_dns_mode controls if ice queries and generates mDNS ICE Candidates
    pub fn with_multicast_dns_mode(mut self, multicast_dns_mode: MulticastDnsMode) -> Self {
        self.0.multicast_dns.mode = multicast_dns_mode;
        self
    }

    /// set_ice_multicast_dns_host_name sets a static HostName to be used by ice instead of generating one on startup
    /// This should only be used for a single PeerConnection. Having multiple PeerConnections with the same HostName will cause
    /// undefined behavior
    pub fn with_multicast_dns_local_name(mut self, local_name: String) -> Self {
        self.0.multicast_dns.local_name = local_name;
        self
    }

    /// Sets the local IP address to use for multicast DNS.
    pub fn with_multicast_dns_local_ip(mut self, local_ip: Option<IpAddr>) -> Self {
        self.0.multicast_dns.local_ip = local_ip;
        self
    }

    /// Sets static ICE credentials for reproducible sessions.
    ///
    /// By default, ICE generates random credentials (ufrag/password) for each
    /// session. Setting static credentials allows for signalless WebRTC or
    /// reproducible testing environments.
    ///
    /// # Parameters
    ///
    /// * `username_fragment` - ICE username fragment (ufrag)
    /// * `password` - ICE password
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Set static credentials for reproducible testing
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_ice_credentials(
    ///         "test_ufrag".to_string(),
    ///         "test_password".to_string(),
    ///     )
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Security Note
    ///
    /// Only use static credentials in controlled environments. Random credentials
    /// provide better security for production deployments.
    pub fn with_ice_credentials(mut self, username_fragment: String, password: String) -> Self {
        self.0.candidates.username_fragment = username_fragment;
        self.0.candidates.password = password;
        self
    }

    /// Disables DTLS certificate fingerprint verification.
    ///
    /// **Warning**: Disabling fingerprint verification removes a critical
    /// security check and should only be used for testing or debugging.
    ///
    /// # Parameters
    ///
    /// * `is_disabled` - `true` to disable verification, `false` to enable
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Only for testing/debugging!
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_disable_certificate_fingerprint_verification(true)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_disable_certificate_fingerprint_verification(mut self, is_disabled: bool) -> Self {
        self.0.disable_certificate_fingerprint_verification = is_disabled;
        self
    }

    /// Allows insecure signature verification algorithms.
    ///
    /// Some signature algorithms are known to be vulnerable or deprecated.
    /// This setting allows their use for compatibility with legacy systems.
    ///
    /// **Warning**: Only enable this if absolutely necessary for compatibility.
    ///
    /// # Parameters
    ///
    /// * `is_allowed` - `true` to allow insecure algorithms, `false` to disallow
    pub fn with_allow_insecure_verification_algorithm(mut self, is_allowed: bool) -> Self {
        self.0.allow_insecure_verification_algorithm = is_allowed;
        self
    }

    /// Sets the DTLS replay protection window size.
    ///
    /// The replay protection window prevents attackers from re-sending captured
    /// packets. Larger windows protect against more packet reordering but use
    /// more memory. Set to 0 to disable (not recommended).
    ///
    /// # Parameters
    ///
    /// * `n` - Window size in packets (0 = disabled)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Increase window for high-latency or reordering networks
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_dtls_replay_protection_window(128)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    ///
    /// - [RFC 6347 §4.1.2.6 - Anti-Replay](https://datatracker.ietf.org/doc/html/rfc6347#section-4.1.2.6)
    pub fn with_dtls_replay_protection_window(mut self, n: usize) -> Self {
        self.0.replay_protection.dtls = n;
        self
    }

    /// Sets the SRTP replay protection window size.
    ///
    /// SRTP replay protection prevents replay attacks on encrypted media packets.
    /// Adjust the window size based on expected packet reordering in your network.
    ///
    /// The default is 1024 packets. A NACK retransmission sent without RTX reuses the lost
    /// packet's sequence number and arrives after every packet sent since, so a window smaller
    /// than the packets sent during a few NACK rounds refuses late repairs as too old.
    ///
    /// # Parameters
    ///
    /// * `n` - Window size in packets (0 = disabled, not recommended)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // A wider window for a high packet rate
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_srtp_replay_protection_window(4096)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    ///
    /// - [RFC 3711 §3.3.2 - Replay Protection](https://datatracker.ietf.org/doc/html/rfc3711#section-3.3.2)
    pub fn with_srtp_replay_protection_window(mut self, n: usize) -> Self {
        self.0.replay_protection.srtp = n;
        self
    }

    /// Sets the SRTCP replay protection window size.
    ///
    /// SRTCP replay protection applies to RTCP control packets. Usually
    /// a smaller window is sufficient since RTCP packets are less frequent.
    ///
    /// # Parameters
    ///
    /// * `n` - Window size in packets (0 = disabled, not recommended)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Smaller window sufficient for RTCP
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_srtcp_replay_protection_window(64)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_srtcp_replay_protection_window(mut self, n: usize) -> Self {
        self.0.replay_protection.srtcp = n;
        self
    }

    /// Allows gathering of loopback candidates.
    ///
    /// By default, loopback candidates (127.x.x.x, ::1) are not gathered per
    /// RFC 8445. However, some VM configurations map public IPs to the loopback
    /// interface, making this necessary.
    ///
    /// # Parameters
    ///
    /// * `allow_loopback` - `true` to gather loopback candidates, `false` to skip
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Enable for certain VM configurations
    /// # #[allow(deprecated)]
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_include_loopback_candidate(true)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Note
    ///
    /// This is non-standard behavior per [RFC 8445 §5.1.1.1](https://datatracker.ietf.org/doc/html/rfc8445#section-5.1.1.1).
    /// Use with caution.
    ///
    /// # Not read by this engine
    ///
    /// The sans-I/O engine gathers no candidates (the application adds them), so this value is
    /// stored for API compatibility with pion and never read.
    #[deprecated(
        note = "a no-op: the sans-I/O engine gathers no candidates; add a loopback candidate \
                with `add_local_candidate` instead"
    )]
    pub fn with_include_loopback_candidate(mut self, allow_loopback: bool) -> Self {
        self.0.candidates.include_loopback_candidate = allow_loopback;
        self
    }

    /// Discards previously gathered local candidates when an ICE restart is applied.
    ///
    /// By default the restarted generation keeps its local candidates, which avoids re-gathering
    /// addresses that are usually still valid. [RFC 8445 §9] describes a restart as flushing all
    /// state except the roles and gathering anew, so keeping them is an optimisation rather than
    /// the specified behaviour — it is sound only while the underlying sockets outlive the
    /// restart.
    ///
    /// Set this when they do not. If the transport rebinds its sockets as part of recovery — for
    /// example an application that replaces its UDP sockets after the platform invalidated them —
    /// the retained candidates name addresses nothing is bound to any more. Connectivity checks
    /// are then written for a local address with no socket behind it and silently go nowhere, so
    /// the restarted generation exchanges credentials successfully and never leaves `Checking`.
    ///
    /// Only the previous generation's candidates are discarded. Those added while the restart
    /// is staged (an answerer's between `set_remote_description` of the restart offer and
    /// `set_local_description` of its answer, an offerer's between `create_offer` and its
    /// `set_local_description`) are the new generation's: the restart keeps them and the
    /// answer lists them (see [`RTCPeerConnection::add_local_candidate`](crate::peer_connection::RTCPeerConnection::add_local_candidate)).
    ///
    /// # Parameters
    ///
    /// * `discard` - `true` to drop local candidates on restart, `false` (default) to keep them
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Pairs with a transport that rebinds its sockets during ICE restart.
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_discard_local_candidates_during_ice_restart(true)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    ///
    /// - [RFC 8445 §9 - ICE Restarts](https://datatracker.ietf.org/doc/html/rfc8445#section-9)
    ///
    /// [RFC 8445 §9]: https://datatracker.ietf.org/doc/html/rfc8445#section-9
    pub fn with_discard_local_candidates_during_ice_restart(mut self, discard: bool) -> Self {
        self.0
            .candidates
            .discard_local_candidates_during_ice_restart = discard;
        self
    }

    /// Allows connectivity checks to loopback and / or link-local remote addresses.
    ///
    /// Remote candidates are chosen by the peer. By default a loopback remote candidate
    /// (`127.0.0.0/8`, `::1`) is paired only with a loopback local candidate, and a link-local
    /// one (`169.254.0.0/16`, `fe80::/10`, which includes cloud metadata services) only with a
    /// link-local local candidate, so a remote peer cannot make this host send checks to its
    /// own services or its link's metadata address. Candidates that are never a unicast
    /// destination (unspecified, multicast, broadcast, port 0) are dropped regardless
    /// ([RFC 8445 §5.1.1.1]).
    ///
    /// [RFC 8445 §5.1.1.1]: https://datatracker.ietf.org/doc/html/rfc8445#section-5.1.1.1
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// // Pair loopback remote candidates with any local candidate; keep link-local ones scoped.
    /// let _setting_engine = SettingEngineBuilder::new()
    ///     .with_remote_candidate_scopes(true, false)
    ///     .build();
    /// ```
    pub fn with_remote_candidate_scopes(mut self, loopback: bool, link_local: bool) -> Self {
        self.0.candidates.allow_loopback_remote_candidates = loopback;
        self.0.candidates.allow_link_local_remote_candidates = link_local;
        self
    }

    /// Whether the first mDNS query for a remote `.local` candidate waits a random 20 to 120
    /// ms (RFC 6762 section 5.2). On by default; `false` sends it at once.
    pub fn with_multicast_dns_first_query_delay(mut self, delay: bool) -> Self {
        self.0.multicast_dns.first_query_delay = Some(delay);
        self
    }

    /// Whether the mDNS answers to a query of more than one question (an A and an AAAA
    /// question for a published name, as browsers ask) wait a random 20 to 120 ms (RFC 6762
    /// section 6.3). On by default; `false` answers a query about the engine's own names only
    /// at once.
    pub fn with_multicast_dns_multi_question_delay(mut self, delay: bool) -> Self {
        self.0.multicast_dns.multi_question_delay = Some(delay);
        self
    }

    /// Declares that the application hands every datagram of its mDNS socket to `handle_read`
    /// with the datagram's destination address in `local_addr` (`IP_PKTINFO` /
    /// `IPV6_PKTINFO`): the mDNS group's (`224.0.0.251:5353`, `[ff02::fb]:5353`) for one that
    /// arrived by multicast, the host's own for one sent to it by unicast. A unicast arrival
    /// is then held to RFC 6762's rules for it (a query is answered by unicast and only to an
    /// on-link sender, section 5.5; a response is taken only when a recent question asked for
    /// a unicast response, section 6). Off by default.
    pub fn with_multicast_dns_destination_reported(mut self, reported: bool) -> Self {
        self.0.multicast_dns.destination_reported = reported;
        self
    }

    /// Makes the first mDNS query for a name ask for a unicast response (the QU bit, RFC 6762
    /// section 5.4; draft-ietf-mmusic-mdns-ice-candidates section 3.2.1). Off by default: a
    /// unicast response reaches only one of the sockets bound to port 5353 on a host (RFC 6762
    /// section 15.1). Use it with
    /// [`with_multicast_dns_destination_reported`](Self::with_multicast_dns_destination_reported)
    /// when the application's socket is the only one.
    pub fn with_multicast_dns_unicast_response(mut self, unicast: bool) -> Self {
        self.0.multicast_dns.unicast_response = unicast;
        self
    }

    /// Shares one limit on mDNS messages between peer connections
    /// (draft-ietf-mmusic-mdns-ice-candidates section 7.1: the limit is the implementation's).
    /// Give every peer connection of the process a clone of one `MdnsRateLimit`. Without it
    /// each peer connection has a limit of its own.
    pub fn with_multicast_dns_rate_limit(mut self, limit: ice::mdns::MdnsRateLimit) -> Self {
        self.0.multicast_dns.rate_limit = Some(limit);
        self
    }

    /// Fixes the ICE tiebreaker (RFC 8445 section 5.2), which is random otherwise. It decides
    /// who keeps the controlling role when both agents claim it (section 7.3.1.1): the larger
    /// one. For reproducible sessions and tests.
    pub fn with_ice_tie_breaker(mut self, tie_breaker: u64) -> Self {
        self.0.ice.tie_breaker = Some(tie_breaker);
        self
    }

    /// Which pair a controlled ICE agent uses when the controlling agent nominates more than
    /// one.
    ///
    /// The default, [`NominationAcceptance::HighestPriority`], is RFC 8445 section 8.1.1: the
    /// nominated pair with the highest priority (a nominated pair of lower priority takes over
    /// when consent is lost on the selected one). [`NominationAcceptance::Latest`] is
    /// libwebrtc's rule: the latest nomination wins (with its `NOMINATION` attribute, the
    /// highest value), whatever the pair's priority; the session then follows a browser that
    /// moves to another pair and stops answering on the one it left.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    /// use rtci::ice::NominationAcceptance;
    ///
    /// let _setting_engine = SettingEngineBuilder::new()
    ///     .with_ice_nomination_acceptance(NominationAcceptance::Latest)
    ///     .build();
    /// ```
    pub fn with_ice_nomination_acceptance(mut self, acceptance: NominationAcceptance) -> Self {
        self.0.ice.nomination_acceptance = acceptance;
        self
    }

    /// What the controlling ICE agent does when its nomination fails (RFC 8445 section
    /// 7.2.5.2: the transaction times out after `Rc` requests, 39.5 s at an RTO of 500 ms, or
    /// is answered with an error or from another address).
    ///
    /// The default, [`FailedNomination::FailChecklist`], is RFC 8445 section 7.2.5.3.4: the
    /// pair and the checklist fail, and with them the ICE transport. Under heavy loss a whole
    /// transaction's requests or responses can be lost while the peer has accepted the
    /// nomination; the peer then stays connected until its consent expires.
    /// [`FailedNomination::Retry`] keeps the pair (it has received a success response) and
    /// nominates it again while the peer is heard from on it, for up to the consent expiry of
    /// 30 seconds of silence; then the next valid pair is nominated.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    /// use rtci::ice::FailedNomination;
    ///
    /// let _setting_engine = SettingEngineBuilder::new()
    ///     .with_ice_failed_nomination(FailedNomination::Retry)
    ///     .build();
    /// ```
    pub fn with_ice_failed_nomination(mut self, failed_nomination: FailedNomination) -> Self {
        self.0.ice.failed_nomination = failed_nomination;
        self
    }

    /// Ta, the pacing of connectivity checks (RFC 8445 section 14.2), `None` for the default
    /// of 50 ms (never under 5 ms). A value other than the default is proposed to the peer as
    /// `a=ice-pacing` (RFC 8839 section 5.5) in every description, and both agents use the
    /// higher of the two proposals.
    pub fn with_ice_pacing(mut self, ta: Option<Duration>) -> Self {
        self.0.ice.pacing = ta;
        self
    }

    /// A pacing budget this peer connection's ICE agent shares with others' (RFC 8445 section
    /// 14.2: "all agents together send no more than one transaction per 5 ms"). An application
    /// that runs several peer connections gives each the same [`ice::PacingGroup`]; without one
    /// (the default) each agent paces by its own Ta only.
    pub fn with_ice_pacing_group(
        mut self,
        group: Option<std::sync::Arc<ice::PacingGroup>>,
    ) -> Self {
        self.0.ice.pacing_group = group;
        self
    }

    /// Makes the controlling ICE agent's consent checks carry USE-CANDIDATE, as libwebrtc's
    /// do. Off by default: RFC 8445 section 7.1.2 has the attribute sent "in order to nominate
    /// a candidate pair", once (section 8.1.1), and a consent check is a plain Binding request
    /// (RFC 7675 section 5.1). Without it the agent repeats its nomination only when the peer
    /// shows it lost it (a role conflict after the nomination).
    pub fn with_ice_nomination_on_consent_checks(mut self, enabled: bool) -> Self {
        self.0.ice.nominate_on_consent_checks = enabled;
        self
    }

    /// What a STUN Binding request that fails the ICE short-term credential check gets.
    ///
    /// The default, [`UnauthenticatedRequests::Reject`], is RFC 8489 section 9.1.3: 400 (Bad
    /// Request) without MESSAGE-INTEGRITY or USERNAME, 401 (Unauthenticated) for an unknown
    /// username or a MESSAGE-INTEGRITY that does not match. The responses are rate limited,
    /// never longer than the request, and unsigned. [`UnauthenticatedRequests::Drop`] answers
    /// nothing (what this crate did before).
    pub fn with_ice_unauthenticated_requests(mut self, requests: UnauthenticatedRequests) -> Self {
        self.0.ice.unauthenticated_requests = requests;
        self
    }

    /// Accepts data (DTLS, SRTP) only from the selected candidate pair's remote address.
    ///
    /// Off by default: RFC 8445 section 12.2 has an agent "prepared to receive data on any of
    /// the candidates provided in the most recent candidate exchange", and data is accepted
    /// from the remote candidate of any valid pair (one a connectivity check of this agent
    /// succeeded on). A peer that sends before it nominates, or from a pair it moved to, is
    /// then heard. Turning this on restores the fork's hardening: everything from another
    /// address is dropped before DTLS sees it.
    pub fn with_ice_data_from_selected_pair_only(mut self, enabled: bool) -> Self {
        self.0.ice.data_from_selected_pair_only = enabled;
        self
    }

    /// Lets data be sent on the best valid candidate pair before a pair is selected (RFC 8445
    /// section 12.1: "An ICE agent MAY send data on any valid pair before selected pairs have
    /// been produced"). Off by default. With it a DTLS server answers a ClientHello that
    /// arrives before the nomination does.
    pub fn with_ice_data_before_nomination(mut self, enabled: bool) -> Self {
        self.0.ice.send_data_before_nomination = enabled;
        self
    }

    /// Pairs a local candidate as soon as it is added, before the application has it to
    /// trickle. Off by default: RFC 8838 section 10 ("A Trickle ICE agent MUST NOT pair a local
    /// candidate until it has been trickled to the remote party"), so a local candidate is
    /// paired once the application has taken its `OnIceCandidateEvent` from
    /// [`poll_event`](crate::sansio::Protocol::poll_event), or once a local description is
    /// set (it lists the candidates gathered so far), whichever comes first. Turning this on
    /// restores the earlier behaviour: checks may leave on a candidate the peer has not
    /// heard of yet, which it then learns as peer-reflexive.
    pub fn with_ice_pairing_before_candidates_are_conveyed(mut self, enabled: bool) -> Self {
        self.0.ice.pair_before_conveyed = enabled;
        self
    }

    /// The SOFTWARE attribute (RFC 8489 section 14.14) of the STUN messages the peer
    /// connection sends: the ICE agent's connectivity checks (consent checks and nominations
    /// included) and its success responses to the peer's, the server-reflexive Binding
    /// requests the engine's gatherer sends to the configuration's `stun:` / `stuns:` servers,
    /// and the requests of the TURN clients the engine runs for its `turn:` / `turns:`
    /// servers, over UDP, TCP, TLS and DTLS (Allocate, Refresh, CreatePermission, ChannelBind,
    /// ConnectionBind).
    ///
    /// Without this call the ICE agent sends `rtci-ice` and its version
    /// ([`ice::agent::DEFAULT_SOFTWARE`]) and the TURN clients `rtci-turn` and its version
    /// ([`turn::client::DEFAULT_SOFTWARE`]): RFC 8489 sections 6.1 and 6.3.1.1, requests and
    /// responses "SHOULD" carry one. `Some(text)` sends `text` in all of them. `None` (or an
    /// empty text) sends no SOFTWARE attribute at all, for an application that must not put
    /// an implementation fingerprint on the wire (section 16.1.2: "Implementers SHOULD
    /// make usage of the SOFTWARE attribute a configurable option"). The gatherer's Binding
    /// requests carry the ICE agent's text. The agent's error responses, the consent checks it keeps up on the previous generation's path during a
    /// restart, keepalive indications and TURN Send indications carry none in any case.
    ///
    /// `text` is fewer than 128 characters and at most 509 bytes (section 14.14); building the
    /// peer connection with a longer one fails with `ErrAttributeSizeOverflow`.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// // No SOFTWARE attribute in any STUN message of the peer connection.
    /// let _setting_engine = SettingEngineBuilder::new().with_ice_software(None).build();
    /// ```
    pub fn with_ice_software(mut self, software: Option<String>) -> Self {
        self.0.ice.software = Some(software);
        self
    }

    /// [`Self::with_ice_software`] with "never called" expressible: `None` restores each
    /// stack's own SOFTWARE text, `Some(software)` is `with_ice_software(software)`. For the
    /// behaviour profiles, whose `Rfc` value is the uncalled state.
    pub(crate) fn ice_software_choice(mut self, software: Option<Option<String>>) -> Self {
        self.0.ice.software = software;
        self
    }

    /// Sets a filter asked about every remote candidate's address (an mDNS candidate's once it
    /// resolves) before the ICE agent holds it; `false` drops the candidate, so no check is ever
    /// sent there. Use it to apply the host's own network policy to peer-chosen destinations.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    /// use std::sync::Arc;
    ///
    /// // Drop every remote candidate on a loopback address.
    /// let _setting_engine = SettingEngineBuilder::new()
    ///     .with_remote_address_filter(Arc::new(|addr: std::net::SocketAddr| {
    ///         !addr.ip().is_loopback()
    ///     }))
    ///     .build();
    /// ```
    pub fn with_remote_address_filter(mut self, filter: RemoteAddressFilter) -> Self {
        self.0.candidates.remote_address_filter = Some(filter);
        self
    }

    /// Controls where DTLS fingerprints are placed in SDP.
    ///
    /// By default (`true`) they are in every media section, as RFC 9429 section 5.2.1 has it:
    /// "Attributes that SDP permits to be at either the session level or the media level
    /// SHOULD generally be at the media level even if they are identical" (browsers write
    /// them there too). `false` puts one at session level, which the same section allows
    /// ("implementations MAY choose to aggregate attributes at the session level").
    ///
    /// # Parameters
    ///
    /// * `sdp_media_level_fingerprints` - `true` for media-level, `false` for session-level
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // One fingerprint at session level
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_sdp_media_level_fingerprints(false)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    pub fn with_sdp_media_level_fingerprints(mut self, sdp_media_level_fingerprints: bool) -> Self {
        self.0.sdp_session_level_fingerprints = !sdp_media_level_fingerprints;
        self
    }

    /// Leaves the `a=tls-id` attribute (RFC 8842 section 4) out of offers and answers.
    ///
    /// By default every offer carries the DTLS association's `tls-id` on its first m-section
    /// (the BUNDLE tag), and an answer carries one when the offer did: the value stays the same
    /// while the association is kept and changes when a new one is asked for. Peers that do not
    /// know the attribute ignore it. Set `true` only for a peer that cannot take an unknown
    /// attribute. A received `a=tls-id` is read either way.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_disable_sdp_tls_id(true)
    ///     .build();
    /// ```
    pub fn with_disable_sdp_tls_id(mut self, is_disabled: bool) -> Self {
        self.0.disable_sdp_tls_id = is_disabled;
        self
    }

    /// Accepts a remote description that asks for a new DTLS association without an ICE
    /// restart.
    ///
    /// By default (`false`) a peer connection follows RFC 9429: a re-offer, or the answer to
    /// one, whose `a=tls-id` differs from the one in use "MUST be part of an ICE restart"
    /// (section 5.8.3), and an answer that changes the remote fingerprint or `tls-id`
    /// without an ICE restart is an error (section 5.11); `set_remote_description` refuses
    /// it. RFC 8842 section 6 asks the same over ICE ("at least one of the endpoints MUST
    /// allocate a completely new set of ICE candidates"). With `true` such a description
    /// establishes a new association on the ICE session it has (RFC 8842 section 3.1 alone),
    /// for a peer that replaces its DTLS identity on the same ICE session.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_dtls_new_association_without_ice_restart(true)
    ///     .build();
    /// ```
    pub fn with_dtls_new_association_without_ice_restart(mut self, allowed: bool) -> Self {
        self.0.dtls_new_association_without_ice_restart = allowed;
        self
    }

    /// Lets a new DTLS association run on the local transport addresses of the one it
    /// replaces.
    ///
    /// By default (`false`) this side allocates a new 3-tuple for every new DTLS association
    /// over UDP, as RFC 8842 section 5.1 asks of an offerer that needs one ("the offerer MUST
    /// allocate a new 3-tuple for the offer in such a way that the offerer can disambiguate
    /// any packets associated with the new DTLS association from any packets associated with
    /// any other DTLS association") and of an answerer when the offerer did not (section 6:
    /// "a completely new set of ICE candidates that were not recently used for any other DTLS
    /// association"). The ICE restart that comes with the new association
    /// (`renew_dtls_association`, a data-only association replaced for SRTP, or the answer to
    /// a remote offer that asks for one) discards the local
    /// candidates, even without
    /// [`with_discard_local_candidates_during_ice_restart`](Self::with_discard_local_candidates_during_ice_restart),
    /// and `add_local_candidate` refuses a UDP candidate on a transport address one of them
    /// had, with `ErrICECandidateAddressOfPreviousDtlsAssociation`: the application gathers on
    /// new ports. The engine-run TURN clients delete their allocations and allocate again.
    /// The addresses stay refused until the next ICE restart that is not for a new
    /// association.
    ///
    /// The answerer allocates whether or not the offerer did: with trickle ICE it cannot know
    /// when it answers.
    ///
    /// `true` keeps the candidates (unless
    /// `with_discard_local_candidates_during_ice_restart` is set) and accepts the previous
    /// addresses, for an application with one fixed socket whose peer allocates the new
    /// 3-tuple; packets of the previous association that arrive late then reach the new one,
    /// which drops them as records it cannot decrypt.
    ///
    /// A new association without an ICE restart
    /// ([`with_dtls_new_association_without_ice_restart`](Self::with_dtls_new_association_without_ice_restart))
    /// runs on the ICE session it has, whatever this setting says.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_dtls_new_association_on_previous_addresses(true)
    ///     .build();
    /// ```
    pub fn with_dtls_new_association_on_previous_addresses(mut self, allowed: bool) -> Self {
        self.0.dtls_new_association_on_previous_addresses = allowed;
        self
    }

    /// Ends a remote track when a later remote description lists no `a=msid` on its media
    /// section any more, while the section still sends.
    ///
    /// RFC 8830 section 3: "When a session description is updated to no longer list any
    /// 'msid' attribute on a specific media description, the recipient can signal to its
    /// application that the corresponding MediaStreamTrack has ended." In this API the W3C
    /// webrtc-pc object model governs the track's state (RFC 9429 defers to it), and its
    /// "process remote tracks" keeps the track, moved to the session's default stream; it is
    /// muted when the section stops sending. That is the default (`false`). With `true` the
    /// receiver's track ends ([`RTCTrackEvent::OnClose`](crate::peer_connection::event::RTCTrackEvent::OnClose)).
    /// A direction change never ends the track (RFC 8830 section 3).
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_msid_removal_ends_track(true)
    ///     .build();
    /// ```
    pub fn with_msid_removal_ends_track(mut self, ends: bool) -> Self {
        self.0.msid_removal_ends_track = ends;
        self
    }

    /// RFC 8830 section 3: "if the application ... receives an msid with a new msid-appdata
    /// value, it SHOULD \[learn\] that a new MediaStreamTrack has been added" (section 3, the
    /// recipient "MUST" learn of a new track for new appdata in the RFC's model). In this API
    /// the W3C webrtc-pc object model governs: a receiver has one track for its lifetime
    /// (webrtc-pc 5.3), with an id the engine generates, and RFC 9429 section 5.2.1 has
    /// senders leave the appdata out. That is the default (`false`): a changed appdata changes
    /// nothing the application sees. With `true`, a remote description whose section names
    /// another appdata than the one before ends the receiver's track
    /// ([`RTCTrackEvent::OnClose`](crate::peer_connection::event::RTCTrackEvent::OnClose)) and
    /// gives the receiver a new track, announced with a track event
    /// ([`RTCTrackEvent::OnRemoteTrack`](crate::peer_connection::event::RTCTrackEvent::OnRemoteTrack)).
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_msid_appdata_change_renews_track(true)
    ///     .build();
    /// ```
    pub fn with_msid_appdata_change_renews_track(mut self, renews: bool) -> Self {
        self.0.msid_appdata_change_renews_track = renews;
        self
    }

    /// Ends a remote track when all of its SSRCs have left: an RTCP BYE for each, or each
    /// timed out (RFC 3550 section 6.3.5, the RTCP session's `SourceTimedOut`).
    ///
    /// RFC 8830 section 3: "a MediaStreamTrack ... has ended when all of its SSRCs have ended
    /// (BYE, timeout)". In this API W3C webrtc section 9.3 governs the track's state: the
    /// track is muted when its source leaves and unmuted when media arrives again, and it ends
    /// only when its transceiver stops (a port of zero included). That is the default
    /// (`false`). With `true` the receiver's track ends
    /// ([`RTCTrackEvent::OnClose`](crate::peer_connection::event::RTCTrackEvent::OnClose))
    /// once every SSRC it receives has left; one that is heard again before then has not.
    pub fn with_source_departure_ends_track(mut self, ends: bool) -> Self {
        self.0.source_departure_ends_track = ends;
        self
    }

    /// Rejects, in an answer, the m= section of a transceiver that was stopped after the
    /// offer arrived.
    ///
    /// RFC 9429 section 5.3.1: an m= section "MUST be rejected ... if the associated
    /// RtpTransceiver has been stopped". W3C webrtc-pc 5.4 `stop()` puts such a transceiver
    /// in the "stopping" state, which "will not affect createAnswer" ("to avoid problems with
    /// RFC 8843"): the section is answered `a=inactive` and rejected by the next offer. That is
    /// the default (`false`), what browsers do. With `true` the answer rejects it (port 0)
    /// and the offerer stops its transceiver when it applies the answer; the offerer's
    /// BUNDLE-tagged section keeps the W3C form, since rejecting it would take the group's
    /// transport with it.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_stopping_transceiver_rejected_in_answer(true)
    ///     .build();
    /// ```
    pub fn with_stopping_transceiver_rejected_in_answer(mut self, rejected: bool) -> Self {
        self.0.stopping_transceiver_rejected_in_answer = rejected;
        self
    }

    /// Offers the DTLS `use_srtp` extension on every association, as browsers do (the
    /// default), or, with `false`, only on one that carries SRTP media.
    ///
    /// RFC 5764 section 4.1: "The use_srtp extension MUST only be used when the data being
    /// transported is RTP or RTCP". With `false` the engine follows it: as the DTLS client it
    /// offers `use_srtp` when the session has an audio or video section, and a
    /// data-channel-only session is set up without it; when such a session gains a media
    /// section later, a new DTLS association is established for it (the offer carries a new
    /// `a=tls-id`, RFC 8842). No browser acts on that `tls-id`: Chromium, Firefox and WebKit
    /// keep the first association, so the engine's new handshake never completes and neither
    /// the media nor the data channels flow afterwards (the browser matrix's "Rust offers
    /// media", "rollback" and "glare" cells, train round 8). The browsers' form is therefore
    /// the default, and the RFC form the setting, for peers that handle `tls-id` (this engine
    /// does). (As the DTLS server the engine takes a profile whenever the client offers one.)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_dtls_use_srtp_on_every_association(true)
    ///     .build();
    /// ```
    pub fn with_dtls_use_srtp_on_every_association(mut self, on_every_association: bool) -> Self {
        self.0.dtls_use_srtp_only_with_media = !on_every_association;
        self
    }

    /// Reads lower-case hex digits in a remote `a=fingerprint`, as browsers do.
    ///
    /// RFC 8122 section 5 defines the fingerprint as upper-case hex (`2UHEX *(":" 2UHEX)`), and
    /// by default a description with anything else is refused as malformed. Set `true` for a
    /// peer that writes lower-case digits.
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_lowercase_fingerprints_accepted(true)
    ///     .build();
    /// ```
    pub fn with_lowercase_fingerprints_accepted(mut self, is_accepted: bool) -> Self {
        self.0.accept_lowercase_fingerprints = is_accepted;
        self
    }

    /// Whether a remote description is refused when a bundled m= section carries
    /// `a=fingerprint` values other than its BUNDLE group's tagged section. The default is
    /// `false`.
    ///
    /// `a=fingerprint` is of the TRANSPORT multiplexing category (RFC 8859 section 4.5 and
    /// appendix A): "the attribute value MUST be taken from the m= line whose information is
    /// used to set up the underlying transport", the tagged section's (RFC 9143 section
    /// 7.1.3). By default the tagged section's fingerprints are the group's and the other
    /// bundled sections' are not used, whatever they say. `true` is a hardening beyond the
    /// RFC: every bundled section must agree with the tagged one (in the sha-256 values when
    /// there are any), as the sections outside a BUNDLE group, which share the transport,
    /// always must; otherwise the description is
    /// [`Error::ErrSessionDescriptionConflictingFingerprints`](shared::error::Error::ErrSessionDescriptionConflictingFingerprints).
    pub fn with_bundled_fingerprints_agreeing(mut self, agreeing: bool) -> Self {
        self.0.bundled_fingerprints_agree = agreeing;
        self
    }

    /// Whether a description has Cryptex for a BUNDLE group only when `a=cryptex` is at the
    /// session level or on every RTP-based m= section of the group. The default is `false`.
    ///
    /// RFC 9335 section 4 binds the writer: "If BUNDLE is in use ... and the 'a=cryptex'
    /// attribute is present for a media line, it MUST be present for all RTP-based 'm='
    /// sections belonging to the same bundle group", and this engine writes it so (at the
    /// session level). The same paragraph assigns the attribute to the TRANSPORT category
    /// (RFC 8859 section 4.5: "the software MUST pick the one that's associated with the 'm='
    /// line whose information is used for setting up the underlying transport"), and RFC 9143
    /// section 7.1.3 has an answerer and a later offerer write such attributes "only in the
    /// tagged 'm=' section". By default the reader therefore takes the group's Cryptex from
    /// its BUNDLE-tagged section, so a peer that follows RFC 9143 keeps Cryptex; a section
    /// outside any group decides by its own attribute.
    ///
    /// `true` reads strictly (the JSEP stack's earlier form): `a=cryptex` on some but not all
    /// RTP-based sections of a group, the tagged one alone included, does not negotiate
    /// Cryptex, and a `Require` policy refuses such a description.
    pub fn with_cryptex_required_on_every_bundled_section(mut self, required: bool) -> Self {
        self.0.cryptex_required_on_every_bundled_section = required;
        self
    }

    /// Whether answers and subsequent offers repeat the BUNDLE attributes (the IDENTICAL and
    /// TRANSPORT categories of RFC 8859: `a=ice-ufrag`, `a=ice-pwd`, `a=setup`, media-level
    /// `a=fingerprint`, `a=rtcp-mux`, `a=rtcp-mux-only`, `a=rtcp-rsize`, candidates) in every
    /// bundled m= section. The default is `true`.
    ///
    /// RFC 9143 section 7.1.3 (and RFC 9429 sections 5.2.2 and 5.3.1): "In a subsequent offer
    /// or in an answer ... the offerer and answerer MUST include IDENTICAL and TRANSPORT
    /// multiplexing category SDP attributes (BUNDLE attributes) only in the tagged 'm='
    /// section" and "MUST NOT include such attributes in any other bundled 'm=' section".
    /// `false` writes that form.
    ///
    /// It is not the default because no browser takes it: given an answer or a subsequent
    /// offer whose second bundled section has no `a=rtcp-mux` of its own, Chromium and WebKit
    /// refuse the description with "rtcp-mux must be enabled when BUNDLE is enabled" and
    /// Firefox with "m-section at level 1 is missing a=rtcp-mux, which is required by
    /// rtcpMuxPolicy" (browser matrix, 2026-11-22). Browsers themselves repeat the
    /// attributes.
    ///
    /// Either form is accepted from the remote side whatever this is set to: the tagged
    /// section's attributes apply to every bundled section.
    pub fn with_bundle_attributes_repeated(mut self, repeated: bool) -> Self {
        self.0.bundle_attributes_tagged_only = !repeated;
        self
    }

    /// Whether a remote offer that arrives while a local offer is pending rolls the local
    /// offer back and is applied, in one `set_remote_description` call. The default is `true`.
    ///
    /// W3C webrtc-pc section 4.4.2, the `setRemoteDescription` steps: a description of type
    /// "offer" that "is invalid for the current connection.\[\[SignalingState\]\] as described in
    /// \[RFC9429\]" has the local description set to `{type: "rollback"}` first, and is then
    /// set. This engine's
    /// `set_remote_description` is that operation, browsers perform it and "perfect
    /// negotiation" relies on it. RFC 9429 does not forbid it: its state machine (section
    /// 3.2) is kept, because the rollback is applied first (section 5.7) and the offer is
    /// then applied in `stable` (section 5.6).
    ///
    /// With `false` the layer above does the rollback: a remote offer in `have-local-offer`
    /// is refused, as RFC 9429 section 5.6 has it for a description whose type is not valid
    /// in the current state.
    ///
    /// An offer that is refused after the implicit rollback leaves the local offer pending,
    /// as if nothing had been called; one that is applied emits the `stable` and then the
    /// `have-remote-offer` signaling state.
    pub fn with_implicit_rollback(mut self, implicit_rollback: bool) -> Self {
        self.0.explicit_rollback_only = !implicit_rollback;
        self
    }

    /// Whether the bundle policy `max-bundle` is honoured as the policy RFC 9429 names
    /// `must-bundle`. The default is `true`.
    ///
    /// W3C webrtc-pc section 4.2.5 (`RTCBundlePolicy`) defines `max-bundle` ("Gather ICE
    /// candidates for only one track. If the remote endpoint is not bundle-aware, negotiate
    /// only one media track") and has no `must-bundle`; RFC 9429 section 4.1.1 introduces
    /// "an identically defined 'must-bundle' policy". So both names select the one policy:
    /// every m= section of an initial offer but the first is bundle-only.
    ///
    /// RFC 9429 section 4.1.1 also says "'max-bundle' is considered deprecated, and
    /// implementations compliant with this specification SHOULD ignore attempts by the
    /// application to select this bundle policy (although some phase-out period may be
    /// necessary to avoid application breakage)". `false` does that: a configuration with
    /// `RTCBundlePolicy::MaxBundle` then behaves as the default policy, `balanced`.
    pub fn with_max_bundle_as_must_bundle(mut self, honoured: bool) -> Self {
        self.0.max_bundle_ignored = !honoured;
        self
    }

    /// Whether each transport of the connection has ICE credentials of its own (RFC 9429
    /// section 5.2.1: "Each "m=" section, provided it is not marked as bundle-only, MUST
    /// contain a unique set of ICE credentials"). The default is `false`: the transports an
    /// offer or answer adds beside the first one (the first section of each media type in a
    /// `balanced` initial offer, every section under `max-compat`, every section answered
    /// outside a BUNDLE group) take the first transport's `ice-ufrag` and `ice-pwd`, and an ICE
    /// restart gives them all one new pair. Each transport keeps its own candidates, DTLS
    /// association and SRTP keys.
    ///
    /// The default is what Pion and webrtc-rs 0.17 can take, and RFC 8839 section 5.4 allows
    /// it ("If two data streams have identical "ice-ufrag"s, they MUST have identical
    /// "ice-pwd"s"): they refuse a description whose sections carry different credentials
    /// (`ErrSessionDescriptionConflictingIceUfrag`), bundled or not, so with `true` an initial
    /// `balanced` offer with audio and video, or media and a data channel, is refused by them.
    ///
    /// Chromium takes the RFC 9429 form, `true` (the `Chrome` behaviour profile's value):
    /// libwebrtc makes a transport description with credentials of its own for each m= section
    /// of an offer and of an answer (`TransportDescriptionFactory::CreateOffer` /
    /// `CreateAnswer`, `p2p/base/transport_description_factory.cc`) and makes the sections of a
    /// BUNDLE group share the first one's (`UpdateTransportInfoForBundle`,
    /// `pc/media_session.cc`). Its offers bundle every section whatever the bundle policy
    /// (`max-compat` included: `bundle_enabled` follows `use_rtp_mux`, `pc/sdp_offer_answer.cc`),
    /// so they show one pair; its answer to an offer without BUNDLE gives each section its own
    /// (fixture `chromium--balanced-rust-s-offer-has-a-transport-per-media-type-answered-without-bundle--1-answer.sdp`).
    ///
    /// With shared credentials the transports are told apart by their local addresses only:
    /// several transports on one local socket need `true`, since the ICE username of a STUN
    /// request is then the same for each.
    pub fn with_unique_transport_credentials(mut self, unique: bool) -> Self {
        self.0.unique_transport_credentials = unique;
        self
    }

    /// Where `a=ice-options` (RFC 8839 section 5.6) is written: once at session level (the
    /// default, `false`), or in each m= section that carries ICE credentials (`true`), after
    /// its `a=ice-pwd`. Section 5.6 allows either ("session-level or media-level"); the options
    /// are the same (`trickle ice2`, or `ice2` for a lite agent). Chromium writes them in every
    /// section (`BuildIceOptions` from `BuildMediaDescription`, `api/webrtc_sdp.cc`), the
    /// `Chrome` behaviour profile's value. Bundled sections that leave the BUNDLE attributes to
    /// the tagged one ([`Self::with_bundle_attributes_repeated`]`(false)`) leave this one too.
    pub fn with_ice_options_in_media_sections(mut self, in_media_sections: bool) -> Self {
        self.0.ice_options_in_media_sections = in_media_sections;
        self
    }

    /// Whether `a=ice-pacing` is written while Ta is the default 50 ms. The default is `true`:
    /// RFC 8839 section 4.3.1 says a full agent "SHOULD include an 'ice-pacing' attribute" in
    /// its offers and answers. With `false` no line is written for the default, which RFC 8445
    /// section 14.2 lets a peer read as the default ("If an agent does not ... indicate a
    /// value, the default value is used"); a Ta other than the default
    /// ([`Self::with_ice_pacing`]) is written either way ("MUST indicate the proposed value").
    /// libwebrtc writes no `a=ice-pacing` (`api/webrtc_sdp.cc` has none), the `Chrome`
    /// behaviour profile's value.
    pub fn with_default_ice_pacing_stated(mut self, stated: bool) -> Self {
        self.0.default_ice_pacing_omitted = !stated;
        self
    }

    /// How the first retransmission timer of the DTLS handshake is chosen; see
    /// [`DtlsFlightInterval`]. The default is [`DtlsFlightInterval::Rfc9147`]; the `Chrome`
    /// behaviour profile takes [`DtlsFlightInterval::Libwebrtc`]. RFC 9147 section 5.8.2's
    /// 1000 ms is a SHOULD "unless implementations have deployment-specific and/or external
    /// information about the round trip time", which ICE's measurement is.
    pub fn with_dtls_flight_interval(mut self, interval: DtlsFlightInterval) -> Self {
        self.0.dtls_flight_interval = interval;
        self
    }

    // SetICETCPMux enables ICE-TCP when set to a non-nil value. Make sure that
    // NetworkTypeTCP4 or NetworkTypeTCP6 is enabled as well.
    //pub fn SetICETCPMux(&mut self, tcpMux ice.TCPMux) {
    //    self.0.iceTCPMux = tcpMux
    //}

    // SetICEProxyDialer sets the proxy dialer interface based on golang.org/x/net/proxy.
    //pub fn SetICEProxyDialer(&mut self, d proxy.Dialer) {
    //    self.0.iceProxyDialer = d
    //}

    /// Prevents the MediaEngine from being copied for each PeerConnection.
    ///
    /// By default, each PeerConnection gets a copy of the MediaEngine, allowing
    /// independent codec configurations. Disabling this allows sharing a single
    /// MediaEngine and modifying it after PeerConnection creation.
    ///
    /// # Parameters
    ///
    /// * `is_disabled` - `true` to share MediaEngine, `false` to copy (default)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Share MediaEngine across connections
    /// # #[allow(deprecated)]
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_disable_media_engine_copy(true)
    ///     .build();
    ///
    /// // Warning: Don't share MediaEngine between multiple PeerConnections
    /// // unless you understand the implications
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Warning
    ///
    /// When disabled, ensure you don't share the same MediaEngine between
    /// multiple PeerConnections unless you specifically intend to do so.
    ///
    /// # Not read by this engine
    ///
    /// A PeerConnection takes its `MediaEngine` by value from the builder, so it always has
    /// its own; there is no shared engine to copy or not. This value is stored for API
    /// compatibility with pion and never read.
    #[deprecated(
        note = "a no-op: a peer connection always owns its MediaEngine (the builder takes it \
                by value), so there is no copy to disable"
    )]
    pub fn with_disable_media_engine_copy(mut self, is_disabled: bool) -> Self {
        self.0.disable_media_engine_copy = is_disabled;
        self
    }

    /// Disables negotiating different codecs for different media sections.
    ///
    /// By default, each media section in the SDP can negotiate different codecs,
    /// which is the spec-compliant behavior. This setting forces all media
    /// sections to use the same codecs.
    ///
    /// # Parameters
    ///
    /// * `is_disabled` - `true` to use single codec set, `false` for per-section (default)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Force same codecs for all media sections
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_disable_media_engine_multiple_codecs(true)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Deprecation Note
    ///
    /// This setting is targeted for removal in a future release (4.2.0 or later).
    ///
    /// # What it changes
    ///
    /// By default every remote description adds the formats of each of its audio and video
    /// sections to the codecs the connection has negotiated, so a second video section that
    /// offers only H.264 is answered with H.264 even when the first one negotiated VP8
    /// (RFC 9429, Section 5.10). A format is kept once, under the first payload type that
    /// named it. With `true`, only the first section of each kind (in the first remote
    /// description that has one) decides the codecs, and later sections are answered from
    /// that set.
    pub fn with_disable_media_engine_multiple_codecs(mut self, is_disabled: bool) -> Self {
        self.0.disable_media_engine_multiple_codecs = is_disabled;
        self
    }

    /// Whether an answer numbers the formats with this endpoint's own payload types (the ones
    /// the [`MediaEngine`](crate::peer_connection::configuration::media_engine::MediaEngine)
    /// registered them with) instead of the offer's. `false` by default.
    ///
    /// RFC 3264 section 6.1: "if a particular codec was referenced with a specific payload
    /// type number in the offer, that same payload type number SHOULD be used for that codec
    /// in the answer", which is the default. An answerer may number the formats otherwise
    /// (section 5.1: "the answer might indicate different payload type numbers for the same
    /// codecs, in which case, the offerer MUST send with the payload type numbers from the
    /// answer"; a gateway to a system with fixed numbers does), and that is `true`: each
    /// format of an offer this endpoint negotiates is answered under its registered payload
    /// type (the one a description of this endpoint already gave it, once it has one), RTX
    /// (`apt`) and RED (the encodings of its fmtp) following their primaries. The endpoint
    /// then receives with its own numbers and sends with the offer's (section 6.1: the
    /// answerer "MUST use the payload type numbers from the offer, even if they differ from
    /// those in the answer").
    ///
    /// Either way each direction of a media section follows the description that governs it:
    /// what is sent carries the remote description's payload types and what is received is
    /// read with the local description's (RFC 9429 sections 5.9 to 5.11).
    pub fn with_own_payload_types_in_answers(mut self, own: bool) -> Self {
        self.0.own_payload_types_in_answers = own;
        self
    }

    /// Sets the MTU size for the receive buffer.
    ///
    /// This controls the maximum size of packets that can be received. Leave
    /// at 0 to use the default MTU (1460 bytes, equal to UDP MTU).
    ///
    /// # Parameters
    ///
    /// * `receive_mtu` - MTU size in bytes, or 0 for default
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Use larger MTU for jumbo frames
    /// # #[allow(deprecated)]
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_receive_mtu(9000)
    ///     .build();
    ///
    /// // Or use default
    /// # #[allow(deprecated)]
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_receive_mtu(0)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Not read by this engine
    ///
    /// The sans-I/O engine reads no sockets: the application receives each datagram into its
    /// own buffer and hands it over whole, so there is no receive buffer to size. This value
    /// is stored for API compatibility with pion and never read.
    #[deprecated(
        note = "a no-op: the sans-I/O engine reads no sockets; size the application's own \
                receive buffer instead"
    )]
    pub fn with_receive_mtu(mut self, receive_mtu: usize) -> Self {
        self.0.receive_mtu = receive_mtu;
        self
    }

    /// Sets a custom MID (media stream ID) generator function.
    ///
    /// By default, MIDs are generated automatically. This allows you to provide
    /// a custom generation scheme, useful for reducing complexity when handling
    /// SDP offer/answer collisions.
    ///
    /// # Parameters
    ///
    /// * `f` - Function that takes the highest seen numeric MID and returns a new MID string
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Generate MIDs with a custom prefix
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_mid_generator(|max_mid| format!("custom_{}", max_mid + 1))
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Note
    ///
    /// - MIDs should be generated without leaking user information (e.g., randomly)
    /// - MIDs should be 3 bytes or less for efficient RTP header extension encoding
    /// - The `isize` argument is the greatest seen _numeric_ MID (doesn't include non-numeric MIDs)
    ///
    /// # See Also
    ///
    /// - [RFC 8843 - MID](https://datatracker.ietf.org/doc/html/rfc8843)
    ///
    /// `create_offer` calls it for each transceiver that has no mid yet, then moves the
    /// argument on (to the generated mid when it is a greater number, else by one), so two
    /// new transceivers of one offer get different arguments.
    pub fn with_mid_generator(
        mut self,
        f: impl Fn(isize) -> String + Send + Sync + 'static,
    ) -> Self {
        self.0.mid_generator = Some(Arc::new(f));
        self
    }

    /// Sets the maximum message size for SCTP data channels.
    ///
    /// This controls the largest message that can be sent through a data channel.
    /// Larger messages will be fragmented or rejected depending on the configuration.
    ///
    /// # Parameters
    ///
    /// * `max_message_size` - Maximum size (Bounded or Unbounded)
    ///
    /// # Examples
    ///
    /// ```
    /// use rtci::peer_connection::configuration::setting_engine::{
    ///     SctpMaxMessageSize, SettingEngineBuilder,
    /// };
    ///
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// // Use default 64KB
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_sctp_max_message_size(SctpMaxMessageSize::Bounded(
    ///         SctpMaxMessageSize::DEFAULT_MESSAGE_SIZE,
    ///     ))
    ///     .build();
    ///
    /// // Or allow larger messages
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_sctp_max_message_size(SctpMaxMessageSize::Bounded(256 * 1024)) // 256KB
    ///     .build();
    ///
    /// // Or unbounded (uses MAX_MESSAGE_SIZE internally)
    /// let setting_engine = SettingEngineBuilder::new()
    ///     .with_sctp_max_message_size(SctpMaxMessageSize::Unbounded)
    ///     .build();
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    ///
    /// - [RFC 8841 §6.1 - max-message-size](https://datatracker.ietf.org/doc/html/rfc8841#section-6.1)
    pub fn with_sctp_max_message_size(mut self, max_message_size: SctpMaxMessageSize) -> Self {
        self.0.sctp_max_message_size = max_message_size;
        self
    }

    /// Overrides the SCTP receive-buffer size (the a_rwnd flow-control window), in bytes.
    ///
    /// This bounds how much unacknowledged data a peer may have in flight toward this
    /// endpoint — a bandwidth-delay-product ceiling. Lowering it reduces per-connection
    /// memory (the buffer fills only under load), which matters for servers holding many
    /// connections; but a smaller window can throttle throughput on high-latency,
    /// high-bandwidth paths, where more data must be in flight to keep the pipe full.
    ///
    /// **Bounds.** RFC 9260 §3.3.2 requires an advertised initial a_rwnd of at least **1500
    /// bytes**; smaller values (including `0`) are raised to that floor here, because a
    /// sub-1500 window makes the peer reject this endpoint's INIT/INIT-ACK and the SCTP
    /// association never establishes. The window should also be **≥ the largest SCTP
    /// message this endpoint will receive** ([`SettingEngineBuilder::with_sctp_max_message_size`], default
    /// 64 KiB): a buffer smaller than one message cannot hold it for reassembly, so a
    /// full-size inbound message would stall that receive direction. `0` here is *not*
    /// "unbounded" (unlike some other knobs) — to keep the default window, leave this
    /// unset (the default is `INITIAL_RECV_BUF_SIZE`, 1 MiB).
    pub fn with_sctp_max_receive_buffer_size(mut self, size: u32) -> Self {
        // RFC 9260 §3.3.2 (INIT) and §3.3.3 (INIT ACK) forbid advertising an initial a_rwnd
        // below 1500 bytes ("The Advertised Receiver Window Credit MUST NOT be smaller than
        // 1500. A receiver of an INIT chunk with the a_rwnd value set to a value smaller than
        // 1500 MUST discard the packet [and] SHOULD send [...] an ABORT chunk."). This crate
        // enforces it in `ChunkInit::check()` (ErrInitAdvertisedReceiver1500), so a
        // sub-1500 (or 0) value would silently break the handshake. Clamp up to that floor.
        const MIN_SCTP_RECEIVE_BUFFER_SIZE: u32 = 1500;
        if size < MIN_SCTP_RECEIVE_BUFFER_SIZE {
            log::warn!(
                "sctp receive buffer size {size} is below the RFC 9260 minimum; raising to \
                 {MIN_SCTP_RECEIVE_BUFFER_SIZE} bytes"
            );
        }

        self.0.sctp_max_receive_buffer_size = Some(size.max(MIN_SCTP_RECEIVE_BUFFER_SIZE));
        self
    }

    /// Overrides the SCTP MTU: the size budget, in bytes, for each outbound DATA packet
    /// (common header plus bundled DATA chunks) this endpoint emits. A SACK keeps within it
    /// too, carrying the gap blocks and duplicate TSNs that fit. Other control packets
    /// (INIT, INIT ACK, RECONFIG, FORWARD TSN, shutdown) marshal without consulting this value.
    ///
    /// Each SCTP packet becomes one DTLS record carried in one UDP datagram, so the wire
    /// size is this value plus the DTLS record overhead (13 bytes of header and what the
    /// cipher suite adds: 24 bytes with AES-GCM, 16 with ChaCha20-Poly1305), the UDP and IP
    /// headers, and 4 bytes of TURN ChannelData framing when relayed.
    ///
    /// Left unset, the budget follows the path: RFC 8831 section 5 says the initial path MTU
    /// at the IP layer "SHOULD NOT exceed 1200 bytes for IPv4 and 1280 bytes for IPv6", so an
    /// SCTP packet starts as what is left of that under the headers of the selected candidate pair and the negotiated DTLS cipher suite
    /// (1135 bytes over IPv4 and UDP with AES-GCM, 1195 over IPv6), recomputed when ICE
    /// selects another pair. Until a pair is selected it is 1191 (the TURN-relayed IPv6
    /// budget, webrtc-rs/rtc#178). Set this only for paths with a known different budget:
    /// raise it where the path genuinely carries more (fewer, larger packets cut per-packet
    /// overhead), lower it for tunnels with an even smaller MTU. A value set here is used as
    /// it is on every path, and path MTU discovery
    /// ([`with_sctp_path_mtu_discovery`](Self::with_sctp_path_mtu_discovery)), which grows the
    /// packets from the initial size otherwise, is off.
    ///
    /// The per-chunk payload budget is derived from this value by the inverse of the
    /// default derivation (minus the 12-byte common header and 16-byte DATA chunk header,
    /// rounded down to the SCTP 4-byte chunk-padding boundary), so a single maximum-size
    /// DATA chunk always marshals within this budget. Values below 32 — the smallest
    /// representable padded DATA packet — are raised to 32 with a warning.
    pub fn with_sctp_mtu(mut self, mtu: u32) -> Self {
        self.0.sctp_mtu = Some(mtu);
        self
    }

    /// SCTP path MTU discovery (RFC 8261 section 4, RFC 8831 section 5: SCTP over DTLS
    /// "MUST" discover the path MTU without ICMP; RFC 8899 section 6.2.3 is the method).
    /// Once the association is established it confirms the initial packet size (RFC 8831
    /// section 5's 1200 / 1280-byte IP packet under the headers of the selected pair and
    /// the DTLS record) with a probe, a HEARTBEAT and a PAD chunk (RFC 4820) of exactly the
    /// size tried, then searches for larger packets up to `max_ip_packet` bytes at the IP
    /// layer (the local interface MTU; 1500, Ethernet's, by default), comes back to the
    /// initial size when the path stops carrying the larger one, and searches again every
    /// 600 seconds and after ICE selects another pair. Probes carry no data and are lost
    /// without consequence where the path is smaller. `None` turns it off: the packet size
    /// then stays the initial one. A fixed [`with_sctp_mtu`](Self::with_sctp_mtu) turns it
    /// off too.
    pub fn with_sctp_path_mtu_discovery(mut self, max_ip_packet: Option<u32>) -> Self {
        self.0.sctp_path_mtu_discovery = Some(max_ip_packet);
        self
    }

    /// Whether SCTP sends from its data channels by weighted fair queueing (RFC 8260 section
    /// 3.6), each channel's share of the bytes in proportion to its priority. On by default:
    /// RFC 8831 section 6.4 says the priorities of data channels "MUST be interpreted as
    /// weighted-fair-queuing scheduling priorities per the definition of the corresponding
    /// stream scheduler supporting interleaving in \[RFC8260\]". `false` sends the messages in
    /// the order they were written, whatever their channel, and the priority of a channel is
    /// then carried to the peer and not otherwise used.
    pub fn with_sctp_weighted_fair_queueing(mut self, enabled: bool) -> Self {
        self.0.sctp_weighted_fair_queueing = Some(enabled);
        self
    }

    /// Whether SCTP announces user message interleaving (RFC 8260). On by default: RFC 8831
    /// sections 6.1 and 6.6 say it "SHOULD be used", so that a large message on one data
    /// channel does not hold up the others. It is used when the peer announces it too;
    /// otherwise the association sends DATA chunks, and nothing else changes. `false` does
    /// not announce it, which is what a browser that has not turned it on does.
    pub fn with_sctp_message_interleaving(mut self, enabled: bool) -> Self {
        self.0.sctp_message_interleaving = Some(enabled);
        self
    }

    /// Whether SCTP announces that it accepts packets with a zero checksum (RFC 9653) and
    /// sends them to a peer that announces the same. On by default: the association runs over
    /// DTLS, whose record protection detects errors better than the CRC32c (RFC 9653 section
    /// 6, "SCTP over DTLS"), and section 5.2 says an endpoint "SHOULD use zero as the checksum"
    /// then. A peer that does not announce it gets a CRC32c on every packet, and must send
    /// one. `false` does not announce it.
    pub fn with_sctp_zero_checksum(mut self, enabled: bool) -> Self {
        self.0.sctp_zero_checksum = Some(enabled);
        self
    }

    /// The number of SCTP streams this endpoint asks for and accepts in each direction
    /// (the OS and MIS of its INIT or INIT ACK, RFC 9260 section 3.3.2). The association has
    /// the smaller of both sides' counts, which is W3C `RTCSctpTransport.maxChannels`
    /// (webrtc-pc 6.1.1): a data channel id at or above it cannot be used. Unset, the default,
    /// is 65535, as browsers ask for; a value of 0 is raised to 1.
    pub fn with_sctp_max_streams(mut self, streams: u16) -> Self {
        self.0.sctp_max_streams = Some(streams.max(1));
        self
    }

    /// The most bytes of incomplete inbound messages a data channel association holds above
    /// SCTP. SCTP gives a message it cannot hold whole out in parts (RFC 9260 section 6.9),
    /// so its receive window reopens while the message is still arriving; the data channel
    /// layer joins the parts, because a data channel delivers whole messages only.
    ///
    /// What a conforming peer can have in progress is bounded by the negotiated parameters:
    /// at most one message per stream (RFC 8260 section 2.2.2, RFC 6458 section 3.1.4), each
    /// at most the `max-message-size` this side advertised (RFC 8841 section 6.1; a larger one
    /// is dropped, as before). The handler holds at most one incomplete message per stream,
    /// so what it holds never exceeds (streams with a message in progress) ×
    /// `max-message-size`. This setting is the ceiling over that product: a peer that would
    /// take the association past it loses the data channel whose part would go over (W3C
    /// `OnError` with `data-channel-failure`, the message naming this limit; RFC 8831
    /// section 6.6: "the receiver SHOULD close the corresponding data channel"). The rest of
    /// that message is read and dropped, then the channel's stream is reset; the other
    /// channels go on.
    ///
    /// Unset, the default, is 64 MiB: 1024 concurrent messages of the default 64 KiB
    /// `max-message-size`, or 256 of the largest this engine accepts (256 KiB), far more
    /// than ordinary use keeps in flight at once, and a quarter of what a peer could pin
    /// with 65535 streams of 1 KiB each. The worst case one association holds for inbound
    /// data is then its SCTP receive buffer
    /// ([`with_sctp_max_receive_buffer_size`](Self::with_sctp_max_receive_buffer_size), 1 MiB
    /// by default) plus this limit: 65 MiB with the defaults. A value below the advertised
    /// `max-message-size` is raised to it, so that a single message that size always fits.
    pub fn with_sctp_inbound_reassembly_limit(mut self, bytes: usize) -> Self {
        self.0.sctp_inbound_reassembly_limit = Some(bytes);
        self
    }

    /// RTO.Initial, RTO.Min and RTO.Max of the SCTP association (RFC 9260 sections 6.3.1 and
    /// 16): the retransmission timeout before a round trip is measured, and its bounds. Unset,
    /// the default, is the RFC's recommendation: 1, 1 and 60 seconds. A minimum or maximum of
    /// zero is raised to a millisecond, a maximum below the minimum to the minimum.
    ///
    /// With [`Self::with_sctp_max_retransmissions`] this bounds how long the association keeps
    /// retransmitting to a peer that does not answer before it fails (W3C webrtc-pc 6.2: the
    /// data channels then fail with `sctp-failure`). At the defaults that is minutes, longer
    /// than ICE consent (30 seconds) takes to fail the path: an application that wants SCTP
    /// itself to report an unreachable peer sooner (a path ICE still thinks alive) lowers it.
    pub fn with_sctp_rto(mut self, initial: Duration, min: Duration, max: Duration) -> Self {
        self.0.sctp_rto = Some(sctp::RtoConfig { initial, min, max });
        self
    }

    /// Back to RFC 9260's RTO.Initial, RTO.Min and RTO.Max (1, 1 and 60 seconds), undoing
    /// [`Self::with_sctp_rto`] (or a profile that set them).
    pub fn without_sctp_rto(mut self) -> Self {
        self.0.sctp_rto = None;
        self
    }

    /// Association.Max.Retrans (RFC 9260 sections 8.1 and 16): how many consecutive
    /// retransmission timeouts (an unanswered HEARTBEAT counts as one) the SCTP association
    /// takes before it declares the peer unreachable and fails. Unset, the default, is the
    /// RFC's 10. See [`Self::with_sctp_rto`]. `usize::MAX` never gives up, which leaves the
    /// failure of an unreachable peer to ICE consent (RFC 7675): what libwebrtc does
    /// (`DcSctpTransport::CreateDcSctpOptions` sets `max_retransmissions` to none,
    /// `media/sctp/dcsctp_transport.cc`), the `Chrome` behaviour profile's value.
    pub fn with_sctp_max_retransmissions(mut self, limit: usize) -> Self {
        self.0.sctp_max_retransmissions = Some(limit);
        self
    }

    /// Back to RFC 9260's Association.Max.Retrans of 10, undoing
    /// [`Self::with_sctp_max_retransmissions`] (or a profile that set it).
    pub fn without_sctp_max_retransmissions(mut self) -> Self {
        self.0.sctp_max_retransmissions = None;
        self
    }

    /// Whether a message over 16 KB is refused when the SCTP association does not interleave
    /// user messages. On by default: RFC 8831 section 6.6 says "As long as message
    /// interleaving is not supported, the sender SHOULD limit the maximum message size to
    /// 16 KB to avoid monopolization", since such a message holds up every other data channel
    /// until all of it has been sent. With the limit,
    /// [`RTCSctpTransport::max_message_size`](crate::peer_connection::transport::RTCSctpTransport::max_message_size)
    /// reports at most 16384 once the association is up without interleaving, and `send`
    /// refuses a larger message. Between two endpoints that both interleave (two of this
    /// engine, by default) the limit does not apply.
    ///
    /// `false` is what browsers do: they send messages up to the peer's `max-message-size`
    /// whether or not the association interleaves, and current browsers do not announce
    /// interleaving. An application that sends more than 16 KB at a time to a browser sets
    /// this to `false`.
    pub fn with_sctp_message_size_limit_without_interleaving(mut self, enabled: bool) -> Self {
        self.0.sctp_message_size_limit_without_interleaving = Some(enabled);
        self
    }

    /// Replaces the SCTP transport's `[[MaxBufferSize]]` with a fixed limit: `send()` refuses
    /// data that would take a channel's `bufferedAmount` above `limit` bytes
    /// (`ErrDataChannelBufferFull`). `None`, the default, is the W3C rule (webrtc-pc 6.1.1.2):
    /// the larger of the peer's `max-message-size` and this side's send buffer, 16 MiB (the
    /// spec leaves the send buffer's size to the implementation; 16 MiB is Chromium's).
    ///
    /// A smaller limit makes `send()` refuse sooner; an application that keeps
    /// `bufferedAmount` low by `bufferedAmountLowThreshold` needs none.
    pub fn with_data_channel_max_buffer_size(mut self, limit: Option<u32>) -> Self {
        self.0.data_channel_max_buffer_size = limit;
        self
    }

    /// Whether `send()` accepts data on an in-band data channel that is still "connecting",
    /// waiting for the DATA_CHANNEL_ACK of the DATA_CHANNEL_OPEN this side sent. Off by
    /// default: W3C webrtc-pc 6.2 `send()` says "If channel.\[\[ReadyState\]\] is not "open",
    /// throw an InvalidStateError", and refuses with [`Error::ErrDataChannelNotOpen`].
    ///
    /// `true` is RFC 8832 section 6: "the peer that sent the DATA_CHANNEL_OPEN message MAY
    /// start sending messages containing user data without waiting for the reception of the
    /// corresponding DATA_CHANNEL_ACK message". The data goes out ordered behind the OPEN, and
    /// a peer that implements RFC 8832 (any browser, this engine) takes it as the ACK would.
    ///
    /// [`Error::ErrDataChannelNotOpen`]: shared::error::Error::ErrDataChannelNotOpen
    pub fn with_data_channel_send_before_ack(mut self, enabled: bool) -> Self {
        self.0.data_channel_send_before_ack = enabled;
        self
    }

    /// Marks video as non-interactive for [`RTCPeerConnection::dscp`]: the AF3x codepoints of
    /// RFC 8837 Table 1 instead of the AF4x ones. RFC 8837 section 5: "Non-browser
    /// implementations of WebRTC MAY use the AF3x DSCP values for video that is known not to
    /// be interactive, e.g., all video in a WebRTC video playback application". The default
    /// is `false`: all WebRTC video is interactive.
    ///
    /// [`RTCPeerConnection::dscp`]: crate::peer_connection::RTCPeerConnection::dscp
    pub fn with_non_interactive_video(mut self, non_interactive: bool) -> Self {
        self.0.non_interactive_video = non_interactive;
        self
    }

    /// Sets how many packets a DTLS-SRTP master key may protect. `None`, the default, is the
    /// profile's: 2^31 for the RFC 5764 profiles (section 4.1.2), 2^48 SRTP and 2^31 SRTCP
    /// packets for the AEAD ones (RFC 7714). When a key has a sixteenth of its lifetime left
    /// the connection raises `OnSrtpKeyLifetimeExpiring` and, unless
    /// [`Self::with_srtp_automatic_rekeying`] turned it off, asks for a new DTLS association
    /// (RFC 5764 section 4.4); at the limit nothing more is protected with the key and the
    /// DTLS transport fails (`dtls-failure`, RFC 3711 section 9.2: re-key, or end the session).
    ///
    /// The re-keying has the last sixteenth of the lifetime to complete: an offer and its
    /// answer through the application's signalling, ICE checks on new candidates and a DTLS
    /// handshake, which takes seconds on a relayed path or with loss (the DTLS retransmission
    /// timer starts at 1 s). A lifetime short enough that a sixteenth of it lasts less than
    /// that at the stream's packet rate ends the session instead (a 9600-packet lifetime at 188
    /// packets a second leaves 3.2 s); the connection logs a warning when the margin it has is
    /// under 2 seconds. Lifetimes shorter than the profile's are for tests and constrained
    /// deployments; size them as `16 x packet rate x re-keying time` at least.
    ///
    /// A lifetime of zero, or above the RFC 3711 limits, makes building the peer connection
    /// fail.
    pub fn with_srtp_key_lifetime(mut self, lifetime: Option<srtp::keying::KeyLifetime>) -> Self {
        self.0.srtp_key_lifetime = lifetime;
        self
    }

    /// Asks the peer to put a connection id of `length` random bytes in every protected DTLS
    /// record it sends (RFC 9146 for DTLS 1.2, RFC 9147 section 9 for DTLS 1.3). `None`, the
    /// default, sends the `connection_id` extension asking for none (RFC 9147 section 5.1).
    ///
    /// With connection ids in use the engine follows RFC 9147 sections 9 and 11: when the ICE
    /// agent's selected pair changes (a new path), a DTLS 1.3 association switches to a fresh
    /// connection id the peer provided and asks for another, so records on the new path are
    /// not linked to the old ones by their id.
    pub fn with_dtls_connection_id(mut self, length: Option<usize>) -> Self {
        self.0.dtls_connection_id_length = length;
        self
    }

    /// Whether the DTLS handshake carries Application-Layer Protocol Negotiation with the
    /// WebRTC identifiers of RFC 8833 (RFC 8835 section 3.5). The default is `true`: as the
    /// DTLS client the engine offers `webrtc` and `c-webrtc` (section 2: "A peer in the client
    /// role MUST offer both identifiers if it is not aware of a need for confidentiality"),
    /// as the server it selects `webrtc`. A peer that sends or answers no ALPN connects all
    /// the same, taken as `webrtc` (section 2). The engine never selects `c-webrtc` as the
    /// server: it cannot promise the media confidentiality that identifier stands for (a
    /// client that requires it is refused with `no_application_protocol`, and section 2 has
    /// such a client abort anyway).
    /// [`RTCPeerConnection::dtls_application_protocol`](crate::peer_connection::RTCPeerConnection::dtls_application_protocol)
    /// gives what was selected. `false` leaves the extension out.
    pub fn with_dtls_alpn(mut self, alpn: bool) -> Self {
        self.0.dtls_alpn_off = !alpn;
        self
    }

    /// Whether the connection re-keys SRTP by itself before a master key's lifetime ends. The
    /// default is `true`.
    ///
    /// RFC 5764 section 4.4: "Once the key lifetime has been reached, a new DTLS session SHOULD
    /// be established" (with section 4.1.2: the keys come from the association). When a key has
    /// a sixteenth of its lifetime left (`OnSrtpKeyLifetimeExpiring`, either direction), the
    /// connection does what [`RTCPeerConnection::renew_dtls_association`] does: the
    /// negotiation-needed flag is raised, and the next offer carries a new `a=tls-id` and new
    /// ICE credentials (RFC 9429 section 5.8.3: a new association is part of an ICE restart).
    /// Media goes on under the old keys until the new handshake hands over the new ones; the
    /// SCTP association and its data channels go on over the new association (RFC 8841
    /// section 10.5). The application only has to answer `negotiationneeded` as usual.
    ///
    /// `false` leaves it to the application: it gets the event only, and at the limit the
    /// DTLS transport fails. A browser peer ignores a new `tls-id` and keeps its association,
    /// so with one the renewal does not re-key and the transport fails at the limit either way;
    /// the default lifetimes (2^31 packets and more) are not reached by a call.
    ///
    /// [`RTCPeerConnection::renew_dtls_association`]: crate::peer_connection::RTCPeerConnection::renew_dtls_association
    pub fn with_srtp_automatic_rekeying(mut self, enabled: bool) -> Self {
        self.0.srtp_automatic_rekeying_off = !enabled;
        self
    }

    /// Which side starts the automatic SRTP re-keying ([`Self::with_srtp_automatic_rekeying`])
    /// when both could. The default is `true`: the DTLS client of the current association asks
    /// for the new association at the lifetime warning (a sixteenth of a key's lifetime left);
    /// the DTLS server waits a little first, for the client's offer: at most 500 ms, and at
    /// most an eighth of the time the keys have left at the rate they are used. If no new
    /// association has come by then (or a thirty-second of the lifetime is left, if that comes
    /// first), it asks itself.
    ///
    /// Two peers with the same key lifetime reach the warning at about the same packet count:
    /// if both asked at once, their re-keying offers would cross (glare), which RFC 9429
    /// section 4.1.10 has resolved by a rollback (this engine does it), at the cost of an offer
    /// made for nothing. With the short wait the client's offer usually reaches the server
    /// first and the server answers it. Lifetimes are local: the server cannot know that the
    /// client re-keys at all (a browser does not, nor an engine with re-keying off or a longer
    /// lifetime), so the wait is bounded in time and never takes more than an eighth of the
    /// margin from a re-keying that has to cross a slow path. Without packets to measure the
    /// rate by, the server does not wait.
    ///
    /// `false`: both sides ask at the warning.
    pub fn with_srtp_rekeying_dtls_client_first(mut self, client_first: bool) -> Self {
        self.0.srtp_rekeying_both_at_warning = !client_first;
        self
    }

    /// Writes the pause mark `~` of `a=simulcast` (RFC 8853 section 5.2) although RTP stream
    /// pause and resume (RFC 7728) is not negotiated: for an inactive encoding of a sender,
    /// and for a stream the offer being answered marks. The default is `false`.
    ///
    /// RFC 8853 section 5.2: "The use of [the pause mark] MUST be accompanied by support
    /// for RTP stream pause/resume \[RFC7728\] ... signaled under the same 'm=' line", and
    /// section 5.3.2: an answerer "MUST NOT" mark a stream paused when the offer shows no
    /// such support. The engine does not implement RFC 7728 and never signals it, so by
    /// default it writes no `~`: an inactive encoding is listed and not sent, and an offered
    /// `~` is answered without the mark. `true` writes the mark all the same, as libwebrtc
    /// does for inactive layers.
    pub fn with_rid_pause_marks(mut self, rid_pause_marks: bool) -> Self {
        self.0.rid_pause_marks = rid_pause_marks;
        self
    }

    /// Ignores the SDP of a `rollback` description instead of refusing one that has any. The
    /// default is `false`.
    ///
    /// RFC 9429 section 4.1.10: a rollback's "contents MUST be empty", so by default
    /// `set_local_description` / `set_remote_description` refuse a rollback whose SDP is not
    /// empty (whitespace aside) and change nothing. W3C webrtc-pc `RTCSessionDescriptionInit`
    /// says the `sdp` member "is unused" for a rollback: `true` takes that form and rolls back
    /// whatever the SDP holds.
    pub fn with_rollback_content_ignored(mut self, ignored: bool) -> Self {
        self.0.rollback_content_ignored = ignored;
        self
    }

    /// Checks each attribute value of a remote description against its grammar exactly,
    /// trailing whitespace included. The default is `false`.
    ///
    /// RFC 9429 section 5.8: a line that is not well formed rejects the description, even
    /// when its value is then ignored; every attribute is checked against its grammar either
    /// way. By default white space at the end of a value is not held against it: webrtc-rs
    /// 0.17.x writes a feedback without parameters as `a=rtcp-fb:96 goog-remb ` (a trailing
    /// space, which RFC 4585 section 4.2's `rtcp-fb-param` does not allow: a space must be
    /// followed by a token), and the strict reading refuses it
    /// (`tests/media_rejection_interop.rs::test_video_only_webrtc_offerer_rtc_answerer`:
    /// "a=rtcp-fb: `96 goog-remb `"), so an engine that reads them strictly cannot answer
    /// its offers. webrtc-rs fixed it on its default branch (`rtci` commit 718d405), as Pion
    /// did in v4.1.5 (pion/webrtc#3207, PR #3218); 0.17.x still writes it. `true` is the
    /// strict RFC form.
    pub fn with_strict_sdp_attribute_grammar(mut self, strict: bool) -> Self {
        self.0.strict_sdp_attribute_grammar = strict;
        self
    }

    /// Answers simulcast as W3C webrtc-pc `createAnswer` (section 4.4.2) does, instead of
    /// RFC 8853 / RFC 8851. The default is `false`.
    ///
    /// - A remote `a=simulcast` line that names a rid-id more than once is taken with each
    ///   rid-id at its first occurrence (W3C: "identically named rids are reduced to the
    ///   first"); by default it refuses the description (RFC 8853 section 5.2: "a single rid-id
    ///   MUST NOT occur more than once per 'a=simulcast' line"; RFC 9429 section 5.8).
    /// - The `a=rid` lines of an answer carry no restrictions (W3C sets none); by default they
    ///   keep the offered ones (RFC 8851 section 6.3).
    pub fn with_w3c_simulcast_answer(mut self, w3c: bool) -> Self {
        self.0.w3c_simulcast_answer = w3c;
        self
    }

    /// Whether each `a=msid` line carries the sender's track id as its `appdata`
    /// (`a=msid:<stream id> <track id>`, RFC 8830 section 2), as browsers write it. The
    /// default is `true`: webrtc-rs 0.17 (and Pion, whose track id it is) drops a simulcast
    /// (rid) section whose `a=msid` has no appdata, as that section has no `a=ssrc` lines to
    /// name the track (`simulcast_rtc_to_webrtc_interop`), so the interoperable form is the
    /// default. `false` is RFC 9429 section 5.2.1 ("an "a=msid" line ... but omitting the
    /// "appdata" field"). Either way a remote line's appdata is read when there is one, and
    /// the recipient names the track itself when there is none (RFC 8830 section 3). The
    /// source-level `a=ssrc:<ssrc> msid:` lines carry the track id in both forms.
    pub fn with_msid_appdata(mut self, appdata: bool) -> Self {
        self.0.msid_appdata_omitted = !appdata;
        self
    }

    /// Once ICE has selected a pair, later offers list the pair's local candidate alone (RFC
    /// 8839 section 4.4.1.2.2: "The offerer MUST NOT include any other SDP "candidate"
    /// attributes in the subsequent offer"). The default is `false`: every candidate of the
    /// most recent gathering, as RFC 9429 section 5.2.2 requires of a JSEP endpoint ("for
    /// each candidate that has been gathered during the most recent gathering phase ..., an
    /// "a=candidate" line MUST be added") and browsers do.
    pub fn with_nominated_candidates_only(mut self, only: bool) -> Self {
        self.0.nominated_candidates_only = only;
        self
    }

    /// The video frame sizes this endpoint can receive, with every video codec (its decoder's
    /// limits, or a policy): descriptions state them in `a=imageattr:* recv [x=...,y=...,q=1.0]`
    /// lines of their receiving video sections (RFC 9429 section 3.6.1, RFC 6236). A range
    /// that permits no size leaves the attribute out and makes those sections not receive
    /// (`sendonly` / `inactive`). The default is none: no `a=imageattr`.
    pub fn with_video_receive_limits(
        mut self,
        limits: crate::peer_connection::sdp::RTCVideoResolutionRange,
    ) -> Self {
        self.0.video_receive_limits = Some(limits);
        self
    }

    /// The video frame sizes this endpoint can receive with one codec (`mime_type`, such as
    /// `video/VP9`), instead of those of [`Self::with_video_receive_limits`]: codecs with
    /// different limits get one `a=imageattr` line per payload type (RFC 9429 section 3.6.1).
    pub fn with_codec_video_receive_limits(
        mut self,
        mime_type: &str,
        limits: crate::peer_connection::sdp::RTCVideoResolutionRange,
    ) -> Self {
        self.0
            .codec_video_receive_limits
            .retain(|(codec, _)| !codec.eq_ignore_ascii_case(mime_type));
        self.0
            .codec_video_receive_limits
            .push((mime_type.to_owned(), limits));
        self
    }

    /// Sends the header extension elements an application puts in a packet whose id is not
    /// negotiated for the sender, as the fork did before. The default is `false`.
    ///
    /// RFC 8285 section 4.1.2: "the local identifier ... MUST be one that has been negotiated"
    /// for an element that appears in a stream, so by default `RTCRtpSender::write_rtp` takes
    /// such an element out of the packet (with a trace log) and sends the rest. `true` passes
    /// them through.
    pub fn with_unnegotiated_header_extensions(mut self, pass_through: bool) -> Self {
        self.0.unnegotiated_header_extensions = pass_through;
        self
    }

    /// Whether a sender's stream moves to a new SSRC when the application writes a packet
    /// with a negotiated codec of another clock rate. The default is `false`: one SSRC for
    /// every clock rate.
    ///
    /// RFC 7160 section 4.1: "An RTP Sender with RTCP turned on MUST use a different SSRC for
    /// each different clock rate. An RTCP BYE MUST be sent and a new SSRC MUST be used if the
    /// clock rate switches back to a value already seen in the RTP stream." With `true`, a
    /// packet written with such a codec (Opus at 48 kHz, then PCMU at 8 kHz, on one audio
    /// sender) goes out as a new SSRC, the encoding's SSRC from then on; the stream it left
    /// stays in the RTCP session and its sender report follows the new one's in the next
    /// compound packet; a return to a rate used before sends a BYE for the SSRC that used it
    /// and takes another new one. Comfort noise and telephone events ride the stream as they
    /// are. `false` keeps one SSRC for every clock rate (RFC 7160 section 4.2 allows it only
    /// with RTCP off).
    ///
    /// `false` is the default because libwebrtc's receivers (Chromium, WebKit) do not follow
    /// the new SSRC: the browser interop matrix's RFC-form cells saw their track stay on the
    /// signalled SSRC and stop counting packets at the switch, in both offer directions, while
    /// Firefox followed. Turn it on for receivers known to follow (another `rtci` peer
    /// connection does).
    pub fn with_ssrc_per_clock_rate(mut self, ssrc_per_clock_rate: bool) -> Self {
        self.0.ssrc_per_clock_rate = ssrc_per_clock_rate;
        self
    }

    /// When a local audio stream whose section negotiated RED (RFC 2198) is sent in RED, each
    /// packet carrying a copy of the one before. The default is
    /// [`AudioRedundancy::WhenPreferred`](interceptor::AudioRedundancy::WhenPreferred): only
    /// when the remote description lists RED ahead of the stream's codec. Received RED is
    /// taken apart in every case.
    ///
    /// RFC 3264 sections 5.1 and 6.1: the formats of an m= line are "listed in order of
    /// preference", and the sender "SHOULD use the format with the highest preference". The
    /// default codecs offer RED after Opus, as Chromium and WebKit do, and those browsers
    /// answer it after Opus too; libwebrtc sends RED only when it precedes the codec it
    /// protects. So by default a call with them, or with another `rtci` peer connection,
    /// carries plain Opus: RED is negotiated, not used. RFC 8854 recommends Opus's in-band FEC
    /// for Opus (section 4.1) and FEC only for the loss observed or when the application asks
    /// (section 8).
    ///
    /// [`AudioRedundancy::Always`](interceptor::AudioRedundancy::Always) is that request: RED
    /// whenever negotiated, whatever the order. It about doubles the audio bitrate (a copy of
    /// the previous packet in each). [`AudioRedundancy::Never`](interceptor::AudioRedundancy::Never)
    /// sends the codec even to a peer that prefers RED.
    ///
    /// Applies to the RED interceptor the connection installs
    /// ([`configure_audio_redundancy_with`](crate::peer_connection::configuration::interceptor_registry::configure_audio_redundancy_with));
    /// a registry that fills [`Slot::Redundancy`](interceptor::Slot::Redundancy) itself
    /// decides with its own interceptor.
    pub fn with_audio_redundancy(mut self, audio_redundancy: interceptor::AudioRedundancy) -> Self {
        self.0.audio_redundancy = audio_redundancy;
        self
    }

    /// With [`Self::with_rid_pause_marks`], controls whether the `~` an offer puts on a
    /// stream it asks to receive is left out of the answer's `send` list.
    ///
    /// # Parameters
    ///
    /// * `ignore_rid_pause_for_recv` - `true` to leave the mark out, `false` to repeat it
    pub fn with_ignore_rid_pause_for_recv(mut self, ignore_rid_pause_for_recv: bool) -> Self {
        self.0.ignore_rid_pause_for_recv = ignore_rid_pause_for_recv;
        self
    }

    /// Controls whether to ignore SSRC attribute in SDP's sendonly or sendrecv for simulcast
    ///
    /// # Parameters
    ///
    /// * `write_ssrc_attributes_for_simulcast` - `true` to write, `false` to ignore
    pub fn with_write_ssrc_attributes_for_simulcast(
        mut self,
        write_ssrc_attributes_for_simulcast: bool,
    ) -> Self {
        self.0.write_ssrc_attributes_for_simulcast = write_ssrc_attributes_for_simulcast;
        self
    }

    /// Explicit Congestion Notification (RFC 3168) and L4S (RFC 9331) on RTP media.
    ///
    /// Turn this on when the application's socket layer carries ECN both ways:
    ///
    /// * **Receive**: each datagram handed to `handle_read` names the codepoint it arrived with
    ///   in [`TransportContext::ecn`](shared::TransportContext::ecn) (from the `IP_RECVTOS` /
    ///   `IPV6_RECVTCLASS` control message; `None` for Not-ECT).
    /// * **Send**: each datagram `poll_write` returns names the codepoint to set in
    ///   [`TransportContext::ecn`](shared::TransportContext::ecn) (`IP_TOS` / `IPV6_TCLASS`;
    ///   `None` for Not-ECT).
    ///
    /// With it on, the RTP packets of a stream whose codec negotiated RFC 8888 congestion
    /// control feedback (`a=rtcp-fb:<pt> ack ccfb`, see `configure_congestion_control_feedback`)
    /// and whose section negotiated ECN (`a=ecn-capable-rtp`, RFC 6679 section 6.1; see
    /// [`with_ecn_without_signalling`](Self::with_ecn_without_signalling)) are sent ECT: a
    /// sender marks only where the receiver reports the CE marks back. The codepoint is ECT(0)
    /// unless the receiver asks for ECT(1) (RFC 6679 section 7.3.1; see
    /// [`with_ecn_marking`](Self::with_ecn_marking) for L4S's ECT(1)). As RFC 8888 section 7
    /// requires, the stream first initiates ECN as RFC 6679
    /// section 7.2.1 has it (one packet in ten a probe, until the feedback shows the probes
    /// arrive ECT), then marks every packet (RTX included), and stops when the feedback shows
    /// the path clears the marking or drops marked packets (section 7.4); see
    /// [`with_ecn_initiation`](Self::with_ecn_initiation) and
    /// [`EcnValidation`](interceptor::EcnValidation). STUN, DTLS, SCTP and RTCP are never
    /// marked. Inbound RTP streams count their ECT(1) and CE receptions
    /// (`packetsReceivedWithEct1`, `packetsReceivedWithCe`) and outbound ones their ECT(1)
    /// packets (`packetsSentWithEct1`); with it off those members are left out of the stats,
    /// since the engine cannot see the IP header itself. Off by default.
    pub fn with_ecn(mut self, enabled: bool) -> Self {
        self.0.ecn = enabled;
        self
    }

    /// How a stream with ECN on ([`with_ecn`](Self::with_ecn)) starts marking:
    /// [`EcnInitiation::RtpAndRtcp`](interceptor::EcnInitiation::RtpAndRtcp), the default,
    /// probes first (RFC 6679 section 7.2.1, the method RFC 8888 section 7 requires);
    /// [`EcnInitiation::LeapOfFaith`](interceptor::EcnInitiation::LeapOfFaith) marks every
    /// packet from the first, as browsers that send L4S do (RFC 6679 section 7.2.3). Failure
    /// detection applies to both.
    pub fn with_ecn_initiation(mut self, initiation: interceptor::EcnInitiation) -> Self {
        self.0.ecn_initiation = initiation;
        self
    }

    /// The ECT codepoint a stream with ECN on ([`with_ecn`](Self::with_ecn)) marks its
    /// packets with: [`EcnMarking::Rfc6679`](interceptor::EcnMarking::Rfc6679), the default,
    /// sends ECT(0) unless the receiver's `a=ecn-capable-rtp` asks for ECT(1) or a random
    /// choice with `ect=` (RFC 6679 section 7.3.1: "SHOULD use ECT(0)");
    /// [`EcnMarking::L4s`](interceptor::EcnMarking::L4s) always sends ECT(1), the L4S
    /// identifier (RFC 9331), as browsers that send L4S do. `packetsSentWithEct1` counts the
    /// ECT(1) packets only.
    pub fn with_ecn_marking(mut self, marking: interceptor::EcnMarking) -> Self {
        self.0.ecn_marking = marking;
        self
    }

    /// With ECN on ([`with_ecn`](Self::with_ecn)), whether a stream marks on `ack ccfb` alone.
    ///
    /// By default ECN is negotiated as RFC 8888 section 7 and RFC 6679 section 6.1.1 have it:
    /// every RTP section of an offer carries `a=ecn-capable-rtp` with the initiation methods
    /// this endpoint implements (the one [`with_ecn_initiation`](Self::with_ecn_initiation)
    /// names first), an answer carries it with one method where the offer had it, and a
    /// stream marks only when both sides' sections carry it ("If the attribute is removed in
    /// the answer, then ECN MUST NOT be used in any direction") and the peer can read the
    /// field (its `mode` is not `setonly`); the answer's method is the initiation used.
    ///
    /// `true` is what browsers that send L4S do: no `a=ecn-capable-rtp` is written, and every
    /// stream with `ack ccfb` negotiated marks, initiating as `with_ecn_initiation` says.
    pub fn with_ecn_without_signalling(mut self, without_signalling: bool) -> Self {
        self.0.ecn_without_signalling = without_signalling;
        self
    }

    /// Whether the engine gathers TCP host candidates itself (RFC 6544 section 4.1 says it
    /// should be configurable; browsers gather them by default). On by default: for the
    /// address of each UDP host candidate the application adds, the engine asks its driver
    /// for listeners (`RTCPeerConnection::poll_stream_command`) and adds a passive and a
    /// simultaneous-open candidate on them and an active one (port 9), whose connections it
    /// opens itself. A driver that never serves the stream interface gets no TCP candidate.
    /// `false`: no TCP candidate but those the application adds.
    pub fn with_tcp_candidate_gathering(mut self, gather: bool) -> Self {
        self.0.streams.tcp_candidates = gather;
        self
    }

    /// Whether the gathered TCP candidates include a simultaneous-open one (RFC 6544 section
    /// 5.1: one passive and one S-O host candidate per interface). On by default; its
    /// listener and its connections bind with `reuse_address`. Browsers neither gather nor
    /// pair them (they connect to one as to a passive candidate, which works).
    pub fn with_tcp_simultaneous_open(mut self, simultaneous_open: bool) -> Self {
        self.0.streams.simultaneous_open = simultaneous_open;
        self
    }

    /// Whether the engine closes its ICE-TCP connections that carry no selected pair once ICE
    /// has completed (RFC 6544 section 8, SHOULD). On by default.
    pub fn with_tcp_close_unused_connections(mut self, close: bool) -> Self {
        self.0.streams.close_unused_connections = close;
        self
    }

    /// The limits of the engine's stream transports: the bytes of a connection the driver may
    /// hold unwritten, the bytes queued beyond which a packet is dropped whole, and the number
    /// of connections. See [`StreamConfig`](shared::stream::StreamConfig).
    pub fn with_stream_limits(mut self, limits: shared::stream::StreamConfig) -> Self {
        self.0.streams.limits = limits;
        self
    }

    /// Whether the engine runs the configuration's `turns:` and `turn:...?transport=tcp`
    /// servers itself, over its stream interface (RFC 8656 section 3.1): it resolves the
    /// name, connects (Happy Eyeballs, RFC 8305), runs TLS, allocates, announces the relayed
    /// candidate with its `relayProtocol` and relays. On by default; a driver that does not
    /// serve the stream interface sees nothing of it. `turn:` over UDP is
    /// [`with_relay_gathering`](Self::with_relay_gathering).
    pub fn with_turn_over_streams(mut self, run: bool) -> Self {
        self.0.streams.turn = run;
        self
    }

    /// Whether the engine also asks each of its `turn:...?transport=tcp` and `turns:` servers
    /// for a TCP allocation (RFC 6062, on a control connection of its own) and announces its
    /// relayed address as a passive TCP candidate and, on port 9, an active one (RFC 6544
    /// section 5.5): a peer that only reaches the relay over TCP connects through it. On by
    /// default.
    pub fn with_turn_tcp_relay(mut self, relay: bool) -> Self {
        self.0.streams.turn_tcp_relay = relay;
        self
    }

    /// Who gathers server-reflexive candidates (RFC 8445 section 5.1.1.2). By default
    /// ([`ReflexiveGathering::Engine`](crate::peer_connection::srflx::ReflexiveGathering)) the
    /// engine sends a Binding request to each `stun:` / `stuns:` server of the configuration:
    /// over UDP from each UDP host candidate the application adds (through `poll_write` /
    /// `handle_read`), and over TCP / TLS from its ICE-TCP listeners for a driver that serves
    /// the stream interface; a base for which the application adds a server-reflexive
    /// candidate itself is left to it. `Application` keeps the engine out of it.
    pub fn with_server_reflexive_gathering(
        mut self,
        who: crate::peer_connection::srflx::ReflexiveGathering,
    ) -> Self {
        self.0.streams.reflexive = who;
        self
    }

    /// Who gathers relayed candidates from the configuration's `turn:` servers over UDP (RFC
    /// 8656; RFC 8445 section 5.1.1.2). By default
    /// ([`RelayGathering::Engine`](crate::peer_connection::turn_streams::RelayGathering)) the
    /// engine allocates on each such server for each transport, from the transport's first UDP
    /// host candidate of the server's family, on the application's socket: the Allocate and
    /// everything after it leave through `poll_write`, the server's answers, Data indications
    /// and ChannelData are taken out of `handle_read` before ICE sees them (only those of the
    /// engine's own transactions and allocations: another TURN client on the socket is not
    /// disturbed). The server's addresses race as RFC 8656 section 3.9 asks for UDP. A server
    /// for which the application adds a relayed candidate itself (its `url`, or any relayed
    /// candidate over UDP without one) is left to it: the engine stops what it has not
    /// announced yet. An application that runs its own TURN client from a socket other than
    /// the one whose host candidate it gives the engine, and adds its relayed candidate late,
    /// would see both: it sets `Application`, and the engine sends nothing to those servers.
    pub fn with_relay_gathering(
        mut self,
        who: crate::peer_connection::turn_streams::RelayGathering,
    ) -> Self {
        self.0.streams.relay = who;
        self
    }

    /// How the engine's TURN-over-TLS connections verify their server: roots, a verifier,
    /// or (feature `turn-tls-webpki-roots`) the Mozilla roots. Without one no `turns:`
    /// server is used.
    pub fn with_turn_tls(mut self, tls: crate::peer_connection::turn_tls::TurnTlsConfig) -> Self {
        self.0.turn_tls = tls;
        self
    }

    /// Lets [`RTCRtpReceiver::write_rtcp`](crate::rtp_transceiver::rtp_receiver::RTCRtpReceiver::write_rtcp)
    /// and [`RTCRtpSender::write_rtcp`](crate::rtp_transceiver::rtp_sender::RTCRtpSender::write_rtcp) send
    /// feedback the section did not negotiate: a departure from RFC 4585 section 4.2 ("no
    /// feedback messages other than those listed ... MUST be used") and RFC 5104 section 7.2.
    /// Off by default: such a write is refused with `ErrRTCPFeedbackNotNegotiated`.
    pub fn with_unnegotiated_rtcp_feedback(mut self, allowed: bool) -> Self {
        self.0.unnegotiated_rtcp_feedback = allowed;
        self
    }

    /// Builds the connection without an RTCP session: a departure from RFC 8834 section 4.1
    /// ("RTCP ... MUST be implemented and used by all WebRTC endpoints").
    ///
    /// Every peer connection sends RTCP: when the interceptor registry given to
    /// [`RTCPeerConnectionBuilder::with_interceptor_registry`](crate::peer_connection::RTCPeerConnectionBuilder::with_interceptor_registry)
    /// has nothing at [`Slot::RtcpSession`](interceptor::Slot::RtcpSession) and no
    /// `SenderReportInterceptor` / `ReceiverReportInterceptor` of its own,
    /// [`RTCPeerConnectionBuilder::build`](crate::peer_connection::RTCPeerConnectionBuilder::build)
    /// installs the session of
    /// [`configure_rtcp_reports`](crate::peer_connection::configuration::interceptor_registry::configure_rtcp_reports)
    /// (an empty registry included). With this on it does not, and a registry without an RTCP
    /// session sends only the RTCP the application writes. Off by default.
    pub fn with_rtcp_session_omitted(mut self, omitted: bool) -> Self {
        self.0.rtcp_session_omitted = omitted;
        self
    }

    /// Holds received RTCP to the validity checks of RFC 3550 Appendix A.2 and section 6.4.1:
    /// a datagram whose first packet is not an SR or RR (unless reduced-size RTCP, RFC 5506,
    /// was negotiated), whose first packet has its padding bit set, or with padding on a
    /// packet that is not the last, is discarded whole.
    ///
    /// Off by default: libwebrtc sends padding on a packet in the middle of a compound packet
    /// (its transport-wide congestion control feedback, padded to a multiple of four), so the
    /// default accepts any datagram whose version and lengths hold, and processes every packet
    /// in it that parses (RFC 8834 section 4.1).
    pub fn with_strict_rtcp_validity(mut self, strict: bool) -> Self {
        self.0.strict_rtcp_validity = strict;
        self
    }

    /// Finalises the settings.
    pub fn build(self) -> SettingEngine {
        self.0
    }
}

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

    // Regression guard for the Safari mDNS interop fix. Safari (and Chrome)
    // emit only mDNS "<uuid>.local" host candidates by default; a default
    // SettingEngine must accept and resolve them (QueryOnly), not silently
    // discard them (Disabled).
    #[test]
    fn test_default_multicast_dns_mode_is_query_only() {
        assert_eq!(MulticastDNS::default().mode, MulticastDnsMode::QueryOnly);
        assert_eq!(
            SettingEngine::default().multicast_dns.mode,
            MulticastDnsMode::QueryOnly
        );
    }

    #[test]
    fn test_turn_allocation_refresh_interval_cap() {
        assert_eq!(
            SettingEngine::default().turn_allocation_refresh_interval_cap(),
            None
        );

        let cap = Duration::from_secs(30);
        let setting_engine = SettingEngineBuilder::new()
            .with_turn_allocation_refresh_interval_cap(cap)
            .build();

        assert_eq!(
            setting_engine.turn_allocation_refresh_interval_cap(),
            Some(cap)
        );
    }

    #[test]
    fn test_ice_credentials_roundtrip() {
        let setting_engine = SettingEngine::default();
        assert_eq!(setting_engine.ice_credentials(), ("", ""));

        let setting_engine = SettingEngineBuilder::new()
            .with_ice_credentials("ufrag".to_owned(), "password".to_owned())
            .build();
        assert_eq!(setting_engine.ice_credentials(), ("ufrag", "password"));

        let mut setting_engine = SettingEngine::default();
        setting_engine.set_ice_credentials("other_ufrag".to_owned(), "other_password".to_owned());
        assert_eq!(
            setting_engine.ice_credentials(),
            ("other_ufrag", "other_password")
        );
    }
}