bairelay 1.1.2

RTSP Relay for Reolink Baichuan cameras
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use tokio::sync::{watch, Notify};
use tokio_util::sync::CancellationToken;

use bairelay_neolink_core::bc_protocol::{BcCamera, CameraDriver};
use bairelay_rtsp::buffer::LastFrameBuffer;
use bairelay_rtsp::provider::StreamError;
use bairelay_rtsp::url::StreamKind as RtspStreamKind;

use crate::audio_presence::AudioPresence;
use crate::bcmedia_dump::BcMediaDumpConfig;
use crate::camera_tasks;
use crate::capabilities::CameraCapabilities;
use crate::config::CameraConfig;
use crate::grace_period::GracePeriod;
use crate::preview_state::PreviewState;
use crate::status_cache::StatusCache;
use crate::stream_source::{MutexPoisonRecover as _, RwLockPoisonRecover as _, StreamSource};
use crate::wake_lock::WakeLockCounter;

// ── Camera State ──────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CameraState {
	Disconnected,
	Connecting,
	Connected,
}

impl CameraState {
	pub fn is_disconnected(&self) -> bool {
		matches!(self, CameraState::Disconnected)
	}

	pub fn is_connecting(&self) -> bool {
		matches!(self, CameraState::Connecting)
	}

	pub fn is_connected(&self) -> bool {
		matches!(self, CameraState::Connected)
	}
}

/// Fold the per-camera [`PreviewState`], the "any active source is
/// currently in `Bridging`" signal, and the aggregator's own last
/// write into the effective state the MQTT overlay should display
///.
///
/// Rules:
/// - [`PreviewState::Sleeping`] passes through unchanged (connect-loop
///   owned — parked / post-grace-disconnect).
/// - [`PreviewState::Live`] + any bridging source → `Connecting`
///   (downgrade).
/// - [`PreviewState::Live`] + no bridging → stays `Live`.
/// - [`PreviewState::Connecting`]:
///   - If `last_own_write == Some(Connecting)` AND no bridging → `Live`
///     (our own downgrade; gap cleared).
///   - Otherwise → `Connecting` (connect-loop owned, or still bridging;
///     don't touch).
fn aggregate_preview_state(
	current: PreviewState,
	any_bridging: bool,
	last_own_write: Option<PreviewState>,
) -> PreviewState {
	match (current, any_bridging, last_own_write) {
		(PreviewState::Sleeping, _, _) => PreviewState::Sleeping,
		(PreviewState::Live, true, _) => PreviewState::Connecting,
		(PreviewState::Live, false, _) => PreviewState::Live,
		(PreviewState::Connecting, false, Some(PreviewState::Connecting)) => PreviewState::Live,
		(PreviewState::Connecting, _, _) => PreviewState::Connecting,
	}
}

/// Pick the PreviewState after `keepalive_loop` returns — the session
/// is over, either because the camera is about to park (idle +
/// idle_disconnect) or because the loop is about to reconnect.
///
/// Returns `Sleeping` only when the next iteration will actually park;
/// otherwise `Connecting`, which avoids a brief SLEEPING flash between
/// reconnect cycles on slow cameras.
fn post_keepalive_preview_state(idle_disconnect: bool, wake_lock_idle: bool) -> PreviewState {
	if idle_disconnect && wake_lock_idle {
		PreviewState::Sleeping
	} else {
		PreviewState::Connecting
	}
}

/// Outcome of a single keepalive probe tick, folded into the counter
/// update that `keepalive_loop` performs. Pure data — extracted so the
/// decision table can be unit-tested without standing up a live
/// `BcCamera` + `keepalive_probe()` round-trip.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KeepaliveTickOutcome {
	/// Probe succeeded. Reset the failure counter.
	Ok,
	/// Probe failed or timed out. Caller should increment the counter
	/// and compare against `MAX_FAILURES`.
	Failed,
}

/// Fold a `timeout(..., keepalive_probe()).await` outcome into a
/// [`KeepaliveTickOutcome`].
///
/// `Ok(Ok(_))` is success; everything else (any protocol error, any
/// timeout) is a failure. The "camera is alive but doesn't speak
/// link-type" special case used to live here — it now lives one layer
/// down in [`CameraDriver::keepalive_probe`], which matches on
/// `Error::UnintelligibleReply` directly instead of stringifying.
pub(crate) fn classify_keepalive_tick<T, E>(
	outcome: std::result::Result<std::result::Result<T, E>, tokio::time::error::Elapsed>,
) -> KeepaliveTickOutcome {
	match outcome {
		Ok(Ok(_)) => KeepaliveTickOutcome::Ok,
		Ok(Err(_)) => KeepaliveTickOutcome::Failed,
		Err(_) => KeepaliveTickOutcome::Failed,
	}
}

/// Update the running failure counter given a new tick outcome, and
/// report whether the loop should break (consecutive failures reached
/// `max_failures`).
///
/// Pure — consumed by `keepalive_loop` to keep its body small enough
/// to audit. Not part of the `CameraHandle` impl so unit tests can
/// construct scenarios with a single `u32`.
pub(crate) fn advance_keepalive_counter(
	consecutive_failures: u32,
	outcome: KeepaliveTickOutcome,
	max_failures: u32,
) -> (u32, bool) {
	match outcome {
		KeepaliveTickOutcome::Ok => (0, false),
		KeepaliveTickOutcome::Failed => {
			let next = consecutive_failures.saturating_add(1);
			(next, next >= max_failures)
		}
	}
}

// ── CameraHandle ──────────────────────────────────────────────────────

pub struct CameraHandle {
	config: CameraConfig,
	cancel: CancellationToken,
	wake_lock: WakeLockCounter,
	state: Arc<RwLock<CameraState>>,
	bc_camera: std::sync::RwLock<Option<Arc<dyn CameraDriver>>>,
	/// Concrete `BcCamera` handle kept in parallel with `bc_camera`.
	/// `stream_source.rs` still needs the concrete type (out of Phase
	/// 3B scope per design), so the trait-object stored above is
	/// accompanied by the same `Arc<BcCamera>` for stream spawns.
	bc_camera_concrete: std::sync::RwLock<Option<Arc<BcCamera>>>,
	mqtt_client: Option<bairelay_mqtt::SharedMqttClient>,
	/// MQTT topic prefix (e.g. `"bairelay"` or `"neolink"`) propagated
	/// from the config so every per-camera task publishes under the
	/// same root. Holds the default `"bairelay"` when MQTT is disabled
	/// — no task consults it in that case.
	topic_prefix: String,
	disconnect_signal: Arc<Notify>,
	stream_sources: std::sync::RwLock<HashMap<RtspStreamKind, Arc<StreamSource>>>,
	/// Serialises the create-and-insert path in [`Self::stream_source`] so
	/// two concurrent SETUPs for the same camera don't each spawn a
	/// [`StreamSource`] (Reolink cameras respond badly to duplicate
	/// `start_video` commands).
	stream_source_create_lock: tokio::sync::Mutex<()>,
	/// Shared last-frame buffer (video burst + MQTT JPEG). Owned by the
	/// camera handle so it persists across stream-source lifetimes; every
	/// [`StreamSource`] spawned for this camera writes into this same
	/// buffer, and the MQTT preview poller reads from it without triggering
	/// a fresh camera snapshot (which would wake battery cameras every tick).
	last_frame_main: Arc<LastFrameBuffer>,
	/// Opt-in fixture-capture configuration. Cloned (cheap `Arc` bump) into
	/// every [`StreamSource`] so reader tasks can mirror raw `BcMedia`
	/// packets to disk. `None` when the operator did not pass
	/// `--dump-bcmedia` on the CLI.
	bcmedia_dump: Option<Arc<BcMediaDumpConfig>>,
	/// Per-camera audio presence. Starts `Unknown`; flipped by the
	/// stream-source reader task on first observed audio packet and by
	/// `startup_wake::warm_one` when its observation window expires.
	/// Read by `CameraProvider::subscribe` to decide whether to wait for
	/// audio SDP before returning a subscription.
	audio_presence: Arc<RwLock<AudioPresence>>,
	/// Cached camera capabilities (PTZ support, etc.), populated once
	/// per successful connect via `BcCamera::get_support`. `None` until
	/// the first connect completes or until the support query
	/// fails-open to default. Read by the HA MQTT discovery publisher
	/// to gate capability-dependent entities.
	capabilities: std::sync::RwLock<Option<CameraCapabilities>>,
	/// Cached PTZ preset list `(id, name)` populated at connect from
	/// `BcCamera::get_ptz_preset()`. Drives the `PtPreset` HA discovery
	/// `select` entity and the dispatcher's name → id resolution for
	/// the `control/ptz/preset` topic. Empty when get_ptz_preset
	/// failed (e.g. fixed-mount camera that returns InvalidXml).
	preset_cache: std::sync::RwLock<Vec<(u8, String)>>,
	/// Optional HA MQTT discovery publisher. `None` when the config
	/// has no `[mqtt.discovery]` table — in that case
	/// [`Self::publish_discovery`] and [`Self::unpublish_discovery`]
	/// are no-ops. Clone-cheap (`SharedMqttClient` is internally
	/// refcounted).
	discovery_publisher: Option<bairelay_mqtt::DiscoveryPublisher>,
	/// Per-camera preview-state broadcaster. Starts `Sleeping`; the
	/// camera connect loop flips it to `Connecting` / `Live` as it
	/// progresses. The MQTT preview publisher subscribes
	/// via [`Self::preview_state_rx`] to overlay captions on the
	/// cached JPEG. `watch::Sender` is not `Clone`, so it lives
	/// behind an `Arc` to share across callers of
	/// [`Self::set_preview_state`]. Written by the connect loop on
	/// each lifecycle transition.
	preview_state_tx: Arc<watch::Sender<PreviewState>>,
	preview_state_rx: watch::Receiver<PreviewState>,
	/// Per-camera cache of the most recently published MQTT status
	/// values (battery, motion, floodlight, floodlight_tasks, pir).
	/// Threaded into every status-publishing task so each successful
	/// publish updates the cache; read by `republish_cached_status`
	/// on every broker ConnAck so HA recovers full state even when
	/// the broker has lost retained messages (no-persistence config,
	/// HA-Mosquitto-add-on restart, etc.). See `src/status_cache.rs`.
	status_cache: Arc<StatusCache>,
	/// Global `Config::stream_prune_grace_secs` propagated by the
	/// orchestrator at construction. Threaded through to
	/// [`crate::config::resolve_idle_disconnect_timeout`] so the floor
	/// guard can clamp `idle_disconnect_timeout_secs` if an operator
	/// configures it shorter than the prune grace. Defaults to
	/// [`Duration::ZERO`] in test constructors that don't go through
	/// the orchestrator — the floor only fires when configured grace
	/// is strictly less than this, so zero disables it for tests that
	/// want short-grace timing.
	prune_grace: Duration,
}

impl CameraHandle {
	pub fn new(
		config: CameraConfig,
		parent_cancel: CancellationToken,
		mqtt_client: Option<bairelay_mqtt::SharedMqttClient>,
	) -> Self {
		Self::with_bcmedia_dump(config, parent_cancel, mqtt_client, None)
	}

	/// Constructor variant that lets callers attach a shared
	/// [`BcMediaDumpConfig`]. Use [`CameraHandle::new`] for the common case;
	/// the orchestrator calls this constructor directly when the operator
	/// passed `--dump-bcmedia` on the CLI.
	///
	/// Uses the default MQTT topic prefix `"bairelay"`. To override (for
	/// neolink-legacy migration, for example), use
	/// [`CameraHandle::with_bcmedia_dump_and_prefix`].
	pub fn with_bcmedia_dump(
		config: CameraConfig,
		parent_cancel: CancellationToken,
		mqtt_client: Option<bairelay_mqtt::SharedMqttClient>,
		bcmedia_dump: Option<Arc<BcMediaDumpConfig>>,
	) -> Self {
		Self::with_bcmedia_dump_and_prefix(
			config,
			parent_cancel,
			mqtt_client,
			"bairelay".to_string(),
			bcmedia_dump,
		)
	}

	/// Full constructor that also takes the `mqtt.topic_prefix` config
	/// value. All other constructors funnel through this one.
	pub fn with_bcmedia_dump_and_prefix(
		config: CameraConfig,
		parent_cancel: CancellationToken,
		mqtt_client: Option<bairelay_mqtt::SharedMqttClient>,
		topic_prefix: String,
		bcmedia_dump: Option<Arc<BcMediaDumpConfig>>,
	) -> Self {
		let (preview_state_tx, preview_state_rx) = watch::channel(PreviewState::Sleeping);
		let preview_state_tx = Arc::new(preview_state_tx);
		Self {
			config,
			cancel: parent_cancel.child_token(),
			wake_lock: WakeLockCounter::new(),
			state: Arc::new(RwLock::new(CameraState::Disconnected)),
			bc_camera: std::sync::RwLock::new(None),
			bc_camera_concrete: std::sync::RwLock::new(None),
			mqtt_client,
			topic_prefix,
			disconnect_signal: Arc::new(Notify::new()),
			stream_sources: std::sync::RwLock::new(HashMap::new()),
			stream_source_create_lock: tokio::sync::Mutex::new(()),
			last_frame_main: Arc::new(LastFrameBuffer::new()),
			bcmedia_dump,
			audio_presence: Arc::new(RwLock::new(AudioPresence::Unknown)),
			capabilities: std::sync::RwLock::new(None),
			preset_cache: std::sync::RwLock::new(Vec::new()),
			discovery_publisher: None,
			preview_state_tx,
			preview_state_rx,
			status_cache: Arc::new(StatusCache::default()),
			prune_grace: Duration::ZERO,
		}
	}

	/// Builder setter: propagate the configured
	/// `Config::stream_prune_grace_secs` so the floor enforcement in
	/// [`crate::config::resolve_idle_disconnect_timeout`] has the right
	/// reference value. Called by the orchestrator after construction.
	#[must_use]
	pub fn with_prune_grace(mut self, prune_grace: Duration) -> Self {
		self.prune_grace = prune_grace;
		self
	}

	/// Builder setter: attach an HA MQTT discovery publisher. Only
	/// meaningful when the binary was built with `[mqtt.discovery]` in
	/// its config. Consumes and returns `self` so callers can chain
	/// this at construction time before wrapping in `Arc`. The
	/// orchestrator is the sole production caller — see
	/// `Orchestrator::with_bcmedia_dump`.
	#[must_use]
	pub fn with_discovery_publisher(
		mut self,
		publisher: bairelay_mqtt::DiscoveryPublisher,
	) -> Self {
		self.discovery_publisher = Some(publisher);
		self
	}

	/// Handle to the shared last-frame buffer for this camera's Main stream.
	///
	/// Safe to call at any time (before/after connect). Reads are cheap so
	/// the MQTT preview poller can query this on a timer without incurring
	/// a camera-side snapshot round-trip.
	pub fn last_frame_main(&self) -> Arc<LastFrameBuffer> {
		Arc::clone(&self.last_frame_main)
	}

	/// Shared handle on this camera's audio presence state. Writers call
	/// `.write()`, subscribers call `.read()`.
	pub fn audio_presence(&self) -> Arc<RwLock<AudioPresence>> {
		Arc::clone(&self.audio_presence)
	}

	/// Current cached capabilities (PTZ support, etc.). Returns `None`
	/// until the first successful connect populates the cache; callers
	/// must treat `None` as "caps not yet known" and either defer
	/// capability-gated work or fall back to defaults. Returning a
	/// `Copy` by value keeps the lock guard off the call site.
	pub fn capabilities(&self) -> Option<CameraCapabilities> {
		*self.capabilities.read_recover()
	}

	/// Snapshot of the cached PTZ preset list. Empty when the camera
	/// hasn't been queried yet, lacks PTZ support, or returned an
	/// invalid PtzPreset XML. Cloned to drop the lock immediately.
	pub fn preset_cache(&self) -> Vec<(u8, String)> {
		self.preset_cache.read_recover().clone()
	}

	/// Test-only helper to install a synthetic preset list. Production
	/// code populates this via `get_ptz_preset()` at connect.
	#[doc(hidden)]
	pub fn set_preset_cache_for_test(&self, presets: Vec<(u8, String)>) {
		*self.preset_cache.write_recover() = presets;
	}

	/// Replace the cached preset list. Production callers are the
	/// connect path (initial populate via `get_support` / `get_ptz_preset`)
	/// and the `query/ptz/preset` MQTT handler (mid-session refresh
	/// after `bairelay ptz assign` or an in-app rename).
	pub fn replace_preset_cache(&self, presets: Vec<(u8, String)>) {
		*self.preset_cache.write_recover() = presets;
	}

	/// Resolve a preset name to its numeric id via the cache. Returns
	/// `None` when the name isn't in the list (cache empty / unknown
	/// preset). Case-insensitive — HA's select normalises options but
	/// firmwares vary in capitalisation across reboots.
	pub fn preset_id_for_name(&self, name: &str) -> Option<u8> {
		let cache = self.preset_cache.read_recover();
		cache
			.iter()
			.find(|(_, n)| n.eq_ignore_ascii_case(name))
			.map(|(id, _)| *id)
	}

	/// Inverse of [`Self::preset_id_for_name`]. Used by the dispatcher
	/// to publish the preset name on `status/ptz_preset` after a
	/// numeric-id move so HA's select reflects the new state.
	pub fn preset_name_for_id(&self, preset_id: u8) -> Option<String> {
		let cache = self.preset_cache.read_recover();
		cache
			.iter()
			.find(|(id, _)| *id == preset_id)
			.map(|(_, n)| n.clone())
	}

	/// Subscribe to this camera's preview-state feed. The returned
	/// [`watch::Receiver`] yields the current [`PreviewState`] on
	/// subscribe and every subsequent transition. Initial value is
	/// [`PreviewState::Sleeping`] until the connect loop runs.
	pub fn preview_state_rx(&self) -> watch::Receiver<PreviewState> {
		self.preview_state_rx.clone()
	}

	/// Update the preview-state channel. Called by the connect loop
	/// on each lifecycle transition. Send errors (no receivers) are
	/// intentionally ignored — the overlay subscriber is optional.
	pub(crate) fn set_preview_state(&self, s: PreviewState) {
		let _ = self.preview_state_tx.send(s);
	}

	/// Publish retained HA discovery config payloads for this camera.
	///
	/// Early-returns `Ok(())` when either (a) no
	/// [`DiscoveryPublisher`] is attached (operator did not opt in
	/// via `[mqtt.discovery]`), or (b) the capability cache is still
	/// `None` (first successful connect has not completed, or the
	/// `get_support` call failed and the next reconnect will re-probe).
	/// In both cases no MQTT traffic is generated.
	///
	/// Intended to be called post-first-connect and on every MQTT
	/// `ConnAck`. Safe to call repeatedly — retained publishes are
	/// idempotent and HA deduplicates by `unique_id`.
	pub async fn publish_discovery(&self) -> Result<(), bairelay_mqtt::MqttError> {
		let Some(publisher) = self.discovery_publisher.as_ref() else {
			return Ok(());
		};
		let Some(caps) = self.capabilities() else {
			return Ok(());
		};
		let flags = bairelay_mqtt::CameraEnableFlags::from(&self.config.mqtt);
		let presets = self.preset_cache();
		publisher
			.publish(
				&self.config.name,
				self.config.address.as_deref(),
				self.config.uid.as_deref(),
				caps.into(),
				&flags,
				&presets,
			)
			.await
	}

	/// Test-only helper: install a synthetic capability set without
	/// going through the connect path. Exposed as `pub` (gated on
	/// `#[doc(hidden)]`) so integration tests in `tests/*.rs` can
	/// construct a camera with populated caps. Production code must
	/// not call this.
	#[doc(hidden)]
	pub fn set_capabilities_for_test(&self, caps: CameraCapabilities) {
		*self.capabilities.write_recover() = Some(caps);
	}

	/// Shared per-camera status cache. Each status-publishing task
	/// (battery / motion / floodlight / floodlight_tasks / pir)
	/// receives a clone of this Arc and writes the just-published
	/// value after every successful publish. Read by
	/// [`Self::republish_cached_status`] on every broker reconnect.
	pub fn status_cache(&self) -> Arc<StatusCache> {
		Arc::clone(&self.status_cache)
	}

	/// Re-emit every cached MQTT status value via retained publishes.
	/// Called by `mqtt_loop::handle_connack` after the discovery
	/// republish so HA recovers full state when the broker has lost
	/// retained messages (no-persistence config, broker restart).
	///
	/// No-op when the camera has no MQTT client attached or when no
	/// status has ever been published yet (cache fully empty).
	pub async fn republish_cached_status(&self) -> Result<(), bairelay_mqtt::MqttError> {
		let Some(ref mqtt) = self.mqtt_client else {
			return Ok(());
		};
		let publisher =
			bairelay_mqtt::StatusPublisher::new(mqtt, &self.topic_prefix, &self.config.name);
		if let Some(level) = self.status_cache.battery_level() {
			publisher.publish_battery_level(level).await?;
		}
		if let Some(motion) = self.status_cache.motion() {
			publisher.publish_motion(motion).await?;
		}
		if let Some(on) = self.status_cache.floodlight() {
			publisher.publish_floodlight(on).await?;
		}
		if let Some(enabled) = self.status_cache.floodlight_tasks() {
			publisher.publish_floodlight_tasks_enabled(enabled).await?;
		}
		if let Some(enabled) = self.status_cache.pir() {
			publisher.publish_pir(enabled).await?;
		}
		Ok(())
	}

	/// Remove retained HA discovery config payloads for this camera
	/// by publishing an empty retained payload on every topic
	/// `publish_discovery` would emit. Same early-return rules apply.
	/// Intended for graceful shutdown so HA clears the entity set.
	pub async fn unpublish_discovery(&self) -> Result<(), bairelay_mqtt::MqttError> {
		let Some(publisher) = self.discovery_publisher.as_ref() else {
			return Ok(());
		};
		let Some(caps) = self.capabilities() else {
			return Ok(());
		};
		let flags = bairelay_mqtt::CameraEnableFlags::from(&self.config.mqtt);
		let presets = self.preset_cache();
		publisher
			.unpublish(
				&self.config.name,
				self.config.address.as_deref(),
				self.config.uid.as_deref(),
				caps.into(),
				&flags,
				&presets,
			)
			.await
	}

	pub fn state(&self) -> CameraState {
		*self.state.read_recover()
	}

	pub fn wake_lock(&self) -> &WakeLockCounter {
		&self.wake_lock
	}

	pub fn is_cancelled(&self) -> bool {
		self.cancel.is_cancelled()
	}

	pub fn name(&self) -> &str {
		&self.config.name
	}

	/// MQTT topic prefix configured for this camera
	/// (`mqtt.topic_prefix` in the config; default `"bairelay"`).
	pub fn topic_prefix(&self) -> &str {
		&self.topic_prefix
	}

	pub fn config(&self) -> &CameraConfig {
		&self.config
	}

	pub fn cancel_token(&self) -> &CancellationToken {
		&self.cancel
	}

	/// Returns the currently-connected camera as a trait object, if any.
	///
	/// Returning `Arc<dyn CameraDriver>` (rather than the concrete
	/// `BcCamera`) lets unit tests substitute a fake camera at this
	/// boundary without touching the binary's call-sites.
	pub fn bc_camera(&self) -> Option<Arc<dyn CameraDriver>> {
		self.bc_camera.read_recover().clone()
	}

	/// Signal the camera to disconnect (used by the watchdog for idle cameras).
	pub fn request_disconnect(&self) {
		self.disconnect_signal.notify_one();
	}

	/// Test-only accessor that returns a clone of the disconnect-signal
	/// `Notify`. Tests can `.notified().await` (with a small timeout)
	/// to assert that `request_disconnect` actually fired — much
	/// stronger than asserting the function "didn't panic".
	#[cfg(test)]
	pub(crate) fn disconnect_signal_for_test(&self) -> Arc<Notify> {
		Arc::clone(&self.disconnect_signal)
	}

	fn set_state(&self, new: CameraState) {
		*self.state.write_recover() = new;
	}

	/// Get (or lazily create) the [`StreamSource`] for the given stream kind.
	///
	/// Waits up to 30 seconds for the camera to reach `Connected` state,
	/// polling every 100 ms. Once connected, the source is started via
	/// `StreamSource::start` and cached in the per-camera registry so
	/// subsequent subscribers share one Baichuan video stream.
	pub async fn stream_source(
		&self,
		kind: RtspStreamKind,
	) -> Result<Arc<StreamSource>, StreamError> {
		// Poll for Connected state up to 30s. Scope guards tightly so no
		// RwLock guard is held across `.await` (clippy::await_holding_lock).
		const POLL_INTERVAL: Duration = Duration::from_millis(100);
		const MAX_WAIT: Duration = Duration::from_secs(30);
		let deadline = std::time::Instant::now() + MAX_WAIT;
		loop {
			if self.cancel.is_cancelled() {
				return Err(StreamError::Unavailable(
					"camera task shutting down".to_string(),
				));
			}
			if self.state() == CameraState::Connected {
				break;
			}
			if std::time::Instant::now() >= deadline {
				return Err(StreamError::Unavailable(format!(
					"camera '{}' did not reach Connected state within {}s",
					self.config.name,
					MAX_WAIT.as_secs()
				)));
			}
			tokio::time::sleep(POLL_INTERVAL).await;
		}

		// Fast path: already registered.
		{
			let guard = self.stream_sources.read_recover();
			if let Some(source) = guard.get(&kind) {
				return Ok(Arc::clone(source));
			}
		}

		// Slow path: hold the async create-mutex across the start + insert
		// so two concurrent callers can't both spawn a StreamSource (and
		// thus both call `start_video` on the camera). The loser awaits the
		// mutex, re-checks the map under the lock, and reuses the winner's
		// source — no duplicate start_video is ever issued.
		let _create_guard = self.stream_source_create_lock.lock().await;

		// Re-check under the mutex: another caller may have just created it.
		{
			let guard = self.stream_sources.read_recover();
			if let Some(source) = guard.get(&kind) {
				return Ok(Arc::clone(source));
			}
		}

		// State re-check under the create-lock: closes the TOCTOU
		// between the polling loop above and the bc_camera_concrete
		// read below. While we waited for the create-lock, the
		// keepalive loop may have failed → `run_connected_session`
		// proceeded to `stop_all_stream_sources()` → just-cleared the
		// registry. Without this guard we would happily insert a new
		// source into the now-empty registry and spawn a reader against
		// a camera that's about to be dropped. Returning Unavailable
		// here surfaces "RTSP SETUP arrived during a teardown window"
		// to the client cleanly — the next SETUP will land in the next
		// session.
		if self.cancel.is_cancelled() || self.state() != CameraState::Connected {
			return Err(StreamError::Unavailable(format!(
				"camera '{}' is no longer in Connected state",
				self.config.name
			)));
		}

		// Need to start a new source. Grab the concrete BcCamera handle —
		// StreamSource still takes `Arc<BcCamera>` (out of scope
		// per design).
		let camera = self
			.bc_camera_concrete
			.read_recover()
			.clone()
			.ok_or_else(|| {
				StreamError::Unavailable(format!(
					"camera '{}' is not currently connected",
					self.config.name
				))
			})?;

		// Start the source. `StreamSource::start` only spawns a task and
		// returns immediately, so it's cheap to do while the async mutex
		// is held. We pass the camera-scoped last-frame buffer so every
		// source for this camera writes to the same buffer (one JPEG per
		// camera, not per stream).
		//
		// Gap threshold: Task 5 will read this in
		// `reader_task` to drive placeholder-frame emission when the
		// camera goes silent. `bridge_gaps = false` folds to
		// `Duration::MAX` so the ticker never fires — no separate
		// "disabled" code path needed.
		let gap_threshold = if self.config.pause.bridge_gaps {
			Duration::from_secs_f64(self.config.pause.gap_threshold_secs)
		} else {
			Duration::MAX
		};
		let source = StreamSource::start(
			camera,
			self.config.name.clone(),
			kind,
			Arc::clone(&self.last_frame_main),
			self.bcmedia_dump.clone(),
			self.audio_presence(),
			gap_threshold,
		);

		// Insert the new source under the sync lock.
		{
			let mut guard = self.stream_sources.write_recover();
			guard.insert(kind, Arc::clone(&source));
		}
		Ok(source)
		// `_create_guard` drops here, unblocking any waiter who will then
		// observe our entry via the re-check.
	}

	/// Remove and stop any [`StreamSource`]s whose broadcast channels have
	/// been without subscribers for at least `grace`. Called by the
	/// watchdog / idle sweeps.
	///
	/// Accepts an explicit `now` and `grace`, enabling
	/// virtual-clock unit tests for the grace window. Production callers
	/// (watchdog) thread the configured `stream_prune_grace_secs` here.
	///
	/// A source is pruned only when it has observed zero subscribers for
	/// at least `grace`. This smooths rapid RTSP disconnect/reconnect so
	/// we don't pay a cold `BcCamera::start_video` handshake on every
	/// reconnect.
	///
	/// A `grace` of zero restores the legacy instantaneous-prune behaviour.
	pub(crate) fn prune_idle_stream_sources_at(&self, now: Instant, grace: Duration) {
		let mut guard = self.stream_sources.write_recover();
		guard.retain(|_kind, source| {
			let subs = source.subscribers();
			let mut marker = source.last_idle_since.lock_recover();
			if subs > 0 {
				*marker = None;
				return true;
			}
			match *marker {
				None => {
					*marker = Some(now);
					// If grace is zero, expire immediately on this sweep.
					if grace.is_zero() {
						// Drop the marker guard before stop/remove to keep
						// the Mutex<Option<Instant>> from being re-locked
						// via the Drop path (belt-and-braces — `Instant` /
						// `Option` have no custom Drop today).
						drop(marker);
						source.stop();
						return false;
					}
					true
				}
				Some(since) if now.saturating_duration_since(since) >= grace => {
					drop(marker);
					source.stop();
					false
				}
				Some(_) => true,
			}
		});
	}

	/// Returns `true` if any currently-registered [`StreamSource`] for
	/// this camera reports [`GapState::Bridging`]. Used by the
	/// per-camera preview-state aggregator to decide
	/// whether to downgrade `PreviewState::Live` → `Connecting` for the
	/// MQTT overlay while upstream frames are stalled.
	pub(crate) fn any_stream_source_bridging(&self) -> bool {
		let guard = self.stream_sources.read_recover();
		guard
			.values()
			.any(|s| s.gap_state() == crate::stream_source::GapState::Bridging)
	}

	/// Stop every registered stream source and clear the registry. Called
	/// during session teardown so no reader task outlives the camera
	/// connection that feeds it.
	///
	/// Awaits each source's reader task with a hard timeout so a
	/// detached reader cannot keep an `Arc<BcCamera>` clone alive past
	/// teardown — that would race the next session's `start_video` on
	/// the same camera. The timeout matches the reader's own
	/// `STOP_VIDEO_TIMEOUT` (5 s) plus a margin: a wedged reader still
	/// gets dropped from the registry, just with a `warn` so the
	/// operator can see the leak.
	async fn stop_all_stream_sources(&self) {
		// Drain entries under the write lock; await outside the lock
		// so we don't hold a sync `RwLockWriteGuard` across `.await`.
		let drained: Vec<(RtspStreamKind, Arc<StreamSource>)> = {
			let mut guard = self.stream_sources.write_recover();
			let entries: Vec<_> = guard.drain().collect();
			entries
		};
		for (kind, source) in drained {
			if let Err(e) = source.stop_and_wait(Self::STREAM_SOURCE_STOP_TIMEOUT).await {
				tracing::warn!(
					camera = %self.config.name,
					stream = ?kind,
					error = %e,
					"stream-source reader did not exit within budget; detached (still holds Arc<BcCamera> until it observes cancel)",
				);
			}
		}
	}

	/// Attempt to connect and login to the camera.
	async fn try_connect(&self) -> anyhow::Result<Arc<BcCamera>> {
		let opts = crate::bc_opts::build_bc_opts(&self.config);
		let max_enc = crate::bc_opts::max_encryption(&self.config);
		let camera = BcCamera::new(&opts).await?;
		camera.login_with_maxenc(max_enc).await?;

		Ok(Arc::new(camera))
	}

	/// Send periodic keepalive probes to detect connection loss.
	/// Uses [`CameraDriver::keepalive_probe`] (a lenient wrapper over
	/// `get_linktype()` matching neolink's choice) and tolerates up to
	/// [`Self::KEEPALIVE_MAX_FAILURES`] consecutive failures before
	/// disconnecting. Cadence and per-probe timeout are both
	/// [`Self::KEEPALIVE_INTERVAL`].
	async fn keepalive_loop(&self, camera: &dyn CameraDriver, session_cancel: &CancellationToken) {
		let mut interval = tokio::time::interval(Self::KEEPALIVE_INTERVAL);
		let mut consecutive_failures: u32 = 0;

		'outer: loop {
			tokio::select! {
				_ = self.cancel.cancelled() => break,
				_ = session_cancel.cancelled() => {
					tracing::info!(camera = %self.config.name, "Session cancelled (grace period or disconnect)");
					break;
				}
				_ = self.disconnect_signal.notified() => {
					tracing::info!(camera = %self.config.name, "Watchdog requested disconnect");
					break;
				}
				_ = interval.tick() => {
					// Race the probe against cancel / disconnect signals so a
					// shutdown that arrives mid-probe preempts immediately
					// instead of stalling for the full
					// `KEEPALIVE_PROBE_TIMEOUT`. Without the inner select,
					// shutdown latency was bounded by the per-probe timeout
					// (previously equal to the interval, so up to 5 s).
					let raw = tokio::select! {
						v = tokio::time::timeout(Self::KEEPALIVE_PROBE_TIMEOUT, camera.keepalive_probe()) => v,
						_ = self.cancel.cancelled() => break 'outer,
						_ = session_cancel.cancelled() => {
							tracing::info!(
								camera = %self.config.name,
								"Session cancelled mid-probe"
							);
							break 'outer;
						}
						_ = self.disconnect_signal.notified() => {
							tracing::info!(
								camera = %self.config.name,
								"Watchdog requested disconnect mid-probe"
							);
							break 'outer;
						}
					};
					let outcome = classify_keepalive_tick(raw);
					let (next, should_break) = advance_keepalive_counter(
						consecutive_failures,
						outcome,
						Self::KEEPALIVE_MAX_FAILURES,
					);
					consecutive_failures = next;
					if outcome == KeepaliveTickOutcome::Failed {
						tracing::debug!(
							camera = %self.config.name,
							failures = consecutive_failures,
							"Keepalive probe failed",
						);
					}
					if should_break {
						tracing::warn!(
							camera = %self.config.name,
							"Keepalive failed {} times, disconnecting",
							Self::KEEPALIVE_MAX_FAILURES,
						);
						break;
					}
				}
			}
		}
	}

	/// Interval between keepalive probes. 5 s matches neolink and is
	/// well below the camera's own idle threshold for sleep-mode
	/// battery devices.
	pub(crate) const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(5);
	/// Per-probe timeout: strictly below [`Self::KEEPALIVE_INTERVAL`]
	/// so a slow probe doesn't consume the entire tick budget, AND
	/// races against cancel signals (see `keepalive_loop`) so shutdown
	/// preempts a probe in flight. 3 s is a comfortable upper bound on
	/// `get_linktype` latency observed on real Argus hardware (typical
	/// is under 200 ms).
	pub(crate) const KEEPALIVE_PROBE_TIMEOUT: Duration = Duration::from_secs(3);
	/// Hard cap on the per-session courtesy `logout()` call during
	/// teardown. Matches `docs/implementation.md` § "Calls that spawn
	/// internal tasks" step 4. A wedged camera that doesn't reply to
	/// `logout` must not stall the next reconnect cycle.
	const LOGOUT_TIMEOUT: Duration = Duration::from_secs(5);
	/// Hard cap on awaiting a stream-source reader task during session
	/// teardown. Reader's own `STOP_VIDEO_TIMEOUT` is 5 s; we add a
	/// small margin so a clean stop_video has time to settle before we
	/// declare the reader detached.
	const STREAM_SOURCE_STOP_TIMEOUT: Duration = Duration::from_secs(7);
	/// Maximum consecutive probe failures before the loop gives up and
	/// lets the outer reconnect loop take over.
	pub(crate) const KEEPALIVE_MAX_FAILURES: u32 = 5;

	/// Drive the "just-connected" lifecycle: publish status, capability
	/// probe, session-task spawn, keepalive loop, graceful teardown.
	///
	/// Extracted from `run()` so tests can drive the whole thing with a
	/// scripted `Arc<dyn CameraDriver>` + `concrete: None`. When
	/// `concrete` is `Some`, stream-source spawns via
	/// [`Self::stream_source`] work and logout is attempted at the end
	/// of teardown; when `None` (test mode) both paths are skipped.
	async fn run_connected_session(
		self: &Arc<Self>,
		driver: Arc<dyn CameraDriver>,
		concrete: Option<Arc<BcCamera>>,
	) {
		self.set_state(CameraState::Connected);
		self.set_preview_state(PreviewState::Live);
		tracing::info!(camera = %self.config.name, "Connected");

		// Publish connected status via MQTT.
		if let Some(ref mqtt) = self.mqtt_client {
			let publisher =
				bairelay_mqtt::StatusPublisher::new(mqtt, &self.topic_prefix, &self.config.name);
			if let Err(e) = publisher.publish_connection(true).await {
				tracing::warn!(camera = %self.config.name, error = %e, "Failed to publish connected status");
			}
		}

		*self.bc_camera.write_recover() = Some(Arc::clone(&driver));
		*self.bc_camera_concrete.write_recover() = concrete.clone();

		// Populate the capability cache for HA discovery.
		// `get_support()` is idempotent and read-only. On success we
		// cache the view; on failure we leave the cache `None` so the
		// next reconnect re-probes — a transient `get_support` error
		// must not stick `has_ptz = false` for the whole session (HA
		// discovery would then never emit PT buttons even for a PT-
		// capable cam that only fluked one probe).
		match driver.get_support().await {
			Ok(support) => {
				let has_ptz = crate::capabilities::ptz_mode_indicates_ptz(
					support.ptz_mode.as_deref(),
					support.ptz_cfg,
				);
				*self.capabilities.write_recover() = Some(CameraCapabilities { has_ptz });

				// Populate the preset cache for HA discovery's
				// `PtPreset` select. Only meaningful when has_ptz
				// — fixed-mount cameras return InvalidXml. Treat
				// any read error as "no presets" (empty Vec) and
				// keep the discovery publish going; the select
				// just won't be emitted.
				if has_ptz {
					match driver.get_ptz_preset().await {
						Ok(p) => {
							let presets: Vec<(u8, String)> = p
								.preset_list
								.preset
								.into_iter()
								.filter_map(|preset| preset.name.map(|n| (preset.id, n)))
								.collect();
							*self.preset_cache.write_recover() = presets;
						}
						Err(e) => {
							tracing::debug!(
								camera = %self.config.name,
								error = %e,
								"get_ptz_preset failed; leaving preset cache empty"
							);
						}
					}
				}

				if let Err(e) = self.publish_discovery().await {
					tracing::warn!(
						camera = %self.config.name,
						error = %e,
						"Failed to publish HA discovery payloads post-connect; will retry on next ConnAck"
					);
				}
			}
			Err(e) => {
				tracing::warn!(
					camera = %self.config.name,
					error = %e,
					"Failed to query camera capabilities via get_support(); leaving cache empty for re-probe on next reconnect"
				);
			}
		}

		// Spawn per-session tasks (motion, pollers, grace period).
		let session_cancel = CancellationToken::new();
		let tasks = self.spawn_session_tasks(&driver, &session_cancel).await;

		self.keepalive_loop(&*driver, &session_cancel).await;

		// Close the PreviewState gap: the Live arm is over. Sleeping
		// only if the next iteration will park; otherwise Connecting
		// so a slow reconnect doesn't flash SLEEPING on the HA
		// overlay.
		self.set_preview_state(post_keepalive_preview_state(
			self.config.idle_disconnect,
			self.wake_lock.is_idle(),
		));

		// Connection lost or shutdown -- cancel all session tasks
		// and tear down everything that depends on the live driver.
		session_cancel.cancel();
		self.teardown_session_tasks(tasks, concrete).await;
	}

	/// Spawn the per-session tasks that hold their own clones of the
	/// driver: preview-state aggregator + motion / battery / floodlight
	/// pollers + PIR one-shot publish + grace-period watcher (battery
	/// cameras only). All tasks honour `session_cancel`. Returns the
	/// `JoinSet` so the caller can drive its own shutdown sequence after
	/// `keepalive_loop` returns.
	async fn spawn_session_tasks(
		self: &Arc<Self>,
		driver: &Arc<dyn CameraDriver>,
		session_cancel: &CancellationToken,
	) -> tokio::task::JoinSet<()> {
		let mut tasks = tokio::task::JoinSet::new();

		// Preview-state aggregator.
		{
			let handle = Arc::clone(self);
			let cancel = session_cancel.clone();
			tasks.spawn(async move {
				aggregate_preview_state_loop(handle, cancel).await;
			});
		}

		if let Some(ref mqtt) = self.mqtt_client {
			if self.config.mqtt.enable_motion {
				tasks.spawn(camera_tasks::motion_listener(
					self.config.name.clone(),
					Arc::clone(driver),
					mqtt.clone(),
					self.topic_prefix.clone(),
					self.wake_lock.clone(),
					session_cancel.clone(),
					Duration::from_secs_f64(self.config.motion_wake_hold_secs),
					self.status_cache(),
				));
			}
			if self.config.mqtt.enable_battery {
				tasks.spawn(camera_tasks::battery_poller(
					self.config.name.clone(),
					Arc::clone(driver),
					mqtt.clone(),
					self.topic_prefix.clone(),
					self.config.mqtt.battery_update,
					session_cancel.clone(),
					self.status_cache(),
				));
			}
			if self.config.mqtt.enable_floodlight {
				tasks.spawn(camera_tasks::floodlight_poller(
					self.config.name.clone(),
					Arc::clone(driver),
					mqtt.clone(),
					self.topic_prefix.clone(),
					self.config.mqtt.floodlight_update,
					session_cancel.clone(),
					self.status_cache(),
				));
				tasks.spawn(camera_tasks::floodlight_listener(
					self.config.name.clone(),
					Arc::clone(driver),
					mqtt.clone(),
					self.topic_prefix.clone(),
					session_cancel.clone(),
					self.status_cache(),
				));
			}
			if self.config.mqtt.enable_pir {
				camera_tasks::publish_pir_state(
					self.config.name.clone(),
					Arc::clone(driver),
					mqtt.clone(),
					self.topic_prefix.clone(),
					self.status_cache(),
				)
				.await;
			}
		}

		// Grace-period watcher for battery cameras: when the wake-lock
		// drops to zero, count down `idle_disconnect_timeout_secs`
		// (default 45 s) and then cancel the session, releasing the
		// camera back to sleep.
		if self.config.idle_disconnect {
			let grace =
				crate::config::resolve_idle_disconnect_timeout(&self.config, self.prune_grace);
			let gp = GracePeriod::new(self.wake_lock.clone(), grace);
			let sc = session_cancel.clone();
			let cam_name = self.config.name.clone();
			tasks.spawn(async move {
				gp.run().await;
				tracing::info!(camera = %cam_name, "Grace period expired, disconnecting");
				sc.cancel();
			});
		}

		tasks
	}

	/// Tear down a session: drain the spawned task `JoinSet` (2 s
	/// graceful window then `abort_all`), stop every live stream
	/// source, log out via the concrete `BcCamera` handle if present,
	/// clear the camera's driver/concrete slots, and publish the
	/// disconnected MQTT status. Symmetric with
	/// [`spawn_session_tasks`]; the caller is expected to have
	/// already cancelled `session_cancel` before invoking this.
	async fn teardown_session_tasks(
		self: &Arc<Self>,
		mut tasks: tokio::task::JoinSet<()>,
		concrete: Option<Arc<BcCamera>>,
	) {
		// Give tasks a brief window to exit gracefully, then abort.
		let drain_deadline = tokio::time::sleep(Duration::from_secs(2));
		tokio::pin!(drain_deadline);
		loop {
			tokio::select! {
				result = tasks.join_next() => {
					if result.is_none() { break; } // All done
				}
				_ = &mut drain_deadline => {
					tracing::debug!(camera = %self.config.name, "Aborting remaining session tasks");
					tasks.abort_all();
					while tasks.join_next().await.is_some() {}
					break;
				}
			}
		}

		// Tear down any live stream sources before clearing the
		// BcCamera handle — they reference the camera we are about to
		// drop.
		self.stop_all_stream_sources().await;

		// Per-session courtesy logout, capped with `LOGOUT_TIMEOUT` so a
		// wedged camera can't stall teardown. Uses the local `concrete`
		// handle (the same Arc that `bc_camera_concrete` exposes) — the
		// trait surface intentionally omits `logout()` because no other
		// call-site needs it. `concrete = None` is the test-mode path
		// (`run_connected_session_for_test`); skip cleanly.
		//
		// This is the documented step-4 of the per-session shutdown
		// sequence in `docs/implementation.md` § "Calls that spawn
		// internal tasks". Pre-fix, the only `logout()` call lived in
		// `run()`'s post-loop block, but `bc_camera_concrete` was
		// always nulled before that block ran, so `cam_for_logout` was
		// always `None` and logout was unreachable in production.
		if let Some(ref cam) = concrete {
			match tokio::time::timeout(Self::LOGOUT_TIMEOUT, cam.logout()).await {
				Ok(Ok(())) => tracing::debug!(camera = %self.config.name, "Logged out"),
				Ok(Err(e)) => {
					tracing::debug!(camera = %self.config.name, error = %e, "Logout failed")
				}
				Err(_) => tracing::debug!(camera = %self.config.name, "Logout timed out, dropping"),
			}
		}

		*self.bc_camera.write_recover() = None;
		*self.bc_camera_concrete.write_recover() = None;

		// Drop the concrete handle (Arc<BcCamera>) so its backing
		// connection threads can shut down.
		drop(concrete);

		self.set_state(CameraState::Disconnected);
		tracing::info!(camera = %self.config.name, "Disconnected");

		// Publish disconnected status via MQTT.
		if let Some(ref mqtt) = self.mqtt_client {
			let publisher =
				bairelay_mqtt::StatusPublisher::new(mqtt, &self.topic_prefix, &self.config.name);
			if let Err(e) = publisher.publish_connection(false).await {
				tracing::warn!(camera = %self.config.name, error = %e, "Failed to publish disconnected status");
			}
		}
	}

	/// Main connection loop: connect, keepalive, reconnect on failure.
	pub async fn run(self: &Arc<Self>) {
		// Publish initial "disconnected" / "unknown" states before connecting.
		if let Some(ref mqtt) = self.mqtt_client {
			let publisher =
				bairelay_mqtt::StatusPublisher::new(mqtt, &self.topic_prefix, &self.config.name);
			let _ = publisher.publish_connection(false).await;
			let _ = publisher.publish_motion_unknown().await;
		}

		// Spawn the preview publisher at camera lifetime (NOT session
		// scope): it must keep publishing status/preview with the
		// current overlay caption across disconnect/sleep windows, so
		// HA dashboards see SLEEPING / CONNECTING on stale frames
		// instead of whatever was last published during Live.
		let preview_task = if let Some(ref mqtt) = self.mqtt_client {
			if self.config.mqtt.enable_preview {
				Some(tokio::spawn(camera_tasks::preview_poller(
					self.config.name.clone(),
					Arc::clone(self),
					Arc::clone(&self.last_frame_main),
					mqtt.clone(),
					self.topic_prefix.clone(),
					self.config.mqtt.preview_update,
					self.preview_state_rx(),
					self.config.pause.preview_overlay,
					self.cancel.clone(),
				)))
			} else {
				None
			}
		} else {
			None
		};

		let mut backoff = ReconnectBackoff::new(Duration::from_secs(2), Duration::from_secs(60));

		loop {
			if self.cancel.is_cancelled() {
				break;
			}

			// For battery cameras: wait for someone to need us before connecting.
			// This is the "parked" state — surface it to the preview overlay as
			// `Sleeping` so subscribers (MQTT preview publisher) can caption the
			// cached JPEG while the wake lock is zero.
			if self.config.idle_disconnect && self.wake_lock.is_idle() {
				self.set_preview_state(PreviewState::Sleeping);
				tracing::info!(camera = %self.config.name, "Idle disconnect enabled, waiting for wake lock...");
				tokio::select! {
					_ = self.cancel.cancelled() => break,
					_ = self.wake_lock.wait_for_acquire() => {
						tracing::info!(camera = %self.config.name, "Wake lock acquired, connecting...");
					}
				}
			}

			self.set_state(CameraState::Connecting);
			self.set_preview_state(PreviewState::Connecting);
			tracing::info!(camera = %self.config.name, "Connecting...");

			// Wrap connection attempt in a timeout + cancellation guard
			// so that Ctrl+C works even if bairelay_neolink_core is stuck
			let connect_result = tokio::select! {
				_ = self.cancel.cancelled() => break,
				result = tokio::time::timeout(Duration::from_secs(30), self.try_connect()) => {
					match result {
						Ok(r) => r,
						Err(_) => {
							tracing::warn!(camera = %self.config.name, "Connection timed out (30s)");
							Err(anyhow::anyhow!("Connection timed out"))
						}
					}
				}
			};
			match connect_result {
				Ok(camera) => {
					let driver: Arc<dyn CameraDriver> =
						Arc::clone(&camera) as Arc<dyn CameraDriver>;
					backoff.reset();
					self.run_connected_session(driver, Some(Arc::clone(&camera)))
						.await;
				}
				Err(e) => {
					if is_login_failure(&e) {
						tracing::error!(camera = %self.config.name, "Authentication failed — check credentials. Stopping retries.");
						self.set_state(CameraState::Disconnected);
						break;
					}
					tracing::warn!(camera = %self.config.name, error = %e, "Connection failed");
					self.set_state(CameraState::Disconnected);
				}
			}

			if !backoff.sleep_with_cancel(&self.cancel).await {
				break;
			}
		}

		// Ensure no stream sources outlive the camera handle even on
		// cancelled/early-exit paths (e.g. auth failure, cancellation
		// during backoff sleep). `run_connected_session` already
		// drained sources + logged out + cleared the BcCamera Arcs on
		// every successful session; this catches the paths that
		// bypassed `run_connected_session` entirely (auth failure,
		// timed-out connect, cancel during backoff).
		self.stop_all_stream_sources().await;

		// Camera-lifetime preview publisher: self.cancel has fired (we
		// exited the outer loop via break), so the task's select arm
		// has fired or will imminently. Await with a small timeout so
		// shutdown doesn't hang on a wedged broker.
		if let Some(task) = preview_task {
			let _ = tokio::time::timeout(Duration::from_secs(2), task).await;
		}
	}
}

/// Per-camera preview-state aggregator body. Spawned inside the
/// session scope of [`CameraHandle::run`] and torn
/// down via `session_cancel`.
///
/// Polls every 200 ms. The full state machine lives in
/// [`aggregate_preview_state`]; this loop only tracks `last_write` so
/// the pure fn can distinguish the aggregator's own `Connecting`
/// downgrade from a connect-loop-owned `Connecting` during reconnects.
async fn aggregate_preview_state_loop(
	handle: Arc<CameraHandle>,
	session_cancel: CancellationToken,
) {
	let mut ticker = tokio::time::interval(Duration::from_millis(200));
	ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
	// First tick fires immediately — skip it so we don't double-sample.
	ticker.tick().await;
	let mut last_write: Option<PreviewState> = None;
	loop {
		tokio::select! {
			_ = session_cancel.cancelled() => break,
			_ = ticker.tick() => {
				let current = *handle.preview_state_rx.borrow();
				let any_bridging = handle.any_stream_source_bridging();
				let next = aggregate_preview_state(current, any_bridging, last_write);
				if next != current {
					handle.set_preview_state(next);
					last_write = Some(next);
				} else if matches!(current, PreviewState::Sleeping)
					|| (matches!(current, PreviewState::Connecting)
						&& last_write != Some(PreviewState::Connecting))
				{
					// Connect-loop-owned state — clear our bookkeeping so
					// we don't later confuse it for our own downgrade.
					last_write = None;
				}
			}
		}
	}
}

/// Check whether an error indicates a login/credential failure that
/// should not be retried.
///
/// Walks the anyhow source chain and matches typed
/// `bairelay_neolink_core::Error::AuthFailed | CameraLoginFail`. The Debug-
/// substring fallback below catches synthesised `anyhow::anyhow!(...)`
/// errors used by some test paths and any future intermediate wrapper
/// that doesn't preserve a downcastable source — keeping it as a
/// belt-and-braces guard so a future bairelay_neolink_core error-type rename
/// doesn't silently regress to "retry auth failures forever" — the
/// brute-force scenario flagged during the strict-code audit.
fn is_login_failure(err: &anyhow::Error) -> bool {
	use bairelay_neolink_core::Error as CoreError;
	for cause in err.chain() {
		if let Some(core) = cause.downcast_ref::<CoreError>() {
			if matches!(core, CoreError::AuthFailed | CoreError::CameraLoginFail) {
				return true;
			}
		}
	}
	let msg = format!("{:?}", err);
	msg.contains("AuthFailed")
		|| msg.contains("CameraLoginFail")
		|| msg.contains("Credential error")
}

// ── ReconnectBackoff ──────────────────────────────────────────────────

pub struct ReconnectBackoff {
	initial: Duration,
	max: Duration,
	current: Duration,
}

impl ReconnectBackoff {
	pub fn new(initial: Duration, max: Duration) -> Self {
		Self {
			initial,
			max,
			current: initial,
		}
	}

	pub fn next_delay(&mut self) -> Duration {
		let delay = self.current;
		self.current = (self.current * 2).min(self.max);
		delay
	}

	pub fn reset(&mut self) {
		self.current = self.initial;
	}

	/// Sleep for the next backoff delay, racing against `cancel`.
	/// Returns `true` if the sleep completed; `false` if the
	/// cancellation token fired first. Delegates the sleep+cancel race
	/// to [`crate::run_support::sleep_or_cancel`] so the contract is
	/// shared with the MQTT event loop's reconnect path.
	pub async fn sleep_with_cancel(&mut self, cancel: &CancellationToken) -> bool {
		let delay = self.next_delay();
		crate::run_support::sleep_or_cancel(delay, cancel).await
	}
}

/// Outcome of [`drive_reconnect_with_backoff`]: either a successful
/// connect, a bail-out signal from the connect fn (auth failure), or
/// cancellation.
#[cfg(test)]
#[derive(Debug)]
#[allow(dead_code)] // variant payloads inspected via Debug only
pub(crate) enum ReconnectOutcome<T> {
	/// Connect callback returned `Ok(T)` — the caller now owns the
	/// connected handle.
	Connected(T),
	/// Connect callback returned `Err` and `should_bail(err) == true`
	/// — e.g. an auth failure that must not be retried. The original
	/// error is returned so the caller can log it.
	Bailed(anyhow::Error),
	/// `cancel.cancelled()` fired while waiting for the next attempt.
	Cancelled,
}

/// Run the connect / backoff / cancel loop against an injected
/// connect callable and clock, producing a [`ReconnectOutcome`].
///
/// Keeps the pure reconnect-timing logic testable independently of
/// the camera's full [`CameraHandle`] machinery (wake lock, preview
/// state, MQTT publish). The backoff sleep delegates to
/// [`ReconnectBackoff::sleep_with_cancel`] — the same path
/// `CameraHandle::run` uses — so the
/// "sleep-then-retry-with-cancel" contract is genuinely shared.
#[cfg(test)]
pub(crate) async fn drive_reconnect_with_backoff<T, F, Fut>(
	mut backoff: ReconnectBackoff,
	cancel: CancellationToken,
	mut connect: F,
	should_bail: impl Fn(&anyhow::Error) -> bool,
) -> ReconnectOutcome<T>
where
	F: FnMut() -> Fut,
	Fut: std::future::Future<Output = anyhow::Result<T>>,
{
	loop {
		if cancel.is_cancelled() {
			return ReconnectOutcome::Cancelled;
		}
		let attempt = tokio::select! {
			_ = cancel.cancelled() => return ReconnectOutcome::Cancelled,
			r = connect() => r,
		};
		match attempt {
			Ok(value) => return ReconnectOutcome::Connected(value),
			Err(e) => {
				if should_bail(&e) {
					return ReconnectOutcome::Bailed(e);
				}
				if !backoff.sleep_with_cancel(&cancel).await {
					return ReconnectOutcome::Cancelled;
				}
			}
		}
	}
}

#[cfg(test)]
impl CameraHandle {
	/// Test helper: insert a pre-built [`StreamSource`] into the per-camera
	/// registry without going through the connect-and-spawn path. Used by
	/// the prune-grace unit tests.
	pub(crate) fn insert_stream_source_for_test(
		&self,
		kind: RtspStreamKind,
		source: Arc<StreamSource>,
	) {
		self.stream_sources.write_recover().insert(kind, source);
	}

	/// Test helper: does the per-camera registry currently hold a source
	/// for `kind`?
	pub(crate) fn has_stream_source_for_test(&self, kind: RtspStreamKind) -> bool {
		self.stream_sources.read_recover().contains_key(&kind)
	}

	/// Test helper: subscribe to a registered source's broadcast channel
	/// (bumping `receiver_count` by one) without pulling in the full RTSP
	/// provider subscription machinery.
	pub(crate) fn subscribe_stream_for_test(
		&self,
		kind: RtspStreamKind,
	) -> tokio::sync::broadcast::Receiver<bairelay_rtsp::provider::Frame> {
		let guard = self.stream_sources.read_recover();
		guard
			.get(&kind)
			.expect("stream source present for test")
			.subscribe_for_test()
	}

	/// Test helper: install a `CameraDriver` (typically a
	/// `FakeCamera`) + flip state to `Connected` so
	/// `mqtt_dispatch::dispatch_control` finds a driver via
	/// `bc_camera()` and proceeds past the disconnected short-circuit.
	/// Bypasses `try_connect`; the concrete `bc_camera_concrete` slot
	/// stays `None` because fakes are trait-only.
	#[allow(dead_code)] // used by upcoming mqtt_dispatch tests in this phase
	pub(crate) fn set_driver_for_test(&self, driver: Arc<dyn CameraDriver>) {
		*self.bc_camera.write_recover() = Some(driver);
		self.set_state(CameraState::Connected);
	}

	/// Test-only entry into the post-connect session lifecycle.
	/// Runs [`Self::run_connected_session`] with `concrete = None`,
	/// skipping the stream-source spawn / logout paths that require a
	/// real [`BcCamera`]. Use together with a scripted `FakeCamera` to
	/// exercise the capability probe + session-tasks + keepalive +
	/// teardown code without a live TCP socket.
	pub(crate) async fn run_connected_session_for_test(
		self: &Arc<Self>,
		driver: Arc<dyn CameraDriver>,
	) {
		self.run_connected_session(driver, None).await
	}
}

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

	#[test]
	fn new_camera_handle_has_audio_presence_unknown() {
		use crate::audio_presence::AudioPresence;
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let config = minimal_camera_config("cam");
		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(config, cancel, None);
		let presence = *handle.audio_presence().read().expect("presence lock");
		assert_eq!(presence, AudioPresence::Unknown);
	}

	#[test]
	fn capabilities_defaults_to_none() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let config = minimal_camera_config("cam");
		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(config, cancel, None);
		assert!(handle.capabilities().is_none());
	}

	#[test]
	fn capabilities_cached_after_set() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let config = minimal_camera_config("cam");
		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(config, cancel, None);
		handle.set_capabilities_for_test(CameraCapabilities { has_ptz: true });
		let caps = handle.capabilities().expect("caps populated");
		assert!(caps.has_ptz);
	}

	#[test]
	fn camera_handle_initial_preview_state_is_sleeping() {
		use crate::config::test_helpers::minimal_camera_config;
		use crate::preview_state::PreviewState;
		use tokio_util::sync::CancellationToken;

		let config = minimal_camera_config("cam");
		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(config, cancel, None);
		let rx = handle.preview_state_rx();
		assert_eq!(*rx.borrow(), PreviewState::Sleeping);
	}

	/// Observes the `Sleeping → Connecting` transition driven by the
	/// connect loop. We spawn `run()` against an unroutable address so
	/// the loop enters its connect attempt (flipping preview state to
	/// `Connecting`) but never actually establishes a session. The test
	/// cancels as soon as the transition is observed.
	#[tokio::test]
	async fn preview_state_transitions_to_connecting_during_connect() {
		use crate::config::test_helpers::minimal_camera_config;
		use crate::preview_state::PreviewState;
		use tokio_util::sync::CancellationToken;

		// Unroutable-but-parseable target: loopback port 1 refuses
		// connections quickly on every supported platform, and the
		// `Local` discovery method short-circuits to a direct connect
		// when an IP is supplied.
		let mut config = minimal_camera_config("cam");
		config.address = Some("127.0.0.1:1".to_string());
		// Disable idle_disconnect so the loop does NOT park in
		// `Sleeping` waiting for a wake lock — we want it to march
		// straight into the connect attempt.
		config.idle_disconnect = false;

		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(config, cancel.clone(), None));
		let mut rx = handle.preview_state_rx();
		assert_eq!(*rx.borrow_and_update(), PreviewState::Sleeping);

		let run_handle = {
			let h = Arc::clone(&handle);
			tokio::spawn(async move { h.run().await })
		};

		let saw_connecting = tokio::time::timeout(Duration::from_secs(2), async {
			loop {
				if *rx.borrow_and_update() == PreviewState::Connecting {
					return true;
				}
				if rx.changed().await.is_err() {
					return false;
				}
			}
		})
		.await
		.unwrap_or(false);

		// Tear down before asserting so a failing assert doesn't leak
		// the background task.
		cancel.cancel();
		let _ = tokio::time::timeout(Duration::from_secs(5), run_handle).await;

		assert!(
			saw_connecting,
			"preview state never transitioned to Connecting"
		);
	}

	// ── Preview-state aggregator ────────────────────

	#[test]
	fn aggregate_preview_state_live_downgrades_on_bridging() {
		assert_eq!(
			aggregate_preview_state(PreviewState::Live, true, None),
			PreviewState::Connecting
		);
		assert_eq!(
			aggregate_preview_state(PreviewState::Live, false, None),
			PreviewState::Live
		);
	}

	#[test]
	fn aggregate_preview_state_sleeping_never_changes() {
		assert_eq!(
			aggregate_preview_state(PreviewState::Sleeping, true, None),
			PreviewState::Sleeping
		);
		assert_eq!(
			aggregate_preview_state(PreviewState::Sleeping, false, None),
			PreviewState::Sleeping
		);
		assert_eq!(
			aggregate_preview_state(PreviewState::Sleeping, true, Some(PreviewState::Connecting)),
			PreviewState::Sleeping
		);
	}

	#[test]
	fn aggregate_upgrades_from_own_downgrade_when_bridging_clears() {
		assert_eq!(
			aggregate_preview_state(
				PreviewState::Connecting,
				false,
				Some(PreviewState::Connecting)
			),
			PreviewState::Live,
		);
	}

	#[test]
	fn aggregate_stays_connecting_on_connect_loop_owned() {
		assert_eq!(
			aggregate_preview_state(PreviewState::Connecting, false, None),
			PreviewState::Connecting,
		);
		assert_eq!(
			aggregate_preview_state(PreviewState::Connecting, true, None),
			PreviewState::Connecting,
		);
	}

	#[test]
	fn aggregate_stays_connecting_when_bridging_still_active() {
		assert_eq!(
			aggregate_preview_state(
				PreviewState::Connecting,
				true,
				Some(PreviewState::Connecting)
			),
			PreviewState::Connecting,
		);
	}

	// ── Post-keepalive PreviewState selector ──────────────────────────

	#[test]
	fn post_keepalive_sleeping_only_when_idle_disconnect_and_wake_lock_idle() {
		// True parked case: idle_disconnect configured + no wake lock held.
		assert_eq!(
			post_keepalive_preview_state(true, true),
			PreviewState::Sleeping,
		);
	}

	#[test]
	fn post_keepalive_connecting_when_wake_lock_held() {
		// Reconnect-in-progress: somebody still wants the camera awake.
		assert_eq!(
			post_keepalive_preview_state(true, false),
			PreviewState::Connecting,
		);
	}

	#[test]
	fn post_keepalive_connecting_when_idle_disconnect_disabled() {
		// Always-on mode: camera never parks, so a keepalive-loop return
		// always implies a reconnect cycle, never a sleep.
		assert_eq!(
			post_keepalive_preview_state(false, true),
			PreviewState::Connecting,
		);
		assert_eq!(
			post_keepalive_preview_state(false, false),
			PreviewState::Connecting,
		);
	}

	// ── Keepalive tick classification & counter ─────────────────────
	//
	// Stage 6 Task 33 coverage. Drives `classify_keepalive_tick` +
	// `advance_keepalive_counter` with scripted inputs so we don't need
	// a live `get_linktype()` round-trip to pin the decision table.

	#[derive(Debug)]
	#[allow(dead_code)] // Field shown in Debug output via the derive.
	struct FakeErr(&'static str);

	#[test]
	fn classify_ok_reply_is_ok() {
		let raw: std::result::Result<
			std::result::Result<(), FakeErr>,
			tokio::time::error::Elapsed,
		> = Ok(Ok(()));
		assert_eq!(classify_keepalive_tick(raw), KeepaliveTickOutcome::Ok);
	}

	// The "UnintelligibleReply ↦ Ok" mapping moved to
	// `CameraDriver::keepalive_probe` and is unit-tested in
	// `crates/core/src/bc_protocol/link.rs`.

	#[test]
	fn classify_other_err_is_failed() {
		let raw: std::result::Result<
			std::result::Result<(), FakeErr>,
			tokio::time::error::Elapsed,
		> = Ok(Err(FakeErr("ConnectionReset")));
		assert_eq!(classify_keepalive_tick(raw), KeepaliveTickOutcome::Failed);
	}

	#[test]
	fn classify_timeout_is_failed() {
		// Construct an `Elapsed` by racing a 0 ms timeout against a
		// never-completing future — only way to get hold of the private
		// `Elapsed` variant in stable tokio. `current_thread` flavour:
		// this test races one timeout to completion and exits, so a
		// worker pool would be wasted overhead.
		let raw: std::result::Result<std::result::Result<(), FakeErr>, _> =
			tokio::runtime::Builder::new_current_thread()
				.enable_all()
				.build()
				.unwrap()
				.block_on(async {
					tokio::time::timeout(
						Duration::from_millis(0),
						futures::future::pending::<std::result::Result<(), FakeErr>>(),
					)
					.await
				});
		assert_eq!(classify_keepalive_tick(raw), KeepaliveTickOutcome::Failed);
	}

	#[test]
	fn advance_ok_resets_counter() {
		assert_eq!(
			advance_keepalive_counter(4, KeepaliveTickOutcome::Ok, 5),
			(0, false),
		);
	}

	#[test]
	fn advance_failed_increments_and_does_not_break_below_max() {
		assert_eq!(
			advance_keepalive_counter(3, KeepaliveTickOutcome::Failed, 5),
			(4, false),
		);
	}

	#[test]
	fn advance_failed_breaks_at_max() {
		// Hitting MAX_FAILURES exactly must trigger the break flag —
		// this is the "5 misses and we're out" edge.
		assert_eq!(
			advance_keepalive_counter(4, KeepaliveTickOutcome::Failed, 5),
			(5, true),
		);
	}

	#[test]
	fn advance_saturates_on_overflow() {
		// Paranoia: if we somehow get past the break (caller ignored
		// the flag) the counter must not wrap around to 0 and re-enable
		// the "healthy" branch.
		assert_eq!(
			advance_keepalive_counter(u32::MAX, KeepaliveTickOutcome::Failed, 5),
			(u32::MAX, true),
		);
	}

	// ── End-to-end cadence with the paused virtual clock ────────────
	//
	// Drives the raw `interval` + `classify` + `advance` wiring via the
	// same helpers the real `keepalive_loop` uses. We don't invoke
	// `keepalive_loop` itself because it wants a real `BcCamera`; the
	// contract under test is "one probe every KEEPALIVE_INTERVAL, and
	// MAX_FAILURES consecutive failures terminate the loop."

	#[tokio::test(flavor = "current_thread", start_paused = true)]
	async fn keepalive_cadence_drives_one_probe_per_interval() {
		let mut interval = tokio::time::interval(CameraHandle::KEEPALIVE_INTERVAL);
		// First tick fires immediately under tokio's default behaviour.
		let t0 = tokio::time::Instant::now();
		interval.tick().await;
		let t1 = tokio::time::Instant::now();
		// Second tick should require advancing the virtual clock by the
		// configured interval.
		tokio::time::advance(CameraHandle::KEEPALIVE_INTERVAL).await;
		interval.tick().await;
		let t2 = tokio::time::Instant::now();
		assert_eq!(t1.duration_since(t0), Duration::ZERO);
		assert_eq!(t2.duration_since(t1), CameraHandle::KEEPALIVE_INTERVAL);
	}

	#[tokio::test(flavor = "current_thread", start_paused = true)]
	async fn keepalive_counter_breaks_after_max_consecutive_failures() {
		// Scripted sequence of all-failed ticks; counter must break on
		// exactly the MAX_FAILURES-th failure, no sooner, no later.
		let mut failures: u32 = 0;
		let mut broke_on: Option<u32> = None;
		for i in 1..=(CameraHandle::KEEPALIVE_MAX_FAILURES + 2) {
			let (next, should_break) = advance_keepalive_counter(
				failures,
				KeepaliveTickOutcome::Failed,
				CameraHandle::KEEPALIVE_MAX_FAILURES,
			);
			failures = next;
			if should_break {
				broke_on = Some(i);
				break;
			}
		}
		assert_eq!(broke_on, Some(CameraHandle::KEEPALIVE_MAX_FAILURES));
	}

	#[tokio::test(flavor = "current_thread", start_paused = true)]
	async fn keepalive_counter_resets_after_single_ok_tick() {
		// Four failures then one success must zero the counter — the
		// "intermittent blip" recovery path.
		let mut failures: u32 = 0;
		for _ in 0..4 {
			let (next, should_break) = advance_keepalive_counter(
				failures,
				KeepaliveTickOutcome::Failed,
				CameraHandle::KEEPALIVE_MAX_FAILURES,
			);
			assert!(!should_break);
			failures = next;
		}
		assert_eq!(failures, 4);
		let (next, should_break) = advance_keepalive_counter(
			failures,
			KeepaliveTickOutcome::Ok,
			CameraHandle::KEEPALIVE_MAX_FAILURES,
		);
		assert_eq!(next, 0);
		assert!(!should_break);
	}

	// ── is_login_failure (typed match + Debug-substring fallback) ───

	/// Pin the production contract: a real `bairelay_neolink_core::Error::AuthFailed`
	/// (the variant `try_connect()` actually returns) must drive the
	/// permanent-bail. Pre-fix, this would have only worked accidentally
	/// via the Debug-substring fallback; with typed downcast it's
	/// guaranteed by the `From` impl in anyhow.
	#[test]
	fn is_login_failure_recognises_typed_auth_failed() {
		let e: anyhow::Error = bairelay_neolink_core::Error::AuthFailed.into();
		assert!(is_login_failure(&e));
	}

	#[test]
	fn is_login_failure_recognises_typed_camera_login_fail() {
		let e: anyhow::Error = bairelay_neolink_core::Error::CameraLoginFail.into();
		assert!(is_login_failure(&e));
	}

	/// `try_connect()` may surface auth failures wrapped in `.context(..)`
	/// from a future call-site; the chain walk must catch them.
	#[test]
	fn is_login_failure_recognises_typed_auth_failed_through_context() {
		use anyhow::Context;
		let e: anyhow::Result<()> = Err(bairelay_neolink_core::Error::AuthFailed.into());
		let wrapped = e.context("connect_with_retry").unwrap_err();
		assert!(is_login_failure(&wrapped));
	}

	// Belt-and-braces: the Debug-substring fallback still fires for
	// synthesised `anyhow::anyhow!(...)` errors (used by some test
	// paths and any future intermediate wrapper that doesn't preserve
	// a downcastable source).

	#[test]
	fn is_login_failure_recognises_auth_fail() {
		let e: anyhow::Error = anyhow::anyhow!("remote reported AuthFailed for cam-alpha");
		assert!(is_login_failure(&e));
	}

	#[test]
	fn is_login_failure_recognises_camera_login_fail() {
		let e: anyhow::Error = anyhow::anyhow!("CameraLoginFail on port 9000");
		assert!(is_login_failure(&e));
	}

	#[test]
	fn is_login_failure_recognises_credential_error() {
		let e: anyhow::Error = anyhow::anyhow!("Credential error: invalid password hash");
		assert!(is_login_failure(&e));
	}

	#[test]
	fn is_login_failure_rejects_generic_io() {
		let e: anyhow::Error = anyhow::anyhow!("connection reset by peer during initial handshake");
		assert!(!is_login_failure(&e));
	}

	// ── ReconnectBackoff mechanics ──────────────────────────────────

	#[test]
	fn reconnect_backoff_starts_at_initial_and_doubles() {
		let mut b = ReconnectBackoff::new(Duration::from_millis(100), Duration::from_millis(400));
		assert_eq!(b.next_delay(), Duration::from_millis(100));
		assert_eq!(b.next_delay(), Duration::from_millis(200));
		assert_eq!(b.next_delay(), Duration::from_millis(400));
		// Clamps at max on subsequent calls.
		assert_eq!(b.next_delay(), Duration::from_millis(400));
	}

	// ── drive_reconnect_with_backoff ────────────────────────────────

	/// After N failed attempts, the next success returns
	/// `Connected(value)` and the virtual clock advanced by exactly
	/// the expected sum of backoff delays. Uses tokio's paused-time
	/// clock so the test is deterministic regardless of real time.
	#[tokio::test(start_paused = true)]
	async fn drive_reconnect_retries_until_success_with_doubling_backoff() {
		use std::sync::atomic::{AtomicU32, Ordering};

		let calls = Arc::new(AtomicU32::new(0));
		let calls_c = Arc::clone(&calls);
		let backoff = ReconnectBackoff::new(Duration::from_secs(2), Duration::from_secs(60));
		let cancel = CancellationToken::new();

		let start = tokio::time::Instant::now();
		let outcome = drive_reconnect_with_backoff::<_, _, _>(
			backoff,
			cancel,
			move || {
				let calls_c = Arc::clone(&calls_c);
				async move {
					let n = calls_c.fetch_add(1, Ordering::AcqRel);
					if n < 3 {
						Err(anyhow::anyhow!("transient failure #{n}"))
					} else {
						Ok::<u32, anyhow::Error>(n)
					}
				}
			},
			|_| false,
		)
		.await;

		match outcome {
			ReconnectOutcome::Connected(n) => assert_eq!(n, 3),
			other => panic!("expected Connected(3), got {:?}", other),
		}
		assert_eq!(
			calls.load(Ordering::Acquire),
			4,
			"connect should be called four times (three Err, one Ok)"
		);

		// Three failures → three sleeps: 2 + 4 + 8 = 14 s.
		let elapsed = start.elapsed();
		assert!(
			elapsed >= Duration::from_secs(14) && elapsed < Duration::from_secs(15),
			"elapsed {:?} should be ~14s (2+4+8 backoff)",
			elapsed
		);
	}

	/// `should_bail` returns true on the first error → the loop exits
	/// with `Bailed(err)` without retrying. Pins the "auth failure
	/// short-circuit" contract used by `is_login_failure` in the real
	/// loop.
	#[tokio::test(start_paused = true)]
	async fn drive_reconnect_bails_on_should_bail() {
		use std::sync::atomic::{AtomicU32, Ordering};

		let calls = Arc::new(AtomicU32::new(0));
		let calls_c = Arc::clone(&calls);
		let backoff = ReconnectBackoff::new(Duration::from_secs(1), Duration::from_secs(60));
		let cancel = CancellationToken::new();

		let outcome = drive_reconnect_with_backoff::<(), _, _>(
			backoff,
			cancel,
			move || {
				calls_c.fetch_add(1, Ordering::AcqRel);
				async move { Err(anyhow::anyhow!("AuthFailed")) }
			},
			|e| format!("{e:?}").contains("AuthFailed"),
		)
		.await;

		assert!(
			matches!(outcome, ReconnectOutcome::Bailed(_)),
			"expected Bailed, got {:?}",
			outcome
		);
		assert_eq!(
			calls.load(Ordering::Acquire),
			1,
			"bail-on-auth must not retry"
		);
	}

	/// Cancellation mid-backoff short-circuits the sleep and returns
	/// `Cancelled` without waiting the full delay. Uses paused time so
	/// the test exits instantly on the cancel signal.
	#[tokio::test(start_paused = true)]
	async fn drive_reconnect_cancels_during_backoff_sleep() {
		let backoff = ReconnectBackoff::new(Duration::from_secs(60), Duration::from_secs(60));
		let cancel = CancellationToken::new();
		let cancel_c = cancel.clone();

		// Trip cancel shortly after the first Err lands. Paused time
		// means the 60 s sleep would otherwise never resolve.
		tokio::spawn(async move {
			tokio::time::sleep(Duration::from_millis(100)).await;
			cancel_c.cancel();
		});

		let outcome = drive_reconnect_with_backoff::<(), _, _>(
			backoff,
			cancel,
			|| async { Err(anyhow::anyhow!("transient")) },
			|_| false,
		)
		.await;

		assert!(
			matches!(outcome, ReconnectOutcome::Cancelled),
			"expected Cancelled, got {:?}",
			outcome
		);
	}

	#[test]
	fn reconnect_backoff_resets_to_initial() {
		let mut b = ReconnectBackoff::new(Duration::from_millis(50), Duration::from_secs(1));
		let _ = b.next_delay();
		let _ = b.next_delay();
		b.reset();
		assert_eq!(b.next_delay(), Duration::from_millis(50));
	}

	#[tokio::test]
	async fn aggregator_upgrades_back_to_live_when_gap_clears() {
		use crate::config::test_helpers::minimal_camera_config;
		use crate::preview_state::PreviewState;
		use crate::stream_source::{GapState, StreamSource};

		let config = minimal_camera_config("cam");
		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(config, cancel, None));

		// Simulate a successful connect by flipping preview state to Live.
		handle.set_preview_state(PreviewState::Live);
		let mut rx = handle.preview_state_rx();
		assert_eq!(*rx.borrow_and_update(), PreviewState::Live);

		// Register an inert source and flip it to `Bridging`.
		let source = StreamSource::start_inert_for_test();
		handle.insert_stream_source_for_test(RtspStreamKind::Main, Arc::clone(&source));
		source.set_gap_state_for_test(GapState::Bridging);

		// Spawn the aggregator loop.
		let session_cancel = CancellationToken::new();
		let loop_handle = {
			let h = Arc::clone(&handle);
			let c = session_cancel.clone();
			tokio::spawn(async move { aggregate_preview_state_loop(h, c).await })
		};

		// Wait for the downgrade to Connecting.
		let saw_downgrade = tokio::time::timeout(Duration::from_secs(2), async {
			loop {
				if *rx.borrow_and_update() == PreviewState::Connecting {
					return true;
				}
				if rx.changed().await.is_err() {
					return false;
				}
			}
		})
		.await
		.unwrap_or(false);
		assert!(
			saw_downgrade,
			"aggregator never downgraded Live → Connecting while Bridging"
		);

		// Gap clears → aggregator should upgrade back to Live.
		source.set_gap_state_for_test(GapState::Live);

		let saw_upgrade = tokio::time::timeout(Duration::from_secs(2), async {
			loop {
				if *rx.borrow_and_update() == PreviewState::Live {
					return true;
				}
				if rx.changed().await.is_err() {
					return false;
				}
			}
		})
		.await
		.unwrap_or(false);

		// Tear down before asserting so a failing assert doesn't leak
		// the background task.
		session_cancel.cancel();
		let _ = tokio::time::timeout(Duration::from_secs(5), loop_handle).await;

		assert!(
			saw_upgrade,
			"aggregator never upgraded Connecting → Live after gap cleared"
		);
	}

	/// Drive `run()` against a parked camera with `idle_disconnect = true`
	/// and no wake lock held. The loop must flip `PreviewState` to
	/// `Sleeping` and await wake-lock acquisition before attempting any
	/// connect. Cancellation lifts the loop out cleanly.
	#[tokio::test]
	async fn run_parks_in_sleeping_when_idle_disconnect_and_no_wake_lock() {
		use crate::config::test_helpers::minimal_camera_config;
		use crate::preview_state::PreviewState;
		use tokio_util::sync::CancellationToken;

		let mut config = minimal_camera_config("cam-park");
		config.idle_disconnect = true;
		// Unroutable address: if the park arm races our assertion,
		// the test still won't reach the live network.
		config.address = Some("127.0.0.1:1".to_string());

		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(config, cancel.clone(), None));
		let mut rx = handle.preview_state_rx();

		let run_handle = {
			let h = Arc::clone(&handle);
			tokio::spawn(async move { h.run().await })
		};

		let saw_sleeping = tokio::time::timeout(Duration::from_secs(2), async {
			loop {
				if *rx.borrow_and_update() == PreviewState::Sleeping {
					return true;
				}
				if rx.changed().await.is_err() {
					return false;
				}
			}
		})
		.await
		.unwrap_or(false);

		cancel.cancel();
		let _ = tokio::time::timeout(Duration::from_secs(5), run_handle).await;

		assert!(saw_sleeping, "idle_disconnect loop must flip to Sleeping");
	}

	/// `run()` with an MQTT client publishes `status/connection = off`
	/// and a motion-unknown payload before entering the connect loop
	/// (lines 736-741). Parks via `idle_disconnect` so the network
	/// attempt never fires.
	#[tokio::test]
	async fn run_publishes_initial_disconnected_status_when_mqtt_present() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let (mqtt, mock) = bairelay_mqtt::test_support::mock_client();
		let mut config = minimal_camera_config("cam-init");
		config.idle_disconnect = true;
		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(config, cancel.clone(), Some(mqtt)));

		let run_handle = {
			let h = Arc::clone(&handle);
			tokio::spawn(async move { h.run().await })
		};

		let saw_init = tokio::time::timeout(Duration::from_secs(2), async {
			loop {
				let rows = mock.published();
				let has_conn = rows
					.iter()
					.any(|(t, p, _)| t == "bairelay/cam-init/status" && p == b"disconnected");
				if has_conn {
					return true;
				}
				tokio::time::sleep(Duration::from_millis(10)).await;
			}
		})
		.await
		.unwrap_or(false);

		cancel.cancel();
		let _ = tokio::time::timeout(Duration::from_secs(5), run_handle).await;
		assert!(
			saw_init,
			"initial disconnected publish must land on connection topic; observed: {:?}",
			mock.published_topics()
		);
	}

	/// `run()` against an unroutable address hits the
	/// `connect_result = Err` → `is_login_failure` branch (returns
	/// false on a connection reset/timeout), logs the connect failure,
	/// and enters the backoff sleep. We cancel before the 2s sleep
	/// finishes so the task exits via the `sleep_with_cancel → false`
	/// branch (line 1046 break).
	#[tokio::test]
	async fn run_backoffs_after_connect_failure_then_exits_on_cancel() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let mut config = minimal_camera_config("cam-fail");
		config.idle_disconnect = false;
		config.address = Some("127.0.0.1:1".to_string());

		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(config, cancel.clone(), None));

		let run_handle = {
			let h = Arc::clone(&handle);
			tokio::spawn(async move { h.run().await })
		};

		// Wait for the connect attempt to start (state flips to
		// Connecting) + fail (flips back to Disconnected) — ~1-5s on
		// a refused-connection socket. Then cancel to break the
		// backoff sleep.
		tokio::time::sleep(Duration::from_millis(500)).await;
		cancel.cancel();
		let join_outcome = tokio::time::timeout(Duration::from_secs(10), run_handle).await;
		// Task must actually have completed (not been canceled by the
		// timeout). A regression that breaks cancel-during-backoff
		// would leave the loop spinning past the 10 s timeout, and the
		// pre-fix `let _ = ...` swallowed that case silently.
		let join_result = join_outcome.expect("run() did not exit within 10 s after cancel");
		join_result.expect("run() task panicked");
		// Final state must be Disconnected — pinned so a future change
		// to leave state as Connecting on cancel-during-backoff would
		// surface here.
		assert_eq!(handle.state(), CameraState::Disconnected);
	}

	// ── Accessor / miscellaneous surface coverage ───────────────────

	#[test]
	fn simple_accessors_reflect_initial_state() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let config = minimal_camera_config("cam-acc");
		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(config, cancel, None);
		assert_eq!(handle.name(), "cam-acc");
		assert_eq!(handle.topic_prefix(), "bairelay");
		assert_eq!(handle.state(), CameraState::Disconnected);
		assert!(!handle.is_cancelled());
		assert!(handle.bc_camera().is_none());
		assert_eq!(handle.config().name, "cam-acc");
		// Touch the cancel_token accessor — it must match the one the
		// `new()` ctor stored internally (child of the parent token).
		assert!(!handle.cancel_token().is_cancelled());
	}

	#[test]
	fn request_disconnect_fires_notify_without_panicking() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(minimal_camera_config("cam-rd"), cancel, None);
		// Safe to call with no waiter — notify_one is edge-triggered.
		handle.request_disconnect();
	}

	#[test]
	fn with_bcmedia_dump_and_prefix_preserves_prefix() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let cancel = CancellationToken::new();
		let handle = CameraHandle::with_bcmedia_dump_and_prefix(
			minimal_camera_config("cam-x"),
			cancel,
			None,
			"neolink".to_string(),
			None,
		);
		assert_eq!(handle.topic_prefix(), "neolink");
	}

	#[test]
	fn last_frame_main_returns_clonable_arc() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(minimal_camera_config("cam-lfm"), cancel, None);
		let a = handle.last_frame_main();
		let b = handle.last_frame_main();
		assert!(Arc::ptr_eq(&a, &b));
	}

	#[test]
	fn camera_state_variant_predicates_are_exclusive() {
		assert!(CameraState::Disconnected.is_disconnected());
		assert!(!CameraState::Disconnected.is_connecting());
		assert!(!CameraState::Disconnected.is_connected());
		assert!(CameraState::Connecting.is_connecting());
		assert!(!CameraState::Connecting.is_disconnected());
		assert!(CameraState::Connected.is_connected());
		assert!(!CameraState::Connected.is_connecting());
	}

	/// `publish_discovery` and `unpublish_discovery` early-return Ok
	/// when no `DiscoveryPublisher` is attached — the common case for
	/// a config without `[mqtt.discovery]`.
	#[tokio::test]
	async fn discovery_no_publisher_noop_ok() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(minimal_camera_config("cam-np"), cancel, None);
		handle.publish_discovery().await.expect("ok noop");
		handle.unpublish_discovery().await.expect("ok noop");
	}

	/// `publish_discovery` / `unpublish_discovery` are also no-ops when
	/// a publisher is present but the capabilities cache is still None
	/// (first connect hasn't finished). Exercises the second early
	/// return branch in both methods.
	#[tokio::test]
	async fn discovery_caps_none_noop_ok() {
		use crate::config::test_helpers::minimal_camera_config;
		use std::collections::HashSet;
		use tokio_util::sync::CancellationToken;

		let (mqtt, _mock) = bairelay_mqtt::test_support::mock_client();
		let publisher = bairelay_mqtt::DiscoveryPublisher::new(
			mqtt,
			"bairelay".to_string(),
			"homeassistant".to_string(),
			HashSet::new(),
			"test".to_string(),
		);
		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(minimal_camera_config("cam-nc"), cancel, None)
			.with_discovery_publisher(publisher);
		// Caps still None → both paths early-return.
		handle
			.publish_discovery()
			.await
			.expect("ok noop on None caps");
		handle
			.unpublish_discovery()
			.await
			.expect("ok noop on None caps");
	}

	/// With caps populated and a publisher attached,
	/// `publish_discovery` emits MQTT traffic and `unpublish_discovery`
	/// emits retained-empty payloads on the same topics. We only
	/// assert the published count increases; the exact topic set is
	/// owned by the bairelay_mqtt crate and has its own tests.
	#[tokio::test]
	async fn discovery_publishes_and_unpublishes_with_caps() {
		use crate::config::test_helpers::minimal_camera_config;
		use std::collections::HashSet;
		use tokio_util::sync::CancellationToken;

		let (mqtt, mock) = bairelay_mqtt::test_support::mock_client();
		let mut features = HashSet::new();
		features.insert(bairelay_mqtt::discovery::Feature::Camera);
		features.insert(bairelay_mqtt::discovery::Feature::Motion);
		let publisher = bairelay_mqtt::DiscoveryPublisher::new(
			mqtt,
			"bairelay".to_string(),
			"homeassistant".to_string(),
			features,
			"test".to_string(),
		);
		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(minimal_camera_config("cam-dp"), cancel, None)
			.with_discovery_publisher(publisher);
		handle.set_capabilities_for_test(CameraCapabilities { has_ptz: true });

		let before = mock.published().len();
		handle.publish_discovery().await.expect("publish ok");
		let after_pub = mock.published().len();
		assert!(
			after_pub > before,
			"publish_discovery must emit retained payloads; before={before} after={after_pub}"
		);

		handle.unpublish_discovery().await.expect("unpublish ok");
		let after_unpub = mock.published().len();
		assert!(
			after_unpub > after_pub,
			"unpublish_discovery must emit retained-empty payloads; after_pub={after_pub} after_unpub={after_unpub}"
		);
	}

	/// `stream_source()` returns `Unavailable` when the parent cancel
	/// token fires before the camera reaches `Connected`.
	#[tokio::test]
	async fn stream_source_returns_unavailable_when_cancelled() {
		use crate::config::test_helpers::minimal_camera_config;
		use tokio_util::sync::CancellationToken;

		let parent = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(
			minimal_camera_config("cam-sc"),
			parent.clone(),
			None,
		));
		parent.cancel();

		let err = handle
			.stream_source(RtspStreamKind::Main)
			.await
			.err()
			.expect("cancelled → Unavailable");
		assert!(matches!(err, StreamError::Unavailable(_)));
	}

	/// With state already `Connected` (via `set_driver_for_test`)
	/// but no concrete `BcCamera` available, `stream_source()`
	/// returns `Unavailable` on the "bc_camera_concrete is None"
	/// branch — because `FakeCamera` only installs the trait object.
	#[tokio::test]
	async fn stream_source_returns_unavailable_without_concrete_bc() {
		use crate::config::test_helpers::minimal_camera_config;
		use bairelay_neolink_core::bc_protocol::FakeCameraBuilder;
		use tokio_util::sync::CancellationToken;

		let fake = FakeCameraBuilder::new().build();
		let driver: Arc<dyn CameraDriver> = fake;
		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(
			minimal_camera_config("cam-nc2"),
			cancel,
			None,
		));
		handle.set_driver_for_test(driver);

		let err = handle
			.stream_source(RtspStreamKind::Main)
			.await
			.err()
			.expect("no concrete → Unavailable");
		assert!(
			matches!(err, StreamError::Unavailable(ref msg) if msg.contains("not currently connected")),
			"got {err:?}"
		);
	}

	/// `stop_all_stream_sources` drops every registered source even
	/// when the caller hasn't explicitly unsubscribed. The internal
	/// helper is `fn`, not `pub`, but it's exercised indirectly via
	/// the `run()` loop's teardown; here we poke it through a
	/// test-only entry point by inserting two inert sources and
	/// calling it through a dedicated `#[cfg(test)]` method.
	#[tokio::test]
	async fn stop_all_stream_sources_clears_registry() {
		use crate::config::test_helpers::minimal_camera_config;
		use crate::stream_source::StreamSource;
		use bairelay_rtsp::url::StreamKind;
		use tokio_util::sync::CancellationToken;

		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(minimal_camera_config("cam-stop"), cancel, None);
		handle
			.insert_stream_source_for_test(StreamKind::Main, StreamSource::start_inert_for_test());
		handle.insert_stream_source_for_test(StreamKind::Sub, StreamSource::start_inert_for_test());
		assert!(handle.has_stream_source_for_test(StreamKind::Main));
		assert!(handle.has_stream_source_for_test(StreamKind::Sub));

		handle.stop_all_stream_sources().await;

		assert!(!handle.has_stream_source_for_test(StreamKind::Main));
		assert!(!handle.has_stream_source_for_test(StreamKind::Sub));
	}

	/// `stream_source()` fast-path: an already-registered source is
	/// returned directly without going through the concrete-BcCamera
	/// branch. Pins the "shared source for two subscribers" contract.
	#[tokio::test]
	async fn stream_source_fast_path_returns_preregistered_source() {
		use crate::config::test_helpers::minimal_camera_config;
		use crate::stream_source::StreamSource;
		use bairelay_neolink_core::bc_protocol::FakeCameraBuilder;
		use tokio_util::sync::CancellationToken;

		let fake = FakeCameraBuilder::new().build();
		let driver: Arc<dyn CameraDriver> = fake;
		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(
			minimal_camera_config("cam-fast"),
			cancel,
			None,
		));
		handle.set_driver_for_test(driver);

		let source = StreamSource::start_inert_for_test();
		handle.insert_stream_source_for_test(RtspStreamKind::Main, Arc::clone(&source));

		let got = handle
			.stream_source(RtspStreamKind::Main)
			.await
			.expect("preregistered source returned");
		// Same Arc → same pointer.
		assert!(Arc::ptr_eq(&got, &source));
	}

	/// `is_login_failure` rejects a plain `anyhow::Error` that doesn't
	/// match any of the three auth substrings. Guards against an
	/// over-eager match that would short-circuit retries on e.g. a
	/// TCP reset.
	#[test]
	fn is_login_failure_rejects_timeout_style_error() {
		let e: anyhow::Error =
			anyhow::anyhow!("deadline elapsed while waiting for handshake reply");
		assert!(!is_login_failure(&e));
	}

	// ── Aggregator fine-grained clearing ────────────────────────────

	#[test]
	fn aggregate_connecting_with_own_downgrade_but_still_bridging_holds() {
		// Own downgrade + still bridging ⇒ stay Connecting.
		assert_eq!(
			aggregate_preview_state(
				PreviewState::Connecting,
				true,
				Some(PreviewState::Connecting)
			),
			PreviewState::Connecting,
		);
	}

	#[test]
	fn aggregate_live_with_own_downgrade_bookkeeping_irrelevant() {
		// `last_own_write` only disambiguates Connecting; Live always
		// passes through based on the bridging flag.
		assert_eq!(
			aggregate_preview_state(PreviewState::Live, false, Some(PreviewState::Live)),
			PreviewState::Live,
		);
	}

	/// Drive `run_connected_session_for_test` against a `FakeCamera`
	/// whose keepalive always errors. Under `start_paused`, the
	/// keepalive loop fires MAX_FAILURES ticks and returns; teardown
	/// then runs synchronously. Asserts on:
	/// - state Connected → Disconnected transition
	/// - publish_connection(true) + publish_connection(false) both
	///   emitted on the MQTT mock
	/// - bc_camera cleared on exit
	/// - session-task span covered (motion, battery, floodlight,
	///   publish_pir_state) — configured via `enable_*` flags.
	#[tokio::test(flavor = "current_thread", start_paused = true)]
	async fn run_connected_session_covers_full_lifecycle() {
		use crate::config::test_helpers::minimal_camera_config;
		use bairelay_neolink_core::bc::xml::{FloodlightStatusList, RfAlarmCfg, Support};
		use bairelay_neolink_core::bc_protocol::FakeCameraBuilder;

		let mut config = minimal_camera_config("cam-sess");
		config.mqtt.enable_motion = true;
		config.mqtt.enable_battery = true;
		config.mqtt.enable_floodlight = true;
		config.mqtt.enable_pir = true;
		config.mqtt.battery_update = 60_000;
		config.mqtt.floodlight_update = 60_000;

		let (mqtt, mock) = bairelay_mqtt::test_support::mock_client();

		// Scripted streams: empty but accept traffic so the two
		// listen_on_* calls succeed cleanly. The floodlight channel
		// is closed so its listener exits via `None → break`.
		type MotionItem = std::result::Result<
			bairelay_neolink_core::bc_protocol::MotionStatus,
			bairelay_neolink_core::bc_protocol::Error,
		>;
		let (_motion_tx, motion_rx) = tokio::sync::mpsc::channel::<MotionItem>(1);
		let motion_data = bairelay_neolink_core::bc_protocol::MotionData::test_new(motion_rx);
		let (fl_tx, fl_rx) = tokio::sync::mpsc::channel::<FloodlightStatusList>(1);
		drop(fl_tx); // close → listener returns

		let fake = FakeCameraBuilder::new()
			.with_motion_stream(motion_data)
			.with_floodlight_stream(fl_rx)
			.with_battery_info(|| {
				Ok(bairelay_neolink_core::bc::xml::BatteryInfo {
					battery_percent: 42,
					..Default::default()
				})
			})
			.with_is_floodlight_tasks_enabled(|| Ok(false))
			.with_pirstate(|| {
				Ok(RfAlarmCfg {
					enable: 1,
					..Default::default()
				})
			})
			.with_support(|| {
				Ok(Support {
					ptz_mode: Some("pt".to_string()),
					..Default::default()
				})
			})
			.with_ptz_preset(|| Ok(bairelay_neolink_core::bc::xml::PtzPreset::default()))
			.with_linktype(|| {
				// Always error → keepalive_loop terminates after
				// MAX_FAILURES consecutive failures.
				Err(bairelay_neolink_core::bc_protocol::Error::Other(
					"scripted link fail",
				))
			})
			.build();
		let driver: Arc<dyn CameraDriver> = fake.clone();

		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::with_bcmedia_dump_and_prefix(
			config,
			cancel,
			Some(mqtt),
			"bairelay".to_string(),
			None,
		));

		// Run the session inline under a hard real-time timeout so
		// a regression (e.g. keepalive never exits) fails in
		// bounded wall time rather than hanging `cargo test`.
		// start_paused virtual time lets the 5-s keepalive cadence
		// + 2-s drain deadline fire without real delay.
		tokio::time::timeout(
			Duration::from_secs(30),
			handle.run_connected_session_for_test(driver),
		)
		.await
		.expect("session must exit after MAX_FAILURES consecutive keepalive errors");

		// Teardown visible: state is Disconnected, bc_camera cleared.
		assert_eq!(handle.state(), CameraState::Disconnected);
		assert!(handle.bc_camera().is_none());

		// Capability cache populated (get_support returned Ok).
		let caps = handle.capabilities().expect("caps populated from fake");
		assert!(caps.has_ptz);

		// Both initial publish_connection(true) and final
		// publish_connection(false) emitted on the broker.
		let pubs = mock.published();
		let conn_on = pubs
			.iter()
			.any(|(t, p, _)| t == "bairelay/cam-sess/status" && p == b"connected");
		let conn_off = pubs
			.iter()
			.any(|(t, p, _)| t == "bairelay/cam-sess/status" && p == b"disconnected");
		assert!(
			conn_on,
			"publish_connection(true) must land; topics: {:?}",
			mock.published_topics()
		);
		assert!(conn_off, "publish_connection(false) must land at teardown");

		// publish_pir_state ran (configured enable_pir = true).
		let pir_published = pubs
			.iter()
			.any(|(t, _, _)| t == "bairelay/cam-sess/status/pir");
		assert!(
			pir_published,
			"enable_pir = true must trigger a status/pir publish"
		);
	}

	/// `get_support` returning Err leaves `capabilities()` as None —
	/// the deliberate "retry on reconnect" path. The session still
	/// runs to completion; the get_support Err-arm is now covered.
	#[tokio::test(flavor = "current_thread", start_paused = true)]
	async fn run_connected_session_tolerates_get_support_error() {
		use crate::config::test_helpers::minimal_camera_config;
		use bairelay_neolink_core::bc_protocol::FakeCameraBuilder;

		let config = minimal_camera_config("cam-no-caps");
		// No MQTT so publish paths short-circuit.

		let fake = FakeCameraBuilder::new()
			.with_support(|| {
				Err(bairelay_neolink_core::bc_protocol::Error::Other(
					"support probe refused",
				))
			})
			.with_linktype(|| {
				Err(bairelay_neolink_core::bc_protocol::Error::Other(
					"link fail",
				))
			})
			.build();
		let driver: Arc<dyn CameraDriver> = fake;

		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(config, cancel, None));

		tokio::time::timeout(
			Duration::from_secs(30),
			handle.run_connected_session_for_test(driver),
		)
		.await
		.expect("session must exit");

		// Caps cache stays None → "retry on next reconnect" contract.
		assert!(
			handle.capabilities().is_none(),
			"get_support Err must leave caps cache empty",
		);
		assert_eq!(handle.state(), CameraState::Disconnected);
	}

	/// `idle_disconnect = true` spawns the grace-period task; with
	/// `wake_lock.is_idle()` also true, the grace period fires quickly
	/// under paused time and cancels the session via `sc.cancel()`
	/// before keepalive hits MAX_FAILURES.
	#[tokio::test(flavor = "current_thread", start_paused = true)]
	async fn run_connected_session_idle_disconnect_grace_path() {
		use crate::config::test_helpers::minimal_camera_config;
		use bairelay_neolink_core::bc::xml::Support;
		use bairelay_neolink_core::bc_protocol::FakeCameraBuilder;

		let mut config = minimal_camera_config("cam-idle");
		config.idle_disconnect = true;
		config.idle_disconnect_timeout_secs = Some(1.0);

		let fake = FakeCameraBuilder::new()
			.with_support(|| Ok(Support::default()))
			.with_linktype(|| {
				Err(bairelay_neolink_core::bc_protocol::Error::Other(
					"link fail",
				))
			})
			.build();
		let driver: Arc<dyn CameraDriver> = fake;

		let cancel = CancellationToken::new();
		let handle = Arc::new(CameraHandle::new(config, cancel, None));

		tokio::time::timeout(
			Duration::from_secs(30),
			handle.run_connected_session_for_test(driver),
		)
		.await
		.expect("grace period path must exit");

		assert_eq!(handle.state(), CameraState::Disconnected);
	}

	#[tokio::test]
	async fn any_stream_source_bridging_reflects_registry() {
		use crate::config::test_helpers::minimal_camera_config;
		use crate::stream_source::StreamSource;

		let config = minimal_camera_config("cam");
		let cancel = CancellationToken::new();
		let handle = CameraHandle::new(config, cancel, None);

		// Empty registry → no bridging.
		assert!(!handle.any_stream_source_bridging());

		// Inert source defaults to `GapState::Live`.
		let source = StreamSource::start_inert_for_test();
		handle.insert_stream_source_for_test(RtspStreamKind::Main, Arc::clone(&source));
		assert!(!handle.any_stream_source_bridging());

		// Flip the source to `Bridging`.
		source.set_gap_state_for_test(crate::stream_source::GapState::Bridging);
		assert!(handle.any_stream_source_bridging());

		// Back to Live.
		source.set_gap_state_for_test(crate::stream_source::GapState::Live);
		assert!(!handle.any_stream_source_bridging());
	}
}

#[cfg(test)]
mod prune_grace_tests {
	use super::*;
	use crate::config::test_helpers::minimal_camera_config;
	use crate::stream_source::StreamSource;
	use bairelay_rtsp::url::StreamKind;
	use std::time::{Duration, Instant};

	const GRACE: Duration = Duration::from_secs(60);

	fn new_handle() -> Arc<CameraHandle> {
		let config = minimal_camera_config("prune-test-cam");
		let cancel = CancellationToken::new();
		Arc::new(CameraHandle::new(config, cancel, None))
	}

	fn insert_inert_source(handle: &CameraHandle, kind: StreamKind) -> Arc<StreamSource> {
		let source = StreamSource::start_inert_for_test();
		handle.insert_stream_source_for_test(kind, Arc::clone(&source));
		source
	}

	#[tokio::test]
	async fn prune_with_zero_grace_drops_idle_source_on_first_sweep() {
		// The `grace.is_zero()` shortcut: with no grace window
		// configured, the prune sweep drops the source the moment it
		// sees zero subscribers — no "mark + wait" cycle.
		let handle = new_handle();
		let source = insert_inert_source(&handle, StreamKind::Main);
		let rx = source.subscribe_for_test();
		drop(rx);

		handle.prune_idle_stream_sources_at(Instant::now(), Duration::ZERO);
		assert!(!handle.has_stream_source_for_test(StreamKind::Main));
	}

	#[tokio::test]
	async fn prune_retains_source_within_grace_window() {
		let handle = new_handle();
		let source = insert_inert_source(&handle, StreamKind::Main);
		let rx = source.subscribe_for_test();

		// Drop the lone receiver — broadcast subscriber count goes to zero.
		drop(rx);

		// First sweep at t0: marks `last_idle_since`, keeps the source.
		let t0 = Instant::now();
		handle.prune_idle_stream_sources_at(t0, GRACE);
		assert!(handle.has_stream_source_for_test(StreamKind::Main));

		// Halfway through the grace window: still retained.
		handle.prune_idle_stream_sources_at(t0 + Duration::from_secs(30), GRACE);
		assert!(handle.has_stream_source_for_test(StreamKind::Main));
	}

	#[tokio::test]
	async fn prune_drops_source_after_grace_expires() {
		let handle = new_handle();
		let source = insert_inert_source(&handle, StreamKind::Main);
		let rx = source.subscribe_for_test();
		drop(rx);

		let t0 = Instant::now();
		handle.prune_idle_stream_sources_at(t0, GRACE);
		// 61s past t0 — past the 60 s grace.
		handle.prune_idle_stream_sources_at(t0 + Duration::from_secs(61), GRACE);
		assert!(!handle.has_stream_source_for_test(StreamKind::Main));
	}

	#[tokio::test]
	async fn prune_resets_idle_marker_on_new_subscriber() {
		let handle = new_handle();
		let source = insert_inert_source(&handle, StreamKind::Main);
		let rx = source.subscribe_for_test();
		drop(rx);

		let t0 = Instant::now();
		handle.prune_idle_stream_sources_at(t0, GRACE);

		// A new subscriber appears well before the grace expires.
		let rx2 = handle.subscribe_stream_for_test(StreamKind::Main);

		// Next sweep observes subs > 0 — marker cleared, source retained.
		handle.prune_idle_stream_sources_at(t0 + Duration::from_secs(30), GRACE);
		assert!(handle.has_stream_source_for_test(StreamKind::Main));

		// Drop the new subscriber — grace countdown must restart from here,
		// not from `t0`.
		drop(rx2);
		let t1 = t0 + Duration::from_secs(30);
		handle.prune_idle_stream_sources_at(t1, GRACE);
		// 59 s after t1: still within the fresh grace → retained.
		handle.prune_idle_stream_sources_at(t1 + Duration::from_secs(59), GRACE);
		assert!(handle.has_stream_source_for_test(StreamKind::Main));
		// 61 s after t1: past the fresh grace → pruned.
		handle.prune_idle_stream_sources_at(t1 + Duration::from_secs(61), GRACE);
		assert!(!handle.has_stream_source_for_test(StreamKind::Main));
	}
}