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