Svace issue fix. use snprintf instead of memcpy
[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_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
808 {
809         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
810
811         if (!image) {
812                 media_svc_error("invalid image..");
813                 return MS_MEDIA_ERR_INVALID_PARAMETER;
814         }
815
816         struct statfs fs;
817         char *thumb_path = __media_svc_get_thumb_path(uid);
818         if (!STRING_VALID(thumb_path)) {
819                 media_svc_error("fail to get thumb_path");
820                 return MS_MEDIA_ERR_INTERNAL;
821         }
822
823         if (-1 == statfs(thumb_path, &fs)) {
824                 media_svc_error("error in statfs");
825                 SAFE_FREE(thumb_path);
826                 return MS_MEDIA_ERR_INTERNAL;
827         }
828
829         SAFE_FREE(thumb_path);
830
831         long bsize_kbytes = fs.f_bsize >> 10;
832
833         if ((bsize_kbytes * fs.f_bavail) < 1024) {
834                 media_svc_error("not enought space...");
835                 return MS_MEDIA_ERR_INTERNAL;
836         }
837
838         FILE *fp = NULL;
839         int nwrite = -1;
840         if (image != NULL && size > 0) {
841                 fp = fopen(image_path, "w");
842
843                 if (fp == NULL) {
844                         media_svc_error("failed to open file");
845                         return MS_MEDIA_ERR_INTERNAL;
846                 }
847
848                 nwrite = fwrite(image, 1, size, fp);
849                 if (nwrite != size) {
850                         media_svc_error("failed to write thumbnail");
851                         fclose(fp);
852                         return MS_MEDIA_ERR_INTERNAL;
853                 }
854                 fclose(fp);
855         }
856
857         return MS_MEDIA_ERR_NONE;
858 }
859
860 static char *_media_svc_get_title_from_filepath(const char *path)
861 {
862         char *filename = NULL;
863         char *title = NULL;
864         char    *ext = NULL;
865         int filename_len = -1;
866         int new_title_len = -1;
867
868         if (!path) {
869                 media_svc_error("path is NULL");
870                 return NULL;
871         }
872
873         filename = g_path_get_basename(path);
874         if (!STRING_VALID(filename)) {
875                 media_svc_error("wrong file name");
876                 SAFE_FREE(filename);
877                 return NULL;
878         }
879
880         filename_len = strlen(filename);
881
882         ext = g_strrstr(filename, ".");
883         if (!ext) {
884                 media_svc_error("there is no file extention");
885                 return filename;
886         }
887
888         new_title_len = filename_len - strlen(ext);
889         if (new_title_len < 1) {
890                 media_svc_error("title length is zero");
891                 SAFE_FREE(filename);
892                 return NULL;
893         }
894
895         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
896
897         SAFE_FREE(filename);
898
899         media_svc_debug("extract title is [%s]", title);
900
901         return title;
902 }
903
904 int _media_svc_rename_file(const char *old_name, const char *new_name)
905 {
906         if ((old_name == NULL) || (new_name == NULL)) {
907                 media_svc_error("invalid file name");
908                 return MS_MEDIA_ERR_INVALID_PARAMETER;
909         }
910
911         if (rename(old_name, new_name) < 0) {
912                 media_svc_stderror(" ");
913                 return MS_MEDIA_ERR_INTERNAL;
914         }
915
916         return MS_MEDIA_ERR_NONE;
917 }
918
919 int _media_svc_remove_file(const char *path)
920 {
921         int result = -1;
922
923         result = remove(path);
924         if (result == 0) {
925                 media_svc_debug("success to remove file");
926                 return MS_MEDIA_ERR_NONE;
927         } else {
928                 media_svc_stderror("fail to remove file result");
929                 return MS_MEDIA_ERR_INTERNAL;
930         }
931 }
932
933 int _media_svc_remove_all_files_in_dir(const char *dir_path)
934 {
935         struct dirent entry;
936         struct dirent *result;
937         struct stat st;
938         char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
939         DIR *dir = NULL;
940
941         dir = opendir(dir_path);
942         if (dir == NULL) {
943                 media_svc_error("%s is not exist", dir_path);
944                 return MS_MEDIA_ERR_INVALID_PARAMETER;
945         }
946
947         while (!readdir_r(dir, &entry, &result)) {
948                 if (result == NULL)
949                         break;
950
951                 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0)
952                         continue;
953
954                 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
955
956                 if (stat(filename, &st) != 0)
957                         continue;
958
959                 if (S_ISDIR(st.st_mode))
960                         continue;
961
962                 if (unlink(filename) != 0) {
963                         media_svc_stderror("failed to remove");
964                         closedir(dir);
965                         return MS_MEDIA_ERR_INTERNAL;
966                 }
967         }
968
969         closedir(dir);
970         return MS_MEDIA_ERR_NONE;
971 }
972
973 char *_media_svc_get_thumb_internal_path(uid_t uid)
974 {
975         int ret = 0;
976         char *result_path = NULL;
977
978         ret = tzplatform_set_user(uid);
979         if (ret != 0) {
980                 media_svc_error("Invalid UID[%d]", uid);
981                 return NULL;
982         } else {
983                 const char *result = tzplatform_mkpath(TZ_USER_SHARE, "media/.thumb/phone");
984                 result_path = strndup(result, strlen(result));
985                 tzplatform_reset_user();
986         }
987
988         return result_path;
989 }
990
991 char *_media_svc_get_thumb_external_path(uid_t uid)
992 {
993         int ret = 0;
994         char *result_path = NULL;
995
996         ret = tzplatform_set_user(uid);
997         if (ret != 0) {
998                 media_svc_error("Invalid UID[%d]", uid);
999                 return NULL;
1000         } else {
1001                 const char *result = tzplatform_mkpath(TZ_USER_SHARE, "media/.thumb/mmc");
1002                 result_path = strndup(result, strlen(result));
1003                 tzplatform_reset_user();
1004         }
1005
1006         return result_path;
1007 }
1008
1009 static int __media_svc_check_thumb_dir(const char *thumb_dir)
1010 {
1011         DIR *dir = NULL;
1012
1013         dir = opendir(thumb_dir);
1014         if (dir != NULL)
1015                 closedir(dir);
1016         else
1017                 return MS_MEDIA_ERR_INTERNAL;
1018
1019         return MS_MEDIA_ERR_NONE;
1020 }
1021
1022 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)
1023 {
1024         int ret = MS_MEDIA_ERR_NONE;
1025         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
1026         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
1027         char *thumb_dir = NULL;
1028         char hash[255 + 1] = {0, };
1029         char *thumbfile_ext = NULL;
1030         char *internal_thumb_path = _media_svc_get_thumb_internal_path(uid);
1031         char *external_thumb_path = _media_svc_get_thumb_external_path(uid);
1032
1033         if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
1034                 media_svc_error("fail to get thumbnail path");
1035                 SAFE_FREE(internal_thumb_path);
1036                 SAFE_FREE(external_thumb_path);
1037                 return MS_MEDIA_ERR_INTERNAL;
1038         }
1039
1040         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
1041
1042         ret = __media_svc_check_thumb_dir(thumb_dir);
1043         if (ret != MS_MEDIA_ERR_NONE) {
1044                 media_svc_error("__media_svc_check_thumb_dir");
1045                 SAFE_FREE(internal_thumb_path);
1046                 SAFE_FREE(external_thumb_path);
1047                 return MS_MEDIA_ERR_INTERNAL;
1048         }
1049
1050         memset(file_ext, 0, sizeof(file_ext));
1051         if (!__media_svc_get_file_ext(pathname, file_ext))
1052                 media_svc_error("get file ext fail");
1053
1054         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1055         if (ret != MS_MEDIA_ERR_NONE) {
1056                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1057                 SAFE_FREE(internal_thumb_path);
1058                 SAFE_FREE(external_thumb_path);
1059                 return MS_MEDIA_ERR_INTERNAL;
1060         }
1061
1062         /*media_svc_debug("img format is [%s]", img_format); */
1063
1064         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1065                 thumbfile_ext = (char *)"jpg";
1066         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1067                 thumbfile_ext = (char *)"png";
1068         } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1069                 thumbfile_ext = (char *)"gif";
1070         } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1071                 thumbfile_ext = (char *)"bmp";
1072         } else {
1073                 media_svc_error("Not proper img format");
1074                 SAFE_FREE(internal_thumb_path);
1075                 SAFE_FREE(external_thumb_path);
1076                 return MS_MEDIA_ERR_INTERNAL;
1077         }
1078
1079         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1080         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1081         /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1082
1083         SAFE_FREE(internal_thumb_path);
1084         SAFE_FREE(external_thumb_path);
1085
1086         return MS_MEDIA_ERR_NONE;
1087 }
1088
1089 int _media_svc_get_file_time(const char *full_path)
1090 {
1091         struct stat statbuf;
1092         int fd = 0;
1093
1094         memset(&statbuf, 0, sizeof(struct stat));
1095         fd = stat(full_path, &statbuf);
1096         if (fd == -1) {
1097                 media_svc_error("stat(%s) fails.", full_path);
1098                 return MS_MEDIA_ERR_INTERNAL;
1099         }
1100
1101         return statbuf.st_mtime;
1102 }
1103
1104 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1105 {
1106         int ret = MS_MEDIA_ERR_NONE;
1107
1108         /* Set default GPS value before extracting meta information */
1109         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1110         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1111         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1112
1113         /* Set filename to title for all media */
1114         char *title = NULL;
1115         title = _media_svc_get_title_from_filepath(content_info->path);
1116         if (title) {
1117                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1118                 if (ret != MS_MEDIA_ERR_NONE)
1119                         media_svc_error("strcpy error");
1120                 SAFE_FREE(title);
1121         } else {
1122                 media_svc_error("Can't extract title");
1123                 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1124                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1125         }
1126
1127         /* Set default value before extracting meta information */
1128         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1129         media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1130
1131         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1132         media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1133
1134         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1135         media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1136
1137         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1138         media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1139
1140         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1141         media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1142
1143         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1144         media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1145
1146         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1147         media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1148
1149         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1150         media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1151
1152         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1153         media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1154
1155         if (refresh) {
1156                 media_svc_debug("refresh");
1157                 return MS_MEDIA_ERR_NONE;
1158         }
1159
1160         content_info->played_count = 0;
1161         content_info->last_played_time = 0;
1162         content_info->last_played_position = 0;
1163         content_info->favourate = 0;
1164         content_info->media_meta.rating = 0;
1165
1166         return MS_MEDIA_ERR_NONE;
1167 }
1168
1169 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1170                         const char *path, media_svc_media_type_e *media_type, bool refresh)
1171 {
1172         int ret = MS_MEDIA_ERR_NONE;
1173         char * media_uuid = NULL;
1174         char * file_name = NULL;
1175         bool drm_type = false;
1176         char mime_type[256] = {0, };
1177
1178         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1179         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1180
1181         struct stat st;
1182         memset(&st, 0, sizeof(struct stat));
1183         if (stat(path, &st) == 0) {
1184                 content_info->modified_time = st.st_mtime;
1185                 content_info->timeline = content_info->modified_time;
1186                 content_info->size = st.st_size;
1187         } else {
1188                 media_svc_stderror("stat failed");
1189         }
1190
1191         _media_svc_set_default_value(content_info, refresh);
1192
1193         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1194         if (refresh) {
1195                 media_svc_debug("refresh");
1196                 return MS_MEDIA_ERR_NONE;
1197         }
1198
1199         ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1200         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1201
1202         content_info->storage_type = storage_type;
1203         time(&content_info->added_time);
1204
1205         media_uuid = _media_info_generate_uuid();
1206         if (media_uuid == NULL) {
1207                 _media_svc_destroy_content_info(content_info);
1208                 return MS_MEDIA_ERR_INTERNAL;
1209         }
1210
1211         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1212         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1213
1214         file_name = g_path_get_basename(path);
1215         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1216         SAFE_FREE(file_name);
1217         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1218
1219         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1220         if drm_contentinfo is not NULL, the file is OMA DRM.*/
1221         ret = __media_svc_get_mime_type(path, mime_type);
1222         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1223
1224         media_svc_debug("mime [%s]", mime_type);
1225         content_info->is_drm = drm_type;
1226
1227         ret = __media_svc_get_media_type(path, mime_type, media_type);
1228         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1229
1230         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1231                 media_svc_error("invalid media_type condition[%d]", *media_type);
1232                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1233         }
1234
1235         ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1236         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1237
1238         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1239
1240         content_info->media_type = *media_type;
1241
1242         return MS_MEDIA_ERR_NONE;
1243 }
1244
1245 int image_360_check(char *path)
1246 {
1247         FILE *fp = NULL;
1248         long app1_size = 0;
1249         int size = 1;
1250         unsigned char exif_header[4];
1251         unsigned char exif_app1[2];
1252         unsigned char exif_app1_xmp[2];
1253         long exif_app1_xmp_size = 0;
1254         unsigned char exif_app1_xmp_t[2];
1255         char *xmp_data = NULL;
1256         int size1 = 0;
1257         int size2 = 0;
1258         int fdata = 0;
1259         int temp = 0;
1260
1261         fp = fopen(path, "rb");
1262         if (fp == NULL)
1263                 goto ERROR;
1264
1265         size = fread(exif_header, 1, sizeof(exif_header), fp);
1266         if (size <= 0)
1267                 goto ERROR;
1268
1269         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1270                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
1271                 if (size <= 0)
1272                         goto ERROR;
1273
1274                 size1 = exif_app1[0];
1275                 size2 = exif_app1[1];
1276
1277                 app1_size = size1 * 256 + size2 - 2;
1278
1279                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1280                         goto ERROR;
1281
1282                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
1283                 if (size <= 0)
1284                         goto ERROR;
1285
1286                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1287                         int result = 0;
1288                         char *ptr = NULL;
1289                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
1290                         if (size <= 0)
1291                                 goto ERROR;
1292
1293                         size1 = exif_app1_xmp_t[0];
1294                         size2 = exif_app1_xmp_t[1];
1295
1296                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
1297
1298                         xmp_data = (char *)malloc(exif_app1_xmp_size);
1299                         if (xmp_data != NULL) {
1300                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
1301                                 ptr = xmp_data;
1302
1303                                 while (exif_app1_xmp_size >= 0) {
1304                                         exif_app1_xmp_size--;
1305                                         fdata = fgetc(fp);
1306                                         if (fdata == EOF)
1307                                                 continue;
1308                                         if (fdata == '\0')
1309                                                 continue;
1310                                         *ptr = (char)fdata;
1311                                         ptr++;
1312                                         temp++;
1313                                 }
1314                                 ptr = ptr - temp;
1315
1316                                 if (strstr(ptr, "UsePanoramaViewer")
1317                                 && strstr(ptr, "True")
1318                                 && strstr(ptr, "ProjectionType")
1319                                 && strstr(ptr, "equirectangular"))
1320                                         result = 1;
1321
1322                                 SAFE_FREE(xmp_data);
1323                         } else {
1324                                 media_svc_error("malloc failed");
1325                         }
1326
1327                         if (fp) {
1328                                 fclose(fp);
1329                                 fp = NULL;
1330                         }
1331                         return result;
1332                 } else {
1333                         goto ERROR;
1334                 }
1335         } else {
1336                 goto ERROR;
1337         }
1338 ERROR:
1339         if (fp) {
1340                 fclose(fp);
1341                 fp = NULL;
1342         }
1343         return 0;
1344 }
1345
1346 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1347 {
1348         int ret = MS_MEDIA_ERR_NONE;
1349         double value = 0.0;
1350         int orient_value = 0;
1351         int exif_width = 0;
1352         int exif_height = 0;
1353         ExifData *ed = NULL;
1354         int has_datetaken = FALSE;
1355         int datetaken_size = 19;
1356         double fnumber = 0.0;
1357         int iso = 0;
1358         char *path = NULL;
1359
1360         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1361         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1362         char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1363         char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1364
1365         memset(buf, 0x00, sizeof(buf));
1366         memset(description_buf, 0x00, sizeof(description_buf));
1367         memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1368         memset(model_buf, 0x00, sizeof(model_buf));
1369
1370         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1371                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1372                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1373         }
1374
1375         path = content_info->path;
1376         if (!STRING_VALID(path)) {
1377                 media_svc_error("Invalid Path");
1378                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1379         }
1380
1381         /* Load an ExifData object from an EXIF file */
1382         ed = exif_data_new_from_file(path);
1383
1384         if (!ed) {
1385                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1386                 goto GET_WIDTH_HEIGHT;
1387         }
1388
1389         content_info->media_meta.is_360 = image_360_check(path);
1390
1391         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1392                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1393                         if (strlen(buf) > 0) {
1394                                 if (strcmp(buf, "S") == 0)
1395                                         value = -1 * value;
1396                         }
1397                         content_info->media_meta.latitude = value;
1398                 } else {
1399                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1400                 }
1401         } else {
1402                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1403         }
1404
1405         memset(buf, 0x00, sizeof(buf));
1406
1407         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1408                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1409                         if (strlen(buf) > 0) {
1410                                 if (strcmp(buf, "W") == 0)
1411                                         value = -1 * value;
1412                         }
1413                         content_info->media_meta.longitude = value;
1414                 } else {
1415                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1416                 }
1417         } else {
1418                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1419         }
1420
1421         memset(buf, 0x00, sizeof(buf));
1422
1423         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1424                 if (strlen(description_buf) == 0) {
1425                         /*media_svc_debug("Use 'No description'"); */
1426                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1427                         if (ret != MS_MEDIA_ERR_NONE)
1428                                 media_svc_error("strcpy error");
1429                 } else {
1430                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1431                         if (ret != MS_MEDIA_ERR_NONE)
1432                                 media_svc_error("strcpy error");
1433                 }
1434         } else {
1435                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1436                 if (ret != MS_MEDIA_ERR_NONE)
1437                         media_svc_error("strcpy error");
1438         }
1439
1440         memset(buf, 0x00, sizeof(buf));
1441
1442         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1443                 if (strlen(buf) == 0) {
1444                         /*media_svc_debug("time is NULL"); */
1445                 } else {
1446                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1447                         if (ret != MS_MEDIA_ERR_NONE) {
1448                                 media_svc_error("strcpy error");
1449                         } else {
1450                                 has_datetaken = TRUE;
1451                                 /* This is same as recorded_date */
1452                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1453                                 if (ret != MS_MEDIA_ERR_NONE)
1454                                         media_svc_error("strcpy error");
1455                         }
1456                 }
1457         }
1458
1459         memset(buf, 0x00, sizeof(buf));
1460
1461         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1462                 if (strlen(buf) == 0) {
1463                         /*media_svc_debug("time is NULL"); */
1464                 } else {
1465                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1466                         if (ret != MS_MEDIA_ERR_NONE) {
1467                                 media_svc_error("strcpy error");
1468                         } else {
1469                                 has_datetaken = TRUE;
1470                                 /* This is same as recorded_date */
1471                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1472                                 if (ret != MS_MEDIA_ERR_NONE)
1473                                         media_svc_error("strcpy error");
1474                         }
1475                 }
1476         }
1477
1478         if (has_datetaken) {
1479                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1480                 if (content_info->timeline == 0)
1481                         content_info->timeline = content_info->modified_time;
1482                 else
1483                         media_svc_debug("Timeline : %ld", content_info->timeline);
1484         }
1485
1486         /* Get exposure_time value from exif. */
1487         if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1488                 if (strlen(exposure_time_buf) == 0) {
1489                         /* media_svc_debug("exposure_time_buf is NULL"); */
1490                 } else {
1491                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1492                         if (ret != MS_MEDIA_ERR_NONE)
1493                                 media_svc_error("strcpy error");
1494                 }
1495         }
1496
1497         /* Get fnumber value from exif. */
1498         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1499                 if (fnumber > 0.0)
1500                         content_info->media_meta.fnumber = fnumber;
1501                 else
1502                         content_info->media_meta.fnumber = 0.0;
1503         } else {
1504                 content_info->media_meta.fnumber = 0.0;
1505         }
1506
1507         /* Get iso value from exif. */
1508         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1509                 if (iso > 0)
1510                         content_info->media_meta.iso = iso;
1511                 else
1512                         content_info->media_meta.iso = 0;
1513         } else {
1514                 content_info->media_meta.iso = 0;
1515         }
1516
1517         /* Get model value from exif. */
1518         if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1519                 if (strlen(model_buf) == 0) {
1520                         /* media_svc_debug("model_buf is NULL"); */
1521                 } else {
1522                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1523                         if (ret != MS_MEDIA_ERR_NONE)
1524                                 media_svc_error("strcpy error");
1525                 }
1526         }
1527
1528         /* Get orientation value from exif. */
1529         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1530                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1531                         content_info->media_meta.orientation = orient_value;
1532                 else
1533                         content_info->media_meta.orientation = 0;
1534         } else {
1535                 content_info->media_meta.orientation = 0;
1536         }
1537
1538         /* Get width value from exif. */
1539         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1540                 if (exif_width > 0)
1541                         content_info->media_meta.width = exif_width;
1542                 else
1543                         content_info->media_meta.width = 0;
1544         } else {
1545                 content_info->media_meta.width = 0;
1546         }
1547
1548         /* Get height value from exif. */
1549         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1550                 if (exif_height > 0)
1551                         content_info->media_meta.height = exif_height;
1552                 else
1553                         content_info->media_meta.height = 0;
1554         } else {
1555                 content_info->media_meta.height = 0;
1556         }
1557
1558         if (ed != NULL) exif_data_unref(ed);
1559
1560 GET_WIDTH_HEIGHT:
1561
1562         if (content_info->media_meta.width == 0 ||
1563                 content_info->media_meta.height == 0) {
1564                 /*Get image width, height*/
1565                 unsigned int img_width = 0;
1566                 unsigned int img_height = 0;
1567                 ImgCodecType img_type = IMG_CODEC_NONE;
1568
1569                 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1570
1571                 if (content_info->media_meta.width == 0)
1572                         content_info->media_meta.width = img_width;
1573
1574                 if (content_info->media_meta.height == 0)
1575                         content_info->media_meta.height = img_height;
1576         }
1577
1578         return MS_MEDIA_ERR_NONE;
1579 }
1580
1581 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1582 {
1583         MMHandleType tag = 0;
1584         char *p = NULL;
1585         int size = -1;
1586         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1587         int mmf_error = FILEINFO_ERROR_NONE;
1588         char *err_attr_name = NULL;
1589         char *title = NULL;
1590         int album_id = 0;
1591         int ret = MS_MEDIA_ERR_NONE;
1592
1593         /*Get Content Tag attribute ===========*/
1594         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1595
1596         if (mmf_error == FILEINFO_ERROR_NONE) {
1597                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1598                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1599                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1600                         if (ret != MS_MEDIA_ERR_NONE)
1601                                 media_svc_error("strcpy error");
1602
1603                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1604                 } else {
1605                         SAFE_FREE(err_attr_name);
1606                         /*media_svc_debug("album - unknown"); */
1607                 }
1608
1609                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1610                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1611                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1612                         if (ret != MS_MEDIA_ERR_NONE)
1613                                 media_svc_error("strcpy error");
1614                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1615                 } else {
1616                         SAFE_FREE(err_attr_name);
1617                         /*media_svc_debug("artist - unknown"); */
1618                 }
1619
1620                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1621                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1622                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1623                         if (ret != MS_MEDIA_ERR_NONE)
1624                                 media_svc_error("strcpy error");
1625                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1626                 } else {
1627                         SAFE_FREE(err_attr_name);
1628                         /*media_svc_debug("album_artist - unknown"); */
1629                 }
1630
1631                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1632                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1633                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1634                         if (ret != MS_MEDIA_ERR_NONE)
1635                                 media_svc_error("strcpy error");
1636
1637                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1638                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1639                         /*
1640                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1641                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1642                         }
1643                         */
1644                 } else {
1645                         SAFE_FREE(err_attr_name);
1646                         /*media_svc_debug("genre - unknown"); */
1647                 }
1648
1649                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1650                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1651                         if (!isspace(*p)) {
1652                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1653                                 if (ret != MS_MEDIA_ERR_NONE)
1654                                         media_svc_error("strcpy error");
1655
1656                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1657                         } else {
1658                                 int idx = 0;
1659
1660                                 for (idx = 0; idx < size; idx++) {
1661                                         if (isspace(*p)) {
1662                                                 media_svc_debug("SPACE [%s]", p);
1663                                                 p++;
1664                                                 continue;
1665                                         } else {
1666                                                 media_svc_debug("Not SPACE [%s]", p);
1667                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1668                                                 if (ret != MS_MEDIA_ERR_NONE)
1669                                                         media_svc_error("strcpy error");
1670                                                 break;
1671                                         }
1672                                 }
1673
1674                                 if (idx == size) {
1675                                         media_svc_debug("Can't extract title. All string is space");
1676                                         title = _media_svc_get_title_from_filepath(content_info->path);
1677                                         if (title) {
1678                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1679                                                 if (ret != MS_MEDIA_ERR_NONE)
1680                                                         media_svc_error("strcpy error");
1681                                                 SAFE_FREE(title);
1682                                         } else {
1683                                                 media_svc_error("Can't extract title");
1684                                         }
1685                                 }
1686                         }
1687                 } else {
1688                         SAFE_FREE(err_attr_name);
1689                         title = _media_svc_get_title_from_filepath(content_info->path);
1690                         if (title) {
1691                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1692                                 if (ret != MS_MEDIA_ERR_NONE)
1693                                         media_svc_error("strcpy error");
1694                                 SAFE_FREE(title);
1695                         } else {
1696                                 media_svc_error("Can't extract title");
1697                         }
1698                 }
1699
1700                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1701                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1702                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1703                         if (ret != MS_MEDIA_ERR_NONE)
1704                                 media_svc_error("strcpy error");
1705                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1706                 } else {
1707                         SAFE_FREE(err_attr_name);
1708                 }
1709
1710                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1711                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1712                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1713                         if (ret != MS_MEDIA_ERR_NONE)
1714                                 media_svc_error("strcpy error");
1715                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1716                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1717                 } else {
1718                         /*media_svc_debug("composer - unknown"); */
1719                         SAFE_FREE(err_attr_name);
1720                 }
1721
1722                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1723                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1724                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1725                         if (ret != MS_MEDIA_ERR_NONE)
1726                                 media_svc_error("strcpy error");
1727                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1728                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1729                 } else {
1730                         /*media_svc_debug("copyright - unknown"); */
1731                         SAFE_FREE(err_attr_name);
1732                 }
1733
1734                 mmf_error = mm_file_destroy_tag_attrs(tag);
1735                 if (mmf_error != FILEINFO_ERROR_NONE)
1736                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1737         } else {
1738                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1739                 char *no_tag_title = NULL;
1740                 media_svc_error("no tag information");
1741
1742                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1743                 if (no_tag_title) {
1744                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1745                         if (ret != MS_MEDIA_ERR_NONE)
1746                                 media_svc_error("strcpy error");
1747                         SAFE_FREE(no_tag_title);
1748                 } else {
1749                         media_svc_error("Can't extract title");
1750                 }
1751
1752                 content_info->album_id = album_id;
1753         }
1754
1755         return MS_MEDIA_ERR_NONE;
1756 }
1757
1758 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1759 {
1760         MMHandleType content = 0;
1761         MMHandleType tag = 0;
1762         char *p = NULL;
1763         unsigned char *image = NULL;
1764         unsigned int size = 0;
1765         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1766         int mmf_error = FILEINFO_ERROR_NONE;
1767         char *err_attr_name = NULL;
1768         char *title = NULL;
1769         bool extract_thumbnail = FALSE;
1770         bool append_album = FALSE;
1771         int album_id = 0;
1772         int ret = MS_MEDIA_ERR_NONE;
1773         int cdis_value = 0;
1774         unsigned int resize_size = 0;
1775         unsigned char *resize_image = NULL;
1776
1777         /*Get Content Tag attribute ===========*/
1778         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1779
1780         if (mmf_error == FILEINFO_ERROR_NONE) {
1781                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1782                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1783                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1784                         if (ret != MS_MEDIA_ERR_NONE)
1785                                 media_svc_error("strcpy error");
1786
1787                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1788                 } else {
1789                         SAFE_FREE(err_attr_name);
1790                         /*media_svc_debug("album - unknown"); */
1791                 }
1792
1793                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1794                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1795                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1796                         if (ret != MS_MEDIA_ERR_NONE)
1797                                 media_svc_error("strcpy error");
1798                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1799                 } else {
1800                         SAFE_FREE(err_attr_name);
1801                         /*media_svc_debug("artist - unknown"); */
1802                 }
1803
1804                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1805                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1806                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1807                         if (ret != MS_MEDIA_ERR_NONE)
1808                                 media_svc_error("strcpy error");
1809                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1810                 } else {
1811                         SAFE_FREE(err_attr_name);
1812                         /*media_svc_debug("album_artist - unknown"); */
1813                 }
1814
1815                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1816                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1817                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1818                         if (ret != MS_MEDIA_ERR_NONE)
1819                                 media_svc_error("strcpy error");
1820
1821                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1822                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1823                         /*
1824                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1825                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1826                         }
1827                         */
1828                 } else {
1829                         SAFE_FREE(err_attr_name);
1830                         /*media_svc_debug("genre - unknown"); */
1831                 }
1832
1833                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1834                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1835                         if (!isspace(*p)) {
1836                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1837                                 if (ret != MS_MEDIA_ERR_NONE)
1838                                         media_svc_error("strcpy error");
1839
1840                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1841                         } else {
1842                                 int idx = 0;
1843
1844                                 for (idx = 0; idx < size; idx++) {
1845                                         if (isspace(*p)) {
1846                                                 media_svc_debug("SPACE [%s]", p);
1847                                                 p++;
1848                                                 continue;
1849                                         } else {
1850                                                 media_svc_debug("Not SPACE [%s]", p);
1851                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1852                                                 if (ret != MS_MEDIA_ERR_NONE)
1853                                                         media_svc_error("strcpy error");
1854                                                 break;
1855                                         }
1856                                 }
1857
1858                                 if (idx == size) {
1859                                         media_svc_debug("Can't extract title. All string is space");
1860                                         title = _media_svc_get_title_from_filepath(content_info->path);
1861                                         if (title) {
1862                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1863                                                 if (ret != MS_MEDIA_ERR_NONE)
1864                                                         media_svc_error("strcpy error");
1865                                                 SAFE_FREE(title);
1866                                         } else {
1867                                                 media_svc_error("Can't extract title");
1868                                         }
1869                                 }
1870                         }
1871                 } else {
1872                         SAFE_FREE(err_attr_name);
1873                         title = _media_svc_get_title_from_filepath(content_info->path);
1874                         if (title) {
1875                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1876                                 if (ret != MS_MEDIA_ERR_NONE)
1877                                         media_svc_error("strcpy error");
1878                                 SAFE_FREE(title);
1879                         } else {
1880                                 media_svc_error("Can't extract title");
1881                         }
1882                 }
1883
1884                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1885                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1886                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1887                         if (ret != MS_MEDIA_ERR_NONE)
1888                                 media_svc_error("strcpy error");
1889                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1890                 } else {
1891                         SAFE_FREE(err_attr_name);
1892                 }
1893
1894                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1895
1896                 if (mmf_error != FILEINFO_ERROR_NONE)
1897                         SAFE_FREE(err_attr_name);
1898
1899                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1900                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1901                         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1902                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1903                                 char *time_info = (char*)calloc(1, (size + 1));
1904                                 char *p_value = p;
1905                                 char *time_value = time_info;
1906                                 if (time_info != NULL) {
1907                                         while (*p_value != '\0') {
1908                                                 if (*p_value == '-')
1909                                                         *time_value = ':';
1910                                                 else
1911                                                         *time_value = *p_value;
1912                                                 time_value++;
1913                                                 p_value++;
1914                                         }
1915                                         *time_value = '\0';
1916                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1917                                         SAFE_FREE(time_info);
1918                                 } else {
1919                                         media_svc_error("memory allocation error");
1920                                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
1921                                 }
1922                         } else {
1923                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1924                         }
1925
1926                         if (ret != MS_MEDIA_ERR_NONE) {
1927                                 media_svc_error("strcpy error");
1928                         } else {
1929                                 /* This is same as datetaken */
1930 #if 0
1931                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1932 #else
1933                                 int datetaken_size = 19;
1934                                 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1935 #endif
1936                                 if (ret != MS_MEDIA_ERR_NONE)
1937                                         media_svc_error("strcpy error");
1938
1939                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1940                                 if (content_info->timeline == 0)
1941                                         content_info->timeline = content_info->modified_time;
1942                                 else
1943                                         media_svc_debug("Timeline : %ld", content_info->timeline);
1944                         }
1945                         /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1946                 } else {
1947                         SAFE_FREE(err_attr_name);
1948                 }
1949
1950                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1951                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1952                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1953                         if (ret != MS_MEDIA_ERR_NONE)
1954                                 media_svc_error("strcpy error");
1955                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1956                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1957                 } else {
1958                         /*media_svc_debug("composer - unknown"); */
1959                         SAFE_FREE(err_attr_name);
1960                 }
1961
1962                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1963                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1964                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1965                         if (ret != MS_MEDIA_ERR_NONE)
1966                                 media_svc_error("strcpy error");
1967                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1968                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1969                 } else {
1970                         /*media_svc_debug("copyright - unknown"); */
1971                         SAFE_FREE(err_attr_name);
1972                 }
1973
1974                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1975                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1976                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1977                         if (ret != MS_MEDIA_ERR_NONE)
1978                                 media_svc_error("strcpy error");
1979                 } else {
1980                         SAFE_FREE(err_attr_name);
1981                 }
1982
1983                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1984                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1985                         int year = 0;
1986                         if ((p != NULL) && (sscanf(p, "%d", &year))) {
1987                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1988                                 if (ret != MS_MEDIA_ERR_NONE)
1989                                         media_svc_error("strcpy error");
1990                         } else {
1991                                 media_svc_debug("Wrong Year Information [%s]", p);
1992                         }
1993                 } else {
1994                         SAFE_FREE(err_attr_name);
1995                 }
1996
1997                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1998                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1999                         content_info->media_meta.rating = atoi(p);
2000                 } else {
2001                         SAFE_FREE(err_attr_name);
2002                         content_info->media_meta.rating = 0;
2003                 }
2004
2005                 /*Initialize album_id to 0. below code will set the album_id*/
2006                 content_info->album_id = album_id;
2007                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
2008
2009                 if (ret != MS_MEDIA_ERR_NONE) {
2010                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
2011                                 media_svc_debug("album does not exist. So start to make album art");
2012                                 extract_thumbnail = TRUE;
2013                                 append_album = TRUE;
2014                         } else {
2015                                 extract_thumbnail = TRUE;
2016                                 append_album = FALSE;
2017                         }
2018                 } else {
2019                         content_info->album_id = album_id;
2020                         append_album = FALSE;
2021
2022                         if ((!g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
2023                                 (!g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
2024                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
2025                                 extract_thumbnail = TRUE;
2026                         } else {
2027                                 media_svc_debug("album already exists. don't need to make album art");
2028                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2029                                 extract_thumbnail = TRUE;
2030                         }
2031                 }
2032
2033                 /*Do not extract artwork for the USB Storage content*/
2034                 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2035                         extract_thumbnail = FALSE;
2036
2037                 if (extract_thumbnail == TRUE) {
2038                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2039                         if (mmf_error != FILEINFO_ERROR_NONE) {
2040                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2041                                 SAFE_FREE(err_attr_name);
2042                         } else {
2043                                 /*media_svc_debug("artwork size1 [%d]", size); */
2044                         }
2045
2046                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2047                         if (mmf_error != FILEINFO_ERROR_NONE) {
2048                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2049                                 SAFE_FREE(err_attr_name);
2050                         } else {
2051                                 /*media_svc_debug("artwork size2 [%d]", size); */
2052                         }
2053                         if (image != NULL && size > 0) {
2054                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2055                                 int artwork_mime_size = -1;
2056
2057                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2058                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2059                                         ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2060                                         if (ret != MS_MEDIA_ERR_NONE)
2061                                                 media_svc_error("Fail to Get Thumbnail Path");
2062                                         /* albumart resizing */
2063                                         __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2064                                 } else {
2065                                         SAFE_FREE(err_attr_name);
2066                                 }
2067
2068                                 if (strlen(thumb_path) > 0) {
2069                                         ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2070                                         if (ret != MS_MEDIA_ERR_NONE) {
2071                                                 media_svc_error("Fail to Save Thumbnail Image");
2072                                         } else {
2073                                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2074                                                 if (ret != MS_MEDIA_ERR_NONE)
2075                                                         media_svc_error("strcpy error");
2076                                         }
2077                                 }
2078                         }
2079                 }
2080
2081                 if (append_album == TRUE) {
2082                         if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
2083                                 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
2084                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2085                         else
2086                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2087
2088                         content_info->album_id = album_id;
2089                 }
2090
2091                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2092                         double longitude = 0.0;
2093                         double latitude = 0.0;
2094                         double altitude = 0.0;
2095
2096                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2097                         content_info->media_meta.longitude = longitude;
2098                         content_info->media_meta.latitude = latitude;
2099                         content_info->media_meta.altitude = altitude;
2100
2101                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2102                         if (mmf_error != FILEINFO_ERROR_NONE) {
2103                                 cdis_value = 0;
2104                                 SAFE_FREE(err_attr_name);
2105                         }
2106
2107                         media_svc_debug("CDIS : %d", cdis_value);
2108
2109                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
2110                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2111                                 content_info->media_meta.orientation = atoi(p);
2112                         } else {
2113                                 SAFE_FREE(err_attr_name);
2114                                 content_info->media_meta.orientation = 0;
2115                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
2116                         }
2117                 }
2118
2119                 mmf_error = mm_file_destroy_tag_attrs(tag);
2120                 if (mmf_error != FILEINFO_ERROR_NONE)
2121                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2122         }       else {
2123                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2124                 char *no_tag_title = NULL;
2125                 media_svc_error("no tag information");
2126
2127                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2128                 if (no_tag_title) {
2129                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2130                         if (ret != MS_MEDIA_ERR_NONE)
2131                                 media_svc_error("strcpy error");
2132                         SAFE_FREE(no_tag_title);
2133                 } else {
2134                         media_svc_error("Can't extract title");
2135                 }
2136
2137                 content_info->album_id = album_id;
2138         }
2139
2140         /*Get Content attribute ===========*/
2141         if (cdis_value == 1)
2142                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2143         else
2144                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2145
2146         if (mmf_error == FILEINFO_ERROR_NONE) {
2147                 /*Common attribute*/
2148                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2149                 if (mmf_error != FILEINFO_ERROR_NONE) {
2150                         SAFE_FREE(err_attr_name);
2151                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2152                 } else {
2153                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2154                 }
2155
2156                 /*Sound/Music attribute*/
2157                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2158
2159                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2160                         if (mmf_error != FILEINFO_ERROR_NONE) {
2161                                 SAFE_FREE(err_attr_name);
2162                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2163                         } else {
2164                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2165                         }
2166
2167                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2168                         if (mmf_error != FILEINFO_ERROR_NONE) {
2169                                 SAFE_FREE(err_attr_name);
2170                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2171                         } else {
2172                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2173                         }
2174
2175                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2176                         if (mmf_error != FILEINFO_ERROR_NONE) {
2177                                 SAFE_FREE(err_attr_name);
2178                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2179                         } else {
2180                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2181                         }
2182
2183                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2184                         if (mmf_error != FILEINFO_ERROR_NONE) {
2185                                 SAFE_FREE(err_attr_name);
2186                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2187                         } else {
2188                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2189                         }
2190                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
2191                         int audio_bitrate = 0;
2192                         int video_bitrate = 0;
2193
2194                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2195                         if (mmf_error != FILEINFO_ERROR_NONE) {
2196                                 SAFE_FREE(err_attr_name);
2197                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2198                         } else {
2199                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2200                         }
2201
2202                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2203                         if (mmf_error != FILEINFO_ERROR_NONE) {
2204                                 SAFE_FREE(err_attr_name);
2205                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2206                         } else {
2207                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2208                         }
2209
2210                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2211
2212                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2213                         if (mmf_error != FILEINFO_ERROR_NONE) {
2214                                 SAFE_FREE(err_attr_name);
2215                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2216                         } else {
2217                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2218                         }
2219
2220                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2221                         if (mmf_error != FILEINFO_ERROR_NONE) {
2222                                 SAFE_FREE(err_attr_name);
2223                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2224                         } else {
2225                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2226                         }
2227                 } else {
2228                         media_svc_error("Not support type");
2229                         mmf_error = mm_file_destroy_content_attrs(content);
2230                         if (mmf_error != FILEINFO_ERROR_NONE)
2231                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2232
2233                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2234                 }
2235
2236                 mmf_error = mm_file_destroy_content_attrs(content);
2237                 if (mmf_error != FILEINFO_ERROR_NONE)
2238                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
2239         } else {
2240                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2241         }
2242
2243         return MS_MEDIA_ERR_NONE;
2244 }
2245
2246 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2247 {
2248         media_svc_retm_if(content_info == NULL, "content info is NULL");
2249
2250         /* Delete media_svc_content_info_s */
2251         SAFE_FREE(content_info->media_uuid);
2252         SAFE_FREE(content_info->path);
2253         SAFE_FREE(content_info->file_name);
2254         SAFE_FREE(content_info->mime_type);
2255         SAFE_FREE(content_info->folder_uuid);
2256         SAFE_FREE(content_info->thumbnail_path);
2257         SAFE_FREE(content_info->storage_uuid);
2258
2259         /* Delete media_svc_content_meta_s */
2260         SAFE_FREE(content_info->media_meta.title);
2261         SAFE_FREE(content_info->media_meta.album);
2262         SAFE_FREE(content_info->media_meta.artist);
2263         SAFE_FREE(content_info->media_meta.album_artist);
2264         SAFE_FREE(content_info->media_meta.genre);
2265         SAFE_FREE(content_info->media_meta.composer);
2266         SAFE_FREE(content_info->media_meta.year);
2267         SAFE_FREE(content_info->media_meta.recorded_date);
2268         SAFE_FREE(content_info->media_meta.copyright);
2269         SAFE_FREE(content_info->media_meta.track_num);
2270         SAFE_FREE(content_info->media_meta.description);
2271         SAFE_FREE(content_info->media_meta.datetaken);
2272         SAFE_FREE(content_info->media_meta.exposure_time);
2273         SAFE_FREE(content_info->media_meta.model);
2274         SAFE_FREE(content_info->media_meta.weather);
2275         SAFE_FREE(content_info->media_meta.category);
2276         SAFE_FREE(content_info->media_meta.keyword);
2277         SAFE_FREE(content_info->media_meta.location_tag);
2278         SAFE_FREE(content_info->media_meta.content_name);
2279         SAFE_FREE(content_info->media_meta.age_rating);
2280         SAFE_FREE(content_info->media_meta.author);
2281         SAFE_FREE(content_info->media_meta.provider);
2282
2283         SAFE_FREE(content_info->media_meta.title_pinyin);
2284         SAFE_FREE(content_info->media_meta.album_pinyin);
2285         SAFE_FREE(content_info->media_meta.artist_pinyin);
2286         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2287         SAFE_FREE(content_info->media_meta.genre_pinyin);
2288         SAFE_FREE(content_info->media_meta.composer_pinyin);
2289         SAFE_FREE(content_info->media_meta.copyright_pinyin);
2290         SAFE_FREE(content_info->media_meta.description_pinyin);
2291
2292         return;
2293 }
2294
2295 int _media_svc_get_storage_type_by_path(const char *path, media_svc_storage_type_e *storage_type, uid_t uid)
2296 {
2297         if (STRING_VALID(path)) {
2298                 char *internal_path = _media_svc_get_path(uid);
2299                 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0)) {
2300                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
2301                 } else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)) {
2302                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2303                 } else if (STRING_VALID(MEDIA_ROOT_PATH_USB) && (strncmp(path, MEDIA_ROOT_PATH_USB, strlen(MEDIA_ROOT_PATH_USB)) == 0)) {
2304                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL_USB;
2305                 } else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && (strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)) {
2306                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
2307                 } else {
2308                         media_svc_error("Invalid Path");
2309                         SAFE_FREE(internal_path);
2310                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2311                 }
2312                 SAFE_FREE(internal_path);
2313         } else {
2314                 media_svc_error("INVALID parameter");
2315                 return MS_MEDIA_ERR_INVALID_PARAMETER;
2316         }
2317
2318         return MS_MEDIA_ERR_NONE;
2319 }
2320
2321 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2322 {
2323         char result[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
2324         memset(result, 0x00, sizeof(result));
2325
2326         if (STRING_VALID(s) && STRING_VALID(olds) && STRING_VALID(news)) {
2327                 if (strncmp(s, olds, strlen(olds)) == 0) {
2328                         snprintf(result, sizeof(result), "%s%s", news, s + strlen(olds));
2329                 }
2330         }
2331
2332         if (STRING_VALID(result))
2333                 return g_strdup(result);
2334         else
2335                 return NULL;
2336
2337 }
2338
2339 bool _media_svc_is_drm_file(const char *path)
2340 {
2341         return FALSE;
2342 }
2343
2344 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)
2345 {
2346         int ret = MS_MEDIA_ERR_NONE;
2347
2348         ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2349
2350         if (ret != MS_MEDIA_ERR_NONE) {
2351                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2352                 ret = MS_MEDIA_ERR_INTERNAL;
2353         } else {
2354                 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2355         }
2356
2357         return ret;
2358 }
2359
2360 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2361 {
2362         int ret = MS_MEDIA_ERR_NONE;
2363         int size = 0;
2364         pinyin_name_s *pinyinname = NULL;
2365
2366         *pinyin_str = NULL;
2367
2368         if (!STRING_VALID(src_str)) {
2369                 media_svc_debug("String is invalid");
2370                 return ret;
2371         }
2372
2373         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2374         if (ret == MS_MEDIA_ERR_NONE) {
2375                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
2376                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
2377                 else
2378                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
2379         }
2380
2381         _media_svc_pinyin_free(pinyinname, size);
2382
2383         return ret;
2384 }
2385
2386 bool _media_svc_check_pinyin_support(void)
2387 {
2388         /*Check CSC*/
2389         return TRUE;
2390 }
2391
2392 char* _media_svc_get_title_from_path(const char *path)
2393 {
2394         char *filename = NULL;
2395         char *title = NULL;
2396         char    *ext = NULL;
2397         int filename_len = -1;
2398         int new_title_len = -1;
2399
2400         if (!path) {
2401                 media_svc_error("path is NULL");
2402                 return NULL;
2403         }
2404
2405         filename = g_path_get_basename(path);
2406         if (!STRING_VALID(filename)) {
2407                 media_svc_error("wrong file name");
2408                 SAFE_FREE(filename);
2409                 return NULL;
2410         }
2411
2412         filename_len = strlen(filename);
2413
2414         ext = g_strrstr(filename, ".");
2415         if (!ext) {
2416                 media_svc_error("there is no file extention");
2417                 return filename;
2418         }
2419
2420         new_title_len = filename_len - strlen(ext);
2421         if (new_title_len < 1) {
2422                 media_svc_error("title length is zero");
2423                 SAFE_FREE(filename);
2424                 return NULL;
2425         }
2426
2427         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2428
2429         SAFE_FREE(filename);
2430
2431         media_svc_debug("extract title is [%s]", title);
2432
2433         return title;
2434 }
2435
2436 #define BUF_LENGHT 256
2437
2438 void _media_svc_print_stderror(void)
2439 {
2440         char buf[BUF_LENGHT] = {0,};
2441
2442         media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));
2443 }
2444
2445 int _media_svc_get_media_type(const char *path, int *mediatype)
2446 {
2447         int ret = MS_MEDIA_ERR_NONE;
2448         char mime_type[256] = {0};
2449         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
2450
2451         ret = __media_svc_get_mime_type(path, mime_type);
2452         if (ret == MS_MEDIA_ERR_NONE) {
2453                 __media_svc_get_media_type(path, mime_type, &media_type);
2454         } else {
2455                 media_svc_error("__media_svc_get_mime_type failed");
2456         }
2457
2458         *mediatype = media_type;
2459
2460         return ret;
2461 }
2462