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