summaryrefslogtreecommitdiff
path: root/channels/chan_motif.c
blob: 210cf36c6c7cffd96d8eea3e1a6fc9687a2e3d93 (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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012, 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
 *
 * \author Joshua Colp <jcolp@digium.com>
 *
 * \brief Motif Jingle Channel Driver
 *
 * Iksemel http://iksemel.jabberstudio.org/
 *
 * \ingroup channel_drivers
 */

/*! \li \ref chan_motif.c uses the configuration file \ref motif.conf
 * \addtogroup configuration_file
 */

/*! \page motif.conf motif.conf
 * \verbinclude motif.conf.sample
 */

/*** MODULEINFO
	<depend>iksemel</depend>
	<depend>res_xmpp</depend>
	<use type="external">openssl</use>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <iksemel.h>
#include <pthread.h>

#include "asterisk/lock.h"
#include "asterisk/channel.h"
#include "asterisk/config_options.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/sched.h"
#include "asterisk/io.h"
#include "asterisk/rtp_engine.h"
#include "asterisk/acl.h"
#include "asterisk/callerid.h"
#include "asterisk/file.h"
#include "asterisk/cli.h"
#include "asterisk/app.h"
#include "asterisk/musiconhold.h"
#include "asterisk/manager.h"
#include "asterisk/stringfields.h"
#include "asterisk/utils.h"
#include "asterisk/causes.h"
#include "asterisk/abstract_jb.h"
#include "asterisk/xmpp.h"
#include "asterisk/endpoints.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/format_cache.h"

/*** DOCUMENTATION
	<configInfo name="chan_motif" language="en_US">
		<synopsis>Jingle Channel Driver</synopsis>
		<description>
			<para><emphasis>Transports</emphasis></para>
			<para>There are three different transports and protocol derivatives
			supported by <literal>chan_motif</literal>. They are in order of
			preference: Jingle using ICE-UDP, Google Jingle, and Google-V1.</para>
			<para>Jingle as defined in XEP-0166 supports the widest range of
			features. It is referred to as <literal>ice-udp</literal>. This is
			the specification that Jingle clients implement.</para>
			<para>Google Jingle follows the Jingle specification for signaling
			but uses a custom transport for media. It is supported by the
			Google Talk Plug-in in Gmail and by some other Jingle clients. It
			is referred to as <literal>google</literal> in this file.</para>
			<para>Google-V1 is the original Google Talk signaling protocol
			which uses an initial preliminary version of Jingle. It also uses
			the same custom transport as Google Jingle for media. It is
			supported by Google Voice, some other Jingle clients, and the
			Windows Google Talk client. It is referred to as <literal>google-v1</literal>
			in this file.</para>
			<para>Incoming sessions will automatically switch to the correct
			transport once it has been determined.</para>
			<para>Outgoing sessions are capable of determining if the target
			is capable of Jingle or a Google transport if the target is in the
			roster. Unfortunately it is not possible to differentiate between
			a Google Jingle or Google-V1 capable resource until a session
			initiate attempt occurs. If a resource is determined to use a
			Google transport it will initially use Google Jingle but will fall
			back to Google-V1 if required.</para>
			<para>If an outgoing session attempt fails due to failure to
			support the given transport <literal>chan_motif</literal> will
			fall back in preference order listed previously until all
			transports have been exhausted.</para>
			<para><emphasis>Dialing and Resource Selection Strategy</emphasis></para>
			<para>Placing a call through an endpoint can be accomplished using the
			following dial string:</para>
			<para><literal>Motif/[endpoint name]/[target]</literal></para>
			<para>When placing an outgoing call through an endpoint the requested
			target is searched for in the roster list. If present the first Jingle
			or Google Jingle capable resource is specifically targeted. Since the
			capabilities of the resource are known the outgoing session initiation
			will disregard the configured transport and use the determined one.</para>
			<para>If the target is not found in the roster the target will be used
			as-is and a session will be initiated using the transport specified
			in this configuration file. If no transport has been specified the
			endpoint defaults to <literal>ice-udp</literal>.</para>
			<para><emphasis>Video Support</emphasis></para>
			<para>Support for video does not need to be explicitly enabled.
			Configuring any video codec on your endpoint will automatically enable
			it.</para>
			<para><emphasis>DTMF</emphasis></para>
			<para>The only supported method for DTMF is RFC2833. This is always
			enabled on audio streams and negotiated if possible.</para>
			<para><emphasis>Incoming Calls</emphasis></para>
			<para>Incoming calls will first look for the extension matching the
			name of the endpoint in the configured context. If no such extension
			exists the call will automatically fall back to the <literal>s</literal> extension.</para>
			<para><emphasis>CallerID</emphasis></para>
			<para>The incoming caller id number is populated with the username of
			the caller and the name is populated with the full identity of the
			caller. If you would like to perform authentication or filtering
			of incoming calls it is recommended that you use these fields to do so.</para>
			<para>Outgoing caller id can <emphasis>not</emphasis> be set.</para>
			<warning>
				<para>Multiple endpoints using the
				same connection is <emphasis>NOT</emphasis> supported. Doing so
				may result in broken calls.</para>
			</warning>
		</description>
		<configFile name="motif.conf">
			<configObject name="endpoint">
				<synopsis>The configuration for an endpoint.</synopsis>
				<configOption name="context">
					<synopsis>Default dialplan context that incoming sessions will be routed to</synopsis>
				</configOption>
				<configOption name="callgroup">
					<synopsis>A callgroup to assign to this endpoint.</synopsis>
				</configOption>
				<configOption name="pickupgroup">
					<synopsis>A pickup group to assign to this endpoint.</synopsis>
				</configOption>
				<configOption name="language">
					<synopsis>The default language for this endpoint.</synopsis>
				</configOption>
				<configOption name="musicclass">
					<synopsis>Default music on hold class for this endpoint.</synopsis>
				</configOption>
				<configOption name="parkinglot">
					<synopsis>Default parking lot for this endpoint.</synopsis>
				</configOption>
				<configOption name="accountcode">
					<synopsis>Accout code for CDR purposes</synopsis>
				</configOption>
				<configOption name="allow">
					<synopsis>Codecs to allow</synopsis>
				</configOption>
				<configOption name="disallow">
					<synopsis>Codecs to disallow</synopsis>
				</configOption>
				<configOption name="connection">
					<synopsis>Connection to accept traffic on and on which to send traffic out</synopsis>
				</configOption>
				<configOption name="transport">
					<synopsis>The transport to use for the endpoint.</synopsis>
					<description>
						<para>The default outbound transport for this endpoint. Inbound
						messages are inferred. Allowed transports are <literal>ice-udp</literal>,
						<literal>google</literal>, or <literal>google-v1</literal>. Note
						that <literal>chan_motif</literal> will fall back to transport
						preference order if the transport value chosen here fails.</para>
						<enumlist>
							<enum name="ice-udp">
								<para>The Jingle protocol, as defined in XEP 0166.</para>
							</enum>
							<enum name="google">
								<para>The Google Jingle protocol, which follows the Jingle
								specification for signaling but uses a custom transport for
								media.</para>
							</enum>
							<enum name="google-v1">
								<para>Google-V1 is the original Google Talk signaling
								protocol which uses an initial preliminary version of Jingle.
								It also uses the same custom transport as <literal>google</literal> for media.</para>
							</enum>
						</enumlist>
					</description>
				</configOption>
				<configOption name="maxicecandidates">
					<synopsis>Maximum number of ICE candidates to offer</synopsis>
				</configOption>
				<configOption name="maxpayloads">
					<synopsis>Maximum number of pyaloads to offer</synopsis>
				</configOption>
			</configObject>
		</configFile>
	</configInfo>
***/

/*! \brief Default maximum number of ICE candidates we will offer */
#define DEFAULT_MAX_ICE_CANDIDATES "10"

/*! \brief Default maximum number of payloads we will offer */
#define DEFAULT_MAX_PAYLOADS "30"

/*! \brief Number of buckets for endpoints */
#define ENDPOINT_BUCKETS 37

/*! \brief Number of buckets for sessions, on a per-endpoint basis */
#define SESSION_BUCKETS 37

/*! \brief Namespace for Jingle itself */
#define JINGLE_NS "urn:xmpp:jingle:1"

/*! \brief Namespace for Jingle RTP sessions */
#define JINGLE_RTP_NS "urn:xmpp:jingle:apps:rtp:1"

/*! \brief Namespace for Jingle RTP info */
#define JINGLE_RTP_INFO_NS "urn:xmpp:jingle:apps:rtp:info:1"

/*! \brief Namespace for Jingle ICE-UDP */
#define JINGLE_ICE_UDP_NS "urn:xmpp:jingle:transports:ice-udp:1"

/*! \brief Namespace for Google Talk ICE-UDP */
#define GOOGLE_TRANSPORT_NS "http://www.google.com/transport/p2p"

/*! \brief Namespace for Google Talk Raw UDP */
#define GOOGLE_TRANSPORT_RAW_NS "http://www.google.com/transport/raw-udp"

/*! \brief Namespace for Google Session */
#define GOOGLE_SESSION_NS "http://www.google.com/session"

/*! \brief Namespace for Google Phone description */
#define GOOGLE_PHONE_NS "http://www.google.com/session/phone"

/*! \brief Namespace for Google Video description */
#define GOOGLE_VIDEO_NS "http://www.google.com/session/video"

/*! \brief Namespace for XMPP stanzas */
#define XMPP_STANZAS_NS "urn:ietf:params:xml:ns:xmpp-stanzas"

/*! \brief The various transport methods supported, from highest priority to lowest priority when doing fallback */
enum jingle_transport {
	JINGLE_TRANSPORT_ICE_UDP = 3,   /*!< XEP-0176 */
	JINGLE_TRANSPORT_GOOGLE_V2 = 2, /*!< https://developers.google.com/talk/call_signaling */
	JINGLE_TRANSPORT_GOOGLE_V1 = 1, /*!< Undocumented initial Google specification */
	JINGLE_TRANSPORT_NONE = 0,      /*!< No transport specified */
};

/*! \brief Endpoint state information */
struct jingle_endpoint_state {
	struct ao2_container *sessions; /*!< Active sessions to or from the endpoint */
};

/*! \brief Endpoint which contains configuration information and active sessions */
struct jingle_endpoint {
	AST_DECLARE_STRING_FIELDS(
		AST_STRING_FIELD(name);              /*!< Name of the endpoint */
		AST_STRING_FIELD(context);           /*!< Context to place incoming calls into */
		AST_STRING_FIELD(accountcode);       /*!< Account code */
		AST_STRING_FIELD(language);          /*!< Default language for prompts */
		AST_STRING_FIELD(musicclass);        /*!< Configured music on hold class */
		AST_STRING_FIELD(parkinglot);        /*!< Configured parking lot */
		);
	struct ast_xmpp_client *connection;     /*!< Connection to use for traffic */
	iksrule *rule;                          /*!< Active matching rule */
	unsigned int maxicecandidates;          /*!< Maximum number of ICE candidates we will offer */
	unsigned int maxpayloads;               /*!< Maximum number of payloads we will offer */
	struct ast_format_cap *cap;             /*!< Formats to use */
	ast_group_t callgroup;                  /*!< Call group */
	ast_group_t pickupgroup;                /*!< Pickup group */
	enum jingle_transport transport;        /*!< Default transport to use on outgoing sessions */
	struct jingle_endpoint_state *state;    /*!< Endpoint state information */
};

/*! \brief Session which contains information about an active session */
struct jingle_session {
	AST_DECLARE_STRING_FIELDS(
		AST_STRING_FIELD(sid);        /*!< Session identifier */
		AST_STRING_FIELD(audio_name); /*!< Name of the audio content */
		AST_STRING_FIELD(video_name); /*!< Name of the video content */
		);
	struct jingle_endpoint_state *state;  /*!< Endpoint we are associated with */
	struct ast_xmpp_client *connection;   /*!< Connection to use for traffic */
	enum jingle_transport transport;      /*!< Transport type to use for this session */
	unsigned int maxicecandidates;        /*!< Maximum number of ICE candidates we will offer */
	unsigned int maxpayloads;             /*!< Maximum number of payloads we will offer */
	char remote_original[XMPP_MAX_JIDLEN];/*!< Identifier of the original remote party (remote may have changed due to redirect) */
	char remote[XMPP_MAX_JIDLEN];         /*!< Identifier of the remote party */
	iksrule *rule;                        /*!< Session matching rule */
	struct ast_channel *owner;            /*!< Master Channel */
	struct ast_rtp_instance *rtp;         /*!< RTP audio session */
	struct ast_rtp_instance *vrtp;        /*!< RTP video session */
	struct ast_format_cap *cap;           /*!< Local codec capabilities */
	struct ast_format_cap *jointcap;      /*!< Joint codec capabilities */
	struct ast_format_cap *peercap;       /*!< Peer codec capabilities */
	unsigned int outgoing:1;              /*!< Whether this is an outgoing leg or not */
	unsigned int gone:1;                  /*!< In the eyes of Jingle this session is already gone */
	ast_callid callid;                    /*!< Bound session call-id */
};

static const char channel_type[] = "Motif";

struct jingle_config {
	struct ao2_container *endpoints; /*!< Configured endpoints */
};

static AO2_GLOBAL_OBJ_STATIC(globals);

static struct ast_sched_context *sched; /*!< Scheduling context for RTCP */

/* \brief Asterisk core interaction functions */
static struct ast_channel *jingle_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
static int jingle_sendtext(struct ast_channel *ast, const char *text);
static int jingle_digit_begin(struct ast_channel *ast, char digit);
static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static int jingle_call(struct ast_channel *ast, const char *dest, int timeout);
static int jingle_hangup(struct ast_channel *ast);
static int jingle_answer(struct ast_channel *ast);
static struct ast_frame *jingle_read(struct ast_channel *ast);
static int jingle_write(struct ast_channel *ast, struct ast_frame *f);
static int jingle_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static struct jingle_session *jingle_alloc(struct jingle_endpoint *endpoint, const char *from, const char *sid);

/*! \brief Action handlers */
static void jingle_action_session_initiate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
static void jingle_action_transport_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
static void jingle_action_session_accept(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
static void jingle_action_session_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
static void jingle_action_session_terminate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);

/*! \brief PBX interface structure for channel registration */
static struct ast_channel_tech jingle_tech = {
	.type = "Motif",
	.description = "Motif Jingle Channel Driver",
	.requester = jingle_request,
	.send_text = jingle_sendtext,
	.send_digit_begin = jingle_digit_begin,
	.send_digit_end = jingle_digit_end,
	.call = jingle_call,
	.hangup = jingle_hangup,
	.answer = jingle_answer,
	.read = jingle_read,
	.write = jingle_write,
	.write_video = jingle_write,
	.exception = jingle_read,
	.indicate = jingle_indicate,
	.fixup = jingle_fixup,
	.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
};

/*! \brief Defined handlers for different Jingle actions */
static const struct jingle_action_handler {
	const char *action;
	void (*handler)(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak);
} jingle_action_handlers[] = {
	/* Jingle actions */
	{ "session-initiate", jingle_action_session_initiate, },
	{ "transport-info", jingle_action_transport_info, },
	{ "session-accept", jingle_action_session_accept, },
	{ "session-info", jingle_action_session_info, },
	{ "session-terminate", jingle_action_session_terminate, },
	/* Google-V1 actions */
	{ "initiate", jingle_action_session_initiate, },
	{ "candidates", jingle_action_transport_info, },
	{ "accept", jingle_action_session_accept, },
	{ "terminate", jingle_action_session_terminate, },
	{ "reject", jingle_action_session_terminate, },
};

/*! \brief Reason text <-> cause code mapping */
static const struct jingle_reason_mapping {
	const char *reason;
	int cause;
} jingle_reason_mappings[] = {
	{ "busy", AST_CAUSE_BUSY, },
	{ "cancel", AST_CAUSE_CALL_REJECTED, },
	{ "connectivity-error", AST_CAUSE_INTERWORKING, },
	{ "decline", AST_CAUSE_CALL_REJECTED, },
	{ "expired", AST_CAUSE_NO_USER_RESPONSE, },
	{ "failed-transport", AST_CAUSE_PROTOCOL_ERROR, },
	{ "failed-application", AST_CAUSE_SWITCH_CONGESTION, },
	{ "general-error", AST_CAUSE_CONGESTION, },
	{ "gone", AST_CAUSE_NORMAL_CLEARING, },
	{ "incompatible-parameters", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
	{ "media-error", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
	{ "security-error", AST_CAUSE_PROTOCOL_ERROR, },
	{ "success", AST_CAUSE_NORMAL_CLEARING, },
	{ "timeout", AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, },
	{ "unsupported-applications", AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, },
	{ "unsupported-transports", AST_CAUSE_FACILITY_NOT_IMPLEMENTED, },
};

/*! \brief Hashing function for Jingle sessions */
static int jingle_session_hash(const void *obj, const int flags)
{
	const struct jingle_session *session = obj;
	const char *sid = obj;

	return ast_str_hash(flags & OBJ_KEY ? sid : session->sid);
}

/*! \brief Comparator function for Jingle sessions */
static int jingle_session_cmp(void *obj, void *arg, int flags)
{
	struct jingle_session *session1 = obj, *session2 = arg;
	const char *sid = arg;

	return !strcmp(session1->sid, flags & OBJ_KEY ? sid : session2->sid) ? CMP_MATCH | CMP_STOP : 0;
}

/*! \brief Destructor for Jingle endpoint state */
static void jingle_endpoint_state_destructor(void *obj)
{
	struct jingle_endpoint_state *state = obj;

	ao2_ref(state->sessions, -1);
}

/*! \brief Destructor for Jingle endpoints */
static void jingle_endpoint_destructor(void *obj)
{
	struct jingle_endpoint *endpoint = obj;

	if (endpoint->rule) {
		iks_filter_remove_rule(endpoint->connection->filter, endpoint->rule);
	}

	if (endpoint->connection) {
		ast_xmpp_client_unref(endpoint->connection);
	}

	ao2_cleanup(endpoint->cap);
	ao2_ref(endpoint->state, -1);

	ast_string_field_free_memory(endpoint);
}

/*! \brief Find function for Jingle endpoints */
static void *jingle_endpoint_find(struct ao2_container *tmp_container, const char *category)
{
	return ao2_find(tmp_container, category, OBJ_KEY);
}

/*! \brief Allocator function for Jingle endpoint state */
static struct jingle_endpoint_state *jingle_endpoint_state_create(void)
{
	struct jingle_endpoint_state *state;

	if (!(state = ao2_alloc(sizeof(*state), jingle_endpoint_state_destructor))) {
		return NULL;
	}

	if (!(state->sessions = ao2_container_alloc(SESSION_BUCKETS, jingle_session_hash, jingle_session_cmp))) {
		ao2_ref(state, -1);
		return NULL;
	}

	return state;
}

/*! \brief State find/create function */
static struct jingle_endpoint_state *jingle_endpoint_state_find_or_create(const char *category)
{
	RAII_VAR(struct jingle_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
	RAII_VAR(struct jingle_endpoint *, endpoint, NULL, ao2_cleanup);

	if (!cfg || !cfg->endpoints || !(endpoint = jingle_endpoint_find(cfg->endpoints, category))) {
		return jingle_endpoint_state_create();
	}

	ao2_ref(endpoint->state, +1);
	return endpoint->state;
}

/*! \brief Allocator function for Jingle endpoints */
static void *jingle_endpoint_alloc(const char *cat)
{
	struct jingle_endpoint *endpoint;

	if (!(endpoint = ao2_alloc(sizeof(*endpoint), jingle_endpoint_destructor))) {
		return NULL;
	}

	if (ast_string_field_init(endpoint, 512)) {
		ao2_ref(endpoint, -1);
		return NULL;
	}

	if (!(endpoint->state = jingle_endpoint_state_find_or_create(cat))) {
		ao2_ref(endpoint, -1);
		return NULL;
	}

	ast_string_field_set(endpoint, name, cat);

	endpoint->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
	endpoint->transport = JINGLE_TRANSPORT_ICE_UDP;

	return endpoint;
}

/*! \brief Hashing function for Jingle endpoints */
static int jingle_endpoint_hash(const void *obj, const int flags)
{
	const struct jingle_endpoint *endpoint = obj;
	const char *name = obj;

	return ast_str_hash(flags & OBJ_KEY ? name : endpoint->name);
}

/*! \brief Comparator function for Jingle endpoints */
static int jingle_endpoint_cmp(void *obj, void *arg, int flags)
{
	struct jingle_endpoint *endpoint1 = obj, *endpoint2 = arg;
	const char *name = arg;

	return !strcmp(endpoint1->name, flags & OBJ_KEY ? name : endpoint2->name) ? CMP_MATCH | CMP_STOP : 0;
}

static struct aco_type endpoint_option = {
	.type = ACO_ITEM,
	.name = "endpoint",
	.category_match = ACO_BLACKLIST,
	.category = "^general$",
	.item_alloc = jingle_endpoint_alloc,
	.item_find = jingle_endpoint_find,
	.item_offset = offsetof(struct jingle_config, endpoints),
};

struct aco_type *endpoint_options[] = ACO_TYPES(&endpoint_option);

struct aco_file jingle_conf = {
	.filename = "motif.conf",
	.types = ACO_TYPES(&endpoint_option),
};

/*! \brief Destructor for Jingle sessions */
static void jingle_session_destructor(void *obj)
{
	struct jingle_session *session = obj;

	if (session->rule) {
		iks_filter_remove_rule(session->connection->filter, session->rule);
	}

	if (session->connection) {
		ast_xmpp_client_unref(session->connection);
	}

	if (session->rtp) {
		ast_rtp_instance_stop(session->rtp);
		ast_rtp_instance_destroy(session->rtp);
	}

	if (session->vrtp) {
		ast_rtp_instance_stop(session->vrtp);
		ast_rtp_instance_destroy(session->vrtp);
	}

	ao2_cleanup(session->cap);
	ao2_cleanup(session->jointcap);
	ao2_cleanup(session->peercap);

	ast_string_field_free_memory(session);
}

/*! \brief Destructor called when module configuration goes away */
static void jingle_config_destructor(void *obj)
{
	struct jingle_config *cfg = obj;
	ao2_cleanup(cfg->endpoints);
}

/*! \brief Allocator called when module configuration should appear */
static void *jingle_config_alloc(void)
{
	struct jingle_config *cfg;

	if (!(cfg = ao2_alloc(sizeof(*cfg), jingle_config_destructor))) {
		return NULL;
	}

	if (!(cfg->endpoints = ao2_container_alloc(ENDPOINT_BUCKETS, jingle_endpoint_hash, jingle_endpoint_cmp))) {
		ao2_ref(cfg, -1);
		return NULL;
	}

	return cfg;
}

CONFIG_INFO_STANDARD(cfg_info, globals, jingle_config_alloc,
		     .files = ACO_FILES(&jingle_conf),
	);

/*! \brief Function called by RTP engine to get local RTP peer */
static enum ast_rtp_glue_result jingle_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
{
	struct jingle_session *session = ast_channel_tech_pvt(chan);
	enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;

	if (!session->rtp) {
		return AST_RTP_GLUE_RESULT_FORBID;
	}

	ao2_ref(session->rtp, +1);
	*instance = session->rtp;

	return res;
}

/*! \brief Function called by RTP engine to get peer capabilities */
static void jingle_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
{
}

/*! \brief Function called by RTP engine to change where the remote party should send media */
static int jingle_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *tpeer, const struct ast_format_cap *cap, int nat_active)
{
	return -1;
}

/*! \brief Local glue for interacting with the RTP engine core */
static struct ast_rtp_glue jingle_rtp_glue = {
	.type = "Motif",
	.get_rtp_info = jingle_get_rtp_peer,
	.get_codec = jingle_get_codec,
	.update_peer = jingle_set_rtp_peer,
};

/*! \brief Set the channel owner on the \ref jingle_session object and related objects */
static void jingle_set_owner(struct jingle_session *session, struct ast_channel *chan)
{
	session->owner = chan;
	if (session->rtp) {
		ast_rtp_instance_set_channel_id(session->rtp, session->owner ? ast_channel_uniqueid(session->owner) : "");
	}
	if (session->vrtp) {
		ast_rtp_instance_set_channel_id(session->vrtp, session->owner ? ast_channel_uniqueid(session->owner) : "");
	}
}

/*! \brief Internal helper function which enables video support on a sesson if possible */
static void jingle_enable_video(struct jingle_session *session)
{
	struct ast_sockaddr tmp;
	struct ast_rtp_engine_ice *ice;

	/* If video is already present don't do anything */
	if (session->vrtp) {
		return;
	}

	/* If there are no configured video codecs do not turn video support on, it just won't work */
	if (!ast_format_cap_has_type(session->cap, AST_MEDIA_TYPE_VIDEO)) {
		return;
	}

	ast_sockaddr_parse(&tmp, "0.0.0.0", 0);

	if (!(session->vrtp = ast_rtp_instance_new("asterisk", sched, &tmp, NULL))) {
		return;
	}

	ast_rtp_instance_set_prop(session->vrtp, AST_RTP_PROPERTY_RTCP, 1);
	ast_rtp_instance_set_channel_id(session->vrtp, ast_channel_uniqueid(session->owner));
	ast_channel_set_fd(session->owner, 2, ast_rtp_instance_fd(session->vrtp, 0));
	ast_channel_set_fd(session->owner, 3, ast_rtp_instance_fd(session->vrtp, 1));
	ast_rtp_codecs_set_framing(ast_rtp_instance_get_codecs(session->vrtp),
		ast_format_cap_get_framing(session->cap));
	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2 && (ice = ast_rtp_instance_get_ice(session->vrtp))) {
		ice->stop(session->vrtp);
	}
}

/*! \brief Internal helper function used to allocate Jingle session on an endpoint */
static struct jingle_session *jingle_alloc(struct jingle_endpoint *endpoint, const char *from, const char *sid)
{
	struct jingle_session *session;
	ast_callid callid;
	struct ast_sockaddr tmp;

	if (!(session = ao2_alloc(sizeof(*session), jingle_session_destructor))) {
		return NULL;
	}

	callid = ast_read_threadstorage_callid();
	session->callid = (callid ? callid : ast_create_callid());

	if (ast_string_field_init(session, 512)) {
		ao2_ref(session, -1);
		return NULL;
	}

	if (!ast_strlen_zero(from)) {
		ast_copy_string(session->remote_original, from, sizeof(session->remote_original));
		ast_copy_string(session->remote, from, sizeof(session->remote));
	}

	if (ast_strlen_zero(sid)) {
		ast_string_field_build(session, sid, "%08lx%08lx", (unsigned long)ast_random(), (unsigned long)ast_random());
		session->outgoing = 1;
		ast_string_field_set(session, audio_name, "audio");
		ast_string_field_set(session, video_name, "video");
	} else {
		ast_string_field_set(session, sid, sid);
	}

	ao2_ref(endpoint->state, +1);
	session->state = endpoint->state;
	ao2_ref(endpoint->connection, +1);
	session->connection = endpoint->connection;
	session->transport = endpoint->transport;

	if (!(session->cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
	    !(session->jointcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
	    !(session->peercap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
	    !session->callid) {
		ao2_ref(session, -1);
		return NULL;
	}

	ast_format_cap_append_from_cap(session->cap, endpoint->cap, AST_MEDIA_TYPE_UNKNOWN);

	/* While we rely on res_xmpp for communication we still need a temporary ast_sockaddr to tell the RTP engine
	 * that we want IPv4 */
	ast_sockaddr_parse(&tmp, "0.0.0.0", 0);

	/* Sessions always carry audio, but video is optional so don't enable it here */
	if (!(session->rtp = ast_rtp_instance_new("asterisk", sched, &tmp, NULL))) {
		ao2_ref(session, -1);
		return NULL;
	}
	ast_rtp_instance_set_prop(session->rtp, AST_RTP_PROPERTY_RTCP, 1);
	ast_rtp_instance_set_prop(session->rtp, AST_RTP_PROPERTY_DTMF, 1);

	session->maxicecandidates = endpoint->maxicecandidates;
	session->maxpayloads = endpoint->maxpayloads;

	return session;
}

/*! \brief Function called to create a new Jingle Asterisk channel */
static struct ast_channel *jingle_new(struct jingle_endpoint *endpoint, struct jingle_session *session, int state, const char *title, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *cid_name)
{
	struct ast_channel *chan;
	const char *str = S_OR(title, session->remote);
	struct ast_format_cap *caps;
	struct ast_format *tmpfmt;

	if (!ast_format_cap_count(session->cap)) {
		return NULL;
	}

	caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
	if (!caps) {
		return NULL;
	}

	if (!(chan = ast_channel_alloc_with_endpoint(1, state, S_OR(title, ""), S_OR(cid_name, ""), "", "", "", assignedids, requestor, 0, endpoint->connection->endpoint, "Motif/%s-%04lx", str, (unsigned long)(ast_random() & 0xffff)))) {
		ao2_ref(caps, -1);
		return NULL;
	}

	ast_channel_stage_snapshot(chan);

	ast_channel_tech_set(chan, &jingle_tech);
	ast_channel_tech_pvt_set(chan, session);
	jingle_set_owner(session, chan);

	ast_channel_callid_set(chan, session->callid);

	ast_format_cap_append_from_cap(caps, session->cap, AST_MEDIA_TYPE_UNKNOWN);
	ast_channel_nativeformats_set(chan, caps);
	ao2_ref(caps, -1);

	if (session->rtp) {
		struct ast_rtp_engine_ice *ice;

		ast_channel_set_fd(chan, 0, ast_rtp_instance_fd(session->rtp, 0));
		ast_channel_set_fd(chan, 1, ast_rtp_instance_fd(session->rtp, 1));
		ast_rtp_codecs_set_framing(ast_rtp_instance_get_codecs(session->rtp),
			ast_format_cap_get_framing(session->cap));

		if (((session->transport == JINGLE_TRANSPORT_GOOGLE_V2) ||
		     (session->transport == JINGLE_TRANSPORT_GOOGLE_V1)) &&
		    (ice = ast_rtp_instance_get_ice(session->rtp))) {
			/* We stop built in ICE support because we need to fall back to old old old STUN support */
			ice->stop(session->rtp);
		}
	}

	if (state == AST_STATE_RING) {
		ast_channel_rings_set(chan, 1);
	}

	ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);

	tmpfmt = ast_format_cap_get_format(session->cap, 0);
	ast_channel_set_writeformat(chan, tmpfmt);
	ast_channel_set_rawwriteformat(chan, tmpfmt);
	ast_channel_set_readformat(chan, tmpfmt);
	ast_channel_set_rawreadformat(chan, tmpfmt);
	ao2_ref(tmpfmt, -1);

	ao2_lock(endpoint);

	ast_channel_callgroup_set(chan, endpoint->callgroup);
	ast_channel_pickupgroup_set(chan, endpoint->pickupgroup);

	if (!ast_strlen_zero(endpoint->accountcode)) {
		ast_channel_accountcode_set(chan, endpoint->accountcode);
	}

	if (!ast_strlen_zero(endpoint->language)) {
		ast_channel_language_set(chan, endpoint->language);
	}

	if (!ast_strlen_zero(endpoint->musicclass)) {
		ast_channel_musicclass_set(chan, endpoint->musicclass);
	}

	ast_channel_context_set(chan, endpoint->context);
	if (ast_exists_extension(NULL, endpoint->context, endpoint->name, 1, NULL)) {
		ast_channel_exten_set(chan, endpoint->name);
	} else {
		ast_channel_exten_set(chan, "s");
	}
	ast_channel_priority_set(chan, 1);

	ao2_unlock(endpoint);

	ast_channel_stage_snapshot_done(chan);
	ast_channel_unlock(chan);

	return chan;
}

/*! \brief Internal helper function which sends a response */
static void jingle_send_response(struct ast_xmpp_client *connection, ikspak *pak)
{
	iks *response;

	if (!(response = iks_new("iq"))) {
		ast_log(LOG_ERROR, "Unable to allocate an IKS response stanza\n");
		return;
	}

	iks_insert_attrib(response, "type", "result");
	iks_insert_attrib(response, "from", connection->jid->full);
	iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
	iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));

	ast_xmpp_client_send(connection, response);

	iks_delete(response);
}

/*! \brief Internal helper function which sends an error response */
static void jingle_send_error_response(struct ast_xmpp_client *connection, ikspak *pak, const char *type, const char *reasonstr, const char *reasonstr2)
{
	iks *response, *error = NULL, *reason = NULL, *reason2 = NULL;

	if (!(response = iks_new("iq")) ||
	    !(error = iks_new("error")) ||
	    !(reason = iks_new(reasonstr))) {
		ast_log(LOG_ERROR, "Unable to allocate IKS error response stanzas\n");
		goto end;
	}

	iks_insert_attrib(response, "type", "error");
	iks_insert_attrib(response, "from", connection->jid->full);
	iks_insert_attrib(response, "to", iks_find_attrib(pak->x, "from"));
	iks_insert_attrib(response, "id", iks_find_attrib(pak->x, "id"));

	iks_insert_attrib(error, "type", type);
	iks_insert_node(error, reason);

	if (!ast_strlen_zero(reasonstr2) && (reason2 = iks_new(reasonstr2))) {
		iks_insert_node(error, reason2);
	}

	iks_insert_node(response, error);

	ast_xmpp_client_send(connection, response);
end:
	iks_delete(reason2);
	iks_delete(reason);
	iks_delete(error);
	iks_delete(response);
}

/*! \brief Internal helper function which adds ICE-UDP candidates to a transport node */
static int jingle_add_ice_udp_candidates_to_transport(struct ast_rtp_instance *rtp, iks *transport, iks **candidates, unsigned int maximum)
{
	struct ast_rtp_engine_ice *ice;
	struct ao2_container *local_candidates;
	struct ao2_iterator it;
	struct ast_rtp_engine_ice_candidate *candidate;
	int i = 0, res = 0;

	if (!(ice = ast_rtp_instance_get_ice(rtp)) || !(local_candidates = ice->get_local_candidates(rtp))) {
		ast_log(LOG_ERROR, "Unable to add ICE-UDP candidates as ICE support not available or no candidates available\n");
		return -1;
	}

	iks_insert_attrib(transport, "xmlns", JINGLE_ICE_UDP_NS);
	iks_insert_attrib(transport, "pwd", ice->get_password(rtp));
	iks_insert_attrib(transport, "ufrag", ice->get_ufrag(rtp));

	it = ao2_iterator_init(local_candidates, 0);

	while ((candidate = ao2_iterator_next(&it)) && (i < maximum)) {
		iks *local_candidate;
		char tmp[30];

		if (!(local_candidate = iks_new("candidate"))) {
			res = -1;
			ast_log(LOG_ERROR, "Unable to allocate IKS candidate stanza for ICE-UDP transport\n");
			break;
		}

		snprintf(tmp, sizeof(tmp), "%u", candidate->id);
		iks_insert_attrib(local_candidate, "component", tmp);
		snprintf(tmp, sizeof(tmp), "%d", ast_str_hash(candidate->foundation));
		iks_insert_attrib(local_candidate, "foundation", tmp);
		iks_insert_attrib(local_candidate, "generation", "0");
		iks_insert_attrib(local_candidate, "network", "0");
		snprintf(tmp, sizeof(tmp), "%04lx", (unsigned long)(ast_random() & 0xffff));
		iks_insert_attrib(local_candidate, "id", tmp);
		iks_insert_attrib(local_candidate, "ip", ast_sockaddr_stringify_host(&candidate->address));
		iks_insert_attrib(local_candidate, "port", ast_sockaddr_stringify_port(&candidate->address));
		snprintf(tmp, sizeof(tmp), "%d", candidate->priority);
		iks_insert_attrib(local_candidate, "priority", tmp);
		iks_insert_attrib(local_candidate, "protocol", "udp");

		if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
			iks_insert_attrib(local_candidate, "type", "host");
		} else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
			iks_insert_attrib(local_candidate, "type", "srflx");
		} else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_RELAYED) {
			iks_insert_attrib(local_candidate, "type", "relay");
		}

		iks_insert_node(transport, local_candidate);
		candidates[i++] = local_candidate;
	}

	ao2_iterator_destroy(&it);
	ao2_ref(local_candidates, -1);

	return res;
}

/*! \brief Internal helper function which adds Google candidates to a transport node */
static int jingle_add_google_candidates_to_transport(struct ast_rtp_instance *rtp, iks *transport, iks **candidates, unsigned int video, enum jingle_transport transport_type, unsigned int maximum)
{
	struct ast_rtp_engine_ice *ice;
	struct ao2_container *local_candidates;
	struct ao2_iterator it;
	struct ast_rtp_engine_ice_candidate *candidate;
	int i = 0, res = 0;

	if (!(ice = ast_rtp_instance_get_ice(rtp)) || !(local_candidates = ice->get_local_candidates(rtp))) {
		ast_log(LOG_ERROR, "Unable to add Google ICE candidates as ICE support not available or no candidates available\n");
		return -1;
	}

	if (transport_type != JINGLE_TRANSPORT_GOOGLE_V1) {
		iks_insert_attrib(transport, "xmlns", GOOGLE_TRANSPORT_NS);
	}

	it = ao2_iterator_init(local_candidates, 0);

	while ((candidate = ao2_iterator_next(&it)) && (i < maximum)) {
		iks *local_candidate;
		/* In Google land a username is 16 bytes, explicitly */
		char ufrag[17] = "";

		if (!(local_candidate = iks_new("candidate"))) {
			res = -1;
			ast_log(LOG_ERROR, "Unable to allocate IKS candidate stanza for Google ICE transport\n");
			break;
		}

		if (candidate->id == 1) {
			iks_insert_attrib(local_candidate, "name", !video ? "rtp" : "video_rtp");
		} else if (candidate->id == 2) {
			iks_insert_attrib(local_candidate, "name", !video ? "rtcp" : "video_rtcp");
		} else {
			iks_delete(local_candidate);
			continue;
		}

		iks_insert_attrib(local_candidate, "address", ast_sockaddr_stringify_host(&candidate->address));
		iks_insert_attrib(local_candidate, "port", ast_sockaddr_stringify_port(&candidate->address));

		if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_HOST) {
			iks_insert_attrib(local_candidate, "preference", "0.95");
			iks_insert_attrib(local_candidate, "type", "local");
		} else if (candidate->type == AST_RTP_ICE_CANDIDATE_TYPE_SRFLX) {
			iks_insert_attrib(local_candidate, "preference", "0.9");
			iks_insert_attrib(local_candidate, "type", "stun");
		}

		iks_insert_attrib(local_candidate, "protocol", "udp");
		iks_insert_attrib(local_candidate, "network", "0");
		snprintf(ufrag, sizeof(ufrag), "%s", ice->get_ufrag(rtp));
		iks_insert_attrib(local_candidate, "username", ufrag);
		iks_insert_attrib(local_candidate, "generation", "0");

		if (transport_type == JINGLE_TRANSPORT_GOOGLE_V1) {
			iks_insert_attrib(local_candidate, "password", "");
			iks_insert_attrib(local_candidate, "foundation", "0");
			iks_insert_attrib(local_candidate, "component", "1");
		} else {
			iks_insert_attrib(local_candidate, "password", ice->get_password(rtp));
		}

		/* You may notice a lack of relay support up above - this is because we don't support it for use with
		 * the Google talk transport due to their arcane support. */

		iks_insert_node(transport, local_candidate);
		candidates[i++] = local_candidate;
	}

	ao2_iterator_destroy(&it);
	ao2_ref(local_candidates, -1);

	return res;
}

/*! \brief Internal function which sends a session-terminate message */
static void jingle_send_session_terminate(struct jingle_session *session, const char *reasontext)
{
	iks *iq = NULL, *jingle = NULL, *reason = NULL, *text = NULL;

	if (!(iq = iks_new("iq")) || !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle")) ||
	    !(reason = iks_new("reason")) || !(text = iks_new(reasontext))) {
		ast_log(LOG_ERROR, "Failed to allocate stanzas for session-terminate message on session '%s'\n", session->sid);
		goto end;
	}

	iks_insert_attrib(iq, "to", session->remote);
	iks_insert_attrib(iq, "type", "set");
	iks_insert_attrib(iq, "id", session->connection->mid);
	ast_xmpp_increment_mid(session->connection->mid);

	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
		iks_insert_attrib(jingle, "type", "terminate");
		iks_insert_attrib(jingle, "id", session->sid);
		iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
		iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : session->remote);
	} else {
		iks_insert_attrib(jingle, "action", "session-terminate");
		iks_insert_attrib(jingle, "sid", session->sid);
		iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
	}

	iks_insert_node(iq, jingle);
	iks_insert_node(jingle, reason);
	iks_insert_node(reason, text);

	ast_xmpp_client_send(session->connection, iq);

end:
	iks_delete(text);
	iks_delete(reason);
	iks_delete(jingle);
	iks_delete(iq);
}

/*! \brief Internal function which sends a session-info message */
static void jingle_send_session_info(struct jingle_session *session, const char *info)
{
	iks *iq = NULL, *jingle = NULL, *text = NULL;

	/* Google-V1 has no way to send informational messages so don't even bother trying */
	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
		return;
	}

	if (!(iq = iks_new("iq")) || !(jingle = iks_new("jingle")) || !(text = iks_new(info))) {
		ast_log(LOG_ERROR, "Failed to allocate stanzas for session-info message on session '%s'\n", session->sid);
		goto end;
	}

	iks_insert_attrib(iq, "to", session->remote);
	iks_insert_attrib(iq, "type", "set");
	iks_insert_attrib(iq, "id", session->connection->mid);
	ast_xmpp_increment_mid(session->connection->mid);

	iks_insert_attrib(jingle, "action", "session-info");
	iks_insert_attrib(jingle, "sid", session->sid);
	iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
	iks_insert_node(iq, jingle);
	iks_insert_node(jingle, text);

	ast_xmpp_client_send(session->connection, iq);

end:
	iks_delete(text);
	iks_delete(jingle);
	iks_delete(iq);
}

/*! \internal
 *
 * \brief Locks both pvt and pvt owner if owner is present.
 *
 * \note This function gives a ref to pvt->owner if it is present and locked.
 *       This reference must be decremented after pvt->owner is unlocked.
 *
 * \note This function will never give you up,
 * \note This function will never let you down.
 * \note This function will run around and desert you.
 *
 * \pre pvt is not locked
 * \post pvt is locked
 * \post pvt->owner is locked and its reference count is increased (if pvt->owner is not NULL)
 *
 * \returns a pointer to the locked and reffed pvt->owner channel if it exists.
 */
static struct ast_channel *jingle_session_lock_full(struct jingle_session *pvt)
{
	struct ast_channel *chan;

	/* Locking is simple when it is done right.  If you see a deadlock resulting
	 * in this function, it is not this function's fault, Your problem exists elsewhere.
	 * This function is perfect... seriously. */
	for (;;) {
		/* First, get the channel and grab a reference to it */
		ao2_lock(pvt);
		chan = pvt->owner;
		if (chan) {
			/* The channel can not go away while we hold the pvt lock.
			 * Give the channel a ref so it will not go away after we let
			 * the pvt lock go. */
			ast_channel_ref(chan);
		} else {
			/* no channel, return pvt locked */
			return NULL;
		}

		/* We had to hold the pvt lock while getting a ref to the owner channel
		 * but now we have to let this lock go in order to preserve proper
		 * locking order when grabbing the channel lock */
		ao2_unlock(pvt);

		/* Look, no deadlock avoidance, hooray! */
		ast_channel_lock(chan);
		ao2_lock(pvt);
		if (pvt->owner == chan) {
			/* done */
			break;
		}

		/* If the owner changed while everything was unlocked, no problem,
		 * just start over and everthing will work.  This is rare, do not be
		 * confused by this loop and think this it is an expensive operation.
		 * The majority of the calls to this function will never involve multiple
		 * executions of this loop. */
		ast_channel_unlock(chan);
		ast_channel_unref(chan);
		ao2_unlock(pvt);
	}

	/* If owner exists, it is locked and reffed */
	return pvt->owner;
}

/*! \brief Helper function which queues a hangup frame with cause code */
static void jingle_queue_hangup_with_cause(struct jingle_session *session, int cause)
{
	struct ast_channel *chan;

	if ((chan = jingle_session_lock_full(session))) {
		ast_debug(3, "Hanging up channel '%s' with cause '%d'\n", ast_channel_name(chan), cause);
		ast_queue_hangup_with_cause(chan, cause);
		ast_channel_unlock(chan);
		ast_channel_unref(chan);
	}
	ao2_unlock(session);
}

/*! \brief Internal function which sends a transport-info message */
static void jingle_send_transport_info(struct jingle_session *session, const char *from)
{
	iks *iq, *jingle = NULL, *audio = NULL, *audio_transport = NULL, *video = NULL, *video_transport = NULL;
	iks *audio_candidates[session->maxicecandidates], *video_candidates[session->maxicecandidates];
	int i, res = 0;

	if (!(iq = iks_new("iq")) ||
	    !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle"))) {
		iks_delete(iq);
		jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
		ast_log(LOG_ERROR, "Failed to allocate stanzas for transport-info message, hanging up session '%s'\n", session->sid);
		return;
	}

	memset(audio_candidates, 0, sizeof(audio_candidates));
	memset(video_candidates, 0, sizeof(video_candidates));

	iks_insert_attrib(iq, "from", session->connection->jid->full);
	iks_insert_attrib(iq, "to", from);
	iks_insert_attrib(iq, "type", "set");
	iks_insert_attrib(iq, "id", session->connection->mid);
	ast_xmpp_increment_mid(session->connection->mid);

	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
		iks_insert_attrib(jingle, "type", "candidates");
		iks_insert_attrib(jingle, "id", session->sid);
		iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
		iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : from);
	} else {
		iks_insert_attrib(jingle, "action", "transport-info");
		iks_insert_attrib(jingle, "sid", session->sid);
		iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
	}
	iks_insert_node(iq, jingle);

	if (session->rtp) {
		if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
			/* V1 protocol has the candidates directly in the session */
			res = jingle_add_google_candidates_to_transport(session->rtp, jingle, audio_candidates, 0, session->transport, session->maxicecandidates);
		} else if ((audio = iks_new("content")) && (audio_transport = iks_new("transport"))) {
			iks_insert_attrib(audio, "creator", session->outgoing ? "initiator" : "responder");
			iks_insert_attrib(audio, "name", session->audio_name);
			iks_insert_node(jingle, audio);
			iks_insert_node(audio, audio_transport);

			if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
				res = jingle_add_ice_udp_candidates_to_transport(session->rtp, audio_transport, audio_candidates, session->maxicecandidates);
			} else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
				res = jingle_add_google_candidates_to_transport(session->rtp, audio_transport, audio_candidates, 0, session->transport,
										session->maxicecandidates);
			}
		} else {
			res = -1;
		}
	}

	if ((session->transport != JINGLE_TRANSPORT_GOOGLE_V1) && !res && session->vrtp) {
		if ((video = iks_new("content")) && (video_transport = iks_new("transport"))) {
			iks_insert_attrib(video, "creator", session->outgoing ? "initiator" : "responder");
			iks_insert_attrib(video, "name", session->video_name);
			iks_insert_node(jingle, video);
			iks_insert_node(video, video_transport);

			if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
				res = jingle_add_ice_udp_candidates_to_transport(session->vrtp, video_transport, video_candidates, session->maxicecandidates);
			} else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
				res = jingle_add_google_candidates_to_transport(session->vrtp, video_transport, video_candidates, 1, session->transport,
										session->maxicecandidates);
			}
		} else {
			res = -1;
		}
	}

	if (!res) {
		ast_xmpp_client_send(session->connection, iq);
	} else {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
	}

	/* Clean up after ourselves */
	for (i = 0; i < session->maxicecandidates; i++) {
		iks_delete(video_candidates[i]);
		iks_delete(audio_candidates[i]);
	}

	iks_delete(video_transport);
	iks_delete(video);
	iks_delete(audio_transport);
	iks_delete(audio);
	iks_delete(jingle);
	iks_delete(iq);
}

/*! \brief Internal helper function which adds payloads to a description */
static int jingle_add_payloads_to_description(struct jingle_session *session, struct ast_rtp_instance *rtp, iks *description, iks **payloads, enum ast_media_type type)
{
	int x = 0, i = 0, res = 0;

	for (x = 0; (x < ast_format_cap_count(session->jointcap)) && (i < (session->maxpayloads - 2)); x++) {
		struct ast_format *format = ast_format_cap_get_format(session->jointcap, x);
		int rtp_code;
		iks *payload;
		char tmp[32];

		if (ast_format_get_type(format) != type) {
			ao2_ref(format, -1);
			continue;
		}

		if (((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(rtp), 1, format, 0)) == -1) ||
		    (!(payload = iks_new("payload-type")))) {
			ao2_ref(format, -1);
			return -1;
		}

		if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
			iks_insert_attrib(payload, "xmlns", GOOGLE_PHONE_NS);
		}

		snprintf(tmp, sizeof(tmp), "%d", rtp_code);
		iks_insert_attrib(payload, "id", tmp);
		iks_insert_attrib(payload, "name", ast_rtp_lookup_mime_subtype2(1, format, 0, 0));
		iks_insert_attrib(payload, "channels", "1");

		if ((ast_format_cmp(format, ast_format_g722) == AST_FORMAT_CMP_EQUAL) &&
			((session->transport == JINGLE_TRANSPORT_GOOGLE_V1) || (session->transport == JINGLE_TRANSPORT_GOOGLE_V2))) {
			iks_insert_attrib(payload, "clockrate", "16000");
		} else {
			snprintf(tmp, sizeof(tmp), "%u", ast_rtp_lookup_sample_rate2(1, format, 0));
			iks_insert_attrib(payload, "clockrate", tmp);
		}

		if ((type == AST_MEDIA_TYPE_VIDEO) && (session->transport == JINGLE_TRANSPORT_GOOGLE_V2)) {
			iks *parameter;

			/* Google requires these parameters to be set, but alas we can not give accurate values so use some safe defaults */
			if ((parameter = iks_new("parameter"))) {
				iks_insert_attrib(parameter, "name", "width");
				iks_insert_attrib(parameter, "value", "640");
				iks_insert_node(payload, parameter);
			}
			if ((parameter = iks_new("parameter"))) {
				iks_insert_attrib(parameter, "name", "height");
				iks_insert_attrib(parameter, "value", "480");
				iks_insert_node(payload, parameter);
			}
			if ((parameter = iks_new("parameter"))) {
				iks_insert_attrib(parameter, "name", "framerate");
				iks_insert_attrib(parameter, "value", "30");
				iks_insert_node(payload, parameter);
			}
		}

		iks_insert_node(description, payload);
		payloads[i++] = payload;

		ao2_ref(format, -1);
	}
	/* If this is for audio and there is room for RFC2833 add it in */
	if ((type == AST_MEDIA_TYPE_AUDIO) && (i < session->maxpayloads)) {
		iks *payload;

		if ((payload = iks_new("payload-type"))) {
			if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
				iks_insert_attrib(payload, "xmlns", GOOGLE_PHONE_NS);
			}

			iks_insert_attrib(payload, "id", "101");
			iks_insert_attrib(payload, "name", "telephone-event");
			iks_insert_attrib(payload, "channels", "1");
			iks_insert_attrib(payload, "clockrate", "8000");
			iks_insert_node(description, payload);
			payloads[i++] = payload;
		}
	}

	return res;
}

/*! \brief Helper function which adds content to a description */
static int jingle_add_content(struct jingle_session *session, iks *jingle, iks *content, iks *description, iks *transport,
			      const char *name, enum ast_media_type type, struct ast_rtp_instance *rtp, iks **payloads)
{
	int res = 0;

	if (session->transport != JINGLE_TRANSPORT_GOOGLE_V1) {
		iks_insert_attrib(content, "creator", session->outgoing ? "initiator" : "responder");
		iks_insert_attrib(content, "name", name);
		iks_insert_node(jingle, content);

		iks_insert_attrib(description, "xmlns", JINGLE_RTP_NS);
		if (type == AST_MEDIA_TYPE_AUDIO) {
			iks_insert_attrib(description, "media", "audio");
		} else if (type == AST_MEDIA_TYPE_VIDEO) {
			iks_insert_attrib(description, "media", "video");
		} else {
			return -1;
		}
		iks_insert_node(content, description);
	} else {
		iks_insert_attrib(description, "xmlns", GOOGLE_PHONE_NS);
		iks_insert_node(jingle, description);
	}

	if (!(res = jingle_add_payloads_to_description(session, rtp, description, payloads, type))) {
		if (session->transport == JINGLE_TRANSPORT_ICE_UDP) {
			iks_insert_attrib(transport, "xmlns", JINGLE_ICE_UDP_NS);
			iks_insert_node(content, transport);
		} else if (session->transport == JINGLE_TRANSPORT_GOOGLE_V2) {
			iks_insert_attrib(transport, "xmlns", GOOGLE_TRANSPORT_NS);
			iks_insert_node(content, transport);
		}
	}

	return res;
}

/*! \brief Internal function which sends a complete session message */
static void jingle_send_session_action(struct jingle_session *session, const char *action)
{
	iks *iq, *jingle, *audio = NULL, *audio_description = NULL, *video = NULL, *video_description = NULL;
	iks *audio_payloads[session->maxpayloads], *video_payloads[session->maxpayloads];
	iks *audio_transport = NULL, *video_transport = NULL;
	int i, res = 0;

	if (!(iq = iks_new("iq")) ||
	    !(jingle = iks_new(session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "session" : "jingle"))) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
		iks_delete(iq);
		return;
	}

	memset(audio_payloads, 0, sizeof(audio_payloads));
	memset(video_payloads, 0, sizeof(video_payloads));

	iks_insert_attrib(iq, "from", session->connection->jid->full);
	iks_insert_attrib(iq, "to", session->remote);
	iks_insert_attrib(iq, "type", "set");
	iks_insert_attrib(iq, "id", session->connection->mid);
	ast_xmpp_increment_mid(session->connection->mid);

	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
		iks_insert_attrib(jingle, "type", action);
		iks_insert_attrib(jingle, "id", session->sid);
		iks_insert_attrib(jingle, "xmlns", GOOGLE_SESSION_NS);
	} else {
		iks_insert_attrib(jingle, "action", action);
		iks_insert_attrib(jingle, "sid", session->sid);
		iks_insert_attrib(jingle, "xmlns", JINGLE_NS);
	}

	if (!strcasecmp(action, "session-initiate") || !strcasecmp(action, "initiate") || !strcasecmp(action, "accept")) {
		iks_insert_attrib(jingle, "initiator", session->outgoing ? session->connection->jid->full : session->remote);
	}

	iks_insert_node(iq, jingle);

	if (session->rtp && (audio = iks_new("content")) && (audio_description = iks_new("description")) &&
	    (audio_transport = iks_new("transport"))) {
		res = jingle_add_content(session, jingle, audio, audio_description, audio_transport, session->audio_name,
					 AST_MEDIA_TYPE_AUDIO, session->rtp, audio_payloads);
	} else {
		ast_log(LOG_ERROR, "Failed to allocate audio content stanzas for session '%s', hanging up\n", session->sid);
		res = -1;
	}

	if ((session->transport != JINGLE_TRANSPORT_GOOGLE_V1) && !res && session->vrtp) {
		if ((video = iks_new("content")) && (video_description = iks_new("description")) &&
		    (video_transport = iks_new("transport"))) {
			res = jingle_add_content(session, jingle, video, video_description, video_transport, session->video_name,
						 AST_MEDIA_TYPE_VIDEO, session->vrtp, video_payloads);
		} else {
			ast_log(LOG_ERROR, "Failed to allocate video content stanzas for session '%s', hanging up\n", session->sid);
			res = -1;
		}
	}

	if (!res) {
		ast_xmpp_client_send(session->connection, iq);
	} else {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
	}

	iks_delete(video_transport);
	iks_delete(audio_transport);

	for (i = 0; i < session->maxpayloads; i++) {
		iks_delete(video_payloads[i]);
		iks_delete(audio_payloads[i]);
	}

	iks_delete(video_description);
	iks_delete(video);
	iks_delete(audio_description);
	iks_delete(audio);
	iks_delete(jingle);
	iks_delete(iq);
}

/*! \brief Internal function which sends a session-inititate message */
static void jingle_send_session_initiate(struct jingle_session *session)
{
	jingle_send_session_action(session, session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "initiate" : "session-initiate");
}

/*! \brief Internal function which sends a session-accept message */
static void jingle_send_session_accept(struct jingle_session *session)
{
	jingle_send_session_action(session, session->transport == JINGLE_TRANSPORT_GOOGLE_V1 ? "accept" : "session-accept");
}

/*! \brief Callback for when a response is received for an outgoing session-initiate message */
static int jingle_outgoing_hook(void *data, ikspak *pak)
{
	struct jingle_session *session = data;
	iks *error = iks_find(pak->x, "error"), *redirect;

	/* In all cases this hook is done with */
	iks_filter_remove_rule(session->connection->filter, session->rule);
	session->rule = NULL;

	ast_callid_threadassoc_add(session->callid);

	/* If no error occurred they accepted our session-initiate message happily */
	if (!error) {
		struct ast_channel *chan;

		if ((chan = jingle_session_lock_full(session))) {
			ast_queue_control(chan, AST_CONTROL_PROCEEDING);
			ast_channel_unlock(chan);
			ast_channel_unref(chan);
		}
		ao2_unlock(session);

		jingle_send_transport_info(session, iks_find_attrib(pak->x, "from"));

		goto end;
	}

	/* Assume that because this is an error the session is gone, there is only one case where this is incorrect - a redirect */
	session->gone = 1;

	/* Map the error we received to an appropriate cause code and hang up the channel */
	if ((redirect = iks_find_with_attrib(error, "redirect", "xmlns", XMPP_STANZAS_NS))) {
		iks *to = iks_child(redirect);
		char *target;

		if (to && (target = iks_name(to)) && !ast_strlen_zero(target)) {
			/* Make the xmpp: go away if it is present */
			if (!strncmp(target, "xmpp:", 5)) {
				target += 5;
			}

			/* This is actually a fairly simple operation - we update the remote and send another session-initiate */
			ast_copy_string(session->remote, target, sizeof(session->remote));

			/* Add a new hook so we can get the status of redirected session */
			session->rule = iks_filter_add_rule(session->connection->filter, jingle_outgoing_hook, session,
							    IKS_RULE_ID, session->connection->mid, IKS_RULE_DONE);

			jingle_send_session_initiate(session);

			session->gone = 0;
		} else {
			jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
		}
	} else if (iks_find_with_attrib(error, "service-unavailable", "xmlns", XMPP_STANZAS_NS)) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_CONGESTION);
	} else if (iks_find_with_attrib(error, "resource-constraint", "xmlns", XMPP_STANZAS_NS)) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
	} else if (iks_find_with_attrib(error, "bad-request", "xmlns", XMPP_STANZAS_NS)) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
	} else if (iks_find_with_attrib(error, "remote-server-not-found", "xmlns", XMPP_STANZAS_NS)) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_NO_ROUTE_DESTINATION);
	} else if (iks_find_with_attrib(error, "feature-not-implemented", "xmlns", XMPP_STANZAS_NS)) {
		/* Assume that this occurred because the remote side does not support our transport, so drop it down one and try again */
		session->transport--;

		/* If we still have a viable transport mechanism re-send the session-initiate */
		if (session->transport != JINGLE_TRANSPORT_NONE) {
			struct ast_rtp_engine_ice *ice;

			if (((session->transport == JINGLE_TRANSPORT_GOOGLE_V2) ||
			     (session->transport == JINGLE_TRANSPORT_GOOGLE_V1)) &&
			    (ice = ast_rtp_instance_get_ice(session->rtp))) {
				/* We stop built in ICE support because we need to fall back to old old old STUN support */
				ice->stop(session->rtp);
			}

			/* Re-send the message to the *original* target and not a redirected one */
			ast_copy_string(session->remote, session->remote_original, sizeof(session->remote));

			session->rule = iks_filter_add_rule(session->connection->filter, jingle_outgoing_hook, session,
							    IKS_RULE_ID, session->connection->mid, IKS_RULE_DONE);

			jingle_send_session_initiate(session);

			session->gone = 0;
		} else {
			/* Otherwise we have exhausted all transports */
			jingle_queue_hangup_with_cause(session, AST_CAUSE_FACILITY_NOT_IMPLEMENTED);
		}
	} else {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
	}

end:
	ast_callid_threadassoc_remove();

	return IKS_FILTER_EAT;
}

/*! \brief Function called by core when we should answer a Jingle session */
static int jingle_answer(struct ast_channel *ast)
{
	struct jingle_session *session = ast_channel_tech_pvt(ast);

	/* The channel has already been answered so we don't need to do anything */
	if (ast_channel_state(ast) == AST_STATE_UP) {
		return 0;
	}

	jingle_send_session_accept(session);

	return 0;
}

/*! \brief Function called by core to read any waiting frames */
static struct ast_frame *jingle_read(struct ast_channel *ast)
{
	struct jingle_session *session = ast_channel_tech_pvt(ast);
	struct ast_frame *frame = &ast_null_frame;

	switch (ast_channel_fdno(ast)) {
	case 0:
		if (session->rtp) {
			frame = ast_rtp_instance_read(session->rtp, 0);
		}
		break;
	case 1:
		if (session->rtp) {
			frame = ast_rtp_instance_read(session->rtp, 1);
		}
		break;
	case 2:
		if (session->vrtp) {
			frame = ast_rtp_instance_read(session->vrtp, 0);
		}
		break;
	case 3:
		if (session->vrtp) {
			frame = ast_rtp_instance_read(session->vrtp, 1);
		}
		break;
	default:
		break;
	}

	if (frame && frame->frametype == AST_FRAME_VOICE &&
	    ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
		if (ast_format_cap_iscompatible_format(session->jointcap, frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
			ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
				  ast_format_get_name(frame->subclass.format), ast_channel_name(ast));
			ast_frfree(frame);
			frame = &ast_null_frame;
		} else {
			struct ast_format_cap *caps;

			ast_debug(1, "Oooh, format changed to %s\n",
				  ast_format_get_name(frame->subclass.format));

			caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
			if (caps) {
				ast_format_cap_append(caps, frame->subclass.format, 0);
				ast_channel_nativeformats_set(ast, caps);
				ao2_ref(caps, -1);
			}
			ast_set_read_format(ast, ast_channel_readformat(ast));
			ast_set_write_format(ast, ast_channel_writeformat(ast));
		}
	}

	return frame;
}

/*! \brief Function called by core to write frames */
static int jingle_write(struct ast_channel *ast, struct ast_frame *frame)
{
	struct jingle_session *session = ast_channel_tech_pvt(ast);
	int res = 0;

	switch (frame->frametype) {
	case AST_FRAME_VOICE:
		if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
			struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);

			ast_log(LOG_WARNING,
				"Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
				ast_format_get_name(frame->subclass.format),
				ast_format_cap_get_names(ast_channel_nativeformats(ast), &codec_buf),
				ast_format_get_name(ast_channel_readformat(ast)),
				ast_format_get_name(ast_channel_writeformat(ast)));
			return 0;
		}
		if (session && session->rtp) {
			res = ast_rtp_instance_write(session->rtp, frame);
		}
		break;
	case AST_FRAME_VIDEO:
		if (session && session->vrtp) {
			res = ast_rtp_instance_write(session->vrtp, frame);
		}
		break;
	default:
		ast_log(LOG_WARNING, "Can't send %u type frames with Jingle write\n",
			frame->frametype);
		return 0;
	}

	return res;
}

/*! \brief Function called by core to change the underlying owner channel */
static int jingle_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
{
	struct jingle_session *session = ast_channel_tech_pvt(newchan);

	ao2_lock(session);

	jingle_set_owner(session, newchan);

	ao2_unlock(session);

	return 0;
}

/*! \brief Function called by core to ask the channel to indicate some sort of condition */
static int jingle_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
{
	struct jingle_session *session = ast_channel_tech_pvt(ast);
	int res = 0;

	switch (condition) {
	case AST_CONTROL_RINGING:
		if (ast_channel_state(ast) == AST_STATE_RING) {
			jingle_send_session_info(session, "ringing xmlns='urn:xmpp:jingle:apps:rtp:info:1'");
		} else {
			res = -1;
		}
		break;
	case AST_CONTROL_BUSY:
		if (ast_channel_state(ast) != AST_STATE_UP) {
			ast_channel_hangupcause_set(ast, AST_CAUSE_BUSY);
			ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
		} else {
			res = -1;
		}
		break;
	case AST_CONTROL_CONGESTION:
		if (ast_channel_state(ast) != AST_STATE_UP) {
			ast_channel_hangupcause_set(ast, AST_CAUSE_CONGESTION);
			ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
		} else {
			res = -1;
		}
		break;
	case AST_CONTROL_INCOMPLETE:
		if (ast_channel_state(ast) != AST_STATE_UP) {
			ast_channel_hangupcause_set(ast, AST_CAUSE_CONGESTION);
			ast_softhangup_nolock(ast, AST_SOFTHANGUP_DEV);
		}
		break;
	case AST_CONTROL_HOLD:
		ast_moh_start(ast, data, NULL);
		break;
	case AST_CONTROL_UNHOLD:
		ast_moh_stop(ast);
		break;
	case AST_CONTROL_SRCUPDATE:
		if (session->rtp) {
			ast_rtp_instance_update_source(session->rtp);
		}
		break;
	case AST_CONTROL_SRCCHANGE:
		if (session->rtp) {
			ast_rtp_instance_change_source(session->rtp);
		}
		break;
	case AST_CONTROL_VIDUPDATE:
	case AST_CONTROL_UPDATE_RTP_PEER:
	case AST_CONTROL_CONNECTED_LINE:
		break;
	case AST_CONTROL_PVT_CAUSE_CODE:
	case AST_CONTROL_MASQUERADE_NOTIFY:
	case -1:
		res = -1;
		break;
	default:
		ast_log(LOG_NOTICE, "Don't know how to indicate condition '%d'\n", condition);
		res = -1;
	}

	return res;
}

/*! \brief Function called by core to send text to the remote party of the Jingle session */
static int jingle_sendtext(struct ast_channel *chan, const char *text)
{
	struct jingle_session *session = ast_channel_tech_pvt(chan);

	return ast_xmpp_client_send_message(session->connection, session->remote, text);
}

/*! \brief Function called by core to start a DTMF digit */
static int jingle_digit_begin(struct ast_channel *chan, char digit)
{
	struct jingle_session *session = ast_channel_tech_pvt(chan);

	if (session->rtp) {
		ast_rtp_instance_dtmf_begin(session->rtp, digit);
	}

	return 0;
}

/*! \brief Function called by core to stop a DTMF digit */
static int jingle_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
{
	struct jingle_session *session = ast_channel_tech_pvt(ast);

	if (session->rtp) {
		ast_rtp_instance_dtmf_end_with_duration(session->rtp, digit, duration);
	}

	return 0;
}

/*! \brief Function called by core to actually start calling a remote party */
static int jingle_call(struct ast_channel *ast, const char *dest, int timeout)
{
	struct jingle_session *session = ast_channel_tech_pvt(ast);

	ast_setstate(ast, AST_STATE_RING);

	/* Since we have no idea of the remote capabilities use ours for now */
	ast_format_cap_append_from_cap(session->jointcap, session->cap, AST_MEDIA_TYPE_UNKNOWN);

	/* We set up a hook so we can know when our session-initiate message was accepted or rejected */
	session->rule = iks_filter_add_rule(session->connection->filter, jingle_outgoing_hook, session,
					    IKS_RULE_ID, session->connection->mid, IKS_RULE_DONE);

	jingle_send_session_initiate(session);

	return 0;
}

/*! \brief Function called by core to hang up a Jingle session */
static int jingle_hangup(struct ast_channel *ast)
{
	struct jingle_session *session = ast_channel_tech_pvt(ast);

	ao2_lock(session);

	if ((ast_channel_state(ast) != AST_STATE_DOWN) && !session->gone) {
		int cause = (session->owner ? ast_channel_hangupcause(session->owner) : AST_CAUSE_CONGESTION);
		const char *reason = "success";
		int i;

		/* Get the appropriate reason and send a session-terminate */
		for (i = 0; i < ARRAY_LEN(jingle_reason_mappings); i++) {
			if (jingle_reason_mappings[i].cause == cause) {
				reason = jingle_reason_mappings[i].reason;
				break;
			}
		}

		jingle_send_session_terminate(session, reason);
	}

	ast_channel_tech_pvt_set(ast, NULL);
	jingle_set_owner(session, NULL);

	ao2_unlink(session->state->sessions, session);
	ao2_ref(session->state, -1);

	ao2_unlock(session);
	ao2_ref(session, -1);

	return 0;
}

/*! \brief Function called by core to create a new outgoing Jingle session */
static struct ast_channel *jingle_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
{
	RAII_VAR(struct jingle_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
	RAII_VAR(struct jingle_endpoint *, endpoint, NULL, ao2_cleanup);
	char *dialed, target[1024] = "";
	struct ast_xmpp_buddy *buddy;
	struct jingle_session *session;
	struct ast_channel *chan;
	enum jingle_transport transport = JINGLE_TRANSPORT_NONE;
	struct ast_rtp_engine_ice *ice;
	AST_DECLARE_APP_ARGS(args,
			     AST_APP_ARG(name);
			     AST_APP_ARG(target);
		);

	/* We require at a minimum one audio format to be requested */
	if (!ast_format_cap_has_type(cap, AST_MEDIA_TYPE_AUDIO)) {
		ast_log(LOG_ERROR, "Motif channel driver requires an audio format when dialing a destination\n");
		*cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
		return NULL;
	}

	if (ast_strlen_zero(data) || !(dialed = ast_strdupa(data))) {
		ast_log(LOG_ERROR, "Unable to create channel with empty destination.\n");
		*cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
		return NULL;
	}

	/* Parse the given dial string and validate the results */
	AST_NONSTANDARD_APP_ARGS(args, dialed, '/');

	if (ast_strlen_zero(args.name) || ast_strlen_zero(args.target)) {
		ast_log(LOG_ERROR, "Unable to determine endpoint name and target.\n");
		*cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
		return NULL;
	}

	if (!(endpoint = jingle_endpoint_find(cfg->endpoints, args.name))) {
		ast_log(LOG_ERROR, "Endpoint '%s' does not exist.\n", args.name);
		*cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
		return NULL;
	}

	ao2_lock(endpoint->state);

	/* If we don't have a connection for the endpoint we can't exactly start a session on it */
	if (!endpoint->connection) {
		ast_log(LOG_ERROR, "Unable to create Jingle session on endpoint '%s' as no valid connection exists\n", args.name);
		*cause = AST_CAUSE_SWITCH_CONGESTION;
		ao2_unlock(endpoint->state);
		return NULL;
	}

	/* Find the target in the roster so we can choose a resource */
	if ((buddy = ao2_find(endpoint->connection->buddies, args.target, OBJ_KEY))) {
		struct ao2_iterator res;
		struct ast_xmpp_resource *resource;

		/* Iterate through finding the first viable Jingle capable resource */
		res = ao2_iterator_init(buddy->resources, 0);
		while ((resource = ao2_iterator_next(&res))) {
			if (resource->caps.jingle) {
				snprintf(target, sizeof(target), "%s/%s", args.target, resource->resource);
				transport = JINGLE_TRANSPORT_ICE_UDP;
				break;
			} else if (resource->caps.google) {
				snprintf(target, sizeof(target), "%s/%s", args.target, resource->resource);
				transport = JINGLE_TRANSPORT_GOOGLE_V2;
				break;
			}
			ao2_ref(resource, -1);
		}
		ao2_iterator_destroy(&res);

		ao2_ref(buddy, -1);
	} else {
		/* If the target is NOT in the roster use the provided target as-is */
		ast_copy_string(target, args.target, sizeof(target));
	}

	ao2_unlock(endpoint->state);

	/* If no target was found we can't set up a session */
	if (ast_strlen_zero(target)) {
		ast_log(LOG_ERROR, "Unable to create Jingle session on endpoint '%s' as no capable resource for target '%s' was found\n", args.name, args.target);
		*cause = AST_CAUSE_SWITCH_CONGESTION;
		return NULL;
	}

	if (!(session = jingle_alloc(endpoint, target, NULL))) {
		ast_log(LOG_ERROR, "Unable to create Jingle session on endpoint '%s'\n", args.name);
		*cause = AST_CAUSE_SWITCH_CONGESTION;
		return NULL;
	}

	/* Update the transport if we learned what we should actually use */
	if (transport != JINGLE_TRANSPORT_NONE) {
		session->transport = transport;
		/* Note that for Google-V1 and Google-V2 we don't stop built-in ICE support, this will happen in jingle_new */
	}

	if (!(chan = jingle_new(endpoint, session, AST_STATE_DOWN, target, assignedids, requestor, NULL))) {
		ast_log(LOG_ERROR, "Unable to create Jingle channel on endpoint '%s'\n", args.name);
		*cause = AST_CAUSE_SWITCH_CONGESTION;
		ao2_ref(session, -1);
		return NULL;
	}

	/* If video was requested try to enable it on the session */
	if (ast_format_cap_has_type(cap, AST_MEDIA_TYPE_VIDEO)) {
		jingle_enable_video(session);
	}

	/* As this is outgoing set ourselves as controlling */
	if (session->rtp && (ice = ast_rtp_instance_get_ice(session->rtp))) {
		ice->ice_lite(session->rtp);
	}

	if (session->vrtp && (ice = ast_rtp_instance_get_ice(session->vrtp))) {
		ice->ice_lite(session->vrtp);
	}

	/* We purposely don't decrement the session here as there is a reference on the channel */
	ao2_link(endpoint->state->sessions, session);

	return chan;
}

/*! \brief Helper function which handles content descriptions */
static int jingle_interpret_description(struct jingle_session *session, iks *description, const char *name, struct ast_rtp_instance **rtp)
{
	char *media = iks_find_attrib(description, "media");
	struct ast_rtp_codecs codecs;
	iks *codec;
	int othercapability = 0;

	/* Google-V1 is always carrying audio, but just doesn't tell us so */
	if (session->transport == JINGLE_TRANSPORT_GOOGLE_V1) {
		media = "audio";
	} else if (ast_strlen_zero(media)) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
		ast_log(LOG_ERROR, "Received a content description on session '%s' without a name\n", session->sid);
		return -1;
	}

	/* Determine the type of media that is being carried and update the RTP instance, as well as the name */
	if (!strcasecmp(media, "audio")) {
		if (!ast_strlen_zero(name)) {
			ast_string_field_set(session, audio_name, name);
		}
		*rtp = session->rtp;
		ast_format_cap_remove_by_type(session->peercap, AST_MEDIA_TYPE_AUDIO);
		ast_format_cap_remove_by_type(session->jointcap, AST_MEDIA_TYPE_AUDIO);
	} else if (!strcasecmp(media, "video")) {
		if (!ast_strlen_zero(name)) {
			ast_string_field_set(session, video_name, name);
		}

		jingle_enable_video(session);
		*rtp = session->vrtp;

		/* If video is not present cancel this session */
		if (!session->vrtp) {
			jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
			ast_log(LOG_ERROR, "Received a video content description on session '%s' but could not enable video\n", session->sid);
			return -1;
		}

		ast_format_cap_remove_by_type(session->peercap, AST_MEDIA_TYPE_VIDEO);
		ast_format_cap_remove_by_type(session->jointcap, AST_MEDIA_TYPE_VIDEO);
	} else {
		/* Unknown media type */
		jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
		ast_log(LOG_ERROR, "Unsupported media type '%s' received in content description on session '%s'\n", media, session->sid);
		return -1;
	}

	if (ast_rtp_codecs_payloads_initialize(&codecs)) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
		ast_log(LOG_ERROR, "Could not initialize codecs for negotiation on session '%s'\n", session->sid);
		return -1;
	}

	/* Iterate the codecs updating the relevant RTP instance as we go */
	for (codec = iks_child(description); codec; codec = iks_next(codec)) {
		char *id = iks_find_attrib(codec, "id"), *name = iks_find_attrib(codec, "name");
		char *clockrate = iks_find_attrib(codec, "clockrate");
		int rtp_id, rtp_clockrate;

		if (!ast_strlen_zero(id) && !ast_strlen_zero(name) && (sscanf(id, "%30d", &rtp_id) == 1)) {
			if (!ast_strlen_zero(clockrate) && (sscanf(clockrate, "%30d", &rtp_clockrate) == 1)) {
				ast_rtp_codecs_payloads_set_rtpmap_type_rate(&codecs, NULL, rtp_id, media, name, 0, rtp_clockrate);
			} else {
				ast_rtp_codecs_payloads_set_rtpmap_type(&codecs, NULL, rtp_id, media, name, 0);
			}
		}
	}

	ast_rtp_codecs_payload_formats(&codecs, session->peercap, &othercapability);
	ast_format_cap_get_compatible(session->cap, session->peercap, session->jointcap);

	if (!ast_format_cap_count(session->jointcap)) {
		/* We have no compatible codecs, so terminate the session appropriately */
		jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
		ast_rtp_codecs_payloads_destroy(&codecs);
		return -1;
	}

	ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(*rtp), *rtp);
	ast_rtp_codecs_payloads_destroy(&codecs);

	return 0;
}

/*! \brief Helper function which handles ICE-UDP transport information */
static int jingle_interpret_ice_udp_transport(struct jingle_session *session, iks *transport, struct ast_rtp_instance *rtp)
{
	struct ast_rtp_engine_ice *ice = ast_rtp_instance_get_ice(rtp);
	char *ufrag = iks_find_attrib(transport, "ufrag"), *pwd = iks_find_attrib(transport, "pwd");
	iks *candidate;

	if (!ice) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
		ast_log(LOG_ERROR, "Received ICE-UDP transport information on session '%s' but ICE support not available\n", session->sid);
		return -1;
	}

	if (!ast_strlen_zero(ufrag) && !ast_strlen_zero(pwd)) {
		ice->set_authentication(rtp, ufrag, pwd);
	}

	for (candidate = iks_child(transport); candidate; candidate = iks_next(candidate)) {
		char *component = iks_find_attrib(candidate, "component"), *foundation = iks_find_attrib(candidate, "foundation");
		char *generation = iks_find_attrib(candidate, "generation"), *id = iks_find_attrib(candidate, "id");
		char *ip = iks_find_attrib(candidate, "ip"), *port = iks_find_attrib(candidate, "port");
		char *priority = iks_find_attrib(candidate, "priority"), *protocol = iks_find_attrib(candidate, "protocol");
		char *type = iks_find_attrib(candidate, "type");
		struct ast_rtp_engine_ice_candidate local_candidate = { 0, };
		int real_port;
		struct ast_sockaddr remote_address = { { 0, } };

		/* If this candidate is incomplete skip it */
		if (ast_strlen_zero(component) || ast_strlen_zero(foundation) || ast_strlen_zero(generation) || ast_strlen_zero(id) ||
		    ast_strlen_zero(ip) || ast_strlen_zero(port) || ast_strlen_zero(priority) ||
		    ast_strlen_zero(protocol) || ast_strlen_zero(type)) {
			jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
			ast_log(LOG_ERROR, "Incomplete ICE-UDP candidate received on session '%s'\n", session->sid);
			return -1;
		}

		if ((sscanf(component, "%30u", &local_candidate.id) != 1) ||
		    (sscanf(priority, "%30u", (unsigned *)&local_candidate.priority) != 1) ||
		    (sscanf(port, "%30d", &real_port) != 1)) {
			jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
			ast_log(LOG_ERROR, "Invalid ICE-UDP candidate information received on session '%s'\n", session->sid);
			return -1;
		}

		local_candidate.foundation = foundation;
		local_candidate.transport = protocol;

		ast_sockaddr_parse(&local_candidate.address, ip, PARSE_PORT_FORBID);

		/* We only support IPv4 right now */
		if (!ast_sockaddr_is_ipv4(&local_candidate.address)) {
			continue;
		}

		ast_sockaddr_set_port(&local_candidate.address, real_port);

		if (!strcasecmp(type, "host")) {
			local_candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_HOST;
		} else if (!strcasecmp(type, "srflx")) {
			local_candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_SRFLX;
		} else if (!strcasecmp(type, "relay")) {
			local_candidate.type = AST_RTP_ICE_CANDIDATE_TYPE_RELAYED;
		} else {
			continue;
		}

		/* Worst case use the first viable address */
		ast_rtp_instance_get_remote_address(rtp, &remote_address);

		if (ast_sockaddr_is_ipv4(&local_candidate.address) && ast_sockaddr_isnull(&remote_address)) {
			ast_rtp_instance_set_remote_address(rtp, &local_candidate.address);
		}

		ice->add_remote_candidate(rtp, &local_candidate);
	}

	ice->start(rtp);

	return 0;
}

/*! \brief Helper function which handles Google transport information */
static int jingle_interpret_google_transport(struct jingle_session *session, iks *transport, struct ast_rtp_instance *rtp)
{
	struct ast_rtp_engine_ice *ice = ast_rtp_instance_get_ice(rtp);
	iks *candidate;

	if (!ice) {
		jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
		ast_log(LOG_ERROR, "Received Google transport information on session '%s' but ICE support not available\n", session->sid);
		return -1;
	}

	/* If this session has not transitioned to the Google transport do so now */
	if ((session->transport != JINGLE_TRANSPORT_GOOGLE_V2) &&
	    (session->transport != JINGLE_TRANSPORT_GOOGLE_V1)) {
		/* Stop built-in ICE support... we need to fall back to the old old old STUN */
		ice->stop(rtp);

		session->transport = JINGLE_TRANSPORT_GOOGLE_V2;
	}

	for (candidate = iks_child(transport); candidate; candidate = iks_next(candidate)) {
		char *address = iks_find_attrib(candidate, "address"), *port = iks_find_attrib(candidate, "port");
		char *username = iks_find_attrib(candidate, "username"), *name = iks_find_attrib(candidate, "name");
		char *protocol = iks_find_attrib(candidate, "protocol");
		int real_port;
		struct ast_sockaddr target = { { 0, } };
		/* In Google land the combined value is 32 bytes */
		char combined[33] = "";

		/* If this is NOT actually a candidate just skip it */
		if (strcasecmp(iks_name(candidate), "candidate") &&
		    strcasecmp(iks_name(candidate), "p:candidate") &&
		    strcasecmp(iks_name(candidate), "ses:candidate")) {
			continue;
		}

		/* If this candidate is incomplete skip it */
		if (ast_strlen_zero(address) || ast_strlen_zero(port) || ast_strlen_zero(username) ||
		    ast_strlen_zero(name)) {
			jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
			ast_log(LOG_ERROR, "Incomplete Google candidate received on session '%s'\n", session->sid);
			return -1;
		}

		/* We only support UDP so skip any other protocols */
		if (!ast_strlen_zero(protocol) && strcasecmp(protocol, "udp")) {
			continue;
		}

		/* We only permit audio and video, not RTCP */
		if (strcasecmp(name, "rtp") && strcasecmp(name, "video_rtp")) {
			continue;
		}

		/* Parse the target information so we can send a STUN request to the candidate */
		if (sscanf(port, "%30d", &real_port) != 1) {
			jingle_queue_hangup_with_cause(session, AST_CAUSE_PROTOCOL_ERROR);
			ast_log(LOG_ERROR, "Invalid Google candidate port '%s' received on session '%s'\n", port, session->sid);
			return -1;
		}
		ast_sockaddr_parse(&target, address, PARSE_PORT_FORBID);
		ast_sockaddr_set_port(&target, real_port);

		/* Per the STUN support Google talk uses combine the two usernames */
		snprintf(combined, sizeof(combined), "%s%s", username, ice->get_ufrag(rtp));

		/* This should appease the masses... we will actually change the remote address when we get their STUN packet */
		ast_rtp_instance_stun_request(rtp, &target, combined);
	}

	return 0;
}

/*!
 * \brief Helper function which locates content stanzas and interprets them
 *
 * \note The session *must not* be locked before calling this
 */
static int jingle_interpret_content(struct jingle_session *session, ikspak *pak)
{
	iks *content;
	unsigned int changed = 0;
	struct ast_channel *chan;

	/* Look at the content in the session initiation */
	for (content = iks_child(iks_child(pak->x)); content; content = iks_next(content)) {
		char *name;
		struct ast_rtp_instance *rtp = NULL;
		iks *description, *transport;

		/* Ignore specific parts if they are known not to be useful */
		if (!strcmp(iks_name(content), "conference-info")) {
			continue;
		}

		name = iks_find_attrib(content, "name");

		if (session->transport != JINGLE_TRANSPORT_GOOGLE_V1) {
			/* If this content stanza has no name consider it invalid and move on */
			if (ast_strlen_zero(name) && !(name = iks_find_attrib(content, "jin:name"))) {
				jingle_queue_hangup_with_cause(session, AST_CAUSE_BEARERCAPABILITY_NOTAVAIL);
				ast_log(LOG_ERROR, "Received content without a name on session '%s'\n", session->sid);
				return -1;
			}

			/* Try to pre-populate which RTP instance this content is relevant to */
			if (!strcmp(session->audio_name, name)) {
				rtp = session->rtp;
			} else if (!strcmp(session->video_name, name)) {
				rtp = session->vrtp;
			}
		} else {
			/* Google-V1 has no concept of assocating things like the above does, so since we only support audio over it assume they want audio */
			rtp = session->rtp;
		}

		/* If description information is available use it */
		if ((description = iks_find_with_attrib(content, "description", "xmlns", JINGLE_RTP_NS)) ||
		    (description = iks_find_with_attrib(content, "rtp:description", "xmlns:rtp", JINGLE_RTP_NS)) ||
		    (description = iks_find_with_attrib(content, "pho:description", "xmlns:pho", GOOGLE_PHONE_NS)) ||
		    (description = iks_find_with_attrib(pak->query, "description", "xmlns", GOOGLE_PHONE_NS)) ||
		    (description = iks_find_with_attrib(pak->query, "pho:description", "xmlns:pho", GOOGLE_PHONE_NS)) ||
		    (description = iks_find_with_attrib(pak->query, "vid:description", "xmlns", GOOGLE_VIDEO_NS))) {
			/* If we failed to do something with the content description abort immediately */
			if (jingle_interpret_description(session, description, name, &rtp)) {
				return -1;
			}

			/* If we successfully interpret the description then the codecs need updating */
			changed = 1;
		}

		/* If we get past the description handling and we still don't know what RTP instance this is for... it is unknown content */
		if (!rtp) {
			ast_log(LOG_ERROR, "Received a content stanza but have no RTP instance for it on session '%s'\n", session->sid);
			jingle_queue_hangup_with_cause(session, AST_CAUSE_SWITCH_CONGESTION);
			return -1;
		}

		/* If ICE UDP transport information is available use it */
		if ((transport = iks_find_with_attrib(content, "transport", "xmlns", JINGLE_ICE_UDP_NS))) {
			if (jingle_interpret_ice_udp_transport(session, transport, rtp)) {
				return -1;
			}
		} else if ((transport = iks_find_with_attrib(content, "transport", "xmlns", GOOGLE_TRANSPORT_NS)) ||
			   (transport = iks_find_with_attrib(content, "p:transport", "xmlns:p", GOOGLE_TRANSPORT_NS)) ||
			   (transport = iks_find_with_attrib(pak->x, "session", "xmlns", GOOGLE_SESSION_NS)) ||
			   (transport = iks_find_with_attrib(pak->x, "ses:session", "xmlns:ses", GOOGLE_SESSION_NS))) {
			/* If Google transport support is available use it */
			if (jingle_interpret_google_transport(session, transport, rtp)) {
				return -1;
			}
		} else if (iks_find(content, "transport")) {
			/* If this is a transport we do not support terminate the session as it probably won't work out in the end */
			jingle_queue_hangup_with_cause(session, AST_CAUSE_FACILITY_NOT_IMPLEMENTED);
			ast_log(LOG_ERROR, "Unsupported transport type received on session '%s'\n", session->sid);
			return -1;
		}
	}

	if (!changed) {
		return 0;
	}

	if ((chan = jingle_session_lock_full(session))) {
		struct ast_format_cap *caps;
		struct ast_format *fmt;

		caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
		if (caps) {
			ast_format_cap_append_from_cap(caps, session->jointcap, AST_MEDIA_TYPE_UNKNOWN);
			ast_channel_nativeformats_set(chan, caps);
			ao2_ref(caps, -1);
		}

		fmt = ast_format_cap_get_format(session->jointcap, 0);
		ast_set_read_format(chan, fmt);
		ast_set_write_format(chan, fmt);
		ao2_ref(fmt, -1);

		ast_channel_unlock(chan);
		ast_channel_unref(chan);
	}
	ao2_unlock(session);

	return 0;
}

/*! \brief Handler function for the 'session-initiate' action */
static void jingle_action_session_initiate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
{
	char *sid;
	enum jingle_transport transport = JINGLE_TRANSPORT_NONE;
	struct ast_channel *chan;
	int res;

	if (session) {
		/* This is a duplicate session setup, so respond accordingly */
		jingle_send_error_response(endpoint->connection, pak, "result", "out-of-order", NULL);
		return;
	}

	/* Retrieve the session identifier from the message, note that this may alter the transport */
	if ((sid = iks_find_attrib(pak->query, "id"))) {
		/* The presence of the session identifier in the 'id' attribute tells us that this is Google-V1 as everything else uses 'sid' */
		transport = JINGLE_TRANSPORT_GOOGLE_V1;
	} else if (!(sid = iks_find_attrib(pak->query, "sid"))) {
		jingle_send_error_response(endpoint->connection, pak, "bad-request", NULL, NULL);
		return;
	}

	/* Create a new local session */
	if (!(session = jingle_alloc(endpoint, pak->from->full, sid))) {
		jingle_send_error_response(endpoint->connection, pak, "cancel", "service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", NULL);
		return;
	}

	/* If we determined that the transport should change as a result of how we got the SID change it */
	if (transport != JINGLE_TRANSPORT_NONE) {
		session->transport = transport;
	}

	/* Create a new Asterisk channel using the above local session */
	if (!(chan = jingle_new(endpoint, session, AST_STATE_DOWN, pak->from->user, NULL, NULL, pak->from->full))) {
		ao2_ref(session, -1);
		jingle_send_error_response(endpoint->connection, pak, "cancel", "service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", NULL);
		return;
	}

	ao2_link(endpoint->state->sessions, session);

	ast_channel_lock(chan);
	ast_setstate(chan, AST_STATE_RING);
	ast_channel_unlock(chan);
	res = ast_pbx_start(chan);

	switch (res) {
	case AST_PBX_FAILED:
		ast_log(LOG_WARNING, "Failed to start PBX :(\n");
		jingle_send_error_response(endpoint->connection, pak, "cancel", "service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", NULL);
		session->gone = 1;
		ast_hangup(chan);
		break;
	case AST_PBX_CALL_LIMIT:
		ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
		jingle_send_error_response(endpoint->connection, pak, "wait", "resource-constraint xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'", NULL);
		ast_hangup(chan);
		break;
	case AST_PBX_SUCCESS:
		jingle_send_response(endpoint->connection, pak);

		/* Only send a transport-info message if we successfully interpreted the available content */
		if (!jingle_interpret_content(session, pak)) {
			jingle_send_transport_info(session, iks_find_attrib(pak->x, "from"));
		}
		break;
	}
}

/*! \brief Handler function for the 'transport-info' action */
static void jingle_action_transport_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
{
	if (!session) {
		jingle_send_error_response(endpoint->connection, pak, "cancel", "item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
					   "unknown-session xmlns='urn:xmpp:jingle:errors:1'");
		return;
	}

	jingle_interpret_content(session, pak);
	jingle_send_response(endpoint->connection, pak);
}

/*! \brief Handler function for the 'session-accept' action */
static void jingle_action_session_accept(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
{
	struct ast_channel *chan;

	if (!session) {
		jingle_send_error_response(endpoint->connection, pak, "cancel", "item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
					   "unknown-session xmlns='urn:xmpp:jingle:errors:1'");
		return;
	}


	jingle_interpret_content(session, pak);

	if ((chan = jingle_session_lock_full(session))) {
		ast_queue_control(chan, AST_CONTROL_ANSWER);
		ast_channel_unlock(chan);
		ast_channel_unref(chan);
	}
	ao2_unlock(session);

	jingle_send_response(endpoint->connection, pak);
}

/*! \brief Handler function for the 'session-info' action */
static void jingle_action_session_info(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
{
	struct ast_channel *chan;

	if (!session) {
		jingle_send_error_response(endpoint->connection, pak, "cancel", "item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
					   "unknown-session xmlns='urn:xmpp:jingle:errors:1'");
		return;
	}

	if (!(chan = jingle_session_lock_full(session))) {
		ao2_unlock(session);
		jingle_send_response(endpoint->connection, pak);
		return;
	}

	if (iks_find_with_attrib(pak->query, "ringing", "xmlns", JINGLE_RTP_INFO_NS)) {
		ast_queue_control(chan, AST_CONTROL_RINGING);
		if (ast_channel_state(chan) != AST_STATE_UP) {
			ast_setstate(chan, AST_STATE_RINGING);
		}
	} else if (iks_find_with_attrib(pak->query, "hold", "xmlns", JINGLE_RTP_INFO_NS)) {
		ast_queue_hold(chan, NULL);
	} else if (iks_find_with_attrib(pak->query, "unhold", "xmlns", JINGLE_RTP_INFO_NS)) {
		ast_queue_unhold(chan);
	}

	ast_channel_unlock(chan);
	ast_channel_unref(chan);
	ao2_unlock(session);

	jingle_send_response(endpoint->connection, pak);
}

/*! \brief Handler function for the 'session-terminate' action */
static void jingle_action_session_terminate(struct jingle_endpoint *endpoint, struct jingle_session *session, ikspak *pak)
{
	struct ast_channel *chan;
	iks *reason, *text;
	int cause = AST_CAUSE_NORMAL;
	struct ast_control_pvt_cause_code *cause_code;
	int data_size = sizeof(*cause_code);

	if (!session) {
		jingle_send_error_response(endpoint->connection, pak, "cancel", "item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'",
					   "unknown-session xmlns='urn:xmpp:jingle:errors:1'");
		return;
	}

	if (!(chan = jingle_session_lock_full(session))) {
		ao2_unlock(session);
		jingle_send_response(endpoint->connection, pak);
		return;
	}

	/* Pull the reason text from the session-terminate message and translate it into a cause code */
	if ((reason = iks_find(pak->query, "reason")) && (text = iks_child(reason))) {
		int i;

		/* Size of the string making up the cause code is "Motif " + text */
		data_size += 6 + strlen(iks_name(text));
		cause_code = ast_alloca(data_size);
		memset(cause_code, 0, data_size);

		/* Get the appropriate cause code mapping for this reason */
		for (i = 0; i < ARRAY_LEN(jingle_reason_mappings); i++) {
			if (!strcasecmp(jingle_reason_mappings[i].reason, iks_name(text))) {
				cause = jingle_reason_mappings[i].cause;
				break;
			}
		}

		/* Store the technology specific information */
		snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "Motif %s", iks_name(text));
	} else {
		/* No technology specific information is available */
		cause_code = ast_alloca(data_size);
		memset(cause_code, 0, data_size);
	}

	ast_copy_string(cause_code->chan_name, ast_channel_name(chan), AST_CHANNEL_NAME);
	cause_code->ast_cause = cause;
	ast_queue_control_data(chan, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
	ast_channel_hangupcause_hash_set(chan, cause_code, data_size);

	ast_debug(3, "Hanging up channel '%s' due to session terminate message with cause '%d'\n", ast_channel_name(chan), cause);
	ast_queue_hangup_with_cause(chan, cause);
	session->gone = 1;

	ast_channel_unlock(chan);
	ast_channel_unref(chan);
	ao2_unlock(session);

	jingle_send_response(endpoint->connection, pak);
}

/*! \brief Callback for when a Jingle action is received from an endpoint */
static int jingle_action_hook(void *data, ikspak *pak)
{
	char *action;
	const char *sid = NULL;
	struct jingle_session *session = NULL;
	struct jingle_endpoint *endpoint = data;
	int i, handled = 0;

	/* We accept both Jingle and Google-V1 */
	if (!(action = iks_find_attrib(pak->query, "action")) &&
	    !(action = iks_find_attrib(pak->query, "type"))) {
		/* This occurs if either receive a packet masquerading as Jingle or Google-V1 that is actually not OR we receive a response
		 * to a message that has no response hook. */
		return IKS_FILTER_EAT;
	}

	/* Bump the endpoint reference count up in case a reload occurs. Unfortunately the available synchronization between iksemel and us
	 * does not permit us to make this completely safe. */
	ao2_ref(endpoint, +1);

	/* If a Jingle session identifier is present use it */
	if (!(sid = iks_find_attrib(pak->query, "sid"))) {
		/* If a Google-V1 session identifier is present use it */
		sid = iks_find_attrib(pak->query, "id");
	}

	/* If a session identifier was present in the message attempt to find the session, it is up to the action handler whether
	 * this is required or not */
	if (!ast_strlen_zero(sid)) {
		session = ao2_find(endpoint->state->sessions, sid, OBJ_KEY);
	}

	/* If a session is present associate the callid with this thread */
	if (session) {
		ast_callid_threadassoc_add(session->callid);
	}

	/* Iterate through supported action handlers looking for one that is able to handle this */
	for (i = 0; i < ARRAY_LEN(jingle_action_handlers); i++) {
		if (!strcasecmp(jingle_action_handlers[i].action, action)) {
			jingle_action_handlers[i].handler(endpoint, session, pak);
			handled = 1;
			break;
		}
	}

	/* If no action handler is present for the action they sent us make it evident */
	if (!handled) {
		ast_log(LOG_NOTICE, "Received action '%s' for session '%s' that has no handler\n", action, sid);
	}

	/* If a session was successfully found for this message deref it now since the handler is done */
	if (session) {
		ast_callid_threadassoc_remove();
		ao2_ref(session, -1);
	}

	ao2_ref(endpoint, -1);

	return IKS_FILTER_EAT;
}

/*! \brief Custom handler for groups */
static int custom_group_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
{
	struct jingle_endpoint *endpoint = obj;

	if (!strcasecmp(var->name, "callgroup")) {
		endpoint->callgroup = ast_get_group(var->value);
	} else if (!strcasecmp(var->name, "pickupgroup")) {
		endpoint->pickupgroup = ast_get_group(var->value);
	} else {
		return -1;
	}

	return 0;
}

/*! \brief Custom handler for connection */
static int custom_connection_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
{
	struct jingle_endpoint *endpoint = obj;

	/* You might think... but Josh, shouldn't you do this in a prelink callback? Well I *could* but until the original is destroyed
	 * this will not actually get called, so even if the config turns out to be bogus this is harmless.
	 */
	if (!(endpoint->connection = ast_xmpp_client_find(var->value))) {
		ast_log(LOG_ERROR, "Connection '%s' configured on endpoint '%s' could not be found\n", var->value, endpoint->name);
		return -1;
	}

	if (!(endpoint->rule = iks_filter_add_rule(endpoint->connection->filter, jingle_action_hook, endpoint,
						   IKS_RULE_TYPE, IKS_PAK_IQ,
						   IKS_RULE_NS, JINGLE_NS,
						   IKS_RULE_NS, GOOGLE_SESSION_NS,
						   IKS_RULE_DONE))) {
		ast_log(LOG_ERROR, "Action hook could not be added to connection '%s' on endpoint '%s'\n", var->value, endpoint->name);
		return -1;
	}

	return 0;
}

/*! \brief Custom handler for transport */
static int custom_transport_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
{
	struct jingle_endpoint *endpoint = obj;

	if (!strcasecmp(var->value, "ice-udp")) {
		endpoint->transport = JINGLE_TRANSPORT_ICE_UDP;
	} else if (!strcasecmp(var->value, "google")) {
		endpoint->transport = JINGLE_TRANSPORT_GOOGLE_V2;
	} else if (!strcasecmp(var->value, "google-v1")) {
		endpoint->transport = JINGLE_TRANSPORT_GOOGLE_V1;
	} else {
		ast_log(LOG_WARNING, "Unknown transport type '%s' on endpoint '%s', defaulting to 'ice-udp'\n", var->value, endpoint->name);
		endpoint->transport = JINGLE_TRANSPORT_ICE_UDP;
	}

	return 0;
}

/*!
 * \brief Load the module
 *
 * Module loading including tests for configuration or dependencies.
 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the 
 * configuration file or other non-critical problem return 
 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
 */
static int load_module(void)
{
	if (!(jingle_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
		return AST_MODULE_LOAD_DECLINE;
	}

	if (aco_info_init(&cfg_info)) {
		ast_log(LOG_ERROR, "Unable to intialize configuration for chan_motif.\n");
		goto end;
	}

	aco_option_register(&cfg_info, "context", ACO_EXACT, endpoint_options, "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, context));
	aco_option_register_custom(&cfg_info, "callgroup", ACO_EXACT, endpoint_options, NULL, custom_group_handler, 0);
	aco_option_register_custom(&cfg_info, "pickupgroup", ACO_EXACT, endpoint_options, NULL, custom_group_handler, 0);
	aco_option_register(&cfg_info, "language", ACO_EXACT, endpoint_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, language));
	aco_option_register(&cfg_info, "musicclass", ACO_EXACT, endpoint_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, musicclass));
	aco_option_register(&cfg_info, "parkinglot", ACO_EXACT, endpoint_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, parkinglot));
	aco_option_register(&cfg_info, "accountcode", ACO_EXACT, endpoint_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct jingle_endpoint, accountcode));
	aco_option_register(&cfg_info, "allow", ACO_EXACT, endpoint_options, "ulaw,alaw", OPT_CODEC_T, 1, FLDSET(struct jingle_endpoint, cap));
	aco_option_register(&cfg_info, "disallow", ACO_EXACT, endpoint_options, "all", OPT_CODEC_T, 0, FLDSET(struct jingle_endpoint, cap));
	aco_option_register_custom(&cfg_info, "connection", ACO_EXACT, endpoint_options, NULL, custom_connection_handler, 0);
	aco_option_register_custom(&cfg_info, "transport", ACO_EXACT, endpoint_options, NULL, custom_transport_handler, 0);
	aco_option_register(&cfg_info, "maxicecandidates", ACO_EXACT, endpoint_options, DEFAULT_MAX_ICE_CANDIDATES, OPT_UINT_T, PARSE_DEFAULT,
			    FLDSET(struct jingle_endpoint, maxicecandidates), DEFAULT_MAX_ICE_CANDIDATES);
	aco_option_register(&cfg_info, "maxpayloads", ACO_EXACT, endpoint_options, DEFAULT_MAX_PAYLOADS, OPT_UINT_T, PARSE_DEFAULT,
			    FLDSET(struct jingle_endpoint, maxpayloads), DEFAULT_MAX_PAYLOADS);

	ast_format_cap_append_by_type(jingle_tech.capabilities, AST_MEDIA_TYPE_AUDIO);

	if (aco_process_config(&cfg_info, 0)) {
		ast_log(LOG_ERROR, "Unable to read config file motif.conf. Module loaded but not running.\n");
		aco_info_destroy(&cfg_info);
		ao2_cleanup(jingle_tech.capabilities);
		jingle_tech.capabilities = NULL;
		return AST_MODULE_LOAD_DECLINE;
	}

	if (!(sched = ast_sched_context_create())) {
		ast_log(LOG_ERROR, "Unable to create scheduler context.\n");
		goto end;
	}

	if (ast_sched_start_thread(sched)) {
		ast_log(LOG_ERROR, "Unable to create scheduler context thread.\n");
		goto end;
	}

	ast_rtp_glue_register(&jingle_rtp_glue);

	if (ast_channel_register(&jingle_tech)) {
		ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
		goto end;
	}

	return 0;

end:
	ast_rtp_glue_unregister(&jingle_rtp_glue);

	if (sched) {
		ast_sched_context_destroy(sched);
	}

	aco_info_destroy(&cfg_info);
	ao2_global_obj_release(globals);

	ao2_cleanup(jingle_tech.capabilities);
	jingle_tech.capabilities = NULL;

	return AST_MODULE_LOAD_DECLINE;
}

/*! \brief Reload module */
static int reload(void)
{
	if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
		return -1;
	}

	return 0;
}

/*! \brief Unload the jingle channel from Asterisk */
static int unload_module(void)
{
	ast_channel_unregister(&jingle_tech);
	ao2_cleanup(jingle_tech.capabilities);
	jingle_tech.capabilities = NULL;
	ast_rtp_glue_unregister(&jingle_rtp_glue);
	ast_sched_context_destroy(sched);
	aco_info_destroy(&cfg_info);
	ao2_global_obj_release(globals);

	return 0;
}

AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Motif Jingle Channel Driver",
	.support_level = AST_MODULE_SUPPORT_CORE,
	.load = load_module,
	.unload = unload_module,
	.reload = reload,
	.load_pri = AST_MODPRI_CHANNEL_DRIVER,
);