Fix GPS bug
[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-thumbnail.h>
46 #include "media-util-err.h"
47 #include "media-svc-util.h"
48 #include "media-svc-db-utils.h"
49 #include "media-svc-debug.h"
50 #include "media-svc-env.h"
51 #include "media-svc-hash.h"
52 #include "media-svc-album.h"
53 #include "media-svc-localize_ch.h"
54
55 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
56
57 /* Define data structures for media type and mime type */
58 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
59 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
60 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
61 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
62 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
63 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
64 #define MEDIA_SVC_CATEGORY_PVR  0x00000020      /**< PVR category */
65 #define MEDIA_SVC_CATEGORY_UHD  0x00000040      /**< UHD category */
66 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080      /**< SCSA category */
67
68 #define CONTENT_TYPE_NUM 5
69 #define MUSIC_MIME_NUM 29
70 #define SOUND_MIME_NUM 1
71 #define MIME_TYPE_LENGTH 255
72 #define MIME_LENGTH 50
73 #define _3GP_FILE ".3gp"
74 #define _MP4_FILE ".mp4"
75 #define _ASF_FILE ".asf"
76 #define MEDIA_SVC_ARTWORK_SIZE 2000
77 #define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
78
79 #define MEDIA_SVC_DEFAULT_GPS_VALUE                     -200                    /**< Default GPS Value*/
80
81 typedef struct {
82         char content_type[15];
83         int category_by_mime;
84 } _media_svc_content_table_s;
85
86 enum Exif_Orientation {
87         NOT_AVAILABLE = 0,
88         NORMAL = 1,
89         HFLIP = 2,
90         ROT_180 = 3,
91         VFLIP = 4,
92         TRANSPOSE = 5,
93         ROT_90 = 6,
94         TRANSVERSE = 7,
95         ROT_270 = 8
96 };
97
98 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
99         {"audio", MEDIA_SVC_CATEGORY_SOUND},
100         {"image", MEDIA_SVC_CATEGORY_IMAGE},
101         {"video", MEDIA_SVC_CATEGORY_VIDEO},
102         {"application", MEDIA_SVC_CATEGORY_ETC},
103         {"text", MEDIA_SVC_CATEGORY_ETC},
104 };
105
106 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
107         /*known mime types of normal files*/
108         "mpeg",
109         "ogg",
110         "x-ms-wma",
111         "x-flac",
112         "mp4",
113         /* known mime types of drm files*/
114         "mp3",
115         "x-mp3", /*alias of audio/mpeg*/
116         "x-mpeg", /*alias of audio/mpeg*/
117         "3gpp",
118         "x-ogg", /*alias of audio/ogg*/
119         "vnd.ms-playready.media.pya:*.pya", /*playready*/
120         "wma",
121         "aac",
122         "x-m4a", /*alias of audio/mp4*/
123         /* below mimes are rare*/
124         "x-vorbis+ogg",
125         "x-flac+ogg",
126         "x-matroska",
127         "ac3",
128         "mp2",
129         "x-ape",
130         "x-ms-asx",
131         "vnd.rn-realaudio",
132
133         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
134         "vorbis", /*alias of audio/x-vorbis+ogg*/
135         "x-oggflac",
136         "x-mp2", /*alias of audio/mp2*/
137         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
138         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
139         "x-wav",
140 };
141
142 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
143         "x-smaf",
144 };
145
146 char *_media_info_generate_uuid(void)
147 {
148         uuid_t uuid_value;
149         char uuid_unparsed[37];
150
151 RETRY_GEN:
152         uuid_generate(uuid_value);
153         uuid_unparse(uuid_value, uuid_unparsed);
154
155         if (strlen(uuid_unparsed) < 36) {
156                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
157                 goto RETRY_GEN;
158         }
159
160         return g_strdup(uuid_unparsed);
161 }
162
163 static int __media_svc_split_to_double(char *input, double *arr)
164 {
165         char tmp_arr[255] = {0, };
166         int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
167
168         if (!STRING_VALID(input)) {
169                 media_svc_error("Invalid parameter");
170                 return MS_MEDIA_ERR_INVALID_PARAMETER;
171         }
172         memset(tmp_arr, 0x0, sizeof(tmp_arr));
173
174         /*media_svc_debug("input: [%s]", input); */
175
176         len = strlen(input);
177
178         for (idx = 0; idx < (len + 1); idx++) {
179                 if (input[idx] == ' ') {
180                         continue;
181                 } else if ((input[idx] == ',') || (idx == len)) {
182                         arr[arr_idx] = atof(tmp_arr);
183                         arr_idx++;
184                         str_idx = 0;
185                         /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
186                         memset(tmp_arr, 0x0, sizeof(tmp_arr));
187                 } else {
188                         tmp_arr[str_idx] = input[idx];
189                         str_idx++;
190                 }
191         }
192
193         if (arr_idx != 3) {
194                 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
195                 return MS_MEDIA_ERR_INTERNAL;
196         }
197
198         return MS_MEDIA_ERR_NONE;
199 }
200
201 /* GPS information is not ExifTag member */
202 static int __media_svc_get_exif_gps_double(ExifData *ed, double *value, long tagtype)
203 {
204         int ret = MS_MEDIA_ERR_NONE;
205         ExifEntry *entry;
206         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
207         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
208
209         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
210         media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
211
212         entry = exif_data_get_entry(ed, tagtype);
213         media_svc_retv_if(!entry, MS_MEDIA_ERR_INTERNAL);
214
215         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
216         gps_buf[strlen(gps_buf)] = '\0';
217
218         ret = __media_svc_split_to_double(gps_buf, tmp_arr);
219         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
220
221         *value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
222
223         return MS_MEDIA_ERR_NONE;
224 }
225
226 static int __media_svc_get_exif_gps_str(ExifData *ed, char *value, long tagtype)
227 {
228         ExifEntry *entry;
229
230         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
231         media_svc_retv_if(!value, MS_MEDIA_ERR_INVALID_PARAMETER);
232
233         entry = exif_data_get_entry(ed, tagtype);
234         if (entry) {
235                 exif_entry_get_value(entry, value, MEDIA_SVC_METADATA_LEN_MAX);
236                 value[strlen(value)] = '\0';
237         }
238
239         return MS_MEDIA_ERR_NONE;
240 }
241
242 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, ExifTag tagtype)
243 {
244         ExifEntry *entry;
245         ExifByteOrder mByteOrder;
246         ExifRational mRational;
247         long numerator, denominator;
248
249         media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
250
251         entry = exif_data_get_entry(ed, tagtype);
252         media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
253
254         switch (tagtype) {
255         case EXIF_TAG_ORIENTATION:
256         case EXIF_TAG_PIXEL_X_DIMENSION:
257         case EXIF_TAG_PIXEL_Y_DIMENSION:
258         case EXIF_TAG_ISO_SPEED_RATINGS:
259                 media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
260
261                 mByteOrder = exif_data_get_byte_order(ed);
262                 short exif_value = exif_get_short(entry->data, mByteOrder);
263                 *i_value = (int)exif_value;
264                 break;
265         case EXIF_TAG_EXPOSURE_TIME:
266                 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
267
268                 mByteOrder = exif_data_get_byte_order(ed);
269                 mRational = exif_get_rational(entry->data, mByteOrder);
270                 numerator = mRational.numerator;
271                 denominator = mRational.denominator;
272                 snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
273                 break;
274         case EXIF_TAG_FNUMBER:
275                 media_svc_retvm_if(!d_value, MS_MEDIA_ERR_INVALID_PARAMETER, "d_value is NULL");
276
277                 mByteOrder = exif_data_get_byte_order(ed);
278                 mRational = exif_get_rational(entry->data, mByteOrder);
279                 numerator = mRational.numerator;
280                 denominator = mRational.denominator;
281
282                 *d_value = ((numerator*1.0)/(denominator*1.0));
283                 break;
284         default:
285                 media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
286
287                 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
288                 buf[strlen(buf)] = '\0';
289         }
290
291         return MS_MEDIA_ERR_NONE;
292 }
293
294 static time_t __media_svc_get_timeline_from_str(const char *timstr)
295 {
296         struct tm t;
297         time_t modified_t = 0;
298         time_t rawtime;
299         struct tm timeinfo;
300
301         if (!STRING_VALID(timstr)) {
302                 media_svc_error("Invalid Parameter");
303                 return 0;
304         }
305
306         /*Exif Format : %Y:%m:%d %H:%M:%S
307         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
308         memset(&t, 0x00, sizeof(struct tm));
309
310         tzset();
311         time(&rawtime);
312         localtime_r(&rawtime, &timeinfo);
313
314         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
315                 t.tm_isdst = timeinfo.tm_isdst;
316                 if (t.tm_isdst != 0)
317                         media_svc_debug("DST %d", t.tm_isdst);
318
319                 /* If time string has timezone */
320                 if (strptime(timstr, "%Y:%m:%d %H:%M:%S %z", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S %z", &t)) {
321                         char tim_tmp_str[255] = { 0, };
322
323                         /* ISO8601 Time string format */
324                         strftime(tim_tmp_str, 255, "%Y-%m-%dT%H:%M:%S%z", &t);
325                         GDateTime *pdatetime = g_date_time_new_from_iso8601(tim_tmp_str, NULL);
326                         if (pdatetime)
327                                 modified_t = g_date_time_to_unix(pdatetime);
328                         g_date_time_unref(pdatetime);
329                         media_svc_debug("Calibrated timeval : [%ld][%s]", modified_t, tim_tmp_str);
330                 } else {
331                         /* Just localtime */
332                         modified_t = mktime(&t);
333                 }
334
335                 if (modified_t > 0)
336                         return modified_t;
337                 else
338                         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);
339         } else {
340                 media_svc_error("Failed to get timeline : [%s]", timstr);
341         }
342
343         return 0;
344 }
345
346 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
347 {
348         int idx = 0;
349
350         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
351         media_svc_retvm_if(mimetype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mimetype is null");
352         media_svc_retvm_if(category == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "category is null");
353
354         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
355
356         /*categorize from mimetype */
357         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
358                 if (strncmp(mimetype, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
359                         *category = (*category | content_category[idx].category_by_mime);
360                         break;
361                 }
362         }
363
364         /*in application type, exitst sound file ex) x-smafs, asf */
365         if (*category & MEDIA_SVC_CATEGORY_ETC) {
366                 int prefix_len = strlen(content_category[3].content_type) + 1;
367                 char *ext = NULL;
368
369                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
370                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
371                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
372                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
373                                 break;
374                         }
375                 }
376
377                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
378                         *category ^= MEDIA_SVC_CATEGORY_ETC;
379                         *category |= MEDIA_SVC_CATEGORY_SOUND;
380                 }
381
382                 /*"asf" must check video stream and then categorize in directly. */
383                 ext = strrchr(path, '.');
384                 if (ext != NULL) {
385                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
386                                 int audio = 0;
387                                 int video = 0;
388                                 int err = 0;
389
390                                 err = mm_file_get_stream_info(path, &audio, &video);
391                                 if (err == 0) {
392                                         if (audio > 0 && video == 0) {
393                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
394                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
395                                         } else {
396                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
397                                                 *category |= MEDIA_SVC_CATEGORY_VIDEO;
398                                         }
399                                 }
400                         }
401                 }
402         }
403
404         /*check music file in sound files. */
405         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
406                 int prefix_len = strlen(content_category[0].content_type) + 1;
407
408                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
409                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
410                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
411                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
412                                 break;
413                         }
414                 }
415
416                 /*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*/
417                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
418                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
419                         *category |= MEDIA_SVC_CATEGORY_ETC;
420                 }
421         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
422                 /*some video files don't have video stream. in this case it is categorize as music. */
423                 char *ext = NULL;
424                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
425                 ext = strrchr(path, '.');
426                 if (ext != NULL) {
427                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
428                                 int audio = 0;
429                                 int video = 0;
430                                 int err = 0;
431
432                                 err = mm_file_get_stream_info(path, &audio, &video);
433                                 if (err == 0) {
434                                         if (audio > 0 && video == 0) {
435                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
436                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
437                                         }
438                                 }
439                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
440                         }
441                 }
442         }
443
444         return MS_MEDIA_ERR_NONE;
445 }
446
447 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
448 {
449         int ret = MS_MEDIA_ERR_NONE;
450         int category = 0;
451
452         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
453         if (ret != MS_MEDIA_ERR_NONE)
454                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
455
456         switch (category) {
457         case MEDIA_SVC_CATEGORY_SOUND:
458                 *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
459                 break;
460         case MEDIA_SVC_CATEGORY_MUSIC:
461                 *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
462                 break;
463         case MEDIA_SVC_CATEGORY_IMAGE:
464                 *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
465                 break;
466         case MEDIA_SVC_CATEGORY_VIDEO:
467                 *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
468                 break;
469         default:
470                 *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
471         }
472
473         return ret;
474 }
475
476 /*
477 drm_contentifo is not NULL, if the file is OMA DRM.
478 If the file is not OMA DRM, drm_contentinfo must be NULL.
479 */
480 static int __media_svc_get_mime_type(const char *path, char *mimetype)
481 {
482         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
483
484         /*in case of normal files or failure to get mime in drm */
485         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
486                 media_svc_error("aul_get_mime_from_file fail");
487                 return MS_MEDIA_ERR_INTERNAL;
488         }
489
490         return MS_MEDIA_ERR_NONE;
491 }
492
493 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
494 {
495         int i = 0;
496
497         for (i = strlen(file_path); i >= 0; i--) {
498                 if (file_path[i] == '.') {
499                         SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
500                         return true;
501                 }
502
503                 if (file_path[i] == '/')
504                         return false;
505         }
506         return false;
507 }
508 #if 0
509 static int __media_svc_resize_artwork(const char *path, const char *img_format)
510 {
511         int ret = MS_MEDIA_ERR_NONE;
512         unsigned int width = 0;
513         unsigned int height = 0;
514         unsigned int resized_width = 0;
515         unsigned int resized_height = 0;
516         mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
517
518         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
519                 media_svc_debug("type [jpeg]");
520
521                 mm_util_extract_image_info(path, &img_type, &width, &height);
522
523                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
524                         media_svc_debug("No need resizing");
525                         return MS_MEDIA_ERR_NONE;
526                 }
527
528                 /* resizing */
529                 if (width > height) {
530                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
531                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
532                 } else {
533                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
534                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
535                 }
536
537                 ret = mm_util_resize_P_P(path, resized_width, resized_height, path);
538
539         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
540                 media_svc_debug("type [png]");
541         } else {
542                 media_svc_debug("Not proper img format");
543         }
544
545         return ret;
546 }
547 #endif
548 static int __media_svc_safe_atoi(char *buffer, int *si)
549 {
550         char *end = NULL;
551         errno = 0;
552         media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
553
554         const long sl = strtol(buffer, &end, 10);
555
556         media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
557         media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
558         media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
559         media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
560         media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
561
562         *si = (int)sl;
563
564         return MS_MEDIA_ERR_NONE;
565 }
566
567 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
568 {
569         int ret = MS_MEDIA_ERR_NONE;
570
571         media_svc_sec_debug("start save image, path [%s] image size [%d]", image_path, size);
572
573         if (!image) {
574                 media_svc_error("invalid image..");
575                 return MS_MEDIA_ERR_INVALID_PARAMETER;
576         }
577
578         struct statfs fs;
579         char *thumb_path = NULL;
580         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
581         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
582
583         if (-1 == statfs(thumb_path, &fs)) {
584                 media_svc_error("error in statfs");
585                 g_free(thumb_path);
586                 return MS_MEDIA_ERR_INTERNAL;
587         }
588
589         g_free(thumb_path);
590
591         long bsize_kbytes = fs.f_bsize >> 10;
592
593         if ((bsize_kbytes * fs.f_bavail) < 1024) {
594                 media_svc_error("not enought space...");
595                 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
596         }
597
598         FILE *fp = NULL;
599         int nwrite = -1;
600         if (image != NULL && size > 0) {
601                 fp = fopen(image_path, "w");
602
603                 if (fp == NULL) {
604                         media_svc_error("failed to open file");
605                         return MS_MEDIA_ERR_INTERNAL;
606                 }
607
608                 nwrite = fwrite(image, 1, size, fp);
609                 if (nwrite != size) {
610                         media_svc_error("failed to write thumbnail");
611                         fclose(fp);
612                         return MS_MEDIA_ERR_INTERNAL;
613                 }
614                 fclose(fp);
615         }
616
617         return MS_MEDIA_ERR_NONE;
618 }
619
620 static char *__media_svc_get_title_from_filepath(const char *path)
621 {
622         char *filename = NULL;
623         char *title = NULL;
624         char    *ext = NULL;
625         int filename_len = -1;
626         int new_title_len = -1;
627
628         if (!path) {
629                 media_svc_error("path is NULL");
630                 return NULL;
631         }
632
633         filename = g_path_get_basename(path);
634         if (!STRING_VALID(filename)) {
635                 media_svc_error("wrong file name");
636                 g_free(filename);
637                 return NULL;
638         }
639
640         filename_len = strlen(filename);
641
642         ext = g_strrstr(filename, ".");
643         if (!ext) {
644                 media_svc_error("there is no file extention");
645                 return filename;
646         }
647
648         new_title_len = filename_len - strlen(ext);
649         if (new_title_len < 1) {
650                 media_svc_error("title length is zero");
651                 g_free(filename);
652                 return NULL;
653         }
654
655         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
656
657         g_free(filename);
658
659         media_svc_debug("extract title is [%s]", title);
660
661         return title;
662 }
663
664 void _media_svc_remove_file(const char *path)
665 {
666         if (!STRING_VALID(path))
667                 return;
668
669         if (remove(path) != 0)
670                 media_svc_stderror("fail to remove file result");
671 }
672
673 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)
674 {
675         int ret = MS_MEDIA_ERR_NONE;
676         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
677         char hash[255 + 1] = {0, };
678         char *thumb_dir = NULL;
679         char *thumbfile_ext = NULL;
680
681         ret = ms_user_get_root_thumb_store_path(uid, &thumb_dir);
682         if (!STRING_VALID(thumb_dir)) {
683                 media_svc_error("ms_user_get_root_thumb_store_path failed");
684                 return MS_MEDIA_ERR_INTERNAL;
685         }
686
687         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
688                 media_svc_error("Wrong path[%s]", thumb_dir);
689                 g_free(thumb_dir);
690                 return MS_MEDIA_ERR_INTERNAL;
691         }
692
693         memset(file_ext, 0, sizeof(file_ext));
694         if (!__media_svc_get_file_ext(pathname, file_ext))
695                 media_svc_error("get file ext fail");
696
697         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
698         if (ret != MS_MEDIA_ERR_NONE) {
699                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
700                 g_free(thumb_dir);
701                 return MS_MEDIA_ERR_INTERNAL;
702         }
703
704         if (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC) {
705                 if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
706                         thumbfile_ext = (char *)"jpg";
707                 } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
708                         thumbfile_ext = (char *)"png";
709                 } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
710                         thumbfile_ext = (char *)"gif";
711                 } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
712                         thumbfile_ext = (char *)"bmp";
713                 } else {
714                         media_svc_error("Not proper img format");
715                         g_free(thumb_dir);
716                         return MS_MEDIA_ERR_INTERNAL;
717                 }
718
719                 snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
720         } else {
721                 if (strcasecmp(file_ext, "PNG") == 0)
722                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
723                 else
724                         snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.jpg", thumb_dir, file_ext, hash);
725         }
726
727         g_free(thumb_dir);
728
729         return MS_MEDIA_ERR_NONE;
730 }
731
732 int _media_svc_get_file_time(const char *full_path)
733 {
734         struct stat statbuf = { 0, };
735
736         if (stat(full_path, &statbuf) == -1) {
737                 media_svc_stderror("stat fails.");
738                 return 0;
739         }
740
741         return statbuf.st_mtime;
742 }
743
744 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)
745 {
746         int ret = MS_MEDIA_ERR_NONE;
747         bool drm_type = false;
748         char mime_type[256] = {0, };
749         media_svc_media_type_e media_type;
750
751         media_svc_retvm_if(!_media_svc_is_valid_storage_type(storage_type), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid storage_type");
752         media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
753
754         content_info->path = g_strdup(path);
755         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
756
757         struct stat st;
758         memset(&st, 0, sizeof(struct stat));
759         if (stat(path, &st) == 0) {
760                 content_info->modified_time = st.st_mtime;
761                 content_info->timeline = content_info->modified_time;
762                 content_info->size = st.st_size;
763         } else {
764                 media_svc_stderror("stat failed");
765         }
766
767         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
768         if (refresh) {
769                 media_svc_debug("refresh");
770                 return MS_MEDIA_ERR_NONE;
771         }
772
773         content_info->storage_uuid = g_strdup(storage_id);
774         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
775
776         content_info->storage_type = storage_type;
777         time(&content_info->added_time);
778
779         content_info->media_uuid = _media_info_generate_uuid();
780         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
781
782         content_info->file_name = g_path_get_basename(path);
783         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
784
785         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
786         if drm_contentinfo is not NULL, the file is OMA DRM.*/
787         ret = __media_svc_get_mime_type(path, mime_type);
788         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
789
790         media_svc_debug("mime [%s]", mime_type);
791         content_info->is_drm = drm_type;
792
793         ret = __media_svc_get_media_type(path, mime_type, &media_type);
794         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
795
796         content_info->mime_type = g_strdup(mime_type);
797         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
798
799         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, media_type);
800
801         content_info->media_type = media_type;
802
803         return MS_MEDIA_ERR_NONE;
804 }
805
806 static int __image_360_check(const char *path)
807 {
808         FILE *fp = NULL;
809         size_t size = 0, app1_size = 0, exif_app1_xmp_size = 0;
810         unsigned char exif_header[4] = {0, };
811         unsigned char exif_app1[2] = {0, };
812         unsigned char exif_app1_xmp[2] = {0, };
813         unsigned char exif_app1_xmp_t[2] = {0, };
814         GString *xmp_data = NULL;
815         int fdata = 0;
816         int result = 0;
817
818         memset(exif_header, 0x00, sizeof(exif_header));
819         memset(exif_app1, 0x00, sizeof(exif_app1));
820         memset(exif_app1_xmp, 0x00, sizeof(exif_app1_xmp));
821         memset(exif_app1_xmp_t, 0x00, sizeof(exif_app1_xmp_t));
822
823         fp = fopen(path, "rb");
824         if (fp == NULL)
825                 goto ERROR;
826
827         size = fread(exif_header, 1, sizeof(exif_header), fp);
828         if (size != sizeof(exif_header))
829                 goto ERROR;
830
831         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
832                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
833                 if (size != sizeof(exif_app1))
834                         goto ERROR;
835
836                 if ((size_t)((exif_app1[0] << 8) | (exif_app1[1])) <= 2)
837                         goto ERROR;
838
839                 app1_size = (size_t)((exif_app1[0] << 8) | (exif_app1[1])) - 2 ;
840                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
841                         goto ERROR;
842
843                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
844                 if (size != sizeof(exif_app1_xmp))
845                         goto ERROR;
846
847                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
848                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
849                         if (size != sizeof(exif_app1_xmp_t))
850                                 goto ERROR;
851
852                         if ((size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) <= 2)
853                                 goto ERROR;
854
855                         exif_app1_xmp_size = (size_t)((exif_app1_xmp_t[0] << 8) | (exif_app1_xmp_t[1])) - 2;
856
857                         xmp_data = g_string_sized_new(exif_app1_xmp_size);
858
859                         do {
860                                 exif_app1_xmp_size--;
861                                 fdata = fgetc(fp);
862                                 if (fdata == EOF)
863                                         continue;
864                                 if (fdata == '\0')
865                                         continue;
866
867                                 xmp_data = g_string_append_c(xmp_data, (gchar)fdata);
868                         } while (exif_app1_xmp_size > 0);
869
870                         if (strstr(xmp_data->str, "UsePanoramaViewer") &&
871                                 strstr(xmp_data->str, "True") &&
872                                 strstr(xmp_data->str, "ProjectionType") &&
873                                 strstr(xmp_data->str, "equirectangular"))
874                                 result = 1;
875
876                         g_string_free(xmp_data, TRUE);
877                 }
878         }
879
880 ERROR:
881         if (fp) {
882                 fclose(fp);
883                 fp = NULL;
884         }
885
886         return result;
887 }
888
889 static char * __media_svc_get_title(MMHandleType tag, const char *path)
890 {
891         int ret = FILEINFO_ERROR_NONE;
892         char *p = NULL;
893         int size = 0;
894         char *title = NULL;
895
896         if (tag) {
897                 ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
898                 if (ret == FILEINFO_ERROR_NONE && size > 0) {
899                         while(p && isspace(*p))
900                                 p++;
901
902                         return g_strdup(p);
903                 }
904         }
905
906         title = __media_svc_get_title_from_filepath(path);
907         if (title)
908                 return title;
909
910         media_svc_error("Can't extract title");
911
912         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
913 }
914
915 char * _media_svc_get_title_by_path(const char *path)
916 {
917         /* No MMHandleType in media-svc.c */
918         return __media_svc_get_title(NULL, path);
919 }
920
921 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
922 {
923         double value = 0.0;
924         int orient_value = 0;
925         int exif_width = 0;
926         int exif_height = 0;
927         ExifData *ed = NULL;
928         bool has_datetaken = false;
929         double fnumber = 0.0;
930         int iso = 0;
931         char *path = NULL;
932
933         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
934
935         media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
936         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);
937         media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
938
939         path = content_info->path;
940         content_info->media_meta.title = __media_svc_get_title(NULL, path);
941
942         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
943         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
944         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
945
946         /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
947         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
948         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
949         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
950         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
951         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
952         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
953         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
954         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
955
956         /* Load an ExifData object from an EXIF file */
957         ed = exif_data_new_from_file(path);
958         if (!ed) {
959                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
960                 goto GET_WIDTH_HEIGHT;
961         }
962
963         content_info->media_meta.is_360 = __image_360_check(path);
964
965         memset(buf, 0x00, sizeof(buf));
966         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
967                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
968                         if (!g_strcmp0(buf, "S"))
969                                 value *= -1;
970                         content_info->media_meta.latitude = value;
971                 }
972         }
973
974         memset(buf, 0x00, sizeof(buf));
975         if (__media_svc_get_exif_gps_double(ed, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
976                 if (__media_svc_get_exif_gps_str(ed, buf, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
977                         if (!g_strcmp0(buf, "W"))
978                                 value *= -1;
979
980                         content_info->media_meta.longitude = value;
981                 }
982         }
983
984         memset(buf, 0x00, sizeof(buf));
985         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
986                 if (strlen(buf) > 0)
987                         content_info->media_meta.description = g_strdup(buf);
988                 else
989                         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
990         }
991
992         memset(buf, 0x00, sizeof(buf));
993         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
994                 if (strlen(buf) > 0) {
995                         has_datetaken = true;
996                         content_info->media_meta.datetaken = g_strdup(buf);
997
998                         /* This is same as recorded_date */
999                         content_info->media_meta.recorded_date = g_strdup(buf);
1000                 }
1001         }
1002
1003         memset(buf, 0x00, sizeof(buf));
1004         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1005                 if (strlen(buf) > 0) {
1006                         has_datetaken = true;
1007                         content_info->media_meta.datetaken = g_strdup(buf);
1008
1009                         /* This is same as recorded_date */
1010                         content_info->media_meta.recorded_date = g_strdup(buf);
1011                 }
1012         }
1013
1014         if (has_datetaken) {
1015                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1016                 if (content_info->timeline == 0)
1017                         content_info->timeline = content_info->modified_time;
1018                 else
1019                         media_svc_debug("Timeline : %ld", content_info->timeline);
1020         }
1021
1022         memset(buf, 0x00, sizeof(buf));
1023         /* Get exposure_time value from exif. */
1024         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1025                 if (strlen(buf) > 0)
1026                         content_info->media_meta.exposure_time = g_strdup(buf);
1027         }
1028
1029         /* Get fnumber value from exif. */
1030         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE)
1031                 content_info->media_meta.fnumber = fnumber;
1032
1033         /* Get iso value from exif. */
1034         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE)
1035                 content_info->media_meta.iso = iso;
1036
1037         memset(buf, 0x00, sizeof(buf));
1038         /* Get model value from exif. */
1039         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1040                 if (strlen(buf) > 0)
1041                         content_info->media_meta.model = g_strdup(buf);
1042         }
1043
1044         /* Get orientation value from exif. */
1045         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1046                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1047                         content_info->media_meta.orientation = orient_value;
1048         }
1049
1050         /* Get width value from exif. */
1051         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1052                 if (exif_width > 0)
1053                         content_info->media_meta.width = exif_width;
1054         }
1055
1056         /* Get height value from exif. */
1057         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1058                 if (exif_height > 0)
1059                         content_info->media_meta.height = exif_height;
1060         }
1061
1062         if (ed)
1063                 exif_data_unref(ed);
1064
1065 GET_WIDTH_HEIGHT:
1066
1067         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1068                 /*Get image width, height*/
1069                 unsigned int img_width = 0;
1070                 unsigned int img_height = 0;
1071                 mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
1072
1073                 mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
1074                 if (content_info->media_meta.width == 0)
1075                         content_info->media_meta.width = img_width;
1076
1077                 if (content_info->media_meta.height == 0)
1078                         content_info->media_meta.height = img_height;
1079         }
1080
1081         return MS_MEDIA_ERR_NONE;
1082 }
1083
1084 static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_name)
1085 {
1086         int ret = FILEINFO_ERROR_NONE;
1087         char *p = NULL;
1088         int size = 0;
1089
1090         ret = mm_file_get_attrs(tag, tag_name, &p, &size, NULL);
1091         if (ret == FILEINFO_ERROR_NONE && size > 0)
1092                 return g_strdup(p);
1093
1094         return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1095 }
1096
1097 int _media_svc_extract_music_metadata_for_update(media_svc_content_info_s *content_info, const char *path)
1098 {
1099         MMHandleType tag = 0;
1100         int mmf_error = FILEINFO_ERROR_NONE;
1101
1102         content_info->path = g_strdup(path);
1103
1104         mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
1105         if (mmf_error == FILEINFO_ERROR_NONE) {
1106                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1107                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1108                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1109                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1110                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1111                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1112                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1113                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1114         } else {
1115                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1116         }
1117
1118         mm_file_destroy_tag_attrs(tag);
1119
1120         return MS_MEDIA_ERR_NONE;
1121 }
1122
1123 int _media_svc_extract_media_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
1124 {
1125         MMHandleType content = 0;
1126         MMHandleType tag = 0;
1127         char *p = NULL;
1128         unsigned char *image = NULL;
1129         unsigned int size = 0;
1130         int mmf_error = FILEINFO_ERROR_NONE;
1131         int album_id = 0;
1132         int ret = MS_MEDIA_ERR_NONE;
1133         int convert_value = 0;
1134         int cdis_value = 0;
1135
1136         /*Get Content Tag attribute ===========*/
1137         if (content_info->storage_type == MS_USER_STORAGE_EXTERNAL_USB)
1138                 mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
1139         else
1140                 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1141
1142         if (mmf_error == FILEINFO_ERROR_NONE) {
1143                 content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
1144                 content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
1145                 content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
1146                 content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
1147                 content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
1148                 content_info->media_meta.description = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_DESCRIPTION);
1149                 content_info->media_meta.composer = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_AUTHOR);
1150                 content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
1151                 content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
1152
1153                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1154                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1155                         if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
1156                                 /*Creation time format is 2013-01-01 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
1157                                 char *p_value = g_strdelimit(g_strdup(p), "-", ':');
1158                                 content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
1159                                 g_free(p_value);
1160                         } else {
1161                                 content_info->media_meta.recorded_date = g_strdup(p);
1162                         }
1163
1164                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1165                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1166                                 if (content_info->timeline == 0)
1167                                         content_info->timeline = content_info->modified_time;
1168
1169                                 /* This is same as datetaken */
1170                                 /* Remove compensation string */
1171                                 if (strlen(content_info->media_meta.recorded_date) > MEDIA_SVC_DEFAULT_FORMAT_LEN) {
1172                                         content_info->media_meta.datetaken = g_strndup(content_info->media_meta.recorded_date, MEDIA_SVC_DEFAULT_FORMAT_LEN);
1173                                         g_free(content_info->media_meta.recorded_date);
1174                                         content_info->media_meta.recorded_date = g_strdup(content_info->media_meta.datetaken);
1175                                 } else {
1176                                         content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1177                                 }
1178                         }
1179                 }
1180
1181                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
1182                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1183                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1184                                 content_info->media_meta.year = g_strdup(p);
1185                 }
1186
1187                 if (!content_info->media_meta.year)
1188                                 content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1189
1190                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RATING, &p, &size, NULL);
1191                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1192                         if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
1193                                 content_info->media_meta.rating = convert_value;
1194                 } else {
1195                         content_info->media_meta.rating = 0;
1196                 }
1197
1198                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1199
1200                 /*Do not extract artwork for the USB Storage content*/
1201                 if (content_info->storage_type != MS_USER_STORAGE_EXTERNAL_USB) {
1202                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1203                         if (mmf_error != FILEINFO_ERROR_NONE)
1204                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1205
1206                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1207                         if (mmf_error != FILEINFO_ERROR_NONE)
1208                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1209
1210                         if (image != NULL && size > 0) {
1211                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1212                                 int artwork_mime_size = -1;
1213
1214                                 mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1215                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1216                                         ret = _media_svc_get_thumbnail_path(content_info->media_type, thumb_path, content_info->path, p, uid);
1217                                         if (ret != MS_MEDIA_ERR_NONE) {
1218                                                 media_svc_error("Fail to Get Thumbnail Path");
1219                                         } else {
1220                                                 ret = __media_svc_save_image(image, size, thumb_path, uid);
1221                                                 if (ret != MS_MEDIA_ERR_NONE) {
1222                                                         media_svc_error("Fail to Save Image");
1223                                                 } else {
1224                                                         content_info->thumbnail_path = g_strdup(thumb_path);
1225                                                         /* NOTICE : Prevent resize for performance (2020.02.07)
1226                                                         *  In most cases, artwork's format is jpeg and size is under MEDIA_SVC_ARTWORK_SIZE * MEDIA_SVC_ARTWORK_SIZE.
1227                                                         *  So, doing mm_util_extract_image_info to check image size is a time-consuming task.
1228                                                         */
1229 #if 0
1230                                                         /* albumart resizing */
1231                                                         ret = __media_svc_resize_artwork(thumb_path, p);
1232                                                         if (ret != MS_MEDIA_ERR_NONE) {
1233                                                                 media_svc_error("Fail to Make Thumbnail Image");
1234                                                                 _media_svc_remove_file(thumb_path);
1235
1236                                                         } else {
1237                                                                 content_info->thumbnail_path = g_strdup(thumb_path);
1238                                                         }
1239 #endif
1240                                                 }
1241                                         }
1242                                 }
1243                         }
1244                 }
1245
1246                 /*Initialize album_id to 0. below code will set the album_id*/
1247                 content_info->album_id = album_id;
1248                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1249                 if (ret != MS_MEDIA_ERR_NONE) {
1250                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1251                                 media_svc_debug("album does not exist. So start to make album art");
1252                                 if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1253                                         (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1254                                         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);
1255                                 else
1256                                         ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1257
1258                                 content_info->album_id = album_id;
1259                         }
1260                 } else {
1261                         content_info->album_id = album_id;
1262                 }
1263
1264                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1265                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1266                 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1267
1268                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1269                         double longitude = 0.0;
1270                         double latitude = 0.0;
1271                         double altitude = 0.0;
1272
1273                         mm_file_get_attrs(tag, MM_FILE_TAG_LONGITUDE, &longitude,
1274                                 MM_FILE_TAG_LATIDUE, &latitude,
1275                                 MM_FILE_TAG_ALTIDUE, &altitude,
1276                                 NULL);
1277
1278                         content_info->media_meta.longitude = (longitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : longitude;
1279                         content_info->media_meta.latitude = (latitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : latitude;
1280                         content_info->media_meta.altitude = (altitude == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : altitude;
1281
1282                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1283                         if (mmf_error != FILEINFO_ERROR_NONE)
1284                                 cdis_value = 0;
1285
1286                         media_svc_debug("CDIS : %d", cdis_value);
1287
1288                         mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1289                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1290                                 content_info->media_meta.orientation = atoi(p);
1291                         } else {
1292                                 content_info->media_meta.orientation = 0;
1293                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1294                         }
1295                 }
1296
1297                 mmf_error = mm_file_destroy_tag_attrs(tag);
1298                 if (mmf_error != FILEINFO_ERROR_NONE)
1299                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1300         }       else {
1301                 content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
1302                 content_info->album_id = album_id;
1303         }
1304
1305         /*Get Content attribute ===========*/
1306         if (cdis_value == 1)
1307                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1308         else
1309                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1310
1311         media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
1312
1313         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1314                 int audio_bitrate = 0;
1315                 int video_bitrate = 0;
1316
1317                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1318                         MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate,
1319                         MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate,
1320                         MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
1321                         MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
1322                         NULL);
1323
1324                 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1325         } else {
1326                 mm_file_get_attrs(content, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration,
1327                         MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate,
1328                         MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate,
1329                         MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel,
1330                         MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample,
1331                         NULL);
1332         }
1333
1334         mm_file_destroy_content_attrs(content);
1335
1336         return MS_MEDIA_ERR_NONE;
1337 }
1338
1339 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1340 {
1341         media_svc_retm_if(content_info == NULL, "content info is NULL");
1342
1343         /* Delete media_svc_content_info_s */
1344         g_free(content_info->media_uuid);
1345         g_free(content_info->path);
1346         g_free(content_info->file_name);
1347         g_free(content_info->mime_type);
1348         g_free(content_info->folder_uuid);
1349         g_free(content_info->thumbnail_path);
1350         g_free(content_info->storage_uuid);
1351
1352         /* Delete media_svc_content_meta_s */
1353         g_free(content_info->media_meta.title);
1354         g_free(content_info->media_meta.album);
1355         g_free(content_info->media_meta.artist);
1356         g_free(content_info->media_meta.album_artist);
1357         g_free(content_info->media_meta.genre);
1358         g_free(content_info->media_meta.composer);
1359         g_free(content_info->media_meta.year);
1360         g_free(content_info->media_meta.recorded_date);
1361         g_free(content_info->media_meta.copyright);
1362         g_free(content_info->media_meta.track_num);
1363         g_free(content_info->media_meta.description);
1364         g_free(content_info->media_meta.datetaken);
1365         g_free(content_info->media_meta.exposure_time);
1366         g_free(content_info->media_meta.model);
1367
1368         g_free(content_info->file_name_pinyin);
1369         g_free(content_info->media_meta.title_pinyin);
1370         g_free(content_info->media_meta.album_pinyin);
1371         g_free(content_info->media_meta.artist_pinyin);
1372         g_free(content_info->media_meta.album_artist_pinyin);
1373         g_free(content_info->media_meta.genre_pinyin);
1374         g_free(content_info->media_meta.composer_pinyin);
1375         g_free(content_info->media_meta.copyright_pinyin);
1376         g_free(content_info->media_meta.description_pinyin);
1377 }
1378
1379 int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_media_type_e media_type, uid_t uid)
1380 {
1381         int ret = MS_MEDIA_ERR_NONE;
1382
1383         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
1384         media_svc_retvm_if(!thumb_path, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid thumb_path");
1385         media_svc_retvm_if(!g_file_test(path, G_FILE_TEST_IS_REGULAR), MS_MEDIA_ERR_INVALID_PARAMETER, "File doesn't exist[%s]", path);
1386
1387         ms_user_storage_type_e store_type = -1;
1388         ret = ms_user_get_storage_type(uid, path, &store_type);
1389
1390         if ((ret != MS_MEDIA_ERR_NONE) || ((store_type != MS_USER_STORAGE_INTERNAL) && (store_type != MS_USER_STORAGE_EXTERNAL))) {
1391                 media_svc_sec_error("origin path(%s) is invalid. err : [%d] store_type [%d]", path, ret, store_type);
1392                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1393         }
1394
1395         media_svc_sec_debug("Path[%s] Type[%d]", path, media_type);
1396
1397         //1. make hash path
1398         ret = _media_svc_get_thumbnail_path(media_type, thumb_path, path, NULL, uid);
1399         if (ret != MS_MEDIA_ERR_NONE) {
1400                 media_svc_error("_media_svc_get_thumbnail_path failed - %d", ret);
1401                 SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
1402                 return ret;
1403         }
1404
1405         //2. save thumbnail
1406         if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)
1407                 return create_image_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1408         else
1409                 return create_video_thumbnail_to_file(path, CONTENT_THUMB_DEFAULT_WIDTH, CONTENT_THUMB_DEFAULT_HEIGHT, thumb_path, true);
1410
1411 }
1412
1413 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1414 {
1415         int ret = MS_MEDIA_ERR_NONE;
1416         int size = 0;
1417         pinyin_name_s *pinyinname = NULL;
1418
1419         media_svc_retvm_if(!STRING_VALID(src_str), MS_MEDIA_ERR_INVALID_PARAMETER, "String is NULL");
1420         media_svc_retvm_if(pinyin_str == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "pinyin_str is NULL");
1421
1422         *pinyin_str = NULL;
1423
1424         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1425         if (ret == MS_MEDIA_ERR_NONE) {
1426                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1427                         *pinyin_str = g_strdup(pinyinname[0].pinyin_name);
1428                 else
1429                         *pinyin_str = g_strdup(src_str);        /* Return Original Non China Character */
1430         }
1431
1432         _media_svc_pinyin_free(pinyinname, size);
1433
1434         return ret;
1435 }
1436
1437 bool _media_svc_check_pinyin_support(void)
1438 {
1439         int ret = SYSTEM_INFO_ERROR_NONE;
1440         bool is_supported = false;
1441         static int media_svc_pinyin_support = -1;
1442
1443         if (media_svc_pinyin_support == -1) {
1444                 ret = system_info_get_platform_bool("http://tizen.org/feature/content.filter.pinyin", &is_supported);
1445                 if (ret != SYSTEM_INFO_ERROR_NONE) {
1446                         media_svc_debug("SYSTEM_INFO_ERROR: content.filter.pinyin [%d]", ret);
1447                         return false;
1448                 }
1449
1450                 media_svc_pinyin_support = is_supported;
1451         }
1452
1453         return media_svc_pinyin_support;
1454 }
1455
1456 int _media_svc_get_media_type(const char *path, int *mediatype)
1457 {
1458         int ret = MS_MEDIA_ERR_NONE;
1459         char mime_type[256] = {0};
1460         media_svc_media_type_e media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1461
1462         media_svc_retvm_if(mediatype == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "mediatype is NULL");
1463
1464         ret = __media_svc_get_mime_type(path, mime_type);
1465         if (ret == MS_MEDIA_ERR_NONE)
1466                 __media_svc_get_media_type(path, mime_type, &media_type);
1467         else
1468                 media_svc_error("__media_svc_get_mime_type failed");
1469
1470         *mediatype = media_type;
1471
1472         return ret;
1473 }
1474
1475 bool _media_svc_is_valid_storage_type(ms_user_storage_type_e storage_type)
1476 {
1477         switch (storage_type) {
1478         case MS_USER_STORAGE_INTERNAL:
1479         case MS_USER_STORAGE_EXTERNAL:
1480         case MS_USER_STORAGE_EXTERNAL_USB:
1481                 return true;
1482         default:
1483                 media_svc_error("storage type is incorrect[%d]", storage_type);
1484                 return false;
1485         }
1486 }