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