JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
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
 
/* LinphoneManager.h
 *
 * Copyright (C) 2011  Belledonne Comunications, Grenoble, France
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/sysctl.h>
#import <asl.h>
#import <os/log.h>
 
#import <AVFoundation/AVFoundation.h>
 
#import <AVFoundation/AVCaptureDevice.h>
#import <AudioToolbox/AudioToolbox.h>
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <SystemConfiguration/SystemConfiguration.h>
 
#import "LinphoneManager.h"
#import <CallKit/CallKit.h>
 
 
#include "linphone/linphonecore_utils.h"
#include "linphone/lpconfig.h"
 
#include "mediastreamer2/mscommon.h"
//#import "Log.h"
 
//#import "Utils.h"
 
#import <UserNotifications/UserNotifications.h>
 
static LinphoneCore *theLinphoneCore = nil;
static LinphoneManager *theLinphoneManager = nil;
 
NSString *const LINPHONERC_APPLICATION_KEY = @"app";
 
NSString *const kLinphoneCoreUpdate = @"LinphoneCoreUpdate";
NSString *const kLinphoneDisplayStatusUpdate = @"LinphoneDisplayStatusUpdate";
NSString *const kLinphoneMessageReceived = @"LinphoneMessageReceived";
NSString *const kLinphoneTextComposeEvent = @"LinphoneTextComposeStarted";
NSString *const kLinphoneCallUpdate = @"LinphoneCallUpdate";
NSString *const kLinphoneRegistrationUpdate = @"LinphoneRegistrationUpdate";
NSString *const kLinphoneAddressBookUpdate = @"LinphoneAddressBookUpdate";
NSString *const kLinphoneMainViewChange = @"LinphoneMainViewChange";
NSString *const kLinphoneLogsUpdate = @"LinphoneLogsUpdate";
NSString *const kLinphoneSettingsUpdate = @"LinphoneSettingsUpdate";
NSString *const kLinphoneBluetoothAvailabilityUpdate = @"LinphoneBluetoothAvailabilityUpdate";
NSString *const kLinphoneConfiguringStateUpdate = @"LinphoneConfiguringStateUpdate";
NSString *const kLinphoneGlobalStateUpdate = @"LinphoneGlobalStateUpdate";
NSString *const kLinphoneNotifyReceived = @"LinphoneNotifyReceived";
NSString *const kLinphoneNotifyPresenceReceivedForUriOrTel = @"LinphoneNotifyPresenceReceivedForUriOrTel";
NSString *const kLinphoneCallEncryptionChanged = @"LinphoneCallEncryptionChanged";
NSString *const kLinphoneFileTransferSendUpdate = @"LinphoneFileTransferSendUpdate";
NSString *const kLinphoneFileTransferRecvUpdate = @"LinphoneFileTransferRecvUpdate";
 
const int kLinphoneAudioVbrCodecDefaultBitrate = 36; /*you can override this from linphonerc or linphonerc-factory*/
 
extern void libmsamr_init(MSFactory *factory);
extern void libmsx264_init(MSFactory *factory);
extern void libmsopenh264_init(MSFactory *factory);
extern void libmssilk_init(MSFactory *factory);
extern void libmsbcg729_init(MSFactory *factory);
extern void libmswebrtc_init(MSFactory *factory);
 
#define FRONT_CAM_NAME                                                                                                 \
    "AV Capture: com.apple.avfoundation.avcapturedevice.built-in_video:1" /*"AV Capture: Front Camera"*/
#define BACK_CAM_NAME                                                                                                  \
    "AV Capture: com.apple.avfoundation.avcapturedevice.built-in_video:0" /*"AV Capture: Back Camera"*/
 
NSString *const kLinphoneOldChatDBFilename = @"chat_database.sqlite";
NSString *const kLinphoneInternalChatDBFilename = @"linphone_chats.db";
 
@implementation LinphoneCallAppData
- (id)init {
    if ((self = [super init])) {
        batteryWarningShown = FALSE;
        notification = nil;
        videoRequested = FALSE;
        userInfos = [[NSMutableDictionary alloc] init];
    }
    return self;
}
 
@end
 
@interface LinphoneManager ()
 
//@property ProviderDelegate *providerDelegate;
 
@end
 
@implementation LinphoneManager
 
 
@synthesize connectivity;
 
struct codec_name_pref_table {
    const char *name;
    int rate;
    const char *prefname;
};
 
struct codec_name_pref_table codec_pref_table[] = {{"speex", 8000, "speex_8k_preference"},
    {"speex", 16000, "speex_16k_preference"},
    {"silk", 24000, "silk_24k_preference"},
    {"silk", 16000, "silk_16k_preference"},
    {"amr", 8000, "amr_preference"},
    {"gsm", 8000, "gsm_preference"},
    {"ilbc", 8000, "ilbc_preference"},
    {"isac", 16000, "isac_preference"},
    {"pcmu", 8000, "pcmu_preference"},
    {"pcma", 8000, "pcma_preference"},
    {"g722", 8000, "g722_preference"},
    {"g729", 8000, "g729_preference"},
    {"mp4v-es", 90000, "mp4v-es_preference"},
    {"h264", 90000, "h264_preference"},
    {"vp8", 90000, "vp8_preference"},
    {"mpeg4-generic", 16000, "aaceld_16k_preference"},
    {"mpeg4-generic", 22050, "aaceld_22k_preference"},
    {"mpeg4-generic", 32000, "aaceld_32k_preference"},
    {"mpeg4-generic", 44100, "aaceld_44k_preference"},
    {"mpeg4-generic", 48000, "aaceld_48k_preference"},
    {"opus", 48000, "opus_preference"},
    {"BV16", 8000, "bv16_preference"},
    {NULL, 0, Nil}
};
 
+ (NSString *)getPreferenceForCodec:(const char *)name withRate:(int)rate {
    int i;
    for (i = 0; codec_pref_table[i].name != NULL; ++i) {
        if (strcasecmp(codec_pref_table[i].name, name) == 0 && codec_pref_table[i].rate == rate)
            return [NSString stringWithUTF8String:codec_pref_table[i].prefname];
    }
    return Nil;
}
 
+ (NSSet *)unsupportedCodecs {
    
    NSMutableSet *set = [NSMutableSet set];
    for (int i = 0; codec_pref_table[i].name != NULL; ++i) {
        LinphonePayloadType *available = linphone_core_get_payload_type(
                                                                 theLinphoneCore, codec_pref_table[i].name, codec_pref_table[i].rate, LINPHONE_FIND_PAYLOAD_IGNORE_CHANNELS);
        if ((available == NULL)
            // these two codecs should not be hidden, even if not supported
            && strcmp(codec_pref_table[i].prefname, "h264_preference") != 0 &&
            strcmp(codec_pref_table[i].prefname, "mp4v-es_preference") != 0) {
            [set addObject:[NSString stringWithUTF8String:codec_pref_table[i].prefname]];
        }
    }
    return set;
}
 
+ (BOOL)isCodecSupported:(const char *)codecName {
    return (codecName != NULL) &&
    (NULL != linphone_core_get_payload_type(theLinphoneCore, codecName, LINPHONE_FIND_PAYLOAD_IGNORE_RATE,
                                             LINPHONE_FIND_PAYLOAD_IGNORE_CHANNELS));
}
 
+ (BOOL)runningOnIpad {
    return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
}
 
+ (BOOL)isRunningTests {
    NSDictionary *environment = [[NSProcessInfo processInfo] environment];
    NSString *injectBundle = environment[@"XCInjectBundle"];
    return [[injectBundle pathExtension] isEqualToString:@"xctest"];
}
+ (LinphoneManager *)instance {
    @synchronized(self) {
        if (theLinphoneManager == nil) {
            theLinphoneManager = [[LinphoneManager alloc] init];
        }
    }
    return theLinphoneManager;
}
 
#pragma mark - Lifecycle Functions
 
- (id)init {
    
    if ((self = [super init])) {
        [NSNotificationCenter.defaultCenter addObserver:self
                                               selector:@selector(audioRouteChangeListenerCallback:)
                                                   name:AVAudioSessionRouteChangeNotification
                                                 object:nil];
        
        _sounds.vibrate = kSystemSoundID_Vibrate;
        
        _logs = [[NSMutableArray alloc] init];
        _pushDict = [[NSMutableDictionary alloc] init];
        _database = NULL;
        _speakerEnabled = FALSE;
        _bluetoothEnabled = FALSE;
        _conf = FALSE;
        _fileTransferDelegates = [[NSMutableArray alloc] init];
        
        pushCallIDs = [[NSMutableArray alloc] init];
        _photoLibrary = [[ALAssetsLibrary alloc] init];
        _isTesting = [LinphoneManager isRunningTests];
        [self renameDefaultSettings];
        [self copyDefaultSettings];
        [self overrideDefaultSettings];
        
        // set default values for first boot
        if ([self lpConfigStringForKey:@"debugenable_preference"] == nil) {
#ifdef DEBUG
            [self lpConfigSetInt:1 forKey:@"debugenable_preference"];
#else
            [self lpConfigSetInt:0 forKey:@"debugenable_preference"];
#endif
        }
        
        // by default if handle_content_encoding is not set, we use plain text for debug purposes only
        if ([self lpConfigStringForKey:@"handle_content_encoding" inSection:@"misc"] == nil) {
#ifdef DEBUG
            [self lpConfigSetString:@"none" forKey:@"handle_content_encoding" inSection:@"misc"];
#else
            [self lpConfigSetString:@"conflate" forKey:@"handle_content_encoding" inSection:@"misc"];
#endif
        }
        
        [self migrateFromUserPrefs];
    }
    return self;
}
 
- (void)dealloc {
    [NSNotificationCenter.defaultCenter removeObserver:self];
}
 
#pragma deploymate push "ignored-api-availability"
//静默推送
- (void)silentPushFailed:(NSTimer *)timer {
    if (_silentPushCompletion) {
        NSLog(@"silentPush failed, silentPushCompletion block: %p", _silentPushCompletion);
        _silentPushCompletion(UIBackgroundFetchResultNoData);
        _silentPushCompletion = nil;
    }
}
#pragma deploymate pop
 
#pragma mark - Migration
- (void)migrationAllPost {
    [self migrationLinphoneSettings];
    [self migratePushNotificationPerAccount];
}
 
- (void)migrationAllPre {
    // migrate xmlrpc URL if needed
    if ([self lpConfigBoolForKey:@"migration_xmlrpc"] == NO) {
        [self lpConfigSetString:@"https://subscribe.linphone.org:444/wizard.php"
                         forKey:@"xmlrpc_url"
                      inSection:@"assistant"];
        [self lpConfigSetString:@"sip:rls@sip.linphone.org" forKey:@"rls_uri" inSection:@"sip"];
        [self lpConfigSetBool:YES forKey:@"migration_xmlrpc"];
    }
    [self lpConfigSetBool:NO forKey:@"store_friends" inSection:@"misc"]; //so far, storing friends in files is not needed. may change in the future.
}
#pragma mark 检查是否转移图片
static int check_should_migrate_images(void *data, int argc, char **argv, char **cnames) {
    *((BOOL *)data) = TRUE;
    return 0;
}
#pragma mark 数据库移植
- (BOOL)migrateChatDBIfNeeded:(LinphoneCore *)lc {
    sqlite3 *newDb;
    char *errMsg;
    NSError *error;
    NSString *oldDbPath = [LinphoneManager documentFile:kLinphoneOldChatDBFilename];
    NSString *newDbPath = [LinphoneManager documentFile:kLinphoneInternalChatDBFilename];
    BOOL shouldMigrate = [[NSFileManager defaultManager] fileExistsAtPath:oldDbPath];
    BOOL shouldMigrateImages = FALSE;
    const char *identity = NULL;
    BOOL migrated = FALSE;
    char *attach_stmt = NULL;
    LinphoneProxyConfig *default_proxy = linphone_core_get_default_proxy_config(lc);
    
    if (sqlite3_open([newDbPath UTF8String], &newDb) != SQLITE_OK) {
        NSLog(@"Can't open \"%@\" sqlite3 database.", newDbPath);
        return FALSE;
    }
    
    const char *check_appdata =
    "SELECT url,message FROM history WHERE url LIKE 'assets-library%' OR message LIKE 'assets-library%' LIMIT 1;";
    // will set "needToMigrateImages to TRUE if a result comes by
    sqlite3_exec(newDb, check_appdata, check_should_migrate_images, &shouldMigrateImages, NULL);
    if (!shouldMigrate && !shouldMigrateImages) {
        sqlite3_close(newDb);
        return FALSE;
    }
    
    NSLog(@"Starting migration procedure");
    
    if (shouldMigrate) {
        
        // attach old database to the new one:
        attach_stmt = sqlite3_mprintf("ATTACH DATABASE %Q AS oldchats", [oldDbPath UTF8String]);
        if (sqlite3_exec(newDb, attach_stmt, NULL, NULL, &errMsg) != SQLITE_OK) {
            NSLog(@"Can't attach old chat table, error[%s] ", errMsg);
            sqlite3_free(errMsg);
            goto exit_dbmigration;
        }
        
        // migrate old chats to the new db. The iOS stores timestamp in UTC already, so we can directly put it in the
        // 'utc' field and set 'time' to -1
        const char *migration_statement =
        "INSERT INTO history (localContact,remoteContact,direction,message,utc,read,status,time) "
        "SELECT localContact,remoteContact,direction,message,time,read,state,'-1' FROM oldchats.chat";
        
        if (sqlite3_exec(newDb, migration_statement, NULL, NULL, &errMsg) != SQLITE_OK) {
            NSLog(@"DB migration failed, error[%s] ", errMsg);
            sqlite3_free(errMsg);
            goto exit_dbmigration;
        }
        
        // invert direction of old messages, because iOS was storing the direction flag incorrectly
        const char *invert_direction = "UPDATE history SET direction = NOT direction";
        if (sqlite3_exec(newDb, invert_direction, NULL, NULL, &errMsg) != SQLITE_OK) {
            NSLog(@"Inverting direction failed, error[%s]", errMsg);
            sqlite3_free(errMsg);
            goto exit_dbmigration;
        }
        
        // replace empty from: or to: by the current identity.
        if (default_proxy) {
            identity = linphone_proxy_config_get_identity_address(default_proxy);
        }
        if (!identity) {
            identity = "sip:unknown@sip.linphone.org";
        }
        
        char *from_conversion =
        sqlite3_mprintf("UPDATE history SET localContact = %Q WHERE localContact = ''", identity);
        if (sqlite3_exec(newDb, from_conversion, NULL, NULL, &errMsg) != SQLITE_OK) {
            NSLog(@"FROM conversion failed, error[%s] ", errMsg);
            sqlite3_free(errMsg);
        }
        sqlite3_free(from_conversion);
        
        char *to_conversion =
        sqlite3_mprintf("UPDATE history SET remoteContact = %Q WHERE remoteContact = ''", identity);
        if (sqlite3_exec(newDb, to_conversion, NULL, NULL, &errMsg) != SQLITE_OK) {
            NSLog(@"DB migration failed, error[%s] ", errMsg);
            sqlite3_free(errMsg);
        }
        sqlite3_free(to_conversion);
    }
    
    // local image paths were stored in the 'message' field historically. They were
    // very temporarily stored in the 'url' field, and now we migrated them to a JSON-
    // encoded field. These are the migration steps to migrate them.
    
    // move already stored images from the messages to the appdata JSON field
    const char *assetslib_migration = "UPDATE history SET appdata='{\"localimage\":\"'||message||'\"}' , message='' "
    "WHERE message LIKE 'assets-library%'";
    if (sqlite3_exec(newDb, assetslib_migration, NULL, NULL, &errMsg) != SQLITE_OK) {
        NSLog(@"Assets-history migration for MESSAGE failed, error[%s] ", errMsg);
        sqlite3_free(errMsg);
    }
    
    // move already stored images from the url to the appdata JSON field
    const char *assetslib_migration_fromurl =
    "UPDATE history SET appdata='{\"localimage\":\"'||url||'\"}' , url='' WHERE url LIKE 'assets-library%'";
    if (sqlite3_exec(newDb, assetslib_migration_fromurl, NULL, NULL, &errMsg) != SQLITE_OK) {
        NSLog(@"Assets-history migration for URL failed, error[%s] ", errMsg);
        sqlite3_free(errMsg);
    }
    
    // We will lose received messages with remote url, they will be displayed in plain. We can't do much for them..
    migrated = TRUE;
    
exit_dbmigration:
    
    if (attach_stmt)
        sqlite3_free(attach_stmt);
    
    sqlite3_close(newDb);
    
    // in any case, we should remove the old chat db
    if (shouldMigrate && ![[NSFileManager defaultManager] removeItemAtPath:oldDbPath error:&error]) {
        NSLog(@"Could not remove old chat DB: %@", error);
    }
    
    NSLog(@"Message storage migration finished: success = %@", migrated ? @"TRUE" : @"FALSE");
    return migrated;
}
#pragma mark 转移linphone UserpRefs
- (void)migrateFromUserPrefs {
    static NSString *migration_flag = @"userpref_migration_done";
    
    if (_configDb == nil)
        return;
    
    if ([self lpConfigIntForKey:migration_flag withDefault:0]) {
        return;
    }
    
    NSDictionary *defaults = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    NSArray *defaults_keys = [defaults allKeys];
    NSDictionary *values =
    @{ @"backgroundmode_preference" : @YES,
       @"debugenable_preference" : @NO,
       @"start_at_boot_preference" : @YES };
    BOOL shouldSync = FALSE;
    
    NSLog(@"%lu user prefs", (unsigned long)[defaults_keys count]);
    
    for (NSString *userpref in values) {
        if ([defaults_keys containsObject:userpref]) {
            NSLog(@"Migrating %@ from user preferences: %d", userpref, [[defaults objectForKey:userpref] boolValue]);
            [self lpConfigSetBool:[[defaults objectForKey:userpref] boolValue] forKey:userpref];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:userpref];
            shouldSync = TRUE;
        } else if ([self lpConfigStringForKey:userpref] == nil) {
            // no default value found in our linphonerc, we need to add them
            [self lpConfigSetBool:[[values objectForKey:userpref] boolValue] forKey:userpref];
        }
    }
    
    if (shouldSync) {
        NSLog(@"Synchronizing...");
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    // don't get back here in the future
    [self lpConfigSetBool:YES forKey:migration_flag];
}
#pragma mark 转移linphone设置
- (void)migrationLinphoneSettings {
    // we need to proceed to the migration *after* the chat database was opened, so that we know it is in consistent
    // state
    NSString *chatDBFileName = [LinphoneManager documentFile:kLinphoneInternalChatDBFilename];
    if ([self migrateChatDBIfNeeded:theLinphoneCore]) {
        // if a migration was performed, we should reinitialize the chat database
        linphone_core_set_chat_database_path(theLinphoneCore, [chatDBFileName UTF8String]);
    }
    
    /* AVPF migration */
    if ([self lpConfigBoolForKey:@"avpf_migration_done"] == FALSE) {
        const MSList *proxies = linphone_core_get_proxy_config_list(theLinphoneCore);
        while (proxies) {
            LinphoneProxyConfig *proxy = (LinphoneProxyConfig *)proxies->data;
            const char *addr = linphone_proxy_config_get_addr(proxy);
            // we want to enable AVPF for the proxies
            if (addr &&
                strstr(addr, [LinphoneManager.instance lpConfigStringForKey:@"domain_name"
                                                                  inSection:@"app"
                                                                withDefault:@"sip.linphone.org"]
                       .UTF8String) != 0) {
                    NSLog(@"Migrating proxy config to use AVPF");
                    linphone_proxy_config_set_avpf_mode(proxy, TRUE);
                }
            proxies = proxies->next;
        }
        [self lpConfigSetBool:TRUE forKey:@"avpf_migration_done"];
    }
    /* Quality Reporting migration */
    if ([self lpConfigBoolForKey:@"quality_report_migration_done"] == FALSE) {
        const MSList *proxies = linphone_core_get_proxy_config_list(theLinphoneCore);
        while (proxies) {
            LinphoneProxyConfig *proxy = (LinphoneProxyConfig *)proxies->data;
            const char *addr = linphone_proxy_config_get_addr(proxy);
            // we want to enable quality reporting for the proxies that are on linphone.org
            if (addr &&
                strstr(addr, [LinphoneManager.instance lpConfigStringForKey:@"domain_name"
                                                                  inSection:@"app"
                                                                withDefault:@"sip.linphone.org"]
                       .UTF8String) != 0) {
                    NSLog(@"Migrating proxy config to send quality report");
                    linphone_proxy_config_set_quality_reporting_collector(
                                                                          proxy, "sip:voip-metrics@sip.linphone.org;transport=tls");
                    linphone_proxy_config_set_quality_reporting_interval(proxy, 180);
                    linphone_proxy_config_enable_quality_reporting(proxy, TRUE);
                }
            proxies = proxies->next;
        }
        [self lpConfigSetBool:TRUE forKey:@"quality_report_migration_done"];
    }
    /* File transfer migration */
    if ([self lpConfigBoolForKey:@"file_transfer_migration_done"] == FALSE) {
        const char *newURL = "https://www.linphone.org:444/lft.php";
        NSLog(@"Migrating sharing server url from %s to %s", linphone_core_get_file_transfer_server(LC), newURL);
        linphone_core_set_file_transfer_server(LC, newURL);
        [self lpConfigSetBool:TRUE forKey:@"file_transfer_migration_done"];
    }
}
#pragma mark 转移推送通知当前用户
- (void)migratePushNotificationPerAccount {
    NSString *s = [self lpConfigStringForKey:@"pushnotification_preference"];
    if (s && s.boolValue) {
        NSLog(@"Migrating push notification per account, enabling for ALL");
        [self lpConfigSetBool:NO forKey:@"pushnotification_preference"];
        const MSList *proxies = linphone_core_get_proxy_config_list(LC);
        while (proxies) {
            linphone_proxy_config_set_ref_key(proxies->data, "push_notification");
            [self configurePushTokenForProxyConfig:proxies->data];
            proxies = proxies->next;
        }
    }
}
#pragma mark 转移向导到助手
static void migrateWizardToAssistant(const char *entry, void *user_data) {
    LinphoneManager *thiz = (__bridge LinphoneManager *)(user_data);
    NSString *key = [NSString stringWithUTF8String:entry];
    [thiz lpConfigSetString:[thiz lpConfigStringForKey:key inSection:@"wizard"] forKey:key inSection:@"assistant"];
}
 
 
static OSStatus extracted(CFStringRef *newRoute, UInt32 *newRouteSize) {
    return AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, newRouteSize, newRoute);
}
 
- (OSStatus)extracted:(CFStringRef *)newRoute newRouteSize:(UInt32 *)newRouteSize {
    return extracted(newRoute, newRouteSize);
}
 
- (void)extracted:(NSDictionary *)dict {
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneBluetoothAvailabilityUpdate
                                                      object:self
                                                    userInfo:dict];
}
 
- (void)audioRouteChangeListenerCallback:(NSNotification *)notif {
    if (IPAD)
        return;
    
    // there is at least one bug when you disconnect an audio bluetooth headset
    // since we only get notification of route having changed, we cannot tell if that is due to:
    // -bluetooth headset disconnected or
    // -user wanted to use earpiece
    // the only thing we can assume is that when we lost a device, it must be a bluetooth one (strong hypothesis though)
    if ([[notif.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue] ==
        AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
        _bluetoothAvailable = NO;
    }
    
    CFStringRef newRoute = CFSTR("Unknown");
    UInt32 newRouteSize = sizeof(newRoute);
    
#warning deprecated
    OSStatus status = [self extracted:&newRoute newRouteSize:&newRouteSize];
    
    if (!status && newRouteSize > 0) {
        NSString *route = (__bridge NSString *)newRoute;
        NSLog(@"Current audio route is [%s]", [route UTF8String]);
        
        _speakerEnabled = [route isEqualToString:@"Speaker"] || [route isEqualToString:@"SpeakerAndMicrophone"];
        if ([route isEqualToString:@"HeadsetBT"] && !_speakerEnabled) {
            _bluetoothAvailable = TRUE;
            _bluetoothEnabled = TRUE;
        } else {
            _bluetoothEnabled = FALSE;
        }
        NSDictionary *dict = [NSDictionary
                              dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:_bluetoothAvailable], @"available", nil];
        [self extracted:dict];
        CFRelease(newRoute);
    }
}
 
#pragma mark - Audio route Functions
 
- (bool)allowSpeaker {
    if (IPAD)
        return true;
    
    bool allow = true;
    CFStringRef lNewRoute = CFSTR("Unknown");
    UInt32 lNewRouteSize = sizeof(lNewRoute);
    #warning deprecated
    OSStatus lStatus = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &lNewRouteSize, &lNewRoute);
    if (!lStatus && lNewRouteSize > 0) {
        NSString *route = (__bridge NSString *)lNewRoute;
        allow = ![route containsString:@"Heads"] && ![route isEqualToString:@"Lineout"];
        CFRelease(lNewRoute);
    }
    return allow;
}
 
- (void)setSpeakerEnabled:(BOOL)enable {
    
    OSStatus ret;
    _speakerEnabled = enable;
    UInt32 override = kAudioSessionUnspecifiedError;
    
    if (!enable && _bluetoothAvailable) {
        UInt32 bluetoothInputOverride = _bluetoothEnabled;
        #warning deprecated
        ret = AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryEnableBluetoothInput,
                                      sizeof(bluetoothInputOverride), &bluetoothInputOverride);
        // if setting bluetooth failed, it must be because the device is not available
        // anymore (disconnected), so deactivate bluetooth.
        if (ret != kAudioSessionNoError) {
            _bluetoothAvailable = _bluetoothEnabled = FALSE;
        }
    }
    
    if (override != kAudioSessionNoError) {
        if (enable && [self allowSpeaker]) {
            override = kAudioSessionOverrideAudioRoute_Speaker;
            #warning deprecated
            ret = AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(override), &override);
            _bluetoothEnabled = FALSE;
        } else {
            override = kAudioSessionOverrideAudioRoute_None;
            #warning deprecated
            ret = AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(override), &override);
        }
    }
    
//    if (ret != kAudioSessionNoError) {
//        NSLog(@"Failed to change audio route: err %d", ret);
//    }
}
 
- (void)setBluetoothEnabled:(BOOL)enable {
    if (_bluetoothAvailable) {
        // The change of route will be done in setSpeakerEnabled
        _bluetoothEnabled = enable;
        [self setSpeakerEnabled:!_bluetoothEnabled && _speakerEnabled];
    }
}
 
- (void)overrideDefaultSettings {
    
    NSString *factory = [LinphoneManager bundleFile:@"linphonerc-factory"];
    NSString *factoryIpad = [LinphoneManager bundleFile:@"linphonerc-factory~ipad"];
    if (IPAD && [[NSFileManager defaultManager] fileExistsAtPath:factoryIpad]) {
        factory = factoryIpad;
    }
    NSString *confiFileName = [LinphoneManager documentFile:@"linphonerc"];
    _configDb = lp_config_new_with_factory([confiFileName UTF8String], [factory UTF8String]);
}
 
- (void)copyDefaultSettings {
    NSString *src = [LinphoneManager bundleFile:@"linphonerc"];
    NSString *srcIpad = [LinphoneManager bundleFile:@"linphonerc~ipad"];
    if (IPAD && [[NSFileManager defaultManager] fileExistsAtPath:srcIpad]) {
        src = srcIpad;
    }
    NSString *dst = [LinphoneManager documentFile:@"linphonerc"];
    [LinphoneManager copyFile:src destination:dst override:FALSE];
}
 
 
#pragma mark - Linphone Core Functions
+ (LinphoneCore *)getLc {
    if (theLinphoneCore == nil) {
        @throw([NSException exceptionWithName:@"LinphoneCoreException"
                                       reason:@"Linphone core not initialized yet"
                                     userInfo:nil]);
    }
    return theLinphoneCore;
}
 
#pragma mark Debug functions
+ (void)dumpLcConfig {
    if (theLinphoneCore) {
        LpConfig *conf = LinphoneManager.instance.configDb;
        char *config = lp_config_dump(conf);
        NSLog(@"\n%s", config);
        ms_free(config);
    }
}
#pragma mark - Logs Functions handlers
static void linphone_iphone_log_user_info(struct _LinphoneCore *lc, const char *message) {
//    linphone_core_set_log_handler(NULL, ORTP_MESSAGE, message, NULL);
}
static void linphone_iphone_log_user_warning(struct _LinphoneCore *lc, const char *message) {
//    linphone_iphone_log_handler(NULL, ORTP_WARNING, message, NULL);
}
#pragma mark - Display Status Functions
 
- (void)displayStatus:(NSString *)message {
    // Post event
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneDisplayStatusUpdate
                                                      object:self
                                                    userInfo:@{
                                                               @"message" : message
                                                               }];
}
 
static void linphone_iphone_display_status(struct _LinphoneCore *lc, const char *message) {
    NSString *status = [[NSString alloc] initWithCString:message encoding:[NSString defaultCStringEncoding]];
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) displayStatus:status];
}
#pragma mark - Call State Functions
 
- (void)localNotifContinue:(NSTimer *)timer {
    UILocalNotification *notif = [timer userInfo];
    if (notif) {
        NSLog(@"cancelling/presenting local notif");
        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        [[UIApplication sharedApplication] presentLocalNotificationNow:notif];
    }
}
 
- (void)userNotifContinue:(NSTimer *)timer {
    if (@available(iOS 10.0, *)) {
        UNNotificationContent *content = [timer userInfo];
        if (content && [UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
            NSLog(@"cancelling/presenting user notif");
            UNNotificationRequest *req =
            [UNNotificationRequest requestWithIdentifier:@"call_request" content:content trigger:NULL];
            [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:req
                                                                   withCompletionHandler:^(NSError *_Nullable error) {
                                                                       // Enable or disable features based on authorization.
                                                                       if (error) {
                                                                           NSLog(@"Error while adding notification request :");
                                                                           NSLog(@"%@", error.description);
                                                                       }
                                                                   }];
        }
    } else {
        // Fallback on earlier versions
    }
   
}
 
 
- (void)onCall:(LinphoneCall *)call StateChanged:(LinphoneCallState)state withMessage:(const char *)message {
    // Handling wrapper
    LinphoneCallAppData *data = (__bridge LinphoneCallAppData *)linphone_call_get_user_data(call);
    if (!data) {
        data = [[LinphoneCallAppData alloc] init];
        linphone_call_set_user_data(call, (void *)CFBridgingRetain(data));
    }
    
#pragma deploymate push "ignored-api-availability"
    if (_silentPushCompletion) {
        // we were woken up by a silent push. Call the completion handler with NEWDATA
        // so that the push is notified to the user
        NSLog(@"onCall - handler %p", _silentPushCompletion);
        _silentPushCompletion(UIBackgroundFetchResultNewData);
        _silentPushCompletion = nil;
    }
#pragma deploymate pop
    
//    const LinphoneAddress *addr = linphone_call_get_remote_address(call);
//    NSString *address = [FastAddressBook displayNameForAddress:addr];
    
    if (state == LinphoneCallIncomingReceived) {
        // TESTING !!
        // linphone_call_accept_early_media(call);
        LinphoneCallLog *callLog = linphone_call_get_call_log(call);
        NSString *callId = [NSString stringWithUTF8String:linphone_call_log_get_call_id(callLog)];
        int index = [(NSNumber *)[_pushDict objectForKey:callId] intValue] - 1;
        [_pushDict setValue:[NSNumber numberWithInt:index] forKey:callId];
        BOOL need_bg_task = FALSE;
        for (NSString *key in [_pushDict allKeys]) {
            int value = [(NSNumber *)[_pushDict objectForKey:key] intValue];
            if (value > 0) {
                need_bg_task = TRUE;
                break;
            }
        }
        if (pushBgTask && !need_bg_task) {
            NSLog(@"Call received, stopping background task");
            [[UIApplication sharedApplication] endBackgroundTask:pushBgTask];
            pushBgTask = 0;
        }
        /*first step is to re-enable ctcall center*/
        CTCallCenter *lCTCallCenter = [[CTCallCenter alloc] init];
        
        /*should we reject this call ?*/
        if ([lCTCallCenter currentCalls] != nil &&
            floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
            char *tmp = linphone_call_get_remote_address_as_string(call);
            if (tmp) {
                NSLog(@"Mobile call ongoing... rejecting call from [%s]", tmp);
                ms_free(tmp);
            }
            linphone_call_decline(call, LinphoneReasonBusy);
            return;
        }
        
        if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max && call &&
            (linphone_core_get_calls_nb(LC) < 2)) {
#if !TARGET_IPHONE_SIMULATOR
//            NSString *callId =
//            [NSString stringWithUTF8String:linphone_call_log_get_call_id(linphone_call_get_call_log(call))];
//
//            NSUUID *uuid = [NSUUID UUID];
//            [LinphoneManager.instance.providerDelegate.calls setObject:callId forKey:uuid];
//            [LinphoneManager.instance.providerDelegate.uuids setObject:uuid forKey:callId];
//            BOOL video = FALSE;
//            video = (([UIApplication sharedApplication].applicationState != UIApplicationStateBackground) &&
//                     linphone_core_get_video_policy(LC)->automatically_accept &&
//                     linphone_call_params_video_enabled(linphone_call_get_remote_params(call)));
//            [LinphoneManager.instance.providerDelegate reportIncomingCallwithUUID:uuid handle:address video:video];
#else
//            [PhoneMainView.instance displayIncomingCall:call];
#endif
        } else if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
            // Create a UNNotification
            if (@available(iOS 10.0, *)) {
                UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
                content.title = NSLocalizedString(@"Incoming call", nil);
                //            content.body = address;
                content.sound = [UNNotificationSound soundNamed:@"notes_of_the_optimistic.caf"];
                content.categoryIdentifier = @"call_cat";
                content.userInfo = @{ @"CallId" : callId };
                UNNotificationRequest *req =
                [UNNotificationRequest requestWithIdentifier:@"call_request" content:content trigger:NULL];
                [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:req
                                                                       withCompletionHandler:^(NSError *err){
                                                                       }];
            } else {
                // Fallback on earlier versions
            }
            
        }
        
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
            // if (![LinphoneManager.instance popPushCallID:callId]) {
            // case where a remote notification is not already received
            // Create a new local notification
            if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
                UIMutableUserNotificationAction *answer = [[UIMutableUserNotificationAction alloc] init];
                answer.identifier = @"answer";
                answer.title = NSLocalizedString(@"Answer", nil);
                answer.activationMode = UIUserNotificationActivationModeForeground;
                answer.destructive = NO;
                answer.authenticationRequired = YES;
                
                UIMutableUserNotificationAction *decline = [[UIMutableUserNotificationAction alloc] init];
                decline.identifier = @"decline";
                decline.title = NSLocalizedString(@"Decline", nil);
                decline.activationMode = UIUserNotificationActivationModeBackground;
                decline.destructive = YES;
                decline.authenticationRequired = NO;
                
                NSArray *callactions = @[ decline, answer ];
                
                UIMutableUserNotificationCategory *callcat = [[UIMutableUserNotificationCategory alloc] init];
                callcat.identifier = @"incoming_call";
                [callcat setActions:callactions forContext:UIUserNotificationActionContextDefault];
                [callcat setActions:callactions forContext:UIUserNotificationActionContextMinimal];
                
                NSSet *categories = [NSSet setWithObjects:callcat, nil];
                
                UIUserNotificationSettings *set = [UIUserNotificationSettings
                                                   settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |
                                                                     UIUserNotificationTypeSound)
                                                   categories:categories];
                [[UIApplication sharedApplication] registerUserNotificationSettings:set];
                data->notification = [[UILocalNotification alloc] init];
                if (data->notification) {
                    // iOS8 doesn't need the timer trick for the local notification.
                    data->notification.category = @"incoming_call";
                    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8 &&
                        [self lpConfigBoolForKey:@"repeat_call_notification"] == NO) {
                        NSString *ring =
                        ([LinphoneManager bundleFile:[self lpConfigStringForKey:@"local_ring" inSection:@"sound"].lastPathComponent]
                         ?: [LinphoneManager bundleFile:@"notes_of_the_optimistic.caf"])
                        .lastPathComponent;
                        data->notification.soundName = ring;
                    } else {
                        data->notification.soundName = @"shortring.caf";
                        data->timer = [NSTimer scheduledTimerWithTimeInterval:5
                                                                       target:self
                                                                     selector:@selector(localNotifContinue:)
                                                                     userInfo:data->notification
                                                                      repeats:TRUE];
                    }
                    
                    data->notification.repeatInterval = 0;
                    
//                    data->notification.alertBody =
//                    [NSString stringWithFormat:NSLocalizedString(@"IC_MSG", nil), address];
                    //data->notification.alertAction = NSLocalizedString(@"Answer", nil);
                    data->notification.userInfo = @{ @"callId" : callId, @"timer" : [NSNumber numberWithInt:1] };
                    data->notification.applicationIconBadgeNumber = 1;
                    UIApplication *app = [UIApplication sharedApplication];
                    NSLog(@"%@", [app currentUserNotificationSettings].description);
                    [app presentLocalNotificationNow:data->notification];
                    
                    if (!incallBgTask) {
                        incallBgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
                            NSLog(@"Call cannot ring any more, too late");
                            [[UIApplication sharedApplication] endBackgroundTask:incallBgTask];
                            incallBgTask = 0;
                        }];
                        
                        if (data->timer) {
                            [[NSRunLoop currentRunLoop] addTimer:data->timer forMode:NSRunLoopCommonModes];
                        }
                    }
                }
            }
            
        }
    }
    
    
    // we keep the speaker auto-enabled state in this static so that we don't
    // force-enable it on ICE re-invite if the user disabled it.
    static BOOL speaker_already_enabled = FALSE;
    
    // Disable speaker when no more call
    if ((state == LinphoneCallEnd || state == LinphoneCallError)) {
        speaker_already_enabled = FALSE;
        if (linphone_core_get_calls_nb(theLinphoneCore) == 0) {
            [self setSpeakerEnabled:FALSE];
            [self removeCTCallCenterCb];
            // disable this because I don't find anygood reason for it: _bluetoothAvailable = FALSE;
            // furthermore it introduces a bug when calling multiple times since route may not be
            // reconfigured between cause leading to bluetooth being disabled while it should not
            _bluetoothEnabled = FALSE;
            /*IOS specific*/
            linphone_core_start_dtmf_stream(theLinphoneCore);
        }
        
        if (incallBgTask) {
            [[UIApplication sharedApplication] endBackgroundTask:incallBgTask];
            incallBgTask = 0;
        }
        
        if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) {
            if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
                if (data->timer) {
                    [data->timer invalidate];
                    data->timer = nil;
                }
                LinphoneCallLog *UNlog = linphone_call_get_call_log(call);
                if (UNlog == NULL || linphone_call_log_get_status(UNlog) == LinphoneCallMissed) {
                    if (@available(iOS 10.0, *)) {
                        UNMutableNotificationContent *missed_content = [[UNMutableNotificationContent alloc] init];
                        missed_content.title = NSLocalizedString(@"Missed call", nil);
                        //                    missed_content.body = address;
                        UNNotificationRequest *missed_req = [UNNotificationRequest requestWithIdentifier:@"call_request"
                                                                                                 content:missed_content
                                                                                                 trigger:NULL];
                        [[UNUserNotificationCenter currentNotificationCenter]
                         addNotificationRequest:missed_req
                         withCompletionHandler:^(NSError *_Nullable error) {
                             // Enable or disable features based on authorization.
                             if (error) {
                                 NSLog(@"Error while adding notification request :");
                                 NSLog(@"%@", error.description);
                             }
                         }];
                    } else {
                        // Fallback on earlier versions
                    }
                   
                }
                linphone_core_set_network_reachable(LC, FALSE);
                LinphoneManager.instance.connectivity = none;
            }
//            LinphoneCallLog *callLog2 = linphone_call_get_call_log(call);
//            NSString *callId2 = [NSString stringWithUTF8String:linphone_call_log_get_call_id(callLog2)];
//            NSUUID *uuid = (NSUUID *)[self.providerDelegate.uuids objectForKey:callId2];
//            if (uuid) {
//                // For security reasons do not display name
//                // CXCallUpdate *update = [[CXCallUpdate alloc] init];
//                // update.remoteHandle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:@"Unknown"];
//                //[LinphoneManager.instance.providerDelegate.provider reportCallWithUUID:uuid updated:update];
//                
//                if (linphone_core_get_calls_nb(LC) > 0 && !_conf) {
//                    // Create a CallKit call because there's not !
//                    _conf = FALSE;
//                    LinphoneCall *callKit_call = (LinphoneCall *)linphone_core_get_calls(LC)->data;
//                    NSString *callKit_callId = [NSString
//                                                stringWithUTF8String:linphone_call_log_get_call_id(linphone_call_get_call_log(callKit_call))];
//                    NSUUID *callKit_uuid = [NSUUID UUID];
//                    [LinphoneManager.instance.providerDelegate.uuids setObject:callKit_uuid forKey:callKit_callId];
//                    [LinphoneManager.instance.providerDelegate.calls setObject:callKit_callId forKey:callKit_uuid];
//                    NSString *address =
//                    [FastAddressBook displayNameForAddress:linphone_call_get_remote_address(callKit_call)];
//                    CXHandle *handle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:address];
//                    CXStartCallAction *act = [[CXStartCallAction alloc] initWithCallUUID:callKit_uuid handle:handle];
//                    CXTransaction *tr = [[CXTransaction alloc] initWithAction:act];
//                    [LinphoneManager.instance.providerDelegate.controller requestTransaction:tr
//                                                                                  completion:^(NSError *err){
//                                                                                  }];
//                    [LinphoneManager.instance.providerDelegate.provider reportOutgoingCallWithUUID:callKit_uuid
//                                                                           startedConnectingAtDate:nil];
//                    [LinphoneManager.instance.providerDelegate.provider reportOutgoingCallWithUUID:callKit_uuid
//                                                                                   connectedAtDate:nil];
//                }
//                
//                [self.providerDelegate.uuids removeObjectForKey:callId2];
//                [self.providerDelegate.calls removeObjectForKey:uuid];
//                CXEndCallAction *act = [[CXEndCallAction alloc] initWithCallUUID:uuid];
//                CXTransaction *tr = [[CXTransaction alloc] initWithAction:act];
//                [LinphoneManager.instance.providerDelegate.controller requestTransaction:tr
//                                                                              completion:^(NSError *err){
//                                                                              }];
//            }
        } else {
            if (data != nil && data->notification != nil) {
                LinphoneCallLog *log = linphone_call_get_call_log(call);
                
                // cancel local notif if needed
                if (data->timer) {
                    [data->timer invalidate];
                    data->timer = nil;
                }
                [[UIApplication sharedApplication] cancelLocalNotification:data->notification];
                
                data->notification = nil;
                
                if (log == NULL || linphone_call_log_get_status(log) == LinphoneCallMissed) {
                    UILocalNotification *notification = [[UILocalNotification alloc] init];
                    notification.repeatInterval = 0;
                    notification.alertBody =
//                    [NSString stringWithFormat:NSLocalizedString(@"You missed a call from %@", nil), address];
                    notification.alertAction = NSLocalizedString(@"Show", nil);
                    notification.userInfo = [NSDictionary
                                             dictionaryWithObject:[NSString stringWithUTF8String:linphone_call_log_get_call_id(log)]
                                             forKey:@"callLog"];
                    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
                }
            }
        }
        if (state == LinphoneCallError) {
//            [PhoneMainView.instance popCurrentView];
        }
    }
    
    if (state == LinphoneCallReleased) {
        if (data != NULL) {
            linphone_call_set_user_data(call, NULL);
            CFBridgingRelease((__bridge CFTypeRef)(data));
        }
    }
    
    // Enable speaker when video
    if (state == LinphoneCallIncomingReceived || state == LinphoneCallOutgoingInit || state == LinphoneCallConnected ||
        state == LinphoneCallStreamsRunning) {
        if (linphone_call_params_video_enabled(linphone_call_get_current_params(call)) && !speaker_already_enabled) {
            [self setSpeakerEnabled:TRUE];
            speaker_already_enabled = TRUE;
        }
    }
    
    if (state == LinphoneCallConnected && !mCallCenter) {
        /*only register CT call center CB for connected call*/
        [self setupGSMInteraction];
    }
    // Post event
    NSDictionary *dict = @{
                           @"call" : [NSValue valueWithPointer:call],
                           @"state" : [NSNumber numberWithInt:state],
                           @"message" : [NSString stringWithUTF8String:message]
                           };
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneCallUpdate object:self userInfo:dict];
 
}
static void linphone_iphone_call_state(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState state,
                                       const char *message) {
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onCall:call StateChanged:state withMessage:message];
}
 
#pragma mark - Transfert State Functions
 
static void linphone_iphone_transfer_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState state) {
}
 
#pragma mark - Global state change
 
static void linphone_iphone_global_state_changed(LinphoneCore *lc, LinphoneGlobalState gstate, const char *message) {
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onGlobalStateChanged:gstate withMessage:message];
}
 
- (void)onGlobalStateChanged:(LinphoneGlobalState)state withMessage:(const char *)message {
    NSLog(@"onGlobalStateChanged: %d (message: %s)", state, message);
    
    NSDictionary *dict = [NSDictionary
                          dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:state], @"state",
                          [NSString stringWithUTF8String:message ? message : ""], @"message", nil];
    
    // dispatch the notification asynchronously
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneGlobalStateUpdate object:self userInfo:dict];
    });
}
 
- (void)globalStateChangedNotificationHandler:(NSNotification *)notif {
    if ((LinphoneGlobalState)[[[notif userInfo] valueForKey:@"state"] integerValue] == LinphoneGlobalOn) {
        [self finishCoreConfiguration];
    }
}
 
#pragma mark - Configuring status changed
 
static void linphone_iphone_configuring_status_changed(LinphoneCore *lc, LinphoneConfiguringState status,
                                                       const char *message) {
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onConfiguringStatusChanged:status withMessage:message];
}
 
- (void)onConfiguringStatusChanged:(LinphoneConfiguringState)status withMessage:(const char *)message {
    NSLog(@"onConfiguringStatusChanged: %s %@", linphone_configuring_state_to_string(status),
         message ? [NSString stringWithFormat:@"(message: %s)", message] : @"");
    
    NSDictionary *dict = [NSDictionary
                          dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:status], @"state",
                          [NSString stringWithUTF8String:message ? message : ""], @"message", nil];
    
    // dispatch the notification asynchronously
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneConfiguringStateUpdate
                                                          object:self
                                                        userInfo:dict];
    });
}
 
- (void)configuringStateChangedNotificationHandler:(NSNotification *)notif {
    _wasRemoteProvisioned = ((LinphoneConfiguringState)[[[notif userInfo] valueForKey:@"state"] integerValue] ==
                             LinphoneConfiguringSuccessful);
    if (_wasRemoteProvisioned) {
        LinphoneProxyConfig *cfg = linphone_core_get_default_proxy_config(LC);
        if (cfg) {
            [self configurePushTokenForProxyConfig:cfg];
        }
    }
}
 
#pragma mark - Registration State Functions
 
- (void)onRegister:(LinphoneCore *)lc
               cfg:(LinphoneProxyConfig *)cfg
             state:(LinphoneRegistrationState)state
           message:(const char *)cmessage {
    NSLog(@"New registration state: %s (message: %s)", linphone_registration_state_to_string(state), cmessage);
    
    LinphoneReason reason = linphone_proxy_config_get_error(cfg);
    NSString *message = nil;
    switch (reason) {
        case LinphoneReasonBadCredentials:
            message = NSLocalizedString(@"Bad credentials, check your account settings", nil);
            break;
        case LinphoneReasonNoResponse:
            message = NSLocalizedString(@"No response received from remote", nil);
            break;
        case LinphoneReasonUnsupportedContent:
            message = NSLocalizedString(@"Unsupported content", nil);
            break;
        case LinphoneReasonIOError:
            message = NSLocalizedString(
                                        @"Cannot reach the server: either it is an invalid address or it may be temporary down.", nil);
            break;
            
        case LinphoneReasonUnauthorized:
            message = NSLocalizedString(@"Operation is unauthorized because missing credential", nil);
            break;
        case LinphoneReasonNoMatch:
            message = NSLocalizedString(@"Operation could not be executed by server or remote client because it "
                                        @"didn't have any context for it",
                                        nil);
            break;
        case LinphoneReasonMovedPermanently:
            message = NSLocalizedString(@"Resource moved permanently", nil);
            break;
        case LinphoneReasonGone:
            message = NSLocalizedString(@"Resource no longer exists", nil);
            break;
        case LinphoneReasonTemporarilyUnavailable:
            message = NSLocalizedString(@"Temporarily unavailable", nil);
            break;
        case LinphoneReasonAddressIncomplete:
            message = NSLocalizedString(@"Address incomplete", nil);
            break;
        case LinphoneReasonNotImplemented:
            message = NSLocalizedString(@"Not implemented", nil);
            break;
        case LinphoneReasonBadGateway:
            message = NSLocalizedString(@"Bad gateway", nil);
            break;
        case LinphoneReasonServerTimeout:
            message = NSLocalizedString(@"Server timeout", nil);
            break;
        case LinphoneReasonNotAcceptable:
        case LinphoneReasonDoNotDisturb:
        case LinphoneReasonDeclined:
        case LinphoneReasonNotFound:
        case LinphoneReasonNotAnswered:
        case LinphoneReasonBusy:
        case LinphoneReasonNone:
        case LinphoneReasonUnknown:
            message = NSLocalizedString(@"Unknown error", nil);
            break;
    }
    
    // Post event
    NSDictionary *dict =
    [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:state], @"state",
     [NSValue valueWithPointer:cfg], @"cfg", message, @"message", nil];
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneRegistrationUpdate object:self userInfo:dict];
 
    
}
 
static void linphone_iphone_registration_state(LinphoneCore *lc, LinphoneProxyConfig *cfg,
                                               LinphoneRegistrationState state, const char *message) {
    
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onRegister:lc cfg:cfg state:state message:message];
    
}
#pragma mark - Auth info Function
 
static void linphone_iphone_popup_password_request(LinphoneCore *lc, const char *realmC, const char *usernameC,
                                                   const char *domainC) {
   
}
 
#pragma mark - Text Received Functions
//IM消息接收
- (void)onMessageReceived:(LinphoneCore *)lc room:(LinphoneChatRoom *)room message:(LinphoneChatMessage *)msg {
   
}
 
static void linphone_iphone_message_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *message) {
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onMessageReceived:lc room:room message:message];
}
 
static void linphone_iphone_message_received_unable_decrypt(LinphoneCore *lc, LinphoneChatRoom *room,
                                                            LinphoneChatMessage *message) {
    
    NSString *msgId = [NSString stringWithUTF8String:linphone_chat_message_get_custom_header(message, "Call-ID")];
    int index = [(NSNumber *)[LinphoneManager.instance.pushDict objectForKey:msgId] intValue] - 1;
    [LinphoneManager.instance.pushDict setValue:[NSNumber numberWithInt:index] forKey:msgId];
    BOOL need_bg_task = FALSE;
    for (NSString *key in [LinphoneManager.instance.pushDict allKeys]) {
        int value = [(NSNumber *)[LinphoneManager.instance.pushDict objectForKey:key] intValue];
        if (value > 0) {
            need_bg_task = TRUE;
            break;
        }
    }
    if (theLinphoneManager->pushBgTask && !need_bg_task) {
        NSLog(@"Message received, stopping background task");
        [[UIApplication sharedApplication] endBackgroundTask:theLinphoneManager->pushBgTask];
        theLinphoneManager->pushBgTask = 0;
    }
//    const LinphoneAddress *address = linphone_chat_message_get_peer_address(message);
//    NSString *strAddr = [FastAddressBook displayNameForAddress:address];
    NSString *title = NSLocalizedString(@"LIME warning", nil);
//    NSString *body = [NSString
//                      stringWithFormat:NSLocalizedString(@"You have received an encrypted message you are unable to decrypt from "
//                                                         @"%@.\nYou need to call your correspondant in order to exchange your ZRTP "
//                                                         @"keys if you want to decrypt the future messages you will receive.",
//                                                         nil),
//                      strAddr];
//    NSString *action = NSLocalizedString(@"Call", nil);
    
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
        if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) {
            if (@available(iOS 10.0, *)) {
                UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
                content.title = title;
                //            content.body = body;
                UNNotificationRequest *req =
                [UNNotificationRequest requestWithIdentifier:@"decrypt_request" content:content trigger:NULL];
                [[UNUserNotificationCenter currentNotificationCenter]
                 addNotificationRequest:req
                 withCompletionHandler:^(NSError *_Nullable error) {
                     // Enable or disable features based on authorization.
                     if (error) {
                         NSLog(@"Error while adding notification request :");
                         NSLog(@"%@", error.description);
                     }
                 }];
            } else {
                // Fallback on earlier versions
            }
            
        } else {
            UILocalNotification *notification = [[UILocalNotification alloc] init];
            notification.repeatInterval = 0;
            if (@available(iOS 8.2, *)) {
                notification.alertTitle = title;
            } else {
                // Fallback on earlier versions
            }
//            notification.alertBody = body;
            [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
        }
    } else {
       
    }
}
 
- (LinphoneAddress *)normalizeSipOrPhoneAddress:(NSString *)value {
    
    if (!value) {
        return NULL;
    }
    LinphoneProxyConfig *cfg = linphone_core_get_default_proxy_config(LC);
    const char * normvalue;
    if (linphone_proxy_config_is_phone_number(cfg, value.UTF8String)) {
        normvalue = linphone_proxy_config_normalize_phone_number(cfg, value.UTF8String);
    } else {
        normvalue = value.UTF8String;
    }
    LinphoneAddress *addr = linphone_proxy_config_normalize_sip_uri(cfg, normvalue);
    
    // first try to find a friend with the given address
    
    // since user wants to escape plus, we assume it expects to have phone numbers by default
    if (addr && cfg && (linphone_proxy_config_get_dial_escape_plus(cfg))) {
        linphone_address_set_username(addr, normvalue);
    } else {
        linphone_address_set_username(addr, value.UTF8String);
    }
    
    return addr;
}
- (void)onNotifyReceived:(LinphoneCore *)lc
                   event:(LinphoneEvent *)lev
             notifyEvent:(const char *)notified_event
                 content:(const LinphoneContent *)body {
    // Post event
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[NSValue valueWithPointer:lev] forKey:@"event"];
    [dict setObject:[NSString stringWithUTF8String:notified_event] forKey:@"notified_event"];
    if (body != NULL) {
        [dict setObject:[NSValue valueWithPointer:body] forKey:@"content"];
    }
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneNotifyReceived object:self userInfo:dict];
}
 
static void linphone_iphone_notify_received(LinphoneCore *lc, LinphoneEvent *lev, const char *notified_event,
                                            const LinphoneContent *body) {
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onNotifyReceived:lc
                                                                            event:lev
                                                                      notifyEvent:notified_event
                                                                          content:body];
}
 
- (void)onNotifyPresenceReceivedForUriOrTel:(LinphoneCore *)lc
                                     friend:(LinphoneFriend *)lf
                                        uri:(const char *)uri
                              presenceModel:(const LinphonePresenceModel *)model {
    // Post event
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[NSValue valueWithPointer:lf] forKey:@"friend"];
    [dict setObject:[NSValue valueWithPointer:uri] forKey:@"uri"];
    [dict setObject:[NSValue valueWithPointer:model] forKey:@"presence_model"];
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneNotifyPresenceReceivedForUriOrTel
                                                      object:self
                                                    userInfo:dict];
}
 
static void linphone_iphone_notify_presence_received_for_uri_or_tel(LinphoneCore *lc, LinphoneFriend *lf,
                                                                    const char *uri_or_tel,
                                                                    const LinphonePresenceModel *presence_model) {
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onNotifyPresenceReceivedForUriOrTel:lc
                                                                                              friend:lf
                                                                                                 uri:uri_or_tel
                                                                                       presenceModel:presence_model];
}
 
static void linphone_iphone_call_encryption_changed(LinphoneCore *lc, LinphoneCall *call, bool_t on,
                                                    const char *authentication_token) {
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onCallEncryptionChanged:lc
                                                                                    call:call
                                                                                      on:on
                                                                                   token:authentication_token];
}
 
- (void)onCallEncryptionChanged:(LinphoneCore *)lc
                           call:(LinphoneCall *)call
                             on:(BOOL)on
                          token:(const char *)authentication_token {
    // Post event
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[NSValue valueWithPointer:call] forKey:@"call"];
    [dict setObject:[NSNumber numberWithBool:on] forKey:@"on"];
    if (authentication_token) {
        [dict setObject:[NSString stringWithUTF8String:authentication_token] forKey:@"token"];
    }
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneCallEncryptionChanged object:self userInfo:dict];
}
 
- (void)onMessageComposeReceived:(LinphoneCore *)core forRoom:(LinphoneChatRoom *)room {
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneTextComposeEvent
                                                      object:self
                                                    userInfo:@{
                                                               @"room" : [NSValue valueWithPointer:room]
                                                               }];
}
 
static void linphone_iphone_is_composing_received(LinphoneCore *lc, LinphoneChatRoom *room) {
    [(__bridge LinphoneManager *)linphone_core_get_user_data(lc) onMessageComposeReceived:lc forRoom:room];
}
 
#pragma mark - Network Functions
 
- (SCNetworkReachabilityRef)getProxyReachability {
    return proxyReachability;
}
+ (void)kickOffNetworkConnection {
    static BOOL in_progress = FALSE;
    if (in_progress) {
        NSLog(@"Connection kickoff already in progress");
        return;
    }
    in_progress = TRUE;
    /* start a new thread to avoid blocking the main ui in case of peer host failure */
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        static int sleep_us = 10000;
        static int timeout_s = 5;
        BOOL timeout_reached = FALSE;
        int loop = 0;
        CFWriteStreamRef writeStream;
        CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef) @"192.168.0.200" /*"linphone.org"*/, 15000, nil,
                                           &writeStream);
        BOOL res = CFWriteStreamOpen(writeStream);
        const char *buff = "hello";
        time_t start = time(NULL);
        time_t loop_time;
        
        if (res == FALSE) {
            NSLog(@"Could not open write stream, backing off");
            CFRelease(writeStream);
            in_progress = FALSE;
            return;
        }
        
        // check stream status and handle timeout
        CFStreamStatus status = CFWriteStreamGetStatus(writeStream);
        while (status != kCFStreamStatusOpen && status != kCFStreamStatusError) {
            usleep(sleep_us);
            status = CFWriteStreamGetStatus(writeStream);
            loop_time = time(NULL);
            if (loop_time - start >= timeout_s) {
                timeout_reached = TRUE;
                break;
            }
            loop++;
        }
        
        if (status == kCFStreamStatusOpen) {
            CFWriteStreamWrite(writeStream, (const UInt8 *)buff, strlen(buff));
        } else if (!timeout_reached) {
            CFErrorRef error = CFWriteStreamCopyError(writeStream);
            NSLog(@"CFStreamError: %@", error);
            CFRelease(error);
        } else if (timeout_reached) {
            NSLog(@"CFStream timeout reached");
        }
        CFWriteStreamClose(writeStream);
        CFRelease(writeStream);
        in_progress = FALSE;
    });
}
 
+ (NSString *)getCurrentWifiSSID {
#if TARGET_IPHONE_SIMULATOR
    return @"Sim_err_SSID_NotSupported";
#else
    NSString *data = nil;
    CFDictionaryRef dict = CNCopyCurrentNetworkInfo((CFStringRef) @"en0");
    if (dict) {
        NSLog(@"AP Wifi: %@", dict);
        data = [NSString stringWithString:(NSString *)CFDictionaryGetValue(dict, @"SSID")];
        CFRelease(dict);
    }
    return data;
#endif
}
 
static void showNetworkFlags(SCNetworkReachabilityFlags flags) {
    NSMutableString *log = [[NSMutableString alloc] initWithString:@"Network connection flags: "];
    if (flags == 0)
        [log appendString:@"no flags."];
    if (flags & kSCNetworkReachabilityFlagsTransientConnection)
        [log appendString:@"kSCNetworkReachabilityFlagsTransientConnection, "];
    if (flags & kSCNetworkReachabilityFlagsReachable)
        [log appendString:@"kSCNetworkReachabilityFlagsReachable, "];
    if (flags & kSCNetworkReachabilityFlagsConnectionRequired)
        [log appendString:@"kSCNetworkReachabilityFlagsConnectionRequired, "];
    if (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)
        [log appendString:@"kSCNetworkReachabilityFlagsConnectionOnTraffic, "];
    if (flags & kSCNetworkReachabilityFlagsConnectionOnDemand)
        [log appendString:@"kSCNetworkReachabilityFlagsConnectionOnDemand, "];
    if (flags & kSCNetworkReachabilityFlagsIsLocalAddress)
        [log appendString:@"kSCNetworkReachabilityFlagsIsLocalAddress, "];
    if (flags & kSCNetworkReachabilityFlagsIsDirect)
        [log appendString:@"kSCNetworkReachabilityFlagsIsDirect, "];
    if (flags & kSCNetworkReachabilityFlagsIsWWAN)
        [log appendString:@"kSCNetworkReachabilityFlagsIsWWAN, "];
    NSLog(@"%@", log);
}
 
//This callback keeps tracks of wifi SSID changes.
static void networkReachabilityNotification(CFNotificationCenterRef center, void *observer, CFStringRef name,
                                            const void *object, CFDictionaryRef userInfo) {
    LinphoneManager *mgr = LinphoneManager.instance;
    SCNetworkReachabilityFlags flags;
    
    // for an unknown reason, we are receiving multiple time the notification, so
    // we will skip each time the SSID did not change
    NSString *newSSID = [LinphoneManager getCurrentWifiSSID];
    if ([newSSID compare:mgr.SSID] == NSOrderedSame)
        return;
    
    
    if (newSSID != Nil && newSSID.length > 0 && mgr.SSID != Nil && newSSID.length > 0){
        if (SCNetworkReachabilityGetFlags([mgr getProxyReachability], &flags)) {
            NSLog(@"Wifi SSID changed, resesting transports.");
            mgr.connectivity=none; //this will trigger a connectivity change in networkReachabilityCallback.
            networkReachabilityCallBack([mgr getProxyReachability], flags, nil);
        }
    }
    mgr.SSID = newSSID;
    
    
}
 
void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *nilCtx) {
    showNetworkFlags(flags);
    LinphoneManager *lm = LinphoneManager.instance;
    SCNetworkReachabilityFlags networkDownFlags = kSCNetworkReachabilityFlagsConnectionRequired |
    kSCNetworkReachabilityFlagsConnectionOnTraffic |
    kSCNetworkReachabilityFlagsConnectionOnDemand;
    
    if (theLinphoneCore != nil) {
        LinphoneProxyConfig *proxy = linphone_core_get_default_proxy_config(theLinphoneCore);
        
        struct NetworkReachabilityContext *ctx = nilCtx ? ((struct NetworkReachabilityContext *)nilCtx) : 0;
        if ((flags == 0) || (flags & networkDownFlags)) {
            linphone_core_set_network_reachable(theLinphoneCore, false);
            lm.connectivity = none;
            [LinphoneManager kickOffNetworkConnection];
        } else {
            Connectivity newConnectivity;
            BOOL isWifiOnly = [lm lpConfigBoolForKey:@"wifi_only_preference" withDefault:FALSE];
            if (!ctx || ctx->testWWan)
                newConnectivity = flags & kSCNetworkReachabilityFlagsIsWWAN ? wwan : wifi;
            else
                newConnectivity = wifi;
            
            if (newConnectivity == wwan && proxy && isWifiOnly &&
                (lm.connectivity == newConnectivity || lm.connectivity == none)) {
                linphone_proxy_config_expires(proxy, 0);
            } else if (proxy) {
                NSInteger defaultExpire = [lm lpConfigIntForKey:@"default_expires"];
                if (defaultExpire >= 0)
                    linphone_proxy_config_expires(proxy, (int)defaultExpire);
                // else keep default value from linphonecore
            }
            
            if (lm.connectivity != newConnectivity) {
                // connectivity has changed
                linphone_core_set_network_reachable(theLinphoneCore, false);
                if (newConnectivity == wwan && proxy && isWifiOnly) {
                    linphone_proxy_config_expires(proxy, 0);
                }
                linphone_core_set_network_reachable(theLinphoneCore, true);
                linphone_core_iterate(theLinphoneCore);
                NSLog(@"Network connectivity changed to type [%s]", (newConnectivity == wifi ? "wifi" : "wwan"));
                lm.connectivity = newConnectivity;
            }
        }
        if (ctx && ctx->networkStateChanged) {
            (*ctx->networkStateChanged)(lm.connectivity);
        }
    }
}
 
#pragma mark - 设置网络状态回调
- (void)setupNetworkReachabilityCallback {
    SCNetworkReachabilityContext *ctx = NULL;
    // any internet cnx
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    
    if (proxyReachability) {
        NSLog(@"Cancelling old network reachability");
        SCNetworkReachabilityUnscheduleFromRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
        CFRelease(proxyReachability);
        proxyReachability = nil;
    }
    
    // This notification is used to detect SSID change (switch of Wifi network). The ReachabilityCallback is
    // not triggered when switching between 2 private Wifi...
    // Since we cannot be sure we were already observer, remove ourself each time... to be improved
    _SSID = [LinphoneManager getCurrentWifiSSID];
    CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self),
                                       CFSTR("com.apple.system.config.network_change"), NULL);
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self),
                                    networkReachabilityNotification, CFSTR("com.apple.system.config.network_change"),
                                    NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
    
    proxyReachability =
    SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
    
    if (!SCNetworkReachabilitySetCallback(proxyReachability, (SCNetworkReachabilityCallBack)networkReachabilityCallBack,
                                          ctx)) {
        NSLog(@"Cannot register reachability cb: %s", SCErrorString(SCError()));
        return;
    }
    if (!SCNetworkReachabilityScheduleWithRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) {
        NSLog(@"Cannot register schedule reachability cb: %s", SCErrorString(SCError()));
        return;
    }
    
    // this check is to know network connectivity right now without waiting for a change. Don'nt remove it unless you
    // have good reason. Jehan
    SCNetworkReachabilityFlags flags;
    if (SCNetworkReachabilityGetFlags(proxyReachability, &flags)) {
        networkReachabilityCallBack(proxyReachability, flags, nil);
    }
}
 
- (NetworkType)network {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
        UIApplication *app = [UIApplication sharedApplication];
        NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
        NSNumber *dataNetworkItemView = nil;
        
        for (id subview in subviews) {
            if ([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
                dataNetworkItemView = subview;
                break;
            }
        }
        
        NSNumber *number = (NSNumber *)[dataNetworkItemView valueForKey:@"dataNetworkType"];
        return [number intValue];
    } else {
#pragma deploymate push "ignored-api-availability"
        CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
        NSString *currentRadio = info.currentRadioAccessTechnology;
        if ([currentRadio isEqualToString:CTRadioAccessTechnologyEdge]) {
            return network_2g;
        } else if ([currentRadio isEqualToString:CTRadioAccessTechnologyLTE]) {
            return network_4g;
        }
#pragma deploymate pop
        return network_3g;
    }
}
 
#pragma mark - VTable
 
static LinphoneCoreVTable linphonec_vtable = {
    .call_state_changed = (LinphoneCoreCallStateChangedCb)linphone_iphone_call_state,
    .registration_state_changed = linphone_iphone_registration_state,
    .notify_presence_received_for_uri_or_tel = linphone_iphone_notify_presence_received_for_uri_or_tel,
    .auth_info_requested = linphone_iphone_popup_password_request,
    .message_received = linphone_iphone_message_received,
    .message_received_unable_decrypt = linphone_iphone_message_received_unable_decrypt,
    .transfer_state_changed = linphone_iphone_transfer_state_changed,
    .is_composing_received = linphone_iphone_is_composing_received,
    .configuring_status = linphone_iphone_configuring_status_changed,
    .global_state_changed = linphone_iphone_global_state_changed,
    .notify_received = linphone_iphone_notify_received,
    .call_encryption_changed = linphone_iphone_call_encryption_changed,
};
 
#pragma mark -
 
- (void)audioSessionInterrupted:(NSNotification *)notification {
    int interruptionType = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
    if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
        [self beginInterruption];
    } else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
        [self endInterruption];
    }
}
 
static BOOL libStarted = FALSE;
 
// scheduling loop
- (void)iterate {
    linphone_core_iterate(theLinphoneCore);
}
 
/** Should be called once per linphone_core_new() */
- (void)finishCoreConfiguration {
    
    // get default config from bundle
    NSString *zrtpSecretsFileName = [LinphoneManager documentFile:@"zrtp_secrets"];
    NSString *chatDBFileName = [LinphoneManager documentFile:kLinphoneInternalChatDBFilename];
    
    NSString *device = [[NSMutableString alloc]
                        initWithString:[NSString
                                        stringWithFormat:@"%@_iOS%@",
                                        [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"],
                                        UIDevice.currentDevice.systemVersion]];
    device = [device stringByReplacingOccurrencesOfString:@"," withString:@"."];
    device = [device stringByReplacingOccurrencesOfString:@" " withString:@"."];
    linphone_core_set_user_agent(theLinphoneCore, device.UTF8String, "3.16-122-g79a8bb2");
    
    _contactSipField = [self lpConfigStringForKey:@"contact_im_type_value" withDefault:@"SIP"];
    
//    if (_fastAddressBook == nil) {
//        _fastAddressBook = [[FastAddressBook alloc] init];
//    }
    
    linphone_core_set_zrtp_secrets_file(theLinphoneCore, [zrtpSecretsFileName UTF8String]);
    linphone_core_set_chat_database_path(theLinphoneCore, [chatDBFileName UTF8String]);
    linphone_core_set_call_logs_database_path(theLinphoneCore, [chatDBFileName UTF8String]);
    
    [self setupNetworkReachabilityCallback];
    
    NSString *path = [LinphoneManager bundleFile:@"nowebcamCIF.jpg"];
    if (path) {
        const char *imagePath = [path UTF8String];
        NSLog(@"Using '%s' as source image for no webcam", imagePath);
        linphone_core_set_static_picture(theLinphoneCore, imagePath);
    }
    
    /*DETECT cameras*///检测摄像头
    _frontCamId = _backCamId = nil;
    char **camlist = (char **)linphone_core_get_video_devices(theLinphoneCore);
    if (camlist) {
        for (char *cam = *camlist; *camlist != NULL; cam = *++camlist) {
            if (strcmp(FRONT_CAM_NAME, cam) == 0) {
                _frontCamId = cam;
                // great set default cam to front
                NSLog(@"Setting default camera [%s]", _frontCamId);
                linphone_core_set_video_device(theLinphoneCore, _frontCamId);
            }
            if (strcmp(BACK_CAM_NAME, cam) == 0) {
                _backCamId = cam;
            }
        }
    } else {
        NSLog(@"No camera detected!");
    }
    
    [self enableProxyPublish:([UIApplication sharedApplication].applicationState == UIApplicationStateActive)];
    
    NSLog(@"Linphone [%s]  started on [%s]", linphone_core_get_version(), [[UIDevice currentDevice].model UTF8String]);
    
    // Post event
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:theLinphoneCore] forKey:@"core"];
    
    [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneCoreUpdate
                                                      object:LinphoneManager.instance
                                                    userInfo:dict];
}
 
- (void)startLinphoneCore {
    
    if (libStarted) {
        NSLog(@"Liblinphone is already initialized!");
        return;
    }
    
    libStarted = TRUE;
    
    connectivity = none;
    signal(SIGPIPE, SIG_IGN);
    
    // create linphone core
    [self createLinphoneCore];
//    [self.providerDelegate config];
//    _iapManager = [[InAppProductsManager alloc] init];
    
    // - Security fix - remove multi transport migration, because it enables tcp or udp, if by factoring settings only
    // tls is enabled.     This is a problem for new installations.
    // linphone_core_migrate_to_multi_transport(theLinphoneCore);
    
    // init audio session (just getting the instance will init)
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    BOOL bAudioInputAvailable = audioSession.inputAvailable;
    NSError *err;
    
    if (![audioSession setActive:NO error:&err] && err) {
        NSLog(@"audioSession setActive failed: %@", [err description]);
    }
    if (!bAudioInputAvailable) {
        UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"No microphone", nil)
                                                                         message:NSLocalizedString(@"You need to plug a microphone to your device to use the application.", nil)
                                                                  preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];
        
        [errView addAction:defaultAction];
//        [PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
    }
    
    // Disable notify policy
    LinphoneImNotifPolicy *im_notif_policy;
    im_notif_policy = linphone_core_get_im_notif_policy(theLinphoneCore);
    if (im_notif_policy != NULL) {
        /* The IM notification policy can be NULL at this point in case of remote provisioning. */
        linphone_im_notif_policy_clear(im_notif_policy);
        linphone_im_notif_policy_set_send_is_composing(im_notif_policy, TRUE);
        linphone_im_notif_policy_set_recv_is_composing(im_notif_policy, TRUE);
    }
    
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
        // go directly to bg mode
        [self enterBackgroundMode];
    }
}
 
- (BOOL)enterBackgroundMode {
    
    LinphoneProxyConfig *proxyCfg = linphone_core_get_default_proxy_config(theLinphoneCore);
    BOOL shouldEnterBgMode = FALSE;
    
    // disable presence
    [self enableProxyPublish:NO];
    
    // handle proxy config if any
    if (proxyCfg) {
        const char *refkey = proxyCfg ? linphone_proxy_config_get_ref_key(proxyCfg) : NULL;
        BOOL pushNotifEnabled = (refkey && strcmp(refkey, "push_notification") == 0);
        if ([LinphoneManager.instance lpConfigBoolForKey:@"backgroundmode_preference"] || pushNotifEnabled) {
            if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
                // For registration register
                [self refreshRegisters];
            }
        }
        
        if ([LinphoneManager.instance lpConfigBoolForKey:@"backgroundmode_preference"]) {
            // register keepalive
            if ([[UIApplication sharedApplication]
                 setKeepAliveTimeout:600 /*(NSTimeInterval)linphone_proxy_config_get_expires(proxyCfg)*/
                 handler:^{
                     NSLog(@"keepalive handler");
                     mLastKeepAliveDate = [NSDate date];
                     if (theLinphoneCore == nil) {
                         NSLog(@"It seems that Linphone BG mode was deactivated, just skipping");
                         return;
                     }
//                     [_iapManager check];
                     if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
                         // For registration register
                         [self refreshRegisters];
                     }
                     linphone_core_iterate(theLinphoneCore);
                 }]) {
                     
                     NSLog(@"keepalive handler succesfully registered");
                 } else {
                     NSLog(@"keepalive handler cannot be registered");
                 }
            shouldEnterBgMode = TRUE;
        }
    }
    
    LinphoneCall *currentCall = linphone_core_get_current_call(theLinphoneCore);
    const bctbx_list_t *callList = linphone_core_get_calls(theLinphoneCore);
    if (!currentCall // no active call
        && callList  // at least one call in a non active state
        && bctbx_list_find_custom(callList, (bctbx_compare_func)comp_call_state_paused, NULL)) {
        [self startCallPausedLongRunningTask];
    }
    if (callList) {
        /*if at least one call exist, enter normal bg mode */
        shouldEnterBgMode = TRUE;
    }
    /*stop the video preview*/
    if (theLinphoneCore) {
        linphone_core_enable_video_preview(theLinphoneCore, FALSE);
        linphone_core_iterate(theLinphoneCore);
    }
    linphone_core_stop_dtmf_stream(theLinphoneCore);
    
    NSLog(@"Entering [%s] bg mode", shouldEnterBgMode ? "normal" : "lite");
    
    if (!shouldEnterBgMode && floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
        const char *refkey = proxyCfg ? linphone_proxy_config_get_ref_key(proxyCfg) : NULL;
        BOOL pushNotifEnabled = (refkey && strcmp(refkey, "push_notification") == 0);
        if (pushNotifEnabled) {
            NSLog(@"Keeping lc core to handle push");
            /*destroy voip socket if any and reset connectivity mode*/
            connectivity = none;
            linphone_core_set_network_reachable(theLinphoneCore, FALSE);
            return YES;
        }
        return NO;
        
    } else
        return YES;
}
 
- (void)refreshRegisters {
    
    if (connectivity == none) {
        // don't trust ios when he says there is no network. Create a new reachability context, the previous one might
        // be mis-functionning.
        NSLog(@"None connectivity");
        [self setupNetworkReachabilityCallback];
    }
    NSLog(@"Network reachability callback setup");
    if (theLinphoneCore) {
        linphone_core_refresh_registers(theLinphoneCore); // just to make sure REGISTRATION is up to date
    }
}
 
- (void)becomeActive {
    // enable presence
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max || self.connectivity == none) {
        [self refreshRegisters];
    }
    if (pausedCallBgTask) {
        [[UIApplication sharedApplication] endBackgroundTask:pausedCallBgTask];
        pausedCallBgTask = 0;
    }
    if (incallBgTask) {
        [[UIApplication sharedApplication] endBackgroundTask:incallBgTask];
        incallBgTask = 0;
    }
    
    /*IOS specific*/
    linphone_core_start_dtmf_stream(theLinphoneCore);
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
                             completionHandler:^(BOOL granted){
                             }];
    
    /*start the video preview in case we are in the main view*/
    if (linphone_core_video_display_enabled(theLinphoneCore) && [self lpConfigBoolForKey:@"preview_preference"]) {
        linphone_core_enable_video_preview(theLinphoneCore, TRUE);
    }
    /*check last keepalive handler date*/
    if (mLastKeepAliveDate != Nil) {
        NSDate *current = [NSDate date];
        if ([current timeIntervalSinceDate:mLastKeepAliveDate] > 700) {
            NSString *datestr = [mLastKeepAliveDate description];
            NSLog(@"keepalive handler was called for the last time at %@", datestr);
        }
    }
    
    [self enableProxyPublish:YES];
}
- (void)createLinphoneCore {
    
    [self migrationAllPre];
    if (theLinphoneCore != nil) {
        NSLog(@"linphonecore is already created");
        return;
    }
//    [Log enableLogs:[self lpConfigIntForKey:@"debugenable_preference"]];
    //日志输出相关
    [self enableLogs:ORTP_DEBUG];
    connectivity = none;
    
    // Set audio assets
    NSString *ring =
    ([LinphoneManager bundleFile:[self lpConfigStringForKey:@"local_ring" inSection:@"sound"].lastPathComponent]
     ?: [LinphoneManager bundleFile:@"notes_of_the_optimistic.caf"])
    .lastPathComponent;
    NSString *ringback =
    ([LinphoneManager bundleFile:[self lpConfigStringForKey:@"remote_ring" inSection:@"sound"].lastPathComponent]
     ?: [LinphoneManager bundleFile:@"ringback.wav"])
    .lastPathComponent;
    
    NSString *hold =
    ([LinphoneManager bundleFile:[self lpConfigStringForKey:@"hold_music" inSection:@"sound"].lastPathComponent]
     ?: [LinphoneManager bundleFile:@"hold.mkv"])
    .lastPathComponent;
    [self lpConfigSetString:[LinphoneManager bundleFile:ring] forKey:@"local_ring" inSection:@"sound"];
    [self lpConfigSetString:[LinphoneManager bundleFile:ringback] forKey:@"remote_ring" inSection:@"sound"];
    [self lpConfigSetString:[LinphoneManager bundleFile:hold] forKey:@"hold_music" inSection:@"sound"];
    
    theLinphoneCore = linphone_core_new_with_config(&linphonec_vtable, _configDb, (__bridge void *)(self));
    NSLog(@"Create linphonecore %p", theLinphoneCore);
    
    // Load plugins if available in the linphone SDK - otherwise these calls will do nothing
    MSFactory *f = linphone_core_get_ms_factory(theLinphoneCore);
    libmssilk_init(f);
    libmsamr_init(f);
    libmsx264_init(f);
    libmsopenh264_init(f);
//    libmsbcg729_init(f);
    libmswebrtc_init(f);
    linphone_core_reload_ms_plugins(theLinphoneCore, NULL);
    [self migrationAllPost];
    //设置CA证书
    /* set the CA file no matter what, since the remote provisioning could be hitting an HTTPS server */
    linphone_core_set_root_ca(theLinphoneCore, [LinphoneManager bundleFile:@"rootca.pem"].UTF8String);
    linphone_core_set_user_certificates_path(theLinphoneCore, [LinphoneManager cacheDirectory].UTF8String);
    
    /* The core will call the linphone_iphone_configuring_status_changed callback when the remote provisioning is loaded
     (or skipped).
     Wait for this to finish the code configuration */
    
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(audioSessionInterrupted:)
                                               name:AVAudioSessionInterruptionNotification
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(globalStateChangedNotificationHandler:)
                                               name:kLinphoneGlobalStateUpdate
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(configuringStateChangedNotificationHandler:)
                                               name:kLinphoneConfiguringStateUpdate
                                             object:nil];
    
    /*call iterate once immediately in order to initiate background connections with sip server or remote provisioning
     * grab, if any */
    linphone_core_iterate(theLinphoneCore);
    // start scheduler
    mIterateTimer =
    [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(iterate) userInfo:nil repeats:YES];
}
 
- (void)resetLinphoneCore {
    [self destroyLinphoneCore];
    [self createLinphoneCore];
    // reload friends
//    [self.fastAddressBook reload];
    
    // reset network state to trigger a new network connectivity assessment
    linphone_core_set_network_reachable(theLinphoneCore, FALSE);
}
 
static int comp_call_id(const LinphoneCall *call, const char *callid) {
    if (linphone_call_log_get_call_id(linphone_call_get_call_log(call)) == nil) {
        ms_error("no callid for call [%p]", call);
        return 1;
    }
    return strcmp(linphone_call_log_get_call_id(linphone_call_get_call_log(call)), callid);
}
 
- (LinphoneCall *)callByCallId:(NSString *)call_id {
    const bctbx_list_t *calls = linphone_core_get_calls(theLinphoneCore);
    if (!calls) {
        return NULL;
    }
    bctbx_list_t *call_tmp = bctbx_list_find_custom(calls, (bctbx_compare_func)comp_call_id, [call_id UTF8String]);
    if (!call_tmp) {
        return NULL;
    }
    LinphoneCall *call = (LinphoneCall *)call_tmp->data;
    return call;
}
- (void)cancelLocalNotifTimerForCallId:(NSString *)callid {
    // first, make sure this callid is not already involved in a call
    const bctbx_list_t *calls = linphone_core_get_calls(theLinphoneCore);
    bctbx_list_t *call = bctbx_list_find_custom(calls, (bctbx_compare_func)comp_call_id, [callid UTF8String]);
    if (call != NULL) {
        LinphoneCallAppData *data =
        (__bridge LinphoneCallAppData *)(linphone_call_get_user_data((LinphoneCall *)call->data));
        if (data->timer)
            [data->timer invalidate];
        data->timer = nil;
        return;
    }
}
#pragma mark - LOG
- (void)enableLogs:(OrtpLogLevel)level {
    BOOL enabled = (level >= ORTP_DEBUG && level < ORTP_ERROR);
    static BOOL stderrInUse = NO;
    if (!stderrInUse) {
        asl_add_log_file(NULL, STDERR_FILENO);
        stderrInUse = YES;
    }
//    linphone_core_set_log_collection_path([self cacheDirectory].UTF8String);
//    linphone_core_enable_logs_with_cb(linphone_iphone_log_handler);
//    linphone_core_enable_log_collection(enabled);
    if (level == 0) {
        linphone_core_set_log_level(ORTP_FATAL);
        ortp_set_log_level("ios", ORTP_FATAL);
        NSLog(@"I/%s/Disabling all logs", ORTP_LOG_DOMAIN);
    } else {
        NSLog(@"I/%s/Enabling %s logs", ORTP_LOG_DOMAIN, (enabled ? "all" : "application only"));
        linphone_core_set_log_level(level);
        ortp_set_log_level("ios", level == ORTP_DEBUG ? ORTP_DEBUG : ORTP_MESSAGE);
    }
}
- (void)acceptCallForCallId:(NSString *)callid {
    // first, make sure this callid is not already involved in a call
    const bctbx_list_t *calls = linphone_core_get_calls(theLinphoneCore);
    bctbx_list_t *call = bctbx_list_find_custom(calls, (bctbx_compare_func)comp_call_id, [callid UTF8String]);
    if (call != NULL) {
        const LinphoneVideoPolicy *video_policy = linphone_core_get_video_policy(theLinphoneCore);
        bool with_video = video_policy->automatically_accept;
        [self acceptCall:(LinphoneCall *)call->data evenWithVideo:with_video];
        return;
    };
}
- (void)addPushCallId:(NSString *)callid {
    // first, make sure this callid is not already involved in a call
    const bctbx_list_t *calls = linphone_core_get_calls(theLinphoneCore);
    if (bctbx_list_find_custom(calls, (bctbx_compare_func)comp_call_id, [callid UTF8String])) {
        NSLog(@"Call id [%@] already handled", callid);
        return;
    };
    if ([pushCallIDs count] > 10 /*max number of pending notif*/)
        [pushCallIDs removeObjectAtIndex:0];
    
    [pushCallIDs addObject:callid];
}
 
- (BOOL)popPushCallID:(NSString *)callId {
    for (NSString *pendingNotif in pushCallIDs) {
        if ([pendingNotif compare:callId] == NSOrderedSame) {
            [pushCallIDs removeObject:pendingNotif];
            return TRUE;
        }
    }
    return FALSE;
}
 
- (BOOL)resignActive {
    linphone_core_stop_dtmf_stream(theLinphoneCore);
    
    return YES;
}
#pragma mark - Call Functions
- (void)acceptCall:(LinphoneCall *)call evenWithVideo:(BOOL)video {
    LinphoneCallParams *lcallParams = linphone_core_create_call_params(theLinphoneCore, call);
    if (!lcallParams) {
        NSLog(@"Could not create call parameters for %p, call has probably already ended.", call);
        return;
    }
    
    if ([self lpConfigBoolForKey:@"edge_opt_preference"]) {
        bool low_bandwidth = self.network == network_2g;
        if (low_bandwidth) {
            NSLog(@"Low bandwidth mode");
        }
        linphone_call_params_enable_low_bandwidth(lcallParams, low_bandwidth);
    }
    linphone_call_params_enable_video(lcallParams, video);
    
    linphone_call_accept_with_params(call, lcallParams);
}
 
- (void)call:(const LinphoneAddress *)iaddr {
        
    // First verify that network is available, abort otherwise.
    if (!linphone_core_is_network_reachable(theLinphoneCore)) {
        NSLog(@"Network Error", nil);
        return;
    }
    
    // Then check that no GSM calls are in progress, abort otherwise.
    CTCallCenter *callCenter = [[CTCallCenter alloc] init];
    if ([callCenter currentCalls] != nil && floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
        
        NSLog(@"GSM call in progress, cancelling outgoing SIP call request");
      
        return;
    }
    
    // Then check that the supplied address is valid
    if (!iaddr) {
       
        NSLog(@"Invalid SIP address");
 
        return;
    }
    
    if (linphone_core_get_calls_nb(theLinphoneCore) < 1 &&
        floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max ){
        
        /*
        self.providerDelegate.callKitCalls++;
        NSUUID *uuid = [NSUUID UUID];
        [LinphoneManager.instance.providerDelegate.uuids setObject:uuid forKey:@""];
        LinphoneManager.instance.providerDelegate.pendingAddr = linphone_address_clone(iaddr);
        NSString *address = [FastAddressBook displayNameForAddress:iaddr];
        CXHandle *handle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:address];
        CXStartCallAction *act = [[CXStartCallAction alloc] initWithCallUUID:uuid handle:handle];
        CXTransaction *tr = [[CXTransaction alloc] initWithAction:act];
        [LinphoneManager.instance.providerDelegate.controller requestTransaction:tr
                                                                      completion:^(NSError *err){
                                                                      }];
         */
        [self doCall:iaddr];
    } else {
        [self doCall:iaddr];
    }
}
 
- (BOOL)doCall:(const LinphoneAddress *)iaddr {
    
    LinphoneAddress *addr = linphone_address_clone(iaddr);
//    NSString *displayName = [FastAddressBook displayNameForAddress:addr];
    
    // Finally we can make the call
    LinphoneCallParams *lcallParams = linphone_core_create_call_params(theLinphoneCore, NULL);
    if ([self lpConfigBoolForKey:@"edge_opt_preference"] && (self.network == network_2g)) {
        NSLog(@"Enabling low bandwidth mode");
        linphone_call_params_enable_low_bandwidth(lcallParams, YES);
    }
    
//    if (displayName != nil) {
//        linphone_address_set_display_name(addr, displayName.UTF8String);
//    }
    if ([LinphoneManager.instance lpConfigBoolForKey:@"override_domain_with_default_one"]) {
        linphone_address_set_domain(
                                    addr, [[LinphoneManager.instance lpConfigStringForKey:@"domain" inSection:@"assistant"] UTF8String]);
    }
    
    LinphoneCall *call;
    if (LinphoneManager.instance.nextCallIsTransfer) {
        char *caddr = linphone_address_as_string(addr);
        call = linphone_core_get_current_call(theLinphoneCore);
        linphone_call_transfer(call, caddr);
        LinphoneManager.instance.nextCallIsTransfer = NO;
        ms_free(caddr);
    } else {
        call = linphone_core_invite_address_with_params(theLinphoneCore, addr, lcallParams);
        if (call) {
            
            // The LinphoneCallAppData object should be set on call creation with callback
            // - (void)onCall:StateChanged:withMessage:. If not, we are in big trouble and expect it to crash
            // We are NOT responsible for creating the AppData.
            LinphoneCallAppData *data = (__bridge LinphoneCallAppData *)linphone_call_get_user_data(call);
            
            if (data == nil) {
                NSLog(@"New call instanciated but app data was not set. Expect it to crash.");
                /* will be used later to notify user if video was not activated because of the linphone core*/
            } else {
                data->videoRequested = linphone_call_params_video_enabled(lcallParams);
//                linphone_core_invite(LC, linphone_address_as_string(iaddr));
                
            }
        }
    }
    linphone_address_unref(addr);
    linphone_call_params_unref(lcallParams);
    
    return TRUE;
}
 
#pragma mark - Property Functions
 
- (void)setPushNotificationToken:(NSData *)apushNotificationToken {
    if (apushNotificationToken == _pushNotificationToken) {
        return;
    }
    _pushNotificationToken = apushNotificationToken;
    
    @try {
        const MSList *proxies = linphone_core_get_proxy_config_list(LC);
        while (proxies) {
            [self configurePushTokenForProxyConfig:proxies->data];
            proxies = proxies->next;
        }
    } @catch (NSException* e) {
        NSLog(@"%s: linphone core not ready yet, ignoring push token", __FUNCTION__);
    }
}
 
- (void)configurePushTokenForProxyConfig:(LinphoneProxyConfig *)proxyCfg {
    linphone_proxy_config_edit(proxyCfg);
    
    NSData *tokenData = _pushNotificationToken;
    const char *refkey = linphone_proxy_config_get_ref_key(proxyCfg);
    BOOL pushNotifEnabled = (refkey && strcmp(refkey, "push_notification") == 0);
    if (tokenData != nil && pushNotifEnabled) {
        const unsigned char *tokenBuffer = [tokenData bytes];
        NSMutableString *tokenString = [NSMutableString stringWithCapacity:[tokenData length] * 2];
        for (int i = 0; i < [tokenData length]; ++i) {
            [tokenString appendFormat:@"%02X", (unsigned int)tokenBuffer[i]];
        }
        // NSLocalizedString(@"IC_MSG", nil); // Fake for genstrings
        // NSLocalizedString(@"IM_MSG", nil); // Fake for genstrings
        // NSLocalizedString(@"IM_FULLMSG", nil); // Fake for genstrings
#ifdef DEBUG
#define APPMODE_SUFFIX @"dev"
#else
#define APPMODE_SUFFIX @"prod"
#endif
        NSString *ring =
        ([LinphoneManager bundleFile:[self lpConfigStringForKey:@"local_ring" inSection:@"sound"].lastPathComponent]
         ?: [LinphoneManager bundleFile:@"notes_of_the_optimistic.caf"])
        .lastPathComponent;
        NSString * notif_type;
        if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0) {
            //IOS 8 and more
            notif_type = @".voip";
        } else {
            // IOS 7 and below
            notif_type = @"";
        }
        NSString *timeout;
        if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) {
            timeout = @";pn-timeout=0";
        } else {
            timeout = @"";
        }
        
        NSString *silent;
        if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_8_0) {
            silent = @";pn-silent=1";
        } else {
            silent = @"";
        }
        
        NSString *params = [NSString
                            stringWithFormat:@"app-id=%@%@.%@;pn-type=apple;pn-tok=%@;pn-msg-str=IM_MSG;pn-call-str=IC_MSG;pn-"
                            @"call-snd=%@;pn-msg-snd=msg.caf%@%@",
                            [[NSBundle mainBundle] bundleIdentifier], notif_type, APPMODE_SUFFIX, tokenString, ring, timeout, silent];
        
//        NSLog(@"Proxy config %s configured for push notifications with contact: %@",
//             linphone_proxy_config_get_identity_address(proxyCfg), params);
        linphone_proxy_config_set_contact_uri_parameters(proxyCfg, [params UTF8String]);
        linphone_proxy_config_set_contact_parameters(proxyCfg, NULL);
    } else {
//        NSLog(@"Proxy config %s NOT configured for push notifications", linphone_proxy_config_get_identity_address(proxyCfg));
        // no push token:
        linphone_proxy_config_set_contact_uri_parameters(proxyCfg, NULL);
        linphone_proxy_config_set_contact_parameters(proxyCfg, NULL);
    }
    
    linphone_proxy_config_done(proxyCfg);
}
 
#pragma mark - Misc Functions
 
+ (NSString *)bundleFile:(NSString *)file {
    return [[NSBundle mainBundle] pathForResource:[file stringByDeletingPathExtension] ofType:[file pathExtension]];
}
 
+ (NSString *)documentFile:(NSString *)file {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    return [documentsPath stringByAppendingPathComponent:file];
}
 
+ (NSString *)cacheDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    BOOL isDir = NO;
    NSError *error;
    // cache directory must be created if not existing
    if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
        [[NSFileManager defaultManager] createDirectoryAtPath:cachePath
                                  withIntermediateDirectories:NO
                                                   attributes:nil
                                                        error:&error];
    }
    return cachePath;
}
 
+ (int)unreadMessageCount {
    int count = 0;
    const MSList *rooms = linphone_core_get_chat_rooms(LC);
    const MSList *item = rooms;
    while (item) {
        LinphoneChatRoom *room = (LinphoneChatRoom *)item->data;
        if (room) {
            count += linphone_chat_room_get_unread_messages_count(room);
        }
        item = item->next;
    }
    
    return count;
}
 
+ (BOOL)copyFile:(NSString *)src destination:(NSString *)dst override:(BOOL)override {
    NSFileManager *fileManager = NSFileManager.defaultManager;
    NSError *error = nil;
    if ([fileManager fileExistsAtPath:src] == NO) {
        NSLog(@"Can't find \"%@\": %@", src, [error localizedDescription]);
        return FALSE;
    }
    if ([fileManager fileExistsAtPath:dst] == YES) {
        if (override) {
            [fileManager removeItemAtPath:dst error:&error];
            if (error != nil) {
                NSLog(@"Can't remove \"%@\": %@", dst, [error localizedDescription]);
                return FALSE;
            }
        } else {
            NSLog(@"\"%@\" already exists", dst);
            return FALSE;
        }
    }
    [fileManager copyItemAtPath:src toPath:dst error:&error];
    if (error != nil) {
        NSLog(@"Can't copy \"%@\" to \"%@\": %@", src, dst, [error localizedDescription]);
        return FALSE;
    }
    return TRUE;
}
 
- (void)configureVbrCodecs {
    PayloadType *pt;
    int bitrate = lp_config_get_int(
                                    _configDb, "audio", "codec_bitrate_limit",
                                    kLinphoneAudioVbrCodecDefaultBitrate); /*default value is in linphonerc or linphonerc-factory*/
    const MSList *audio_codecs = linphone_core_get_audio_codecs(theLinphoneCore);
    const MSList *codec = audio_codecs;
    while (codec) {
        pt = codec->data;
        if (linphone_payload_type_is_vbr(pt)) {
            linphone_core_set_payload_type_bitrate(theLinphoneCore, pt, bitrate);
        }
        codec = codec->next;
    }
}
 
 
+ (id)getMessageAppDataForKey:(NSString *)key inMessage:(LinphoneChatMessage *)msg {
    
    if (msg == nil)
        return nil;
    
    id value = nil;
    const char *appData = linphone_chat_message_get_appdata(msg);
    if (appData) {
        NSDictionary *appDataDict =
        [NSJSONSerialization JSONObjectWithData:[NSData dataWithBytes:appData length:strlen(appData)]
                                        options:0
                                          error:nil];
        value = [appDataDict objectForKey:key];
    }
    return value;
}
 
+ (void)setValueInMessageAppData:(id)value forKey:(NSString *)key inMessage:(LinphoneChatMessage *)msg {
    
    NSMutableDictionary *appDataDict = [NSMutableDictionary dictionary];
    const char *appData = linphone_chat_message_get_appdata(msg);
    if (appData) {
        appDataDict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithBytes:appData length:strlen(appData)]
                                                      options:NSJSONReadingMutableContainers
                                                        error:nil];
    }
    
    [appDataDict setValue:value forKey:key];
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:appDataDict options:0 error:nil];
    NSString *appdataJSON = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    linphone_chat_message_set_appdata(msg, [appdataJSON UTF8String]);
}
 
#pragma mark - LPConfig Functions
 
- (void)lpConfigSetString:(NSString *)value forKey:(NSString *)key {
    [self lpConfigSetString:value forKey:key inSection:LINPHONERC_APPLICATION_KEY];
}
- (void)lpConfigSetString:(NSString *)value forKey:(NSString *)key inSection:(NSString *)section {
    if (!key)
        return;
    lp_config_set_string(_configDb, [section UTF8String], [key UTF8String], value ? [value UTF8String] : NULL);
}
- (NSString *)lpConfigStringForKey:(NSString *)key {
    return [self lpConfigStringForKey:key withDefault:nil];
}
- (NSString *)lpConfigStringForKey:(NSString *)key withDefault:(NSString *)defaultValue {
    return [self lpConfigStringForKey:key inSection:LINPHONERC_APPLICATION_KEY withDefault:defaultValue];
}
- (NSString *)lpConfigStringForKey:(NSString *)key inSection:(NSString *)section {
    return [self lpConfigStringForKey:key inSection:section withDefault:nil];
}
- (NSString *)lpConfigStringForKey:(NSString *)key inSection:(NSString *)section withDefault:(NSString *)defaultValue {
    if (!key)
        return defaultValue;
    const char *value = lp_config_get_string(_configDb, [section UTF8String], [key UTF8String], NULL);
    return value ? [NSString stringWithUTF8String:value] : defaultValue;
}
 
- (void)lpConfigSetInt:(int)value forKey:(NSString *)key {
    [self lpConfigSetInt:value forKey:key inSection:LINPHONERC_APPLICATION_KEY];
}
- (void)lpConfigSetInt:(int)value forKey:(NSString *)key inSection:(NSString *)section {
    if (!key)
        return;
    lp_config_set_int(_configDb, [section UTF8String], [key UTF8String], (int)value);
}
- (int)lpConfigIntForKey:(NSString *)key {
    return [self lpConfigIntForKey:key withDefault:-1];
}
- (int)lpConfigIntForKey:(NSString *)key withDefault:(int)defaultValue {
    return [self lpConfigIntForKey:key inSection:LINPHONERC_APPLICATION_KEY withDefault:defaultValue];
}
- (int)lpConfigIntForKey:(NSString *)key inSection:(NSString *)section {
    return [self lpConfigIntForKey:key inSection:section withDefault:-1];
}
- (int)lpConfigIntForKey:(NSString *)key inSection:(NSString *)section withDefault:(int)defaultValue {
    if (!key)
        return defaultValue;
    return lp_config_get_int(_configDb, [section UTF8String], [key UTF8String], (int)defaultValue);
}
 
- (void)lpConfigSetBool:(BOOL)value forKey:(NSString *)key {
    [self lpConfigSetBool:value forKey:key inSection:LINPHONERC_APPLICATION_KEY];
}
- (void)lpConfigSetBool:(BOOL)value forKey:(NSString *)key inSection:(NSString *)section {
    [self lpConfigSetInt:(int)(value == TRUE) forKey:key inSection:section];
}
- (BOOL)lpConfigBoolForKey:(NSString *)key {
    return [self lpConfigBoolForKey:key withDefault:FALSE];
}
- (BOOL)lpConfigBoolForKey:(NSString *)key withDefault:(BOOL)defaultValue {
    return [self lpConfigBoolForKey:key inSection:LINPHONERC_APPLICATION_KEY withDefault:defaultValue];
}
- (BOOL)lpConfigBoolForKey:(NSString *)key inSection:(NSString *)section {
    return [self lpConfigBoolForKey:key inSection:section withDefault:FALSE];
}
- (BOOL)lpConfigBoolForKey:(NSString *)key inSection:(NSString *)section withDefault:(BOOL)defaultValue {
    if (!key)
        return defaultValue;
    int val = [self lpConfigIntForKey:key inSection:section withDefault:-1];
    return (val != -1) ? (val == 1) : defaultValue;
}
 
 
#pragma mark - GSM management
 
- (void)removeCTCallCenterCb {
    if (mCallCenter != nil) {
        NSLog(@"Removing CT call center listener [%p]", mCallCenter);
        mCallCenter.callEventHandler = NULL;
    }
    mCallCenter = nil;
}
 
 
static int comp_call_state_paused(const LinphoneCall *call, const void *param) {
    return linphone_call_get_state(call) != LinphoneCallPaused;
}
- (void)startPushLongRunningTask:(BOOL)msg {
    [[UIApplication sharedApplication] endBackgroundTask:pushBgTask];
    pushBgTask = 0;
    pushBgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
            if (msg) {
                NSLog(@"Incomming message couldn't be received");
                if (@available(iOS 10.0, *)) {
                    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
                    content.title = NSLocalizedString(@"Message received", nil);
                    content.body = NSLocalizedString(@"You have received a message.", nil);
                    content.categoryIdentifier = @"push_msg";
                    
                    UNNotificationRequest *req =
                    [UNNotificationRequest requestWithIdentifier:@"push_msg" content:content trigger:NULL];
                    [[UNUserNotificationCenter currentNotificationCenter]
                     addNotificationRequest:req
                     withCompletionHandler:^(NSError *_Nullable error) {
                         // Enable or disable features based on authorization.
                         if (error) {
                             NSLog(@"Error while adding notification request :");
                             NSLog(@"%@", error.description);
                         }
                     }];
                } else {
                    // Fallback on earlier versions
                }
                
            } else {
                NSLog(@"Incomming call couldn't be received");
                if (@available(iOS 10.0, *)) {
                    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
                    content.title = NSLocalizedString(@"Missed call", nil);
                    content.body = NSLocalizedString(@"You have missed a call.", nil);
                    content.categoryIdentifier = @"push_call";
                    
                    UNNotificationRequest *req =
                    [UNNotificationRequest requestWithIdentifier:@"push_call" content:content trigger:NULL];
                    [[UNUserNotificationCenter currentNotificationCenter]
                     addNotificationRequest:req
                     withCompletionHandler:^(NSError *_Nullable error) {
                         // Enable or disable features based on authorization.
                         if (error) {
                             NSLog(@"Error while adding notification request :");
                             NSLog(@"%@", error.description);
                         }
                     }];
                } else {
                    // Fallback on earlier versions
                }
               
            }
        }
        for (NSString *key in [LinphoneManager.instance.pushDict allKeys]) {
            [LinphoneManager.instance.pushDict setValue:[NSNumber numberWithInt:0] forKey:key];
        }
        [[UIApplication sharedApplication] endBackgroundTask:pushBgTask];
        pushBgTask = 0;
 
    }];    NSLog(@"Long running task started, remaining [%g s] because a push has been received",
         [[UIApplication sharedApplication] backgroundTimeRemaining]);
}
- (void)destroyLinphoneCore {
    [mIterateTimer invalidate];
    // just in case
    [self removeCTCallCenterCb];
    
    if (theLinphoneCore != nil) { // just in case application terminate before linphone core initialization
        
        linphone_core_unref(theLinphoneCore);
        NSLog(@"Destroy linphonecore %p", theLinphoneCore);
        theLinphoneCore = nil;
        
        // Post event
        NSDictionary *dict =
        [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:theLinphoneCore] forKey:@"core"];
        [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneCoreUpdate
                                                          object:LinphoneManager.instance
                                                        userInfo:dict];
        
        SCNetworkReachabilityUnscheduleFromRunLoop(proxyReachability, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
        if (proxyReachability)
            CFRelease(proxyReachability);
        proxyReachability = nil;
    }
    libStarted = FALSE;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
#pragma mark -
 
- (void)removeAllAccounts {
    linphone_core_clear_proxy_config(LC);
    linphone_core_clear_all_auth_info(LC);
}
 
+ (BOOL)isMyself:(const LinphoneAddress *)addr {
    if (!addr)
        return NO;
    
    const MSList *it = linphone_core_get_proxy_config_list(LC);
    while (it) {
        if (linphone_address_weak_equal(addr, linphone_proxy_config_get_identity_address(it->data))) {
            return YES;
        }
        it = it->next;
    }
    return NO;
}
//通话被打断
- (void)beginInterruption {
    LinphoneCall *c = linphone_core_get_current_call(theLinphoneCore);
    NSLog(@"Sound interruption detected!");
    if (c && linphone_call_get_state(c) == LinphoneCallStreamsRunning) {
        linphone_call_pause(c);
    }
}
//被截断状态结束
- (void)endInterruption {
    NSLog(@"Sound interruption ended!");
}
- (void)enableProxyPublish:(BOOL)enabled {
    if (linphone_core_get_global_state(LC) != LinphoneGlobalOn || !linphone_core_get_default_friend_list(LC)) {
        NSLog(@"Not changing presence configuration because linphone core not ready yet");
        return;
    }
    
    if ([self lpConfigBoolForKey:@"publish_presence"]) {
        // set present to "tv", because "available" does not work yet
        if (enabled) {
            linphone_core_set_presence_model(
                                             LC, linphone_core_create_presence_model_with_activity(LC, LinphonePresenceActivityTV, NULL));
        }
        
        const MSList *proxies = linphone_core_get_proxy_config_list(LC);
        while (proxies) {
            LinphoneProxyConfig *cfg = proxies->data;
            linphone_proxy_config_edit(cfg);
            linphone_proxy_config_enable_publish(cfg, enabled);
            linphone_proxy_config_done(cfg);
            proxies = proxies->next;
        }
        // force registration update first, then update friend list subscription
        linphone_core_iterate(theLinphoneCore);
    }
    
    linphone_friend_list_enable_subscriptions(linphone_core_get_default_friend_list(LC),
                                              enabled &&
                                              [LinphoneManager.instance lpConfigBoolForKey:@"use_rls_presence"]);
}
 
 
 
- (void)setupGSMInteraction {
    
    [self removeCTCallCenterCb];
    mCallCenter = [[CTCallCenter alloc] init];
    NSLog(@"Adding CT call center listener [%p]", mCallCenter);
    __block __weak LinphoneManager *weakSelf = self;
    __block __weak CTCallCenter *weakCCenter = mCallCenter;
    mCallCenter.callEventHandler = ^(CTCall *call) {
        // post on main thread
        [weakSelf performSelectorOnMainThread:@selector(handleGSMCallInteration:)
                                   withObject:weakCCenter
                                waitUntilDone:YES];
    };
}
 
- (NSString *)contactFilter {
    NSString *filter = @"*";
    if ([self lpConfigBoolForKey:@"contact_filter_on_default_domain"]) {
        LinphoneProxyConfig *proxy_cfg = linphone_core_get_default_proxy_config(theLinphoneCore);
        if (proxy_cfg && linphone_proxy_config_get_addr(proxy_cfg)) {
            return [NSString stringWithCString:linphone_proxy_config_get_domain(proxy_cfg)
                                      encoding:[NSString defaultCStringEncoding]];
        }
    }
    return filter;
}
#pragma mark- 被电话中断了
 
- (void)handleGSMCallInteration:(id)cCenter {
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
        CTCallCenter *ct = (CTCallCenter *)cCenter;
        // pause current call, if any
        LinphoneCall *call = linphone_core_get_current_call(theLinphoneCore);
        if ([ct currentCalls] != nil) {
            if (call) {
                NSLog(@"Pausing SIP call because GSM call");
                linphone_call_pause(call);
                [self startCallPausedLongRunningTask];
            } else if (linphone_core_is_in_conference(theLinphoneCore)) {
                NSLog(@"Leaving conference call because GSM call");
                linphone_core_leave_conference(theLinphoneCore);
                [self startCallPausedLongRunningTask];
            }
        } // else nop, keep call in paused state
    }
}
- (void)startCallPausedLongRunningTask {
    
    pausedCallBgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"Call cannot be paused any more, too late");
        [[UIApplication sharedApplication] endBackgroundTask:pausedCallBgTask];
    }];
    NSLog(@"Long running task started, remaining [%g s] because at least one call is paused",
         [[UIApplication sharedApplication] backgroundTimeRemaining]);
}
#pragma mark - GSM management
- (void)renameDefaultSettings {
    // rename .linphonerc to linphonerc to ease debugging: when downloading
    // containers from MacOSX, Finder do not display hidden files leading
    // to useless painful operations to display the .linphonerc file
    NSString *src = [LinphoneManager documentFile:@".linphonerc"];
    NSString *dst = [LinphoneManager documentFile:@"linphonerc"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *fileError = nil;
    if ([fileManager fileExistsAtPath:src]) {
        if ([fileManager fileExistsAtPath:dst]) {
            [fileManager removeItemAtPath:src error:&fileError];
            NSLog(@"%@ already exists, simply removing %@ %@", dst, src,
                 fileError ? fileError.localizedDescription : @"successfully");
        } else {
            [fileManager moveItemAtPath:src toPath:dst error:&fileError];
            NSLog(@"%@ moving to %@ %@", dst, src, fileError ? fileError.localizedDescription : @"successfully");
        }
    }
}
 
// ugly hack to export symbol from liblinphone so that they are available for the linphoneTests target
// linphoneTests target do not link with liblinphone but instead dynamically link with ourself which is
// statically linked with liblinphone, so we must have exported required symbols from the library to
// have them available in linphoneTests
// DO NOT INVOKE THIS METHOD
 
- (void)exportSymbolsForUITests {
    linphone_address_set_header(NULL, NULL, NULL);
}
 
@end
 
 
 
#pragma mark - -------categary
@implementation NSString (md5)
 
- (BOOL)containsSubstring:(NSString *)str {
    if (UIDevice.currentDevice.systemVersion.doubleValue >= 8.0) {
#pragma deploymate push "ignored-api-availability"
        return [self containsString:str];
#pragma deploymate pop
    }
    return ([self rangeOfString:str].location != NSNotFound);
}
@end