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