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