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