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