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