Just add msg for debugging
[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 <sys/vfs.h>
34 #include <glib/gstdio.h>
35 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <ctype.h>
38 #include <aul/aul.h>
39 #include <mm_file.h>
40 #include <libexif/exif-data.h>
41 #include <media-thumbnail.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <img-codec-parser.h>
45 #include <image_util.h>
46 #include <image_util_internal.h>
47 #include "media-util-err.h"
48 #include "media-svc-util.h"
49 #include "media-svc-db-utils.h"
50 #include "media-svc-debug.h"
51 #include "media-svc-env.h"
52 #include "media-svc-hash.h"
53 #include "media-svc-album.h"
54 #include "media-svc-localize-utils.h"
55 #include "media-svc-localize_ch.h"
56 #include "media-svc-localize_tw.h"
57
58 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
59
60 /* Define data structures for media type and mime type */
61 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
62 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
63 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
64 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
65 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
66 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
67 #define MEDIA_SVC_CATEGORY_PVR  0x00000020      /**< PVR category */
68 #define MEDIA_SVC_CATEGORY_UHD  0x00000040      /**< UHD category */
69 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080      /**< SCSA category */
70
71 #define CONTENT_TYPE_NUM 5
72 #define MUSIC_MIME_NUM 29
73 #define SOUND_MIME_NUM 1
74 #define MIME_TYPE_LENGTH 255
75 #define MIME_LENGTH 50
76 #define _3GP_FILE ".3gp"
77 #define _MP4_FILE ".mp4"
78 #define _ASF_FILE ".asf"
79 #define MEDIA_SVC_ARTWORK_SIZE 2000
80
81 typedef struct {
82         char content_type[15];
83         int category_by_mime;
84 } _media_svc_content_table_s;
85
86 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
87         {"audio", MEDIA_SVC_CATEGORY_SOUND},
88         {"image", MEDIA_SVC_CATEGORY_IMAGE},
89         {"video", MEDIA_SVC_CATEGORY_VIDEO},
90         {"application", MEDIA_SVC_CATEGORY_ETC},
91         {"text", MEDIA_SVC_CATEGORY_ETC},
92 };
93
94 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
95         /*known mime types of normal files*/
96         "mpeg",
97         "ogg",
98         "x-ms-wma",
99         "x-flac",
100         "mp4",
101         /* known mime types of drm files*/
102         "mp3",
103         "x-mp3", /*alias of audio/mpeg*/
104         "x-mpeg", /*alias of audio/mpeg*/
105         "3gpp",
106         "x-ogg", /*alias of audio/ogg*/
107         "vnd.ms-playready.media.pya:*.pya", /*playready*/
108         "wma",
109         "aac",
110         "x-m4a", /*alias of audio/mp4*/
111         /* below mimes are rare*/
112         "x-vorbis+ogg",
113         "x-flac+ogg",
114         "x-matroska",
115         "ac3",
116         "mp2",
117         "x-ape",
118         "x-ms-asx",
119         "vnd.rn-realaudio",
120
121         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
122         "vorbis", /*alias of audio/x-vorbis+ogg*/
123         "x-oggflac",
124         "x-mp2", /*alias of audio/mp2*/
125         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
126         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
127         "x-wav",
128 };
129
130 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
131         "x-smaf",
132 };
133
134 typedef enum {
135         MEDIA_SVC_EXTRACTED_FIELD_NONE                  = 0x00000001,
136         MEDIA_SVC_EXTRACTED_FIELD_TITLE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
137         MEDIA_SVC_EXTRACTED_FIELD_DESC                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
138         MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT             = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
139         MEDIA_SVC_EXTRACTED_FIELD_AUTHOR                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
140         MEDIA_SVC_EXTRACTED_FIELD_ARTIST                        = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
141         MEDIA_SVC_EXTRACTED_FIELD_GENRE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
142         MEDIA_SVC_EXTRACTED_FIELD_ALBUM                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
143         MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
144         MEDIA_SVC_EXTRACTED_FIELD_YEAR                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
145         MEDIA_SVC_EXTRACTED_FIELD_CATEGORY              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
146         MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
147 } media_svc_extracted_field_e;
148
149 char *_media_info_generate_uuid(void)
150 {
151         uuid_t uuid_value;
152         static char uuid_unparsed[37];
153
154 RETRY_GEN:
155         uuid_generate(uuid_value);
156         uuid_unparse(uuid_value, uuid_unparsed);
157
158         if (strlen(uuid_unparsed) < 36) {
159                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
160                 goto RETRY_GEN;
161         }
162
163         return uuid_unparsed;
164 }
165
166 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
167 {
168         if (!x_src || strlen(x_src) == 0) {
169                 media_svc_error("x_src is NULL");
170                 return;
171         }
172
173         if (max_len < 1) {
174                 media_svc_error("length is Wrong");
175                 return;
176         }
177
178         strncpy(x_dst, x_src, max_len - 1);
179         x_dst[max_len - 1] = '\0';
180 }
181
182 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
183 {
184         int len = 0;
185
186         if (!STRING_VALID(src)) {
187                 media_svc_error("invalid src");
188                 return MS_MEDIA_ERR_INVALID_PARAMETER;
189         }
190
191         SAFE_FREE(*dst);
192
193         len = strlen(src) + 1;
194         *dst = malloc(len);
195
196         if (*dst == NULL) {
197                 media_svc_error("malloc failed");
198                 return MS_MEDIA_ERR_INTERNAL;
199         }
200
201         strncpy(*dst, src, len);
202         char *p = *dst;
203         p[len - 1] = '\0';
204
205         return MS_MEDIA_ERR_NONE;
206 }
207
208 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
209 {
210         if (!STRING_VALID(src)) {
211                 media_svc_error("invalid src");
212                 return MS_MEDIA_ERR_INVALID_PARAMETER;
213         }
214
215         SAFE_FREE(*dst);
216
217         *dst = malloc(copysize + 1);
218
219         if (*dst == NULL) {
220                 media_svc_error("malloc failed");
221                 return MS_MEDIA_ERR_INTERNAL;
222         }
223
224         strncpy(*dst, src, copysize);
225         char *p = *dst;
226         p[copysize] = '\0';
227
228         return MS_MEDIA_ERR_NONE;
229 }
230
231 static int __media_svc_split_to_double(char *input, double *arr)
232 {
233         char tmp_arr[255] = {0, };
234         int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
235
236         if (!STRING_VALID(input)) {
237                 media_svc_error("Invalid parameter");
238                 return MS_MEDIA_ERR_INVALID_PARAMETER;
239         }
240         memset(tmp_arr, 0x0, sizeof(tmp_arr));
241
242         /*media_svc_debug("input: [%s]", input); */
243
244         len = strlen(input);
245
246         for (idx = 0; idx < (len + 1); idx++) {
247                 if (input[idx] == ' ') {
248                         continue;
249                 } else if ((input[idx] == ',') || (idx == len)) {
250                         arr[arr_idx] = atof(tmp_arr);
251                         arr_idx++;
252                         str_idx = 0;
253                         /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
254                         memset(tmp_arr, 0x0, sizeof(tmp_arr));
255                 } else {
256                         tmp_arr[str_idx] = input[idx];
257                         str_idx++;
258                 }
259         }
260
261         if (arr_idx != 3) {
262                 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
263                 return MS_MEDIA_ERR_INTERNAL;
264         }
265
266         return MS_MEDIA_ERR_NONE;
267 }
268
269 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
270 {
271         ExifEntry *entry;
272         ExifTag tag;
273
274         if (ed == NULL)
275                 return MS_MEDIA_ERR_INVALID_PARAMETER;
276
277         tag = tagtype;
278
279         entry = exif_data_get_entry(ed, tag);
280         if (entry) {
281                 /* Get the contents of the tag in human-readable form */
282                 if (tag == EXIF_TAG_ORIENTATION ||
283                         tag == EXIF_TAG_PIXEL_X_DIMENSION ||
284                         tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
285                         tag == EXIF_TAG_ISO_SPEED_RATINGS) {
286
287                         if (i_value == NULL) {
288                                 media_svc_debug("i_value is NULL");
289                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
290                         }
291
292                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
293                         short exif_value = exif_get_short(entry->data, mByteOrder);
294                         *i_value = (int)exif_value;
295
296                 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
297
298                         if (d_value == NULL) {
299                                 media_svc_debug("d_value is NULL");
300                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
301                         }
302
303                         /* Get the contents of the tag in human-readable form */
304                         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
305                         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
306                         gps_buf[strlen(gps_buf)] = '\0';
307                         int ret = MS_MEDIA_ERR_NONE;
308
309                         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
310
311                         ret = __media_svc_split_to_double(gps_buf, tmp_arr);
312                         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
313
314                         *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
315                 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
316
317                         if (buf == NULL) {
318                                 media_svc_debug("buf is NULL");
319                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
320                         }
321
322                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
323                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
324                         long numerator = mRational.numerator;
325                         long denominator = mRational.denominator;
326                         snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
327
328                 } else if (tag == EXIF_TAG_FNUMBER) {
329
330                         if (d_value == NULL) {
331                                 media_svc_debug("d_value is NULL");
332                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
333                         }
334
335                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
336                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
337                         long numerator = mRational.numerator;
338                         long denominator = mRational.denominator;
339
340                         *d_value = ((numerator*1.0)/(denominator*1.0));
341
342                 } else {
343
344                         if (buf == NULL) {
345                                 media_svc_debug("buf is NULL");
346                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
347                         }
348
349                         exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
350                         buf[strlen(buf)] = '\0';
351                 }
352         }
353
354         return MS_MEDIA_ERR_NONE;
355 }
356
357 time_t __media_svc_get_timeline_from_str(const char *timstr)
358 {
359         struct tm t;
360         time_t modified_t = 0;
361         time_t rawtime;
362         struct tm timeinfo;
363
364         if (!STRING_VALID(timstr)) {
365                 media_svc_error("Invalid Parameter");
366                 return 0;
367         }
368
369         /*Exif Format : %Y:%m:%d %H:%M:%S
370         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
371         memset(&t, 0x00, sizeof(struct tm));
372
373         tzset();
374         time(&rawtime);
375         localtime_r(&rawtime, &timeinfo);
376
377         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
378                 t.tm_isdst = timeinfo.tm_isdst;
379                 if (t.tm_isdst != 0)
380                         media_svc_debug("DST %d", t.tm_isdst);
381
382                 modified_t = mktime(&t);
383                 if (modified_t > 0)
384                         return modified_t;
385                 else
386                         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);
387         } else {
388                 media_svc_error("Failed to get timeline : [%s]", timstr);
389         }
390
391         return 0;
392 }
393
394 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
395 {
396         int idx = 0;
397
398         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
399
400         /*categorize from mimetype */
401         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
402                 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
403                         *category = (*category | content_category[idx].category_by_mime);
404                         break;
405                 }
406         }
407
408         /*in application type, exitst sound file ex) x-smafs, asf */
409         if (*category & MEDIA_SVC_CATEGORY_ETC) {
410                 int prefix_len = strlen(content_category[0].content_type);
411                 char *ext = NULL;
412
413                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
414                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
415                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
416                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
417                                 break;
418                         }
419                 }
420
421                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
422                         *category ^= MEDIA_SVC_CATEGORY_ETC;
423                         *category |= MEDIA_SVC_CATEGORY_SOUND;
424                 }
425
426                 /*"asf" must check video stream and then categorize in directly. */
427                 ext = strrchr(path, '.');
428                 if (ext != NULL) {
429                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
430                                 int audio = 0;
431                                 int video = 0;
432                                 int err = 0;
433
434                                 err = mm_file_get_stream_info(path, &audio, &video);
435                                 if (err == 0) {
436                                         if (audio > 0 && video == 0) {
437                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
438                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
439                                         } else {
440                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
441                                                 *category |= MEDIA_SVC_CATEGORY_VIDEO;
442                                         }
443                                 }
444                         }
445                 }
446         }
447
448         /*check music file in soun files. */
449         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
450                 int prefix_len = strlen(content_category[0].content_type) + 1;
451
452                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
453                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
454                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
455                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
456                                 break;
457                         }
458                 }
459
460                 /*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*/
461                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
462                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
463                         *category |= MEDIA_SVC_CATEGORY_ETC;
464                 }
465         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
466                 /*some video files don't have video stream. in this case it is categorize as music. */
467                 char *ext = NULL;
468                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
469                 ext = strrchr(path, '.');
470                 if (ext != NULL) {
471                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
472                                 int audio = 0;
473                                 int video = 0;
474                                 int err = 0;
475
476                                 err = mm_file_get_stream_info(path, &audio, &video);
477                                 if (err == 0) {
478                                         if (audio > 0 && video == 0) {
479                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
480                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
481                                         }
482                                 }
483                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
484                         }
485                 }
486         }
487
488         return MS_MEDIA_ERR_NONE;
489 }
490
491 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
492 {
493         int ret = MS_MEDIA_ERR_NONE;
494         int category = 0;
495
496         media_svc_media_type_e type;
497
498         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
499         if (ret != MS_MEDIA_ERR_NONE)
500                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
501
502         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
503         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
504         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
505         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
506         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
507
508         *media_type = type;
509
510         return ret;
511 }
512
513 /*
514 drm_contentifo is not NULL, if the file is OMA DRM.
515 If the file is not OMA DRM, drm_contentinfo must be NULL.
516 */
517 static int __media_svc_get_mime_type(const char *path, char *mimetype)
518 {
519         if (path == NULL)
520                 return MS_MEDIA_ERR_INVALID_PARAMETER;
521
522         /*in case of normal files or failure to get mime in drm */
523         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
524                 media_svc_error("aul_get_mime_from_file fail");
525                 return MS_MEDIA_ERR_INTERNAL;
526         }
527
528         return MS_MEDIA_ERR_NONE;
529 }
530
531 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
532 {
533         int i = 0;
534
535         for (i = strlen(file_path); i >= 0; i--) {
536                 if (file_path[i] == '.') {
537                         _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
538                         return true;
539                 }
540
541                 if (file_path[i] == '/')
542                         return false;
543         }
544         return false;
545 }
546
547 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
548 {
549         char *err_attr_name = NULL;
550         double gps_value = 0.0;
551         int mmf_error = FILEINFO_ERROR_NONE;
552
553         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
554         if (mmf_error == FILEINFO_ERROR_NONE) {
555                 if (longitude != NULL)
556                         *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
557         } else {
558                 SAFE_FREE(err_attr_name);
559         }
560
561         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
562         if (mmf_error == FILEINFO_ERROR_NONE) {
563                 if (latitude != NULL)
564                         *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
565         } else {
566                 SAFE_FREE(err_attr_name);
567         }
568
569         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
570         if (mmf_error == FILEINFO_ERROR_NONE) {
571                 if (altitude != NULL)
572                         *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
573         } else {
574                 SAFE_FREE(err_attr_name);
575         }
576
577         return MS_MEDIA_ERR_NONE;
578 }
579
580 static char *__media_svc_get_thumb_path(uid_t uid)
581 {
582         int ret = 0;
583         char *result_path = NULL;
584
585         ret = tzplatform_set_user(uid);
586         if (ret != 0) {
587                 media_svc_error("Invalid UID[%d]", uid);
588                 return NULL;
589         } else {
590                 const char *result = tzplatform_mkpath(TZ_USER_SHARE, "media/.thumb");
591                 result_path = strndup(result, strlen(result));
592                 tzplatform_reset_user();
593         }
594
595         return result_path;
596 }
597
598 static int __media_svc_encode_jpeg(unsigned char *src, unsigned long width, unsigned long height, image_util_colorspace_e colorspace, int quality, unsigned char **dst, unsigned long long *dst_size)
599 {
600         int res = IMAGE_UTIL_ERROR_NONE;
601         image_util_encode_h encoder = NULL;
602         unsigned char *encoded_data = NULL;
603         res = image_util_encode_create(IMAGE_UTIL_JPEG , &encoder);
604         if (res != IMAGE_UTIL_ERROR_NONE) {
605                 media_svc_error("image_util_encode_create failed! (%d)", res);
606                 return MS_MEDIA_ERR_INTERNAL;
607         }
608         res = image_util_encode_set_resolution(encoder, width, height);
609         if (res != IMAGE_UTIL_ERROR_NONE) {
610                 media_svc_error("image_util_encode_set_resolution failed! (%d)", res);
611                 image_util_encode_destroy(encoder);
612                 return MS_MEDIA_ERR_INTERNAL;
613         }
614         res = image_util_encode_set_colorspace(encoder, colorspace);
615         if (res != IMAGE_UTIL_ERROR_NONE) {
616                 media_svc_error("image_util_encode_set_colorspace failed! (%d)", res);
617                 image_util_encode_destroy(encoder);
618                 return MS_MEDIA_ERR_INTERNAL;
619         }
620         res = image_util_encode_set_quality(encoder, quality);
621         if (res != IMAGE_UTIL_ERROR_NONE) {
622                 media_svc_error("image_util_encode_set_quality failed! (%d)", res);
623                 image_util_encode_destroy(encoder);
624                 return MS_MEDIA_ERR_INTERNAL;
625         }
626         res = image_util_encode_set_input_buffer(encoder, src);
627         if (res != IMAGE_UTIL_ERROR_NONE) {
628                 media_svc_error("image_util_encode_set_input_buffer failed! (%d)", res);
629                 image_util_encode_destroy(encoder);
630                 return MS_MEDIA_ERR_INTERNAL;
631         }
632         res = image_util_encode_set_output_buffer(encoder, &encoded_data);
633         if (res != IMAGE_UTIL_ERROR_NONE) {
634                 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
635                 image_util_encode_destroy(encoder);
636                 return MS_MEDIA_ERR_INTERNAL;
637         }
638         res = image_util_encode_run(encoder, dst_size);
639         if (res != IMAGE_UTIL_ERROR_NONE) {
640                 media_svc_error("image_util_encode_run failed! (%d)", res);
641                 image_util_encode_destroy(encoder);
642                 return MS_MEDIA_ERR_INTERNAL;
643         }
644         if (encoded_data != NULL) {
645                 *dst = (unsigned char *)calloc(1, *dst_size);
646                 if (*dst == NULL) {
647                         media_svc_error("memory allocation failed! (%lld)", *dst_size);
648                         image_util_encode_destroy(encoder);
649                         return MS_MEDIA_ERR_INTERNAL;
650                 }
651                 memcpy(*dst, encoded_data, *dst_size);
652         }
653         res = image_util_encode_destroy(encoder);
654         if (res != IMAGE_UTIL_ERROR_NONE) {
655                 media_svc_error("image_util_encode_destroy failed! (%d)", res);
656                 return MS_MEDIA_ERR_INTERNAL;
657         }
658         SAFE_FREE(encoded_data);
659         return MS_MEDIA_ERR_NONE;
660 }
661
662 static int __media_svc_decode_jpeg(unsigned char *src, unsigned long long size, image_util_colorspace_e colorspace, unsigned char **dst, unsigned long *width, unsigned long *height, unsigned long long *dst_size)
663 {
664         int res = IMAGE_UTIL_ERROR_NONE;
665         image_util_decode_h decoder = NULL;
666         res = image_util_decode_create(&decoder);
667         if (res != IMAGE_UTIL_ERROR_NONE) {
668                 media_svc_error("image_util_decode_create failed! (%d)", res);
669                 return MS_MEDIA_ERR_INTERNAL;
670         }
671         res = image_util_decode_set_input_buffer(decoder, src, size);
672         if (res != IMAGE_UTIL_ERROR_NONE) {
673                 media_svc_error("image_util_decode_set_input_buffer failed! (%d)", res);
674                 image_util_decode_destroy(decoder);
675                 return MS_MEDIA_ERR_INTERNAL;
676         }
677         res = image_util_decode_set_colorspace(decoder, colorspace);
678         if (res != IMAGE_UTIL_ERROR_NONE) {
679                 media_svc_error("image_util_decode_set_colorspace failed! (%d)", res);
680                 image_util_decode_destroy(decoder);
681                 return MS_MEDIA_ERR_INTERNAL;
682         }
683         res = image_util_decode_set_output_buffer(decoder, dst);
684         if (res != IMAGE_UTIL_ERROR_NONE) {
685                 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
686                 image_util_decode_destroy(decoder);
687                 return MS_MEDIA_ERR_INTERNAL;
688         }
689         res = image_util_decode_run(decoder, width, height, dst_size);
690         if (res != IMAGE_UTIL_ERROR_NONE) {
691                 media_svc_error("image_util_decode_run failed! (%d)", res);
692                 image_util_decode_destroy(decoder);
693                 return MS_MEDIA_ERR_INTERNAL;
694         }
695
696         res = image_util_decode_destroy(decoder);
697         if (res != IMAGE_UTIL_ERROR_NONE) {
698                 media_svc_error("image_util_decode_destroy failed! (%d)", res);
699                 return MS_MEDIA_ERR_INTERNAL;
700         }
701         return MS_MEDIA_ERR_NONE;
702 }
703
704 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
705 {
706         int ret = MS_MEDIA_ERR_NONE;
707         unsigned char *raw_image = NULL;
708         int width = 0;
709         int height = 0;
710         unsigned int raw_size = 0;
711         void *resized_raw_image = NULL;
712         int resized_width = 0;
713         int resized_height = 0;
714         unsigned int buf_size = 0;
715         image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_RGB888;
716
717         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
718                 media_svc_debug("type [jpeg] size [%d]", size);
719                 /* decoding */
720                 ret = __media_svc_decode_jpeg(image, (unsigned long long)size, colorspace, &raw_image, (unsigned long *)&width, (unsigned long *)&height, (unsigned long long *)&raw_size);
721                 if (ret != MS_MEDIA_ERR_NONE) {
722                         media_svc_error("__media_svc_decode_jpeg failed");
723                         *resize_image = image;
724                         *resize_size = size;
725                         return MS_MEDIA_ERR_NONE;
726                 }
727
728                 if (raw_image == NULL) {
729                         media_svc_error("raw_image is null");
730                         *resize_image = image;
731                         *resize_size = size;
732                         return MS_MEDIA_ERR_NONE;
733                 }
734
735                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
736                         media_svc_debug("No need resizing");
737                         *resize_image = image;
738                         *resize_size = size;
739                         SAFE_FREE(raw_image);
740                         return MS_MEDIA_ERR_NONE;
741                 }
742                 /* resizing */
743                 if (width > height) {
744                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
745                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
746                 } else {
747                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
748                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
749                 }
750
751                 image_util_calculate_buffer_size(resized_width, resized_height, colorspace, &buf_size);
752
753                 resized_raw_image = malloc(buf_size);
754
755                 if (resized_raw_image == NULL) {
756                         media_svc_error("malloc failed");
757                         *resize_image = image;
758                         *resize_size = size;
759                         SAFE_FREE(raw_image);
760                         return MS_MEDIA_ERR_NONE;
761                 }
762
763                 memset(resized_raw_image, 0, buf_size);
764
765                 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, colorspace);
766                 if (ret != MS_MEDIA_ERR_NONE) {
767                         media_svc_error("image_util_resize failed");
768                         *resize_image = image;
769                         *resize_size = size;
770                         SAFE_FREE(raw_image);
771                         SAFE_FREE(resized_raw_image);
772                         return MS_MEDIA_ERR_NONE;
773                 }
774                 SAFE_FREE(raw_image);
775
776                 /* encoding */
777                 ret = __media_svc_encode_jpeg((unsigned char *)resized_raw_image, (unsigned long)resized_width, (unsigned long)resized_height, colorspace, 90, resize_image, (unsigned long long *)resize_size);
778                 if (ret != MS_MEDIA_ERR_NONE) {
779                         media_svc_error("__media_svc_encode_jpeg failed");
780                         *resize_image = image;
781                         *resize_size = size;
782                         SAFE_FREE(resized_raw_image);
783                         return MS_MEDIA_ERR_NONE;
784                 }
785                 SAFE_FREE(resized_raw_image);
786
787                 if (*resize_image == NULL) {
788                         media_svc_error("*resize_image is null");
789                         *resize_image = image;
790                         *resize_size = size;
791                         return MS_MEDIA_ERR_NONE;
792                 }
793         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
794                 media_svc_debug("type [png] size [%d]", size);
795                 *resize_image = image;
796                 *resize_size = size;
797
798         } else {
799                 media_svc_debug("Not proper img format");
800                 *resize_image = image;
801                 *resize_size = size;
802         }
803
804         return ret;
805 }
806
807 static int __media_svc_safe_atoi(char *buffer, int *si)
808 {
809         char *end;
810         errno = 0;
811         const long sl = strtol(buffer, &end, 10);
812
813         if (end == buffer) {
814                 media_svc_error("not a decimal number");
815                 return MS_MEDIA_ERR_INTERNAL;
816         } else if ('\0' != *end) {
817                 media_svc_error("extra characters at end of input: %s", end);
818                 return MS_MEDIA_ERR_INTERNAL;
819         } else if ((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno)) {
820                 media_svc_error("out of range of type long");
821                 return MS_MEDIA_ERR_INTERNAL;
822         } else if (sl > INT_MAX) {
823                 media_svc_error("greater than INT_MAX");
824                 return MS_MEDIA_ERR_INTERNAL;
825         } else if (sl < INT_MIN) {
826                 media_svc_error("less than INT_MIN");
827                 return MS_MEDIA_ERR_INTERNAL;
828         } else {
829                 *si = (int)sl;
830         }
831         return MS_MEDIA_ERR_NONE;
832 }
833
834 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
835 {
836         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
837
838         if (!image) {
839                 media_svc_error("invalid image..");
840                 return MS_MEDIA_ERR_INVALID_PARAMETER;
841         }
842
843         struct statfs fs;
844         char *thumb_path = __media_svc_get_thumb_path(uid);
845         if (!STRING_VALID(thumb_path)) {
846                 media_svc_error("fail to get thumb_path");
847                 return MS_MEDIA_ERR_INTERNAL;
848         }
849
850         if (-1 == statfs(thumb_path, &fs)) {
851                 media_svc_error("error in statfs");
852                 SAFE_FREE(thumb_path);
853                 return MS_MEDIA_ERR_INTERNAL;
854         }
855
856         SAFE_FREE(thumb_path);
857
858         long bsize_kbytes = fs.f_bsize >> 10;
859
860         if ((bsize_kbytes * fs.f_bavail) < 1024) {
861                 media_svc_error("not enought space...");
862                 return MS_MEDIA_ERR_INTERNAL;
863         }
864
865         FILE *fp = NULL;
866         int nwrite = -1;
867         if (image != NULL && size > 0) {
868                 fp = fopen(image_path, "w");
869
870                 if (fp == NULL) {
871                         media_svc_error("failed to open file");
872                         return MS_MEDIA_ERR_INTERNAL;
873                 }
874
875                 nwrite = fwrite(image, 1, size, fp);
876                 if (nwrite != size) {
877                         media_svc_error("failed to write thumbnail");
878                         fclose(fp);
879                         return MS_MEDIA_ERR_INTERNAL;
880                 }
881                 fclose(fp);
882         }
883
884         return MS_MEDIA_ERR_NONE;
885 }
886
887 static char *_media_svc_get_title_from_filepath(const char *path)
888 {
889         char *filename = NULL;
890         char *title = NULL;
891         char    *ext = NULL;
892         int filename_len = -1;
893         int new_title_len = -1;
894
895         if (!path) {
896                 media_svc_error("path is NULL");
897                 return NULL;
898         }
899
900         filename = g_path_get_basename(path);
901         if (!STRING_VALID(filename)) {
902                 media_svc_error("wrong file name");
903                 SAFE_FREE(filename);
904                 return NULL;
905         }
906
907         filename_len = strlen(filename);
908
909         ext = g_strrstr(filename, ".");
910         if (!ext) {
911                 media_svc_error("there is no file extention");
912                 return filename;
913         }
914
915         new_title_len = filename_len - strlen(ext);
916         if (new_title_len < 1) {
917                 media_svc_error("title length is zero");
918                 SAFE_FREE(filename);
919                 return NULL;
920         }
921
922         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
923
924         SAFE_FREE(filename);
925
926         media_svc_debug("extract title is [%s]", title);
927
928         return title;
929 }
930
931 int _media_svc_rename_file(const char *old_name, const char *new_name)
932 {
933         if ((old_name == NULL) || (new_name == NULL)) {
934                 media_svc_error("invalid file name");
935                 return MS_MEDIA_ERR_INVALID_PARAMETER;
936         }
937
938         if (rename(old_name, new_name) < 0) {
939                 media_svc_stderror(" ");
940                 return MS_MEDIA_ERR_INTERNAL;
941         }
942
943         return MS_MEDIA_ERR_NONE;
944 }
945
946 int _media_svc_remove_file(const char *path)
947 {
948         int result = -1;
949
950         result = remove(path);
951         if (result == 0) {
952                 media_svc_debug("success to remove file");
953                 return MS_MEDIA_ERR_NONE;
954         } else {
955                 media_svc_stderror("fail to remove file result");
956                 return MS_MEDIA_ERR_INTERNAL;
957         }
958 }
959
960 int _media_svc_remove_all_files_in_dir(const char *dir_path)
961 {
962         struct dirent entry;
963         struct dirent *result;
964         struct stat st;
965         char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
966         DIR *dir = NULL;
967
968         dir = opendir(dir_path);
969         if (dir == NULL) {
970                 media_svc_error("%s is not exist", dir_path);
971                 return MS_MEDIA_ERR_INVALID_PARAMETER;
972         }
973
974         while (!readdir_r(dir, &entry, &result)) {
975                 if (result == NULL)
976                         break;
977
978                 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0)
979                         continue;
980
981                 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
982
983                 if (stat(filename, &st) != 0)
984                         continue;
985
986                 if (S_ISDIR(st.st_mode))
987                         continue;
988
989                 if (unlink(filename) != 0) {
990                         media_svc_stderror("failed to remove");
991                         closedir(dir);
992                         return MS_MEDIA_ERR_INTERNAL;
993                 }
994         }
995
996         closedir(dir);
997         return MS_MEDIA_ERR_NONE;
998 }
999
1000 char *_media_svc_get_thumb_internal_path(uid_t uid)
1001 {
1002         int ret = 0;
1003         char *result_path = NULL;
1004
1005         ret = tzplatform_set_user(uid);
1006         if (ret != 0) {
1007                 media_svc_error("Invalid UID[%d]", uid);
1008                 return NULL;
1009         } else {
1010                 const char *result = tzplatform_mkpath(TZ_USER_SHARE, "media/.thumb/phone");
1011                 result_path = strndup(result, strlen(result));
1012                 tzplatform_reset_user();
1013         }
1014
1015         return result_path;
1016 }
1017
1018 char *_media_svc_get_thumb_external_path(uid_t uid)
1019 {
1020         int ret = 0;
1021         char *result_path = NULL;
1022
1023         ret = tzplatform_set_user(uid);
1024         if (ret != 0) {
1025                 media_svc_error("Invalid UID[%d]", uid);
1026                 return NULL;
1027         } else {
1028                 const char *result = tzplatform_mkpath(TZ_USER_SHARE, "media/.thumb/mmc");
1029                 result_path = strndup(result, strlen(result));
1030                 tzplatform_reset_user();
1031         }
1032
1033         return result_path;
1034 }
1035
1036 static int __media_svc_check_thumb_dir(const char *thumb_dir)
1037 {
1038         DIR *dir = NULL;
1039
1040         dir = opendir(thumb_dir);
1041         if (dir != NULL)
1042                 closedir(dir);
1043         else
1044                 return MS_MEDIA_ERR_INTERNAL;
1045
1046         return MS_MEDIA_ERR_NONE;
1047 }
1048
1049 int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
1050 {
1051         int ret = MS_MEDIA_ERR_NONE;
1052         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1053         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
1054         char *thumb_dir = NULL;
1055         char hash[255 + 1] = {0, };
1056         char *thumbfile_ext = NULL;
1057         char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
1058         char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
1059
1060         if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
1061                 media_svc_error("fail to get thumbnail path");
1062                 SAFE_FREE(internal_thumb_path);
1063                 SAFE_FREE(external_thumb_path);
1064                 return MS_MEDIA_ERR_INTERNAL;
1065         }
1066
1067         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
1068
1069         ret = __media_svc_check_thumb_dir(thumb_dir);
1070         if (ret != MS_MEDIA_ERR_NONE) {
1071                 media_svc_error("__media_svc_check_thumb_dir");
1072                 SAFE_FREE(internal_thumb_path);
1073                 SAFE_FREE(external_thumb_path);
1074                 return MS_MEDIA_ERR_INTERNAL;
1075         }
1076
1077         memset(file_ext, 0, sizeof(file_ext));
1078         if (!__media_svc_get_file_ext(pathname, file_ext))
1079                 media_svc_error("get file ext fail");
1080
1081         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1082         if (ret != MS_MEDIA_ERR_NONE) {
1083                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1084                 SAFE_FREE(internal_thumb_path);
1085                 SAFE_FREE(external_thumb_path);
1086                 return MS_MEDIA_ERR_INTERNAL;
1087         }
1088
1089         /*media_svc_debug("img format is [%s]", img_format); */
1090
1091         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1092                 thumbfile_ext = (char *)"jpg";
1093         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1094                 thumbfile_ext = (char *)"png";
1095         } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1096                 thumbfile_ext = (char *)"gif";
1097         } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1098                 thumbfile_ext = (char *)"bmp";
1099         } else {
1100                 media_svc_error("Not proper img format");
1101                 SAFE_FREE(internal_thumb_path);
1102                 SAFE_FREE(external_thumb_path);
1103                 return MS_MEDIA_ERR_INTERNAL;
1104         }
1105
1106         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1107         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1108         /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1109
1110         SAFE_FREE(internal_thumb_path);
1111         SAFE_FREE(external_thumb_path);
1112
1113         return MS_MEDIA_ERR_NONE;
1114 }
1115
1116 int _media_svc_get_file_time(const char *full_path)
1117 {
1118         struct stat statbuf;
1119         int fd = 0;
1120
1121         memset(&statbuf, 0, sizeof(struct stat));
1122         fd = stat(full_path, &statbuf);
1123         if (fd == -1) {
1124                 media_svc_error("stat(%s) fails.", full_path);
1125                 return MS_MEDIA_ERR_INTERNAL;
1126         }
1127
1128         return statbuf.st_mtime;
1129 }
1130
1131 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1132 {
1133         int ret = MS_MEDIA_ERR_NONE;
1134
1135         /* Set default GPS value before extracting meta information */
1136         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1137         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1138         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1139
1140         /* Set filename to title for all media */
1141         char *title = NULL;
1142         title = _media_svc_get_title_from_filepath(content_info->path);
1143         if (title) {
1144                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1145                 if (ret != MS_MEDIA_ERR_NONE)
1146                         media_svc_error("strcpy error");
1147                 SAFE_FREE(title);
1148         } else {
1149                 media_svc_error("Can't extract title");
1150                 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1151                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1152         }
1153
1154         /* Set default value before extracting meta information */
1155         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1156         media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1157
1158         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1159         media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1160
1161         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1162         media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1163
1164         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1165         media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1166
1167         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1168         media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1169
1170         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1171         media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1172
1173         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1174         media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1175
1176         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1177         media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1178
1179         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1180         media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1181
1182         if (refresh) {
1183                 media_svc_debug("refresh");
1184                 return MS_MEDIA_ERR_NONE;
1185         }
1186
1187         content_info->played_count = 0;
1188         content_info->last_played_time = 0;
1189         content_info->last_played_position = 0;
1190         content_info->favourate = 0;
1191         content_info->media_meta.rating = 0;
1192
1193         return MS_MEDIA_ERR_NONE;
1194 }
1195
1196 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1197                         const char *path, media_svc_media_type_e *media_type, bool refresh)
1198 {
1199         int ret = MS_MEDIA_ERR_NONE;
1200         char * media_uuid = NULL;
1201         char * file_name = NULL;
1202         bool drm_type = false;
1203         char mime_type[256] = {0, };
1204
1205         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1206         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1207
1208         struct stat st;
1209         memset(&st, 0, sizeof(struct stat));
1210         if (stat(path, &st) == 0) {
1211                 content_info->modified_time = st.st_mtime;
1212                 content_info->timeline = content_info->modified_time;
1213                 content_info->size = st.st_size;
1214         } else {
1215                 media_svc_stderror("stat failed");
1216         }
1217
1218         _media_svc_set_default_value(content_info, refresh);
1219
1220         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1221         if (refresh) {
1222                 media_svc_debug("refresh");
1223                 return MS_MEDIA_ERR_NONE;
1224         }
1225
1226         ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1227         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1228
1229         content_info->storage_type = storage_type;
1230         time(&content_info->added_time);
1231
1232         media_uuid = _media_info_generate_uuid();
1233         if (media_uuid == NULL) {
1234                 _media_svc_destroy_content_info(content_info);
1235                 return MS_MEDIA_ERR_INTERNAL;
1236         }
1237
1238         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1239         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1240
1241         file_name = g_path_get_basename(path);
1242         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1243         SAFE_FREE(file_name);
1244         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1245
1246         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1247         if drm_contentinfo is not NULL, the file is OMA DRM.*/
1248         ret = __media_svc_get_mime_type(path, mime_type);
1249         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1250
1251         media_svc_debug("mime [%s]", mime_type);
1252         content_info->is_drm = drm_type;
1253
1254         ret = __media_svc_get_media_type(path, mime_type, media_type);
1255         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1256
1257         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1258                 media_svc_error("invalid media_type condition[%d]", *media_type);
1259                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1260         }
1261
1262         ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1263         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1264
1265         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1266
1267         content_info->media_type = *media_type;
1268
1269         return MS_MEDIA_ERR_NONE;
1270 }
1271
1272 int image_360_check(char *path)
1273 {
1274         FILE *fp = NULL;
1275         long app1_size = 0;
1276         int size = 1;
1277         unsigned char exif_header[4];
1278         unsigned char exif_app1[2];
1279         unsigned char exif_app1_xmp[2];
1280         long exif_app1_xmp_size = 0;
1281         unsigned char exif_app1_xmp_t[2];
1282         char *xmp_data = NULL;
1283         int size1 = 0;
1284         int size2 = 0;
1285         int fdata = 0;
1286         int temp = 0;
1287
1288         fp = fopen(path, "rb");
1289         if (fp == NULL)
1290                 goto ERROR;
1291
1292         size = fread(exif_header, 1, sizeof(exif_header), fp);
1293         if (size <= 0)
1294                 goto ERROR;
1295
1296         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1297                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
1298                 if (size <= 0)
1299                         goto ERROR;
1300
1301                 size1 = exif_app1[0];
1302                 size2 = exif_app1[1];
1303
1304                 app1_size = size1 * 256 + size2 - 2;
1305
1306                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1307                         goto ERROR;
1308
1309                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
1310                 if (size <= 0)
1311                         goto ERROR;
1312
1313                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1314                         int result = 0;
1315                         char *ptr = NULL;
1316                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
1317                         if (size <= 0)
1318                                 goto ERROR;
1319
1320                         size1 = exif_app1_xmp_t[0];
1321                         size2 = exif_app1_xmp_t[1];
1322
1323                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
1324
1325                         xmp_data = (char *)malloc(exif_app1_xmp_size);
1326                         if (xmp_data != NULL) {
1327                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
1328                                 ptr = xmp_data;
1329
1330                                 while (exif_app1_xmp_size >= 0) {
1331                                         exif_app1_xmp_size--;
1332                                         fdata = fgetc(fp);
1333                                         if (fdata == EOF)
1334                                                 continue;
1335                                         if (fdata == '\0')
1336                                                 continue;
1337                                         *ptr = (char)fdata;
1338                                         ptr++;
1339                                         temp++;
1340                                 }
1341                                 ptr = ptr - temp;
1342
1343                                 if (strstr(ptr, "UsePanoramaViewer")
1344                                 && strstr(ptr, "True")
1345                                 && strstr(ptr, "ProjectionType")
1346                                 && strstr(ptr, "equirectangular"))
1347                                         result = 1;
1348
1349                                 SAFE_FREE(xmp_data);
1350                         } else {
1351                                 media_svc_error("malloc failed");
1352                         }
1353
1354                         if (fp) {
1355                                 fclose(fp);
1356                                 fp = NULL;
1357                         }
1358                         return result;
1359                 } else {
1360                         goto ERROR;
1361                 }
1362         } else {
1363                 goto ERROR;
1364         }
1365 ERROR:
1366         if (fp) {
1367                 fclose(fp);
1368                 fp = NULL;
1369         }
1370         return 0;
1371 }
1372
1373 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1374 {
1375         int ret = MS_MEDIA_ERR_NONE;
1376         double value = 0.0;
1377         int orient_value = 0;
1378         int exif_width = 0;
1379         int exif_height = 0;
1380         ExifData *ed = NULL;
1381         int has_datetaken = FALSE;
1382         int datetaken_size = 19;
1383         double fnumber = 0.0;
1384         int iso = 0;
1385         char *path = NULL;
1386
1387         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1388         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1389         char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1390         char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1391
1392         memset(buf, 0x00, sizeof(buf));
1393         memset(description_buf, 0x00, sizeof(description_buf));
1394         memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1395         memset(model_buf, 0x00, sizeof(model_buf));
1396
1397         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1398                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1399                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1400         }
1401
1402         path = content_info->path;
1403         if (!STRING_VALID(path)) {
1404                 media_svc_error("Invalid Path");
1405                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1406         }
1407
1408         /* Load an ExifData object from an EXIF file */
1409         ed = exif_data_new_from_file(path);
1410
1411         if (!ed) {
1412                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1413                 goto GET_WIDTH_HEIGHT;
1414         }
1415
1416         content_info->media_meta.is_360 = image_360_check(path);
1417
1418         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1419                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1420                         if (strlen(buf) > 0) {
1421                                 if (strcmp(buf, "S") == 0)
1422                                         value = -1 * value;
1423                         }
1424                         content_info->media_meta.latitude = value;
1425                 } else {
1426                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1427                 }
1428         } else {
1429                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1430         }
1431
1432         memset(buf, 0x00, sizeof(buf));
1433
1434         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1435                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1436                         if (strlen(buf) > 0) {
1437                                 if (strcmp(buf, "W") == 0)
1438                                         value = -1 * value;
1439                         }
1440                         content_info->media_meta.longitude = value;
1441                 } else {
1442                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1443                 }
1444         } else {
1445                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1446         }
1447
1448         memset(buf, 0x00, sizeof(buf));
1449
1450         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1451                 if (strlen(description_buf) == 0) {
1452                         /*media_svc_debug("Use 'No description'"); */
1453                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1454                         if (ret != MS_MEDIA_ERR_NONE)
1455                                 media_svc_error("strcpy error");
1456                 } else {
1457                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1458                         if (ret != MS_MEDIA_ERR_NONE)
1459                                 media_svc_error("strcpy error");
1460                 }
1461         } else {
1462                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1463                 if (ret != MS_MEDIA_ERR_NONE)
1464                         media_svc_error("strcpy error");
1465         }
1466
1467         memset(buf, 0x00, sizeof(buf));
1468
1469         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1470                 if (strlen(buf) == 0) {
1471                         /*media_svc_debug("time is NULL"); */
1472                 } else {
1473                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1474                         if (ret != MS_MEDIA_ERR_NONE) {
1475                                 media_svc_error("strcpy error");
1476                         } else {
1477                                 has_datetaken = TRUE;
1478                                 /* This is same as recorded_date */
1479                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1480                                 if (ret != MS_MEDIA_ERR_NONE)
1481                                         media_svc_error("strcpy error");
1482                         }
1483                 }
1484         }
1485
1486         memset(buf, 0x00, sizeof(buf));
1487
1488         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1489                 if (strlen(buf) == 0) {
1490                         /*media_svc_debug("time is NULL"); */
1491                 } else {
1492                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1493                         if (ret != MS_MEDIA_ERR_NONE) {
1494                                 media_svc_error("strcpy error");
1495                         } else {
1496                                 has_datetaken = TRUE;
1497                                 /* This is same as recorded_date */
1498                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1499                                 if (ret != MS_MEDIA_ERR_NONE)
1500                                         media_svc_error("strcpy error");
1501                         }
1502                 }
1503         }
1504
1505         if (has_datetaken) {
1506                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1507                 if (content_info->timeline == 0)
1508                         content_info->timeline = content_info->modified_time;
1509                 else
1510                         media_svc_debug("Timeline : %ld", content_info->timeline);
1511         }
1512
1513         /* Get exposure_time value from exif. */
1514         if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1515                 if (strlen(exposure_time_buf) == 0) {
1516                         /* media_svc_debug("exposure_time_buf is NULL"); */
1517                 } else {
1518                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1519                         if (ret != MS_MEDIA_ERR_NONE)
1520                                 media_svc_error("strcpy error");
1521                 }
1522         }
1523
1524         /* Get fnumber value from exif. */
1525         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1526                 if (fnumber > 0.0)
1527                         content_info->media_meta.fnumber = fnumber;
1528                 else
1529                         content_info->media_meta.fnumber = 0.0;
1530         } else {
1531                 content_info->media_meta.fnumber = 0.0;
1532         }
1533
1534         /* Get iso value from exif. */
1535         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1536                 if (iso > 0)
1537                         content_info->media_meta.iso = iso;
1538                 else
1539                         content_info->media_meta.iso = 0;
1540         } else {
1541                 content_info->media_meta.iso = 0;
1542         }
1543
1544         /* Get model value from exif. */
1545         if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1546                 if (strlen(model_buf) == 0) {
1547                         /* media_svc_debug("model_buf is NULL"); */
1548                 } else {
1549                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1550                         if (ret != MS_MEDIA_ERR_NONE)
1551                                 media_svc_error("strcpy error");
1552                 }
1553         }
1554
1555         /* Get orientation value from exif. */
1556         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1557                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1558                         content_info->media_meta.orientation = orient_value;
1559                 else
1560                         content_info->media_meta.orientation = 0;
1561         } else {
1562                 content_info->media_meta.orientation = 0;
1563         }
1564
1565         /* Get width value from exif. */
1566         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1567                 if (exif_width > 0)
1568                         content_info->media_meta.width = exif_width;
1569                 else
1570                         content_info->media_meta.width = 0;
1571         } else {
1572                 content_info->media_meta.width = 0;
1573         }
1574
1575         /* Get height value from exif. */
1576         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1577                 if (exif_height > 0)
1578                         content_info->media_meta.height = exif_height;
1579                 else
1580                         content_info->media_meta.height = 0;
1581         } else {
1582                 content_info->media_meta.height = 0;
1583         }
1584
1585         if (ed != NULL) exif_data_unref(ed);
1586
1587 GET_WIDTH_HEIGHT:
1588
1589         if (content_info->media_meta.width == 0 ||
1590                 content_info->media_meta.height == 0) {
1591                 /*Get image width, height*/
1592                 unsigned int img_width = 0;
1593                 unsigned int img_height = 0;
1594                 ImgCodecType img_type = IMG_CODEC_NONE;
1595
1596                 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1597
1598                 if (content_info->media_meta.width == 0)
1599                         content_info->media_meta.width = img_width;
1600
1601                 if (content_info->media_meta.height == 0)
1602                         content_info->media_meta.height = img_height;
1603         }
1604
1605         return MS_MEDIA_ERR_NONE;
1606 }
1607
1608 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1609 {
1610         MMHandleType tag = 0;
1611         char *p = NULL;
1612         int size = -1;
1613         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1614         int mmf_error = FILEINFO_ERROR_NONE;
1615         char *err_attr_name = NULL;
1616         char *title = NULL;
1617         int album_id = 0;
1618         int ret = MS_MEDIA_ERR_NONE;
1619
1620         /*Get Content Tag attribute ===========*/
1621         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1622
1623         if (mmf_error == FILEINFO_ERROR_NONE) {
1624                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1625                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1626                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1627                         if (ret != MS_MEDIA_ERR_NONE)
1628                                 media_svc_error("strcpy error");
1629
1630                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1631                 } else {
1632                         SAFE_FREE(err_attr_name);
1633                         /*media_svc_debug("album - unknown"); */
1634                 }
1635
1636                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1637                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1638                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1639                         if (ret != MS_MEDIA_ERR_NONE)
1640                                 media_svc_error("strcpy error");
1641                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1642                 } else {
1643                         SAFE_FREE(err_attr_name);
1644                         /*media_svc_debug("artist - unknown"); */
1645                 }
1646
1647                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1648                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1649                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1650                         if (ret != MS_MEDIA_ERR_NONE)
1651                                 media_svc_error("strcpy error");
1652                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1653                 } else {
1654                         SAFE_FREE(err_attr_name);
1655                         /*media_svc_debug("album_artist - unknown"); */
1656                 }
1657
1658                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1659                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1660                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1661                         if (ret != MS_MEDIA_ERR_NONE)
1662                                 media_svc_error("strcpy error");
1663
1664                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1665                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1666                         /*
1667                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1668                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1669                         }
1670                         */
1671                 } else {
1672                         SAFE_FREE(err_attr_name);
1673                         /*media_svc_debug("genre - unknown"); */
1674                 }
1675
1676                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1677                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1678                         if (!isspace(*p)) {
1679                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1680                                 if (ret != MS_MEDIA_ERR_NONE)
1681                                         media_svc_error("strcpy error");
1682
1683                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1684                         } else {
1685                                 int idx = 0;
1686
1687                                 for (idx = 0; idx < size; idx++) {
1688                                         if (isspace(*p)) {
1689                                                 media_svc_debug("SPACE [%s]", p);
1690                                                 p++;
1691                                                 continue;
1692                                         } else {
1693                                                 media_svc_debug("Not SPACE [%s]", p);
1694                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1695                                                 if (ret != MS_MEDIA_ERR_NONE)
1696                                                         media_svc_error("strcpy error");
1697                                                 break;
1698                                         }
1699                                 }
1700
1701                                 if (idx == size) {
1702                                         media_svc_debug("Can't extract title. All string is space");
1703                                         title = _media_svc_get_title_from_filepath(content_info->path);
1704                                         if (title) {
1705                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1706                                                 if (ret != MS_MEDIA_ERR_NONE)
1707                                                         media_svc_error("strcpy error");
1708                                                 SAFE_FREE(title);
1709                                         } else {
1710                                                 media_svc_error("Can't extract title");
1711                                         }
1712                                 }
1713                         }
1714                 } else {
1715                         SAFE_FREE(err_attr_name);
1716                         title = _media_svc_get_title_from_filepath(content_info->path);
1717                         if (title) {
1718                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1719                                 if (ret != MS_MEDIA_ERR_NONE)
1720                                         media_svc_error("strcpy error");
1721                                 SAFE_FREE(title);
1722                         } else {
1723                                 media_svc_error("Can't extract title");
1724                         }
1725                 }
1726
1727                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1728                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1729                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1730                         if (ret != MS_MEDIA_ERR_NONE)
1731                                 media_svc_error("strcpy error");
1732                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1733                 } else {
1734                         SAFE_FREE(err_attr_name);
1735                 }
1736
1737                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1738                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1739                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1740                         if (ret != MS_MEDIA_ERR_NONE)
1741                                 media_svc_error("strcpy error");
1742                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1743                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1744                 } else {
1745                         /*media_svc_debug("composer - unknown"); */
1746                         SAFE_FREE(err_attr_name);
1747                 }
1748
1749                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1750                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1751                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1752                         if (ret != MS_MEDIA_ERR_NONE)
1753                                 media_svc_error("strcpy error");
1754                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1755                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1756                 } else {
1757                         /*media_svc_debug("copyright - unknown"); */
1758                         SAFE_FREE(err_attr_name);
1759                 }
1760
1761                 mmf_error = mm_file_destroy_tag_attrs(tag);
1762                 if (mmf_error != FILEINFO_ERROR_NONE)
1763                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1764         } else {
1765                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1766                 char *no_tag_title = NULL;
1767                 media_svc_error("no tag information");
1768
1769                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1770                 if (no_tag_title) {
1771                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1772                         if (ret != MS_MEDIA_ERR_NONE)
1773                                 media_svc_error("strcpy error");
1774                         SAFE_FREE(no_tag_title);
1775                 } else {
1776                         media_svc_error("Can't extract title");
1777                 }
1778
1779                 content_info->album_id = album_id;
1780         }
1781
1782         return MS_MEDIA_ERR_NONE;
1783 }
1784
1785 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1786 {
1787         MMHandleType content = 0;
1788         MMHandleType tag = 0;
1789         char *p = NULL;
1790         unsigned char *image = NULL;
1791         unsigned int size = 0;
1792         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1793         int mmf_error = FILEINFO_ERROR_NONE;
1794         char *err_attr_name = NULL;
1795         char *title = NULL;
1796         bool extract_thumbnail = FALSE;
1797         bool append_album = FALSE;
1798         int album_id = 0;
1799         int ret = MS_MEDIA_ERR_NONE;
1800         int cdis_value = 0;
1801         unsigned int resize_size = 0;
1802         unsigned char *resize_image = NULL;
1803
1804         /*Get Content Tag attribute ===========*/
1805         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1806
1807         if (mmf_error == FILEINFO_ERROR_NONE) {
1808                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1809                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1810                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1811                         if (ret != MS_MEDIA_ERR_NONE)
1812                                 media_svc_error("strcpy error");
1813
1814                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1815                 } else {
1816                         SAFE_FREE(err_attr_name);
1817                         /*media_svc_debug("album - unknown"); */
1818                 }
1819
1820                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1821                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1822                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1823                         if (ret != MS_MEDIA_ERR_NONE)
1824                                 media_svc_error("strcpy error");
1825                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1826                 } else {
1827                         SAFE_FREE(err_attr_name);
1828                         /*media_svc_debug("artist - unknown"); */
1829                 }
1830
1831                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1832                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1833                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1834                         if (ret != MS_MEDIA_ERR_NONE)
1835                                 media_svc_error("strcpy error");
1836                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1837                 } else {
1838                         SAFE_FREE(err_attr_name);
1839                         /*media_svc_debug("album_artist - unknown"); */
1840                 }
1841
1842                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1843                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1844                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1845                         if (ret != MS_MEDIA_ERR_NONE)
1846                                 media_svc_error("strcpy error");
1847
1848                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1849                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1850                         /*
1851                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1852                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1853                         }
1854                         */
1855                 } else {
1856                         SAFE_FREE(err_attr_name);
1857                         /*media_svc_debug("genre - unknown"); */
1858                 }
1859
1860                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1861                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1862                         if (!isspace(*p)) {
1863                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1864                                 if (ret != MS_MEDIA_ERR_NONE)
1865                                         media_svc_error("strcpy error");
1866
1867                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1868                         } else {
1869                                 int idx = 0;
1870
1871                                 for (idx = 0; idx < size; idx++) {
1872                                         if (isspace(*p)) {
1873                                                 media_svc_debug("SPACE [%s]", p);
1874                                                 p++;
1875                                                 continue;
1876                                         } else {
1877                                                 media_svc_debug("Not SPACE [%s]", p);
1878                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1879                                                 if (ret != MS_MEDIA_ERR_NONE)
1880                                                         media_svc_error("strcpy error");
1881                                                 break;
1882                                         }
1883                                 }
1884
1885                                 if (idx == size) {
1886                                         media_svc_debug("Can't extract title. All string is space");
1887                                         title = _media_svc_get_title_from_filepath(content_info->path);
1888                                         if (title) {
1889                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1890                                                 if (ret != MS_MEDIA_ERR_NONE)
1891                                                         media_svc_error("strcpy error");
1892                                                 SAFE_FREE(title);
1893                                         } else {
1894                                                 media_svc_error("Can't extract title");
1895                                         }
1896                                 }
1897                         }
1898                 } else {
1899                         SAFE_FREE(err_attr_name);
1900                         title = _media_svc_get_title_from_filepath(content_info->path);
1901                         if (title) {
1902                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1903                                 if (ret != MS_MEDIA_ERR_NONE)
1904                                         media_svc_error("strcpy error");
1905                                 SAFE_FREE(title);
1906                         } else {
1907                                 media_svc_error("Can't extract title");
1908                         }
1909                 }
1910
1911                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1912                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1913                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1914                         if (ret != MS_MEDIA_ERR_NONE)
1915                                 media_svc_error("strcpy error");
1916                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1917                 } else {
1918                         SAFE_FREE(err_attr_name);
1919                 }
1920
1921                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1922
1923                 if (mmf_error != FILEINFO_ERROR_NONE)
1924                         SAFE_FREE(err_attr_name);
1925
1926                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1927                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1928                         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1929                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1930                                 char *time_info = (char*)calloc(1, (size + 1));
1931                                 char *p_value = p;
1932                                 char *time_value = time_info;
1933                                 if (time_info != NULL) {
1934                                         while (*p_value != '\0') {
1935                                                 if (*p_value == '-')
1936                                                         *time_value = ':';
1937                                                 else
1938                                                         *time_value = *p_value;
1939                                                 time_value++;
1940                                                 p_value++;
1941                                         }
1942                                         *time_value = '\0';
1943                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1944                                         SAFE_FREE(time_info);
1945                                 } else {
1946                                         media_svc_error("memory allocation error");
1947                                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
1948                                 }
1949                         } else {
1950                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1951                         }
1952
1953                         if (ret != MS_MEDIA_ERR_NONE) {
1954                                 media_svc_error("strcpy error");
1955                         } else {
1956                                 /* This is same as datetaken */
1957 #if 0
1958                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1959 #else
1960                                 int datetaken_size = 19;
1961                                 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1962 #endif
1963                                 if (ret != MS_MEDIA_ERR_NONE)
1964                                         media_svc_error("strcpy error");
1965
1966                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1967                                 if (content_info->timeline == 0)
1968                                         content_info->timeline = content_info->modified_time;
1969                                 else
1970                                         media_svc_debug("Timeline : %ld", content_info->timeline);
1971                         }
1972                         /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1973                 } else {
1974                         SAFE_FREE(err_attr_name);
1975                 }
1976
1977                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1978                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1979                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1980                         if (ret != MS_MEDIA_ERR_NONE)
1981                                 media_svc_error("strcpy error");
1982                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1983                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1984                 } else {
1985                         /*media_svc_debug("composer - unknown"); */
1986                         SAFE_FREE(err_attr_name);
1987                 }
1988
1989                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1990                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1991                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1992                         if (ret != MS_MEDIA_ERR_NONE)
1993                                 media_svc_error("strcpy error");
1994                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1995                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1996                 } else {
1997                         /*media_svc_debug("copyright - unknown"); */
1998                         SAFE_FREE(err_attr_name);
1999                 }
2000
2001                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
2002                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2003                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
2004                         if (ret != MS_MEDIA_ERR_NONE)
2005                                 media_svc_error("strcpy error");
2006                 } else {
2007                         SAFE_FREE(err_attr_name);
2008                 }
2009
2010                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
2011                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
2012                         int year = 0;
2013                         if ((p != NULL) && ((ret != __media_svc_safe_atoi(p, &year)) == MS_MEDIA_ERR_NONE)) {
2014                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
2015                                 if (ret != MS_MEDIA_ERR_NONE)
2016                                         media_svc_error("strcpy error");
2017                         } else {
2018                                 media_svc_debug("Wrong Year Information [%s]", p);
2019                         }
2020                 } else {
2021                         SAFE_FREE(err_attr_name);
2022                 }
2023
2024                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
2025                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2026                         content_info->media_meta.rating = atoi(p);
2027                 } else {
2028                         SAFE_FREE(err_attr_name);
2029                         content_info->media_meta.rating = 0;
2030                 }
2031
2032                 /*Initialize album_id to 0. below code will set the album_id*/
2033                 content_info->album_id = album_id;
2034                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
2035
2036                 if (ret != MS_MEDIA_ERR_NONE) {
2037                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
2038                                 media_svc_debug("album does not exist. So start to make album art");
2039                                 extract_thumbnail = TRUE;
2040                                 append_album = TRUE;
2041                         } else {
2042                                 extract_thumbnail = TRUE;
2043                                 append_album = FALSE;
2044                         }
2045                 } else {
2046                         content_info->album_id = album_id;
2047                         append_album = FALSE;
2048
2049                         if ((!g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
2050                                 (!g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
2051                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
2052                                 extract_thumbnail = TRUE;
2053                         } else {
2054                                 media_svc_debug("album already exists. don't need to make album art");
2055                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2056                                 extract_thumbnail = TRUE;
2057                         }
2058                 }
2059
2060                 /*Do not extract artwork for the USB Storage content*/
2061                 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2062                         extract_thumbnail = FALSE;
2063
2064                 if (extract_thumbnail == TRUE) {
2065                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2066                         if (mmf_error != FILEINFO_ERROR_NONE) {
2067                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2068                                 SAFE_FREE(err_attr_name);
2069                         } else {
2070                                 /*media_svc_debug("artwork size1 [%d]", size); */
2071                         }
2072
2073                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2074                         if (mmf_error != FILEINFO_ERROR_NONE) {
2075                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2076                                 SAFE_FREE(err_attr_name);
2077                         } else {
2078                                 /*media_svc_debug("artwork size2 [%d]", size); */
2079                         }
2080                         if (image != NULL && size > 0) {
2081                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2082                                 int artwork_mime_size = -1;
2083
2084                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2085                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2086                                         ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2087                                         if (ret != MS_MEDIA_ERR_NONE)
2088                                                 media_svc_error("Fail to Get Thumbnail Path");
2089                                         /* albumart resizing */
2090                                         __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2091                                 } else {
2092                                         SAFE_FREE(err_attr_name);
2093                                 }
2094
2095                                 if (strlen(thumb_path) > 0) {
2096                                         ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2097                                         if (ret != MS_MEDIA_ERR_NONE) {
2098                                                 media_svc_error("Fail to Save Thumbnail Image");
2099                                         } else {
2100                                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2101                                                 if (ret != MS_MEDIA_ERR_NONE)
2102                                                         media_svc_error("strcpy error");
2103                                         }
2104                                 }
2105                         }
2106                 }
2107
2108                 if (append_album == TRUE) {
2109                         if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
2110                                 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
2111                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2112                         else
2113                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2114
2115                         content_info->album_id = album_id;
2116                 }
2117
2118                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2119                         double longitude = 0.0;
2120                         double latitude = 0.0;
2121                         double altitude = 0.0;
2122
2123                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2124                         content_info->media_meta.longitude = longitude;
2125                         content_info->media_meta.latitude = latitude;
2126                         content_info->media_meta.altitude = altitude;
2127
2128                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2129                         if (mmf_error != FILEINFO_ERROR_NONE) {
2130                                 cdis_value = 0;
2131                                 SAFE_FREE(err_attr_name);
2132                         }
2133
2134                         media_svc_debug("CDIS : %d", cdis_value);
2135
2136                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
2137                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2138                                 content_info->media_meta.orientation = atoi(p);
2139                         } else {
2140                                 SAFE_FREE(err_attr_name);
2141                                 content_info->media_meta.orientation = 0;
2142                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
2143                         }
2144                 }
2145
2146                 mmf_error = mm_file_destroy_tag_attrs(tag);
2147                 if (mmf_error != FILEINFO_ERROR_NONE)
2148                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2149         }       else {
2150                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2151                 char *no_tag_title = NULL;
2152                 media_svc_error("no tag information");
2153
2154                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2155                 if (no_tag_title) {
2156                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2157                         if (ret != MS_MEDIA_ERR_NONE)
2158                                 media_svc_error("strcpy error");
2159                         SAFE_FREE(no_tag_title);
2160                 } else {
2161                         media_svc_error("Can't extract title");
2162                 }
2163
2164                 content_info->album_id = album_id;
2165         }
2166
2167         /*Get Content attribute ===========*/
2168         if (cdis_value == 1)
2169                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2170         else
2171                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2172
2173         if (mmf_error == FILEINFO_ERROR_NONE) {
2174                 /*Common attribute*/
2175                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2176                 if (mmf_error != FILEINFO_ERROR_NONE) {
2177                         SAFE_FREE(err_attr_name);
2178                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2179                 } else {
2180                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2181                 }
2182
2183                 /*Sound/Music attribute*/
2184                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2185
2186                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2187                         if (mmf_error != FILEINFO_ERROR_NONE) {
2188                                 SAFE_FREE(err_attr_name);
2189                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2190                         } else {
2191                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2192                         }
2193
2194                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2195                         if (mmf_error != FILEINFO_ERROR_NONE) {
2196                                 SAFE_FREE(err_attr_name);
2197                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2198                         } else {
2199                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2200                         }
2201
2202                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2203                         if (mmf_error != FILEINFO_ERROR_NONE) {
2204                                 SAFE_FREE(err_attr_name);
2205                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2206                         } else {
2207                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2208                         }
2209
2210                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2211                         if (mmf_error != FILEINFO_ERROR_NONE) {
2212                                 SAFE_FREE(err_attr_name);
2213                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2214                         } else {
2215                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2216                         }
2217                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
2218                         int audio_bitrate = 0;
2219                         int video_bitrate = 0;
2220
2221                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2222                         if (mmf_error != FILEINFO_ERROR_NONE) {
2223                                 SAFE_FREE(err_attr_name);
2224                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2225                         } else {
2226                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2227                         }
2228
2229                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2230                         if (mmf_error != FILEINFO_ERROR_NONE) {
2231                                 SAFE_FREE(err_attr_name);
2232                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2233                         } else {
2234                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2235                         }
2236
2237                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2238
2239                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2240                         if (mmf_error != FILEINFO_ERROR_NONE) {
2241                                 SAFE_FREE(err_attr_name);
2242                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2243                         } else {
2244                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2245                         }
2246
2247                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2248                         if (mmf_error != FILEINFO_ERROR_NONE) {
2249                                 SAFE_FREE(err_attr_name);
2250                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2251                         } else {
2252                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2253                         }
2254                 } else {
2255                         media_svc_error("Not support type");
2256                         mmf_error = mm_file_destroy_content_attrs(content);
2257                         if (mmf_error != FILEINFO_ERROR_NONE)
2258                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2259
2260                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2261                 }
2262
2263                 mmf_error = mm_file_destroy_content_attrs(content);
2264                 if (mmf_error != FILEINFO_ERROR_NONE)
2265                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
2266         } else {
2267                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2268         }
2269
2270         return MS_MEDIA_ERR_NONE;
2271 }
2272
2273 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2274 {
2275         media_svc_retm_if(content_info == NULL, "content info is NULL");
2276
2277         /* Delete media_svc_content_info_s */
2278         SAFE_FREE(content_info->media_uuid);
2279         SAFE_FREE(content_info->path);
2280         SAFE_FREE(content_info->file_name);
2281         SAFE_FREE(content_info->mime_type);
2282         SAFE_FREE(content_info->folder_uuid);
2283         SAFE_FREE(content_info->thumbnail_path);
2284         SAFE_FREE(content_info->storage_uuid);
2285
2286         /* Delete media_svc_content_meta_s */
2287         SAFE_FREE(content_info->media_meta.title);
2288         SAFE_FREE(content_info->media_meta.album);
2289         SAFE_FREE(content_info->media_meta.artist);
2290         SAFE_FREE(content_info->media_meta.album_artist);
2291         SAFE_FREE(content_info->media_meta.genre);
2292         SAFE_FREE(content_info->media_meta.composer);
2293         SAFE_FREE(content_info->media_meta.year);
2294         SAFE_FREE(content_info->media_meta.recorded_date);
2295         SAFE_FREE(content_info->media_meta.copyright);
2296         SAFE_FREE(content_info->media_meta.track_num);
2297         SAFE_FREE(content_info->media_meta.description);
2298         SAFE_FREE(content_info->media_meta.datetaken);
2299         SAFE_FREE(content_info->media_meta.exposure_time);
2300         SAFE_FREE(content_info->media_meta.model);
2301         SAFE_FREE(content_info->media_meta.weather);
2302         SAFE_FREE(content_info->media_meta.category);
2303         SAFE_FREE(content_info->media_meta.keyword);
2304         SAFE_FREE(content_info->media_meta.location_tag);
2305         SAFE_FREE(content_info->media_meta.content_name);
2306         SAFE_FREE(content_info->media_meta.age_rating);
2307         SAFE_FREE(content_info->media_meta.author);
2308         SAFE_FREE(content_info->media_meta.provider);
2309
2310         SAFE_FREE(content_info->media_meta.title_pinyin);
2311         SAFE_FREE(content_info->media_meta.album_pinyin);
2312         SAFE_FREE(content_info->media_meta.artist_pinyin);
2313         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2314         SAFE_FREE(content_info->media_meta.genre_pinyin);
2315         SAFE_FREE(content_info->media_meta.composer_pinyin);
2316         SAFE_FREE(content_info->media_meta.copyright_pinyin);
2317         SAFE_FREE(content_info->media_meta.description_pinyin);
2318
2319         return;
2320 }
2321
2322 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2323 {
2324         if (STRING_VALID(path)) {
2325                 char *internal_path = _media_svc_get_path(uid);
2326                 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2327                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2328                 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2329                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2330                 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2331                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2332                 } else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)) {
2333                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2334                 } else {
2335                         media_svc_error("Invalid Path");
2336                         media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
2337                         SAFE_FREE(internal_path);
2338                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2339                 }
2340                 SAFE_FREE(internal_path);
2341         } else {
2342                 media_svc_error("INVALID parameter");
2343                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2344         }
2345
2346         return MS_MEDIA_ERR_NONE;
2347 }
2348
2349 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2350 {
2351         char result[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
2352         memset(result, 0x00, sizeof(result));
2353
2354         if (STRING_VALID(s) && STRING_VALID(olds) && STRING_VALID(news)) {
2355                 if (strncmp(s, olds, strlen(olds)) == 0) {
2356                         snprintf(result, sizeof(result), "%s%s", news, s + strlen(olds));
2357                 }
2358         }
2359
2360         if (STRING_VALID(result))
2361                 return g_strdup(result);
2362         else
2363                 return NULL;
2364
2365 }
2366
2367 bool _media_svc_is_drm_file(const char *path)
2368 {
2369         return FALSE;
2370 }
2371
2372 int _media_svc_request_thumbnail_with_origin_size(const char *path, char *thumb_path, int max_length, int *origin_width, int *origin_height, uid_t uid)
2373 {
2374         int ret = MS_MEDIA_ERR_NONE;
2375
2376         ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2377
2378         if (ret != MS_MEDIA_ERR_NONE) {
2379                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2380                 ret = MS_MEDIA_ERR_INTERNAL;
2381         } else {
2382                 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2383         }
2384
2385         return ret;
2386 }
2387
2388 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2389 {
2390         int ret = MS_MEDIA_ERR_NONE;
2391         int size = 0;
2392         pinyin_name_s *pinyinname = NULL;
2393
2394         *pinyin_str = NULL;
2395
2396         if (!STRING_VALID(src_str)) {
2397                 media_svc_debug("String is invalid");
2398                 return ret;
2399         }
2400
2401         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2402         if (ret == MS_MEDIA_ERR_NONE) {
2403                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
2404                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
2405                 else
2406                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
2407         }
2408
2409         _media_svc_pinyin_free(pinyinname, size);
2410
2411         return ret;
2412 }
2413
2414 bool _media_svc_check_pinyin_support(void)
2415 {
2416         /*Check CSC*/
2417         return TRUE;
2418 }
2419
2420 char* _media_svc_get_title_from_path(const char *path)
2421 {
2422         char *filename = NULL;
2423         char *title = NULL;
2424         char    *ext = NULL;
2425         int filename_len = -1;
2426         int new_title_len = -1;
2427
2428         if (!path) {
2429                 media_svc_error("path is NULL");
2430                 return NULL;
2431         }
2432
2433         filename = g_path_get_basename(path);
2434         if (!STRING_VALID(filename)) {
2435                 media_svc_error("wrong file name");
2436                 SAFE_FREE(filename);
2437                 return NULL;
2438         }
2439
2440         filename_len = strlen(filename);
2441
2442         ext = g_strrstr(filename, ".");
2443         if (!ext) {
2444                 media_svc_error("there is no file extention");
2445                 return filename;
2446         }
2447
2448         new_title_len = filename_len - strlen(ext);
2449         if (new_title_len < 1) {
2450                 media_svc_error("title length is zero");
2451                 SAFE_FREE(filename);
2452                 return NULL;
2453         }
2454
2455         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2456
2457         SAFE_FREE(filename);
2458
2459         media_svc_debug("extract title is [%s]", title);
2460
2461         return title;
2462 }
2463
2464 #define BUF_LENGHT 256
2465
2466 void _media_svc_print_stderror(void)
2467 {
2468         char buf[BUF_LENGHT] = {0,};
2469
2470         media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));
2471 }
2472
2473 int _media_svc_get_media_type(const char *path, int *mediatype)
2474 {
2475         int ret = MS_MEDIA_ERR_NONE;
2476         char mime_type[256] = {0};
2477         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
2478
2479         ret = __media_svc_get_mime_type(path, mime_type);
2480         if (ret == MS_MEDIA_ERR_NONE) {
2481                 __media_svc_get_media_type(path, mime_type, &media_type);
2482         } else {
2483                 media_svc_error("__media_svc_get_mime_type failed");
2484         }
2485
2486         *mediatype = media_type;
2487
2488         return ret;
2489 }
2490