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