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

#include <mm_error.h>
#include "mm_camcorder_internal.h"
#include <mm_types.h>

#include <gst/interfaces/colorbalance.h>
#include <gst/interfaces/cameracontrol.h>
#include <asm/types.h>

#include <mm_session.h>
#include <mm_session_private.h>
#include <audio-session-manager.h>

#include <pmapi.h>

/*---------------------------------------------------------------------------------------
|    GLOBAL VARIABLE DEFINITIONS for internal						|
---------------------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------------------
|    LOCAL VARIABLE DEFINITIONS for internal						|
---------------------------------------------------------------------------------------*/
#define __MMCAMCORDER_CMD_ITERATE_MAX           3
#define __MMCAMCORDER_SECURITY_HANDLE_DEFAULT   -1
#define __MMCAMCORDER_SET_GST_STATE_TIMEOUT     5

/*---------------------------------------------------------------------------------------
|    LOCAL FUNCTION PROTOTYPES:								|
---------------------------------------------------------------------------------------*/
/* STATIC INTERNAL FUNCTION */
static gboolean __mmcamcorder_gstreamer_init(camera_conf * conf);

static gboolean __mmcamcorder_handle_gst_error(MMHandleType handle, GstMessage *message, GError *error);
static gint     __mmcamcorder_gst_handle_stream_error(MMHandleType handle, int code, GstMessage *message);
static gint     __mmcamcorder_gst_handle_resource_error(MMHandleType handle, int code, GstMessage *message);
static gint     __mmcamcorder_gst_handle_library_error(MMHandleType handle, int code, GstMessage *message);
static gint     __mmcamcorder_gst_handle_core_error(MMHandleType handle, int code, GstMessage *message);
static gint     __mmcamcorder_gst_handle_resource_warning(MMHandleType handle, GstMessage *message , GError *error);
static gboolean __mmcamcorder_handle_gst_warning(MMHandleType handle, GstMessage *message, GError *error);

static int      __mmcamcorder_asm_get_event_type(int sessionType);
static void     __mmcamcorder_force_stop(mmf_camcorder_t *hcamcorder);
static void     __mmcamcorder_force_resume(mmf_camcorder_t *hcamcorder);
ASM_cb_result_t _mmcamcorder_asm_callback(int handle, ASM_event_sources_t event_src,
                                          ASM_sound_commands_t command,
                                          unsigned int sound_status, void *cb_data);

static gboolean __mmcamcorder_set_attr_to_camsensor_cb(gpointer data);

/*=======================================================================================
|  FUNCTION DEFINITIONS									|
=======================================================================================*/
/*---------------------------------------------------------------------------------------
|    GLOBAL FUNCTION DEFINITIONS:							|
---------------------------------------------------------------------------------------*/

/* Internal command functions {*/
int _mmcamcorder_create(MMHandleType *handle, MMCamPreset *info)
{
	int ret = MM_ERROR_NONE;
	int UseConfCtrl = 0;
	int sessionType = MM_SESSION_TYPE_EXCLUSIVE;
	int errorcode = MM_ERROR_NONE;
	int rcmd_fmt_capture = MM_PIXEL_FORMAT_YUYV;
	int rcmd_fmt_recording = MM_PIXEL_FORMAT_NV12;
	int rcmd_dpy_rotation = MM_DISPLAY_ROTATION_270;
	int play_capture_sound = TRUE;
	char *err_attr_name = NULL;
	char *ConfCtrlFile = NULL;
	mmf_camcorder_t *hcamcorder = NULL;
	ASM_resource_t mm_resource = ASM_RESOURCE_NONE;
	type_element *EvasSurfaceElement = NULL;

	_mmcam_dbg_log("Entered");

	mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);
	mmf_return_val_if_fail(info, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);

	/* Create mmf_camcorder_t handle and initialize every variable */
	hcamcorder = (mmf_camcorder_t *)malloc(sizeof(mmf_camcorder_t));
	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_LOW_MEMORY);
	memset(hcamcorder, 0x00, sizeof(mmf_camcorder_t));

	/* init values */
	hcamcorder->type = 0;
	hcamcorder->state = MM_CAMCORDER_STATE_NONE;
	hcamcorder->sub_context = NULL;
	hcamcorder->target_state = MM_CAMCORDER_STATE_NULL;

	/* thread - for g_mutex_new() */
	if (!g_thread_supported()) {
		g_thread_init(NULL);
	}

	(hcamcorder->mtsafe).lock = g_mutex_new();
	(hcamcorder->mtsafe).cond = g_cond_new();

	(hcamcorder->mtsafe).cmd_lock = g_mutex_new();
	(hcamcorder->mtsafe).state_lock = g_mutex_new();
	(hcamcorder->mtsafe).gst_state_lock = g_mutex_new();
	(hcamcorder->mtsafe).message_cb_lock = g_mutex_new();
	(hcamcorder->mtsafe).vcapture_cb_lock = g_mutex_new();
	(hcamcorder->mtsafe).vstream_cb_lock = g_mutex_new();
	(hcamcorder->mtsafe).astream_cb_lock = g_mutex_new();

	pthread_mutex_init(&(hcamcorder->sound_lock), NULL);
	pthread_cond_init(&(hcamcorder->sound_cond), NULL);

	/* Sound mutex/cond init */
	pthread_mutex_init(&(hcamcorder->snd_info.open_mutex), NULL);
	pthread_cond_init(&(hcamcorder->snd_info.open_cond), NULL);

	if (info->videodev_type < MM_VIDEO_DEVICE_NONE ||
	    info->videodev_type >= MM_VIDEO_DEVICE_NUM) {
		_mmcam_dbg_err("_mmcamcorder_create::video device type is out of range.");
		ret = MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
		goto _ERR_DEFAULT_VALUE_INIT;
	}

	/* Check and register ASM */
	if (MM_ERROR_NONE != _mm_session_util_read_type(-1, &sessionType)) {
		_mmcam_dbg_warn("Read _mm_session_util_read_type failed. use default \"exclusive\" type");
		sessionType = MM_SESSION_TYPE_EXCLUSIVE;
		if (MM_ERROR_NONE != mm_session_init(sessionType)) {
			_mmcam_dbg_err("mm_session_init() failed");
			ret = MM_ERROR_POLICY_INTERNAL;
			goto _ERR_DEFAULT_VALUE_INIT;
		}
	}
	if ((sessionType != MM_SESSION_TYPE_CALL) && (sessionType != MM_SESSION_TYPE_VIDEOCALL)) {
		int asm_session_type = ASM_EVENT_NONE;
		int asm_handle;
		int pid = -1; /* process id of itself */

		asm_session_type = __mmcamcorder_asm_get_event_type( sessionType );
		/* Call will not be interrupted. so does not need callback function */
		if (!ASM_register_sound(pid, &asm_handle, asm_session_type, ASM_STATE_NONE,
		                        (ASM_sound_cb_t)_mmcamcorder_asm_callback,
		                        (void*)hcamcorder, mm_resource, &errorcode)) {
			_mmcam_dbg_err("ASM_register_sound() failed[%x]", errorcode);
			ret = MM_ERROR_POLICY_INTERNAL;
			goto _ERR_DEFAULT_VALUE_INIT;
		}
		hcamcorder->asm_handle = asm_handle;
	}

	/* Get Camera Configure information from Camcorder INI file */
	__ta__( "    _mmcamcorder_conf_get_info main",
	_mmcamcorder_conf_get_info(CONFIGURE_TYPE_MAIN, CONFIGURE_MAIN_FILE, &hcamcorder->conf_main);
	);

	if (!(hcamcorder->conf_main)) {
		_mmcam_dbg_err( "Failed to get configure(main) info." );

		ret = MM_ERROR_CAMCORDER_CREATE_CONFIGURE;
		goto _ERR_AUDIO_BLOCKED;
	}

	__ta__("    _mmcamcorder_alloc_attribute",
	hcamcorder->attributes = _mmcamcorder_alloc_attribute((MMHandleType)hcamcorder, info);
	);
	if (!(hcamcorder->attributes)) {
		_mmcam_dbg_err("_mmcamcorder_create::alloc attribute error.");
		
		ret = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
		goto _ERR_AUDIO_BLOCKED;
	}

	if (info->videodev_type != MM_VIDEO_DEVICE_NONE) {
		_mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
		                                CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
		                                "UseConfCtrl", &UseConfCtrl);

		if (UseConfCtrl) {
			_mmcam_dbg_log( "Enable Configure Control system." );
#if 1
			switch (info->videodev_type) {
			case MM_VIDEO_DEVICE_CAMERA0:
				_mmcamcorder_conf_get_value_string(hcamcorder->conf_main,
				                                   CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
				                                   "ConfCtrlFile0", &ConfCtrlFile);
				break;
			case MM_VIDEO_DEVICE_CAMERA1:
				_mmcamcorder_conf_get_value_string(hcamcorder->conf_main,
				                                   CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
				                                   "ConfCtrlFile1", &ConfCtrlFile);
				break;
			default:
				_mmcam_dbg_err( "Not supported camera type." );
				ret = MM_ERROR_CAMCORDER_NOT_SUPPORTED;
				goto _ERR_ALLOC_ATTRIBUTE;
			}

			_mmcam_dbg_log("videodev_type : [%d], ConfCtrlPath : [%s]", info->videodev_type, ConfCtrlFile);

			__ta__( "    _mmcamcorder_conf_get_info ctrl",
			_mmcamcorder_conf_get_info(CONFIGURE_TYPE_CTRL, ConfCtrlFile, &hcamcorder->conf_ctrl);
			);
/*
			_mmcamcorder_conf_print_info(&hcamcorder->conf_main);
			_mmcamcorder_conf_print_info(&hcamcorder->conf_ctrl);
*/
#else
			_mmcamcorder_conf_query_info(CONFIGURE_TYPE_CTRL, info->videodev_type, &hcamcorder->conf_ctrl);
#endif
			if (!(hcamcorder->conf_ctrl)) {
				_mmcam_dbg_err( "Failed to get configure(control) info." );
				ret = MM_ERROR_CAMCORDER_CREATE_CONFIGURE;
				goto _ERR_ALLOC_ATTRIBUTE;
			}

			__ta__( "    _mmcamcorder_init_convert_table",
			ret = _mmcamcorder_init_convert_table((MMHandleType)hcamcorder);
			);
			if (ret != MM_ERROR_NONE) {
				_mmcam_dbg_warn("converting table initialize error!!");
			}

			__ta__( "    _mmcamcorder_init_attr_from_configure",
			ret = _mmcamcorder_init_attr_from_configure((MMHandleType)hcamcorder);
			);
			if (ret != MM_ERROR_NONE) {
				_mmcam_dbg_warn("converting table initialize error!!");
			}
		}
		else
		{
			_mmcam_dbg_log( "Disable Configure Control system." );
			hcamcorder->conf_ctrl = NULL;
		}
	}

	__ta__( "    __mmcamcorder_gstreamer_init",
	ret = __mmcamcorder_gstreamer_init(hcamcorder->conf_main);
	);
	if (!ret) {
		_mmcam_dbg_err( "Failed to initialize gstreamer!!" );
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		goto _ERR_ALLOC_ATTRIBUTE;
	}

	/* Get recommend preview format and display rotation from INI */
	_mmcamcorder_conf_get_value_int(hcamcorder->conf_ctrl,
	                                CONFIGURE_CATEGORY_CTRL_CAMERA,
	                                "RecommendPreviewFormatCapture",
	                                &rcmd_fmt_capture);

	_mmcamcorder_conf_get_value_int(hcamcorder->conf_ctrl,
	                                CONFIGURE_CATEGORY_CTRL_CAMERA,
	                                "RecommendPreviewFormatRecord",
	                                &rcmd_fmt_recording);

	_mmcamcorder_conf_get_value_int(hcamcorder->conf_ctrl,
	                                CONFIGURE_CATEGORY_CTRL_CAMERA,
	                                "RecommendDisplayRotation",
	                                &rcmd_dpy_rotation);

	_mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
	                                CONFIGURE_CATEGORY_MAIN_CAPTURE,
	                                "PlayCaptureSound",
	                                &play_capture_sound);

	_mmcam_dbg_log("Recommend preview rot[capture:%d,recording:%d], dpy rot[%d], play capture sound[%d]",
			rcmd_fmt_capture, rcmd_fmt_recording, rcmd_dpy_rotation, play_capture_sound);

	ret = mm_camcorder_set_attributes((MMHandleType)hcamcorder, &err_attr_name,
	                                  MMCAM_RECOMMEND_PREVIEW_FORMAT_FOR_CAPTURE, rcmd_fmt_capture,
	                                  MMCAM_RECOMMEND_PREVIEW_FORMAT_FOR_RECORDING, rcmd_fmt_recording,
	                                  MMCAM_RECOMMEND_DISPLAY_ROTATION, rcmd_dpy_rotation,
	                                  "capture-sound-enable", play_capture_sound,
	                                  NULL);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err( "Set %s FAILED.", err_attr_name );
		if (err_attr_name != NULL) {
			free( err_attr_name );
			err_attr_name = NULL;
		}

		goto _ERR_ALLOC_ATTRIBUTE;
	}

	/* Get UseZeroCopyFormat value from INI */
	_mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
	                                CONFIGURE_CATEGORY_MAIN_VIDEO_INPUT,
	                                "UseZeroCopyFormat",
	                                &(hcamcorder->use_zero_copy_format));
	_mmcam_dbg_log("UseZeroCopyFormat : %d", hcamcorder->use_zero_copy_format);

	/* Make some attributes as read-only type */
	__ta__( "    _mmcamcorder_lock_readonly_attributes",
	_mmcamcorder_lock_readonly_attributes((MMHandleType)hcamcorder);
	);

	/* Disable attributes in each model */
	__ta__( "    _mmcamcorder_set_disabled_attributes",
	_mmcamcorder_set_disabled_attributes((MMHandleType)hcamcorder);
	);

	/* Determine state change as sync or async */
	_mmcamcorder_conf_get_value_int(hcamcorder->conf_main,
	                                CONFIGURE_CATEGORY_MAIN_GENERAL,
	                                "SyncStateChange",
	                                &hcamcorder->sync_state_change );
	if (!(hcamcorder->sync_state_change)) {
		_mmcamcorder_create_command_loop((MMHandleType)hcamcorder);
	}

	/* Get videosink name for evas surface */
	_mmcamcorder_conf_get_element(hcamcorder->conf_main,
	                              CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
	                              "VideosinkElementEvas",
	                              &EvasSurfaceElement);
	if (EvasSurfaceElement) {
		int attr_index = 0;
		char *evassink_name = NULL;
		mmf_attribute_t *item_evassink_name = NULL;
		mmf_attrs_t *attrs = MMF_CAMCORDER_ATTRS(hcamcorder);

		_mmcamcorder_conf_get_value_element_name(EvasSurfaceElement, &evassink_name);
		mm_attrs_get_index((MMHandleType)attrs, MMCAM_DISPLAY_EVAS_SURFACE_SINK, &attr_index);
		item_evassink_name = &attrs->items[attr_index];
		mmf_attribute_set_string(item_evassink_name, evassink_name, strlen(evassink_name));
		mmf_attribute_commit(item_evassink_name);

		_mmcam_dbg_log("Evassink name : %d", evassink_name);
	}

	/* get shutter sound policy */
	vconf_get_int(VCONFKEY_CAMERA_SHUTTER_SOUND_POLICY, &hcamcorder->shutter_sound_policy);
	_mmcam_dbg_log("current shutter sound policy : %d", hcamcorder->shutter_sound_policy);

	/* Set initial state */
	_mmcamcorder_set_state((MMHandleType)hcamcorder, MM_CAMCORDER_STATE_NULL);
	_mmcam_dbg_log("_mmcamcorder_set_state");

	*handle = (MMHandleType)hcamcorder;

	return MM_ERROR_NONE;

_ERR_ALLOC_ATTRIBUTE:
_ERR_AUDIO_BLOCKED:
	/* unregister audio session manager */
	{
		sessionType = MM_SESSION_TYPE_EXCLUSIVE;
		errorcode = MM_ERROR_NONE;
		if (MM_ERROR_NONE != _mm_session_util_read_type(-1, &sessionType)) {
			sessionType = MM_SESSION_TYPE_EXCLUSIVE;
		}

		if((sessionType != MM_SESSION_TYPE_CALL) && (sessionType != MM_SESSION_TYPE_VIDEOCALL)) {
			int asm_session_type = __mmcamcorder_asm_get_event_type( sessionType );
			if (!ASM_unregister_sound(hcamcorder->asm_handle, asm_session_type, &errorcode)) {
				_mmcam_dbg_err("ASM_unregister_sound() failed(hdl:%p, stype:%d, err:%x)",
				               (void*)hcamcorder->asm_handle, sessionType, errorcode);
			}
		}
	}

_ERR_DEFAULT_VALUE_INIT:
	g_mutex_free ((hcamcorder->mtsafe).lock);
	g_cond_free ((hcamcorder->mtsafe).cond);
	g_mutex_free ((hcamcorder->mtsafe).cmd_lock);
	g_mutex_free ((hcamcorder->mtsafe).state_lock);
	g_mutex_free ((hcamcorder->mtsafe).gst_state_lock);	

	if (hcamcorder->conf_ctrl) {
		_mmcamcorder_conf_release_info( &hcamcorder->conf_ctrl );
	}

	if (hcamcorder->conf_main) {
		_mmcamcorder_conf_release_info( &hcamcorder->conf_main );
	}

	return ret;
}


int _mmcamcorder_destroy(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM = MM_CAMCORDER_STATE_NULL;
	int state_TO = MM_CAMCORDER_STATE_NONE;
	int asm_session_type = ASM_EVENT_EXCLUSIVE_MMCAMCORDER;
	int sessionType = MM_SESSION_TYPE_SHARE;
	int errorcode = MM_ERROR_NONE;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* Set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret < 0) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* Release sound handle */
	__ta__("_mmcamcorder_sound_finalize",
	ret = _mmcamcorder_sound_finalize(handle);
	);
	_mmcam_dbg_log("sound finalize [%d]", ret);

	/* Release SubContext and pipeline */
	if (hcamcorder->sub_context) {
		if (hcamcorder->sub_context->element) {
			__ta__("    _mmcamcorder_destroy_pipeline",
			_mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
			);
		}

		_mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
		hcamcorder->sub_context = NULL;
	}

	/* Remove idle function which is not called yet */
	if (hcamcorder->setting_event_id) {
		_mmcam_dbg_log("Remove remaining idle function");
		g_source_remove(hcamcorder->setting_event_id);
		hcamcorder->setting_event_id = 0;
	}

	/* Remove attributes */
	if (hcamcorder->attributes) {
		_mmcamcorder_dealloc_attribute(hcamcorder->attributes);
		hcamcorder->attributes = 0;
	}

	/* Remove exif info */
	if (hcamcorder->exif_info) {
		mm_exif_destory_exif_info(hcamcorder->exif_info);
		hcamcorder->exif_info=NULL;

	}

	/* Remove command loop when async state change mode */
	if (!hcamcorder->sync_state_change) {
		_mmcamcorder_destroy_command_loop(handle);
	}

	/* Release configure info */
	if (hcamcorder->conf_ctrl) {
		_mmcamcorder_conf_release_info( &hcamcorder->conf_ctrl );
	}
	if (hcamcorder->conf_main) {
		_mmcamcorder_conf_release_info( &hcamcorder->conf_main );
	}

	/* Remove messages which are not called yet */
	_mmcamcroder_remove_message_all(handle);

	/* Unregister ASM */
	if (MM_ERROR_NONE != _mm_session_util_read_type(-1, &sessionType)) {
		_mmcam_dbg_err("_mm_session_util_read_type Fail");
	}
	if ((sessionType != MM_SESSION_TYPE_CALL) && (sessionType != MM_SESSION_TYPE_VIDEOCALL)) {
		asm_session_type = __mmcamcorder_asm_get_event_type(sessionType);
		if (!ASM_unregister_sound(hcamcorder->asm_handle, asm_session_type, &errorcode)) {
			_mmcam_dbg_err("ASM_unregister_sound() failed(hdl:%p, stype:%d, err:%x)",
			               (void*)hcamcorder->asm_handle, sessionType, errorcode);
		}
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	/* Release lock, cond */
	if ((hcamcorder->mtsafe).lock) {
		g_mutex_free ((hcamcorder->mtsafe).lock);
		(hcamcorder->mtsafe).lock = NULL;
	}
	if ((hcamcorder->mtsafe).cond) {
		g_cond_free ((hcamcorder->mtsafe).cond);
		(hcamcorder->mtsafe).cond = NULL;
	}
	if ((hcamcorder->mtsafe).cmd_lock) {
		g_mutex_free ((hcamcorder->mtsafe).cmd_lock);
		(hcamcorder->mtsafe).cmd_lock = NULL;
	}
	if ((hcamcorder->mtsafe).state_lock) {
		g_mutex_free ((hcamcorder->mtsafe).state_lock);
		(hcamcorder->mtsafe).state_lock = NULL;
	}
	if ((hcamcorder->mtsafe).gst_state_lock) {
		g_mutex_free ((hcamcorder->mtsafe).gst_state_lock);
		(hcamcorder->mtsafe).gst_state_lock = NULL;
	}
	if ((hcamcorder->mtsafe).message_cb_lock) {
		g_mutex_free ((hcamcorder->mtsafe).message_cb_lock);
		(hcamcorder->mtsafe).message_cb_lock = NULL;
	}
	if ((hcamcorder->mtsafe).vcapture_cb_lock) {
		g_mutex_free ((hcamcorder->mtsafe).vcapture_cb_lock);
		(hcamcorder->mtsafe).vcapture_cb_lock = NULL;
	}
	if ((hcamcorder->mtsafe).vstream_cb_lock) {
		g_mutex_free ((hcamcorder->mtsafe).vstream_cb_lock);
		(hcamcorder->mtsafe).vstream_cb_lock = NULL;
	}
	if ((hcamcorder->mtsafe).astream_cb_lock) {
		g_mutex_free ((hcamcorder->mtsafe).astream_cb_lock);
		(hcamcorder->mtsafe).astream_cb_lock = NULL;
	}

	pthread_mutex_destroy(&(hcamcorder->sound_lock));
	pthread_cond_destroy(&(hcamcorder->sound_cond));
	pthread_mutex_destroy(&(hcamcorder->snd_info.open_mutex));
	pthread_cond_destroy(&(hcamcorder->snd_info.open_cond));

	/* Release handle */
	memset(hcamcorder, 0x00, sizeof(mmf_camcorder_t));
	free(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	if (hcamcorder) {
		_mmcam_dbg_err("Destroy fail (type %d, state %d)", hcamcorder->type, state);
	}

	_mmcam_dbg_err("Destroy fail (ret %x)", ret);

	return ret;
}


int _mmcamcorder_realize(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM = MM_CAMCORDER_STATE_NULL;
	int state_TO = MM_CAMCORDER_STATE_READY;
	int sessionType = MM_SESSION_TYPE_EXCLUSIVE;
	int errorcode = MM_ERROR_NONE;
	int display_surface_type = MM_DISPLAY_SURFACE_X;
	double motion_rate = _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE;
	char *videosink_element_type = NULL;
	char *videosink_name = NULL;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	/* Check quick-device-close for emergency */
	if (hcamcorder->quick_device_close) {
		_mmcam_dbg_err("_mmcamcorder_realize can't be called!!!!");
		ret = MM_ERROR_CAMCORDER_DEVICE;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* set camera state to vconf key */
	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		/* get current camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_OPEN);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_OPEN);

		/* LOCK PM control */
		pm_lock_state(LCD_NORMAL, GOTO_STATE_NOW | HOLD_KEY_BLOCK, 0);
		_mmcam_dbg_log("LOCK PM State");
	}

	/* Set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret < 0) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	mm_camcorder_get_attributes(handle, NULL,
	                            MMCAM_MODE, &hcamcorder->type,
	                            MMCAM_DISPLAY_SURFACE, &display_surface_type,
	                            MMCAM_CAMERA_RECORDING_MOTION_RATE, &motion_rate,
	                            NULL);

	/* Get profile mode */
	_mmcam_dbg_log("Profile mode set is (%d)", hcamcorder->type);

	/* Check and register ASM */
	if (MM_ERROR_NONE != _mm_session_util_read_type(-1, &sessionType)) {
		_mmcam_dbg_warn("Read _mm_session_util_read_type failed. use default \"exclusive\" type");
		sessionType = MM_SESSION_TYPE_EXCLUSIVE;
	}
	if ((sessionType != MM_SESSION_TYPE_CALL) && (sessionType != MM_SESSION_TYPE_VIDEOCALL)) {
		int asm_session_type = ASM_EVENT_NONE;
		ASM_resource_t mm_resource = ASM_RESOURCE_NONE;
		
		asm_session_type = __mmcamcorder_asm_get_event_type(sessionType);
		switch (hcamcorder->type) {
		case MM_CAMCORDER_MODE_VIDEO:
			mm_resource = ASM_RESOURCE_CAMERA | ASM_RESOURCE_VIDEO_OVERLAY | ASM_RESOURCE_HW_ENCODER;
			break;
		case MM_CAMCORDER_MODE_AUDIO:
			mm_resource = ASM_RESOURCE_NONE;
			break;
		case MM_CAMCORDER_MODE_IMAGE:
		default:
			mm_resource = ASM_RESOURCE_CAMERA | ASM_RESOURCE_VIDEO_OVERLAY;
			break;
		}

		if (!ASM_set_sound_state(hcamcorder->asm_handle, asm_session_type,
		                         ASM_STATE_PLAYING, mm_resource, &errorcode)) {
			debug_error("Set state to playing failed 0x%X\n", errorcode);
			ret =  MM_ERROR_POLICY_BLOCKED;
			goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
		}
	}

	/* alloc sub context */
	hcamcorder->sub_context = _mmcamcorder_alloc_subcontext(hcamcorder->type);
	if(!hcamcorder->sub_context) {
		ret = MM_ERROR_CAMCORDER_RESOURCE_CREATION;
		goto _ERR_CAMCORDER_CMD;
	}

	/* Set basic configure information */
	if (motion_rate != _MMCAMCORDER_DEFAULT_RECORDING_MOTION_RATE) {
		hcamcorder->sub_context->is_modified_rate = TRUE;
	}

	switch (display_surface_type) {
	case MM_DISPLAY_SURFACE_X:
		videosink_element_type = strdup("VideosinkElementX");
		break;
	case MM_DISPLAY_SURFACE_EVAS:
		videosink_element_type = strdup("VideosinkElementEvas");
		break;
	case MM_DISPLAY_SURFACE_GL:
		videosink_element_type = strdup("VideosinkElementGL");
		break;
	case MM_DISPLAY_SURFACE_NULL:
		videosink_element_type = strdup("VideosinkElementNull");
		break;
	default:
		videosink_element_type = strdup("VideosinkElementX");
		break;
	}

	/* check string of videosink element */
	if (videosink_element_type) {
		_mmcamcorder_conf_get_element(hcamcorder->conf_main,
		                              CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
		                              videosink_element_type,
		                              &hcamcorder->sub_context->VideosinkElement);
		free(videosink_element_type);
		videosink_element_type = NULL;
	} else {
		_mmcam_dbg_warn("strdup failed(display_surface_type %d). Use default X type",
		                display_surface_type);

		_mmcamcorder_conf_get_element(hcamcorder->conf_main,
		                              CONFIGURE_CATEGORY_MAIN_VIDEO_OUTPUT,
		                              _MMCAMCORDER_DEFAULT_VIDEOSINK_TYPE,
		                              &hcamcorder->sub_context->VideosinkElement );
	}

	_mmcamcorder_conf_get_value_element_name(hcamcorder->sub_context->VideosinkElement, &videosink_name);
	_mmcam_dbg_log("Videosink name : %s", videosink_name);

	_mmcamcorder_conf_get_value_int(hcamcorder->conf_ctrl,
	                                CONFIGURE_CATEGORY_CTRL_CAPTURE,
	                                "SensorEncodedCapture",
	                                &(hcamcorder->sub_context->SensorEncodedCapture));
	_mmcam_dbg_log("Support sensor encoded capture : %d", hcamcorder->sub_context->SensorEncodedCapture);

	/* create pipeline */
	__ta__("    _mmcamcorder_create_pipeline",
	ret = _mmcamcorder_create_pipeline(handle, hcamcorder->type);
	);
	if (ret != MM_ERROR_NONE) {
		/* check internal error of gstreamer */
		if (hcamcorder->sub_context->error_code != MM_ERROR_NONE) {
			ret = hcamcorder->sub_context->error_code;
			_mmcam_dbg_log("gstreamer error is occurred. return it %x", ret);
		}

		/* release sub context */
		_mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
		hcamcorder->sub_context = NULL;
		goto _ERR_CAMCORDER_CMD;
	}

	/* set command function */
	ret = _mmcamcorder_set_functions(handle, hcamcorder->type);
	if (ret != MM_ERROR_NONE) {
		_mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
		_mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
		hcamcorder->sub_context = NULL;
		goto _ERR_CAMCORDER_CMD;
	}

	_mmcamcorder_set_state(handle, state_TO);

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);
	
_ERR_CAMCORDER_CMD_PRECON:
	_mmcam_dbg_err("Realize fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	/* set camera state to vconf key */
	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		/* get current camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_NULL);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_NULL);
	}

	return ret;
}


int _mmcamcorder_unrealize(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM = MM_CAMCORDER_STATE_READY;
	int state_TO = MM_CAMCORDER_STATE_NULL;
	int sessionType = MM_SESSION_TYPE_SHARE;
	int asm_session_type = ASM_EVENT_NONE;
	ASM_resource_t mm_resource = ASM_RESOURCE_NONE;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* Release sound handle */
	__ta__("_mmcamcorder_sound_finalize",
	ret = _mmcamcorder_sound_finalize(handle);
	);
	_mmcam_dbg_log("sound finalize [%d]", ret);

	/* Release SubContext */
	if (hcamcorder->sub_context) {
		/* destroy pipeline */
		_mmcamcorder_destroy_pipeline(handle, hcamcorder->type);
		/* Deallocate SubContext */
		_mmcamcorder_dealloc_subcontext(hcamcorder->sub_context);
		hcamcorder->sub_context = NULL;
	}

	/* Deinitialize main context member */

	hcamcorder->command = NULL;

	/* check who calls unrealize. it's no need to set ASM state if caller is ASM */
	if (hcamcorder->state_change_by_system != _MMCAMCORDER_STATE_CHANGE_BY_ASM) {
		if (MM_ERROR_NONE != _mm_session_util_read_type(-1, &sessionType)) {
			_mmcam_dbg_err("_mm_session_util_read_type Fail\n");
		}

		if ((sessionType != MM_SESSION_TYPE_CALL) && (sessionType != MM_SESSION_TYPE_VIDEOCALL)) {
			asm_session_type = ASM_EVENT_NONE;
			mm_resource = ASM_RESOURCE_NONE;
			asm_session_type = __mmcamcorder_asm_get_event_type(sessionType);

			switch (hcamcorder->type) {
			case MM_CAMCORDER_MODE_VIDEO:
				mm_resource = ASM_RESOURCE_CAMERA | ASM_RESOURCE_VIDEO_OVERLAY | ASM_RESOURCE_HW_ENCODER;
				break;
			case MM_CAMCORDER_MODE_AUDIO:
				mm_resource = ASM_RESOURCE_NONE;
				break;
			case MM_CAMCORDER_MODE_IMAGE:
			default:
				mm_resource = ASM_RESOURCE_CAMERA | ASM_RESOURCE_VIDEO_OVERLAY;
				break;
			}

			/* Call session is not ended here */
			if (!ASM_set_sound_state(hcamcorder->asm_handle, asm_session_type,
						ASM_STATE_STOP, mm_resource, &ret)) {
				_mmcam_dbg_err("Set state to playing failed 0x%X\n", ret);
			}
		}
	}

	_mmcamcorder_set_state(handle, state_TO);

	/* set camera state to vconf key */
	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		/* get current camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_NULL);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_NULL);

		/* UNLOCK PM State */
		pm_unlock_state(LCD_NORMAL, PM_RESET_TIMER);
		_mmcam_dbg_log("UNLOCK PM State");
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* send message */
	_mmcam_dbg_err("Unrealize fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}

int _mmcamcorder_start(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM = MM_CAMCORDER_STATE_READY;
	int state_TO =MM_CAMCORDER_STATE_PREPARE;

	_MMCamcorderSubContext *sc = NULL;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	/* check quick-device-close for emergency */
	if (hcamcorder->quick_device_close) {
		_mmcam_dbg_err("_mmcamcorder_start can't be called!!!!");
		ret = MM_ERROR_CAMCORDER_DEVICE;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* initialize error code */
	hcamcorder->sub_context->error_code = MM_ERROR_NONE;

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_START);
	if (ret != MM_ERROR_NONE) {
		goto _ERR_CAMCORDER_CMD;
	}

	_mmcamcorder_set_state(handle, state_TO);

	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		_mmcamcorder_set_attribute_to_camsensor(handle);

		/* check camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);

		/* set camera state to vconf key */
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_PREVIEW);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_PREVIEW);
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);
	
_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* check internal error of gstreamer */
	if (hcamcorder->sub_context->error_code != MM_ERROR_NONE) {
		ret = hcamcorder->sub_context->error_code;
		hcamcorder->sub_context->error_code = MM_ERROR_NONE;

		_mmcam_dbg_log("gstreamer error is occurred. return it %x", ret);
	}

	_mmcam_dbg_err("Start fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}

int _mmcamcorder_stop(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM = MM_CAMCORDER_STATE_PREPARE;
	int state_TO = MM_CAMCORDER_STATE_READY;
	int frame_rate = 0;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_STOP);
	if (ret != MM_ERROR_NONE) {
		goto _ERR_CAMCORDER_CMD;
	}

	/* KPI : frame rate */
	frame_rate=_mmcamcorder_video_average_framerate(handle);
	__ta__(__tafmt__("MM_CAM_006:: Frame per sec : %d", frame_rate), ;);

	_mmcamcorder_set_state(handle, state_TO);

	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		/* check camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);

		/* set camera state to vconf key */
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_OPEN);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_OPEN);
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* send message */
	_mmcam_dbg_err("Stop fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}


int _mmcamcorder_capture_start(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM = MM_CAMCORDER_STATE_PREPARE;
	int state_FROM2 = MM_CAMCORDER_STATE_RECORDING;
	int state_TO = MM_CAMCORDER_STATE_CAPTURING;
	char *err_name = NULL;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM && state != state_FROM2) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_CAPTURE);
	if (ret != MM_ERROR_NONE) {
		goto _ERR_CAMCORDER_CMD;
	}

	/* Do not change state when recording snapshot capture */
	if (state == state_FROM) {
		_mmcamcorder_set_state(handle, state_TO);
	}

	/* Init break continuous shot attr */
	mm_camcorder_set_attributes(handle, &err_name, "capture-break-cont-shot", 0, NULL);

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* send message */
	_mmcam_dbg_err("Capture start fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}

int _mmcamcorder_capture_stop(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM = MM_CAMCORDER_STATE_CAPTURING;
	int state_TO = MM_CAMCORDER_STATE_PREPARE;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PREVIEW_START);
	if (ret != MM_ERROR_NONE) {
		goto _ERR_CAMCORDER_CMD;
	}

	_mmcamcorder_set_state(handle, state_TO);

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	MMTA_ACUM_ITEM_END("Real First Capture Start", FALSE);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* send message */
	_mmcam_dbg_err("Capture stop fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}

int _mmcamcorder_record(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM1 = MM_CAMCORDER_STATE_PREPARE;
	int state_FROM2 = MM_CAMCORDER_STATE_PAUSED;
	int state_TO = MM_CAMCORDER_STATE_RECORDING;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM1 && state != state_FROM2) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* initialize error code */
	hcamcorder->sub_context->error_code = MM_ERROR_NONE;

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_RECORD);
	if (ret != MM_ERROR_NONE) {
		goto _ERR_CAMCORDER_CMD;
	}

	_mmcamcorder_set_state(handle, state_TO);

	/* set camera state to vconf key */
	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		/* get current camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_RECORDING);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_RECORDING);
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* check internal error of gstreamer */
	if (hcamcorder->sub_context->error_code != MM_ERROR_NONE) {
		ret = hcamcorder->sub_context->error_code;
		hcamcorder->sub_context->error_code = MM_ERROR_NONE;

		_mmcam_dbg_log("gstreamer error is occurred. return it %x", ret);
	}

	_mmcam_dbg_err("Record fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}


int _mmcamcorder_pause(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM = MM_CAMCORDER_STATE_RECORDING;
	int state_TO = MM_CAMCORDER_STATE_PAUSED;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_PAUSE);
	if (ret != MM_ERROR_NONE) {
		goto _ERR_CAMCORDER_CMD;
	}

	_mmcamcorder_set_state(handle, state_TO);

	/* set camera state to vconf key */
	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		/* get current camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_RECORDING_PAUSE);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_RECORDING_PAUSE);
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* send message */
	_mmcam_dbg_err("Pause fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}


int _mmcamcorder_commit(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM1 = MM_CAMCORDER_STATE_RECORDING;
	int state_FROM2 = MM_CAMCORDER_STATE_PAUSED;
	int state_TO = MM_CAMCORDER_STATE_PREPARE;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	
	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM1 && state != state_FROM2) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_COMMIT);
	if (ret != MM_ERROR_NONE) {
		goto _ERR_CAMCORDER_CMD;
	}

	_mmcamcorder_set_state(handle,state_TO);

	/* set camera state to vconf key */
	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		/* get current camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_PREVIEW);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_PREVIEW);
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* send message */
	_mmcam_dbg_err("Commit fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}


int _mmcamcorder_cancel(MMHandleType handle)
{
	int ret = MM_ERROR_NONE;
	int state = MM_CAMCORDER_STATE_NONE;
	int state_FROM1 = MM_CAMCORDER_STATE_RECORDING;
	int state_FROM2 = MM_CAMCORDER_STATE_PAUSED;
	int state_TO = MM_CAMCORDER_STATE_PREPARE;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	
	_mmcam_dbg_log("");

	if (!hcamcorder) {
		_mmcam_dbg_err("Not initialized");
		ret = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
		return ret;
	}

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		ret = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _ERR_CAMCORDER_CMD_PRECON;
	}

	state = _mmcamcorder_get_state(handle);
	if (state != state_FROM1 && state != state_FROM2) {
		_mmcam_dbg_err("Wrong state(%d)", state);
		ret = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	/* set async state */
	ret = _mmcamcorder_set_async_state(handle, state_TO);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_err("Can't set async state");
		goto _ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK;
	}

	ret = hcamcorder->command((MMHandleType)hcamcorder, _MMCamcorder_CMD_CANCEL);
	if (ret != MM_ERROR_NONE) {
		goto _ERR_CAMCORDER_CMD;
	}

	_mmcamcorder_set_state(handle, state_TO);

	/* set camera state to vconf key */
	if (hcamcorder->type != MM_CAMCORDER_MODE_AUDIO) {
		int vconf_camera_state = 0;

		/* get current camera state of vconf key */
		vconf_get_int(VCONFKEY_CAMERA_STATE, &vconf_camera_state);
		vconf_set_int(VCONFKEY_CAMERA_STATE, VCONFKEY_CAMERA_STATE_PREVIEW);

		_mmcam_dbg_log("VCONFKEY_CAMERA_STATE prev %d -> cur %d",
		               vconf_camera_state, VCONFKEY_CAMERA_STATE_PREVIEW);
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	return MM_ERROR_NONE;

_ERR_CAMCORDER_CMD:
	/* set async state and (set async cancel or set state) are pair. */
	_mmcamcorder_set_async_cancel(handle);

_ERR_CAMCORDER_CMD_PRECON_AFTER_LOCK:
	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

_ERR_CAMCORDER_CMD_PRECON:
	/* send message */
	_mmcam_dbg_err("Cancel fail (type %d, state %d, ret %x)",
	               hcamcorder->type, state, ret);

	return ret;
}
/* } Internal command functions */


int _mmcamcorder_commit_async_end(MMHandleType handle)
{
	_mmcam_dbg_log("");

	_mmcam_dbg_warn("_mmcamcorder_commit_async_end : MM_CAMCORDER_STATE_PREPARE");
	_mmcamcorder_set_state(handle, MM_CAMCORDER_STATE_PREPARE);

	return MM_ERROR_NONE;
}


int _mmcamcorder_set_message_callback(MMHandleType handle, MMMessageCallback callback, void *user_data)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("%p", hcamcorder);

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	if (callback == NULL) {
		_mmcam_dbg_warn("Message Callback is disabled, because application sets it to NULL");
	}

	if (!_MMCAMCORDER_TRYLOCK_MESSAGE_CALLBACK(hcamcorder)) {
		_mmcam_dbg_warn("Application's message callback is running now");
		return MM_ERROR_CAMCORDER_INVALID_CONDITION;
	}

	/* set message callback to message handle */
	hcamcorder->msg_cb = callback;
	hcamcorder->msg_cb_param = user_data;

	_MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);

	return MM_ERROR_NONE;
}


int _mmcamcorder_set_video_stream_callback(MMHandleType handle, mm_camcorder_video_stream_callback callback, void *user_data)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	if (callback == NULL) {
		_mmcam_dbg_warn("Video Stream Callback is disabled, because application sets it to NULL");
	}

	if (!_MMCAMCORDER_TRYLOCK_VSTREAM_CALLBACK(hcamcorder)) {
		_mmcam_dbg_warn("Application's video stream callback is running now");
		return MM_ERROR_CAMCORDER_INVALID_CONDITION;
	}

	hcamcorder->vstream_cb = callback;
	hcamcorder->vstream_cb_param = user_data;

	_MMCAMCORDER_UNLOCK_VSTREAM_CALLBACK(hcamcorder);

	return MM_ERROR_NONE;
}


int _mmcamcorder_set_audio_stream_callback(MMHandleType handle, mm_camcorder_audio_stream_callback callback, void *user_data)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	if (callback == NULL) {
		_mmcam_dbg_warn("Audio Stream Callback is disabled, because application sets it to NULL");
	}

	if (!_MMCAMCORDER_TRYLOCK_ASTREAM_CALLBACK(hcamcorder)) {
		_mmcam_dbg_warn("Application's audio stream callback is running now");
		return MM_ERROR_CAMCORDER_INVALID_CONDITION;
	}

	hcamcorder->astream_cb = callback;
	hcamcorder->astream_cb_param = user_data;

	_MMCAMCORDER_UNLOCK_ASTREAM_CALLBACK(hcamcorder);

	return MM_ERROR_NONE;
}


int _mmcamcorder_set_video_capture_callback(MMHandleType handle, mm_camcorder_video_capture_callback callback, void *user_data)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	if (callback == NULL) {
		_mmcam_dbg_warn("Video Capture Callback is disabled, because application sets it to NULLL");
	}

	if (!_MMCAMCORDER_TRYLOCK_VCAPTURE_CALLBACK(hcamcorder)) {
		_mmcam_dbg_warn("Application's video capture callback is running now");
		return MM_ERROR_CAMCORDER_INVALID_CONDITION;
	}

	hcamcorder->vcapture_cb = callback;
	hcamcorder->vcapture_cb_param = user_data;

	_MMCAMCORDER_UNLOCK_VCAPTURE_CALLBACK(hcamcorder);

	return MM_ERROR_NONE;
}

int _mmcamcorder_get_current_state(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	return _mmcamcorder_get_state(handle);
}

int _mmcamcorder_init_focusing(MMHandleType handle)
{
	int ret = TRUE;
	int state = MM_CAMCORDER_STATE_NONE;
	int focus_mode = MM_CAMCORDER_FOCUS_MODE_NONE;
	int af_range = MM_CAMCORDER_AUTO_FOCUS_NORMAL;
	int sensor_focus_mode = 0;
	int sensor_af_range = 0;
	int current_focus_mode = 0;
	int current_af_range = 0;
	mmf_camcorder_t *hcamcorder = NULL;
	_MMCamcorderSubContext *sc = NULL;
	mmf_attrs_t *attr = NULL;
	GstCameraControl *control = NULL;

	_mmcam_dbg_log("");

	hcamcorder = MMF_CAMCORDER(handle);
	mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
	mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		return MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
	}

	state = _mmcamcorder_get_state(handle);

	if (state == MM_CAMCORDER_STATE_CAPTURING ||
	    state < MM_CAMCORDER_STATE_PREPARE) {
		_mmcam_dbg_err( "Not proper state. state[%d]", state );
		_MMCAMCORDER_UNLOCK_CMD(hcamcorder);
		return MM_ERROR_CAMCORDER_INVALID_STATE;
	}

	if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
		_mmcam_dbg_log("Can't cast Video source into camera control.");
		_MMCAMCORDER_UNLOCK_CMD(hcamcorder);
		return MM_ERROR_NONE;
	}
	control = GST_CAMERA_CONTROL (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);

	ret = gst_camera_control_stop_auto_focus(control);
	if (!ret) {
		_mmcam_dbg_err("Auto focusing stop fail.");
		_MMCAMCORDER_UNLOCK_CMD(hcamcorder);
		return MM_ERROR_CAMCORDER_DEVICE_IO;
	}

	/* Initialize lens position */
	attr = (mmf_attrs_t *)MMF_CAMCORDER_ATTRS(handle);
	mm_camcorder_get_attributes(handle, NULL,
	                            MMCAM_CAMERA_FOCUS_MODE, &focus_mode,
	                            MMCAM_CAMERA_AF_SCAN_RANGE, &af_range,
	                            NULL);
	sensor_af_range = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_AF_SCAN_RANGE, af_range);
	sensor_focus_mode = _mmcamcorder_convert_msl_to_sensor(handle, MM_CAM_CAMERA_FOCUS_MODE, focus_mode);

	gst_camera_control_get_focus(control, &current_focus_mode, &current_af_range);

	if (current_focus_mode != sensor_focus_mode ||
	    current_af_range != sensor_af_range) {
		ret = gst_camera_control_set_focus(control, sensor_focus_mode, sensor_af_range);
	} else {
		_mmcam_dbg_log("No need to init FOCUS [mode:%d, range:%d]", focus_mode, af_range );
		ret = TRUE;
	}

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	if (ret) {
		_mmcam_dbg_log("Lens init success.");
		return MM_ERROR_NONE;
	} else {
		_mmcam_dbg_err("Lens init fail.");
		return MM_ERROR_CAMCORDER_DEVICE_IO;
	}
}

int _mmcamcorder_adjust_focus(MMHandleType handle, int direction)
{
	int state;
	int focus_mode = MM_CAMCORDER_FOCUS_MODE_NONE;
	int ret = MM_ERROR_UNKNOWN;
	char *err_attr_name = NULL;

	mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
	mmf_return_val_if_fail(direction, MM_ERROR_CAMCORDER_INVALID_ARGUMENT);

	_mmcam_dbg_log("");

	state = _mmcamcorder_get_state(handle);
	if (state == MM_CAMCORDER_STATE_CAPTURING ||
	    state < MM_CAMCORDER_STATE_PREPARE) {
		_mmcam_dbg_err("Not proper state. state[%d]", state);
		return MM_ERROR_CAMCORDER_INVALID_STATE;
	}

	/* TODO : call a auto or manual focus function */
	ret = mm_camcorder_get_attributes(handle, &err_attr_name,
	                                  MMCAM_CAMERA_FOCUS_MODE, &focus_mode,
	                                  NULL);
	if (ret != MM_ERROR_NONE) {
		_mmcam_dbg_warn("Get focus-mode fail. (%s:%x)", err_attr_name, ret);
		SAFE_FREE (err_attr_name);
		return ret;
	}

	if (focus_mode == MM_CAMCORDER_FOCUS_MODE_MANUAL) {
		return _mmcamcorder_adjust_manual_focus(handle, direction);
	} else if (focus_mode == MM_CAMCORDER_FOCUS_MODE_AUTO ||
		   focus_mode == MM_CAMCORDER_FOCUS_MODE_TOUCH_AUTO ||
		   focus_mode == MM_CAMCORDER_FOCUS_MODE_CONTINUOUS) {
		return _mmcamcorder_adjust_auto_focus(handle);
	} else {
		_mmcam_dbg_err("It doesn't adjust focus. Focusing mode(%d)", focus_mode);
		return MM_ERROR_CAMCORDER_NOT_SUPPORTED;
	}
}

int _mmcamcorder_adjust_manual_focus(MMHandleType handle, int direction)
{
	int max_level = 0;
	int min_level = 0;
	int cur_level = 0;
	int focus_level = 0;
	float unit_level = 0;
	GstCameraControl *control = NULL;
	_MMCamcorderSubContext *sc = NULL;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
	mmf_return_val_if_fail(_MMFCAMCORDER_FOCUS_TOTAL_LEVEL != 1, MM_ERROR_CAMCORDER_NOT_SUPPORTED);
	mmf_return_val_if_fail((direction >= MM_CAMCORDER_MF_LENS_DIR_FORWARD) &&
	                       (direction <= MM_CAMCORDER_MF_LENS_DIR_BACKWARD),
	                       MM_ERROR_CAMCORDER_INVALID_ARGUMENT );

	sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
	mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
		_mmcam_dbg_log("Can't cast Video source into camera control.");
		return MM_ERROR_NONE;
	}
	control = GST_CAMERA_CONTROL (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);

	/* TODO : get max, min level */
	if (max_level - min_level + 1 < _MMFCAMCORDER_FOCUS_TOTAL_LEVEL) {
		_mmcam_dbg_warn("Total level  of manual focus of MMF is greater than that of the camera driver.");
	}

	unit_level = ((float)max_level - (float)min_level)/(float)(_MMFCAMCORDER_FOCUS_TOTAL_LEVEL - 1);

	if (!gst_camera_control_get_focus_level(control, &cur_level)) {
		_mmcam_dbg_err("Can't get current level of manual focus.");
		return MM_ERROR_CAMCORDER_DEVICE_IO;
	}

	//TODO : adjust unit level value
	if (direction == MM_CAMCORDER_MF_LENS_DIR_FORWARD) {
		focus_level = cur_level + unit_level;
	} else if (direction == MM_CAMCORDER_MF_LENS_DIR_BACKWARD) {
		focus_level = cur_level - unit_level;
	}

	if (focus_level > max_level) {
		focus_level = max_level;
	} else if (focus_level < min_level) {
		focus_level = min_level;
	}

	if (!gst_camera_control_set_focus_level(control, focus_level)) {
		_mmcam_dbg_err("Manual focusing fail.");
		return MM_ERROR_CAMCORDER_DEVICE_IO;
	}

	return MM_ERROR_NONE;
}


int _mmcamcorder_adjust_auto_focus(MMHandleType handle)
{
	gboolean ret;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	GstCameraControl *control = NULL;
	_MMCamcorderSubContext *sc = NULL;

	mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("");

	sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);

	if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
		_mmcam_dbg_log("Can't cast Video source into camera control.");
		return MM_ERROR_NONE;
	}
	control = GST_CAMERA_CONTROL (sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);

	/* Start AF */
	ret = gst_camera_control_start_auto_focus(control);
	if (ret) {
		_mmcam_dbg_log("Auto focusing start success.");
		return MM_ERROR_NONE;
	} else {
		_mmcam_dbg_err("Auto focusing start fail.");
		return MM_ERROR_CAMCORDER_DEVICE_IO;
	}
}

int _mmcamcorder_stop_focusing(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;

	int ret, state;
	GstCameraControl *control = NULL;

	mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("");

	sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);

	if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
		_mmcam_dbg_err("Another command is running.");
		return MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
	}

	state = _mmcamcorder_get_state(handle);
	if (state == MM_CAMCORDER_STATE_CAPTURING ||
	    state < MM_CAMCORDER_STATE_PREPARE) {
		_mmcam_dbg_err( "Not proper state. state[%d]", state );
		_MMCAMCORDER_UNLOCK_CMD(hcamcorder);
		return MM_ERROR_CAMCORDER_INVALID_STATE;
	}

	if (!GST_IS_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst)) {
		_mmcam_dbg_log("Can't cast Video source into camera control.");
		_MMCAMCORDER_UNLOCK_CMD(hcamcorder);
		return MM_ERROR_NONE;
	}
	control = GST_CAMERA_CONTROL(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);

	ret = gst_camera_control_stop_auto_focus(control);

	_MMCAMCORDER_UNLOCK_CMD(hcamcorder);

	if (ret) {
		_mmcam_dbg_log("Auto focusing stop success.");
		return MM_ERROR_NONE;
	} else {
		_mmcam_dbg_err("Auto focusing stop fail.");
		return MM_ERROR_CAMCORDER_DEVICE_IO;
	}
}


/*-----------------------------------------------
|	  CAMCORDER INTERNAL LOCAL		|
-----------------------------------------------*/
static gboolean
__mmcamcorder_gstreamer_init(camera_conf * conf)
{
	static const int max_argc = 10;
	int i = 0;
	int cnt_str = 0;
	gint *argc = NULL;
	gchar **argv = NULL;
	GError *err = NULL;
	gboolean ret = FALSE;
	type_string_array *GSTInitOption = NULL;

	mmf_return_val_if_fail(conf, FALSE);

	_mmcam_dbg_log("");

	/* alloc */
	argc = malloc(sizeof(int));
	argv = malloc(sizeof(gchar *) * max_argc);

	if (!argc || !argv) {
		goto ERROR;
	}

	memset(argv, 0, sizeof(gchar *) * max_argc);

	/* add initial */
	*argc = 1;
	argv[0] = g_strdup("mmcamcorder");

	/* add gst_param */
	_mmcamcorder_conf_get_value_string_array(conf,
	                                         CONFIGURE_CATEGORY_MAIN_GENERAL,
	                                         "GSTInitOption",
	                                         &GSTInitOption);
	if (GSTInitOption != NULL && GSTInitOption->value) {
		cnt_str = GSTInitOption->count;
		for( ; *argc < max_argc && *argc <= cnt_str ; (*argc)++ )
		{
			argv[*argc] = g_strdup(GSTInitOption->value[(*argc)-1]);
		}
	}

	_mmcam_dbg_log("initializing gstreamer with following parameter[argc:%d]", *argc);

	for (i = 0; i < *argc; i++) {
		_mmcam_dbg_log("argv[%d] : %s", i, argv[i]);
	}

	/* initializing gstreamer */
	__ta__("        gst_init_check",
	ret = gst_init_check (argc, &argv, &err);
	);

	if (!ret) {
		_mmcam_dbg_err("Could not initialize GStreamer: %s\n",
		               err ? err->message : "unknown error occurred");
		if (err) {
			g_error_free (err);
		}
	}

	/* release */
	for (i = 0; i < *argc; i++) {
		if (argv[i]) {
			free(argv[i]);
			argv[i] = NULL;
		}
	}

	if (argv) {
		free(argv);
		argv = NULL;
	}

	if (argc) {
		free(argc);
		argc = NULL;
	}

	return ret;

ERROR:
	_mmcam_dbg_err("failed to initialize gstreamer");

	if (argv) {
		free(argv);
		argv = NULL;
	}

	if (argc) {
		free(argc);
		argc = NULL;
	}

	return FALSE;
}


int _mmcamcorder_get_state(MMHandleType handle)
{
	int state;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	mmf_return_val_if_fail(hcamcorder, -1);

	_MMCAMCORDER_LOCK_STATE(handle);

	state = hcamcorder->state;
	_mmcam_dbg_log("state=%d",state);

	_MMCAMCORDER_UNLOCK_STATE(handle);

	return state;
}


void _mmcamcorder_set_state(MMHandleType handle, int state)
{
	int old_state;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderMsgItem msg;

	mmf_return_if_fail(hcamcorder);

	_mmcam_dbg_log("");

	_MMCAMCORDER_LOCK_STATE(handle);

	old_state = hcamcorder->state;
	if(old_state != state) {
		hcamcorder->state = state;
		hcamcorder->target_state = state;

		_mmcam_dbg_log("set state[%d] and send state-changed message", state);

		/* To discern who changes the state */
		switch (hcamcorder->state_change_by_system) {
		case _MMCAMCORDER_STATE_CHANGE_BY_ASM:
			msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_ASM;
			msg.param.state.code = hcamcorder->asm_event_code;
			break;
		case _MMCAMCORDER_STATE_CHANGE_NORMAL:
		default:
			msg.id = MM_MESSAGE_CAMCORDER_STATE_CHANGED;
			msg.param.state.code = MM_ERROR_NONE;
			break;
		}

		msg.param.state.previous = old_state;
		msg.param.state.current = state;

		_mmcam_dbg_log("_mmcamcroder_send_message : msg : %p, id:%x", &msg, msg.id);
		_mmcamcroder_send_message(handle, &msg);
	}

	_MMCAMCORDER_UNLOCK_STATE(handle);

	return;
}


int _mmcamcorder_get_async_state(MMHandleType handle)
{
	int state;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_MMCAMCORDER_LOCK_STATE(handle);
	state = hcamcorder->target_state;

	_MMCAMCORDER_UNLOCK_STATE(handle);

	return state;
}

int _mmcamcorder_set_async_state(MMHandleType handle, int target_state)
{
	int error = MM_ERROR_NONE;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_INVALID_CONDITION);
		
	_mmcam_dbg_log("");

	if (hcamcorder->sync_state_change) {
		return error;
	}

	_MMCAMCORDER_LOCK_STATE(handle);

	/* check changing state */
	if (hcamcorder->state != hcamcorder->target_state) {
		_mmcam_dbg_warn("State is changing now.");
		error = MM_ERROR_CAMCORDER_CMD_IS_RUNNING;
		goto _SET_ASYNC_END;
	}

	/* check already set */
	if (hcamcorder->state == target_state) {
		_mmcam_dbg_log("Target state already set.");
		error = MM_ERROR_CAMCORDER_INVALID_STATE;
		goto _SET_ASYNC_END;
	}

	hcamcorder->target_state = target_state;

	_mmcam_dbg_log("Done");

_SET_ASYNC_END:
	_MMCAMCORDER_UNLOCK_STATE(handle);
	return error;
}


gboolean _mmcamcorder_set_async_cancel(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	
	mmf_return_val_if_fail(hcamcorder, FALSE);
	
	_mmcam_dbg_log("");

	if (hcamcorder->sync_state_change) {
		return TRUE;
	}

	_MMCAMCORDER_LOCK_STATE(handle);

	if (hcamcorder->target_state != hcamcorder->state) {
		hcamcorder->target_state = hcamcorder->state;
		_mmcam_dbg_log("Async state change is canceled.");
		_MMCAMCORDER_UNLOCK_STATE(handle);
		return TRUE;
	} else {
		_mmcam_dbg_log("Nothing to be cancel or Already processed.");
	}

	_MMCAMCORDER_UNLOCK_STATE(handle);

	return FALSE;
}


gboolean _mmcamcorder_is_state_changing(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	mmf_return_val_if_fail(hcamcorder, FALSE);
	
	_mmcam_dbg_log("(%d)", (hcamcorder->target_state != hcamcorder->state));

	if (hcamcorder->sync_state_change) {
		return FALSE;
	}

	_MMCAMCORDER_LOCK_STATE(handle);

	if (hcamcorder->target_state != hcamcorder->state) {
		_MMCAMCORDER_UNLOCK_STATE(handle);
		return TRUE;
	}

	_MMCAMCORDER_UNLOCK_STATE(handle);

	return FALSE;
}


_MMCamcorderSubContext *_mmcamcorder_alloc_subcontext(int type)
{
	int i;
	_MMCamcorderSubContext *sc = NULL;

	_mmcam_dbg_log("");

	/* alloc container */
	sc = (_MMCamcorderSubContext *)malloc(sizeof(_MMCamcorderSubContext));
	mmf_return_val_if_fail(sc != NULL, NULL);

	/* init members */
	memset(sc, 0x00, sizeof(_MMCamcorderSubContext));

	sc->element_num = _MMCamcorder_PIPELINE_ELEMENT_NUM;

	/* alloc info for each mode */
	switch (type) {
	case MM_CAMCORDER_MODE_IMAGE:
		sc->info = malloc( sizeof(_MMCamcorderImageInfo));
		if(sc->info == NULL) {
			_mmcam_dbg_err("Failed to alloc info structure");
			free(sc);
			return NULL;
		}
		memset(sc->info, 0x00, sizeof(_MMCamcorderImageInfo));
		break;
	case MM_CAMCORDER_MODE_AUDIO:
		sc->info = malloc( sizeof(_MMCamcorderAudioInfo));
		if(sc->info == NULL) {
			_mmcam_dbg_err("Failed to alloc info structure");
			free(sc);
			return NULL;
		}
		memset(sc->info, 0x00, sizeof(_MMCamcorderAudioInfo));
		break;
	case MM_CAMCORDER_MODE_VIDEO:
		sc->info = malloc( sizeof(_MMCamcorderVideoInfo));
		if(sc->info == NULL) {
			_mmcam_dbg_err("Failed to alloc info structure");
			free(sc);
			return NULL;
		}
		memset(sc->info, 0x00, sizeof(_MMCamcorderVideoInfo));
		break;
	default:
		_mmcam_dbg_err("unknown type[%d]", type);
		free(sc);
		return NULL;
	}

	/* alloc element array */
	sc->element = (_MMCamcorderGstElement *)malloc(sizeof(_MMCamcorderGstElement) * sc->element_num);
	if(!sc->element) {
		_mmcam_dbg_err("Failed to alloc element structure");
		free(sc->info);
		free(sc);
		return NULL;
	}

	for (i = 0; i< sc->element_num; i++) {
		sc->element[i].id = _MMCAMCORDER_NONE;
		sc->element[i].gst = NULL;
	}

	sc->fourcc = 0x80000000;
	sc->cam_stability_count = 0;
	sc->drop_vframe = 0;
	sc->pass_first_vframe = 0;
	sc->is_modified_rate = FALSE;

	return sc;
}


void _mmcamcorder_dealloc_subcontext(_MMCamcorderSubContext *sc)
{
	_mmcam_dbg_log("");

	if (sc) {
		if (sc->element) {
			free(sc->element);
			sc->element = NULL;
		}

		if (sc->info) {
			free(sc->info);
			sc->info = NULL;
		}

		free(sc);
		sc = NULL;
	}

	return;
}


int _mmcamcorder_set_functions(MMHandleType handle, int type)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);

	_mmcam_dbg_log("");

	switch (type) {
	case MM_CAMCORDER_MODE_VIDEO:
		hcamcorder->command = _mmcamcorder_video_command;
		break;
	case MM_CAMCORDER_MODE_AUDIO:
		hcamcorder->command = _mmcamcorder_audio_command;
		break;
	case MM_CAMCORDER_MODE_IMAGE:
		hcamcorder->command = _mmcamcorder_image_command;
		break;
	default:
		return MM_ERROR_CAMCORDER_INTERNAL;
		break;
	}

	return MM_ERROR_NONE;
}


gboolean _mmcamcorder_pipeline_cb_message(GstBus *bus, GstMessage *message, gpointer data)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);
	_MMCamcorderMsgItem msg;
	_MMCamcorderSubContext *sc = NULL;

	mmf_return_val_if_fail(hcamcorder, FALSE);
	mmf_return_val_if_fail(message, FALSE);
	//_mmcam_dbg_log("message type=(%d)", GST_MESSAGE_TYPE(message));

	switch (GST_MESSAGE_TYPE(message)) {
	case GST_MESSAGE_UNKNOWN:
		_mmcam_dbg_log("GST_MESSAGE_UNKNOWN");
		break;
	case GST_MESSAGE_EOS:
	{
		_mmcam_dbg_log ("Got EOS from element \"%s\".",
		                GST_STR_NULL(GST_ELEMENT_NAME(GST_MESSAGE_SRC(message))));

		sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
		mmf_return_val_if_fail(sc, TRUE);
		mmf_return_val_if_fail(sc->info, TRUE);

		if (hcamcorder->type == MM_CAMCORDER_MODE_VIDEO) {
			_MMCamcorderVideoInfo *info = sc->info;
			if (info->b_commiting) {
				_mmcamcorder_video_handle_eos((MMHandleType)hcamcorder);
			}
		} else if (hcamcorder->type == MM_CAMCORDER_MODE_AUDIO) {
			_MMCamcorderAudioInfo *info = sc->info;
			if (info->b_commiting) {
				_mmcamcorder_audio_handle_eos((MMHandleType)hcamcorder);
			}
		}

		sc->bget_eos = TRUE;

		break;
	}
	case GST_MESSAGE_ERROR:
	{
		GError *err;
		gchar *debug;
		gst_message_parse_error(message, &err, &debug);

		_mmcam_dbg_err ("GSTERR: %s", err->message);
		_mmcam_dbg_err ("Error Debug: %s", debug);

		__mmcamcorder_handle_gst_error((MMHandleType)hcamcorder, message, err);

		g_error_free (err);
		g_free (debug);
		break;
	}
	case GST_MESSAGE_WARNING:
	{
		GError *err;
		gchar *debug;
		gst_message_parse_warning (message, &err, &debug);

		_mmcam_dbg_warn("GSTWARN: %s", err->message);

		__mmcamcorder_handle_gst_warning((MMHandleType)hcamcorder, message, err);

		g_error_free (err);
		g_free (debug);
		break;
	}
	case GST_MESSAGE_INFO:
		_mmcam_dbg_log("GST_MESSAGE_INFO");
		break;
	case GST_MESSAGE_TAG:
		_mmcam_dbg_log("GST_MESSAGE_TAG");
		break;
	case GST_MESSAGE_BUFFERING:
		_mmcam_dbg_log("GST_MESSAGE_BUFFERING");
		break;
	case GST_MESSAGE_STATE_CHANGED:
	{
		GValue *vnewstate;
		GstState newstate;
		GstElement *pipeline = NULL;

		sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
		if ((sc) && (sc->element)) {
			if (sc->element[_MMCAMCORDER_MAIN_PIPE].gst) {
				pipeline = sc->element[_MMCAMCORDER_MAIN_PIPE].gst;
				if (message->src == (GstObject*)pipeline) {
					vnewstate = (GValue*)gst_structure_get_value(message->structure, "new-state");
					newstate = (GstState)vnewstate->data[0].v_int;
					_mmcam_dbg_log("GST_MESSAGE_STATE_CHANGED[%s]",gst_element_state_get_name(newstate));
				}
			}
		}
		break;
	}
	case GST_MESSAGE_STATE_DIRTY:
		_mmcam_dbg_log("GST_MESSAGE_STATE_DIRTY");
		break;
	case GST_MESSAGE_STEP_DONE:
		_mmcam_dbg_log("GST_MESSAGE_STEP_DONE");
		break;
	case GST_MESSAGE_CLOCK_PROVIDE:
		_mmcam_dbg_log("GST_MESSAGE_CLOCK_PROVIDE");
		break;
	case GST_MESSAGE_CLOCK_LOST:
		_mmcam_dbg_log("GST_MESSAGE_CLOCK_LOST");
		break;
	case GST_MESSAGE_NEW_CLOCK:
	{
		GstClock *clock;
		gst_message_parse_new_clock(message, &clock);
		_mmcam_dbg_log("GST_MESSAGE_NEW_CLOCK : %s", (clock ? GST_OBJECT_NAME (clock) : "NULL"));
		break;
	}
	case GST_MESSAGE_STRUCTURE_CHANGE:
		_mmcam_dbg_log("GST_MESSAGE_STRUCTURE_CHANGE");
		break;
	case GST_MESSAGE_STREAM_STATUS:
		_mmcam_dbg_log("GST_MESSAGE_STREAM_STATUS");
		break;
	case GST_MESSAGE_APPLICATION:
		_mmcam_dbg_log("GST_MESSAGE_APPLICATION");
		break;
	case GST_MESSAGE_ELEMENT:
		_mmcam_dbg_log("GST_MESSAGE_ELEMENT");

		if (gst_structure_has_name(message->structure, "avsysvideosrc-AF") ||
		    gst_structure_has_name(message->structure, "camerasrc-AF")) {
			int focus_state = 0;

			gst_structure_get_int(message->structure, "focus-state", &focus_state);
			_mmcam_dbg_log("Focus State:%d", focus_state);

			msg.id = MM_MESSAGE_CAMCORDER_FOCUS_CHANGED;
			msg.param.code = focus_state;
			_mmcamcroder_send_message((MMHandleType)hcamcorder, &msg);
		} else if (gst_structure_has_name(message->structure, "camerasrc-HDR")) {
			int progress = 0;
			int status = 0;

			gst_structure_get_int(message->structure, "progress", &progress);
			gst_structure_get_int(message->structure, "status", &status);
			_mmcam_dbg_log("HDR progress %d percent, status %d", progress, status);

			msg.id = MM_MESSAGE_CAMCORDER_HDR_PROGRESS;
			msg.param.code = progress;
			_mmcamcroder_send_message((MMHandleType)hcamcorder, &msg);
		} else if (gst_structure_has_name(message->structure, "camerasrc-FD")) {
			int i = 0;
			GValue *g_value = NULL;
			GstCameraControlFaceDetectInfo *fd_info = NULL;
			MMCamFaceDetectInfo *cam_fd_info = NULL;

			g_value = gst_structure_get_value(message->structure, "face-info");
			if (g_value) {
				fd_info = (GstCameraControlFaceDetectInfo *)g_value_get_pointer(g_value);
			}

			if (fd_info == NULL) {
				_mmcam_dbg_warn("fd_info is NULL");
				return TRUE;
			}

			cam_fd_info = (MMCamFaceDetectInfo *)malloc(sizeof(MMCamFaceDetectInfo));
			if (cam_fd_info == NULL) {
				_mmcam_dbg_warn("cam_fd_info alloc failed");

				free(fd_info);
				fd_info = NULL;

				return TRUE;
			}

			/* set total face count */
			cam_fd_info->num_of_faces = fd_info->num_of_faces;

			if (cam_fd_info->num_of_faces > 0) {
				cam_fd_info->face_info = (MMCamFaceInfo *)malloc(sizeof(MMCamFaceInfo) * cam_fd_info->num_of_faces);
				if (cam_fd_info->face_info) {
					/* set information of each face */
					for (i = 0 ; i < fd_info->num_of_faces ; i++) {
						cam_fd_info->face_info[i].id = fd_info->face_info[i].id;
						cam_fd_info->face_info[i].score = fd_info->face_info[i].score;
						cam_fd_info->face_info[i].rect.x = fd_info->face_info[i].rect.x;
						cam_fd_info->face_info[i].rect.y = fd_info->face_info[i].rect.y;
						cam_fd_info->face_info[i].rect.width = fd_info->face_info[i].rect.width;
						cam_fd_info->face_info[i].rect.height = fd_info->face_info[i].rect.height;
						/*
						_mmcam_dbg_log("id %d, score %d, [%d,%d,%dx%d]",
						               fd_info->face_info[i].id,
						               fd_info->face_info[i].score,
						               fd_info->face_info[i].rect.x,
						               fd_info->face_info[i].rect.y,
						               fd_info->face_info[i].rect.width,
						               fd_info->face_info[i].rect.height);
						*/
					}
				} else {
					_mmcam_dbg_warn("MMCamFaceInfo alloc failed");

					/* free allocated memory that is not sent */
					free(cam_fd_info);
					cam_fd_info = NULL;
				}
			} else {
				cam_fd_info->face_info = NULL;
			}

			if (cam_fd_info) {
				/* send message  - cam_fd_info should be freed by application */
				msg.id = MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO;
				msg.param.data = cam_fd_info;
				msg.param.size = sizeof(MMCamFaceDetectInfo);
				msg.param.code = 0;

				_mmcamcroder_send_message((MMHandleType)hcamcorder, &msg);
			}

			/* free fd_info allocated by plugin */
			free(fd_info);
			fd_info = NULL;
		}
		break;
	case GST_MESSAGE_SEGMENT_START:
		_mmcam_dbg_log("GST_MESSAGE_SEGMENT_START");
		break;
	case GST_MESSAGE_SEGMENT_DONE:
		_mmcam_dbg_log("GST_MESSAGE_SEGMENT_DONE");
		break;
	case GST_MESSAGE_DURATION:
		_mmcam_dbg_log("GST_MESSAGE_DURATION");
		break;
	case GST_MESSAGE_LATENCY:
		_mmcam_dbg_log("GST_MESSAGE_LATENCY");
		break;
	case GST_MESSAGE_ASYNC_START:
		_mmcam_dbg_log("GST_MESSAGE_ASYNC_START");
		break;
	case GST_MESSAGE_ASYNC_DONE:
		_mmcam_dbg_log("GST_MESSAGE_ASYNC_DONE");
		break;
	case GST_MESSAGE_ANY:
		_mmcam_dbg_log("GST_MESSAGE_ANY");
		break;
	default:
		_mmcam_dbg_log("not handled message type=(%d)", GST_MESSAGE_TYPE(message));
		break;
	}

	return TRUE;
}


GstBusSyncReply _mmcamcorder_pipeline_bus_sync_callback(GstBus *bus, GstMessage *message, gpointer data)
{
	GstElement *element = NULL;
	GError *err = NULL;
	gchar *debug_info = NULL;

	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);
	_MMCamcorderSubContext *sc = NULL;

	mmf_return_val_if_fail(hcamcorder, GST_BUS_PASS);
	mmf_return_val_if_fail(message, GST_BUS_PASS);

	sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
	mmf_return_val_if_fail(sc, GST_BUS_PASS);

	sc->error_code = MM_ERROR_NONE;

	if (GST_MESSAGE_TYPE(message) == GST_MESSAGE_ERROR) {
		/* parse error message */
		gst_message_parse_error(message, &err, &debug_info);

		if (!err) {
			_mmcam_dbg_warn("failed to parse error message");
			return GST_BUS_PASS;
		}

		if (debug_info) {
			_mmcam_dbg_err("GST ERROR : %s", debug_info);
			g_free(debug_info);
			debug_info = NULL;
		}

		/* set videosrc element to compare */
		element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);

		/* check domain[RESOURCE] and element[VIDEOSRC] */
		if (err->domain == GST_RESOURCE_ERROR &&
		    GST_ELEMENT_CAST(message->src) == element) {
			switch (err->code) {
			case GST_RESOURCE_ERROR_BUSY:
				_mmcam_dbg_err("Camera device [busy]");
				sc->error_code = MM_ERROR_CAMCORDER_DEVICE_BUSY;
				break;
			case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
			case GST_RESOURCE_ERROR_OPEN_WRITE:
				_mmcam_dbg_err("Camera device [open failed]");
				sc->error_code = MM_ERROR_CAMCORDER_DEVICE_OPEN;
				break;
			case GST_RESOURCE_ERROR_OPEN_READ:
				_mmcam_dbg_err("Camera device [register trouble]");
				sc->error_code = MM_ERROR_CAMCORDER_DEVICE_REG_TROUBLE;
				break;
			case GST_RESOURCE_ERROR_NOT_FOUND:
				_mmcam_dbg_err("Camera device [device not found]");
				sc->error_code = MM_ERROR_CAMCORDER_DEVICE_NOT_FOUND;
				break;
			case GST_RESOURCE_ERROR_TOO_LAZY:
				_mmcam_dbg_err("Camera device [timeout]");
				sc->error_code = MM_ERROR_CAMCORDER_DEVICE_TIMEOUT;
				break;
			case GST_RESOURCE_ERROR_SETTINGS:
				_mmcam_dbg_err("Camera device [not supported]");
				sc->error_code = MM_ERROR_CAMCORDER_NOT_SUPPORTED;
				break;
			case GST_RESOURCE_ERROR_FAILED:
				_mmcam_dbg_err("Camera device [working failed].");
				sc->error_code = MM_ERROR_CAMCORDER_DEVICE_IO;
				break;
			default:
				_mmcam_dbg_err("Camera device [General(%d)]", err->code);
				sc->error_code = MM_ERROR_CAMCORDER_DEVICE;
				break;
			}

			sc->error_occurs = TRUE;
		}

		g_error_free(err);

		/* store error code and drop this message if cmd is running */
		if (sc->error_code != MM_ERROR_NONE) {
			if (!_MMCAMCORDER_TRYLOCK_CMD(hcamcorder)) {
				_mmcam_dbg_err("cmd is running and will be returned with this error %x",
				               sc->error_code);
				return GST_BUS_DROP;
			}

			_MMCAMCORDER_UNLOCK_CMD(hcamcorder);
		}
	}

	return GST_BUS_PASS;
}


static int __mmcamcorder_asm_get_event_type(int sessionType)
{
	switch (sessionType) {
	case MM_SESSION_TYPE_SHARE:
		return ASM_EVENT_SHARE_MMCAMCORDER;
	case MM_SESSION_TYPE_EXCLUSIVE:
		return ASM_EVENT_EXCLUSIVE_MMCAMCORDER;
	case MM_SESSION_TYPE_NOTIFY:
		return ASM_EVENT_NOTIFY;
	case MM_SESSION_TYPE_CALL:
		return ASM_EVENT_CALL;
	case MM_SESSION_TYPE_VIDEOCALL:
		return ASM_EVENT_VIDEOCALL;
	case MM_SESSION_TYPE_ALARM:
		return ASM_EVENT_ALARM;
	default:
		return ASM_EVENT_SHARE_MMCAMCORDER;
	}
}

ASM_cb_result_t _mmcamcorder_asm_callback(int handle, ASM_event_sources_t event_src,
                                          ASM_sound_commands_t command,
                                          unsigned int sound_status, void* cb_data)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(cb_data);
	int current_state = MM_CAMCORDER_STATE_NONE;
	ASM_cb_result_t cb_res = ASM_CB_RES_NONE;

	mmf_return_val_if_fail((MMHandleType)hcamcorder, ASM_CB_RES_NONE);

	current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
	if (current_state <= MM_CAMCORDER_STATE_NONE ||
	    current_state >= MM_CAMCORDER_STATE_NUM) {
		_mmcam_dbg_err("Abnormal state. Or null handle. (%p, %d, %d)", hcamcorder, command, current_state);
	}

	/* set value to inform a status is changed by asm */
	hcamcorder->state_change_by_system = _MMCAMCORDER_STATE_CHANGE_BY_ASM;
	/* set ASM event code for sending it to application */
	hcamcorder->asm_event_code = event_src;

	switch (command) {
	case ASM_COMMAND_STOP:
	case ASM_COMMAND_PAUSE:
		_mmcam_dbg_log("Got msg from asm to Stop or Pause(%d, %d)", command, current_state);

		__mmcamcorder_force_stop(hcamcorder);
		cb_res = ASM_CB_RES_STOP;

		_mmcam_dbg_log("Finish opeartion. Camera is released.(%d)", cb_res);
		break;
	case ASM_COMMAND_PLAY:
		_mmcam_dbg_log("Got msg from asm to Play(%d, %d)", command, current_state);

		if (current_state >= MM_CAMCORDER_STATE_PREPARE) {
			_mmcam_dbg_log("Already start previewing");
			return ASM_CB_RES_PLAYING;
		}

		__mmcamcorder_force_resume(hcamcorder);
		cb_res = ASM_CB_RES_PLAYING;

		_mmcam_dbg_log("Finish opeartion. Preview is started.(%d)", cb_res);
		break;
	case ASM_COMMAND_RESUME:
	{
		_MMCamcorderMsgItem msg;

		_mmcam_dbg_log("Got msg from asm to Resume(%d, %d)", command, current_state);

		msg.id = MM_MESSAGE_READY_TO_RESUME;
		_mmcamcroder_send_message((MMHandleType)hcamcorder, &msg);
		cb_res = ASM_CB_RES_PLAYING;

		_mmcam_dbg_log("Finish opeartion.(%d)", cb_res);
		break;
	}
	default: /* should not be reached here */
		cb_res = ASM_CB_RES_PLAYING;
		_mmcam_dbg_err("Command err.");
		break;
	}

	/* restore value */
	hcamcorder->state_change_by_system = _MMCAMCORDER_STATE_CHANGE_NORMAL;

	return cb_res;
}


int _mmcamcorder_create_pipeline(MMHandleType handle, int type)
{
	int ret = MM_ERROR_NONE;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;
	GstElement *pipeline = NULL;

	_mmcam_dbg_log("handle : %x, type : %d", handle, type);

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	switch (type) {
	case MM_CAMCORDER_MODE_IMAGE:
		__ta__("        _mmcamcorder_create_preview_pipeline",
		ret = _mmcamcorder_create_preview_pipeline(handle);
		);
		if (ret != MM_ERROR_NONE) {
			return ret;
		}

		__ta__("        _mmcamcorder_add_stillshot_pipeline",
		ret = _mmcamcorder_add_stillshot_pipeline(handle);
		);
		if (ret != MM_ERROR_NONE) {
			return ret;
		}
		break;
	case MM_CAMCORDER_MODE_AUDIO:
		__ta__("        _mmcamcorder_create_audio_pipeline",
		ret = _mmcamcorder_create_audio_pipeline(handle);
		);
		if (ret != MM_ERROR_NONE) {
			return ret;
		}
		break;
	case MM_CAMCORDER_MODE_VIDEO:
		__ta__("        _mmcamcorder_create_preview_pipeline",
		ret = _mmcamcorder_create_preview_pipeline(handle);
		);
		if (ret != MM_ERROR_NONE) {
			return ret;
		}
		break;
	default:
		return MM_ERROR_CAMCORDER_INTERNAL;
	}

	pipeline = sc->element[_MMCAMCORDER_MAIN_PIPE].gst;
	if (type != MM_CAMCORDER_MODE_AUDIO) {
		if (hcamcorder->sync_state_change) {
			ret = _mmcamcorder_gst_set_state(handle, pipeline, GST_STATE_READY);
		} else {
			ret = _mmcamcorder_gst_set_state_async(handle, pipeline, GST_STATE_READY);
			if (ret == GST_STATE_CHANGE_FAILURE) {
				return MM_ERROR_CAMCORDER_GST_STATECHANGE;
			}
		}
	}
#ifdef _MMCAMCORDER_GET_DEVICE_INFO
	if (!_mmcamcorder_get_device_info(handle)) {
		_mmcam_dbg_err("Getting device information error!!");
	}
#endif
	_mmcam_dbg_log("ret[%x]", ret);
	return ret;
}


void _mmcamcorder_destroy_pipeline(MMHandleType handle, int type)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;	
	gint i = 0;
	GstBus *bus = NULL;

	mmf_return_if_fail(hcamcorder);

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_if_fail(sc);

	_mmcam_dbg_log("");

	bus = gst_pipeline_get_bus(GST_PIPELINE(sc->element[_MMCAMCORDER_MAIN_PIPE].gst));

	/* Inside each pipeline destroy function, Set GST_STATE_NULL to Main pipeline */
	switch (type) {
	case MM_CAMCORDER_MODE_IMAGE:
		_mmcamcorder_destroy_image_pipeline(handle);
		break;
	case MM_CAMCORDER_MODE_AUDIO:
		_mmcamcorder_destroy_audio_pipeline(handle);
		break;
	case MM_CAMCORDER_MODE_VIDEO:
		_mmcamcorder_destroy_video_pipeline(handle);
		break;
	default:
		break;
	}

	_mmcam_dbg_log("Pipeline clear!!");

	/* Remove pipeline message callback */
	g_source_remove(hcamcorder->pipeline_cb_event_id);

	/* Remove remained message in bus */
	if (bus != NULL) {
		GstMessage *gst_msg = NULL;
		while ((gst_msg = gst_bus_pop(bus)) != NULL) {
			_mmcamcorder_pipeline_cb_message(bus, gst_msg, (gpointer)hcamcorder);
			gst_message_unref( gst_msg );
			gst_msg = NULL;
		}
		gst_object_unref( bus );
		bus = NULL;
	}

	/* checking unreleased element */
	for (i = 0 ; i < _MMCamcorder_PIPELINE_ELEMENT_NUM ; i++ ) {
		if (sc->element[i].gst) {
			if (GST_IS_ELEMENT(sc->element[i].gst)) {
				_mmcam_dbg_warn("Still alive element - ID[%d], name [%s], ref count[%d], status[%s]",
				                sc->element[i].id,
				                GST_OBJECT_NAME(sc->element[i].gst),
				                GST_OBJECT_REFCOUNT_VALUE(sc->element[i].gst),
				                gst_element_state_get_name (GST_STATE (sc->element[i].gst)));
				g_object_weak_unref(G_OBJECT(sc->element[i].gst), (GWeakNotify)_mmcamcorder_element_release_noti, sc);
			} else {
				_mmcam_dbg_warn("The element[%d] is still aliving, check it", sc->element[i].id);
			}

			sc->element[i].id = _MMCAMCORDER_NONE;
			sc->element[i].gst = NULL;
		}
	}
}


int _mmcamcorder_gst_set_state_async(MMHandleType handle, GstElement *pipeline, GstState target_state)
{
	GstStateChangeReturn setChangeReturn = GST_STATE_CHANGE_FAILURE;

	_MMCAMCORDER_LOCK_GST_STATE(handle);
	setChangeReturn = gst_element_set_state(pipeline, target_state);
	_MMCAMCORDER_UNLOCK_GST_STATE(handle);

	return setChangeReturn;
}


static gboolean __mmcamcorder_set_attr_to_camsensor_cb(gpointer data)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(data);

	mmf_return_val_if_fail(hcamcorder, FALSE);

	_mmcam_dbg_log("");

	_mmcamcorder_set_attribute_to_camsensor((MMHandleType)hcamcorder);

	/* initialize */
	hcamcorder->setting_event_id = 0;

	_mmcam_dbg_log("Done");

	/* once */
	return FALSE;
}


int _mmcamcorder_gst_set_state (MMHandleType handle, GstElement *pipeline, GstState target_state)
{
	unsigned int k = 0;
	GstState pipeline_state = GST_STATE_VOID_PENDING;
	GstStateChangeReturn setChangeReturn = GST_STATE_CHANGE_FAILURE;
	GstStateChangeReturn getChangeReturn = GST_STATE_CHANGE_FAILURE;
	GstClockTime get_timeout = __MMCAMCORDER_SET_GST_STATE_TIMEOUT * GST_SECOND;

	mmf_return_val_if_fail(handle, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("Set state to %d", target_state);

	_MMCAMCORDER_LOCK_GST_STATE(handle);

	for (k = 0; k < _MMCAMCORDER_STATE_SET_COUNT; k++) {
		setChangeReturn = gst_element_set_state(pipeline, target_state);
		_mmcam_dbg_log("gst_element_set_state[%d] return %d",
		               target_state, setChangeReturn);
		if (setChangeReturn != GST_STATE_CHANGE_FAILURE) {
			getChangeReturn = gst_element_get_state(pipeline, &pipeline_state, NULL, get_timeout);
			switch (getChangeReturn) {
			case GST_STATE_CHANGE_NO_PREROLL:
				_mmcam_dbg_log("status=GST_STATE_CHANGE_NO_PREROLL.");
			case GST_STATE_CHANGE_SUCCESS:
				/* if we reached the final target state, exit */
				if (pipeline_state == target_state) {
					_MMCAMCORDER_UNLOCK_GST_STATE(handle);
					return MM_ERROR_NONE;
				}
				break;
			case GST_STATE_CHANGE_ASYNC:
				_mmcam_dbg_log("status=GST_STATE_CHANGE_ASYNC.");
				break;
			default :
				_MMCAMCORDER_UNLOCK_GST_STATE(handle);
				_mmcam_dbg_log("status=GST_STATE_CHANGE_FAILURE.");
				return MM_ERROR_CAMCORDER_GST_STATECHANGE;
			}

			_MMCAMCORDER_UNLOCK_GST_STATE(handle);
			_mmcam_dbg_err("timeout of gst_element_get_state()!!");
			return MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
		}
		usleep(_MMCAMCORDER_STATE_CHECK_INTERVAL);
	}

	_MMCAMCORDER_UNLOCK_GST_STATE(handle);

	_mmcam_dbg_err("Failure. gst_element_set_state timeout!!");

	return MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;	
}


/* For performance check */
int _mmcamcorder_video_current_framerate(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;

	mmf_return_val_if_fail(hcamcorder, -1);

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, -1);
	
	return sc->kpi.current_fps;
}


int _mmcamcorder_video_average_framerate(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;

	mmf_return_val_if_fail(hcamcorder, -1);

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, -1);

	return sc->kpi.average_fps;
}


void _mmcamcorder_video_current_framerate_init(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;

	mmf_return_if_fail(hcamcorder);

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_if_fail(sc);

	memset(&(sc->kpi), 0x00, sizeof(_MMCamcorderKPIMeasure));

	return;
}

/* Async state change */
void _mmcamcorder_delete_command_info(__MMCamcorderCmdInfo *cmdinfo)
{
	if (cmdinfo) {
		g_free(cmdinfo);
	}
}

static void __mmcamcorder_force_stop(mmf_camcorder_t *hcamcorder)
{
	int i = 0;
	int loop = 0;
	int itr_cnt = 0;
	int result = MM_ERROR_NONE;
	int current_state = MM_CAMCORDER_STATE_NONE;

	mmf_return_if_fail(hcamcorder);

	current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);

	_mmcam_dbg_log( "Force STOP MMFW Camcorder" );

	for (loop = 0 ; current_state > MM_CAMCORDER_STATE_NULL && loop < __MMCAMCORDER_CMD_ITERATE_MAX * 3 ; loop++) {
		itr_cnt = __MMCAMCORDER_CMD_ITERATE_MAX;
		switch (current_state) {
		case MM_CAMCORDER_STATE_CAPTURING:
		{
			_MMCamcorderSubContext *sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
			_MMCamcorderImageInfo *info = NULL;

			mmf_return_if_fail(sc);
			mmf_return_if_fail((info = sc->info));

			_mmcam_dbg_log("Stop capturing.");

			/* if caturing isn't finished, waiting for 2 sec to finish real capture operation. check 'info->capturing'. */
			mm_camcorder_set_attributes((MMHandleType)hcamcorder, NULL,
			                            MMCAM_CAPTURE_BREAK_CONTINUOUS_SHOT, TRUE,
			                            NULL);

			for (i = 0; i < 20 && info->capturing; i++) {
				usleep(100000);
			}

			if (i == 20) {
				_mmcam_dbg_err("Timeout. Can't check stop capturing.");
			}

			while ((itr_cnt--) && ((result = _mmcamcorder_capture_stop((MMHandleType)hcamcorder)) != MM_ERROR_NONE)) {
					_mmcam_dbg_warn("Can't stop capturing.(%x)", result);
			}

			break;
		}
		case MM_CAMCORDER_STATE_RECORDING:
		case MM_CAMCORDER_STATE_PAUSED:
		{
			_mmcam_dbg_log("Stop recording.");

			while ((itr_cnt--) && ((result = _mmcamcorder_commit((MMHandleType)hcamcorder)) != MM_ERROR_NONE)) {
				_mmcam_dbg_warn("Can't commit.(%x)", result);
			}
			break;
		}
		case MM_CAMCORDER_STATE_PREPARE:
		{
			_mmcam_dbg_log("Stop preview.");

			while ((itr_cnt--) && ((result = _mmcamcorder_stop((MMHandleType)hcamcorder)) != MM_ERROR_NONE)) {
				_mmcam_dbg_warn("Can't stop preview.(%x)", result);
			}
			break;
		}
		case MM_CAMCORDER_STATE_READY:
		{
			_mmcam_dbg_log("unrealize");

			if ((result = _mmcamcorder_unrealize((MMHandleType)hcamcorder)) != MM_ERROR_NONE) {
				_mmcam_dbg_warn("Can't unrealize.(%x)", result);
			}
			break;
		}
		case MM_CAMCORDER_STATE_NULL:
		default:
			_mmcam_dbg_log("Already stopped.");
			break;
		}

		current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
	}

	_mmcam_dbg_log( "Done." );

	return;
}


static void __mmcamcorder_force_resume(mmf_camcorder_t *hcamcorder)
{
	int loop = 0;
	int result = MM_ERROR_NONE;
	int itr_cnt = 0;
	int current_state = MM_CAMCORDER_STATE_NONE;

	mmf_return_if_fail(hcamcorder);

	_mmcam_dbg_log("Force RESUME MMFW Camcorder");

	current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);

	for (loop = 0 ; current_state < MM_CAMCORDER_STATE_PREPARE && loop < __MMCAMCORDER_CMD_ITERATE_MAX * 3 ; loop++) {
		itr_cnt = __MMCAMCORDER_CMD_ITERATE_MAX;

		switch (current_state) {
		case MM_CAMCORDER_STATE_NULL:
			_mmcam_dbg_log("Realize");
			while ((itr_cnt--) && ((result = _mmcamcorder_realize((MMHandleType)hcamcorder)) != MM_ERROR_NONE)) {
				_mmcam_dbg_warn("Can't realize.(%x)", result);
			}
			break;
		case MM_CAMCORDER_STATE_READY:
			_mmcam_dbg_log("Start previewing");

			while ((itr_cnt--) && ((result = _mmcamcorder_start((MMHandleType)hcamcorder)) != MM_ERROR_NONE)) {
				_mmcam_dbg_warn("Can't start previewing.(%x)", result);
			}
			break;
		default:
			_mmcam_dbg_log("Abnormal state.");
			break;
		}

		current_state = _mmcamcorder_get_state((MMHandleType)hcamcorder);
	}

	_mmcam_dbg_log( "Done." );

	return;
}

int _mmcamcorder_create_command_loop(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderCommand *cmd;

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("");

	cmd = (_MMCamcorderCommand *)&(hcamcorder->cmd);
	cmd->cmd_queue = g_queue_new();
	mmf_return_val_if_fail(cmd->cmd_queue, MM_ERROR_CAMCORDER_INVALID_CONDITION);

	sem_init(&cmd->sema, 0, 0);

	if (pthread_create(&cmd->pCommandThread, NULL, _mmcamcorder_command_loop_thread, hcamcorder)) {
		perror("Make Command Thread Fail");
		return MM_ERROR_COMMON_UNKNOWN;
	}

	return MM_ERROR_NONE;
}


int _mmcamcorder_destroy_command_loop(MMHandleType handle)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderCommand *cmd;

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);
	_mmcam_dbg_log("");

	cmd = (_MMCamcorderCommand *)&(hcamcorder->cmd);
	mmf_return_val_if_fail(cmd->cmd_queue, MM_ERROR_CAMCORDER_INVALID_CONDITION);

	_mmcamcorder_append_simple_command(handle, _MMCAMCORDER_CMD_QUIT);

	sem_post(&cmd->sema); /* why is this needed? */

	_mmcam_dbg_log("wait for pthread join");

	pthread_join(cmd->pCommandThread, NULL);

	_mmcam_dbg_log("pthread join!!");
	
	sem_destroy(&cmd->sema);

	while (!g_queue_is_empty(cmd->cmd_queue)) {
		__MMCamcorderCmdInfo *info = NULL;
		info = g_queue_pop_head(cmd->cmd_queue);
		_mmcamcorder_delete_command_info(info);
	}
	g_queue_free(cmd->cmd_queue);

	_mmcam_dbg_log("Command loop clear.");	

	return MM_ERROR_NONE;
}


int _mmcamcorder_append_command(MMHandleType handle, __MMCamcorderCmdInfo *info)
{
	int value = 0;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderCommand *cmd;

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("");

	cmd = (_MMCamcorderCommand *)&(hcamcorder->cmd);
	mmf_return_val_if_fail(cmd->cmd_queue, MM_ERROR_CAMCORDER_INVALID_CONDITION);

	g_queue_push_tail (cmd->cmd_queue, (gpointer)info);

	sem_getvalue(&cmd->sema, &value);

	if (value == 0) {
		sem_post(&cmd->sema);
	} else {
		/* Don't need to post. */
	}

	return MM_ERROR_NONE;
}


int _mmcamcorder_append_simple_command(MMHandleType handle, _MMCamcorderCommandType type)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	__MMCamcorderCmdInfo *info = NULL;

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("Command Type=%d", type);

	info = (__MMCamcorderCmdInfo*)malloc(sizeof(__MMCamcorderCmdInfo));

	info->handle = handle;
	info->type = type;

	_mmcamcorder_append_command(handle, info);	

	return MM_ERROR_NONE;
}


void *_mmcamcorder_command_loop_thread(void *arg)
{
	gboolean bExit_loop = FALSE;
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(arg);
	_MMCamcorderCommand *cmd = NULL;
	__MMCamcorderCmdInfo *cmdinfo = NULL;

	mmf_return_val_if_fail(hcamcorder, NULL);

	cmd = (_MMCamcorderCommand *)&(hcamcorder->cmd);
	mmf_return_val_if_fail(cmd->cmd_queue, NULL);

	_mmcam_dbg_log("");

	while (!bExit_loop) {
		sem_wait(&cmd->sema);

		/* send command */
		while (!g_queue_is_empty (cmd->cmd_queue)) {
			int bRet = MM_ERROR_NONE;

			cmdinfo = g_queue_pop_head(cmd->cmd_queue);
			if (cmdinfo->handle == (MMHandleType)NULL) {
				_mmcam_dbg_log("Handle in cmdinfo is Null.");
				bRet = MM_ERROR_CAMCORDER_NOT_INITIALIZED;
			} else {
				switch (cmdinfo->type) {
				case _MMCAMCORDER_CMD_CREATE:
				case _MMCAMCORDER_CMD_DESTROY:
				case _MMCAMCORDER_CMD_CAPTURESTART:
				case _MMCAMCORDER_CMD_CAPTURESTOP:
				case _MMCAMCORDER_CMD_RECORD:
				case _MMCAMCORDER_CMD_PAUSE:
				case _MMCAMCORDER_CMD_COMMIT:
					__ta__("_mmcamcorder_commit",
					bRet = _mmcamcorder_commit(cmdinfo->handle);
					);
					break;
				case _MMCAMCORDER_CMD_CANCEL:
					//Not used yet.
					break;
				case _MMCAMCORDER_CMD_REALIZE:
					__ta__("_mmcamcorder_realize",
					bRet = _mmcamcorder_realize(cmdinfo->handle);
					);
					break;
				case _MMCAMCORDER_CMD_UNREALIZE:
					__ta__("_mmcamcorder_unrealize",
					bRet = _mmcamcorder_unrealize(cmdinfo->handle);
					);
					break;
				case _MMCAMCORDER_CMD_START:
					__ta__("_mmcamcorder_start",
					bRet = _mmcamcorder_start(cmdinfo->handle);
					);
					break;
				case _MMCAMCORDER_CMD_STOP:
					__ta__("_mmcamcorder_stop",
					bRet = _mmcamcorder_stop(cmdinfo->handle);
					);
					break;
				case _MMCAMCORDER_CMD_QUIT:
					_mmcam_dbg_log("Exit command loop!!");
					bExit_loop = TRUE;
					break;
				default:
					_mmcam_dbg_log("Wrong command type!!!");
					break;
				}
			}

			if (bRet != MM_ERROR_NONE) {
				_mmcam_dbg_log("Error on command process!(%x)", bRet);
				/* Do something? */
			}

			_mmcamcorder_delete_command_info(cmdinfo);

			if (bExit_loop) {
				break;
			}
			usleep(1);
		}
	}

	return NULL;
}


static gboolean __mmcamcorder_handle_gst_error(MMHandleType handle, GstMessage *message, GError *error)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderMsgItem msg;
	gchar *msg_src_element;
	_MMCamcorderSubContext *sc = NULL;
	
	return_val_if_fail(hcamcorder, FALSE);
	return_val_if_fail(error, FALSE);
	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, FALSE);

	_mmcam_dbg_log("");

	/* filtering filesink related errors */
	if (hcamcorder->state == MM_CAMCORDER_STATE_RECORDING &&
	    (error->code ==  GST_RESOURCE_ERROR_WRITE || error->code ==  GST_RESOURCE_ERROR_SEEK)) {
		if (sc->ferror_count == 2 && sc->ferror_send == FALSE) {
			sc->ferror_send = TRUE;
			msg.param.code = __mmcamcorder_gst_handle_resource_error(handle, error->code, message);
		} else {
			sc->ferror_count++;
			_mmcam_dbg_warn("Skip error");
			return TRUE;
		}
	}

	if (error->domain == GST_CORE_ERROR) {
		msg.param.code = __mmcamcorder_gst_handle_core_error(handle, error->code, message);
	} else if (error->domain == GST_LIBRARY_ERROR) {
		msg.param.code = __mmcamcorder_gst_handle_library_error(handle, error->code, message);
	} else if (error->domain == GST_RESOURCE_ERROR) {
		msg.param.code = __mmcamcorder_gst_handle_resource_error(handle, error->code, message);
	} else if (error->domain == GST_STREAM_ERROR) {
		msg.param.code = __mmcamcorder_gst_handle_stream_error(handle, error->code, message);
	} else {
		debug_warning("This error domain is not defined.\n");

		/* we treat system error as an internal error */
		msg.param.code = MM_ERROR_CAMCORDER_INTERNAL;
	}

	if (message->src) {
		msg_src_element = GST_ELEMENT_NAME(GST_ELEMENT_CAST(message->src));
		debug_error("-Msg src : [%s]	Domain : [%s]   Error : [%s]  Code : [%d] is tranlated to error code : [0x%x]\n",
		            msg_src_element, g_quark_to_string (error->domain), error->message, error->code, msg.param.code);
	} else {
		debug_error("Domain : [%s]   Error : [%s]  Code : [%d] is tranlated to error code : [0x%x]\n",
		            g_quark_to_string (error->domain), error->message, error->code, msg.param.code);
	}

#ifdef _MMCAMCORDER_SKIP_GST_FLOW_ERROR
	/* Check whether send this error to application */
	if (msg.param.code == MM_ERROR_CAMCORDER_GST_FLOW_ERROR) {
		_mmcam_dbg_log("We got the error. But skip it.");
		return TRUE;
	}
#endif /* _MMCAMCORDER_SKIP_GST_FLOW_ERROR */

	/* post error to application */
	sc->error_occurs = TRUE;
	msg.id = MM_MESSAGE_CAMCORDER_ERROR;
	_mmcamcroder_send_message(handle, &msg);

	return TRUE;
}


static gint __mmcamcorder_gst_handle_core_error(MMHandleType handle, int code, GstMessage *message)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;
	GstElement *element = NULL;

	_mmcam_dbg_log("");

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	/* Specific plugin - video encoder plugin */
	element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_ENCSINK_VENC].gst);

	if (GST_ELEMENT_CAST(message->src) == element) {
		if (code == GST_CORE_ERROR_NEGOTIATION) {
			return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
		} else {
			return MM_ERROR_CAMCORDER_ENCODER;
		}
	}

	/* General */
	switch (code)
	{
		case GST_CORE_ERROR_STATE_CHANGE:
			return MM_ERROR_CAMCORDER_GST_STATECHANGE;
		case GST_CORE_ERROR_NEGOTIATION:
			return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
		case GST_CORE_ERROR_MISSING_PLUGIN:
		case GST_CORE_ERROR_SEEK:
		case GST_CORE_ERROR_NOT_IMPLEMENTED:
		case GST_CORE_ERROR_FAILED:
		case GST_CORE_ERROR_TOO_LAZY:	
		case GST_CORE_ERROR_PAD:
		case GST_CORE_ERROR_THREAD:
		case GST_CORE_ERROR_EVENT:
		case GST_CORE_ERROR_CAPS:
		case GST_CORE_ERROR_TAG:
		case GST_CORE_ERROR_CLOCK:
		case GST_CORE_ERROR_DISABLED:
		default:
			return MM_ERROR_CAMCORDER_GST_CORE;
		break;
	}
}

static gint __mmcamcorder_gst_handle_library_error(MMHandleType handle, int code, GstMessage *message)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("");

	/* Specific plugin - NONE */

	/* General */
	switch (code) {
	case GST_LIBRARY_ERROR_FAILED:
	case GST_LIBRARY_ERROR_TOO_LAZY:
	case GST_LIBRARY_ERROR_INIT:
	case GST_LIBRARY_ERROR_SHUTDOWN:
	case GST_LIBRARY_ERROR_SETTINGS:
	case GST_LIBRARY_ERROR_ENCODE:
	default:
		_mmcam_dbg_err("Library error(%d)", code);
		return MM_ERROR_CAMCORDER_GST_LIBRARY;
	}
}


static gint __mmcamcorder_gst_handle_resource_error(MMHandleType handle, int code, GstMessage *message)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;
	GstElement *element = NULL;

	mmf_return_val_if_fail(hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("");

	/* Specific plugin */
	/* video sink */
	element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSINK_SINK].gst);
	if (GST_ELEMENT_CAST(message->src) == element) {
		if (code == GST_RESOURCE_ERROR_WRITE) {
			_mmcam_dbg_err("Display device [Off]");
			return MM_ERROR_CAMCORDER_DISPLAY_DEVICE_OFF;
		} else {
			_mmcam_dbg_err("Display device [General(%d)]", code);
		}
	}

	/* encodebin */
	element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_ENCSINK_VENC].gst);
	if (GST_ELEMENT_CAST(message->src) == element) {
		if (code == GST_RESOURCE_ERROR_FAILED) {
			_mmcam_dbg_err("Encoder [Resource error]");
			return MM_ERROR_CAMCORDER_ENCODER_BUFFER;
		} else {
			_mmcam_dbg_err("Encoder [General(%d)]", code);
			return MM_ERROR_CAMCORDER_ENCODER;
		}
	}

	/* General */
	switch (code) {
	case GST_RESOURCE_ERROR_WRITE:
		_mmcam_dbg_err("File write error");
		return MM_ERROR_FILE_WRITE;
	case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
		_mmcam_dbg_err("No left space");
		return MM_MESSAGE_CAMCORDER_NO_FREE_SPACE;
	case GST_RESOURCE_ERROR_OPEN_WRITE:
		_mmcam_dbg_err("Out of storage");
		return MM_ERROR_OUT_OF_STORAGE;
	case GST_RESOURCE_ERROR_SEEK:
		_mmcam_dbg_err("File read(seek)");
		return MM_ERROR_FILE_READ;
	case GST_RESOURCE_ERROR_NOT_FOUND:
	case GST_RESOURCE_ERROR_FAILED:
	case GST_RESOURCE_ERROR_TOO_LAZY:
	case GST_RESOURCE_ERROR_BUSY:
	case GST_RESOURCE_ERROR_OPEN_READ:
	case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
	case GST_RESOURCE_ERROR_CLOSE:
	case GST_RESOURCE_ERROR_READ:
	case GST_RESOURCE_ERROR_SYNC:
	case GST_RESOURCE_ERROR_SETTINGS:
	default:
		_mmcam_dbg_err("Resource error(%d)", code);
		return MM_ERROR_CAMCORDER_GST_RESOURCE;
	}
}


static gint __mmcamcorder_gst_handle_stream_error(MMHandleType handle, int code, GstMessage *message)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;
	GstElement *element =NULL;

	mmf_return_val_if_fail( hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED );

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("");

	/* Specific plugin */
	/* video encoder */
	element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_ENCSINK_VENC].gst);
	if (GST_ELEMENT_CAST(message->src) == element) {
		switch (code) {
		case GST_STREAM_ERROR_WRONG_TYPE:
			_mmcam_dbg_err("Video encoder [wrong stream type]");
			return MM_ERROR_CAMCORDER_ENCODER_WRONG_TYPE;
		case GST_STREAM_ERROR_ENCODE:
			_mmcam_dbg_err("Video encoder [encode error]");
			return MM_ERROR_CAMCORDER_ENCODER_WORKING;
		case GST_STREAM_ERROR_FAILED:
			_mmcam_dbg_err("Video encoder [stream failed]");
			return MM_ERROR_CAMCORDER_ENCODER_WORKING;
		default:
			_mmcam_dbg_err("Video encoder [General(%d)]", code);
			return MM_ERROR_CAMCORDER_ENCODER;
		}
	}

	/* General plugin */
	switch (code) {
	case GST_STREAM_ERROR_FORMAT:
		_mmcam_dbg_err("General [negotiation error(%d)]", code);
		return MM_ERROR_CAMCORDER_GST_NEGOTIATION;
	case GST_STREAM_ERROR_FAILED:
		_mmcam_dbg_err("General [flow error(%d)]", code);
		return MM_ERROR_CAMCORDER_GST_FLOW_ERROR;
	case GST_STREAM_ERROR_TYPE_NOT_FOUND:
	case GST_STREAM_ERROR_DECODE:
	case GST_STREAM_ERROR_CODEC_NOT_FOUND:
	case GST_STREAM_ERROR_NOT_IMPLEMENTED:
	case GST_STREAM_ERROR_TOO_LAZY:
	case GST_STREAM_ERROR_ENCODE:
	case GST_STREAM_ERROR_DEMUX:
	case GST_STREAM_ERROR_MUX:
	case GST_STREAM_ERROR_DECRYPT:
	case GST_STREAM_ERROR_DECRYPT_NOKEY:
	case GST_STREAM_ERROR_WRONG_TYPE:
	default:
		_mmcam_dbg_err("General [error(%d)]", code);
		return MM_ERROR_CAMCORDER_GST_STREAM;
	}
}


static gboolean __mmcamcorder_handle_gst_warning (MMHandleType handle, GstMessage *message, GError *error)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	gchar *debug = NULL;
	GError *err = NULL;

	return_val_if_fail(hcamcorder, FALSE);
	return_val_if_fail(error, FALSE);

	_mmcam_dbg_log("");

	gst_message_parse_warning(message, &err, &debug);

	if (error->domain == GST_CORE_ERROR) {
		_mmcam_dbg_warn("GST warning: GST_CORE domain");
	} else if (error->domain == GST_LIBRARY_ERROR) {
		_mmcam_dbg_warn("GST warning: GST_LIBRARY domain");
	} else if (error->domain == GST_RESOURCE_ERROR) {
		_mmcam_dbg_warn("GST warning: GST_RESOURCE domain");
		__mmcamcorder_gst_handle_resource_warning(handle, message, error);
	} else if (error->domain == GST_STREAM_ERROR ) {
		_mmcam_dbg_warn("GST warning: GST_STREAM domain");
	} else {
		_mmcam_dbg_warn("This error domain(%d) is not defined.", error->domain);
	}

	if (err != NULL) {
		g_error_free(err);
		err = NULL;
	}
	if (debug != NULL) {
		_mmcam_dbg_err ("Debug: %s", debug);
		g_free(debug);
		debug = NULL;
	}

	return TRUE;
}


static gint __mmcamcorder_gst_handle_resource_warning(MMHandleType handle, GstMessage *message , GError *error)
{
	mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
	_MMCamcorderSubContext *sc = NULL;
	GstElement *element =NULL;
	gchar *msg_src_element;

	mmf_return_val_if_fail( hcamcorder, MM_ERROR_CAMCORDER_NOT_INITIALIZED );

	sc = MMF_CAMCORDER_SUBCONTEXT(handle);
	mmf_return_val_if_fail(sc, MM_ERROR_CAMCORDER_NOT_INITIALIZED);

	_mmcam_dbg_log("");

	/* Special message handling */
	/* video source */
	element = GST_ELEMENT_CAST(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst);
	if (GST_ELEMENT_CAST(message->src) == element) {
		if (error->code == GST_RESOURCE_ERROR_FAILED) {
			msg_src_element = GST_ELEMENT_NAME(GST_ELEMENT_CAST(message->src));
			_mmcam_dbg_warn("-Msg src:[%s] Domain:[%s] Error:[%s]",
			               msg_src_element, g_quark_to_string(error->domain), error->message);
			return MM_ERROR_NONE;
		}
	}

	/* General plugin */
	switch (error->code) {
	case GST_RESOURCE_ERROR_WRITE:
	case GST_RESOURCE_ERROR_NO_SPACE_LEFT:
	case GST_RESOURCE_ERROR_SEEK:
	case GST_RESOURCE_ERROR_NOT_FOUND:
	case GST_RESOURCE_ERROR_FAILED:
	case GST_RESOURCE_ERROR_TOO_LAZY:
	case GST_RESOURCE_ERROR_BUSY:
	case GST_RESOURCE_ERROR_OPEN_READ:
	case GST_RESOURCE_ERROR_OPEN_WRITE:
	case GST_RESOURCE_ERROR_OPEN_READ_WRITE:
	case GST_RESOURCE_ERROR_CLOSE:
	case GST_RESOURCE_ERROR_READ:
	case GST_RESOURCE_ERROR_SYNC:
	case GST_RESOURCE_ERROR_SETTINGS:
	default:
		_mmcam_dbg_warn("General GST warning(%d)", error->code);
		break;
	}

	return MM_ERROR_NONE;
}