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


/*****************************************************************************
 *
 *  Filename:      btif_av.c
 *
 *  Description:   Bluedroid AV implementation
 *
 *****************************************************************************/

#include <assert.h>
#include <string.h>

#include <hardware/bluetooth.h>
#include <system/audio.h>
#include "hardware/bt_av.h"
#include "osi/include/allocator.h"

#define LOG_TAG "bt_btif_av"

#include "btif_av.h"
#include "btif_util.h"
#include "btif_profile_queue.h"
#include "bta_api.h"
#include "btif_media.h"
#include "bta_av_api.h"
#include "gki.h"
#include "btu.h"
#include "bt_utils.h"

/*****************************************************************************
**  Constants & Macros
******************************************************************************/
#define BTIF_AV_SERVICE_NAME "Advanced Audio"
#define BTIF_AVK_SERVICE_NAME "Advanced Audio Sink"

#define BTIF_TIMEOUT_AV_OPEN_ON_RC_SECS  2

/* Number of BTIF-AV control blocks */
/* Now supports Two AV connections. */
#define BTIF_AV_NUM_CB       2
#define HANDLE_TO_INDEX(x) ((x & BTA_AV_HNDL_MSK) - 1)

typedef enum {
    BTIF_AV_STATE_IDLE = 0x0,
    BTIF_AV_STATE_OPENING,
    BTIF_AV_STATE_OPENED,
    BTIF_AV_STATE_STARTED,
    BTIF_AV_STATE_CLOSING
} btif_av_state_t;

/* Should not need dedicated suspend state as actual actions are no
   different than open state. Suspend flags are needed however to prevent
   media task from trying to restart stream during remote suspend or while
   we are in the process of a local suspend */

#define BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING 0x1
#define BTIF_AV_FLAG_REMOTE_SUSPEND        0x2
#define BTIF_AV_FLAG_PENDING_START         0x4
#define BTIF_AV_FLAG_PENDING_STOP          0x8
/* Host role defenitions */
#define HOST_ROLE_MASTER                   0x00
#define HOST_ROLE_SLAVE                    0x01
#define HOST_ROLE_UNKNOWN                  0xff

/*****************************************************************************
**  Local type definitions
******************************************************************************/

typedef struct
{
    tBTA_AV_HNDL bta_handle;
    bt_bdaddr_t peer_bda;
    btif_sm_handle_t sm_handle;
    UINT8 flags;
    tBTA_AV_EDR edr;
    UINT8   peer_sep;  /* sep type of peer device */
    UINT8 edr_3mbps;
    BOOLEAN dual_handoff;
    BOOLEAN current_playing;
    btif_sm_state_t state;
    int service;
    BOOLEAN is_slave;
    BOOLEAN is_device_playing;
} btif_av_cb_t;

typedef struct
{
    bt_bdaddr_t *target_bda;
    uint16_t uuid;
} btif_av_connect_req_t;

typedef struct
{
    int sample_rate;
    int channel_count;
    UINT8 peer_bd[6];
} btif_av_sink_config_req_t;

/*****************************************************************************
**  Static variables
******************************************************************************/
static btav_callbacks_t *bt_av_src_callbacks = NULL;
static btav_callbacks_t *bt_av_sink_callbacks = NULL;
static btif_av_cb_t btif_av_cb[BTIF_AV_NUM_CB];
static TIMER_LIST_ENT tle_av_open_on_rc;
static btif_sm_event_t idle_rc_event;
static tBTA_AV idle_rc_data;
static int btif_max_av_clients = 1;
static BOOLEAN enable_multicast = FALSE;
static BOOLEAN is_multicast_supported = FALSE;
static BOOLEAN multicast_disabled = FALSE;

/* both interface and media task needs to be ready to alloc incoming request */
#define CHECK_BTAV_INIT() if (((bt_av_src_callbacks == NULL) &&(bt_av_sink_callbacks == NULL)) \
        || (btif_av_cb[0].sm_handle == NULL))\
{\
     BTIF_TRACE_WARNING("%s: BTAV not initialized", __FUNCTION__);\
     return BT_STATUS_NOT_READY;\
}\
else\
{\
     BTIF_TRACE_EVENT("%s", __FUNCTION__);\
}

/* Helper macro to avoid code duplication in the state machine handlers */
#define CHECK_RC_EVENT(e, d) \
    case BTA_AV_RC_CLOSE_EVT: \
    case BTA_AV_REMOTE_CMD_EVT: \
    case BTA_AV_VENDOR_CMD_EVT: \
    case BTA_AV_META_MSG_EVT: \
    case BTA_AV_BROWSE_MSG_EVT: \
    case BTA_AV_RC_FEAT_EVT: \
    case BTA_AV_REMOTE_RSP_EVT: \
    { \
         btif_rc_handler(e, d);\
    }break; \


static BOOLEAN btif_av_state_idle_handler(btif_sm_event_t event, void *data, int index);
static BOOLEAN btif_av_state_opening_handler(btif_sm_event_t event, void *data, int index);
static BOOLEAN btif_av_state_opened_handler(btif_sm_event_t event, void *data, int index);
static BOOLEAN btif_av_state_started_handler(btif_sm_event_t event, void *data,int index);

static BOOLEAN btif_av_state_closing_handler(btif_sm_event_t event, void *data,int index);

static BOOLEAN btif_av_get_valid_idx(int idx);
static UINT8 btif_av_idx_by_bdaddr( BD_ADDR bd_addr);
static int btif_get_latest_playing_device_idx();
static int btif_get_latest_device_idx_to_start();
static int btif_av_get_valid_idx_for_rc_events(BD_ADDR bd_addr, int rc_handle);
static int btif_get_conn_state_of_device(BD_ADDR address);
static bt_status_t connect_int(bt_bdaddr_t *bd_addr, uint16_t uuid);
static void btif_av_update_current_playing_device(int index);
static void btif_av_check_rc_connection_priority(void *p_data);
#ifdef AVK_BACKPORT
void btif_av_request_audio_focus( BOOLEAN enable);
#endif
static const btif_sm_handler_t btif_av_state_handlers[] =
{
    btif_av_state_idle_handler,
    btif_av_state_opening_handler,
    btif_av_state_opened_handler,
    btif_av_state_started_handler,
    btif_av_state_closing_handler
};

static void btif_av_event_free_data(btif_sm_event_t event, void *p_data);

/*************************************************************************
** Extern functions
*************************************************************************/
extern void btif_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data);
extern BOOLEAN btif_rc_get_connected_peer(BD_ADDR peer_addr);
extern UINT8 btif_rc_get_connected_peer_handle(BD_ADDR peer_addr);
extern void btif_rc_check_handle_pending_play (BD_ADDR peer_addr, BOOLEAN bSendToApp);
extern void btif_rc_get_playing_device(BD_ADDR address);
extern void btif_rc_clear_playing_state(BOOLEAN play);
extern void btif_rc_clear_priority(BD_ADDR address);
extern void btif_rc_send_pause_command();
extern UINT16 btif_dm_get_br_edr_links();
extern UINT16 btif_dm_get_le_links();

/*****************************************************************************
** Local helper functions
******************************************************************************/
void btif_av_trigger_dual_handoff(BOOLEAN handoff, BD_ADDR address);
BOOLEAN btif_av_is_device_connected(BD_ADDR address);

BOOLEAN btif_av_is_connected_on_other_idx(int current_index);
BOOLEAN btif_av_is_playing_on_other_idx(int current_index);
BOOLEAN btif_av_is_playing();
void btif_av_update_multicast_state(int index);
BOOLEAN btif_av_get_ongoing_multicast();

const char *dump_av_sm_state_name(btif_av_state_t state)
{
    switch (state)
    {
        CASE_RETURN_STR(BTIF_AV_STATE_IDLE)
        CASE_RETURN_STR(BTIF_AV_STATE_OPENING)
        CASE_RETURN_STR(BTIF_AV_STATE_OPENED)
        CASE_RETURN_STR(BTIF_AV_STATE_STARTED)
        CASE_RETURN_STR(BTIF_AV_STATE_CLOSING)
        default: return "UNKNOWN_STATE";
    }
}

const char *dump_av_sm_event_name(btif_av_sm_event_t event)
{
    switch((int)event)
    {
        CASE_RETURN_STR(BTA_AV_ENABLE_EVT)
        CASE_RETURN_STR(BTA_AV_REGISTER_EVT)
        CASE_RETURN_STR(BTA_AV_OPEN_EVT)
        CASE_RETURN_STR(BTA_AV_CLOSE_EVT)
        CASE_RETURN_STR(BTA_AV_START_EVT)
        CASE_RETURN_STR(BTA_AV_STOP_EVT)
        CASE_RETURN_STR(BTA_AV_PROTECT_REQ_EVT)
        CASE_RETURN_STR(BTA_AV_PROTECT_RSP_EVT)
        CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
        CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
        CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
        CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
        CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
        CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
        CASE_RETURN_STR(BTA_AV_RECONFIG_EVT)
        CASE_RETURN_STR(BTA_AV_SUSPEND_EVT)
        CASE_RETURN_STR(BTA_AV_PENDING_EVT)
        CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
        CASE_RETURN_STR(BTA_AV_REJECT_EVT)
        CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
        CASE_RETURN_STR(BTIF_SM_ENTER_EVT)
        CASE_RETURN_STR(BTIF_SM_EXIT_EVT)
        CASE_RETURN_STR(BTIF_AV_CONNECT_REQ_EVT)
        CASE_RETURN_STR(BTIF_AV_DISCONNECT_REQ_EVT)
        CASE_RETURN_STR(BTIF_AV_START_STREAM_REQ_EVT)
        CASE_RETURN_STR(BTIF_AV_STOP_STREAM_REQ_EVT)
        CASE_RETURN_STR(BTIF_AV_SUSPEND_STREAM_REQ_EVT)
        CASE_RETURN_STR(BTIF_AV_SINK_CONFIG_REQ_EVT)
        default: return "UNKNOWN_EVENT";
   }
}
//TODO.. We will remove this data structure
static BD_ADDR bd_null= {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

/****************************************************************************
**  Local helper functions
*****************************************************************************/
/*******************************************************************************
**
** Function         btif_initiate_av_open_tmr_hdlr
**
** Description      Timer to trigger AV open if the remote headset establishes
**                  RC connection w/o AV connection. The timer is needed to IOP
**                  with headsets that do establish AV after RC connection.
**
** Returns          void
**
*******************************************************************************/
static void btif_initiate_av_open_tmr_hdlr(TIMER_LIST_ENT *tle)
{
    BD_ADDR peer_addr;
    UNUSED(tle);
    btif_av_connect_req_t connect_req;
    int index = 0;
    int i;
    // Need to get the proper index to initiate AV connection
    UNUSED(tle);
    /* is there at least one RC connection - There should be */
    /*We have Two Connections.*/
    if (btif_rc_get_connected_peer(peer_addr))
    {
        /*Check if this peer_addr is same as currently connected AV*/
        if (btif_get_conn_state_of_device(peer_addr) == BTIF_AV_STATE_OPENED)
        {
            BTIF_TRACE_DEBUG("AV is already connected");
        }
        else
        {
            UINT8 rc_handle;
            int index;
            /* Multicast: Check if AV slot is available for connection
             * If not available, AV got connected to different devices.
             * Disconnect this RC connection without AV connection.
             */
            rc_handle = btif_rc_get_connected_peer_handle(peer_addr);
            index = btif_av_get_valid_idx_for_rc_events(peer_addr, rc_handle);
            if(index >= btif_max_av_clients)
            {
                BTIF_TRACE_ERROR("%s No slot free for AV connection, back off",
                            __FUNCTION__);
                return;
            }
            BTIF_TRACE_DEBUG("%s Issuing connect to the remote RC peer", __FUNCTION__);
            if(bt_av_sink_callbacks != NULL)
                btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, (bt_bdaddr_t*)&peer_addr,
                        connect_int);
            if(bt_av_src_callbacks != NULL)
                btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, (bt_bdaddr_t*)&peer_addr,
                        connect_int);
        }
    }
    else
    {
        BTIF_TRACE_ERROR("%s No connected RC peers", __FUNCTION__);
    }

}


/*****************************************************************************
**  Static functions
******************************************************************************/

static void btif_report_connection_state(btav_connection_state_t state, bt_bdaddr_t *bd_addr)
{
    if (bt_av_sink_callbacks != NULL) {
        HAL_CBACK(bt_av_sink_callbacks, connection_state_cb, state, bd_addr);
    } else if ( bt_av_src_callbacks != NULL) {
        HAL_CBACK(bt_av_src_callbacks, connection_state_cb, state, bd_addr);
    }
}

static void btif_report_audio_state(btav_audio_state_t state, bt_bdaddr_t *bd_addr)
{
    if (bt_av_sink_callbacks != NULL) {
        HAL_CBACK(bt_av_sink_callbacks, audio_state_cb, state, bd_addr);
    } else if (bt_av_src_callbacks != NULL) {
        HAL_CBACK(bt_av_src_callbacks, audio_state_cb, state, bd_addr);
    }
}

/*****************************************************************************
**
** Function     btif_av_state_idle_handler
**
** Description  State managing disconnected AV link
**
** Returns      TRUE if event was processed, FALSE otherwise
**
*******************************************************************************/

static BOOLEAN btif_av_state_idle_handler(btif_sm_event_t event, void *p_data, int index)
{
    BTIF_TRACE_IMP("%s event:%s flags %x on Index = %d", __FUNCTION__,
                     dump_av_sm_event_name(event), btif_av_cb[index].flags, index);

    switch (event)
    {
        case BTIF_SM_ENTER_EVT:
            /* clear the peer_bda */
            BTIF_TRACE_EVENT("IDLE state for index: %d", index);
            memset(&btif_av_cb[index].peer_bda, 0, sizeof(bt_bdaddr_t));
            btif_av_cb[index].flags = 0;
            btif_av_cb[index].edr_3mbps = 0;
            btif_av_cb[index].edr = 0;
            btif_av_cb[index].current_playing = FALSE;
            btif_av_cb[index].is_slave = FALSE;
            btif_av_cb[index].is_device_playing = FALSE;
            for (int i = 0; i < btif_max_av_clients; i++)
            {
                btif_av_cb[i].dual_handoff = FALSE;
            }
            /* This API will be called twice at initialization
            ** Idle can be moved when device is disconnected too.
            ** Take care of other connected device here.*/
            if (!btif_av_is_connected())
            {
                BTIF_TRACE_EVENT("reset A2dp states in IDLE ");
                btif_a2dp_on_idle();
            }
            else
            {
                //There is another AV connection, update current playin
                BTIF_TRACE_EVENT("reset A2dp states in IDLE ");
                btif_av_update_current_playing_device(index);
            }
            break;

        case BTIF_SM_EXIT_EVT:
            break;

        case BTA_AV_ENABLE_EVT:
            BTIF_TRACE_EVENT("AV is enabled now for index: %d", index);
            break;

        case BTA_AV_REGISTER_EVT:
            BTIF_TRACE_EVENT("The AV Handle:%d", ((tBTA_AV*)p_data)->registr.hndl);
            btif_av_cb[index].bta_handle = ((tBTA_AV*)p_data)->registr.hndl;
            break;

        case BTIF_AV_CONNECT_REQ_EVT:
            /* For outgoing connect stack and app are in sync.
            */
            memcpy(&btif_av_cb[index].peer_bda, ((btif_av_connect_req_t*)p_data)->target_bda,
                                                                        sizeof(bt_bdaddr_t));
            BTA_AvOpen(btif_av_cb[index].peer_bda.address, btif_av_cb[index].bta_handle,
                        TRUE, BTA_SEC_NONE, ((btif_av_connect_req_t*)p_data)->uuid);
            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENING);
            break;

        case BTA_AV_PENDING_EVT:
        case BTA_AV_RC_OPEN_EVT:
            /* IOP_FIX: Jabra 620 only does RC open without AV open whenever it connects. So
             * as per the AV WP, an AVRC connection cannot exist without an AV connection. Therefore,
             * we initiate an AV connection if an RC_OPEN_EVT is received when we are in AV_CLOSED state.
             * We initiate the AV connection after a small 3s timeout to avoid any collisions from the
             * headsets, as some headsets initiate the AVRC connection first and then
             * immediately initiate the AV connection
             *
             * TODO: We may need to do this only on an AVRCP Play. FixMe
             */
            /* Check if connection allowed with this device */
            /* In Dual A2dp case, this event can come for both the headsets.
             * Reject second connection request as we are already checking
             * for device priority for first device and we cannot queue
             * incoming connections requests.
             */

            if (idle_rc_event != 0)
            {
                BTIF_TRACE_DEBUG("Processing another RC Event ");
                return FALSE;
            }
            memcpy(&idle_rc_data, ((tBTA_AV*)p_data), sizeof(tBTA_AV));
            if (event == BTA_AV_RC_OPEN_EVT)
            {
                if (((tBTA_AV*)p_data)->rc_open.status == BTA_AV_SUCCESS)
                {
                    bdcpy(btif_av_cb[index].peer_bda.address,
                        ((tBTA_AV*)p_data)->rc_open.peer_addr);
                }
                else
                {
                    idle_rc_event = 0;
                    return TRUE;
                }
            }
            else
            {
                bdcpy(btif_av_cb[index].peer_bda.address, ((tBTA_AV*)p_data)->pend.bd_addr);
            }

            // Only for AVDTP connection request move to opening state
            if (event == BTA_AV_PENDING_EVT)
                btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENING);

            if (bt_av_src_callbacks != NULL)
            {
                BTIF_TRACE_DEBUG("Calling connection priority callback ");
                idle_rc_event = event;
                HAL_CBACK(bt_av_src_callbacks, connection_priority_cb,
                         &(btif_av_cb[index].peer_bda));
            }
            if (bt_av_sink_callbacks != NULL)
            {
                if(event == BTA_AV_PENDING_EVT)
                {
                    BTA_AvOpen(btif_av_cb[index].peer_bda.address, btif_av_cb[index].bta_handle,
                       TRUE, BTA_SEC_NONE, UUID_SERVCLASS_AUDIO_SINK);
                }
                else if(event == BTA_AV_RC_OPEN_EVT)
                {
                    memset(&tle_av_open_on_rc, 0, sizeof(tle_av_open_on_rc));
                    tle_av_open_on_rc.param = (UINT32)btif_initiate_av_open_tmr_hdlr;
                    btu_start_timer(&tle_av_open_on_rc, BTU_TTYPE_USER_FUNC,
                            BTIF_TIMEOUT_AV_OPEN_ON_RC_SECS);
                    btif_rc_handler(event, p_data);
                }
            }
            break;

        case BTIF_AV_SINK_CONFIG_REQ_EVT:
        {
            btif_av_sink_config_req_t req;
            // copy to avoid alignment problems
            memcpy(&req, p_data, sizeof(req));

            BTIF_TRACE_WARNING("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate,
                    req.channel_count);
            if (bt_av_sink_callbacks != NULL) {
                HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(req.peer_bd),
                        req.sample_rate, req.channel_count);
            }
        } break;

        case BTA_AV_OPEN_EVT:
        {
            /* We get this event in Idle State if Signaling
             * channel is not closed, only Streaming channel was
             * closed earlier, and now only stream setup process is
             * initiated.
             */
            tBTA_AV *p_bta_data = (tBTA_AV*)p_data;
            btav_connection_state_t state;
            BTIF_TRACE_DEBUG("status:%d, edr 0x%x",p_bta_data->open.status,
                               p_bta_data->open.edr);

            if (p_bta_data->open.status == BTA_AV_SUCCESS)
            {
                 state = BTAV_CONNECTION_STATE_CONNECTED;
                 btif_av_cb[index].edr = p_bta_data->open.edr;
                 if (p_bta_data->open.role == HOST_ROLE_SLAVE)
                 {
                    btif_av_cb[index].is_slave = TRUE;
                 }
                 btif_av_cb[index].peer_sep = p_bta_data->open.sep;
                 btif_a2dp_set_peer_sep(p_bta_data->open.sep);

                 if (p_bta_data->open.edr & BTA_AV_EDR_3MBPS)
                 {
                     BTIF_TRACE_DEBUG("remote supports 3 mbps");
                     btif_av_cb[index].edr_3mbps = TRUE;
                 }

                 bdcpy(btif_av_cb[index].peer_bda.address, ((tBTA_AV*)p_data)->open.bd_addr);
            }
            else
            {
                BTIF_TRACE_WARNING("BTA_AV_OPEN_EVT::FAILED status: %d",
                                     p_bta_data->open.status );
                state = BTAV_CONNECTION_STATE_DISCONNECTED;
            }

            /* change state to open based on the status */
            if (p_bta_data->open.status == BTA_AV_SUCCESS)
            {
                /* inform the application of the event */
                btif_report_connection_state(state, &(btif_av_cb[index].peer_bda));
                btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENED);
                /* BTIF AV State updated, now check
                 * and update multicast state
                 */
                btif_av_update_multicast_state(index);
            }

            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
            {
                /* if queued PLAY command,  send it now */
                btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr,
                                             (p_bta_data->open.status == BTA_AV_SUCCESS));
            }
            else if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
            {
                /* if queued PLAY command,  send it now */
                btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr, FALSE);
                /* Bring up AVRCP connection too */
                BTA_AvOpenRc(btif_av_cb[index].bta_handle);
            }
            btif_queue_advance();
        } break;

        case BTA_AV_REMOTE_CMD_EVT:
        case BTA_AV_VENDOR_CMD_EVT:
        case BTA_AV_META_MSG_EVT:
        case BTA_AV_RC_FEAT_EVT:
        case BTA_AV_REMOTE_RSP_EVT:
        case BTA_AV_BROWSE_MSG_EVT:
            btif_rc_handler(event, (tBTA_AV*)p_data);
            break;

        case BTA_AV_RC_CLOSE_EVT:
            if (tle_av_open_on_rc.in_use) {
                BTIF_TRACE_DEBUG("BTA_AV_RC_CLOSE_EVT: Stopping AV timer.");
                btu_stop_timer(&tle_av_open_on_rc);
            }
            btif_rc_handler(event, p_data);
            break;

        default:
            BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
                                dump_av_sm_event_name(event));
            return FALSE;

    }

    return TRUE;
}
/*****************************************************************************
**
** Function        btif_av_state_opening_handler
**
** Description     Intermediate state managing events during establishment
**                 of avdtp channel
**
** Returns         TRUE if event was processed, FALSE otherwise
**
*******************************************************************************/

static BOOLEAN btif_av_state_opening_handler(btif_sm_event_t event, void *p_data, int index)
{
    int i;
    BTIF_TRACE_IMP("%s event:%s flags %x on index = %d", __FUNCTION__,
                     dump_av_sm_event_name(event), btif_av_cb[index].flags, index);
    switch (event)
    {
        case BTIF_SM_ENTER_EVT:
            /* inform the application that we are entering connecting state */
            if (bt_av_sink_callbacks != NULL)
            {
                HAL_CBACK(bt_av_sink_callbacks, connection_state_cb,
                         BTAV_CONNECTION_STATE_CONNECTING, &(btif_av_cb[index].peer_bda));
            }
            else if (bt_av_src_callbacks != NULL)
            {
                HAL_CBACK(bt_av_src_callbacks, connection_state_cb,
                         BTAV_CONNECTION_STATE_CONNECTING, &(btif_av_cb[index].peer_bda));
            }
            break;

        case BTIF_SM_EXIT_EVT:
            break;

        case BTA_AV_REJECT_EVT:
            BTIF_TRACE_DEBUG(" Received  BTA_AV_REJECT_EVT ");
            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
                                        &(btif_av_cb[index].peer_bda));
            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
            break;

        case BTA_AV_OPEN_EVT:
        {
            tBTA_AV *p_bta_data = (tBTA_AV*)p_data;
            btav_connection_state_t state;
            btif_sm_state_t av_state;
            BTIF_TRACE_DEBUG("status:%d, edr 0x%x, role: 0x%x",p_bta_data->open.status,
                             p_bta_data->open.edr, p_bta_data->open.role);

            if (p_bta_data->open.status == BTA_AV_SUCCESS)
            {
                 state = BTAV_CONNECTION_STATE_CONNECTED;
                 av_state = BTIF_AV_STATE_OPENED;
                 btif_av_cb[index].edr = p_bta_data->open.edr;
                 if (p_bta_data->open.role == HOST_ROLE_SLAVE)
                 {
                    btif_av_cb[index].is_slave = TRUE;
                 }
                 btif_av_cb[index].peer_sep = p_bta_data->open.sep;
                 btif_a2dp_set_peer_sep(p_bta_data->open.sep);
                 if (p_bta_data->open.edr & BTA_AV_EDR_3MBPS)
                 {
                     BTIF_TRACE_DEBUG("remote supports 3 mbps");
                     btif_av_cb[index].edr_3mbps = TRUE;
                 }
            }
            else
            {
                BTIF_TRACE_WARNING("BTA_AV_OPEN_EVT::FAILED status: %d",
                                     p_bta_data->open.status );
                /* Multicast: Check if connected to AVRC only device
                 * disconnect when Dual A2DP/Multicast is supported.
                 */
                BD_ADDR peer_addr;
                if ((btif_rc_get_connected_peer(peer_addr))
                    &&(!bdcmp(btif_av_cb[index].peer_bda.address, peer_addr)))
                {
                    /* Disconnect AVRCP connection, if A2DP
                     * conneciton failed, for any reason
                     */
                    BTIF_TRACE_WARNING(" Disconnecting AVRCP ");
                    BTA_AvCloseRc(btif_rc_get_connected_peer_handle(peer_addr));
                }
                state = BTAV_CONNECTION_STATE_DISCONNECTED;
                av_state  = BTIF_AV_STATE_IDLE;
            }

            /* inform the application of the event */
            btif_report_connection_state(state, &(btif_av_cb[index].peer_bda));
            /* change state to open/idle based on the status */
            btif_sm_change_state(btif_av_cb[index].sm_handle, av_state);
            /* Check if the other connected AV is playing,
            * If YES, trigger DUAL Handoff. */
            if ((p_bta_data->open.status == BTA_AV_SUCCESS))
            {
                /* BTIF AV State updated, now check
                 * and update multicast state
                 */
                btif_av_update_multicast_state(index);

                /*This device should be now ready for all next playbacks*/
                btif_av_cb[index].current_playing = TRUE;
                if (enable_multicast == FALSE)
                {
                    for (i = 0; i < btif_max_av_clients; i++)
                    {
                        //Other device is not current playing
                        if (i != index)
                            btif_av_cb[i].current_playing = FALSE;
                    }
                    /* In A2dp Multicast, stack will take care of starting
                     * the stream on newly connected A2dp device. If Handoff
                     * is supported, trigger Handoff here. */
                    if (btif_av_is_playing())
                    {
                        BTIF_TRACE_DEBUG("Trigger Dual A2dp Handoff on %d", index);
                        btif_av_trigger_dual_handoff(TRUE, btif_av_cb[index].peer_bda.address);
                    }
                }
                if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
                {
                    /* if queued PLAY command,  send it now */
                    btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr,
                                (p_bta_data->open.status == BTA_AV_SUCCESS));
                }
                else if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
                {
                    /* if queued PLAY command,  send it now */
                    btif_rc_check_handle_pending_play(p_bta_data->open.bd_addr, FALSE);
                    /* Bring up AVRCP connection too */
                    BTA_AvOpenRc(btif_av_cb[index].bta_handle);
                }
            }
            btif_queue_advance();
        } break;

        case BTIF_AV_SINK_CONFIG_REQ_EVT:
        {
            btif_av_sink_config_req_t req;
            // copy to avoid alignment problems
            memcpy(&req, p_data, sizeof(req));

            BTIF_TRACE_WARNING("BTIF_AV_SINK_CONFIG_REQ_EVT %d %d", req.sample_rate,
                    req.channel_count);
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC && bt_av_sink_callbacks != NULL) {
                HAL_CBACK(bt_av_sink_callbacks, audio_config_cb, &(btif_av_cb[index].peer_bda),
                        req.sample_rate, req.channel_count);
            }
        } break;

        case BTIF_AV_CONNECT_REQ_EVT:
            // Check for device, if same device which moved to opening then ignore callback
            if (memcmp ((bt_bdaddr_t*)p_data, &(btif_av_cb[index].peer_bda),
                sizeof(btif_av_cb[index].peer_bda)) == 0)
            {
                BTIF_TRACE_DEBUG("%s: Same device moved to Opening state,ignore Connect Req", __func__);
                btif_queue_advance();
                break;
            }
            else
            {
                BTIF_TRACE_DEBUG("%s: Moved from idle by Incoming Connection request", __func__);
                btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, (bt_bdaddr_t*)p_data);
                btif_queue_advance();
                break;
            }

        case BTA_AV_PENDING_EVT:
            // Check for device, if same device which moved to opening then ignore callback
            if (memcmp (((tBTA_AV*)p_data)->pend.bd_addr, &(btif_av_cb[index].peer_bda),
                sizeof(btif_av_cb[index].peer_bda)) == 0)
            {
                BTIF_TRACE_DEBUG("%s: Same device moved to Opening state,ignore Pending Req", __func__);
                break;
            }
            else
            {
                BTIF_TRACE_DEBUG("%s: Moved from idle by outgoing Connection request", __func__);
                BTA_AvDisconnect(((tBTA_AV*)p_data)->pend.bd_addr);
                break;
            }

        case BTA_AV_CLOSE_EVT:
            /* avdtp link is closed */
            /* Check if any other device is playing
            * and this is not the one.*/
            if (!btif_av_is_playing())
            {
                btif_a2dp_on_stopped(NULL);
            }
            /* inform the application that we are disconnected */
            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
                    &(btif_av_cb[index].peer_bda));
            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
            break;

        case BTIF_AV_DISCONNECT_REQ_EVT:
            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
                &(btif_av_cb[index].peer_bda));
            BTA_AvClose(btif_av_cb[index].bta_handle);
            btif_queue_advance();
            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
            break;

        case BTA_AV_RC_OPEN_EVT:
            btif_rc_handler(event, p_data);;
            break;

        CHECK_RC_EVENT(event, p_data);

        default:
            BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
                                dump_av_sm_event_name(event));
            return FALSE;

   }
   return TRUE;
}


/*****************************************************************************
**
** Function        btif_av_state_closing_handler
**
** Description     Intermediate state managing events during closing
**                 of avdtp channel
**
** Returns         TRUE if event was processed, FALSE otherwise
**
*******************************************************************************/

static BOOLEAN btif_av_state_closing_handler(btif_sm_event_t event, void *p_data, int index)
{
    BTIF_TRACE_IMP("%s event:%s flags %x and index = %d", __FUNCTION__,
                     dump_av_sm_event_name(event), btif_av_cb[index].flags, index);

    switch (event)
    {
        case BTIF_SM_ENTER_EVT:
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
            {
                /* Multicast/Soft Hand-off:
                 * If MC/SHO is enabled we need to keep/start playing on
                 * other device.
                 */
                if (btif_av_is_connected_on_other_idx(index))
                {
                    if (btif_av_is_playing())
                    {
                        APPL_TRACE_DEBUG("Keep playing on other device");
                    }
                    else
                    {
                        APPL_TRACE_DEBUG("Not playing on other devie: Set Flush");
                        btif_a2dp_set_tx_flush(TRUE);
                    }
                }
                else
                {
                    /* Single connections scenario:
                     * Immediately stop transmission of frames
                     * wait for audioflinger to stop a2dp
                     */
                    btif_a2dp_set_tx_flush(TRUE);
                }
            }
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
            {
                btif_a2dp_set_rx_flush(TRUE);
            }
            break;

        case BTA_AV_STOP_EVT:
        case BTIF_AV_STOP_STREAM_REQ_EVT:
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
            {
                /* Dont stop in DUAL A2dp connections, as
                * UIPC will keep waiting for Audio CTRL channel
                * to get closed which is not required in Dual A2dp.
                * We will stop only when only single A2dp conn is present.*/
                if (btif_av_is_connected_on_other_idx(index))
                {
                    if (!btif_av_is_playing())
                    {
                        APPL_TRACE_WARNING("Suspend the AV Data channel");
                        //Flush and close media channel
                        btif_a2dp_set_tx_flush(TRUE);
                        btif_media_task_stop_aa_req();
                    }
                }
                else
                {
                    /* immediately flush any pending tx frames while suspend is pending */
                    APPL_TRACE_WARNING("Stop the AV Data channel");
                    btif_a2dp_set_tx_flush(TRUE);
                    btif_a2dp_on_stopped(NULL);
                }
            }
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
            {
                btif_a2dp_set_rx_flush(TRUE);
                btif_a2dp_on_stopped(NULL);
            }
            break;

        case BTIF_SM_EXIT_EVT:
            break;

        case BTA_AV_CLOSE_EVT:

            /* inform the application that we are disconnecting */
            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb[index].peer_bda));

            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
            break;

        /* Handle the RC_CLOSE event for the cleanup */
        case BTA_AV_RC_CLOSE_EVT:
            btif_rc_handler(event, (tBTA_AV*)p_data);
            break;

        default:
            BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
                                dump_av_sm_event_name(event));
            return FALSE;
   }
   return TRUE;
}


/*****************************************************************************
**
** Function     btif_av_state_opened_handler
**
** Description  Handles AV events while AVDTP is in OPEN state
**
** Returns      TRUE if event was processed, FALSE otherwise
**
*******************************************************************************/

static BOOLEAN btif_av_state_opened_handler(btif_sm_event_t event, void *p_data, int index)
{
    tBTA_AV *p_av = (tBTA_AV*)p_data;
    tBTIF_STATUS status = BTIF_SUCCESS;

    BTIF_TRACE_IMP("%s event:%s flags %x and index = %d", __FUNCTION__,
                     dump_av_sm_event_name(event), btif_av_cb[index].flags, index);

    if ((event == BTA_AV_REMOTE_CMD_EVT) &&
         (p_av->remote_cmd.rc_id == BTA_AV_RC_PLAY) )
    {
        for (int i = 0; i < btif_max_av_clients; i++)
        {
            if (btif_av_cb[i].flags & BTIF_AV_FLAG_REMOTE_SUSPEND)
            {
                BTIF_TRACE_EVENT("%s: Resetting remote suspend flag on RC PLAY",
                        __FUNCTION__);
                btif_av_cb[i].flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
            }
        }
    }

    switch (event)
    {
        case BTIF_SM_ENTER_EVT:
            btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_STOP;
            btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
            break;

        case BTIF_SM_EXIT_EVT:
            btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
            break;

        case BTIF_AV_START_STREAM_REQ_EVT:
            /* update multicast state here if new device is connected
             * after A2dp connection. New A2dp device is connected
             * whlie playing */
            btif_av_update_multicast_state(index);
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
            {
                BTA_AvStart(btif_av_cb[index].bta_handle);
                btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_START;
                break;
            }
            status = btif_a2dp_setup_codec();
            if (status == BTIF_SUCCESS)
            {
                int idx = 0;
                BTA_AvStart(btif_av_cb[index].bta_handle);
                if (enable_multicast == TRUE)
                {
                    /* In A2dp Multicast, DUT initiated stream request
                    * should be true for all connected A2dp devices. */
                    for (; idx < btif_max_av_clients; idx++)
                    {
                        btif_av_cb[idx].flags |= BTIF_AV_FLAG_PENDING_START;
                    }
                }
                else
                {
                    btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_START;
                }
            }
            else if (status == BTIF_ERROR_SRV_AV_CP_NOT_SUPPORTED)
            {
#if defined(BTA_AV_DISCONNECT_IF_NO_SCMS_T) && (BTA_AV_DISCONNECT_IF_NO_SCMS_T == TRUE)
                BTIF_TRACE_ERROR0("SCMST enabled, disconnect as remote does not support SCMST");
                BTA_AvDisconnect(btif_av_cb[index].peer_bda.address);
#else
                BTIF_TRACE_WARNING("SCMST enabled, connecting to non SCMST SEP");
                BTA_AvStart(btif_av_cb[index].bta_handle);
                btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_START;
#endif
            }
            else
            {
                BTIF_TRACE_ERROR("## AV Disconnect## status : %x",status);
                BTA_AvDisconnect(btif_av_cb[index].peer_bda.address);
            }
            break;

        case BTA_AV_START_EVT:
        {
            BTIF_TRACE_DEBUG("BTA_AV_START_EVT status %d, suspending %d, init %d",
                p_av->start.status, p_av->start.suspending, p_av->start.initiator);
            BTIF_TRACE_DEBUG("BTA_AV_START_EVT role: %d", p_av->start.role);
            if (p_av->start.role == HOST_ROLE_SLAVE)
            {
                btif_av_cb[index].is_slave = TRUE;
            }
            else
            {
                // update if we are master after role switch before start
                btif_av_cb[index].is_slave = FALSE;
            }
            /* There can be role switch after device is connected,
             * hence check for role before starting multicast, and
             * disable if we are in slave role for any connection
             */
            btif_av_update_multicast_state(index);

            if ((p_av->start.status == BTA_SUCCESS) && (p_av->start.suspending == TRUE))
                return TRUE;

            /* if remote tries to start a2dp when call is in progress, suspend it right away */
            if ((!(btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START)) && (!btif_hf_is_call_idle())) {
                BTIF_TRACE_EVENT("%s: trigger suspend as call is in progress!!", __FUNCTION__);
                btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
            }


            /* if remote tries to start a2dp when DUT is a2dp source
             * then suspend. In case a2dp is sink and call is active
             * then disconnect the AVDTP channel
             */
            if (!(btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START))
            {
                if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
                {
                    if (enable_multicast)
                    {
                        /* Stack will start the playback on newly connected
                         * A2dp device, if the playback is already happening on
                         * other connected device.*/
                        if (btif_av_is_playing())
                        {
                            /* when HS2 is connected during HS1 playing, stack directly
                             * sends start event hence update encoder so that least L2CAP
                             *  MTU is selected.*/
                            //btif_a2dp_update_codec();
                            BTIF_TRACE_DEBUG("%s: A2dp Multicast playback",
                                    __FUNCTION__);
                        }
                        /* initiate suspend if start is initiate by remote and multicast
                         * is enabled.
                         * Avoid suspend if stream is started as quick suspend-start
                         * creates IOT issue, seen with SBH50.
                         */

                        if (!p_av->start.initiator && !btif_av_is_playing())
                        {
                            BTIF_TRACE_DEBUG("initiate suspend for remote start");
                            btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
                        }
                    }
                    else
                    {
                        BTIF_TRACE_DEBUG("%s: trigger suspend as remote initiated!!",
                                __FUNCTION__);
                        btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
                    }
                }
            }

            /*  In case peer is A2DP SRC we do not want to ack commands on UIPC*/
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
            {
                if (btif_a2dp_on_started(&p_av->start,
                    ((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) != 0)))
                {
                    /* only clear pending flag after acknowledgement */
                    btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
                }
            }

            /* remain in open state if status failed */
            /* Multicast-soft Handoff:
             * START failed, cleanup Handoff flag.
             */
            if (p_av->start.status != BTA_AV_SUCCESS)
            {
                int i;

                for (i = 0; i < btif_max_av_clients; i++)
                {
                    btif_av_cb[i].dual_handoff = FALSE;
                }
                return FALSE;
            }

#ifndef AVK_BACKPORT
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
            {
                btif_a2dp_set_rx_flush(FALSE); /*  remove flush state, ready for streaming*/
            }
#endif

            /* change state to started, send acknowledgement if start is pending */
            if (btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) {
                if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
                    btif_a2dp_on_started(NULL, TRUE);
                /* pending start flag will be cleared when exit current state */
            }
            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_STARTED);

        } break;

        case BTIF_AV_DISCONNECT_REQ_EVT:
            BTA_AvClose(btif_av_cb[index].bta_handle);
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC) {
                BTA_AvCloseRc(btif_av_cb[index].bta_handle);
            }

            /* inform the application that we are disconnecting */
            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb[index].peer_bda));
            break;

        case BTA_AV_CLOSE_EVT:
             /* avdtp link is closed */
             /*Dont close the A2dp when Dual playback is happening*/
             if (btif_av_is_connected_on_other_idx(index))
             {
                 APPL_TRACE_WARNING("Conn is closing,close AV data channel");
                 if (!btif_av_is_playing())
                 {
                     APPL_TRACE_WARNING("Suspend the AV Data channel");
                     /* ensure tx frames are immediately suspended */
                     btif_a2dp_set_tx_flush(TRUE);
                     btif_media_task_stop_aa_req();
                 }
             }
             else
             {
                 APPL_TRACE_WARNING("Stop the AV Data channel");
                 btif_a2dp_on_stopped(NULL);
             }

            /* inform the application that we are disconnected */
            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
                                        &(btif_av_cb[index].peer_bda));

            /* change state to idle, send acknowledgement if start is pending */
            if (btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) {
                btif_a2dp_ack_fail();
                /* pending start flag will be cleared when exit current state */
            }

            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
            break;

        case BTA_AV_RECONFIG_EVT:
            if((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START) &&
                (p_av->reconfig.status == BTA_AV_SUCCESS))
            {
               APPL_TRACE_WARNING("reconfig done BTA_AVstart()");
               BTA_AvStart(btif_av_cb[index].bta_handle);
            }
            else if(btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START)
            {
               btif_av_cb[index].flags &= ~BTIF_AV_FLAG_PENDING_START;
               btif_a2dp_ack_fail();
            }
            break;

        case BTIF_AV_CONNECT_REQ_EVT:
            if (memcmp ((bt_bdaddr_t*)p_data, &(btif_av_cb[index].peer_bda),
                sizeof(btif_av_cb[index].peer_bda)) == 0)
            {
                BTIF_TRACE_DEBUG("%s: Ignore BTIF_AV_CONNECT_REQ_EVT for same device", __func__);
            }
            else
            {
                BTIF_TRACE_DEBUG("%s: Moved to opened by Other Incoming Conn req", __func__);
                btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
                        (bt_bdaddr_t*)p_data);
            }
            btif_queue_advance();
            break;

        case BTA_AV_RC_OPEN_EVT:
            btif_av_check_rc_connection_priority(p_data);
            break;
        CHECK_RC_EVENT(event, p_data);

        default:
            BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
                               dump_av_sm_event_name(event));
            return FALSE;

    }
    return TRUE;
}

/*****************************************************************************
**
** Function     btif_av_state_started_handler
**
** Description  Handles AV events while A2DP stream is started
**
** Returns      TRUE if event was processed, FALSE otherwise
**
*******************************************************************************/

static BOOLEAN btif_av_state_started_handler(btif_sm_event_t event, void *p_data, int index)
{
    tBTA_AV *p_av = (tBTA_AV*)p_data;
    btif_sm_state_t state = BTIF_AV_STATE_IDLE;
    int i;

    BTIF_TRACE_IMP("%s event:%s flags %x  index =%d", __FUNCTION__,
                     dump_av_sm_event_name(event), btif_av_cb[index].flags, index);

    switch (event)
    {
        case BTIF_SM_ENTER_EVT:
            /* we are again in started state, clear any remote suspend flags */
            btif_av_cb[index].flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;

            btif_report_audio_state(BTAV_AUDIO_STATE_STARTED, &(btif_av_cb[index].peer_bda));
            btif_av_cb[index].is_device_playing = TRUE;

            /* increase the a2dp consumer task priority temporarily when start
            ** audio playing, to avoid overflow the audio packet queue. */
            adjust_priority_a2dp(TRUE);
#ifdef AVK_BACKPORT
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC)
            {
                btif_av_request_audio_focus(TRUE);
            }
#endif
            //Clear Dual Handoff for all SCBs
            for (i = 0; i < btif_max_av_clients; i++)
            {
                btif_av_cb[i].dual_handoff = FALSE;
                //Other device is not current playing
                if (i != index)
                    btif_av_cb[i].current_playing = FALSE;
            }
            //This is latest device to play now
            btif_av_cb[index].current_playing = TRUE;
            break;

        case BTIF_SM_EXIT_EVT:
            /* restore the a2dp consumer task priority when stop audio playing. */
            adjust_priority_a2dp(FALSE);

            break;

        case BTIF_AV_START_STREAM_REQ_EVT:
            /* we were remotely started, just ack back the local request */
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
                btif_a2dp_on_started(NULL, TRUE);
            break;

        /* fixme -- use suspend = true always to work around issue with BTA AV */
        case BTIF_AV_STOP_STREAM_REQ_EVT:
        case BTIF_AV_SUSPEND_STREAM_REQ_EVT:

            /* set pending flag to ensure btif task is not trying to restart
             * stream while suspend is in progress.
             * Multicast: If streaming is happening on both devices, we need
             * to update flag for both connections as SUSPEND request will
             * be sent to only one stream as internally BTA takes care of
             * suspending both streams.
             */
            for(i = 0; i < btif_max_av_clients; i++)
            {
                state = btif_sm_get_state(btif_av_cb[i].sm_handle);
                if (state == BTIF_AV_STATE_STARTED)
                {
                    btif_av_cb[i].flags |= BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
                }
            }

            /* if we were remotely suspended but suspend locally, local suspend
               always overrides */
            btif_av_cb[index].flags &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;

            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
            {
            /* immediately stop transmission of frames while suspend is pending */
                btif_a2dp_set_tx_flush(TRUE);
            }

            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC) {
                btif_a2dp_set_rx_flush(TRUE);
                btif_a2dp_on_stopped(NULL);
            }

            BTA_AvStop(TRUE, btif_av_cb[index].bta_handle);
            break;

        case BTIF_AV_DISCONNECT_REQ_EVT:

            //Now it is not the current playing
            btif_av_cb[index].current_playing = FALSE;
            btif_av_update_current_playing_device(index);
            btif_rc_clear_priority(btif_av_cb[index].peer_bda.address);
            /* request avdtp to close */
            BTA_AvClose(btif_av_cb[index].bta_handle);
            if (btif_av_cb[index].peer_sep == AVDT_TSEP_SRC) {
                BTA_AvCloseRc(btif_av_cb[index].bta_handle);
            }

            /* inform the application that we are disconnecting */
            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTING, &(btif_av_cb[index].peer_bda));

            /* wait in closing state until fully closed */
            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_CLOSING);
            break;

        case BTA_AV_SUSPEND_EVT:

            BTIF_TRACE_EVENT("BTA_AV_SUSPEND_EVT status %d, init %d",
                 p_av->suspend.status, p_av->suspend.initiator);
            //Check if this suspend is due to DUAL_Handoff
            if ((btif_av_cb[index].dual_handoff) &&
                (p_av->suspend.status == BTA_AV_SUCCESS))
            {
                BTIF_TRACE_EVENT("BTA_AV_SUSPEND_EVT: Dual handoff");
                btif_dispatch_sm_event(BTIF_AV_START_STREAM_REQ_EVT, NULL, 0);
            }
            if (p_av->suspend.initiator != TRUE)
            {
                /* remote suspend, notify HAL and await audioflinger to
                 * suspend/stop stream
                 * set remote suspend flag to block media task from restarting
                 * stream only if we did not already initiate a local suspend
                 * set remote suspend flag before suspending stream as in race conditions
                 * when stream is suspended, but flag is things ge tossed up
                 */
                BTIF_TRACE_EVENT("Clear before suspending");
                if ((btif_av_cb[index].flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING) == 0)
                    btif_av_cb[index].flags |= BTIF_AV_FLAG_REMOTE_SUSPEND;
                for (int i = 0; i < btif_max_av_clients; i++)
                {
                    if ((i != index) && btif_av_get_ongoing_multicast())
                    {
                        multicast_disabled = TRUE;
                        btif_av_update_multicast_state(index);
                        BTIF_TRACE_EVENT("Initiate suspend for other HS also");
                        btif_sm_dispatch(btif_av_cb[i].sm_handle,
                                BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL);
                    }
                }
            }

            /* a2dp suspended, stop media task until resumed */
            /* Multicast: If streaming on other device, don't call onsuspended
             * as it unblocks the audio process and audio process may send
             * subsequent commands and create problem during the time where we
             * still did not receive response for SUSPEND sent to other device.
             * Keep the suspend failure handling untouched and handle
             * only success case to check and avoid calling onsuspended.
             */
            if ((p_av->suspend.status != BTA_AV_SUCCESS) ||
                !btif_av_is_playing_on_other_idx(index))
            {
                btif_a2dp_on_suspended(&p_av->suspend);
            }
            else if(btif_av_is_playing_on_other_idx(index))
            {
                BTIF_TRACE_DEBUG("Other device not suspended, don't ack the suspend");
            }

            /* if not successful, remain in current state */
            if (p_av->suspend.status != BTA_AV_SUCCESS)
            {
                btif_av_cb[index].flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;

               if (btif_av_cb[index].peer_sep == AVDT_TSEP_SNK)
               {
                /* suspend failed, reset back tx flush state */
                    btif_a2dp_set_tx_flush(FALSE);
               }
                return FALSE;
            }

            if (p_av->suspend.initiator != TRUE)
            {
                btif_report_audio_state(BTAV_AUDIO_STATE_REMOTE_SUSPEND, &(btif_av_cb[index].peer_bda));
            }
            else
            {
                btif_report_audio_state(BTAV_AUDIO_STATE_REMOTE_SUSPEND, &(btif_av_cb[index].peer_bda));
            }
            btif_av_cb[index].is_device_playing = FALSE;
            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENED);

            /* suspend completed and state changed, clear pending status */
            btif_av_cb[index].flags &= ~BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING;
            break;

        case BTA_AV_STOP_EVT:

            btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_STOP;
            btif_av_cb[index].current_playing = FALSE;
            if (btif_av_is_connected_on_other_idx(index))
            {
                if (enable_multicast == FALSE)
                {
                    APPL_TRACE_WARNING("other Idx is connected, move to SUSPENDED");
                    btif_rc_send_pause_command();
                    btif_a2dp_on_stopped(&p_av->suspend);
                }
            }
            else
            {
                APPL_TRACE_WARNING("Stop the AV Data channel as no connection is present");
                btif_a2dp_on_stopped(&p_av->suspend);
            }
            btif_av_cb[index].is_device_playing = FALSE;


            btif_report_audio_state(BTAV_AUDIO_STATE_STOPPED, &(btif_av_cb[index].peer_bda));
            /* if stop was successful, change state to open */
            if (p_av->suspend.status == BTA_AV_SUCCESS)
                btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_OPENED);

            break;

        case BTA_AV_CLOSE_EVT:

             btif_av_cb[index].flags |= BTIF_AV_FLAG_PENDING_STOP;

            /* avdtp link is closed */
            APPL_TRACE_WARNING("Stop the AV Data channel");
            btif_a2dp_on_stopped(NULL);

            /* inform the application that we are disconnected */
            btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED,
                                        &(btif_av_cb[index].peer_bda));

            btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
            break;

        case BTA_AV_RC_OPEN_EVT:
            btif_av_check_rc_connection_priority(p_data);
            break;

        CHECK_RC_EVENT(event, p_data);

        default:
            BTIF_TRACE_WARNING("%s : unhandled event:%s", __FUNCTION__,
                                 dump_av_sm_event_name(event));
            return FALSE;

    }
    return TRUE;
}


void btif_av_event_deep_copy(UINT16 event, char *p_dest, char *p_src)
{
    tBTA_AV *av_src = (tBTA_AV *)p_src;
    tBTA_AV *av_dest = (tBTA_AV *)p_dest;

    // First copy the structure
    memcpy(p_dest, p_src, sizeof(tBTA_AV));

    switch (event)
    {
        case BTA_AV_META_MSG_EVT:
            if (av_src->meta_msg.p_data && av_src->meta_msg.len)
            {
                av_dest->meta_msg.p_data = osi_calloc(av_src->meta_msg.len);
                assert(av_dest->meta_msg.p_data);
                memcpy(av_dest->meta_msg.p_data, av_src->meta_msg.p_data, av_src->meta_msg.len);
            }

            if (av_src->meta_msg.p_msg)
            {
                av_dest->meta_msg.p_msg = osi_calloc(sizeof(tAVRC_MSG));
                assert(av_dest->meta_msg.p_msg);
                memcpy(av_dest->meta_msg.p_msg, av_src->meta_msg.p_msg, sizeof(tAVRC_MSG));

                if (av_src->meta_msg.p_msg->vendor.p_vendor_data &&
                    av_src->meta_msg.p_msg->vendor.vendor_len)
                {
                    av_dest->meta_msg.p_msg->vendor.p_vendor_data = osi_calloc(
                        av_src->meta_msg.p_msg->vendor.vendor_len);
                    assert(av_dest->meta_msg.p_msg->vendor.p_vendor_data);
                    memcpy(av_dest->meta_msg.p_msg->vendor.p_vendor_data,
                        av_src->meta_msg.p_msg->vendor.p_vendor_data,
                        av_src->meta_msg.p_msg->vendor.vendor_len);
                }
            }
            break;
        case BTA_AV_BROWSE_MSG_EVT:
            if (av_src->browse_msg.p_msg)
            {
                av_dest->browse_msg.p_msg = osi_calloc(sizeof(tAVRC_MSG));
                assert(av_dest->browse_msg.p_msg);
                memcpy(av_dest->browse_msg.p_msg, av_src->browse_msg.p_msg, sizeof(tAVRC_MSG));

                if (av_src->browse_msg.p_msg->browse.p_browse_data &&
                    av_src->browse_msg.p_msg->browse.browse_len)
                {
                    av_dest->browse_msg.p_msg->browse.p_browse_data = osi_calloc(
                        av_src->browse_msg.p_msg->browse.browse_len);
                    assert(av_dest->browse_msg.p_msg->browse.p_browse_data);
                    memcpy(av_dest->browse_msg.p_msg->browse.p_browse_data,
                        av_src->browse_msg.p_msg->browse.p_browse_data,
                        av_src->browse_msg.p_msg->browse.browse_len);
                }
            }
            break;

        default:
            break;
    }
}

static void btif_av_event_free_data(btif_sm_event_t event, void *p_data)
{
    switch (event)
    {
        case BTA_AV_META_MSG_EVT:
            {
                tBTA_AV *av = (tBTA_AV*)p_data;
                if (av->meta_msg.p_data)
                    osi_free(av->meta_msg.p_data);

                if (av->meta_msg.p_msg)
                {
                    if (av->meta_msg.p_msg->vendor.p_vendor_data)
                        osi_free(av->meta_msg.p_msg->vendor.p_vendor_data);
                    osi_free(av->meta_msg.p_msg);
                }
            }
            break;
        case BTA_AV_BROWSE_MSG_EVT:
            {
                tBTA_AV *av = (tBTA_AV*)p_data;

                if (av->browse_msg.p_msg)
                {
                    if (av->browse_msg.p_msg->browse.p_browse_data)
                        osi_free(av->browse_msg.p_msg->browse.p_browse_data);
                    osi_free(av->browse_msg.p_msg);
                }
            }
            break;

        default:
            break;
    }
}

/*****************************************************************************
**  Local event handlers
******************************************************************************/

static void btif_av_handle_event(UINT16 event, char* p_param)
{
    int index = 0;
    tBTA_AV *p_bta_data = (tBTA_AV*)p_param;
    bt_bdaddr_t * bt_addr;
    UINT8 role;

    switch (event)
    {
        /*events from Upper layer and Media Task*/
        case BTIF_AV_CLEANUP_REQ_EVT: /*Clean up to be called on default index*/
            BTIF_TRACE_EVENT("%s: BTIF_AV_CLEANUP_REQ_EVT", __FUNCTION__);
            btif_a2dp_stop_media_task();
            return;
        case BTIF_AV_CONNECT_REQ_EVT:
            break;
        case BTIF_AV_DISCONNECT_REQ_EVT:
            /*Bd address passed should help us in getting the handle*/
            bt_addr = (bt_bdaddr_t *)p_param;
            index = btif_av_idx_by_bdaddr(bt_addr->address);
            break;
        case BTIF_AV_START_STREAM_REQ_EVT:
            /* Get the last connected device on which START can be issued
            * Get the Dual A2dp Handoff Device first, if none is present,
            * go for lastest connected.
            * In A2dp Multicast, the index selected can be any of the
            * connected device. Stack will ensure to START the steaming
            * on both the devices. */
            index = btif_get_latest_device_idx_to_start();
            break;
        case BTIF_AV_STOP_STREAM_REQ_EVT:
        case BTIF_AV_SUSPEND_STREAM_REQ_EVT:
            /*Should be handled by current STARTED*/
            index = btif_get_latest_playing_device_idx();
            break;
        /*Events from the stack, BTA*/
        case BTA_AV_ENABLE_EVT:
            index = 0;
            break;
        case BTA_AV_REGISTER_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->registr.hndl);
            break;
        case BTA_AV_OPEN_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->open.hndl);
            break;
        case BTA_AV_ROLE_CHANGED_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->role_changed.hndl);
            role = p_bta_data->role_changed.new_role;
            BTIF_TRACE_EVENT("Role change: 0x%x: new role: %s",
                p_bta_data->role_changed.hndl, (role == HOST_ROLE_SLAVE) ? "Slave" : "Master");
            if (index >= 0 && index < btif_max_av_clients)
            {
                btif_av_cb[index].is_slave = (role == HOST_ROLE_SLAVE) ? TRUE : FALSE;
                btif_av_update_multicast_state(index);
            }
            else
            {
                BTIF_TRACE_ERROR("%s: Invalid index for connection", __FUNCTION__);
            }
            return;

        case BTA_AV_PENDING_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->pend.hndl);
            break;
        case BTA_AV_REJECT_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->reject.hndl);
            break;
        case BTA_AV_STOP_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->suspend.hndl);
            break;
        case BTA_AV_CLOSE_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->close.hndl);
            break;
        case BTA_AV_START_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->start.hndl);
            break;
        case BTA_AV_RECONFIG_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->reconfig.hndl);
            break;
        case BTA_AV_SUSPEND_EVT:
            index = HANDLE_TO_INDEX(p_bta_data->suspend.hndl);
            break;

        /* Handle all RC events on default index. RC handling should take
         * care of the events. All events come with BD Address
         * Handled well in AV Opening, opened and started state
         * AV Idle handler needs to take care of this event properly.
         */
        case BTA_AV_RC_OPEN_EVT:
            index = btif_av_get_valid_idx_for_rc_events(p_bta_data->rc_open.peer_addr,
                    p_bta_data->rc_open.rc_handle);
            break;
        case BTA_AV_RC_CLOSE_EVT:
        /* If there is no entry in the connection table
         * RC handler has to be called for cleanup.
         * Directly call the RC handler as we cannot
         * associate any AV handle to it.
         */
            index = btif_av_idx_by_bdaddr(p_bta_data->rc_open.peer_addr);
            if (index == btif_max_av_clients)
            {
                btif_rc_handler(event, p_bta_data);
            }
            break;
        /* Let the RC handler decide on these passthrough cmds
         * Use rc_handle to get the active AV device and use that mapping.
         */
        case BTA_AV_REMOTE_CMD_EVT:
        case BTA_AV_VENDOR_CMD_EVT:
        case BTA_AV_META_MSG_EVT:
        case BTA_AV_RC_FEAT_EVT:
        case BTA_AV_BROWSE_MSG_EVT:
            index = 0;
            BTIF_TRACE_EVENT("RC events: on index = %d", index);
            break;
        default:
            BTIF_TRACE_ERROR("Unhandled event = %d", event);
            break;
    }
    BTIF_TRACE_DEBUG("Handle the AV event = %x on index = %d", event, index);
    if (index >= 0 && index < btif_max_av_clients)
        btif_sm_dispatch(btif_av_cb[index].sm_handle, event, (void*)p_param);
    else
        BTIF_TRACE_ERROR("Unhandled Index = %d", index);
}

/*******************************************************************************
**
** Function         btif_av_get_valid_idx
**
** Description      Check the validity of the current index for the connection
**
** Returns          Boolean
**
*******************************************************************************/

static BOOLEAN btif_av_get_valid_idx(int idx)
{
    btif_sm_state_t state = btif_sm_get_state(btif_av_cb[idx].sm_handle);
    return ((state == BTIF_AV_STATE_OPENED) ||
            (state ==  BTIF_AV_STATE_STARTED) ||
            (state == BTIF_AV_STATE_OPENING));
}

/*******************************************************************************
**
** Function         btif_av_idx_by_bdaddr
**
** Description      Get the index corresponding to BD addr
**
** Returns          UNIT8
**
*******************************************************************************/

static UINT8 btif_av_idx_by_bdaddr(BD_ADDR bd_addr)
{
    int i;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if ((bdcmp(bd_addr,
                  btif_av_cb[i].peer_bda.address) == 0))
            return i;
    }
    return i;
}

BOOLEAN btif_av_is_current_device(BD_ADDR address)
{
    UINT8 index;

    index = btif_av_idx_by_bdaddr(address);
    if((index < btif_max_av_clients) && btif_av_cb[index].current_playing)
    {
        return TRUE;
    }
    return FALSE;
}

/*******************************************************************************
**
** Function         btif_get_latest_device_idx_to_start
**
** Description      Get the index of the AV where streaming is to be started
**
** Returns          int
**
*******************************************************************************/

static int btif_get_latest_device_idx_to_start()
{
    int i, j;
    BD_ADDR playing_address;

    /* Get the device which sent PLAY command
     * If found, START on that index.
     */
    memset(playing_address, 0, sizeof(BD_ADDR));
    btif_rc_get_playing_device(playing_address);
    if (bdcmp(playing_address, bd_addr_null) != 0)
    {
        /* Got some valid Playing device.
         * Get the AV index for this device.
         */
        i = btif_av_idx_by_bdaddr(playing_address);
        if (i == btif_max_av_clients)
            return btif_max_av_clients;
        BTIF_TRACE_EVENT("Got some valid Playing device; %d", i);
        /*Clear the Current playing device*/
        for (j = 0; j < btif_max_av_clients; j++)
        {
            if (j != i)
              btif_av_cb[j].current_playing = FALSE;
        }
        /*Clear the Play command in RC*/
        btif_rc_clear_playing_state(FALSE);
        return i;
    }

    /*No playing device, get the latest*/
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if (btif_av_cb[i].current_playing)
            break;
    }
    if (i == btif_max_av_clients)
    {
        BTIF_TRACE_ERROR("Play on default");
        i = 0; /*play on default*/
    }
    return i;
}

/*******************************************************************************
**
** Function         btif_get_latest_playing_device_idx
**
** Description      Get the index of AV where streaming is happening
**
** Returns          int
**
*******************************************************************************/

static int btif_get_latest_playing_device_idx()
{
    int i;
    btif_sm_state_t state;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        state = btif_sm_get_state(btif_av_cb[i].sm_handle);
        if (state == BTIF_AV_STATE_STARTED)
        {
            break;
        }
    }
    return i;
}

/*******************************************************************************
**
** Function         btif_av_is_playing
**
** Description      Is AV in streaming state
**
** Returns          BOOLEAN
**
*******************************************************************************/

BOOLEAN btif_av_is_playing()
{
    int i;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
        if (btif_av_cb[i].state == BTIF_AV_STATE_STARTED)
        {
            BTIF_TRACE_EVENT("btif_av_is_playing on index= %d", i);
            return TRUE;
        }
    }
    return FALSE;
}

/*******************************************************************************
**
** Function         btif_get_conn_state_of_device
**
** Description      Returns the state of AV scb
**
** Returns          int
**
*******************************************************************************/

static int btif_get_conn_state_of_device(BD_ADDR address)
{
    btif_sm_state_t state = BTIF_AV_STATE_IDLE;
    int i;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if ((bdcmp(address,
            btif_av_cb[i].peer_bda.address) == 0))
        {
            state = btif_sm_get_state(btif_av_cb[i].sm_handle);
            BTIF_TRACE_EVENT("BD Found: %02X %02X %02X %02X %02X %02X :state: %s",
                address[5], address[4], address[3],
                address[2], address[1], address[0],
                dump_av_sm_state_name(state));
        }
    }
    return state;
}

/*******************************************************************************
**
** Function         btif_av_get_valid_idx_for_rc_events
**
** Description      gets th valid index for the RC event address
**
** Returns          int
**
*******************************************************************************/

static int btif_av_get_valid_idx_for_rc_events(BD_ADDR bd_addr, int rc_handle)
{
    int index = 0;
    /* First try to find if it is first event in AV IF
    * both the handles would be in IDLE state, pick the first
    * If we get second RC event while processing the priority
    * for the first, reject the second connection. */

    /*Get the index from connected SCBs*/
    index = btif_av_idx_by_bdaddr(bd_addr);
    if (index == btif_max_av_clients)
    {
        /* None of the SCBS matched
        * Allocate free SCB, null address SCB*/
        index = btif_av_idx_by_bdaddr(bd_null);
        BTIF_TRACE_EVENT("btif_av_get_valid_idx_for_rc_events is %d", index);
        if (index >= btif_max_av_clients)
        {
            BTIF_TRACE_EVENT("disconnect only AVRCP device rc_handle %d", rc_handle);
            BTA_AvCloseRc(rc_handle);
        }
    }
    return index;
}

/*******************************************************************************
**
** Function         btif_av_check_rc_connection_priority
**
** Description      Handles Priority callback for RC connections
**
** Returns          void
**
*******************************************************************************/

static void btif_av_check_rc_connection_priority(void *p_data)
{
    bt_bdaddr_t peer_bda;
    bdstr_t addr1, addr2;

    /*Check if it is for same AV device*/
    if (btif_av_is_device_connected(((tBTA_AV*)p_data)->rc_open.peer_addr))
    {
        /*AV is connected */
        BTIF_TRACE_DEBUG("AV is connected, process RC connect event");
        btif_rc_handler(BTA_AV_RC_OPEN_EVT, (tBTA_AV*)p_data);
        return;
    }
    BTIF_TRACE_DEBUG("btif_av_check_rc_connection_priority");
    bdcpy(peer_bda.address, ((tBTA_AV*)p_data)->rc_open.peer_addr);
    BTIF_TRACE_IMP("AVCTP Address : %s ", bdaddr_to_string(&peer_bda, &addr1,
            sizeof(addr1)));
    if (idle_rc_event != 0)
    {
        BTIF_TRACE_DEBUG("Processing another RC Event ");
        return;
    }
    idle_rc_event = BTA_AV_RC_OPEN_EVT;
    memcpy(&idle_rc_data, ((tBTA_AV*)p_data), sizeof(tBTA_AV));
    if (((tBTA_AV*)p_data)->rc_open.status == BTA_AV_SUCCESS)
    {
        BTIF_TRACE_DEBUG("RC conn is success ");
        if (bt_av_src_callbacks != NULL)
        {
            BTIF_TRACE_DEBUG(" Check Device priority");
            HAL_CBACK(bt_av_src_callbacks, connection_priority_cb,
                    &peer_bda);
        }
    }
    else
    {
        idle_rc_event = 0;
        memset(&idle_rc_data, 0, sizeof(tBTA_AV));
    }
    return;
}


static void bte_av_callback(tBTA_AV_EVT event, tBTA_AV *p_data)
{
    btif_transfer_context(btif_av_handle_event, event,
                          (char*)p_data, sizeof(tBTA_AV), btif_av_event_deep_copy);
}

/*Called only in case of A2dp SInk, which runs on index 0*/
static void bte_av_media_callback(tBTA_AV_EVT event, tBTA_AV_MEDIA *p_data)
{
    btif_sm_state_t state;
    UINT8 que_len;
    tA2D_STATUS a2d_status;
    tA2D_SBC_CIE sbc_cie;
    btif_av_sink_config_req_t config_req;
    int index =0;

    if (event == BTA_AV_MEDIA_DATA_EVT)/* Switch to BTIF_MEDIA context */
    {
        state= btif_sm_get_state(btif_av_cb[index].sm_handle);
        if ( (state == BTIF_AV_STATE_STARTED) || /* send SBC packets only in Started State */
             (state == BTIF_AV_STATE_OPENED) )
        {
            que_len = btif_media_sink_enque_buf((BT_HDR *)p_data);
            BTIF_TRACE_DEBUG(" Packets in Que %d",que_len);
        }
        else
            return;
    }

    if (event == BTA_AV_MEDIA_SINK_CFG_EVT) {
        /* send a command to BT Media Task */
        btif_reset_decoder((UINT8*)(p_data->avk_config.codec_info));
        a2d_status = A2D_ParsSbcInfo(&sbc_cie, (UINT8 *)(p_data->avk_config.codec_info), FALSE);
        if (a2d_status == A2D_SUCCESS) {
            /* Switch to BTIF context */
            config_req.sample_rate = btif_a2dp_get_track_frequency(sbc_cie.samp_freq);
            config_req.channel_count = btif_a2dp_get_track_channel_count(sbc_cie.ch_mode);
            memcpy(config_req.peer_bd,(UINT8*)(p_data->avk_config.bd_addr),
                                                              sizeof(config_req.peer_bd));
            btif_transfer_context(btif_av_handle_event, BTIF_AV_SINK_CONFIG_REQ_EVT,
                                     (char*)&config_req, sizeof(config_req), NULL);
        }
        else
        {
            APPL_TRACE_ERROR("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d", a2d_status);
        }
    }
}
/*******************************************************************************
**
** Function         btif_av_init
**
** Description      Initializes btif AV if not already done
**
** Returns          bt_status_t
**
*******************************************************************************/

bt_status_t btif_av_init(int service_id)
{
    int i;
    if (btif_av_cb[0].sm_handle == NULL)
    {
        BTIF_TRACE_EVENT(" Start Media Task");
        if (!btif_a2dp_start_media_task())
            return BT_STATUS_FAIL;
        btif_av_cb[0].service = service_id;
        btif_enable_service(service_id);

        /* Also initialize the AV state machine */
        for (i = 0; i < btif_max_av_clients; i++)
        {
            btif_av_cb[i].sm_handle = btif_sm_init((const btif_sm_handler_t*)btif_av_state_handlers,
                                                    BTIF_AV_STATE_IDLE, i);
        }
        btif_a2dp_on_init();
    }

    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         init_src
**
** Description      Initializes the AV interface for source mode
**
** Returns          bt_status_t
**
*******************************************************************************/

static bt_status_t init_src(btav_callbacks_t* callbacks, int max_a2dp_connections,
                            int a2dp_multicast_state)
{
    bt_status_t status;

    BTIF_TRACE_EVENT("%s with max conn = %d", __FUNCTION__, max_a2dp_connections);

    if (bt_av_sink_callbacks != NULL) {
        // already did btif_av_init()
        status = BT_STATUS_SUCCESS;
    }
    else
    {
        if (a2dp_multicast_state)
        {
            is_multicast_supported = TRUE;
        }
        btif_max_av_clients = max_a2dp_connections;
        status = btif_av_init(BTA_A2DP_SOURCE_SERVICE_ID);
    }

    if (status == BT_STATUS_SUCCESS) {
        bt_av_src_callbacks = callbacks;
    }

    return status;
}

/*******************************************************************************
**
** Function         init_sink
**
** Description      Initializes the AV interface for sink mode
**
** Returns          bt_status_t
**
*******************************************************************************/

static bt_status_t init_sink(btav_callbacks_t* callbacks, int max,
                             int a2dp_multicast_state)
{
    bt_status_t status;

    BTIF_TRACE_EVENT("%s", __FUNCTION__);

    if (bt_av_src_callbacks != NULL) {
        // already did btif_av_init()
        status = BT_STATUS_SUCCESS;
    }
    else
    {
        enable_multicast = FALSE; // Clear multicast flag for sink
        if (max > 1)
        {
            BTIF_TRACE_ERROR("Only one Sink can be initialized");
            max = 1;
        }
        btif_max_av_clients = max; //Should be 1
        status = btif_av_init(BTA_A2DP_SINK_SERVICE_ID);
    }

    if (status == BT_STATUS_SUCCESS) {
        bt_av_sink_callbacks = callbacks;
        //BTA_AvEnable_Sink(TRUE);
    }

    return status;
}

#ifdef AVK_BACKPORT
/*******************************************************************************
**
** Function         suspend_sink
**
** Description      Suspends stream  in case of A2DP Sink
**
** Returns          None
**
*******************************************************************************/
void suspend_sink()
{
    BTIF_TRACE_DEBUG(" suspend Stream Suspend called");
    btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
}


/*******************************************************************************
**
** Function         resume_sink
**
** Description      Resumes stream  in case of A2DP Sink
**
** Returns          None
**
*******************************************************************************/
void resume_sink()
{
    BTIF_TRACE_DEBUG(" resume Stream called");
    btif_dispatch_sm_event(BTIF_AV_START_STREAM_REQ_EVT, NULL, 0);
}

/*******************************************************************************
**
** Function         audio_focus_status
**
** Description      Update Audio Focus State
**
** Returns          None
**
*******************************************************************************/
static void audio_focus_status(int state)
{
    BTIF_TRACE_DEBUG(" Audio focus Granted  %d ",state);
    btif_a2dp_set_audio_focus_state(state);
}
/*******************************************************************************
**
** Function         btif_av_request_audio_focus
**
** Description      Usend request to gain audio focus
**
** Returns          void
**
*******************************************************************************/
void btif_av_request_audio_focus( BOOLEAN enable)
{
    int i;
    /* If we are in started state, suspend shld not have been initiated */
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if ((btif_av_cb[i].flags & BTIF_AV_FLAG_REMOTE_SUSPEND )||
            (btif_av_cb[i].flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING))
        {
            return;
        }
    }

    if (enable)
    {
        HAL_CBACK(bt_av_sink_callbacks, audio_focus_request_cb,
          1, &(btif_av_cb[0].peer_bda));
    }
}
#endif

void btif_get_latest_playing_device(BD_ADDR address)
{
    int index;
    index = btif_get_latest_playing_device_idx();
    if (index < btif_max_av_clients)
    {
        //copy bdaddrsss
        bdcpy(address, btif_av_cb[index].peer_bda.address);
    }
    else
    {
        bdcpy(address, bd_null);
    }
}

BOOLEAN btif_av_is_device_connected(BD_ADDR address)
{
    btif_sm_state_t state = btif_get_conn_state_of_device(address);

    if ((state == BTIF_AV_STATE_OPENED) ||
        (state == BTIF_AV_STATE_STARTED))
        return TRUE;
    else
        return FALSE;
}

/*This function will trigger remote suspend for currently
* playing device and then initiate START on Handoff device
* whose address is passed as an argument. */
/*******************************************************************************
**
** Function         btif_av_trigger_dual_handoff
**
** Description      Trigger the DUAL HANDOFF
**
** Returns          void
**
*******************************************************************************/

void btif_av_trigger_dual_handoff(BOOLEAN handoff, BD_ADDR address)
{
    int index;
    /*Get the current playing device*/
    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
    index = btif_get_latest_playing_device_idx();
    if (index != btif_max_av_clients)
    {
        btif_av_cb[index].dual_handoff = handoff; /*Initiate Handoff*/
        /*Initiate SUSPEND for this device*/
        BTIF_TRACE_DEBUG("Initiate SUSPEND for this device on index = %d", index);
        btif_sm_dispatch(btif_av_cb[index].sm_handle, BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL);
        //btif_dispatch_sm_event(BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL, 0);
    }
    else
    {
        BTIF_TRACE_ERROR("Handoff on invalid index");
    }
}

/*******************************************************************************
**
** Function         btif_av_trigger_suspend
**
** Description      Trigger suspend when multicast is ongoing for tuch tones
**                  and new ACL is created.
**
** Returns          void
**
*******************************************************************************/

void btif_av_trigger_suspend()
{
    int index;
    /*Get the current playing device*/
    BTIF_TRACE_DEBUG("%s", __FUNCTION__);
    index = btif_get_latest_playing_device_idx();
    if (index <= btif_max_av_clients)
    {
        /*Initiate SUSPEND for this device*/
        BTIF_TRACE_DEBUG("Initiate SUSPEND for this device on index = %d", index);
        btif_sm_dispatch(btif_av_cb[index].sm_handle, BTIF_AV_SUSPEND_STREAM_REQ_EVT, NULL);
    }
    else
    {
        BTIF_TRACE_ERROR("suspend on invalid index");
    }
}

/*******************************************************************************
**
** Function         connect
**
** Description      Establishes the AV signalling channel with the remote headset
**
** Returns          bt_status_t
**
*******************************************************************************/

static bt_status_t connect_int(bt_bdaddr_t *bd_addr, uint16_t uuid)
{
    btif_av_connect_req_t connect_req;
    int i;
    connect_req.target_bda = bd_addr;
    connect_req.uuid = uuid;
    BTIF_TRACE_EVENT("%s", __FUNCTION__);

    for (i = 0; i < btif_max_av_clients;)
    {
        if(btif_av_get_valid_idx(i))
        {
            if (bdcmp(bd_addr->address, btif_av_cb[i].peer_bda.address) == 0)
            {
                BTIF_TRACE_ERROR("Attempting connection for non idle device.. back off ");
                btif_queue_advance();
                return BT_STATUS_SUCCESS;
            }
            i++;
        }
        else
            break;
    }
    if (i == btif_max_av_clients)
    {
        UINT8 rc_handle;
        bdstr_t bdstr;

        BTIF_TRACE_ERROR("%s: All indexes are full", __FUNCTION__);

        /* Multicast: Check if AV slot is available for connection
         * If not available, AV got connected to different devices.
         * Disconnect this RC connection without AV connection.
         */
        rc_handle = btif_rc_get_connected_peer_handle(bd_addr->address);
        if (rc_handle != BTIF_RC_HANDLE_NONE)
        {
            BTIF_TRACE_IMP("Disconnect only AVRC on : %s",
                    bdaddr_to_string (bd_addr, &bdstr, sizeof(bdstr)));
            BTA_AvCloseRc(rc_handle);
        }
        btif_queue_advance();
        return BT_STATUS_FAIL;
    }

    btif_sm_dispatch(btif_av_cb[i].sm_handle, BTIF_AV_CONNECT_REQ_EVT, (char*)&connect_req);


    return BT_STATUS_SUCCESS;
}

static bt_status_t src_connect_sink(bt_bdaddr_t *bd_addr)
{
    BTIF_TRACE_EVENT("%s", __FUNCTION__);
    CHECK_BTAV_INIT();

    return btif_queue_connect(UUID_SERVCLASS_AUDIO_SOURCE, bd_addr, connect_int);
}

static bt_status_t sink_connect_src(bt_bdaddr_t *bd_addr)
{
    BTIF_TRACE_EVENT("%s", __FUNCTION__);
    CHECK_BTAV_INIT();

    return btif_queue_connect(UUID_SERVCLASS_AUDIO_SINK, bd_addr, connect_int);
}

/*******************************************************************************
**
** Function         disconnect
**
** Description      Tears down the AV signalling channel with the remote headset
**
** Returns          bt_status_t
**
*******************************************************************************/
static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
{
    BTIF_TRACE_EVENT("%s", __FUNCTION__);

    CHECK_BTAV_INIT();

    /* Switch to BTIF context */
    return btif_transfer_context(btif_av_handle_event, BTIF_AV_DISCONNECT_REQ_EVT,
                                 (char*)bd_addr, sizeof(bt_bdaddr_t), NULL);
}

/*******************************************************************************
**
** Function         cleanup
**
** Description      Shuts down the AV interface and does the cleanup
**
** Returns          None
**
*******************************************************************************/
static void cleanup(int service_uuid)
{
    int i;
    BTIF_TRACE_IMP("AV %s", __FUNCTION__);

    btif_transfer_context(btif_av_handle_event, BTIF_AV_CLEANUP_REQ_EVT, NULL, 0, NULL);

    btif_disable_service(service_uuid);

    /* Also shut down the AV state machine */
    for (i = 0; i < btif_max_av_clients; i++ )
    {
        btif_sm_shutdown(btif_av_cb[i].sm_handle);
        btif_av_cb[i].sm_handle = NULL;
    }
}

static void cleanup_src(void) {
    BTIF_TRACE_EVENT("%s", __FUNCTION__);

    if (bt_av_src_callbacks)
    {
        bt_av_src_callbacks = NULL;
        if (bt_av_sink_callbacks == NULL)
            cleanup(BTA_A2DP_SOURCE_SERVICE_ID);
    }
}

static void cleanup_sink(void) {
    BTIF_TRACE_EVENT("%s", __FUNCTION__);

    if (bt_av_sink_callbacks)
    {
        bt_av_sink_callbacks = NULL;
        if (bt_av_src_callbacks == NULL)
            cleanup(BTA_A2DP_SINK_SERVICE_ID);
    }
}

static void allow_connection(int is_valid, bt_bdaddr_t *bd_addr)
{
    int index = 0;
    BTIF_TRACE_DEBUG(" %s isValid is %d event %d", __FUNCTION__,is_valid,idle_rc_event);
    switch (idle_rc_event)
    {
        case BTA_AV_RC_OPEN_EVT:
            if (is_valid)
            {
                BTIF_TRACE_DEBUG("allowconn for RC connection");
                memset(&tle_av_open_on_rc, 0, sizeof(tle_av_open_on_rc));
                tle_av_open_on_rc.param = (UINT32)btif_initiate_av_open_tmr_hdlr;
                btu_start_timer(&tle_av_open_on_rc, BTU_TTYPE_USER_FUNC,
                        BTIF_TIMEOUT_AV_OPEN_ON_RC_SECS);
                btif_rc_handler(idle_rc_event, &idle_rc_data);
            }
            else
            {
                UINT8 rc_handle =  idle_rc_data.rc_open.rc_handle;
                BTA_AvCloseRc(rc_handle);
            }
            break;

        case BTA_AV_PENDING_EVT:
            if (is_valid)
            {
                index = btif_av_idx_by_bdaddr(bd_addr->address);
                if (index >= btif_max_av_clients)
                {
                    BTIF_TRACE_DEBUG("Invalid index for device");
                    break;
                }
                BTIF_TRACE_DEBUG("The connection is allowed for the device at index = %d", index);
                BTA_AvOpen(btif_av_cb[index].peer_bda.address, btif_av_cb[index].bta_handle,
                       TRUE, BTA_SEC_NONE, UUID_SERVCLASS_AUDIO_SOURCE);
            }
            else
            {
                BTA_AvDisconnect(idle_rc_data.pend.bd_addr);
            }
            break;

        default:
            BTIF_TRACE_DEBUG("%s : unhandled event:%s", __FUNCTION__,
                                dump_av_sm_event_name(idle_rc_event));
    }
    idle_rc_event = 0;
    memset(&idle_rc_data, 0, sizeof(tBTA_AV));
}

static const btav_interface_t bt_av_src_interface = {
    sizeof(btav_interface_t),
    init_src,
    src_connect_sink,
    disconnect,
    cleanup_src,
    allow_connection,
};

static const btav_interface_t bt_av_sink_interface = {
    sizeof(btav_interface_t),
    init_sink,
    sink_connect_src,
    disconnect,
    cleanup_sink,
};

/*******************************************************************************
**
** Function         btif_av_get_sm_handle
**
** Description      Fetches current av SM handle
**
** Returns          None
**
*******************************************************************************/
/* Media task uses this info
* But dont use it. */
btif_sm_handle_t btif_av_get_sm_handle(void)
{
    return btif_av_cb[0].sm_handle;
}

/*******************************************************************************
**
** Function         btif_av_get_addr
**
** Description      Fetches current AV BD address
**
** Returns          BD address
**
*******************************************************************************/

bt_bdaddr_t btif_av_get_addr(BD_ADDR address)
{
    int i;
    bt_bdaddr_t not_found ;
    memset (&not_found, 0, sizeof(bt_bdaddr_t));
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if (bdcmp(btif_av_cb[i].peer_bda.address, address) == 0)
            return btif_av_cb[i].peer_bda;
    }
    return not_found;
}

/*******************************************************************************
**
** Function         btif_av_stream_ready
**
** Description      Checks whether AV is ready for starting a stream
**
** Returns          None
**
*******************************************************************************/

BOOLEAN btif_av_stream_ready(void)
{
    int i;
    BOOLEAN status = FALSE;
    /* also make sure main adapter is enabled */
    if (btif_is_enabled() == 0)
    {
        BTIF_TRACE_EVENT("main adapter not enabled");
        return FALSE;
    }

    for (i = 0; i < btif_max_av_clients; i++)
    {
        btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
        /* Multicast:
         * If any of the stream is in pending suspend state when
         * we initiate start, it will result in inconsistent behavior
         * Check the pending SUSPEND flag and return failure
         * if suspend is in progress.
         */
        if (btif_av_cb[i].dual_handoff ||
            (btif_av_cb[i].flags & BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING))
        {
            status = FALSE;
            break;
        }
        else if (btif_av_cb[i].flags &
            (BTIF_AV_FLAG_REMOTE_SUSPEND|BTIF_AV_FLAG_PENDING_STOP))
        {
            status = FALSE;
            break;
        }
        else if (btif_av_cb[i].state == BTIF_AV_STATE_OPENED)
        {
            status = TRUE;
        }
    }
    BTIF_TRACE_DEBUG("btif_av_stream_ready: %d", status);
    return status;
}

/*******************************************************************************
**
** Function         btif_av_stream_started_ready
**
** Description      Checks whether AV ready for media start in streaming state
**
** Returns          None
**
*******************************************************************************/

BOOLEAN btif_av_stream_started_ready(void)
{
    int i;
    BOOLEAN status = FALSE;

    for (i = 0; i < btif_max_av_clients; i++)
    {
        btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
        if (btif_av_cb[i].dual_handoff)
        {
            BTIF_TRACE_ERROR("%s: Under Dual handoff ",__FUNCTION__ );
            status = FALSE;
            break;
        } else if (btif_av_cb[i].flags &
            (BTIF_AV_FLAG_LOCAL_SUSPEND_PENDING |
            BTIF_AV_FLAG_REMOTE_SUSPEND |
            BTIF_AV_FLAG_PENDING_STOP))
        {
            status = FALSE;
            break;
        } else if (btif_av_cb[i].state == BTIF_AV_STATE_STARTED)
        {
            status = TRUE;
        }
    }
    BTIF_TRACE_DEBUG("btif_av_stream_started_ready: %d", status);
    return status;
}

/*******************************************************************************
**
** Function         btif_dispatch_sm_event
**
** Description      Send event to AV statemachine
**
** Returns          None
**
*******************************************************************************/

/* used to pass events to AV statemachine from other tasks */
void btif_dispatch_sm_event(btif_av_sm_event_t event, void *p_data, int len)
{
    /* Switch to BTIF context */
    btif_transfer_context(btif_av_handle_event, event,
                          (char*)p_data, len, NULL);
}

/*******************************************************************************
**
** Function         btif_av_execute_service
**
** Description      Initializes/Shuts down the service
**
** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
**
*******************************************************************************/
bt_status_t btif_av_execute_service(BOOLEAN b_enable)
{
    int i;
    if (b_enable)
    {
        /* TODO: Removed BTA_SEC_AUTHORIZE since the Java/App does not
        * handle this request in order to allow incoming connections to succeed.
        * We need to put this back once support for this is added */

         /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
          * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
          * be initiated by the app/audioflinger layers */
         /* Support for browsing for SDP record should work only if we enable BROWSE
          * while registering. */
#if (AVRC_METADATA_INCLUDED == TRUE)
        BTA_AvEnable(BTA_SEC_AUTHENTICATE,
            BTA_AV_FEAT_RCTG|BTA_AV_FEAT_METADATA|BTA_AV_FEAT_VENDOR|BTA_AV_FEAT_NO_SCO_SSPD
            |BTA_AV_FEAT_ACP_START
#if (AVRC_ADV_CTRL_INCLUDED == TRUE)
            |BTA_AV_FEAT_RCCT
            |BTA_AV_FEAT_ADV_CTRL
			|BTA_AV_FEAT_BROWSE
#endif
            ,bte_av_callback);
#else
        BTA_AvEnable(BTA_SEC_AUTHENTICATE, (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_NO_SCO_SSPD
            |BTA_AV_FEAT_ACP_START), bte_av_callback);
#endif
        for (i = 0; i < btif_max_av_clients; i++)
        {
            BTIF_TRACE_DEBUG("%s: BTA_AvRegister : %d", __FUNCTION__, i);
            BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AV_SERVICE_NAME, 0, bte_av_media_callback,
            UUID_SERVCLASS_AUDIO_SOURCE);
        }
    }
    else
    {
        for (i = 0; i < btif_max_av_clients; i++)
        {
            BTA_AvDeregister(btif_av_cb[i].bta_handle);
        }
        BTA_AvDisable();
    }
    return BT_STATUS_SUCCESS;
}

/*******************************************************************************
**
** Function         btif_av_sink_execute_service
**
** Description      Initializes/Shuts down the service
**
** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
**
*******************************************************************************/
bt_status_t btif_av_sink_execute_service(BOOLEAN b_enable)
{
     if (b_enable)
     {
         /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
          * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
          * be initiated by the app/audioflinger layers */
         BTA_AvEnable(BTA_SEC_AUTHENTICATE, BTA_AV_FEAT_NO_SCO_SSPD|BTA_AV_FEAT_RCCT|
                                            BTA_AV_FEAT_METADATA|BTA_AV_FEAT_VENDOR|
                                            BTA_AV_FEAT_ADV_CTRL|BTA_AV_FEAT_RCTG,
                                                                        bte_av_callback);
         BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTIF_AVK_SERVICE_NAME, 0, bte_av_media_callback,
                                                                UUID_SERVCLASS_AUDIO_SINK);
     }
     else {
         BTA_AvDeregister(btif_av_cb[0].bta_handle);
         BTA_AvDisable();
     }
     return BT_STATUS_SUCCESS;
}
/*******************************************************************************
**
** Function         btif_av_get_src_interface
**
** Description      Get the AV callback interface for A2DP source profile
**
** Returns          btav_interface_t
**
*******************************************************************************/
const btav_interface_t *btif_av_get_src_interface(void)
{
    BTIF_TRACE_EVENT("%s", __FUNCTION__);
    return &bt_av_src_interface;
}

/*******************************************************************************
**
** Function         btif_av_get_sink_interface
**
** Description      Get the AV callback interface for A2DP sink profile
**
** Returns          btav_interface_t
**
*******************************************************************************/
const btav_interface_t *btif_av_get_sink_interface(void)
{
    BTIF_TRACE_EVENT("%s", __FUNCTION__);
    return &bt_av_sink_interface;
}

/*******************************************************************************
**
** Function         btif_av_is_connected
**
** Description      Checks if av has a connected sink
**
** Returns          BOOLEAN
**
*******************************************************************************/
BOOLEAN btif_av_is_connected(void)
{
    int i;
    BOOLEAN status = FALSE;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
        if ((btif_av_cb[i].state == BTIF_AV_STATE_OPENED) ||
            (btif_av_cb[i].state ==  BTIF_AV_STATE_STARTED))
            status = TRUE;
    }
    return status;
}

/*******************************************************************************
**
** Function         btif_av_is_connected_on_other_idx
**
** Description      Checks if any other AV SCB is connected
**
** Returns          BOOLEAN
**
*******************************************************************************/

BOOLEAN btif_av_is_connected_on_other_idx(int current_index)
{
    //return true if other IDx is connected
    btif_sm_state_t state = BTIF_AV_STATE_IDLE;
    int i;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if (i != current_index)
        {
            state = btif_sm_get_state(btif_av_cb[i].sm_handle);
            if ((state == BTIF_AV_STATE_OPENED) ||
                (state == BTIF_AV_STATE_STARTED))
                return TRUE;
        }
    }
    return FALSE;
}

/*******************************************************************************
**
** Function         btif_av_is_playing_on_other_idx
**
** Description      Checks if any other AV SCB is connected
**
** Returns          BOOLEAN
**
*******************************************************************************/

BOOLEAN btif_av_is_playing_on_other_idx(int current_index)
{
    //return true if other IDx is playing
    btif_sm_state_t state = BTIF_AV_STATE_IDLE;
    int i;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if (i != current_index)
        {
            state = btif_sm_get_state(btif_av_cb[i].sm_handle);
            if (state == BTIF_AV_STATE_STARTED)
                return TRUE;
        }
    }
    return FALSE;
}

/*******************************************************************************
**
** Function         btif_av_update_current_playing_device
**
** Description      Update the next connected device as playing
**
** Returns          void
**
*******************************************************************************/

static void btif_av_update_current_playing_device(int index)
{
    int i;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if (i != index)
            btif_av_cb[i].current_playing = TRUE;
    }
}

/*******************************************************************************
**
** Function         btif_av_is_peer_edr
**
** Description      Check if the connected a2dp device supports
**                  EDR or not. Only when connected this function
**                  will accurately provide a true capability of
**                  remote peer. If not connected it will always be false.
**
** Returns          TRUE if remote device is capable of EDR
**
*******************************************************************************/
BOOLEAN btif_av_is_peer_edr(void)
{
    int index = 0;
    btif_sm_state_t state;
    BOOLEAN peer_edr = FALSE;

    ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0);

    /* If any of the remote in streaming state is BR
     * return FALSE to ensure proper configuration
     * is used. Ideally, since multicast is not supported
     * if any of the connected device is BR device,
     * we should not see both devices in START state.
     */
    for (index; index < btif_max_av_clients; index ++)
    {
        state = btif_sm_get_state(btif_av_cb[index].sm_handle);
        if ((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START)
            || (state == BTIF_AV_STATE_STARTED))
        {
            if (btif_av_cb[index].edr)
            {
                peer_edr = TRUE;
            }
            else
            {
                return FALSE;
            }
        }
    }
    return peer_edr;
}

/*******************************************************************************
**
** Function         btif_av_any_br_peer
**
** Description      Check if the any of connected devices is BR device.
**
** Returns          TRUE if connected to any BR device, FALSE otherwise.
**
*******************************************************************************/
BOOLEAN btif_av_any_br_peer(void)
{
    int index = 0;
    btif_sm_state_t state;
    bdstr_t addr_string;

    for (index; index < btif_max_av_clients; index ++)
    {
        state = btif_sm_get_state(btif_av_cb[index].sm_handle);
        if (state >= BTIF_AV_STATE_OPENED)
        {
            if (!btif_av_cb[index].edr)
            {
                BTIF_TRACE_WARNING("%s : Connected to BR device : %s",
                    __FUNCTION__, bdaddr_to_string(&btif_av_cb[index].peer_bda,
                    &addr_string), sizeof(addr_string));
                return TRUE;
            }
        }
    }
    return FALSE;
}

/*******************************************************************************
**
** Function         btif_av_peer_supports_3mbps
**
** Description      check if the connected a2dp device supports
**                  3mbps edr. Only when connected this function
**                  will accurately provide a true capability of
**                  remote peer. If not connected it will always be false.
**
** Returns          TRUE if remote device is EDR and supports 3mbps
**
*******************************************************************************/
BOOLEAN btif_av_peer_supports_3mbps(void)
{
    int index =0;
    btif_sm_state_t state;
    ASSERTC(btif_av_is_connected(), "No active a2dp connection", 0);

    for (index; index < btif_max_av_clients; index ++)
    {
        state = btif_sm_get_state(btif_av_cb[index].sm_handle);
        if ((btif_av_cb[index].flags & BTIF_AV_FLAG_PENDING_START)
            || (state == BTIF_AV_STATE_STARTED))
        {
            if(btif_av_cb[index].edr_3mbps)
                return TRUE;
        }
    }
    return FALSE;
}

/******************************************************************************
**
** Function        btif_av_clear_remote_suspend_flag
**
** Description     Clears btif_av_cd.flags if BTIF_AV_FLAG_REMOTE_SUSPEND is set
**
** Returns         void
******************************************************************************/
void btif_av_clear_remote_suspend_flag(void)
{
    int i;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        BTIF_TRACE_DEBUG(" flag :%x",btif_av_cb[i].flags);
        btif_av_cb[i].flags  &= ~BTIF_AV_FLAG_REMOTE_SUSPEND;
    }
}

/*******************************************************************************
**
** Function         btif_av_move_idle
**
** Description      Opening state is intermediate state. It cannot handle
**                  incoming/outgoing connect/disconnect requests.When ACL
**                  is disconnected and we are in opening state then move back
**                  to idle state which is proper to handle connections.
**
** Returns          Void
**
*******************************************************************************/
void btif_av_move_idle(bt_bdaddr_t bd_addr)
{
    int index =0;
    /* inform the application that ACL is disconnected and move to idle state */
    index = btif_av_idx_by_bdaddr(bd_addr.address);
    if (index == btif_max_av_clients)
    {
        BTIF_TRACE_DEBUG("btif_av_move_idle: Already in IDLE");
        return;
    }
    btif_sm_state_t state = btif_sm_get_state(btif_av_cb[index].sm_handle);
    BTIF_TRACE_DEBUG("ACL Disconnected state %d  is same device %d",state,
            memcmp (&bd_addr, &(btif_av_cb[index].peer_bda), sizeof(bd_addr)));
    if (state == BTIF_AV_STATE_OPENING &&
            (memcmp (&bd_addr, &(btif_av_cb[index].peer_bda), sizeof(bd_addr)) == 0))
    {
        BTIF_TRACE_DEBUG("Moving State from Opening to Idle due to ACL disconnect");
        btif_report_connection_state(BTAV_CONNECTION_STATE_DISCONNECTED, &(btif_av_cb[index].peer_bda));

        btif_sm_change_state(btif_av_cb[index].sm_handle, BTIF_AV_STATE_IDLE);
        btif_queue_advance();
    }
}
/******************************************************************************
**
** Function        btif_av_get_num_playing_devices
**
** Description     Return number of A2dp playing devices
**
** Returns         int
******************************************************************************/
UINT16 btif_av_get_num_playing_devices(void)
{
    UINT16 i;
    UINT16 playing_devices = 0;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
        if (btif_av_cb[i].state ==  BTIF_AV_STATE_STARTED)
        {
            playing_devices++;
        }
    }
    BTIF_TRACE_DEBUG("AV devices playing: %d", playing_devices);

    return playing_devices;
}

/******************************************************************************
**
** Function        btif_av_get_num_connected_devices
**
** Description     Return number of A2dp connected devices
**
** Returns         int
******************************************************************************/
UINT16 btif_av_get_num_connected_devices(void)
{
    UINT16 i;
    UINT16 connected_devies = 0;
    for (i = 0; i < btif_max_av_clients; i++)
    {
        btif_av_cb[i].state = btif_sm_get_state(btif_av_cb[i].sm_handle);
        if ((btif_av_cb[i].state == BTIF_AV_STATE_OPENED) ||
            (btif_av_cb[i].state ==  BTIF_AV_STATE_STARTED))
        {
            connected_devies++;
        }
    }
    BTIF_TRACE_DEBUG("AV Connection count: %d", connected_devies);

    return connected_devies;
}

/******************************************************************************
**
** Function        btif_av_update_multicast_state
**
** Description     Enable Multicast only if below conditions are satisfied
**                 1. Connected to only 2 EDR HS.
**                 2. Connected to both HS as master.
**                 3. Connected to 2 EDR HS and one BLE device
**                 Multicast will fall back to soft handsoff in below conditions
**                 1. Number of ACL links is more than 2,like connected to HID
**                    initiating connection for HS1 and HS2.
**                 2. Connected to BR and EDR HS.
**                 3. Connected to more then 1 BLE device
**
** Returns         void
******************************************************************************/
void btif_av_update_multicast_state(int index)
{
    UINT16 num_connected_br_edr_devices = 0;
    UINT16 num_connected_le_devices = 0;
    UINT16 num_av_connected = 0;
    UINT16 i = 0;
    BOOLEAN is_slave = FALSE;
    BOOLEAN is_br_hs_connected = FALSE;
    BOOLEAN prev_multicast_state = enable_multicast;
    bdstr_t addr_string;

    if (!is_multicast_supported)
    {
        BTIF_TRACE_DEBUG("%s Multicast is Disabled", __FUNCTION__);
        return;
    }

    if (multicast_disabled == TRUE)
    {
        multicast_disabled = FALSE;
        enable_multicast = FALSE;
        BTA_AvEnableMultiCast(FALSE, btif_av_cb[index].bta_handle);
        return;
    }

    BTIF_TRACE_DEBUG("%s Multicast previous state : %s", __FUNCTION__,
        enable_multicast ? "Enabled" : "Disabled" );

    num_connected_br_edr_devices = btif_dm_get_br_edr_links();
    num_connected_le_devices = btif_dm_get_le_links();
    num_av_connected = btif_av_get_num_connected_devices();
    is_br_hs_connected = btif_av_any_br_peer();

    for (i = 0; i < btif_max_av_clients; i++)
    {
        if (btif_av_cb[i].is_slave == TRUE)
        {
            BTIF_TRACE_WARNING("Conected as slave to : %s",
                bdaddr_to_string(&btif_av_cb[i].peer_bda, &addr_string,
                        sizeof(addr_string)));
            is_slave = TRUE;
            break;
        }
    }

    if ((num_av_connected <= 2) && (is_br_hs_connected != TRUE) &&
        (is_slave == FALSE) && ((num_connected_br_edr_devices <= 2) &&
        (num_connected_le_devices <= 1)))
    {
        enable_multicast = TRUE;
    }
    else
    {
        enable_multicast = FALSE;
    }

    BTIF_TRACE_DEBUG("%s Multicast current state : %s", __FUNCTION__,
        enable_multicast ? "Enabled" : "Disabled" );

    if (prev_multicast_state != enable_multicast)
    {
        BTA_AvEnableMultiCast(enable_multicast,
                btif_av_cb[index].bta_handle);
        HAL_CBACK(bt_av_src_callbacks, multicast_state_cb,
              enable_multicast);
    }
}
/******************************************************************************
**
** Function        btif_av_get_multicast_state
**
** Description     Returns TRUE if multicast is enabled else false
**
** Returns         BOOLEAN
******************************************************************************/
BOOLEAN btif_av_get_multicast_state()
{
    return enable_multicast;
}
/******************************************************************************
**
** Function        btif_av_get_ongoing_multicast
**
** Description     Returns TRUE if multicast is ongoing
**
** Returns         BOOLEAN
******************************************************************************/
BOOLEAN btif_av_get_ongoing_multicast()
{
    int i = 0, j = 0;
    if (!is_multicast_supported)
    {
        BTIF_TRACE_DEBUG("Multicast is Disabled");
        return FALSE;
    }
    for (i = 0; i < btif_max_av_clients; i++)
    {
        if (btif_av_cb[i].is_device_playing)
        {
            j++;
        }
    }
    if (j == btif_max_av_clients)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}