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