1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
//! Cross-transport entry point.
//!
//! Per CARVE_PLAN §6 step 4 ("Define ConnectionAdapter trait + Orchestrator
//! shell. Still no impls."): the trait surface is fully defined; the
//! Orchestrator dispatches every per-connection command through the
//! [`ConnectionAdapter`] for the connection's transport. Without a registered
//! adapter (steps 7+), commands return [`RvoipError::NoAdapterForTransport`].
//!
//! Bridging is intentionally still stubbed at this step: the cross-transport
//! frame-pump (INTERFACE_DESIGN §10.2) and the SIP-fast-path bridge strategy
//! (CARVE_PLAN §3) land in subsequent steps.
use crate::adapter::{
AdapterEvent, ConnectionAdapter, ConnectionHandle, EndReason, OriginateRequest, PlaybackHandle,
TransferTarget,
};
use crate::bridge::{codec_to_pt, frame_pump, BridgeManager, CrossBridgeHandle};
use crate::capability::{CapabilityDescriptor, CapabilityIntersection};
use crate::commands::{AudioSource, InboundAction, MuteDirection};
use crate::config::Config;
use crate::connection::Transport;
use crate::conversation::{Conversation, ConversationPolicy, ConversationState};
use crate::error::{Result, RvoipError};
use crate::events::Event;
use crate::ids::{
BridgeId, ConnectionId, ConversationId, MessageId, ParticipantId, SessionId, StreamId, TenantId,
};
use crate::message::Message;
use crate::participant::{Participant, ParticipantKind, ParticipantRole};
use crate::session::{ConnectionRef, Session, SessionMedium, SessionState};
use crate::stream::StreamKind;
use crate::vcon::VconBuilderHandle;
use chrono::Utc;
use dashmap::DashMap;
use rvoip_infra_common::events::coordinator::GlobalEventCoordinator;
use rvoip_media_core::codec::transcoding::Transcoder;
use rvoip_media_core::processing::format::FormatConverter;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use tokio::sync::{broadcast, RwLock as TokioRwLock, Semaphore};
use tracing::{debug, instrument, warn};
/// Per-connection registration tracked by the orchestrator so subsequent
/// commands (`end`, `hold`, `transfer`, `send_dtmf`, ...) can route to the
/// right adapter without the caller re-stating the transport.
#[derive(Clone, Debug)]
struct ConnectionEntry {
transport: Transport,
}
pub struct Orchestrator {
pub config: Config,
pub bridges: BridgeManager,
/// Cross-transport bridges — siblings of `bridges` (which holds the
/// SIP-fast-path `BridgeHandle`s from media-core). Dropping a handle
/// from this map aborts its two pump tasks.
cross_bridges: Arc<DashMap<BridgeId, CrossBridgeHandle>>,
pub admission: Arc<Semaphore>,
adapters: Arc<DashMap<Transport, Arc<dyn ConnectionAdapter>>>,
connections: Arc<DashMap<ConnectionId, ConnectionEntry>>,
events: broadcast::Sender<Event>,
/// Optional cross-crate publication. When `Some`, every emitted event is
/// also published through `infra-common::GlobalEventCoordinator` as the
/// `RvoipCrossCrateEvent::Core(...)` variant.
coordinator: Option<Arc<GlobalEventCoordinator>>,
/// Per-Session multi-party subscription routing tables. v0.x MP1 lands
/// the data structure + API; MP2 wires the UCTP coordinator to call
/// `add_subscription` on `stream.subscribe`; MP3 wires the media-path
/// fanout that consults `subscribers_for`. See INTERFACE_DESIGN.md
/// §10.6 and CONVERSATION_PROTOCOL.md §7.7.
subscriptions: Arc<crate::subscriptions::SubscriptionRegistry>,
/// Process-shared publisher registry — `(SessionId, strm_id) -> publisher
/// ConnectionId`. Populated by the publishing coordinator at
/// `stream.opened` time (MP2.6); consumed by the subscribing
/// coordinator's `OrchestratorSubscriptionHandler` to resolve
/// `stream.subscribe` requests. Lazily initialized via
/// [`publisher_registry`].
publisher_registry: std::sync::OnceLock<Arc<crate::subscriptions::PublisherRegistry>>,
/// Per-(sid, subscriber, publisher, publisher_strm_id) →
/// subscriber-side MediaStream allocated lazily by
/// [`Self::fanout_frame`] (plan §12 MP3c / G4). The MediaStream is
/// obtained via [`crate::adapter::ConnectionAdapter::allocate_subscriber_stream`]
/// the first time a frame is fanned out on that subscription;
/// subsequent fanouts reuse the same stream so the subscriber sees
/// each publisher's media on a stable `stream_local_id`.
///
/// For adapters that return `NotImplemented` (SIP, WebRTC, anything
/// not UCTP-family) the map stays unused and `fanout_frame` falls
/// back to the legacy pick-by-kind path so single-publisher rooms
/// keep working everywhere.
subscriber_streams: Arc<
DashMap<
(SessionId, ConnectionId, ConnectionId, StreamId),
Arc<dyn crate::stream::MediaStream>,
>,
>,
/// Per-Conversation live state (P1). Lookup key is the
/// `ConversationId` returned by [`open_conversation`]. Each value is
/// individually `RwLock`ed so lifecycle ops on different
/// Conversations don't serialize through one global lock. The
/// per-Conversation lock is held only for the brief read/mutate
/// window inside a lifecycle method — never across an `.await`.
conversations: Arc<DashMap<ConversationId, Arc<RwLock<Conversation>>>>,
/// Per-Session live state (P1). Same locking discipline as
/// `conversations`. Population by [`start_session`]; removal happens
/// when the orchestrator forgets the last Connection bound to the
/// Session (via the auto-end path in `detach_connection_from_session`)
/// or on explicit [`end_session`] + later close.
sessions: Arc<DashMap<SessionId, Arc<RwLock<Session>>>>,
/// Reverse index `ConnectionId → SessionId`. Populated by
/// [`route_inbound_connection`] when `InboundAction::Accept` carries
/// a `session_id`; cleared in `forget_connection`. Drives
/// [`session_of`] (P1.12) and the auto-end-on-last-leave path
/// (P1.10).
sessions_by_connection: Arc<DashMap<ConnectionId, SessionId>>,
/// P3 — per-Session vCon builder.
session_vcons: Arc<DashMap<SessionId, Arc<crate::vcon::DefaultVconBuilder>>>,
/// P5 — provider registry (name → `Arc<dyn Provider>`). Populated
/// by `register_asr_provider` etc. before `attach_ai` /
/// `start_recording` / `start_transcription` resolve the name.
asr_providers: Arc<DashMap<String, Arc<dyn crate::harness::AsrProvider>>>,
tts_providers: Arc<DashMap<String, Arc<dyn crate::harness::TtsProvider>>>,
dialog_managers: Arc<DashMap<String, Arc<dyn crate::harness::DialogManager>>>,
recording_sinks: Arc<DashMap<String, Arc<dyn crate::harness::RecordingSink>>>,
/// P5 — live recording sessions. Drop the JoinHandle on
/// `stop_recording` to abort the pump.
recordings: Arc<DashMap<crate::ids::RecordingId, RecordingHandle>>,
/// P5 — live transcription sessions.
transcriptions: Arc<DashMap<crate::ids::TranscriptionId, TranscriptionHandle>>,
/// P5 — live AI attachments.
ai_attachments: Arc<DashMap<crate::ids::AiAttachmentId, AiAttachmentHandle>>,
/// P5 — per-listener channel receivers (for `ListenerSink::Channel`).
listener_channels: Arc<
DashMap<
crate::ids::ListenerId,
std::sync::Mutex<Option<tokio::sync::mpsc::Receiver<crate::stream::MediaFrame>>>,
>,
>,
/// P5 — abort handles for live listener tasks. `detach` /
/// listener-target Connection ending fires the abort so the
/// forwarder task doesn't leak after its source dies. Bug-fix
/// round of the gap-plan completion sweep.
listener_tasks: Arc<DashMap<crate::ids::ListenerId, tokio::task::AbortHandle>>,
/// P9 — per-Session quality accumulator. Each `AdapterEvent::Quality`
/// updates the aggregator for the Session that owns the
/// Connection; `end_session` snapshots + fills
/// `SessionEnded.report`.
session_quality: Arc<DashMap<SessionId, QualityAggregator>>,
/// P6 — per-tenant quotas. Empty map = unlimited everywhere.
tenant_quotas: Arc<DashMap<TenantId, crate::config::TenantQuotas>>,
/// P6 — per-tenant Conversation index.
conversations_by_tenant: Arc<DashMap<TenantId, dashmap::DashSet<ConversationId>>>,
/// V2.B — per-tenant admission semaphores. When a tenant has a
/// quota for `max_concurrent_recordings`, an `Arc<Semaphore>` is
/// installed here with that capacity; `start_recording` acquires
/// an `OwnedSemaphorePermit` that lives in the `RecordingHandle`
/// and is released by Drop on `stop_recording`. Absent entry =
/// unlimited (no admission check). Replaces the DashMap-shard-
/// contention-bound check-then-increment from v1.
recording_sems: Arc<DashMap<TenantId, Arc<Semaphore>>>,
ai_sems: Arc<DashMap<TenantId, Arc<Semaphore>>>,
}
/// P5 — internal handles for live attachments.
pub(crate) struct RecordingHandle {
pub sink: Arc<dyn crate::harness::RecordingSink>,
pub abort: tokio::task::AbortHandle,
/// P5 — `false` while paused; pump task watches this and drops
/// frames silently rather than writing them to the sink. Resumed
/// by flipping back to `true`.
pub paused: Arc<std::sync::atomic::AtomicBool>,
/// V2.B — admission permit; held while recording is live, released
/// automatically on Drop (i.e. on `stop_recording` removal). `None`
/// when the tenant had no `max_concurrent_recordings` quota at
/// start time.
pub _permit: Option<tokio::sync::OwnedSemaphorePermit>,
}
pub(crate) struct TranscriptionHandle {
pub abort: tokio::task::AbortHandle,
}
/// P9 — running aggregator for per-Session quality samples.
/// Accumulated by `handle_adapter_event` on `AdapterEvent::Quality`
/// and snapshotted by `end_session` to populate
/// `Event::SessionEnded.report`.
#[derive(Debug, Default)]
pub(crate) struct QualityAggregator {
pub samples: usize,
pub jitter_ms_sum: f64,
pub packet_loss_pct_sum: f64,
pub mos_sum: f64,
pub mos_samples: usize,
pub codec: Option<String>,
pub started_at: Option<chrono::DateTime<chrono::Utc>>,
}
impl QualityAggregator {
pub fn add(&mut self, snap: &crate::stream::QualitySnapshot, codec: Option<String>) {
self.samples += 1;
self.jitter_ms_sum += snap.jitter_ms as f64;
self.packet_loss_pct_sum += snap.packet_loss_pct as f64;
if let Some(mos) = snap.mos {
self.mos_sum += mos as f64;
self.mos_samples += 1;
}
if self.codec.is_none() {
self.codec = codec;
}
if self.started_at.is_none() {
self.started_at = Some(chrono::Utc::now());
}
}
pub fn finish(self) -> Option<crate::events::SessionQualityReport> {
if self.samples == 0 {
return None;
}
let avg_jitter = (self.jitter_ms_sum / self.samples as f64) as f32;
let avg_loss = (self.packet_loss_pct_sum / self.samples as f64) as f32;
let avg_mos = if self.mos_samples > 0 {
Some((self.mos_sum / self.mos_samples as f64) as f32)
} else {
None
};
Some(crate::events::SessionQualityReport {
mos: avg_mos,
packet_loss_pct: avg_loss,
jitter_ms: avg_jitter,
rtt_ms: None,
codec: self.codec,
bitrate_bps: None,
talk_pct: None,
silence_pct: None,
pdd_ms: None,
ring_time_ms: None,
setup_time_ms: None,
hangup_reason: None,
})
}
}
pub(crate) struct AiAttachmentHandle {
pub abort: tokio::task::AbortHandle,
/// P5 — flips to `true` when a TTS playback is in flight and to
/// `false` when it isn't. Barge-in inspects this to decide
/// whether an incoming ASR partial should cancel a playback.
/// Stored here only to keep the Arc alive at the orchestrator
/// level; the dialog task holds its own clone and does all the
/// reads. Retained so a future external "is speaking?" / "stop
/// speaking" API can hook into it without re-plumbing the task.
#[allow(dead_code)]
pub speaking: Arc<std::sync::atomic::AtomicBool>,
/// P5 — current playback cancel signal. When barge-in fires, the
/// orchestrator sends `()` to abort the in-flight TTS pipe.
/// Same lifetime/retention rationale as `speaking` above.
#[allow(dead_code)]
pub speak_cancel: Arc<tokio::sync::Mutex<Option<tokio::sync::oneshot::Sender<()>>>>,
/// V2.B — admission permit; released on detach via Drop.
pub _permit: Option<tokio::sync::OwnedSemaphorePermit>,
}
impl Orchestrator {
pub fn new(config: Config) -> Arc<Self> {
let admission = Arc::new(Semaphore::new(config.max_concurrent_setups));
let (events, _rx) = broadcast::channel(1024);
Arc::new(Self {
config,
bridges: BridgeManager::new(),
cross_bridges: Arc::new(DashMap::new()),
admission,
adapters: Arc::new(DashMap::new()),
connections: Arc::new(DashMap::new()),
events,
coordinator: None,
subscriptions: Arc::new(crate::subscriptions::SubscriptionRegistry::new()),
publisher_registry: std::sync::OnceLock::new(),
subscriber_streams: Arc::new(DashMap::new()),
conversations: Arc::new(DashMap::new()),
sessions: Arc::new(DashMap::new()),
sessions_by_connection: Arc::new(DashMap::new()),
session_vcons: Arc::new(DashMap::new()),
asr_providers: Arc::new(DashMap::new()),
tts_providers: Arc::new(DashMap::new()),
dialog_managers: Arc::new(DashMap::new()),
recording_sinks: Arc::new(DashMap::new()),
recordings: Arc::new(DashMap::new()),
transcriptions: Arc::new(DashMap::new()),
ai_attachments: Arc::new(DashMap::new()),
listener_channels: Arc::new(DashMap::new()),
listener_tasks: Arc::new(DashMap::new()),
session_quality: Arc::new(DashMap::new()),
tenant_quotas: Arc::new(DashMap::new()),
conversations_by_tenant: Arc::new(DashMap::new()),
recording_sems: Arc::new(DashMap::new()),
ai_sems: Arc::new(DashMap::new()),
})
}
pub fn new_with_coordinator(
config: Config,
coordinator: Arc<GlobalEventCoordinator>,
) -> Arc<Self> {
let admission = Arc::new(Semaphore::new(config.max_concurrent_setups));
let (events, _rx) = broadcast::channel(1024);
Arc::new(Self {
config,
bridges: BridgeManager::new(),
cross_bridges: Arc::new(DashMap::new()),
admission,
adapters: Arc::new(DashMap::new()),
connections: Arc::new(DashMap::new()),
events,
coordinator: Some(coordinator),
subscriptions: Arc::new(crate::subscriptions::SubscriptionRegistry::new()),
publisher_registry: std::sync::OnceLock::new(),
subscriber_streams: Arc::new(DashMap::new()),
conversations: Arc::new(DashMap::new()),
sessions: Arc::new(DashMap::new()),
sessions_by_connection: Arc::new(DashMap::new()),
session_vcons: Arc::new(DashMap::new()),
asr_providers: Arc::new(DashMap::new()),
tts_providers: Arc::new(DashMap::new()),
dialog_managers: Arc::new(DashMap::new()),
recording_sinks: Arc::new(DashMap::new()),
recordings: Arc::new(DashMap::new()),
transcriptions: Arc::new(DashMap::new()),
ai_attachments: Arc::new(DashMap::new()),
listener_channels: Arc::new(DashMap::new()),
listener_tasks: Arc::new(DashMap::new()),
session_quality: Arc::new(DashMap::new()),
tenant_quotas: Arc::new(DashMap::new()),
conversations_by_tenant: Arc::new(DashMap::new()),
recording_sems: Arc::new(DashMap::new()),
ai_sems: Arc::new(DashMap::new()),
})
}
/// Register a transport adapter. Spawns a background task that pulls
/// `AdapterEvent`s from the adapter's subscribe channel and normalizes
/// them into rvoip-core [`Event`]s on the orchestrator's broadcast bus.
/// Returns [`RvoipError::AdapterAlreadyRegistered`] on collision.
pub fn register(self: &Arc<Self>, adapter: Arc<dyn ConnectionAdapter>) -> Result<()> {
let transport = adapter.transport();
if self.adapters.contains_key(&transport) {
return Err(RvoipError::AdapterAlreadyRegistered(transport));
}
let mut events = adapter.subscribe_events();
self.adapters.insert(transport, adapter);
// Spawn the per-adapter event-normalize loop. Each AdapterEvent is
// translated into one or more rvoip-core Events and republished.
let me = Arc::clone(self);
tokio::spawn(async move {
while let Some(event) = events.recv().await {
me.handle_adapter_event(transport, event);
}
debug!(?transport, "adapter event stream ended");
});
Ok(())
}
pub fn adapter(&self, transport: Transport) -> Result<Arc<dyn ConnectionAdapter>> {
self.adapters
.get(&transport)
.map(|e| e.value().clone())
.ok_or(RvoipError::NoAdapterForTransport(transport))
}
pub fn subscribe_events(&self) -> broadcast::Receiver<Event> {
self.events.subscribe()
}
/// Look up which adapter owns a given connection. Returns
/// [`RvoipError::ConnectionNotFound`] if the connection isn't registered.
fn adapter_for(&self, conn: &ConnectionId) -> Result<Arc<dyn ConnectionAdapter>> {
let entry = self
.connections
.get(conn)
.ok_or_else(|| RvoipError::ConnectionNotFound(conn.clone()))?;
let transport = entry.transport;
drop(entry);
self.adapter(transport)
}
fn track_connection(&self, conn: &ConnectionId, transport: Transport) {
self.connections
.insert(conn.clone(), ConnectionEntry { transport });
}
/// If `conn` is currently in a cross-transport bridge, return the
/// peer `ConnectionId` on the other leg. Gap plan §4.3 / v1 punch
/// list — used by the DTMF auto-route in the `AdapterEvent::Dtmf`
/// handler to forward digits across the bridge when one side
/// signals DTMF out-of-band (e.g. UCTP `dtmf.send` envelope) and
/// the bridged peer needs to inject the corresponding RFC 4733
/// telephone-event packets onto its outbound RTP.
fn bridge_peer_of(&self, conn: &ConnectionId) -> Option<ConnectionId> {
for entry in self.cross_bridges.iter() {
let h = entry.value();
if &h.a == conn {
return Some(h.b.clone());
}
if &h.b == conn {
return Some(h.a.clone());
}
}
None
}
fn forget_connection(&self, conn: &ConnectionId) {
self.connections.remove(conn);
// P1.10 — if this Connection was bound to a Session, detach it
// and auto-end the Session when it loses its last Connection.
// Must run before subscription cleanup so the Session lookup
// sees a stable connection set.
self.detach_connection_from_session(conn);
// Eagerly clean up any subscriptions that name this Connection
// (either as publisher or subscriber). Idempotent — see
// `SessionSubscriptions::drop_connection` for the contract.
self.subscriptions.drop_connection(conn);
// Mirror the cleanup into the publisher registry so a publisher
// that hangs up doesn't leave stale `(sid, strm_id) -> connid`
// and `(sid, participant) -> [strm_id]` rows that a subsequent
// `from_participant` subscribe would resolve to a dead Connection.
// Skip if the registry was never lazily initialized.
if let Some(reg) = self.publisher_registry.get() {
reg.drop_publisher(conn);
}
// MP3c subscriber-stream map: drop rows that name this
// Connection as subscriber OR publisher so the per-subscription
// MediaStream goes out of scope along with the substrate-level
// Connection. Without this, the per-publisher MediaStreams keep
// a strong reference to the dead Connection's quinn handle.
self.subscriber_streams
.retain(|(_, sub, pubr, _), _| sub != conn && pubr != conn);
}
// --- Conversation / Session / Participant lifecycle (P1) -----------
//
// Implements the 7 lifecycle Commands (`OpenConversation`,
// `CloseConversation`, `StartSession`, `EndSession`, `JoinSession`,
// `LeaveSession`, `RouteInboundConnection::Accept`) per
// INTERFACE_DESIGN.md §3 + PRD §10. Each method is `async` to match
// the trait-friendly shape the GAP_PLAN promised, even though the
// work today is purely synchronous lock acquisition + event emit.
/// Open a new Conversation. Emits `Event::ConversationOpened`.
/// Returns the freshly-allocated `ConversationId`.
#[instrument(skip(self, metadata), fields(tenant = %tenant_id, conversation_id))]
pub async fn open_conversation(
&self,
tenant_id: TenantId,
policy: ConversationPolicy,
metadata: HashMap<String, String>,
) -> Result<ConversationId> {
let id = ConversationId::new();
let now = Utc::now();
let conv = Conversation {
id: id.clone(),
tenant_id,
state: ConversationState::Open,
policy,
participants: Vec::new(),
sessions: Vec::new(),
messages: Vec::new(),
opened_at: now,
closed_at: None,
last_activity_at: now,
metadata,
};
self.conversations
.insert(id.clone(), Arc::new(RwLock::new(conv)));
// P6 — index by tenant for `list_for_tenant` and isolation
// enforcement.
self.conversations_by_tenant
.entry(tenant_id_for_index(&self.conversations, &id))
.or_default()
.insert(id.clone());
self.emit(Event::ConversationOpened {
conversation_id: id.clone(),
at: now,
});
Ok(id)
}
/// P6 — install/replace per-tenant quotas. V2.B provisions the
/// per-tenant admission semaphores from the quota config: each
/// `max_concurrent_*` slot gets an `Arc<Semaphore>` with that
/// capacity. Resize-up is supported (extra permits added via
/// `Semaphore::add_permits`); resize-down with live permits would
/// require revoking issued permits and is intentionally rejected
/// — call sites that want to shrink a quota should drain the
/// active sessions first.
pub fn set_tenant_quotas(
&self,
tenant: TenantId,
quotas: crate::config::TenantQuotas,
) -> Result<()> {
// Provision / resize recording semaphore.
if let Some(new_cap) = quotas.max_concurrent_recordings {
match self.recording_sems.entry(tenant.clone()) {
dashmap::mapref::entry::Entry::Vacant(v) => {
v.insert(Arc::new(Semaphore::new(new_cap)));
}
dashmap::mapref::entry::Entry::Occupied(o) => {
// Compare against an implicit "total issued" — we
// can't directly read total capacity from a tokio
// Semaphore, so we track resize-up by checking if
// new_cap exceeds current available + outstanding.
// Outstanding = total - available. We approximate
// by using the Semaphore's add_permits which always
// adds (no resize-down possible).
let sem = o.get();
let available = sem.available_permits();
// For resize-up: add (new - available) permits when
// new > available. This is conservative — if the
// existing cap was already higher than `available`,
// we may end up adding too few permits (loss of
// capacity that's currently held). Documented as
// a v2.B.1 caveat — call sites that mix shrink and
// expand on the same tenant need explicit drain
// semantics.
if new_cap > available {
sem.add_permits(new_cap - available);
} else if new_cap < available {
return Err(RvoipError::InvalidState(
"set_tenant_quotas: shrinking recording quota \
not supported while permits are held; drain first",
));
}
}
}
}
if let Some(new_cap) = quotas.max_concurrent_ai_sessions {
match self.ai_sems.entry(tenant.clone()) {
dashmap::mapref::entry::Entry::Vacant(v) => {
v.insert(Arc::new(Semaphore::new(new_cap)));
}
dashmap::mapref::entry::Entry::Occupied(o) => {
let sem = o.get();
let available = sem.available_permits();
if new_cap > available {
sem.add_permits(new_cap - available);
} else if new_cap < available {
return Err(RvoipError::InvalidState(
"set_tenant_quotas: shrinking AI quota not \
supported while permits are held; drain first",
));
}
}
}
}
self.tenant_quotas.insert(tenant, quotas);
Ok(())
}
/// P6 — best-effort snapshot for the periodic capacity scheduler
/// and on-demand inspection. P9 — also updates the global
/// Prometheus gauges so a scraper sees current state without
/// having to subscribe to the event bus.
pub fn capacity_report(&self) -> Event {
let active_connections = self.connections.len() as u64;
let active_bridges = self.cross_bridges.len() as u64;
let admission_in_use =
(self.config.max_concurrent_setups - self.admission.available_permits()) as u64;
let active_sessions = self.sessions.len() as u64;
let active_conversations = self.conversations.len() as u64;
let active_recordings = self.recordings.len() as u64;
let active_ai = self.ai_attachments.len() as u64;
metrics::gauge!("rvoip_active_connections").set(active_connections as f64);
metrics::gauge!("rvoip_active_bridges").set(active_bridges as f64);
metrics::gauge!("rvoip_admission_in_use").set(admission_in_use as f64);
metrics::gauge!("rvoip_active_sessions").set(active_sessions as f64);
metrics::gauge!("rvoip_active_conversations").set(active_conversations as f64);
metrics::gauge!("rvoip_active_recordings").set(active_recordings as f64);
metrics::gauge!("rvoip_active_ai_attachments").set(active_ai as f64);
Event::CapacityReport {
tenant_id: None,
active_connections,
active_bridges,
admission_in_use,
at: Utc::now(),
}
}
/// P9 — sample current `QualitySnapshot` for every active
/// Connection at the configured cadence and emit
/// `Event::MediaQuality`. Spawns one task that ticks `every`.
pub fn spawn_media_quality_sampler(self: &Arc<Self>, every: std::time::Duration) {
let me = Arc::clone(self);
tokio::spawn(async move {
let mut tick = tokio::time::interval(every);
tick.tick().await;
loop {
tick.tick().await;
// Snapshot connections.
let conns: Vec<(ConnectionId, Transport)> = me
.connections
.iter()
.map(|e| (e.key().clone(), e.value().transport))
.collect();
for (cid, transport) in conns {
let Ok(adapter) = me.adapter(transport) else {
continue;
};
let Ok(streams) = adapter.streams(cid.clone()).await else {
continue;
};
let mut totaled = crate::stream::QualitySnapshot {
jitter_ms: 0.0,
packet_loss_pct: 0.0,
mos: None,
};
let mut n = 0usize;
for s in streams {
let snap = s.quality_snapshot();
totaled.jitter_ms += snap.jitter_ms;
totaled.packet_loss_pct += snap.packet_loss_pct;
if let Some(m) = snap.mos {
totaled.mos = Some(totaled.mos.map_or(m, |a| a + m));
}
n += 1;
}
if n == 0 {
continue;
}
totaled.jitter_ms /= n as f32;
totaled.packet_loss_pct /= n as f32;
totaled.mos = totaled.mos.map(|m| m / n as f32);
me.emit(Event::MediaQuality {
connection_id: cid,
snapshot: totaled,
at: Utc::now(),
});
}
}
});
}
/// P10 — drive idle-close of `Ephemeral` Conversations. Spawns
/// one task that ticks `every` and force-closes any Conversation
/// whose `last_activity_at` is older than its policy's
/// `idle_close_secs` AND has no `Active` Sessions.
pub fn spawn_idle_closer(self: &Arc<Self>, every: std::time::Duration) {
let me = Arc::clone(self);
tokio::spawn(async move {
let mut tick = tokio::time::interval(every);
tick.tick().await;
loop {
tick.tick().await;
let now = Utc::now();
let mut to_close: Vec<ConversationId> = Vec::new();
for entry in me.conversations.iter() {
let c = entry.value().read().expect("conv lock poisoned");
let ConversationPolicy::Ephemeral { idle_close_secs } = c.policy else {
continue;
};
if c.state != ConversationState::Open {
continue;
}
let idle = (now - c.last_activity_at).num_seconds().max(0) as u64;
if idle < idle_close_secs {
continue;
}
// Skip if any Session is Active.
let any_active = c.sessions.iter().any(|sid| {
me.sessions
.get(sid)
.map(|s| {
s.value().read().expect("sess lock poisoned").state
== SessionState::Active
})
.unwrap_or(false)
});
if any_active {
continue;
}
to_close.push(entry.key().clone());
}
for cid in to_close {
let _ = me.close_conversation(cid, false).await;
}
}
});
}
/// P6 — start the periodic capacity-report emitter using the
/// cadence in `Config::capacity_report_interval`. Returns
/// immediately; the scheduler task is owned by the Orchestrator
/// and aborts when the Orchestrator is dropped (best-effort —
/// real teardown semantics ship with P11 graceful-shutdown).
pub fn spawn_capacity_scheduler(self: &Arc<Self>) {
let Some(interval) = self.config.capacity_report_interval else {
return;
};
let me = Arc::clone(self);
tokio::spawn(async move {
let mut tick = tokio::time::interval(interval);
// Skip the immediate tick — first emit happens after one
// interval.
tick.tick().await;
loop {
tick.tick().await;
me.emit(me.capacity_report());
}
});
}
fn check_session_quota(&self, conv_id: &ConversationId) -> Result<()> {
let Some(tenant) = self.conversations.get(conv_id).map(|e| {
e.value()
.read()
.expect("conv lock poisoned")
.tenant_id
.clone()
}) else {
return Ok(());
};
let Some(quotas) = self.tenant_quotas.get(&tenant).map(|e| *e.value()) else {
return Ok(());
};
if let Some(max) = quotas.max_concurrent_sessions {
// Count active sessions for this tenant.
let mut active = 0usize;
if let Some(convs) = self.conversations_by_tenant.get(&tenant) {
for cid in convs.iter() {
if let Some(conv_arc) =
self.conversations.get(&*cid).map(|e| Arc::clone(e.value()))
{
for sid in &conv_arc.read().expect("conv lock poisoned").sessions {
if let Some(sess) = self.sessions.get(sid) {
if sess.value().read().expect("sess lock poisoned").state
== SessionState::Active
{
active += 1;
}
}
}
}
}
}
if active >= max {
return Err(RvoipError::AdmissionRejected(
"tenant max_concurrent_sessions exceeded",
));
}
}
Ok(())
}
/// Close a Conversation. `force=false` rejects with `InvalidState`
/// when any Session under the Conversation is still active;
/// `force=true` first ends those Sessions (best-effort), then
/// transitions the Conversation to Closed and emits
/// `Event::ConversationClosed`. Closing an already-Closed
/// Conversation is a no-op (idempotent).
#[instrument(skip(self), fields(conversation_id = %id, force))]
pub async fn close_conversation(&self, id: ConversationId, force: bool) -> Result<()> {
let conv_arc = self
.conversations
.get(&id)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::ConversationNotFound(id.clone()))?;
let active_sessions: Vec<SessionId> = {
let conv = conv_arc.read().expect("conversation lock poisoned");
if conv.state == ConversationState::Closed {
return Ok(());
}
conv.sessions
.iter()
.filter(|sid| {
self.sessions
.get(sid)
.map(|s| {
let st = s.value().read().expect("session lock poisoned").state;
!matches!(st, SessionState::Ended | SessionState::Failed)
})
.unwrap_or(false)
})
.cloned()
.collect()
};
if !active_sessions.is_empty() && !force {
return Err(RvoipError::InvalidState(
"close_conversation: active sessions exist; pass force=true to end them",
));
}
if force {
for sid in active_sessions {
let _ = self.end_session(sid, EndReason::Normal).await;
}
}
let now = Utc::now();
{
let mut conv = conv_arc.write().expect("conversation lock poisoned");
conv.state = ConversationState::Closed;
conv.closed_at = Some(now);
conv.last_activity_at = now;
}
self.emit(Event::ConversationClosed {
conversation_id: id,
at: now,
});
Ok(())
}
/// Start a new Session within an Open Conversation. Emits
/// `Event::SessionStarted`. `invitees` populates the
/// `Session::participants` set immediately; matching `Participant`
/// entries are added to the Conversation when each invitee actually
/// joins via `join_session` (so identity_ref / kind / role land
/// from a real join, not from the invite).
#[instrument(skip(self, invitees), fields(conversation_id = %conversation_id, medium = ?medium, session_id))]
pub async fn start_session(
&self,
conversation_id: ConversationId,
medium: SessionMedium,
invitees: Vec<ParticipantId>,
) -> Result<SessionId> {
let conv_arc = self
.conversations
.get(&conversation_id)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::ConversationNotFound(conversation_id.clone()))?;
{
let conv = conv_arc.read().expect("conversation lock poisoned");
if conv.state != ConversationState::Open {
return Err(RvoipError::InvalidState(
"start_session: conversation is not Open",
));
}
}
// P6 — quota check.
self.check_session_quota(&conversation_id)?;
let sid = SessionId::new();
let now = Utc::now();
let session = Session {
id: sid.clone(),
conversation_id: conversation_id.clone(),
state: SessionState::Initiating,
medium,
participants: invitees.into_iter().collect(),
connections: HashMap::new(),
negotiated_capabilities: CapabilityIntersection::default(),
started_at: now,
ended_at: None,
end_reason: None,
};
self.sessions
.insert(sid.clone(), Arc::new(RwLock::new(session)));
// P3 — every Session gets a vCon builder bound to it on start.
self.session_vcons.insert(
sid.clone(),
Arc::new(crate::vcon::DefaultVconBuilder::new()),
);
{
let mut conv = conv_arc.write().expect("conversation lock poisoned");
conv.sessions.push(sid.clone());
conv.last_activity_at = now;
}
self.emit(Event::SessionStarted {
session_id: sid.clone(),
conversation_id,
at: now,
});
Ok(sid)
}
/// End a Session. Transitions state to `Ended`, drops multi-party
/// subscriptions, clears the reverse Connection→Session index, and
/// emits `Event::SessionEnded`. Idempotent: ending an already-
/// Ended or Failed Session returns `Ok(())`.
#[instrument(skip(self), fields(session_id = %session_id, reason = ?reason))]
pub async fn end_session(&self, session_id: SessionId, reason: EndReason) -> Result<()> {
let sess_arc = self
.sessions
.get(&session_id)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::SessionNotFound(session_id.clone()))?;
let now = Utc::now();
let conv_id = {
let mut sess = sess_arc.write().expect("session lock poisoned");
if matches!(sess.state, SessionState::Ended | SessionState::Failed) {
return Ok(());
}
sess.state = SessionState::Ended;
sess.ended_at = Some(now);
sess.end_reason = Some(reason);
sess.conversation_id.clone()
};
// Multi-party cleanup + reverse-index cleanup.
self.drop_session_subscriptions(&session_id);
self.sessions_by_connection
.retain(|_, sid| sid != &session_id);
// P3 — finalize the Session's vCon: snapshot, encode, persist,
// emit VconReady. Best-effort — a store failure logs but does
// not block SessionEnded emission.
let tenant_id = self.conversations.get(&conv_id).map(|e| {
e.value()
.read()
.expect("conv lock poisoned")
.tenant_id
.clone()
});
if let (Some((_, builder)), Some(tenant_id)) =
(self.session_vcons.remove(&session_id), tenant_id)
{
let snap = builder.snapshot();
let bytes = crate::vcon::encode_snapshot(&snap);
let store = Arc::clone(&self.config.vcon_store);
let sid_clone = session_id.clone();
let events_tx = self.events.clone();
let coordinator = self.coordinator.clone();
tokio::spawn(async move {
match store.put(&tenant_id, &sid_clone, bytes).await {
Ok(handle) => {
let ev = Event::VconReady {
session_id: sid_clone,
handle,
at: Utc::now(),
};
if let Some(coord) = coordinator {
let cross = ev.to_cross_crate();
let _ = coord.publish(Arc::new(cross)).await;
}
let _ = events_tx.send(ev);
}
Err(e) => warn!(?e, "VconStore::put failed; VconReady not emitted"),
}
});
}
if let Some(conv_arc) = self
.conversations
.get(&conv_id)
.map(|e| Arc::clone(e.value()))
{
conv_arc
.write()
.expect("conversation lock poisoned")
.last_activity_at = now;
}
// P9 — snapshot the per-Session quality aggregator.
let report = self
.session_quality
.remove(&session_id)
.and_then(|(_, agg)| agg.finish());
self.emit(Event::SessionEnded {
report,
session_id,
at: now,
});
Ok(())
}
/// P3 — read access to a Session's vCon builder. Returns None if
/// the Session is not active.
pub fn session_vcon_handle(
&self,
session_id: &SessionId,
) -> Option<Arc<dyn crate::vcon::VconBuilderHandle>> {
self.session_vcons
.get(session_id)
.map(|e| Arc::clone(e.value()) as Arc<dyn crate::vcon::VconBuilderHandle>)
}
/// Join a Participant to a Session. First join transitions the
/// Session from `Initiating` to `Active`. Adds a matching
/// `Participant` entry to the parent Conversation if one doesn't
/// exist yet. Emits `Event::ParticipantJoined`. Rejects with
/// `InvalidState` for Sessions in `Ending`, `Ended`, or `Failed`.
pub async fn join_session(
&self,
session_id: SessionId,
participant_id: ParticipantId,
kind: ParticipantKind,
role: ParticipantRole,
) -> Result<()> {
let sess_arc = self
.sessions
.get(&session_id)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::SessionNotFound(session_id.clone()))?;
let now = Utc::now();
let conv_id = {
let mut sess = sess_arc.write().expect("session lock poisoned");
if matches!(
sess.state,
SessionState::Ending | SessionState::Ended | SessionState::Failed
) {
return Err(RvoipError::InvalidState(
"join_session: session is ending or ended",
));
}
sess.participants.insert(participant_id.clone());
if sess.state == SessionState::Initiating {
sess.state = SessionState::Active;
}
sess.conversation_id.clone()
};
if let Some(conv_arc) = self
.conversations
.get(&conv_id)
.map(|e| Arc::clone(e.value()))
{
let mut conv = conv_arc.write().expect("conversation lock poisoned");
let exists = conv.participants.iter().any(|p| p.id == participant_id);
if !exists {
conv.participants.push(Participant {
id: participant_id.clone(),
conversation_id: conv_id.clone(),
identity_ref: None,
kind,
role,
display_name: None,
joined_at: now,
left_at: None,
});
}
conv.last_activity_at = now;
}
// P3 — auto-collect the joining party into the Session's vCon.
if let Some(builder) = self
.session_vcons
.get(&session_id)
.map(|e| Arc::clone(e.value()))
{
builder.add_party(crate::vcon::VconParty {
participant_id: participant_id.clone(),
display_name: None,
did_or_stir: None,
validation: crate::identity::IdentityAssurance::Anonymous,
});
}
self.emit(Event::ParticipantJoined {
session_id,
participant_id,
at: now,
});
Ok(())
}
/// Remove a Participant from a Session. Sets `left_at` on the
/// matching Conversation-level `Participant` entry if present.
/// Emits `Event::ParticipantLeft`. Idempotent — leaving a Session
/// the Participant isn't in is a no-op (still emits the event so
/// downstream consumers see the intent).
pub async fn leave_session(
&self,
session_id: SessionId,
participant_id: ParticipantId,
) -> Result<()> {
let sess_arc = self
.sessions
.get(&session_id)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::SessionNotFound(session_id.clone()))?;
let now = Utc::now();
let conv_id = {
let mut sess = sess_arc.write().expect("session lock poisoned");
sess.participants.remove(&participant_id);
sess.conversation_id.clone()
};
if let Some(conv_arc) = self
.conversations
.get(&conv_id)
.map(|e| Arc::clone(e.value()))
{
let mut conv = conv_arc.write().expect("conversation lock poisoned");
if let Some(p) = conv
.participants
.iter_mut()
.find(|p| p.id == participant_id)
{
p.left_at = Some(now);
}
conv.last_activity_at = now;
}
self.emit(Event::ParticipantLeft {
session_id,
participant_id,
at: now,
});
Ok(())
}
/// P1.12 — reverse lookup `ConnectionId → SessionId`. Populated by
/// `route_inbound_connection` on `InboundAction::Accept`; cleared
/// by `forget_connection`.
pub fn session_of(&self, connection_id: &ConnectionId) -> Option<SessionId> {
self.sessions_by_connection
.get(connection_id)
.map(|e| e.value().clone())
}
/// Read-only handle to a live Conversation. Holds the inner Arc
/// across the borrow; the caller manages the `RwLock`. Returns
/// `None` if the Conversation was never opened or has already been
/// purged.
pub fn conversation(&self, id: &ConversationId) -> Option<Arc<RwLock<Conversation>>> {
self.conversations.get(id).map(|e| Arc::clone(e.value()))
}
/// Read-only handle to a live Session. See [`Self::conversation`]
/// for the locking contract.
pub fn session(&self, id: &SessionId) -> Option<Arc<RwLock<Session>>> {
self.sessions.get(id).map(|e| Arc::clone(e.value()))
}
/// P1.10 — Connection has gone away (adapter `Ended` / `Failed`).
/// If it was bound to a Session, remove it from
/// `Session.connections`. When the removal drops the last
/// Connection from an `Active` Session, auto-transition to `Ended`
/// and emit `SessionEnded`. Inline (no spawn) — the work is all
/// synchronous lock acquisition + event emission.
fn detach_connection_from_session(&self, conn: &ConnectionId) {
let Some((_, sid)) = self.sessions_by_connection.remove(conn) else {
return;
};
let Some(sess_arc) = self.sessions.get(&sid).map(|e| Arc::clone(e.value())) else {
return;
};
let (auto_end, conv_id) = {
let mut sess = sess_arc.write().expect("session lock poisoned");
sess.connections.remove(conn);
let auto_end = sess.state == SessionState::Active && sess.connections.is_empty();
(auto_end, sess.conversation_id.clone())
};
if !auto_end {
return;
}
let now = Utc::now();
{
let mut sess = sess_arc.write().expect("session lock poisoned");
sess.state = SessionState::Ended;
sess.ended_at = Some(now);
sess.end_reason = Some(EndReason::Normal);
}
self.drop_session_subscriptions(&sid);
if let Some(conv_arc) = self
.conversations
.get(&conv_id)
.map(|e| Arc::clone(e.value()))
{
conv_arc
.write()
.expect("conversation lock poisoned")
.last_activity_at = now;
}
// P9 — snapshot the per-Session quality aggregator.
let report = self
.session_quality
.remove(&sid)
.and_then(|(_, agg)| agg.finish());
self.emit(Event::SessionEnded {
report,
session_id: sid,
at: now,
});
}
// --- Multi-party subscription routing (v0.x MP1) -------------------
//
// Wire layer (`stream.subscribe` / `stream.unsubscribe` from the UCTP
// coordinator) lands in MP2; media-path fanout that consults
// `subscribers_for` lands in MP3. The methods below are the stable
// surface those two PRs target.
/// Add a subscription: `subscriber` will receive media datagrams
/// from `publisher`'s `strm_id` Stream within `sid`. Idempotent.
///
/// v0.x scope: stores the routing row only. The wire-side handler
/// translating `stream.subscribe` envelopes into one or more
/// `add_subscription` calls lands in MP2; the media-path fanout
/// that drives this lookup lands in MP3.
pub fn add_subscription(
&self,
sid: SessionId,
subscriber: ConnectionId,
publisher: ConnectionId,
strm_id: StreamId,
) {
let table = self.subscriptions.for_session(&sid);
table.add(publisher, strm_id, subscriber);
}
/// Remove a single subscription. Idempotent — removing a
/// subscription that doesn't exist is a no-op (returns `false`).
pub fn remove_subscription(
&self,
sid: &SessionId,
subscriber: &ConnectionId,
publisher: &ConnectionId,
strm_id: &StreamId,
) -> bool {
let table = self.subscriptions.for_session(sid);
table.remove(publisher, strm_id, subscriber)
}
/// Snapshot the set of Connections subscribed to `(publisher,
/// strm_id)` within `sid`. The media-path fanout (MP3) iterates
/// the returned vec without holding any subscription-table lock.
pub fn subscribers_for(
&self,
sid: &SessionId,
publisher: &ConnectionId,
strm_id: &StreamId,
) -> Vec<ConnectionId> {
let table = self.subscriptions.for_session(sid);
table.subscribers_for(publisher, strm_id)
}
/// Drop the entire subscription table for a Session. Called on
/// `session.ended`. Idempotent.
pub fn drop_session_subscriptions(&self, sid: &SessionId) {
self.subscriptions.drop_session(sid);
// Same mirror as `forget_connection`: clear publisher rows for
// this Session so a `from_participant` subscribe issued after a
// late peer joins on a recycled SessionId can't resolve to a
// dead row from the previous tenant.
if let Some(reg) = self.publisher_registry.get() {
reg.drop_session(sid);
}
// MP3c: drop all per-subscription MediaStreams owned by this
// Session.
self.subscriber_streams.retain(|(s, _, _, _), _| s != sid);
}
/// Fan a publisher's `MediaFrame` out to every subscriber of
/// `(sid, publisher, strm_id)`. v0.x MP3a primitive — adapter
/// datagram-receive loops call this after unpacking a publisher's
/// datagram (MP3b wires the publisher-side trigger).
///
/// Per-subscriber stream resolution (plan §12 MP3c / G4):
/// 1. Try the cached subscriber-side MediaStream for
/// `(sid, subscriber, publisher, strm_id)`. Reuses prior
/// allocation so each publisher's frames keep landing on the
/// same `stream_local_id`.
/// 2. If absent, ask the subscriber's adapter to allocate a fresh
/// one via [`crate::adapter::ConnectionAdapter::allocate_subscriber_stream`].
/// The adapter picks the next free `stream_local_id`, registers
/// the MediaStream for inbound routing, and emits a
/// `stream.opened` envelope so the peer learns the new id.
/// 3. If the adapter doesn't support allocation (returns
/// `NotImplemented` — e.g. SIP, WebRTC, or any adapter that
/// doesn't own the multi-party wire surface), fall back to the
/// legacy "first matching MediaStream by kind" path. Keeps
/// single-publisher rooms working unchanged.
///
/// Returns the number of subscribers a frame was successfully
/// delivered to. Best-effort: per-subscriber failures (channel
/// full, adapter error) are logged at `debug` and do not block the
/// remaining subscribers.
///
/// Refinement still deferred: codec mismatch validation.
/// `add_subscription` accepts any pair today; codec checking
/// alongside `PublisherRegistry` codec metadata is plan B2.
pub async fn fanout_frame(
&self,
sid: &SessionId,
publisher: &ConnectionId,
strm_id: &StreamId,
frame: crate::stream::MediaFrame,
) -> usize {
let subscribers = self.subscribers_for(sid, publisher, strm_id);
let mut delivered = 0;
for subscriber_connid in subscribers {
let Ok(adapter) = self.adapter_for(&subscriber_connid) else {
continue;
};
let key = (
sid.clone(),
subscriber_connid.clone(),
publisher.clone(),
strm_id.clone(),
);
// (1) Cached per-subscription stream — MP3c path.
let target_opt: Option<Arc<dyn crate::stream::MediaStream>> = self
.subscriber_streams
.get(&key)
.map(|entry| Arc::clone(entry.value()));
let target = if let Some(s) = target_opt {
Some(s)
} else {
// (2) Try to allocate a fresh per-subscription stream.
// Adapters that don't carry multi-party responsibility
// (SIP, WebRTC) return NotImplemented; we fall through
// to (3) for them.
let codec = self
.publisher_registry
.get()
.and_then(|reg| reg.entry(sid, &strm_id.to_string()))
.and_then(|entry| entry.codec.clone())
.unwrap_or_else(crate::capability::default_audio_codec);
match adapter
.allocate_subscriber_stream(subscriber_connid.clone(), frame.kind, codec)
.await
{
Ok(stream) => {
self.subscriber_streams
.insert(key.clone(), Arc::clone(&stream));
Some(stream)
}
Err(RvoipError::NotImplemented(_)) => {
// (3) Legacy fallback — pick first MediaStream
// by kind. Single-publisher rooms / non-UCTP
// substrates keep working unchanged.
adapter
.streams(subscriber_connid.clone())
.await
.ok()
.and_then(|streams| {
streams.into_iter().find(|s| s.kind() == frame.kind)
})
}
Err(e) => {
debug!(
error = %e,
?subscriber_connid,
"fanout_frame: allocate_subscriber_stream failed"
);
None
}
}
};
let Some(stream) = target else {
continue;
};
let tx = stream.frames_out();
if tx.send(frame.clone()).await.is_ok() {
delivered += 1;
}
}
delivered
}
/// Process-shared `PublisherRegistry` for the multi-party fanout
/// path. Adapters build an `OrchestratorSubscriptionHandler` from
/// this registry plus the orchestrator itself; the registry is
/// the bridge from "publisher emitted `stream.opened`" (registered
/// from the publishing coordinator) to "subscriber sent
/// `stream.subscribe` with this strm_id" (resolved by the
/// subscriber's coordinator's handler).
pub fn publisher_registry(&self) -> Arc<crate::subscriptions::PublisherRegistry> {
// Lazily ensure the registry exists. We don't pre-allocate it
// in `new()` because Orchestrators that never run multi-party
// routing shouldn't pay for the storage; but we want a single
// shared instance once it's requested.
Arc::clone(self.publisher_registry_inner())
}
fn publisher_registry_inner(&self) -> &Arc<crate::subscriptions::PublisherRegistry> {
self.publisher_registry
.get_or_init(|| Arc::new(crate::subscriptions::PublisherRegistry::new()))
}
/// Publish an event on the in-process broadcast channel and, if a
/// `GlobalEventCoordinator` is configured, on the cross-crate bus too.
fn emit(&self, event: Event) {
if let Some(coordinator) = &self.coordinator {
let cross = event.to_cross_crate();
let coord = Arc::clone(coordinator);
tokio::spawn(async move {
if let Err(err) = coord.publish(Arc::new(cross)).await {
warn!(?err, "rvoip-core cross-crate event publish failed");
}
});
}
let _ = self.events.send(event);
}
fn handle_adapter_event(&self, transport: Transport, event: AdapterEvent) {
match event {
AdapterEvent::InboundConnection { connection } => {
self.track_connection(&connection.id, transport);
self.emit(Event::ConnectionInbound {
connection_id: connection.id.clone(),
at: Utc::now(),
});
}
AdapterEvent::Connected { connection_id } => {
self.emit(Event::ConnectionConnected {
connection_id,
at: Utc::now(),
});
}
AdapterEvent::Authenticated {
connection_id,
identity_id,
participant_id,
assurance,
} => {
self.emit(Event::ConnectionAuthenticated {
connection_id,
identity_id,
participant_id,
assurance,
at: Utc::now(),
});
}
AdapterEvent::Ended {
connection_id,
reason,
} => {
self.forget_connection(&connection_id);
self.emit(Event::ConnectionEnded {
connection_id,
reason,
at: Utc::now(),
});
}
AdapterEvent::Failed {
connection_id,
detail,
} => {
self.forget_connection(&connection_id);
self.emit(Event::ConnectionFailed {
connection_id,
detail,
at: Utc::now(),
});
}
AdapterEvent::Dtmf {
connection_id,
digits,
duration_ms,
} => {
// `Event::DtmfReceived` carries digits + connection_id
// only — duration_ms is dropped at the orchestrator
// boundary (it's transport-detail). Consumers that need
// per-digit timing subscribe to the adapter event
// stream directly. Plan C2.
self.emit(Event::DtmfReceived {
connection_id: connection_id.clone(),
digits: digits.clone(),
at: Utc::now(),
});
// Gap plan §4.3 / v1 punch list — cross-bridge DTMF
// auto-route. When the connection is part of a
// cross-transport bridge, forward the digits to the
// peer leg via the adapter's `send_dtmf`. This is what
// makes UCTP→SIP DTMF work end-to-end without app
// code: a UCTP peer signals digits out-of-band via
// `dtmf.send`, the SIP-side adapter synthesizes RFC
// 4733 packets onto outbound RTP.
//
// `handle_adapter_event` is synchronous; spawn a task
// so the forward doesn't block adapter-event ingest.
if let Some(peer) = self.bridge_peer_of(&connection_id) {
metrics::counter!("uctp_bridge_dtmf_forwarded_total").increment(1);
let peer_for_task = peer.clone();
let digits_for_task = digits.clone();
let adapter = self.adapter_for(&peer);
match adapter {
Ok(adapter) => {
let src = connection_id.clone();
tokio::spawn(async move {
match adapter
.send_dtmf(peer_for_task.clone(), &digits_for_task, duration_ms)
.await
{
Ok(()) => {
debug!(
?src,
?peer_for_task,
digits = %digits_for_task,
"orchestrator: auto-forwarded DTMF across cross-transport bridge"
);
}
Err(e) => {
warn!(
?src,
?peer_for_task,
error = %e,
"orchestrator: cross-bridge DTMF auto-forward failed"
);
}
}
});
}
Err(e) => {
warn!(
?connection_id,
?peer,
error = %e,
"orchestrator: cross-bridge DTMF auto-forward — no adapter for peer transport"
);
}
}
}
}
AdapterEvent::Quality {
connection_id,
snapshot,
} => {
// P9 — feed the per-Session aggregator so
// `Event::SessionEnded.report` carries averaged
// quality at session end.
if let Some(sid) = self.session_of(&connection_id) {
let mut entry = self.session_quality.entry(sid).or_default();
entry.add(&snapshot, None);
}
metrics::gauge!("rvoip_media_jitter_ms").set(snapshot.jitter_ms as f64);
metrics::gauge!("rvoip_media_packet_loss_pct").set(snapshot.packet_loss_pct as f64);
self.emit(Event::MediaQuality {
connection_id,
snapshot,
at: Utc::now(),
});
}
AdapterEvent::StepUpResponse {
connection_id,
method,
credential,
} => {
// P12.6 — re-emit as a public event so the consumer
// can resolve `(method, credential)` to a real
// `Credential` and call `complete_step_up`. The
// orchestrator deliberately doesn't auto-call
// `complete_step_up` because that requires an
// `IdentityProvider`, which is consumer-owned per
// INTERFACE_DESIGN §8.
self.emit(Event::IdentityStepUpResponseReceived {
connection_id,
method,
credential,
at: Utc::now(),
});
}
AdapterEvent::Native { kind, detail } => {
debug!(
?transport,
?kind,
?detail,
"adapter native event (unmapped)"
);
}
}
}
// ------------------------------------------------------------------
// Command surface — dispatched via ConnectionAdapter.
// ------------------------------------------------------------------
pub async fn route_inbound_connection(
&self,
connection_id: ConnectionId,
action: InboundAction,
) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
match action {
// P1.8 — bind the Connection to its target Session before
// accepting, so the first `AdapterEvent::Connected` arrives
// on a Session that already lists this connection. Auto-
// transitions Initiating → Active on first attach.
InboundAction::Accept {
session_id,
participant_id,
} => {
let sess_arc = self
.sessions
.get(&session_id)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::SessionNotFound(session_id.clone()))?;
{
let mut sess = sess_arc.write().expect("session lock poisoned");
if matches!(sess.state, SessionState::Ended | SessionState::Failed) {
return Err(RvoipError::InvalidState(
"route_inbound_connection: target session is ended",
));
}
sess.connections.insert(
connection_id.clone(),
ConnectionRef {
id: connection_id.clone(),
participant_id,
},
);
if sess.state == SessionState::Initiating {
sess.state = SessionState::Active;
}
}
self.sessions_by_connection
.insert(connection_id.clone(), session_id);
adapter.accept(connection_id).await
}
InboundAction::Reject { reason } => adapter.reject(connection_id, reason).await,
// P2 — inbound gateway pattern: accept the inbound leg,
// originate the outbound leg, bridge them. The outbound's
// transport selection still uses the v0 "first adapter"
// heuristic until P6 adds the `transport` field to
// OriginateRequest; if the outbound and inbound share a
// transport (common case: SIP↔SIP gateway), that's fine.
InboundAction::BridgeTo {
session_id,
outbound,
} => {
// 1. Bind inbound to the named Session + accept it.
let sess_arc = self
.sessions
.get(&session_id)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::SessionNotFound(session_id.clone()))?;
{
let mut sess = sess_arc.write().expect("session lock poisoned");
if matches!(sess.state, SessionState::Ended | SessionState::Failed) {
return Err(RvoipError::InvalidState(
"BridgeTo: target session is ended",
));
}
sess.connections.insert(
connection_id.clone(),
ConnectionRef {
id: connection_id.clone(),
// BridgeTo doesn't carry a participant_id;
// use the outbound's invitee as the
// gateway-side identity placeholder.
participant_id: outbound.participant_id.clone(),
},
);
if sess.state == SessionState::Initiating {
sess.state = SessionState::Active;
}
}
self.sessions_by_connection
.insert(connection_id.clone(), session_id.clone());
adapter.accept(connection_id.clone()).await?;
// 2. Originate the outbound.
let out_handle = self.originate_connection(outbound).await?;
let out_id = out_handle.connection.id.clone();
// Bind the outbound to the same Session.
{
let mut sess = sess_arc.write().expect("session lock poisoned");
sess.connections.insert(
out_id.clone(),
ConnectionRef {
id: out_id.clone(),
participant_id: out_handle.connection.participant_id.clone(),
},
);
}
self.sessions_by_connection
.insert(out_id.clone(), session_id);
// 3. Bridge them. Errors here roll up; we leave the
// legs attached to the Session so the caller can
// observe + tear down explicitly.
self.bridge_connections(connection_id, out_id).await?;
Ok(())
}
}
}
#[instrument(skip(self, request), fields(target = %request.target, transport = ?request.transport, connection_id))]
pub async fn originate_connection(
&self,
request: OriginateRequest,
) -> Result<ConnectionHandle> {
// P6 — caller-selected transport takes precedence; fall back
// to the v0 "first registered adapter" path when the request
// doesn't specify (back-compat for single-adapter
// deployments).
let transport = match request.transport {
Some(t) => t,
None => self
.adapters
.iter()
.next()
.map(|entry| *entry.key())
.ok_or(RvoipError::NotImplemented(
"no adapter registered — register one before originating",
))?,
};
let adapter = self.adapter(transport)?;
let handle = adapter.originate(request).await?;
self.track_connection(&handle.connection.id, transport);
self.emit(Event::ConnectionOutbound {
connection_id: handle.connection.id.clone(),
at: Utc::now(),
});
Ok(handle)
}
/// P6 — ergonomic wrapper that sets `request.transport = Some(transport)`
/// before dispatch. Equivalent to mutating the field directly.
pub async fn originate_connection_via(
&self,
transport: Transport,
mut request: OriginateRequest,
) -> Result<ConnectionHandle> {
request.transport = Some(transport);
self.originate_connection(request).await
}
pub async fn end_connection(
&self,
connection_id: ConnectionId,
reason: EndReason,
) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
adapter.end(connection_id, reason).await
}
pub async fn hold(&self, connection_id: ConnectionId) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
adapter.hold(connection_id).await
}
pub async fn resume(&self, connection_id: ConnectionId) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
adapter.resume(connection_id).await
}
#[instrument(skip(self), fields(connection_id = %connection_id, target = ?target))]
pub async fn transfer_connection(
&self,
connection_id: ConnectionId,
target: TransferTarget,
) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
adapter.transfer(connection_id, target).await
}
pub async fn send_dtmf(
&self,
connection_id: ConnectionId,
digits: &str,
duration_ms: u32,
) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
adapter.send_dtmf(connection_id, digits, duration_ms).await
}
/// Legacy name retained for compatibility — alias of
/// [`Self::send_message_to_connection`].
pub async fn send_message(&self, connection_id: ConnectionId, message: Message) -> Result<()> {
self.send_message_to_connection(connection_id, message)
.await
}
/// P4 — send a Message to a single Connection (single-substrate hop).
/// Persists the Message in the configured `MessageStore`, dispatches
/// to the adapter, emits `MessageSent` then `MessageDelivered`.
pub async fn send_message_to_connection(
&self,
connection_id: ConnectionId,
message: Message,
) -> Result<()> {
Self::validate_inline_body(&message)?;
let adapter = self.adapter_for(&connection_id)?;
let msg_id = message.id.clone();
let cid = message.conversation_id.clone();
self.config.message_store.put(message.clone()).await?;
adapter.send_message(connection_id, message).await?;
self.emit(Event::MessageSent {
message_id: msg_id.clone(),
conversation_id: cid,
at: Utc::now(),
});
self.emit(Event::MessageDelivered {
message_id: msg_id,
at: Utc::now(),
});
Ok(())
}
/// P4 — fan-out a Message to every active Connection across every
/// active Session within a Conversation. Persists once; emits
/// `MessageSent` once + `MessageDelivered` per successful per-leg
/// dispatch. Per-leg adapter errors are logged at `warn` and do
/// not abort the fan-out.
pub async fn send_message_to_conversation(
&self,
conversation_id: ConversationId,
message: Message,
) -> Result<MessageId> {
Self::validate_inline_body(&message)?;
let conv_arc = self
.conversations
.get(&conversation_id)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::ConversationNotFound(conversation_id.clone()))?;
let session_ids: Vec<SessionId> = {
let conv = conv_arc.read().expect("conv lock poisoned");
if conv.state != ConversationState::Open {
return Err(RvoipError::InvalidState(
"send_message_to_conversation: conversation not Open",
));
}
conv.sessions.clone()
};
// Collect (connection_id, transport) snapshots for active Sessions.
let mut legs: Vec<ConnectionId> = Vec::new();
for sid in &session_ids {
if let Some(sess_arc) = self.sessions.get(sid).map(|e| Arc::clone(e.value())) {
let sess = sess_arc.read().expect("sess lock poisoned");
if sess.state == SessionState::Active {
for cref in sess.connections.keys() {
legs.push(cref.clone());
}
}
}
}
let msg_id = message.id.clone();
self.config.message_store.put(message.clone()).await?;
self.emit(Event::MessageSent {
message_id: msg_id.clone(),
conversation_id,
at: Utc::now(),
});
for connection_id in legs {
match self.adapter_for(&connection_id) {
Ok(adapter) => {
if let Err(e) = adapter
.send_message(connection_id.clone(), message.clone())
.await
{
warn!(?connection_id, error=%e, "per-leg send_message failed");
continue;
}
self.emit(Event::MessageDelivered {
message_id: msg_id.clone(),
at: Utc::now(),
});
}
Err(e) => warn!(?connection_id, error=%e, "no adapter for leg"),
}
}
Ok(msg_id)
}
/// P4 — paginated history.
pub async fn list_messages(
&self,
conversation_id: ConversationId,
filter: crate::store::MessageFilter,
page: Option<crate::store::PageCursor>,
) -> Result<crate::store::MessagePage> {
self.config
.message_store
.list(&conversation_id, filter, page)
.await
}
/// P4 — record a read receipt + emit `MessageRead`.
pub async fn mark_message_read(
&self,
message_id: crate::ids::MessageId,
by_participant: ParticipantId,
) -> Result<()> {
self.config
.message_store
.mark_read(&message_id, &by_participant)
.await?;
self.emit(Event::MessageRead {
message_id,
at: Utc::now(),
});
Ok(())
}
/// P9 — record a per-tenant usage unit. Emits `UsageRecord` on
/// the bus so downstream billing pipelines can aggregate.
pub fn record_usage(&self, tenant_id: TenantId, kind: crate::events::UsageKind, units: u64) {
self.emit(Event::UsageRecord {
tenant_id,
kind,
units,
at: Utc::now(),
});
}
/// P9 — registrar adapters call this once they observe a
/// registration refresh.
pub fn notify_registration_heartbeat(&self, aor: impl Into<String>) {
self.emit(Event::RegistrationHeartbeat {
aor: aor.into(),
at: Utc::now(),
});
}
/// P9 — registrar adapters call this when registration state
/// changes (registered / expired / unregistered / contact-changed).
pub fn notify_registration_changed(&self, aor: impl Into<String>) {
self.emit(Event::RegistrationChanged {
aor: aor.into(),
at: Utc::now(),
});
}
/// P8 — emit an `ActiveSpeakerChanged` advisory. Called by the
/// UCTP adapter when audio-level extension data shows a new
/// dominant speaker. The Orchestrator just forwards on the bus;
/// there's no routing-side change because the multi-party fanout
/// is publisher-driven (subscribers always receive their
/// subscribed publishers regardless of who's loudest).
pub fn notify_active_speaker(
&self,
session_id: SessionId,
connection_id: ConnectionId,
audio_level_dbov: i8,
) {
self.emit(Event::ActiveSpeakerChanged {
session_id,
connection_id,
audio_level_dbov,
at: Utc::now(),
});
}
// --- P7 step-up auth ------------------------------------------------
/// Request a step-up to a higher IdentityAssurance level on an
/// existing Connection. P12.6 wires the full round-trip:
///
/// 1. Dispatches an `identity.step-up-request` envelope through the
/// Connection's adapter (`ConnectionAdapter::send_step_up_request`).
/// UCTP-family adapters serialize the envelope per
/// CONVERSATION_PROTOCOL.md §5.8; non-UCTP adapters
/// (SIP / WebRTC) return `NotImplemented`.
/// 2. Emits [`Event::IdentityStepUpRequested`] so the consumer
/// sees the request reached the wire.
/// 3. When the peer's `identity.step-up-response` arrives, the
/// adapter forwards it as `AdapterEvent::StepUpResponse`; the
/// orchestrator re-emits it as
/// [`Event::IdentityStepUpResponseReceived`]. The consumer
/// resolves the `(method, credential)` pair to a
/// [`crate::identity::Credential`] and calls
/// [`Self::complete_step_up`] to finalize the assurance change.
pub async fn request_step_up(
&self,
connection_id: ConnectionId,
required: crate::capability::IdentityAssuranceRequirement,
) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
adapter
.send_step_up_request(connection_id.clone(), required.clone(), Vec::new(), None)
.await?;
self.emit(Event::IdentityStepUpRequested {
connection_id,
required,
at: Utc::now(),
});
Ok(())
}
/// P7 — accept a step-up credential and emit
/// `IdentityAssuranceChanged`.
pub async fn complete_step_up(
&self,
connection_id: ConnectionId,
credential: crate::identity::Credential,
provider: Arc<dyn crate::identity::IdentityProvider>,
) -> Result<crate::identity::IdentityAssurance> {
let (identity_id, assurance) = provider.authenticate(credential).await?;
self.emit(Event::IdentityAssuranceChanged {
connection_id,
identity_id: Some(identity_id),
at: Utc::now(),
});
Ok(assurance)
}
// --- P5 provider registration ---------------------------------------
pub fn register_asr_provider(
&self,
name: impl Into<String>,
provider: Arc<dyn crate::harness::AsrProvider>,
) {
self.asr_providers.insert(name.into(), provider);
}
pub fn register_tts_provider(
&self,
name: impl Into<String>,
provider: Arc<dyn crate::harness::TtsProvider>,
) {
self.tts_providers.insert(name.into(), provider);
}
pub fn register_dialog_manager(
&self,
name: impl Into<String>,
manager: Arc<dyn crate::harness::DialogManager>,
) {
self.dialog_managers.insert(name.into(), manager);
}
pub fn register_recording_sink(
&self,
name: impl Into<String>,
sink: Arc<dyn crate::harness::RecordingSink>,
) {
self.recording_sinks.insert(name.into(), sink);
}
// --- P5 recording / transcription -----------------------------------
/// P5 — start recording the audio MediaStream of a Connection (or
/// of every Connection in a Session) into a registered
/// RecordingSink. Returns the `RecordingId` for stop/pause/resume.
pub async fn start_recording(
self: &Arc<Self>,
target: crate::commands::RecordingTarget,
sink_name: impl Into<String>,
) -> Result<crate::ids::RecordingId> {
use crate::commands::RecordingTarget;
let sink_name = sink_name.into();
let sink = self
.recording_sinks
.get(&sink_name)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::AdmissionRejected("recording sink not registered"))?;
// Resolve target → list of Connections to tap.
let (conns, tenant_id) = match target {
RecordingTarget::Connection(c) => {
let tid = self
.session_of(&c)
.and_then(|sid| {
self.sessions.get(&sid).map(|e| {
self.conversations
.get(
&e.value()
.read()
.expect("sess lock poisoned")
.conversation_id,
)
.map(|c| {
c.value()
.read()
.expect("conv lock poisoned")
.tenant_id
.clone()
})
})
})
.flatten();
(vec![c], tid)
}
RecordingTarget::Session(sid) => {
let (cs, tid) = self
.sessions
.get(&sid)
.map(|e| {
let s = e.value().read().expect("sess lock poisoned");
let conns = s.connections.keys().cloned().collect::<Vec<_>>();
let tid = self.conversations.get(&s.conversation_id).map(|c| {
c.value()
.read()
.expect("conv lock poisoned")
.tenant_id
.clone()
});
(conns, tid)
})
.ok_or_else(|| RvoipError::SessionNotFound(sid))?;
(cs, tid)
}
};
if conns.is_empty() {
return Err(RvoipError::AdmissionRejected(
"recording target has no Connections",
));
}
// V2.B — per-tenant Semaphore admission. When the tenant has
// a `max_concurrent_recordings` quota, the semaphore was
// provisioned in `set_tenant_quotas`. `try_acquire_owned`
// returns the permit directly (no shard contention); the
// permit is stored in `RecordingHandle._permit` and released
// by Drop when the handle is removed.
let permit = if let Some(ref tid) = tenant_id {
self.recording_sems
.get(tid)
.map(|s| Arc::clone(s.value()))
.and_then(|sem| match sem.try_acquire_owned() {
Ok(p) => Some(Ok(p)),
Err(_) => Some(Err(RvoipError::AdmissionRejected(
"tenant max_concurrent_recordings exceeded",
))),
})
.transpose()?
} else {
None
};
let rid = crate::ids::RecordingId::new();
let paused = Arc::new(std::sync::atomic::AtomicBool::new(false));
let me = Arc::clone(self);
let sink_for_task = Arc::clone(&sink);
let conns_for_task = conns.clone();
let paused_for_task = Arc::clone(&paused);
let task = tokio::spawn(async move {
for cid in conns_for_task {
if let Ok(adapter) = me.adapter_for(&cid) {
if let Ok(streams) = adapter.streams(cid.clone()).await {
for stream in streams
.into_iter()
.filter(|s| s.kind() == StreamKind::Audio)
{
let sink_clone = Arc::clone(&sink_for_task);
let paused_clone = Arc::clone(&paused_for_task);
tokio::spawn(async move {
let mut rx = stream.frames_in();
while let Some(frame) = rx.recv().await {
if paused_clone.load(std::sync::atomic::Ordering::Relaxed) {
// Drop frame silently while paused.
continue;
}
if sink_clone.write(frame).await.is_err() {
break;
}
}
});
}
}
}
}
futures_alive().await;
});
// V2.B — the permit (if any) is stored in the handle and
// drops alongside it on stop_recording.
let _ = tenant_id;
self.recordings.insert(
rid.clone(),
RecordingHandle {
sink: Arc::clone(&sink),
abort: task.abort_handle(),
paused: Arc::clone(&paused),
_permit: permit,
},
);
self.emit(Event::RecordingStarted {
recording_id: rid.clone(),
at: Utc::now(),
});
Ok(rid)
}
pub async fn stop_recording(
&self,
recording_id: crate::ids::RecordingId,
) -> Result<crate::harness::RecordingArtifact> {
let (_, handle) = self
.recordings
.remove(&recording_id)
.ok_or_else(|| RvoipError::AdmissionRejected("recording not found"))?;
handle.abort.abort();
// V2.B — permit drops with the handle struct, releasing the
// tenant's admission slot.
let artifact = handle.sink.close().await?;
self.emit(Event::RecordingStopped {
recording_id: recording_id.clone(),
at: Utc::now(),
});
self.emit(Event::RecordingComplete {
recording_id,
sink: artifact.url.clone(),
vcon_ref: None,
at: Utc::now(),
});
Ok(artifact)
}
/// P5 — set the pause flag on the recording's pump task. Frames
/// arriving while the flag is set are dropped silently (the sink
/// doesn't see them). `resume_recording` clears the flag.
///
/// Concurrency note: the pause flag is `Relaxed`-ordered and
/// checked per-frame in each per-stream pump task. Frames that are
/// already in the per-stream mpsc buffer at the moment `pause` is
/// called may still be drained and written before subsequent
/// per-frame checks observe the flag — pause means "drop new
/// frames", not "abandon frames already accepted". For strict
/// drain-on-pause semantics, follow `pause_recording` with
/// `stop_recording` (no resume) instead.
pub async fn pause_recording(&self, id: crate::ids::RecordingId) -> Result<()> {
let entry = self
.recordings
.get(&id)
.ok_or_else(|| RvoipError::AdmissionRejected("recording not found"))?;
entry
.value()
.paused
.store(true, std::sync::atomic::Ordering::Relaxed);
Ok(())
}
pub async fn resume_recording(&self, id: crate::ids::RecordingId) -> Result<()> {
let entry = self
.recordings
.get(&id)
.ok_or_else(|| RvoipError::AdmissionRejected("recording not found"))?;
entry
.value()
.paused
.store(false, std::sync::atomic::Ordering::Relaxed);
Ok(())
}
/// P5 — start transcription. Pulls audio frames into the named
/// AsrProvider; emits `TranscriptTurn` for each final result.
pub async fn start_transcription(
self: &Arc<Self>,
target: crate::commands::RecordingTarget,
provider_ref: impl Into<String>,
) -> Result<crate::ids::TranscriptionId> {
use crate::commands::RecordingTarget;
let provider_name = provider_ref.into();
let provider = self
.asr_providers
.get(&provider_name)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| RvoipError::AdmissionRejected("ASR provider not registered"))?;
let conn = match target {
RecordingTarget::Connection(c) => c,
RecordingTarget::Session(sid) => self
.sessions
.get(&sid)
.and_then(|e| {
e.value()
.read()
.expect("sess lock poisoned")
.connections
.keys()
.next()
.cloned()
})
.ok_or_else(|| RvoipError::SessionNotFound(sid))?,
};
let tid = crate::ids::TranscriptionId::new();
let me = Arc::clone(self);
let task = tokio::spawn(async move {
let stream = match provider
.open_stream(conn.clone(), crate::harness::AsrConfig::default())
.await
{
Ok(s) => s,
Err(_) => return,
};
// Producer: frames → stream.push.
let stream_arc: Arc<dyn crate::harness::AsrStream> = Arc::from(stream);
let stream_for_push = Arc::clone(&stream_arc);
let conn_for_push = conn.clone();
let push_task = {
let me = Arc::clone(&me);
tokio::spawn(async move {
if let Ok(adapter) = me.adapter_for(&conn_for_push) {
if let Ok(streams) = adapter.streams(conn_for_push).await {
for s in streams
.into_iter()
.filter(|s| s.kind() == StreamKind::Audio)
{
let stream_clone = Arc::clone(&stream_for_push);
tokio::spawn(async move {
let mut rx = s.frames_in();
while let Some(f) = rx.recv().await {
if stream_clone.push(f).await.is_err() {
break;
}
}
});
}
}
}
})
};
// Consumer: stream.next → TranscriptTurn event.
while let Some(result) = stream_arc.next().await {
me.emit(Event::TranscriptTurn {
stream_id: result.stream_id,
speaker: result.speaker,
text: result.text,
confidence: result.confidence,
is_final: result.is_final,
assigned_provider: Some(provider_name.clone()),
at: Utc::now(),
});
}
let _ = push_task;
});
self.transcriptions.insert(
tid.clone(),
TranscriptionHandle {
abort: task.abort_handle(),
},
);
Ok(tid)
}
pub async fn stop_transcription(&self, id: crate::ids::TranscriptionId) -> Result<()> {
if let Some((_, h)) = self.transcriptions.remove(&id) {
h.abort.abort();
Ok(())
} else {
Err(RvoipError::AdmissionRejected("transcription not found"))
}
}
// --- P5 AI harness --------------------------------------------------
/// P5 — attach an AI runtime to a Connection. Uses registered
/// AsrProvider + DialogManager + TtsProvider names looked up from
/// `config`. Returns the AiAttachmentId for detach.
///
/// `config["asr"]` / `config["tts"]` / `config["dialog"]` keys
/// must point to registered providers.
///
/// P5 barge-in: when ASR yields a partial / final result while a
/// TTS playback is in flight, the orchestrator cancels the
/// playback and emits `Event::BargeInDetected` before continuing
/// the dialog loop.
#[instrument(skip(self, provider_ref, config), fields(connection_id = %connection_id))]
pub async fn attach_ai(
self: &Arc<Self>,
connection_id: ConnectionId,
provider_ref: impl Into<String>,
config: std::collections::HashMap<String, String>,
) -> Result<crate::ids::AiAttachmentId> {
// P6 — tenant attribution + AI quota enforcement.
let tenant_id = self
.session_of(&connection_id)
.and_then(|sid| {
self.sessions.get(&sid).map(|e| {
self.conversations
.get(
&e.value()
.read()
.expect("sess lock poisoned")
.conversation_id,
)
.map(|c| {
c.value()
.read()
.expect("conv lock poisoned")
.tenant_id
.clone()
})
})
})
.flatten();
// V2.B — per-tenant Semaphore admission. Permit stored in the
// AiAttachmentHandle and released by Drop on detach.
let ai_permit = if let Some(ref tid) = tenant_id {
self.ai_sems
.get(tid)
.map(|s| Arc::clone(s.value()))
.and_then(|sem| match sem.try_acquire_owned() {
Ok(p) => Some(Ok(p)),
Err(_) => Some(Err(RvoipError::AdmissionRejected(
"tenant max_concurrent_ai_sessions exceeded",
))),
})
.transpose()?
} else {
None
};
let provider_ref = provider_ref.into();
let asr_name = config
.get("asr")
.cloned()
.unwrap_or_else(|| provider_ref.clone());
let tts_name = config
.get("tts")
.cloned()
.unwrap_or_else(|| provider_ref.clone());
let dialog_name = config
.get("dialog")
.cloned()
.unwrap_or_else(|| provider_ref.clone());
let asr = self
.asr_providers
.get(&asr_name)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| {
RvoipError::AdmissionRejected("attach_ai: ASR provider not registered")
})?;
let tts = self
.tts_providers
.get(&tts_name)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| {
RvoipError::AdmissionRejected("attach_ai: TTS provider not registered")
})?;
let dialog = self
.dialog_managers
.get(&dialog_name)
.map(|e| Arc::clone(e.value()))
.ok_or_else(|| {
RvoipError::AdmissionRejected("attach_ai: DialogManager not registered")
})?;
let aid = crate::ids::AiAttachmentId::new();
let speaking = Arc::new(std::sync::atomic::AtomicBool::new(false));
let speak_cancel: Arc<tokio::sync::Mutex<Option<tokio::sync::oneshot::Sender<()>>>> =
Arc::new(tokio::sync::Mutex::new(None));
let me = Arc::clone(self);
let connection_id_for_task = connection_id.clone();
let aid_for_task = aid.clone();
let speaking_for_task = Arc::clone(&speaking);
let speak_cancel_for_task = Arc::clone(&speak_cancel);
let task = tokio::spawn(async move {
let connection_id = connection_id_for_task;
let stream: Arc<dyn crate::harness::AsrStream> = match asr
.open_stream(connection_id.clone(), crate::harness::AsrConfig::default())
.await
{
Ok(s) => Arc::from(s),
Err(_) => return,
};
// Push loop.
let conn_for_push = connection_id.clone();
let stream_for_push = Arc::clone(&stream);
let me_for_push = Arc::clone(&me);
let _push = tokio::spawn(async move {
if let Ok(adapter) = me_for_push.adapter_for(&conn_for_push) {
if let Ok(streams) = adapter.streams(conn_for_push).await {
for s in streams
.into_iter()
.filter(|s| s.kind() == StreamKind::Audio)
{
let sc = Arc::clone(&stream_for_push);
tokio::spawn(async move {
let mut rx = s.frames_in();
while let Some(f) = rx.recv().await {
if sc.push(f).await.is_err() {
break;
}
}
});
}
}
}
});
// Dialog loop with barge-in.
while let Some(asr_result) = stream.next().await {
// P5 barge-in: if user speech detected while we're
// speaking, cancel current playback + fire event.
if speaking_for_task.load(std::sync::atomic::Ordering::Relaxed) {
if let Some(tx) = speak_cancel_for_task.lock().await.take() {
let _ = tx.send(());
}
speaking_for_task.store(false, std::sync::atomic::Ordering::Relaxed);
me.emit(Event::BargeInDetected {
connection_id: connection_id.clone(),
ai_attachment_id: aid_for_task.clone(),
at: Utc::now(),
});
}
if !asr_result.is_final {
continue;
}
let action = match dialog.turn(&asr_result).await {
Ok(a) => a,
Err(_) => break,
};
match action {
crate::harness::DialogAction::Listen => continue,
crate::harness::DialogAction::End => break,
crate::harness::DialogAction::Say { text, voice } => {
let playback = match tts
.synthesize(crate::harness::TtsRequest {
voice,
text,
sample_rate_hz: None,
})
.await
{
Ok(p) => p,
Err(_) => continue,
};
let (cancel_tx, mut cancel_rx) = tokio::sync::oneshot::channel::<()>();
*speak_cancel_for_task.lock().await = Some(cancel_tx);
speaking_for_task.store(true, std::sync::atomic::Ordering::Relaxed);
if let Ok(adapter) = me.adapter_for(&connection_id) {
if let Ok(streams) = adapter.streams(connection_id.clone()).await {
let out =
streams.into_iter().find(|s| s.kind() == StreamKind::Audio);
if let Some(audio) = out {
let tx = audio.frames_out();
loop {
tokio::select! {
_ = &mut cancel_rx => {
let _ = playback.cancel().await;
break;
}
frame_opt = playback.next_frame() => {
let Some(frame) = frame_opt else {
break;
};
let _ = tx.send(frame).await;
}
}
}
}
}
}
speaking_for_task.store(false, std::sync::atomic::Ordering::Relaxed);
// Drain any stale cancel sender (defensive).
let _ = speak_cancel_for_task.lock().await.take();
}
}
}
});
// V2.B — permit (if any) stored in the handle; releases on
// Drop when detach removes the entry.
let _ = tenant_id;
self.ai_attachments.insert(
aid.clone(),
AiAttachmentHandle {
abort: task.abort_handle(),
speaking,
speak_cancel,
_permit: ai_permit,
},
);
self.emit(Event::AiAttached {
connection_id,
attachment_id: aid.clone(),
provider_ref,
at: Utc::now(),
});
Ok(aid)
}
/// P5 — attach a listener tap. Spawns a per-Connection task that
/// forwards inbound audio frames to the chosen sink. Separated-
/// streams default: each Connection's audio lands as its own
/// stream into the sink (no mixing). The `ListenerSink::Channel`
/// variant is consumed via [`Self::listener_channel`] which
/// returns the receive end the consumer can pull from.
pub fn attach_listener(
self: &Arc<Self>,
target: crate::commands::ListenerTarget,
sink: crate::commands::ListenerSink,
) -> Result<crate::ids::ListenerId> {
use crate::commands::{ListenerSink, ListenerTarget};
let conns: Vec<ConnectionId> = match target {
ListenerTarget::Connection(c) => vec![c],
ListenerTarget::Session(sid) => self
.sessions
.get(&sid)
.map(|e| {
e.value()
.read()
.expect("sess lock poisoned")
.connections
.keys()
.cloned()
.collect()
})
.ok_or_else(|| RvoipError::SessionNotFound(sid))?,
};
if conns.is_empty() {
return Err(RvoipError::AdmissionRejected(
"listener target has no Connections",
));
}
let lid = crate::ids::ListenerId::new();
let me = Arc::clone(self);
// Build the per-sink frame consumer. Channel sinks expose a
// receiver via `listener_channels`; File/Url sinks just log
// the byte count (full file/HTTP implementations live in
// consumer crates).
let sink_kind = match sink {
ListenerSink::Channel => "channel",
ListenerSink::File { .. } => "file",
ListenerSink::Url(_) => "url",
};
let (tx_for_channel, rx_for_channel) = match sink {
ListenerSink::Channel => {
let (t, r) = tokio::sync::mpsc::channel::<crate::stream::MediaFrame>(256);
(Some(t), Some(r))
}
_ => (None, None),
};
if let Some(rx) = rx_for_channel {
self.listener_channels
.insert(lid.clone(), Mutex::new(Some(rx)));
}
let lid_for_task = lid.clone();
let task = tokio::spawn(async move {
for cid in conns {
let Ok(adapter) = me.adapter_for(&cid) else {
continue;
};
let streams = match adapter.streams(cid.clone()).await {
Ok(s) => s,
Err(_) => continue,
};
for s in streams
.into_iter()
.filter(|s| s.kind() == StreamKind::Audio)
{
let tx_clone = tx_for_channel.clone();
let lid_clone = lid_for_task.clone();
tokio::spawn(async move {
let mut rx = s.frames_in();
while let Some(frame) = rx.recv().await {
if let Some(tx) = &tx_clone {
if tx.send(frame).await.is_err() {
break;
}
} else {
// File/URL — drop after counting.
let _ = (frame, &lid_clone);
}
}
});
}
}
let _ = sink_kind;
// Hold the parent task alive so its abort_handle remains
// meaningful for the lifetime of the listener.
futures_alive().await;
});
// Bug-fix sweep — register the abort handle so `detach` can
// tear down the listener cleanly (was leaking before).
self.listener_tasks.insert(lid.clone(), task.abort_handle());
self.emit(Event::ListenerAttached {
listener_id: lid.clone(),
at: Utc::now(),
});
Ok(lid)
}
/// P5 — take the receiver for a `Channel` listener.
/// Single-take per listener; subsequent calls return `None`.
pub fn listener_channel(
&self,
id: &crate::ids::ListenerId,
) -> Option<tokio::sync::mpsc::Receiver<crate::stream::MediaFrame>> {
self.listener_channels
.get(id)
.and_then(|e| e.value().lock().expect("listener lock poisoned").take())
}
pub async fn detach(&self, attachment: crate::commands::AttachmentRef) -> Result<()> {
use crate::commands::AttachmentRef;
match attachment {
AttachmentRef::Ai(id) => {
if let Some((_, h)) = self.ai_attachments.remove(&id) {
h.abort.abort();
// V2.B — permit drops with the handle struct.
self.emit(Event::AiDetached {
attachment_id: id,
at: Utc::now(),
});
Ok(())
} else {
Err(RvoipError::AdmissionRejected("ai attachment not found"))
}
}
AttachmentRef::Listener(id) => {
if let Some((_, abort)) = self.listener_tasks.remove(&id) {
abort.abort();
}
// Drop any cached channel receiver so a re-attach with
// the same ID (unlikely, but defensive) starts clean.
self.listener_channels.remove(&id);
self.emit(Event::ListenerDetached {
listener_id: id,
at: Utc::now(),
});
Ok(())
}
AttachmentRef::Recording(id) => self.stop_recording(id).await.map(|_| ()),
}
}
/// P4 — enforce inline body cap. >64KB must use attachments[].
fn validate_inline_body(message: &Message) -> Result<()> {
const MAX_INLINE_BODY: usize = 64 * 1024;
if message.body.len() > MAX_INLINE_BODY && message.attachments.is_empty() {
return Err(RvoipError::AdmissionRejected(
"message body exceeds 64KB inline cap; use attachments[] with an OOB URL",
));
}
Ok(())
}
pub async fn renegotiate_media(
&self,
connection_id: ConnectionId,
capabilities: CapabilityDescriptor,
) -> Result<crate::capability::NegotiatedCodecs> {
let adapter = self.adapter_for(&connection_id)?;
let negotiated = adapter
.renegotiate_media(connection_id.clone(), capabilities)
.await?;
// Gap plan §4.2 v1 punch list — if the connection is in a
// cross-transport bridge, hot-swap its transcoders so the
// pump's `from_pt`/`to_pt` reflect the post-renegotiation
// codec on this leg. The other leg's codec is unchanged
// (renegotiate_media is per-connection); the swap only
// touches the direction whose PT actually moved.
if let Some(peer) = self.bridge_peer_of(&connection_id) {
if let Some(audio) = negotiated.audio.as_ref() {
if let Some(new_pt) = codec_to_pt(&audio.name) {
// A2 — snapshot the bridge handle's relevant state
// (orientation + swap channel availability) WITHOUT
// holding the DashMap iterator guard across any
// .await. Extract bridge_id first, then re-fetch
// by id inside a tight non-async scope.
let bridge_id_opt: Option<BridgeId> = {
self.cross_bridges
.iter()
.find(|e| e.value().a == connection_id || e.value().b == connection_id)
.map(|e| e.key().clone())
};
if let Some(bridge_id) = bridge_id_opt {
// Snapshot orientation (no .await held).
let orientation_this_is_a = self
.cross_bridges
.get(&bridge_id)
.map(|e| e.value().a == connection_id);
let Some(orientation_this_is_a) = orientation_this_is_a else {
return Ok(negotiated);
};
// A2 — direct .await for the peer's stream
// lookup (was `block_in_place + block_on`).
let peer_pt = if let Ok(adp) = self.adapter_for(&peer) {
adp.streams(peer.clone())
.await
.ok()
.and_then(|streams| {
streams
.into_iter()
.find(|s| s.kind() == StreamKind::Audio)
.map(|s| s.codec().name)
})
.and_then(|n| codec_to_pt(&n))
.unwrap_or(new_pt)
} else {
new_pt
};
// Build per-direction swap messages.
let (a_swap, b_swap) = if orientation_this_is_a {
// a is "this" connection (new_pt), b is peer (peer_pt).
(make_swap(new_pt, peer_pt), make_swap(peer_pt, new_pt))
} else {
(make_swap(peer_pt, new_pt), make_swap(new_pt, peer_pt))
};
// Re-fetch the bridge entry just to call
// swap_transcoders. The entry guard is held
// only across this single .await — swap_transcoders
// itself sends on the swap channels and (with
// A3) awaits the pumps' acks. The guard never
// covers a media-path operation.
let swap_result = {
if let Some(entry) = self.cross_bridges.get(&bridge_id) {
let bridge = entry.value();
bridge.swap_transcoders(a_swap, b_swap).await
} else {
Ok(())
}
};
if let Err(e) = swap_result {
warn!(
?connection_id,
error = %e,
"orchestrator: bridge transcoder hot-swap failed; bridge may carry stale codecs"
);
} else {
metrics::counter!(
"uctp_renegotiations_completed_total",
"outcome" => "hot-swapped",
)
.increment(1);
}
}
}
}
}
Ok(negotiated)
}
/// P2 — mute one direction (Send / Receive / Both) on a Connection.
/// Dispatches through the registered adapter; adapters that don't
/// implement mute return `RvoipError::NotImplemented`.
pub async fn mute(&self, connection_id: ConnectionId, direction: MuteDirection) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
adapter.mute(connection_id, direction).await
}
pub async fn unmute(
&self,
connection_id: ConnectionId,
direction: MuteDirection,
) -> Result<()> {
let adapter = self.adapter_for(&connection_id)?;
adapter.unmute(connection_id, direction).await
}
/// P2 — start playback of `source` toward the peer on
/// `connection_id`. The returned [`PlaybackHandle`] cancels
/// playback on `.cancel()`.
pub async fn play_audio(
&self,
connection_id: ConnectionId,
source: AudioSource,
) -> Result<PlaybackHandle> {
let adapter = self.adapter_for(&connection_id)?;
adapter.play_audio(connection_id, source).await
}
/// Bridge two connections — wires a bidirectional frame pump between
/// their audio streams, inserting a transcoder when the negotiated
/// codecs differ. Per INTERFACE_DESIGN.md §10.2.
///
/// Adapters populate audio streams lazily (typically on
/// `connection.ready`), so a caller that calls
/// `bridge_connections` immediately from `Event::ConnectionInbound`
/// may race the stream registration. This method polls for both
/// streams up to [`Config::bridge_stream_deadline`] before failing
/// with `AdmissionRejected("no audio stream")`. Set the deadline to
/// zero in `Config` for strict no-wait behavior.
///
/// Errors:
/// - `AdmissionRejected` if `a == b` or either is already bridged.
/// - `ConnectionNotFound` if either connection is unknown.
/// - `NoAdapterForTransport` if either connection's transport has no adapter.
/// - `AdmissionRejected("no audio stream")` if either side still has no
/// audio `MediaStream` after the deadline.
/// - `UnsupportedCodec(name)` if a negotiated codec has no PT mapping.
#[instrument(skip(self), fields(a = %a, b = %b, bridge_id))]
pub async fn bridge_connections(&self, a: ConnectionId, b: ConnectionId) -> Result<BridgeId> {
if a == b {
return Err(RvoipError::AdmissionRejected(
"cannot bridge a connection to itself",
));
}
// Reject if either ConnectionId is already in a cross-transport bridge.
for entry in self.cross_bridges.iter() {
let h = entry.value();
if h.a == a || h.b == a || h.a == b || h.b == b {
return Err(RvoipError::AdmissionRejected("connection already bridged"));
}
}
let a_transport = self
.connections
.get(&a)
.ok_or_else(|| RvoipError::ConnectionNotFound(a.clone()))?
.transport;
let b_transport = self
.connections
.get(&b)
.ok_or_else(|| RvoipError::ConnectionNotFound(b.clone()))?
.transport;
let a_adapter = self.adapter(a_transport)?;
let b_adapter = self.adapter(b_transport)?;
// Poll both adapters for an audio stream up to the configured
// deadline. Adapters create streams on connection.ready, so a
// bridge requested from Event::ConnectionInbound usually has to
// wait a handful of ms. 50ms polling interval is small enough
// to be inaudible at the call setup level and large enough not
// to spin.
let deadline = self.config.bridge_stream_deadline;
let poll_interval = std::time::Duration::from_millis(50);
let start = std::time::Instant::now();
let (a_audio, b_audio) = loop {
let a_streams = a_adapter.streams(a.clone()).await?;
let b_streams = b_adapter.streams(b.clone()).await?;
let a_audio = a_streams
.into_iter()
.find(|s| s.kind() == StreamKind::Audio);
let b_audio = b_streams
.into_iter()
.find(|s| s.kind() == StreamKind::Audio);
match (a_audio, b_audio) {
(Some(a_s), Some(b_s)) => break (a_s, b_s),
_ if start.elapsed() >= deadline => {
return Err(RvoipError::AdmissionRejected(
"no audio stream on one or both connections within deadline",
));
}
_ => {
tokio::time::sleep(poll_interval).await;
}
}
};
let a_pt = codec_to_pt(&a_audio.codec().name)
.ok_or_else(|| RvoipError::UnsupportedCodec(a_audio.codec().name.clone()))?;
let b_pt = codec_to_pt(&b_audio.codec().name)
.ok_or_else(|| RvoipError::UnsupportedCodec(b_audio.codec().name.clone()))?;
// One transcoder per direction with its own FormatConverter.
//
// FormatConverter caches a Resampler keyed by the *input* sample
// rate, so sharing across directions would thrash the cache (and
// could cross-contaminate state) on every flip — e.g. G.711-mu
// (8 kHz) <-> Opus (48 kHz) would tear down and rebuild the
// resampler on every frame. Per-direction also removes the
// RwLock contention point under bidirectional traffic.
let (transcoder_a_to_b, transcoder_b_to_a) = if a_pt != b_pt {
(
Some(Transcoder::new(Arc::new(TokioRwLock::new(
FormatConverter::new(),
)))),
Some(Transcoder::new(Arc::new(TokioRwLock::new(
FormatConverter::new(),
)))),
)
} else {
(None, None)
};
// Single-take channels per MediaStream contract.
let a_in = a_audio.frames_in();
let a_out = a_audio.frames_out();
let b_in = b_audio.frames_in();
let b_out = b_audio.frames_out();
// Gap plan §4.2 v1 punch list — wire each pump with a swap
// channel so `Orchestrator::renegotiate_media` can hot-swap
// the transcoders after a successful codec renegotiation.
let (swap_a_to_b_tx, swap_a_to_b_rx) =
tokio::sync::mpsc::channel::<frame_pump::TranscoderSwap>(4);
let (swap_b_to_a_tx, swap_b_to_a_rx) =
tokio::sync::mpsc::channel::<frame_pump::TranscoderSwap>(4);
let a_to_b = frame_pump::spawn_pump_with_swap(
"a->b",
a_in,
b_out,
transcoder_a_to_b,
a_pt,
b_pt,
swap_a_to_b_rx,
);
let b_to_a = frame_pump::spawn_pump_with_swap(
"b->a",
b_in,
a_out,
transcoder_b_to_a,
b_pt,
a_pt,
swap_b_to_a_rx,
);
let id = BridgeId::new();
self.cross_bridges.insert(
id.clone(),
CrossBridgeHandle::with_swap_channels(
id.clone(),
a.clone(),
b.clone(),
a_to_b.abort_handle(),
b_to_a.abort_handle(),
swap_a_to_b_tx,
swap_b_to_a_tx,
),
);
self.emit(Event::ConnectionsBridged {
bridge_id: id.clone(),
a,
b,
at: Utc::now(),
});
Ok(id)
}
pub async fn unbridge_connections(&self, bridge_id: BridgeId) -> Result<()> {
// Cross-transport bridges first (new path). Drop aborts both pumps.
if let Some((_, _handle)) = self.cross_bridges.remove(&bridge_id) {
self.emit(Event::ConnectionsUnbridged {
bridge_id,
at: Utc::now(),
});
return Ok(());
}
// SIP-fast-path BridgeManager.
match self.bridges.remove(&bridge_id) {
Some(_handle) => {
// Drop tears down the bridge synchronously.
self.emit(Event::ConnectionsUnbridged {
bridge_id,
at: Utc::now(),
});
Ok(())
}
None => Err(RvoipError::BridgeNotFound(bridge_id)),
}
}
}
// Allow forwarding the `RejectReason` argument from older call sites that
// already had it imported. Re-exported for consumer convenience.
pub use crate::adapter::RejectReason as InboundRejectReason;
/// P6 — tenant-id lookup keyed on the freshly-inserted Conversation.
/// Cheap: one DashMap get + one RwLock read.
fn tenant_id_for_index(
conversations: &Arc<DashMap<ConversationId, Arc<RwLock<Conversation>>>>,
id: &ConversationId,
) -> TenantId {
conversations
.get(id)
.map(|e| {
e.value()
.read()
.expect("conv lock poisoned")
.tenant_id
.clone()
})
.unwrap_or_default()
}
/// Helper that blocks until the holding task is aborted. Used by
/// `start_recording` to keep the per-connection spawn task alive so
/// its abort handle remains meaningful.
async fn futures_alive() {
std::future::pending::<()>().await;
}
/// Gap plan §4.2 v1 punch list — construct a [`TranscoderSwap`] for
/// one direction of a hot-swap. Builds a fresh `Transcoder` (with a
/// new per-direction `FormatConverter`) when `from_pt != to_pt`;
/// otherwise leaves the transcoder slot empty (passthrough).
fn make_swap(from_pt: u8, to_pt: u8) -> frame_pump::TranscoderSwap {
let transcoder = if from_pt != to_pt {
Some(Transcoder::new(Arc::new(TokioRwLock::new(
FormatConverter::new(),
))))
} else {
None
};
frame_pump::TranscoderSwap {
new_transcoder: transcoder,
new_from_pt: from_pt,
new_to_pt: to_pt,
// A3 — ack is wired by `swap_transcoders` itself when it
// needs synchronization. `make_swap` leaves it None so the
// caller decides.
ack: None,
}
}