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