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