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