[libmm-camcorder] g_free and g_printf function fixes
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_util.c
1 /*
2  * libmm-camcorder
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jeongmo Yang <jm80.yang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 /*=======================================================================================
23 |  INCLUDE FILES                                                                        |
24 =======================================================================================*/
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <sys/vfs.h> /* struct statfs */
29 #include <sys/time.h> /* gettimeofday */
30 #include <sys/stat.h>
31 #include <gst/video/video-info.h>
32 #include <gio/gio.h>
33 #include <errno.h>
34
35 #include "mm_camcorder_internal.h"
36 #include "mm_camcorder_util.h"
37 #include "mm_camcorder_sound.h"
38 #include <mm_util_imgp.h>
39 #include <mm_util_jpeg.h>
40
41 #include <storage.h>
42
43 /*-----------------------------------------------------------------------
44 |    GLOBAL VARIABLE DEFINITIONS for internal                           |
45 -----------------------------------------------------------------------*/
46
47 /*-----------------------------------------------------------------------
48 |    LOCAL VARIABLE DEFINITIONS for internal                            |
49 -----------------------------------------------------------------------*/
50 #define TIME_STRING_MAX_LEN                     64
51 #define __MMCAMCORDER_CAPTURE_WAIT_TIMEOUT      5
52
53 #define FPUTC_CHECK(x_char, x_file)\
54 {\
55         if (fputc(x_char, x_file) == EOF) \
56         {\
57                 _mmcam_dbg_err("[Critical] fputc() returns fail.\n");   \
58                 return FALSE;\
59         }\
60 }
61 #define FPUTS_CHECK(x_str, x_file)\
62 {\
63         if (fputs(x_str, x_file) == EOF) \
64         {\
65                 _mmcam_dbg_err("[Critical] fputs() returns fail.\n");\
66                 SAFE_G_FREE(str);       \
67                 return FALSE;\
68         }\
69 }
70
71 /*---------------------------------------------------------------------------
72 |    LOCAL FUNCTION PROTOTYPES:                                                                                         |
73 ---------------------------------------------------------------------------*/
74 /* STATIC INTERNAL FUNCTION */
75
76 //static gint           skip_mdat(FILE *f);
77 static guint16           get_language_code(const char *str);
78 static gchar*            str_to_utf8(const gchar *str);
79 static inline gboolean   write_tag(FILE *f, const gchar *tag);
80 static inline gboolean   write_to_32(FILE *f, guint val);
81 static inline gboolean   write_to_16(FILE *f, guint val);
82 static inline gboolean   write_to_24(FILE *f, guint val);
83 #ifdef _USE_YUV_TO_RGB888_
84 static gboolean _mmcamcorder_convert_YUV_to_RGB888(unsigned char *src, int src_fmt, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
85 #endif /* _USE_YUV_TO_RGB888_ */
86 static gboolean _mmcamcorder_convert_YUYV_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
87 static gboolean _mmcamcorder_convert_UYVY_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
88 static gboolean _mmcamcorder_convert_NV12_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len);
89
90
91 /*===========================================================================================
92 |                                                                                                                                                                                       |
93 |  FUNCTION DEFINITIONS                                                                                                                                         |
94 |                                                                                                                                                                                       |
95 ========================================================================================== */
96 /*---------------------------------------------------------------------------
97 |    GLOBAL FUNCTION DEFINITIONS:                                                                                       |
98 ---------------------------------------------------------------------------*/
99
100 static int __gdbus_method_call_sync(GDBusConnection *conn, const char *bus_name,
101         const char *object, const char *iface, const char *method,
102         GVariant *args, GVariant **result, bool is_sync)
103 {
104         int ret = MM_ERROR_NONE;
105         GVariant *dbus_reply = NULL;
106
107         if (!conn || !object || !iface || !method) {
108                 _mmcam_dbg_err("Invalid Argument %p %p %p %p",
109                         conn, object, iface, method);
110                 return MM_ERROR_INVALID_ARGUMENT;
111         }
112
113         _mmcam_dbg_log("Dbus call - obj [%s], iface [%s], method [%s]", object, iface, method);
114
115         if (is_sync) {
116                 dbus_reply = g_dbus_connection_call_sync(conn,
117                         bus_name, object, iface, method, args, NULL,
118                         G_DBUS_CALL_FLAGS_NONE, G_DBUS_REPLY_TIMEOUT, NULL, NULL);
119                 if (dbus_reply) {
120                         _mmcam_dbg_log("Method Call '%s.%s' Success", iface, method);
121                         *result = dbus_reply;
122                 } else {
123                         _mmcam_dbg_err("dbus method call sync reply failed");
124                         ret = MM_ERROR_CAMCORDER_INTERNAL;
125                 }
126         } else {
127                 g_dbus_connection_call(conn, bus_name, object, iface, method, args, NULL,
128                         G_DBUS_CALL_FLAGS_NONE, G_DBUS_REPLY_TIMEOUT, NULL, NULL, NULL);
129         }
130
131         return ret;
132 }
133
134 static int __gdbus_subscribe_signal(GDBusConnection *conn,
135         const char *object_name, const char *iface_name, const char *signal_name,
136         GDBusSignalCallback signal_cb, guint *subscribe_id, void *userdata)
137 {
138         guint subs_id = 0;
139
140         if (!conn || !object_name || !iface_name || !signal_name || !signal_cb || !subscribe_id) {
141                 _mmcam_dbg_err("Invalid Argument %p %p %p %p %p %p",
142                         conn, object_name, iface_name, signal_name, signal_cb, subscribe_id);
143                 return MM_ERROR_INVALID_ARGUMENT;
144         }
145
146         _mmcam_dbg_log("subscirbe signal Obj %s, iface_name %s, sig_name %s",
147                 object_name, iface_name, signal_name);
148
149         subs_id = g_dbus_connection_signal_subscribe(conn,
150                 NULL, iface_name, signal_name, object_name, NULL,
151                 G_DBUS_SIGNAL_FLAGS_NONE, signal_cb, userdata, NULL);
152         if (!subs_id) {
153                 _mmcam_dbg_err("g_dbus_connection_signal_subscribe() failed");
154                 return MM_ERROR_CAMCORDER_INTERNAL;
155         } else {
156                 *subscribe_id = subs_id;
157                 _mmcam_dbg_log("subs_id %u", subs_id);
158         }
159
160         return MM_ERROR_NONE;
161 }
162
163
164 static void __gdbus_stream_eos_cb(GDBusConnection *connection,
165         const gchar *sender_name, const gchar *object_path, const gchar *interface_name,
166         const gchar *signal_name, GVariant *param, gpointer user_data)
167 {
168         int played_idx = 0;
169         _MMCamcorderGDbusCbInfo *gdbus_info = NULL;
170
171         _mmcam_dbg_log("entered");
172
173         if (!param || !user_data) {
174                 _mmcam_dbg_err("invalid parameter %p %p", param, user_data);
175                 return;
176         }
177
178         gdbus_info = (_MMCamcorderGDbusCbInfo *)user_data;
179
180         g_variant_get(param, "(i)", &played_idx);
181
182         g_mutex_lock(&gdbus_info->sync_mutex);
183
184         _mmcam_dbg_log("gdbus_info->param %d, played_idx : %d",
185                 gdbus_info->param, played_idx);
186
187         if (gdbus_info->param == played_idx) {
188                 g_dbus_connection_signal_unsubscribe(connection, gdbus_info->subscribe_id);
189
190                 gdbus_info->is_playing = FALSE;
191                 gdbus_info->subscribe_id = 0;
192                 gdbus_info->param = 0;
193
194                 g_cond_signal(&gdbus_info->sync_cond);
195         }
196
197         g_mutex_unlock(&gdbus_info->sync_mutex);
198
199         return;
200 }
201
202 static int __gdbus_wait_for_cb_return(_MMCamcorderGDbusCbInfo *gdbus_info, int time_out)
203 {
204         int ret = MM_ERROR_NONE;
205         gint64 end_time = 0;
206
207         if (!gdbus_info) {
208                 _mmcam_dbg_err("invalid info");
209                 return MM_ERROR_CAMCORDER_INVALID_ARGUMENT;
210         }
211
212         g_mutex_lock(&gdbus_info->sync_mutex);
213
214         _mmcam_dbg_log("entered");
215
216         if (gdbus_info->is_playing == FALSE) {
217                 _mmcam_dbg_log("callback is already returned");
218                 g_mutex_unlock(&gdbus_info->sync_mutex);
219                 return MM_ERROR_NONE;
220         }
221
222         end_time = g_get_monotonic_time()+ (time_out * G_TIME_SPAN_MILLISECOND);
223
224         if (g_cond_wait_until(&gdbus_info->sync_cond, &gdbus_info->sync_mutex, end_time)) {
225                 _mmcam_dbg_log("wait signal received");
226         } else {
227                 _mmcam_dbg_err("wait time is expired");
228                 ret = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
229         }
230
231         g_mutex_unlock(&gdbus_info->sync_mutex);
232
233         return ret;
234 }
235
236
237 gint32 _mmcamcorder_double_to_fix(gdouble d_number)
238 {
239         return (gint32) (d_number * 65536.0);
240 }
241
242 // find top level tag only, do not use this function for finding sub level tags
243 gint _mmcamcorder_find_tag(FILE *f, guint32 tag_fourcc, gboolean do_rewind)
244 {
245         size_t read_item = 0;
246         guchar buf[8];
247
248         if (do_rewind) {
249                 rewind(f);
250         }
251
252         while ((read_item = fread(&buf, sizeof(guchar), 8, f)) > 0) {
253                 uint64_t buf_size = 0;
254                 uint32_t buf_fourcc = 0;
255
256                 if (read_item < 8) {
257                         _mmcam_dbg_err("fread failed : %d", read_item);
258                         break;
259                 }
260
261                 buf_fourcc = MMCAM_FOURCC(buf[4], buf[5], buf[6], buf[7]);
262
263                 if (tag_fourcc == buf_fourcc) {
264                         _mmcam_dbg_log("find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
265                         return TRUE;
266                 } else {
267                         _mmcam_dbg_log("skip [%c%c%c%c] tag", MMCAM_FOURCC_ARGS(buf_fourcc));
268
269                         buf_size = _mmcamcorder_get_container_size(buf);
270
271                         /* if size of mdat is 1, it means largesize is used.(bigger than 4GB) */
272                         if (buf_fourcc == MMCAM_FOURCC('m','d','a','t') &&
273                             buf_size == 1) {
274                                 read_item = fread(&buf, sizeof(guchar), 8, f);
275                                 if (read_item < 8) {
276                                         _mmcam_dbg_err("fread failed");
277                                         return FALSE;
278                                 }
279
280                                 buf_size = _mmcamcorder_get_container_size64(buf);
281                                 buf_size = buf_size - 16; /* include tag and large file flag(size 1) */
282                         } else {
283                                 buf_size = buf_size - 8; /* include tag */
284                         }
285
286                         _mmcam_dbg_log("seek %llu", buf_size);
287                         if (fseeko(f, (off_t)buf_size, SEEK_CUR) != 0) {
288                                 _mmcam_dbg_err("fseeko() fail");
289                                 return FALSE;
290                         }
291                 }
292         }
293
294         _mmcam_dbg_log("cannot find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
295
296         return FALSE;
297 }
298
299 gboolean _mmcamcorder_find_fourcc(FILE *f, guint32 tag_fourcc, gboolean do_rewind)
300 {
301         size_t read_item = 0;
302         guchar buf[8];
303
304         if (do_rewind) {
305                 rewind(f);
306         }
307
308         while ((read_item = fread(&buf, sizeof(guchar), 8, f))  > 0) {
309                 uint64_t buf_size = 0;
310                 uint32_t buf_fourcc = 0;
311
312                 if (read_item < 8) {
313                         _mmcam_dbg_err("fread failed : %d", read_item);
314                         break;
315                 }
316
317                 buf_fourcc = MMCAM_FOURCC(buf[4], buf[5], buf[6], buf[7]);
318
319                 if (tag_fourcc == buf_fourcc) {
320                         _mmcam_dbg_log("find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
321                         return TRUE;
322                 } else if (buf_fourcc == MMCAM_FOURCC('m','o','o','v') &&
323                            tag_fourcc != buf_fourcc) {
324                         if (_mmcamcorder_find_fourcc(f, tag_fourcc, FALSE)) {
325                                 return TRUE;
326                         } else {
327                                 continue;
328                         }
329                 } else {
330                         _mmcam_dbg_log("skip [%c%c%c%c] tag", MMCAM_FOURCC_ARGS(buf_fourcc));
331
332                         buf_size = _mmcamcorder_get_container_size(buf);
333
334                         /* if size of mdat is 1, it means largesize is used.(bigger than 4GB) */
335                         if (buf_fourcc == MMCAM_FOURCC('m','d','a','t') &&
336                             buf_size == 1) {
337                                 read_item = fread(&buf, sizeof(guchar), 8, f);
338                                 if (read_item < 8) {
339                                         _mmcam_dbg_err("fread failed");
340                                         return FALSE;
341                                 }
342
343                                 buf_size = _mmcamcorder_get_container_size64(buf);
344                                 buf_size = buf_size - 16; /* include tag and large file flag(size 1) */
345                         } else {
346                                 buf_size = buf_size - 8; /* include tag */
347                         }
348
349                         _mmcam_dbg_log("seek %llu", buf_size);
350                         if (fseeko(f, (off_t)buf_size, SEEK_CUR) != 0) {
351                                 _mmcam_dbg_err("fseeko() fail");
352                                 return FALSE;
353                         }
354                 }
355         }
356
357         _mmcam_dbg_log("cannot find tag : %c%c%c%c", MMCAM_FOURCC_ARGS(tag_fourcc));
358
359         return FALSE;
360 }
361
362 gboolean _mmcamcorder_update_size(FILE *f, gint64 prev_pos, gint64 curr_pos)
363 {
364         _mmcam_dbg_log("size : %"G_GINT64_FORMAT"", curr_pos-prev_pos);
365         if(fseeko(f, prev_pos, SEEK_SET) != 0)
366         {
367                 _mmcam_dbg_err("fseeko() fail");
368                 return FALSE;
369         }
370
371         if (!write_to_32(f, curr_pos -prev_pos))
372                 return FALSE;
373
374         if(fseeko(f, curr_pos, SEEK_SET) != 0)
375         {
376                 _mmcam_dbg_err("fseeko() fail");
377                 return FALSE;
378         }
379
380         return TRUE;
381 }
382
383 gboolean _mmcamcorder_write_loci(FILE *f, _MMCamcorderLocationInfo info)
384 {
385         gint64 current_pos, pos;
386         gchar *str = NULL;
387
388         _mmcam_dbg_log("");
389
390         if((pos = ftello(f))<0)
391         {
392                 _mmcam_dbg_err("ftello() returns negative value");
393                 return FALSE;
394         }
395
396         if(!write_to_32(f, 0)) //size
397                 return FALSE;
398
399         if(!write_tag(f, "loci"))       // type
400                 return FALSE;
401
402         FPUTC_CHECK(0, f);              // version
403
404         if(!write_to_24(f, 0))  // flags
405                 return FALSE;
406
407         if(!write_to_16(f, get_language_code("eng"))) // language
408                 return FALSE;
409
410         str = str_to_utf8("location_name");
411
412         FPUTS_CHECK(str, f); // name
413         SAFE_G_FREE(str);
414
415         FPUTC_CHECK('\0', f);
416         FPUTC_CHECK(0, f);              //role
417
418         if(!write_to_32(f, info.longitude))     // Longitude
419                 return FALSE;
420
421         if(!write_to_32(f, info.latitude)) // Latitude
422                 return FALSE;
423
424         if(! write_to_32(f, info.altitude))     // Altitude
425                 return FALSE;
426
427         str = str_to_utf8("Astronomical_body");
428         FPUTS_CHECK(str, f);//Astronomical_body
429         SAFE_G_FREE(str);
430
431         FPUTC_CHECK('\0', f);
432
433         str = str_to_utf8("Additional_notes");
434         FPUTS_CHECK(str, f); // Additional_notes
435         SAFE_G_FREE(str);
436
437         FPUTC_CHECK('\0', f);
438
439         if((current_pos = ftello(f))<0)
440         {
441                 _mmcam_dbg_err("ftello() returns negative value");
442                 return FALSE;
443         }
444
445         if(! _mmcamcorder_update_size(f, pos, current_pos))
446                 return FALSE;
447
448         return TRUE;
449 }
450
451 void _mmcamcorder_write_Latitude(FILE *f,int value) {
452         char s_latitude[9];
453         int l_decimal = 0;
454         int l_below_decimal = 0;
455
456         l_decimal = value / 10000;
457         if (value < 0) {
458                 if( l_decimal == 0) {
459                         snprintf(s_latitude, 5, "-%.2d.", l_decimal);
460                 } else {
461                         snprintf(s_latitude, 5, "%.2d.", l_decimal);
462                 }
463         } else {
464                 snprintf(s_latitude, 5, "+%.2d.", l_decimal);
465         }
466
467         l_below_decimal = value - (l_decimal * 10000);
468         if (l_below_decimal < 0) {
469                 l_below_decimal = -l_below_decimal;
470         }
471         snprintf(&s_latitude[4], 5, "%.4d", l_below_decimal);
472
473         write_tag(f, s_latitude);
474 }
475
476 void _mmcamcorder_write_Longitude(FILE *f,int value) {
477         char s_longitude[10];
478         int l_decimal = 0;
479         int l_below_decimal = 0;
480
481         l_decimal = value / 10000;
482         if (value < 0) {
483                 if( l_decimal == 0) {
484                         snprintf(s_longitude, 6, "-%.3d.", l_decimal);
485                 } else {
486                         snprintf(s_longitude, 6, "%.3d.", l_decimal);
487                 }
488         } else {
489                 snprintf(s_longitude, 6, "+%.3d.", l_decimal);
490         }
491
492         l_below_decimal = value - (l_decimal * 10000);
493         if (l_below_decimal < 0) {
494                 l_below_decimal = -l_below_decimal;
495         }
496         snprintf(&s_longitude[5], 5, "%.4d", l_below_decimal);
497
498         write_tag(f, s_longitude);
499 }
500
501 #define D_GEOGRAPH "\xA9xyz"
502 // 0x0012 -> latitude(8) + longitude(9) + seperator(1) = 18
503 // 0x15c7 -> encode in english
504 #define D_INFO_GEOGRAPH 0x001215c7
505
506 gboolean _mmcamcorder_write_geodata(FILE *f,_MMCamcorderLocationInfo info) {
507
508         gint64 current_pos, pos;
509
510         _mmcam_dbg_log("");
511
512         if((pos = ftello(f))<0)
513         {
514                 _mmcam_dbg_err("ftello() returns negative value");
515                 return FALSE;
516         }
517
518         if(!write_to_32(f, 0))  //size
519                 return FALSE;
520         // tag -> .xyz
521         if(!write_tag(f, D_GEOGRAPH))   // type
522                 return FALSE;
523
524         if(!write_to_32(f, D_INFO_GEOGRAPH))
525                 return FALSE;
526
527         _mmcamcorder_write_Latitude(f,info.latitude);
528         _mmcamcorder_write_Longitude(f,info.longitude);
529
530         FPUTC_CHECK(0x2F, f);
531
532         if((current_pos = ftello(f))<0)
533         {
534                 _mmcam_dbg_err("ftello() returns negative value");
535                 return FALSE;
536         }
537
538         if(! _mmcamcorder_update_size(f, pos, current_pos))
539                 return FALSE;
540
541         return TRUE;
542 }
543
544
545 gboolean _mmcamcorder_write_udta(FILE *f, int gps_enable, _MMCamcorderLocationInfo info, _MMCamcorderLocationInfo geotag)
546 {
547         gint64 current_pos, pos;
548
549         _mmcam_dbg_log("gps enable : %d", gps_enable);
550         if (gps_enable == FALSE) {
551                 _mmcam_dbg_log("no need to write udta");
552                 return TRUE;
553         }
554
555         if ((pos = ftello(f))<0) {
556                 _mmcam_dbg_err("ftello() returns negative value");
557                 return FALSE;
558         }
559
560         /* size */
561         if (!write_to_32(f, 0)) {
562                 _mmcam_dbg_err("failed to write size");
563                 return FALSE;
564         }
565
566         /* type */
567         if (!write_tag(f, "udta")) {
568                 _mmcam_dbg_err("failed to write type udta");
569                 return FALSE;
570         }
571
572         if (gps_enable) {
573                 if (!_mmcamcorder_write_loci(f, info)) {
574                         _mmcam_dbg_err("failed to write loci");
575                         return FALSE;
576                 }
577
578                 if (!_mmcamcorder_write_geodata(f,geotag)) {
579                         _mmcam_dbg_err("failed to write geodata");
580                         return FALSE;
581                 }
582         }
583
584         if ((current_pos = ftello(f))<0) {
585                 _mmcam_dbg_err("ftello() returns negative value");
586                 return FALSE;
587         }
588
589         if (!_mmcamcorder_update_size(f, pos, current_pos)) {
590                 _mmcam_dbg_err("failed to update size");
591                 return FALSE;
592         }
593
594         _mmcam_dbg_log("done");
595
596         return TRUE;
597 }
598
599
600 guint64 _mmcamcorder_get_container_size(const guchar *size)
601 {
602         guint64 result = 0;
603         guint64 temp = 0;
604
605         temp = size[0];
606         result = temp << 24;
607         temp = size[1];
608         result = result | (temp << 16);
609         temp = size[2];
610         result = result | (temp << 8);
611         result = result | size[3];
612
613         _mmcam_dbg_log("result : %llu", result);
614
615         return result;
616 }
617
618
619 guint64 _mmcamcorder_get_container_size64(const guchar *size)
620 {
621         guint64 result = 0;
622         guint64 temp = 0;
623
624         temp = size[0];
625         result = temp << 56;
626         temp = size[1];
627         result = result | (temp << 48);
628         temp = size[2];
629         result = result | (temp << 40);
630         temp = size[3];
631         result = result | (temp << 32);
632         temp = size[4];
633         result = result | (temp << 24);
634         temp = size[5];
635         result = result | (temp << 16);
636         temp = size[6];
637         result = result | (temp << 8);
638         result = result | size[7];
639
640         _mmcam_dbg_log("result : %llu", result);
641
642         return result;
643 }
644
645
646 gboolean _mmcamcorder_update_composition_matrix(FILE *f, int orientation)
647 {
648         /* for 0 degree */
649         guint32 a = 0x00010000;
650         guint32 b = 0;
651         guint32 c = 0;
652         guint32 d = 0x00010000;
653
654         switch (orientation) {
655         case MM_CAMCORDER_TAG_VIDEO_ORT_90:/* 90 degree */
656                 a = 0;
657                 b = 0x00010000;
658                 c = 0xffff0000;
659                 d = 0;
660                 break;
661         case MM_CAMCORDER_TAG_VIDEO_ORT_180:/* 180 degree */
662                 a = 0xffff0000;
663                 d = 0xffff0000;
664                 break;
665         case MM_CAMCORDER_TAG_VIDEO_ORT_270:/* 270 degree */
666                 a = 0;
667                 b = 0xffff0000;
668                 c = 0x00010000;
669                 d = 0;
670                 break;
671         case MM_CAMCORDER_TAG_VIDEO_ORT_NONE:/* 0 degree */
672         default:
673                 break;
674         }
675
676         write_to_32(f, a);
677         write_to_32(f, b);
678         write_to_32(f, 0);
679         write_to_32(f, c);
680         write_to_32(f, d);
681         write_to_32(f, 0);
682         write_to_32(f, 0);
683         write_to_32(f, 0);
684         write_to_32(f, 0x40000000);
685
686         _mmcam_dbg_log("orientation : %d, write data 0x%x 0x%x 0x%x 0x%x",
687                        orientation, a, b, c, d);
688
689         return TRUE;
690 }
691
692
693 int _mmcamcorder_get_freespace(const gchar *path, const gchar *root_directory, guint64 *free_space)
694 {
695         int ret = 0;
696         struct statvfs vfs;
697
698         int is_internal = TRUE;
699         struct stat stat_path;
700         struct stat stat_root;
701
702         if (path == NULL || free_space == NULL) {
703                 _mmcam_dbg_err("invalid parameter %p, %p", path, free_space);
704                 return -1;
705         }
706
707         if (root_directory && strlen(root_directory) > 0) {
708                 if (stat(path, &stat_path) != 0) {
709                         *free_space = 0;
710                         _mmcam_dbg_err("failed to stat for [%s][errno %d]", path, errno);
711                         return -1;
712                 }
713
714                 if (stat(root_directory, &stat_root) != 0) {
715                         *free_space = 0;
716                         _mmcam_dbg_err("failed to stat for [%s][errno %d]", root_directory, errno);
717                         return -1;
718                 }
719
720                 if (stat_path.st_dev != stat_root.st_dev) {
721                         is_internal = FALSE;
722                 }
723         } else {
724                 _mmcam_dbg_warn("root_directory is NULL, assume that it's internal storage.");
725         }
726
727         if (is_internal) {
728                 ret = storage_get_internal_memory_size(&vfs);
729         } else {
730                 ret = storage_get_external_memory_size(&vfs);
731         }
732
733         if (ret < 0) {
734                 *free_space = 0;
735                 _mmcam_dbg_err("failed to get memory size [%s]", path);
736                 return -1;
737         } else {
738                 *free_space = vfs.f_bsize * vfs.f_bavail;
739                 /*
740                 _mmcam_dbg_log("vfs.f_bsize [%lu], vfs.f_bavail [%lu]", vfs.f_bsize, vfs.f_bavail);
741                 _mmcam_dbg_log("memory size %llu [%s]", *free_space, path);
742                 */
743                 return 1;
744         }
745 }
746
747
748 int _mmcamcorder_get_file_system_type(const gchar *path, int *file_system_type)
749 {
750         struct statfs fs;
751
752         g_assert(path);
753
754         if (!g_file_test(path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
755                 _mmcam_dbg_log("File(%s) doesn't exist.", path);
756                 return -2;
757         }
758
759         if (-1 == statfs(path, &fs)) {
760                 _mmcam_dbg_log("statfs failed.(%s)", path);
761                 return -1;
762         }
763
764         *file_system_type = (int)fs.f_type;
765
766         return 0;
767 }
768
769
770 int _mmcamcorder_get_device_flash_brightness(GDBusConnection *conn, int *brightness)
771 {
772         int get_value = 0;
773         int ret = MM_ERROR_NONE;
774         GVariant *params = NULL;
775         GVariant *result = NULL;
776
777         ret = __gdbus_method_call_sync(conn, "org.tizen.system.deviced",
778                 "/Org/Tizen/System/DeviceD/Led", "org.tizen.system.deviced.Led",
779                 "GetBrightnessForCamera", params, &result, TRUE);
780         if (ret != MM_ERROR_NONE) {
781                 _mmcam_dbg_err("Dbus Call on Client Error");
782                 return ret;
783         }
784
785         if (result) {
786                 g_variant_get(result, "(i)", &get_value);
787                 *brightness = get_value;
788                 _mmcam_dbg_log("flash brightness : %d", *brightness);
789         } else {
790                 _mmcam_dbg_err("replied result is null");
791                 ret = MM_ERROR_CAMCORDER_INTERNAL;
792         }
793
794         return ret;
795 }
796
797
798 int _mmcamcorder_send_sound_play_message(GDBusConnection *conn, _MMCamcorderGDbusCbInfo *gdbus_info,
799         const char *sample_name, const char *stream_role, const char *volume_gain, int sync_play)
800 {
801         int get_value = 0;
802         int ret = MM_ERROR_NONE;
803         GVariant *params = NULL, *result = NULL;
804         guint subs_id = 0;
805
806         if (!conn || !gdbus_info) {
807                 _mmcam_dbg_err("Invalid parameter %p %p", conn, gdbus_info);
808                 return MM_ERROR_CAMCORDER_INTERNAL;
809         }
810
811         params = g_variant_new("(sss)", sample_name, stream_role, volume_gain);
812         result = g_variant_new("(i)", get_value);
813
814         ret = __gdbus_method_call_sync(conn, "org.pulseaudio.Server",
815                 "/org/pulseaudio/SoundPlayer", "org.pulseaudio.SoundPlayer",
816                 "SamplePlay", params, &result, TRUE);
817         if (ret != MM_ERROR_NONE) {
818                 _mmcam_dbg_err("Dbus Call on Client Error");
819                 return ret;
820         }
821
822         if (result) {
823                 g_variant_get(result, "(i)", &get_value);
824                 _mmcam_dbg_log("played index : %d", get_value);
825         } else {
826                 _mmcam_dbg_err("replied result is null");
827                 return MM_ERROR_CAMCORDER_INTERNAL;
828         }
829
830         g_mutex_lock(&gdbus_info->sync_mutex);
831
832         if (gdbus_info->subscribe_id > 0) {
833                 _mmcam_dbg_warn("subscribe_id[%u] is remained. remove it.", gdbus_info->subscribe_id);
834
835                 g_dbus_connection_signal_unsubscribe(conn, gdbus_info->subscribe_id);
836
837                 gdbus_info->subscribe_id = 0;
838         }
839
840         gdbus_info->is_playing = TRUE;
841         gdbus_info->param = get_value;
842
843         ret = __gdbus_subscribe_signal(conn,
844                 "/org/pulseaudio/SoundPlayer", "org.pulseaudio.SoundPlayer", "EOS",
845                 __gdbus_stream_eos_cb, &subs_id, gdbus_info);
846
847         if (ret == MM_ERROR_NONE)
848                 gdbus_info->subscribe_id = subs_id;
849
850         g_mutex_unlock(&gdbus_info->sync_mutex);
851
852         if (sync_play && ret == MM_ERROR_NONE) {
853                 ret = __gdbus_wait_for_cb_return(gdbus_info, G_DBUS_CB_TIMEOUT_MSEC);
854         }
855
856         return ret;
857 }
858
859
860 int _mmcamcorder_get_file_size(const char *filename, guint64 *size)
861 {
862         struct stat buf;
863
864         if (stat(filename, &buf) != 0)
865                 return -1;
866         *size = (guint64)buf.st_size;
867         return 1;
868 }
869
870
871 void _mmcamcorder_remove_buffer_probe(MMHandleType handle, _MMCamcorderHandlerCategory category)
872 {
873         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
874         GList *list = NULL;
875         MMCamcorderHandlerItem *item = NULL;
876
877         mmf_return_if_fail(hcamcorder);
878
879         if (!hcamcorder->buffer_probes) {
880                 _mmcam_dbg_warn("list for buffer probe is NULL");
881                 return;
882         }
883
884         _mmcam_dbg_log("start - category : 0x%x", category);
885
886         list = hcamcorder->buffer_probes;
887         while (list) {
888                 item = list->data;
889                 if (!item) {
890                         _mmcam_dbg_err("Remove buffer probe faild, the item is NULL");
891                         list = g_list_next(list);
892                         continue;
893                 }
894
895                 if (item->category & category) {
896                         if (item->object && GST_IS_PAD(item->object)) {
897                                 _mmcam_dbg_log("Remove buffer probe on [%s:%s] - [ID : %lu], [Category : %x]",
898                                                GST_DEBUG_PAD_NAME(item->object), item->handler_id,  item->category);
899                                 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
900                         } else {
901                                 _mmcam_dbg_warn("Remove buffer probe faild, the pad is null or not pad, just remove item from list and free it");
902                         }
903
904                         list = g_list_next(list);
905                         hcamcorder->buffer_probes = g_list_remove(hcamcorder->buffer_probes, item);
906                         SAFE_G_FREE(item);
907                 } else {
908                         _mmcam_dbg_log("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
909                         list = g_list_next(list);
910                 }
911         }
912
913         if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
914                 g_list_free(hcamcorder->buffer_probes);
915                 hcamcorder->buffer_probes = NULL;
916         }
917
918         _mmcam_dbg_log("done");
919
920         return;
921 }
922
923
924 void _mmcamcorder_remove_one_buffer_probe(MMHandleType handle, void *object)
925 {
926         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
927         GList *list = NULL;
928         MMCamcorderHandlerItem *item = NULL;
929
930         mmf_return_if_fail(hcamcorder);
931
932         if (!hcamcorder->buffer_probes) {
933                 _mmcam_dbg_warn("list for buffer probe is NULL");
934                 return;
935         }
936
937         _mmcam_dbg_log("start - object : %p", object);
938
939         list = hcamcorder->buffer_probes;
940         while (list) {
941                 item = list->data;
942                 if (!item) {
943                         _mmcam_dbg_err("Remove buffer probe faild, the item is NULL");
944                         list = g_list_next(list);
945                         continue;
946                 }
947
948                 if (item->object && item->object == object) {
949                         if (GST_IS_PAD(item->object)) {
950                                 _mmcam_dbg_log("Remove buffer probe on [%s:%s] - [ID : %lu], [Category : %x]",
951                                                GST_DEBUG_PAD_NAME(item->object), item->handler_id,  item->category);
952                                 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
953                         } else {
954                                 _mmcam_dbg_warn("Remove buffer probe faild, the pad is null or not pad, just remove item from list and free it");
955                         }
956
957                         list = g_list_next(list);
958                         hcamcorder->buffer_probes = g_list_remove(hcamcorder->buffer_probes, item);
959                         SAFE_G_FREE(item);
960
961                         break;
962                 } else {
963                         _mmcam_dbg_log("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
964                         list = g_list_next(list);
965                 }
966         }
967
968         _mmcam_dbg_log("done");
969
970         return;
971 }
972
973
974 void _mmcamcorder_remove_event_probe(MMHandleType handle, _MMCamcorderHandlerCategory category)
975 {
976         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
977         GList *list = NULL;
978         MMCamcorderHandlerItem *item = NULL;
979
980         mmf_return_if_fail(hcamcorder);
981
982         if (!hcamcorder->event_probes) {
983                 _mmcam_dbg_warn("list for event probe is NULL");
984                 return;
985         }
986
987         _mmcam_dbg_log("start - category : 0x%x", category);
988
989         list = hcamcorder->event_probes;
990         while (list) {
991                 item = list->data;
992                 if (!item) {
993                         _mmcam_dbg_err("Remove event probe faild, the item is NULL");
994                         list = g_list_next(list);
995                         continue;
996                 }
997
998                 if (item->category & category) {
999                         if (item->object && GST_IS_PAD(item->object)) {
1000                                 _mmcam_dbg_log("Remove event probe on [%s:%s] - [ID : %lu], [Category : %x]",
1001                                                GST_DEBUG_PAD_NAME(item->object), item->handler_id,  item->category);
1002                                 gst_pad_remove_probe(GST_PAD(item->object), item->handler_id);
1003                         } else {
1004                                 _mmcam_dbg_warn("Remove event probe faild, the pad is null or not pad, just remove item from list and free it");
1005                         }
1006
1007                         list = g_list_next(list);
1008                         hcamcorder->event_probes = g_list_remove(hcamcorder->event_probes, item);
1009                         SAFE_G_FREE(item);
1010                 } else {
1011                         _mmcam_dbg_log("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
1012                         list = g_list_next(list);
1013                 }
1014         }
1015
1016         if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
1017                 g_list_free(hcamcorder->event_probes);
1018                 hcamcorder->event_probes = NULL;
1019         }
1020
1021         _mmcam_dbg_log("done");
1022
1023         return;
1024 }
1025
1026
1027 void _mmcamcorder_disconnect_signal(MMHandleType handle, _MMCamcorderHandlerCategory category)
1028 {
1029         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1030         GList *list = NULL;
1031         MMCamcorderHandlerItem *item = NULL;
1032
1033         mmf_return_if_fail(hcamcorder);
1034
1035         if (!hcamcorder->signals) {
1036                 _mmcam_dbg_warn("list for signal is NULL");
1037                 return;
1038         }
1039
1040         _mmcam_dbg_log("start - category : 0x%x", category);
1041
1042         list = hcamcorder->signals;
1043         while (list) {
1044                 item = list->data;
1045                 if (!item) {
1046                         _mmcam_dbg_err("Fail to Disconnecting signal, the item is NULL");
1047                         list = g_list_next(list);
1048                         continue;
1049                 }
1050
1051                 if (item->category & category) {
1052                         if (item->object && GST_IS_ELEMENT(item->object)) {
1053                                 if (g_signal_handler_is_connected(item->object, item->handler_id)) {
1054                                         _mmcam_dbg_log("Disconnect signal from [%s] : [ID : %lu], [Category : %x]",
1055                                                        GST_OBJECT_NAME(item->object), item->handler_id,  item->category);
1056                                         g_signal_handler_disconnect(item->object, item->handler_id);
1057                                 } else {
1058                                         _mmcam_dbg_warn("Signal was not connected, cannot disconnect it :  [%s]  [ID : %lu], [Category : %x]",
1059                                                         GST_OBJECT_NAME(item->object), item->handler_id,  item->category);
1060                                 }
1061                         } else {
1062                                 _mmcam_dbg_err("Fail to Disconnecting signal, the element is null or not element, just remove item from list and free it");
1063                         }
1064
1065                         list = g_list_next(list);
1066                         hcamcorder->signals = g_list_remove(hcamcorder->signals, item);
1067                         SAFE_G_FREE(item);
1068                 } else {
1069                         _mmcam_dbg_log("Skip item : [ID : %lu], [Category : %x] ", item->handler_id, item->category);
1070                         list = g_list_next(list);
1071                 }
1072         }
1073
1074         if (category == _MMCAMCORDER_HANDLER_CATEGORY_ALL) {
1075                 g_list_free(hcamcorder->signals);
1076                 hcamcorder->signals = NULL;
1077         }
1078
1079         _mmcam_dbg_log("done");
1080
1081         return;
1082 }
1083
1084
1085 void _mmcamcorder_remove_all_handlers(MMHandleType handle,  _MMCamcorderHandlerCategory category)
1086 {
1087         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1088
1089         _mmcam_dbg_log("ENTER");
1090
1091         if(hcamcorder->signals)
1092                 _mmcamcorder_disconnect_signal((MMHandleType)hcamcorder, category);
1093         if(hcamcorder->event_probes)
1094                 _mmcamcorder_remove_event_probe((MMHandleType)hcamcorder, category);
1095         if(hcamcorder->buffer_probes)
1096                 _mmcamcorder_remove_buffer_probe((MMHandleType)hcamcorder, category);
1097
1098         _mmcam_dbg_log("LEAVE");
1099 }
1100
1101
1102 void _mmcamcorder_element_release_noti(gpointer data, GObject *where_the_object_was)
1103 {
1104         int i=0;
1105         _MMCamcorderSubContext *sc = (_MMCamcorderSubContext *)data;
1106
1107         mmf_return_if_fail(sc);
1108         mmf_return_if_fail(sc->element);
1109
1110         for (i = 0 ; i < _MMCAMCORDER_PIPELINE_ELEMENT_NUM ; i++) {
1111                 if (sc->element[i].gst && (G_OBJECT(sc->element[i].gst) == where_the_object_was)) {
1112                         _mmcam_dbg_warn("The element[%d][%p] is finalized",
1113                                         sc->element[i].id, sc->element[i].gst);
1114                         sc->element[i].gst = NULL;
1115                         sc->element[i].id = _MMCAMCORDER_NONE;
1116                         return;
1117                 }
1118         }
1119
1120         mmf_return_if_fail(sc->encode_element);
1121
1122         for (i = 0 ; i < _MMCAMCORDER_ENCODE_PIPELINE_ELEMENT_NUM ; i++) {
1123                 if (sc->encode_element[i].gst && (G_OBJECT(sc->encode_element[i].gst) == where_the_object_was)) {
1124                         _mmcam_dbg_warn("The encode element[%d][%p] is finalized",
1125                                         sc->encode_element[i].id, sc->encode_element[i].gst);
1126                         sc->encode_element[i].gst = NULL;
1127                         sc->encode_element[i].id = _MMCAMCORDER_ENCODE_NONE;
1128                         return;
1129                 }
1130         }
1131
1132         _mmcam_dbg_warn("there is no matching element %p", where_the_object_was);
1133
1134         return;
1135 }
1136
1137
1138 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1139 gboolean _mmcamcorder_msg_callback(void *data)
1140 {
1141         _MMCamcorderMsgItem *item = (_MMCamcorderMsgItem*)data;
1142         mmf_camcorder_t *hcamcorder = NULL;
1143         mmf_return_val_if_fail(item, FALSE);
1144
1145         g_mutex_lock(&item->lock);
1146
1147         hcamcorder = MMF_CAMCORDER(item->handle);
1148         if (hcamcorder == NULL) {
1149                 _mmcam_dbg_warn("msg id:0x%x, item:%p, handle is NULL", item->id, item);
1150                 goto MSG_CALLBACK_DONE;
1151         }
1152
1153         /*_mmcam_dbg_log("msg id:%x, msg_cb:%p, msg_data:%p, item:%p", item->id, hcamcorder->msg_cb, hcamcorder->msg_data, item);*/
1154
1155         _MMCAMCORDER_LOCK((MMHandleType)hcamcorder);
1156
1157         /* remove item from msg data */
1158         if (hcamcorder->msg_data) {
1159                 hcamcorder->msg_data = g_list_remove(hcamcorder->msg_data, item);
1160         } else {
1161                 _mmcam_dbg_warn("msg_data is NULL but item[%p] will be removed", item);
1162         }
1163
1164         _MMCAMCORDER_UNLOCK((MMHandleType)hcamcorder);
1165
1166         _MMCAMCORDER_LOCK_MESSAGE_CALLBACK(hcamcorder);
1167
1168         if ((hcamcorder) && (hcamcorder->msg_cb)) {
1169                 hcamcorder->msg_cb(item->id, (MMMessageParamType*)(&(item->param)), hcamcorder->msg_cb_param);
1170         }
1171
1172         _MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);
1173
1174         _MMCAMCORDER_SIGNAL(hcamcorder);
1175
1176 MSG_CALLBACK_DONE:
1177         /* release allocated memory */
1178         if (item->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1179                 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)item->param.data;
1180                 if (cam_fd_info) {
1181                         SAFE_G_FREE(cam_fd_info->face_info);
1182                         SAFE_G_FREE(cam_fd_info);
1183
1184                         item->param.data = NULL;
1185                         item->param.size = 0;
1186                 }
1187         } else if (item->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED ||
1188                    item->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1189                 MMCamRecordingReport *report = (MMCamRecordingReport *)item->param.data;
1190                 if (report) {
1191                         if (report->recording_filename) {
1192                                 SAFE_G_FREE(report->recording_filename);
1193                         }
1194                         SAFE_G_FREE(report);
1195                         item->param.data = NULL;
1196                 }
1197         }
1198
1199         g_mutex_unlock(&item->lock);
1200         g_mutex_clear(&item->lock);
1201
1202         SAFE_G_FREE(item);
1203
1204         /* For not being called again */
1205         return FALSE;
1206 }
1207 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1208
1209
1210 gboolean _mmcamcorder_send_message(MMHandleType handle, _MMCamcorderMsgItem *data)
1211 {
1212         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1213 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1214         _MMCamcorderMsgItem *item = NULL;
1215 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1216
1217         mmf_return_val_if_fail(hcamcorder, FALSE);
1218         mmf_return_val_if_fail(data, FALSE);
1219
1220         switch (data->id)
1221         {
1222                 case MM_MESSAGE_CAMCORDER_STATE_CHANGED:
1223                 case MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_ASM:
1224                 case MM_MESSAGE_CAMCORDER_STATE_CHANGED_BY_RM:
1225                         data->param.union_type = MM_MSG_UNION_STATE;
1226                         break;
1227                 case MM_MESSAGE_CAMCORDER_RECORDING_STATUS:
1228                         data->param.union_type = MM_MSG_UNION_RECORDING_STATUS;
1229                         break;
1230                 case MM_MESSAGE_CAMCORDER_FIRMWARE_UPDATE:
1231                         data->param.union_type = MM_MSG_UNION_FIRMWARE;
1232                         break;
1233                 case MM_MESSAGE_CAMCORDER_CURRENT_VOLUME:
1234                         data->param.union_type = MM_MSG_UNION_REC_VOLUME_DB;
1235                         break;
1236                 case MM_MESSAGE_CAMCORDER_TIME_LIMIT:
1237                 case MM_MESSAGE_CAMCORDER_MAX_SIZE:
1238                 case MM_MESSAGE_CAMCORDER_NO_FREE_SPACE:
1239                 case MM_MESSAGE_CAMCORDER_ERROR:
1240                 case MM_MESSAGE_CAMCORDER_FOCUS_CHANGED:
1241                 case MM_MESSAGE_CAMCORDER_CAPTURED:
1242                 case MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED:
1243                 case MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED:
1244                 case MM_MESSAGE_READY_TO_RESUME:
1245                 default:
1246                         data->param.union_type = MM_MSG_UNION_CODE;
1247                         break;
1248         }
1249
1250 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1251         item = g_malloc(sizeof(_MMCamcorderMsgItem));
1252         if (item) {
1253                 memcpy(item, data, sizeof(_MMCamcorderMsgItem));
1254                 item->handle = handle;
1255                 g_mutex_init(&item->lock);
1256
1257                 _MMCAMCORDER_LOCK(handle);
1258                 hcamcorder->msg_data = g_list_append(hcamcorder->msg_data, item);
1259                 /*_mmcam_dbg_log("item[%p]", item);*/
1260
1261                 /* Use DEFAULT priority */
1262                 g_idle_add_full(G_PRIORITY_DEFAULT, _mmcamcorder_msg_callback, item, NULL);
1263
1264                 _MMCAMCORDER_UNLOCK(handle);
1265         } else {
1266                 _mmcam_dbg_err("item[id:0x%x] malloc failed : %d", data->id, sizeof(_MMCamcorderMsgItem));
1267         }
1268 #else /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1269         _MMCAMCORDER_LOCK_MESSAGE_CALLBACK(hcamcorder);
1270
1271         if (hcamcorder->msg_cb) {
1272                 hcamcorder->msg_cb(data->id, (MMMessageParamType*)(&(data->param)), hcamcorder->msg_cb_param);
1273         } else {
1274                 _mmcam_dbg_log("message callback is NULL. message id %d", data->id);
1275         }
1276
1277         _MMCAMCORDER_UNLOCK_MESSAGE_CALLBACK(hcamcorder);
1278
1279         /* release allocated memory */
1280         if (data->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1281                 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)data->param.data;
1282                 if (cam_fd_info) {
1283                         SAFE_G_FREE(cam_fd_info->face_info);
1284                         SAFE_G_FREE(cam_fd_info);
1285                         data->param.size = 0;
1286                 }
1287         } else if (data->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED ||
1288                    data->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1289                 MMCamRecordingReport *report = (MMCamRecordingReport *)data->param.data;
1290                 if (report) {
1291                         SAFE_G_FREE(report->recording_filename);
1292                         data->param.data = NULL;
1293                 }
1294                 SAFE_G_FREE(report);
1295         }
1296 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1297
1298         return TRUE;
1299 }
1300
1301
1302 void _mmcamcorder_remove_message_all(MMHandleType handle)
1303 {
1304         mmf_camcorder_t* hcamcorder = MMF_CAMCORDER(handle);
1305         gboolean ret = TRUE;
1306 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1307         _MMCamcorderMsgItem *item = NULL;
1308         GList *list = NULL;
1309         gint64 end_time = 0;
1310 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1311
1312         mmf_return_if_fail(hcamcorder);
1313
1314         _MMCAMCORDER_LOCK(handle);
1315
1316 #ifdef _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK
1317         if (!hcamcorder->msg_data) {
1318                 _mmcam_dbg_log("No message data is remained.");
1319         } else {
1320                 list = hcamcorder->msg_data;
1321
1322                 while (list) {
1323                         item = list->data;
1324                         list = g_list_next(list);
1325
1326                         if (!item) {
1327                                 _mmcam_dbg_err("Fail to remove message. The item is NULL");
1328                         } else {
1329                                 if (g_mutex_trylock(&(item->lock))) {
1330                                         ret = g_idle_remove_by_data(item);
1331
1332                                         _mmcam_dbg_log("remove msg item[%p], ret[%d]", item, ret);
1333
1334                                         if (ret == FALSE) {
1335                                                 item->handle = 0;
1336                                                 _mmcam_dbg_warn("failed to remove msg cb for item %p, it will be called later with NULL handle", item);
1337                                         }
1338
1339                                         /* release allocated memory */
1340                                         if (item->id == MM_MESSAGE_CAMCORDER_FACE_DETECT_INFO) {
1341                                                 MMCamFaceDetectInfo *cam_fd_info = (MMCamFaceDetectInfo *)item->param.data;
1342                                                 if (cam_fd_info) {
1343                                                         SAFE_G_FREE(cam_fd_info->face_info);
1344                                                         item->param.size = 0;
1345                                                 }
1346                                                 SAFE_G_FREE(cam_fd_info);
1347                                         } else if (item->id == MM_MESSAGE_CAMCORDER_VIDEO_CAPTURED ||
1348                                                    item->id == MM_MESSAGE_CAMCORDER_AUDIO_CAPTURED) {
1349                                                 MMCamRecordingReport *report = (MMCamRecordingReport *)item->param.data;
1350                                                 if (report) {
1351                                                         SAFE_G_FREE(report->recording_filename);
1352                                                 }
1353                                                 SAFE_G_FREE(report);
1354                                         }
1355
1356                                         hcamcorder->msg_data = g_list_remove(hcamcorder->msg_data, item);
1357
1358                                         g_mutex_unlock(&(item->lock));
1359
1360                                         if (ret == TRUE) {
1361                                                 g_mutex_clear(&item->lock);
1362
1363                                                 SAFE_G_FREE(item);
1364
1365                                                 _mmcam_dbg_log("remove msg done");
1366                                         }
1367                                 } else {
1368                                         _mmcam_dbg_warn("item lock failed. it's being called...");
1369
1370                                         end_time = g_get_monotonic_time() + (100 * G_TIME_SPAN_MILLISECOND);
1371
1372                                         if (_MMCAMCORDER_WAIT_UNTIL(handle, end_time)) {
1373                                                 _mmcam_dbg_warn("signal received");
1374                                         } else {
1375                                                 _mmcam_dbg_warn("timeout");
1376                                         }
1377                                 }
1378                         }
1379                 }
1380
1381                 g_list_free(hcamcorder->msg_data);
1382                 hcamcorder->msg_data = NULL;
1383         }
1384 #endif /* _MMCAMCORDER_ENABLE_IDLE_MESSAGE_CALLBACK */
1385
1386         /* remove idle function for playing capture sound */
1387         do {
1388                 ret = g_idle_remove_by_data(hcamcorder);
1389                 _mmcam_dbg_log("remove idle function for playing capture sound. ret[%d]", ret);
1390         } while (ret);
1391
1392         _MMCAMCORDER_UNLOCK(handle);
1393
1394         return;
1395 }
1396
1397
1398 int _mmcamcorder_get_pixel_format(GstCaps *caps)
1399 {
1400         const GstStructure *structure;
1401         const char *media_type;
1402         GstVideoInfo media_info;
1403         MMPixelFormatType type = 0;
1404         unsigned int fourcc = 0;
1405
1406         mmf_return_val_if_fail( caps != NULL, MM_PIXEL_FORMAT_INVALID );
1407
1408         structure = gst_caps_get_structure (caps, 0);
1409         media_type = gst_structure_get_name (structure);
1410         if (media_type == NULL) {
1411                 _mmcam_dbg_err("failed to get media_type");
1412                 return MM_PIXEL_FORMAT_INVALID;
1413         }
1414
1415         gst_video_info_init (&media_info);
1416
1417         if (!strcmp (media_type, "image/jpeg") ) {
1418                 _mmcam_dbg_log("It is jpeg.");
1419                 type = MM_PIXEL_FORMAT_ENCODED;
1420         } else if (!strcmp (media_type, "video/x-raw") &&
1421                    gst_video_info_from_caps(&media_info, caps) &&
1422                    GST_VIDEO_INFO_IS_YUV(&media_info)) {
1423                 _mmcam_dbg_log("It is yuv.");
1424                 fourcc = gst_video_format_to_fourcc(GST_VIDEO_INFO_FORMAT(&media_info));
1425                 type = _mmcamcorder_get_pixtype(fourcc);
1426         } else if (!strcmp (media_type, "video/x-raw") &&
1427                    gst_video_info_from_caps(&media_info, caps) &&
1428                    GST_VIDEO_INFO_IS_RGB(&media_info)) {
1429                 _mmcam_dbg_log("It is rgb.");
1430                 type = MM_PIXEL_FORMAT_RGB888;
1431         } else if (!strcmp (media_type, "video/x-h264")) {
1432                 _mmcam_dbg_log("It is H264");
1433                 type = MM_PIXEL_FORMAT_ENCODED_H264;
1434         } else {
1435                 _mmcam_dbg_err("Not supported format [%s]",media_type);
1436                 type = MM_PIXEL_FORMAT_INVALID;
1437         }
1438
1439         /*_mmcam_dbg_log( "Type [%d]", type );*/
1440
1441         return type;
1442 }
1443
1444 unsigned int _mmcamcorder_get_fourcc(int pixtype, int codectype, int use_zero_copy_format)
1445 {
1446         unsigned int fourcc = 0;
1447
1448         /*_mmcam_dbg_log("pixtype(%d)", pixtype);*/
1449
1450         switch (pixtype) {
1451         case MM_PIXEL_FORMAT_NV12:
1452                 if (use_zero_copy_format) {
1453                         fourcc = GST_MAKE_FOURCC ('S', 'N', '1', '2');
1454                 } else {
1455                         fourcc = GST_MAKE_FOURCC ('N', 'V', '1', '2');
1456                 }
1457                 break;
1458         case MM_PIXEL_FORMAT_NV21:
1459                 if (use_zero_copy_format) {
1460                         fourcc = GST_MAKE_FOURCC ('S', 'N', '2', '1');
1461                 } else {
1462                         fourcc = GST_MAKE_FOURCC ('N', 'V', '2', '1');
1463                 }
1464                 break;
1465         case MM_PIXEL_FORMAT_YUYV:
1466                 if (use_zero_copy_format) {
1467                         fourcc = GST_MAKE_FOURCC ('S', 'U', 'Y', 'V');
1468                 } else {
1469                         fourcc = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
1470                 }
1471                 break;
1472         case MM_PIXEL_FORMAT_UYVY:
1473                 if (use_zero_copy_format) {
1474                         fourcc = GST_MAKE_FOURCC ('S', 'Y', 'V', 'Y');
1475                 } else {
1476                         fourcc = GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y');
1477                 }
1478                 break;
1479         case MM_PIXEL_FORMAT_I420:
1480                 if (use_zero_copy_format) {
1481                         fourcc = GST_MAKE_FOURCC ('S', '4', '2', '0');
1482                 } else {
1483                         fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
1484                 }
1485                 break;
1486         case MM_PIXEL_FORMAT_YV12:
1487                 fourcc = GST_MAKE_FOURCC ('Y', 'V', '1', '2');
1488                 break;
1489         case MM_PIXEL_FORMAT_422P:
1490                 fourcc = GST_MAKE_FOURCC ('4', '2', '2', 'P');
1491                 break;
1492         case MM_PIXEL_FORMAT_RGB565:
1493                 fourcc = GST_MAKE_FOURCC ('R', 'G', 'B', 'P');
1494                 break;
1495         case MM_PIXEL_FORMAT_RGB888:
1496                 fourcc = GST_MAKE_FOURCC ('R', 'G', 'B', ' ');
1497                 break;
1498         case MM_PIXEL_FORMAT_ENCODED:
1499                 if (codectype == MM_IMAGE_CODEC_JPEG) {
1500                         fourcc = GST_MAKE_FOURCC ('J', 'P', 'E', 'G');
1501                 } else if (codectype == MM_IMAGE_CODEC_JPEG_SRW) {
1502                         fourcc = GST_MAKE_FOURCC ('J', 'P', 'E', 'G'); /*TODO: JPEG+SamsungRAW format */
1503                 } else if (codectype == MM_IMAGE_CODEC_SRW) {
1504                         fourcc = GST_MAKE_FOURCC ('J', 'P', 'E', 'G'); /*TODO: SamsungRAW format */
1505                 } else if (codectype == MM_IMAGE_CODEC_PNG) {
1506                         fourcc = GST_MAKE_FOURCC ('P', 'N', 'G', ' ');
1507                 } else {
1508                         /* Please let us know what other fourcces are. ex) BMP, GIF?*/
1509                         fourcc = GST_MAKE_FOURCC ('J', 'P', 'E', 'G');
1510                 }
1511                 break;
1512         case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
1513                 fourcc = GST_MAKE_FOURCC('I','T','L','V');
1514                 break;
1515         case MM_PIXEL_FORMAT_ENCODED_H264:
1516                 fourcc = GST_MAKE_FOURCC('H','2','6','4');
1517                 break;
1518         default:
1519                 _mmcam_dbg_log("Not proper pixel type[%d]. Set default - I420", pixtype);
1520                 if (use_zero_copy_format) {
1521                         fourcc = GST_MAKE_FOURCC ('S', '4', '2', '0');
1522                 } else {
1523                         fourcc = GST_MAKE_FOURCC ('I', '4', '2', '0');
1524                 }
1525                 break;
1526         }
1527
1528         return fourcc;
1529 }
1530
1531
1532 int _mmcamcorder_get_pixtype(unsigned int fourcc)
1533 {
1534         int pixtype = MM_PIXEL_FORMAT_INVALID;
1535         /*
1536         char *pfourcc = (char*)&fourcc;
1537
1538         _mmcam_dbg_log("fourcc(%c%c%c%c)",
1539                          pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
1540         */
1541
1542         switch (fourcc) {
1543         case GST_MAKE_FOURCC ('S', 'N', '1', '2'):
1544         case GST_MAKE_FOURCC ('N', 'V', '1', '2'):
1545                 pixtype = MM_PIXEL_FORMAT_NV12;
1546                 break;
1547         case GST_MAKE_FOURCC ('S', 'N', '2', '1'):
1548         case GST_MAKE_FOURCC ('N', 'V', '2', '1'):
1549                 pixtype = MM_PIXEL_FORMAT_NV21;
1550                 break;
1551         case GST_MAKE_FOURCC ('S', 'U', 'Y', 'V'):
1552         case GST_MAKE_FOURCC ('Y', 'U', 'Y', 'V'):
1553         case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
1554                 pixtype = MM_PIXEL_FORMAT_YUYV;
1555                 break;
1556         case GST_MAKE_FOURCC ('S', 'Y', 'V', 'Y'):
1557         case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
1558                 pixtype = MM_PIXEL_FORMAT_UYVY;
1559                 break;
1560         case GST_MAKE_FOURCC ('S', '4', '2', '0'):
1561         case GST_MAKE_FOURCC ('I', '4', '2', '0'):
1562                 pixtype = MM_PIXEL_FORMAT_I420;
1563                 break;
1564         case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
1565                 pixtype = MM_PIXEL_FORMAT_YV12;
1566                 break;
1567         case GST_MAKE_FOURCC ('4', '2', '2', 'P'):
1568                 pixtype = MM_PIXEL_FORMAT_422P;
1569                 break;
1570         case GST_MAKE_FOURCC ('R', 'G', 'B', 'P'):
1571                 pixtype = MM_PIXEL_FORMAT_RGB565;
1572                 break;
1573         case GST_MAKE_FOURCC ('R', 'G', 'B', '3'):
1574                 pixtype = MM_PIXEL_FORMAT_RGB888;
1575                 break;
1576         case GST_MAKE_FOURCC ('A', 'R', 'G', 'B'):
1577         case GST_MAKE_FOURCC ('x', 'R', 'G', 'B'):
1578                 pixtype = MM_PIXEL_FORMAT_ARGB;
1579                 break;
1580         case GST_MAKE_FOURCC ('B', 'G', 'R', 'A'):
1581         case GST_MAKE_FOURCC ('B', 'G', 'R', 'x'):
1582         case GST_MAKE_FOURCC ('S', 'R', '3', '2'):
1583                 pixtype = MM_PIXEL_FORMAT_RGBA;
1584                 break;
1585         case GST_MAKE_FOURCC ('J', 'P', 'E', 'G'):
1586         case GST_MAKE_FOURCC ('P', 'N', 'G', ' '):
1587                 pixtype = MM_PIXEL_FORMAT_ENCODED;
1588                 break;
1589         /*FIXME*/
1590         case GST_MAKE_FOURCC ('I', 'T', 'L', 'V'):
1591                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
1592                 break;
1593         case GST_MAKE_FOURCC ('H', '2', '6', '4'):
1594                 pixtype = MM_PIXEL_FORMAT_ENCODED_H264;
1595                 break;
1596         default:
1597                 _mmcam_dbg_log("Not supported fourcc type(%c%c%c%c)",
1598                                fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
1599                 pixtype = MM_PIXEL_FORMAT_INVALID;
1600                 break;
1601         }
1602
1603         return pixtype;
1604 }
1605
1606
1607 gboolean _mmcamcorder_add_elements_to_bin(GstBin *bin, GList *element_list)
1608 {
1609         GList *local_list = element_list;
1610         _MMCamcorderGstElement *element = NULL;
1611
1612         mmf_return_val_if_fail(bin && local_list, FALSE);
1613
1614         while (local_list) {
1615                 element = (_MMCamcorderGstElement*)local_list->data;
1616                 if (element && element->gst) {
1617                         if (!gst_bin_add(bin, GST_ELEMENT(element->gst))) {
1618                                 _mmcam_dbg_err( "Add element [%s] to bin [%s] FAILED",
1619                                                 GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
1620                                                 GST_ELEMENT_NAME(GST_ELEMENT(bin)) );
1621                                 return FALSE;
1622                         } else {
1623                                 _mmcam_dbg_log("Add element [%s] to bin [%s] OK",
1624                                                GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
1625                                                GST_ELEMENT_NAME(GST_ELEMENT(bin)));
1626                         }
1627                 }
1628                 local_list = local_list->next;
1629         }
1630
1631         return TRUE;
1632 }
1633
1634 gboolean _mmcamcorder_link_elements(GList *element_list)
1635 {
1636         GList                  *local_list  = element_list;
1637         _MMCamcorderGstElement *element     = NULL;
1638         _MMCamcorderGstElement *pre_element = NULL;
1639
1640         mmf_return_val_if_fail(local_list, FALSE);
1641
1642         pre_element = (_MMCamcorderGstElement*)local_list->data;
1643         local_list = local_list->next;
1644
1645         while (local_list) {
1646                 element = (_MMCamcorderGstElement*)local_list->data;
1647                 if (pre_element && pre_element->gst && element && element->gst) {
1648                         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(pre_element->gst), GST_ELEMENT(element->gst))) {
1649                                 _mmcam_dbg_log("Link [%s] to [%s] OK",
1650                                                GST_ELEMENT_NAME(GST_ELEMENT(pre_element->gst)),
1651                                                GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
1652                         } else {
1653                                 _mmcam_dbg_err("Link [%s] to [%s] FAILED",
1654                                                GST_ELEMENT_NAME(GST_ELEMENT(pre_element->gst)),
1655                                                GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
1656                                 return FALSE;
1657                         }
1658                 }
1659
1660                 pre_element = element;
1661                 local_list = local_list->next;
1662         }
1663
1664         return TRUE;
1665 }
1666
1667 gboolean _mmcamcorder_resize_frame(unsigned char *src_data, unsigned int src_width, unsigned int src_height, unsigned int src_length, int src_format,
1668         unsigned char **dst_data, unsigned int *dst_width, unsigned int *dst_height, unsigned int *dst_length)
1669 {
1670         int ret = TRUE;
1671         int mm_ret = MM_ERROR_NONE;
1672         int input_format = MM_UTIL_IMG_FMT_YUV420;
1673         unsigned char *dst_tmp_data = NULL;
1674
1675         if (!src_data || !dst_data || !dst_width || !dst_height || !dst_length) {
1676                 _mmcam_dbg_err("something is NULL %p,%p,%p,%p,%p",
1677                                src_data, dst_data, dst_width, dst_height, dst_length);
1678                 return FALSE;
1679         }
1680
1681         /* set input format for mm-util */
1682         switch (src_format) {
1683         case MM_PIXEL_FORMAT_I420:
1684                 input_format = MM_UTIL_IMG_FMT_I420;
1685                 break;
1686         case MM_PIXEL_FORMAT_YV12:
1687                 input_format = MM_UTIL_IMG_FMT_YUV420;
1688                 break;
1689         case MM_PIXEL_FORMAT_NV12:
1690                 input_format = MM_UTIL_IMG_FMT_NV12;
1691                 break;
1692         case MM_PIXEL_FORMAT_YUYV:
1693                 input_format = MM_UTIL_IMG_FMT_YUYV;
1694                 break;
1695         case MM_PIXEL_FORMAT_UYVY:
1696                 input_format = MM_UTIL_IMG_FMT_UYVY;
1697                 break;
1698         case MM_PIXEL_FORMAT_RGB888:
1699                 input_format = MM_UTIL_IMG_FMT_RGB888;
1700                 break;
1701         default:
1702                 _mmcam_dbg_err("NOT supported format", src_format);
1703                 return FALSE;
1704         }
1705
1706         _mmcam_dbg_log("src size %dx%d -> dst size %dx%d",
1707                         src_width, src_height, *dst_width, *dst_height);
1708
1709         /* get length of resized image */
1710         mm_ret = mm_util_get_image_size(input_format, *dst_width, *dst_height, dst_length);
1711         if (mm_ret != MM_ERROR_NONE) {
1712                 GST_ERROR("mm_util_get_image_size failed 0x%x", ret);
1713                 return FALSE;
1714         }
1715
1716         _mmcam_dbg_log("dst_length : %d", *dst_length);
1717
1718         dst_tmp_data = (unsigned char *)malloc(*dst_length);
1719         if (dst_tmp_data == NULL) {
1720                 _mmcam_dbg_err("failed to alloc dst_thumb_size(size %d)", *dst_length);
1721                 return FALSE;
1722         }
1723
1724         mm_ret = mm_util_resize_image(src_data, src_width, src_height, input_format,
1725                                       dst_tmp_data, dst_width, dst_height);
1726         if (mm_ret != MM_ERROR_NONE) {
1727                 GST_ERROR("mm_util_resize_image failed 0x%x", ret);
1728                 free(dst_tmp_data);
1729                 return FALSE;
1730         }
1731
1732         *dst_data = dst_tmp_data;
1733
1734         _mmcam_dbg_log("resize done %p, %dx%d", *dst_data, *dst_width, *dst_height);
1735
1736         return TRUE;
1737 }
1738
1739
1740 gboolean _mmcamcorder_encode_jpeg(void *src_data, unsigned int src_width, unsigned int src_height,
1741         int src_format, unsigned int src_length, unsigned int jpeg_quality,
1742         void **result_data, unsigned int *result_length)
1743 {
1744         int ret = 0;
1745         gboolean ret_conv = TRUE;
1746         int jpeg_format = 0;
1747         unsigned char *converted_src = NULL;
1748         unsigned int converted_src_size = 0;
1749
1750         mmf_return_val_if_fail(src_data && result_data && result_length, FALSE);
1751
1752         switch (src_format) {
1753         case MM_PIXEL_FORMAT_NV12:
1754                 //jpeg_format = MM_UTIL_JPEG_FMT_NV12;
1755                 ret_conv = _mmcamcorder_convert_NV12_to_I420(src_data,src_width,src_height,&converted_src,&converted_src_size);
1756                 jpeg_format = MM_UTIL_JPEG_FMT_YUV420;
1757                 break;
1758         case MM_PIXEL_FORMAT_NV16:
1759                 jpeg_format = MM_UTIL_JPEG_FMT_NV16;
1760                 converted_src = src_data;
1761                 break;
1762         case MM_PIXEL_FORMAT_NV21:
1763                 jpeg_format = MM_UTIL_JPEG_FMT_NV21;
1764                 converted_src = src_data;
1765                 break;
1766         case MM_PIXEL_FORMAT_YUYV:
1767                 //jpeg_format = MM_UTIL_JPEG_FMT_YUYV;
1768                 ret_conv = _mmcamcorder_convert_YUYV_to_I420(src_data,src_width,src_height,&converted_src,&converted_src_size);
1769                 jpeg_format = MM_UTIL_JPEG_FMT_YUV420;
1770                 break;
1771         case MM_PIXEL_FORMAT_UYVY:
1772                 //jpeg_format = MM_UTIL_JPEG_FMT_UYVY;
1773                 ret_conv = _mmcamcorder_convert_UYVY_to_I420(src_data,src_width,src_height,&converted_src,&converted_src_size);
1774                 jpeg_format = MM_UTIL_JPEG_FMT_YUV420;
1775                 break;
1776         case MM_PIXEL_FORMAT_I420:
1777                 jpeg_format = MM_UTIL_JPEG_FMT_YUV420;
1778                 converted_src = src_data;
1779                 break;
1780         case MM_PIXEL_FORMAT_RGB888:
1781                 jpeg_format = MM_UTIL_JPEG_FMT_RGB888;
1782                 converted_src = src_data;
1783                 break;
1784         case MM_PIXEL_FORMAT_RGBA:
1785                 jpeg_format = MM_UTIL_JPEG_FMT_RGBA8888;
1786                 converted_src = src_data;
1787                 break;
1788         case MM_PIXEL_FORMAT_ARGB:
1789                 jpeg_format = MM_UTIL_JPEG_FMT_ARGB8888;
1790                 converted_src = src_data;
1791                 break;
1792         case MM_PIXEL_FORMAT_422P:
1793                 jpeg_format = MM_UTIL_JPEG_FMT_YUV422;  // not supported
1794                 return FALSE;
1795         case MM_PIXEL_FORMAT_NV12T:
1796         case MM_PIXEL_FORMAT_YV12:
1797         case MM_PIXEL_FORMAT_RGB565:
1798         case MM_PIXEL_FORMAT_ENCODED:
1799         case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
1800         default:
1801                 return FALSE;
1802         }
1803
1804         if (ret_conv == FALSE) {
1805                 if (converted_src &&
1806                     converted_src != src_data) {
1807                         free(converted_src);
1808                         converted_src = NULL;
1809                 }
1810                 _mmcam_dbg_err("color convert error source format[%d], jpeg format[%d]", src_format, jpeg_format);
1811                 return FALSE;
1812         }
1813
1814         ret = mm_util_jpeg_encode_to_memory(result_data, (int *)result_length,
1815                                             converted_src, src_width, src_height,
1816                                             jpeg_format, jpeg_quality);
1817
1818         if (converted_src && (converted_src != src_data)) {
1819                 free(converted_src);
1820                 converted_src = NULL;
1821         }
1822
1823         if (ret != MM_ERROR_NONE) {
1824                 _mmcam_dbg_err("No encoder supports %d format, error code %x", src_format, ret);
1825                 return FALSE;
1826         }
1827
1828         return TRUE;
1829 }
1830
1831
1832 /* make UYVY smaller as multiple size. ex: 640x480 -> 320x240 or 160x120 ... */
1833 gboolean _mmcamcorder_downscale_UYVYorYUYV(unsigned char *src, unsigned int src_width, unsigned int src_height,
1834         unsigned char **dst, unsigned int dst_width, unsigned int dst_height)
1835 {
1836         unsigned int i = 0;
1837         int j = 0;
1838         int k = 0;
1839         int src_index = 0;
1840         int ratio_width = 0;
1841         int ratio_height = 0;
1842         int line_base = 0;
1843         int line_width = 0;
1844         int jump_width = 0;
1845         unsigned char *result = NULL;
1846
1847         if (src == NULL || dst == NULL) {
1848                 _mmcam_dbg_err("src[%p] or dst[%p] is NULL", src, dst);
1849                 return FALSE;
1850         }
1851
1852         result = (unsigned char *)malloc((dst_width * dst_height)<<1);
1853         if (!result) {
1854                 _mmcam_dbg_err("failed to alloc dst data");
1855                 return FALSE;
1856         }
1857
1858         ratio_width = src_width / dst_width;
1859         ratio_height = src_height / dst_height;
1860         line_width = src_width << 1;
1861         jump_width = ratio_width << 1;
1862
1863         _mmcam_dbg_warn("[src %dx%d] [dst %dx%d] [line width %d] [ratio width %d, height %d]",
1864                         src_width, src_height, dst_width, dst_height,
1865                         line_width, ratio_width, ratio_height);
1866
1867         for (i = 0 ; i < src_height ; i += ratio_height) {
1868                 line_base = i * line_width;
1869                 for (j = 0 ; j < line_width ; j += jump_width) {
1870                         src_index = line_base + j;
1871                         result[k++] = src[src_index];
1872                         result[k++] = src[src_index+1];
1873
1874                         j += jump_width;
1875                         src_index = line_base + j;
1876                         if (src_index % 4 == 0) {
1877                                 result[k++] = src[src_index+2];
1878                         } else {
1879                                 result[k++] = src[src_index];
1880                         }
1881                         result[k++] = src[src_index+1];
1882                 }
1883         }
1884
1885         *dst = result;
1886
1887         _mmcam_dbg_warn("converting done - result %p", result);
1888
1889         return TRUE;
1890 }
1891
1892
1893 static guint16 get_language_code(const char *str)
1894 {
1895     return (guint16) (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1896 }
1897
1898 static gchar * str_to_utf8(const gchar *str)
1899 {
1900         return g_convert (str, -1, "UTF-8", "ASCII", NULL, NULL, NULL);
1901 }
1902
1903 static inline gboolean write_tag(FILE *f, const gchar *tag)
1904 {
1905         while(*tag)
1906                 FPUTC_CHECK(*tag++, f);
1907
1908         return TRUE;
1909 }
1910
1911 static inline gboolean write_to_32(FILE *f, guint val)
1912 {
1913         FPUTC_CHECK(val >> 24, f);
1914         FPUTC_CHECK(val >> 16, f);
1915         FPUTC_CHECK(val >> 8, f);
1916         FPUTC_CHECK(val, f);
1917         return TRUE;
1918 }
1919
1920 static inline gboolean write_to_16(FILE *f, guint val)
1921 {
1922         FPUTC_CHECK(val >> 8, f);
1923         FPUTC_CHECK(val, f);
1924         return TRUE;
1925 }
1926
1927 static inline gboolean write_to_24(FILE *f, guint val)
1928 {
1929         write_to_16(f, val >> 8);
1930         FPUTC_CHECK(val, f);
1931         return TRUE;
1932 }
1933
1934 void *_mmcamcorder_util_task_thread_func(void *data)
1935 {
1936         int ret = MM_ERROR_NONE;
1937         mmf_camcorder_t *hcamcorder = (mmf_camcorder_t *)data;
1938
1939         if (!hcamcorder) {
1940                 _mmcam_dbg_err("handle is NULL");
1941                 return NULL;
1942         }
1943
1944         _mmcam_dbg_warn("start thread");
1945
1946         g_mutex_lock(&hcamcorder->task_thread_lock);
1947
1948         while (hcamcorder->task_thread_state != _MMCAMCORDER_TASK_THREAD_STATE_EXIT) {
1949                 switch (hcamcorder->task_thread_state) {
1950                 case _MMCAMCORDER_TASK_THREAD_STATE_NONE:
1951                         _mmcam_dbg_warn("wait for task signal");
1952                         g_cond_wait(&hcamcorder->task_thread_cond, &hcamcorder->task_thread_lock);
1953                         _mmcam_dbg_warn("task signal received : state %d", hcamcorder->task_thread_state);
1954                         break;
1955                 case _MMCAMCORDER_TASK_THREAD_STATE_SOUND_PLAY_START:
1956                         _mmcamcorder_sound_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE02, FALSE);
1957                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
1958                         break;
1959                 case _MMCAMCORDER_TASK_THREAD_STATE_SOUND_SOLO_PLAY_START:
1960                         _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE01, FALSE);
1961                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
1962                         break;
1963                 case _MMCAMCORDER_TASK_THREAD_STATE_ENCODE_PIPE_CREATE:
1964                         ret = _mmcamcorder_video_prepare_record((MMHandleType)hcamcorder);
1965
1966                         /* Play record start sound */
1967                         _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_REC_START, FALSE);
1968
1969                         _mmcam_dbg_log("_mmcamcorder_video_prepare_record return 0x%x", ret);
1970                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
1971                         break;
1972                 case _MMCAMCORDER_TASK_THREAD_STATE_CHECK_CAPTURE_IN_RECORDING:
1973                         {
1974                                 gint64 end_time = 0;
1975
1976                                 _mmcam_dbg_warn("wait for capture data in recording. wait signal...");
1977
1978                                 end_time = g_get_monotonic_time() + (5 * G_TIME_SPAN_SECOND);
1979
1980                                 if (g_cond_wait_until(&hcamcorder->task_thread_cond, &hcamcorder->task_thread_lock, end_time)) {
1981                                         _mmcam_dbg_warn("signal received");
1982                                 } else {
1983                                         _MMCamcorderMsgItem message;
1984
1985                                         memset(&message, 0x0, sizeof(_MMCamcorderMsgItem));
1986
1987                                         _mmcam_dbg_err("capture data wait time out, send error message");
1988
1989                                         message.id = MM_MESSAGE_CAMCORDER_ERROR;
1990                                         message.param.code = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
1991
1992                                         _mmcamcorder_send_message((MMHandleType)hcamcorder, &message);
1993
1994                                         hcamcorder->capture_in_recording = FALSE;
1995                                 }
1996
1997                                 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
1998                         }
1999                         break;
2000                 default:
2001                         _mmcam_dbg_warn("invalid task thread state %d", hcamcorder->task_thread_state);
2002                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_EXIT;
2003                         break;
2004                 }
2005         }
2006
2007         g_mutex_unlock(&hcamcorder->task_thread_lock);
2008
2009         _mmcam_dbg_warn("exit thread");
2010
2011         return NULL;
2012 }
2013
2014 #ifdef _USE_YUV_TO_RGB888_
2015 static gboolean
2016 _mmcamcorder_convert_YUV_to_RGB888(unsigned char *src, int src_fmt, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2017 {
2018         int ret = 0;
2019         int src_cs = MM_UTIL_IMG_FMT_UYVY;
2020         int dst_cs = MM_UTIL_IMG_FMT_RGB888;
2021         unsigned int dst_size = 0;
2022
2023         if (src_fmt == COLOR_FORMAT_YUYV)
2024         {
2025                 _mmcam_dbg_log("Convert YUYV to RGB888\n");
2026                 src_cs = MM_UTIL_IMG_FMT_YUYV;
2027         }
2028         else if (src_fmt == COLOR_FORMAT_UYVY)
2029         {
2030                 _mmcam_dbg_log("Convert UYVY to RGB888\n");
2031                 src_cs = MM_UTIL_IMG_FMT_UYVY;
2032         }
2033         else if (src_fmt == COLOR_FORMAT_NV12)
2034         {
2035                 _mmcam_dbg_log("Convert NV12 to RGB888\n");
2036                 src_cs = MM_UTIL_IMG_FMT_NV12;
2037         }
2038         else
2039         {
2040                 _mmcam_dbg_err("NOT supported format [%d]\n", src_fmt);
2041                 return FALSE;
2042         }
2043
2044         ret = mm_util_get_image_size(dst_cs, width, height, &dst_size);
2045         if (ret != 0) {
2046                 _mmcam_dbg_err("mm_util_get_image_size failed [%x]\n", ret);
2047                 return FALSE;
2048         }
2049
2050         *dst = malloc(dst_size);
2051         if (*dst == NULL)
2052         {
2053                 _mmcam_dbg_err("malloc failed\n");
2054                 return FALSE;
2055         }
2056
2057         *dst_len = dst_size;
2058         ret = mm_util_convert_colorspace(src, width, height, src_cs, *dst, dst_cs);
2059         if(ret == 0)
2060         {
2061                 _mmcam_dbg_log("Convert [dst_size:%d] OK.\n", dst_size);
2062                 return TRUE;
2063         }
2064         else
2065         {
2066                 free(*dst);
2067                 *dst = NULL;
2068
2069                 _mmcam_dbg_err("Convert [size:%d] FAILED.\n", dst_size);
2070                 return FALSE;
2071         }
2072 }
2073 #endif /* _USE_YUV_TO_RGB888_ */
2074
2075
2076 static gboolean _mmcamcorder_convert_YUYV_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2077 {
2078         unsigned int i = 0;
2079         int j = 0;
2080         int src_offset = 0;
2081         int dst_y_offset = 0;
2082         int dst_u_offset = 0;
2083         int dst_v_offset = 0;
2084         int loop_length = 0;
2085         unsigned int dst_size = 0;
2086         unsigned char *dst_data = NULL;
2087
2088         if (!src || !dst || !dst_len) {
2089                 _mmcam_dbg_err("NULL pointer %p, %p, %p", src, dst, dst_len);
2090                 return FALSE;
2091         }
2092
2093         dst_size = (width * height * 3) >> 1;
2094
2095         _mmcam_dbg_log("YUVY -> I420 : %dx%d, dst size %d", width, height, dst_size);
2096
2097         dst_data = (unsigned char *)malloc(dst_size);
2098         if (!dst_data) {
2099                 _mmcam_dbg_err("failed to alloc dst_data. size %d", dst_size);
2100                 return FALSE;
2101         }
2102
2103         loop_length = width << 1;
2104         dst_u_offset = width * height;
2105         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2106
2107         _mmcam_dbg_log("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2108
2109         for (i = 0 ; i < height ; i++) {
2110                 for (j = 0 ; j < loop_length ; j += 2) {
2111                         dst_data[dst_y_offset++] = src[src_offset++]; /*Y*/
2112
2113                         if (i % 2 == 0) {
2114                                 if (j % 4 == 0) {
2115                                         dst_data[dst_u_offset++] = src[src_offset++]; /*U*/
2116                                 } else {
2117                                         dst_data[dst_v_offset++] = src[src_offset++]; /*V*/
2118                                 }
2119                         } else {
2120                                 src_offset++;
2121                         }
2122                 }
2123         }
2124
2125         *dst = dst_data;
2126         *dst_len = dst_size;
2127
2128         _mmcam_dbg_log("DONE: YUVY -> I420 : %dx%d, dst data %p, size %d",
2129                                 width, height, *dst, dst_size);
2130
2131         return TRUE;
2132 }
2133
2134
2135 static gboolean _mmcamcorder_convert_UYVY_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2136 {
2137         unsigned int i = 0;
2138         int j = 0;
2139         int src_offset = 0;
2140         int dst_y_offset = 0;
2141         int dst_u_offset = 0;
2142         int dst_v_offset = 0;
2143         int loop_length = 0;
2144         unsigned int dst_size = 0;
2145         unsigned char *dst_data = NULL;
2146
2147         if (!src || !dst || !dst_len) {
2148                 _mmcam_dbg_err("NULL pointer %p, %p, %p", src, dst, dst_len);
2149                 return FALSE;
2150         }
2151
2152         dst_size = (width * height * 3) >> 1;
2153
2154         _mmcam_dbg_log("UYVY -> I420 : %dx%d, dst size %d", width, height, dst_size);
2155
2156         dst_data = (unsigned char *)malloc(dst_size);
2157         if (!dst_data) {
2158                 _mmcam_dbg_err("failed to alloc dst_data. size %d", dst_size);
2159                 return FALSE;
2160         }
2161
2162         loop_length = width << 1;
2163         dst_u_offset = width * height;
2164         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2165
2166         _mmcam_dbg_log("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2167
2168         for (i = 0 ; i < height ; i++) {
2169                 for (j = 0 ; j < loop_length ; j += 2) {
2170                         if (i % 2 == 0) {
2171                                 if (j % 4 == 0) {
2172                                         dst_data[dst_u_offset++] = src[src_offset++]; /*U*/
2173                                 } else {
2174                                         dst_data[dst_v_offset++] = src[src_offset++]; /*V*/
2175                                 }
2176                         } else {
2177                                 src_offset++;
2178                         }
2179
2180                         dst_data[dst_y_offset++] = src[src_offset++]; /*Y*/
2181                 }
2182         }
2183
2184         *dst = dst_data;
2185         *dst_len = dst_size;
2186
2187         _mmcam_dbg_log("DONE: UYVY -> I420 : %dx%d, dst data %p, size %d",
2188                                 width, height, *dst, dst_size);
2189
2190         return TRUE;
2191 }
2192
2193
2194 static gboolean _mmcamcorder_convert_NV12_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2195 {
2196         int i = 0;
2197         int src_offset = 0;
2198         int dst_y_offset = 0;
2199         int dst_u_offset = 0;
2200         int dst_v_offset = 0;
2201         int loop_length = 0;
2202         unsigned int dst_size = 0;
2203         unsigned char *dst_data = NULL;
2204
2205         if (!src || !dst || !dst_len) {
2206                 _mmcam_dbg_err("NULL pointer %p, %p, %p", src, dst, dst_len);
2207                 return FALSE;
2208         }
2209
2210         dst_size = (width * height * 3) >> 1;
2211
2212         _mmcam_dbg_log("NV12 -> I420 : %dx%d, dst size %d", width, height, dst_size);
2213
2214         dst_data = (unsigned char *)malloc(dst_size);
2215         if (!dst_data) {
2216                 _mmcam_dbg_err("failed to alloc dst_data. size %d", dst_size);
2217                 return FALSE;
2218         }
2219
2220         loop_length = width << 1;
2221         dst_u_offset = width * height;
2222         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2223
2224         _mmcam_dbg_log("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2225
2226         /* memcpy Y */
2227         memcpy(dst_data, src, dst_u_offset);
2228
2229         loop_length = dst_u_offset >> 1;
2230         src_offset = dst_u_offset;
2231
2232         /* set U and V */
2233         for (i = 0 ; i < loop_length ; i++) {
2234                 if (i % 2 == 0) {
2235                         dst_data[dst_u_offset++] = src[src_offset++];
2236                 } else {
2237                         dst_data[dst_v_offset++] = src[src_offset++];
2238                 }
2239         }
2240
2241         *dst = dst_data;
2242         *dst_len = dst_size;
2243
2244         _mmcam_dbg_log("DONE: NV12 -> I420 : %dx%d, dst data %p, size %d",
2245                                 width, height, *dst, dst_size);
2246
2247         return TRUE;
2248 }