Check input parmeters
[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         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
454
455         /*in case of normal files or failure to get mime in drm */
456         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
457                 media_svc_error("aul_get_mime_from_file fail");
458                 return MS_MEDIA_ERR_INTERNAL;
459         }
460
461         return MS_MEDIA_ERR_NONE;
462 }
463
464 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
465 {
466         int i = 0;
467
468         for (i = strlen(file_path); i >= 0; i--) {
469                 if (file_path[i] == '.') {
470                         SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
471                         return true;
472                 }
473
474                 if (file_path[i] == '/')
475                         return false;
476         }
477         return false;
478 }
479
480 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
481 {
482         char *err_attr_name = NULL;
483         double gps_value = 0.0;
484         int mmf_error = FILEINFO_ERROR_NONE;
485
486         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
487         if (mmf_error == FILEINFO_ERROR_NONE) {
488                 if (longitude != NULL)
489                         *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
490         } else {
491                 SAFE_FREE(err_attr_name);
492         }
493
494         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
495         if (mmf_error == FILEINFO_ERROR_NONE) {
496                 if (latitude != NULL)
497                         *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
498         } else {
499                 SAFE_FREE(err_attr_name);
500         }
501
502         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
503         if (mmf_error == FILEINFO_ERROR_NONE) {
504                 if (altitude != NULL)
505                         *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
506         } else {
507                 SAFE_FREE(err_attr_name);
508         }
509
510         return MS_MEDIA_ERR_NONE;
511 }
512
513 static int __media_svc_resize_artwork(const char *path, const char *img_format)
514 {
515         int ret = MS_MEDIA_ERR_NONE;
516         unsigned int width = 0;
517         unsigned int height = 0;
518         unsigned int resized_width = 0;
519         unsigned int resized_height = 0;
520         mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
521
522         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
523                 media_svc_debug("type [jpeg]");
524
525                 mm_util_extract_image_info(path, &img_type, &width, &height);
526
527                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
528                         media_svc_debug("No need resizing");
529                         return MS_MEDIA_ERR_NONE;
530                 }
531
532                 /* resizing */
533                 if (width > height) {
534                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
535                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
536                 } else {
537                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
538                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
539                 }
540
541                 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
542
543         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
544                 media_svc_debug("type [png]");
545         } else {
546                 media_svc_debug("Not proper img format");
547         }
548
549         return ret;
550 }
551
552 static int __media_svc_safe_atoi(char *buffer, int *si)
553 {
554         char *end = NULL;
555         errno = 0;
556         media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
557
558         const long sl = strtol(buffer, &end, 10);
559
560         media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
561         media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
562         media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
563         media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
564         media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
565
566         *si = (int)sl;
567
568         return MS_MEDIA_ERR_NONE;
569 }
570
571 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
572 {
573         int ret = MS_MEDIA_ERR_NONE;
574
575         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
576
577         if (!image) {
578                 media_svc_error("invalid image..");
579                 return MS_MEDIA_ERR_INVALID_PARAMETER;
580         }
581
582         struct statfs fs;
583         char *thumb_path = NULL;
584         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
585         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
586
587         if (-1 == statfs(thumb_path, &fs)) {
588                 media_svc_error("error in statfs");
589                 SAFE_FREE(thumb_path);
590                 return MS_MEDIA_ERR_INTERNAL;
591         }
592
593         SAFE_FREE(thumb_path);
594
595         long bsize_kbytes = fs.f_bsize >> 10;
596
597         if ((bsize_kbytes * fs.f_bavail) < 1024) {
598                 media_svc_error("not enought space...");
599                 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
600         }
601
602         FILE *fp = NULL;
603         int nwrite = -1;
604         if (image != NULL && size > 0) {
605                 fp = fopen(image_path, "w");
606
607                 if (fp == NULL) {
608                         media_svc_error("failed to open file");
609                         return MS_MEDIA_ERR_INTERNAL;
610                 }
611
612                 nwrite = fwrite(image, 1, size, fp);
613                 if (nwrite != size) {
614                         media_svc_error("failed to write thumbnail");
615                         fclose(fp);
616                         return MS_MEDIA_ERR_INTERNAL;
617                 }
618                 fclose(fp);
619         }
620
621         return MS_MEDIA_ERR_NONE;
622 }
623
624 static char *__media_svc_get_title_from_filepath(const char *path)
625 {
626         char *filename = NULL;
627         char *title = NULL;
628         char    *ext = NULL;
629         int filename_len = -1;
630         int new_title_len = -1;
631
632         if (!path) {
633                 media_svc_error("path is NULL");
634                 return NULL;
635         }
636
637         filename = g_path_get_basename(path);
638         if (!STRING_VALID(filename)) {
639                 media_svc_error("wrong file name");
640                 SAFE_FREE(filename);
641                 return NULL;
642         }
643
644         filename_len = strlen(filename);
645
646         ext = g_strrstr(filename, ".");
647         if (!ext) {
648                 media_svc_error("there is no file extention");
649                 return filename;
650         }
651
652         new_title_len = filename_len - strlen(ext);
653         if (new_title_len < 1) {
654                 media_svc_error("title length is zero");
655                 SAFE_FREE(filename);
656                 return NULL;
657         }
658
659         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
660
661         SAFE_FREE(filename);
662
663         media_svc_debug("extract title is [%s]", title);
664
665         return title;
666 }
667
668 int _media_svc_remove_file(const char *path)
669 {
670         int result = -1;
671
672         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
673
674         result = remove(path);
675         if (result == 0) {
676                 media_svc_debug("success to remove file");
677                 return MS_MEDIA_ERR_NONE;
678         } else {
679                 media_svc_stderror("fail to remove file result");
680                 return MS_MEDIA_ERR_INTERNAL;
681         }
682 }
683
684 int _media_svc_get_thumbnail_path(media_svc_media_type_e media_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
685 {
686         int ret = MS_MEDIA_ERR_NONE;
687         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
688         char hash[255 + 1] = {0, };
689         char *thumb_dir = NULL;
690         char *thumbfile_ext = NULL;
691
692         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
693         if (!STRING_VALID(thumb_dir)) {
694                 media_svc_error("ms_user_get_root_thumb_store_path failed");
695                 return MS_MEDIA_ERR_INTERNAL;
696         }
697
698         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
699                 media_svc_error("Wrong path[%s]", thumb_dir);
700                 SAFE_FREE(thumb_dir);
701                 return MS_MEDIA_ERR_INTERNAL;
702         }
703
704         memset(file_ext, 0, sizeof(file_ext));
705         if (!__media_svc_get_file_ext(pathname, file_ext))
706                 media_svc_error("get file ext fail");
707
708         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
709         if (ret != MS_MEDIA_ERR_NONE) {
710                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
711                 SAFE_FREE(thumb_dir);
712                 return MS_MEDIA_ERR_INTERNAL;
713         }
714
715         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
716                 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
717                         thumbfile_ext = (char *)"jpg";
718                 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
719                         thumbfile_ext = (char *)"png";
720                 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
721                         thumbfile_ext = (char *)"gif";
722                 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
723                         thumbfile_ext = (char *)"bmp";
724                 } else {
725                         media_svc_error("Not proper img format");
726                         SAFE_FREE(thumb_dir);
727                         return MS_MEDIA_ERR_INTERNAL;
728                 }
729
730                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
731         } else {
732                 if (strcasecmp(file_ext, "PNG") == 0)
733                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
734                 else
735                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
736         }
737
738         SAFE_FREE(thumb_dir);
739
740         return MS_MEDIA_ERR_NONE;
741 }
742
743 int _media_svc_get_file_time(const char *full_path)
744 {
745         struct stat statbuf;
746         int fd = 0;
747
748         memset(&statbuf, 0, sizeof(struct stat));
749         fd = stat(full_path, &statbuf);
750         if (fd == -1) {
751                 media_svc_error("stat(%s) fails.", full_path);
752                 return MS_MEDIA_ERR_INTERNAL;
753         }
754
755         return statbuf.st_mtime;
756 }
757
758 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
759 {
760         /* Set default GPS value before extracting meta information */
761         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
762         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
763         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
764
765         /* Set filename to title for all media */
766         char *title = NULL;
767         title = __media_svc_get_title_from_filepath(content_info->path);
768         if (title) {
769                 content_info->media_meta.title = g_strdup(title);
770                 SAFE_FREE(title);
771                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
772         } else {
773                 media_svc_error("Can't extract title");
774                 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
775                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
776         }
777
778         /* Set default value before extracting meta information */
779         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
780         media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
781
782         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
783         media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
784
785         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
786         media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
787
788         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
789         media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
790
791         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
792         media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
793
794         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
795         media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
796
797         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
798         media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
799
800         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
801         media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
802
803         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
804         media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
805
806         if (refresh) {
807                 media_svc_debug("refresh");
808                 return MS_MEDIA_ERR_NONE;
809         }
810
811         content_info->favourate = 0;
812         content_info->media_meta.rating = 0;
813
814         return MS_MEDIA_ERR_NONE;
815 }
816
817 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, ms_user_storage_type_e storage_type,
818                         const char *path, media_svc_media_type_e *media_type, bool refresh)
819 {
820         int ret = MS_MEDIA_ERR_NONE;
821         char * media_uuid = NULL;
822         bool drm_type = false;
823         char mime_type[256] = {0, };
824
825         media_svc_retvm_if(_media_svc_check_storage_type(storage_type) != TRUE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
826         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
827
828         content_info->path = g_strdup(path);
829         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
830
831         struct stat st;
832         memset(&st, 0, sizeof(struct stat));
833         if (stat(path, &st) == 0) {
834                 content_info->modified_time = st.st_mtime;
835                 content_info->timeline = content_info->modified_time;
836                 content_info->size = st.st_size;
837         } else {
838                 media_svc_stderror("stat failed");
839         }
840
841         _media_svc_set_default_value(content_info, refresh);
842
843         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
844         if (refresh) {
845                 media_svc_debug("refresh");
846                 return MS_MEDIA_ERR_NONE;
847         }
848
849         content_info->storage_uuid = g_strdup(storage_id);
850         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
851
852         content_info->storage_type = storage_type;
853         time(&content_info->added_time);
854
855         media_uuid = _media_info_generate_uuid();
856         if (media_uuid == NULL) {
857                 _media_svc_destroy_content_info(content_info);
858                 return MS_MEDIA_ERR_INTERNAL;
859         }
860
861         content_info->media_uuid = g_strdup(media_uuid);
862         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
863
864         content_info->file_name = g_path_get_basename(path);
865         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
866
867         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
868         if drm_contentinfo is not NULL, the file is OMA DRM.*/
869         ret = __media_svc_get_mime_type(path, mime_type);
870         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
871
872         media_svc_debug("mime [%s]", mime_type);
873         content_info->is_drm = drm_type;
874
875         ret = __media_svc_get_media_type(path, mime_type, media_type);
876         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
877         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
878                 media_svc_error("invalid media_type condition[%d]", *media_type);
879                 return MS_MEDIA_ERR_INVALID_PARAMETER;
880         }
881
882         content_info->mime_type = g_strdup(mime_type);
883         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
884
885         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
886
887         content_info->media_type = *media_type;
888
889         return MS_MEDIA_ERR_NONE;
890 }
891
892 int image_360_check(char *path)
893 {
894         FILE *fp = NULL;
895         long app1_size = 0;
896         int size = 1;
897         unsigned char exif_header[4] = {0, };
898         unsigned char exif_app1[2] = {0, };
899         unsigned char exif_app1_xmp[2] = {0, };
900         long exif_app1_xmp_size = 0;
901         unsigned char exif_app1_xmp_t[2] = {0, };
902         char *xmp_data = NULL;
903         int size1 = 0;
904         int size2 = 0;
905         int fdata = 0;
906         int temp = 0;
907
908         memset(exif_header, 0x00, sizeof(exif_header));
909         memset(exif_app1, 0x00, sizeof(exif_app1));
910         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
911         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
912
913         fp = fopen(path, "rb");
914         if (fp == NULL)
915                 goto ERROR;
916
917         size = fread(exif_header, 1, sizeof(exif_header), fp);
918         if (size <= 0)
919                 goto ERROR;
920
921         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
922                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
923                 if (size <= 0)
924                         goto ERROR;
925
926                 size1 = exif_app1[0];
927                 size2 = exif_app1[1];
928
929                 app1_size = size1 * 256 + size2 - 2;
930
931                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
932                         goto ERROR;
933
934                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
935                 if (size <= 0)
936                         goto ERROR;
937
938                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
939                         int result = 0;
940                         char *ptr = NULL;
941                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
942                         if (size <= 0)
943                                 goto ERROR;
944
945                         size1 = exif_app1_xmp_t[0];
946                         size2 = exif_app1_xmp_t[1];
947
948                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
949
950                         if (exif_app1_xmp_size > 0) {
951                                 xmp_data = (char *)malloc(exif_app1_xmp_size);
952                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
953
954                                 ptr = xmp_data;
955
956                                 while (exif_app1_xmp_size >= 0) {
957                                         exif_app1_xmp_size--;
958                                         fdata = fgetc(fp);
959                                         if (fdata == EOF)
960                                                 continue;
961                                         if (fdata == '\0')
962                                                 continue;
963                                         *ptr = (char)fdata;
964                                         ptr++;
965                                         temp++;
966                                 }
967                                 ptr = ptr - temp;
968
969                                 if (strstr(ptr, "UsePanoramaViewer")
970                                 && strstr(ptr, "True")
971                                 && strstr(ptr, "ProjectionType")
972                                 && strstr(ptr, "equirectangular"))
973                                         result = 1;
974
975                                 SAFE_FREE(xmp_data);
976                         } else {
977                                 media_svc_error("invalid exif_app1_xmp_size [%ld]", exif_app1_xmp_size);
978                         }
979
980                         if (fp) {
981                                 fclose(fp);
982                                 fp = NULL;
983                         }
984                         return result;
985                 } else {
986                         goto ERROR;
987                 }
988         } else {
989                 goto ERROR;
990         }
991 ERROR:
992         if (fp) {
993                 fclose(fp);
994                 fp = NULL;
995         }
996         return 0;
997 }
998
999 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
1000 {
1001         double value = 0.0;
1002         int orient_value = 0;
1003         int exif_width = 0;
1004         int exif_height = 0;
1005         ExifData *ed = NULL;
1006         int has_datetaken = FALSE;
1007         double fnumber = 0.0;
1008         int iso = 0;
1009         char *path = NULL;
1010
1011         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1012
1013         memset(buf, 0x00, sizeof(buf));
1014
1015         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1016                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1017                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1018         }
1019
1020         path = content_info->path;
1021         if (!STRING_VALID(path)) {
1022                 media_svc_error("Invalid Path");
1023                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1024         }
1025
1026         /* Load an ExifData object from an EXIF file */
1027         ed = exif_data_new_from_file(path);
1028
1029         if (!ed) {
1030                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1031                 goto GET_WIDTH_HEIGHT;
1032         }
1033
1034         content_info->media_meta.is_360 = image_360_check(path);
1035
1036         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1037                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1038                         if (strlen(buf) > 0) {
1039                                 if (strcmp(buf, "S") == 0)
1040                                         value = -1 * value;
1041                         }
1042                         content_info->media_meta.latitude = value;
1043                 } else {
1044                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1045                 }
1046         } else {
1047                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1048         }
1049
1050         memset(buf, 0x00, sizeof(buf));
1051
1052         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1053                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1054                         if (strlen(buf) > 0) {
1055                                 if (strcmp(buf, "W") == 0)
1056                                         value = -1 * value;
1057                         }
1058                         content_info->media_meta.longitude = value;
1059                 } else {
1060                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1061                 }
1062         } else {
1063                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1064         }
1065
1066         memset(buf, 0x00, sizeof(buf));
1067
1068         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1069                 if (strlen(buf) == 0)
1070                         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1071                 else
1072                         content_info->media_meta.description = g_strdup(buf);
1073         } else {
1074                 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1075         }
1076
1077         memset(buf, 0x00, sizeof(buf));
1078
1079         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1080                 if (strlen(buf) > 0) {
1081                         has_datetaken = TRUE;
1082                         content_info->media_meta.datetaken = g_strdup(buf);
1083
1084                         /* This is same as recorded_date */
1085                         content_info->media_meta.recorded_date = g_strdup(buf);
1086                 }
1087         }
1088
1089         memset(buf, 0x00, sizeof(buf));
1090
1091         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1092                 if (strlen(buf) > 0) {
1093                         has_datetaken = TRUE;
1094                         content_info->media_meta.datetaken = g_strdup(buf);
1095
1096                         /* This is same as recorded_date */
1097                         content_info->media_meta.recorded_date =  g_strdup(buf);
1098                 }
1099         }
1100
1101         if (has_datetaken) {
1102                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1103                 if (content_info->timeline == 0)
1104                         content_info->timeline = content_info->modified_time;
1105                 else
1106                         media_svc_debug("Timeline : %ld", content_info->timeline);
1107         }
1108
1109         memset(buf, 0x00, sizeof(buf));
1110
1111         /* Get exposure_time value from exif. */
1112         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1113                 if (strlen(buf) > 0)
1114                         content_info->media_meta.exposure_time = g_strdup(buf);
1115         }
1116
1117         /* Get fnumber value from exif. */
1118         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1119                 if (fnumber > 0.0)
1120                         content_info->media_meta.fnumber = fnumber;
1121                 else
1122                         content_info->media_meta.fnumber = 0.0;
1123         } else {
1124                 content_info->media_meta.fnumber = 0.0;
1125         }
1126
1127         /* Get iso value from exif. */
1128         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1129                 if (iso > 0)
1130                         content_info->media_meta.iso = iso;
1131                 else
1132                         content_info->media_meta.iso = 0;
1133         } else {
1134                 content_info->media_meta.iso = 0;
1135         }
1136
1137         memset(buf, 0x00, sizeof(buf));
1138
1139         /* Get model value from exif. */
1140         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1141                 if (strlen(buf) > 0)
1142                         content_info->media_meta.model = g_strdup(buf);
1143         }
1144
1145         /* Get orientation value from exif. */
1146         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1147                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1148                         content_info->media_meta.orientation = orient_value;
1149                 else
1150                         content_info->media_meta.orientation = 0;
1151         } else {
1152                 content_info->media_meta.orientation = 0;
1153         }
1154
1155         /* Get width value from exif. */
1156         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1157                 if (exif_width > 0)
1158                         content_info->media_meta.width = exif_width;
1159                 else
1160                         content_info->media_meta.width = 0;
1161         } else {
1162                 content_info->media_meta.width = 0;
1163         }
1164
1165         /* Get height value from exif. */
1166         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1167                 if (exif_height > 0)
1168                         content_info->media_meta.height = exif_height;
1169                 else
1170                         content_info->media_meta.height = 0;
1171         } else {
1172                 content_info->media_meta.height = 0;
1173         }
1174
1175         if (ed != NULL) exif_data_unref(ed);
1176
1177 GET_WIDTH_HEIGHT:
1178
1179         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1180                 /*Get image width, height*/
1181                 unsigned int img_width = 0;
1182                 unsigned int img_height = 0;
1183                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1184
1185                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1186                 if (content_info->media_meta.width == 0)
1187                         content_info->media_meta.width = img_width;
1188
1189                 if (content_info->media_meta.height == 0)
1190                         content_info->media_meta.height = img_height;
1191         }
1192
1193         return MS_MEDIA_ERR_NONE;
1194 }
1195
1196 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1197 {
1198         MMHandleType tag = 0;
1199         char *p = NULL;
1200         int size = -1;
1201         int mmf_error = FILEINFO_ERROR_NONE;
1202         char *err_attr_name = NULL;
1203         int album_id = 0;
1204
1205         /*Get Content Tag attribute ===========*/
1206         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1207
1208         if (mmf_error == FILEINFO_ERROR_NONE) {
1209                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1210                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1211                         content_info->media_meta.album = g_strdup(p);
1212                 else
1213                         SAFE_FREE(err_attr_name);
1214
1215                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1216                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1217                         content_info->media_meta.artist = g_strdup(p);
1218                 else
1219                         SAFE_FREE(err_attr_name);
1220
1221                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1222                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1223                         content_info->media_meta.album_artist = g_strdup(p);
1224                 else
1225                         SAFE_FREE(err_attr_name);
1226
1227                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1228                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1229                         content_info->media_meta.genre = g_strdup(p);
1230
1231                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1232                         /*
1233                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1234                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1235                         }
1236                         */
1237                 } else {
1238                         SAFE_FREE(err_attr_name);
1239                 }
1240
1241                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1242                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1243                         if (!isspace(*p)) {
1244                                 content_info->media_meta.title = g_strdup(p);
1245                         } else {
1246                                 int idx = 0;
1247
1248                                 for (idx = 0; idx < size; idx++) {
1249                                         if (isspace(*p)) {
1250                                                 media_svc_debug("SPACE [%s]", p);
1251                                                 p++;
1252                                                 continue;
1253                                         } else {
1254                                                 media_svc_debug("Not SPACE [%s]", p);
1255                                                 content_info->media_meta.title = g_strdup(p);
1256                                                 break;
1257                                         }
1258                                 }
1259                         }
1260                 }
1261
1262                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1263                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1264                         content_info->media_meta.description = g_strdup(p);
1265                 else
1266                         SAFE_FREE(err_attr_name);
1267
1268                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1269                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1270                         content_info->media_meta.composer = g_strdup(p);
1271                 else
1272                         SAFE_FREE(err_attr_name);
1273
1274                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1275                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1276                         content_info->media_meta.copyright = g_strdup(p);
1277                 else
1278                         SAFE_FREE(err_attr_name);
1279
1280                 mmf_error = mm_file_destroy_tag_attrs(tag);
1281                 if (mmf_error != FILEINFO_ERROR_NONE)
1282                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1283         } else {
1284                 content_info->album_id = album_id;
1285         }
1286
1287         return MS_MEDIA_ERR_NONE;
1288 }
1289
1290 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1291 {
1292         MMHandleType content = 0;
1293         MMHandleType tag = 0;
1294         char *p = NULL;
1295         unsigned char *image = NULL;
1296         unsigned int size = 0;
1297         int mmf_error = FILEINFO_ERROR_NONE;
1298         char *err_attr_name = NULL;
1299         int album_id = 0;
1300         int ret = MS_MEDIA_ERR_NONE;
1301         int cdis_value = 0;
1302
1303         /*Get Content Tag attribute ===========*/
1304         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1305
1306         if (mmf_error == FILEINFO_ERROR_NONE) {
1307                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1308                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1309                         content_info->media_meta.album = g_strdup(p);
1310                 else
1311                         SAFE_FREE(err_attr_name);
1312
1313                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1314                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1315                         content_info->media_meta.artist = g_strdup(p);
1316                 else
1317                         SAFE_FREE(err_attr_name);
1318
1319                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1320                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1321                         content_info->media_meta.album_artist = g_strdup(p);
1322                 else
1323                         SAFE_FREE(err_attr_name);
1324
1325                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1326                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1327                         content_info->media_meta.genre = g_strdup(p);
1328
1329                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1330                         /*
1331                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1332                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1333                         }
1334                         */
1335                 } else {
1336                         SAFE_FREE(err_attr_name);
1337                 }
1338
1339                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1340                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1341                         if (!isspace(*p)) {
1342                                 content_info->media_meta.title = g_strdup(p);
1343                         } else {
1344                                 int idx = 0;
1345
1346                                 for (idx = 0; idx < size; idx++) {
1347                                         if (isspace(*p)) {
1348                                                 media_svc_debug("SPACE [%s]", p);
1349                                                 p++;
1350                                                 continue;
1351                                         } else {
1352                                                 media_svc_debug("Not SPACE [%s]", p);
1353                                                 content_info->media_meta.title = g_strdup(p);
1354                                                 break;
1355                                         }
1356                                 }
1357                         }
1358                 }
1359
1360                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1361                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1362                         content_info->media_meta.description = g_strdup(p);
1363                 else
1364                         SAFE_FREE(err_attr_name);
1365
1366                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1367                 if (mmf_error != FILEINFO_ERROR_NONE)
1368                         SAFE_FREE(err_attr_name);
1369
1370                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1371                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1372                         char mime_type[255] = {0, };
1373                         ret = __media_svc_get_mime_type(content_info->path, mime_type);
1374                         /*if 3gp that audio only, media_type is music */
1375                         if ((ret == MS_MEDIA_ERR_NONE) &&
1376                                 ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/mp4") == 0) ||
1377                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO && strcmp(mime_type, "video/3gpp") == 0) ||
1378                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "video/3gpp") == 0) ||
1379                                 (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC && strcmp(mime_type, "audio/mp4") == 0))) {
1380                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1381                                 char *time_info = g_strdup_printf("0000:00:00 00:00:00 +0000");
1382                                 char *p_value = p;
1383                                 char *time_value = time_info;
1384                                 if (time_info != NULL) {
1385                                         while (*p_value != '\0') {
1386                                                 if (*p_value == '-')
1387                                                         *time_value = ':';
1388                                                 else
1389                                                         *time_value = *p_value;
1390                                                 time_value++;
1391                                                 p_value++;
1392                                         }
1393                                         content_info->media_meta.recorded_date = g_strdup(time_info);
1394                                         SAFE_FREE(time_info);
1395                                 } else {
1396                                         media_svc_error("memory allocation error");
1397                                 }
1398                         } else {
1399                                 content_info->media_meta.recorded_date = g_strdup(p);
1400                         }
1401
1402                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1403                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1404                                 if (content_info->timeline == 0)
1405                                         content_info->timeline = content_info->modified_time;
1406                                 else
1407                                         media_svc_debug("Timeline : %ld", content_info->timeline);
1408
1409                                 /* This is same as datetaken */
1410                                 /* Remove compensation string */
1411                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1412                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1413                                         G_SAFE_FREE(content_info->media_meta.recorded_date);
1414                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1415                                 } else {
1416                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1417                                 }
1418                         }
1419                 } else {
1420                         SAFE_FREE(err_attr_name);
1421                 }
1422
1423                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1424                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1425                         content_info->media_meta.composer = g_strdup(p);
1426                 else
1427                         SAFE_FREE(err_attr_name);
1428
1429                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1430                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1431                         content_info->media_meta.copyright = g_strdup(p);
1432                 else
1433                         SAFE_FREE(err_attr_name);
1434
1435                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1436                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1437                         content_info->media_meta.track_num = g_strdup(p);
1438                 else
1439                         SAFE_FREE(err_attr_name);
1440
1441                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1442                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1443                         int year = 0;
1444                         if ((p != NULL) && (__media_svc_safe_atoi(p, &year) == MS_MEDIA_ERR_NONE))
1445                                 content_info->media_meta.year = g_strdup(p);
1446                         else
1447                                 media_svc_debug("Wrong Year Information [%s]", p);
1448                 } else {
1449                         SAFE_FREE(err_attr_name);
1450                 }
1451
1452                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1453                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1454                         int rate = 0;
1455                         if ((p != NULL) && (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE))
1456                                 content_info->media_meta.rating = rate;
1457                 } else {
1458                         SAFE_FREE(err_attr_name);
1459                         content_info->media_meta.rating = 0;
1460                 }
1461
1462                 /*Do not extract artwork for the USB Storage content*/
1463                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1464                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1465                         if (mmf_error != FILEINFO_ERROR_NONE) {
1466                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1467                                 SAFE_FREE(err_attr_name);
1468                         } else {
1469                                 /*media_svc_debug("artwork size1 [%d]", size); */
1470                         }
1471
1472                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1473                         if (mmf_error != FILEINFO_ERROR_NONE) {
1474                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1475                                 SAFE_FREE(err_attr_name);
1476                         } else {
1477                                 /*media_svc_debug("artwork size2 [%d]", size); */
1478                         }
1479
1480                         if (image != NULL && size > 0) {
1481                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1482                                 int artwork_mime_size = -1;
1483
1484                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1485                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1486                                         ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1487                                         if (ret != MS_MEDIA_ERR_NONE)
1488                                                 media_svc_error("Fail to Get Thumbnail Path");
1489
1490                                         if (strlen(thumb_path) > 0) {
1491                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1492                                                 if (ret != MS_MEDIA_ERR_NONE) {
1493                                                         media_svc_error("Fail to Save Image");
1494                                                 } else {
1495                                                         /* albumart resizing */
1496                                                         ret = __media_svc_resize_artwork(thumb_path, p);
1497                                                         if (ret != MS_MEDIA_ERR_NONE) {
1498                                                                 media_svc_error("Fail to Make Thumbnail Image");
1499                                                                 _media_svc_remove_file(thumb_path);
1500
1501                                                         } else {
1502                                                                 content_info->thumbnail_path = g_strdup(thumb_path);
1503                                                         }
1504                                                 }
1505                                         }
1506                                 } else {
1507                                         SAFE_FREE(err_attr_name);
1508                                 }
1509                         }
1510                 }
1511
1512                 /*Initialize album_id to 0. below code will set the album_id*/
1513                 content_info->album_id = album_id;
1514                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1515                 if (ret != MS_MEDIA_ERR_NONE) {
1516                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1517                                 media_svc_debug("album does not exist. So start to make album art");
1518                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1519                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1520                                         ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1521                                 else
1522                                         ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1523
1524                                 content_info->album_id = album_id;
1525                         }
1526                 } else {
1527                         content_info->album_id = album_id;
1528                 }
1529
1530                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1531                         double longitude = 0.0;
1532                         double latitude = 0.0;
1533                         double altitude = 0.0;
1534
1535                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1536                         content_info->media_meta.longitude = longitude;
1537                         content_info->media_meta.latitude = latitude;
1538                         content_info->media_meta.altitude = altitude;
1539
1540                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1541                         if (mmf_error != FILEINFO_ERROR_NONE) {
1542                                 cdis_value = 0;
1543                                 SAFE_FREE(err_attr_name);
1544                         }
1545
1546                         media_svc_debug("CDIS : %d", cdis_value);
1547
1548                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1549                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1550                                 content_info->media_meta.orientation = atoi(p);
1551                         } else {
1552                                 SAFE_FREE(err_attr_name);
1553                                 content_info->media_meta.orientation = 0;
1554                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1555                         }
1556                 }
1557
1558                 mmf_error = mm_file_destroy_tag_attrs(tag);
1559                 if (mmf_error != FILEINFO_ERROR_NONE)
1560                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1561         }       else {
1562                 content_info->album_id = album_id;
1563         }
1564
1565         /*Get Content attribute ===========*/
1566         if (cdis_value == 1)
1567                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1568         else
1569                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1570
1571         if (mmf_error == FILEINFO_ERROR_NONE) {
1572                 /*Common attribute*/
1573                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1574                 if (mmf_error != FILEINFO_ERROR_NONE) {
1575                         SAFE_FREE(err_attr_name);
1576                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1577                 } else {
1578                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1579                 }
1580
1581                 /*Sound/Music attribute*/
1582                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1583
1584                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1585                         if (mmf_error != FILEINFO_ERROR_NONE) {
1586                                 SAFE_FREE(err_attr_name);
1587                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1588                         } else {
1589                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1590                         }
1591
1592                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1593                         if (mmf_error != FILEINFO_ERROR_NONE) {
1594                                 SAFE_FREE(err_attr_name);
1595                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1596                         } else {
1597                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
1598                         }
1599
1600                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1601                         if (mmf_error != FILEINFO_ERROR_NONE) {
1602                                 SAFE_FREE(err_attr_name);
1603                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1604                         } else {
1605                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
1606                         }
1607
1608                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1609                         if (mmf_error != FILEINFO_ERROR_NONE) {
1610                                 SAFE_FREE(err_attr_name);
1611                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1612                         } else {
1613                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1614                         }
1615                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1616                         int audio_bitrate = 0;
1617                         int video_bitrate = 0;
1618
1619                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1620                         if (mmf_error != FILEINFO_ERROR_NONE) {
1621                                 SAFE_FREE(err_attr_name);
1622                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1623                         } else {
1624                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
1625                         }
1626
1627                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1628                         if (mmf_error != FILEINFO_ERROR_NONE) {
1629                                 SAFE_FREE(err_attr_name);
1630                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1631                         } else {
1632                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
1633                         }
1634
1635                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1636
1637                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1638                         if (mmf_error != FILEINFO_ERROR_NONE) {
1639                                 SAFE_FREE(err_attr_name);
1640                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1641                         } else {
1642                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
1643                         }
1644
1645                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1646                         if (mmf_error != FILEINFO_ERROR_NONE) {
1647                                 SAFE_FREE(err_attr_name);
1648                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1649                         } else {
1650                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
1651                         }
1652                 } else {
1653                         media_svc_error("Not support type");
1654                         mmf_error = mm_file_destroy_content_attrs(content);
1655                         if (mmf_error != FILEINFO_ERROR_NONE)
1656                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1657
1658                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1659                 }
1660
1661                 mmf_error = mm_file_destroy_content_attrs(content);
1662                 if (mmf_error != FILEINFO_ERROR_NONE)
1663                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1664         } else {
1665                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1666         }
1667
1668         return MS_MEDIA_ERR_NONE;
1669 }
1670
1671 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1672 {
1673         media_svc_retm_if(content_info == NULL, "content info is NULL");
1674
1675         /* Delete media_svc_content_info_s */
1676         SAFE_FREE(content_info->media_uuid);
1677         SAFE_FREE(content_info->path);
1678         SAFE_FREE(content_info->file_name);
1679         SAFE_FREE(content_info->mime_type);
1680         SAFE_FREE(content_info->folder_uuid);
1681         SAFE_FREE(content_info->thumbnail_path);
1682         SAFE_FREE(content_info->storage_uuid);
1683
1684         /* Delete media_svc_content_meta_s */
1685         SAFE_FREE(content_info->media_meta.title);
1686         SAFE_FREE(content_info->media_meta.album);
1687         SAFE_FREE(content_info->media_meta.artist);
1688         SAFE_FREE(content_info->media_meta.album_artist);
1689         SAFE_FREE(content_info->media_meta.genre);
1690         SAFE_FREE(content_info->media_meta.composer);
1691         SAFE_FREE(content_info->media_meta.year);
1692         SAFE_FREE(content_info->media_meta.recorded_date);
1693         SAFE_FREE(content_info->media_meta.copyright);
1694         SAFE_FREE(content_info->media_meta.track_num);
1695         SAFE_FREE(content_info->media_meta.description);
1696         SAFE_FREE(content_info->media_meta.datetaken);
1697         SAFE_FREE(content_info->media_meta.exposure_time);
1698         SAFE_FREE(content_info->media_meta.model);
1699
1700         SAFE_FREE(content_info->media_meta.title_pinyin);
1701         SAFE_FREE(content_info->media_meta.album_pinyin);
1702         SAFE_FREE(content_info->media_meta.artist_pinyin);
1703         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1704         SAFE_FREE(content_info->media_meta.genre_pinyin);
1705         SAFE_FREE(content_info->media_meta.composer_pinyin);
1706         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1707         SAFE_FREE(content_info->media_meta.description_pinyin);
1708
1709         return;
1710 }
1711
1712 int __media_svc_get_proper_thumb_size(unsigned int orig_w, unsigned int orig_h, unsigned int *thumb_w, unsigned int *thumb_h)
1713 {
1714         bool portrait = false;
1715         double ratio;
1716
1717         if (orig_w < orig_h)
1718                 portrait = true;
1719
1720         /* Set smaller length to default size */
1721         if (portrait) {
1722                 if (orig_w < *thumb_w)
1723                         *thumb_w = orig_w;
1724                 ratio = (double)orig_h / (double)orig_w;
1725                 *thumb_h = *thumb_w * ratio;
1726         } else {
1727                 if (orig_h < *thumb_h)
1728                         *thumb_h = orig_h;
1729                 ratio = (double)orig_w / (double)orig_h;
1730                 *thumb_w = *thumb_h * ratio;
1731         }
1732
1733         media_svc_debug("proper thumb w: %d h: %d", *thumb_w, *thumb_h);
1734
1735         return MS_MEDIA_ERR_NONE;
1736 }
1737
1738 int _media_svc_create_thumbnail(const char *path, char *thumb_path, int max_length, media_svc_media_type_e media_type, uid_t uid)
1739 {
1740         int ret = MS_MEDIA_ERR_NONE;
1741         unsigned int origin_w = 0;
1742         unsigned int origin_h = 0;
1743         unsigned int thumb_w = THUMB_WIDTH;
1744         unsigned int thumb_h = THUMB_HEIGHT;
1745         mm_util_img_codec_type image_type = IMG_CODEC_UNKNOWN_TYPE;
1746
1747         if (path == NULL || thumb_path == NULL) {
1748                 media_svc_error("Invalid parameter");
1749                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1750         }
1751
1752         if (!g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
1753                         media_svc_error("Original path(%s) doesn't exist.", path);
1754                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1755         }
1756
1757         if (max_length <= 0) {
1758                 media_svc_error("Length is invalid");
1759                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1760         }
1761
1762         ms_user_storage_type_e store_type = -1;
1763         ret = ms_user_get_storage_type(uid, path, &store_type);
1764
1765         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1766                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1767                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1768         }
1769
1770         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1771
1772         //1. make hash path
1773         ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, THUMB_EXT, uid);
1774         if (ret != MS_MEDIA_ERR_NONE) {
1775                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1776                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1777                 return ret;
1778         }
1779
1780         //2. save thumbnail
1781         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1782                 ret = mm_util_extract_image_info(path, &image_type, &origin_w, &origin_h);
1783                 if (ret != MS_MEDIA_ERR_NONE) {
1784                         media_svc_error("Getting image info is failed err: %d", ret);
1785                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1786                         return MS_MEDIA_ERR_INTERNAL;
1787                 }
1788
1789                 if (image_type == IMG_CODEC_UNKNOWN_TYPE) {
1790                         media_svc_error("Unsupported image codec");
1791                         return MS_MEDIA_ERR_UNSUPPORTED_CONTENT;
1792                 }
1793
1794                 if ((image_type != IMG_CODEC_JPEG) && (origin_w * origin_h > THUMB_MAX_ALLOWED_MEM)) {
1795                         media_svc_error("This original image is too big");
1796                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1797                         return MS_MEDIA_ERR_THUMB_TOO_BIG;
1798                 }
1799
1800                 __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1801                 ret = mm_util_resize_P_P(path, thumb_w, thumb_h, thumb_path);
1802                 if (ret != MM_UTIL_ERROR_NONE) {
1803                         media_svc_error("mm_util_resize_P_P err: %d", ret);
1804                         SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1805                         return MS_MEDIA_ERR_INTERNAL;
1806                 }
1807         } else {
1808                 MMHandleType content = (MMHandleType) NULL;
1809                 MMHandleType tag = (MMHandleType) NULL;
1810
1811                 char *p = NULL;
1812                 int cdis_value = 0;
1813                 void *frame = NULL;
1814                 int video_track_num = 0;
1815                 char *err_msg = NULL;
1816                 int size = 0;
1817                 mm_util_image_h img = NULL;
1818                 mm_util_image_h resize_img = NULL;
1819
1820                 /* Get Content Tag attribute for orientation */
1821                 ret = mm_file_create_tag_attrs(&tag, path);
1822                 mm_util_magick_rotate_type rot_type = MM_UTIL_ROTATE_NUM;
1823
1824                 if (ret == FILEINFO_ERROR_NONE) {
1825                         ret = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1826                         if (ret == FILEINFO_ERROR_NONE && size >= 0) {
1827                                 if (p == NULL) {
1828                                         rot_type = MM_UTIL_ROTATE_0;
1829                                 } else {
1830                                         if (strncmp(p, "90", size) == 0)
1831                                                 rot_type = MM_UTIL_ROTATE_90;
1832                                         else if (strncmp(p, "180", size) == 0)
1833                                                 rot_type = MM_UTIL_ROTATE_180;
1834                                         else if (strncmp(p, "270", size) == 0)
1835                                                 rot_type = MM_UTIL_ROTATE_270;
1836                                         else
1837                                                 rot_type = MM_UTIL_ROTATE_0;
1838                                 }
1839                                 media_svc_debug("There is tag rotate : %d", rot_type);
1840                         } else {
1841                                 media_svc_debug("There is NOT tag rotate");
1842                                 rot_type = MM_UTIL_ROTATE_0;
1843                                 SAFE_FREE(err_msg);
1844                         }
1845
1846                         ret = mm_file_get_attrs(tag, &err_msg, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1847                         if (ret != FILEINFO_ERROR_NONE) {
1848                                 cdis_value = 0;
1849                                 SAFE_FREE(err_msg);
1850                         }
1851                 } else {
1852                         rot_type = MM_UTIL_ROTATE_0;
1853                         cdis_value = 0;
1854                 }
1855
1856                 ret = mm_file_destroy_tag_attrs(tag);
1857                 if (ret != FILEINFO_ERROR_NONE) {
1858                         media_svc_error("fail to free tag attr - err(%x)", ret);
1859                 }
1860
1861                 if (cdis_value == 1) {
1862                         media_svc_debug("This is CDIS vlaue 1");
1863                         ret = mm_file_create_content_attrs_safe(&content, path);
1864                 } else {
1865                         ret = mm_file_create_content_attrs(&content, path);
1866                 }
1867
1868                 if (ret != FILEINFO_ERROR_NONE) {
1869                         media_svc_error("mm_file_create_content_attrs fails : %d", ret);
1870                         return MS_MEDIA_ERR_INTERNAL;
1871                 }
1872
1873                 ret = mm_file_get_attrs(content, &err_msg, MM_FILE_CONTENT_VIDEO_TRACK_COUNT, &video_track_num, NULL);
1874                 if (ret != FILEINFO_ERROR_NONE) {
1875                         media_svc_error("mm_file_get_attrs fails : %s", err_msg);
1876                         SAFE_FREE(err_msg);
1877                         goto ERROR;
1878                 }
1879
1880                 if (video_track_num > 0) {
1881                         ret = mm_file_get_attrs(content, &err_msg,
1882                                                 MM_FILE_CONTENT_VIDEO_WIDTH,
1883                                                 &origin_w,
1884                                                 MM_FILE_CONTENT_VIDEO_HEIGHT,
1885                                                 &origin_h,
1886                                                 MM_FILE_CONTENT_VIDEO_THUMBNAIL, &frame, /* raw image is RGB888 format */
1887                                                 &size, NULL);
1888
1889                         if (ret != FILEINFO_ERROR_NONE) {
1890                                 media_svc_error("mm_file_get_attrs fails : %s", err_msg);
1891                                 SAFE_FREE(err_msg);
1892                                 goto ERROR;
1893                         }
1894
1895                         media_svc_debug("W[%d] H[%d] Size[%d] Frame[%p] Rotate[%d]", origin_w, origin_h, size, frame, rot_type);
1896                         if (frame == NULL || origin_w == 0 || origin_h == 0) {
1897                                 media_svc_error("Failed to get frame data");
1898                                 goto ERROR;
1899                         }
1900
1901                         ret = __media_svc_get_proper_thumb_size(origin_w, origin_h, &thumb_w, &thumb_h);
1902                         if (thumb_w <= 0 || thumb_h <= 0) {
1903                                 media_svc_error("Failed to get thumb size");
1904                                 goto ERROR;
1905                         }
1906
1907                         media_svc_debug("Origin:W[%d] H[%d] Proper:W[%d] H[%d]", origin_w, origin_h, thumb_w, thumb_h);
1908
1909                         ret = mm_util_create_handle(&img, (unsigned char *)frame, origin_w, origin_h, size, MM_UTIL_COLOR_RGB24);
1910                         if (origin_w > thumb_w || origin_h > thumb_h) {
1911                                 if (rot_type != MM_UTIL_ROTATE_0) {
1912                                         ret = mm_util_resize_B_B(img, thumb_w, thumb_h, &resize_img);
1913                                         if (ret != MM_UTIL_ERROR_NONE)
1914                                                 goto ERROR;
1915                                         ret = mm_util_rotate_B_P(resize_img, rot_type, thumb_path);
1916                                 } else {
1917                                         ret = mm_util_resize_B_P(img, thumb_w, thumb_h, thumb_path);
1918                                 }
1919                         } else {
1920                                 if (rot_type != MM_UTIL_ROTATE_0)
1921                                         ret = mm_util_rotate_B_P(img, rot_type, thumb_path);
1922                                 else
1923                                         ret = mm_util_resize_B_P(img, origin_w, origin_h, thumb_path);
1924                         }
1925                 }
1926
1927         ERROR:
1928                 mm_util_destroy_handle(img);
1929                 mm_util_destroy_handle(resize_img);
1930                 mm_file_destroy_content_attrs(content);
1931
1932                 if (ret != MS_MEDIA_ERR_NONE)
1933                         return MS_MEDIA_ERR_INTERNAL;
1934
1935                 return MS_MEDIA_ERR_NONE;
1936         }
1937
1938         return ret;
1939 }
1940
1941 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1942 {
1943         int ret = MS_MEDIA_ERR_NONE;
1944         int size = 0;
1945         pinyin_name_s *pinyinname = NULL;
1946
1947         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1948         media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1949
1950         *pinyin_str = NULL;
1951
1952         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1953         if (ret == MS_MEDIA_ERR_NONE) {
1954                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1955                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1956                 else
1957                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
1958         }
1959
1960         _media_svc_pinyin_free(pinyinname, size);
1961
1962         return ret;
1963 }
1964
1965 bool _media_svc_check_pinyin_support(void)
1966 {
1967         int ret = SYSTEM_INFO_ERROR_NONE;
1968         bool is_supported = false;
1969
1970         if (media_svc_pinyin_support == -1) {
1971                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1972                 if (ret != SYSTEM_INFO_ERROR_NONE) {
1973                         media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1974                         return false;
1975                 }
1976
1977                 media_svc_pinyin_support = is_supported;
1978         }
1979
1980         return media_svc_pinyin_support;
1981 }
1982
1983 int _media_svc_get_media_type(const char *path, int *mediatype)
1984 {
1985         int ret = MS_MEDIA_ERR_NONE;
1986         char mime_type[256] = {0};
1987         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
1988
1989         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1990
1991         ret = __media_svc_get_mime_type(path, mime_type);
1992         if (ret == MS_MEDIA_ERR_NONE)
1993                 __media_svc_get_media_type(path, mime_type, &media_type);
1994         else
1995                 media_svc_error("__media_svc_get_mime_type failed");
1996
1997         *mediatype = media_type;
1998
1999         return ret;
2000 }
2001
2002 bool _media_svc_check_storage_type(ms_user_storage_type_e storage_type)
2003 {
2004         if ((storage_type != MS_USER_STORAGE_INTERNAL)
2005                 && (storage_type != MS_USER_STORAGE_EXTERNAL)
2006                 && (storage_type != MS_USER_STORAGE_EXTERNAL_USB)) {
2007                 media_svc_error("storage type is incorrect[%d]", storage_type);
2008                 return FALSE;
2009         }
2010
2011         return TRUE;
2012 }