Fix typo
[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         else if (!strcmp(media_type, "video/x-jpeg"))
1558                 type = MM_PIXEL_FORMAT_ENCODED_MJPEG;
1559         else if (!strcmp(media_type, "video/x-vp8"))
1560                 type = MM_PIXEL_FORMAT_ENCODED_VP8;
1561         else if (!strcmp(media_type, "video/x-vp9"))
1562                 type = MM_PIXEL_FORMAT_ENCODED_VP9;
1563
1564         if (type == MM_PIXEL_FORMAT_INVALID)
1565                 MMCAM_LOG_ERROR("Not supported format[%s,GST:%d]", media_type, GST_VIDEO_INFO_FORMAT(&vinfo));
1566         else
1567                 MMCAM_LOG_DEBUG("Type[%d]", type);
1568
1569         return type;
1570 }
1571
1572
1573 unsigned int _mmcamcorder_get_fourcc(int pixtype, int codectype, int use_zero_copy_format)
1574 {
1575         unsigned int fourcc = 0;
1576
1577         /*MMCAM_LOG_INFO("pixtype(%d)", pixtype);*/
1578
1579         switch (pixtype) {
1580         case MM_PIXEL_FORMAT_NV12:
1581                 if (use_zero_copy_format)
1582                         fourcc = GST_MAKE_FOURCC('S', 'N', '1', '2');
1583                 else
1584                         fourcc = GST_MAKE_FOURCC('N', 'V', '1', '2');
1585
1586                 break;
1587         case MM_PIXEL_FORMAT_NV21:
1588                 if (use_zero_copy_format)
1589                         fourcc = GST_MAKE_FOURCC('S', 'N', '2', '1');
1590                 else
1591                         fourcc = GST_MAKE_FOURCC('N', 'V', '2', '1');
1592
1593                 break;
1594         case MM_PIXEL_FORMAT_YUYV:
1595                 if (use_zero_copy_format)
1596                         fourcc = GST_MAKE_FOURCC('S', 'U', 'Y', 'V');
1597                 else
1598                         fourcc = GST_MAKE_FOURCC('Y', 'U', 'Y', '2');
1599
1600                 break;
1601         case MM_PIXEL_FORMAT_UYVY:
1602                 if (use_zero_copy_format)
1603                         fourcc = GST_MAKE_FOURCC('S', 'Y', 'V', 'Y');
1604                 else
1605                         fourcc = GST_MAKE_FOURCC('U', 'Y', 'V', 'Y');
1606
1607                 break;
1608         case MM_PIXEL_FORMAT_I420:
1609                 if (use_zero_copy_format)
1610                         fourcc = GST_MAKE_FOURCC('S', '4', '2', '0');
1611                 else
1612                         fourcc = GST_MAKE_FOURCC('I', '4', '2', '0');
1613
1614                 break;
1615         case MM_PIXEL_FORMAT_YV12:
1616                 fourcc = GST_MAKE_FOURCC('Y', 'V', '1', '2');
1617                 break;
1618         case MM_PIXEL_FORMAT_422P:
1619                 fourcc = GST_MAKE_FOURCC('4', '2', '2', 'P');
1620                 break;
1621         case MM_PIXEL_FORMAT_RGB565:
1622                 fourcc = GST_MAKE_FOURCC('R', 'G', 'B', 'P');
1623                 break;
1624         case MM_PIXEL_FORMAT_RGB888:
1625                 fourcc = GST_MAKE_FOURCC('R', 'G', 'B', ' ');
1626                 break;
1627         case MM_PIXEL_FORMAT_RGBA:
1628                 fourcc = GST_MAKE_FOURCC('B', 'G', 'R', 'x');
1629                 break;
1630         case MM_PIXEL_FORMAT_ARGB:
1631                 fourcc = GST_MAKE_FOURCC('x', 'R', 'G', 'B');
1632                 break;
1633         case MM_PIXEL_FORMAT_ENCODED:
1634                 if (codectype == MM_IMAGE_CODEC_JPEG) {
1635                         fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G');
1636                 } else if (codectype == MM_IMAGE_CODEC_JPEG_SRW) {
1637                         fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G'); /*TODO: JPEG+SamsungRAW format */
1638                 } else if (codectype == MM_IMAGE_CODEC_SRW) {
1639                         fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G'); /*TODO: SamsungRAW format */
1640                 } else if (codectype == MM_IMAGE_CODEC_PNG) {
1641                         fourcc = GST_MAKE_FOURCC('P', 'N', 'G', ' ');
1642                 } else {
1643                         /* Please let us know what other fourcces are. ex) BMP, GIF?*/
1644                         fourcc = GST_MAKE_FOURCC('J', 'P', 'E', 'G');
1645                 }
1646                 break;
1647         case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
1648                 fourcc = GST_MAKE_FOURCC('I', 'T', 'L', 'V');
1649                 break;
1650         case MM_PIXEL_FORMAT_ENCODED_H264:
1651                 fourcc = GST_MAKE_FOURCC('H', '2', '6', '4');
1652                 break;
1653         case MM_PIXEL_FORMAT_INVZ:
1654                 fourcc = GST_MAKE_FOURCC('I', 'N', 'V', 'Z');
1655                 break;
1656         case MM_PIXEL_FORMAT_ENCODED_MJPEG:
1657                 fourcc = GST_MAKE_FOURCC ('M', 'J', 'P', 'G');
1658                 break;
1659         case MM_PIXEL_FORMAT_ENCODED_VP8:
1660                 fourcc = GST_MAKE_FOURCC ('V', 'P', '8', '0');
1661                 break;
1662         case MM_PIXEL_FORMAT_ENCODED_VP9:
1663                 fourcc = GST_MAKE_FOURCC ('V', 'P', '9', '0');
1664                 break;
1665         default:
1666                 MMCAM_LOG_INFO("Not proper pixel type[%d]. Set default - I420", pixtype);
1667                 if (use_zero_copy_format)
1668                         fourcc = GST_MAKE_FOURCC('S', '4', '2', '0');
1669                 else
1670                         fourcc = GST_MAKE_FOURCC('I', '4', '2', '0');
1671
1672                 break;
1673         }
1674
1675         return fourcc;
1676 }
1677
1678
1679 int _mmcamcorder_get_pixtype(unsigned int fourcc)
1680 {
1681         int pixtype = MM_PIXEL_FORMAT_INVALID;
1682         /*
1683         char *pfourcc = (char*)&fourcc;
1684
1685         MMCAM_LOG_INFO("fourcc(%c%c%c%c)",
1686                                    pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
1687         */
1688
1689         switch (fourcc) {
1690         case GST_MAKE_FOURCC('S', 'N', '1', '2'):
1691                 /* fall through */
1692         case GST_MAKE_FOURCC('N', 'V', '1', '2'):
1693                 pixtype = MM_PIXEL_FORMAT_NV12;
1694                 break;
1695         case GST_MAKE_FOURCC('S', 'N', '2', '1'):
1696                 /* fall through */
1697         case GST_MAKE_FOURCC('N', 'V', '2', '1'):
1698                 pixtype = MM_PIXEL_FORMAT_NV21;
1699                 break;
1700         case GST_MAKE_FOURCC('S', 'U', 'Y', 'V'):
1701                 /* fall through */
1702         case GST_MAKE_FOURCC('Y', 'U', 'Y', 'V'):
1703                 /* fall through */
1704         case GST_MAKE_FOURCC('Y', 'U', 'Y', '2'):
1705                 pixtype = MM_PIXEL_FORMAT_YUYV;
1706                 break;
1707         case GST_MAKE_FOURCC('S', 'Y', 'V', 'Y'):
1708                 /* fall through */
1709         case GST_MAKE_FOURCC('U', 'Y', 'V', 'Y'):
1710                 pixtype = MM_PIXEL_FORMAT_UYVY;
1711                 break;
1712         case GST_MAKE_FOURCC('S', '4', '2', '0'):
1713                 /* fall through */
1714         case GST_MAKE_FOURCC('I', '4', '2', '0'):
1715                 pixtype = MM_PIXEL_FORMAT_I420;
1716                 break;
1717         case GST_MAKE_FOURCC('Y', 'V', '1', '2'):
1718                 pixtype = MM_PIXEL_FORMAT_YV12;
1719                 break;
1720         case GST_MAKE_FOURCC('4', '2', '2', 'P'):
1721                 pixtype = MM_PIXEL_FORMAT_422P;
1722                 break;
1723         case GST_MAKE_FOURCC('R', 'G', 'B', 'P'):
1724                 pixtype = MM_PIXEL_FORMAT_RGB565;
1725                 break;
1726         case GST_MAKE_FOURCC('R', 'G', 'B', '3'):
1727                 pixtype = MM_PIXEL_FORMAT_RGB888;
1728                 break;
1729         case GST_MAKE_FOURCC('A', 'R', 'G', 'B'):
1730                 /* fall through */
1731         case GST_MAKE_FOURCC('x', 'R', 'G', 'B'):
1732                 pixtype = MM_PIXEL_FORMAT_ARGB;
1733                 break;
1734         case GST_MAKE_FOURCC('B', 'G', 'R', 'A'):
1735                 /* fall through */
1736         case GST_MAKE_FOURCC('B', 'G', 'R', 'x'):
1737                 /* fall through */
1738         case GST_MAKE_FOURCC('S', 'R', '3', '2'):
1739                 pixtype = MM_PIXEL_FORMAT_RGBA;
1740                 break;
1741         case GST_MAKE_FOURCC('J', 'P', 'E', 'G'):
1742                 pixtype = MM_PIXEL_FORMAT_ENCODED;
1743                 break;
1744         /*FIXME*/
1745         case GST_MAKE_FOURCC('I', 'T', 'L', 'V'):
1746                 pixtype = MM_PIXEL_FORMAT_ITLV_JPEG_UYVY;
1747                 break;
1748         case GST_MAKE_FOURCC('H', '2', '6', '4'):
1749                 pixtype = MM_PIXEL_FORMAT_ENCODED_H264;
1750                 break;
1751         case GST_MAKE_FOURCC ('M', 'J', 'P', 'G'):
1752                 pixtype = MM_PIXEL_FORMAT_ENCODED_MJPEG;
1753                 break;
1754         case GST_MAKE_FOURCC ('V', 'P', '8', '0'):
1755                 /* fall through */
1756         case GST_MAKE_FOURCC ('v', 'p', '0', '8'):
1757                 pixtype = MM_PIXEL_FORMAT_ENCODED_VP8;
1758                 break;
1759         case GST_MAKE_FOURCC ('V', 'P', '9', '0'):
1760                 /* fall through */
1761         case GST_MAKE_FOURCC ('v', 'p', '0', '9'):
1762                 pixtype = MM_PIXEL_FORMAT_ENCODED_VP9;
1763                 break;
1764         case GST_MAKE_FOURCC('I', 'N', 'V', 'Z'):
1765                 pixtype = MM_PIXEL_FORMAT_INVZ;
1766                 break;
1767         default:
1768                 MMCAM_LOG_INFO("Not supported fourcc type(%c%c%c%c)", fourcc, fourcc>>8, fourcc>>16, fourcc>>24);
1769                 pixtype = MM_PIXEL_FORMAT_INVALID;
1770                 break;
1771         }
1772
1773         return pixtype;
1774 }
1775
1776
1777 gboolean _mmcamcorder_add_elements_to_bin(GstBin *bin, GList *element_list)
1778 {
1779         GList *local_list = element_list;
1780         _MMCamcorderGstElement *element = NULL;
1781
1782         mmf_return_val_if_fail(bin && local_list, FALSE);
1783
1784         while (local_list) {
1785                 element = (_MMCamcorderGstElement*)local_list->data;
1786                 if (element && element->gst) {
1787                         if (!gst_bin_add(bin, GST_ELEMENT(element->gst))) {
1788                                 MMCAM_LOG_ERROR("Add element [%s] to bin [%s] FAILED",
1789                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
1790                                         GST_ELEMENT_NAME(GST_ELEMENT(bin)));
1791                                 return FALSE;
1792                         } else {
1793                                 MMCAM_LOG_INFO("Add element [%s] to bin [%s] OK",
1794                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)),
1795                                         GST_ELEMENT_NAME(GST_ELEMENT(bin)));
1796                         }
1797                 }
1798                 local_list = local_list->next;
1799         }
1800
1801         return TRUE;
1802 }
1803
1804 gboolean _mmcamcorder_link_elements(GList *element_list)
1805 {
1806         GList                  *local_list  = element_list;
1807         _MMCamcorderGstElement *element     = NULL;
1808         _MMCamcorderGstElement *pre_element = NULL;
1809
1810         mmf_return_val_if_fail(local_list, FALSE);
1811
1812         pre_element = (_MMCamcorderGstElement*)local_list->data;
1813         local_list = local_list->next;
1814
1815         while (local_list) {
1816                 element = (_MMCamcorderGstElement*)local_list->data;
1817                 if (pre_element && pre_element->gst && element && element->gst) {
1818                         if (_MM_GST_ELEMENT_LINK(GST_ELEMENT(pre_element->gst), GST_ELEMENT(element->gst))) {
1819                                 MMCAM_LOG_INFO("Link [%s] to [%s] OK",
1820                                         GST_ELEMENT_NAME(GST_ELEMENT(pre_element->gst)),
1821                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
1822                         } else {
1823                                 MMCAM_LOG_ERROR("Link [%s] to [%s] FAILED",
1824                                         GST_ELEMENT_NAME(GST_ELEMENT(pre_element->gst)),
1825                                         GST_ELEMENT_NAME(GST_ELEMENT(element->gst)));
1826                                 return FALSE;
1827                         }
1828                 }
1829
1830                 pre_element = element;
1831                 local_list = local_list->next;
1832         }
1833
1834         return TRUE;
1835 }
1836
1837 gboolean _mmcamcorder_resize_frame(unsigned char *src_data, unsigned int src_width, unsigned int src_height, unsigned int src_length, int src_format,
1838         unsigned char **dst_data, unsigned int *dst_width, unsigned int *dst_height, size_t *dst_length)
1839 {
1840         int mm_ret = MM_ERROR_NONE;
1841         int input_format = MM_UTIL_COLOR_YUV420;
1842         mm_util_image_h src_image = NULL;
1843         mm_util_image_h dst_image = NULL;
1844
1845         if (!src_data || !dst_data || !dst_width || !dst_height || !dst_length) {
1846                 MMCAM_LOG_ERROR("something is NULL %p,%p,%p,%p,%p",
1847                         src_data, dst_data, dst_width, dst_height, dst_length);
1848                 return FALSE;
1849         }
1850
1851         /* set input format for mm-util */
1852         switch (src_format) {
1853         case MM_PIXEL_FORMAT_I420:
1854                 input_format = MM_UTIL_COLOR_I420;
1855                 break;
1856         case MM_PIXEL_FORMAT_YV12:
1857                 input_format = MM_UTIL_COLOR_YUV420;
1858                 break;
1859         case MM_PIXEL_FORMAT_NV12:
1860                 input_format = MM_UTIL_COLOR_NV12;
1861                 break;
1862         case MM_PIXEL_FORMAT_YUYV:
1863                 input_format = MM_UTIL_COLOR_YUYV;
1864                 break;
1865         case MM_PIXEL_FORMAT_UYVY:
1866                 input_format = MM_UTIL_COLOR_UYVY;
1867                 break;
1868         case MM_PIXEL_FORMAT_RGB888:
1869                 input_format = MM_UTIL_COLOR_RGB24;
1870                 break;
1871         default:
1872                 MMCAM_LOG_ERROR("NOT supported format [%d]", src_format);
1873                 return FALSE;
1874         }
1875
1876         MMCAM_LOG_INFO("src size %dx%d -> dst size %dx%d", src_width, src_height, *dst_width, *dst_height);
1877
1878         mm_ret = mm_image_create_image(src_width, src_height, input_format, src_data, (size_t)src_length, &src_image);
1879         if (mm_ret != MM_ERROR_NONE) {
1880                 MMCAM_LOG_ERROR("mm_image_create_image failed 0x%x", mm_ret);
1881                 return FALSE;
1882         }
1883
1884         mm_ret = mm_util_resize_image(src_image, *dst_width, *dst_height, &dst_image);
1885         mm_image_destroy_image(src_image);
1886         if (mm_ret != MM_ERROR_NONE) {
1887                 MMCAM_LOG_ERROR("mm_util_resize_image failed 0x%x", mm_ret);
1888                 return FALSE;
1889         }
1890
1891         mm_ret = mm_image_get_image(dst_image, dst_width, dst_height, NULL, dst_data, dst_length);
1892         mm_image_destroy_image(dst_image);
1893         if (mm_ret != MM_ERROR_NONE) {
1894                 MMCAM_LOG_ERROR("mm_image_get_image failed 0x%x", mm_ret);
1895                 return FALSE;
1896         }
1897
1898         MMCAM_LOG_INFO("resize done %dx%d -> %dx%d, %p, length %zu",
1899                 src_width, src_height, *dst_width, *dst_height, *dst_data, *dst_length);
1900
1901         return TRUE;
1902 }
1903
1904
1905 gboolean _mmcamcorder_encode_jpeg(void *src_data, unsigned int src_width, unsigned int src_height,
1906         int src_format, unsigned int src_length, unsigned int jpeg_quality,
1907         void **result_data, unsigned int *result_length)
1908 {
1909         int ret = MM_UTIL_ERROR_NONE;
1910         gboolean ret_conv = TRUE;
1911         mm_util_color_format_e jpeg_format = MM_UTIL_COLOR_NUM;
1912         unsigned char *converted_src = NULL;
1913         unsigned int converted_src_size = 0;
1914         mm_util_image_h decoded = NULL;
1915         void *encoded_data = NULL;
1916         size_t encoded_data_size = 0;
1917
1918         mmf_return_val_if_fail(src_data && result_data && result_length, FALSE);
1919
1920         switch (src_format) {
1921         case MM_PIXEL_FORMAT_NV12:
1922                 //jpeg_format = MM_UTIL_COLOR_NV12;
1923                 ret_conv = _mmcamcorder_convert_NV12_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1924                 jpeg_format = MM_UTIL_COLOR_YUV420;
1925                 break;
1926         case MM_PIXEL_FORMAT_NV16:
1927                 jpeg_format = MM_UTIL_COLOR_NV16;
1928                 converted_src = src_data;
1929                 break;
1930         case MM_PIXEL_FORMAT_NV21:
1931                 jpeg_format = MM_UTIL_COLOR_NV21;
1932                 converted_src = src_data;
1933                 break;
1934         case MM_PIXEL_FORMAT_YUYV:
1935                 //jpeg_format = MM_UTIL_COLOR_YUYV;
1936                 ret_conv = _mmcamcorder_convert_YUYV_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1937                 jpeg_format = MM_UTIL_COLOR_YUV420;
1938                 break;
1939         case MM_PIXEL_FORMAT_UYVY:
1940                 //jpeg_format = MM_UTIL_COLOR_UYVY;
1941                 ret_conv = _mmcamcorder_convert_UYVY_to_I420(src_data, src_width, src_height, &converted_src, &converted_src_size);
1942                 jpeg_format = MM_UTIL_COLOR_YUV420;
1943                 break;
1944         case MM_PIXEL_FORMAT_I420:
1945                 jpeg_format = MM_UTIL_COLOR_YUV420;
1946                 converted_src = src_data;
1947                 break;
1948         case MM_PIXEL_FORMAT_RGB888:
1949                 jpeg_format = MM_UTIL_COLOR_RGB24;
1950                 converted_src = src_data;
1951                 break;
1952         case MM_PIXEL_FORMAT_RGBA:
1953                 jpeg_format = MM_UTIL_COLOR_RGBA;
1954                 converted_src = src_data;
1955                 break;
1956         case MM_PIXEL_FORMAT_ARGB:
1957                 jpeg_format = MM_UTIL_COLOR_ARGB;
1958                 converted_src = src_data;
1959                 break;
1960         case MM_PIXEL_FORMAT_422P:
1961                 jpeg_format = MM_UTIL_COLOR_YUV422;     // not supported
1962                 return FALSE;
1963         case MM_PIXEL_FORMAT_NV12T:
1964         case MM_PIXEL_FORMAT_YV12:
1965         case MM_PIXEL_FORMAT_RGB565:
1966         case MM_PIXEL_FORMAT_ENCODED:
1967         case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
1968         default:
1969                 return FALSE;
1970         }
1971
1972         if (ret_conv == FALSE) {
1973                 if (converted_src &&
1974                     converted_src != src_data) {
1975                         free(converted_src);
1976                         converted_src = NULL;
1977                 }
1978                 MMCAM_LOG_ERROR("color convert error source format[%d], jpeg format[%d]", src_format, jpeg_format);
1979                 return FALSE;
1980         }
1981
1982         converted_src_size = (converted_src && (converted_src != src_data)) ? converted_src_size : src_length;
1983         ret = mm_image_create_image(src_width, src_height, jpeg_format, converted_src, (size_t)converted_src_size, &decoded);
1984         if (ret != MM_UTIL_ERROR_NONE) {
1985                 MMCAM_LOG_ERROR("mm_image_create_image error [%d]", ret);
1986                 if (converted_src && (converted_src != src_data))
1987                         free(converted_src);
1988                 return FALSE;
1989         }
1990
1991         ret = mm_util_encode_to_jpeg_memory(decoded, jpeg_quality, &encoded_data, &encoded_data_size);
1992         mm_image_destroy_image(decoded);
1993
1994         if (converted_src && (converted_src != src_data)) {
1995                 free(converted_src);
1996                 converted_src = NULL;
1997         }
1998
1999         if (ret != MM_UTIL_ERROR_NONE) {
2000                 MMCAM_LOG_ERROR("No encoder supports %d format, error code %x", src_format, ret);
2001                 if (encoded_data)
2002                         free(encoded_data);
2003                 return FALSE;
2004         }
2005
2006         *result_data = encoded_data;
2007         *result_length = (unsigned int)encoded_data_size;
2008
2009         return TRUE;
2010 }
2011
2012
2013 /* make UYVY smaller as multiple size. ex: 640x480 -> 320x240 or 160x120 ... */
2014 gboolean _mmcamcorder_downscale_UYVYorYUYV(unsigned char *src, unsigned int src_width, unsigned int src_height,
2015         unsigned char **dst, unsigned int dst_width, unsigned int dst_height)
2016 {
2017         unsigned int i = 0;
2018         int j = 0;
2019         int k = 0;
2020         int src_index = 0;
2021         int ratio_width = 0;
2022         int ratio_height = 0;
2023         int line_base = 0;
2024         int line_width = 0;
2025         int jump_width = 0;
2026         unsigned char *result = NULL;
2027
2028         if (src == NULL || dst == NULL) {
2029                 MMCAM_LOG_ERROR("src[%p] or dst[%p] is NULL", src, dst);
2030                 return FALSE;
2031         }
2032
2033         result = (unsigned char *)malloc((dst_width * dst_height)<<1);
2034         if (!result) {
2035                 MMCAM_LOG_ERROR("failed to alloc dst data");
2036                 return FALSE;
2037         }
2038
2039         ratio_width = src_width / dst_width;
2040         ratio_height = src_height / dst_height;
2041         line_width = src_width << 1;
2042         jump_width = ratio_width << 1;
2043
2044         MMCAM_LOG_WARNING("[src %dx%d] [dst %dx%d] [line width %d] [ratio width %d, height %d]",
2045                 src_width, src_height, dst_width, dst_height, line_width, ratio_width, ratio_height);
2046
2047         for (i = 0 ; i < src_height ; i += ratio_height) {
2048                 line_base = i * line_width;
2049                 for (j = 0 ; j < line_width ; j += jump_width) {
2050                         src_index = line_base + j;
2051                         result[k++] = src[src_index];
2052                         result[k++] = src[src_index+1];
2053
2054                         j += jump_width;
2055                         src_index = line_base + j;
2056                         if (src_index % 4 == 0)
2057                                 result[k++] = src[src_index+2];
2058                         else
2059                                 result[k++] = src[src_index];
2060
2061                         result[k++] = src[src_index+1];
2062                 }
2063         }
2064
2065         *dst = result;
2066
2067         MMCAM_LOG_WARNING("converting done - result %p", result);
2068
2069         return TRUE;
2070 }
2071
2072
2073 static guint16 get_language_code(const char *str)
2074 {
2075         return (guint16)(((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
2076 }
2077
2078 static gchar * str_to_utf8(const gchar *str)
2079 {
2080         return g_convert(str, -1, "UTF-8", "ASCII", NULL, NULL, NULL);
2081 }
2082
2083 static inline gboolean write_tag(FILE *f, const gchar *tag)
2084 {
2085         while (*tag)
2086                 FPUTC_CHECK(*tag++, f);
2087
2088         return TRUE;
2089 }
2090
2091 static inline gboolean write_to_32(FILE *f, guint val)
2092 {
2093         FPUTC_CHECK(val >> 24, f);
2094         FPUTC_CHECK(val >> 16, f);
2095         FPUTC_CHECK(val >> 8, f);
2096         FPUTC_CHECK(val, f);
2097         return TRUE;
2098 }
2099
2100 static inline gboolean write_to_16(FILE *f, guint val)
2101 {
2102         FPUTC_CHECK(val >> 8, f);
2103         FPUTC_CHECK(val, f);
2104         return TRUE;
2105 }
2106
2107 static inline gboolean write_to_24(FILE *f, guint val)
2108 {
2109         write_to_16(f, val >> 8);
2110         FPUTC_CHECK(val, f);
2111         return TRUE;
2112 }
2113
2114 void *_mmcamcorder_util_task_thread_func(void *data)
2115 {
2116         int ret = MM_ERROR_NONE;
2117         mmf_camcorder_t *hcamcorder = (mmf_camcorder_t *)data;
2118
2119         if (!hcamcorder) {
2120                 MMCAM_LOG_ERROR("handle is NULL");
2121                 return NULL;
2122         }
2123
2124         MMCAM_LOG_WARNING("start thread");
2125
2126         g_mutex_lock(&hcamcorder->task_thread_lock);
2127
2128         while (hcamcorder->task_thread_state != _MMCAMCORDER_TASK_THREAD_STATE_EXIT) {
2129                 switch (hcamcorder->task_thread_state) {
2130                 case _MMCAMCORDER_TASK_THREAD_STATE_NONE:
2131                         MMCAM_LOG_WARNING("wait for task signal");
2132                         g_cond_wait(&hcamcorder->task_thread_cond, &hcamcorder->task_thread_lock);
2133                         MMCAM_LOG_WARNING("task signal received : state %d", hcamcorder->task_thread_state);
2134                         break;
2135                 case _MMCAMCORDER_TASK_THREAD_STATE_SOUND_PLAY_START:
2136                         _mmcamcorder_sound_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE02, FALSE);
2137                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
2138                         break;
2139                 case _MMCAMCORDER_TASK_THREAD_STATE_SOUND_SOLO_PLAY_START:
2140                         _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_CAPTURE01, FALSE);
2141                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
2142                         break;
2143                 case _MMCAMCORDER_TASK_THREAD_STATE_ENCODE_PIPE_CREATE:
2144                         ret = _mmcamcorder_video_prepare_record((MMHandleType)hcamcorder);
2145
2146                         /* Play record start sound */
2147                         _mmcamcorder_sound_solo_play((MMHandleType)hcamcorder, _MMCAMCORDER_SAMPLE_SOUND_NAME_REC_START, FALSE);
2148
2149                         MMCAM_LOG_INFO("_mmcamcorder_video_prepare_record return 0x%x", ret);
2150                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
2151                         break;
2152                 case _MMCAMCORDER_TASK_THREAD_STATE_CHECK_CAPTURE_IN_RECORDING:
2153                         {
2154                                 gint64 end_time = 0;
2155
2156                                 MMCAM_LOG_WARNING("wait for capture data in recording. wait signal...");
2157
2158                                 end_time = g_get_monotonic_time() + (5 * G_TIME_SPAN_SECOND);
2159
2160                                 if (g_cond_wait_until(&hcamcorder->task_thread_cond, &hcamcorder->task_thread_lock, end_time)) {
2161                                         MMCAM_LOG_WARNING("signal received");
2162                                 } else {
2163                                         _MMCamcorderMsgItem message;
2164
2165                                         memset(&message, 0x0, sizeof(_MMCamcorderMsgItem));
2166
2167                                         MMCAM_LOG_ERROR("capture data wait time out, send error message");
2168
2169                                         message.id = MM_MESSAGE_CAMCORDER_ERROR;
2170                                         message.param.code = MM_ERROR_CAMCORDER_RESPONSE_TIMEOUT;
2171
2172                                         _mmcamcorder_send_message((MMHandleType)hcamcorder, &message);
2173
2174                                         hcamcorder->capture_in_recording = FALSE;
2175                                 }
2176
2177                                 hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_NONE;
2178                         }
2179                         break;
2180                 default:
2181                         MMCAM_LOG_WARNING("invalid task thread state %d", hcamcorder->task_thread_state);
2182                         hcamcorder->task_thread_state = _MMCAMCORDER_TASK_THREAD_STATE_EXIT;
2183                         break;
2184                 }
2185         }
2186
2187         g_mutex_unlock(&hcamcorder->task_thread_lock);
2188
2189         MMCAM_LOG_WARNING("exit thread");
2190
2191         return NULL;
2192 }
2193
2194
2195 static gboolean _mmcamcorder_convert_YUYV_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2196 {
2197         unsigned int i = 0;
2198         int j = 0;
2199         int src_offset = 0;
2200         int dst_y_offset = 0;
2201         int dst_u_offset = 0;
2202         int dst_v_offset = 0;
2203         int loop_length = 0;
2204         unsigned int dst_size = 0;
2205         unsigned char *dst_data = NULL;
2206
2207         if (!src || !dst || !dst_len) {
2208                 MMCAM_LOG_ERROR("NULL pointer %p, %p, %p", src, dst, dst_len);
2209                 return FALSE;
2210         }
2211
2212         dst_size = (width * height * 3) >> 1;
2213
2214         MMCAM_LOG_INFO("YUVY -> I420 : %dx%d, dst size %d", width, height, dst_size);
2215
2216         dst_data = (unsigned char *)malloc(dst_size);
2217         if (!dst_data) {
2218                 MMCAM_LOG_ERROR("failed to alloc dst_data. size %d", dst_size);
2219                 return FALSE;
2220         }
2221
2222         loop_length = width << 1;
2223         dst_u_offset = width * height;
2224         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2225
2226         MMCAM_LOG_INFO("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2227
2228         for (i = 0 ; i < height ; i++) {
2229                 for (j = 0 ; j < loop_length ; j += 2) {
2230                         dst_data[dst_y_offset++] = src[src_offset++]; /*Y*/
2231
2232                         if (i % 2 == 0) {
2233                                 if (j % 4 == 0)
2234                                         dst_data[dst_u_offset++] = src[src_offset++]; /*U*/
2235                                 else
2236                                         dst_data[dst_v_offset++] = src[src_offset++]; /*V*/
2237                         } else {
2238                                 src_offset++;
2239                         }
2240                 }
2241         }
2242
2243         *dst = dst_data;
2244         *dst_len = dst_size;
2245
2246         MMCAM_LOG_INFO("DONE: YUVY -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2247
2248         return TRUE;
2249 }
2250
2251
2252 static gboolean _mmcamcorder_convert_UYVY_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2253 {
2254         unsigned int i = 0;
2255         int j = 0;
2256         int src_offset = 0;
2257         int dst_y_offset = 0;
2258         int dst_u_offset = 0;
2259         int dst_v_offset = 0;
2260         int loop_length = 0;
2261         unsigned int dst_size = 0;
2262         unsigned char *dst_data = NULL;
2263
2264         if (!src || !dst || !dst_len) {
2265                 MMCAM_LOG_ERROR("NULL pointer %p, %p, %p", src, dst, dst_len);
2266                 return FALSE;
2267         }
2268
2269         dst_size = (width * height * 3) >> 1;
2270
2271         MMCAM_LOG_INFO("UYVY -> I420 : %dx%d, dst size %d", width, height, dst_size);
2272
2273         dst_data = (unsigned char *)malloc(dst_size);
2274         if (!dst_data) {
2275                 MMCAM_LOG_ERROR("failed to alloc dst_data. size %d", dst_size);
2276                 return FALSE;
2277         }
2278
2279         loop_length = width << 1;
2280         dst_u_offset = width * height;
2281         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2282
2283         MMCAM_LOG_INFO("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2284
2285         for (i = 0 ; i < height ; i++) {
2286                 for (j = 0 ; j < loop_length ; j += 2) {
2287                         if (i % 2 == 0) {
2288                                 if (j % 4 == 0)
2289                                         dst_data[dst_u_offset++] = src[src_offset++]; /*U*/
2290                                 else
2291                                         dst_data[dst_v_offset++] = src[src_offset++]; /*V*/
2292                         } else {
2293                                 src_offset++;
2294                         }
2295
2296                         dst_data[dst_y_offset++] = src[src_offset++]; /*Y*/
2297                 }
2298         }
2299
2300         *dst = dst_data;
2301         *dst_len = dst_size;
2302
2303         MMCAM_LOG_INFO("DONE: UYVY -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2304
2305         return TRUE;
2306 }
2307
2308
2309 static gboolean _mmcamcorder_convert_NV12_to_I420(unsigned char *src, guint width, guint height, unsigned char **dst, unsigned int *dst_len)
2310 {
2311         int i = 0;
2312         int src_offset = 0;
2313         int dst_y_offset = 0;
2314         int dst_u_offset = 0;
2315         int dst_v_offset = 0;
2316         int loop_length = 0;
2317         unsigned int dst_size = 0;
2318         unsigned char *dst_data = NULL;
2319
2320         if (!src || !dst || !dst_len) {
2321                 MMCAM_LOG_ERROR("NULL pointer %p, %p, %p", src, dst, dst_len);
2322                 return FALSE;
2323         }
2324
2325         /* buffer overflow prevention check */
2326         if (width > __MMCAMCORDER_MAX_WIDTH || height > __MMCAMCORDER_MAX_HEIGHT) {
2327                 MMCAM_LOG_ERROR("too large size %d x %d", width, height);
2328                 return FALSE;
2329         }
2330
2331         dst_size = (width * height * 3) >> 1;
2332
2333         MMCAM_LOG_INFO("NV12 -> I420 : %dx%d, dst size %d", width, height, dst_size);
2334
2335         dst_data = (unsigned char *)malloc(dst_size);
2336         if (!dst_data) {
2337                 MMCAM_LOG_ERROR("failed to alloc dst_data. size %d", dst_size);
2338                 return FALSE;
2339         }
2340
2341         loop_length = width << 1;
2342         dst_u_offset = width * height;
2343         dst_v_offset = dst_u_offset + (dst_u_offset >> 2);
2344
2345         MMCAM_LOG_INFO("offset y %d, u %d, v %d", dst_y_offset, dst_u_offset, dst_v_offset);
2346
2347         /* memcpy Y */
2348         memcpy(dst_data, src, dst_u_offset);
2349
2350         loop_length = dst_u_offset >> 1;
2351         src_offset = dst_u_offset;
2352
2353         /* set U and V */
2354         for (i = 0 ; i < loop_length ; i++) {
2355                 if (i % 2 == 0)
2356                         dst_data[dst_u_offset++] = src[src_offset++];
2357                 else
2358                         dst_data[dst_v_offset++] = src[src_offset++];
2359         }
2360
2361         *dst = dst_data;
2362         *dst_len = dst_size;
2363
2364         MMCAM_LOG_INFO("DONE: NV12 -> I420 : %dx%d, dst data %p, size %d", width, height, *dst, dst_size);
2365
2366         return TRUE;
2367 }
2368
2369
2370 void _mmcamcorder_emit_dbus_signal(GDBusConnection *conn, const char *object_name,
2371         const char *interface_name, const char *signal_name, int value)
2372 {
2373         if (!conn || !object_name || !interface_name || !signal_name) {
2374                 MMCAM_LOG_ERROR("NULL pointer %p %p %p %p",
2375                         conn, object_name, interface_name, signal_name);
2376                 return;
2377         }
2378
2379         if (!g_dbus_connection_emit_signal(conn, NULL,
2380                 object_name, interface_name, signal_name,
2381                 g_variant_new("(i)", value), NULL)) {
2382                 MMCAM_LOG_WARNING("failed to emit signal");
2383         } else {
2384                 MMCAM_LOG_INFO("emit signal done - value 0x%.8x", value);
2385                 g_dbus_connection_flush(conn, NULL, NULL, NULL);
2386                 MMCAM_LOG_INFO("signal flush done");
2387         }
2388
2389         return;
2390 }
2391
2392
2393 int _mmcamcorder_get_audiosrc_blocksize(int samplerate, int format, int channel, int interval, int *blocksize)
2394 {
2395         int depth = 8;
2396
2397         if (!blocksize) {
2398                 MMCAM_LOG_ERROR("NULL ptr");
2399                 return FALSE;
2400         }
2401
2402         if (samplerate == 0 || channel == 0 || interval == 0) {
2403                 MMCAM_LOG_ERROR("invalid param %d %d %d", samplerate, channel, interval);
2404                 return FALSE;
2405         }
2406
2407         if (format == MM_CAMCORDER_AUDIO_FORMAT_PCM_S16_LE)
2408                 depth = 16;
2409
2410         *blocksize = samplerate * depth * channel * interval / 8000;
2411
2412         return TRUE;
2413 }
2414
2415 gboolean _mmcamcorder_is_encoded_preview_pixel_format(int pixel_format)
2416 {
2417         return (pixel_format == MM_PIXEL_FORMAT_ENCODED_H264 ||
2418                 pixel_format == MM_PIXEL_FORMAT_ENCODED_MJPEG ||
2419                 pixel_format == MM_PIXEL_FORMAT_ENCODED_VP8 ||
2420                 pixel_format == MM_PIXEL_FORMAT_ENCODED_VP9);
2421 }
2422
2423 void _mmcamcorder_set_log_level(int level)
2424 {
2425         MMCAM_LOG_WARNING("set log level[%d]", level);
2426         mmcam_log_level = level;
2427 }
2428
2429 int _mmcamcorder_get_log_level(void)
2430 {
2431         return mmcam_log_level;
2432 }
2433
2434 void _mmcamcorder_measure_fps(void *data)
2435 {
2436         int diff_sec;
2437         int frame_count = 0;
2438         struct timeval current_video_time;
2439         _MMCamcorderKPIMeasure *kpi = (_MMCamcorderKPIMeasure *)data;
2440
2441         mmf_return_if_fail(kpi);
2442
2443         if (!timerisset(&kpi->init_video_time) && !timerisset(&kpi->last_video_time)) {
2444                 MMCAM_LOG_INFO("START to measure FPS");
2445                 gettimeofday(&(kpi->init_video_time), NULL);
2446         }
2447
2448         frame_count = ++(kpi->video_framecount);
2449
2450         gettimeofday(&current_video_time, NULL);
2451
2452         diff_sec = current_video_time.tv_sec - kpi->last_video_time.tv_sec;
2453         if (diff_sec == 0)
2454                 return;
2455
2456         kpi->current_fps = (frame_count - kpi->last_framecount) / diff_sec;
2457
2458         if ((current_video_time.tv_sec - kpi->init_video_time.tv_sec) != 0)
2459                 kpi->average_fps = kpi->video_framecount / (current_video_time.tv_sec - kpi->init_video_time.tv_sec);
2460
2461         kpi->last_framecount = frame_count;
2462         kpi->last_video_time.tv_sec = current_video_time.tv_sec;
2463         kpi->last_video_time.tv_usec = current_video_time.tv_usec;
2464
2465         MMCAM_LOG_INFO("current fps[%d], average[%d]", kpi->current_fps, kpi->average_fps);
2466 }