summaryrefslogtreecommitdiff
path: root/mm_sound_client.c
blob: 18daf8e44d003e73ade5e45b24a37cffc74c78db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
/*
 * libmm-sound
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungbae Shin <seungbae.shin@samsung.com>
 *
 * 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.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <assert.h>
#include <errno.h>

#include <pthread.h>
#include <semaphore.h>

#include <mm_error.h>
#include <mm_debug.h>

#include "include/mm_sound.h"
#include "include/mm_sound_msg.h"
#include "include/mm_sound_client.h"
#include "include/mm_sound_common.h"
#include "include/mm_sound_device.h"

#include <mm_session.h>
#include <mm_session_private.h>

#define __DIRECT_CALLBACK__
//#define __GIDLE_CALLBACK__

#include <glib.h>
#if defined(__GSOURCE_CALLBACK__)
#include <sys/poll.h>
#endif

#define CLIENT_HANDLE_MAX 256

#define MEMTYPE_SUPPORT_MAX (1024 * 1024) /* 1MB */
#define MEMTYPE_TRANS_PER_MAX (128 * 1024) /* 128K */

int g_msg_scsnd;	/* global msg queue id sound client snd */
int g_msg_scrcv;	/* global msg queue id sound client rcv */
int g_msg_sccb;		/* global msg queue id sound client callback */

/* global variables for device list */
static __thread GList *g_device_list = NULL;
static __thread mm_sound_device_list_t g_device_list_t;

/* callback */
struct __callback_param
{
	mm_sound_stop_callback_func     callback;
    void                    *data;
};

pthread_t g_thread;
static int g_exit_thread = 0;
int g_thread_id = -1;
int g_mutex_initted = -1;
pthread_mutex_t g_thread_mutex;

static void* callbackfunc(void *param);

/* manage IPC (msg contorl) */
static int __MMIpcCBSndMsg(mm_ipc_msg_t *msg);
static int __MMIpcRecvMsg(int msgtype, mm_ipc_msg_t *msg);
static int __MMIpcSndMsg(mm_ipc_msg_t *msg);
static int __MMIpcCBRecvMsg(int msgtype, mm_ipc_msg_t *msg);
static int __MMSoundGetMsg(void);

int MMSoundClientInit(void)
{
	int ret = MM_ERROR_NONE;
	debug_fenter();

	debug_fleave();
	return ret;
}

int MMSoundClientCallbackFini(void)
{
	mm_ipc_msg_t msgsnd={0,};
	int ret = MM_ERROR_NONE;

	debug_fenter();

	/* When the the callback thread is not created, do not wait destroy thread */
	/* g_thread_id is initialized : -1 */
	/* g_thread_id is set to 0, when the callback thread is created */
	if (g_thread_id != -1)
	{
		msgsnd.sound_msg.msgtype = MM_SOUND_MSG_INF_DESTROY_CB;
		msgsnd.sound_msg.msgid = getpid();
		ret = __MMIpcCBSndMsg(&msgsnd);
		if (ret != MM_ERROR_NONE)
		{
			debug_critical("[Client] Fail to send message\n");
		}
		pthread_join(g_thread, 0);
	}

	if (g_mutex_initted != -1)
	{
		pthread_mutex_destroy(&g_thread_mutex);
		g_mutex_initted = -1;
	}
	debug_fleave();
	return MM_ERROR_NONE;
}

#if defined(__GSOURCE_CALLBACK__)
gboolean sndcb_fd_check(GSource * source)
{
	GSList *fd_list;
	fd_list = source->poll_fds;
	GPollFD* temp;

	do
	{
		temp = (GPollFD*)fd_list->data;
		if (temp->revents & (POLLIN|POLLPRI))
			return TRUE;
		fd_list = fd_list->next;
	}while(fd_list);

	return FALSE; /* there is no change in any fd state */
}

gboolean sndcb_fd_prepare(GSource *source, gint *timeout)
{
	return FALSE;
}

gboolean sndcb_fd_dispatch(GSource *source,	GSourceFunc callback, gpointer user_data)
{
	callback(user_data);
	return TRUE;
}
#endif
gboolean RunCallback(gpointer data)
{
	mm_ipc_msg_t* msg = NULL;

	debug_msg("[Client] execute mm_sound stop callback function\n");

	msg = (mm_ipc_msg_t*)data;
	((mm_sound_stop_callback_func)msg->sound_msg.callback)(msg->sound_msg.cbdata, msg->sound_msg.handle);

	return FALSE;
}

gboolean _volume_change_cb(gpointer data)
{
	mm_ipc_msg_t* msg = NULL;

	if (!data) {
		debug_error("[Client] NULL param\n");
		return FALSE;
	}
	msg = (mm_ipc_msg_t*)data;

	((mm_sound_volume_changed_cb)msg->sound_msg.callback)(msg->sound_msg.type, msg->sound_msg.val, msg->sound_msg.cbdata);

	if(msg != NULL) {
		free(msg);
		msg = NULL;
	}
	return FALSE;
}

static void* callbackfunc(void *param)
{
	int ret = MM_ERROR_SOUND_INTERNAL;
	mm_ipc_msg_t *msgrcv = NULL;
	int run = 1;
	int instance;

	debug_fenter();

	instance = getpid();
	debug_msg("[Client] callback thread for [%d] is created\n", instance);

	msgrcv = (mm_ipc_msg_t*)malloc(sizeof(mm_ipc_msg_t));
	if(NULL == msgrcv)
	{
		debug_critical("[Client] Failed to memory allocation\n");
		return NULL;
	}

	while(run)
	{
#if defined(__GSOURCE_CALLBACK__)
		int eventFd = 0;
		gchar* eventFile = NULL;
		GSource* cmd_fd_gsrc = NULL;
		GSourceFuncs *src_funcs = NULL;		// handler function
		guint gsource_handle;
		GPollFD *g_fd_cmd = NULL;			// file descriptor
#endif

		debug_msg("[Client] Waiting message\n");
		ret = __MMIpcCBRecvMsg(instance, msgrcv);
		if (ret != MM_ERROR_NONE)
		{
			debug_error("[Client] Fail to receive msg in callback\n");
			continue;
		}

		debug_msg("[Client] Receive msgtype : [%d]\n", msgrcv->sound_msg.msgtype);

		switch (msgrcv->sound_msg.msgtype)
		{
		case MM_SOUND_MSG_INF_STOP_CB:
			debug_msg("[Client] callback : %p, data : %p\n", msgrcv->sound_msg.callback, msgrcv->sound_msg.cbdata);

			if (msgrcv->sound_msg.callback)
			{
#if defined(__DIRECT_CALLBACK__)
				((mm_sound_stop_callback_func)msgrcv->sound_msg.callback)(msgrcv->sound_msg.cbdata, msgrcv->sound_msg.handle);
#elif defined(__GIDLE_CALLBACK__)
				guint eventid = 0;
				eventid = g_idle_add((GSourceFunc)RunCallback, (gpointer)msgrcv);
				debug_msg("[Client] Event Source ID : %d\n", eventid);

#elif defined(__GSOURCE_CALLBACK__)
				char eventBuf[3]="OK";
				////////////////////////
				// 0. Make event source
				eventFile = g_strdup_printf("/tmp/%d_0x%08x_0x%08x", instance, (unsigned int)msgrcv->sound_msg.callback, (unsigned int)msgrcv->sound_msg.cbdata);
				eventFd = open(eventFile, O_RDWR|O_CREAT);
				if(eventFd == -1)
				{
					debug_critical("Event File creation failed\n");
					break;
				}

				// 1. make GSource Object
				src_funcs = (GSourceFuncs *)g_malloc(sizeof(GSourceFuncs));
				if(!src_funcs){
					debug_error("MMSoundCallback :  g_malloc failed on g_src_funcs");
					break;
				}
				src_funcs->prepare = sndcb_fd_prepare;
				src_funcs->check = sndcb_fd_check;
				src_funcs->dispatch = sndcb_fd_dispatch;
				src_funcs->finalize = NULL;
				cmd_fd_gsrc = g_source_new(src_funcs,sizeof(GSource));
			 	if(!cmd_fd_gsrc){
			 		debug_error("MMSoundCallback : g_malloc failed on m_readfd");
			 		break;
			 	}

			 	// 2. add file description which used in g_loop()
			 	g_fd_cmd = (GPollFD*)g_malloc(sizeof(GPollFD));
			 	g_fd_cmd->fd = eventFd;
			 	g_fd_cmd->events = POLLIN|POLLPRI;

			 	// 3. combine g_source object and file descriptor
			 	g_source_add_poll(cmd_fd_gsrc,g_fd_cmd);
			 	gsource_handle = g_source_attach(cmd_fd_gsrc, NULL);
			 	if(!gsource_handle){
			 		debug_error("MMSoundCallback : Error: Failed to attach the source to context");
			 		break;
			 	}

			 	// 4. set callback
			 	g_source_set_callback(cmd_fd_gsrc,RunCallback,(gpointer)g_fd_cmd,NULL);
			 	debug_msg("MMSoundCallback : g_source_set_callback() done\n")

			 	// 5. Set Event
			 	write(eventFd, eventBuf, sizeof(eventBuf));
			 	sleep(1);
			 	// 6. Cleanup
			 	close(eventFd);
			 	unlink(eventFile);
			 	g_source_remove_poll(cmd_fd_gsrc, g_fd_cmd);
			 	g_source_remove(gsource_handle);
			 	if(g_fd_cmd)
			 		free(g_fd_cmd);
			 	if(src_funcs)
			 		free(src_funcs);
			 	if(eventFile)
			 		g_free(eventFile);
				////////////////////////
#endif
			}
			break;
		case MM_SOUND_MSG_INF_DESTROY_CB:
			run = 0;
			break;

		case MM_SOUND_MSG_INF_DEVICE_CONNECTED_CB:
			debug_msg("[Client] device handle : %x, is_connected : %d, callback : %p, data : %p\n",
				&msgrcv->sound_msg.device_handle, msgrcv->sound_msg.is_connected, msgrcv->sound_msg.callback, msgrcv->sound_msg.cbdata);
			if (msgrcv->sound_msg.callback) {
				((mm_sound_device_connected_cb)msgrcv->sound_msg.callback)(&msgrcv->sound_msg.device_handle, msgrcv->sound_msg.is_connected, msgrcv->sound_msg.cbdata);
			}
			break;

		case MM_SOUND_MSG_INF_DEVICE_INFO_CHANGED_CB:
			debug_msg("[Client] device handle : %x, changed_info_type : %d, callback : %p, data : %p\n",
				&msgrcv->sound_msg.device_handle, msgrcv->sound_msg.changed_device_info_type, msgrcv->sound_msg.callback, msgrcv->sound_msg.cbdata);
			if (msgrcv->sound_msg.callback) {
				((mm_sound_device_info_changed_cb)msgrcv->sound_msg.callback)(&msgrcv->sound_msg.device_handle, msgrcv->sound_msg.changed_device_info_type, msgrcv->sound_msg.cbdata);
			}
			break;

		case MM_SOUND_MSG_INF_ACTIVE_DEVICE_CB:
			debug_msg("[Client] device_in : %d, device_out : %d\n", msgrcv->sound_msg.device_in, msgrcv->sound_msg.device_out);
			debug_log("[Client] callback : %p, data : %p\n", msgrcv->sound_msg.callback, msgrcv->sound_msg.cbdata);

			if (msgrcv->sound_msg.callback)
			{
				((mm_sound_active_device_changed_cb)msgrcv->sound_msg.callback)(msgrcv->sound_msg.device_in, msgrcv->sound_msg.device_out, msgrcv->sound_msg.cbdata);
			}
			break;
		case MM_SOUND_MSG_INF_AVAILABLE_ROUTE_CB:
			debug_log("[Client] callback : %p, data : %p\n", msgrcv->sound_msg.callback, msgrcv->sound_msg.cbdata);

			if (msgrcv->sound_msg.callback)
			{
				int route_index;
				mm_sound_route route;

				int list_count = sizeof(msgrcv->sound_msg.route_list) / sizeof(int);

				for (route_index = list_count-1; route_index >= 0; route_index--) {
					route = msgrcv->sound_msg.route_list[route_index];
					if (route == 0)
						continue;
					if (msgrcv->sound_msg.is_available) {
						debug_msg("[Client] available route : 0x%x\n", route);
					} else {
						debug_msg("[Client] unavailable route : 0x%x\n", route);
					}
					((mm_sound_available_route_changed_cb)msgrcv->sound_msg.callback)(route, msgrcv->sound_msg.is_available, msgrcv->sound_msg.cbdata);

					if (route == MM_SOUND_ROUTE_INOUT_HEADSET || route == MM_SOUND_ROUTE_IN_MIC_OUT_HEADPHONE) {
						debug_msg("[Client] no need to proceed further more....\n");
						break;
					}
				}
			}
			break;
		case MM_SOUND_MSG_INF_VOLUME_CB:
			debug_msg("[Client] type: %d, volume value : %d, callback : %p, data : %p\n", msgrcv->sound_msg.type, msgrcv->sound_msg.val, msgrcv->sound_msg.callback, msgrcv->sound_msg.cbdata);

			if (msgrcv->sound_msg.callback)
			{
				mm_ipc_msg_t* tmp = NULL;
				tmp = (mm_ipc_msg_t*)malloc(sizeof(mm_ipc_msg_t));
				if(tmp == NULL) {
					debug_critical("Fail malloc");
					break;
				}
				memcpy(tmp, msgrcv, sizeof(mm_ipc_msg_t));
				g_idle_add((GSourceFunc)_volume_change_cb, (gpointer)tmp);
			}
			break;

		default:
			/* Unexpected msg */
			debug_msg("Receive wrong msg in callback func\n");
			break;
		}
	}
	if(msgrcv) {
		free(msgrcv);
	}

	g_exit_thread = 1;
	debug_msg("[Client] callback [%d] is leaved\n", instance);
	debug_fleave();
	return NULL;
}

static int __mm_sound_client_get_msg_queue(void)
{
	int ret = MM_ERROR_NONE;

	if (g_mutex_initted == -1)
	{
		if(pthread_mutex_init(&g_thread_mutex, NULL)) {
			debug_error("pthread_mutex_init failed\n");
			return MM_ERROR_SOUND_INTERNAL;
		}
		debug_log("[Client] mutex initialized. \n");
		g_mutex_initted = 1;

		/* Get msg queue id */
		ret = __MMSoundGetMsg();
		if(ret != MM_ERROR_NONE)
		{
			debug_error("[Client] Fail to get message queue id. sound_server is not initialized.\n");
			pthread_mutex_destroy(&g_thread_mutex);
			g_mutex_initted = -1;
		}
	}

	return ret;
}

int MMSoundClientPlayTone(int number, int volume_config, double volume, int time, int *handle, bool enable_session)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};

	int ret = MM_ERROR_NONE;
	int instance = -1; 	/* instance is unique to communicate with server : client message queue filter type */

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}
	/* read session information */
	int session_type = MM_SESSION_TYPE_MEDIA;
	int session_options = 0;
	if (enable_session)
	{
		if (MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options))
		{
			debug_warning("[Client] Read Session Information failed. use default \"media\" type\n");
			session_type = MM_SESSION_TYPE_MEDIA;

			if(MM_ERROR_NONE != mm_session_init(session_type))
			{
				debug_critical("[Client] MMSessionInit() failed\n");
				return MM_ERROR_POLICY_INTERNAL;
			}
		}
	}

	instance = getpid();
	debug_log("[Client] pid for client ::: [%d]\n", instance);

	pthread_mutex_lock(&g_thread_mutex);

	/* Send msg */
	debug_msg("[Client] Input number : %d\n", number);
	/* Send req memory */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_DTMF;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.session_type = session_type;       //session type
	msgsnd.sound_msg.session_options = session_options; //session options
	msgsnd.sound_msg.volume = volume;                   //This does not effect anymore
	msgsnd.sound_msg.volume_config = volume_config;
	msgsnd.sound_msg.tone = number;
	msgsnd.sound_msg.handle = -1;
	msgsnd.sound_msg.repeat = time;
	msgsnd.sound_msg.enable_session = enable_session;

	ret = __MMIpcSndMsg(&msgsnd);
	if (ret != MM_ERROR_NONE)
	{
		debug_error("[Client] Fail to send msg\n");
		goto cleanup;
	}

	/* Receive */
	ret = __MMIpcRecvMsg(instance, &msgrcv);
	if (ret != MM_ERROR_NONE)
	{
		debug_error("[Client] Fail to recieve msg\n");
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_DTMF:
		*handle = msgrcv.sound_msg.handle;
		if(*handle == -1) {
			debug_error("[Client] The handle is not get\n");
		} else {
			debug_msg("[Client] Success to play sound sound handle : [%d]\n", *handle);
		}
		break;

	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;

	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}
cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}


int MMSoundClientPlaySound(MMSoundPlayParam *param, int tone, int keytone, int *handle)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	unsigned char* sharedmem = NULL;

	int ret = MM_ERROR_NONE;
	int instance = -1; 	/* instance is unique to communicate with server : client message queue filter type */

	char shm_name[512];
	void *mmap_buf = NULL;
	static int keybase = 0;
	int shm_fd = -1;

	debug_fenter();
	memset(shm_name, 0, sizeof(shm_name));

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}
	/* read session information */
	int session_type = MM_SESSION_TYPE_MEDIA;
	int session_options = 0;

	if (param->skip_session == false) {
		if(MM_ERROR_NONE != _mm_session_util_read_information(-1, &session_type, &session_options))
		{
			debug_warning("[Client] Read MMSession Type failed. use default \"media\" type\n");
			session_type = MM_SESSION_TYPE_MEDIA;

			if(MM_ERROR_NONE != mm_session_init(session_type))
			{
				debug_critical("[Client] MMSessionInit() failed\n");
				return MM_ERROR_POLICY_INTERNAL;
			}
		}
	}

	instance = getpid();
	debug_msg("[Client] pid for client ::: [%d]\n", instance);

	/* callback */
	/* callback thread is created just once & when the callback is exist */
	if (param->callback)
	{
		if (g_thread_id == -1)
		{
			g_thread_id = pthread_create(&g_thread, NULL, callbackfunc, NULL);
			if (g_thread_id == -1)
			{
				debug_critical("[Client] Fail to create thread %s\n", strerror(errno));
				return MM_ERROR_SOUND_INTERNAL;
			}
		}
	}

	pthread_mutex_lock(&g_thread_mutex);

	/* Send msg */
	if ((param->mem_ptr && param->mem_size))
	{
		debug_msg("The memptr : [%p]\n", param->mem_ptr);
		debug_msg("The memptr : [%d]\n", param->mem_size);
		/* Limited memory size */
		if (param->mem_ptr && param->mem_size > MEMTYPE_SUPPORT_MAX)
		{
			debug_msg("[Client] Memory size is too big. We support size of media to 1MB\n");
			goto cleanup;
		}

		debug_msg("[Client] memory size : %d\n", param->mem_size);
		snprintf(shm_name, sizeof(shm_name)-1, "%d_%d", instance, keybase);
		debug_msg("[Client] The shm_path : [%s]\n", shm_name);
		keybase++;

		shm_fd = shm_open(shm_name, O_RDWR |O_CREAT, 0666);
		if(shm_fd < 0)
		{
			perror("[Client] Fail create shm_open\n");
			debug_error("[Client] Fail to create shm_open\n");
			goto cleanup;
		}

		if(ftruncate(shm_fd, param->mem_size) == -1)
		{
			debug_error("[Client] Fail to ftruncate\n");
			goto cleanup;
		}

		mmap_buf = mmap (0, MEMTYPE_SUPPORT_MAX, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
		if (mmap_buf == MAP_FAILED)
		{
			debug_error("[Client] MMAP failed (%s)\n", strerror(errno));
			goto cleanup;
		}

		sharedmem = mmap_buf;

		debug_msg("[Client] Sheared mem ptr : %p\n", sharedmem);
		debug_msg("[Client] Sheared key : [%d]\n", 1);
		/* Send req memory */
		msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_MEMORY;
		msgsnd.sound_msg.msgid = instance;
		msgsnd.sound_msg.callback = (void*)(param->callback);
		msgsnd.sound_msg.cbdata = (void*)(param->data);
		msgsnd.sound_msg.memptr = (int)sharedmem;
		msgsnd.sound_msg.sharedkey = 1;		/* In case of shared memory file name */
		msgsnd.sound_msg.session_type = session_type;       //session type
		msgsnd.sound_msg.session_options = session_options; //session options
		msgsnd.sound_msg.priority = param->priority;

		MMSOUND_STRNCPY(msgsnd.sound_msg.filename, shm_name, FILE_PATH);
		debug_msg("[Client] shm_name : %s\n", msgsnd.sound_msg.filename);

		msgsnd.sound_msg.memsize = param->mem_size;
		msgsnd.sound_msg.volume = param->volume;
		msgsnd.sound_msg.tone = tone;
		msgsnd.sound_msg.handle = -1;
		msgsnd.sound_msg.repeat = param->loop;
		msgsnd.sound_msg.volume_config = param->volume_config;
		msgsnd.sound_msg.keytone = keytone;
		msgsnd.sound_msg.handle_route = param->handle_route;
		msgsnd.sound_msg.enable_session = !param->skip_session;

		/* Send req memory */
		debug_msg("[Client] Shared mem ptr %p, %p, size %d\n", sharedmem, param->mem_ptr, param->mem_size);
		memcpy(sharedmem, param->mem_ptr, param->mem_size);


		if (close(shm_fd) == -1)
		{
			debug_error("[Client] Fail to close file\n");
			ret = MM_ERROR_SOUND_INTERNAL;
			goto cleanup;
		}

		ret = __MMIpcSndMsg(&msgsnd);
		if (ret != MM_ERROR_NONE)
		{
			debug_error("[Client] Fail to send msg\n");
			goto cleanup;
		}
	}
	else
	{
		/* File type set for send msg */
		msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_FILE;
		msgsnd.sound_msg.msgid = instance;
		msgsnd.sound_msg.callback = (void*)(param->callback);
		msgsnd.sound_msg.cbdata = (void*)(param->data);
		msgsnd.sound_msg.volume = param->volume;
		msgsnd.sound_msg.tone = tone;
		msgsnd.sound_msg.handle = -1;
		msgsnd.sound_msg.repeat = param->loop;
		msgsnd.sound_msg.volume_config = param->volume_config;
		msgsnd.sound_msg.session_type = session_type;       //session type
		msgsnd.sound_msg.session_options = session_options; //session options
		msgsnd.sound_msg.priority = param->priority;
		msgsnd.sound_msg.handle_route = param->handle_route;
		msgsnd.sound_msg.enable_session = !param->skip_session;

		if((strlen(param->filename)) < FILE_PATH)
		{
			MMSOUND_STRNCPY(msgsnd.sound_msg.filename, param->filename, FILE_PATH);
		}
		else
		{
			debug_error("File name is over count\n");
			ret = MM_ERROR_SOUND_INVALID_PATH;
		}

		msgsnd.sound_msg.keytone = keytone;

		debug_msg("[Client] callback : %p\n", msgsnd.sound_msg.callback);
		debug_msg("[Client] cbdata : %p\n", msgsnd.sound_msg.cbdata);

		ret = __MMIpcSndMsg(&msgsnd);
		if (ret != MM_ERROR_NONE)
		{
			debug_error("[Client] Fail to send msg\n");
			goto cleanup;
		}
	}


	/* Receive */
	ret = __MMIpcRecvMsg(instance, &msgrcv);
	if (ret != MM_ERROR_NONE)
	{
		debug_error("[Client] Fail to recieve msg\n");
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_FILE:
		*handle = msgrcv.sound_msg.handle;
		debug_msg("[Client] Success to play sound sound handle : [%d]\n", *handle);
		break;
	case MM_SOUND_MSG_RES_MEMORY:
		*handle = msgrcv.sound_msg.handle;
		if(*handle == -1) {
			debug_error("[Client] The handle is not get\n");
		}
		int r = shm_unlink(shm_name);
		if (r == -1)
		{
			debug_critical("[Client] Fail to remove shared memory, must be checked\n");
			goto cleanup;
		}
		debug_msg("[Client] Success to play sound sound handle : [%d]\n", *handle);
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}
cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int MMSoundClientStopSound(int handle)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();
	debug_msg("[Client] The stop audio handle ::: [%d]\n", handle);

	instance = getpid();

	if (handle < 0 || handle > CLIENT_HANDLE_MAX) 
	{
		ret = MM_ERROR_INVALID_ARGUMENT;
		return ret;
	}


	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	/* Send req STOP */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_STOP;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.handle = handle;		/* handle means audio handle slot id */

	ret = __MMIpcSndMsg(&msgsnd);
	if (ret != MM_ERROR_NONE)
	{
		debug_error("Fail to send msg\n");
		goto cleanup;
	}

	/* Receive */
	ret = __MMIpcRecvMsg(instance, &msgrcv);
	if (ret != MM_ERROR_NONE)
	{
		debug_error("[Client] Fail to recieve msg\n");
		goto cleanup;
	}
	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_STOP:
		debug_msg("[Client] Success to stop sound\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);


	debug_fleave();
	return ret;
}

static int __mm_sound_device_check_flags_to_append (int device_flags, mm_sound_device_t *device_h, bool *is_good_to_append)
{
	bool need_to_append = false;
	int need_to_check_for_io_direction = device_flags & DEVICE_IO_DIRECTION_FLAGS;
	int need_to_check_for_state = device_flags & DEVICE_STATE_FLAGS;
	int need_to_check_for_type = device_flags & DEVICE_TYPE_FLAGS;

	debug_warning("device_h[0x%x], device_flags[0x%x], need_to_check(io_direction[0x%x],state[0x%x],type[0x%x])\n",
			device_h, device_flags, need_to_check_for_io_direction, need_to_check_for_state, need_to_check_for_type);

	if(!device_h) {
		return MM_ERROR_INVALID_ARGUMENT;
	}
	if (device_flags == DEVICE_ALL_FLAG) {
		*is_good_to_append = true;
		return MM_ERROR_NONE;
	}

	if (need_to_check_for_io_direction) {
		if ((device_h->io_direction == DEVICE_IO_DIRECTION_IN || device_h->io_direction == DEVICE_IO_DIRECTION_BOTH) && (device_flags & DEVICE_IO_DIRECTION_IN_FLAG)) {
			need_to_append = true;
		} else if ((device_h->io_direction == DEVICE_IO_DIRECTION_OUT || device_h->io_direction == DEVICE_IO_DIRECTION_BOTH) && (device_flags & DEVICE_IO_DIRECTION_OUT_FLAG)) {
			need_to_append = true;
		} else if ((device_h->io_direction == DEVICE_IO_DIRECTION_BOTH) && (device_flags & DEVICE_IO_DIRECTION_BOTH_FLAG)) {
			need_to_append = true;
		}
		if (need_to_append) {
			if (!need_to_check_for_state && !need_to_check_for_type) {
				*is_good_to_append = true;
				return MM_ERROR_NONE;
			}
		} else {
			*is_good_to_append = false;
			return MM_ERROR_NONE;
		}
	}
	if (need_to_check_for_state) {
		need_to_append = false;
		if ((device_h->state == DEVICE_STATE_DEACTIVATED) && (device_flags & DEVICE_STATE_DEACTIVATED_FLAG)) {
			need_to_append = true;
		} else if ((device_h->state == DEVICE_STATE_ACTIVATED) && (device_flags & DEVICE_STATE_ACTIVATED_FLAG)) {
			need_to_append = true;
		}
		if (need_to_append) {
			if (!need_to_check_for_type) {
				*is_good_to_append = true;
				return MM_ERROR_NONE;
			}
		} else {
			*is_good_to_append = false;
			return MM_ERROR_NONE;
		}
	}
	if (need_to_check_for_type) {
		need_to_append = false;
		bool is_internal_device = IS_INTERNAL_DEVICE(device_h->type);
		if (is_internal_device && (device_flags & DEVICE_TYPE_INTERNAL_FLAG)) {
			need_to_append = true;
		} else if (!is_internal_device && (device_flags & DEVICE_TYPE_EXTERNAL_FLAG)) {
			need_to_append = true;
		}
		if (need_to_append) {
			*is_good_to_append = true;
			return MM_ERROR_NONE;
		} else {
			*is_good_to_append = false;
			return MM_ERROR_NONE;
		}
	}
	return MM_ERROR_NONE;
}

int __mm_sound_client_device_list_clear ()
{
	int ret = MM_ERROR_NONE;


	if (g_device_list) {
		g_list_free_full(g_device_list, g_free);
		g_device_list = NULL;
	}


	return ret;
}

int __mm_sound_client_device_list_append_item (mm_sound_device_t *device_h)
{
	int ret = MM_ERROR_NONE;
	mm_sound_device_t *device_node = g_malloc0(sizeof(mm_sound_device_t));
	if (!device_node)
		return MM_ERROR_SOUND_INTERNAL;
	memcpy(device_node, device_h, sizeof(mm_sound_device_t));


	g_device_list = g_list_append(g_device_list, device_node);
	debug_log("[Client] g_device_list[0x%x], new device_node[0x%x] is appended, type[%d], direction[0x%x], id[%d]\n",
			g_device_list, device_node, device_node->type, device_node->io_direction, device_node->id);


	return ret;
}

int _mm_sound_client_device_list_dump (GList *device_list)
{
	int ret = MM_ERROR_NONE;
	GList *list = NULL;
	mm_sound_device_t *device_node = NULL;
	int count = 0;

	debug_log("======================== device list : start ==========================\n");
	for (list = device_list; list != NULL; list = list->next) {
		device_node = (mm_sound_device_t *)list->data;
		if (device_node) {
			debug_log(" list idx[%d]: type[%02d], id[%02d], io_direction[%d], state[%d], name[%s]\n",
						count++, device_node->type, device_node->id, device_node->io_direction, device_node->state, device_node->name);
		}
	}
	debug_log("======================== device list : end ============================\n");

	return ret;
}

int _mm_sound_client_get_current_connected_device_list(int device_flags, mm_sound_device_list_t **device_list)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}
	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();

	/* Send REQ_ADD_ACTIVE_DEVICE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_GET_CONNECTED_DEVICE_LIST;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.device_flags = device_flags;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_GET_CONNECTED_DEVICE_LIST:
	{
		int i = 0;
		int total_device_num = msgrcv.sound_msg.total_device_num;
		bool is_good_to_append = false;
		mm_sound_device_t* device_h = &msgrcv.sound_msg.device_handle;

		ret = __mm_sound_client_device_list_clear();
		if (ret) {
			debug_error("[Client] failed to __mm_sound_client_device_list_clear(), ret[0x%x]\n", ret);
			goto cleanup;
		}

		debug_msg("[Client] supposed to receive %d messages\n", total_device_num);
		for (i = 0; i < total_device_num; i++) {
			/* check if this device_handle is suitable according to flags */
			ret = __mm_sound_device_check_flags_to_append (device_flags, device_h, &is_good_to_append);
			if (is_good_to_append) {
				ret = __mm_sound_client_device_list_append_item(device_h);
				if (ret) {
					debug_error("[Client] failed to __mm_sound_client_device_list_append_item(), ret[0x%x]\n", ret);
				}
			}
			if (total_device_num-i > 1) {
				if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
					debug_error("[Client] failed to [%d]th of __MMIpcRecvMsg()\n", i);
					goto cleanup;
				}
				switch (msgrcv.sound_msg.msgtype)
				{
				case MM_SOUND_MSG_RES_GET_CONNECTED_DEVICE_LIST:
					break;
				default:
					debug_error("[Client] failed to [%d]th of __MMIpcRecvMsg(), msgtype[%d] is not expected\n", msgrcv.sound_msg.msgtype);
					goto cleanup;
					break;
				}
			}
		}
		g_device_list_t.list = g_device_list;
		if (g_device_list) {
			*device_list = &g_device_list_t;
			debug_msg("[Client] Success to get connected device list, g_device_list_t[0x%x]->list[0x%x], device_list[0x%x]\n", &g_device_list_t, g_device_list_t.list, *device_list);
			_mm_sound_client_device_list_dump((*device_list)->list);
		} else {
			debug_msg("[Client] device_list null\n");
			*device_list = NULL;
			ret = MM_ERROR_SOUND_NO_DATA;
		}

	}
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code; // no data is possible
		goto cleanup;
		break;
	default:
		debug_error("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_add_device_connected_callback(int device_flags, mm_sound_device_connected_cb func, void* user_data)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_ADD_ACTIVE_DEVICE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_ADD_DEVICE_CONNECTED_CB;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.device_flags = device_flags;
	msgsnd.sound_msg.callback = func;
	msgsnd.sound_msg.cbdata = user_data;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_ADD_DEVICE_CONNECTED_CB:
		debug_msg("[Client] Success to add device connected callback\n");
		if (g_thread_id == -1)
		{
			g_thread_id = pthread_create(&g_thread, NULL, callbackfunc, NULL);
			if (g_thread_id == -1)
			{
				debug_critical("[Client] Fail to create thread %s\n", strerror(errno));
				ret = MM_ERROR_SOUND_INTERNAL;
				goto cleanup;
			}
		}
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_remove_device_connected_callback(void)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_REMOVE_ACTIVE_DEVICE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_REMOVE_DEVICE_CONNECTED_CB;
	msgsnd.sound_msg.msgid = instance;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}
	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}
	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_REMOVE_DEVICE_CONNECTED_CB:
		debug_msg("[Client] Success to remove device connected callback\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_add_device_info_changed_callback(int device_flags, mm_sound_device_info_changed_cb func, void* user_data)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_ADD_ACTIVE_DEVICE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_ADD_DEVICE_INFO_CHANGED_CB;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.device_flags = device_flags;
	msgsnd.sound_msg.callback = func;
	msgsnd.sound_msg.cbdata = user_data;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_ADD_DEVICE_INFO_CHANGED_CB:
		debug_msg("[Client] Success to add device connected callback\n");
		if (g_thread_id == -1)
		{
			g_thread_id = pthread_create(&g_thread, NULL, callbackfunc, NULL);
			if (g_thread_id == -1)
			{
				debug_critical("[Client] Fail to create thread %s\n", strerror(errno));
				ret = MM_ERROR_SOUND_INTERNAL;
				goto cleanup;
			}
		}
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_remove_device_info_changed_callback(void)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_REMOVE_ACTIVE_DEVICE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_REMOVE_DEVICE_INFO_CHANGED_CB;
	msgsnd.sound_msg.msgid = instance;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_REMOVE_DEVICE_INFO_CHANGED_CB:
		debug_msg("[Client] Success to remove device info changed callback\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_is_route_available(mm_sound_route route, bool *is_available)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	*is_available = FALSE;

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_IS_ROUTE_AVAILABLE */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_IS_ROUTE_AVAILABLE;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.route = route;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Receive */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_IS_ROUTE_AVAILABLE:
		*is_available = msgrcv.sound_msg.is_available;
		debug_msg("[Client] Success to check given route is available %d\n", *is_available);
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

static int _handle_foreach_callback(mm_ipc_msg_t *msg)
{
	int route_index;
	mm_sound_route route;

	debug_fenter();

	if (msg->sound_msg.callback == NULL) {
		debug_error ("[Client] Foreach callback is [%p], cbdata = %p => exit",
				msg->sound_msg.callback, msg->sound_msg.cbdata);
		return MM_ERROR_SOUND_INTERNAL;
	}

	for (route_index = 0; route_index < MM_SOUND_ROUTE_NUM; route_index++) {
		route = msg->sound_msg.route_list[route_index];
		if (route == 0) {
			break;
		}
		debug_msg("[Client] available route : %d\n", route);
		if (((mm_sound_available_route_cb)msg->sound_msg.callback)(route, msg->sound_msg.cbdata) == false) {
			debug_msg ("[Client] user doesn't want anymore. quit loop!!\n");
			break;
		}
	}

	debug_fleave();

	return MM_ERROR_NONE;
}

int _mm_sound_client_foreach_available_route_cb(mm_sound_available_route_cb available_route_cb, void *user_data)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_FOREACH_AVAILABLE_ROUTE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_FOREACH_AVAILABLE_ROUTE_CB;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.callback = (void *)available_route_cb;
	msgsnd.sound_msg.cbdata = (void *)user_data;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_FOREACH_AVAILABLE_ROUTE_CB:
		debug_msg("[Client] Success to set foreach available route callback\n");
		msgrcv.sound_msg.callback = (void *)available_route_cb;
		msgrcv.sound_msg.cbdata = (void *)user_data;
		ret = _handle_foreach_callback (&msgrcv);
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_set_active_route(mm_sound_route route, bool need_broadcast)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_SET_ACTIVE_ROUTE */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_SET_ACTIVE_ROUTE;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.route = route;
	msgsnd.sound_msg.need_broadcast = need_broadcast;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE)
		goto cleanup;

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_SET_ACTIVE_ROUTE:
		debug_msg("[Client] Success to add active device callback\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_set_active_route_auto(void)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_SET_ACTIVE_ROUTE */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_SET_ACTIVE_ROUTE_AUTO;
	msgsnd.sound_msg.msgid = instance;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_SET_ACTIVE_ROUTE_AUTO:
		debug_msg("[Client] Success to set active device auto\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_get_active_device(mm_sound_device_in *device_in, mm_sound_device_out *device_out)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_GET_ACTIVE_DEVICE */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_GET_ACTIVE_DEVICE;
	msgsnd.sound_msg.msgid = instance;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_GET_ACTIVE_DEVICE:
		*device_in = msgrcv.sound_msg.device_in;
		*device_out = msgrcv.sound_msg.device_out;
		debug_log("[Client] Success to get active device in=[%x], out=[%x]\n", *device_in, *device_out);
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_get_audio_path(mm_sound_device_in *device_in, mm_sound_device_out *device_out)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_GET_AUDIO_PATH */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_GET_AUDIO_PATH;
	msgsnd.sound_msg.msgid = instance;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_GET_AUDIO_PATH:
		*device_in = msgrcv.sound_msg.device_in;
		*device_out = msgrcv.sound_msg.device_out;
		debug_log("[Client] Success to get active device in=[%x], out=[%x]\n", *device_in, *device_out);
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_add_active_device_changed_callback(const char *name, mm_sound_active_device_changed_cb func, void* user_data)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_ADD_ACTIVE_DEVICE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_ADD_ACTIVE_DEVICE_CB;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.callback = func;
	msgsnd.sound_msg.cbdata = user_data;
	MMSOUND_STRNCPY(msgsnd.sound_msg.name, name, MM_SOUND_NAME_NUM);

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_ADD_ACTIVE_DEVICE_CB:
		debug_msg("[Client] Success to add active device callback\n");
		if (g_thread_id == -1)
		{
			g_thread_id = pthread_create(&g_thread, NULL, callbackfunc, NULL);
			if (g_thread_id == -1)
			{
				debug_critical("[Client] Fail to create thread %s\n", strerror(errno));
				ret = MM_ERROR_SOUND_INTERNAL;
				goto cleanup;
			}
		}
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_remove_active_device_changed_callback(const char *name)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_REMOVE_ACTIVE_DEVICE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_REMOVE_ACTIVE_DEVICE_CB;
	msgsnd.sound_msg.msgid = instance;
	MMSOUND_STRNCPY(msgsnd.sound_msg.name, name, MM_SOUND_NAME_NUM);

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_REMOVE_ACTIVE_DEVICE_CB:
		debug_msg("[Client] Success to remove active device callback\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_add_volume_changed_callback(mm_sound_volume_changed_cb func, void* user_data)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_ADD_VOLUME_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_ADD_VOLUME_CB;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.callback = func;
	msgsnd.sound_msg.cbdata = user_data;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_ADD_VOLUME_CB:
		debug_msg("[Client] Success to add volume callback\n");
		if (g_thread_id == -1)
		{
			g_thread_id = pthread_create(&g_thread, NULL, callbackfunc, NULL);
			if (g_thread_id == -1)
			{
				debug_critical("[Client] Fail to create thread %s\n", strerror(errno));
				ret = MM_ERROR_SOUND_INTERNAL;
				goto cleanup;
			}
		}
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_remove_volume_changed_callback(void)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_REMOVE_VOLUME_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_REMOVE_VOLUME_CB;
	msgsnd.sound_msg.msgid = instance;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_REMOVE_VOLUME_CB:
		debug_msg("[Client] Success to remove volume callback\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_add_available_route_changed_callback(mm_sound_available_route_changed_cb func, void* user_data)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_ADD_AVAILABLE_ROUTE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_ADD_AVAILABLE_ROUTE_CB;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.callback = func;
	msgsnd.sound_msg.cbdata = user_data;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_ADD_AVAILABLE_ROUTE_CB:
		debug_msg("[Client] Success to add available route callback\n");
		if (g_thread_id == -1)
		{
			g_thread_id = pthread_create(&g_thread, NULL, callbackfunc, NULL);
			if (g_thread_id == -1)
			{
				debug_critical("[Client] Fail to create thread %s\n", strerror(errno));
				ret = MM_ERROR_SOUND_INTERNAL;
				goto cleanup;
			}
		}
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_remove_available_route_changed_callback(void)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE) {
		return ret;
	}

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send REQ_REMOVE_AVAILABLE_ROUTE_CB */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_REMOVE_AVAILABLE_ROUTE_CB;
	msgsnd.sound_msg.msgid = instance;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE) {
		goto cleanup;
	}

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE) {
		goto cleanup;
	}

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_REMOVE_AVAILABLE_ROUTE_CB:
		debug_msg("[Client] Success to remove available route callback\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

static int __MMIpcCBSndMsg(mm_ipc_msg_t *msg)
{
	/* rcv message */
	msg->msg_type = msg->sound_msg.msgid;
	if (msgsnd(g_msg_sccb, msg,DSIZE, 0)== -1)
	{
		debug_error("[Client] Fail to callback send message msgid : [%d], [%d][%s]", g_msg_sccb, errno, strerror(errno));
		return MM_ERROR_COMMON_UNKNOWN;
	}
	return MM_ERROR_NONE;
}

static int __MMIpcCBRecvMsg(int msgtype, mm_ipc_msg_t *msg)
{
	/* rcv message */
	if(msgrcv(g_msg_sccb, msg, DSIZE, msgtype, 0) == -1)
	{
		debug_error("[Client] Fail to callback receive message msgid : [%d], [%d][%s]", g_msg_sccb, errno, strerror(errno));
		return MM_ERROR_COMMON_UNKNOWN;
	}
	return MM_ERROR_NONE;
}

#define MAX_RCV_RETRY 20000

static int __MMIpcRecvMsg(int msgtype, mm_ipc_msg_t *msg)
{
	int retry_count = 0;

	/* rcv message */
	while (msgrcv(g_msg_scrcv, msg, DSIZE, msgtype, IPC_NOWAIT) == -1) {
		if (errno == ENOMSG) {
			if (retry_count < MAX_RCV_RETRY) { /* usec is 10^-6 sec so, 5ms * 20000 = 10sec. */
				usleep(5000);
				retry_count++;
				continue;
			} else {
				debug_error("[Client] retry(%d) is over : [%d] \n", MAX_RCV_RETRY, g_msg_scrcv);
				return MM_ERROR_SOUND_INTERNAL;
			}
		} else if (errno == EINTR) {
			debug_warning("[Client] Interrupted by signal, continue loop");
			continue;
		}

		debug_error("[Client] Fail to receive msgid : [%d], [%d][%s]", g_msg_scrcv, errno, strerror(errno));

		return MM_ERROR_COMMON_UNKNOWN;
	}
	debug_log("[Client] Retry %d times when receive msg\n", retry_count);
	return MM_ERROR_NONE;
}

static int __MMIpcSndMsg(mm_ipc_msg_t *msg)
{
	/* rcv message */
	int try_again = 0;

	msg->msg_type = msg->sound_msg.msgid;
	while (msgsnd(g_msg_scsnd, msg,DSIZE, IPC_NOWAIT) == -1)
	{
		if(errno == EACCES) {
			debug_warning("Not acces.\n");
		} else if(errno == EAGAIN || errno == ENOMEM) {
			mm_ipc_msg_t msgdata = {0,};
			debug_warning("Blocked process [msgflag & IPC_NOWAIT != 0]\n");
			debug_warning("The system does not have enough memory to make a copy of the message pointed to by msgp\n");
			/* wait 10 msec ,then it will try again */
			usleep(10000);
			/*  it will try 5 times, after 5 times ,if it still fail ,then it will clear the message queue */
			if (try_again <= 5) {
				try_again ++;
				continue;
			}
			/* message queue is full ,it need to clear the queue */
			while( msgrcv(g_msg_scsnd, &msgdata, DSIZE, 0, IPC_NOWAIT) != -1 ) {
				debug_warning("msg queue is full ,remove msgtype:[%d] from the queue",msgdata.sound_msg.msgtype);
			}
			try_again++;
			continue;
		} else if(errno == EIDRM) {
			debug_warning("Removed msgid from system\n");
		} else if(errno == EINTR) {
			debug_warning("Iterrrupted by singnal\n");
		} else if(errno == EINVAL) {
			debug_warning("Invalid msgid or msgtype < 1 or out of data size \n");
		} else if(errno == EFAULT) {
			debug_warning("The address pointed to by msgp isn't accessible \n");
		}
		debug_error("[Client] Fail to send message msgid : [%d], [%d][%s]", g_msg_scsnd, errno, strerror(errno));
		return MM_ERROR_SOUND_INTERNAL;
	}
	return MM_ERROR_NONE;
}

static int __MMSoundGetMsg(void)
{
	/* Init message queue, generate msgid for communication to server */
	/* The key value to get msgid is defined "mm_sound_msg.h". Shared with server */
	int i = 0;

	debug_fenter();

	/* get msg queue rcv, snd, cb */
	g_msg_scsnd = msgget(ftok(KEY_BASE_PATH, RCV_MSG), 0666);
	g_msg_scrcv = msgget(ftok(KEY_BASE_PATH, SND_MSG), 0666);
	g_msg_sccb = msgget(ftok(KEY_BASE_PATH, CB_MSG), 0666);

	if ((g_msg_scsnd == -1 || g_msg_scrcv == -1 || g_msg_sccb == -1) != MM_ERROR_NONE) {
		if (errno == EACCES) {
			debug_warning("Require ROOT permission.\n");
		} else if (errno == ENOMEM) {
			debug_warning("System memory is empty.\n");
		} else if(errno == ENOSPC) {
			debug_warning("Resource is empty.\n");
		}
		/* Some app would start before Sound Server IPC ready. */
		/* Let's try it again in 50ms later by 10 times */
		for (i=0;i<10;i++) {
			usleep(50000);
			g_msg_scsnd = msgget(ftok(KEY_BASE_PATH, RCV_MSG), 0666);
			g_msg_scrcv = msgget(ftok(KEY_BASE_PATH, SND_MSG), 0666);
			g_msg_sccb = msgget(ftok(KEY_BASE_PATH, CB_MSG), 0666);
			if ((g_msg_scsnd == -1 || g_msg_scrcv == -1 || g_msg_sccb == -1) != MM_ERROR_NONE) {
				debug_error("Fail to GET msgid by retrying %d times\n", i+1);
			} else
				break;
		}
		if ((g_msg_scsnd == -1 || g_msg_scrcv == -1 || g_msg_sccb == -1) != MM_ERROR_NONE) {
			debug_error("Fail to GET msgid finally, just return internal error.\n");
			return MM_ERROR_SOUND_INTERNAL;
		}
	}

	debug_log("Get msg queue id from server : snd[%d], rcv[%d], cb[%d]\n", g_msg_scsnd, g_msg_scrcv, g_msg_sccb);

	debug_fleave();
	return MM_ERROR_NONE;
}

#ifdef PULSE_CLIENT

int MMSoundClientIsBtA2dpOn (bool *connected, char** bt_name)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	instance = getpid();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE)
		return ret;

	pthread_mutex_lock(&g_thread_mutex);

	/* Send req  */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_IS_BT_A2DP_ON;
	msgsnd.sound_msg.msgid = instance;

	ret = __MMIpcSndMsg(&msgsnd);
	if (ret != MM_ERROR_NONE)
	{
		debug_error("Fail to send msg\n");
		goto cleanup;
	}

	/* Receive */
	ret = __MMIpcRecvMsg(instance, &msgrcv);
	if (ret != MM_ERROR_NONE)
	{
		debug_error("Fail to recieve msg\n");
		goto cleanup;
	}
	switch (msgrcv.sound_msg.msgtype)
	{
		case MM_SOUND_MSG_RES_IS_BT_A2DP_ON:
			debug_msg("Success to get IS_BT_A2DP_ON [%d][%s]\n", msgrcv.sound_msg.code, msgrcv.sound_msg.filename);
			*connected  = (bool)msgrcv.sound_msg.code;
			if (*connected)
				*bt_name = strdup (msgrcv.sound_msg.filename);
			else
				*bt_name = NULL;
			break;
		case MM_SOUND_MSG_RES_ERROR:
			debug_error("Error occurred \n");
			ret = msgrcv.sound_msg.code;
			goto cleanup;
			break;
		default:
			debug_critical("Unexpected state with communication \n");
			ret = msgrcv.sound_msg.code;
			goto cleanup;
			break;
	}

cleanup:
		pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}

int _mm_sound_client_set_sound_path_for_active_device(mm_sound_device_out device_out, mm_sound_device_in device_in)
{
	mm_ipc_msg_t msgrcv = {0,};
	mm_ipc_msg_t msgsnd = {0,};
	int ret = MM_ERROR_NONE;
	int instance;

	debug_fenter();

	ret = __mm_sound_client_get_msg_queue();
	if (ret  != MM_ERROR_NONE)
		return ret;

	pthread_mutex_lock(&g_thread_mutex);

	instance = getpid();
	/* Send MM_SOUND_MSG_REQ_SET_PATH_FOR_ACTIVE_DEVICE */
	msgsnd.sound_msg.msgtype = MM_SOUND_MSG_REQ_SET_PATH_FOR_ACTIVE_DEVICE;
	msgsnd.sound_msg.msgid = instance;
	msgsnd.sound_msg.device_in =  device_in;
	msgsnd.sound_msg.device_out = device_out;

	if (__MMIpcSndMsg(&msgsnd) != MM_ERROR_NONE)
		goto cleanup;

	/* Recieve */
	if (__MMIpcRecvMsg(instance, &msgrcv) != MM_ERROR_NONE)
		goto cleanup;

	switch (msgrcv.sound_msg.msgtype)
	{
	case MM_SOUND_MSG_RES_SET_PATH_FOR_ACTIVE_DEVICE:
		debug_msg("[Client] Success to setsound path for active device\n");
		break;
	case MM_SOUND_MSG_RES_ERROR:
		debug_error("[Client] Error occurred \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	default:
		debug_critical("[Client] Unexpected state with communication \n");
		ret = msgrcv.sound_msg.code;
		goto cleanup;
		break;
	}

cleanup:
	pthread_mutex_unlock(&g_thread_mutex);

	debug_fleave();
	return ret;
}


#endif // PULSE_CLIENT