summaryrefslogtreecommitdiff
path: root/main/bridge.c
blob: 1109c4b7653047f59fb1946af2981a0344d55225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2007 - 2009, Digium, Inc.
 *
 * Joshua Colp <jcolp@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Bridging API
 *
 * \author Joshua Colp <jcolp@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

/*** DOCUMENTATION
	<manager name="BridgeTechnologyList" language="en_US">
		<synopsis>
			List available bridging technologies and their statuses.
		</synopsis>
		<syntax>
			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
		</syntax>
		<description>
			<para>Returns detailed information about the available bridging technologies.</para>
		</description>
		<see-also>
			<ref type="manager">BridgeTechnologySuspend</ref>
			<ref type="manager">BridgeTechnologyUnsuspend</ref>
		</see-also>
	</manager>
	<manager name="BridgeTechnologySuspend" language="en_US">
		<synopsis>
			Suspend a bridging technology.
		</synopsis>
		<syntax>
			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
			<parameter name="BridgeTechnology" required="true">
				<para>The name of the bridging technology to suspend.</para>
			</parameter>
		</syntax>
		<description>
			<para>Marks a bridging technology as suspended, which prevents subsequently created bridges from using it.</para>
		</description>
		<see-also>
			<ref type="manager">BridgeTechnologySuspend</ref>
			<ref type="manager">BridgeTechnologyUnsuspend</ref>
		</see-also>
	</manager>
	<manager name="BridgeTechnologyUnsuspend" language="en_US">
		<synopsis>
			Unsuspend a bridging technology.
		</synopsis>
		<syntax>
			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
			<parameter name="BridgeTechnology" required="true">
				<para>The name of the bridging technology to unsuspend.</para>
			</parameter>
		</syntax>
		<description>
			<para>Clears a previously suspended bridging technology, which allows subsequently created bridges to use it.</para>
		</description>
		<see-also>
			<ref type="manager">BridgeTechnologyList</ref>
			<ref type="manager">BridgeTechnologySuspend</ref>
		</see-also>
	</manager>
***/

#include "asterisk.h"

#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
#include "asterisk/lock.h"
#include "asterisk/linkedlists.h"
#include "asterisk/bridge.h"
#include "asterisk/bridge_internal.h"
#include "asterisk/bridge_channel_internal.h"
#include "asterisk/bridge_features.h"
#include "asterisk/bridge_basic.h"
#include "asterisk/bridge_technology.h"
#include "asterisk/bridge_channel.h"
#include "asterisk/bridge_after.h"
#include "asterisk/stasis_bridges.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/stasis_cache_pattern.h"
#include "asterisk/app.h"
#include "asterisk/file.h"
#include "asterisk/module.h"
#include "asterisk/astobj2.h"
#include "asterisk/pbx.h"
#include "asterisk/test.h"
#include "asterisk/_private.h"
#include "asterisk/heap.h"
#include "asterisk/say.h"
#include "asterisk/timing.h"
#include "asterisk/stringfields.h"
#include "asterisk/musiconhold.h"
#include "asterisk/features.h"
#include "asterisk/cli.h"
#include "asterisk/parking.h"
#include "asterisk/core_local.h"
#include "asterisk/core_unreal.h"
#include "asterisk/causes.h"

/*! All bridges container. */
static struct ao2_container *bridges;

static AST_RWLIST_HEAD_STATIC(bridge_technologies, ast_bridge_technology);

static unsigned int optimization_id;

/* Initial starting point for the bridge array of channels */
#define BRIDGE_ARRAY_START 128

/* Grow rate of bridge array of channels */
#define BRIDGE_ARRAY_GROW 32

/* Variable name - stores peer information about the most recent blind transfer */
#define BLINDTRANSFER "BLINDTRANSFER"

/* Variable name - stores peer information about the most recent attended transfer */
#define ATTENDEDTRANSFER "ATTENDEDTRANSFER"

static void cleanup_video_mode(struct ast_bridge *bridge);

/*! Default DTMF keys for built in features */
static char builtin_features_dtmf[AST_BRIDGE_BUILTIN_END][MAXIMUM_DTMF_FEATURE_STRING];

/*! Function handlers for the built in features */
static ast_bridge_hook_callback builtin_features_handlers[AST_BRIDGE_BUILTIN_END];

/*! Function handlers for built in interval features */
static ast_bridge_builtin_set_limits_fn builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_END];

/*! Bridge manager service request */
struct bridge_manager_request {
	/*! List of bridge service requests. */
	AST_LIST_ENTRY(bridge_manager_request) node;
	/*! Refed bridge requesting service. */
	struct ast_bridge *bridge;
};

struct bridge_manager_controller {
	/*! Condition, used to wake up the bridge manager thread. */
	ast_cond_t cond;
	/*! Queue of bridge service requests. */
	AST_LIST_HEAD_NOLOCK(, bridge_manager_request) service_requests;
	/*! Manager thread */
	pthread_t thread;
	/*! TRUE if the manager needs to stop. */
	unsigned int stop:1;
};

/*! Bridge manager controller. */
static struct bridge_manager_controller *bridge_manager;

/*!
 * \internal
 * \brief Request service for a bridge from the bridge manager.
 * \since 12.0.0
 *
 * \param bridge Requesting service.
 *
 * \return Nothing
 */
static void bridge_manager_service_req(struct ast_bridge *bridge)
{
	struct bridge_manager_request *request;

	ao2_lock(bridge_manager);
	if (bridge_manager->stop) {
		ao2_unlock(bridge_manager);
		return;
	}

	/* Create the service request. */
	request = ast_calloc(1, sizeof(*request));
	if (!request) {
		/* Well. This isn't good. */
		ao2_unlock(bridge_manager);
		return;
	}
	ao2_ref(bridge, +1);
	request->bridge = bridge;

	/* Put request into the queue and wake the bridge manager. */
	AST_LIST_INSERT_TAIL(&bridge_manager->service_requests, request, node);
	ast_cond_signal(&bridge_manager->cond);
	ao2_unlock(bridge_manager);
}

int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *module)
{
	struct ast_bridge_technology *current;

	/* Perform a sanity check to make sure the bridge technology conforms to our needed requirements */
	if (ast_strlen_zero(technology->name)
		|| !technology->capabilities
		|| !technology->write) {
		ast_log(LOG_WARNING, "Bridge technology %s failed registration sanity check.\n",
			technology->name);
		return -1;
	}

	AST_RWLIST_WRLOCK(&bridge_technologies);

	/* Look for duplicate bridge technology already using this name, or already registered */
	AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
		if ((!strcasecmp(current->name, technology->name)) || (current == technology)) {
			ast_log(LOG_WARNING, "A bridge technology of %s already claims to exist in our world.\n",
				technology->name);
			AST_RWLIST_UNLOCK(&bridge_technologies);
			return -1;
		}
	}

	/* Copy module pointer so reference counting can keep the module from unloading */
	technology->mod = module;

	/* Find the correct position to insert the technology. */
	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, entry) {
		/* Put the highest preference tech's first in the list. */
		if (technology->preference >= current->preference) {
			AST_RWLIST_INSERT_BEFORE_CURRENT(technology, entry);

			break;
		}
	}
	AST_RWLIST_TRAVERSE_SAFE_END;

	if (!current) {
		/* Insert our new bridge technology to the end of the list. */
		AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, entry);
	}

	AST_RWLIST_UNLOCK(&bridge_technologies);

	ast_verb(2, "Registered bridge technology %s\n", technology->name);

	return 0;
}

int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
{
	struct ast_bridge_technology *current;

	AST_RWLIST_WRLOCK(&bridge_technologies);

	/* Ensure the bridge technology is registered before removing it */
	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, entry) {
		if (current == technology) {
			AST_RWLIST_REMOVE_CURRENT(entry);
			ast_verb(2, "Unregistered bridge technology %s\n", technology->name);
			break;
		}
	}
	AST_RWLIST_TRAVERSE_SAFE_END;

	AST_RWLIST_UNLOCK(&bridge_technologies);

	return current ? 0 : -1;
}

/*!
 * \internal
 * \brief Put an action onto the specified bridge. Don't dup the action frame.
 * \since 12.0.0
 *
 * \param bridge What to queue the action on.
 * \param action What to do.
 *
 * \return Nothing
 */
static void bridge_queue_action_nodup(struct ast_bridge *bridge, struct ast_frame *action)
{
	ast_debug(1, "Bridge %s: queueing action type:%u sub:%d\n",
		bridge->uniqueid, action->frametype, action->subclass.integer);

	ast_bridge_lock(bridge);
	AST_LIST_INSERT_TAIL(&bridge->action_queue, action, frame_list);
	ast_bridge_unlock(bridge);
	bridge_manager_service_req(bridge);
}

int ast_bridge_queue_action(struct ast_bridge *bridge, struct ast_frame *action)
{
	struct ast_frame *dup;

	dup = ast_frdup(action);
	if (!dup) {
		return -1;
	}
	bridge_queue_action_nodup(bridge, dup);
	return 0;
}

void bridge_dissolve(struct ast_bridge *bridge, int cause)
{
	struct ast_bridge_channel *bridge_channel;
	struct ast_frame action = {
		.frametype = AST_FRAME_BRIDGE_ACTION,
		.subclass.integer = BRIDGE_CHANNEL_ACTION_DEFERRED_DISSOLVING,
	};

	if (bridge->dissolved) {
		return;
	}
	bridge->dissolved = 1;

	if (cause <= 0) {
		cause = AST_CAUSE_NORMAL_CLEARING;
	}
	bridge->cause = cause;

	ast_debug(1, "Bridge %s: dissolving bridge with cause %d(%s)\n",
		bridge->uniqueid, cause, ast_cause2str(cause));

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		ast_bridge_channel_leave_bridge(bridge_channel,
			BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, cause);
	}

	/* Must defer dissolving bridge because it is already locked. */
	ast_bridge_queue_action(bridge, &action);
}

/*!
 * \internal
 * \brief Check if a bridge should dissolve because of a stolen channel and do it.
 * \since 12.0.0
 *
 * \param bridge Bridge to check.
 * \param bridge_channel Stolen channel causing the check.  It is not in the bridge to check and may be in another bridge.
 *
 * \note On entry, bridge and bridge_channel->bridge are already locked.
 *
 * \return Nothing
 */
static void bridge_dissolve_check_stolen(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
	if (bridge->dissolved) {
		return;
	}

	if (bridge_channel->features->usable
		&& ast_test_flag(&bridge_channel->features->feature_flags,
			AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP)) {
		/* The stolen channel controlled the bridge it was stolen from. */
		bridge_dissolve(bridge, 0);
		return;
	}
	if (bridge->num_channels < 2
		&& ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)) {
		/*
		 * The stolen channel has not left enough channels to keep the
		 * bridge alive.  Assume the stolen channel hung up.
		 */
		bridge_dissolve(bridge, 0);
		return;
	}
}

/*!
 * \internal
 * \brief Update connected line information after a bridge has been reconfigured.
 *
 * \param bridge The bridge itself.
 *
 * \return Nothing
 */
static void bridge_reconfigured_connected_line_update(struct ast_bridge *bridge)
{
	struct ast_party_connected_line connected;
	struct ast_bridge_channel *bridge_channel = AST_LIST_FIRST(&bridge->channels), *peer;
	unsigned char data[1024];
	size_t datalen;

	if (!bridge_channel ||
		!(bridge->technology->capabilities & (AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE)) ||
		!(peer = ast_bridge_channel_peer(bridge_channel)) ||
		ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE) ||
		ast_test_flag(ast_channel_flags(peer->chan), AST_FLAG_ZOMBIE) ||
		ast_check_hangup_locked(bridge_channel->chan) ||
		ast_check_hangup_locked(peer->chan)) {
		return;
	}

	ast_party_connected_line_init(&connected);

	ast_channel_lock(bridge_channel->chan);
	ast_connected_line_copy_from_caller(&connected, ast_channel_caller(bridge_channel->chan));
	ast_channel_unlock(bridge_channel->chan);

	if ((datalen = ast_connected_line_build_data(data, sizeof(data), &connected, NULL)) != (size_t) -1) {
		ast_bridge_channel_queue_control_data(peer, AST_CONTROL_CONNECTED_LINE, data, datalen);
	}

	ast_channel_lock(peer->chan);
	ast_connected_line_copy_from_caller(&connected, ast_channel_caller(peer->chan));
	ast_channel_unlock(peer->chan);

	if ((datalen = ast_connected_line_build_data(data, sizeof(data), &connected, NULL)) != (size_t) -1) {
		ast_bridge_channel_queue_control_data(bridge_channel, AST_CONTROL_CONNECTED_LINE, data, datalen);
	}

	ast_party_connected_line_free(&connected);
}

/*!
 * \internal
 * \brief Complete joining a channel to the bridge.
 * \since 12.0.0
 *
 * \param bridge What to operate upon.
 * \param bridge_channel What is joining the bridge technology.
 *
 * \note On entry, bridge is already locked.
 *
 * \return Nothing
 */
static void bridge_channel_complete_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
{
	/* Tell the bridge technology we are joining so they set us up */
	ast_debug(1, "Bridge %s: %p(%s) is joining %s technology\n",
		bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
		bridge->technology->name);
	if (bridge->technology->join
		&& bridge->technology->join(bridge, bridge_channel)) {
		/* We cannot leave the channel partially in the bridge so we must kick it out */
		ast_debug(1, "Bridge %s: %p(%s) failed to join %s technology (Kicking it out)\n",
			bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
			bridge->technology->name);
		bridge_channel->just_joined = 1;
		ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, 0);
		return;
	}

	bridge_channel->just_joined = 0;

	/*
	 * When a channel joins the bridge its streams need to be mapped to the bridge's
	 * media types vector. This way all streams map to the same media type index for
	 * a given channel.
	 */
	if (bridge_channel->bridge->technology->stream_topology_changed) {
		bridge_channel->bridge->technology->stream_topology_changed(
			bridge_channel->bridge, bridge_channel);
	} else {
		ast_bridge_channel_stream_map(bridge_channel);
	}
}

/*!
 * \internal
 * \brief Complete joining new channels to the bridge.
 * \since 12.0.0
 *
 * \param bridge Check for new channels on this bridge.
 *
 * \note On entry, bridge is already locked.
 *
 * \return Nothing
 */
static void bridge_complete_join(struct ast_bridge *bridge)
{
	struct ast_bridge_channel *bridge_channel;

	if (bridge->dissolved) {
		/*
		 * No sense in completing the join on channels for a dissolved
		 * bridge.  They are just going to be removed soon anyway.
		 * However, we do have reason to abort here because the bridge
		 * technology may not be able to handle the number of channels
		 * still in the bridge.
		 */
		return;
	}

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		bridge_channel_queue_deferred_frames(bridge_channel);
		if (!bridge_channel->just_joined) {
			continue;
		}
		bridge_channel_complete_join(bridge, bridge_channel);
	}
}

/*! \brief Helper function used to find the "best" bridge technology given specified capabilities */
static struct ast_bridge_technology *find_best_technology(uint32_t capabilities, struct ast_bridge *bridge)
{
	struct ast_bridge_technology *current;
	struct ast_bridge_technology *best = NULL;

	AST_RWLIST_RDLOCK(&bridge_technologies);
	AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
		if (current->suspended) {
			ast_debug(1, "Bridge technology %s is suspended. Skipping.\n",
				current->name);
			continue;
		}
		if (!(current->capabilities & capabilities)) {
			ast_debug(1, "Bridge technology %s does not have any capabilities we want.\n",
				current->name);
			continue;
		}
		if (best && current->preference <= best->preference) {
			ast_debug(1, "Bridge technology %s has less preference than %s (%u <= %u). Skipping.\n",
				current->name, best->name, current->preference, best->preference);
			continue;
		}
		if (current->compatible && !current->compatible(bridge)) {
			ast_debug(1, "Bridge technology %s is not compatible with properties of existing bridge.\n",
				current->name);
			continue;
		}
		if (!ast_module_running_ref(current->mod)) {
			ast_debug(1, "Bridge technology %s is not running, skipping.\n", current->name);
			continue;
		}
		if (best) {
			ast_module_unref(best->mod);
		}
		best = current;
	}

	if (best) {
		ast_debug(1, "Chose bridge technology %s\n", best->name);
	}

	AST_RWLIST_UNLOCK(&bridge_technologies);

	return best;
}

struct tech_deferred_destroy {
	struct ast_bridge_technology *tech;
	void *tech_pvt;
};

/*!
 * \internal
 * \brief Deferred destruction of bridge tech private structure.
 * \since 12.0.0
 *
 * \param bridge What to execute the action on.
 * \param action Deferred bridge tech destruction.
 *
 * \note On entry, bridge must not be locked.
 *
 * \return Nothing
 */
static void bridge_tech_deferred_destroy(struct ast_bridge *bridge, struct ast_frame *action)
{
	struct tech_deferred_destroy *deferred = action->data.ptr;
	struct ast_bridge dummy_bridge = {
		.technology = deferred->tech,
		.tech_pvt = deferred->tech_pvt,
		.creator = bridge->creator,
		.name = bridge->name,
		.uniqueid = bridge->uniqueid,
		};

	ast_debug(1, "Bridge %s: calling %s technology destructor (deferred, dummy)\n",
		dummy_bridge.uniqueid, dummy_bridge.technology->name);
	dummy_bridge.technology->destroy(&dummy_bridge);
	ast_module_unref(dummy_bridge.technology->mod);
}

/*!
 * \internal
 * \brief Handle bridge action frame.
 * \since 12.0.0
 *
 * \param bridge What to execute the action on.
 * \param action What to do.
 *
 * \note On entry, bridge is already locked.
 * \note Can be called by the bridge destructor.
 *
 * \return Nothing
 */
static void bridge_action_bridge(struct ast_bridge *bridge, struct ast_frame *action)
{
#if 0	/* In case we need to know when the destructor is calling us. */
	int in_destructor = !ao2_ref(bridge, 0);
#endif

	switch (action->subclass.integer) {
	case BRIDGE_CHANNEL_ACTION_DEFERRED_TECH_DESTROY:
		ast_bridge_unlock(bridge);
		bridge_tech_deferred_destroy(bridge, action);
		ast_bridge_lock(bridge);
		break;
	case BRIDGE_CHANNEL_ACTION_DEFERRED_DISSOLVING:
		ast_bridge_unlock(bridge);
		bridge->v_table->dissolving(bridge);
		ast_bridge_lock(bridge);
		break;
	default:
		/* Unexpected deferred action type.  Should never happen. */
		ast_assert(0);
		break;
	}
}

/*!
 * \internal
 * \brief Do any pending bridge actions.
 * \since 12.0.0
 *
 * \param bridge What to do actions on.
 *
 * \note On entry, bridge is already locked.
 * \note Can be called by the bridge destructor.
 *
 * \return Nothing
 */
static void bridge_handle_actions(struct ast_bridge *bridge)
{
	struct ast_frame *action;

	while ((action = AST_LIST_REMOVE_HEAD(&bridge->action_queue, frame_list))) {
		switch (action->frametype) {
		case AST_FRAME_BRIDGE_ACTION:
			bridge_action_bridge(bridge, action);
			break;
		default:
			/* Unexpected deferred frame type.  Should never happen. */
			ast_assert(0);
			break;
		}
		ast_frfree(action);
	}
}

static struct stasis_message *create_bridge_snapshot_message(struct ast_bridge *bridge)
{
	RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);

	if (!ast_bridge_snapshot_type()) {
		return NULL;
	}

	ast_bridge_lock(bridge);
	snapshot = ast_bridge_snapshot_create(bridge);
	ast_bridge_unlock(bridge);

	if (!snapshot) {
		return NULL;
	}

	return stasis_message_create(ast_bridge_snapshot_type(), snapshot);
}

static void destroy_bridge(void *obj)
{
	struct ast_bridge *bridge = obj;

	ast_debug(1, "Bridge %s: actually destroying %s bridge, nobody wants it anymore\n",
		bridge->uniqueid, bridge->v_table->name);

	if (bridge->construction_completed) {
		RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);

		clear_msg = create_bridge_snapshot_message(bridge);
		if (clear_msg) {
			RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);

			msg = stasis_cache_clear_create(clear_msg);
			if (msg) {
				stasis_publish(ast_bridge_topic(bridge), msg);
			}
		}
	}

	/* Do any pending actions in the context of destruction. */
	ast_bridge_lock(bridge);
	bridge_handle_actions(bridge);
	ast_bridge_unlock(bridge);

	/* There should not be any channels left in the bridge. */
	ast_assert(AST_LIST_EMPTY(&bridge->channels));

	ast_debug(1, "Bridge %s: calling %s bridge destructor\n",
		bridge->uniqueid, bridge->v_table->name);
	bridge->v_table->destroy(bridge);

	/* Pass off the bridge to the technology to destroy if needed */
	if (bridge->technology) {
		ast_debug(1, "Bridge %s: calling %s technology stop\n",
			bridge->uniqueid, bridge->technology->name);
		if (bridge->technology->stop) {
			ast_bridge_lock(bridge);
			bridge->technology->stop(bridge);
			ast_bridge_unlock(bridge);
		}
		ast_debug(1, "Bridge %s: calling %s technology destructor\n",
			bridge->uniqueid, bridge->technology->name);
		if (bridge->technology->destroy) {
			bridge->technology->destroy(bridge);
		}
		ast_module_unref(bridge->technology->mod);
		bridge->technology = NULL;
	}

	AST_VECTOR_FREE(&bridge->media_types);

	bridge->callid = 0;

	cleanup_video_mode(bridge);

	stasis_cp_single_unsubscribe(bridge->topics);

	ast_string_field_free_memory(bridge);
}

struct ast_bridge *bridge_register(struct ast_bridge *bridge)
{
	if (bridge) {
		bridge->construction_completed = 1;
		ast_bridge_lock(bridge);
		ast_bridge_publish_state(bridge);
		ast_bridge_unlock(bridge);
		if (!ao2_link(bridges, bridge)) {
			ast_bridge_destroy(bridge, 0);
			bridge = NULL;
		}
	}
	return bridge;
}

struct ast_bridge *bridge_alloc(size_t size, const struct ast_bridge_methods *v_table)
{
	struct ast_bridge *bridge;

	/* Check v_table that all methods are present. */
	if (!v_table
		|| !v_table->name
		|| !v_table->destroy
		|| !v_table->dissolving
		|| !v_table->push
		|| !v_table->pull
		|| !v_table->notify_masquerade
		|| !v_table->get_merge_priority) {
		ast_log(LOG_ERROR, "Virtual method table for bridge class %s not complete.\n",
			v_table && v_table->name ? v_table->name : "<unknown>");
		ast_assert(0);
		return NULL;
	}

	bridge = ao2_alloc(size, destroy_bridge);
	if (!bridge) {
		return NULL;
	}

	if (ast_string_field_init(bridge, 80)) {
		ao2_cleanup(bridge);
		return NULL;
	}

	bridge->v_table = v_table;

	AST_VECTOR_INIT(&bridge->media_types, AST_MEDIA_TYPE_END);

	return bridge;
}

struct ast_bridge *bridge_base_init(struct ast_bridge *self, uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id)
{
	char uuid_hold[AST_UUID_STR_LEN];

	if (!self) {
		return NULL;
	}

	if (!ast_strlen_zero(id)) {
		ast_string_field_set(self, uniqueid, id);
	} else {
		ast_uuid_generate_str(uuid_hold, AST_UUID_STR_LEN);
		ast_string_field_set(self, uniqueid, uuid_hold);
	}
	ast_string_field_set(self, creator, creator);
	if (!ast_strlen_zero(creator)) {
		ast_string_field_set(self, name, name);
	}

	ast_set_flag(&self->feature_flags, flags);
	self->allowed_capabilities = capabilities;

	if (!(flags & AST_BRIDGE_FLAG_INVISIBLE)) {
		if (bridge_topics_init(self) != 0) {
			ast_log(LOG_WARNING, "Bridge %s: Could not initialize topics\n",
				self->uniqueid);
			ao2_ref(self, -1);
			return NULL;
		}
	}

	/* Use our helper function to find the "best" bridge technology. */
	self->technology = find_best_technology(capabilities, self);
	if (!self->technology) {
		ast_log(LOG_WARNING, "Bridge %s: Could not create class %s.  No technology to support it.\n",
			self->uniqueid, self->v_table->name);
		ao2_ref(self, -1);
		return NULL;
	}

	/* Pass off the bridge to the technology to manipulate if needed */
	ast_debug(1, "Bridge %s: calling %s technology constructor\n",
		self->uniqueid, self->technology->name);
	if (self->technology->create && self->technology->create(self)) {
		ast_log(LOG_WARNING, "Bridge %s: failed to setup bridge technology %s\n",
			self->uniqueid, self->technology->name);
		ao2_ref(self, -1);
		return NULL;
	}
	ast_debug(1, "Bridge %s: calling %s technology start\n",
		self->uniqueid, self->technology->name);
	if (self->technology->start && self->technology->start(self)) {
		ast_log(LOG_WARNING, "Bridge %s: failed to start bridge technology %s\n",
			self->uniqueid, self->technology->name);
		ao2_ref(self, -1);
		return NULL;
	}

	if (!(flags & AST_BRIDGE_FLAG_INVISIBLE)) {
		if (!ast_bridge_topic(self)) {
			ao2_ref(self, -1);
			return NULL;
		}
	}

	return self;
}

/*!
 * \internal
 * \brief ast_bridge base class destructor.
 * \since 12.0.0
 *
 * \param self Bridge to operate upon.
 *
 * \note Stub because of nothing to do.
 *
 * \return Nothing
 */
static void bridge_base_destroy(struct ast_bridge *self)
{
}

/*!
 * \internal
 * \brief The bridge is being dissolved.
 * \since 12.0.0
 *
 * \param self Bridge to operate upon.
 *
 * \return Nothing
 */
static void bridge_base_dissolving(struct ast_bridge *self)
{
	ao2_unlink(bridges, self);
}

/*!
 * \internal
 * \brief ast_bridge base push method.
 * \since 12.0.0
 *
 * \param self Bridge to operate upon.
 * \param bridge_channel Bridge channel to push.
 * \param swap Bridge channel to swap places with if not NULL.
 *
 * \note On entry, self is already locked.
 * \note Stub because of nothing to do.
 *
 * \retval 0 on success
 * \retval -1 on failure
 */
static int bridge_base_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
{
	return 0;
}

/*!
 * \internal
 * \brief ast_bridge base pull method.
 * \since 12.0.0
 *
 * \param self Bridge to operate upon.
 * \param bridge_channel Bridge channel to pull.
 *
 * \note On entry, self is already locked.
 *
 * \return Nothing
 */
static void bridge_base_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
{
	ast_bridge_features_remove(bridge_channel->features, AST_BRIDGE_HOOK_REMOVE_ON_PULL);
}

/*!
 * \internal
 * \brief ast_bridge base notify_masquerade method.
 * \since 12.0.0
 *
 * \param self Bridge to operate upon.
 * \param bridge_channel Bridge channel that was masqueraded.
 *
 * \note On entry, self is already locked.
 *
 * \return Nothing
 */
static void bridge_base_notify_masquerade(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
{
	self->reconfigured = 1;
}

/*!
 * \internal
 * \brief Get the merge priority of this bridge.
 * \since 12.0.0
 *
 * \param self Bridge to operate upon.
 *
 * \note On entry, self is already locked.
 *
 * \return Merge priority
 */
static int bridge_base_get_merge_priority(struct ast_bridge *self)
{
	return 0;
}

/*!
 * \internal
 * \brief ast_bridge base push_peek method.
 * \since 13.2.0
 *
 * \param self Bridge to operate upon.
 * \param bridge_channel Bridge channel to push.
 * \param swap Bridge channel to swap places with if not NULL.
 *
 * \note On entry, self is already locked.
 * \note Stub because of nothing to do.
 *
 * \retval 0 on success
 * \retval -1 on failure
 */
static int bridge_base_push_peek(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
{
	return 0;
}

struct ast_bridge_methods ast_bridge_base_v_table = {
	.name = "base",
	.destroy = bridge_base_destroy,
	.dissolving = bridge_base_dissolving,
	.push = bridge_base_push,
	.pull = bridge_base_pull,
	.notify_masquerade = bridge_base_notify_masquerade,
	.get_merge_priority = bridge_base_get_merge_priority,
	.push_peek = bridge_base_push_peek,
};

struct ast_bridge *ast_bridge_base_new(uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id)
{
	void *bridge;

	bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_base_v_table);
	bridge = bridge_base_init(bridge, capabilities, flags, creator, name, id);
	bridge = bridge_register(bridge);
	return bridge;
}

int ast_bridge_destroy(struct ast_bridge *bridge, int cause)
{
	ast_debug(1, "Bridge %s: telling all channels to leave the party\n", bridge->uniqueid);
	ast_bridge_lock(bridge);
	bridge_dissolve(bridge, cause);
	ast_bridge_unlock(bridge);

	ao2_ref(bridge, -1);

	return 0;
}

/*!
 * \internal
 * \brief Perform the smart bridge operation.
 * \since 12.0.0
 *
 * \param bridge Work on this bridge.
 *
 * \details
 * Basically see if a new bridge technology should be used instead
 * of the current one.
 *
 * \note On entry, bridge is already locked.
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int smart_bridge_operation(struct ast_bridge *bridge)
{
	uint32_t new_capabilities;
	struct ast_bridge_technology *new_technology;
	struct ast_bridge_technology *old_technology = bridge->technology;
	struct ast_bridge_channel *bridge_channel;
	struct ast_frame *deferred_action;
	struct ast_bridge dummy_bridge = {
		.technology = bridge->technology,
		.tech_pvt = bridge->tech_pvt,
		.creator = bridge->creator,
		.name = bridge->name,
		.uniqueid = bridge->uniqueid,
	};

	if (bridge->dissolved) {
		ast_debug(1, "Bridge %s is dissolved, not performing smart bridge operation.\n",
			bridge->uniqueid);
		return 0;
	}

	/* Determine new bridge technology capabilities needed. */
	if (2 < bridge->num_channels) {
		new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
		new_capabilities &= bridge->allowed_capabilities;
	} else {
		new_capabilities = AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX;
		new_capabilities &= bridge->allowed_capabilities;
		if (!new_capabilities
			&& (bridge->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
			/* Allow switching between different multimix bridge technologies. */
			new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
		}
	}

	/* Find a bridge technology to satisfy the new capabilities. */
	new_technology = find_best_technology(new_capabilities, bridge);
	if (!new_technology) {
		int is_compatible = 0;

		if (old_technology->compatible) {
			is_compatible = old_technology->compatible(bridge);
		} else if (old_technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
			is_compatible = 1;
		} else if (bridge->num_channels <= 2
			&& (old_technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX)) {
			is_compatible = 1;
		}

		if (is_compatible) {
			ast_debug(1, "Bridge %s could not get a new technology, staying with old technology.\n",
				bridge->uniqueid);
			return 0;
		}
		ast_log(LOG_WARNING, "Bridge %s has no technology available to support it.\n",
			bridge->uniqueid);
		return -1;
	}
	if (new_technology == old_technology) {
		ast_debug(1, "Bridge %s is already using the new technology.\n",
			bridge->uniqueid);
		ast_module_unref(old_technology->mod);
		return 0;
	}

	if (old_technology->destroy) {
		struct tech_deferred_destroy deferred_tech_destroy = {
			.tech = dummy_bridge.technology,
			.tech_pvt = dummy_bridge.tech_pvt,
		};
		struct ast_frame action = {
			.frametype = AST_FRAME_BRIDGE_ACTION,
			.subclass.integer = BRIDGE_CHANNEL_ACTION_DEFERRED_TECH_DESTROY,
			.data.ptr = &deferred_tech_destroy,
			.datalen = sizeof(deferred_tech_destroy),
		};

		/*
		 * We need to defer the bridge technology destroy callback
		 * because we have the bridge locked.
		 */
		deferred_action = ast_frdup(&action);
		if (!deferred_action) {
			ast_module_unref(new_technology->mod);
			return -1;
		}
	} else {
		deferred_action = NULL;
	}

	/*
	 * We are now committed to changing the bridge technology.  We
	 * must not release the bridge lock until we have installed the
	 * new bridge technology.
	 */
	ast_verb(4, "Bridge %s: switching from %s technology to %s\n",
		bridge->uniqueid, old_technology->name, new_technology->name);

	/*
	 * Since we are soon going to pass this bridge to a new
	 * technology we need to NULL out the tech_pvt pointer but
	 * don't worry as it still exists in dummy_bridge, ditto for the
	 * old technology.
	 */
	bridge->tech_pvt = NULL;
	bridge->technology = new_technology;

	/* Setup the new bridge technology. */
	ast_debug(1, "Bridge %s: calling %s technology constructor\n",
		bridge->uniqueid, new_technology->name);
	if (new_technology->create && new_technology->create(bridge)) {
		ast_log(LOG_WARNING, "Bridge %s: failed to setup bridge technology %s\n",
			bridge->uniqueid, new_technology->name);
		bridge->tech_pvt = dummy_bridge.tech_pvt;
		bridge->technology = dummy_bridge.technology;
		ast_module_unref(new_technology->mod);
		return -1;
	}

	/* To ensure that things are sane for the old technology move the channels it
	 * expects to the dummy bridge
	 */
	AST_LIST_TRAVERSE_SAFE_BEGIN(&bridge->channels, bridge_channel, entry) {
		if (bridge_channel->just_joined) {
			continue;
		}
		ast_debug(1, "Bridge %s: moving %p(%s) to dummy bridge temporarily\n",
			bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
		AST_LIST_REMOVE_CURRENT(entry);
		AST_LIST_INSERT_TAIL(&dummy_bridge.channels, bridge_channel, entry);
		dummy_bridge.num_channels++;
		if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
			dummy_bridge.num_lonely++;
		}
		if (!bridge_channel->suspended) {
			dummy_bridge.num_active++;
		}
	}
	AST_LIST_TRAVERSE_SAFE_END;

	/* Take all the channels out of the old technology */
	AST_LIST_TRAVERSE_SAFE_BEGIN(&dummy_bridge.channels, bridge_channel, entry) {
		ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology (dummy)\n",
			dummy_bridge.uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
			old_technology->name);
		if (old_technology->leave) {
			old_technology->leave(&dummy_bridge, bridge_channel);
		}
		AST_LIST_REMOVE_CURRENT(entry);
		AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
		dummy_bridge.num_channels--;
		if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
			dummy_bridge.num_lonely--;
		}
		if (!bridge_channel->suspended) {
			dummy_bridge.num_active--;
		}
	}
	AST_LIST_TRAVERSE_SAFE_END;

	ast_debug(1, "Bridge %s: calling %s technology stop\n",
		dummy_bridge.uniqueid, old_technology->name);
	if (old_technology->stop) {
		old_technology->stop(&dummy_bridge);
	}

	/* Add any new channels or re-add existing channels to the bridge. */
	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		bridge_channel_complete_join(bridge, bridge_channel);
	}

	ast_debug(1, "Bridge %s: calling %s technology start\n",
		bridge->uniqueid, new_technology->name);
	if (new_technology->start && new_technology->start(bridge)) {
		ast_log(LOG_WARNING, "Bridge %s: failed to start bridge technology %s\n",
			bridge->uniqueid, new_technology->name);
	}

	/*
	 * Now that all the channels have been moved over we need to get
	 * rid of all the information the old technology may have left
	 * around.
	 */
	if (old_technology->destroy) {
		ast_debug(1, "Bridge %s: deferring %s technology destructor\n",
			dummy_bridge.uniqueid, old_technology->name);
		bridge_queue_action_nodup(bridge, deferred_action);
	} else {
		ast_debug(1, "Bridge %s: calling %s technology destructor\n",
			dummy_bridge.uniqueid, old_technology->name);
		ast_module_unref(old_technology->mod);
	}

	return 0;
}

/*!
 * \internal
 * \brief Bridge channel to check if a BRIDGE_PLAY_SOUND needs to be played.
 * \since 12.0.0
 *
 * \param bridge_channel What to check.
 *
 * \return Nothing
 */
static void check_bridge_play_sound(struct ast_bridge_channel *bridge_channel)
{
	const char *play_file;

	ast_channel_lock(bridge_channel->chan);
	play_file = pbx_builtin_getvar_helper(bridge_channel->chan, "BRIDGE_PLAY_SOUND");
	if (!ast_strlen_zero(play_file)) {
		play_file = ast_strdupa(play_file);
		pbx_builtin_setvar_helper(bridge_channel->chan, "BRIDGE_PLAY_SOUND", NULL);
	} else {
		play_file = NULL;
	}
	ast_channel_unlock(bridge_channel->chan);

	if (play_file) {
		ast_bridge_channel_queue_playfile(bridge_channel, NULL, play_file, NULL);
	}
}

/*!
 * \internal
 * \brief Check for any BRIDGE_PLAY_SOUND channel variables in the bridge.
 * \since 12.0.0
 *
 * \param bridge What to operate on.
 *
 * \note On entry, the bridge is already locked.
 *
 * \return Nothing
 */
static void check_bridge_play_sounds(struct ast_bridge *bridge)
{
	struct ast_bridge_channel *bridge_channel;

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		check_bridge_play_sound(bridge_channel);
	}
}

void ast_bridge_vars_set(struct ast_channel *chan, const char *name, const char *pvtid)
{
	ast_channel_stage_snapshot(chan);
	pbx_builtin_setvar_helper(chan, "BRIDGEPEER", name);
	pbx_builtin_setvar_helper(chan, "BRIDGEPVTCALLID", pvtid);
	ast_channel_stage_snapshot_done(chan);
}

/*!
 * \internal
 * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a 2 party bridge.
 * \since 12.0.0
 *
 * \param c0 Party of the first part.
 * \param c1 Party of the second part.
 *
 * \note On entry, the bridge is already locked.
 * \note The bridge is expected to have exactly two parties.
 *
 * \return Nothing
 */
static void set_bridge_peer_vars_2party(struct ast_channel *c0, struct ast_channel *c1)
{
	const char *c0_name;
	const char *c1_name;
	const char *c0_pvtid = NULL;
	const char *c1_pvtid = NULL;
#define UPDATE_BRIDGE_VARS_GET(chan, name, pvtid)									\
	do {																			\
		name = ast_strdupa(ast_channel_name(chan));									\
		if (ast_channel_tech(chan)->get_pvt_uniqueid) {								\
			pvtid = ast_strdupa(ast_channel_tech(chan)->get_pvt_uniqueid(chan));	\
		}																			\
	} while (0)

	ast_channel_lock(c1);
	UPDATE_BRIDGE_VARS_GET(c1, c1_name, c1_pvtid);
	ast_channel_unlock(c1);

	ast_channel_lock(c0);
	ast_bridge_vars_set(c0, c1_name, c1_pvtid);
	UPDATE_BRIDGE_VARS_GET(c0, c0_name, c0_pvtid);
	ast_channel_unlock(c0);

	ast_channel_lock(c1);
	ast_bridge_vars_set(c1, c0_name, c0_pvtid);
	ast_channel_unlock(c1);
}

/*!
 * \internal
 * \brief Fill the BRIDGEPEER value buffer with a comma separated list of channel names.
 * \since 12.0.0
 *
 * \param buf Buffer to fill.  The caller must guarantee the buffer is large enough.
 * \param cur_idx Which index into names[] to skip.
 * \param names Channel names to put in the buffer.
 * \param num_names Number of names in the array.
 *
 * \return Nothing
 */
static void fill_bridgepeer_buf(char *buf, unsigned int cur_idx, const char *names[], unsigned int num_names)
{
	int need_separator = 0;
	unsigned int idx;
	const char *src;
	char *pos;

	pos = buf;
	for (idx = 0; idx < num_names; ++idx) {
		if (idx == cur_idx) {
			continue;
		}

		if (need_separator) {
			*pos++ = ',';
		}
		need_separator = 1;

		/* Copy name into buffer. */
		src = names[idx];
		while (*src) {
			*pos++ = *src++;
		}
	}
	*pos = '\0';
}

/*!
 * \internal
 * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a multi-party bridge.
 * \since 12.0.0
 *
 * \param bridge What to operate on.
 *
 * \note On entry, the bridge is already locked.
 * \note The bridge is expected to have more than two parties.
 *
 * \return Nothing
 */
static void set_bridge_peer_vars_multiparty(struct ast_bridge *bridge)
{
/*
 * Set a maximum number of channel names for the BRIDGEPEER
 * list.  The plus one is for the current channel which is not
 * put in the list.
 */
#define MAX_BRIDGEPEER_CHANS	(10 + 1)

	unsigned int idx;
	unsigned int num_names;
	unsigned int len;
	const char **names;
	char *buf;
	struct ast_bridge_channel *bridge_channel;

	/* Get first MAX_BRIDGEPEER_CHANS channel names. */
	num_names = MIN(bridge->num_channels, MAX_BRIDGEPEER_CHANS);
	names = ast_alloca(num_names * sizeof(*names));
	idx = 0;
	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		if (num_names <= idx) {
			break;
		}
		ast_channel_lock(bridge_channel->chan);
		names[idx++] = ast_strdupa(ast_channel_name(bridge_channel->chan));
		ast_channel_unlock(bridge_channel->chan);
	}

	/* Determine maximum buf size needed. */
	len = num_names;
	for (idx = 0; idx < num_names; ++idx) {
		len += strlen(names[idx]);
	}
	buf = ast_alloca(len);

	/* Set the bridge channel variables. */
	idx = 0;
	buf[0] = '\0';
	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		if (idx < num_names) {
			fill_bridgepeer_buf(buf, idx, names, num_names);
		}
		++idx;

		ast_channel_lock(bridge_channel->chan);
		ast_bridge_vars_set(bridge_channel->chan, buf, NULL);
		ast_channel_unlock(bridge_channel->chan);
	}
}

/*!
 * \internal
 * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a holding bridge.
 * \since 12.0.0
 *
 * \param bridge What to operate on.
 *
 * \note On entry, the bridge is already locked.
 *
 * \return Nothing
 */
static void set_bridge_peer_vars_holding(struct ast_bridge *bridge)
{
	struct ast_bridge_channel *bridge_channel;

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		ast_channel_lock(bridge_channel->chan);
		ast_bridge_vars_set(bridge_channel->chan, NULL, NULL);
		ast_channel_unlock(bridge_channel->chan);
	}
}

/*!
 * \internal
 * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in the bridge.
 * \since 12.0.0
 *
 * \param bridge What to operate on.
 *
 * \note On entry, the bridge is already locked.
 *
 * \return Nothing
 */
static void set_bridge_peer_vars(struct ast_bridge *bridge)
{
	if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
		set_bridge_peer_vars_holding(bridge);
		return;
	}
	if (bridge->num_channels < 2) {
		return;
	}
	if (bridge->num_channels == 2) {
		set_bridge_peer_vars_2party(AST_LIST_FIRST(&bridge->channels)->chan,
			AST_LIST_LAST(&bridge->channels)->chan);
	} else {
		set_bridge_peer_vars_multiparty(bridge);
	}
}

void bridge_reconfigured(struct ast_bridge *bridge, unsigned int colp_update)
{
	if (!bridge->reconfigured) {
		return;
	}
	bridge->reconfigured = 0;
	if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_SMART)
		&& smart_bridge_operation(bridge)) {
		/* Smart bridge failed. */
		bridge_dissolve(bridge, 0);
		return;
	}
	bridge_complete_join(bridge);

	if (bridge->dissolved) {
		return;
	}
	check_bridge_play_sounds(bridge);
	set_bridge_peer_vars(bridge);
	ast_bridge_publish_state(bridge);

	if (colp_update) {
		bridge_reconfigured_connected_line_update(bridge);
	}
}

struct ast_bridge_channel *bridge_find_channel(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;

	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		if (bridge_channel->chan == chan) {
			break;
		}
	}

	return bridge_channel;
}

void ast_bridge_notify_masquerade(struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;
	struct ast_bridge *bridge;

	/* Safely get the bridge_channel pointer for the chan. */
	ast_channel_lock(chan);
	bridge_channel = ast_channel_get_bridge_channel(chan);
	ast_channel_unlock(chan);
	if (!bridge_channel) {
		/* Not in a bridge */
		return;
	}

	ast_bridge_channel_lock_bridge(bridge_channel);
	bridge = bridge_channel->bridge;
	if (bridge_channel == bridge_find_channel(bridge, chan)) {
/*
 * XXX ASTERISK-22366 this needs more work.  The channels need
 * to be made compatible again if the formats change. The
 * bridge_channel thread needs to monitor for this case.
 */
		/* The channel we want to notify is still in a bridge. */
		bridge->v_table->notify_masquerade(bridge, bridge_channel);
		bridge_reconfigured(bridge, 1);
	}
	ast_bridge_unlock(bridge);
	ao2_ref(bridge_channel, -1);
}

/*!
 * \brief Internal bridge impart wait condition and associated conditional.
 */
struct bridge_channel_impart_cond {
	AST_LIST_ENTRY(bridge_channel_impart_cond) node;
	/*! Lock for the data structure */
	ast_mutex_t lock;
	/*! Wait condition */
	ast_cond_t cond;
	/*! Wait until done */
	int done;
};

AST_LIST_HEAD_NOLOCK(bridge_channel_impart_ds_head, bridge_channel_impart_cond);

/*!
 * \internal
 * \brief Signal imparting threads to wake up.
 * \since 13.9.0
 *
 * \param ds_head List of imparting threads to wake up.
 *
 * \return Nothing
 */
static void bridge_channel_impart_ds_head_signal(struct bridge_channel_impart_ds_head *ds_head)
{
	if (ds_head) {
		struct bridge_channel_impart_cond *cond;

		while ((cond = AST_LIST_REMOVE_HEAD(ds_head, node))) {
			ast_mutex_lock(&cond->lock);
			cond->done = 1;
			ast_cond_signal(&cond->cond);
			ast_mutex_unlock(&cond->lock);
		}
	}
}

static void bridge_channel_impart_ds_head_dtor(void *doomed)
{
	bridge_channel_impart_ds_head_signal(doomed);
	ast_free(doomed);
}

/*!
 * \internal
 * \brief Fixup the bridge impart datastore.
 * \since 13.9.0
 *
 * \param data Bridge impart datastore data to fixup from old_chan.
 * \param old_chan The datastore is moving from this channel.
 * \param new_chan The datastore is moving to this channel.
 *
 * \return Nothing
 */
static void bridge_channel_impart_ds_head_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
{
	/*
	 * Signal any waiting impart threads.  The masquerade is going to kill
	 * old_chan and we don't need to be waiting on new_chan.
	 */
	bridge_channel_impart_ds_head_signal(data);
}

static const struct ast_datastore_info bridge_channel_impart_ds_info = {
	.type = "bridge-impart-ds",
	.destroy = bridge_channel_impart_ds_head_dtor,
	.chan_fixup = bridge_channel_impart_ds_head_fixup,
};

/*!
 * \internal
 * \brief Add impart wait datastore conditional to channel.
 * \since 13.9.0
 *
 * \param chan Channel to add the impart wait conditional.
 * \param cond Imparting conditional to add.
 *
 * \retval 0 on success.
 * \retval -1 on error.
 */
static int bridge_channel_impart_add(struct ast_channel *chan, struct bridge_channel_impart_cond *cond)
{
	struct ast_datastore *datastore;
	struct bridge_channel_impart_ds_head *ds_head;

	ast_channel_lock(chan);

	datastore = ast_channel_datastore_find(chan, &bridge_channel_impart_ds_info, NULL);
	if (!datastore) {
		datastore = ast_datastore_alloc(&bridge_channel_impart_ds_info, NULL);
		if (!datastore) {
			ast_channel_unlock(chan);
			return -1;
		}
		ds_head = ast_calloc(1, sizeof(*ds_head));
		if (!ds_head) {
			ast_channel_unlock(chan);
			ast_datastore_free(datastore);
			return -1;
		}
		datastore->data = ds_head;
		ast_channel_datastore_add(chan, datastore);
	} else {
		ds_head = datastore->data;
		ast_assert(ds_head != NULL);
	}

	AST_LIST_INSERT_TAIL(ds_head, cond, node);

	ast_channel_unlock(chan);
	return 0;
}

void bridge_channel_impart_signal(struct ast_channel *chan)
{
	struct ast_datastore *datastore;

	ast_channel_lock(chan);
	datastore = ast_channel_datastore_find(chan, &bridge_channel_impart_ds_info, NULL);
	if (datastore) {
		bridge_channel_impart_ds_head_signal(datastore->data);
	}
	ast_channel_unlock(chan);
}

/*!
 * \internal
 * \brief Block imparting channel thread until signaled.
 * \since 13.9.0
 *
 * \param cond Imparting conditional to wait for.
 *
 * \return Nothing
 */
static void bridge_channel_impart_wait(struct bridge_channel_impart_cond *cond)
{
	ast_mutex_lock(&cond->lock);
	while (!cond->done) {
		ast_cond_wait(&cond->cond, &cond->lock);
	}
	ast_mutex_unlock(&cond->lock);
}

/*
 * XXX ASTERISK-21271 make ast_bridge_join() require features to be allocated just like ast_bridge_impart() and not expect the struct back.
 *
 * This change is really going to break ConfBridge.  All other
 * users are easily changed.  However, it is needed so the
 * bridging code can manipulate features on all channels
 * consistently no matter how they joined.
 *
 * Need to update the features parameter doxygen when this
 * change is made to be like ast_bridge_impart().
 */
int ast_bridge_join(struct ast_bridge *bridge,
	struct ast_channel *chan,
	struct ast_channel *swap,
	struct ast_bridge_features *features,
	struct ast_bridge_tech_optimizations *tech_args,
	enum ast_bridge_join_flags flags)
{
	struct ast_bridge_channel *bridge_channel;
	int res = 0;

	bridge_channel = bridge_channel_internal_alloc(bridge);
	if (flags & AST_BRIDGE_JOIN_PASS_REFERENCE) {
		ao2_ref(bridge, -1);
	}
	if (!bridge_channel) {
		ao2_t_cleanup(swap, "Error exit: bridge_channel alloc failed");
		res = -1;
		goto join_exit;
	}
/* XXX ASTERISK-21271 features cannot be NULL when passed in. When it is changed to allocated we can do like ast_bridge_impart() and allocate one. */
	ast_assert(features != NULL);
	if (!features) {
		ao2_ref(bridge_channel, -1);
		ao2_t_cleanup(swap, "Error exit: features is NULL");
		res = -1;
		goto join_exit;
	}
	if (tech_args) {
		bridge_channel->tech_args = *tech_args;
	}

	ast_channel_lock(chan);
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
		res = -1;
	} else {
		ast_channel_internal_bridge_channel_set(chan, bridge_channel);
	}
	ast_channel_unlock(chan);
	bridge_channel->thread = pthread_self();
	bridge_channel->chan = chan;
	bridge_channel->swap = swap;
	bridge_channel->features = features;
	bridge_channel->inhibit_colp = !!(flags & AST_BRIDGE_JOIN_INHIBIT_JOIN_COLP);

	/* allow subclass to peek at upcoming push operation */
	if (bridge->v_table->push_peek && !res) {
		struct ast_bridge_channel *bcswap = NULL;

		ast_bridge_lock(bridge);
		if (bridge_channel->swap) {
			bcswap = bridge_find_channel(bridge, bridge_channel->swap);
		}
		res = bridge->v_table->push_peek(bridge, bridge_channel, bcswap);
		ast_bridge_unlock(bridge);
	}

	if (!res) {
		res = bridge_channel_internal_join(bridge_channel);
	}

	/* Cleanup all the data in the bridge channel after it leaves the bridge. */
	ast_channel_lock(chan);
	ast_channel_internal_bridge_channel_set(chan, NULL);
	ast_channel_unlock(chan);
	bridge_channel->chan = NULL;
	/* If bridge_channel->swap is not NULL then the join failed. */
	ao2_t_cleanup(bridge_channel->swap, "Bridge complete: join failed");
	bridge_channel->swap = NULL;
	bridge_channel->features = NULL;

	ao2_ref(bridge_channel, -1);

join_exit:
	ast_bridge_run_after_callback(chan);
	bridge_channel_impart_signal(chan);
	if (!(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)
		&& !ast_bridge_setup_after_goto(chan)) {
		/* Claim the after bridge goto is an async goto destination. */
		ast_channel_lock(chan);
		ast_softhangup_nolock(chan, AST_SOFTHANGUP_ASYNCGOTO);
		ast_channel_unlock(chan);
	}
	return res;
}

/*! \brief Thread responsible for imparted bridged channels to be departed */
static void *bridge_channel_depart_thread(void *data)
{
	struct ast_bridge_channel *bridge_channel = data;
	int res = 0;

	if (bridge_channel->callid) {
		ast_callid_threadassoc_add(bridge_channel->callid);
	}

	res = bridge_channel_internal_join(bridge_channel);

	/*
	 * cleanup
	 *
	 * If bridge_channel->swap is not NULL then the join failed.
	 */
	ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Departable impart join failed");
	bridge_channel->swap = NULL;
	ast_bridge_features_destroy(bridge_channel->features);
	bridge_channel->features = NULL;

	ast_bridge_discard_after_callback(bridge_channel->chan,
		res ? AST_BRIDGE_AFTER_CB_REASON_IMPART_FAILED : AST_BRIDGE_AFTER_CB_REASON_DEPART);
	/* If join failed there will be impart threads waiting. */
	bridge_channel_impart_signal(bridge_channel->chan);
	ast_bridge_discard_after_goto(bridge_channel->chan);

	return NULL;
}

/*! \brief Thread responsible for independent imparted bridged channels */
static void *bridge_channel_ind_thread(void *data)
{
	struct ast_bridge_channel *bridge_channel = data;
	struct ast_channel *chan;

	if (bridge_channel->callid) {
		ast_callid_threadassoc_add(bridge_channel->callid);
	}

	bridge_channel_internal_join(bridge_channel);
	chan = bridge_channel->chan;

	/* cleanup */
	ast_channel_lock(chan);
	ast_channel_internal_bridge_channel_set(chan, NULL);
	ast_channel_unlock(chan);
	bridge_channel->chan = NULL;
	/* If bridge_channel->swap is not NULL then the join failed. */
	ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Independent impart join failed");
	bridge_channel->swap = NULL;
	ast_bridge_features_destroy(bridge_channel->features);
	bridge_channel->features = NULL;

	ao2_ref(bridge_channel, -1);

	ast_bridge_run_after_callback(chan);
	/* If join failed there will be impart threads waiting. */
	bridge_channel_impart_signal(chan);
	ast_bridge_run_after_goto(chan);
	return NULL;
}

static int bridge_impart_internal(struct ast_bridge *bridge,
	struct ast_channel *chan,
	struct ast_channel *swap,
	struct ast_bridge_features *features,
	enum ast_bridge_impart_flags flags,
	struct bridge_channel_impart_cond *cond)
{
	int res = 0;
	struct ast_bridge_channel *bridge_channel;

	/* Imparted channels cannot have a PBX. */
	if (ast_channel_pbx(chan)) {
		ast_log(AST_LOG_WARNING, "Channel %s has a PBX thread and cannot be imparted into bridge %s\n",
			ast_channel_name(chan), bridge->uniqueid);
		ast_bridge_features_destroy(features);
		return -1;
	}

	/* Supply an empty features structure if the caller did not. */
	if (!features) {
		features = ast_bridge_features_new();
		if (!features) {
			return -1;
		}
	}

	/* Try to allocate a structure for the bridge channel */
	bridge_channel = bridge_channel_internal_alloc(bridge);
	if (!bridge_channel) {
		ast_bridge_features_destroy(features);
		return -1;
	}

	ast_channel_lock(chan);
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
		ast_log(AST_LOG_NOTICE, "Channel %s is a zombie and cannot be imparted into bridge %s\n",
			ast_channel_name(chan), bridge->uniqueid);
		res = -1;
	} else {
		ast_channel_internal_bridge_channel_set(chan, bridge_channel);
	}
	ast_channel_unlock(chan);
	bridge_channel->chan = chan;
	bridge_channel->swap = ao2_t_bump(swap, "Setting up bridge impart");
	bridge_channel->features = features;
	bridge_channel->inhibit_colp = !!(flags & AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP);
	bridge_channel->depart_wait =
		(flags & AST_BRIDGE_IMPART_CHAN_MASK) == AST_BRIDGE_IMPART_CHAN_DEPARTABLE;
	bridge_channel->callid = ast_read_threadstorage_callid();

	/* allow subclass to peek at swap channel before it can hangup */
	if (bridge->v_table->push_peek && !res) {
		struct ast_bridge_channel *bcswap = NULL;

		ast_bridge_lock(bridge);
		if (bridge_channel->swap) {
			bcswap = bridge_find_channel(bridge, bridge_channel->swap);
		}
		res = bridge->v_table->push_peek(bridge, bridge_channel, bcswap);
		ast_bridge_unlock(bridge);
	}

	/* Actually create the thread that will handle the channel */
	if (!res) {
		res = bridge_channel_impart_add(chan, cond);
	}
	if (!res) {
		if ((flags & AST_BRIDGE_IMPART_CHAN_MASK) == AST_BRIDGE_IMPART_CHAN_INDEPENDENT) {
			res = ast_pthread_create_detached(&bridge_channel->thread, NULL,
				bridge_channel_ind_thread, bridge_channel);
		} else {
			res = ast_pthread_create(&bridge_channel->thread, NULL,
				bridge_channel_depart_thread, bridge_channel);
		}

		if (!res) {
			bridge_channel_impart_wait(cond);
		}
	}

	if (res) {
		/* cleanup */
		ast_channel_lock(chan);
		ast_channel_internal_bridge_channel_set(chan, NULL);
		ast_channel_unlock(chan);
		bridge_channel->chan = NULL;
		ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Impart failed");
		bridge_channel->swap = NULL;
		ast_bridge_features_destroy(bridge_channel->features);
		bridge_channel->features = NULL;

		ao2_ref(bridge_channel, -1);
		return -1;
	}

	return 0;
}

int ast_bridge_impart(struct ast_bridge *bridge,
	struct ast_channel *chan,
	struct ast_channel *swap,
	struct ast_bridge_features *features,
	enum ast_bridge_impart_flags flags)
{
	struct bridge_channel_impart_cond cond = {
		.done = 0,
	};
	int res;

	ast_mutex_init(&cond.lock);
	ast_cond_init(&cond.cond, NULL);

	res = bridge_impart_internal(bridge, chan, swap, features, flags, &cond);
	if (res) {
		/* Impart failed.  Signal any other waiting impart threads */
		bridge_channel_impart_signal(chan);
	}

	ast_cond_destroy(&cond.cond);
	ast_mutex_destroy(&cond.lock);

	return res;
}

int ast_bridge_depart(struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;
	int departable;

	ast_channel_lock(chan);
	bridge_channel = ast_channel_internal_bridge_channel(chan);
	departable = bridge_channel && bridge_channel->depart_wait;
	ast_channel_unlock(chan);
	if (!departable) {
		ast_log(LOG_ERROR, "Channel %s cannot be departed.\n",
			ast_channel_name(chan));
		/*
		 * Should never happen.  It likely means that
		 * ast_bridge_depart() is called by two threads for the same
		 * channel, the channel was never imparted to be departed, or it
		 * has already been departed.
		 */
		ast_assert(0);
		return -1;
	}

	/*
	 * We are claiming the bridge_channel reference held by
	 * bridge_channel_depart_thread().
	 */

	ast_bridge_channel_leave_bridge(bridge_channel,
		BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);

	/* Wait for the depart thread to die */
	ast_debug(1, "Waiting for %p(%s) bridge thread to die.\n",
		bridge_channel, ast_channel_name(bridge_channel->chan));
	pthread_join(bridge_channel->thread, NULL);

	ast_channel_lock(chan);
	ast_channel_internal_bridge_channel_set(chan, NULL);
	ast_channel_unlock(chan);

	/* We can get rid of the bridge_channel after the depart thread has died. */
	ao2_ref(bridge_channel, -1);
	return 0;
}

int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;

	ast_bridge_lock(bridge);

	/* Try to find the channel that we want to remove */
	if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
		ast_bridge_unlock(bridge);
		return -1;
	}

	ast_bridge_channel_leave_bridge(bridge_channel,
		BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);

	ast_bridge_unlock(bridge);

	return 0;
}

static void kick_it(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
{
	ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
}

int ast_bridge_kick(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;
	int res;

	ast_bridge_lock(bridge);

	/* Try to find the channel that we want to kick. */
	if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
		ast_bridge_unlock(bridge);
		return -1;
	}

	res = ast_bridge_channel_queue_callback(bridge_channel, 0, kick_it, NULL, 0);

	ast_bridge_unlock(bridge);

	return res;
}

/*!
 * \internal
 * \brief Point the bridge_channel to a new bridge.
 * \since 12.0.0
 *
 * \param bridge_channel What is to point to a new bridge.
 * \param new_bridge Where the bridge channel should point.
 *
 * \return Nothing
 */
static void bridge_channel_change_bridge(struct ast_bridge_channel *bridge_channel, struct ast_bridge *new_bridge)
{
	struct ast_bridge *old_bridge;

	ao2_ref(new_bridge, +1);
	ast_bridge_channel_lock(bridge_channel);
	ast_channel_lock(bridge_channel->chan);
	old_bridge = bridge_channel->bridge;
	bridge_channel->bridge = new_bridge;
	ast_channel_internal_bridge_set(bridge_channel->chan, new_bridge);
	ast_channel_unlock(bridge_channel->chan);
	ast_bridge_channel_unlock(bridge_channel);
	ao2_ref(old_bridge, -1);
}

static void bridge_channel_moving(struct ast_bridge_channel *bridge_channel, struct ast_bridge *src, struct ast_bridge *dst)
{
	struct ast_bridge_features *features = bridge_channel->features;
	struct ast_bridge_hook *hook;
	struct ao2_iterator iter;

	/* Run any moving hooks. */
	iter = ao2_iterator_init(features->other_hooks, 0);
	for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
		int remove_me;
		ast_bridge_move_indicate_callback move_cb;

		if (hook->type != AST_BRIDGE_HOOK_TYPE_MOVE) {
			continue;
		}
		move_cb = (ast_bridge_move_indicate_callback) hook->callback;
		remove_me = move_cb(bridge_channel, hook->hook_pvt, src, dst);
		if (remove_me) {
			ast_debug(1, "Move detection hook %p is being removed from %p(%s)\n",
				hook, bridge_channel, ast_channel_name(bridge_channel->chan));
			ao2_unlink(features->other_hooks, hook);
		}
	}
	ao2_iterator_destroy(&iter);
}

void bridge_do_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_bridge_channel **kick_me, unsigned int num_kick,
	unsigned int optimized)
{
	struct ast_bridge_channel *bridge_channel;
	unsigned int idx;

	ast_debug(1, "Merging bridge %s into bridge %s\n",
		src_bridge->uniqueid, dst_bridge->uniqueid);

	ast_bridge_publish_merge(dst_bridge, src_bridge);

	/*
	 * Move channels from src_bridge over to dst_bridge.
	 *
	 * We must use AST_LIST_TRAVERSE_SAFE_BEGIN() because
	 * bridge_channel_internal_pull() alters the list we are traversing.
	 */
	AST_LIST_TRAVERSE_SAFE_BEGIN(&src_bridge->channels, bridge_channel, entry) {
		if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
			/*
			 * The channel is already leaving let it leave normally because
			 * pulling it may delete hooks that should run for this channel.
			 */
			continue;
		}
		if (ast_test_flag(&bridge_channel->features->feature_flags,
			AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE)) {
			continue;
		}

		if (kick_me) {
			for (idx = 0; idx < num_kick; ++idx) {
				if (bridge_channel == kick_me[idx]) {
					ast_bridge_channel_leave_bridge(bridge_channel,
						BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
					break;
				}
			}
		}
		bridge_channel_internal_pull(bridge_channel);
		if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
			/*
			 * The channel died as a result of being pulled or it was
			 * kicked.  Leave it pointing to the original bridge.
			 */
			continue;
		}

		bridge_channel_moving(bridge_channel, bridge_channel->bridge, dst_bridge);

		/* Point to new bridge.*/
		bridge_channel_change_bridge(bridge_channel, dst_bridge);

		if (bridge_channel_internal_push(bridge_channel)) {
			ast_bridge_features_remove(bridge_channel->features,
				AST_BRIDGE_HOOK_REMOVE_ON_PULL);
			ast_bridge_channel_leave_bridge(bridge_channel,
				BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, bridge_channel->bridge->cause);
		}
	}
	AST_LIST_TRAVERSE_SAFE_END;

	if (kick_me) {
		/*
		 * Now we can kick any channels in the dst_bridge without
		 * potentially dissolving the bridge.
		 */
		for (idx = 0; idx < num_kick; ++idx) {
			bridge_channel = kick_me[idx];
			ast_bridge_channel_lock(bridge_channel);
			if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
				ast_bridge_channel_leave_bridge_nolock(bridge_channel,
					BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
				bridge_channel_internal_pull(bridge_channel);
			}
			ast_bridge_channel_unlock(bridge_channel);
		}
	}

	bridge_reconfigured(dst_bridge, !optimized);
	bridge_reconfigured(src_bridge, !optimized);

	ast_debug(1, "Merged bridge %s into bridge %s\n",
		src_bridge->uniqueid, dst_bridge->uniqueid);
}

struct merge_direction {
	/*! Destination merge bridge. */
	struct ast_bridge *dest;
	/*! Source merge bridge. */
	struct ast_bridge *src;
};

/*!
 * \internal
 * \brief Determine which bridge should merge into the other.
 * \since 12.0.0
 *
 * \param bridge1 A bridge for merging
 * \param bridge2 A bridge for merging
 *
 * \note The two bridges are assumed already locked.
 *
 * \return Which bridge merges into which or NULL bridges if cannot merge.
 */
static struct merge_direction bridge_merge_determine_direction(struct ast_bridge *bridge1, struct ast_bridge *bridge2)
{
	struct merge_direction merge = { NULL, NULL };
	int bridge1_priority;
	int bridge2_priority;

	if (!ast_test_flag(&bridge1->feature_flags,
			AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)
		&& !ast_test_flag(&bridge2->feature_flags,
			AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
		/*
		 * Can merge either way.  Merge to the higher priority merge
		 * bridge.  Otherwise merge to the larger bridge.
		 */
		bridge1_priority = bridge1->v_table->get_merge_priority(bridge1);
		bridge2_priority = bridge2->v_table->get_merge_priority(bridge2);
		if (bridge2_priority < bridge1_priority) {
			merge.dest = bridge1;
			merge.src = bridge2;
		} else if (bridge1_priority < bridge2_priority) {
			merge.dest = bridge2;
			merge.src = bridge1;
		} else {
			/* Merge to the larger bridge. */
			if (bridge2->num_channels <= bridge1->num_channels) {
				merge.dest = bridge1;
				merge.src = bridge2;
			} else {
				merge.dest = bridge2;
				merge.src = bridge1;
			}
		}
	} else if (!ast_test_flag(&bridge1->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
		&& !ast_test_flag(&bridge2->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
		/* Can merge only one way. */
		merge.dest = bridge1;
		merge.src = bridge2;
	} else if (!ast_test_flag(&bridge2->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
		&& !ast_test_flag(&bridge1->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
		/* Can merge only one way. */
		merge.dest = bridge2;
		merge.src = bridge1;
	}

	return merge;
}

/*!
 * \internal
 * \brief Merge two bridges together
 * \since 12.0.0
 *
 * \param dst_bridge Destination bridge of merge.
 * \param src_bridge Source bridge of merge.
 * \param merge_best_direction TRUE if don't care about which bridge merges into the other.
 * \param kick_me Array of channels to kick from the bridges.
 * \param num_kick Number of channels in the kick_me array.
 *
 * \note The dst_bridge and src_bridge are assumed already locked.
 *
 * \retval 0 on success
 * \retval -1 on failure
 */
static int bridge_merge_locked(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, int merge_best_direction, struct ast_channel **kick_me, unsigned int num_kick)
{
	struct merge_direction merge;
	struct ast_bridge_channel **kick_them = NULL;

	/* Sanity check. */
	ast_assert(dst_bridge && src_bridge && dst_bridge != src_bridge && (!num_kick || kick_me));

	if (dst_bridge->dissolved || src_bridge->dissolved) {
		ast_debug(1, "Can't merge bridges %s and %s, at least one bridge is dissolved.\n",
			src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}
	if (ast_test_flag(&dst_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)
		|| ast_test_flag(&src_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
		ast_debug(1, "Can't merge bridges %s and %s, masquerade only.\n",
			src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}
	if (dst_bridge->inhibit_merge || src_bridge->inhibit_merge) {
		ast_debug(1, "Can't merge bridges %s and %s, merging temporarily inhibited.\n",
			src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}

	if (merge_best_direction) {
		merge = bridge_merge_determine_direction(dst_bridge, src_bridge);
	} else {
		merge.dest = dst_bridge;
		merge.src = src_bridge;
	}

	if (!merge.dest
		|| ast_test_flag(&merge.dest->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
		|| ast_test_flag(&merge.src->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
		ast_debug(1, "Can't merge bridges %s and %s, merging inhibited.\n",
			src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}
	if (merge.src->num_channels < 2) {
		/*
		 * For a two party bridge, a channel may be temporarily removed
		 * from the source bridge or the initial bridge members have not
		 * joined yet.
		 */
		ast_debug(1, "Can't merge bridge %s into bridge %s, not enough channels in source bridge.\n",
			merge.src->uniqueid, merge.dest->uniqueid);
		return -1;
	}
	if (2 + num_kick < merge.dest->num_channels + merge.src->num_channels
		&& !(merge.dest->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)
		&& (!ast_test_flag(&merge.dest->feature_flags, AST_BRIDGE_FLAG_SMART)
			|| !(merge.dest->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX))) {
		ast_debug(1, "Can't merge bridge %s into bridge %s, multimix is needed and it cannot be acquired.\n",
			merge.src->uniqueid, merge.dest->uniqueid);
		return -1;
	}

	if (num_kick) {
		unsigned int num_to_kick = 0;
		unsigned int idx;

		kick_them = ast_alloca(num_kick * sizeof(*kick_them));
		for (idx = 0; idx < num_kick; ++idx) {
			kick_them[num_to_kick] = bridge_find_channel(merge.src, kick_me[idx]);
			if (!kick_them[num_to_kick]) {
				kick_them[num_to_kick] = bridge_find_channel(merge.dest, kick_me[idx]);
			}
			if (kick_them[num_to_kick]) {
				++num_to_kick;
			}
		}

		if (num_to_kick != num_kick) {
			ast_debug(1, "Can't merge bridge %s into bridge %s, at least one kicked channel is not in either bridge.\n",
				merge.src->uniqueid, merge.dest->uniqueid);
			return -1;
		}
	}

	bridge_do_merge(merge.dest, merge.src, kick_them, num_kick, 0);
	return 0;
}

int ast_bridge_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, int merge_best_direction, struct ast_channel **kick_me, unsigned int num_kick)
{
	int res;

	/* Sanity check. */
	ast_assert(dst_bridge && src_bridge);

	ast_bridge_lock_both(dst_bridge, src_bridge);
	res = bridge_merge_locked(dst_bridge, src_bridge, merge_best_direction, kick_me, num_kick);
	ast_bridge_unlock(src_bridge);
	ast_bridge_unlock(dst_bridge);
	return res;
}

int bridge_do_move(struct ast_bridge *dst_bridge, struct ast_bridge_channel *bridge_channel, int attempt_recovery,
	unsigned int optimized)
{
	struct ast_bridge *orig_bridge;
	int was_in_bridge;
	int res = 0;

	if (bridge_channel->swap) {
		ast_debug(1, "Moving %p(%s) into bridge %s swapping with %s\n",
			bridge_channel, ast_channel_name(bridge_channel->chan), dst_bridge->uniqueid,
			ast_channel_name(bridge_channel->swap));
	} else {
		ast_debug(1, "Moving %p(%s) into bridge %s\n",
			bridge_channel, ast_channel_name(bridge_channel->chan), dst_bridge->uniqueid);
	}

	orig_bridge = bridge_channel->bridge;
	was_in_bridge = bridge_channel->in_bridge;

	bridge_channel_internal_pull(bridge_channel);
	if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
		/*
		 * The channel died as a result of being pulled.  Leave it
		 * pointing to the original bridge.
		 *
		 * Clear out the swap channel pointer.  A ref is not held
		 * by bridge_channel->swap at this point.
		 */
		bridge_channel->swap = NULL;
		bridge_reconfigured(orig_bridge, 0);
		return -1;
	}

	/* Point to new bridge.*/
	ao2_ref(orig_bridge, +1);/* Keep a ref in case the push fails. */
	bridge_channel_change_bridge(bridge_channel, dst_bridge);

	bridge_channel_moving(bridge_channel, orig_bridge, dst_bridge);

	if (bridge_channel_internal_push_full(bridge_channel, optimized)) {
		/* Try to put the channel back into the original bridge. */
		ast_bridge_features_remove(bridge_channel->features,
			AST_BRIDGE_HOOK_REMOVE_ON_PULL);
		if (attempt_recovery && was_in_bridge) {
			/* Point back to original bridge. */
			bridge_channel_change_bridge(bridge_channel, orig_bridge);

			if (bridge_channel_internal_push(bridge_channel)) {
				ast_bridge_features_remove(bridge_channel->features,
					AST_BRIDGE_HOOK_REMOVE_ON_PULL);
				ast_bridge_channel_leave_bridge(bridge_channel,
					BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, bridge_channel->bridge->cause);
			}
		} else {
			ast_bridge_channel_leave_bridge(bridge_channel,
				BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, bridge_channel->bridge->cause);
			bridge_channel_settle_owed_events(orig_bridge, bridge_channel);
		}
		res = -1;
	} else if (!optimized) {
		bridge_channel_settle_owed_events(orig_bridge, bridge_channel);
	}

	bridge_reconfigured(dst_bridge, !optimized);
	bridge_reconfigured(orig_bridge, !optimized);
	ao2_ref(orig_bridge, -1);
	return res;
}

/*!
 * \internal
 * \brief Move a channel from one bridge to another.
 * \since 12.0.0
 *
 * \param dst_bridge Destination bridge of bridge channel move.
 * \param src_bridge Source bridge of bridge channel move.
 * \param chan Channel to move.
 * \param swap Channel to replace in dst_bridge.
 * \param attempt_recovery TRUE if failure attempts to push channel back into original bridge.
 *
 * \note The dst_bridge and src_bridge are assumed already locked.
 *
 * \retval 0 on success.
 * \retval -1 on failure.
 */
static int bridge_move_locked(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery)
{
	struct ast_bridge_channel *bridge_channel;

	if (dst_bridge->dissolved || src_bridge->dissolved) {
		ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, at least one bridge is dissolved.\n",
			ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}
	if (ast_test_flag(&dst_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)
		|| ast_test_flag(&src_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
		ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, masquerade only.\n",
			ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}
	if (dst_bridge->inhibit_merge || src_bridge->inhibit_merge) {
		ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, temporarily inhibited.\n",
			ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}

	bridge_channel = bridge_find_channel(src_bridge, chan);
	if (!bridge_channel) {
		ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel not in bridge.\n",
			ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}
	if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
		ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel leaving bridge.\n",
			ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}
	if (ast_test_flag(&bridge_channel->features->feature_flags,
		AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE)) {
		ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel immovable.\n",
			ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
		return -1;
	}

	if (swap) {
		struct ast_bridge_channel *bridge_channel_swap;

		bridge_channel_swap = bridge_find_channel(dst_bridge, swap);
		if (!bridge_channel_swap) {
			ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, swap channel %s not in bridge.\n",
				ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid,
				ast_channel_name(swap));
			return -1;
		}
		if (bridge_channel_swap->state != BRIDGE_CHANNEL_STATE_WAIT) {
			ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, swap channel %s leaving bridge.\n",
				ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid,
				ast_channel_name(swap));
			return -1;
		}
	}

	bridge_channel->swap = swap;
	return bridge_do_move(dst_bridge, bridge_channel, attempt_recovery, 0);
}

int ast_bridge_move(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery)
{
	int res;

	ast_bridge_lock_both(dst_bridge, src_bridge);
	res = bridge_move_locked(dst_bridge, src_bridge, chan, swap, attempt_recovery);
	ast_bridge_unlock(src_bridge);
	ast_bridge_unlock(dst_bridge);
	return res;
}

int ast_bridge_add_channel(struct ast_bridge *bridge, struct ast_channel *chan,
	struct ast_bridge_features *features, int play_tone, const char *xfersound)
{
	RAII_VAR(struct ast_bridge *, chan_bridge, NULL, ao2_cleanup);
	RAII_VAR(struct ast_channel *, yanked_chan, NULL, ao2_cleanup);

	ast_moh_stop(chan);

	ast_channel_lock(chan);
	chan_bridge = ast_channel_get_bridge(chan);
	ast_channel_unlock(chan);

	if (chan_bridge) {
		struct ast_bridge_channel *bridge_channel;

		/* The channel is in a bridge so it is not getting any new features. */
		ast_bridge_features_destroy(features);

		ast_bridge_lock_both(bridge, chan_bridge);
		bridge_channel = bridge_find_channel(chan_bridge, chan);

		if (bridge_move_locked(bridge, chan_bridge, chan, NULL, 1)) {
			ast_bridge_unlock(chan_bridge);
			ast_bridge_unlock(bridge);
			return -1;
		}

		/*
		 * bridge_move_locked() will implicitly ensure that
		 * bridge_channel is not NULL.
		 */
		ast_assert(bridge_channel != NULL);

		/*
		 * Additional checks if the channel we just stole dissolves the
		 * original bridge.
		 */
		bridge_dissolve_check_stolen(chan_bridge, bridge_channel);
		ast_bridge_unlock(chan_bridge);
		ast_bridge_unlock(bridge);
	} else {
		/* Slightly less easy case. We need to yank channel A from
		 * where he currently is and impart him into our bridge.
		 */
		yanked_chan = ast_channel_yank(chan);
		if (!yanked_chan) {
			ast_log(LOG_WARNING, "Could not gain control of channel %s\n", ast_channel_name(chan));
			ast_bridge_features_destroy(features);
			return -1;
		}
		if (ast_channel_state(yanked_chan) != AST_STATE_UP) {
			ast_answer(yanked_chan);
		}
		ast_channel_ref(yanked_chan);
		if (ast_bridge_impart(bridge, yanked_chan, NULL, features,
			AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
			/* It is possible for us to yank a channel and have some other
			 * thread start a PBX on the channl after we yanked it. In particular,
			 * this can theoretically happen on the ;2 of a Local channel if we
			 * yank it prior to the ;1 being answered. Make sure that it isn't
			 * executing a PBX before hanging it up.
			 */
			if (ast_channel_pbx(yanked_chan)) {
				ast_channel_unref(yanked_chan);
			} else {
				ast_hangup(yanked_chan);
			}
			return -1;
		}
	}

	if (play_tone && !ast_strlen_zero(xfersound)) {
		struct ast_channel *play_chan = yanked_chan ?: chan;
		RAII_VAR(struct ast_bridge_channel *, play_bridge_channel, NULL, ao2_cleanup);

		ast_channel_lock(play_chan);
		play_bridge_channel = ast_channel_get_bridge_channel(play_chan);
		ast_channel_unlock(play_chan);

		if (!play_bridge_channel) {
			ast_log(LOG_WARNING, "Unable to play tone for channel %s. No longer in a bridge.\n",
				ast_channel_name(play_chan));
		} else {
			ast_bridge_channel_queue_playfile(play_bridge_channel, NULL, xfersound, NULL);
		}
	}
	return 0;
}

static int bridge_allows_optimization(struct ast_bridge *bridge)
{
	return !(bridge->inhibit_merge
		|| bridge->dissolved
		|| ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY));
}

/*!
 * \internal
 * \brief Lock the unreal channel stack for chan and prequalify it.
 * \since 12.0.0
 *
 * \param chan Unreal channel writing a frame into the channel driver.
 *
 * \note It is assumed that chan is already locked.
 *
 * \retval bridge on success with bridge and bridge_channel locked.
 * \retval NULL if cannot do optimization now.
 */
static struct ast_bridge *optimize_lock_chan_stack(struct ast_channel *chan)
{
	struct ast_bridge *bridge;
	struct ast_bridge_channel *bridge_channel;

	if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
		return NULL;
	}
	if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
		return NULL;
	}
	if (ast_channel_has_audio_frame_or_monitor(chan)) {
		/* Channel has an active monitor, audiohook, or framehook. */
		return NULL;
	}
	bridge_channel = ast_channel_internal_bridge_channel(chan);
	if (!bridge_channel || ast_bridge_channel_trylock(bridge_channel)) {
		return NULL;
	}
	bridge = bridge_channel->bridge;
	if (bridge_channel->activity != BRIDGE_CHANNEL_THREAD_SIMPLE
		|| bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
		|| ast_bridge_trylock(bridge)) {
		ast_bridge_channel_unlock(bridge_channel);
		return NULL;
	}
	if (!bridge_channel_internal_allows_optimization(bridge_channel) ||
			!bridge_allows_optimization(bridge)) {
		ast_bridge_unlock(bridge);
		ast_bridge_channel_unlock(bridge_channel);
		return NULL;
	}
	return bridge;
}

/*!
 * \internal
 * \brief Lock the unreal channel stack for peer and prequalify it.
 * \since 12.0.0
 *
 * \param peer Other unreal channel in the pair.
 *
 * \retval bridge on success with bridge, bridge_channel, and peer locked.
 * \retval NULL if cannot do optimization now.
 */
static struct ast_bridge *optimize_lock_peer_stack(struct ast_channel *peer)
{
	struct ast_bridge *bridge;
	struct ast_bridge_channel *bridge_channel;

	if (ast_channel_trylock(peer)) {
		return NULL;
	}
	if (!AST_LIST_EMPTY(ast_channel_readq(peer))) {
		ast_channel_unlock(peer);
		return NULL;
	}
	if (ast_test_flag(ast_channel_flags(peer), AST_FLAG_EMULATE_DTMF)) {
		ast_channel_unlock(peer);
		return NULL;
	}
	if (ast_channel_has_audio_frame_or_monitor(peer)) {
		/* Peer has an active monitor, audiohook, or framehook. */
		ast_channel_unlock(peer);
		return NULL;
	}
	bridge_channel = ast_channel_internal_bridge_channel(peer);
	if (!bridge_channel || ast_bridge_channel_trylock(bridge_channel)) {
		ast_channel_unlock(peer);
		return NULL;
	}
	bridge = bridge_channel->bridge;
	if (bridge_channel->activity != BRIDGE_CHANNEL_THREAD_IDLE
		|| bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
		|| ast_bridge_trylock(bridge)) {
		ast_bridge_channel_unlock(bridge_channel);
		ast_channel_unlock(peer);
		return NULL;
	}
	if (!bridge_allows_optimization(bridge) ||
			!bridge_channel_internal_allows_optimization(bridge_channel)) {
		ast_bridge_unlock(bridge);
		ast_bridge_channel_unlock(bridge_channel);
		ast_channel_unlock(peer);
		return NULL;
	}
	return bridge;
}

/*!
 * \internal
 * \brief Indicates allowability of a swap optimization
 */
enum bridge_allow_swap {
	/*! Bridges cannot allow for a swap optimization to occur */
	SWAP_PROHIBITED,
	/*! Bridge swap optimization can occur into the chan_bridge */
	SWAP_TO_CHAN_BRIDGE,
	/*! Bridge swap optimization can occur into the peer_bridge */
	SWAP_TO_PEER_BRIDGE,
};

/*!
 * \internal
 * \brief Determine if two bridges allow for swap optimization to occur
 *
 * \param chan_bridge First bridge being tested
 * \param peer_bridge Second bridge being tested
 * \return Allowability of swap optimization
 */
static enum bridge_allow_swap bridges_allow_swap_optimization(struct ast_bridge *chan_bridge,
		struct ast_bridge *peer_bridge)
{
	int chan_priority;
	int peer_priority;

	if (!ast_test_flag(&chan_bridge->feature_flags,
			AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM |
			AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
		&& !ast_test_flag(&peer_bridge->feature_flags,
			AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM |
			AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)) {
		/*
		 * Can swap either way.  Swap to the higher priority merge
		 * bridge.
		 */
		chan_priority = chan_bridge->v_table->get_merge_priority(chan_bridge);
		peer_priority = peer_bridge->v_table->get_merge_priority(peer_bridge);
		if (chan_bridge->num_channels == 2
			&& chan_priority <= peer_priority) {
			return SWAP_TO_PEER_BRIDGE;
		} else if (peer_bridge->num_channels == 2
			&& peer_priority <= chan_priority) {
			return SWAP_TO_CHAN_BRIDGE;
		}
	} else if (chan_bridge->num_channels == 2
		&& !ast_test_flag(&chan_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
		&& !ast_test_flag(&peer_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_TO)) {
		/* Can swap optimize only one way. */
		return SWAP_TO_PEER_BRIDGE;
	} else if (peer_bridge->num_channels == 2
		&& !ast_test_flag(&peer_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
		&& !ast_test_flag(&chan_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_TO)) {
		/* Can swap optimize only one way. */
		return SWAP_TO_CHAN_BRIDGE;
	}

	return SWAP_PROHIBITED;
}

/*!
 * \internal
 * \brief Check and attempt to swap optimize out the unreal channels.
 * \since 12.0.0
 *
 * \param chan_bridge
 * \param chan_bridge_channel
 * \param peer_bridge
 * \param peer_bridge_channel
 * \param pvt Unreal data containing callbacks to call if the optimization actually
 * happens
 *
 * \retval 1 if unreal channels failed to optimize out.
 * \retval 0 if unreal channels were not optimized out.
 * \retval -1 if unreal channels were optimized out.
 */
static int try_swap_optimize_out(struct ast_bridge *chan_bridge,
	struct ast_bridge_channel *chan_bridge_channel, struct ast_bridge *peer_bridge,
	struct ast_bridge_channel *peer_bridge_channel,
	struct ast_unreal_pvt *pvt)
{
	struct ast_bridge *dst_bridge;
	struct ast_bridge_channel *dst_bridge_channel;
	struct ast_bridge_channel *src_bridge_channel;
	struct ast_bridge_channel *other;
	int res = 1;

	switch (bridges_allow_swap_optimization(chan_bridge, peer_bridge)) {
	case SWAP_TO_CHAN_BRIDGE:
		dst_bridge = chan_bridge;
		dst_bridge_channel = chan_bridge_channel;
		src_bridge_channel = peer_bridge_channel;
		break;
	case SWAP_TO_PEER_BRIDGE:
		dst_bridge = peer_bridge;
		dst_bridge_channel = peer_bridge_channel;
		src_bridge_channel = chan_bridge_channel;
		break;
	case SWAP_PROHIBITED:
	default:
		return 0;
	}

	other = ast_bridge_channel_peer(src_bridge_channel);
	if (other && other->state == BRIDGE_CHANNEL_STATE_WAIT) {
		unsigned int id;

		if (ast_channel_trylock(other->chan)) {
			return 1;
		}

		id = ast_atomic_fetchadd_int((int *) &optimization_id, +1);

		ast_verb(4, "Move-swap optimizing %s <-- %s.\n",
			ast_channel_name(dst_bridge_channel->chan),
			ast_channel_name(other->chan));

		if (pvt && !ast_test_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN) && pvt->callbacks
				&& pvt->callbacks->optimization_started) {
			pvt->callbacks->optimization_started(pvt, other->chan,
					dst_bridge_channel->chan == pvt->owner ? AST_UNREAL_OWNER : AST_UNREAL_CHAN,
					id);
			ast_set_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN);
		}
		other->swap = dst_bridge_channel->chan;
		if (!bridge_do_move(dst_bridge, other, 1, 1)) {
			ast_bridge_channel_leave_bridge(src_bridge_channel,
				BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
			res = -1;
		}
		if (pvt && pvt->callbacks && pvt->callbacks->optimization_finished) {
			pvt->callbacks->optimization_finished(pvt, res == 1, id);
		}
		ast_channel_unlock(other->chan);
	}
	return res;
}

/*!
 * \internal
 * \brief Indicates allowability of a merge optimization
 */
enum bridge_allow_merge {
	/*! Bridge properties prohibit merge optimization */
	MERGE_PROHIBITED,
	/*! Merge optimization cannot occur because the source bridge has too few channels */
	MERGE_NOT_ENOUGH_CHANNELS,
	/*! Merge optimization cannot occur because multimix capability could not be requested */
	MERGE_NO_MULTIMIX,
	/*! Merge optimization allowed between bridges */
	MERGE_ALLOWED,
};

/*!
 * \internal
 * \brief Determines allowability of a merge optimization
 *
 * \note The merge output parameter is undefined if MERGE_PROHIBITED is returned. For success
 * and other failure returns, a merge direction was determined, and the parameter is safe to
 * access.
 *
 * \param chan_bridge First bridge being tested
 * \param peer_bridge Second bridge being tested
 * \param num_kick_channels The number of channels to remove from the bridges during merging
 * \param[out] merge Indicates the recommended direction for the bridge merge
 */
static enum bridge_allow_merge bridges_allow_merge_optimization(struct ast_bridge *chan_bridge,
		struct ast_bridge *peer_bridge, int num_kick_channels, struct merge_direction *merge)
{
	*merge = bridge_merge_determine_direction(chan_bridge, peer_bridge);
	if (!merge->dest) {
		return MERGE_PROHIBITED;
	}
	if (merge->src->num_channels < 2) {
		return MERGE_NOT_ENOUGH_CHANNELS;
	} else if ((2 + num_kick_channels) < merge->dest->num_channels + merge->src->num_channels
		&& !(merge->dest->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)
		&& (!ast_test_flag(&merge->dest->feature_flags, AST_BRIDGE_FLAG_SMART)
			|| !(merge->dest->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX))) {
		return MERGE_NO_MULTIMIX;
	}

	return MERGE_ALLOWED;
}

/*!
 * \internal
 * \brief Check and attempt to merge optimize out the unreal channels.
 * \since 12.0.0
 *
 * \param chan_bridge
 * \param chan_bridge_channel
 * \param peer_bridge
 * \param peer_bridge_channel
 * \param pvt Unreal data containing callbacks to call if the optimization actually
 * happens
 *
 * \retval 0 if unreal channels were not optimized out.
 * \retval -1 if unreal channels were optimized out.
 */
static int try_merge_optimize_out(struct ast_bridge *chan_bridge,
	struct ast_bridge_channel *chan_bridge_channel, struct ast_bridge *peer_bridge,
	struct ast_bridge_channel *peer_bridge_channel,
	struct ast_unreal_pvt *pvt)
{
	struct merge_direction merge;
	struct ast_bridge_channel *kick_me[] = {
		chan_bridge_channel,
		peer_bridge_channel,
	};
	unsigned int id;

	switch (bridges_allow_merge_optimization(chan_bridge, peer_bridge, ARRAY_LEN(kick_me), &merge)) {
	case MERGE_ALLOWED:
		break;
	case MERGE_PROHIBITED:
		return 0;
	case MERGE_NOT_ENOUGH_CHANNELS:
		ast_debug(4, "Can't optimize %s -- %s out, not enough channels in bridge %s.\n",
			ast_channel_name(chan_bridge_channel->chan),
			ast_channel_name(peer_bridge_channel->chan),
			merge.src->uniqueid);
		return 0;
	case MERGE_NO_MULTIMIX:
		ast_debug(4, "Can't optimize %s -- %s out, multimix is needed and it cannot be acquired.\n",
			ast_channel_name(chan_bridge_channel->chan),
			ast_channel_name(peer_bridge_channel->chan));
		return 0;
	}

	ast_verb(4, "Merge optimizing %s -- %s out.\n",
		ast_channel_name(chan_bridge_channel->chan),
		ast_channel_name(peer_bridge_channel->chan));

	id = ast_atomic_fetchadd_int((int *) &optimization_id, +1);

	if (pvt && !ast_test_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN) && pvt->callbacks
			&& pvt->callbacks->optimization_started) {
		pvt->callbacks->optimization_started(pvt, NULL,
				merge.dest == ast_channel_internal_bridge(pvt->owner) ? AST_UNREAL_OWNER : AST_UNREAL_CHAN,
				id);
		ast_set_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN);
	}
	bridge_do_merge(merge.dest, merge.src, kick_me, ARRAY_LEN(kick_me), 1);
	if (pvt && pvt->callbacks && pvt->callbacks->optimization_finished) {
		pvt->callbacks->optimization_finished(pvt, 1, id);
	}

	return -1;
}

int ast_bridge_unreal_optimize_out(struct ast_channel *chan, struct ast_channel *peer, struct ast_unreal_pvt *pvt)
{
	struct ast_bridge *chan_bridge;
	struct ast_bridge *peer_bridge;
	struct ast_bridge_channel *chan_bridge_channel;
	struct ast_bridge_channel *peer_bridge_channel;
	int res = 0;

	chan_bridge = optimize_lock_chan_stack(chan);
	if (!chan_bridge) {
		return res;
	}
	chan_bridge_channel = ast_channel_internal_bridge_channel(chan);

	peer_bridge = optimize_lock_peer_stack(peer);
	if (peer_bridge) {
		peer_bridge_channel = ast_channel_internal_bridge_channel(peer);

		res = try_swap_optimize_out(chan_bridge, chan_bridge_channel,
			peer_bridge, peer_bridge_channel, pvt);
		if (!res) {
			res = try_merge_optimize_out(chan_bridge, chan_bridge_channel,
				peer_bridge, peer_bridge_channel, pvt);
		} else if (0 < res) {
			res = 0;
		}

		/* Release peer locks. */
		ast_bridge_unlock(peer_bridge);
		ast_bridge_channel_unlock(peer_bridge_channel);
		ast_channel_unlock(peer);
	}

	/* Release chan locks. */
	ast_bridge_unlock(chan_bridge);
	ast_bridge_channel_unlock(chan_bridge_channel);

	return res;
}

enum ast_bridge_optimization ast_bridges_allow_optimization(struct ast_bridge *chan_bridge,
		struct ast_bridge *peer_bridge)
{
	struct merge_direction merge;

	if (!bridge_allows_optimization(chan_bridge) || !bridge_allows_optimization(peer_bridge)) {
		return AST_BRIDGE_OPTIMIZE_PROHIBITED;
	}

	switch (bridges_allow_swap_optimization(chan_bridge, peer_bridge)) {
	case SWAP_TO_CHAN_BRIDGE:
		return AST_BRIDGE_OPTIMIZE_SWAP_TO_CHAN_BRIDGE;
	case SWAP_TO_PEER_BRIDGE:
		return AST_BRIDGE_OPTIMIZE_SWAP_TO_PEER_BRIDGE;
	case SWAP_PROHIBITED:
	default:
		break;
	}

	/* Two channels will be kicked from the bridges, the unreal;1 and unreal;2 channels */
	if (bridges_allow_merge_optimization(chan_bridge, peer_bridge, 2, &merge) != MERGE_ALLOWED) {
		return AST_BRIDGE_OPTIMIZE_PROHIBITED;
	}

	if (merge.dest == chan_bridge) {
		return AST_BRIDGE_OPTIMIZE_MERGE_TO_CHAN_BRIDGE;
	} else {
		return AST_BRIDGE_OPTIMIZE_MERGE_TO_PEER_BRIDGE;
	}
}

/*!
 * \internal
 * \brief Adjust the bridge merge inhibit request count.
 * \since 12.0.0
 *
 * \param bridge What to operate on.
 * \param request Inhibit request increment.
 *     (Positive to add requests.  Negative to remove requests.)
 *
 * \note This function assumes bridge is locked.
 *
 * \return Nothing
 */
void bridge_merge_inhibit_nolock(struct ast_bridge *bridge, int request)
{
	int new_request;

	new_request = bridge->inhibit_merge + request;
	ast_assert(0 <= new_request);
	bridge->inhibit_merge = new_request;
}

void ast_bridge_merge_inhibit(struct ast_bridge *bridge, int request)
{
	ast_bridge_lock(bridge);
	bridge_merge_inhibit_nolock(bridge, request);
	ast_bridge_unlock(bridge);
}

int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;
/* XXX ASTERISK-21271 the case of a disolved bridge while channel is suspended is not handled. */
/* XXX ASTERISK-21271 suspend/unsuspend needs to be rethought. The caller must block until it has successfully suspended the channel for temporary control. */
/* XXX ASTERISK-21271 external suspend/unsuspend needs to be eliminated. The channel may be playing a file at the time and stealing it then is not good. */

	ast_bridge_lock(bridge);

	if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
		ast_bridge_unlock(bridge);
		return -1;
	}

	bridge_channel_internal_suspend_nolock(bridge_channel);

	ast_bridge_unlock(bridge);

	return 0;
}

int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_bridge_channel *bridge_channel;
/* XXX ASTERISK-21271 the case of a disolved bridge while channel is suspended is not handled. */

	ast_bridge_lock(bridge);

	if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
		ast_bridge_unlock(bridge);
		return -1;
	}

	bridge_channel_internal_unsuspend_nolock(bridge_channel);

	ast_bridge_unlock(bridge);

	return 0;
}

void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
{
	technology->suspended = 1;
}

void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
{
	/*
	 * XXX We may want the act of unsuspending a bridge technology
	 * to prod all existing bridges to see if they should start
	 * using it.
	 */
	technology->suspended = 0;
}

int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_hook_callback callback, const char *dtmf)
{
	if (ARRAY_LEN(builtin_features_handlers) <= feature
		|| builtin_features_handlers[feature]) {
		return -1;
	}

	if (!ast_strlen_zero(dtmf)) {
		ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
	}

	builtin_features_handlers[feature] = callback;

	return 0;
}

int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
{
	if (ARRAY_LEN(builtin_features_handlers) <= feature
		|| !builtin_features_handlers[feature]) {
		return -1;
	}

	builtin_features_handlers[feature] = NULL;

	return 0;
}

int ast_bridge_features_do(enum ast_bridge_builtin_feature feature, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
{
	ast_bridge_hook_callback callback;

	if (ARRAY_LEN(builtin_features_handlers) <= feature) {
		return -1;
	}

	callback = builtin_features_handlers[feature];
	if (!callback) {
		return -1;
	}
	callback(bridge_channel, hook_pvt);

	return 0;
}

int ast_bridge_interval_register(enum ast_bridge_builtin_interval interval, ast_bridge_builtin_set_limits_fn callback)
{
	if (ARRAY_LEN(builtin_interval_handlers) <= interval
		|| builtin_interval_handlers[interval]) {
		return -1;
	}

	builtin_interval_handlers[interval] = callback;

	return 0;
}

int ast_bridge_interval_unregister(enum ast_bridge_builtin_interval interval)
{
	if (ARRAY_LEN(builtin_interval_handlers) <= interval
		|| !builtin_interval_handlers[interval]) {
		return -1;
	}

	builtin_interval_handlers[interval] = NULL;

	return 0;

}

/*!
 * \internal
 * \brief Bridge hook destructor.
 * \since 12.0.0
 *
 * \param vhook Object to destroy.
 *
 * \return Nothing
 */
static void bridge_hook_destroy(void *vhook)
{
	struct ast_bridge_hook *hook = vhook;

	if (hook->destructor) {
		hook->destructor(hook->hook_pvt);
	}
}

/*!
 * \internal
 * \brief Allocate and setup a generic bridge hook.
 * \since 12.0.0
 *
 * \param size How big an object to allocate.
 * \param callback Function to execute upon activation
 * \param hook_pvt Unique data
 * \param destructor Optional destructor callback for hook_pvt data
 * \param remove_flags Dictates what situations the hook should be removed.
 *
 * \retval hook on success.
 * \retval NULL on error.
 */
static struct ast_bridge_hook *bridge_hook_generic(size_t size,
	ast_bridge_hook_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	struct ast_bridge_hook *hook;

	/* Allocate new hook and setup it's basic variables */
	hook = ao2_alloc_options(size, bridge_hook_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
	if (hook) {
		hook->callback = callback;
		hook->destructor = destructor;
		hook->hook_pvt = hook_pvt;
		ast_set_flag(&hook->remove_flags, remove_flags);
	}

	return hook;
}

int ast_bridge_dtmf_hook(struct ast_bridge_features *features,
	const char *dtmf,
	ast_bridge_hook_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	struct ast_bridge_hook_dtmf *hook;
	int res;

	/* Allocate new hook and setup it's various variables */
	hook = (struct ast_bridge_hook_dtmf *) bridge_hook_generic(sizeof(*hook), callback,
		hook_pvt, destructor, remove_flags);
	if (!hook) {
		return -1;
	}
	hook->generic.type = AST_BRIDGE_HOOK_TYPE_DTMF;
	ast_copy_string(hook->dtmf.code, dtmf, sizeof(hook->dtmf.code));

	/* Once done we put it in the container. */
	res = ao2_link(features->dtmf_hooks, hook) ? 0 : -1;
	if (res) {
		/*
		 * Could not link the hook into the container.
		 *
		 * Remove the hook_pvt destructor call from the hook since we
		 * are returning failure to install the hook.
		 */
		hook->generic.destructor = NULL;
	}
	ao2_ref(hook, -1);

	return res;
}

/*!
 * \internal
 * \brief Attach an other hook to a bridge features structure
 *
 * \param features Bridge features structure
 * \param callback Function to execute upon activation
 * \param hook_pvt Unique data
 * \param destructor Optional destructor callback for hook_pvt data
 * \param remove_flags Dictates what situations the hook should be removed.
 * \param type What type of hook is being attached.
 *
 * \retval 0 on success
 * \retval -1 on failure (The caller must cleanup any hook_pvt resources.)
 */
static int bridge_other_hook(struct ast_bridge_features *features,
	ast_bridge_hook_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags,
	enum ast_bridge_hook_type type)
{
	struct ast_bridge_hook *hook;
	int res;

	/* Allocate new hook and setup it's various variables */
	hook = bridge_hook_generic(sizeof(*hook), callback, hook_pvt, destructor,
		remove_flags);
	if (!hook) {
		return -1;
	}
	hook->type = type;

	/* Once done we put it in the container. */
	res = ao2_link(features->other_hooks, hook) ? 0 : -1;
	if (res) {
		/*
		 * Could not link the hook into the container.
		 *
		 * Remove the hook_pvt destructor call from the hook since we
		 * are returning failure to install the hook.
		 */
		hook->destructor = NULL;
	}
	ao2_ref(hook, -1);

	return res;
}

int ast_bridge_hangup_hook(struct ast_bridge_features *features,
	ast_bridge_hook_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
		AST_BRIDGE_HOOK_TYPE_HANGUP);
}

int ast_bridge_join_hook(struct ast_bridge_features *features,
	ast_bridge_hook_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
		AST_BRIDGE_HOOK_TYPE_JOIN);
}

int ast_bridge_leave_hook(struct ast_bridge_features *features,
	ast_bridge_hook_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
		AST_BRIDGE_HOOK_TYPE_LEAVE);
}

int ast_bridge_talk_detector_hook(struct ast_bridge_features *features,
	ast_bridge_talking_indicate_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	ast_bridge_hook_callback hook_cb = (ast_bridge_hook_callback) callback;

	return bridge_other_hook(features, hook_cb, hook_pvt, destructor, remove_flags,
		AST_BRIDGE_HOOK_TYPE_TALK);
}

int ast_bridge_move_hook(struct ast_bridge_features *features,
	ast_bridge_move_indicate_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	ast_bridge_hook_callback hook_cb = (ast_bridge_hook_callback) callback;

	return bridge_other_hook(features, hook_cb, hook_pvt, destructor, remove_flags,
		AST_BRIDGE_HOOK_TYPE_MOVE);
}

int ast_bridge_interval_hook(struct ast_bridge_features *features,
	enum ast_bridge_hook_timer_option flags,
	unsigned int interval,
	ast_bridge_hook_callback callback,
	void *hook_pvt,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	struct ast_bridge_hook_timer *hook;
	int res;

	if (!features ||!interval || !callback) {
		return -1;
	}

	/* Allocate new hook and setup it's various variables */
	hook = (struct ast_bridge_hook_timer *) bridge_hook_generic(sizeof(*hook), callback,
		hook_pvt, destructor, remove_flags);
	if (!hook) {
		return -1;
	}
	hook->generic.type = AST_BRIDGE_HOOK_TYPE_TIMER;
	hook->timer.interval = interval;
	hook->timer.trip_time = ast_tvadd(ast_tvnow(), ast_samp2tv(interval, 1000));
	hook->timer.seqno = ast_atomic_fetchadd_int((int *) &features->interval_sequence, +1);
	hook->timer.flags = flags;

	ast_debug(1, "Putting interval hook %p with interval %u in the heap on features %p\n",
		hook, hook->timer.interval, features);
	ast_heap_wrlock(features->interval_hooks);
	res = ast_heap_push(features->interval_hooks, hook);
	ast_heap_unlock(features->interval_hooks);
	if (res) {
		/*
		 * Could not push the hook into the heap
		 *
		 * Remove the hook_pvt destructor call from the hook since we
		 * are returning failure to install the hook.
		 */
		hook->generic.destructor = NULL;
		ao2_ref(hook, -1);
	}

	return res ? -1 : 0;
}

int ast_bridge_features_enable(struct ast_bridge_features *features,
	enum ast_bridge_builtin_feature feature,
	const char *dtmf,
	void *config,
	ast_bridge_hook_pvt_destructor destructor,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	if (ARRAY_LEN(builtin_features_handlers) <= feature
		|| !builtin_features_handlers[feature]) {
		return -1;
	}

	/* If no alternate DTMF stream was provided use the default one */
	if (ast_strlen_zero(dtmf)) {
		dtmf = builtin_features_dtmf[feature];
		/* If no DTMF is still available (ie: it has been disabled) then error out now */
		if (ast_strlen_zero(dtmf)) {
			ast_debug(1, "Failed to enable built in feature %u on %p, no DTMF string is available for it.\n",
				feature, features);
			return -1;
		}
	}

	/*
	 * The rest is basically pretty easy.  We create another hook
	 * using the built in feature's DTMF callback.  Easy as pie.
	 */
	return ast_bridge_dtmf_hook(features, dtmf, builtin_features_handlers[feature],
		config, destructor, remove_flags);
}

int ast_bridge_features_limits_construct(struct ast_bridge_features_limits *limits)
{
	memset(limits, 0, sizeof(*limits));

	if (ast_string_field_init(limits, 256)) {
		return -1;
	}

	return 0;
}

void ast_bridge_features_limits_destroy(struct ast_bridge_features_limits *limits)
{
	ast_string_field_free_memory(limits);
}

int ast_bridge_features_set_limits(struct ast_bridge_features *features,
	struct ast_bridge_features_limits *limits,
	enum ast_bridge_hook_remove_flags remove_flags)
{
	if (builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_LIMITS]) {
		ast_bridge_builtin_set_limits_fn callback;

		callback = builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_LIMITS];
		return callback(features, limits, remove_flags);
	}

	ast_log(LOG_ERROR, "Attempted to set limits without an AST_BRIDGE_BUILTIN_INTERVAL_LIMITS callback registered.\n");
	return -1;
}

void ast_bridge_features_set_flag(struct ast_bridge_features *features, unsigned int flag)
{
	ast_set_flag(&features->feature_flags, flag);
	features->usable = 1;
}

/*!
 * \internal
 * \brief ao2 object match hooks with appropriate remove_flags.
 * \since 12.0.0
 *
 * \param obj Feature hook object.
 * \param arg Removal flags
 * \param flags Not used
 *
 * \retval CMP_MATCH if hook's remove_flags match the removal flags set.
 * \retval 0 if not match.
 */
static int hook_remove_match(void *obj, void *arg, int flags)
{
	struct ast_bridge_hook *hook = obj;
	enum ast_bridge_hook_remove_flags *remove_flags = arg;

	if (ast_test_flag(&hook->remove_flags, *remove_flags)) {
		return CMP_MATCH;
	} else {
		return 0;
	}
}

/*!
 * \internal
 * \brief Remove all hooks with appropriate remove_flags in the container.
 * \since 12.0.0
 *
 * \param hooks Hooks container to work on.
 * \param remove_flags Determinator for whether hook is removed
 *
 * \return Nothing
 */
static void hooks_remove_container(struct ao2_container *hooks, enum ast_bridge_hook_remove_flags remove_flags)
{
	ao2_callback(hooks, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE,
		hook_remove_match, &remove_flags);
}

/*!
 * \internal
 * \brief Remove all hooks in the heap with appropriate remove_flags set.
 * \since 12.0.0
 *
 * \param hooks Hooks heap to work on.
 * \param remove_flags Determinator for whether hook is removed
 *
 * \return Nothing
 */
static void hooks_remove_heap(struct ast_heap *hooks, enum ast_bridge_hook_remove_flags remove_flags)
{
	struct ast_bridge_hook *hook;
	int changed;

	ast_heap_wrlock(hooks);
	do {
		int idx;

		changed = 0;
		for (idx = ast_heap_size(hooks); idx; --idx) {
			hook = ast_heap_peek(hooks, idx);
			if (ast_test_flag(&hook->remove_flags, remove_flags)) {
				ast_heap_remove(hooks, hook);
				ao2_ref(hook, -1);
				changed = 1;
			}
		}
	} while (changed);
	ast_heap_unlock(hooks);
}

void ast_bridge_features_remove(struct ast_bridge_features *features, enum ast_bridge_hook_remove_flags remove_flags)
{
	hooks_remove_container(features->dtmf_hooks, remove_flags);
	hooks_remove_container(features->other_hooks, remove_flags);
	hooks_remove_heap(features->interval_hooks, remove_flags);
}

static int interval_hook_time_cmp(void *a, void *b)
{
	struct ast_bridge_hook_timer *hook_a = a;
	struct ast_bridge_hook_timer *hook_b = b;
	int cmp;

	cmp = ast_tvcmp(hook_b->timer.trip_time, hook_a->timer.trip_time);
	if (cmp) {
		return cmp;
	}

	cmp = hook_b->timer.seqno - hook_a->timer.seqno;
	return cmp;
}

/*!
 * \internal
 * \brief DTMF hook container sort comparison function.
 * \since 12.0.0
 *
 * \param obj_left pointer to the (user-defined part) of an object.
 * \param obj_right pointer to the (user-defined part) of an object.
 * \param flags flags from ao2_callback()
 *   OBJ_POINTER - if set, 'obj_right', is an object.
 *   OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
 *   OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
 *
 * \retval <0 if obj_left < obj_right
 * \retval =0 if obj_left == obj_right
 * \retval >0 if obj_left > obj_right
 */
static int bridge_dtmf_hook_sort(const void *obj_left, const void *obj_right, int flags)
{
	const struct ast_bridge_hook_dtmf *hook_left = obj_left;
	const struct ast_bridge_hook_dtmf *hook_right = obj_right;
	const char *right_key = obj_right;
	int cmp;

	switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
	default:
	case OBJ_POINTER:
		right_key = hook_right->dtmf.code;
		/* Fall through */
	case OBJ_KEY:
		cmp = strcasecmp(hook_left->dtmf.code, right_key);
		break;
	case OBJ_PARTIAL_KEY:
		cmp = strncasecmp(hook_left->dtmf.code, right_key, strlen(right_key));
		break;
	}
	return cmp;
}

/*! \brief Callback for merging hook ao2_containers */
static int merge_container_cb(void *obj, void *data, int flags)
{
	ao2_link(data, obj);
	return 0;
}

/*! \brief Wrapper for interval hooks that calls into the wrapped hook */
static int interval_wrapper_cb(struct ast_bridge_channel *bridge_channel, void *obj)
{
	struct ast_bridge_hook_timer *hook = obj;

	return hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
}

/*! \brief Destructor for the hook wrapper */
static void interval_wrapper_pvt_dtor(void *obj)
{
	ao2_cleanup(obj);
}

/*! \brief Wrap the provided interval hook and add it to features */
static void wrap_hook(struct ast_bridge_features *features, struct ast_bridge_hook_timer *hook)
{
	/* Break out of the current wrapper if it exists to avoid multiple layers */
	if (hook->generic.callback == interval_wrapper_cb) {
		hook = hook->generic.hook_pvt;
	}

	ast_bridge_interval_hook(features, hook->timer.flags, hook->timer.interval,
		interval_wrapper_cb, ao2_bump(hook), interval_wrapper_pvt_dtor,
		hook->generic.remove_flags.flags);
}

void ast_bridge_features_merge(struct ast_bridge_features *into, const struct ast_bridge_features *from)
{
	struct ast_bridge_hook_timer *hook;
	int idx;

	/* Merge hook containers */
	ao2_callback(from->dtmf_hooks, 0, merge_container_cb, into->dtmf_hooks);
	ao2_callback(from->other_hooks, 0, merge_container_cb, into->other_hooks);

	/* Merge hook heaps */
	ast_heap_wrlock(from->interval_hooks);
	for (idx = 1; (hook = ast_heap_peek(from->interval_hooks, idx)); idx++) {
		wrap_hook(into, hook);
	}
	ast_heap_unlock(from->interval_hooks);

	/* Merge feature flags */
	into->feature_flags.flags |= from->feature_flags.flags;
	into->usable |= from->usable;

	into->mute |= from->mute;
	into->dtmf_passthrough |= from->dtmf_passthrough;
}

/* XXX ASTERISK-21271 make ast_bridge_features_init() static when make ast_bridge_join() requires features to be allocated. */
int ast_bridge_features_init(struct ast_bridge_features *features)
{
	/* Zero out the structure */
	memset(features, 0, sizeof(*features));

	/* Initialize the DTMF hooks container */
	features->dtmf_hooks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX,
		AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, bridge_dtmf_hook_sort, NULL);
	if (!features->dtmf_hooks) {
		return -1;
	}

	/* Initialize the miscellaneous other hooks container */
	features->other_hooks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL,
		NULL);
	if (!features->other_hooks) {
		return -1;
	}

	/* Initialize the interval hooks heap */
	features->interval_hooks = ast_heap_create(8, interval_hook_time_cmp,
		offsetof(struct ast_bridge_hook_timer, timer.heap_index));
	if (!features->interval_hooks) {
		return -1;
	}

	features->dtmf_passthrough = 1;

	return 0;
}

/* XXX ASTERISK-21271 make ast_bridge_features_cleanup() static when make ast_bridge_join() requires features to be allocated. */
void ast_bridge_features_cleanup(struct ast_bridge_features *features)
{
	struct ast_bridge_hook_timer *hook;

	/* Destroy the interval hooks heap. */
	if (features->interval_hooks) {
		while ((hook = ast_heap_pop(features->interval_hooks))) {
			ao2_ref(hook, -1);
		}
		features->interval_hooks = ast_heap_destroy(features->interval_hooks);
	}

	/* Destroy the miscellaneous other hooks container. */
	ao2_cleanup(features->other_hooks);
	features->other_hooks = NULL;

	/* Destroy the DTMF hooks container. */
	ao2_cleanup(features->dtmf_hooks);
	features->dtmf_hooks = NULL;
}

void ast_bridge_features_destroy(struct ast_bridge_features *features)
{
	if (!features) {
		return;
	}
	ast_bridge_features_cleanup(features);
	ast_free(features);
}

struct ast_bridge_features *ast_bridge_features_new(void)
{
	struct ast_bridge_features *features;

	features = ast_malloc(sizeof(*features));
	if (features) {
		if (ast_bridge_features_init(features)) {
			ast_bridge_features_destroy(features);
			features = NULL;
		}
	}

	return features;
}

void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval)
{
	ast_bridge_lock(bridge);
	bridge->softmix.internal_mixing_interval = mixing_interval;
	ast_bridge_unlock(bridge);
}

void ast_bridge_set_binaural_active(struct ast_bridge *bridge, unsigned int binaural_active)
{
	ast_bridge_lock(bridge);
	bridge->softmix.binaural_active = binaural_active;
	ast_bridge_unlock(bridge);
}

void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
{
	ast_bridge_lock(bridge);
	bridge->softmix.internal_sample_rate = sample_rate;
	ast_bridge_unlock(bridge);
}

static void cleanup_video_mode(struct ast_bridge *bridge)
{
	switch (bridge->softmix.video_mode.mode) {
	case AST_BRIDGE_VIDEO_MODE_NONE:
		break;
	case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
		if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
			ast_channel_unref(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc);
		}
		break;
	case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
		if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
			ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc);
		}
		if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
			ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc);
		}
	case AST_BRIDGE_VIDEO_MODE_SFU:
		break;
	}
	memset(&bridge->softmix.video_mode, 0, sizeof(bridge->softmix.video_mode));
}

void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan)
{
	ast_bridge_lock(bridge);
	cleanup_video_mode(bridge);
	bridge->softmix.video_mode.mode = AST_BRIDGE_VIDEO_MODE_SINGLE_SRC;
	bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc = ast_channel_ref(video_src_chan);
	ast_verb(5, "Video source in bridge '%s' (%s) is now '%s' (%s)\n",
		bridge->name, bridge->uniqueid,
		ast_channel_name(video_src_chan),
		ast_channel_uniqueid(video_src_chan));
	ast_bridge_publish_state(bridge);
	ast_indicate(video_src_chan, AST_CONTROL_VIDUPDATE);
	ast_bridge_unlock(bridge);
}

void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge)
{
	ast_bridge_lock(bridge);
	cleanup_video_mode(bridge);
	bridge->softmix.video_mode.mode = AST_BRIDGE_VIDEO_MODE_TALKER_SRC;
	ast_bridge_unlock(bridge);
}

void ast_bridge_set_sfu_video_mode(struct ast_bridge *bridge)
{
	ast_bridge_lock(bridge);
	cleanup_video_mode(bridge);
	bridge->softmix.video_mode.mode = AST_BRIDGE_VIDEO_MODE_SFU;
	ast_bridge_unlock(bridge);
}

void ast_bridge_set_video_update_discard(struct ast_bridge *bridge, unsigned int video_update_discard)
{
	ast_bridge_lock(bridge);
	bridge->softmix.video_mode.video_update_discard = video_update_discard;
	ast_bridge_unlock(bridge);
}

void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyframe)
{
	struct ast_bridge_video_talker_src_data *data;

	/* If the channel doesn't support video, we don't care about it */
	if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO)) {
		return;
	}

	ast_bridge_lock(bridge);
	data = &bridge->softmix.video_mode.mode_data.talker_src_data;

	if (data->chan_vsrc == chan) {
		data->average_talking_energy = talker_energy;
	} else if ((data->average_talking_energy < talker_energy) && is_keyframe) {
		if (data->chan_old_vsrc) {
			data->chan_old_vsrc = ast_channel_unref(data->chan_old_vsrc);
		}
		if (data->chan_vsrc) {
			data->chan_old_vsrc = data->chan_vsrc;
			ast_indicate(data->chan_old_vsrc, AST_CONTROL_VIDUPDATE);
		}
		data->chan_vsrc = ast_channel_ref(chan);
		data->average_talking_energy = talker_energy;
		ast_verb(5, "Video source in bridge '%s' (%s) is now '%s' (%s)\n",
			bridge->name, bridge->uniqueid,
			ast_channel_name(data->chan_vsrc),
			ast_channel_uniqueid(data->chan_vsrc));
		ast_bridge_publish_state(bridge);
		ast_indicate(data->chan_vsrc, AST_CONTROL_VIDUPDATE);
	} else if ((data->average_talking_energy < talker_energy) && !is_keyframe) {
		ast_indicate(chan, AST_CONTROL_VIDUPDATE);
	} else if (!data->chan_vsrc && is_keyframe) {
		data->chan_vsrc = ast_channel_ref(chan);
		data->average_talking_energy = talker_energy;
		ast_verb(5, "Video source in bridge '%s' (%s) is now '%s' (%s)\n",
			bridge->name, bridge->uniqueid,
			ast_channel_name(data->chan_vsrc),
			ast_channel_uniqueid(data->chan_vsrc));
		ast_bridge_publish_state(bridge);
		ast_indicate(chan, AST_CONTROL_VIDUPDATE);
	} else if (!data->chan_old_vsrc && is_keyframe) {
		data->chan_old_vsrc = ast_channel_ref(chan);
		ast_indicate(chan, AST_CONTROL_VIDUPDATE);
	}
	ast_bridge_unlock(bridge);
}

int ast_bridge_number_video_src(struct ast_bridge *bridge)
{
	int res = 0;

	ast_bridge_lock(bridge);
	switch (bridge->softmix.video_mode.mode) {
	case AST_BRIDGE_VIDEO_MODE_NONE:
		break;
	case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
		if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
			res = 1;
		}
		break;
	case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
		if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
			res++;
		}
		if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
			res++;
		}
	case AST_BRIDGE_VIDEO_MODE_SFU:
		break;
	}
	ast_bridge_unlock(bridge);
	return res;
}

int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
{
	int res = 0;

	ast_bridge_lock(bridge);
	switch (bridge->softmix.video_mode.mode) {
	case AST_BRIDGE_VIDEO_MODE_NONE:
		break;
	case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
		if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc == chan) {
			res = 1;
		}
		break;
	case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
		if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
			res = 1;
		} else if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
			res = 2;
		}
	case AST_BRIDGE_VIDEO_MODE_SFU:
		break;
	}
	ast_bridge_unlock(bridge);
	return res;
}

void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
{
	ast_bridge_lock(bridge);
	switch (bridge->softmix.video_mode.mode) {
	case AST_BRIDGE_VIDEO_MODE_NONE:
		break;
	case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
		if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc == chan) {
			if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
				ast_channel_unref(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc);
			}
			bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc = NULL;
		}
		break;
	case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
		if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
			if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
				ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc);
			}
			bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc = NULL;
			bridge->softmix.video_mode.mode_data.talker_src_data.average_talking_energy = 0;
		}
		if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
			if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
				ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc);
			}
			bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc = NULL;
		}
	case AST_BRIDGE_VIDEO_MODE_SFU:
		break;
	}
	ast_bridge_unlock(bridge);
}

const char *ast_bridge_video_mode_to_string(enum ast_bridge_video_mode_type video_mode)
{
	switch (video_mode) {
	case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
		return "talker";
	case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
		return "single";
	case AST_BRIDGE_VIDEO_MODE_SFU:
		return "sfu";
	case AST_BRIDGE_VIDEO_MODE_NONE:
	default:
		return "none";
	}
}

static int channel_hash(const void *obj, int flags)
{
	const struct ast_channel *chan = obj;
	const char *name = obj;
	int hash;

	switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
	default:
	case OBJ_POINTER:
		name = ast_channel_name(chan);
		/* Fall through */
	case OBJ_KEY:
		hash = ast_str_hash(name);
		break;
	case OBJ_PARTIAL_KEY:
		/* Should never happen in hash callback. */
		ast_assert(0);
		hash = 0;
		break;
	}
	return hash;
}

static int channel_cmp(void *obj, void *arg, int flags)
{
	const struct ast_channel *left = obj;
	const struct ast_channel *right = arg;
	const char *right_name = arg;
	int cmp;

	switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
	default:
	case OBJ_POINTER:
		right_name = ast_channel_name(right);
		/* Fall through */
	case OBJ_KEY:
		cmp = strcmp(ast_channel_name(left), right_name);
		break;
	case OBJ_PARTIAL_KEY:
		cmp = strncmp(ast_channel_name(left), right_name, strlen(right_name));
		break;
	}
	return cmp ? 0 : CMP_MATCH;
}

struct ao2_container *ast_bridge_peers_nolock(struct ast_bridge *bridge)
{
	struct ao2_container *channels;
	struct ast_bridge_channel *iter;

	channels = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK,
		13, channel_hash, channel_cmp);
	if (!channels) {
		return NULL;
	}

	AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
		ao2_link(channels, iter->chan);
	}

	return channels;
}

struct ao2_container *ast_bridge_peers(struct ast_bridge *bridge)
{
	struct ao2_container *channels;

	ast_bridge_lock(bridge);
	channels = ast_bridge_peers_nolock(bridge);
	ast_bridge_unlock(bridge);

	return channels;
}

struct ast_channel *ast_bridge_peer_nolock(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_channel *peer = NULL;
	struct ast_bridge_channel *iter;

	/* Asking for the peer channel only makes sense on a two-party bridge. */
	if (bridge->num_channels == 2
		&& bridge->technology->capabilities
			& (AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX)) {
		int in_bridge = 0;

		AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
			if (iter->chan != chan) {
				peer = iter->chan;
			} else {
				in_bridge = 1;
			}
		}
		if (in_bridge && peer) {
			ast_channel_ref(peer);
		} else {
			peer = NULL;
		}
	}

	return peer;
}

struct ast_channel *ast_bridge_peer(struct ast_bridge *bridge, struct ast_channel *chan)
{
	struct ast_channel *peer;

	ast_bridge_lock(bridge);
	peer = ast_bridge_peer_nolock(bridge, chan);
	ast_bridge_unlock(bridge);

	return peer;
}

/*!
 * \internal
 * \brief Transfer an entire bridge to a specific destination.
 *
 * This creates a local channel to dial out and swaps the called local channel
 * with the transferer channel. By doing so, all participants in the bridge are
 * connected to the specified destination.
 *
 * While this means of transferring would work for both two-party and multi-party
 * bridges, this method is only used for multi-party bridges since this method would
 * be less efficient for two-party bridges.
 *
 * \param is_external Whether the transfer is externally initiated
 * \param transferer The channel performing a transfer
 * \param bridge The bridge where the transfer is being performed
 * \param exten The destination extension for the blind transfer
 * \param context The destination context for the blind transfer
 * \param transferee The party being transferred if there is only one
 * \param new_channel_cb Callback to call on channel that is created to
 *        facilitate the blind transfer.
 * \param user_data_wrapper User-provided data needed in new_channel_cb
 * \param transfer_message The Stasis publication for this transfer.
 *
 * \return The success or failure of the operation
 */
static enum ast_transfer_result blind_transfer_bridge(int is_external,
		struct ast_channel *transferer, struct ast_bridge *bridge,
		const char *exten, const char *context, struct ast_channel *transferee,
		transfer_channel_cb new_channel_cb,
		struct transfer_channel_data *user_data_wrapper,
		struct ast_blind_transfer_message *transfer_message)
{
	struct ast_channel *local;
	char chan_name[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
	int cause;

	snprintf(chan_name, sizeof(chan_name), "%s@%s", exten, context);
	local = ast_request("Local", ast_channel_nativeformats(transferer), NULL, transferer,
			chan_name, &cause);
	if (!local) {
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	ast_channel_lock_both(local, transferer);
	ast_channel_req_accountcodes(local, transferer, AST_CHANNEL_REQUESTOR_REPLACEMENT);

	transfer_message->replace_channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(local));
	if (!transfer_message->replace_channel) {
		ast_hangup(local);
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	pbx_builtin_setvar_helper(local, BLINDTRANSFER, ast_channel_name(transferer));
	ast_channel_unlock(local);
	ast_channel_unlock(transferer);

	if (new_channel_cb) {
		new_channel_cb(local, user_data_wrapper, AST_BRIDGE_TRANSFER_MULTI_PARTY);
	}

	if (ast_call(local, chan_name, 0)) {
		ast_hangup(local);
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	if (ast_bridge_impart(bridge, local, transferer, NULL,
		AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
		ast_hangup(local);
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	return AST_BRIDGE_TRANSFER_SUCCESS;
}

/*!
 * \internal
 * \brief Get the transferee channel
 *
 * This is only applicable to cases where a transfer is occurring on a
 * two-party bridge. The channels container passed in is expected to only
 * contain two channels, the transferer and the transferee. The transferer
 * channel is passed in as a parameter to ensure we don't return it as
 * the transferee channel.
 *
 * \param channels A two-channel container containing the transferer and transferee
 * \param transferer The party that is transfering the call
 * \return The party that is being transferred
 */
static struct ast_channel *get_transferee(struct ao2_container *channels, struct ast_channel *transferer)
{
	struct ao2_iterator channel_iter;
	struct ast_channel *transferee;

	for (channel_iter = ao2_iterator_init(channels, 0);
			(transferee = ao2_iterator_next(&channel_iter));
			ao2_cleanup(transferee)) {
		if (transferee != transferer) {
			break;
		}
	}

	ao2_iterator_destroy(&channel_iter);
	return transferee;
}

/*!
 * \brief Perform an attended transfer of a bridge
 *
 * This performs an attended transfer of an entire bridge to a target.
 * The target varies, depending on what bridges exist during the transfer
 * attempt.
 *
 * If two bridges exist, then a local channel is created to link the two
 * bridges together.
 *
 * If only one bridge exists, then a local channel is created with one end
 * placed into the existing bridge and the other end masquerading into
 * the unbridged channel.
 *
 * \param chan1 Transferer channel. Guaranteed to be bridged.
 * \param chan2 Other transferer channel. May or may not be bridged.
 * \param bridge1 Bridge that chan1 is in. Guaranteed to be non-NULL.
 * \param bridge2 Bridge that chan2 is in. If NULL, then chan2 is not bridged.
 * \param publication Data to publish for a stasis attended transfer message.
 * \retval AST_BRIDGE_TRANSFER_FAIL Internal error occurred
 * \retval AST_BRIDGE_TRANSFER_SUCCESS Succesfully transferred the bridge
 */
static enum ast_transfer_result attended_transfer_bridge(struct ast_channel *chan1,
		struct ast_channel *chan2, struct ast_bridge *bridge1, struct ast_bridge *bridge2,
		struct ast_attended_transfer_message *transfer_msg)
{
#define BRIDGE_LOCK_ONE_OR_BOTH(b1, b2) \
	do { \
		if (b2) { \
			ast_bridge_lock_both(b1, b2); \
		} else { \
			ast_bridge_lock(b1); \
		} \
	} while (0)

	static const char *dest = "_attended@transfer/m";
	struct ast_channel *local_chan;
	int cause;
	int res;
	const char *app = NULL;

	local_chan = ast_request("Local", ast_channel_nativeformats(chan1), NULL, chan1,
			dest, &cause);
	if (!local_chan) {
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	ast_channel_lock_both(local_chan, chan1);
	ast_channel_req_accountcodes(local_chan, chan1, AST_CHANNEL_REQUESTOR_REPLACEMENT);
	pbx_builtin_setvar_helper(local_chan, ATTENDEDTRANSFER, ast_channel_name(chan1));
	ast_channel_unlock(local_chan);
	ast_channel_unlock(chan1);

	if (bridge2) {
		res = ast_local_setup_bridge(local_chan, bridge2, chan2, NULL);
	} else {
		app = ast_strdupa(ast_channel_appl(chan2));
		res = ast_local_setup_masquerade(local_chan, chan2);
	}

	if (res) {
		ast_hangup(local_chan);
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	/*
	 * Since bridges need to be unlocked before entering ast_bridge_impart and
	 * core_local may call into it then the bridges need to be unlocked here.
	 */
	ast_bridge_unlock(bridge1);
	if (bridge2) {
		ast_bridge_unlock(bridge2);
	}

	if (ast_call(local_chan, dest, 0)) {
		ast_hangup(local_chan);
		BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	/* Get a ref for use later since this one is being stolen */
	ao2_ref(local_chan, +1);
	if (ast_bridge_impart(bridge1, local_chan, chan1, NULL,
		AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
		ast_hangup(local_chan);
		ao2_cleanup(local_chan);
		BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);
		return AST_BRIDGE_TRANSFER_FAIL;
	}
	BRIDGE_LOCK_ONE_OR_BOTH(bridge1, bridge2);

	if (bridge2) {
		void *tech;
		struct ast_channel *locals[2];

		/* Have to lock everything just in case a hangup comes in early */
		ast_local_lock_all(local_chan, &tech, &locals[0], &locals[1]);
		if (!locals[0] || !locals[1]) {
			ast_log(LOG_ERROR, "Transfer failed probably due to an early hangup - "
				"missing other half of '%s'\n", ast_channel_name(local_chan));
			ast_local_unlock_all(tech, locals[0], locals[1]);
			ao2_cleanup(local_chan);
			return AST_BRIDGE_TRANSFER_FAIL;
		}

		/* Make sure the peer is properly set */
		if (local_chan != locals[0]) {
			SWAP(locals[0], locals[1]);
		}

		ast_attended_transfer_message_add_link(transfer_msg, locals);
		ast_local_unlock_all(tech, locals[0], locals[1]);
	} else {
		ast_attended_transfer_message_add_app(transfer_msg, app, local_chan);
	}

	ao2_cleanup(local_chan);
	return AST_BRIDGE_TRANSFER_SUCCESS;
}

static enum ast_transfer_result try_parking(struct ast_channel *transferer,
	const char *context, const char *exten, transfer_channel_cb new_channel_cb,
	struct transfer_channel_data *user_data_wrapper)
{
	RAII_VAR(struct ast_bridge_channel *, transferer_bridge_channel, NULL, ao2_cleanup);

	if (!ast_parking_provider_registered()) {
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	ast_channel_lock(transferer);
	transferer_bridge_channel = ast_channel_get_bridge_channel(transferer);
	ast_channel_unlock(transferer);

	if (!transferer_bridge_channel) {
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	if (ast_parking_blind_transfer_park(transferer_bridge_channel,
		context, exten, new_channel_cb, user_data_wrapper)) {
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	return AST_BRIDGE_TRANSFER_SUCCESS;
}

void ast_bridge_set_transfer_variables(struct ast_channel *chan, const char *value, int attended)
{
	char *writevar;
	char *erasevar;

	if (attended) {
		writevar = ATTENDEDTRANSFER;
		erasevar = BLINDTRANSFER;
	} else {
		writevar = BLINDTRANSFER;
		erasevar = ATTENDEDTRANSFER;
	}

	pbx_builtin_setvar_helper(chan, writevar, value);
	pbx_builtin_setvar_helper(chan, erasevar, NULL);
}

/*!
 * \internal
 * \brief Set the transfer variable as appropriate on channels involved in the transfer
 *
 * The transferer channel will have its variable set the same as its BRIDGEPEER
 * variable. This will account for all channels that it is bridged to. The other channels
 * involved in the transfer will have their variable set to the transferer
 * channel's name.
 *
 * \param transferer The channel performing the transfer
 * \param channels The channels belonging to the bridge
 * \param is_attended false  set BLINDTRANSFER and unset ATTENDEDTRANSFER
 *                    true   set ATTENDEDTRANSFER and unset BLINDTRANSFER
 */
static void set_transfer_variables_all(struct ast_channel *transferer, struct ao2_container *channels, int is_attended)
{
	struct ao2_iterator iter;
	struct ast_channel *chan;
	const char *transferer_name;
	const char *transferer_bridgepeer;

	ast_channel_lock(transferer);
	transferer_name = ast_strdupa(ast_channel_name(transferer));
	transferer_bridgepeer = ast_strdupa(S_OR(pbx_builtin_getvar_helper(transferer, "BRIDGEPEER"), ""));
	ast_channel_unlock(transferer);

	for (iter = ao2_iterator_init(channels, 0);
			(chan = ao2_iterator_next(&iter));
			ao2_cleanup(chan)) {
		if (chan == transferer) {
			ast_bridge_set_transfer_variables(chan, transferer_bridgepeer, is_attended);
		} else {
			ast_bridge_set_transfer_variables(chan, transferer_name, is_attended);
		}
	}

	ao2_iterator_destroy(&iter);
}

static struct ast_bridge *acquire_bridge(struct ast_channel *chan)
{
	struct ast_bridge *bridge;

	ast_channel_lock(chan);
	bridge = ast_channel_get_bridge(chan);
	ast_channel_unlock(chan);

	if (bridge && ast_test_flag(&bridge->feature_flags,
			(AST_BRIDGE_FLAG_MASQUERADE_ONLY | AST_BRIDGE_FLAG_INVISIBLE))) {
		ao2_ref(bridge, -1);
		bridge = NULL;
	}

	return bridge;
}

enum ast_transfer_result ast_bridge_transfer_blind(int is_external,
		struct ast_channel *transferer, const char *exten, const char *context,
		transfer_channel_cb new_channel_cb, void *user_data)
{
	RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
	RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
	RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);
	RAII_VAR(struct ast_channel *, transferee, NULL, ast_channel_cleanup);
	RAII_VAR(struct transfer_channel_data *, user_data_wrapper, NULL, ao2_cleanup);
	RAII_VAR(struct ast_blind_transfer_message *, transfer_message, NULL, ao2_cleanup);
	int do_bridge_transfer;
	int transfer_prohibited;
	enum ast_transfer_result transfer_result;

	transfer_message = ast_blind_transfer_message_create(is_external, transferer, exten, context);
	if (!transfer_message) {
		/* Out of memory. Not even possible to publish a Stasis message about the
		 * failure
		 */
		ast_log(LOG_ERROR, "Unable to allocate memory for blind transfer publication from %s\n",
				ast_channel_name(transferer));
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	bridge = acquire_bridge(transferer);
	if (!bridge) {
		transfer_result = AST_BRIDGE_TRANSFER_INVALID;
		goto publish;
	}

	ast_bridge_lock(bridge);
	transfer_message->bridge = ast_bridge_snapshot_create(bridge);
	ast_bridge_unlock(bridge);
	if (!transfer_message->bridge) {
		transfer_result = AST_BRIDGE_TRANSFER_FAIL;
		goto publish;
	}

	transferee = ast_bridge_peer(bridge, transferer);
	if (transferee) {
		transfer_message->transferee = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferee));
		if (!transfer_message->transferee) {
			transfer_result = AST_BRIDGE_TRANSFER_FAIL;
			goto publish;
		}
	}

	ast_channel_lock(transferer);
	bridge_channel = ast_channel_get_bridge_channel(transferer);
	ast_channel_unlock(transferer);
	if (!bridge_channel) {
		transfer_result = AST_BRIDGE_TRANSFER_INVALID;
		goto publish;
	}

	user_data_wrapper = ao2_alloc(sizeof(*user_data_wrapper), NULL);
	if (!user_data_wrapper) {
		transfer_result = AST_BRIDGE_TRANSFER_FAIL;
		goto publish;
	}

	user_data_wrapper->data = user_data;

	/* Take off hold if they are on hold. */
	ast_bridge_channel_write_unhold(bridge_channel);

	transfer_result = try_parking(transferer, context, exten, new_channel_cb, user_data_wrapper);
	if (transfer_result == AST_BRIDGE_TRANSFER_SUCCESS) {
		goto publish;
	}

	/* Since parking didn't take control of the user_data_wrapper, we are just going to raise the completed flag now. */
	user_data_wrapper->completed = 1;

	{
		SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);

		channels = ast_bridge_peers_nolock(bridge);
		if (!channels) {
			transfer_result = AST_BRIDGE_TRANSFER_FAIL;
			goto publish;
		}
		if (ao2_container_count(channels) <= 1) {
			transfer_result = AST_BRIDGE_TRANSFER_INVALID;
			goto publish;
		}
		transfer_prohibited = ast_test_flag(&bridge->feature_flags,
				AST_BRIDGE_FLAG_TRANSFER_PROHIBITED);
		do_bridge_transfer = ast_test_flag(&bridge->feature_flags,
				AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY) ||
				ao2_container_count(channels) > 2;
	}

	if (transfer_prohibited) {
		transfer_result = AST_BRIDGE_TRANSFER_NOT_PERMITTED;
		goto publish;
	}

	set_transfer_variables_all(transferer, channels, 0);

	if (do_bridge_transfer) {
		transfer_result = blind_transfer_bridge(is_external, transferer, bridge,
			exten, context, transferee, new_channel_cb, user_data_wrapper, transfer_message);
		goto publish;
	}

	/* Reaching this portion means that we're dealing with a two-party bridge */

	if (!transferee) {
		transfer_result = AST_BRIDGE_TRANSFER_FAIL;
		goto publish;
	}

	if (bridge_channel_internal_queue_blind_transfer(transferee, exten, context,
				new_channel_cb, user_data_wrapper)) {
		transfer_result = AST_BRIDGE_TRANSFER_FAIL;
		goto publish;
	}

	ast_bridge_remove(bridge, transferer);
	transfer_result = AST_BRIDGE_TRANSFER_SUCCESS;

publish:
	transfer_message->result = transfer_result;
	ast_bridge_publish_blind_transfer(transfer_message);
	return transfer_result;
}

/*!
 * \internal
 * \brief Performs an attended transfer by moving a channel from one bridge to another
 *
 * The channel that is bridged to the source_channel is moved into the dest_bridge from
 * the source_bridge_channel's bridge. The swap_channel is swapped out of the dest_bridge and placed in
 * the source_bridge_channel's bridge.
 *
 * \note dest_bridge and source_bridge_channel's bridge MUST be locked before calling this function.
 *
 * \param dest_bridge The final bridge for the attended transfer
 * \param source_channel Channel who is bridged to the channel that will move
 * \param swap_channel Channel to be swapped out of the dest_bridge
 * \return The success or failure of the swap attempt
 */
static enum ast_transfer_result bridge_swap_attended_transfer(struct ast_bridge *dest_bridge,
		struct ast_bridge_channel *source_bridge_channel, struct ast_channel *swap_channel)
{
	struct ast_bridge_channel *bridged_to_source;

	bridged_to_source = ast_bridge_channel_peer(source_bridge_channel);
	if (bridged_to_source
		&& bridged_to_source->state == BRIDGE_CHANNEL_STATE_WAIT
		&& !ast_test_flag(&bridged_to_source->features->feature_flags,
			AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE)) {
		bridged_to_source->swap = swap_channel;
		if (bridge_do_move(dest_bridge, bridged_to_source, 1, 0)) {
			return AST_BRIDGE_TRANSFER_FAIL;
		}
		/* Must kick the source channel out of its bridge. */
		ast_bridge_channel_leave_bridge(source_bridge_channel,
			BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
		return AST_BRIDGE_TRANSFER_SUCCESS;
	} else {
		return AST_BRIDGE_TRANSFER_INVALID;
	}
}

/*!
 * \internal
 * \brief Function that performs an attended transfer when both transferer channels are bridged
 *
 * The method by which the transfer is performed is dependent on whether the bridges allow for
 * optimization to occur between them. If no optimization is permitted, then an unreal channel
 * is placed as a link between the two bridges. If optimization is permitted, then that means
 * we are free to perform move or merge operations in order to perform the transfer.
 *
 * \note to_transferee_bridge and to_target_bridge MUST be locked before calling this function
 *
 * \param to_transferee The channel that is bridged to the transferee
 * \param to_transferee_bridge_channel to_transferee's bridge_channel
 * \param to_transfer_target The channel that is bridged to the transfer target
 * \param to_target_bridge_channel to_transfer_target's bridge_channel
 * \param to_transferee_bridge The bridge between to_transferee and the transferee
 * \param to_target_bridge The bridge between to_transfer_target and the transfer_target
 * \param publication Data to publish for a stasis attended transfer message
 * \return The success or failure of the attended transfer
 */
static enum ast_transfer_result two_bridge_attended_transfer(struct ast_channel *to_transferee,
		struct ast_bridge_channel *to_transferee_bridge_channel,
		struct ast_channel *to_transfer_target,
		struct ast_bridge_channel *to_target_bridge_channel,
		struct ast_bridge *to_transferee_bridge, struct ast_bridge *to_target_bridge,
		struct ast_attended_transfer_message *transfer_msg)
{
	struct ast_bridge_channel *kick_me[] = {
			to_transferee_bridge_channel,
			to_target_bridge_channel,
	};
	enum ast_transfer_result res;
	struct ast_bridge *final_bridge = NULL;
	RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);

	channels = ast_bridge_peers_nolock(to_transferee_bridge);

	if (!channels) {
		res = AST_BRIDGE_TRANSFER_FAIL;
		goto end;
	}

	set_transfer_variables_all(to_transferee, channels, 1);

	switch (ast_bridges_allow_optimization(to_transferee_bridge, to_target_bridge)) {
	case AST_BRIDGE_OPTIMIZE_SWAP_TO_CHAN_BRIDGE:
		final_bridge = to_transferee_bridge;
		res = bridge_swap_attended_transfer(to_transferee_bridge, to_target_bridge_channel, to_transferee);
		goto end;
	case AST_BRIDGE_OPTIMIZE_SWAP_TO_PEER_BRIDGE:
		final_bridge = to_target_bridge;
		res = bridge_swap_attended_transfer(to_target_bridge, to_transferee_bridge_channel, to_transfer_target);
		goto end;
	case AST_BRIDGE_OPTIMIZE_MERGE_TO_CHAN_BRIDGE:
		final_bridge = to_transferee_bridge;
		bridge_do_merge(to_transferee_bridge, to_target_bridge, kick_me, ARRAY_LEN(kick_me), 0);
		res = AST_BRIDGE_TRANSFER_SUCCESS;
		goto end;
	case AST_BRIDGE_OPTIMIZE_MERGE_TO_PEER_BRIDGE:
		final_bridge = to_target_bridge;
		bridge_do_merge(to_target_bridge, to_transferee_bridge, kick_me, ARRAY_LEN(kick_me), 0);
		res = AST_BRIDGE_TRANSFER_SUCCESS;
		goto end;
	case AST_BRIDGE_OPTIMIZE_PROHIBITED:
	default:
		/* Just because optimization wasn't doable doesn't necessarily mean
		 * that we can actually perform the transfer. Some reasons for non-optimization
		 * indicate bridge invalidity, so let's check those before proceeding.
		 */
		if (to_transferee_bridge->inhibit_merge || to_transferee_bridge->dissolved ||
				to_target_bridge->inhibit_merge || to_target_bridge->dissolved) {
			return AST_BRIDGE_TRANSFER_INVALID;
		}

		return attended_transfer_bridge(to_transferee, to_transfer_target,
			to_transferee_bridge, to_target_bridge, transfer_msg);
	}

end:
	if (res == AST_BRIDGE_TRANSFER_SUCCESS) {
		ast_attended_transfer_message_add_merge(transfer_msg, final_bridge);
	}

	return res;
}

enum ast_transfer_result ast_bridge_transfer_attended(struct ast_channel *to_transferee,
		struct ast_channel *to_transfer_target)
{
	RAII_VAR(struct ast_bridge *, to_transferee_bridge, NULL, ao2_cleanup);
	RAII_VAR(struct ast_bridge *, to_target_bridge, NULL, ao2_cleanup);
	RAII_VAR(struct ast_bridge_channel *, to_transferee_bridge_channel, NULL, ao2_cleanup);
	RAII_VAR(struct ast_bridge_channel *, to_target_bridge_channel, NULL, ao2_cleanup);
	RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);
	RAII_VAR(struct ast_channel *, transferee, NULL, ao2_cleanup);
	RAII_VAR(struct ast_attended_transfer_message *, transfer_msg, NULL, ao2_cleanup);
	struct ast_bridge *the_bridge = NULL;
	struct ast_channel *chan_bridged;
	struct ast_channel *chan_unbridged;
	int transfer_prohibited;
	int do_bridge_transfer;
	enum ast_transfer_result res;
	const char *app = NULL;
	int hangup_target = 0;

	to_transferee_bridge = acquire_bridge(to_transferee);
	to_target_bridge = acquire_bridge(to_transfer_target);

	transfer_msg = ast_attended_transfer_message_create(1, to_transferee, to_transferee_bridge,
			to_transfer_target, to_target_bridge, NULL, NULL);
	if (!transfer_msg) {
		ast_log(LOG_ERROR, "Unable to create Stasis publication for attended transfer from %s\n",
				ast_channel_name(to_transferee));
		return AST_BRIDGE_TRANSFER_FAIL;
	}

	/* They can't both be unbridged, you silly goose! */
	if (!to_transferee_bridge && !to_target_bridge) {
		res = AST_BRIDGE_TRANSFER_INVALID;
		goto end;
	}

	ast_channel_lock(to_transferee);
	to_transferee_bridge_channel = ast_channel_get_bridge_channel(to_transferee);
	ast_channel_unlock(to_transferee);

	ast_channel_lock(to_transfer_target);
	to_target_bridge_channel = ast_channel_get_bridge_channel(to_transfer_target);
	ast_channel_unlock(to_transfer_target);

	if (to_transferee_bridge_channel) {
		/* Take off hold if they are on hold. */
		ast_bridge_channel_write_unhold(to_transferee_bridge_channel);
	}

	if (to_target_bridge_channel) {
		const char *target_complete_sound;

		/* Take off hold if they are on hold. */
		ast_bridge_channel_write_unhold(to_target_bridge_channel);

		/* Is there a courtesy sound to play to the target? */
		ast_channel_lock(to_transfer_target);
		target_complete_sound = pbx_builtin_getvar_helper(to_transfer_target,
			"ATTENDED_TRANSFER_COMPLETE_SOUND");
		if (!ast_strlen_zero(target_complete_sound)) {
			target_complete_sound = ast_strdupa(target_complete_sound);
		} else {
			target_complete_sound = NULL;
		}
		ast_channel_unlock(to_transfer_target);
		if (!target_complete_sound) {
			ast_channel_lock(to_transferee);
			target_complete_sound = pbx_builtin_getvar_helper(to_transferee,
				"ATTENDED_TRANSFER_COMPLETE_SOUND");
			if (!ast_strlen_zero(target_complete_sound)) {
				target_complete_sound = ast_strdupa(target_complete_sound);
			} else {
				target_complete_sound = NULL;
			}
			ast_channel_unlock(to_transferee);
		}
		if (target_complete_sound) {
			ast_bridge_channel_write_playfile(to_target_bridge_channel, NULL,
				target_complete_sound, NULL);
		}
	}

	/* Let's get the easy one out of the way first */
	if (to_transferee_bridge && to_target_bridge) {

		if (!to_transferee_bridge_channel || !to_target_bridge_channel) {
			res = AST_BRIDGE_TRANSFER_INVALID;
			goto end;
		}

		ast_bridge_lock_both(to_transferee_bridge, to_target_bridge);
		res = two_bridge_attended_transfer(to_transferee, to_transferee_bridge_channel,
				to_transfer_target, to_target_bridge_channel,
				to_transferee_bridge, to_target_bridge, transfer_msg);
		ast_bridge_unlock(to_transferee_bridge);
		ast_bridge_unlock(to_target_bridge);

		hangup_target = 1;
		goto end;
	}

	the_bridge = to_transferee_bridge ?: to_target_bridge;
	chan_bridged = to_transferee_bridge ? to_transferee : to_transfer_target;
	chan_unbridged = to_transferee_bridge ? to_transfer_target : to_transferee;

	/*
	 * Race condition makes it possible for app to be NULL, so get the app prior to
	 * transferring with a fallback of "unknown".
	 */
	app = ast_strdupa(ast_channel_appl(chan_unbridged) ?: "unknown");

	{
		int chan_count;
		SCOPED_LOCK(lock, the_bridge, ast_bridge_lock, ast_bridge_unlock);

		channels = ast_bridge_peers_nolock(the_bridge);
		if (!channels) {
			res = AST_BRIDGE_TRANSFER_FAIL;
			goto end;
		}
		chan_count = ao2_container_count(channels);
		if (chan_count <= 1) {
			res = AST_BRIDGE_TRANSFER_INVALID;
			goto end;
		}
		transfer_prohibited = ast_test_flag(&the_bridge->feature_flags,
				AST_BRIDGE_FLAG_TRANSFER_PROHIBITED);
		do_bridge_transfer = ast_test_flag(&the_bridge->feature_flags,
				AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY) ||
				chan_count > 2;
	}

	if (transfer_prohibited) {
		res = AST_BRIDGE_TRANSFER_NOT_PERMITTED;
		goto end;
	}

	set_transfer_variables_all(to_transferee, channels, 1);

	if (do_bridge_transfer) {
		/*
		 * Hang up the target if it was bridged. Note, if it is not bridged
		 * it is hung up during the masquerade.
		 */
		hangup_target = chan_bridged == to_transfer_target;
		ast_bridge_lock(the_bridge);
		res = attended_transfer_bridge(chan_bridged, chan_unbridged, the_bridge, NULL, transfer_msg);
		ast_bridge_unlock(the_bridge);
		goto end;
	}

	transferee = get_transferee(channels, chan_bridged);
	if (!transferee) {
		res = AST_BRIDGE_TRANSFER_FAIL;
		goto end;
	}

	if (bridge_channel_internal_queue_attended_transfer(transferee, chan_unbridged)) {
		res = AST_BRIDGE_TRANSFER_FAIL;
		goto end;
	}

	ast_bridge_remove(the_bridge, chan_bridged);

	ast_attended_transfer_message_add_app(transfer_msg, app, NULL);
	res = AST_BRIDGE_TRANSFER_SUCCESS;

end:
	if ((res == AST_BRIDGE_TRANSFER_SUCCESS && hangup_target) || res == AST_BRIDGE_TRANSFER_FAIL) {
		ast_softhangup(to_transfer_target, AST_SOFTHANGUP_DEV);
	}

	transfer_msg->result = res;
	ast_bridge_publish_attended_transfer(transfer_msg);
	return res;
}

/*!
 * \internal
 * \brief Service the bridge manager request.
 * \since 12.0.0
 *
 * \param bridge requesting service.
 *
 * \return Nothing
 */
static void bridge_manager_service(struct ast_bridge *bridge)
{
	ast_bridge_lock(bridge);
	if (bridge->callid) {
		ast_callid_threadassoc_change(bridge->callid);
	}

	/* Do any pending bridge actions. */
	bridge_handle_actions(bridge);
	ast_bridge_unlock(bridge);
}

/*!
 * \internal
 * \brief Bridge manager service thread.
 * \since 12.0.0
 *
 * \return Nothing
 */
static void *bridge_manager_thread(void *data)
{
	struct bridge_manager_controller *manager = data;
	struct bridge_manager_request *request;

	ao2_lock(manager);
	while (!manager->stop) {
		request = AST_LIST_REMOVE_HEAD(&manager->service_requests, node);
		if (!request) {
			ast_cond_wait(&manager->cond, ao2_object_get_lockaddr(manager));
			continue;
		}
		ao2_unlock(manager);

		/* Service the bridge. */
		bridge_manager_service(request->bridge);
		ao2_ref(request->bridge, -1);
		ast_free(request);

		ao2_lock(manager);
	}
	ao2_unlock(manager);

	return NULL;
}

/*!
 * \internal
 * \brief Destroy the bridge manager controller.
 * \since 12.0.0
 *
 * \param obj Bridge manager to destroy.
 *
 * \return Nothing
 */
static void bridge_manager_destroy(void *obj)
{
	struct bridge_manager_controller *manager = obj;
	struct bridge_manager_request *request;

	if (manager->thread != AST_PTHREADT_NULL) {
		/* Stop the manager thread. */
		ao2_lock(manager);
		manager->stop = 1;
		ast_cond_signal(&manager->cond);
		ao2_unlock(manager);
		ast_debug(1, "Waiting for bridge manager thread to die.\n");
		pthread_join(manager->thread, NULL);
	}

	/* Destroy the service request queue. */
	while ((request = AST_LIST_REMOVE_HEAD(&manager->service_requests, node))) {
		ao2_ref(request->bridge, -1);
		ast_free(request);
	}

	ast_cond_destroy(&manager->cond);
}

/*!
 * \internal
 * \brief Create the bridge manager controller.
 * \since 12.0.0
 *
 * \retval manager on success.
 * \retval NULL on error.
 */
static struct bridge_manager_controller *bridge_manager_create(void)
{
	struct bridge_manager_controller *manager;

	manager = ao2_alloc(sizeof(*manager), bridge_manager_destroy);
	if (!manager) {
		/* Well. This isn't good. */
		return NULL;
	}
	ast_cond_init(&manager->cond, NULL);
	AST_LIST_HEAD_INIT_NOLOCK(&manager->service_requests);

	/* Create the bridge manager thread. */
	if (ast_pthread_create(&manager->thread, NULL, bridge_manager_thread, manager)) {
		/* Well. This isn't good either. */
		manager->thread = AST_PTHREADT_NULL;
		ao2_ref(manager, -1);
		manager = NULL;
	}

	return manager;
}

/*!
 * \internal
 * \brief Bridge ao2 container sort function.
 * \since 12.0.0
 *
 * \param obj_left pointer to the (user-defined part) of an object.
 * \param obj_right pointer to the (user-defined part) of an object.
 * \param flags flags from ao2_callback()
 *   OBJ_POINTER - if set, 'obj_right', is an object.
 *   OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
 *   OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
 *
 * \retval <0 if obj_left < obj_right
 * \retval =0 if obj_left == obj_right
 * \retval >0 if obj_left > obj_right
 */
static int bridge_sort_cmp(const void *obj_left, const void *obj_right, int flags)
{
	const struct ast_bridge *bridge_left = obj_left;
	const struct ast_bridge *bridge_right = obj_right;
	const char *right_key = obj_right;
	int cmp;

	switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
	default:
	case OBJ_POINTER:
		right_key = bridge_right->uniqueid;
		/* Fall through */
	case OBJ_KEY:
		cmp = strcmp(bridge_left->uniqueid, right_key);
		break;
	case OBJ_PARTIAL_KEY:
		cmp = strncmp(bridge_left->uniqueid, right_key, strlen(right_key));
		break;
	}
	return cmp;
}

struct ast_bridge *ast_bridge_find_by_id(const char *bridge_id)
{
	return ao2_find(bridges, bridge_id, OBJ_SEARCH_KEY);
}

static int complete_bridge_live_search(void *obj, void *arg, int flags)
{
	struct ast_bridge *bridge = obj;

	if (ast_cli_completion_add(ast_strdup(bridge->uniqueid))) {
		return CMP_STOP;
	}

	return 0;
}

static char *complete_bridge_live(const char *word)
{
	ao2_callback(bridges, ast_strlen_zero(word) ? 0 : OBJ_PARTIAL_KEY,
		complete_bridge_live_search, (char *) word);

	return NULL;
}

static char *complete_bridge_stasis(const char *word)
{
	int wordlen = strlen(word);
	struct ao2_container *cached_bridges;
	struct ao2_iterator iter;
	struct stasis_message *msg;

	cached_bridges = stasis_cache_dump(ast_bridge_cache(), ast_bridge_snapshot_type());
	if (!cached_bridges) {
		return NULL;
	}

	iter = ao2_iterator_init(cached_bridges, 0);
	for (; (msg = ao2_iterator_next(&iter)); ao2_ref(msg, -1)) {
		struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);

		if (!strncasecmp(word, snapshot->uniqueid, wordlen)) {
			if (ast_cli_completion_add(ast_strdup(snapshot->uniqueid))) {
				ao2_ref(msg, -1);
				break;
			}
		}
	}
	ao2_iterator_destroy(&iter);
	ao2_ref(cached_bridges, -1);

	return NULL;
}

static char *handle_bridge_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
#define FORMAT_HDR "%-36s %5s %-15s %s\n"
#define FORMAT_ROW "%-36s %5u %-15s %s\n"

	RAII_VAR(struct ao2_container *, cached_bridges, NULL, ao2_cleanup);
	struct ao2_iterator iter;
	struct stasis_message *msg;

	switch (cmd) {
	case CLI_INIT:
		e->command = "bridge show all";
		e->usage =
			"Usage: bridge show all\n"
			"       List all bridges\n";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	cached_bridges = stasis_cache_dump(ast_bridge_cache(), ast_bridge_snapshot_type());
	if (!cached_bridges) {
		ast_cli(a->fd, "Failed to retrieve cached bridges\n");
		return CLI_SUCCESS;
	}

	ast_cli(a->fd, FORMAT_HDR, "Bridge-ID", "Chans", "Type", "Technology");

	iter = ao2_iterator_init(cached_bridges, 0);
	for (; (msg = ao2_iterator_next(&iter)); ao2_ref(msg, -1)) {
		struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);

		ast_cli(a->fd, FORMAT_ROW,
			snapshot->uniqueid,
			snapshot->num_channels,
			S_OR(snapshot->subclass, "<unknown>"),
			S_OR(snapshot->technology, "<unknown>"));
	}
	ao2_iterator_destroy(&iter);
	return CLI_SUCCESS;

#undef FORMAT_HDR
#undef FORMAT_ROW
}

/*! \brief Internal callback function for sending channels in a bridge to the CLI */
static int bridge_show_specific_print_channel(void *obj, void *arg, int flags)
{
	const char *uniqueid = obj;
	struct ast_cli_args *a = arg;
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
	struct ast_channel_snapshot *snapshot;

	msg = stasis_cache_get(ast_channel_cache(), ast_channel_snapshot_type(), uniqueid);
	if (!msg) {
		return 0;
	}
	snapshot = stasis_message_data(msg);

	ast_cli(a->fd, "Channel: %s\n", snapshot->name);

	return 0;
}

static char *handle_bridge_show_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
	struct ast_bridge_snapshot *snapshot;

	switch (cmd) {
	case CLI_INIT:
		e->command = "bridge show";
		e->usage =
			"Usage: bridge show <bridge-id>\n"
			"       Show information about the <bridge-id> bridge\n";
		return NULL;
	case CLI_GENERATE:
		if (a->pos == 2) {
			return complete_bridge_stasis(a->word);
		}
		return NULL;
	}

	if (a->argc != 3) {
		return CLI_SHOWUSAGE;
	}

	msg = stasis_cache_get(ast_bridge_cache(), ast_bridge_snapshot_type(), a->argv[2]);
	if (!msg) {
		ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
		return CLI_SUCCESS;
	}

	snapshot = stasis_message_data(msg);
	ast_cli(a->fd, "Id: %s\n", snapshot->uniqueid);
	ast_cli(a->fd, "Type: %s\n", S_OR(snapshot->subclass, "<unknown>"));
	ast_cli(a->fd, "Technology: %s\n", S_OR(snapshot->technology, "<unknown>"));
	ast_cli(a->fd, "Num-Channels: %u\n", snapshot->num_channels);
	ao2_callback(snapshot->channels, OBJ_NODATA, bridge_show_specific_print_channel, a);

	return CLI_SUCCESS;
}

#ifdef AST_DEVMODE
static char *handle_bridge_destroy_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	struct ast_bridge *bridge;

	switch (cmd) {
	case CLI_INIT:
		e->command = "bridge destroy";
		e->usage =
			"Usage: bridge destroy <bridge-id>\n"
			"       Destroy the <bridge-id> bridge\n";
		return NULL;
	case CLI_GENERATE:
		if (a->pos == 2) {
			return complete_bridge_live(a->word);
		}
		return NULL;
	}

	if (a->argc != 3) {
		return CLI_SHOWUSAGE;
	}

	bridge = ast_bridge_find_by_id(a->argv[2]);
	if (!bridge) {
		ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
		return CLI_SUCCESS;
	}

	ast_cli(a->fd, "Destroying bridge '%s'\n", a->argv[2]);
	ast_bridge_destroy(bridge, 0);

	return CLI_SUCCESS;
}
#endif

static char *complete_bridge_participant(const char *bridge_name, const char *word)
{
	struct ast_bridge *bridge;
	struct ast_bridge_channel *bridge_channel;
	int wordlen;

	bridge = ast_bridge_find_by_id(bridge_name);
	if (!bridge) {
		return NULL;
	}

	wordlen = strlen(word);

	ast_bridge_lock(bridge);
	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
		if (!strncasecmp(ast_channel_name(bridge_channel->chan), word, wordlen)) {
			if (ast_cli_completion_add(ast_strdup(ast_channel_name(bridge_channel->chan)))) {
				break;
			}
		}
	}
	ast_bridge_unlock(bridge);

	ao2_ref(bridge, -1);

	return NULL;
}

static char *handle_bridge_kick_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	static const char * const completions[] = { "all", NULL };
	struct ast_bridge *bridge;

	switch (cmd) {
	case CLI_INIT:
		e->command = "bridge kick";
		e->usage =
			"Usage: bridge kick <bridge-id> <channel-name | all>\n"
			"       Kick the <channel-name> channel out of the <bridge-id> bridge\n"
			"       If all is specified as the channel name then all channels will be\n"
			"       kicked out of the bridge.\n";
		return NULL;
	case CLI_GENERATE:
		if (a->pos == 2) {
			return complete_bridge_live(a->word);
		}
		if (a->pos == 3) {
			ast_cli_complete(a->word, completions, -1);
			return complete_bridge_participant(a->argv[2], a->word);
		}
		return NULL;
	}

	if (a->argc != 4) {
		return CLI_SHOWUSAGE;
	}

	bridge = ast_bridge_find_by_id(a->argv[2]);
	if (!bridge) {
		ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
		return CLI_SUCCESS;
	}

	if (!strcasecmp(a->argv[3], "all")) {
		struct ast_bridge_channel *bridge_channel;

		ast_cli(a->fd, "Kicking all channels from bridge '%s'\n", a->argv[2]);

		ast_bridge_lock(bridge);
		AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
			ast_bridge_channel_queue_callback(bridge_channel, 0, kick_it, NULL, 0);
		}
		ast_bridge_unlock(bridge);
	} else {
		struct ast_channel *chan;

		chan = ast_channel_get_by_name_prefix(a->argv[3], strlen(a->argv[3]));
		if (!chan) {
			ast_cli(a->fd, "Channel '%s' not found\n", a->argv[3]);
			ao2_ref(bridge, -1);
			return CLI_SUCCESS;
		}

		ast_cli(a->fd, "Kicking channel '%s' from bridge '%s'\n",
			ast_channel_name(chan), a->argv[2]);
		ast_bridge_kick(bridge, chan);
		ast_channel_unref(chan);
	}

	ao2_ref(bridge, -1);
	return CLI_SUCCESS;
}

/*! Bridge technology capabilities to string. */
static const char *tech_capability2str(uint32_t capabilities)
{
	const char *type;

	if (capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
		type = "Holding";
	} else if (capabilities & AST_BRIDGE_CAPABILITY_EARLY) {
		type = "Early";
	} else if (capabilities & AST_BRIDGE_CAPABILITY_NATIVE) {
		type = "Native";
	} else if (capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) {
		type = "1to1Mix";
	} else if (capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
		type = "MultiMix";
	} else {
		type = "<Unknown>";
	}
	return type;
}

static char *handle_bridge_technology_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
#define FORMAT_HDR "%-20s %-20s %8s %s\n"
#define FORMAT_ROW "%-20s %-20s %8u %s\n"

	struct ast_bridge_technology *cur;

	switch (cmd) {
	case CLI_INIT:
		e->command = "bridge technology show";
		e->usage =
			"Usage: bridge technology show\n"
			"       List registered bridge technologies\n";
		return NULL;
	case CLI_GENERATE:
		return NULL;
	}

	ast_cli(a->fd, FORMAT_HDR, "Name", "Type", "Priority", "Suspended");
	AST_RWLIST_RDLOCK(&bridge_technologies);
	AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
		const char *type;

		/* Decode type for display */
		type = tech_capability2str(cur->capabilities);

		ast_cli(a->fd, FORMAT_ROW, cur->name, type, cur->preference,
			AST_CLI_YESNO(cur->suspended));
	}
	AST_RWLIST_UNLOCK(&bridge_technologies);
	return CLI_SUCCESS;

#undef FORMAT
}

static char *complete_bridge_technology(const char *word)
{
	struct ast_bridge_technology *cur;
	int wordlen;

	wordlen = strlen(word);
	AST_RWLIST_RDLOCK(&bridge_technologies);
	AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
		if (!strncasecmp(cur->name, word, wordlen)) {
			if (ast_cli_completion_add(ast_strdup(cur->name))) {
				break;
			}
		}
	}
	AST_RWLIST_UNLOCK(&bridge_technologies);

	return NULL;
}

static char *handle_bridge_technology_suspend(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
	struct ast_bridge_technology *cur;
	int suspend;
	int successful;

	switch (cmd) {
	case CLI_INIT:
		e->command = "bridge technology {suspend|unsuspend}";
		e->usage =
			"Usage: bridge technology {suspend|unsuspend} <technology-name>\n"
			"       Suspend or unsuspend a bridge technology.\n";
		return NULL;
	case CLI_GENERATE:
		if (a->pos == 3) {
			return complete_bridge_technology(a->word);
		}
		return NULL;
	}

	if (a->argc != 4) {
		return CLI_SHOWUSAGE;
	}

	suspend = !strcasecmp(a->argv[2], "suspend");
	successful = 0;
	AST_RWLIST_WRLOCK(&bridge_technologies);
	AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
		if (!strcasecmp(cur->name, a->argv[3])) {
			successful = 1;
			if (suspend) {
				ast_bridge_technology_suspend(cur);
			} else {
				ast_bridge_technology_unsuspend(cur);
			}
			break;
		}
	}
	AST_RWLIST_UNLOCK(&bridge_technologies);

	if (successful) {
		if (suspend) {
			ast_cli(a->fd, "Suspended bridge technology '%s'\n", a->argv[3]);
		} else {
			ast_cli(a->fd, "Unsuspended bridge technology '%s'\n", a->argv[3]);
		}
	} else {
		ast_cli(a->fd, "Bridge technology '%s' not found\n", a->argv[3]);
	}

	return CLI_SUCCESS;
}

static struct ast_cli_entry bridge_cli[] = {
	AST_CLI_DEFINE(handle_bridge_show_all, "List all bridges"),
	AST_CLI_DEFINE(handle_bridge_show_specific, "Show information about a bridge"),
#ifdef AST_DEVMODE
	AST_CLI_DEFINE(handle_bridge_destroy_specific, "Destroy a bridge"),
#endif
	AST_CLI_DEFINE(handle_bridge_kick_channel, "Kick a channel from a bridge"),
	AST_CLI_DEFINE(handle_bridge_technology_show, "List registered bridge technologies"),
	AST_CLI_DEFINE(handle_bridge_technology_suspend, "Suspend/unsuspend a bridge technology"),
};


static int handle_manager_bridge_tech_suspend(struct mansession *s, const struct message *m, int suspend)
{
	const char *name = astman_get_header(m, "BridgeTechnology");
	struct ast_bridge_technology *cur;
	int successful = 0;

	if (ast_strlen_zero(name)) {
		astman_send_error(s, m, "BridgeTechnology must be provided");
		return 0;
	}

	AST_RWLIST_RDLOCK(&bridge_technologies);
	AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {

		if (!strcasecmp(cur->name, name)) {
			successful = 1;
			if (suspend) {
				ast_bridge_technology_suspend(cur);
			} else {
				ast_bridge_technology_unsuspend(cur);
			}
			break;
		}
	}
	AST_RWLIST_UNLOCK(&bridge_technologies);
	if (!successful) {
		astman_send_error(s, m, "BridgeTechnology not found");
		return 0;
	}

	astman_send_ack(s, m, (suspend ? "Suspended bridge technology" : "Unsuspended bridge technology"));
	return 0;
}

static int manager_bridge_tech_suspend(struct mansession *s, const struct message *m)
{
	return handle_manager_bridge_tech_suspend(s, m, 1);
}

static int manager_bridge_tech_unsuspend(struct mansession *s, const struct message *m)
{
	return handle_manager_bridge_tech_suspend(s, m, 0);
}

static int manager_bridge_tech_list(struct mansession *s, const struct message *m)
{
	const char *id = astman_get_header(m, "ActionID");
	RAII_VAR(struct ast_str *, id_text, ast_str_create(128), ast_free);
	struct ast_bridge_technology *cur;
	int num_items = 0;

	if (!id_text) {
		astman_send_error(s, m, "Internal error");
		return -1;
	}

	if (!ast_strlen_zero(id)) {
		ast_str_set(&id_text, 0, "ActionID: %s\r\n", id);
	}

	astman_send_listack(s, m, "Bridge technology listing will follow", "start");

	AST_RWLIST_RDLOCK(&bridge_technologies);
	AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
		const char *type;

		type = tech_capability2str(cur->capabilities);

		astman_append(s,
			"Event: BridgeTechnologyListItem\r\n"
			"BridgeTechnology: %s\r\n"
			"BridgeType: %s\r\n"
			"BridgePriority: %u\r\n"
			"BridgeSuspended: %s\r\n"
			"%s"
			"\r\n",
			cur->name, type, cur->preference, AST_YESNO(cur->suspended),
			ast_str_buffer(id_text));
		++num_items;
	}
	AST_RWLIST_UNLOCK(&bridge_technologies);

	astman_send_list_complete_start(s, m, "BridgeTechnologyListComplete", num_items);
	astman_send_list_complete_end(s);

	return 0;
}

/*!
 * \internal
 * \brief Print bridge object key (name).
 * \since 12.0.0
 *
 * \param v_obj A pointer to the object we want the key printed.
 * \param where User data needed by prnt to determine where to put output.
 * \param prnt Print output callback function to use.
 *
 * \return Nothing
 */
static void bridge_prnt_obj(void *v_obj, void *where, ao2_prnt_fn *prnt)
{
	struct ast_bridge *bridge = v_obj;

	if (!bridge) {
		return;
	}
	prnt(where, "%s %s chans:%u",
		bridge->uniqueid, bridge->v_table->name, bridge->num_channels);
}

/*!
 * \internal
 * \brief Shutdown the bridging system.  Stuff to do on graceful shutdown.
 * \since 13.3.0
 *
 * \return Nothing
 */
static void bridge_cleanup(void)
{
	ast_manager_unregister("BridgeTechnologyList");
	ast_manager_unregister("BridgeTechnologySuspend");
	ast_manager_unregister("BridgeTechnologyUnsuspend");
	ast_cli_unregister_multiple(bridge_cli, ARRAY_LEN(bridge_cli));
	ao2_container_unregister("bridges");

	ao2_cleanup(bridges);
	bridges = NULL;
	ao2_cleanup(bridge_manager);
	bridge_manager = NULL;
}

int ast_bridging_init(void)
{
	ast_register_cleanup(bridge_cleanup);

	if (ast_stasis_bridging_init()) {
		return -1;
	}

	bridge_manager = bridge_manager_create();
	if (!bridge_manager) {
		return -1;
	}

	bridges = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX,
		AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, bridge_sort_cmp, NULL);
	if (!bridges) {
		return -1;
	}
	ao2_container_register("bridges", bridges, bridge_prnt_obj);

	ast_bridging_init_basic();

	ast_cli_register_multiple(bridge_cli, ARRAY_LEN(bridge_cli));

	ast_manager_register_xml_core("BridgeTechnologyList", 0, manager_bridge_tech_list);
	ast_manager_register_xml_core("BridgeTechnologySuspend", 0, manager_bridge_tech_suspend);
	ast_manager_register_xml_core("BridgeTechnologyUnsuspend", 0, manager_bridge_tech_unsuspend);

	return 0;
}