summaryrefslogtreecommitdiff
path: root/qahw_api/test/qap_wrapper_extn.c
blob: b3da21abae54d022062234edbe287f27dd74f57a (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
/*
 * Copyright (c) 2016-2017,2019 The Linux Foundation. All rights reserved.
 * Not a Contribution.
 *
 * Copyright (C) 2015 The Android Open Source Project *
 * 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.
 */

/* Test app extension to exercise QAP (Non-tunnel Decode) */

#include <ctype.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <cutils/properties.h>
#include <cutils/list.h>
#include <cutils/str_parms.h>
#include <log/log.h>
#include <system/audio.h>
#include <qap_api.h>
#include <qti_audio.h>
#include "qahw_playback_test.h"
#include <dolby_ms12.h>

#undef LOG_TAG
#define LOG_TAG "HAL_TEST"
#undef LOG_NDEBUG
/*#define LOG_NDEBUG 0*/

#if LINUX_ENABLED
#if defined(__LP64__)
#define QAC_LIB_MS12 "/usr/lib64/libdolby_ms12_wrapper.so"
#else
#define QAC_LIB_MS12 "/usr/lib/libdolby_ms12_wrapper.so"
#endif
#define QAC_LIB_M8   "libdts_m8_wrapper.so"
#else
#define QAC_LIB_MS12 "/system/lib/libdolby_ms12_wrapper.so"
#define QAC_LIB_M8   "/system/lib/libdts_m8_wrapper.so"
#endif

#define SESSION_BLURAY   1
#define SESSION_BROADCAST 2
#define MAX_OUTPUT_CHANNELS 8
#define FRAME_SIZE 32768 /* 32k size */
#define MAX_BUFFER_SIZE 32768 /* 32k size */
#define CONTIGUOUS_TIMESTAMP 0x7fffffff
#define TIMESTAMP_ARRAY_SIZE 2048
#define DOLBY 1
#define DTS   2
#define FRAME_SIZE_FOR_2CH_PCM 6144 /* For 48k samplerate, 2 ch, 2 bytes */
#define PCM_16_BITWIDTH 16
#define PCM_24_BITWIDTH 24
#define DEFAULT_SAMPLE_RATE 48000
#define MAX_QAP_MODULE_OUT 3

extern bool stop_playback;
bool is_media_fmt_changed[MAX_QAP_MODULE_OUT];
int new_output_conf_index = 0;

qap_lib_handle_t ms12_lib_handle = NULL;
qap_lib_handle_t m8_lib_handle = NULL;
qap_session_handle_t qap_session_handle = NULL;
qahw_module_handle_t *qap_out_hal_handle = NULL;
qahw_module_handle_t *qap_out_spk_handle = NULL;
qahw_module_handle_t *qap_out_hdmi_handle = NULL;
qahw_module_handle_t *qap_out_hp_handle = NULL;

audio_io_handle_t qap_stream_out_spk_handle = 0x999;
audio_io_handle_t qap_stream_out_hdmi_handle = 0x998;
audio_io_handle_t qap_stream_out_hp_handle = 0x997;
audio_io_handle_t qap_stream_out_cmpr_handle = 0x996;

FILE *fp_output_writer_spk = NULL;
FILE *fp_output_writer_hp = NULL;
FILE *fp_output_writer_hdmi = NULL;
FILE *fp_output_timestamp_file = NULL;
FILE *fp_ecref = NULL;
unsigned char data_buf[MAX_BUFFER_SIZE];
uint32_t output_device_id = 0;
uint16_t input_streams_count = 0;

bool hdmi_connected = false;
bool play_through_bt = false;
bool encode = false;
bool dolby_formats = false;
bool timestamp_mode = false;
int  data_write_count = 0;
int data_callback_count = 0;
bool play_list = false;
int play_list_cnt = 0;
uint8_t session_type = SESSION_BLURAY;

pthread_t main_input_thread;
pthread_attr_t main_input_thrd_attr;
pthread_cond_t main_eos_cond;
pthread_mutex_t main_eos_lock;
pthread_cond_t sec_eos_cond;
pthread_mutex_t sec_eos_lock;
pthread_cond_t main2_eos_cond;
pthread_mutex_t main2_eos_lock;
bool main_eos_received = false;
bool main2_eos_received = false;
bool sec_eos_received = false;

dlb_ms12_session_param_t dlb_param;
dlb_ms12_session_param_t dlb_param_hp;
qap_session_outputs_config_t session_output_config;
bool session_output_configured = false;
struct timeval tcold_start, tcold_stop;
struct timeval tcont_ts1, tcont_ts2;
double cold_start, cold_stop;
long int data_callback_ts_arr[TIMESTAMP_ARRAY_SIZE];
long int data_input_ts_arr[TIMESTAMP_ARRAY_SIZE];
double data_input_st_arr[TIMESTAMP_ARRAY_SIZE];
double data_callback_st_arr[TIMESTAMP_ARRAY_SIZE];
bool has_system_input = false;
char session_kv_pairs[256];
bool primary_stream_close = false;
int8_t stream_cnt = 0;
uint32_t dsp_latency = 0;

static int get_qap_session_out_config_index_for_id(uint32_t out_id)
{
    int index = -1, i;

    for (i = 0; i < MAX_QAP_MODULE_OUT; i++)
        if (session_output_config.output_config[i].id == out_id)
            index = i;

    return index;
}

static void set_qahw_stream_channel_map(qahw_stream_handle_t *out_handle, qap_output_config_t *qap_config)
{
    struct qahw_out_channel_map_param chmap_param = {0};
    int i = 0;
    if (qap_config == NULL || out_handle == NULL) {
        return;
    }
    chmap_param.channels = qap_config->channels;
    for (i = 0; i < chmap_param.channels && i < AUDIO_CHANNEL_COUNT_MAX && i < QAP_AUDIO_MAX_CHANNELS;
            i++) {
        chmap_param.channel_map[i] = qap_config->ch_map[i];
    }
    qahw_out_set_param_data(out_handle, QAHW_PARAM_OUT_CHANNEL_MAP, (qahw_param_payload *) &chmap_param);
}

static void update_combo_dev_kvpairs()
{
    bool enable_spk = false;
    bool enable_hp = false;
    bool enable_hdmi = false;
    bool combo_enabled = false;
    char dev_kv_pair[16] = {0};

    ALOGV("%s:%d output device id %d", __func__, __LINE__, output_device_id);

    if (output_device_id & AUDIO_DEVICE_OUT_HDMI)
        enable_hdmi = true;
    if (output_device_id & AUDIO_DEVICE_OUT_WIRED_HEADPHONE)
        enable_hp = true;
    if (output_device_id & AUDIO_DEVICE_OUT_SPEAKER)
        enable_spk = true;

    // Update the kv_pair based on the output device selsction
    // To select hdmi, spekaer and headphone: o_device=1,2,8
    // To select hdmi, headphone: o_device=2,8
    // To select hdmi, spekaer : o_device=1,8
    // To select spekaer and headphone: o_device=1,2
    if (enable_hdmi && enable_hp && enable_spk) {
        sprintf(dev_kv_pair, "o_device=1,2,8");
        combo_enabled = true;
    } else if (enable_hdmi && enable_hp) {
        sprintf(dev_kv_pair, "o_device=2,8");
        combo_enabled = true;
    } else if (enable_hdmi && enable_spk) {
        sprintf(dev_kv_pair, "o_device=1,8");
        combo_enabled = true;
    } else if (enable_hp && enable_spk) {
        sprintf(dev_kv_pair, "o_device=1,2");
        combo_enabled = true;
    }

    if (combo_enabled)
        strcat(session_kv_pairs, dev_kv_pair);

    ALOGV("%s:%d session set param %s and combo_enabled %d", __func__, __LINE__, session_kv_pairs, combo_enabled);
    return;
}
static void update_session_outputs_config(int hdmi_render_format, int in_channels, int bitwidth, int smpl_rate)
{
    bool enable_spk = false;
    bool enable_hp = false;
    bool enable_hdmi = false;
    bool combo_enabled = false;
    char dev_kv_pair[16] = {0};
    bool enable_ecref = false;

    ALOGV("%s:%d output device id %d render format = %d", __func__, __LINE__, output_device_id, hdmi_render_format);

    if (output_device_id & AUDIO_DEVICE_OUT_HDMI)
        enable_hdmi = true;
    if (output_device_id & AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
        output_device_id & AUDIO_DEVICE_OUT_LINE)
        enable_hp = true;
    if (output_device_id & AUDIO_DEVICE_OUT_SPEAKER)
        enable_spk = true;
    if (ec_ref)
        enable_ecref = true;

    if (enable_hdmi) {
        session_output_config.output_config[session_output_config.num_output].id = AUDIO_DEVICE_OUT_HDMI;
        if (hdmi_render_format == 1) {
            session_output_config.output_config[session_output_config.num_output].id = AUDIO_DEVICE_OUT_HDMI|AUDIO_FORMAT_AC3;
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_AC3;
        } else if (hdmi_render_format == 2) {
            session_output_config.output_config[session_output_config.num_output].id = AUDIO_DEVICE_OUT_HDMI|AUDIO_FORMAT_E_AC3;
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_EAC3;
        } else if (hdmi_render_format == 3) {
            session_output_config.output_config[session_output_config.num_output].id = AUDIO_DEVICE_OUT_HDMI|AUDIO_FORMAT_DTS;
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_DTS;
        } else {
            if (bitwidth == PCM_24_BITWIDTH) {
                session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_PCM_24_BIT_PACKED;
                session_output_config.output_config[session_output_config.num_output].bit_width = PCM_24_BITWIDTH;
            } else {
                session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_PCM_16_BIT;
                session_output_config.output_config[session_output_config.num_output].bit_width = PCM_16_BITWIDTH;
            }
        }
        session_output_config.output_config[session_output_config.num_output].channels = in_channels;
        session_output_config.output_config[session_output_config.num_output].sample_rate = smpl_rate;
        session_output_config.num_output++;
    }

    if (enable_hp) {
        session_output_config.output_config[session_output_config.num_output].id = AUDIO_DEVICE_OUT_LINE;
        session_output_config.output_config[session_output_config.num_output].channels = popcount(AUDIO_CHANNEL_OUT_STEREO);
        session_output_config.output_config[session_output_config.num_output].sample_rate = smpl_rate;
        if (bitwidth == PCM_24_BITWIDTH) {
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_PCM_24_BIT_PACKED;
            session_output_config.output_config[session_output_config.num_output].bit_width = PCM_24_BITWIDTH;
        } else {
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_PCM_16_BIT;
            session_output_config.output_config[session_output_config.num_output].bit_width = PCM_16_BITWIDTH;
        }
        session_output_config.num_output++;
    }
    if (enable_spk) {
        session_output_config.output_config[session_output_config.num_output].channels = popcount(AUDIO_CHANNEL_OUT_STEREO);
        session_output_config.output_config[session_output_config.num_output].id = AUDIO_DEVICE_OUT_SPEAKER;
        session_output_config.output_config[session_output_config.num_output].sample_rate = smpl_rate;
        if (bitwidth == PCM_24_BITWIDTH) {
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_PCM_24_BIT_PACKED;
            session_output_config.output_config[session_output_config.num_output].bit_width = PCM_24_BITWIDTH;
        } else {
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_PCM_16_BIT;
            session_output_config.output_config[session_output_config.num_output].bit_width = PCM_16_BITWIDTH;
        }
        session_output_config.num_output++;
    }

    if (enable_ecref) {
        session_output_config.output_config[session_output_config.num_output].channels = popcount(AUDIO_CHANNEL_OUT_STEREO);
        session_output_config.output_config[session_output_config.num_output].id = AUDIO_DEVICE_OUT_PROXY;
        session_output_config.output_config[session_output_config.num_output].sample_rate = smpl_rate;
        if (bitwidth == PCM_24_BITWIDTH) {
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_PCM_24_BIT_PACKED;
            session_output_config.output_config[session_output_config.num_output].bit_width = PCM_24_BITWIDTH;
        } else {
            session_output_config.output_config[session_output_config.num_output].format = QAP_AUDIO_FORMAT_PCM_16_BIT;
            session_output_config.output_config[session_output_config.num_output].bit_width = PCM_16_BITWIDTH;
        }
        session_output_config.num_output++;
    }

    ALOGV("%s:%d num_output = %d", __func__, __LINE__, session_output_config.num_output);
    return;
}

static void update_kvpairs_for_encode(int out_device_id) {
    uint8_t device_id;
    char command[64];
    if (out_device_id == AUDIO_DEVICE_OUT_HDMI)
        device_id = 8;
    switch (render_format) {
        case 1:
            sprintf(command, "o_device=%d;od=;", device_id);
            strcat(session_kv_pairs, command);
            encode = true;
            break;
        case 2:
            sprintf(command, "o_device=%d;odp=;", device_id);
            strcat(session_kv_pairs, command);
            encode = true;
            break;
        case 3:
            sprintf(command, "o_device=%d;render_format=odts;enabletransencode;", device_id);
            strcat(session_kv_pairs, command);
            encode = true;
            break;
        default:
            encode = false;
            break;
    }
    ALOGV("%s::%d output device %d and session set params %s", __func__, __LINE__, out_device_id, session_kv_pairs);
    return;
}

static void qap_wrapper_create_multi_channel_dump(char *path) {
    fp_output_writer_hdmi = fopen(path,"wb");
    if (fp_output_writer_hdmi)
        fprintf(stdout, "output file ::%s has been generated.\n", path);
    else
        fprintf(stderr, "Failed open hdmi dump file\n");
}

/*
 * cold_time_latency is the time difference between the session open
 * and the first data output received from decoder.
 *
 * cont_time_latency is the avg time taken to decode the input buffers
 * i.e. avg of the time differences between the time the input buffer feeded
 * and the time the o/p buffer received
 */
static void qap_wrapper_measure_kpi_values(double cold_start, double cold_stop)
{
    int i=0, m=0, j=0;
    int cnt = 0;
    double syst_time_arr[TIMESTAMP_ARRAY_SIZE];
    double total_lat = 0;
    double cold_time_latency, cont_time_latency;
    cold_time_latency = cold_stop - cold_start;
    memset(syst_time_arr,0, sizeof(syst_time_arr));

    if (data_write_count > TIMESTAMP_ARRAY_SIZE)
        data_write_count = TIMESTAMP_ARRAY_SIZE;
    if (data_callback_count > TIMESTAMP_ARRAY_SIZE)
        data_callback_count = TIMESTAMP_ARRAY_SIZE;

    for (i = 6; i < data_write_count; i++) {
        for(j = 6; j < ((data_callback_count < TIMESTAMP_ARRAY_SIZE) ? data_callback_count : TIMESTAMP_ARRAY_SIZE); j++) {
            if( abs(data_input_ts_arr[i] - data_callback_ts_arr[j]) <= 4000) {
                syst_time_arr[cnt++] = (data_callback_st_arr[j] - data_input_st_arr[i]);
            }
        }
    }

    for(m = 0; m < cnt; m++) {
        total_lat += syst_time_arr[m];
        ALOGV("%d system time diff %lf", __LINE__, syst_time_arr[m]);
    }
    cont_time_latency = total_lat/(cnt);
    fprintf(stdout, "cold time latency %lf ms, avg cont time latency %lf ms,"
            "total cont time latency %f ms, total count %d\n",
            cold_time_latency, cont_time_latency, total_lat, cnt);
    if (dsp_latency)
        fprintf(stdout, "Dsp latency = %lu ms \n", dsp_latency);
}

static void qap_wrapper_read_frame_size_from_file(qap_audio_buffer_t *buffer, FILE *fp_framesize)
{
    if (NULL != fp_framesize) {
        char tempstr[100];
        fgets(tempstr, sizeof(tempstr), fp_framesize);
        buffer->common_params.size = atoi(tempstr);
    }
}

static void read_bytes_timestamps_from_file(qap_audio_buffer_t *buffer, FILE *fp_timestamp, FILE *fp_input_file)
{
    if (NULL != fp_timestamp) {
        char tempstr[100] = {0};
        int seek_offset = 0;
        fgets(tempstr, sizeof(tempstr), fp_timestamp);
        printf("%s and tempstr is %s \n", __FUNCTION__,  tempstr);
        char * token = strtok(tempstr, ",");
        if (token != NULL) {
            buffer->common_params.size = atoi(token);
            if(token!= NULL) {
                token = strtok(NULL, ",");
                if (token!= NULL) {
                    buffer->common_params.timestamp = atoi(token);
                    ALOGV("%s and timestamp to be pushed to queue is %lld", __FUNCTION__, buffer->common_params.timestamp);
                }
                token = strtok(NULL, ",");
                if (token != NULL) {
                    seek_offset = atoi(token);
                    if (fp_input_file && seek_offset > 0)
                        fseek(fp_input_file, seek_offset, SEEK_CUR);
                }
            }
        } else {
            buffer->common_params.timestamp = CONTIGUOUS_TIMESTAMP;
            buffer->common_params.size = 0;
        }
    }
}

bool is_qap_session_active(int argc, char* argv[], char *kvp_string) {
    char *qap_kvp = NULL;
    char *cmd_str = NULL;
    char *tmp_str = NULL;
    int status = 0;
    cmd_str = (char *)qap_wrapper_get_cmd_string_from_arg_array(argc, argv, &status);
    if (status > 0) {
        qap_kvp = qap_wrapper_get_single_kvp("qap", cmd_str, &status);
        if (qap_kvp == NULL) {
            return false;
        }
        strncpy(kvp_string, cmd_str, strlen(cmd_str));
        if (cmd_str != NULL) {
            free(cmd_str);
            cmd_str = NULL;
        }
    }
    return true;
}

char* check_for_playlist(char *kvp_string) {
    char *file_str = NULL;
    char *tmp_str = NULL;
    char *play_list = NULL;
    int len = 0;

    tmp_str = strstr(kvp_string, "g=/");
    if (tmp_str != NULL) {
        file_str = strstr(kvp_string, ".txt");
        len = file_str - tmp_str;
        play_list = (char*) calloc(1, sizeof(char) * (len+4));
        strncpy(play_list, tmp_str+2, len+2);
    }
    return play_list;
}

int start_playback_through_qap_playlist(char *cmd_kvp_str[], int num_of_streams, char *kvp_string, stream_config stream_param[],
                                     bool qap_wrapper_session_active, qahw_module_handle_t *hal_handle) {
    stream_config *stream = NULL;
    int rc = 0;
    bool broad_cast = false, bd = false;
    int i = 0, curr_clip_type = DOLBY, prev_clip_type;

    if (strstr(kvp_string, "broadcast"))
       broad_cast = true;
    else if(strstr(kvp_string, "bd"))
       bd = true;
    do {
        fprintf(stdout, "cmd_kvp_string is %s kvp_string %s and num_of_streams %d\n", cmd_kvp_str[i], kvp_string, num_of_streams);
        stream = &stream_param[i];
        fprintf(stdout, "stream->filename is %s\n", stream->filename);
        if (stream->filename) {
            prev_clip_type = curr_clip_type;
            if ((stream->file_stream = fopen(stream->filename, "r"))== NULL) {
                fprintf(stderr, "Cannot open audio file %s\n", stream->filename);
                return -EINVAL;
            }
            if (strstr(stream->filename, ".dts")) {
                curr_clip_type = DTS;
            } else {
                curr_clip_type = DOLBY;
            }
        }
        get_file_format(stream);
        fprintf(stdout, "Playing from:%s\n", stream->filename);
        qap_module_handle_t qap_module_handle = NULL;
        if ((bd || (prev_clip_type != curr_clip_type)) && qap_wrapper_session_active) {
            fprintf(stdout, " prev_clip_type is %d curr_clip_type is %d\n", prev_clip_type, curr_clip_type);
            qap_wrapper_session_close();
            qap_wrapper_session_active = false;
        }
        if (!qap_wrapper_session_active) {
            if (broad_cast) {
                cmd_kvp_str[i] = realloc(cmd_kvp_str[i], strlen(cmd_kvp_str[i])+11);
                strcat(strcat(cmd_kvp_str[i], ";"), "broadcast");
            } else if (bd) {
                cmd_kvp_str[i] = realloc(cmd_kvp_str[i], strlen(cmd_kvp_str[i])+4);
                strcat(strcat(cmd_kvp_str[i], ";"), "bd");
            }
            rc = qap_wrapper_session_open(cmd_kvp_str[i], stream, num_of_streams, hal_handle);
            if (rc != 0) {
                fprintf(stderr, "Session Open failed\n");
                return -EINVAL;
            }
            qap_wrapper_session_active = true;
        }

        if (qap_wrapper_session_active) {
            stream->qap_module_handle = qap_wrapper_stream_open(stream);
            if (stream->qap_module_handle == NULL) {
                fprintf(stderr, "QAP Stream open Failed\n");
            } else {
                fprintf(stdout, "QAP module handle is %p and file name is %s\n", stream->qap_module_handle, stream->filename);
                qap_wrapper_start_stream(&stream_param[i]);
                free(stream->filename);
                stream->filename = NULL;
                free(cmd_kvp_str[i]);
                cmd_kvp_str[i] = NULL;
            }
        }
        i++;
        while (!primary_stream_close) {
            usleep(50000);
            fprintf(stderr, "QAP Stream not closed\n");
        }
        fprintf(stderr, "QAP Stream closed\n");
    } while (i <num_of_streams);
    if (qap_wrapper_session_active) {
        qap_wrapper_session_close();
        qap_wrapper_session_active = false;
    }
    return 0;
}

#ifdef QAP
char *qap_wrapper_get_single_kvp(const char *key, const char *kv_pairs, int *status)
{
    char *kvp = NULL;
    char *tempstr = NULL;
    char *token = NULL;
    char *context1 = NULL;
    char *context2 = NULL;
    char *temp_kvp = NULL;
    char *temp_key = NULL;

    if (NULL == key || NULL == kv_pairs) {
        *status = -EINVAL;
        return NULL;
    }
    tempstr = strdup(kv_pairs);
    token = strtok_r(tempstr, ";", &context1);
    if (token != NULL) {
        temp_kvp = strdup(token);
        if (temp_kvp != NULL) {
            temp_key = strtok_r(temp_kvp, "=", &context2);
            if (!strncmp(key, temp_key, strlen(key))) {
                kvp = calloc(1, (strlen(token) + 1) * sizeof(char));
                strncat(kvp, token, strlen(token));
                return kvp;
            }
            free(temp_kvp);
        }
        while (token != NULL) {
            token = strtok_r(NULL, ";", &context1);
            if (token != NULL) {
                temp_kvp = strdup(token);
                if (temp_kvp != NULL) {
                    temp_key = strtok_r(temp_kvp, "=", &context2);
                    if (!strncmp(key, temp_key, strlen(key))) {
                        kvp = calloc(1, (strlen(token) + 1) * sizeof(char));
                        strncat(kvp, token, strlen(token));
                        return kvp;
                    }
                    free(temp_kvp);
                    temp_kvp = NULL;
                }
            }
        }
        free(tempstr);
    }
    return NULL;
}
#endif

int *qap_wrapper_get_int_value_array(const char *kvp, int *count, int *status __unused)
{
    char *tempstr1;
    char *tempstr2;
    char *l1;
    char *l2 __unused;
    char *ctx1;
    char *ctx2 __unused;
    int *val = NULL;
    int i = 0;
    char *s;
    char *endstr;
    int temp = 0;
    char *jump;

    *count = 0;
    if (kvp == NULL) {
        return NULL;
    }
    tempstr1 = strdup(kvp);
    l1 = strtok_r(tempstr1, "=", &ctx1);
    if (l1 != NULL) {
        /* jump from key to value */
        l1 = strtok_r(NULL, "=", &ctx1);
        if (l1 != NULL) {
            tempstr2 = strdup(l1);

            s = tempstr2;
            for (i=0; s[i]; s[i]==',' ? i++ : *s++);

            temp = i;
            val = calloc(1, (i + 1)*sizeof(int));
            i = 0;
            val[i++] = strtol(tempstr2, &endstr, 0);

            while (i <= temp) {
                 jump = endstr + 1;
                val[i++] = strtol(jump, &endstr, 0);
            }
            free(tempstr2);
        }
    }
    free(tempstr1);
    *count = i;
    return val;
}

char * qap_wrapper_get_cmd_string_from_arg_array(int argc, char * argv[], int *status)
{
    char * kvps;
    int idx;
    int has_key = 0;
    int mem = 0;

    fprintf(stdout, "%s %d in", __func__, __LINE__);
    if (argc < 2 || NULL == argv) {
        fprintf(stdout, "%s %d returning EINVAL\n", __func__, __LINE__);
        *status = -EINVAL;
        return NULL;
    }

    for (idx = 0; idx < argc; idx++) {
        mem += (strlen(argv[idx]) + 2);     /* Extra byte to insert delim ';' */
    }

    if (mem > 0)
        kvps = calloc(1, mem * sizeof(char));
    else {
        *status = -EINVAL;
        fprintf(stdout, "%s %d returning EINVAL\n", __func__, __LINE__);
        return NULL;
    }

    if (NULL == kvps) {
        *status = -ENOMEM;
        fprintf(stdout, "%s %d returning EINVAL\n", __func__, __LINE__);
        return NULL;
    }

    for (idx = 1; idx < argc; idx++) {
        if (( argv[idx][0] == '-') &&
                (argv[idx][1] < '0' || argv[idx][1] > '9')) {
            if (has_key) {
                strcat(kvps, ";");
                has_key = 0;
            }
            strcat(kvps, argv[idx]+1);
            strcat(kvps, "=");
            has_key = 1;
        } else if (has_key) {
            strcat(kvps, argv[idx]);
            strcat(kvps, ";");
            has_key = 0;
        } else {
            *status = -EINVAL;
            if (kvps != NULL) {
                free(kvps);
                kvps = NULL;
            }
            fprintf(stdout, "%s %d returning EINVAL\n", __func__, __LINE__);
            return NULL;
        }
    }
    *status = mem;
    fprintf(stdout, "%s %d returning\n", __func__, __LINE__);
    return kvps;
}

static int qap_wrapper_map_input_format(audio_format_t audio_format, qap_audio_format_t *format)
{
    if (audio_format == AUDIO_FORMAT_AC3) {
        *format = QAP_AUDIO_FORMAT_AC3;
        fprintf(stdout, "File Format is AC3!\n");
    } else if (audio_format == AUDIO_FORMAT_E_AC3) {
        *format = QAP_AUDIO_FORMAT_EAC3;
        fprintf(stdout, "File Format is E_AC3!\n");
    } else if ((audio_format == AUDIO_FORMAT_AAC_ADTS_LC) ||
               (audio_format == AUDIO_FORMAT_AAC_ADTS_HE_V1) ||
               (audio_format == AUDIO_FORMAT_AAC_ADTS_HE_V2) ||
               (audio_format == AUDIO_FORMAT_AAC_LC) ||
               (audio_format == AUDIO_FORMAT_AAC_HE_V1) ||
               (audio_format == AUDIO_FORMAT_AAC_HE_V2) ||
               (audio_format == AUDIO_FORMAT_AAC_LATM_LC) ||
               (audio_format == AUDIO_FORMAT_AAC_LATM_HE_V1) ||
               (audio_format == AUDIO_FORMAT_AAC_LATM_HE_V2)) {
        *format = QAP_AUDIO_FORMAT_AAC_ADTS;
        fprintf(stdout, "File Format is AAC!\n");
    } else if (audio_format == AUDIO_FORMAT_DTS) {
        *format = QAP_AUDIO_FORMAT_DTS;
        fprintf(stdout, "File Format is DTS!\n");
    } else if (audio_format == AUDIO_FORMAT_DTS_HD) {
        *format = QAP_AUDIO_FORMAT_DTS_HD;
        fprintf(stdout, "File Format is DTS_HD!\n");
    } else if (audio_format == AUDIO_FORMAT_PCM_16_BIT) {
        *format = QAP_AUDIO_FORMAT_PCM_16_BIT;
        fprintf(stdout, "File Format is PCM_16!\n");
    } else if (audio_format == AUDIO_FORMAT_PCM_32_BIT) {
        *format = QAP_AUDIO_FORMAT_PCM_32_BIT;
        fprintf(stdout, "File Format is PCM_32!\n");
    } else if (audio_format == AUDIO_FORMAT_PCM_24_BIT_PACKED) {
        *format = QAP_AUDIO_FORMAT_PCM_24_BIT_PACKED;
        fprintf(stdout, "File Format is PCM_24!\n");
    } else if ((audio_format == AUDIO_FORMAT_PCM_8_BIT) ||
               (audio_format == AUDIO_FORMAT_PCM_8_24_BIT)) {
        *format = QAP_AUDIO_FORMAT_PCM_8_24_BIT;
        fprintf(stdout, "File Format is PCM_8_24!\n");
    } else {
        fprintf(stdout, "File Format not supported!\n");
        return -EINVAL;
    }
    return 0;
}

char *get_string_value(const char *kvp, int *status)
{
    char *tempstr1 = NULL;
    char *tempstr2 = NULL;
    char *l1;
    char *ctx1;
    if (kvp == NULL)
        return NULL;
    tempstr1 = strdup(kvp);
    l1 = strtok_r(tempstr1, "=", &ctx1);
    if (l1 != NULL) {
        /* jump from key to value */
        l1 = strtok_r(NULL, "=", &ctx1);
        if (l1 != NULL)
            tempstr2 = strdup(l1);
    }
    free(tempstr1);
    return tempstr2;
}

int qap_wrapper_write_to_hal(qahw_stream_handle_t* out_handle, char *data, size_t bytes)
{
    ssize_t ret;
    qahw_out_buffer_t out_buf;

    memset(&out_buf,0, sizeof(qahw_out_buffer_t));
    out_buf.buffer = data;
    out_buf.bytes = bytes;

    ret = qahw_out_write(out_handle, &out_buf);
    if (ret < 0)
        fprintf(stderr, "%s::%d: writing data to hal failed (ret = %zd)\n", __func__, __LINE__, ret);
    else if (ret != bytes)
        fprintf(stdout, "%s::%d provided bytes %zd, written bytes %d\n",__func__, __LINE__, bytes, ret);

    return ret;
}

static void close_output_streams()
{
    int ret;
    if (qap_out_hal_handle && qap_out_spk_handle) {
        ret = qahw_out_standby(qap_out_spk_handle);
        if (ret)
            fprintf(stderr, "%s::%d: out standby failed %d \n", __func__, __LINE__, ret);
        if (play_through_bt) {
            fprintf(stdout, "%s::%d: disconnecting BT\n", __func__, __LINE__);
            char param[100] = {0};
            snprintf(param, sizeof(param), "%s=%d", "disconnect", AUDIO_DEVICE_OUT_BLUETOOTH_A2DP);
            qahw_set_parameters(qap_out_hal_handle, param);
        }
        fprintf(stdout, "%s::%d: closing output stream\n", __func__, __LINE__);
        ret = qahw_close_output_stream(qap_out_spk_handle);
        if (ret)
            fprintf(stderr, "%s::%d: could not close output stream, error - %d\n", __func__, __LINE__, ret);
        qap_out_spk_handle = NULL;
    }
    if (qap_out_hal_handle && qap_out_hp_handle) {
        ret = qahw_out_standby(qap_out_hp_handle);
        if (ret)
            fprintf(stderr, "%s::%d: out standby failed %d \n", __func__, __LINE__, ret);
        fprintf(stdout, "%s::%d: closing output stream\n", __func__, __LINE__);
        ret = qahw_close_output_stream(qap_out_hp_handle);
        if (ret)
            fprintf(stderr, "%s::%d: could not close output stream, error - %d\n", __func__, __LINE__, ret);
        qap_out_hp_handle = NULL;
    }
    if (qap_out_hal_handle && qap_out_hdmi_handle) {
        char param[100] = {0};
        snprintf(param, sizeof(param), "%s=%d", "disconnect", AUDIO_DEVICE_OUT_HDMI);
        ret = qahw_out_standby(qap_out_hdmi_handle);
        if (ret)
            fprintf(stderr, "%s::%d: out standby failed %d\n", __func__, __LINE__, ret);
        qahw_set_parameters(qap_out_hal_handle, param);
        fprintf(stdout, "%s::%d: closing output stream\n", __func__, __LINE__);
        ret = qahw_close_output_stream(qap_out_hdmi_handle);
        if (ret)
            fprintf(stderr, "%s::%d: could not close output stream, error - %d\n", __func__, __LINE__, ret);
        qap_out_hdmi_handle = NULL;
    }
    primary_stream_close = true;
}

void qap_wrapper_module_callback(qap_module_handle_t module_handle, void* priv_data, qap_module_callback_event_t event_id, int size, void *data)
{
    stream_config *p_stream_param = (stream_config*)priv_data;
    if(p_stream_param == NULL) {
        ALOGE("%s %d, callback handle is null.",__func__,__LINE__);
    }
    ALOGV("%s %d, %s Received event id %d\n", __func__, __LINE__, p_stream_param->filename, event_id);

    switch (event_id) {
        case QAP_MODULE_CALLBACK_EVENT_SEND_INPUT_BUFFER:
        {
            if (size < sizeof(qap_send_buffer_t)) {
                ALOGE("%s %d event id %d, wrong payload size %d\n",
                      __func__, __LINE__, event_id, size);
                break;
            }
            qap_send_buffer_t *p_send_buffer_event = (qap_send_buffer_t*)data;
            pthread_mutex_lock(&p_stream_param->input_buffer_available_lock);
            p_stream_param->input_buffer_available_size = p_send_buffer_event->bytes_available;
            pthread_cond_signal(&p_stream_param->input_buffer_available_cond);
            pthread_mutex_unlock(&p_stream_param->input_buffer_available_lock);

            break;
        }
        case QAP_MODULE_CALLBACK_EVENT_INPUT_CFG_CHANGE:
        {
            if (size < sizeof(qap_input_config_t)) {
                ALOGE("%s %d event id %d, wrong payload size %d\n",
                      __func__, __LINE__, event_id, size);
                break;
            }
            qap_input_config_t *p_stream_format = (qap_input_config_t*)data;

            ALOGV(" %s %d Input format updated; sample_rate %lu, channels %lu, bitwidth %lu",
                  __func__, __LINE__,
                  p_stream_format->sample_rate,
                  p_stream_format->channels,
                  p_stream_format->bit_width);
            break;
        }
        default:
        break;
    }
}

void qap_wrapper_session_callback(qap_session_handle_t session_handle __unused, void* priv_data __unused, qap_callback_event_t event_id, int size, void *data)
{
    int ret = 0;
    int bytes_written = 0;
    int bytes_remaining = 0;
    int offset = 0;
    audio_output_flags_t flags;
    flags = (AUDIO_OUTPUT_FLAG_NON_BLOCKING |
             AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD |
             AUDIO_OUTPUT_FLAG_DIRECT);
    ALOGV("%s %d Received event id %d\n", __func__, __LINE__, event_id);
    switch (event_id) {
        case QAP_CALLBACK_EVENT_EOS:
            ALOGV("%s %d Received Main Input EOS", __func__, __LINE__);
            if (stream_cnt > 0)
                stream_cnt--;
            pthread_mutex_lock(&main_eos_lock);
            pthread_cond_signal(&main_eos_cond);
            main_eos_received = true;
            pthread_mutex_unlock(&main_eos_lock);

            if (!stream_cnt)
                close_output_streams();
            if (play_list_cnt && input_streams_count) {
                play_list_cnt--;
                input_streams_count = 0;
            }
            break;
        case QAP_CALLBACK_EVENT_EOS_ASSOC:
            if (stream_cnt > 0)
                stream_cnt--;
            //if (!has_system_input)
            {
                ALOGV("%s %d Received Secondary Input EOS", __func__, __LINE__);
                pthread_mutex_lock(&sec_eos_lock);
                pthread_cond_signal(&sec_eos_cond);
                sec_eos_received = true;
                pthread_mutex_unlock(&sec_eos_lock);
            }
            if (!stream_cnt)
                close_output_streams();
            break;
        case QAP_CALLBACK_EVENT_MAIN_2_EOS:
            if (stream_cnt > 0)
                stream_cnt--;
            //if (!has_system_input)
            {
                ALOGV("%s %d Received main2 Input EOS", __func__, __LINE__);
                pthread_mutex_lock(&main2_eos_lock);
                pthread_cond_signal(&main2_eos_cond);
                main2_eos_received = true;
                pthread_mutex_unlock(&main2_eos_lock);
            }
            if (!stream_cnt)
                close_output_streams();
            break;
        case QAP_CALLBACK_EVENT_ERROR:
            break;
        case QAP_CALLBACK_EVENT_SUCCESS:
            break;
        case QAP_CALLBACK_EVENT_METADATA:
            break;
        case QAP_CALLBACK_EVENT_OUTPUT_CFG_CHANGE:
            if (data != NULL) {
                qap_audio_buffer_t *buffer = (qap_audio_buffer_t *) data;
                qap_output_config_t *new_conf = &buffer->buffer_parms.output_buf_params.output_config;
                qap_output_config_t *cached_conf = NULL;
                int index = -1;

                ALOGV("%s %d Received Output cfg change", __func__, __LINE__);
                if (buffer) {
                    index = get_qap_session_out_config_index_for_id(
                              buffer->buffer_parms.output_buf_params.output_id);
                    if (index >= 0)
                        cached_conf = &session_output_config.output_config[index];
                }
                if (cached_conf == NULL) {
                    ALOGE("Invalid output config from QAP is reached");
                    return;
                }
                if (memcmp(cached_conf, new_conf, sizeof(qap_output_config_t)) != 0) {
                    memcpy(cached_conf, new_conf, sizeof(qap_output_config_t));
                    cached_conf->id = buffer->buffer_parms.output_buf_params.output_id;
                    is_media_fmt_changed[index] = true;
                }
            }
            break;
        case QAP_CALLBACK_EVENT_DATA:
            if (data != NULL) {
                qap_audio_buffer_t *buffer = (qap_audio_buffer_t *) data;

                if (buffer && timestamp_mode) {
                    char ch[100] = {0};
                    if (kpi_mode) {
                        if ((data_callback_count > 5) && (data_callback_count < TIMESTAMP_ARRAY_SIZE)) {
                            gettimeofday(&tcont_ts2, NULL);
                            data_callback_ts_arr[data_callback_count] = buffer->common_params.timestamp;
                            data_callback_st_arr[data_callback_count] = (tcont_ts2.tv_sec) * 1000 + (tcont_ts2.tv_usec) / 1000;
                            ALOGV("%s::%d data size %d, Kpi cont ts2 %lf, buffer timestamp %ld",
                                  __func__, __LINE__, buffer->common_params.size,
                                 data_callback_st_arr[data_callback_count], data_callback_ts_arr[data_callback_count]);
                        }
                        if (data_callback_count < TIMESTAMP_ARRAY_SIZE)
                            data_callback_count++;
                    }
                    if (fp_output_timestamp_file == NULL) {
                        fp_output_timestamp_file =
                                 fopen("/sdcard/output_timestamp_file.txt","w");
                        if(fp_output_timestamp_file) {
                            fprintf(stdout, "output file :: "
                                   "/sdcard/output_file_timestamp.txt"
                                   " has been generated.");
                            }
                    }
                    if (fp_output_timestamp_file) {
                        sprintf(ch, "%d,%lld\n", buffer->common_params.size, buffer->common_params.timestamp);
                        fprintf(stdout, "%s: %s", __func__, ch);
                        ret = fwrite((char *)&ch, sizeof(char),
                                     strlen(ch), fp_output_timestamp_file);
                        fflush(fp_output_timestamp_file);
                    }
                }

                if (buffer && buffer->common_params.data) {
                    int index = -1;
                    bool is_reopen_stream = false;
                    index = get_qap_session_out_config_index_for_id(buffer->buffer_parms.output_buf_params.output_id);
                    if (index > -1 && is_media_fmt_changed[index]) {
                        is_reopen_stream = true;
                        is_media_fmt_changed[index] = false;
                    } else if (index < 0) {
                        ALOGE("%s: No Valid Output Config found for id = %d",
                                     __func__, buffer->buffer_parms.output_buf_params.output_id);
                        break;
                    }

                    if ((buffer->buffer_parms.output_buf_params.output_id &
                            AUDIO_DEVICE_OUT_HDMI) == AUDIO_DEVICE_OUT_HDMI) {
                        if (!hdmi_connected) {
                            char param[100] = {0};
                            snprintf(param, sizeof(param), "%s=%d", "connect", AUDIO_DEVICE_OUT_HDMI);
                            qahw_set_parameters(qap_out_hal_handle, param);
                            hdmi_connected = true;
                        }
                        if (encode) {
                            if (enable_dump && fp_output_writer_hdmi == NULL) {
                                if (buffer->buffer_parms.output_buf_params.output_id ==
                                        (AUDIO_FORMAT_E_AC3|AUDIO_DEVICE_OUT_HDMI))
                                    qap_wrapper_create_multi_channel_dump("/sdcard/output_hdmi.ddp");
                                else if (buffer->buffer_parms.output_buf_params.output_id ==
                                            (AUDIO_FORMAT_AC3|AUDIO_DEVICE_OUT_HDMI))
                                    qap_wrapper_create_multi_channel_dump("/sdcard/output_hdmi.dd");
                                else
                                    qap_wrapper_create_multi_channel_dump("/sdcard/output_hdmi_dts.dts");
                            }
                        } else {
                            if (enable_dump && fp_output_writer_hdmi == NULL)
                                qap_wrapper_create_multi_channel_dump("/sdcard/output_hdmi.dump");
                        }
                        if (fp_output_writer_hdmi) {
                            ret = fwrite((unsigned char *)buffer->common_params.data, sizeof(unsigned char),
                                          buffer->common_params.size, fp_output_writer_hdmi);
                            fflush(fp_output_writer_hdmi);
                        }

                        if (is_reopen_stream && qap_out_hdmi_handle) {
                            qahw_close_output_stream(qap_out_hdmi_handle);
                            qap_out_hdmi_handle = NULL;
                            is_reopen_stream = false;
                        }

                        if (hdmi_connected && qap_out_hdmi_handle == NULL) {
                            struct audio_config config;
                            audio_devices_t devices;

                            config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
                            config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
                            config.sample_rate = config.offload_info.sample_rate = DEFAULT_SAMPLE_RATE;

                            if (index > -1) {
                                if (session_output_config.output_config[index].sample_rate > 0)
                                    config.sample_rate = config.offload_info.sample_rate = session_output_config.output_config[index].sample_rate;
                                config.offload_info.channel_mask = config.channel_mask =
                                                   audio_channel_out_mask_from_count(session_output_config.output_config[index].channels);
                                if (session_output_config.output_config[index].bit_width == 24) {
                                    config.format = config.offload_info.format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
                                    config.offload_info.bit_width = 24;
                                } else {
                                    config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT;
                                    config.offload_info.bit_width = 16;
                                }
                                if (session_output_config.output_config[index].format == QAP_AUDIO_FORMAT_AC3)
                                    config.format = config.offload_info.format = AUDIO_FORMAT_AC3;
                                else if (session_output_config.output_config[index].format == QAP_AUDIO_FORMAT_EAC3)
                                    config.format = config.offload_info.format = AUDIO_FORMAT_E_AC3;
                                else if (session_output_config.output_config[index].format == QAP_AUDIO_FORMAT_DTS)
                                    config.format = config.offload_info.format = AUDIO_FORMAT_DTS;
                            }

                            devices = AUDIO_DEVICE_OUT_HDMI;
                            if (timestamp_mode)
                                flags |= AUDIO_OUTPUT_FLAG_TIMESTAMP;
                            if (encode) {
                                ALOGV("%s:%d output format %x", __func__, __LINE__,
                                        config.format, config.offload_info.format);
                                ret = qahw_open_output_stream(qap_out_hal_handle, qap_stream_out_cmpr_handle, devices,
                                                              flags, &config, &qap_out_hdmi_handle, "stream");
                            } else {
                                ret = qahw_open_output_stream(qap_out_hal_handle, qap_stream_out_hdmi_handle, devices,
                                                              flags, &config, &qap_out_hdmi_handle, "stream");
                                if (index > -1)
                                    set_qahw_stream_channel_map(qap_out_hdmi_handle, &session_output_config.output_config[index]);
                            }

                            ret = qahw_out_set_volume(qap_out_hdmi_handle, vol_level, vol_level);
                            if (ret < 0)
                                ALOGE("unable to set volume");
                        }
                        if (qap_out_hdmi_handle) {
                                bytes_written = qap_wrapper_write_to_hal(qap_out_hdmi_handle,
                                                    buffer->common_params.data, buffer->common_params.size);
                                if (bytes_written == -1) {
                                    ALOGE("%s::%d write failed in hal", __func__, __LINE__);
                                }
                            if (kpi_mode && data_callback_count == 6)
                                dsp_latency = qahw_out_get_latency(qap_out_hdmi_handle);
                        }
                        if (kpi_mode && data_callback_count == 1) {
                             gettimeofday(&tcold_stop, NULL);
                             cold_stop = (tcold_stop.tv_sec) * 1000 + (tcold_stop.tv_usec) / 1000;
                             ALOGD("%s::%d Measuring Kpi cold stop %lf", __func__, __LINE__, cold_stop);
                        }
                    }
                    if (buffer->buffer_parms.output_buf_params.output_id == AUDIO_DEVICE_OUT_WIRED_HEADPHONE ||
                        buffer->buffer_parms.output_buf_params.output_id == AUDIO_DEVICE_OUT_LINE) {
                        if (enable_dump && fp_output_writer_hp == NULL) {
                            fp_output_writer_hp =
                                         fopen("/sdcard/output_hp.dump","wb");
                            if (fp_output_writer_hp) {
                                fprintf(stdout, "output file :: "
                                      "/sdcard/output_hp.dump"
                                      " has been generated.\n");
                            } else {
                                fprintf(stderr, "Failed open hp dump file\n");
                            }
                        }
                        if (fp_output_writer_hp) {
                            ret = fwrite((unsigned char *)buffer->common_params.data, sizeof(unsigned char),
                                          buffer->common_params.size, fp_output_writer_hp);
                            fflush(fp_output_writer_hp);
                        }
                        if (is_reopen_stream && qap_out_hp_handle) {
                            qahw_close_output_stream(qap_out_hp_handle);
                            qap_out_hp_handle = NULL;
                            is_reopen_stream = false;
                        }

                        if (qap_out_hp_handle == NULL) {
                            struct audio_config config;
                            audio_devices_t devices;
                            config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
                            config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
                            config.sample_rate = config.offload_info.sample_rate = DEFAULT_SAMPLE_RATE;
                            config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT;
                            config.offload_info.bit_width = 16;
                            config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;

                           if (index > -1) {
                                config.sample_rate = config.offload_info.sample_rate = session_output_config.output_config[index].sample_rate;
                                config.offload_info.channel_mask = config.channel_mask =
                                                   audio_channel_out_mask_from_count(session_output_config.output_config[index].channels);
                                if (session_output_config.output_config[index].bit_width == 24) {
                                    config.format = config.offload_info.format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
                                    config.offload_info.bit_width = 24;
                                }
                            }

                            devices = AUDIO_DEVICE_OUT_LINE;//ToDO - Need to change to AUDIO_DEVICE_OUT_WIRED_HEADPHONE

                            if (timestamp_mode)
                                flags |= AUDIO_OUTPUT_FLAG_TIMESTAMP;
                            ret = qahw_open_output_stream(qap_out_hal_handle, qap_stream_out_hp_handle, devices,
                                 flags, &config, &qap_out_hp_handle, "stream");

                            if (ret) {
                                ALOGE("%s:%d could not open output stream, error - %d", __func__, __LINE__, ret);
                                return;
                            }
                            if (index > -1)
                                set_qahw_stream_channel_map(qap_out_hp_handle, &session_output_config.output_config[index]);
                            ret = qahw_out_set_volume(qap_out_hp_handle, vol_level, vol_level);
                            if (ret < 0)
                                 ALOGE("unable to set volume");
                        }
                        if (qap_out_hp_handle) {
                                bytes_written = qap_wrapper_write_to_hal(qap_out_hp_handle,
                                                    buffer->common_params.data, buffer->common_params.size);
                                if (bytes_written == -1) {
                                    ALOGE("%s::%d write failed in hal", __func__, __LINE__);
                                }
                            if (kpi_mode && data_callback_count == 6)
                                dsp_latency = qahw_out_get_latency(qap_out_hp_handle);
                        }
                        if (kpi_mode && data_callback_count == 1) {
                             gettimeofday(&tcold_stop, NULL);
                             cold_stop = (tcold_stop.tv_sec) * 1000 + (tcold_stop.tv_usec) / 1000;
                             ALOGD("%s::%d Measuring Kpi cold stop %lf", __func__, __LINE__, cold_stop);
                        }
                    }
                    if (buffer->buffer_parms.output_buf_params.output_id == AUDIO_DEVICE_OUT_SPEAKER) {
                        if (enable_dump && fp_output_writer_spk == NULL) {
                            char ch[4] = {0};
                            fp_output_writer_spk =
                                         fopen("/sdcard/output_speaker.dump","wb");
                            if (fp_output_writer_spk) {
                                fprintf(stdout, "output file :: "
                                      "/sdcard/output_speaker.dump"
                                      " has been generated.\n");
                                if (dolby_formats) {
                                    ret = fwrite((unsigned char *)&ch, sizeof(unsigned char),
                                                  4, fp_output_writer_spk);
                                }
                            } else {
                                fprintf(stderr, "Failed open speaker dump file\n");
                            }
                        }
                        if (fp_output_writer_spk) {
                            ret = fwrite((unsigned char *)buffer->common_params.data, sizeof(unsigned char),
                                          buffer->common_params.size, fp_output_writer_spk);
                            fflush(fp_output_writer_spk);
                        }
                        if (is_reopen_stream && qap_out_spk_handle) {
                            qahw_close_output_stream(qap_out_spk_handle);
                            qap_out_spk_handle = NULL;
                            is_reopen_stream = false;
                        }
                        if (qap_out_spk_handle == NULL) {
                            struct audio_config config;
                            audio_devices_t devices;

                            config.offload_info.version = AUDIO_INFO_INITIALIZER.version;
                            config.offload_info.size = AUDIO_INFO_INITIALIZER.size;
                            config.sample_rate = config.offload_info.sample_rate = DEFAULT_SAMPLE_RATE;
                            config.format = config.offload_info.format = AUDIO_FORMAT_PCM_16_BIT;
                            config.offload_info.bit_width = 16;
                            config.offload_info.channel_mask = config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;

                           if (index > -1) {
                                 config.sample_rate = config.offload_info.sample_rate = session_output_config.output_config[index].sample_rate;
                                 config.offload_info.channel_mask = config.channel_mask =
                                 audio_channel_out_mask_from_count(session_output_config.output_config[index].channels);
                                 if (session_output_config.output_config[index].bit_width == 24) {
                                     config.format = config.offload_info.format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
                                     config.offload_info.bit_width = 24;
                                 }
                            }

                            if (play_through_bt) {
                                fprintf(stderr, "%s::%d: connecting BT\n", __func__, __LINE__);
                                char param[100] = {0};
                                snprintf(param, sizeof(param), "%s=%d", "connect", AUDIO_DEVICE_OUT_BLUETOOTH_A2DP);
                                qahw_set_parameters(qap_out_hal_handle, param);
                                devices = AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
                            } else {
                                devices = AUDIO_DEVICE_OUT_SPEAKER;
                            }
                            if (timestamp_mode)
                                flags |= AUDIO_OUTPUT_FLAG_TIMESTAMP;
                            ALOGD("%s::%d: open output for device %d", __func__, __LINE__, devices);
                            ret = qahw_open_output_stream(qap_out_hal_handle, qap_stream_out_spk_handle, devices,
                                 flags, &config, &qap_out_spk_handle, "stream");

                            if (ret) {
                                ALOGE("%s:%d could not open output stream, error - %d", __func__, __LINE__, ret);
                                return;
                            }
                            if (index > -1)
                                set_qahw_stream_channel_map(qap_out_spk_handle, &session_output_config.output_config[index]);
                            ret = qahw_out_set_volume(qap_out_spk_handle, vol_level, vol_level);
                            if (ret < 0)
                                 ALOGE("unable to set volume");
                        }
                        if (qap_out_spk_handle) {
                                bytes_written = qap_wrapper_write_to_hal(qap_out_spk_handle,
                                                    buffer->common_params.data, buffer->common_params.size);
                                if (bytes_written == -1) {
                                    ALOGE("%s::%d write failed in hal", __func__, __LINE__);
                                }
                            if (kpi_mode && data_callback_count == 6)
                                dsp_latency = qahw_out_get_latency(qap_out_spk_handle);
                        }
                        if (kpi_mode && data_callback_count == 1) {
                             gettimeofday(&tcold_stop, NULL);
                             cold_stop = (tcold_stop.tv_sec) * 1000 + (tcold_stop.tv_usec) / 1000;
                             ALOGD("%s::%d Measuring Kpi cold stop %lf", __func__, __LINE__, cold_stop);
                        }
                    }
                    if (buffer->buffer_parms.output_buf_params.output_id == AUDIO_DEVICE_OUT_PROXY) {

                        if (fp_ecref == NULL) {
                            fp_ecref = fopen("/data/vendor/misc/audio/ecref", "w+");
                        }

                        if (fp_ecref) {
                            ALOGD("%s: write %d bytes to ecref dump",__func__,buffer->common_params.size);
                            fwrite((unsigned char *)buffer->common_params.data, 1, buffer->common_params.size, fp_ecref);
                        } else {
                            ALOGE("%s: failed to open ecref dump file",__func__);
                        }

                    }
                }
            }
            break;
        default:
            break;
    }
}

static void qap_wrapper_is_dap_enabled(char *kv_pairs, int out_device_id, qap_session_handle_t handle) {
    int status = 0;
    int temp = 0;
    char *dap_kvp = NULL;
    int *dap_value = NULL;
    int dap_enable = 0;
    qap_session_pp_configs_t dap_pp_config;
    dap_kvp = qap_wrapper_get_single_kvp("dap_enable", kv_pairs, &status);
    if (dap_kvp != NULL) {
        dap_value = qap_wrapper_get_int_value_array(dap_kvp, &temp, &status);
        if (dap_value != NULL)
            dap_enable = dap_value[0];
        if (dap_enable) {
            fprintf(stdout, "dap enable %d and device id %d\n", dap_enable, out_device_id);
            char *dev_kvp = NULL;
            if ((out_device_id & AUDIO_DEVICE_OUT_SPEAKER) == AUDIO_DEVICE_OUT_SPEAKER) {
                dev_kvp = (char *) calloc(1, status + strlen("o_device=1; "));
                if (dev_kvp != NULL) {
                    strcat(dev_kvp, "o_device=1;");
                    strcat(session_kv_pairs, dev_kvp);
                    fprintf(stdout, "session set params %s\n", session_kv_pairs);
                    free(dev_kvp);
                    dev_kvp = NULL;
                }
                dap_pp_config.pp_config[dap_pp_config.num_confs].id = AUDIO_DEVICE_OUT_SPEAKER;
                dlb_param = MS12_SESSION_CFG_DAP_ENABLE_SPEAKER;
                dap_pp_config.pp_config[dap_pp_config.num_confs].pp_type = (void *) &dlb_param;
                dap_pp_config.num_confs++;
            }
            if (((out_device_id & AUDIO_DEVICE_OUT_LINE) == AUDIO_DEVICE_OUT_LINE) ||
                ((out_device_id & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) == AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) {
                dev_kvp = (char *) calloc(1, status + strlen("o_device=2; "));
                if (dev_kvp != NULL) {
                    strcat(dev_kvp, "o_device=2;");
                    strcat(session_kv_pairs, dev_kvp);
                    fprintf(stdout, "session set params %s\n", session_kv_pairs);
                    free(dev_kvp);
                    dev_kvp = NULL;
                }
                if ((out_device_id & AUDIO_DEVICE_OUT_LINE) == AUDIO_DEVICE_OUT_LINE)
                dap_pp_config.pp_config[dap_pp_config.num_confs].id = AUDIO_DEVICE_OUT_LINE;
                else
                dap_pp_config.pp_config[dap_pp_config.num_confs].id = AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
                dlb_param_hp = MS12_SESSION_CFG_DAP_ENABLE_HEADPHONE;
                dap_pp_config.pp_config[dap_pp_config.num_confs].pp_type = (void *) &dlb_param_hp;
                dap_pp_config.num_confs++;
            }
            status = qap_session_cmd(qap_session_handle, QAP_SESSION_CMD_SET_PP_OUTPUTS,
                                          sizeof(qap_session_pp_configs_t), &dap_pp_config, NULL, NULL);
            if (status != QAP_STATUS_OK)
                fprintf(stderr, "Output config failed\n");
        }
        free(dap_kvp);
        dap_kvp = NULL;
    }
}

void update_qap_session_init_params(char *kv_pairs)
{
    int status = 0;
    char *kvp = NULL;
    int temp = 0;
    int *temp_val = NULL;
    uint32_t cmd_data[16] = {0};
    uint32_t cmd_size = 0;

    kvp = qap_wrapper_get_single_kvp("max_chs", kv_pairs, &status);
    if (kvp != NULL) {
        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
        if (temp_val != NULL) {
            cmd_data[cmd_size++] = MS12_SESSION_CFG_MAX_CHS;
            cmd_data[cmd_size++] = temp_val[0];
            free(temp_val);
            temp_val = NULL;
        }
        free(kvp);
        kvp = NULL;
    }

    kvp = qap_wrapper_get_single_kvp("bs_out_mode", kv_pairs, &status);
    if (kvp != NULL) {
        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
        if (temp_val != NULL) {
            cmd_data[cmd_size++] = MS12_SESSION_CFG_BS_OUTPUT_MODE;
            cmd_data[cmd_size++] = temp_val[0];
            free(temp_val);
            temp_val = NULL;
        }
        free(kvp);
        kvp = NULL;
    }

    kvp = qap_wrapper_get_single_kvp("chmod_locking", kv_pairs, &status);
    if (kvp != NULL) {
        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
        if (temp_val != NULL) {
            cmd_data[cmd_size++] = MS12_SESSION_CFG_CHMOD_LOCKING;
            cmd_data[cmd_size++] = temp_val[0];
            free(temp_val);
            temp_val = NULL;
        }
        free(kvp);
        kvp = NULL;
    }

    kvp = qap_wrapper_get_single_kvp("dn", kv_pairs, &status);
    if (kvp != NULL) {
        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
        if (temp_val != NULL) {
            cmd_data[cmd_size++] = MS12_SESSION_CFG_DIALOG_NORM;
            cmd_data[cmd_size++] = temp_val[0];
            free(temp_val);
            temp_val = NULL;
        }
        free(kvp);
        kvp = NULL;
    }

    kvp = qap_wrapper_get_single_kvp("rp", kv_pairs, &status);
    if (kvp != NULL) {
        temp_val = qap_wrapper_get_int_value_array(kvp, &temp, &status);
        if (temp_val != NULL) {
            cmd_data[cmd_size++] = MS12_SESSION_CFG_COMPR_PROF;
            cmd_data[cmd_size++] = temp_val[0];
            free(temp_val);
            temp_val = NULL;
        }
        free(kvp);
        kvp = NULL;
    }

    if (!cmd_size) {
        return;
    }

    temp = qap_session_cmd(qap_session_handle,
            QAP_SESSION_CMD_SET_PARAM,
            cmd_size * sizeof(uint32_t),
            &cmd_data[0],
            NULL,
            NULL);
    if (temp != QAP_STATUS_OK) {
        fprintf(stderr, "session init config failed\n");
    }
}

int qap_wrapper_session_open(char *kv_pairs, void* stream_data, int num_of_streams,  qahw_module_handle_t *hal_handle)
{
    int status = 0;
    int ret = 0;
    int i;
    int temp = 0;
    stream_config *stream = (stream_config *)stream_data;
    char *session_type_kvp = NULL;
    char *encode_kvp = NULL;
    int *temp_val = NULL;
    char *bitwidth_kvp = NULL;
    int out_bitwidth = PCM_16_BITWIDTH;
    int out_sample_rate = DEFAULT_SAMPLE_RATE;

    qap_out_hal_handle = hal_handle;
    if (kpi_mode) {
        memset(data_input_st_arr, 0, sizeof(data_input_st_arr));
        memset(data_input_ts_arr, 0, sizeof(data_input_ts_arr));
        memset(data_callback_st_arr, 0, sizeof(data_callback_st_arr));
        memset(data_callback_ts_arr, 0, sizeof(data_callback_ts_arr));
        gettimeofday(&tcold_start, NULL);
        cold_start = (tcold_start.tv_sec) * 1000 + (tcold_start.tv_usec) / 1000;
        ALOGD("%s::%d Measuring Kpi cold start %lf", __func__, __LINE__, cold_start);
    }
    if (play_list)
        play_list_cnt = num_of_streams;

    memset(&session_output_config, 0, sizeof(session_output_config));
    strcpy(session_kv_pairs, kv_pairs);
    ALOGV("%s session_kv_pairs = %s", __func__, session_kv_pairs);
    if (NULL != (session_type_kvp = qap_wrapper_get_single_kvp("broadcast", kv_pairs, &status))) {
        session_type = SESSION_BROADCAST;
        fprintf(stdout, "Session Type is Broadcast\n");
        free(session_type_kvp);
        session_type_kvp = NULL;
    } else if (NULL != (session_type_kvp = qap_wrapper_get_single_kvp("bd", kv_pairs, &status))) {
        session_type = SESSION_BLURAY;
        free(session_type_kvp);
        session_type_kvp = NULL;
        fprintf(stdout, "Session Type is Bluray\n");
    }

    if (session_type == SESSION_BLURAY) {
        if ((stream->filetype == FILE_WAV) ||
            (stream->filetype == FILE_AAC)) {
            fprintf(stderr, "Format is not supported for BD usecase\n");
            return -EINVAL;
        }
        if (!play_list && num_of_streams > 1) {
            fprintf(stderr, "Please specifiy proper session type\n");
            return -EINVAL;
        }
    }

    if (stream->filetype == FILE_DTS && (NULL == m8_lib_handle)) {
        m8_lib_handle = (qap_session_handle_t) qap_load_library(QAC_LIB_M8);
        if (m8_lib_handle == NULL) {
            fprintf(stdout, "Failed to load M8 library\n");
            return -EINVAL;
        }
        fprintf(stdout, "loaded M8 library\n");
        dolby_formats = false;
    } else if ((stream->filetype == FILE_AC3) ||
                (stream->filetype == FILE_EAC3) ||
                (stream->filetype == FILE_EAC3_JOC) ||
                (stream->filetype == FILE_WAV) ||
                (stream->filetype == FILE_AAC) ||
                (stream->filetype == FILE_AAC_ADTS) ||
                (stream->filetype == FILE_AAC_LATM) && (NULL == ms12_lib_handle)) {
        ms12_lib_handle = (qap_session_handle_t) qap_load_library(QAC_LIB_MS12);
        if (ms12_lib_handle == NULL) {
            fprintf(stderr, "Failed to load MS12 library\n");
            return -EINVAL;
        }
        dolby_formats = true;
    }


    // To-Do - Need to check SPDIF out also when SPDIF out is supported
    ALOGD("%s::%d output device %d", __func__, __LINE__, stream->output_device);

    bitwidth_kvp = qap_wrapper_get_single_kvp("bitwidth", kv_pairs, &status);
    if (bitwidth_kvp != NULL) {
        temp_val = qap_wrapper_get_int_value_array(bitwidth_kvp, &temp, &status);
        if (temp_val != NULL) {
            if (stream->filetype == FILE_DTS)
                out_bitwidth = temp_val[0];
            free(temp_val);
            temp_val = NULL;
        }
        free(bitwidth_kvp);
        bitwidth_kvp = NULL;
    }

    if ((session_type == SESSION_BROADCAST) && dolby_formats) {
        fprintf(stdout, "%s::%d Setting BROADCAST session for dolby formats\n", __func__, __LINE__);
        qap_session_handle = (qap_session_handle_t) qap_session_open(QAP_SESSION_MS12_OTT, ms12_lib_handle);
        if (qap_session_handle == NULL)
            return -EINVAL;
    } else if ((session_type == SESSION_BROADCAST) && !dolby_formats) {
        fprintf(stdout, "%s::%d Setting BROADCAST session for dts formats\n", __func__, __LINE__);
        qap_session_handle = (qap_session_handle_t) qap_session_open(QAP_SESSION_BROADCAST, m8_lib_handle);
        if (qap_session_handle == NULL)
            return -EINVAL;
    } else if (session_type == SESSION_BLURAY) {
        fprintf(stdout, "%s::%d Setting BD session\n", __func__, __LINE__);
        if (!encode && dolby_formats) {
            fprintf(stdout, "%s::%d Setting BD session for decoding dolby formats\n", __func__, __LINE__);
            qap_session_handle = (qap_session_handle_t) qap_session_open(QAP_SESSION_DECODE_ONLY, ms12_lib_handle);
            if (qap_session_handle == NULL)
                return -EINVAL;
        } else if (!encode && !dolby_formats) {
            fprintf(stdout, "%s::%d Setting BD session for decoding dts formats \n", __func__, __LINE__, qap_session_handle);
            qap_session_handle = (qap_session_handle_t) qap_session_open(QAP_SESSION_DECODE_ONLY, m8_lib_handle);
            if (qap_session_handle == NULL)
                return -EINVAL;
        } else if (encode && dolby_formats)  {
            fprintf(stdout, "%s::%d Setting BD session for encoding dolby formats \n", __func__, __LINE__, qap_session_handle);
            qap_session_handle = (qap_session_handle_t) qap_session_open(QAP_SESSION_ENCODE_ONLY, ms12_lib_handle);
            if (qap_session_handle == NULL)
                return -EINVAL;
        } else if (encode && !dolby_formats) {
            fprintf(stdout, "%s::%d Setting BD session for encoding dts formats \n", __func__, __LINE__, qap_session_handle);
            qap_session_handle = (qap_session_handle_t) qap_session_open(QAP_SESSION_ENCODE_ONLY, m8_lib_handle);
            if (qap_session_handle == NULL)
                return -EINVAL;
        }
    }

    ret = qap_session_set_callback(qap_session_handle, &qap_wrapper_session_callback, NULL);
    if (ret != QAP_STATUS_OK) {
        fprintf(stderr, "!!!! Please specify appropriate Session\n");
        return -EINVAL;
    }

    if (dolby_formats) {
        update_qap_session_init_params(kv_pairs);
    }

    if (!session_output_configured) {
        if (session_type != SESSION_BROADCAST)
            out_sample_rate = stream->config.sample_rate;;

        output_device_id = stream->output_device;
        if (output_device_id & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) {
            output_device_id |= AUDIO_DEVICE_OUT_SPEAKER;
            play_through_bt = true;
        }
        update_session_outputs_config(render_format, stream->channels, out_bitwidth, out_sample_rate);
        ret = qap_session_cmd(qap_session_handle, QAP_SESSION_CMD_SET_OUTPUTS, sizeof(session_output_config), &session_output_config, NULL, NULL);
        if (ret != QAP_STATUS_OK) {
            fprintf(stderr, "Output config failed\n");
            return -EINVAL;
        }
        qap_wrapper_is_dap_enabled(kv_pairs, stream->output_device, qap_session_handle);

        bitwidth_kvp = qap_wrapper_get_single_kvp("k", kv_pairs, &status);
        if (bitwidth_kvp && strncmp(bitwidth_kvp, "k=", 2) == 0) {
            fprintf(stdout, "Session set params, kvpair %s\n",&bitwidth_kvp[2]);
            ret = qap_session_cmd(qap_session_handle, QAP_SESSION_CMD_SET_KVPAIRS, (sizeof(bitwidth_kvp) - 2), &bitwidth_kvp[2], NULL, NULL);
            if (ret != QAP_STATUS_OK)
                fprintf(stderr, "Session set params failed\n");
        }
        usleep(2000);
        session_output_configured = true;
    }

    pthread_mutex_init(&main_eos_lock, (const pthread_mutexattr_t *)NULL);
    pthread_mutex_init(&main2_eos_lock, (const pthread_mutexattr_t *)NULL);
    pthread_mutex_init(&sec_eos_lock, (const pthread_mutexattr_t *)NULL);
    pthread_cond_init(&main_eos_cond, (const pthread_condattr_t *) NULL);
    pthread_cond_init(&main2_eos_cond, (const pthread_condattr_t *) NULL);
    pthread_cond_init(&sec_eos_cond, (const pthread_condattr_t *) NULL);
    fprintf(stdout, "Session open returing success\n");
    return 0;
}

int qap_wrapper_session_close ()
{
    ALOGD("closing QAP session");
    session_output_configured = false;
    qap_session_close(qap_session_handle);
    qap_session_handle = NULL;
    if (stream_cnt == 0) {
        if (NULL != m8_lib_handle) {
            qap_unload_library(m8_lib_handle);
            m8_lib_handle = NULL;
        }
        if (NULL != ms12_lib_handle) {
            qap_unload_library(ms12_lib_handle);
            ms12_lib_handle = NULL;
        }
    }
}

void *qap_wrapper_start_stream (void* stream_data)
{
    int ret = 0;
    qap_audio_buffer_t *buffer;
    int8_t first_read = 1;
    int bytes_wanted;
    int bytes_read;
    int bytes_consumed = 0, status = 0;;
    qap_module_handle_t qap_module_handle = NULL;
    stream_config *stream_info = (stream_config *)stream_data;
    FILE *fp_input = stream_info->file_stream;
    int is_buffer_available = 0;
    char *temp_str = NULL;
    void *reply_data;
    char* temp_ptr = NULL;
    qap_audio_format_t format;

    if (fp_input == NULL) {
        fprintf(stderr, "Open File Failed for %s\n", stream_info->filename);
        return NULL;
    }
    qap_module_handle = stream_info->qap_module_handle;
    buffer = (qap_audio_buffer_t *) calloc(1, sizeof(qap_audio_buffer_t));
    if (buffer == NULL) {
        fprintf(stderr, "%s::%d: Memory Alloc Error\n", __func__, __LINE__);
        return NULL;
    }
    buffer->common_params.data = calloc(1, FRAME_SIZE);
    if (buffer->common_params.data == NULL) {
        fprintf(stderr, "%s::%d: Memory Alloc Error\n", __func__, __LINE__);
        if (NULL != buffer) {
            free( buffer);
            buffer = NULL;
        }
        return NULL;
    }
    buffer->buffer_parms.output_buf_params.output_id = output_device_id;
    fprintf(stdout, "%s::%d: output device id %d\n",
                __func__, __LINE__, buffer->buffer_parms.output_buf_params.output_id);

    fprintf(stdout, "Opened Input File %s format %d handle %p\n", stream_info->filename, format, fp_input);

    ret = qap_module_cmd(qap_module_handle, QAP_MODULE_CMD_START, sizeof(QAP_MODULE_CMD_START), NULL, NULL, NULL);
    if (ret != QAP_STATUS_OK) {
        fprintf(stderr, "START failed\n");
        if (NULL != buffer &&  NULL != buffer->common_params.data) {
            free( buffer->common_params.data);
            buffer->common_params.data = NULL;
            free( buffer);
            buffer = NULL;
        }
        return NULL;
    }

    do {
        if (stream_info->filetype == FILE_WAV) {
            if (first_read) {
                first_read = 0;
                int wav_header_len = get_wav_header_length(stream_info->file_stream);
                fseek(fp_input, wav_header_len, SEEK_SET);
            }
            if (stream_info->channels > 6)
                stream_info->bytes_to_read = (FRAME_SIZE_FOR_2CH_PCM * 4);
            else
                stream_info->bytes_to_read = (FRAME_SIZE_FOR_2CH_PCM * 3);
        }
        buffer->buffer_parms.input_buf_params.flags = QAP_BUFFER_NO_TSTAMP;
        buffer->common_params.timestamp = QAP_BUFFER_NO_TSTAMP;
        buffer->common_params.size = stream_info->bytes_to_read;

        if (stream_info->timestamp_filename != NULL) {
            if (!stream_info->timestamp_file_ptr) {
                stream_info->timestamp_file_ptr = fopen(stream_info->timestamp_filename, "r");
                if (!stream_info->timestamp_file_ptr) {
                    fprintf(stderr, "Cannot open audio file %s\n", stream_info->filename);
                    goto exit;
                }
            }
            read_bytes_timestamps_from_file(buffer, stream_info->timestamp_file_ptr, fp_input);
            if (buffer->common_params.timestamp == CONTIGUOUS_TIMESTAMP)
                buffer->buffer_parms.input_buf_params.flags = QAP_BUFFER_TSTAMP_CONTINUE;
            else
                buffer->buffer_parms.input_buf_params.flags = QAP_BUFFER_TSTAMP;
            timestamp_mode = true;
        }

        bytes_wanted = buffer->common_params.size;
        bytes_read = fread(buffer->common_params.data, sizeof(unsigned char), bytes_wanted, fp_input);

        buffer->common_params.offset = 0;
        buffer->common_params.size = bytes_read;
        //memcpy(buffer->common_params.data, data_buf, bytes_read);
        if (bytes_read <= 0 || stop_playback) {
            buffer->buffer_parms.input_buf_params.flags = QAP_BUFFER_EOS;
            bytes_consumed = qap_module_process(qap_module_handle, buffer);
            if (stop_playback)
                qap_module_cmd(qap_module_handle, QAP_MODULE_CMD_FLUSH, sizeof(QAP_MODULE_CMD_FLUSH), NULL, NULL, NULL);

            ret = qap_module_cmd(qap_module_handle, QAP_MODULE_CMD_STOP, sizeof(QAP_MODULE_CMD_STOP), NULL, NULL, NULL);
            fprintf(stdout, "Stopped feeding input %s : %p\n", stream_info->filename, fp_input);
            ALOGV("Stopped feeding input %s : %p", stream_info->filename, fp_input);
            break;
        }

        reply_data = (char*) calloc(1, 100);
        is_buffer_available = 0;
        temp_ptr = buffer->common_params.data;
        int time_index = data_write_count;
        if (kpi_mode) {
            if (data_write_count > 5 && data_write_count < TIMESTAMP_ARRAY_SIZE) {
                gettimeofday(&tcont_ts1, NULL);
                data_input_ts_arr[data_write_count] = buffer->common_params.timestamp;
                data_input_st_arr[data_write_count] = (tcont_ts1.tv_sec) * 1000 + (tcont_ts1.tv_usec) / 1000;
                ALOGV("%s::%d Kpi cont ts1 %lf, buffer timestamp %ld count %d",
                      __func__, __LINE__, data_input_st_arr[data_write_count], data_input_ts_arr[data_write_count], data_write_count);
            }
            if (data_write_count < TIMESTAMP_ARRAY_SIZE)
                data_write_count++;
        }
        do {
            bytes_consumed = qap_module_process(qap_module_handle, buffer);
            if (bytes_consumed > 0) {
                buffer->common_params.data += bytes_consumed;
                buffer->common_params.size -= bytes_consumed;
            }
            ALOGV("%s %d, %s feeding Input of size %d  and bytes_cosumed is %d",
                      __FUNCTION__, __LINE__,stream_info->filename, bytes_read, bytes_consumed);
            {
                if (bytes_consumed < 0) {
                    pthread_mutex_lock(&stream_info->input_buffer_available_lock);
                    stream_info->input_buffer_available_size = 0;
                    pthread_mutex_unlock(&stream_info->input_buffer_available_lock);

                    while (buffer->common_params.size > stream_info->input_buffer_available_size) {
                        ALOGV("%s %d: %s waiting for input buffer availability.",
                                     __FUNCTION__, __LINE__, stream_info->filename);
                        pthread_mutex_lock(&stream_info->input_buffer_available_lock);
                        pthread_cond_wait(&stream_info->input_buffer_available_cond,
                                          &stream_info->input_buffer_available_lock);
                        pthread_mutex_unlock(&stream_info->input_buffer_available_lock);
                        ALOGV("%s %d: %s input buffer available, size %lu.",
                                     __FUNCTION__, __LINE__,
                                     stream_info->filename,
                                     stream_info->input_buffer_available_size);
                    }
                    if(kpi_mode && time_index > 5) {
                        gettimeofday(&tcont_ts1, NULL);
                        data_input_st_arr[time_index] = (tcont_ts1.tv_sec) * 1000 + (tcont_ts1.tv_usec) / 1000;
                    }
                }
            }
        } while (buffer->common_params.size > 0 && !stop_playback);
        if (reply_data)
            free(reply_data);
        buffer->common_params.data = temp_ptr;
        if (!(stream_info->system_input || stream_info->sec_input) && !(kpi_mode)) {
            usleep(5000); //To swtich between main and secondary threads incase of dual input
        }
    } while (1);

wait_for_eos:
    if (stream_info->sec_input) {
        if (!(stream_info->flags & AUDIO_OUTPUT_FLAG_ASSOCIATED)) {
            if (!main2_eos_received) {
                pthread_mutex_lock(&main2_eos_lock);
                pthread_cond_wait(&main2_eos_cond, &main2_eos_lock);
                pthread_mutex_unlock(&main2_eos_lock);
            }
            main2_eos_received = false;
            fprintf(stdout, "Received EOS event for main2 input\n");
            ALOGV("Received EOS event for main2 input\n");
        } else {
            if (!sec_eos_received) {
                pthread_mutex_lock(&sec_eos_lock);
                pthread_cond_wait(&sec_eos_cond, &sec_eos_lock);
                pthread_mutex_unlock(&sec_eos_lock);
            }
            sec_eos_received = false;
            fprintf(stdout, "Received EOS event for secondary input\n");
            ALOGV("Received EOS event for secondary input\n");
        }
    }
    if (!(stream_info->system_input || stream_info->sec_input)){
        if (!main_eos_received) {
            pthread_mutex_lock(&main_eos_lock);
            pthread_cond_wait(&main_eos_cond, &main_eos_lock);
            pthread_mutex_unlock(&main_eos_lock);
        }
        main_eos_received = false;
        fprintf(stdout, "Received EOS event for main input\n");
        ALOGV("Received EOS event for main input\n");
    }

exit:
    if (NULL != buffer &&  NULL != buffer->common_params.data) {
        free( buffer->common_params.data);
        buffer->common_params.data = NULL;
        free( buffer);
        buffer = NULL;
    }
    qap_module_deinit(qap_module_handle);
    if ((true == play_list) && (0 == play_list_cnt) && qap_out_hal_handle) {
         ALOGV("%s %d QAP_CALLBACK_EVENT_EOS for play list received", __func__, __LINE__);
         qap_out_hal_handle = NULL;
    } else if (!play_list && qap_out_hal_handle) {
         ALOGV("%s %d QAP_CALLBACK_EVENT_EOS received", __func__, __LINE__);
         qap_out_hal_handle = NULL;
    }
    if (kpi_mode) {
        qap_wrapper_measure_kpi_values(cold_start, cold_stop);
    }
    fprintf(stdout, "%s::%d , THREAD EXIT \n", __func__, __LINE__);
    ALOGD("%s::%d , THREAD EXIT \n", __func__, __LINE__);
    return NULL;
}

qap_module_handle_t qap_wrapper_stream_open(void* stream_data)
{
    qap_module_config_t input_config = {0};
    int ret = 0;
    int i = 0;
    stream_config *stream_info = (stream_config *)stream_data;
    qap_module_handle_t qap_module_handle = NULL;

    input_config.sample_rate = stream_info->config.sample_rate;
    input_config.channels = stream_info->channels;
    input_config.bit_width = stream_info->config.offload_info.bit_width;

    if (stream_info->filetype == FILE_DTS)
        stream_info->bytes_to_read = FRAME_SIZE;
    else
        stream_info->bytes_to_read = 1024;
    input_streams_count++;

    if (stream_info->filetype == FILE_WAV) {
        switch (stream_info->flags)
        {
            case QAP_MODULE_FLAG_SYSTEM_SOUND:
                ALOGV("%s::%d Set System Sound Flag", __func__, __LINE__);
                break;
            case QAP_MODULE_FLAG_APP_SOUND:
                ALOGV("%s::%d Set System APP Flag", __func__, __LINE__);
                break;
            case QAP_MODULE_FLAG_OTT_SOUND:
                ALOGV("%s::%d Set OTT Sound Flag", __func__, __LINE__);
                break;
            default:
                ALOGE("%s::%d unsupported flag for PCM input.", __func__, __LINE__);
                return NULL;
        }
        input_config.flags = stream_info->flags;
        stream_info->system_input = true;
        has_system_input = true;
    } else {
        if (input_streams_count > 1) {
            if (stream_info->flags & AUDIO_OUTPUT_FLAG_ASSOCIATED) {
                ALOGV("%s::%d Set Secondary Assoc Input Flag", __func__, __LINE__);
                input_config.flags = QAP_MODULE_FLAG_SECONDARY;
                stream_info->sec_input = true;
            } else {
                ALOGV("%s::%d Set Secondary Main Input Flag", __func__, __LINE__);
                input_config.flags = QAP_MODULE_FLAG_PRIMARY;
                stream_info->sec_input = true;
            }
            stream_info->bytes_to_read = 2048;
        } else {
            if (stream_info->flags & AUDIO_OUTPUT_FLAG_ASSOCIATED) {
                ALOGV("%s::%d Set Secondary Assoc Input Flag", __func__, __LINE__);
                input_config.flags = QAP_MODULE_FLAG_SECONDARY;
                stream_info->sec_input = true;
            } else {
                ALOGV("%s::%d Set Primary Main Input Flag", __func__, __LINE__);
                input_config.flags = QAP_MODULE_FLAG_PRIMARY;
            }
        }
    }

    if (!encode)
        input_config.module_type = QAP_MODULE_DECODER;
    else
        input_config.module_type = QAP_MODULE_ENCODER;

    ret = qap_wrapper_map_input_format(stream_info->config.offload_info.format, &input_config.format);
    if (ret == -EINVAL)
        return NULL;

    ret = qap_module_init(qap_session_handle, &input_config, &qap_module_handle);
    if (qap_module_handle == NULL) {
        fprintf(stderr, "%s Module Handle is Null\n", __func__);
        return NULL;
    }

    qap_module_set_callback(qap_module_handle, &qap_wrapper_module_callback, stream_info);

    primary_stream_close = false;
    stream_cnt++;
    return qap_module_handle;

}
void get_play_list(FILE *fp, stream_config (*stream_param)[], int *num_of_streams, char *kvp_str[])
{
    char *token = NULL;
    char *strings[100] = {NULL};
    char cmd_str[1024] = {0};
    char *tmp_str = NULL;
    int i = 0;

    do {
        int j = 0, cnt = 0, status = 0;

        if (fgets(cmd_str, sizeof(cmd_str), fp) != NULL)
            tmp_str = strdup(cmd_str);
        else
            break;
        fprintf(stdout, "%s %d tmp_str is %s", __FUNCTION__, __LINE__, tmp_str);
        token = strtok(tmp_str, " ");
        if (NULL != token) {
            strings[cnt++] = strdup("playlist");
            strings[cnt++] = strdup(token);
            while (NULL != (token = strtok(NULL, " "))) {
                strings[cnt] = strdup(token);
                ALOGV("%s %d strings[%d] is %s", __FUNCTION__, __LINE__, cnt, strings[cnt]);
                cnt++;
            }
            for (j = 0;j< cnt;j++) {
                if (!strncmp(strings[j], "-f", 2)) {
                   (*stream_param)[i].filename = strdup(strings[j+1]);
                } else if (!strncmp(strings[j], "-r", 2)) {
                    (*stream_param)[i].config.offload_info.sample_rate = atoi(strings[j+1]);
                    (*stream_param)[i].config.sample_rate = atoi(strings[j+1]);
                } else if (!strncmp(strings[j], "-c", 2)) {
                   (*stream_param)[i].channels = atoi(strings[j+1]);
                   (*stream_param)[i].config.channel_mask = audio_channel_out_mask_from_count(atoi(strings[j+1]));
                } else if (!strncmp(strings[j], "-b", 2)) {
                    (*stream_param)[i].config.offload_info.bit_width = atoi(strings[j+1]);
                } else if (!strncmp(strings[j], "-d", 2)) {
                    (*stream_param)[i].output_device = atoll(strings[j+1]);
                } else if (!strncmp(strings[j], "-t", 2)) {
                    (*stream_param)[i].filetype = atoi(strings[j+1]);
                } else if (!strncmp(strings[j], "-a", 2)) {
                    (*stream_param)[i].aac_fmt_type = atoi(strings[j+1]);
                }
            }
            free(tmp_str);
            tmp_str = NULL;
        }
        if(NULL != (*stream_param)[i].filename) {
            *num_of_streams = i+1;
            play_list = true;
            kvp_str[i] = (char *)qap_wrapper_get_cmd_string_from_arg_array(cnt, strings, &status);
            ALOGV("%s %d kvp_str[%d] is %s", __FUNCTION__, __LINE__, i, kvp_str[i]);
        }
        for (j=0; j < cnt; j++) {
           if (NULL != strings[j]){
               free(strings[j]);
               strings[j] = NULL;
           }
        }
        i++;
    }while(NULL != cmd_str);

    return;
}

void hal_test_qap_usage() {
    printf(" \n qap commands \n");
    printf(" -qap                                      - Enabling playback through QAP for nun tunnel decoding mode\n");
    printf(" -bd                                       - Enabling Broadcast Decode/Encode session through QAP\n");
    printf(" -broadcast                                - Enabling playback through QAP for nun tunnel decoding mode\n");
    printf(" -y  --timestamp filename                  - Input timestamp file to be used to send timestamp and bytes to be read from main input file.\n");
    printf(" -z  --framesize filename                  - Input framesize file to be used to send bytes to be read from main input file.\n");
    printf(" hal_play_test -qap -broadcast -f /data/5ch_dd_25fps_channeld_id.ac3 -t 9 -d 2 -v 0.01 -r 48000 -c 6 \n");
    printf("                                          -> plays AC3 stream(-t = 9) on speaker device(-d = 2)\n");
    printf("                                          -> 6 channels and 48000 sample rate\n\n");
    printf("                                          -> using QAP with Broadcast session\n\n");
    printf(" hal_play_test -qap -bd -f /data/200_48_16_ieq_mix_voice_40s.ec3 -t 11 -d 2 -v 0.01 -r 48000 -c 2 \n");
    printf("                                          -> plays EAC3 stream(-t = 11) on speaker device(-d = 2)\n");
    printf("                                          -> 2 channels and 48000 sample rate\n\n");
    printf("                                          -> using QAP with Bluray session\n\n");
}