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