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