media-pp 0.2.0

A small, GStreamer-flavored media pipeline library built on FFmpeg. Capture, composite and encode without leaving the GPU, on D3D11 and CUDA.
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
//! Long-running stress and leak scenarios.
//!
//! Every test here is `#[ignore]`d: they run for tens of seconds each, which
//! does not belong in `cargo test`'s normal path. Run them explicitly:
//!
//! ```text
//! cargo test -p media-pp --features d3d11,d3d12,cuda --test soak -- --ignored --nocapture
//! ```
//!
//! On Linux, `pipewire-screen-capture` replaces `d3d11`.
//!
//! `--nocapture` matters — every scenario prints the series it measured, so
//! a passing run still shows how much headroom it had.
//!
//! Knobs (all optional):
//!
//! - `MEDIA_PP_SOAK_ITERS` — measured iterations per cycle-driven scenario.
//! - `MEDIA_PP_SOAK_SECS` — how long each duration-driven scenario runs.
//! - `MEDIA_PP_TEST_VIDEO` — a real video file. The seek and hardware-decode
//!   scenarios skip without one: no synthetic source in this crate can seek,
//!   and no hardware decoder has anything to decode.
//! - `MEDIA_PP_SOAK_RESTORE_TOKEN` — an xdg-desktop-portal restore token.
//!   The Linux screen-capture scenarios skip without one, since the portal
//!   would otherwise show its picker and block; `cargo run -p screen_record_software
//!   -- out.mp4 2 monitor` prints a token to reuse.
//!
//! What the numbers mean, and how the thresholds were picked, is in
//! `common/mod.rs`; the short version is that each scenario fits a line
//! through its samples and fails on sustained growth, not on a single
//! endpoint delta. The thresholds below are set from measured runs on the
//! development machine with roughly an order of magnitude of headroom over
//! observed noise, so a real leak — a decoder context, a frame pool, a
//! texture set per cycle — trips them immediately while allocator jitter
//! does not.
//!
//! **What a pass here does and does not claim.** A pass is not "there is no
//! leak"; it is "no growth above what this window could resolve", and every
//! scenario prints that figure next to its result (`resolves growth of
//! X/iter and above`). Resolution is set by the noise and the sample count
//! together — the fitted slope's standard error falls off as `n^1.5`, so at
//! the small default counts a scenario resolves a fraction of a MiB per
//! cycle, and `MEDIA_PP_SOAK_ITERS=100` tightens that by roughly 30x.
//! Anything below the printed figure is invisible no matter how often the
//! suite runs at that count, and a window too noisy for its own threshold
//! fails outright rather than passing quietly.

mod common;

use std::{
    path::Path,
    sync::atomic::Ordering,
    thread,
    time::{Duration, Instant},
};

use common::{MIB, TempDir, Trend, iterations, settle, soak_duration, try_test_video};
use ffmpeg_next as ffmpeg;
use media_pp::{
    bus::BusEvent,
    color::Color,
    elements::{
        FileDemuxer, FileMuxer, FrameCounter, SegmentPolicy, SegmentedFileMuxer, SwDecoder,
        SwEncoder, SwEncoderOptions, SwVideoCompositor, TeeBuilder, TestVideoOptions,
        TestVideoSource, VideoCodec, VideoCompositorOptions, VideoFit, VideoLayer, VideoRect,
    },
    pipeline::{Pipeline, SeekMode},
};

/// Hands the scenario its own process, and returns from the parent once
/// that child has run (see `common::spawn_isolated`). Every scenario starts
/// with this.
///
/// The name the child is filtered by comes from the enclosing function
/// itself, through the type name of a local item, so it cannot drift out of
/// sync with the test the way a hand-written string would.
macro_rules! isolate {
    () => {{
        fn probe() {}
        fn type_name_of<T>(_: T) -> &'static str {
            std::any::type_name::<T>()
        }
        let full = type_name_of(probe);
        let path = full.strip_suffix("::probe").unwrap_or(full);
        // `soak::d3d11::scenario` -> `d3d11::scenario`, which is how the
        // test harness names it.
        let name = path.split_once("::").map_or(path, |(_crate, rest)| rest);
        if crate::common::spawn_isolated(name) {
            return;
        }
    }};
}

/// Cycle-driven scenarios discard this many cycles before measuring, so the
/// lazily-initialized FFmpeg tables and the first heap growth are not part
/// of the trend.
const WARMUP: usize = 3;

const WIDTH: u32 = 320;
const HEIGHT: u32 = 240;

fn frame_rate() -> ffmpeg::Rational {
    ffmpeg::Rational::new(30, 1)
}

fn test_source(name: &str) -> TestVideoSource {
    TestVideoSource::new(
        name,
        TestVideoOptions {
            width: WIDTH,
            height: HEIGHT,
            framerate: frame_rate(),
        },
    )
}

/// How a cycle ends. Both paths tear a running pipeline down, but through
/// different code: `finish` drains EOS through every stage and joins the
/// workers, `stop` abandons buffered work. A leak in either one is a leak,
/// so the scenarios alternate rather than picking a favorite.
#[derive(Clone, Copy, PartialEq)]
enum Teardown {
    Finish,
    Stop,
}

impl Teardown {
    fn for_cycle(cycle: usize) -> Self {
        if cycle.is_multiple_of(2) {
            Self::Finish
        } else {
            Self::Stop
        }
    }

    /// Returns only once the pipeline's threads have actually finished:
    /// `finish` joins them itself, while `stop` merely asks, so draining
    /// the bus to exhaustion — which ends when the last `Bus` sender drops
    /// — is what makes the two comparable. Draining is also how a cycle
    /// notices a failure that a frame count alone would hide, such as a
    /// decode surface pool that ran out partway through.
    fn apply(self, pipeline: &Pipeline) {
        match self {
            Self::Finish => pipeline.finish(),
            Self::Stop => pipeline.stop(),
        }
        let events: Vec<_> = pipeline.bus().iter().collect();
        assert_no_errors(&events);
    }
}

fn assert_no_errors(events: &[BusEvent]) {
    let errors: Vec<_> = events
        .iter()
        .filter(|event| matches!(event, BusEvent::Error { .. }))
        .collect();
    assert!(errors.is_empty(), "unexpected error event(s): {errors:?}");
}

fn encoder(name: &str, time_base: ffmpeg::Rational, gop_size: u32) -> SwEncoder {
    SwEncoder::new(
        name,
        SwEncoderOptions {
            codec: VideoCodec::OpenH264,
            width: WIDTH,
            height: HEIGHT,
            time_base,
            frame_rate: frame_rate(),
            bit_rate: 1_000_000,
            gop_size,
            max_b_frames: None,
        },
    )
    .expect("libopenh264 encoder")
}

/// The fixture's demuxer, the index of its video stream, and that stream's
/// codec parameters — what every scenario that decodes a real file needs,
/// and nothing a particular file has to provide beyond one video track.
fn open_fixture(path: &str) -> (FileDemuxer, usize, ffmpeg::codec::Parameters) {
    let (source, streams) = FileDemuxer::open("demux", path).expect("open the fixture");
    let index = streams
        .iter()
        .find(|stream| stream.kind == ffmpeg::media::Type::Video)
        .expect("the fixture has a video stream")
        .index;
    let parameters = source
        .stream_parameters(index)
        .expect("the stream the demuxer just listed");
    (source, index, parameters)
}

/// One complete recording: build the whole graph, run it, tear it down, and
/// drop every piece — the cycle a caller repeats whenever it records
/// several clips in one process.
fn record_once(path: &Path, teardown: Teardown) {
    let source = test_source("video");
    let time_base = source.time_base();
    let encoder = encoder("encoder", time_base, 30);
    let mut muxer = FileMuxer::create(path).expect("create the recording");
    muxer
        .add_stream("video", encoder.parameters(), time_base)
        .expect("add the video track");
    let sink = muxer
        .open()
        .expect("open the recording")
        .pop()
        .expect("one track");

    let pipeline = Pipeline::new("soak-record", source, |source, ctx| {
        let branch = ctx
            .branch()
            .queue("encode-frames", 8)
            .pipe(encoder)
            .to(sink)?;
        ctx.attach(source, 0, branch)?;
        Ok(())
    })
    .expect("wire the recording pipeline");

    pipeline.run().unwrap();
    thread::sleep(Duration::from_millis(250));
    teardown.apply(&pipeline);
}

/// The most common shape of a leak in a library like this one: something
/// that survives a whole build/run/teardown cycle. Encoder, muxer, queue,
/// frame pool, and source thread are all created and destroyed per cycle
/// here, so anything not released shows up as a slope.
#[test]
#[ignore = "soak test; run with --ignored"]
fn repeated_record_cycles_do_not_grow_process_memory() {
    isolate!();
    let _exclusive = common::exclusive();
    media_pp::init().expect("ffmpeg init");
    let dir = TempDir::new("record-cycles");
    let iterations = iterations(20);
    let mut memory = Trend::private_bytes("record cycle private bytes");

    for cycle in 0..(WARMUP + iterations) {
        let path = dir.join("cycle.mp4");
        record_once(&path, Teardown::for_cycle(cycle));
        assert!(path.is_file(), "cycle {cycle} produced no recording");
        std::fs::remove_file(&path)
            .expect("the muxer must have closed the file before the cycle ended");
        if cycle + 1 == WARMUP {
            settle();
        }
        if cycle >= WARMUP {
            memory.sample();
        }
    }

    // Measured noise on the development machine is well under 0.1 MiB per
    // cycle; a leaked encoder context or frame pool would be several.
    memory.assert_flat(0.5 * MIB);
}

/// Pause/resume hammering on one long-lived pipeline. Each round-trip runs
/// the whole synchronous control cascade — source `drain_control`, every
/// `Queue` worker, every `Sink::control` — which is where a per-message
/// allocation or a retained buffer would accumulate.
#[test]
#[ignore = "soak test; run with --ignored"]
fn pause_resume_storm_does_not_grow_process_memory() {
    isolate!();
    let _exclusive = common::exclusive();
    media_pp::init().expect("ffmpeg init");
    let (counter, frames) = FrameCounter::new("counter");
    let pipeline = Pipeline::new("soak-control", test_source("video"), |source, ctx| {
        let branch = ctx.branch().queue("frames", 8).to(Box::new(counter))?;
        ctx.attach(source, 0, branch)?;
        Ok(())
    })
    .expect("wire the control-storm pipeline");

    pipeline.run().unwrap();
    thread::sleep(Duration::from_millis(200));

    let iterations = iterations(60);
    let mut memory = Trend::private_bytes("control storm private bytes");
    for round in 0..(WARMUP + iterations) {
        pipeline.pause();
        thread::sleep(Duration::from_millis(20));
        pipeline.resume();
        thread::sleep(Duration::from_millis(30));
        if round + 1 == WARMUP {
            settle();
        }
        if round >= WARMUP {
            memory.sample();
        }
    }

    let before_teardown = frames.load(Ordering::Relaxed);
    pipeline.finish();
    assert!(
        before_teardown > 0,
        "the storm paused the pipeline so hard it never delivered a frame"
    );
    // A single control round-trip allocates almost nothing, so this
    // threshold is deliberately tight compared to the cycle scenarios.
    memory.assert_flat(0.25 * MIB);
}

/// Seeking repeatedly through a real file: the one control path that makes
/// a decoder flush, a `Queue` drop its backlog, and a `Pacer` re-anchor,
/// all while frames are in flight. No synthetic source in this crate can
/// seek, so this scenario needs a fixture and skips without one.
#[test]
#[ignore = "soak test; run with --ignored"]
fn seek_storm_does_not_grow_process_memory() {
    isolate!();
    let _exclusive = common::exclusive();
    media_pp::init().expect("ffmpeg init");
    let Some(path) = try_test_video() else { return };
    let (source, index, parameters) = open_fixture(&path);

    let (counter, frames) = FrameCounter::new("counter");
    let pipeline = Pipeline::new("soak-seek", source, |source, ctx| {
        let decoder = SwDecoder::new("decoder", parameters)?;
        let branch = ctx
            .branch()
            .pipe(decoder)
            .queue("frames", 8)
            .to(Box::new(counter))?;
        ctx.attach(source, index, branch)?;
        Ok(())
    })
    .expect("wire the seek-storm pipeline");

    pipeline.run().unwrap();
    thread::sleep(Duration::from_millis(200));

    let iterations = iterations(30);
    let mut memory = Trend::private_bytes("seek storm private bytes");
    for round in 0..(WARMUP + iterations) {
        // Two positions, alternating, so every seek is a real reposition
        // in one direction or the other rather than a no-op.
        let target = if round % 2 == 0 {
            Duration::from_millis(200)
        } else {
            Duration::from_millis(1200)
        };
        pipeline.seek(target, SeekMode::Accurate).expect("seek");
        thread::sleep(Duration::from_millis(60));
        if round + 1 == WARMUP {
            settle();
        }
        if round >= WARMUP {
            memory.sample();
        }
    }

    assert!(
        frames.load(Ordering::Relaxed) > 0,
        "the storm seeked so hard nothing ever decoded"
    );
    pipeline.stop();
    let events: Vec<_> = pipeline.bus().iter().collect();
    assert_no_errors(&events);
    // A seek flushes a decoder and drops a queue backlog, so its per-round
    // footprint is larger than a pause round-trip's but still bounded.
    memory.assert_flat(0.5 * MIB);
}

/// Attaching and detaching a `Tee` branch while frames flow through it —
/// the dynamic-topology path, where a stale branch, sink, or handle from a
/// previous attach is exactly the kind of thing that accumulates.
#[test]
#[ignore = "soak test; run with --ignored"]
fn tee_branch_churn_does_not_grow_process_memory() {
    isolate!();
    let _exclusive = common::exclusive();
    media_pp::init().expect("ffmpeg init");
    let (fixed_counter, fixed_frames) = FrameCounter::new("fixed-counter");
    let mut tee_handle = None;
    let pipeline = Pipeline::new("soak-tee", test_source("video"), |source, ctx| {
        let fixed = ctx.branch().to(Box::new(fixed_counter))?;
        let (tee_branch, handle) = TeeBuilder::new("tee", ctx.clone())
            .branch(fixed)
            .build_dynamic()?;
        ctx.attach(source, 0, tee_branch)?;
        tee_handle = Some(handle);
        Ok(())
    })
    .expect("wire the tee-churn pipeline");
    let tee_handle = tee_handle.expect("the wire closure provides the handle");

    pipeline.run().unwrap();
    thread::sleep(Duration::from_millis(200));

    let iterations = iterations(40);
    let mut memory = Trend::private_bytes("tee churn private bytes");
    for round in 0..(WARMUP + iterations) {
        let (counter, _frames) = FrameCounter::new("churn-counter");
        let branch = tee_handle
            .branch()
            .expect("the tee is alive while its pipeline runs")
            .to(Box::new(counter))
            .expect("build the runtime branch");
        let id = tee_handle
            .attach(branch)
            .expect("attach the runtime branch");
        thread::sleep(Duration::from_millis(40));
        tee_handle.detach(id).expect("detach the runtime branch");
        if round + 1 == WARMUP {
            settle();
        }
        if round >= WARMUP {
            memory.sample();
        }
    }

    assert_eq!(
        tee_handle.sink_count(),
        1,
        "every churned branch must be gone, leaving only the fixed one"
    );
    assert!(
        fixed_frames.load(Ordering::Relaxed) > 0,
        "the fixed branch must keep receiving frames throughout the churn"
    );
    pipeline.finish();
    memory.assert_flat(0.5 * MIB);
}

/// Adding and removing compositor inputs while the compositor keeps
/// emitting. Each round builds a whole input pipeline of its own, so this
/// churns the compositor's input registry, its layer handles, and a
/// short-lived source pipeline at the same time.
#[test]
#[ignore = "soak test; run with --ignored"]
fn compositor_input_churn_does_not_grow_process_memory() {
    isolate!();
    let _exclusive = common::exclusive();
    media_pp::init().expect("ffmpeg init");
    let (compositor, handle) = SwVideoCompositor::new(
        "compositor",
        VideoCompositorOptions {
            width: WIDTH,
            height: HEIGHT,
            frame_rate: frame_rate(),
            background: Color::new(16, 16, 16),
        },
    )
    .expect("create the compositor");

    let (counter, frames) = FrameCounter::new("counter");
    let output = Pipeline::new("soak-compositor", compositor, |source, ctx| {
        let branch = ctx.branch().queue("composited", 4).to(Box::new(counter))?;
        ctx.attach(source, 0, branch)?;
        Ok(())
    })
    .expect("wire the compositor output pipeline");
    output.run().unwrap();

    let iterations = iterations(20);
    let mut memory = Trend::private_bytes("compositor churn private bytes");
    for round in 0..(WARMUP + iterations) {
        let mut layer = VideoLayer::new(VideoRect::new(0, 0, WIDTH, HEIGHT));
        layer.fit = VideoFit::Cover;
        let input = handle
            .add_source("churned", layer)
            .expect("add a compositor input")
            .expect("the compositor is alive while its pipeline runs");

        let feeder = Pipeline::new("soak-compositor-input", test_source("input"), {
            let sink = input.sink;
            move |source, ctx| {
                let branch = ctx.branch().to(sink)?;
                ctx.attach(source, 0, branch)?;
                Ok(())
            }
        })
        .expect("wire the compositor input pipeline");
        feeder.run().unwrap();
        thread::sleep(Duration::from_millis(150));

        // The input's own pipeline is torn down first, the way a real
        // caller stops feeding a layer before removing it.
        feeder.finish();
        handle.remove_source("churned");
        assert_eq!(
            handle.source_count(),
            0,
            "round {round} left a compositor input behind"
        );
        if round + 1 == WARMUP {
            settle();
        }
        if round >= WARMUP {
            memory.sample();
        }
    }

    assert!(
        frames.load(Ordering::Relaxed) > 0,
        "the compositor must keep emitting while its inputs churn"
    );
    output.finish();
    // Each round builds and destroys a whole extra pipeline, so this one
    // is allowed more room than the pure control scenarios.
    memory.assert_flat(1.0 * MIB);
}

/// A compositor left running, rather than built and torn down: what the
/// scenarios above cannot see.
///
/// Answering a repeated picture with the one already composed is retention
/// per *frame*, not per cycle. The compositor holds the picture it may offer
/// again, and holds a replaced one until nothing references it any more —
/// `buffer::picture_is_referenced`, checked on every composite. If that
/// release ever fails to happen, this is where it shows: an output frame is
/// a whole picture, so a retained one per tick at this rate is megabytes a
/// second, and a cycle-driven scenario that runs for 150 ms would not notice.
///
/// The input changes on every frame, which is deliberately the case the
/// repeat path cannot help with: every tick composes, every tick replaces
/// what was held, and the queue behind it keeps some of those frames alive
/// while it does. The still case retains strictly less.
#[test]
#[ignore = "soak test; run with --ignored"]
fn a_running_compositor_does_not_grow_while_it_answers_frames() {
    isolate!();
    let _exclusive = common::exclusive();
    media_pp::init().expect("ffmpeg init");
    let (compositor, handle) = SwVideoCompositor::new(
        "compositor",
        VideoCompositorOptions {
            width: WIDTH,
            height: HEIGHT,
            frame_rate: frame_rate(),
            background: Color::new(16, 16, 16),
        },
    )
    .expect("create the compositor");

    let mut layer = VideoLayer::new(VideoRect::new(0, 0, WIDTH, HEIGHT));
    layer.fit = VideoFit::Cover;
    let input = handle
        .add_source("moving", layer)
        .expect("add a compositor input")
        .expect("the compositor is alive");

    let (counter, frames) = FrameCounter::new("counter");
    let output = Pipeline::new("soak-running-compositor", compositor, |source, ctx| {
        // A queue, so composited frames are still referenced when the next
        // composite replaces them — with a synchronous sink nothing is ever
        // held and the release has nothing to prove.
        let branch = ctx.branch().queue("composited", 4).to(Box::new(counter))?;
        ctx.attach(source, 0, branch)?;
        Ok(())
    })
    .expect("wire the compositor output pipeline");
    let feeder = Pipeline::new("soak-running-input", test_source("input"), {
        let sink = input.sink;
        move |source, ctx| {
            let branch = ctx.branch().to(sink)?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        }
    })
    .expect("wire the compositor input pipeline");
    output.run().unwrap();
    feeder.run().unwrap();

    let duration = soak_duration(20);
    let sample_interval = Duration::from_secs(1);
    let deadline = Instant::now() + duration;
    let mut memory = Trend::private_bytes("running compositor private bytes");
    // The first interval is the pools filling; every later one is steady
    // state, which is the only part a trend can be read from.
    thread::sleep(sample_interval);
    settle();
    let started = frames.load(Ordering::Relaxed);
    while Instant::now() < deadline {
        thread::sleep(sample_interval);
        memory.sample();
    }
    let composited = frames.load(Ordering::Relaxed) - started;
    feeder.finish();
    output.finish();

    assert!(
        composited > 0,
        "the compositor stopped emitting, so a flat trend proves nothing"
    );
    eprintln!("composited {composited} frames over {duration:?}");
    // One retained 1280x720 BGRA frame is 3.5 MiB, so a per-tick leak is
    // hundreds of times this slope; the budget is the allocator's own
    // jitter, not a fraction of what would be leaked.
    memory.assert_flat(0.5 * MIB);
}

/// A long segmented recording: one pipeline, many files. Rotation reopens a
/// muxer per segment, which is the per-file allocation most likely to
/// accumulate over a recording that runs for hours.
#[test]
#[ignore = "soak test; run with --ignored"]
fn segment_rotation_does_not_grow_process_memory_or_hold_files() {
    isolate!();
    let _exclusive = common::exclusive();
    media_pp::init().expect("ffmpeg init");
    let dir = TempDir::new("segments");
    let source = test_source("video");
    let time_base = source.time_base();
    // A keyframe every half second, so a one-second policy actually cuts
    // rather than waiting on a sparse GOP.
    let encoder = encoder("encoder", time_base, 15);

    let segment_dir = dir.path().to_path_buf();
    let mut muxer = SegmentedFileMuxer::create(
        SegmentPolicy::Duration(Duration::from_secs(1)),
        move |index| segment_dir.join(format!("segment_{index:04}.mp4")),
    );
    muxer.add_stream("video", encoder.parameters(), time_base);
    let sink = muxer
        .open()
        .expect("open the first segment")
        .pop()
        .expect("one track");

    let pipeline = Pipeline::new("soak-segments", source, |source, ctx| {
        let branch = ctx
            .branch()
            .queue("encode-frames", 8)
            .pipe(encoder)
            .to(sink)?;
        ctx.attach(source, 0, branch)?;
        Ok(())
    })
    .expect("wire the segmented pipeline");
    pipeline.run().unwrap();

    let duration = soak_duration(20);
    let sample_interval = Duration::from_secs(1);
    let deadline = Instant::now() + duration;
    let mut memory = Trend::private_bytes("segment rotation private bytes");
    // The first interval covers the warm-up of the first segment; every
    // later one is a steady-state sample.
    thread::sleep(sample_interval);
    settle();
    while Instant::now() < deadline {
        thread::sleep(sample_interval);
        memory.sample();
    }
    pipeline.finish();

    let segments: Vec<_> = std::fs::read_dir(dir.path())
        .expect("read the segment directory")
        .map(|entry| entry.expect("read a segment entry").path())
        .collect();
    assert!(
        segments.len() > 1,
        "the policy never actually rotated: {} file(s) in {duration:?}",
        segments.len()
    );
    for segment in &segments {
        assert!(
            std::fs::metadata(segment).expect("stat a segment").len() > 0,
            "{} was left empty",
            segment.display()
        );
        // Removing each one proves no rotation left a file handle open;
        // `TempDir`'s own teardown then proves the same for the last.
        std::fs::remove_file(segment).expect("a finalized segment must be closed");
    }

    memory.assert_flat(0.5 * MIB);
}

/// The D3D11 zero-copy path. Textures are both the resource most likely to
/// leak here and the one no CPU-side gauge can see at all, so this scenario
/// watches adapter video memory — and, when the SDK debug layer is
/// installed, the device's own live-object count — alongside private bytes.
#[cfg(all(windows, feature = "d3d11"))]
mod d3d11 {
    use std::{
        sync::{Arc, Mutex, atomic::Ordering},
        thread,
        time::{Duration, Instant},
    };

    use ffmpeg_next as ffmpeg;
    use media_pp::{
        color::Color,
        elements::{
            ChromaKeyMethod, ChromaKeyOptions, D3d11ChromaKey, D3d11Decoder, D3d11Scaler,
            D3d11ScalerFormat, D3d11Upload, D3d11VideoCodec, D3d11VideoCompositor,
            D3d11VideoEncoder, D3d11VideoEncoderOptions, D3d11VideoInputFormat, FrameCounter,
            PacketCounter, SwScaler, VideoCompositorOptions, VideoLayer, VideoRect,
        },
        pipeline::Pipeline,
    };
    use windows::Win32::Graphics::Direct3D11::{ID3D11Device, ID3D11DeviceContext};

    use crate::common::{
        MIB, Trend, Unit, exclusive,
        gpu::{D3d11LiveObjects, try_d3d11_device, vram_bytes},
        iterations, settle, soak_duration, try_test_video,
    };
    use crate::{HEIGHT, Teardown, WARMUP, WIDTH, frame_rate, test_source};

    /// How long the running-compositor scenario feeds the graph before its
    /// measurement window opens — see that scenario's own comment on the
    /// driver allocation cache this waits out.
    const WARMUP_SECS: Duration = Duration::from_secs(20);

    /// Deliberately not a multiple of the input size, so the scaler really
    /// scales, and even in both axes, which NV12 requires.
    const SCALED_WIDTH: u32 = 176;
    const SCALED_HEIGHT: u32 = 144;

    /// Upload every frame to a texture, scale it on the video processor,
    /// and hold the results in a queue — the shape of a real GPU pipeline,
    /// minus the renderer a headless test cannot present to.
    fn cycle(
        device: &ID3D11Device,
        context: &Arc<Mutex<ID3D11DeviceContext>>,
        teardown: Teardown,
    ) -> usize {
        let (counter, frames) = FrameCounter::new("counter");
        let device = device.clone();
        let context = context.clone();
        let pipeline = Pipeline::new("soak-d3d11", test_source("video"), move |source, ctx| {
            let to_nv12 = SwScaler::new(
                "to-nv12",
                ffmpeg::format::Pixel::NV12,
                WIDTH,
                HEIGHT,
                ffmpeg::software::scaling::Flags::BILINEAR,
            );
            let upload = D3d11Upload::new("upload", &device, WIDTH, HEIGHT);
            let scaler = D3d11Scaler::new(
                "scaler",
                &device,
                context.clone(),
                D3d11ScalerFormat::Preserve,
                SCALED_WIDTH,
                SCALED_HEIGHT,
            )?;
            let branch = ctx
                .branch()
                .pipe(to_nv12)
                .pipe(upload)
                .pipe(scaler)
                .queue("gpu-frames", 4)
                .to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire the D3D11 pipeline");

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(250));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    /// Key a compositor's own BGRA output on the GPU. What this adds over
    /// the upload/scale scenario is the per-frame render-target and
    /// shader-resource views `D3d11ChromaKey` creates around each draw:
    /// those are COM objects the shared context also holds a reference to
    /// until the element clears its bindings, so a missed release shows up
    /// on the debug layer's live-object count long before it is visible in
    /// adapter memory.
    ///
    /// The compositor is here because it is the only headless producer of
    /// GPU-resident BGRA in this crate — `D3d11Upload` is NV12-only, and
    /// desktop capture needs a desktop. It is also the real topology: this
    /// element exists to key a layer without leaving video memory.
    fn chroma_key_cycle(
        device: &ID3D11Device,
        context: &Arc<Mutex<ID3D11DeviceContext>>,
        teardown: Teardown,
    ) -> usize {
        let (compositor, compositor_handle) = D3d11VideoCompositor::new(
            "compositor",
            device,
            context.clone(),
            VideoCompositorOptions {
                width: WIDTH,
                height: HEIGHT,
                frame_rate: frame_rate(),
                background: Color::new(0, 255, 0),
            },
        )
        .expect("build the compositor");
        let layer_sink = compositor_handle
            .add_source(
                "layer",
                VideoLayer::new(VideoRect::new(0, 0, WIDTH, HEIGHT)),
            )
            .expect("register the compositor input")
            .expect("the compositor is alive")
            .sink;

        let input_device = device.clone();
        let input_pipeline = Pipeline::new("soak-d3d11-key-input", test_source("video"), {
            move |source, ctx| {
                let to_nv12 = SwScaler::new(
                    "to-nv12",
                    ffmpeg::format::Pixel::NV12,
                    WIDTH,
                    HEIGHT,
                    ffmpeg::software::scaling::Flags::BILINEAR,
                );
                let upload = D3d11Upload::new("upload", &input_device, WIDTH, HEIGHT);
                let branch = ctx.branch().pipe(to_nv12).pipe(upload).to(layer_sink)?;
                ctx.attach(source, 0, branch)?;
                Ok(())
            }
        })
        .expect("wire the compositor input pipeline");

        let (counter, frames) = FrameCounter::new("counter");
        let key_device = device.clone();
        let key_context = context.clone();
        let output_pipeline = Pipeline::new("soak-d3d11-key", compositor, move |source, ctx| {
            let key = D3d11ChromaKey::new(
                "key",
                &key_device,
                key_context,
                ChromaKeyOptions {
                    method: ChromaKeyMethod::Green,
                    threshold: 0.15,
                    smoothing: 0.1,
                },
            )?;
            let branch = ctx
                .branch()
                .pipe(key)
                .queue("keyed-frames", 4)
                .to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire the chroma-key pipeline");

        output_pipeline.run().unwrap();
        input_pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(250));
        // The input goes first: the compositor keeps drawing from whatever
        // it last received, so tearing it down first would leave the input
        // pushing into a sink that is already gone.
        teardown.apply(&input_pipeline);
        teardown.apply(&output_pipeline);
        frames.load(Ordering::Relaxed)
    }

    /// How deep the decode scenario's queue is, and therefore how many
    /// extra decode surfaces its decoder has to be given: D3D11VA's pool is
    /// a fixed-size texture array sized once at init, so every frame still
    /// sitting in that queue holds a slot (see `D3d11Decoder::new`).
    const DECODE_QUEUE_DEPTH: usize = 4;

    /// Decode a real file straight onto the GPU. What this adds over the
    /// upload scenario is the decoder's own fixed-size surface pool: a
    /// decoder that outlived its cycle would keep that whole texture array
    /// while the next cycle allocates another one.
    fn decode_cycle(device: &ID3D11Device, path: &str, teardown: Teardown) -> usize {
        let (source, index, parameters) = crate::open_fixture(path);
        let (counter, frames) = FrameCounter::new("counter");
        let device = device.clone();
        let pipeline = Pipeline::new("soak-d3d11-decode", source, move |source, ctx| {
            let decoder =
                D3d11Decoder::new("decoder", parameters, &device, DECODE_QUEUE_DEPTH as i32)?;
            let branch = ctx
                .branch()
                .pipe(decoder)
                .queue("decoded-frames", DECODE_QUEUE_DEPTH)
                .to(Box::new(counter))?;
            ctx.attach(source, index, branch)?;
            Ok(())
        })
        .expect(
            "wire the D3D11 decode pipeline — a failure here after the first cycle means \
             an earlier decoder never released its fixed-size surface pool",
        );

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(250));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    /// What every NVENC cycle below encodes with. Kept in one place because
    /// the support probe has to open an encoder with exactly the options a
    /// cycle will use — a codec or input format the driver refuses is a
    /// skip, not a leak.
    fn nvenc_options(time_base: ffmpeg::Rational) -> D3d11VideoEncoderOptions {
        D3d11VideoEncoderOptions {
            codec: D3d11VideoCodec::H264Nvenc,
            input_format: D3d11VideoInputFormat::Nv12,
            width: WIDTH,
            height: HEIGHT,
            time_base,
            frame_rate: frame_rate(),
            bit_rate: 1_000_000,
            gop_size: 30,
            max_b_frames: None,
        }
    }

    /// Encode on the GPU's NVENC block. Each cycle opens and closes a
    /// hardware encoding session, and consumer drivers cap how many can be
    /// open at once — so a session that outlived its cycle stops a later
    /// cycle from opening at all, rather than merely costing memory.
    fn encode_cycle(
        device: &ID3D11Device,
        context: &Arc<Mutex<ID3D11DeviceContext>>,
        teardown: Teardown,
    ) -> usize {
        let (counter, packets) = PacketCounter::new("counter");
        let source = test_source("video");
        let time_base = source.time_base();
        let device = device.clone();
        let context = context.clone();
        let pipeline = Pipeline::new("soak-d3d11-nvenc", source, move |source, ctx| {
            let to_nv12 = SwScaler::new(
                "to-nv12",
                ffmpeg::format::Pixel::NV12,
                WIDTH,
                HEIGHT,
                ffmpeg::software::scaling::Flags::BILINEAR,
            );
            let upload = D3d11Upload::new("upload", &device, WIDTH, HEIGHT);
            let encoder = D3d11VideoEncoder::new(
                "encoder",
                &device,
                context.clone(),
                nvenc_options(time_base),
            )?;
            let branch = ctx
                .branch()
                .pipe(to_nv12)
                .pipe(upload)
                .queue("gpu-frames", 4)
                .pipe(encoder)
                .to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect(
            "wire the D3D11 NVENC pipeline — a failure here after the first cycle means an \
             earlier encoder never closed its NVENC session",
        );

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(250));
        teardown.apply(&pipeline);
        packets.load(Ordering::Relaxed)
    }

    /// Whether this GPU and FFmpeg build have NVENC at all.
    fn nvenc_supported(device: &ID3D11Device, context: &Arc<Mutex<ID3D11DeviceContext>>) -> bool {
        let time_base = test_source("probe").time_base();
        match D3d11VideoEncoder::new("probe", device, context.clone(), nvenc_options(time_base)) {
            Ok(_) => true,
            Err(error) => {
                eprintln!("skipping: no D3D11 NVENC encoder on this machine ({error})");
                false
            }
        }
    }

    /// Whether this adapter has a D3D11VA decoder for the fixture's codec.
    /// One that does not is not a failure of anything this scenario
    /// measures, so it skips with a reason the same way a missing device
    /// does.
    fn decode_supported(device: &ID3D11Device, path: &str) -> bool {
        let (_source, _index, parameters) = crate::open_fixture(path);
        match D3d11Decoder::new("probe", parameters, device, 0) {
            Ok(_) => true,
            Err(error) => {
                eprintln!("skipping: no D3D11VA decoder for this fixture ({error})");
                false
            }
        }
    }

    /// How long a scenario warms up for, how long it measures, and how much
    /// growth each gauge is allowed per cycle.
    ///
    /// `memory` is optional because on this side of the crate it often
    /// cannot resolve anything worth asserting: a GPU cycle's private bytes
    /// move by megabytes in both directions as the graphics driver grows and
    /// trims its own bookkeeping, so the residual spread swamps any
    /// threshold small enough to catch a leak. Where that is the case the
    /// scenario records the trend and says so instead of pretending to
    /// judge it — adapter memory and the live-object count are the gauges
    /// with real sensitivity here, and they carry the assertion.
    struct Budget {
        warmup: usize,
        iterations: usize,
        memory: Option<f64>,
        vram: f64,
    }

    /// Runs `cycle` and judges every GPU gauge. The scenarios below differ
    /// only in the graph each cycle builds and in their `Budget`, not in how
    /// one is measured.
    fn measure_cycles(
        label: &str,
        device: &ID3D11Device,
        live: Option<Arc<D3d11LiveObjects>>,
        budget: Budget,
        mut cycle: impl FnMut(Teardown) -> usize,
    ) {
        let Budget {
            warmup,
            iterations,
            memory: max_memory_slope,
            vram: max_vram_slope,
        } = budget;
        let mut memory = Trend::private_bytes(format!("{label} private bytes"));
        let mut vram = Trend::new(format!("{label} adapter memory"), Unit::Bytes, {
            let device = device.clone();
            move || vram_bytes(&device)
        });
        let mut objects = live.clone().map(|live| {
            Trend::new(format!("{label} live objects"), Unit::Objects, move || {
                live.count()
            })
        });

        for index in 0..(warmup + iterations) {
            let teardown = Teardown::for_cycle(index);
            let frames = cycle(teardown);
            // Only a `finish` cycle has to have produced something. `stop`
            // means abandon, so a stateful stage — NVENC holds several
            // frames before its first packet — legitimately emits nothing
            // when a cycle ends that way.
            if teardown == Teardown::Finish {
                assert!(
                    frames > 0,
                    "{label} {index} pushed nothing through the GPU path before draining"
                );
            }
            if index + 1 == warmup {
                // The warm-up cycles are also what makes the previous
                // scenario's release land before this window opens.
                settle();
            }
            if index >= warmup {
                memory.sample();
                vram.sample();
                if let Some(objects) = objects.as_mut() {
                    objects.sample();
                }
            }
        }

        match max_memory_slope {
            Some(max) => memory.assert_flat(max),
            None => {
                memory.print();
                eprintln!(
                    "  recorded, not asserted: the graphics driver's own allocation cache \
                     dominates this gauge here and saturates at a different cycle every run \
                     (see Budget); adapter memory carries this scenario's assertion"
                );
            }
        }
        vram.assert_flat(max_vram_slope);

        let Some(objects) = objects else {
            return;
        };
        if objects.slope() > 0.0 {
            let live = live.expect("the trend exists only with the debug layer");
            eprintln!("live D3D11 objects after the last cycle:");
            for line in live.describe() {
                eprintln!("  {line}");
            }
        }
        // The device and its context are live throughout, so the count is
        // never zero — but an identical cycle must not add to it.
        objects.assert_flat(0.0);
    }

    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn upload_and_scale_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some((device, context, live)) = try_d3d11_device() else {
            return;
        };
        // Every cycle allocates its own upload and scaler textures and
        // releases them at teardown; the driver frees lazily enough for a
        // cycle's worth of jitter, but not for a cycle's worth of growth.
        measure_cycles(
            "d3d11 cycle",
            &device,
            live.map(Arc::new),
            Budget {
                warmup: WARMUP,
                iterations: iterations(15),
                memory: Some(0.5 * MIB),
                vram: 1.0 * MIB,
            },
            |teardown| cycle(&device, &context, teardown),
        );
    }

    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn chroma_key_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some((device, context, live)) = try_d3d11_device() else {
            return;
        };
        // The live-object count is the gauge that matters here, and the
        // reason this scenario exists. Every frame builds an output
        // texture, its render-target view, and the input.s shader-resource
        // view in both the compositor and the key, and a D3D11 object whose
        // last reference is dropped is only queued for destruction — it
        // survives until the context is flushed. Before `D3d11ChromaKey`
        // flushed, this cycle grew the device.s object count by 65 per
        // cycle and adapter memory by 5 MiB per cycle, both dead straight
        // over 40 cycles; all three gauges are flat with the flush in
        // place, which is where these thresholds come from.
        measure_cycles(
            "d3d11 chroma key cycle",
            &device,
            live.map(Arc::new),
            Budget {
                warmup: WARMUP,
                iterations: iterations(15),
                memory: Some(0.5 * MIB),
                vram: 1.0 * MIB,
            },
            |teardown| chroma_key_cycle(&device, &context, teardown),
        );
    }

    /// The GPU half of `a_running_compositor_does_not_grow_while_it_answers_frames`.
    ///
    /// What is retained here is a texture rather than a heap allocation, so
    /// the gauges that matter are adapter memory and the device's live-object
    /// count. A frame the compositor holds past its release — the picture it
    /// may offer again, or one replaced while a queue still refers to it — is
    /// a whole render target, and at this rate a per-tick retention is tens of
    /// MiB a second. `D3d11Upload` answers repeats the same way, so its
    /// retention is inside this window too.
    ///
    /// The input changes on every frame, deliberately: that is the case where
    /// every tick composes, replaces what was held, and asks the release to
    /// happen again.
    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn a_running_d3d11_compositor_does_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some((device, context, live)) = try_d3d11_device() else {
            return;
        };

        let (compositor, handle) = D3d11VideoCompositor::new(
            "compositor",
            &device,
            context.clone(),
            VideoCompositorOptions {
                width: WIDTH,
                height: HEIGHT,
                frame_rate: frame_rate(),
                background: Color::new(16, 16, 16),
            },
        )
        .expect("build the compositor");
        let layer_sink = handle
            .add_source(
                "moving",
                VideoLayer::new(VideoRect::new(0, 0, WIDTH, HEIGHT)),
            )
            .expect("register the compositor input")
            .expect("the compositor is alive")
            .sink;

        let input_device = device.clone();
        let feeder = Pipeline::new("soak-running-d3d11-input", test_source("video"), {
            move |source, ctx| {
                let to_nv12 = SwScaler::new(
                    "to-nv12",
                    ffmpeg::format::Pixel::NV12,
                    WIDTH,
                    HEIGHT,
                    ffmpeg::software::scaling::Flags::BILINEAR,
                );
                let upload = D3d11Upload::new("upload", &input_device, WIDTH, HEIGHT);
                let branch = ctx.branch().pipe(to_nv12).pipe(upload).to(layer_sink)?;
                ctx.attach(source, 0, branch)?;
                Ok(())
            }
        })
        .expect("wire the compositor input pipeline");

        let (counter, frames) = FrameCounter::new("counter");
        let output = Pipeline::new("soak-running-d3d11", compositor, |source, ctx| {
            // As in the software scenario: without a queue nothing is ever
            // still referenced when the next composite replaces it.
            let branch = ctx.branch().queue("composited", 4).to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire the compositor output pipeline");
        output.run().unwrap();
        feeder.run().unwrap();

        let duration = soak_duration(20);
        let sample_interval = Duration::from_secs(1);
        let mut memory = Trend::private_bytes("running d3d11 compositor private bytes");
        let mut vram = Trend::new("running d3d11 compositor adapter memory", Unit::Bytes, {
            let device = device.clone();
            move || vram_bytes(&device)
        });
        let mut objects = live.map(Arc::new).map(|live| {
            Trend::new(
                "running d3d11 compositor live objects",
                Unit::Objects,
                move || live.count(),
            )
        });
        // Far more warm-up than the software scenario, and measured rather
        // than guessed: `D3d11Upload` allocates a texture per frame, and the
        // user-mode driver keeps freed ones in its own allocation cache
        // instead of returning the pages. Private bytes therefore climb
        // about 4 MiB a second from a cold device and stop dead once that
        // cache is saturated — a run that opened its window at one second
        // measured +2.1 MiB/iter over the rise and flat over the rest of the
        // same run. This waits for the plateau, which is what the scenario
        // is about; `CAPTURE_WARMUP` documents the same driver behavior for
        // the capture scenarios.
        thread::sleep(WARMUP_SECS);
        settle();
        let started = frames.load(Ordering::Relaxed);
        let deadline = Instant::now() + duration;
        while Instant::now() < deadline {
            thread::sleep(sample_interval);
            memory.sample();
            vram.sample();
            if let Some(objects) = objects.as_mut() {
                objects.sample();
            }
        }
        let composited = frames.load(Ordering::Relaxed) - started;
        feeder.finish();
        output.finish();

        assert!(
            composited > 0,
            "the compositor stopped emitting, so a flat trend proves nothing"
        );
        eprintln!("composited {composited} frames over {duration:?}");
        memory.assert_flat(0.5 * MIB);
        // One retained 1280x720 BGRA render target is 3.5 MiB.
        vram.assert_flat(1.0 * MIB);
        if let Some(objects) = objects {
            objects.print();
            eprintln!(
                "  recorded, not asserted: a running graph creates D3D11 objects per frame — an \
                 upload texture and the shader-resource views a draw binds — and dropping the \
                 last reference only queues one for destruction, so this gauge climbs and then \
                 falls back wholesale when the driver flushes. It measures that queue rather \
                 than what this graph owns; the cycle scenarios above are where it is an \
                 ownership gauge, and adapter memory carries the assertion here"
            );
        }
    }

    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn decode_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(path) = try_test_video() else { return };
        let Some((device, _context, live)) = try_d3d11_device() else {
            return;
        };
        if !decode_supported(&device, &path) {
            return;
        }
        // A decode surface pool is much larger than the upload scenario's
        // textures — one leaked pool is tens of MiB, so this threshold is
        // still far below a single missed release.
        measure_cycles(
            "d3d11 decode cycle",
            &device,
            live.map(Arc::new),
            Budget {
                warmup: WARMUP,
                iterations: iterations(10),
                memory: Some(0.5 * MIB),
                vram: 2.0 * MIB,
            },
            |teardown| decode_cycle(&device, &path, teardown),
        );
    }

    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn nvenc_encode_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some((device, context, live)) = try_d3d11_device() else {
            return;
        };
        if !nvenc_supported(&device, &context) {
            return;
        }
        // NVENC keeps its own reference-frame surfaces per session, so a
        // leaked session costs more than the upload scenario's textures.
        measure_cycles(
            "d3d11 nvenc cycle",
            &device,
            live.map(Arc::new),
            Budget {
                warmup: WARMUP,
                iterations: iterations(10),
                memory: Some(0.5 * MIB),
                vram: 2.0 * MIB,
            },
            |teardown| encode_cycle(&device, &context, teardown),
        );
    }

    /// Far more warm-up than any other scenario, because of a driver
    /// behavior this scenario had to be measured against rather than
    /// assumed away.
    ///
    /// Opening a capture session allocates screen-sized textures on a
    /// device the session owns and destroys with itself. D3D11 releases
    /// resources lazily, and the user-mode driver keeps the committed pages
    /// in an allocation cache instead of returning them, so the first
    /// sessions a process opens each add roughly one screen's worth of
    /// private bytes — ~8 MiB at 1080p. It is a *cache*, not a leak: on
    /// this machine it saturates after six or so sessions and then stays
    /// put (measured flat across the following 24). Two things pinned that
    /// down: the same growth reproduces with plain `CreateTexture2D` calls
    /// and no media-pp code at all, and `CaptureMode::Cpu`, which allocates
    /// no per-frame textures, stays flat from the first cycle.
    ///
    /// So the measurement window has to start after saturation. What the
    /// scenario still catches is the thing that matters: growth that keeps
    /// going, which no cache explains.
    #[cfg(any(feature = "dxgi-capture", feature = "wgc-capture"))]
    const CAPTURE_WARMUP: usize = 10;

    /// One capture session: open it, let it emit for a while, tear it all
    /// down. Unlike every other scenario here the source brings its own
    /// `ID3D11Device` (see `DxgiCaptureSource::open`, which returns one), so
    /// each cycle creates and destroys a whole device, duplication, and
    /// texture set.
    #[cfg(feature = "dxgi-capture")]
    fn capture_cycle(mode: media_pp::elements::CaptureMode, teardown: Teardown) -> usize {
        use media_pp::elements::{CaptureArea, DxgiCaptureOptions, DxgiCaptureSource};

        let (counter, frames) = FrameCounter::new("counter");
        let (source, _format, _device) = DxgiCaptureSource::open(
            "capture",
            DxgiCaptureOptions {
                area: CaptureArea::Output { output_index: 0 },
                capture_mode: mode,
                ..Default::default()
            },
        )
        .expect(
            "open desktop duplication — a failure here after the first cycle means an earlier \
             capture source never released its output duplication",
        );

        let pipeline = Pipeline::new("soak-dxgi-capture", source, |source, ctx| {
            let branch = ctx.branch().queue("captured", 4).to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire the capture pipeline");

        pipeline.run().unwrap();
        // Longer than the other cycles: this source emits on its own fixed
        // schedule, so a cycle has to span several of its ticks to prove
        // frames really flowed.
        thread::sleep(Duration::from_millis(400));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    /// Whether desktop duplication is available at all — it is not, for
    /// instance, in a session without an attached output.
    #[cfg(feature = "dxgi-capture")]
    fn capture_supported(mode: media_pp::elements::CaptureMode) -> bool {
        use media_pp::elements::{CaptureArea, DxgiCaptureOptions, DxgiCaptureSource};

        match DxgiCaptureSource::open(
            "probe",
            DxgiCaptureOptions {
                area: CaptureArea::Output { output_index: 0 },
                capture_mode: mode,
                ..Default::default()
            },
        ) {
            Ok(_) => true,
            Err(error) => {
                eprintln!("skipping: no desktop duplication available here ({error})");
                false
            }
        }
    }

    /// The default capture mode: staging texture, `Map`, row copies into
    /// pooled CPU frames. No per-frame GPU allocation at all, which is what
    /// lets this scenario hold private bytes to the same tight threshold as
    /// the CPU-only scenarios — the sensitive half of the capture coverage.
    ///
    /// It still opens one staging texture per session, so it sees a small
    /// version of the same driver cache `CAPTURE_WARMUP` describes: a run of
    /// 250 cycles climbs about 4 MiB and then stops, its slope falling from
    /// +0.021 MiB/cycle over the first half to +0.004 over the second. At
    /// the default cycle count that step is under the measurement's own
    /// noise; at a hundred cycles or more it is visible and still bounded,
    /// which is what it should look like — not a reason to go hunting.
    #[test]
    #[ignore = "soak test; run with --ignored"]
    #[cfg(feature = "dxgi-capture")]
    fn cpu_desktop_capture_cycles_do_not_grow_process_memory() {
        use media_pp::elements::CaptureMode;

        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        // `CaptureMode` is not `Copy`, so each use builds its own value.
        let cpu_mode = || CaptureMode::Cpu {
            include_cursor: false,
        };
        if !capture_supported(cpu_mode()) {
            return;
        }
        let Some((device, _context, _live)) = try_d3d11_device() else {
            return;
        };
        measure_cycles(
            "cpu capture cycle",
            &device,
            None,
            Budget {
                warmup: WARMUP,
                iterations: iterations(10),
                memory: Some(0.5 * MIB),
                vram: 1.0 * MIB,
            },
            |teardown| capture_cycle(cpu_mode(), teardown),
        );
    }

    /// The zero-copy mode: every emitted frame is its own GPU texture. Its
    /// private-bytes budget is much looser than any other scenario's, for
    /// the driver-cache reason `CAPTURE_WARMUP` documents — 3 MiB still
    /// catches a screen-sized allocation retained per cycle, which is what a
    /// leak here would look like, and adapter memory stays strict.
    #[test]
    #[ignore = "soak test; run with --ignored"]
    #[cfg(feature = "dxgi-capture")]
    fn gpu_desktop_capture_cycles_do_not_grow_gpu_memory() {
        use media_pp::elements::CaptureMode;

        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        if !capture_supported(CaptureMode::Gpu) {
            return;
        }
        let Some((device, _context, _live)) = try_d3d11_device() else {
            return;
        };
        // No live-object trend: this scenario's D3D11 objects belong to the
        // capture source's own device, which the gauge device cannot see.
        // Adapter memory is per process, so it still covers them.
        measure_cycles(
            "gpu capture cycle",
            &device,
            None,
            Budget {
                warmup: CAPTURE_WARMUP,
                iterations: iterations(10),
                memory: None,
                vram: 1.0 * MIB,
            },
            |teardown| capture_cycle(CaptureMode::Gpu, teardown),
        );
    }

    #[cfg(feature = "wgc-capture")]
    struct WgcTestWindow {
        hwnd: usize,
        thread: Option<std::thread::JoinHandle<()>>,
    }

    #[cfg(feature = "wgc-capture")]
    impl WgcTestWindow {
        fn create() -> windows::core::Result<Self> {
            use windows::{
                Win32::UI::WindowsAndMessaging::{
                    CreateWindowExW, DestroyWindow, DispatchMessageW, GetMessageW, IsWindow, MSG,
                    TranslateMessage, WINDOW_EX_STYLE, WS_OVERLAPPEDWINDOW, WS_VISIBLE,
                },
                core::w,
            };

            let (ready_tx, ready_rx) = std::sync::mpsc::sync_channel(1);
            let thread = thread::spawn(move || {
                // SAFETY: `STATIC` is a preregistered window class and every
                // other argument is a plain value or an absent
                // parent/menu/instance, so nothing here has to outlive the
                // call. This thread owns the window for its whole lifetime,
                // which is what `GetMessageW`/`DestroyWindow` below require.
                let created = unsafe {
                    CreateWindowExW(
                        WINDOW_EX_STYLE::default(),
                        w!("STATIC"),
                        w!("media-pp WGC soak window"),
                        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                        0,
                        0,
                        320,
                        240,
                        None,
                        None,
                        None,
                        None,
                    )
                };
                let hwnd = match created {
                    Ok(hwnd) => hwnd,
                    Err(error) => {
                        let _ = ready_tx.send(Err(error));
                        return;
                    }
                };
                if ready_tx.send(Ok(hwnd.0 as usize)).is_err() {
                    // SAFETY: this thread created `hwnd` and no one else has
                    // seen it, so destroying it here happens exactly once on
                    // the owning thread.
                    let _ = unsafe { DestroyWindow(hwnd) };
                    return;
                }

                let mut message = MSG::default();
                // SAFETY: `message` is a live out-parameter and a `None` window
                // filter asks for this thread's own queue, which is the queue
                // the window above belongs to.
                while unsafe { GetMessageW(&mut message, None, 0, 0) }.0 > 0 {
                    // SAFETY: both calls only read the message `GetMessageW`
                    // just filled in.
                    unsafe {
                        let _ = TranslateMessage(&message);
                        DispatchMessageW(&message);
                    }
                    // SAFETY: reads only whether `hwnd` still identifies a
                    // window; it retains no caller storage.
                    if !unsafe { IsWindow(Some(hwnd)) }.as_bool() {
                        break;
                    }
                }
                // SAFETY: same as the check above — a stale handle simply
                // reports false.
                if unsafe { IsWindow(Some(hwnd)) }.as_bool() {
                    // SAFETY: the loop exited with the window still alive, so
                    // this destroys it once, on the thread that created it.
                    let _ = unsafe { DestroyWindow(hwnd) };
                }
            });
            let hwnd = ready_rx
                .recv()
                .expect("WGC window thread stopped before reporting readiness")?;
            Ok(Self {
                hwnd,
                thread: Some(thread),
            })
        }

        fn hwnd(&self) -> windows::Win32::Foundation::HWND {
            windows::Win32::Foundation::HWND(self.hwnd as *mut _)
        }
    }

    #[cfg(feature = "wgc-capture")]
    impl Drop for WgcTestWindow {
        fn drop(&mut self) {
            use windows::Win32::{
                Foundation::{LPARAM, WPARAM},
                UI::WindowsAndMessaging::{PostMessageW, WM_CLOSE},
            };

            // SAFETY: posting is thread-agnostic by design — it queues
            // `WM_CLOSE` for the owning thread's loop to dispatch rather than
            // touching the window here. A stale handle only fails the call.
            let _ = unsafe {
                PostMessageW(
                    Some(self.hwnd()),
                    WM_CLOSE,
                    WPARAM::default(),
                    LPARAM::default(),
                )
            };
            if let Some(thread) = self.thread.take() {
                thread.join().expect("WGC window thread panicked");
            }
        }
    }

    /// Opens and tears down the complete WGC graph. WGC owns a fresh device,
    /// frame pool, session, WinRT callbacks, destroy-hook registration, owner
    /// process handle, and changing textures in every cycle — and with one
    /// source alive at a time the shared watcher thread and its hook are
    /// created and torn down every cycle too — so the process-wide adapter
    /// gauge is used just like the DXGI source above.
    #[cfg(feature = "wgc-capture")]
    fn wgc_capture_cycle(hwnd: windows::Win32::Foundation::HWND, teardown: Teardown) -> usize {
        use media_pp::elements::{WgcCaptureOptions, WgcCaptureSource};
        use windows::Win32::UI::WindowsAndMessaging::{
            SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOZORDER, SetWindowPos,
        };

        static CYCLE: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);

        let (counter, frames) = FrameCounter::new("counter");
        let (source, _device) = WgcCaptureSource::open(
            "window-capture",
            hwnd,
            WgcCaptureOptions {
                fps: 30,
                include_cursor: false,
            },
        )
        .expect("open WGC source");
        let pipeline = Pipeline::new("soak-wgc-capture", source, |source, ctx| {
            let branch = ctx.branch().queue("captured", 4).to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire WGC pipeline");

        pipeline.run().expect("start WGC pipeline");
        // WGC is change-driven. Repeatedly capturing an otherwise motionless
        // test window is allowed to produce no new notification, so resize by
        // one pixel after every session starts. This also exercises the frame-
        // pool recreation path instead of relying on a cosmetic repaint.
        let cycle = CYCLE.fetch_add(1, Ordering::Relaxed);
        let width = if cycle.is_multiple_of(2) { 320 } else { 321 };
        // SAFETY: `hwnd` is the live window owned by `WgcTestWindow`, which
        // outlives this call, and `SWP_NOMOVE`/`SWP_NOZORDER` make the ignored
        // position and z-order arguments inert.
        unsafe {
            SetWindowPos(
                hwnd,
                None,
                0,
                0,
                width,
                240,
                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE,
            )
        }
        .expect("resize WGC target window");
        thread::sleep(Duration::from_millis(750));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    #[test]
    #[ignore = "soak test; requires an interactive Windows Graphics Capture session"]
    #[cfg(feature = "wgc-capture")]
    fn window_capture_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let window = match WgcTestWindow::create() {
            Ok(window) => window,
            Err(error) => {
                eprintln!("skipping: cannot create WGC target window ({error})");
                return;
            }
        };
        let Some((gauge_device, _context, _live)) = try_d3d11_device() else {
            return;
        };

        // WGC and the user-mode driver retain bounded initialization caches,
        // so discard the same longer warm-up used for GPU DXGI sessions. The
        // source devices differ per cycle; adapter memory is process-wide,
        // while a live-object gauge on `gauge_device` could not see them.
        measure_cycles(
            "WGC window capture cycle",
            &gauge_device,
            None,
            Budget {
                warmup: CAPTURE_WARMUP,
                iterations: iterations(10),
                memory: None,
                vram: 1.0 * MIB,
            },
            |teardown| wgc_capture_cycle(window.hwnd(), teardown),
        );
    }
}

/// The D3D12VA transfer and video-process path. Each cycle creates and
/// destroys the upload/scaler FFmpeg contexts, both fixed GPU surface pools,
/// the video processor's queue/list/fences, and the CPU frame pool
/// `D3d12Download` fills. The device itself stays alive so the trend measures
/// element ownership rather than adapter initialization.
#[cfg(all(windows, feature = "d3d12"))]
mod d3d12 {
    use std::{sync::atomic::Ordering, thread, time::Duration};

    use ffmpeg_next as ffmpeg;
    use media_pp::{
        elements::{D3d12Download, D3d12Scaler, D3d12Upload, FrameCounter, SwScaler},
        pipeline::Pipeline,
    };
    use windows::Win32::Graphics::{
        Direct3D::D3D_FEATURE_LEVEL_11_0,
        Direct3D12::{D3D12CreateDevice, ID3D12Device},
    };

    use crate::common::{Trend, Unit, exclusive, gpu::d3d12_vram_bytes, iterations, settle};
    use crate::{HEIGHT, Teardown, WARMUP, WIDTH, test_source};

    const SCALED_WIDTH: u32 = 176;
    const SCALED_HEIGHT: u32 = 144;

    fn try_device() -> Option<ID3D12Device> {
        let mut device = None;
        // SAFETY: null adapter requests the default hardware adapter and
        // `device` is the correctly typed live out-parameter for this feature
        // level.
        let result = unsafe { D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut device) };
        if let Err(error) = result {
            eprintln!("skipping: D3D12CreateDevice failed on this machine: {error}");
            return None;
        }
        device
    }

    fn cycle(device: &ID3D12Device, teardown: Teardown) -> usize {
        let (counter, frames) = FrameCounter::new("counter");
        let device = device.clone();
        let pipeline = Pipeline::new("soak-d3d12", test_source("video"), move |source, ctx| {
            let to_nv12 = SwScaler::new(
                "to-nv12",
                ffmpeg::format::Pixel::NV12,
                WIDTH,
                HEIGHT,
                ffmpeg::software::scaling::Flags::BILINEAR,
            );
            let upload = D3d12Upload::new("upload", &device, WIDTH, HEIGHT)?;
            let scaler = D3d12Scaler::new("scaler", &device, SCALED_WIDTH, SCALED_HEIGHT)?;
            let download = D3d12Download::new("download", SCALED_WIDTH, SCALED_HEIGHT);
            let branch = ctx
                .branch()
                .pipe(to_nv12)
                .pipe(upload)
                .pipe(scaler)
                .queue("gpu-frames", 4)
                .pipe(download)
                .to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect(
            "wire the D3D12 upload/scale/download pipeline — a failure after the first cycle may mean \
             an earlier FFmpeg D3D12VA frames context retained its surface pool",
        );

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(250));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn upload_scale_and_download_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(device) = try_device() else { return };

        let mut memory = Trend::private_bytes("d3d12 scale cycle private bytes");
        let mut vram = Trend::new("d3d12 scale cycle adapter memory", Unit::Bytes, {
            let device = device.clone();
            move || d3d12_vram_bytes(&device)
        });
        for index in 0..(WARMUP + iterations(15)) {
            let teardown = Teardown::for_cycle(index);
            let frames = cycle(&device, teardown);
            if teardown == Teardown::Finish {
                assert!(frames > 0, "D3D12 finish cycle {index} produced no frames");
            }
            if index + 1 == WARMUP {
                settle();
            }
            if index >= WARMUP {
                memory.sample();
                vram.sample();
            }
        }

        // Measured with upload + video-process scale + download over the
        // default 15-cycle window: private bytes grew +0.032 MiB/cycle
        // (0.018 MiB/cycle standard error), while adapter memory was flat.
        // The thresholds remain below one retained output pool at this
        // resolution and comfortably above the observed fitted slopes.
        memory.assert_flat(0.25 * crate::common::MIB);
        vram.assert_flat(0.5 * crate::common::MIB);
    }
}

/// The CUDA-resident path, same shape as the D3D11 one. CUDA surfaces are
/// invisible to both the process heap and DXGI, so this scenario asks the
/// driver directly when it can.
#[cfg(feature = "cuda")]
mod cuda {
    use std::{sync::atomic::Ordering, thread, time::Duration};

    use ffmpeg_next as ffmpeg;
    use media_pp::{
        color::Color,
        elements::{
            CudaCodec, CudaDecoder, CudaDevice, CudaDownload, CudaEncoder, CudaEncoderOptions,
            CudaFrameFormat, CudaScaler, CudaScalerInterp, CudaUpload, CudaVideoCompositor,
            FrameCounter, PacketCounter, SwScaler, VideoCompositorOptions, VideoLayer, VideoRect,
        },
        pipeline::Pipeline,
    };

    use crate::common::{
        MIB, Trend, Unit, exclusive, gpu::nvidia_process_bytes, iterations, settle, soak_duration,
        try_test_video,
    };
    use crate::{HEIGHT, Teardown, WARMUP, WIDTH, frame_rate, test_source};

    const SCALED_WIDTH: u32 = 176;
    const SCALED_HEIGHT: u32 = 144;

    /// One device for the whole scenario, on purpose: creating and
    /// destroying a `CudaDevice` retains and releases the process-wide
    /// primary context, which is exactly what `test_support`'s own CUDA
    /// helper documents as unsafe to churn next to in-flight NVDEC/NVENC
    /// work. The elements each take their own reference to it per cycle,
    /// which is the ownership this scenario is actually measuring.
    fn cycle(device: &CudaDevice, teardown: Teardown) -> usize {
        let (counter, frames) = FrameCounter::new("counter");
        let pipeline = Pipeline::new("soak-cuda", test_source("video"), move |source, ctx| {
            let to_nv12 = SwScaler::new(
                "to-nv12",
                ffmpeg::format::Pixel::NV12,
                WIDTH,
                HEIGHT,
                ffmpeg::software::scaling::Flags::BILINEAR,
            );
            let upload = CudaUpload::new("upload", device, CudaFrameFormat::Nv12, WIDTH, HEIGHT)?;
            let scaler = CudaScaler::new(
                "scaler",
                device,
                SCALED_WIDTH,
                SCALED_HEIGHT,
                CudaScalerInterp::Bilinear,
            );
            let download = CudaDownload::new(
                "download",
                device,
                CudaFrameFormat::Nv12,
                SCALED_WIDTH,
                SCALED_HEIGHT,
            );
            let branch = ctx
                .branch()
                .pipe(to_nv12)
                .pipe(upload)
                .pipe(scaler)
                .queue("gpu-frames", 4)
                .pipe(download)
                .to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire the CUDA pipeline");

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(250));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    /// NVDEC's surface pool is fixed-size *and* capped at 32 surfaces
    /// including the codec's own reference frames (see `CudaDecoder::new`),
    /// so a decode scenario's queue depth is part of that budget rather
    /// than a free choice.
    const DECODE_QUEUE_DEPTH: usize = 4;

    /// NVDEC's process-side driver cache takes substantially longer to
    /// settle than the lazily initialized tables covered by [`WARMUP`]. On
    /// this machine the first 15 cycles climbed by 5-7 MiB each, but a
    /// 250-cycle A/B run both before and after the `AvBufferRef` RAII change
    /// repeatedly returned tens of MiB and flattened to +0.083 MiB/cycle.
    /// Discard enough cycles to measure after that cache has saturated;
    /// otherwise this scenario deterministically reports driver warm-up as
    /// a decoder leak.
    const DECODE_CACHE_WARMUP: usize = 50;

    /// Decode a real file straight into CUDA memory. The decoder's pool is
    /// allocated per cycle and, unlike everything else measured here, is
    /// capped — a cycle that failed to release one would run the next
    /// cycle's `cuvidCreateDecoder` into that cap rather than merely using
    /// more memory.
    fn decode_cycle(device: &CudaDevice, path: &str, teardown: Teardown) -> usize {
        let (source, index, parameters) = crate::open_fixture(path);
        let (counter, frames) = FrameCounter::new("counter");
        let pipeline = Pipeline::new("soak-cuda-decode", source, move |source, ctx| {
            let decoder =
                CudaDecoder::new("decoder", parameters, device, DECODE_QUEUE_DEPTH as i32)?;
            let branch = ctx
                .branch()
                .pipe(decoder)
                .queue("decoded-frames", DECODE_QUEUE_DEPTH)
                .to(Box::new(counter))?;
            ctx.attach(source, index, branch)?;
            Ok(())
        })
        .expect(
            "wire the CUDA decode pipeline — a failure here after the first cycle means \
             an earlier decoder never released its NVDEC surfaces, which are capped at 32",
        );

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(250));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    /// What every NVENC cycle below encodes with — same reasoning as the
    /// D3D11 module's own options helper.
    fn nvenc_options(time_base: ffmpeg::Rational) -> CudaEncoderOptions {
        CudaEncoderOptions {
            codec: CudaCodec::H264,
            input_format: CudaFrameFormat::Nv12,
            width: WIDTH,
            height: HEIGHT,
            time_base,
            frame_rate: frame_rate(),
            bit_rate: 1_000_000,
            gop_size: 30,
            max_b_frames: None,
        }
    }

    /// Encode CUDA-resident surfaces on NVENC. A session that outlived its
    /// cycle would run a later cycle into the driver's concurrent-session
    /// cap, so opening successfully every cycle is itself the assertion.
    fn encode_cycle(device: &CudaDevice, teardown: Teardown) -> usize {
        let (counter, packets) = PacketCounter::new("counter");
        let source = test_source("video");
        let time_base = source.time_base();
        let pipeline = Pipeline::new("soak-cuda-nvenc", source, move |source, ctx| {
            let to_nv12 = SwScaler::new(
                "to-nv12",
                ffmpeg::format::Pixel::NV12,
                WIDTH,
                HEIGHT,
                ffmpeg::software::scaling::Flags::BILINEAR,
            );
            let upload = CudaUpload::new("upload", device, CudaFrameFormat::Nv12, WIDTH, HEIGHT)?;
            let encoder = CudaEncoder::new("encoder", device, nvenc_options(time_base))?;
            let branch = ctx
                .branch()
                .pipe(to_nv12)
                .pipe(upload)
                .queue("gpu-frames", 4)
                .pipe(encoder)
                .to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect(
            "wire the CUDA NVENC pipeline — a failure here after the first cycle means an \
             earlier encoder never closed its NVENC session",
        );

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(250));
        teardown.apply(&pipeline);
        packets.load(Ordering::Relaxed)
    }

    /// Whether this GPU and FFmpeg build have NVENC at all.
    fn nvenc_supported(device: &CudaDevice) -> bool {
        let time_base = test_source("probe").time_base();
        match CudaEncoder::new("probe", device, nvenc_options(time_base)) {
            Ok(_) => true,
            Err(error) => {
                eprintln!("skipping: no CUDA NVENC encoder on this machine ({error})");
                false
            }
        }
    }

    /// Whether NVDEC has a decoder for the fixture's codec on this machine.
    fn decode_supported(device: &CudaDevice, path: &str) -> bool {
        let (_source, _index, parameters) = crate::open_fixture(path);
        match CudaDecoder::new("probe", parameters, device, 0) {
            Ok(_) => true,
            Err(error) => {
                eprintln!("skipping: no NVDEC decoder for this fixture ({error})");
                false
            }
        }
    }

    /// A CUDA device, or `None` after printing why. Kept for the whole
    /// scenario on purpose: creating and destroying a `CudaDevice` retains
    /// and releases the process-wide primary context, which
    /// `test_support`'s own CUDA helper documents as unsafe to churn next
    /// to in-flight NVDEC/NVENC work.
    ///
    /// `pub(crate)` for the `pipewire` module's GPU capture scenario, whose
    /// frames land in CUDA memory through this same device.
    pub(crate) fn try_device() -> Option<CudaDevice> {
        match CudaDevice::new() {
            Ok(device) => Some(device),
            Err(error) => {
                eprintln!("skipping: no usable CUDA device on this machine ({error})");
                None
            }
        }
    }

    /// Runs `cycle` and judges what this platform lets us see. Both
    /// scenarios below differ only in the graph each cycle builds.
    fn measure_cycles(
        label: &str,
        post_settle_warmup: usize,
        iterations: usize,
        max_memory_slope: f64,
        mut cycle: impl FnMut(Teardown) -> usize,
    ) {
        let mut memory = Trend::private_bytes(format!("{label} private bytes"));
        let mut gpu = match nvidia_process_bytes() {
            Some(_) => Some(Trend::new(
                format!("{label} driver-reported GPU memory"),
                Unit::Bytes,
                || nvidia_process_bytes().unwrap_or_default(),
            )),
            None => {
                eprintln!(
                    "note: this driver does not report per-process GPU memory; measuring private \
                     bytes only"
                );
                None
            }
        };

        let measurement_start = WARMUP + post_settle_warmup;
        for index in 0..(measurement_start + iterations) {
            let teardown = Teardown::for_cycle(index);
            let frames = cycle(teardown);
            // See the D3D11 module's own note: only a `finish` cycle owes
            // us output, since `stop` abandons whatever a stateful stage
            // was still holding.
            if teardown == Teardown::Finish {
                assert!(
                    frames > 0,
                    "{label} {index} pushed nothing through the CUDA path before draining"
                );
            }
            if index + 1 == WARMUP {
                settle();
            }
            if index >= measurement_start {
                memory.sample();
                if let Some(gpu) = gpu.as_mut() {
                    gpu.sample();
                }
            }
        }

        memory.assert_flat(max_memory_slope);
        if let Some(gpu) = gpu {
            // Reported in whole MiB, so a threshold below that would fail
            // on quantization alone.
            gpu.assert_flat(1.0 * MIB);
        }
    }

    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn upload_scale_and_download_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(device) = try_device() else { return };

        // More cycles than the D3D11 scenario on purpose: the CUDA
        // driver's own host-side allocations make private bytes jitter by
        // roughly +-4 MiB peak to peak, several times what the CPU-only
        // scenarios show, so a longer window and a looser threshold are
        // what keep that jitter from dominating the fitted slope.
        //
        // At the default count this gauge also shows a climb of about
        // +0.1 MiB per cycle, which is the same host-side allocation
        // settling rather than anything retained: measured over 250
        // cycles it is a bounded ~14 MiB rise that stops outright, its
        // slope falling +0.075 over the first half, +0.032 over the
        // second, and exactly 0.000 over the last 62 cycles. A leak would
        // hold its slope as the window grows instead of flattening.
        measure_cycles("cuda cycle", 0, iterations(25), 1.0 * MIB, |teardown| {
            cycle(&device, teardown)
        });
    }

    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn decode_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(path) = try_test_video() else { return };
        let Some(device) = try_device() else { return };
        if !decode_supported(&device, &path) {
            return;
        }

        measure_cycles(
            "cuda decode cycle",
            DECODE_CACHE_WARMUP,
            iterations(50),
            1.0 * MIB,
            |teardown| decode_cycle(&device, &path, teardown),
        );
    }

    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn nvenc_encode_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(device) = try_device() else { return };
        if !nvenc_supported(&device) {
            return;
        }

        measure_cycles(
            "cuda nvenc cycle",
            0,
            iterations(15),
            1.0 * MIB,
            |teardown| encode_cycle(&device, teardown),
        );
    }

    /// The CUDA half of `a_running_compositor_does_not_grow_while_it_answers_frames`.
    ///
    /// The cycle scenarios above build and tear a graph down, which is the
    /// wrong shape to see retention per *frame*: `CudaVideoCompositor` holds
    /// the surface it may offer again when nothing changed, and `CudaUpload`
    /// and `CudaConverter` hold their own inputs and outputs for the same
    /// reason. What is retained is a surface out of an FFmpeg hardware frames
    /// pool, so the gauge that matters is the driver's per-process GPU
    /// memory; at this rate a per-tick retention is tens of MiB a second, and
    /// a 250 ms cycle would not notice.
    ///
    /// The input changes on every frame, deliberately: that is the case the
    /// repeat path cannot help with, where every tick composes and replaces
    /// what was held. A queue behind the compositor keeps some of those
    /// frames alive while it does, so the release has something to prove.
    /// The still case retains strictly less.
    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn a_running_cuda_compositor_does_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(device) = try_device() else { return };

        let (compositor, handle) = CudaVideoCompositor::new(
            "compositor",
            &device,
            VideoCompositorOptions {
                width: WIDTH,
                height: HEIGHT,
                frame_rate: frame_rate(),
                background: Color::new(16, 16, 16),
            },
        )
        .expect("build the compositor");
        let layer_sink = handle
            .add_source(
                "moving",
                VideoLayer::new(VideoRect::new(0, 0, WIDTH, HEIGHT)),
            )
            .expect("register the compositor input")
            .sink;

        // Borrowed into the builder, the way every cycle above does it:
        // `CudaDevice` is not cloneable, and retaining the primary context a
        // second time is what its own docs warn against next to in-flight work.
        let input_device = &device;
        let feeder = Pipeline::new("soak-running-cuda-input", test_source("video"), {
            move |source, ctx| {
                let to_nv12 = SwScaler::new(
                    "to-nv12",
                    ffmpeg::format::Pixel::NV12,
                    WIDTH,
                    HEIGHT,
                    ffmpeg::software::scaling::Flags::BILINEAR,
                );
                let upload =
                    CudaUpload::new("upload", input_device, CudaFrameFormat::Nv12, WIDTH, HEIGHT)?;
                let branch = ctx.branch().pipe(to_nv12).pipe(upload).to(layer_sink)?;
                ctx.attach(source, 0, branch)?;
                Ok(())
            }
        })
        .expect("wire the compositor input pipeline");

        let (counter, frames) = FrameCounter::new("counter");
        let output = Pipeline::new("soak-running-cuda", compositor, |source, ctx| {
            // As in the software and D3D11 scenarios: without a queue nothing
            // is ever still referenced when the next composite replaces it.
            let branch = ctx.branch().queue("composited", 4).to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire the compositor output pipeline");
        output.run().unwrap();
        feeder.run().unwrap();

        let duration = soak_duration(20);
        let sample_interval = Duration::from_secs(1);
        let mut memory = Trend::private_bytes("running cuda compositor private bytes");
        let mut gpu = match nvidia_process_bytes() {
            Some(_) => Some(Trend::new(
                "running cuda compositor driver-reported GPU memory",
                Unit::Bytes,
                || nvidia_process_bytes().unwrap_or_default(),
            )),
            None => {
                eprintln!(
                    "note: this driver does not report per-process GPU memory; measuring private \
                     bytes only"
                );
                None
            }
        };
        // The hardware frames pools fill over the first interval — the
        // compositor's own, `CudaUpload`'s, and the surface the compositor
        // holds to offer again — and every later one is steady state, which
        // is the only part a trend can be read from.
        thread::sleep(sample_interval);
        settle();
        let started = frames.load(Ordering::Relaxed);
        let deadline = std::time::Instant::now() + duration;
        while std::time::Instant::now() < deadline {
            thread::sleep(sample_interval);
            memory.sample();
            if let Some(gpu) = gpu.as_mut() {
                gpu.sample();
            }
        }
        let composited = frames.load(Ordering::Relaxed) - started;
        feeder.finish();
        output.finish();

        assert!(
            composited > 0,
            "the compositor stopped emitting, so a flat trend proves nothing"
        );
        eprintln!("composited {composited} frames over {duration:?}");
        // Private bytes carry the CUDA driver's own host-side jitter, which
        // the cycle scenarios above measure at several MiB peak to peak, so
        // this gauge is deliberately the looser of the two.
        memory.assert_flat(1.0 * MIB);
        if let Some(gpu) = gpu {
            // One retained NV12 surface at this size is a fraction of a MiB
            // and the driver reports in whole MiB, so the threshold is
            // quantization rather than a fraction of what a per-tick leak
            // would be: that is tens of MiB a second.
            gpu.assert_flat(1.0 * MIB);
        }
    }
}

/// The Linux desktop-capture path, the counterpart of the D3D11 module's
/// two DXGI capture scenarios. Each cycle runs a whole portal handshake and
/// PipeWire stream — session, node, negotiated format, and in GPU mode a
/// set of DMA-BUF imports — so what these measure is whether a session
/// releases all of that when its source drops.
///
/// Both need `MEDIA_PP_SOAK_RESTORE_TOKEN`; see `common::try_restore_token`
/// for why that cannot be defaulted or detected.
#[cfg(all(target_os = "linux", feature = "pipewire-screen-capture"))]
mod pipewire {
    use std::{sync::atomic::Ordering, thread, time::Duration};

    use media_pp::{
        elements::{
            CaptureSourceKind, FrameCounter, PipeWireScreenCaptureOptions,
            PipeWireScreenCaptureSource,
        },
        pipeline::Pipeline,
    };

    use crate::common::{MIB, Trend, exclusive, iterations, settle, try_restore_token};
    use crate::{Teardown, WARMUP};

    /// Every ScreenCast session **this process** holds open on the portal, by
    /// object path.
    ///
    /// Read from the portal itself rather than from anything this crate
    /// tracks, because what is being tested is the portal's own view: a
    /// session it still holds is one still shown as "screen is being shared",
    /// whatever the process believes.
    ///
    /// Filtered by the owning connection's pid, which is the only thing that
    /// makes this scenario sound. Every gauge in this file is isolated by
    /// `isolate!` giving the scenario its own process — but the portal's
    /// session tree is system-wide, and `exclusive()` is an in-process mutex
    /// that the parent never reaches, so the capture scenarios above really do
    /// run alongside this one, opening and dropping sessions of their own. The
    /// portal files each session under the unique name of the connection that
    /// opened it, and the bus will say which pid owns that name.
    ///
    /// This also settles what `ashpd` will not: it opens its connection lazily
    /// into a private `OnceLock` and never exposes it, so the unique name its
    /// sessions live under cannot be asked for directly.
    fn portal_sessions() -> Vec<String> {
        use ashpd::zbus;

        const ROOT: &str = "/org/freedesktop/portal/desktop/session";

        async fn children(connection: &zbus::Connection, path: &str) -> Vec<String> {
            let Ok(proxy) = zbus::fdo::IntrospectableProxy::builder(connection)
                .destination("org.freedesktop.portal.Desktop")
                .expect("a valid destination")
                .path(path.to_owned())
                .expect("a valid path")
                .build()
                .await
            else {
                return Vec::new();
            };
            // A path with no node is not a failure — it is zero children.
            let Ok(xml) = proxy.introspect().await else {
                return Vec::new();
            };
            xml.split("<node name=\"")
                .skip(1)
                .filter_map(|rest| rest.split('"').next())
                .map(|name| format!("{path}/{name}"))
                .collect()
        }

        pollster::block_on(async {
            let connection = zbus::Connection::session()
                .await
                .expect("a session bus to read the portal from");
            let bus = zbus::fdo::DBusProxy::new(&connection)
                .await
                .expect("the bus's own interface");
            let me = std::process::id();

            let mut sessions = Vec::new();
            for owner in children(&connection, ROOT).await {
                // The node name is the owning connection's unique name with
                // its dots turned into underscores: `1_290` is `:1.290`.
                let unique = format!(
                    ":{}",
                    owner
                        .rsplit('/')
                        .next()
                        .expect("a non-empty node name")
                        .replace('_', ".")
                );
                let Ok(name) = zbus::names::BusName::try_from(unique) else {
                    continue;
                };
                // A connection that has gone since the tree was read is not
                // ours, and asking about it fails rather than answering.
                if bus.get_connection_unix_process_id(name).await != Ok(me) {
                    continue;
                }
                sessions.extend(children(&connection, &owner).await);
            }
            sessions.sort();
            sessions
        })
    }

    /// `ashpd::desktop::Session` has no `Drop` of its own, and every proxy in
    /// the process shares one cached `zbus` connection — so nothing about
    /// dropping a capture closes its portal session, and without an explicit
    /// `Close` the portal keeps it, and its "screen is being shared"
    /// indicator, until the process exits.
    ///
    /// A cycle scenario would not catch this. The sessions are on the portal's
    /// side of the bus, so they cost this process almost nothing and no memory
    /// gauge here moves; the only thing that notices is the user, and the
    /// compositor.
    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn dropping_a_capture_closes_its_portal_session() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(restore_token) = try_restore_token() else {
            return;
        };
        if !capture_supported(&restore_token) {
            return;
        }

        let before = portal_sessions();
        let opened: Vec<_> = (0..3)
            .map(|index| {
                PipeWireScreenCaptureSource::open(format!("probe-{index}"), options(&restore_token))
                    .expect("open a capture with the restore token")
                    .0
            })
            .collect();

        let during = portal_sessions();
        assert_eq!(
            during.len(),
            before.len() + 3,
            "three captures did not open three portal sessions; before {before:?}, during \
             {during:?}"
        );

        drop(opened);
        let after = portal_sessions();
        assert_eq!(
            after, before,
            "a dropped capture left its portal session open — the portal still shows the screen \
             as being shared. before {before:?}, after {after:?}"
        );
    }

    /// What every cycle below opens with. A monitor rather than a window:
    /// the token restores whichever source was picked once, and a monitor
    /// is the one kind that is certain to still exist on a later run.
    fn options(restore_token: &str) -> PipeWireScreenCaptureOptions {
        PipeWireScreenCaptureOptions {
            fps: 30,
            source_kind: CaptureSourceKind::Monitor,
            include_cursor: false,
            restore_token: Some(restore_token.to_owned()),
        }
    }

    /// How long a cycle lets the capture run. Longer than the CPU-only
    /// scenarios' cycles for the same reason the DXGI one is: this source
    /// emits on its own fixed schedule, so a cycle has to span several of
    /// its ticks to prove frames really flowed.
    const CAPTURE_MILLIS: u64 = 400;

    /// One CPU capture session: portal handshake, stream, frames into
    /// pooled BGRA `AVFrame`s, teardown.
    fn cpu_cycle(restore_token: &str, teardown: Teardown) -> usize {
        let (counter, frames) = FrameCounter::new("counter");
        let (source, _format, _token) =
            PipeWireScreenCaptureSource::open("capture", options(restore_token)).expect(
                "open the portal capture — a failure here after the first cycle means an \
                 earlier source never closed its portal session or PipeWire stream",
            );

        let pipeline = Pipeline::new("soak-pipewire-capture", source, |source, ctx| {
            let branch = ctx.branch().queue("captured", 4).to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire the capture pipeline");

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(CAPTURE_MILLIS));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    /// The same session in GPU mode: `open_gpu` negotiates DMA-BUF only and
    /// imports each buffer into CUDA memory, so a cycle also creates and
    /// destroys the EGL display, the cached `EGLImage`s, and a CUDA frames
    /// context. None of that is visible to the process heap, which is why
    /// this scenario asks the driver for its own number.
    #[cfg(feature = "cuda")]
    fn gpu_cycle(
        device: &media_pp::elements::CudaDevice,
        restore_token: &str,
        teardown: Teardown,
    ) -> usize {
        let (counter, frames) = FrameCounter::new("counter");
        let (source, _format, _token) =
            PipeWireScreenCaptureSource::open_gpu("capture", options(restore_token), device)
                .expect(
                    "open the portal capture in GPU mode — a failure here after the first \
                     cycle means an earlier source never released its EGL images or CUDA \
                     surfaces",
                );

        let pipeline = Pipeline::new("soak-pipewire-capture-gpu", source, |source, ctx| {
            let branch = ctx.branch().queue("captured", 4).to(Box::new(counter))?;
            ctx.attach(source, 0, branch)?;
            Ok(())
        })
        .expect("wire the GPU capture pipeline");

        pipeline.run().unwrap();
        thread::sleep(Duration::from_millis(CAPTURE_MILLIS));
        teardown.apply(&pipeline);
        frames.load(Ordering::Relaxed)
    }

    /// Whether this desktop will hand out a capture at all with the token
    /// it was given, so that a stale token or a compositor without the
    /// portal skips with a reason instead of failing the suite.
    ///
    /// The probe is the honest place to find out: an `open` whose token no
    /// longer restores falls back to the picker, and the picker being
    /// dismissed is `Cancelled` — a routine outcome, not a malfunction.
    fn capture_supported(restore_token: &str) -> bool {
        match PipeWireScreenCaptureSource::open("probe", options(restore_token)) {
            Ok(_) => true,
            Err(error) => {
                eprintln!("skipping: no portal screen capture available here ({error})");
                false
            }
        }
    }

    /// Runs `cycle` and judges what this platform lets us see — the same
    /// shape as the `cuda` module's own helper, since the GPU gauge is the
    /// same driver query.
    ///
    /// `watch_gpu` is what the cycle's own mode decides: the CPU path never
    /// creates a CUDA context, so querying the driver for its share would
    /// only add a flat zero to the report and assert nothing.
    fn measure_cycles(
        label: &str,
        watch_gpu: bool,
        iterations: usize,
        max_memory_slope: f64,
        mut cycle: impl FnMut(Teardown) -> usize,
    ) {
        use crate::common::{Unit, gpu::nvidia_process_bytes};

        let mut memory = Trend::private_bytes(format!("{label} private bytes"));
        let mut gpu = match nvidia_process_bytes() {
            Some(_) if watch_gpu => Some(Trend::new(
                format!("{label} driver-reported GPU memory"),
                Unit::Bytes,
                || nvidia_process_bytes().unwrap_or_default(),
            )),
            None if watch_gpu => {
                eprintln!(
                    "note: this driver does not report per-process GPU memory; measuring private \
                     bytes only"
                );
                None
            }
            _ => None,
        };

        for index in 0..(WARMUP + iterations) {
            let teardown = Teardown::for_cycle(index);
            let frames = cycle(teardown);
            // Only a `finish` cycle owes us output, same as everywhere
            // else here — and an idle desktop still produces frames,
            // because this source re-emits its latest image on its own
            // schedule rather than only when the screen changes.
            if teardown == Teardown::Finish {
                assert!(
                    frames > 0,
                    "{label} {index} captured nothing before draining"
                );
            }
            if index + 1 == WARMUP {
                settle();
            }
            if index >= WARMUP {
                memory.sample();
                if let Some(gpu) = gpu.as_mut() {
                    gpu.sample();
                }
            }
        }

        memory.assert_flat(max_memory_slope);
        if let Some(gpu) = gpu {
            // Reported in whole MiB, so a threshold below that would fail
            // on quantization alone.
            gpu.assert_flat(1.0 * MIB);
        }
    }

    /// The CPU path: every captured image is copied into a pooled frame in
    /// system memory, so a session that kept its buffers shows up in
    /// private bytes directly — a screen's worth of BGRA is ~8 MiB at
    /// 1080p, sixteen times this threshold.
    ///
    /// What it does show is a bounded step, not a ramp: over 40 cycles on
    /// the development machine private bytes climbed 74.4 -> 75.1 MiB and
    /// then stayed there, all of it inside the first twenty cycles. Same
    /// shape as the DXGI CPU scenario's own note — an allocator and driver
    /// settling into a working set, which no per-cycle threshold above it
    /// mistakes for growth.
    #[test]
    #[ignore = "soak test; run with --ignored"]
    fn cpu_desktop_capture_cycles_do_not_grow_process_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(token) = try_restore_token() else {
            return;
        };
        if !capture_supported(&token) {
            return;
        }

        measure_cycles(
            "pipewire capture cycle",
            false,
            iterations(10),
            0.5 * MIB,
            |teardown| cpu_cycle(&token, teardown),
        );
    }

    /// The zero-copy path: nothing captured reaches system memory, so the
    /// driver's per-process figure carries this scenario and private bytes
    /// only covers the host side of the EGL and CUDA bookkeeping.
    ///
    /// That driver figure is the sharp instrument here: 40 cycles measured
    /// 82.0 MiB on every single one, so a retained frames context or a
    /// leaked DMA-BUF import has nowhere to hide. Private bytes gets the
    /// looser threshold instead, because the CUDA and EGL host allocations
    /// arrive as occasional sub-MiB steps rather than a flat line.
    #[test]
    #[ignore = "soak test; run with --ignored"]
    #[cfg(feature = "cuda")]
    fn gpu_desktop_capture_cycles_do_not_grow_gpu_memory() {
        isolate!();
        let _exclusive = exclusive();
        media_pp::init().expect("ffmpeg init");
        let Some(token) = try_restore_token() else {
            return;
        };
        let Some(device) = crate::cuda::try_device() else {
            return;
        };
        if !capture_supported(&token) {
            return;
        }

        measure_cycles(
            "pipewire gpu capture cycle",
            true,
            iterations(10),
            1.0 * MIB,
            |teardown| gpu_cycle(&device, &token, teardown),
        );
    }
}