86c9473469b0830c68d517483782f822261fde62
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@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 #include <unistd.h>
23 #include <stdlib.h>
24 #ifndef __USE_XOPEN
25 #define DEF_XOPEN
26 #define __USE_XOPEN /* needed for strptime */
27 #endif
28 #include <time.h>
29 #ifdef DEF_XOPEN
30 #undef __USE_XOPEN
31 #endif
32 #include <string.h>
33 #include <system_info.h>
34 #include <sys/vfs.h>
35 #include <glib/gstdio.h>
36 #include <sys/stat.h>
37 #include <dirent.h>
38 #include <ctype.h>
39 #include <aul/aul.h>
40 #include <mm_file.h>
41 #include <libexif/exif-data.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <mm_util_magick.h>
45 #include "media-util-err.h"
46 #include "media-svc-util.h"
47 #include "media-svc-db-utils.h"
48 #include "media-svc-debug.h"
49 #include "media-svc-env.h"
50 #include "media-svc-hash.h"
51 #include "media-svc-album.h"
52 #include "media-svc-localize_ch.h"
53
54 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
55
56 /* Define data structures for media type and mime type */
57 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
58 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
59 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
60 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
61 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
62 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
63 #define MEDIA_SVC_CATEGORY_PVR  0x00000020      /**< PVR category */
64 #define MEDIA_SVC_CATEGORY_UHD  0x00000040      /**< UHD category */
65 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080      /**< SCSA category */
66
67 #define CONTENT_TYPE_NUM 5
68 #define MUSIC_MIME_NUM 29
69 #define SOUND_MIME_NUM 1
70 #define MIME_TYPE_LENGTH 255
71 #define MIME_LENGTH 50
72 #define _3GP_FILE ".3gp"
73 #define _MP4_FILE ".mp4"
74 #define _ASF_FILE ".asf"
75 #define MEDIA_SVC_ARTWORK_SIZE 2000
76 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
77
78 #define MEDIA_SVC_DEFAULT_GPS_VALUE                     -200                    /**< Default GPS Value*/
79
80 static int media_svc_pinyin_support = -1;
81
82 typedef struct {
83         char content_type[15];
84         int category_by_mime;
85 } _media_svc_content_table_s;
86
87 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
88         {"audio", MEDIA_SVC_CATEGORY_SOUND},
89         {"image", MEDIA_SVC_CATEGORY_IMAGE},
90         {"video", MEDIA_SVC_CATEGORY_VIDEO},
91         {"application", MEDIA_SVC_CATEGORY_ETC},
92         {"text", MEDIA_SVC_CATEGORY_ETC},
93 };
94
95 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
96         /*known mime types of normal files*/
97         "mpeg",
98         "ogg",
99         "x-ms-wma",
100         "x-flac",
101         "mp4",
102         /* known mime types of drm files*/
103         "mp3",
104         "x-mp3", /*alias of audio/mpeg*/
105         "x-mpeg", /*alias of audio/mpeg*/
106         "3gpp",
107         "x-ogg", /*alias of audio/ogg*/
108         "vnd.ms-playready.media.pya:*.pya", /*playready*/
109         "wma",
110         "aac",
111         "x-m4a", /*alias of audio/mp4*/
112         /* below mimes are rare*/
113         "x-vorbis+ogg",
114         "x-flac+ogg",
115         "x-matroska",
116         "ac3",
117         "mp2",
118         "x-ape",
119         "x-ms-asx",
120         "vnd.rn-realaudio",
121
122         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
123         "vorbis", /*alias of audio/x-vorbis+ogg*/
124         "x-oggflac",
125         "x-mp2", /*alias of audio/mp2*/
126         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
127         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
128         "x-wav",
129 };
130
131 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
132         "x-smaf",
133 };
134
135 char *_media_info_generate_uuid(void)
136 {
137         uuid_t uuid_value;
138         static char uuid_unparsed[37];
139
140 RETRY_GEN:
141         uuid_generate(uuid_value);
142         uuid_unparse(uuid_value, uuid_unparsed);
143
144         if (strlen(uuid_unparsed) < 36) {
145                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
146                 goto RETRY_GEN;
147         }
148
149         return uuid_unparsed;
150 }
151
152 static int __media_svc_split_to_double(char *input, double *arr)
153 {
154         char tmp_arr[255] = {0, };
155         int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
156
157         if (!STRING_VALID(input)) {
158                 media_svc_error("Invalid parameter");
159                 return MS_MEDIA_ERR_INVALID_PARAMETER;
160         }
161         memset(tmp_arr, 0x0, sizeof(tmp_arr));
162
163         /*media_svc_debug("input: [%s]", input); */
164
165         len = strlen(input);
166
167         for (idx = 0; idx < (len + 1); idx++) {
168                 if (input[idx] == ' ') {
169                         continue;
170                 } else if ((input[idx] == ',') || (idx == len)) {
171                         arr[arr_idx] = atof(tmp_arr);
172                         arr_idx++;
173                         str_idx = 0;
174                         /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
175                         memset(tmp_arr, 0x0, sizeof(tmp_arr));
176                 } else {
177                         tmp_arr[str_idx] = input[idx];
178                         str_idx++;
179                 }
180         }
181
182         if (arr_idx != 3) {
183                 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
184                 return MS_MEDIA_ERR_INTERNAL;
185         }
186
187         return MS_MEDIA_ERR_NONE;
188 }
189
190 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
191 {
192         ExifEntry *entry;
193         ExifTag tag;
194
195         if (ed == NULL)
196                 return MS_MEDIA_ERR_INVALID_PARAMETER;
197
198         tag = tagtype;
199
200         entry = exif_data_get_entry(ed, tag);
201         if (entry) {
202                 /* Get the contents of the tag in human-readable form */
203                 if (tag == EXIF_TAG_ORIENTATION ||
204                         tag == EXIF_TAG_PIXEL_X_DIMENSION ||
205                         tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
206                         tag == EXIF_TAG_ISO_SPEED_RATINGS) {
207
208                         if (i_value == NULL) {
209                                 media_svc_debug("i_value is NULL");
210                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
211                         }
212
213                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
214                         short exif_value = exif_get_short(entry->data, mByteOrder);
215                         *i_value = (int)exif_value;
216
217                 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
218
219                         if (d_value == NULL) {
220                                 media_svc_debug("d_value is NULL");
221                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
222                         }
223
224                         /* Get the contents of the tag in human-readable form */
225                         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
226                         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
227                         gps_buf[strlen(gps_buf)] = '\0';
228                         int ret = MS_MEDIA_ERR_NONE;
229
230                         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
231
232                         ret = __media_svc_split_to_double(gps_buf, tmp_arr);
233                         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
234
235                         *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
236                 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
237
238                         if (buf == NULL) {
239                                 media_svc_debug("buf is NULL");
240                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
241                         }
242
243                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
244                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
245                         long numerator = mRational.numerator;
246                         long denominator = mRational.denominator;
247                         snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
248
249                 } else if (tag == EXIF_TAG_FNUMBER) {
250
251                         if (d_value == NULL) {
252                                 media_svc_debug("d_value is NULL");
253                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
254                         }
255
256                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
257                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
258                         long numerator = mRational.numerator;
259                         long denominator = mRational.denominator;
260
261                         *d_value = ((numerator*1.0)/(denominator*1.0));
262
263                 } else {
264
265                         if (buf == NULL) {
266                                 media_svc_debug("buf is NULL");
267                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
268                         }
269
270                         exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
271                         buf[strlen(buf)] = '\0';
272                 }
273         }
274
275         return MS_MEDIA_ERR_NONE;
276 }
277
278 time_t __media_svc_get_timeline_from_str(const char *timstr)
279 {
280         struct tm t;
281         time_t modified_t = 0;
282         time_t rawtime;
283         struct tm timeinfo;
284
285         if (!STRING_VALID(timstr)) {
286                 media_svc_error("Invalid Parameter");
287                 return 0;
288         }
289
290         /*Exif Format : %Y:%m:%d %H:%M:%S
291         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
292         memset(&t, 0x00, sizeof(struct tm));
293
294         tzset();
295         time(&rawtime);
296         localtime_r(&rawtime, &timeinfo);
297
298         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
299                 t.tm_isdst = timeinfo.tm_isdst;
300                 if (t.tm_isdst != 0)
301                         media_svc_debug("DST %d", t.tm_isdst);
302
303                 /* If time string has timezone */
304                 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
305                         GTimeVal timeval;
306                         char tim_tmp_str[255] = { 0, };
307
308                         /* ISO8601 Time string format */
309                         strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
310                         g_time_val_from_iso8601(tim_tmp_str, &timeval);
311                         modified_t = timeval.tv_sec;
312                         media_svc_debug("Calibrated timeval : [%lu][%s]", modified_t, tim_tmp_str);
313                 } else {
314                         /* Just localtime */
315                         modified_t = mktime(&t);
316                 }
317
318                 if (modified_t > 0)
319                         return modified_t;
320                 else
321                         media_svc_debug("Failed to get timeline : [%s] [%d:%d:%d: %d:%d:%d]", timstr, t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
322         } else {
323                 media_svc_error("Failed to get timeline : [%s]", timstr);
324         }
325
326         return 0;
327 }
328
329 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
330 {
331         int idx = 0;
332
333         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
334         media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
335         media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
336
337         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
338
339         /*categorize from mimetype */
340         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
341                 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
342                         *category = (*category | content_category[idx].category_by_mime);
343                         break;
344                 }
345         }
346
347         /*in application type, exitst sound file ex) x-smafs, asf */
348         if (*category & MEDIA_SVC_CATEGORY_ETC) {
349                 int prefix_len = strlen(content_category[3].content_type) + 1;
350                 char *ext = NULL;
351
352                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
353                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
354                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
355                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
356                                 break;
357                         }
358                 }
359
360                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
361                         *category ^= MEDIA_SVC_CATEGORY_ETC;
362                         *category |= MEDIA_SVC_CATEGORY_SOUND;
363                 }
364
365                 /*"asf" must check video stream and then categorize in directly. */
366                 ext = strrchr(path, '.');
367                 if (ext != NULL) {
368                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
369                                 int audio = 0;
370                                 int video = 0;
371                                 int err = 0;
372
373                                 err = mm_file_get_stream_info(path, &audio, &video);
374                                 if (err == 0) {
375                                         if (audio > 0 && video == 0) {
376                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
377                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
378                                         } else {
379                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
380                                                 *category |= MEDIA_SVC_CATEGORY_VIDEO;
381                                         }
382                                 }
383                         }
384                 }
385         }
386
387         /*check music file in sound files. */
388         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
389                 int prefix_len = strlen(content_category[0].content_type) + 1;
390
391                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
392                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
393                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
394                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
395                                 break;
396                         }
397                 }
398
399                 /*m3u file is playlist but mime type is "audio/x-mpegurl". but It has to be classified into MS_CATEGORY_ETC since playlist is not a sound track*/
400                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
401                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
402                         *category |= MEDIA_SVC_CATEGORY_ETC;
403                 }
404         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
405                 /*some video files don't have video stream. in this case it is categorize as music. */
406                 char *ext = NULL;
407                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
408                 ext = strrchr(path, '.');
409                 if (ext != NULL) {
410                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
411                                 int audio = 0;
412                                 int video = 0;
413                                 int err = 0;
414
415                                 err = mm_file_get_stream_info(path, &audio, &video);
416                                 if (err == 0) {
417                                         if (audio > 0 && video == 0) {
418                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
419                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
420                                         }
421                                 }
422                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
423                         }
424                 }
425         }
426
427         return MS_MEDIA_ERR_NONE;
428 }
429
430 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
431 {
432         int ret = MS_MEDIA_ERR_NONE;
433         int category = 0;
434
435         media_svc_media_type_e type;
436
437         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
438         if (ret != MS_MEDIA_ERR_NONE)
439                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
440
441         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
442         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
443         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
444         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
445         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
446
447         *media_type = type;
448
449         return ret;
450 }
451
452 /*
453 drm_contentifo is not NULL, if the file is OMA DRM.
454 If the file is not OMA DRM, drm_contentinfo must be NULL.
455 */
456 static int __media_svc_get_mime_type(const char *path, char *mimetype)
457 {
458         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
459
460         /*in case of normal files or failure to get mime in drm */
461         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
462                 media_svc_error("aul_get_mime_from_file fail");
463                 return MS_MEDIA_ERR_INTERNAL;
464         }
465
466         return MS_MEDIA_ERR_NONE;
467 }
468
469 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
470 {
471         int i = 0;
472
473         for (i = strlen(file_path); i >= 0; i--) {
474                 if (file_path[i] == '.') {
475                         SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
476                         return true;
477                 }
478
479                 if (file_path[i] == '/')
480                         return false;
481         }
482         return false;
483 }
484
485 static int __media_svc_resize_artwork(const char *path, const char *img_format)
486 {
487         int ret = MS_MEDIA_ERR_NONE;
488         unsigned int width = 0;
489         unsigned int height = 0;
490         unsigned int resized_width = 0;
491         unsigned int resized_height = 0;
492         mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
493
494         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
495                 media_svc_debug("type [jpeg]");
496
497                 mm_util_extract_image_info(path, &img_type, &width, &height);
498
499                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
500                         media_svc_debug("No need resizing");
501                         return MS_MEDIA_ERR_NONE;
502                 }
503
504                 /* resizing */
505                 if (width > height) {
506                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
507                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
508                 } else {
509                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
510                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
511                 }
512
513                 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
514
515         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
516                 media_svc_debug("type [png]");
517         } else {
518                 media_svc_debug("Not proper img format");
519         }
520
521         return ret;
522 }
523
524 static int __media_svc_safe_atoi(char *buffer, int *si)
525 {
526         char *end = NULL;
527         errno = 0;
528         media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
529
530         const long sl = strtol(buffer, &end, 10);
531
532         media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
533         media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
534         media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
535         media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
536         media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
537
538         *si = (int)sl;
539
540         return MS_MEDIA_ERR_NONE;
541 }
542
543 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
544 {
545         int ret = MS_MEDIA_ERR_NONE;
546
547         media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
548
549         if (!image) {
550                 media_svc_error("invalid image..");
551                 return MS_MEDIA_ERR_INVALID_PARAMETER;
552         }
553
554         struct statfs fs;
555         char *thumb_path = NULL;
556         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
557         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
558
559         if (-1 == statfs(thumb_path, &fs)) {
560                 media_svc_error("error in statfs");
561                 SAFE_FREE(thumb_path);
562                 return MS_MEDIA_ERR_INTERNAL;
563         }
564
565         SAFE_FREE(thumb_path);
566
567         long bsize_kbytes = fs.f_bsize >> 10;
568
569         if ((bsize_kbytes * fs.f_bavail) < 1024) {
570                 media_svc_error("not enought space...");
571                 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
572         }
573
574         FILE *fp = NULL;
575         int nwrite = -1;
576         if (image != NULL && size > 0) {
577                 fp = fopen(image_path, "w");
578
579                 if (fp == NULL) {
580                         media_svc_error("failed to open file");
581                         return MS_MEDIA_ERR_INTERNAL;
582                 }
583
584                 nwrite = fwrite(image, 1, size, fp);
585                 if (nwrite != size) {
586                         media_svc_error("failed to write thumbnail");
587                         fclose(fp);
588                         return MS_MEDIA_ERR_INTERNAL;
589                 }
590                 fclose(fp);
591         }
592
593         return MS_MEDIA_ERR_NONE;
594 }
595
596 static char *__media_svc_get_title_from_filepath(const char *path)
597 {
598         char *filename = NULL;
599         char *title = NULL;
600         char    *ext = NULL;
601         int filename_len = -1;
602         int new_title_len = -1;
603
604         if (!path) {
605                 media_svc_error("path is NULL");
606                 return NULL;
607         }
608
609         filename = g_path_get_basename(path);
610         if (!STRING_VALID(filename)) {
611                 media_svc_error("wrong file name");
612                 SAFE_FREE(filename);
613                 return NULL;
614         }
615
616         filename_len = strlen(filename);
617
618         ext = g_strrstr(filename, ".");
619         if (!ext) {
620                 media_svc_error("there is no file extention");
621                 return filename;
622         }
623
624         new_title_len = filename_len - strlen(ext);
625         if (new_title_len < 1) {
626                 media_svc_error("title length is zero");
627                 SAFE_FREE(filename);
628                 return NULL;
629         }
630
631         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
632
633         SAFE_FREE(filename);
634
635         media_svc_debug("extract title is [%s]", title);
636
637         return title;
638 }
639
640 void _media_svc_remove_file(const char *path)
641 {
642         if (!STRING_VALID(path))
643                 return;
644
645         if (remove(path) != 0)
646                 media_svc_stderror("fail to remove file result");
647 }
648
649 int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
650 {
651         int ret = MS_MEDIA_ERR_NONE;
652         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
653         char hash[255 + 1] = {0, };
654         char *thumb_dir = NULL;
655         char *thumbfile_ext = NULL;
656
657         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
658         if (!STRING_VALID(thumb_dir)) {
659                 media_svc_error("ms_user_get_root_thumb_store_path failed");
660                 return MS_MEDIA_ERR_INTERNAL;
661         }
662
663         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
664                 media_svc_error("Wrong path[%s]", thumb_dir);
665                 SAFE_FREE(thumb_dir);
666                 return MS_MEDIA_ERR_INTERNAL;
667         }
668
669         memset(file_ext, 0, sizeof(file_ext));
670         if (!__media_svc_get_file_ext(pathname, file_ext))
671                 media_svc_error("get file ext fail");
672
673         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
674         if (ret != MS_MEDIA_ERR_NONE) {
675                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
676                 SAFE_FREE(thumb_dir);
677                 return MS_MEDIA_ERR_INTERNAL;
678         }
679
680         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
681                 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
682                         thumbfile_ext = (char *)"jpg";
683                 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
684                         thumbfile_ext = (char *)"png";
685                 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
686                         thumbfile_ext = (char *)"gif";
687                 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
688                         thumbfile_ext = (char *)"bmp";
689                 } else {
690                         media_svc_error("Not proper img format");
691                         SAFE_FREE(thumb_dir);
692                         return MS_MEDIA_ERR_INTERNAL;
693                 }
694
695                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
696         } else {
697                 if (strcasecmp(file_ext, "PNG") == 0)
698                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
699                 else
700                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
701         }
702
703         SAFE_FREE(thumb_dir);
704
705         return MS_MEDIA_ERR_NONE;
706 }
707
708 int _media_svc_get_file_time(const char *full_path)
709 {
710         struct stat statbuf;
711         int fd = 0;
712
713         memset(&statbuf, 0, sizeof(struct stat));
714         fd = stat(full_path, &statbuf);
715         if (fd == -1) {
716                 media_svc_sec_error("stat(%s) fails.", full_path);
717                 return MS_MEDIA_ERR_INTERNAL;
718         }
719
720         return statbuf.st_mtime;
721 }
722
723 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
724 {
725         /* Set default GPS value before extracting meta information */
726         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
727         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
728         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
729
730         /* Set filename to title for all media */
731         char *title = NULL;
732         title = __media_svc_get_title_from_filepath(content_info->path);
733         if (title) {
734                 content_info->media_meta.title = g_strdup(title);
735                 SAFE_FREE(title);
736                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
737         } else {
738                 media_svc_error("Can't extract title");
739                 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
740                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
741         }
742
743         /* Set default value before extracting meta information */
744         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
745         media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
746
747         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
748         media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
749
750         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
751         media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
752
753         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
754         media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
755
756         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
757         media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
758
759         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
760         media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
761
762         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
763         media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
764
765         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
766         media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
767
768         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
769         media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
770
771         if (refresh) {
772                 media_svc_debug("refresh");
773                 return MS_MEDIA_ERR_NONE;
774         }
775
776         content_info->favourate = 0;
777         content_info->media_meta.rating = 0;
778
779         return MS_MEDIA_ERR_NONE;
780 }
781
782 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type, const char *path, bool refresh)
783 {
784         int ret = MS_MEDIA_ERR_NONE;
785         char * media_uuid = NULL;
786         bool drm_type = false;
787         char mime_type[256] = {0, };
788         media_svc_media_type_e media_type;
789
790         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
791         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
792
793         content_info->path = g_strdup(path);
794         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
795
796         struct stat st;
797         memset(&st, 0, sizeof(struct stat));
798         if (stat(path, &st) == 0) {
799                 content_info->modified_time = st.st_mtime;
800                 content_info->timeline = content_info->modified_time;
801                 content_info->size = st.st_size;
802         } else {
803                 media_svc_stderror("stat failed");
804         }
805
806         _media_svc_set_default_value(content_info, refresh);
807
808         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
809         if (refresh) {
810                 media_svc_debug("refresh");
811                 return MS_MEDIA_ERR_NONE;
812         }
813
814         content_info->storage_uuid = g_strdup(storage_id);
815         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
816
817         content_info->storage_type = storage_type;
818         time(&content_info->added_time);
819
820         media_uuid = _media_info_generate_uuid();
821         if (media_uuid == NULL) {
822                 _media_svc_destroy_content_info(content_info);
823                 return MS_MEDIA_ERR_INTERNAL;
824         }
825
826         content_info->media_uuid = g_strdup(media_uuid);
827         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
828
829         content_info->file_name = g_path_get_basename(path);
830         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
831
832         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
833         if drm_contentinfo is not NULL, the file is OMA DRM.*/
834         ret = __media_svc_get_mime_type(path, mime_type);
835         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
836
837         media_svc_debug("mime [%s]", mime_type);
838         content_info->is_drm = drm_type;
839
840         ret = __media_svc_get_media_type(path, mime_type, &media_type);
841         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
842
843         content_info->mime_type = g_strdup(mime_type);
844         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
845
846         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
847
848         content_info->media_type = media_type;
849
850         return MS_MEDIA_ERR_NONE;
851 }
852
853 int image_360_check(char *path)
854 {
855         FILE *fp = NULL;
856         long app1_size = 0;
857         int size = 1;
858         unsigned char exif_header[4] = {0, };
859         unsigned char exif_app1[2] = {0, };
860         unsigned char exif_app1_xmp[2] = {0, };
861         long exif_app1_xmp_size = 0;
862         unsigned char exif_app1_xmp_t[2] = {0, };
863         char *xmp_data = NULL;
864         int size1 = 0;
865         int size2 = 0;
866         int fdata = 0;
867         int temp = 0;
868
869         memset(exif_header, 0x00, sizeof(exif_header));
870         memset(exif_app1, 0x00, sizeof(exif_app1));
871         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
872         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
873
874         fp = fopen(path, "rb");
875         if (fp == NULL)
876                 goto ERROR;
877
878         size = fread(exif_header, 1, sizeof(exif_header), fp);
879         if (size <= 0)
880                 goto ERROR;
881
882         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
883                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
884                 if (size <= 0)
885                         goto ERROR;
886
887                 size1 = exif_app1[0];
888                 size2 = exif_app1[1];
889
890                 app1_size = size1 * 256 + size2 - 2;
891
892                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
893                         goto ERROR;
894
895                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
896                 if (size <= 0)
897                         goto ERROR;
898
899                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
900                         int result = 0;
901                         char *ptr = NULL;
902                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
903                         if (size <= 0)
904                                 goto ERROR;
905
906                         size1 = exif_app1_xmp_t[0];
907                         size2 = exif_app1_xmp_t[1];
908
909                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
910
911                         if (exif_app1_xmp_size > 0) {
912                                 xmp_data = (char *)malloc(exif_app1_xmp_size);
913                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
914
915                                 ptr = xmp_data;
916
917                                 while (exif_app1_xmp_size >= 0) {
918                                         exif_app1_xmp_size--;
919                                         fdata = fgetc(fp);
920                                         if (fdata == EOF)
921                                                 continue;
922                                         if (fdata == '\0')
923                                                 continue;
924                                         *ptr = (char)fdata;
925                                         ptr++;
926                                         temp++;
927                                 }
928                                 ptr = ptr - temp;
929
930                                 if (strstr(ptr, "UsePanoramaViewer")
931                                 && strstr(ptr, "True")
932                                 && strstr(ptr, "ProjectionType")
933                                 && strstr(ptr, "equirectangular"))
934                                         result = 1;
935
936                                 SAFE_FREE(xmp_data);
937                         } else {
938                                 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
939                         }
940
941                         if (fp) {
942                                 fclose(fp);
943                                 fp = NULL;
944                         }
945                         return result;
946                 } else {
947                         goto ERROR;
948                 }
949         } else {
950                 goto ERROR;
951         }
952 ERROR:
953         if (fp) {
954                 fclose(fp);
955                 fp = NULL;
956         }
957         return 0;
958 }
959
960 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
961 {
962         double value = 0.0;
963         int orient_value = 0;
964         int exif_width = 0;
965         int exif_height = 0;
966         ExifData *ed = NULL;
967         bool has_datetaken = false;
968         double fnumber = 0.0;
969         int iso = 0;
970         char *path = NULL;
971
972         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
973
974         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
975         media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type [%d]", content_info->media_type);
976         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
977
978         path = content_info->path;
979
980         /* Load an ExifData object from an EXIF file */
981         ed = exif_data_new_from_file(path);
982
983         if (!ed) {
984                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
985                 goto GET_WIDTH_HEIGHT;
986         }
987
988         content_info->media_meta.is_360 = image_360_check(path);
989
990         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
991         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
992         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
993         content_info->media_meta.fnumber = 0.0;
994         content_info->media_meta.iso = 0;
995         content_info->media_meta.orientation = 0;
996         content_info->media_meta.width = 0;
997         content_info->media_meta.height = 0;
998
999         memset(buf, 0x00, sizeof(buf));
1000         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1001                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1002                         if (strlen(buf) > 0) {
1003                                 if (strcmp(buf, "S") == 0)
1004                                         value = -1 * value;
1005                         }
1006                         content_info->media_meta.latitude = value;
1007                 }
1008         }
1009
1010         memset(buf, 0x00, sizeof(buf));
1011         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1012                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1013                         if (strlen(buf) > 0) {
1014                                 if (strcmp(buf, "W") == 0)
1015                                         value = -1 * value;
1016                         }
1017                         content_info->media_meta.longitude = value;
1018                 }
1019         }
1020
1021         memset(buf, 0x00, sizeof(buf));
1022         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1023                 if (strlen(buf) > 0) {
1024                         SAFE_FREE(content_info->media_meta.description);
1025                         content_info->media_meta.description = g_strdup(buf);
1026                 }
1027         }
1028
1029         memset(buf, 0x00, sizeof(buf));
1030         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1031                 if (strlen(buf) > 0) {
1032                         has_datetaken = true;
1033                         content_info->media_meta.datetaken = g_strdup(buf);
1034
1035                         /* This is same as recorded_date */
1036                         content_info->media_meta.recorded_date = g_strdup(buf);
1037                 }
1038         }
1039
1040         memset(buf, 0x00, sizeof(buf));
1041         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1042                 if (strlen(buf) > 0) {
1043                         has_datetaken = true;
1044                         content_info->media_meta.datetaken = g_strdup(buf);
1045
1046                         /* This is same as recorded_date */
1047                         content_info->media_meta.recorded_date =  g_strdup(buf);
1048                 }
1049         }
1050
1051         if (has_datetaken) {
1052                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1053                 if (content_info->timeline == 0)
1054                         content_info->timeline = content_info->modified_time;
1055                 else
1056                         media_svc_debug("Timeline : %ld", content_info->timeline);
1057         }
1058
1059         memset(buf, 0x00, sizeof(buf));
1060         /* Get exposure_time value from exif. */
1061         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1062                 if (strlen(buf) > 0)
1063                         content_info->media_meta.exposure_time = g_strdup(buf);
1064         }
1065
1066         /* Get fnumber value from exif. */
1067         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1068                 if (fnumber > 0.0)
1069                         content_info->media_meta.fnumber = fnumber;
1070         }
1071
1072         /* Get iso value from exif. */
1073         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1074                 if (iso > 0)
1075                         content_info->media_meta.iso = iso;
1076         }
1077
1078         memset(buf, 0x00, sizeof(buf));
1079         /* Get model value from exif. */
1080         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1081                 if (strlen(buf) > 0)
1082                         content_info->media_meta.model = g_strdup(buf);
1083         }
1084
1085         /* Get orientation value from exif. */
1086         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1087                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1088                         content_info->media_meta.orientation = orient_value;
1089         }
1090
1091         /* Get width value from exif. */
1092         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1093                 if (exif_width > 0)
1094                         content_info->media_meta.width = exif_width;
1095         }
1096
1097         /* Get height value from exif. */
1098         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1099                 if (exif_height > 0)
1100                         content_info->media_meta.height = exif_height;
1101         }
1102
1103         if (ed != NULL) exif_data_unref(ed);
1104
1105 GET_WIDTH_HEIGHT:
1106
1107         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1108                 /*Get image width, height*/
1109                 unsigned int img_width = 0;
1110                 unsigned int img_height = 0;
1111                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1112
1113                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1114                 if (content_info->media_meta.width == 0)
1115                         content_info->media_meta.width = img_width;
1116
1117                 if (content_info->media_meta.height == 0)
1118                         content_info->media_meta.height = img_height;
1119         }
1120
1121         return MS_MEDIA_ERR_NONE;
1122 }
1123
1124 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1125 {
1126         MMHandleType tag = 0;
1127         char *p = NULL;
1128         int size = -1;
1129         int mmf_error = FILEINFO_ERROR_NONE;
1130
1131         content_info->path = g_strdup(path);
1132
1133         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1134         if (mmf_error != FILEINFO_ERROR_NONE)
1135                 return MS_MEDIA_ERR_INTERNAL;
1136
1137         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1138         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1139                 content_info->media_meta.album = g_strdup(p);
1140
1141         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1142         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1143                 content_info->media_meta.artist = g_strdup(p);
1144
1145         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1146         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1147                 content_info->media_meta.album_artist = g_strdup(p);
1148
1149         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_GENRE, &p, &size, NULL);
1150         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1151                 content_info->media_meta.genre = g_strdup(p);
1152
1153         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
1154         if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1155                 if (!isspace(*p)) {
1156                         content_info->media_meta.title = g_strdup(p);
1157                 } else {
1158                         int idx = 0;
1159
1160                         for (idx = 0; idx < size; idx++) {
1161                                 if (isspace(*p)) {
1162                                         media_svc_debug("SPACE [%s]", p);
1163                                         p++;
1164                                         continue;
1165                                 } else {
1166                                         media_svc_debug("Not SPACE [%s]", p);
1167                                         content_info->media_meta.title = g_strdup(p);
1168                                         break;
1169                                 }
1170                         }
1171                 }
1172         }
1173
1174         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1175         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1176                 content_info->media_meta.description = g_strdup(p);
1177
1178         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1179         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1180                 content_info->media_meta.composer = g_strdup(p);
1181
1182         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1183         if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1184                 content_info->media_meta.copyright = g_strdup(p);
1185
1186         mmf_error = mm_file_destroy_tag_attrs(tag);
1187         if (mmf_error != FILEINFO_ERROR_NONE)
1188                 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1189
1190         return MS_MEDIA_ERR_NONE;
1191 }
1192
1193 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1194 {
1195         MMHandleType content = 0;
1196         MMHandleType tag = 0;
1197         char *p = NULL;
1198         unsigned char *image = NULL;
1199         unsigned int size = 0;
1200         int mmf_error = FILEINFO_ERROR_NONE;
1201         int album_id = 0;
1202         int ret = MS_MEDIA_ERR_NONE;
1203         int cdis_value = 0;
1204
1205         /*Get Content Tag attribute ===========*/
1206         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1207
1208         if (mmf_error == FILEINFO_ERROR_NONE) {
1209                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1210                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1211                         content_info->media_meta.album = g_strdup(p);
1212
1213                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1214                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1215                         content_info->media_meta.artist = g_strdup(p);
1216
1217                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1218                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1219                         content_info->media_meta.album_artist = g_strdup(p);
1220
1221                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_GENRE, &p, &size, NULL);
1222                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1223                         content_info->media_meta.genre = g_strdup(p);
1224
1225                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
1226                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1227                         while(p && isspace(*p))
1228                                 p++;
1229
1230                         content_info->media_meta.title = g_strdup(p);
1231                 }
1232
1233                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1234                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1235                         content_info->media_meta.description = g_strdup(p);
1236
1237                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1238
1239                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1240                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1241                         char mime_type[255] = {0, };
1242                         ret = __media_svc_get_mime_type(content_info->path, mime_type);
1243                         /*if 3gp that audio only, media_type is music */
1244                         if ((ret == MS_MEDIA_ERR_NONE) &&
1245                                 ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/mp4") == 0) ||
1246                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/3gpp") == 0) ||
1247                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "video/3gpp") == 0) ||
1248                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "audio/mp4") == 0))) {
1249                                 /*Creation time format is 2013-01-01 00:00:00 +0000. change it to 2013:01:01 00:00:00 like exif time format*/
1250                                 char *time_info = g_strdup_printf("0000:00:00 00:00:00 +0000");
1251                                 char *p_value = p;
1252                                 char *time_value = time_info;
1253                                 if (time_info != NULL) {
1254                                         while (*p_value != '\0') {
1255                                                 if (*p_value == '-')
1256                                                         *time_value = ':';
1257                                                 else
1258                                                         *time_value = *p_value;
1259                                                 time_value++;
1260                                                 p_value++;
1261                                         }
1262                                         content_info->media_meta.recorded_date = g_strdup(time_info);
1263                                         SAFE_FREE(time_info);
1264                                 } else {
1265                                         media_svc_error("memory allocation error");
1266                                 }
1267                         } else {
1268                                 content_info->media_meta.recorded_date = g_strdup(p);
1269                         }
1270
1271                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1272                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1273                                 if (content_info->timeline == 0)
1274                                         content_info->timeline = content_info->modified_time;
1275
1276                                 /* This is same as datetaken */
1277                                 /* Remove compensation string */
1278                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1279                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1280                                         g_free(content_info->media_meta.recorded_date);
1281                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1282                                 } else {
1283                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1284                                 }
1285                         }
1286                 }
1287
1288                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1289                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1290                         content_info->media_meta.composer = g_strdup(p);
1291
1292                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1293                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1294                         content_info->media_meta.copyright = g_strdup(p);
1295
1296                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1297                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1298                         content_info->media_meta.track_num = g_strdup(p);
1299
1300                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1301                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1302                         int year = 0;
1303                         if ((p != NULL) && (__media_svc_safe_atoi(p, &year) == MS_MEDIA_ERR_NONE))
1304                                 content_info->media_meta.year = g_strdup(p);
1305                         else
1306                                 media_svc_debug("Wrong Year Information [%s]", p);
1307                 }
1308
1309                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1310                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1311                         int rate = 0;
1312                         if ((p != NULL) && (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE))
1313                                 content_info->media_meta.rating = rate;
1314                 } else {
1315                         content_info->media_meta.rating = 0;
1316                 }
1317
1318                 /*Do not extract artwork for the USB Storage content*/
1319                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1320                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1321                         if (mmf_error != FILEINFO_ERROR_NONE) {
1322                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1323                         } else {
1324                                 /*media_svc_debug("artwork size1 [%d]", size); */
1325                         }
1326
1327                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1328                         if (mmf_error != FILEINFO_ERROR_NONE) {
1329                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1330                         } else {
1331                                 /*media_svc_debug("artwork size2 [%d]", size); */
1332                         }
1333
1334                         if (image != NULL && size > 0) {
1335                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1336                                 int artwork_mime_size = -1;
1337
1338                                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1339                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1340                                         ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1341                                         if (ret != MS_MEDIA_ERR_NONE)
1342                                                 media_svc_error("Fail to Get Thumbnail Path");
1343
1344                                         if (strlen(thumb_path) > 0) {
1345                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1346                                                 if (ret != MS_MEDIA_ERR_NONE) {
1347                                                         media_svc_error("Fail to Save Image");
1348                                                 } else {
1349                                                         /* albumart resizing */
1350                                                         ret = __media_svc_resize_artwork(thumb_path, p);
1351                                                         if (ret != MS_MEDIA_ERR_NONE) {
1352                                                                 media_svc_error("Fail to Make Thumbnail Image");
1353                                                                 _media_svc_remove_file(thumb_path);
1354
1355                                                         } else {
1356                                                                 content_info->thumbnail_path = g_strdup(thumb_path);
1357                                                         }
1358                                                 }
1359                                         }
1360                                 }
1361                         }
1362                 }
1363
1364                 /*Initialize album_id to 0. below code will set the album_id*/
1365                 content_info->album_id = album_id;
1366                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1367                 if (ret != MS_MEDIA_ERR_NONE) {
1368                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1369                                 media_svc_debug("album does not exist. So start to make album art");
1370                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1371                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1372                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1373                                 else
1374                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1375
1376                                 content_info->album_id = album_id;
1377                         }
1378                 } else {
1379                         content_info->album_id = album_id;
1380                 }
1381
1382                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1383                         double longitude = 0.0;
1384                         double latitude = 0.0;
1385                         double altitude = 0.0;
1386
1387                         mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1388                                 MM_FILE_TAG_LATIDUE, &latitude,
1389                                 MM_FILE_TAG_ALTIDUE, &altitude,
1390                                 NULL);
1391
1392                         content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1393                         content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1394                         content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1395
1396                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1397                         if (mmf_error != FILEINFO_ERROR_NONE)
1398                                 cdis_value = 0;
1399
1400                         media_svc_debug("CDIS : %d", cdis_value);
1401
1402                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1403                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1404                                 content_info->media_meta.orientation = atoi(p);
1405                         } else {
1406                                 content_info->media_meta.orientation = 0;
1407                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1408                         }
1409                 }
1410
1411                 mmf_error = mm_file_destroy_tag_attrs(tag);
1412                 if (mmf_error != FILEINFO_ERROR_NONE)
1413                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1414         }       else {
1415                 content_info->album_id = album_id;
1416         }
1417
1418         /*Get Content attribute ===========*/
1419         if (cdis_value == 1)
1420                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1421         else
1422                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1423
1424         if (mmf_error == FILEINFO_ERROR_NONE) {
1425                 /*Common attribute*/
1426                 mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1427                 if (mmf_error != FILEINFO_ERROR_NONE) {
1428                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1429                 } else {
1430                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1431                 }
1432
1433                 /*Sound/Music attribute*/
1434                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1435
1436                         mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1437                         if (mmf_error != FILEINFO_ERROR_NONE) {
1438                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1439                         } else {
1440                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1441                         }
1442
1443                         mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1444                         if (mmf_error != FILEINFO_ERROR_NONE) {
1445                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1446                         } else {
1447                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
1448                         }
1449
1450                         mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1451                         if (mmf_error != FILEINFO_ERROR_NONE) {
1452                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1453                         } else {
1454                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
1455                         }
1456
1457                         mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1458                         if (mmf_error != FILEINFO_ERROR_NONE) {
1459                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1460                         } else {
1461                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1462                         }
1463                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1464                         int audio_bitrate = 0;
1465                         int video_bitrate = 0;
1466
1467                         mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1468                         if (mmf_error != FILEINFO_ERROR_NONE) {
1469                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1470                         } else {
1471                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
1472                         }
1473
1474                         mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1475                         if (mmf_error != FILEINFO_ERROR_NONE) {
1476                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1477                         } else {
1478                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
1479                         }
1480
1481                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1482
1483                         mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1484                         if (mmf_error != FILEINFO_ERROR_NONE) {
1485                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1486                         } else {
1487                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
1488                         }
1489
1490                         mmf_error = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1491                         if (mmf_error != FILEINFO_ERROR_NONE) {
1492                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1493                         } else {
1494                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
1495                         }
1496                 } else {
1497                         media_svc_error("Not support type");
1498                         mmf_error = mm_file_destroy_content_attrs(content);
1499                         if (mmf_error != FILEINFO_ERROR_NONE)
1500                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1501
1502                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1503                 }
1504
1505                 mmf_error = mm_file_destroy_content_attrs(content);
1506                 if (mmf_error != FILEINFO_ERROR_NONE)
1507                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1508         } else {
1509                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1510         }
1511
1512         return MS_MEDIA_ERR_NONE;
1513 }
1514
1515 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1516 {
1517         media_svc_retm_if(content_info == NULL, "content info is NULL");
1518
1519         /* Delete media_svc_content_info_s */
1520         SAFE_FREE(content_info->media_uuid);
1521         SAFE_FREE(content_info->path);
1522         SAFE_FREE(content_info->file_name);
1523         SAFE_FREE(content_info->mime_type);
1524         SAFE_FREE(content_info->folder_uuid);
1525         SAFE_FREE(content_info->thumbnail_path);
1526         SAFE_FREE(content_info->storage_uuid);
1527
1528         /* Delete media_svc_content_meta_s */
1529         SAFE_FREE(content_info->media_meta.title);
1530         SAFE_FREE(content_info->media_meta.album);
1531         SAFE_FREE(content_info->media_meta.artist);
1532         SAFE_FREE(content_info->media_meta.album_artist);
1533         SAFE_FREE(content_info->media_meta.genre);
1534         SAFE_FREE(content_info->media_meta.composer);
1535         SAFE_FREE(content_info->media_meta.year);
1536         SAFE_FREE(content_info->media_meta.recorded_date);
1537         SAFE_FREE(content_info->media_meta.copyright);
1538         SAFE_FREE(content_info->media_meta.track_num);
1539         SAFE_FREE(content_info->media_meta.description);
1540         SAFE_FREE(content_info->media_meta.datetaken);
1541         SAFE_FREE(content_info->media_meta.exposure_time);
1542         SAFE_FREE(content_info->media_meta.model);
1543
1544         SAFE_FREE(content_info->media_meta.title_pinyin);
1545         SAFE_FREE(content_info->media_meta.album_pinyin);
1546         SAFE_FREE(content_info->media_meta.artist_pinyin);
1547         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1548         SAFE_FREE(content_info->media_meta.genre_pinyin);
1549         SAFE_FREE(content_info->media_meta.composer_pinyin);
1550         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1551         SAFE_FREE(content_info->media_meta.description_pinyin);
1552 }
1553
1554 static void __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1555 {
1556         bool portrait = false;
1557         double ratio = 0.0;
1558
1559         media_svc_retm_if(!orig_w, "Invalid orig_w");
1560         media_svc_retm_if(!orig_h, "Invalid orig_h");
1561         media_svc_retm_if(!thumb_w, "Invalid thumb_w");
1562         media_svc_retm_if(!thumb_h, "Invalid thumb_h");
1563
1564         if (orig_w < orig_h)
1565                 portrait = true;
1566
1567         /* Set smaller length to default size */
1568         if (portrait) {
1569                 if (orig_w < *thumb_w)
1570                         *thumb_w = orig_w;
1571                 ratio = (double)orig_h / (double)orig_w;
1572                 *thumb_h = *thumb_w * ratio;
1573         } else {
1574                 if (orig_h < *thumb_h)
1575                         *thumb_h = orig_h;
1576                 ratio = (double)orig_w / (double)orig_h;
1577                 *thumb_w = *thumb_h * ratio;
1578         }
1579
1580         media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1581
1582         return;
1583 }
1584
1585 static void __get_rotation_and_cdis(const char *origin_path, mm_util_magick_rotate_type *rot_type, int *cdis_value)
1586 {
1587         int err = MS_MEDIA_ERR_NONE;
1588         MMHandleType tag = (MMHandleType) NULL;
1589         char *p = NULL;
1590         int size = 0;
1591         int _cdis_value = 0;
1592         mm_util_magick_rotate_type _rot_type = MM_UTIL_ROTATE_NUM;
1593
1594         /* Get Content Tag attribute for orientation */
1595         err = mm_file_create_tag_attrs(&tag, origin_path);
1596         if (err != FILEINFO_ERROR_NONE) {
1597                 *rot_type = MM_UTIL_ROTATE_0;
1598                 *cdis_value = 0;
1599                 return;
1600         }
1601
1602         err = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1603         if (err == FILEINFO_ERROR_NONE && size >= 0) {
1604                 if (p == NULL) {
1605                         _rot_type = MM_UTIL_ROTATE_0;
1606                 } else {
1607                         if (strncmp(p, "90", size) == 0)
1608                                 _rot_type = MM_UTIL_ROTATE_90;
1609                         else if (strncmp(p, "180", size) == 0)
1610                                 _rot_type = MM_UTIL_ROTATE_180;
1611                         else if (strncmp(p, "270", size) == 0)
1612                                 _rot_type = MM_UTIL_ROTATE_270;
1613                         else
1614                                 _rot_type = MM_UTIL_ROTATE_0;
1615                 }
1616                 media_svc_debug("There is tag rotate : %d", _rot_type);
1617         } else {
1618                 media_svc_debug("There is NOT tag rotate");
1619                 _rot_type = MM_UTIL_ROTATE_0;
1620         }
1621
1622         err = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &_cdis_value, NULL);
1623         if (err != FILEINFO_ERROR_NONE)
1624                 _cdis_value = 0;
1625
1626         *rot_type = _rot_type;
1627         *cdis_value = _cdis_value;
1628
1629         err = mm_file_destroy_tag_attrs(tag);
1630         if (err != FILEINFO_ERROR_NONE) {
1631                 media_svc_error("fail to free tag attr - err(%x)", err);
1632         }
1633
1634         return;
1635 }
1636
1637 static int __get_video_info(int cdis_value, const char *origin_path, int *video_track_num, unsigned int *width, unsigned int *height, void **frame, size_t *size)
1638 {
1639         int err = MS_MEDIA_ERR_NONE;
1640         MMHandleType content = (MMHandleType) NULL;
1641         int _video_track_num = 0;
1642         int _width = 0;
1643         int _height = 0;
1644         size_t _size = 0;
1645         void *_frame = NULL;
1646
1647         if (cdis_value == 1) {
1648                 media_svc_debug("This is CDIS vlaue 1");
1649                 err = mm_file_create_content_attrs_safe(&content, origin_path);
1650         } else {
1651                 err = mm_file_create_content_attrs(&content, origin_path);
1652         }
1653
1654         if (err != FILEINFO_ERROR_NONE) {
1655                 media_svc_error("mm_file_create_content_attrs fails : %d", err);
1656                 return MS_MEDIA_ERR_INTERNAL;
1657         }
1658
1659         err = mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &_video_track_num, NULL);
1660         if (err != FILEINFO_ERROR_NONE) {
1661                 media_svc_error("mm_file_get_attrs fails : %d", err);
1662                 mm_file_destroy_content_attrs(content);
1663                 return MS_MEDIA_ERR_INTERNAL;
1664         }
1665
1666         *video_track_num = _video_track_num;
1667
1668         if (_video_track_num == 0) {
1669                 mm_file_destroy_content_attrs(content);
1670                 return MS_MEDIA_ERR_NONE;
1671         }
1672
1673         err = mm_file_get_attrs(content,
1674                                 MM_FILE_CONTENT_VIDEO_WIDTH,
1675                                 &_width,
1676                                 MM_FILE_CONTENT_VIDEO_HEIGHT,
1677                                 &_height,
1678                                 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &_frame, /* raw image is RGB888 format */
1679                                 &_size, NULL);
1680
1681         if (err != FILEINFO_ERROR_NONE) {
1682                 media_svc_error("mm_file_get_attrs fails : %d", err);
1683                 mm_file_destroy_content_attrs(content);
1684                 return MS_MEDIA_ERR_INTERNAL;
1685         }
1686
1687         media_svc_debug("W[%d] H[%d] Size[%zu] Frame[%p]", _width, _height, _size, _frame);
1688         if (!_frame || !_width || !_height) {
1689                 mm_file_destroy_content_attrs(content);
1690                 return MS_MEDIA_ERR_INTERNAL;
1691         }
1692
1693
1694         *width = _width;
1695         *height = _height;
1696         *size = _size;
1697         *frame = calloc(1, _size);
1698         memcpy(*frame, _frame, _size);
1699
1700         mm_file_destroy_content_attrs(content);
1701
1702         return MS_MEDIA_ERR_NONE;
1703 }
1704
1705 static int __get_video_thumb(int width, int height, void *frame, size_t size, mm_util_magick_rotate_type rot_type, const char *thumb_path, unsigned int thumb_width, unsigned int thumb_height)
1706 {
1707         int err = MS_MEDIA_ERR_NONE;
1708         mm_util_image_h img = NULL;
1709         mm_util_image_h resize_img = NULL;
1710
1711         media_svc_retvm_if(!STRING_VALID(thumb_path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1712         __media_svc_get_proper_thumb_size(width, height, &thumb_width, &thumb_height);
1713         if (thumb_width <= 0 || thumb_height <= 0) {
1714                 media_svc_error("Failed to get thumb size");
1715                 return MS_MEDIA_ERR_INTERNAL;
1716         }
1717
1718         media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", width, height, thumb_width, thumb_height);
1719
1720         err = mm_image_create_image(width, height, MM_UTIL_COLOR_RGB24, (unsigned char *)frame, size, &img);
1721         media_svc_retvm_if(err != MM_UTIL_ERROR_NONE, err, "fail to mm_image_create_image [%d]", err);
1722
1723         if (width > thumb_width || height > thumb_height) {
1724                 if (rot_type != MM_UTIL_ROTATE_0) {
1725                         err = mm_util_resize_B_B(img, thumb_width, thumb_height, &resize_img);
1726                         if (err != MM_UTIL_ERROR_NONE)
1727                                 goto ERROR;
1728
1729                         err = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1730                 } else {
1731                         err = mm_util_resize_B_P(img, thumb_width, thumb_height, thumb_path);
1732                 }
1733         } else {
1734                 if (rot_type != MM_UTIL_ROTATE_0)
1735                         err = mm_util_rotate_B_P(img, rot_type, thumb_path);
1736                 else
1737                         err = mm_util_resize_B_P(img, width, height, thumb_path);
1738         }
1739
1740 ERROR:
1741         mm_image_destroy_image(img);
1742         mm_image_destroy_image(resize_img);
1743         if (err != MS_MEDIA_ERR_NONE)
1744                 return MS_MEDIA_ERR_INTERNAL;
1745
1746         return MS_MEDIA_ERR_NONE;
1747 }
1748
1749 static int __create_video_thumbnail(const char *path, char *thumb_path)
1750 {
1751         int ret = MS_MEDIA_ERR_NONE;
1752         int cdis_value = 0;
1753         unsigned int width = 0;
1754         unsigned int height = 0;
1755         void *frame = NULL;
1756         int video_track_num = 0;
1757         size_t size = 0;
1758         mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1759
1760         __get_rotation_and_cdis(path, &rot_type, &cdis_value);
1761         ret = __get_video_info(cdis_value, path, &video_track_num, &width, &height, &frame, &size);
1762         media_svc_retvm_if(ret != MM_UTIL_ERROR_NONE, ret, "fail to __get_video_info [%d]", ret);
1763         media_svc_retvm_if(video_track_num == 0, MM_UTIL_ERROR_NONE, "No video track");
1764
1765         ret = __get_video_thumb(width, height, frame, size, rot_type, thumb_path, THUMB_WIDTH, THUMB_HEIGHT);
1766
1767         SAFE_FREE(frame);
1768
1769         return ret;
1770 }
1771
1772 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1773 {
1774         int ret = MS_MEDIA_ERR_NONE;
1775         unsigned int origin_w = 0;
1776         unsigned int origin_h = 0;
1777         unsigned int thumb_w = THUMB_WIDTH;
1778         unsigned int thumb_h = THUMB_HEIGHT;
1779         mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1780
1781         if (path == NULL || thumb_path == NULL) {
1782                 media_svc_error("Invalid parameter");
1783                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1784         }
1785
1786         if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
1787                         media_svc_error("Original path(%s) doesn't exist.", path);
1788                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1789         }
1790
1791         ms_user_storage_type_e store_type = -1;
1792         ret = ms_user_get_storage_type(uid, path, &store_type);
1793
1794         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1795                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1796                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1797         }
1798
1799         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1800
1801         //1. make hash path
1802         ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, THUMB_EXT, uid);
1803         if (ret != MS_MEDIA_ERR_NONE) {
1804                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1805                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1806                 return ret;
1807         }
1808
1809         //2. save thumbnail
1810         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1811                 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1812                 if (ret != MS_MEDIA_ERR_NONE) {
1813                         media_svc_error("Getting image info is failed err: %d", ret);
1814                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1815                         return MS_MEDIA_ERR_INTERNAL;
1816                 }
1817
1818                 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1819                         media_svc_error("Unsupported image codec");
1820                         return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1821                 }
1822
1823                 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1824                         media_svc_error("This original image is too big");
1825                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1826                         return MS_MEDIA_ERR_THUMB_TOO_BIG;
1827                 }
1828
1829                 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1830                 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1831                 if (ret != MM_UTIL_ERROR_NONE) {
1832                         media_svc_error("mm_util_resize_P_P err: %d", ret);
1833                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1834                         return MS_MEDIA_ERR_INTERNAL;
1835                 }
1836         } else {
1837                 return __create_video_thumbnail(path, thumb_path);
1838         }
1839
1840         return ret;
1841 }
1842
1843 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1844 {
1845         int ret = MS_MEDIA_ERR_NONE;
1846         int size = 0;
1847         pinyin_name_s *pinyinname = NULL;
1848
1849         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1850         media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1851
1852         *pinyin_str = NULL;
1853
1854         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1855         if (ret == MS_MEDIA_ERR_NONE) {
1856                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1857                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1858                 else
1859                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
1860         }
1861
1862         _media_svc_pinyin_free(pinyinname, size);
1863
1864         return ret;
1865 }
1866
1867 bool _media_svc_check_pinyin_support(void)
1868 {
1869         int ret = SYSTEM_INFO_ERROR_NONE;
1870         bool is_supported = false;
1871
1872         if (media_svc_pinyin_support == -1) {
1873                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1874                 if (ret != SYSTEM_INFO_ERROR_NONE) {
1875                         media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1876                         return false;
1877                 }
1878
1879                 media_svc_pinyin_support = is_supported;
1880         }
1881
1882         return media_svc_pinyin_support;
1883 }
1884
1885 int _media_svc_get_media_type(const char *path, int *mediatype)
1886 {
1887         int ret = MS_MEDIA_ERR_NONE;
1888         char mime_type[256] = {0};
1889         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
1890
1891         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1892
1893         ret = __media_svc_get_mime_type(path, mime_type);
1894         if (ret == MS_MEDIA_ERR_NONE)
1895                 __media_svc_get_media_type(path, mime_type, &media_type);
1896         else
1897                 media_svc_error("__media_svc_get_mime_type failed");
1898
1899         *mediatype = media_type;
1900
1901         return ret;
1902 }
1903
1904 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1905 {
1906         if ((storage_type != MS_USER_STORAGE_INTERNAL)
1907                 && (storage_type != MS_USER_STORAGE_EXTERNAL)
1908                 && (storage_type != MS_USER_STORAGE_EXTERNAL_USB)) {
1909                 media_svc_error("storage type is incorrect[%d]", storage_type);
1910                 return false;
1911         }
1912
1913         return true;
1914 }