Use Common API in libmedia-util to get User path
[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 <sys/vfs.h>
34 #include <glib/gstdio.h>
35 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <ctype.h>
38 #include <aul/aul.h>
39 #include <mm_file.h>
40 #include <libexif/exif-data.h>
41 #include <media-thumbnail.h>
42 #include <media-util.h>
43 #include <uuid/uuid.h>
44 #include <img-codec-parser.h>
45 #include <image_util.h>
46 #include <image_util_internal.h>
47 #include "media-util-err.h"
48 #include "media-svc-util.h"
49 #include "media-svc-db-utils.h"
50 #include "media-svc-debug.h"
51 #include "media-svc-env.h"
52 #include "media-svc-hash.h"
53 #include "media-svc-album.h"
54 #include "media-svc-localize-utils.h"
55 #include "media-svc-localize_ch.h"
56 #include "media-svc-localize_tw.h"
57
58 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**< Maximum file ext lenth*/
59
60 /* Define data structures for media type and mime type */
61 #define MEDIA_SVC_CATEGORY_UNKNOWN      0x00000000      /**< Default */
62 #define MEDIA_SVC_CATEGORY_ETC          0x00000001      /**< ETC category */
63 #define MEDIA_SVC_CATEGORY_IMAGE        0x00000002      /**< Image category */
64 #define MEDIA_SVC_CATEGORY_VIDEO        0x00000004      /**< Video category */
65 #define MEDIA_SVC_CATEGORY_MUSIC        0x00000008      /**< Music category */
66 #define MEDIA_SVC_CATEGORY_SOUND        0x00000010      /**< Sound category */
67 #define MEDIA_SVC_CATEGORY_PVR  0x00000020      /**< PVR category */
68 #define MEDIA_SVC_CATEGORY_UHD  0x00000040      /**< UHD category */
69 #define MEDIA_SVC_CATEGORY_SCSA 0x00000080      /**< SCSA category */
70
71 #define CONTENT_TYPE_NUM 5
72 #define MUSIC_MIME_NUM 29
73 #define SOUND_MIME_NUM 1
74 #define MIME_TYPE_LENGTH 255
75 #define MIME_LENGTH 50
76 #define _3GP_FILE ".3gp"
77 #define _MP4_FILE ".mp4"
78 #define _ASF_FILE ".asf"
79 #define MEDIA_SVC_ARTWORK_SIZE 2000
80
81 typedef struct {
82         char content_type[15];
83         int category_by_mime;
84 } _media_svc_content_table_s;
85
86 static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
87         {"audio", MEDIA_SVC_CATEGORY_SOUND},
88         {"image", MEDIA_SVC_CATEGORY_IMAGE},
89         {"video", MEDIA_SVC_CATEGORY_VIDEO},
90         {"application", MEDIA_SVC_CATEGORY_ETC},
91         {"text", MEDIA_SVC_CATEGORY_ETC},
92 };
93
94 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
95         /*known mime types of normal files*/
96         "mpeg",
97         "ogg",
98         "x-ms-wma",
99         "x-flac",
100         "mp4",
101         /* known mime types of drm files*/
102         "mp3",
103         "x-mp3", /*alias of audio/mpeg*/
104         "x-mpeg", /*alias of audio/mpeg*/
105         "3gpp",
106         "x-ogg", /*alias of audio/ogg*/
107         "vnd.ms-playready.media.pya:*.pya", /*playready*/
108         "wma",
109         "aac",
110         "x-m4a", /*alias of audio/mp4*/
111         /* below mimes are rare*/
112         "x-vorbis+ogg",
113         "x-flac+ogg",
114         "x-matroska",
115         "ac3",
116         "mp2",
117         "x-ape",
118         "x-ms-asx",
119         "vnd.rn-realaudio",
120
121         "x-vorbis", /*alias of audio/x-vorbis+ogg*/
122         "vorbis", /*alias of audio/x-vorbis+ogg*/
123         "x-oggflac",
124         "x-mp2", /*alias of audio/mp2*/
125         "x-pn-realaudio", /*alias of audio/vnd.rn-realaudio*/
126         "vnd.m-realaudio", /*alias of audio/vnd.rn-realaudio*/
127         "x-wav",
128 };
129
130 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
131         "x-smaf",
132 };
133
134 typedef enum {
135         MEDIA_SVC_EXTRACTED_FIELD_NONE                  = 0x00000001,
136         MEDIA_SVC_EXTRACTED_FIELD_TITLE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
137         MEDIA_SVC_EXTRACTED_FIELD_DESC                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
138         MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT             = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
139         MEDIA_SVC_EXTRACTED_FIELD_AUTHOR                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
140         MEDIA_SVC_EXTRACTED_FIELD_ARTIST                        = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
141         MEDIA_SVC_EXTRACTED_FIELD_GENRE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
142         MEDIA_SVC_EXTRACTED_FIELD_ALBUM                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
143         MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
144         MEDIA_SVC_EXTRACTED_FIELD_YEAR                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
145         MEDIA_SVC_EXTRACTED_FIELD_CATEGORY              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
146         MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 11,
147 } media_svc_extracted_field_e;
148
149 char *_media_info_generate_uuid(void)
150 {
151         uuid_t uuid_value;
152         static char uuid_unparsed[37];
153
154 RETRY_GEN:
155         uuid_generate(uuid_value);
156         uuid_unparse(uuid_value, uuid_unparsed);
157
158         if (strlen(uuid_unparsed) < 36) {
159                 media_svc_debug("INVALID UUID : %s. RETRY GENERATE.", uuid_unparsed);
160                 goto RETRY_GEN;
161         }
162
163         return uuid_unparsed;
164 }
165
166 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
167 {
168         if (!x_src || strlen(x_src) == 0) {
169                 media_svc_error("x_src is NULL");
170                 return;
171         }
172
173         if (max_len < 1) {
174                 media_svc_error("length is Wrong");
175                 return;
176         }
177
178         strncpy(x_dst, x_src, max_len - 1);
179         x_dst[max_len - 1] = '\0';
180 }
181
182 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
183 {
184         int len = 0;
185
186         if (!STRING_VALID(src)) {
187                 media_svc_error("invalid src");
188                 return MS_MEDIA_ERR_INVALID_PARAMETER;
189         }
190
191         SAFE_FREE(*dst);
192
193         len = strlen(src) + 1;
194         *dst = malloc(len);
195
196         if (*dst == NULL) {
197                 media_svc_error("malloc failed");
198                 return MS_MEDIA_ERR_INTERNAL;
199         }
200
201         strncpy(*dst, src, len);
202         char *p = *dst;
203         p[len - 1] = '\0';
204
205         return MS_MEDIA_ERR_NONE;
206 }
207
208 int __media_svc_malloc_and_strncpy_with_size(char **dst, const char *src, int copysize)
209 {
210         if (!STRING_VALID(src)) {
211                 media_svc_error("invalid src");
212                 return MS_MEDIA_ERR_INVALID_PARAMETER;
213         }
214
215         SAFE_FREE(*dst);
216
217         *dst = malloc(copysize + 1);
218
219         if (*dst == NULL) {
220                 media_svc_error("malloc failed");
221                 return MS_MEDIA_ERR_INTERNAL;
222         }
223
224         strncpy(*dst, src, copysize);
225         char *p = *dst;
226         p[copysize] = '\0';
227
228         return MS_MEDIA_ERR_NONE;
229 }
230
231 static int __media_svc_split_to_double(char *input, double *arr)
232 {
233         char tmp_arr[255] = {0, };
234         int len = 0, idx = 0, arr_idx = 0, str_idx = 0;
235
236         if (!STRING_VALID(input)) {
237                 media_svc_error("Invalid parameter");
238                 return MS_MEDIA_ERR_INVALID_PARAMETER;
239         }
240         memset(tmp_arr, 0x0, sizeof(tmp_arr));
241
242         /*media_svc_debug("input: [%s]", input); */
243
244         len = strlen(input);
245
246         for (idx = 0; idx < (len + 1); idx++) {
247                 if (input[idx] == ' ') {
248                         continue;
249                 } else if ((input[idx] == ',') || (idx == len)) {
250                         arr[arr_idx] = atof(tmp_arr);
251                         arr_idx++;
252                         str_idx = 0;
253                         /*media_svc_debug("idx=[%d] arr_idx=[%d] tmp_attr[%s] atof(tmp_arr)=[%f]", idx, arr_idx, tmp_arr, atof(tmp_arr)); */
254                         memset(tmp_arr, 0x0, sizeof(tmp_arr));
255                 } else {
256                         tmp_arr[str_idx] = input[idx];
257                         str_idx++;
258                 }
259         }
260
261         if (arr_idx != 3) {
262                 media_svc_debug("Error when parsing GPS [%d]", arr_idx);
263                 return MS_MEDIA_ERR_INTERNAL;
264         }
265
266         return MS_MEDIA_ERR_NONE;
267 }
268
269 static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, double *d_value, long tagtype)
270 {
271         ExifEntry *entry;
272         ExifTag tag;
273
274         if (ed == NULL)
275                 return MS_MEDIA_ERR_INVALID_PARAMETER;
276
277         tag = tagtype;
278
279         entry = exif_data_get_entry(ed, tag);
280         if (entry) {
281                 /* Get the contents of the tag in human-readable form */
282                 if (tag == EXIF_TAG_ORIENTATION ||
283                         tag == EXIF_TAG_PIXEL_X_DIMENSION ||
284                         tag == EXIF_TAG_PIXEL_Y_DIMENSION ||
285                         tag == EXIF_TAG_ISO_SPEED_RATINGS) {
286
287                         if (i_value == NULL) {
288                                 media_svc_debug("i_value is NULL");
289                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
290                         }
291
292                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
293                         short exif_value = exif_get_short(entry->data, mByteOrder);
294                         *i_value = (int)exif_value;
295
296                 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
297
298                         if (d_value == NULL) {
299                                 media_svc_debug("d_value is NULL");
300                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
301                         }
302
303                         /* Get the contents of the tag in human-readable form */
304                         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = {0, };
305                         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
306                         gps_buf[strlen(gps_buf)] = '\0';
307                         int ret = MS_MEDIA_ERR_NONE;
308
309                         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
310
311                         ret = __media_svc_split_to_double(gps_buf, tmp_arr);
312                         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
313
314                         *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
315                 } else if (tag == EXIF_TAG_EXPOSURE_TIME) {
316
317                         if (buf == NULL) {
318                                 media_svc_debug("buf is NULL");
319                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
320                         }
321
322                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
323                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
324                         long numerator = mRational.numerator;
325                         long denominator = mRational.denominator;
326                         snprintf(buf, MEDIA_SVC_METADATA_LEN_MAX, "%ld/%ld", numerator, denominator);
327
328                 } else if (tag == EXIF_TAG_FNUMBER) {
329
330                         if (d_value == NULL) {
331                                 media_svc_debug("d_value is NULL");
332                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
333                         }
334
335                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
336                         ExifRational mRational = exif_get_rational(entry->data, mByteOrder);
337                         long numerator = mRational.numerator;
338                         long denominator = mRational.denominator;
339
340                         *d_value = ((numerator*1.0)/(denominator*1.0));
341
342                 } else {
343
344                         if (buf == NULL) {
345                                 media_svc_debug("buf is NULL");
346                                 return MS_MEDIA_ERR_INVALID_PARAMETER;
347                         }
348
349                         exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
350                         buf[strlen(buf)] = '\0';
351                 }
352         }
353
354         return MS_MEDIA_ERR_NONE;
355 }
356
357 time_t __media_svc_get_timeline_from_str(const char *timstr)
358 {
359         struct tm t;
360         time_t modified_t = 0;
361         time_t rawtime;
362         struct tm timeinfo;
363
364         if (!STRING_VALID(timstr)) {
365                 media_svc_error("Invalid Parameter");
366                 return 0;
367         }
368
369         /*Exif Format : %Y:%m:%d %H:%M:%S
370         Videoc Content Creation_time format of FFMpeg : %Y-%m-%d %H:%M:%S*/
371         memset(&t, 0x00, sizeof(struct tm));
372
373         tzset();
374         time(&rawtime);
375         localtime_r(&rawtime, &timeinfo);
376
377         if (strptime(timstr, "%Y:%m:%d %H:%M:%S", &t) || strptime(timstr, "%Y-%m-%d %H:%M:%S", &t)) {
378                 t.tm_isdst = timeinfo.tm_isdst;
379                 if (t.tm_isdst != 0)
380                         media_svc_debug("DST %d", t.tm_isdst);
381
382                 modified_t = mktime(&t);
383                 if (modified_t > 0)
384                         return modified_t;
385                 else
386                         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);
387         } else {
388                 media_svc_error("Failed to get timeline : [%s]", timstr);
389         }
390
391         return 0;
392 }
393
394 static int __media_svc_get_content_type_from_mime(const char *path, const char *mimetype, int *category)
395 {
396         int idx = 0;
397
398         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
399
400         /*categorize from mimetype */
401         for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
402                 if (strstr(mimetype, content_category[idx].content_type) != NULL) {
403                         *category = (*category | content_category[idx].category_by_mime);
404                         break;
405                 }
406         }
407
408         /*in application type, exitst sound file ex) x-smafs, asf */
409         if (*category & MEDIA_SVC_CATEGORY_ETC) {
410                 int prefix_len = strlen(content_category[0].content_type);
411                 char *ext = NULL;
412
413                 for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
414                         if (strstr(mimetype + prefix_len, sound_mime_table[idx]) != NULL) {
415                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
416                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
417                                 break;
418                         }
419                 }
420
421                 if (strncasecmp(mimetype, "text/x-iMelody", strlen("text/x-iMelody")) == 0) {
422                         *category ^= MEDIA_SVC_CATEGORY_ETC;
423                         *category |= MEDIA_SVC_CATEGORY_SOUND;
424                 }
425
426                 /*"asf" must check video stream and then categorize in directly. */
427                 ext = strrchr(path, '.');
428                 if (ext != NULL) {
429                         if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
430                                 int audio = 0;
431                                 int video = 0;
432                                 int err = 0;
433
434                                 err = mm_file_get_stream_info(path, &audio, &video);
435                                 if (err == 0) {
436                                         if (audio > 0 && video == 0) {
437                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
438                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
439                                         } else {
440                                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
441                                                 *category |= MEDIA_SVC_CATEGORY_VIDEO;
442                                         }
443                                 }
444                         }
445                 }
446         }
447
448         /*check music file in soun files. */
449         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
450                 int prefix_len = strlen(content_category[0].content_type) + 1;
451
452                 for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
453                         if (strcmp(mimetype + prefix_len, music_mime_table[idx]) == 0) {
454                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
455                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
456                                 break;
457                         }
458                 }
459
460                 /*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*/
461                 if (strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
462                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
463                         *category |= MEDIA_SVC_CATEGORY_ETC;
464                 }
465         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
466                 /*some video files don't have video stream. in this case it is categorize as music. */
467                 char *ext = NULL;
468                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
469                 ext = strrchr(path, '.');
470                 if (ext != NULL) {
471                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
472                                 int audio = 0;
473                                 int video = 0;
474                                 int err = 0;
475
476                                 err = mm_file_get_stream_info(path, &audio, &video);
477                                 if (err == 0) {
478                                         if (audio > 0 && video == 0) {
479                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
480                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
481                                         }
482                                 }
483                                 /*even though error occued in mm_file_get_stream_info return MS_MEDIA_ERR_NONE. fail means invalid media content. */
484                         }
485                 }
486         }
487
488         return MS_MEDIA_ERR_NONE;
489 }
490
491 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
492 {
493         int ret = MS_MEDIA_ERR_NONE;
494         int category = 0;
495
496         media_svc_media_type_e type;
497
498         ret = __media_svc_get_content_type_from_mime(path, mime_type, &category);
499         if (ret != MS_MEDIA_ERR_NONE)
500                 media_svc_error("__media_svc_get_content_type_from_mime failed : %d", ret);
501
502         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
503         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
504         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
505         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
506         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
507
508         *media_type = type;
509
510         return ret;
511 }
512
513 /*
514 drm_contentifo is not NULL, if the file is OMA DRM.
515 If the file is not OMA DRM, drm_contentinfo must be NULL.
516 */
517 static int __media_svc_get_mime_type(const char *path, char *mimetype)
518 {
519         if (path == NULL)
520                 return MS_MEDIA_ERR_INVALID_PARAMETER;
521
522         /*in case of normal files or failure to get mime in drm */
523         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
524                 media_svc_error("aul_get_mime_from_file fail");
525                 return MS_MEDIA_ERR_INTERNAL;
526         }
527
528         return MS_MEDIA_ERR_NONE;
529 }
530
531 static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
532 {
533         int i = 0;
534
535         for (i = strlen(file_path); i >= 0; i--) {
536                 if (file_path[i] == '.') {
537                         _strncpy_safe(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
538                         return true;
539                 }
540
541                 if (file_path[i] == '/')
542                         return false;
543         }
544         return false;
545 }
546
547 static int __media_svc_get_location_value(MMHandleType tag, double *longitude, double *latitude, double *altitude)
548 {
549         char *err_attr_name = NULL;
550         double gps_value = 0.0;
551         int mmf_error = FILEINFO_ERROR_NONE;
552
553         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
554         if (mmf_error == FILEINFO_ERROR_NONE) {
555                 if (longitude != NULL)
556                         *longitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
557         } else {
558                 SAFE_FREE(err_attr_name);
559         }
560
561         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
562         if (mmf_error == FILEINFO_ERROR_NONE) {
563                 if (latitude != NULL)
564                         *latitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
565         } else {
566                 SAFE_FREE(err_attr_name);
567         }
568
569         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
570         if (mmf_error == FILEINFO_ERROR_NONE) {
571                 if (altitude != NULL)
572                         *altitude = (gps_value == 0.0) ? MEDIA_SVC_DEFAULT_GPS_VALUE : gps_value;
573         } else {
574                 SAFE_FREE(err_attr_name);
575         }
576
577         return MS_MEDIA_ERR_NONE;
578 }
579
580 static int __media_svc_encode_jpeg(unsigned char *src, unsigned long width, unsigned long height, image_util_colorspace_e colorspace, int quality, unsigned char **dst, unsigned long long *dst_size)
581 {
582         int res = IMAGE_UTIL_ERROR_NONE;
583         image_util_encode_h encoder = NULL;
584         unsigned char *encoded_data = NULL;
585         res = image_util_encode_create(IMAGE_UTIL_JPEG , &encoder);
586         if (res != IMAGE_UTIL_ERROR_NONE) {
587                 media_svc_error("image_util_encode_create failed! (%d)", res);
588                 return MS_MEDIA_ERR_INTERNAL;
589         }
590         res = image_util_encode_set_resolution(encoder, width, height);
591         if (res != IMAGE_UTIL_ERROR_NONE) {
592                 media_svc_error("image_util_encode_set_resolution failed! (%d)", res);
593                 image_util_encode_destroy(encoder);
594                 return MS_MEDIA_ERR_INTERNAL;
595         }
596         res = image_util_encode_set_colorspace(encoder, colorspace);
597         if (res != IMAGE_UTIL_ERROR_NONE) {
598                 media_svc_error("image_util_encode_set_colorspace failed! (%d)", res);
599                 image_util_encode_destroy(encoder);
600                 return MS_MEDIA_ERR_INTERNAL;
601         }
602         res = image_util_encode_set_quality(encoder, quality);
603         if (res != IMAGE_UTIL_ERROR_NONE) {
604                 media_svc_error("image_util_encode_set_quality failed! (%d)", res);
605                 image_util_encode_destroy(encoder);
606                 return MS_MEDIA_ERR_INTERNAL;
607         }
608         res = image_util_encode_set_input_buffer(encoder, src);
609         if (res != IMAGE_UTIL_ERROR_NONE) {
610                 media_svc_error("image_util_encode_set_input_buffer failed! (%d)", res);
611                 image_util_encode_destroy(encoder);
612                 return MS_MEDIA_ERR_INTERNAL;
613         }
614         res = image_util_encode_set_output_buffer(encoder, &encoded_data);
615         if (res != IMAGE_UTIL_ERROR_NONE) {
616                 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
617                 image_util_encode_destroy(encoder);
618                 return MS_MEDIA_ERR_INTERNAL;
619         }
620         res = image_util_encode_run(encoder, dst_size);
621         if (res != IMAGE_UTIL_ERROR_NONE) {
622                 media_svc_error("image_util_encode_run failed! (%d)", res);
623                 image_util_encode_destroy(encoder);
624                 return MS_MEDIA_ERR_INTERNAL;
625         }
626         if (encoded_data != NULL) {
627                 *dst = (unsigned char *)calloc(1, *dst_size);
628                 if (*dst == NULL) {
629                         media_svc_error("memory allocation failed! (%lld)", *dst_size);
630                         image_util_encode_destroy(encoder);
631                         return MS_MEDIA_ERR_INTERNAL;
632                 }
633                 memcpy(*dst, encoded_data, *dst_size);
634         }
635         res = image_util_encode_destroy(encoder);
636         if (res != IMAGE_UTIL_ERROR_NONE) {
637                 media_svc_error("image_util_encode_destroy failed! (%d)", res);
638                 return MS_MEDIA_ERR_INTERNAL;
639         }
640         SAFE_FREE(encoded_data);
641         return MS_MEDIA_ERR_NONE;
642 }
643
644 static int __media_svc_decode_jpeg(unsigned char *src, unsigned long long size, image_util_colorspace_e colorspace, unsigned char **dst, unsigned long *width, unsigned long *height, unsigned long long *dst_size)
645 {
646         int res = IMAGE_UTIL_ERROR_NONE;
647         image_util_decode_h decoder = NULL;
648         res = image_util_decode_create(&decoder);
649         if (res != IMAGE_UTIL_ERROR_NONE) {
650                 media_svc_error("image_util_decode_create failed! (%d)", res);
651                 return MS_MEDIA_ERR_INTERNAL;
652         }
653         res = image_util_decode_set_input_buffer(decoder, src, size);
654         if (res != IMAGE_UTIL_ERROR_NONE) {
655                 media_svc_error("image_util_decode_set_input_buffer failed! (%d)", res);
656                 image_util_decode_destroy(decoder);
657                 return MS_MEDIA_ERR_INTERNAL;
658         }
659         res = image_util_decode_set_colorspace(decoder, colorspace);
660         if (res != IMAGE_UTIL_ERROR_NONE) {
661                 media_svc_error("image_util_decode_set_colorspace failed! (%d)", res);
662                 image_util_decode_destroy(decoder);
663                 return MS_MEDIA_ERR_INTERNAL;
664         }
665         res = image_util_decode_set_output_buffer(decoder, dst);
666         if (res != IMAGE_UTIL_ERROR_NONE) {
667                 media_svc_error("image_util_decode_set_output_buffer failed! (%d)", res);
668                 image_util_decode_destroy(decoder);
669                 return MS_MEDIA_ERR_INTERNAL;
670         }
671         res = image_util_decode_run(decoder, width, height, dst_size);
672         if (res != IMAGE_UTIL_ERROR_NONE) {
673                 media_svc_error("image_util_decode_run failed! (%d)", res);
674                 image_util_decode_destroy(decoder);
675                 return MS_MEDIA_ERR_INTERNAL;
676         }
677
678         res = image_util_decode_destroy(decoder);
679         if (res != IMAGE_UTIL_ERROR_NONE) {
680                 media_svc_error("image_util_decode_destroy failed! (%d)", res);
681                 return MS_MEDIA_ERR_INTERNAL;
682         }
683         return MS_MEDIA_ERR_NONE;
684 }
685
686 static int __media_svc_resize_artwork(unsigned char *image, unsigned int size, const char *img_format, unsigned char **resize_image, unsigned int *resize_size)
687 {
688         int ret = MS_MEDIA_ERR_NONE;
689         unsigned char *raw_image = NULL;
690         int width = 0;
691         int height = 0;
692         unsigned int raw_size = 0;
693         void *resized_raw_image = NULL;
694         int resized_width = 0;
695         int resized_height = 0;
696         unsigned int buf_size = 0;
697         image_util_colorspace_e colorspace = IMAGE_UTIL_COLORSPACE_RGB888;
698
699         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
700                 media_svc_debug("type [jpeg] size [%d]", size);
701                 /* decoding */
702                 ret = __media_svc_decode_jpeg(image, (unsigned long long)size, colorspace, &raw_image, (unsigned long *)&width, (unsigned long *)&height, (unsigned long long *)&raw_size);
703                 if (ret != MS_MEDIA_ERR_NONE) {
704                         media_svc_error("__media_svc_decode_jpeg failed");
705                         *resize_image = image;
706                         *resize_size = size;
707                         return MS_MEDIA_ERR_NONE;
708                 }
709
710                 if (raw_image == NULL) {
711                         media_svc_error("raw_image is null");
712                         *resize_image = image;
713                         *resize_size = size;
714                         return MS_MEDIA_ERR_NONE;
715                 }
716
717                 if (width <= MEDIA_SVC_ARTWORK_SIZE || height <= MEDIA_SVC_ARTWORK_SIZE) {
718                         media_svc_debug("No need resizing");
719                         *resize_image = image;
720                         *resize_size = size;
721                         SAFE_FREE(raw_image);
722                         return MS_MEDIA_ERR_NONE;
723                 }
724                 /* resizing */
725                 if (width > height) {
726                         resized_height = MEDIA_SVC_ARTWORK_SIZE;
727                         resized_width = width * MEDIA_SVC_ARTWORK_SIZE / height;
728                 } else {
729                         resized_width = MEDIA_SVC_ARTWORK_SIZE;
730                         resized_height = height * MEDIA_SVC_ARTWORK_SIZE / width;
731                 }
732
733                 image_util_calculate_buffer_size(resized_width, resized_height, colorspace, &buf_size);
734
735                 resized_raw_image = malloc(buf_size);
736
737                 if (resized_raw_image == NULL) {
738                         media_svc_error("malloc failed");
739                         *resize_image = image;
740                         *resize_size = size;
741                         SAFE_FREE(raw_image);
742                         return MS_MEDIA_ERR_NONE;
743                 }
744
745                 memset(resized_raw_image, 0, buf_size);
746
747                 ret = image_util_resize(resized_raw_image, &resized_width, &resized_height, raw_image, width, height, colorspace);
748                 if (ret != MS_MEDIA_ERR_NONE) {
749                         media_svc_error("image_util_resize failed");
750                         *resize_image = image;
751                         *resize_size = size;
752                         SAFE_FREE(raw_image);
753                         SAFE_FREE(resized_raw_image);
754                         return MS_MEDIA_ERR_NONE;
755                 }
756                 SAFE_FREE(raw_image);
757
758                 /* encoding */
759                 ret = __media_svc_encode_jpeg((unsigned char *)resized_raw_image, (unsigned long)resized_width, (unsigned long)resized_height, colorspace, 90, resize_image, (unsigned long long *)resize_size);
760                 if (ret != MS_MEDIA_ERR_NONE) {
761                         media_svc_error("__media_svc_encode_jpeg failed");
762                         *resize_image = image;
763                         *resize_size = size;
764                         SAFE_FREE(resized_raw_image);
765                         return MS_MEDIA_ERR_NONE;
766                 }
767                 SAFE_FREE(resized_raw_image);
768
769                 if (*resize_image == NULL) {
770                         media_svc_error("*resize_image is null");
771                         *resize_image = image;
772                         *resize_size = size;
773                         return MS_MEDIA_ERR_NONE;
774                 }
775         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
776                 media_svc_debug("type [png] size [%d]", size);
777                 *resize_image = image;
778                 *resize_size = size;
779
780         } else {
781                 media_svc_debug("Not proper img format");
782                 *resize_image = image;
783                 *resize_size = size;
784         }
785
786         return ret;
787 }
788
789 static int __media_svc_safe_atoi(char *buffer, int *si)
790 {
791         char *end;
792         errno = 0;
793         const long sl = strtol(buffer, &end, 10);
794
795         if (end == buffer) {
796                 media_svc_error("not a decimal number");
797                 return MS_MEDIA_ERR_INTERNAL;
798         } else if ('\0' != *end) {
799                 media_svc_error("extra characters at end of input: %s", end);
800                 return MS_MEDIA_ERR_INTERNAL;
801         } else if ((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno)) {
802                 media_svc_error("out of range of type long");
803                 return MS_MEDIA_ERR_INTERNAL;
804         } else if (sl > INT_MAX) {
805                 media_svc_error("greater than INT_MAX");
806                 return MS_MEDIA_ERR_INTERNAL;
807         } else if (sl < INT_MIN) {
808                 media_svc_error("less than INT_MIN");
809                 return MS_MEDIA_ERR_INTERNAL;
810         } else {
811                 *si = (int)sl;
812         }
813         return MS_MEDIA_ERR_NONE;
814 }
815
816 static int _media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
817 {
818         int ret = MS_MEDIA_ERR_NONE;
819
820         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
821
822         if (!image) {
823                 media_svc_error("invalid image..");
824                 return MS_MEDIA_ERR_INVALID_PARAMETER;
825         }
826
827         struct statfs fs;
828         char *thumb_path = NULL;
829         ret = ms_user_get_root_thumb_store_path(uid, &thumb_path);
830         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "ms_user_get_root_thumb_store_path fail");
831
832         if (-1 == statfs(thumb_path, &fs)) {
833                 media_svc_error("error in statfs");
834                 SAFE_FREE(thumb_path);
835                 return MS_MEDIA_ERR_INTERNAL;
836         }
837
838         SAFE_FREE(thumb_path);
839
840         long bsize_kbytes = fs.f_bsize >> 10;
841
842         if ((bsize_kbytes * fs.f_bavail) < 1024) {
843                 media_svc_error("not enought space...");
844                 return MS_MEDIA_ERR_NOT_ENOUGH_SPACE;
845         }
846
847         FILE *fp = NULL;
848         int nwrite = -1;
849         if (image != NULL && size > 0) {
850                 fp = fopen(image_path, "w");
851
852                 if (fp == NULL) {
853                         media_svc_error("failed to open file");
854                         return MS_MEDIA_ERR_INTERNAL;
855                 }
856
857                 nwrite = fwrite(image, 1, size, fp);
858                 if (nwrite != size) {
859                         media_svc_error("failed to write thumbnail");
860                         fclose(fp);
861                         return MS_MEDIA_ERR_INTERNAL;
862                 }
863                 fclose(fp);
864         }
865
866         return MS_MEDIA_ERR_NONE;
867 }
868
869 static char *_media_svc_get_title_from_filepath(const char *path)
870 {
871         char *filename = NULL;
872         char *title = NULL;
873         char    *ext = NULL;
874         int filename_len = -1;
875         int new_title_len = -1;
876
877         if (!path) {
878                 media_svc_error("path is NULL");
879                 return NULL;
880         }
881
882         filename = g_path_get_basename(path);
883         if (!STRING_VALID(filename)) {
884                 media_svc_error("wrong file name");
885                 SAFE_FREE(filename);
886                 return NULL;
887         }
888
889         filename_len = strlen(filename);
890
891         ext = g_strrstr(filename, ".");
892         if (!ext) {
893                 media_svc_error("there is no file extention");
894                 return filename;
895         }
896
897         new_title_len = filename_len - strlen(ext);
898         if (new_title_len < 1) {
899                 media_svc_error("title length is zero");
900                 SAFE_FREE(filename);
901                 return NULL;
902         }
903
904         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE - 1);
905
906         SAFE_FREE(filename);
907
908         media_svc_debug("extract title is [%s]", title);
909
910         return title;
911 }
912
913 int _media_svc_rename_file(const char *old_name, const char *new_name)
914 {
915         if ((old_name == NULL) || (new_name == NULL)) {
916                 media_svc_error("invalid file name");
917                 return MS_MEDIA_ERR_INVALID_PARAMETER;
918         }
919
920         if (rename(old_name, new_name) < 0) {
921                 media_svc_stderror(" ");
922                 return MS_MEDIA_ERR_INTERNAL;
923         }
924
925         return MS_MEDIA_ERR_NONE;
926 }
927
928 int _media_svc_remove_file(const char *path)
929 {
930         int result = -1;
931
932         result = remove(path);
933         if (result == 0) {
934                 media_svc_debug("success to remove file");
935                 return MS_MEDIA_ERR_NONE;
936         } else {
937                 media_svc_stderror("fail to remove file result");
938                 return MS_MEDIA_ERR_INTERNAL;
939         }
940 }
941
942 int _media_svc_remove_all_files_in_dir(const char *dir_path)
943 {
944         struct dirent entry;
945         struct dirent *result;
946         struct stat st;
947         char filename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
948         DIR *dir = NULL;
949
950         dir = opendir(dir_path);
951         if (dir == NULL) {
952                 media_svc_error("%s is not exist", dir_path);
953                 return MS_MEDIA_ERR_INVALID_PARAMETER;
954         }
955
956         while (!readdir_r(dir, &entry, &result)) {
957                 if (result == NULL)
958                         break;
959
960                 if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0)
961                         continue;
962
963                 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry.d_name);
964
965                 if (stat(filename, &st) != 0)
966                         continue;
967
968                 if (S_ISDIR(st.st_mode))
969                         continue;
970
971                 if (unlink(filename) != 0) {
972                         media_svc_stderror("failed to remove");
973                         closedir(dir);
974                         return MS_MEDIA_ERR_INTERNAL;
975                 }
976         }
977
978         closedir(dir);
979         return MS_MEDIA_ERR_NONE;
980 }
981
982 static int __media_svc_check_thumb_dir(const char *thumb_dir)
983 {
984         DIR *dir = NULL;
985
986         dir = opendir(thumb_dir);
987         if (dir != NULL)
988                 closedir(dir);
989         else
990                 return MS_MEDIA_ERR_INTERNAL;
991
992         return MS_MEDIA_ERR_NONE;
993 }
994
995 int _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
996 {
997         int ret = MS_MEDIA_ERR_NONE;
998         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
999         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
1000         char *thumb_dir = NULL;
1001         char hash[255 + 1] = {0, };
1002         char *thumbfile_ext = NULL;
1003         char *internal_thumb_path = NULL;
1004         char *external_thumb_path = NULL;
1005
1006         ret = ms_user_get_default_thumb_store_path(uid, &internal_thumb_path);
1007         ret = ms_user_get_mmc_thumb_store_path(uid, &external_thumb_path);
1008
1009         if (!STRING_VALID(internal_thumb_path) || !STRING_VALID(external_thumb_path)) {
1010                 media_svc_error("fail to get thumbnail path");
1011                 SAFE_FREE(internal_thumb_path);
1012                 SAFE_FREE(external_thumb_path);
1013                 return MS_MEDIA_ERR_INTERNAL;
1014         }
1015
1016         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? internal_thumb_path : external_thumb_path;
1017
1018         ret = __media_svc_check_thumb_dir(thumb_dir);
1019         if (ret != MS_MEDIA_ERR_NONE) {
1020                 media_svc_error("__media_svc_check_thumb_dir");
1021                 SAFE_FREE(internal_thumb_path);
1022                 SAFE_FREE(external_thumb_path);
1023                 return MS_MEDIA_ERR_INTERNAL;
1024         }
1025
1026         memset(file_ext, 0, sizeof(file_ext));
1027         if (!__media_svc_get_file_ext(pathname, file_ext))
1028                 media_svc_error("get file ext fail");
1029
1030         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
1031         if (ret != MS_MEDIA_ERR_NONE) {
1032                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
1033                 SAFE_FREE(internal_thumb_path);
1034                 SAFE_FREE(external_thumb_path);
1035                 return MS_MEDIA_ERR_INTERNAL;
1036         }
1037
1038         /*media_svc_debug("img format is [%s]", img_format); */
1039
1040         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
1041                 thumbfile_ext = (char *)"jpg";
1042         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
1043                 thumbfile_ext = (char *)"png";
1044         } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
1045                 thumbfile_ext = (char *)"gif";
1046         } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
1047                 thumbfile_ext = (char *)"bmp";
1048         } else {
1049                 media_svc_error("Not proper img format");
1050                 SAFE_FREE(internal_thumb_path);
1051                 SAFE_FREE(external_thumb_path);
1052                 return MS_MEDIA_ERR_INTERNAL;
1053         }
1054
1055         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
1056         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
1057         /*media_svc_debug("thumb_path is [%s]", thumb_path); */
1058
1059         SAFE_FREE(internal_thumb_path);
1060         SAFE_FREE(external_thumb_path);
1061
1062         return MS_MEDIA_ERR_NONE;
1063 }
1064
1065 int _media_svc_get_file_time(const char *full_path)
1066 {
1067         struct stat statbuf;
1068         int fd = 0;
1069
1070         memset(&statbuf, 0, sizeof(struct stat));
1071         fd = stat(full_path, &statbuf);
1072         if (fd == -1) {
1073                 media_svc_error("stat(%s) fails.", full_path);
1074                 return MS_MEDIA_ERR_INTERNAL;
1075         }
1076
1077         return statbuf.st_mtime;
1078 }
1079
1080 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
1081 {
1082         int ret = MS_MEDIA_ERR_NONE;
1083
1084         /* Set default GPS value before extracting meta information */
1085         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1086         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1087         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1088
1089         /* Set filename to title for all media */
1090         char *title = NULL;
1091         title = _media_svc_get_title_from_filepath(content_info->path);
1092         if (title) {
1093                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1094                 if (ret != MS_MEDIA_ERR_NONE)
1095                         media_svc_error("strcpy error");
1096                 SAFE_FREE(title);
1097         } else {
1098                 media_svc_error("Can't extract title");
1099                 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1100                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1101         }
1102
1103         /* Set default value before extracting meta information */
1104         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1105         media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1106
1107         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1108         media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1109
1110         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1111         media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1112
1113         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1114         media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1115
1116         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1117         media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1118
1119         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1120         media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1121
1122         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1123         media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1124
1125         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1126         media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1127
1128         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1129         media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1130
1131         if (refresh) {
1132                 media_svc_debug("refresh");
1133                 return MS_MEDIA_ERR_NONE;
1134         }
1135
1136         content_info->played_count = 0;
1137         content_info->last_played_time = 0;
1138         content_info->last_played_position = 0;
1139         content_info->favourate = 0;
1140         content_info->media_meta.rating = 0;
1141
1142         return MS_MEDIA_ERR_NONE;
1143 }
1144
1145 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1146                         const char *path, media_svc_media_type_e *media_type, bool refresh)
1147 {
1148         int ret = MS_MEDIA_ERR_NONE;
1149         char * media_uuid = NULL;
1150         char * file_name = NULL;
1151         bool drm_type = false;
1152         char mime_type[256] = {0, };
1153
1154         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
1155         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1156
1157         struct stat st;
1158         memset(&st, 0, sizeof(struct stat));
1159         if (stat(path, &st) == 0) {
1160                 content_info->modified_time = st.st_mtime;
1161                 content_info->timeline = content_info->modified_time;
1162                 content_info->size = st.st_size;
1163         } else {
1164                 media_svc_stderror("stat failed");
1165         }
1166
1167         _media_svc_set_default_value(content_info, refresh);
1168
1169         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1170         if (refresh) {
1171                 media_svc_debug("refresh");
1172                 return MS_MEDIA_ERR_NONE;
1173         }
1174
1175         ret = __media_svc_malloc_and_strncpy(&content_info->storage_uuid, storage_id);
1176         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1177
1178         content_info->storage_type = storage_type;
1179         time(&content_info->added_time);
1180
1181         media_uuid = _media_info_generate_uuid();
1182         if (media_uuid == NULL) {
1183                 _media_svc_destroy_content_info(content_info);
1184                 return MS_MEDIA_ERR_INTERNAL;
1185         }
1186
1187         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
1188         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1189
1190         file_name = g_path_get_basename(path);
1191         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
1192         SAFE_FREE(file_name);
1193         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1194
1195         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1196         if drm_contentinfo is not NULL, the file is OMA DRM.*/
1197         ret = __media_svc_get_mime_type(path, mime_type);
1198         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1199
1200         media_svc_debug("mime [%s]", mime_type);
1201         content_info->is_drm = drm_type;
1202
1203         ret = __media_svc_get_media_type(path, mime_type, media_type);
1204         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1205
1206         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1207                 media_svc_error("invalid media_type condition[%d]", *media_type);
1208                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1209         }
1210
1211         ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
1212         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1213
1214         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1215
1216         content_info->media_type = *media_type;
1217
1218         return MS_MEDIA_ERR_NONE;
1219 }
1220
1221 int image_360_check(char *path)
1222 {
1223         FILE *fp = NULL;
1224         long app1_size = 0;
1225         int size = 1;
1226         unsigned char exif_header[4];
1227         unsigned char exif_app1[2];
1228         unsigned char exif_app1_xmp[2];
1229         long exif_app1_xmp_size = 0;
1230         unsigned char exif_app1_xmp_t[2];
1231         char *xmp_data = NULL;
1232         int size1 = 0;
1233         int size2 = 0;
1234         int fdata = 0;
1235         int temp = 0;
1236
1237         fp = fopen(path, "rb");
1238         if (fp == NULL)
1239                 goto ERROR;
1240
1241         size = fread(exif_header, 1, sizeof(exif_header), fp);
1242         if (size <= 0)
1243                 goto ERROR;
1244
1245         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1246                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
1247                 if (size <= 0)
1248                         goto ERROR;
1249
1250                 size1 = exif_app1[0];
1251                 size2 = exif_app1[1];
1252
1253                 app1_size = size1 * 256 + size2 - 2;
1254
1255                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1256                         goto ERROR;
1257
1258                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
1259                 if (size <= 0)
1260                         goto ERROR;
1261
1262                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1263                         int result = 0;
1264                         char *ptr = NULL;
1265                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
1266                         if (size <= 0)
1267                                 goto ERROR;
1268
1269                         size1 = exif_app1_xmp_t[0];
1270                         size2 = exif_app1_xmp_t[1];
1271
1272                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
1273
1274                         xmp_data = (char *)malloc(exif_app1_xmp_size);
1275                         if (xmp_data != NULL) {
1276                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
1277                                 ptr = xmp_data;
1278
1279                                 while (exif_app1_xmp_size >= 0) {
1280                                         exif_app1_xmp_size--;
1281                                         fdata = fgetc(fp);
1282                                         if (fdata == EOF)
1283                                                 continue;
1284                                         if (fdata == '\0')
1285                                                 continue;
1286                                         *ptr = (char)fdata;
1287                                         ptr++;
1288                                         temp++;
1289                                 }
1290                                 ptr = ptr - temp;
1291
1292                                 if (strstr(ptr, "UsePanoramaViewer")
1293                                 && strstr(ptr, "True")
1294                                 && strstr(ptr, "ProjectionType")
1295                                 && strstr(ptr, "equirectangular"))
1296                                         result = 1;
1297
1298                                 SAFE_FREE(xmp_data);
1299                         } else {
1300                                 media_svc_error("malloc failed");
1301                         }
1302
1303                         if (fp) {
1304                                 fclose(fp);
1305                                 fp = NULL;
1306                         }
1307                         return result;
1308                 } else {
1309                         goto ERROR;
1310                 }
1311         } else {
1312                 goto ERROR;
1313         }
1314 ERROR:
1315         if (fp) {
1316                 fclose(fp);
1317                 fp = NULL;
1318         }
1319         return 0;
1320 }
1321
1322 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1323 {
1324         int ret = MS_MEDIA_ERR_NONE;
1325         double value = 0.0;
1326         int orient_value = 0;
1327         int exif_width = 0;
1328         int exif_height = 0;
1329         ExifData *ed = NULL;
1330         int has_datetaken = FALSE;
1331         int datetaken_size = 19;
1332         double fnumber = 0.0;
1333         int iso = 0;
1334         char *path = NULL;
1335
1336         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1337         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
1338         char exposure_time_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1339         char model_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1340
1341         memset(buf, 0x00, sizeof(buf));
1342         memset(description_buf, 0x00, sizeof(description_buf));
1343         memset(exposure_time_buf, 0x00, sizeof(exposure_time_buf));
1344         memset(model_buf, 0x00, sizeof(model_buf));
1345
1346         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1347                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1348                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1349         }
1350
1351         path = content_info->path;
1352         if (!STRING_VALID(path)) {
1353                 media_svc_error("Invalid Path");
1354                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1355         }
1356
1357         /* Load an ExifData object from an EXIF file */
1358         ed = exif_data_new_from_file(path);
1359
1360         if (!ed) {
1361                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1362                 goto GET_WIDTH_HEIGHT;
1363         }
1364
1365         content_info->media_meta.is_360 = image_360_check(path);
1366
1367         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1368                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1369                         if (strlen(buf) > 0) {
1370                                 if (strcmp(buf, "S") == 0)
1371                                         value = -1 * value;
1372                         }
1373                         content_info->media_meta.latitude = value;
1374                 } else {
1375                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1376                 }
1377         } else {
1378                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1379         }
1380
1381         memset(buf, 0x00, sizeof(buf));
1382
1383         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1384                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1385                         if (strlen(buf) > 0) {
1386                                 if (strcmp(buf, "W") == 0)
1387                                         value = -1 * value;
1388                         }
1389                         content_info->media_meta.longitude = value;
1390                 } else {
1391                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1392                 }
1393         } else {
1394                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1395         }
1396
1397         memset(buf, 0x00, sizeof(buf));
1398
1399         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1400                 if (strlen(description_buf) == 0) {
1401                         /*media_svc_debug("Use 'No description'"); */
1402                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1403                         if (ret != MS_MEDIA_ERR_NONE)
1404                                 media_svc_error("strcpy error");
1405                 } else {
1406                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
1407                         if (ret != MS_MEDIA_ERR_NONE)
1408                                 media_svc_error("strcpy error");
1409                 }
1410         } else {
1411                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
1412                 if (ret != MS_MEDIA_ERR_NONE)
1413                         media_svc_error("strcpy error");
1414         }
1415
1416         memset(buf, 0x00, sizeof(buf));
1417
1418         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1419                 if (strlen(buf) == 0) {
1420                         /*media_svc_debug("time is NULL"); */
1421                 } else {
1422                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1423                         if (ret != MS_MEDIA_ERR_NONE) {
1424                                 media_svc_error("strcpy error");
1425                         } else {
1426                                 has_datetaken = TRUE;
1427                                 /* This is same as recorded_date */
1428                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1429                                 if (ret != MS_MEDIA_ERR_NONE)
1430                                         media_svc_error("strcpy error");
1431                         }
1432                 }
1433         }
1434
1435         memset(buf, 0x00, sizeof(buf));
1436
1437         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1438                 if (strlen(buf) == 0) {
1439                         /*media_svc_debug("time is NULL"); */
1440                 } else {
1441                         ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, buf, datetaken_size);
1442                         if (ret != MS_MEDIA_ERR_NONE) {
1443                                 media_svc_error("strcpy error");
1444                         } else {
1445                                 has_datetaken = TRUE;
1446                                 /* This is same as recorded_date */
1447                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
1448                                 if (ret != MS_MEDIA_ERR_NONE)
1449                                         media_svc_error("strcpy error");
1450                         }
1451                 }
1452         }
1453
1454         if (has_datetaken) {
1455                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1456                 if (content_info->timeline == 0)
1457                         content_info->timeline = content_info->modified_time;
1458                 else
1459                         media_svc_debug("Timeline : %ld", content_info->timeline);
1460         }
1461
1462         /* Get exposure_time value from exif. */
1463         if (__media_svc_get_exif_info(ed, exposure_time_buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1464                 if (strlen(exposure_time_buf) == 0) {
1465                         /* media_svc_debug("exposure_time_buf is NULL"); */
1466                 } else {
1467                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.exposure_time, exposure_time_buf);
1468                         if (ret != MS_MEDIA_ERR_NONE)
1469                                 media_svc_error("strcpy error");
1470                 }
1471         }
1472
1473         /* Get fnumber value from exif. */
1474         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1475                 if (fnumber > 0.0)
1476                         content_info->media_meta.fnumber = fnumber;
1477                 else
1478                         content_info->media_meta.fnumber = 0.0;
1479         } else {
1480                 content_info->media_meta.fnumber = 0.0;
1481         }
1482
1483         /* Get iso value from exif. */
1484         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1485                 if (iso > 0)
1486                         content_info->media_meta.iso = iso;
1487                 else
1488                         content_info->media_meta.iso = 0;
1489         } else {
1490                 content_info->media_meta.iso = 0;
1491         }
1492
1493         /* Get model value from exif. */
1494         if (__media_svc_get_exif_info(ed, model_buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1495                 if (strlen(model_buf) == 0) {
1496                         /* media_svc_debug("model_buf is NULL"); */
1497                 } else {
1498                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.model, model_buf);
1499                         if (ret != MS_MEDIA_ERR_NONE)
1500                                 media_svc_error("strcpy error");
1501                 }
1502         }
1503
1504         /* Get orientation value from exif. */
1505         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1506                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1507                         content_info->media_meta.orientation = orient_value;
1508                 else
1509                         content_info->media_meta.orientation = 0;
1510         } else {
1511                 content_info->media_meta.orientation = 0;
1512         }
1513
1514         /* Get width value from exif. */
1515         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1516                 if (exif_width > 0)
1517                         content_info->media_meta.width = exif_width;
1518                 else
1519                         content_info->media_meta.width = 0;
1520         } else {
1521                 content_info->media_meta.width = 0;
1522         }
1523
1524         /* Get height value from exif. */
1525         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1526                 if (exif_height > 0)
1527                         content_info->media_meta.height = exif_height;
1528                 else
1529                         content_info->media_meta.height = 0;
1530         } else {
1531                 content_info->media_meta.height = 0;
1532         }
1533
1534         if (ed != NULL) exif_data_unref(ed);
1535
1536 GET_WIDTH_HEIGHT:
1537
1538         if (content_info->media_meta.width == 0 ||
1539                 content_info->media_meta.height == 0) {
1540                 /*Get image width, height*/
1541                 unsigned int img_width = 0;
1542                 unsigned int img_height = 0;
1543                 ImgCodecType img_type = IMG_CODEC_NONE;
1544
1545                 ret = ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1546
1547                 if (content_info->media_meta.width == 0)
1548                         content_info->media_meta.width = img_width;
1549
1550                 if (content_info->media_meta.height == 0)
1551                         content_info->media_meta.height = img_height;
1552         }
1553
1554         return MS_MEDIA_ERR_NONE;
1555 }
1556
1557 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1558 {
1559         MMHandleType tag = 0;
1560         char *p = NULL;
1561         int size = -1;
1562         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1563         int mmf_error = FILEINFO_ERROR_NONE;
1564         char *err_attr_name = NULL;
1565         char *title = NULL;
1566         int album_id = 0;
1567         int ret = MS_MEDIA_ERR_NONE;
1568
1569         /*Get Content Tag attribute ===========*/
1570         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1571
1572         if (mmf_error == FILEINFO_ERROR_NONE) {
1573                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1574                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1575                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1576                         if (ret != MS_MEDIA_ERR_NONE)
1577                                 media_svc_error("strcpy error");
1578
1579                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1580                 } else {
1581                         SAFE_FREE(err_attr_name);
1582                         /*media_svc_debug("album - unknown"); */
1583                 }
1584
1585                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1586                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1587                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1588                         if (ret != MS_MEDIA_ERR_NONE)
1589                                 media_svc_error("strcpy error");
1590                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1591                 } else {
1592                         SAFE_FREE(err_attr_name);
1593                         /*media_svc_debug("artist - unknown"); */
1594                 }
1595
1596                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1597                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1598                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1599                         if (ret != MS_MEDIA_ERR_NONE)
1600                                 media_svc_error("strcpy error");
1601                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1602                 } else {
1603                         SAFE_FREE(err_attr_name);
1604                         /*media_svc_debug("album_artist - unknown"); */
1605                 }
1606
1607                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1608                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1609                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1610                         if (ret != MS_MEDIA_ERR_NONE)
1611                                 media_svc_error("strcpy error");
1612
1613                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1614                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1615                         /*
1616                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1617                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1618                         }
1619                         */
1620                 } else {
1621                         SAFE_FREE(err_attr_name);
1622                         /*media_svc_debug("genre - unknown"); */
1623                 }
1624
1625                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1626                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1627                         if (!isspace(*p)) {
1628                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1629                                 if (ret != MS_MEDIA_ERR_NONE)
1630                                         media_svc_error("strcpy error");
1631
1632                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1633                         } else {
1634                                 int idx = 0;
1635
1636                                 for (idx = 0; idx < size; idx++) {
1637                                         if (isspace(*p)) {
1638                                                 media_svc_debug("SPACE [%s]", p);
1639                                                 p++;
1640                                                 continue;
1641                                         } else {
1642                                                 media_svc_debug("Not SPACE [%s]", p);
1643                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1644                                                 if (ret != MS_MEDIA_ERR_NONE)
1645                                                         media_svc_error("strcpy error");
1646                                                 break;
1647                                         }
1648                                 }
1649
1650                                 if (idx == size) {
1651                                         media_svc_debug("Can't extract title. All string is space");
1652                                         title = _media_svc_get_title_from_filepath(content_info->path);
1653                                         if (title) {
1654                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1655                                                 if (ret != MS_MEDIA_ERR_NONE)
1656                                                         media_svc_error("strcpy error");
1657                                                 SAFE_FREE(title);
1658                                         } else {
1659                                                 media_svc_error("Can't extract title");
1660                                         }
1661                                 }
1662                         }
1663                 } else {
1664                         SAFE_FREE(err_attr_name);
1665                         title = _media_svc_get_title_from_filepath(content_info->path);
1666                         if (title) {
1667                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1668                                 if (ret != MS_MEDIA_ERR_NONE)
1669                                         media_svc_error("strcpy error");
1670                                 SAFE_FREE(title);
1671                         } else {
1672                                 media_svc_error("Can't extract title");
1673                         }
1674                 }
1675
1676                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1677                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1678                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1679                         if (ret != MS_MEDIA_ERR_NONE)
1680                                 media_svc_error("strcpy error");
1681                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1682                 } else {
1683                         SAFE_FREE(err_attr_name);
1684                 }
1685
1686                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1687                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1688                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1689                         if (ret != MS_MEDIA_ERR_NONE)
1690                                 media_svc_error("strcpy error");
1691                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1692                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1693                 } else {
1694                         /*media_svc_debug("composer - unknown"); */
1695                         SAFE_FREE(err_attr_name);
1696                 }
1697
1698                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1699                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1700                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1701                         if (ret != MS_MEDIA_ERR_NONE)
1702                                 media_svc_error("strcpy error");
1703                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1704                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1705                 } else {
1706                         /*media_svc_debug("copyright - unknown"); */
1707                         SAFE_FREE(err_attr_name);
1708                 }
1709
1710                 mmf_error = mm_file_destroy_tag_attrs(tag);
1711                 if (mmf_error != FILEINFO_ERROR_NONE)
1712                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1713         } else {
1714                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1715                 char *no_tag_title = NULL;
1716                 media_svc_error("no tag information");
1717
1718                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
1719                 if (no_tag_title) {
1720                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
1721                         if (ret != MS_MEDIA_ERR_NONE)
1722                                 media_svc_error("strcpy error");
1723                         SAFE_FREE(no_tag_title);
1724                 } else {
1725                         media_svc_error("Can't extract title");
1726                 }
1727
1728                 content_info->album_id = album_id;
1729         }
1730
1731         return MS_MEDIA_ERR_NONE;
1732 }
1733
1734 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1735 {
1736         MMHandleType content = 0;
1737         MMHandleType tag = 0;
1738         char *p = NULL;
1739         unsigned char *image = NULL;
1740         unsigned int size = 0;
1741         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
1742         int mmf_error = FILEINFO_ERROR_NONE;
1743         char *err_attr_name = NULL;
1744         char *title = NULL;
1745         bool extract_thumbnail = FALSE;
1746         bool append_album = FALSE;
1747         int album_id = 0;
1748         int ret = MS_MEDIA_ERR_NONE;
1749         int cdis_value = 0;
1750         unsigned int resize_size = 0;
1751         unsigned char *resize_image = NULL;
1752
1753         /*Get Content Tag attribute ===========*/
1754         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1755
1756         if (mmf_error == FILEINFO_ERROR_NONE) {
1757                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1758                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1759                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1760                         if (ret != MS_MEDIA_ERR_NONE)
1761                                 media_svc_error("strcpy error");
1762
1763                         /*media_svc_debug("album[%d] : %s", size, content_info->media_meta.album); */
1764                 } else {
1765                         SAFE_FREE(err_attr_name);
1766                         /*media_svc_debug("album - unknown"); */
1767                 }
1768
1769                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1770                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1771                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1772                         if (ret != MS_MEDIA_ERR_NONE)
1773                                 media_svc_error("strcpy error");
1774                         /*media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist); */
1775                 } else {
1776                         SAFE_FREE(err_attr_name);
1777                         /*media_svc_debug("artist - unknown"); */
1778                 }
1779
1780                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1781                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM_ARTIST)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1782                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, p);
1783                         if (ret != MS_MEDIA_ERR_NONE)
1784                                 media_svc_error("strcpy error");
1785                         /*media_svc_debug("album_artist[%d] : %s", size, content_info->media_meta.album_artist); */
1786                 } else {
1787                         SAFE_FREE(err_attr_name);
1788                         /*media_svc_debug("album_artist - unknown"); */
1789                 }
1790
1791                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1792                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1793                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1794                         if (ret != MS_MEDIA_ERR_NONE)
1795                                 media_svc_error("strcpy error");
1796
1797                         /*media_svc_debug("genre : %s", content_info->media_meta.genre); */
1798                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1799                         /*
1800                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1801                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1802                         }
1803                         */
1804                 } else {
1805                         SAFE_FREE(err_attr_name);
1806                         /*media_svc_debug("genre - unknown"); */
1807                 }
1808
1809                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1810                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1811                         if (!isspace(*p)) {
1812                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1813                                 if (ret != MS_MEDIA_ERR_NONE)
1814                                         media_svc_error("strcpy error");
1815
1816                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1817                         } else {
1818                                 int idx = 0;
1819
1820                                 for (idx = 0; idx < size; idx++) {
1821                                         if (isspace(*p)) {
1822                                                 media_svc_debug("SPACE [%s]", p);
1823                                                 p++;
1824                                                 continue;
1825                                         } else {
1826                                                 media_svc_debug("Not SPACE [%s]", p);
1827                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1828                                                 if (ret != MS_MEDIA_ERR_NONE)
1829                                                         media_svc_error("strcpy error");
1830                                                 break;
1831                                         }
1832                                 }
1833
1834                                 if (idx == size) {
1835                                         media_svc_debug("Can't extract title. All string is space");
1836                                         title = _media_svc_get_title_from_filepath(content_info->path);
1837                                         if (title) {
1838                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1839                                                 if (ret != MS_MEDIA_ERR_NONE)
1840                                                         media_svc_error("strcpy error");
1841                                                 SAFE_FREE(title);
1842                                         } else {
1843                                                 media_svc_error("Can't extract title");
1844                                         }
1845                                 }
1846                         }
1847                 } else {
1848                         SAFE_FREE(err_attr_name);
1849                         title = _media_svc_get_title_from_filepath(content_info->path);
1850                         if (title) {
1851                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1852                                 if (ret != MS_MEDIA_ERR_NONE)
1853                                         media_svc_error("strcpy error");
1854                                 SAFE_FREE(title);
1855                         } else {
1856                                 media_svc_error("Can't extract title");
1857                         }
1858                 }
1859
1860                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1861                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1862                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1863                         if (ret != MS_MEDIA_ERR_NONE)
1864                                 media_svc_error("strcpy error");
1865                         /*media_svc_debug("desc : %s", content_info->media_meta.description); */
1866                 } else {
1867                         SAFE_FREE(err_attr_name);
1868                 }
1869
1870                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_360, &content_info->media_meta.is_360, NULL);
1871
1872                 if (mmf_error != FILEINFO_ERROR_NONE)
1873                         SAFE_FREE(err_attr_name);
1874
1875                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1876                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1877                         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1878                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1879                                 char *time_info = (char*)calloc(1, (size + 1));
1880                                 char *p_value = p;
1881                                 char *time_value = time_info;
1882                                 if (time_info != NULL) {
1883                                         while (*p_value != '\0') {
1884                                                 if (*p_value == '-')
1885                                                         *time_value = ':';
1886                                                 else
1887                                                         *time_value = *p_value;
1888                                                 time_value++;
1889                                                 p_value++;
1890                                         }
1891                                         *time_value = '\0';
1892                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, time_info);
1893                                         SAFE_FREE(time_info);
1894                                 } else {
1895                                         media_svc_error("memory allocation error");
1896                                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
1897                                 }
1898                         } else {
1899                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1900                         }
1901
1902                         if (ret != MS_MEDIA_ERR_NONE) {
1903                                 media_svc_error("strcpy error");
1904                         } else {
1905                                 /* This is same as datetaken */
1906 #if 0
1907                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date);
1908 #else
1909                                 int datetaken_size = 19;
1910                                 ret = __media_svc_malloc_and_strncpy_with_size(&content_info->media_meta.datetaken, content_info->media_meta.recorded_date, datetaken_size);
1911 #endif
1912                                 if (ret != MS_MEDIA_ERR_NONE)
1913                                         media_svc_error("strcpy error");
1914
1915                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1916                                 if (content_info->timeline == 0)
1917                                         content_info->timeline = content_info->modified_time;
1918                                 else
1919                                         media_svc_debug("Timeline : %ld", content_info->timeline);
1920                         }
1921                         /*media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date); */
1922                 } else {
1923                         SAFE_FREE(err_attr_name);
1924                 }
1925
1926                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1927                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1928                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1929                         if (ret != MS_MEDIA_ERR_NONE)
1930                                 media_svc_error("strcpy error");
1931                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1932                         /*media_svc_debug("extract composer from content : %s", content_info->media_meta.composer); */
1933                 } else {
1934                         /*media_svc_debug("composer - unknown"); */
1935                         SAFE_FREE(err_attr_name);
1936                 }
1937
1938                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1939                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1940                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1941                         if (ret != MS_MEDIA_ERR_NONE)
1942                                 media_svc_error("strcpy error");
1943                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1944                         /*media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright); */
1945                 } else {
1946                         /*media_svc_debug("copyright - unknown"); */
1947                         SAFE_FREE(err_attr_name);
1948                 }
1949
1950                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1951                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1952                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1953                         if (ret != MS_MEDIA_ERR_NONE)
1954                                 media_svc_error("strcpy error");
1955                 } else {
1956                         SAFE_FREE(err_attr_name);
1957                 }
1958
1959                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1960                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == FILEINFO_ERROR_NONE) && (size == 4)) {
1961                         int year = 0;
1962                         if ((p != NULL) && ((ret != __media_svc_safe_atoi(p, &year)) == MS_MEDIA_ERR_NONE)) {
1963                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1964                                 if (ret != MS_MEDIA_ERR_NONE)
1965                                         media_svc_error("strcpy error");
1966                         } else {
1967                                 media_svc_debug("Wrong Year Information [%s]", p);
1968                         }
1969                 } else {
1970                         SAFE_FREE(err_attr_name);
1971                 }
1972
1973                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1974                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1975                         content_info->media_meta.rating = atoi(p);
1976                 } else {
1977                         SAFE_FREE(err_attr_name);
1978                         content_info->media_meta.rating = 0;
1979                 }
1980
1981                 /*Initialize album_id to 0. below code will set the album_id*/
1982                 content_info->album_id = album_id;
1983                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1984
1985                 if (ret != MS_MEDIA_ERR_NONE) {
1986                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1987                                 media_svc_debug("album does not exist. So start to make album art");
1988                                 extract_thumbnail = TRUE;
1989                                 append_album = TRUE;
1990                         } else {
1991                                 extract_thumbnail = TRUE;
1992                                 append_album = FALSE;
1993                         }
1994                 } else {
1995                         content_info->album_id = album_id;
1996                         append_album = FALSE;
1997
1998                         if ((!g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1999                                 (!g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
2000                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
2001                                 extract_thumbnail = TRUE;
2002                         } else {
2003                                 media_svc_debug("album already exists. don't need to make album art");
2004                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
2005                                 extract_thumbnail = TRUE;
2006                         }
2007                 }
2008
2009                 /*Do not extract artwork for the USB Storage content*/
2010                 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
2011                         extract_thumbnail = FALSE;
2012
2013                 if (extract_thumbnail == TRUE) {
2014                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
2015                         if (mmf_error != FILEINFO_ERROR_NONE) {
2016                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
2017                                 SAFE_FREE(err_attr_name);
2018                         } else {
2019                                 /*media_svc_debug("artwork size1 [%d]", size); */
2020                         }
2021
2022                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
2023                         if (mmf_error != FILEINFO_ERROR_NONE) {
2024                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
2025                                 SAFE_FREE(err_attr_name);
2026                         } else {
2027                                 /*media_svc_debug("artwork size2 [%d]", size); */
2028                         }
2029                         if (image != NULL && size > 0) {
2030                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
2031                                 int artwork_mime_size = -1;
2032
2033                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
2034                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
2035                                         ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
2036                                         if (ret != MS_MEDIA_ERR_NONE)
2037                                                 media_svc_error("Fail to Get Thumbnail Path");
2038                                         /* albumart resizing */
2039                                         __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
2040                                 } else {
2041                                         SAFE_FREE(err_attr_name);
2042                                 }
2043
2044                                 if (strlen(thumb_path) > 0) {
2045                                         ret = _media_svc_save_image(resize_image, resize_size, thumb_path, uid);
2046                                         if (ret != MS_MEDIA_ERR_NONE) {
2047                                                 media_svc_error("Fail to Save Thumbnail Image");
2048                                         } else {
2049                                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
2050                                                 if (ret != MS_MEDIA_ERR_NONE)
2051                                                         media_svc_error("strcpy error");
2052                                         }
2053                                 }
2054                         }
2055                 }
2056
2057                 if (append_album == TRUE) {
2058                         if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
2059                                 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
2060                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
2061                         else
2062                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
2063
2064                         content_info->album_id = album_id;
2065                 }
2066
2067                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
2068                         double longitude = 0.0;
2069                         double latitude = 0.0;
2070                         double altitude = 0.0;
2071
2072                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
2073                         content_info->media_meta.longitude = longitude;
2074                         content_info->media_meta.latitude = latitude;
2075                         content_info->media_meta.altitude = altitude;
2076
2077                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
2078                         if (mmf_error != FILEINFO_ERROR_NONE) {
2079                                 cdis_value = 0;
2080                                 SAFE_FREE(err_attr_name);
2081                         }
2082
2083                         media_svc_debug("CDIS : %d", cdis_value);
2084
2085                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
2086                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
2087                                 content_info->media_meta.orientation = atoi(p);
2088                         } else {
2089                                 SAFE_FREE(err_attr_name);
2090                                 content_info->media_meta.orientation = 0;
2091                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
2092                         }
2093                 }
2094
2095                 mmf_error = mm_file_destroy_tag_attrs(tag);
2096                 if (mmf_error != FILEINFO_ERROR_NONE)
2097                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
2098         }       else {
2099                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
2100                 char *no_tag_title = NULL;
2101                 media_svc_error("no tag information");
2102
2103                 no_tag_title = _media_svc_get_title_from_filepath(content_info->path);
2104                 if (no_tag_title) {
2105                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, no_tag_title);
2106                         if (ret != MS_MEDIA_ERR_NONE)
2107                                 media_svc_error("strcpy error");
2108                         SAFE_FREE(no_tag_title);
2109                 } else {
2110                         media_svc_error("Can't extract title");
2111                 }
2112
2113                 content_info->album_id = album_id;
2114         }
2115
2116         /*Get Content attribute ===========*/
2117         if (cdis_value == 1)
2118                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
2119         else
2120                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
2121
2122         if (mmf_error == FILEINFO_ERROR_NONE) {
2123                 /*Common attribute*/
2124                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
2125                 if (mmf_error != FILEINFO_ERROR_NONE) {
2126                         SAFE_FREE(err_attr_name);
2127                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
2128                 } else {
2129                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
2130                 }
2131
2132                 /*Sound/Music attribute*/
2133                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
2134
2135                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
2136                         if (mmf_error != FILEINFO_ERROR_NONE) {
2137                                 SAFE_FREE(err_attr_name);
2138                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2139                         } else {
2140                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
2141                         }
2142
2143                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
2144                         if (mmf_error != FILEINFO_ERROR_NONE) {
2145                                 SAFE_FREE(err_attr_name);
2146                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
2147                         } else {
2148                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
2149                         }
2150
2151                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
2152                         if (mmf_error != FILEINFO_ERROR_NONE) {
2153                                 SAFE_FREE(err_attr_name);
2154                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
2155                         } else {
2156                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
2157                         }
2158
2159                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
2160                         if (mmf_error != FILEINFO_ERROR_NONE) {
2161                                 SAFE_FREE(err_attr_name);
2162                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
2163                         } else {
2164                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
2165                         }
2166                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
2167                         int audio_bitrate = 0;
2168                         int video_bitrate = 0;
2169
2170                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
2171                         if (mmf_error != FILEINFO_ERROR_NONE) {
2172                                 SAFE_FREE(err_attr_name);
2173                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2174                         } else {
2175                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
2176                         }
2177
2178                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
2179                         if (mmf_error != FILEINFO_ERROR_NONE) {
2180                                 SAFE_FREE(err_attr_name);
2181                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
2182                         } else {
2183                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
2184                         }
2185
2186                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
2187
2188                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
2189                         if (mmf_error != FILEINFO_ERROR_NONE) {
2190                                 SAFE_FREE(err_attr_name);
2191                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
2192                         } else {
2193                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
2194                         }
2195
2196                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
2197                         if (mmf_error != FILEINFO_ERROR_NONE) {
2198                                 SAFE_FREE(err_attr_name);
2199                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
2200                         } else {
2201                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
2202                         }
2203                 } else {
2204                         media_svc_error("Not support type");
2205                         mmf_error = mm_file_destroy_content_attrs(content);
2206                         if (mmf_error != FILEINFO_ERROR_NONE)
2207                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
2208
2209                         return MS_MEDIA_ERR_INVALID_PARAMETER;
2210                 }
2211
2212                 mmf_error = mm_file_destroy_content_attrs(content);
2213                 if (mmf_error != FILEINFO_ERROR_NONE)
2214                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
2215         } else {
2216                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
2217         }
2218
2219         return MS_MEDIA_ERR_NONE;
2220 }
2221
2222 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
2223 {
2224         media_svc_retm_if(content_info == NULL, "content info is NULL");
2225
2226         /* Delete media_svc_content_info_s */
2227         SAFE_FREE(content_info->media_uuid);
2228         SAFE_FREE(content_info->path);
2229         SAFE_FREE(content_info->file_name);
2230         SAFE_FREE(content_info->mime_type);
2231         SAFE_FREE(content_info->folder_uuid);
2232         SAFE_FREE(content_info->thumbnail_path);
2233         SAFE_FREE(content_info->storage_uuid);
2234
2235         /* Delete media_svc_content_meta_s */
2236         SAFE_FREE(content_info->media_meta.title);
2237         SAFE_FREE(content_info->media_meta.album);
2238         SAFE_FREE(content_info->media_meta.artist);
2239         SAFE_FREE(content_info->media_meta.album_artist);
2240         SAFE_FREE(content_info->media_meta.genre);
2241         SAFE_FREE(content_info->media_meta.composer);
2242         SAFE_FREE(content_info->media_meta.year);
2243         SAFE_FREE(content_info->media_meta.recorded_date);
2244         SAFE_FREE(content_info->media_meta.copyright);
2245         SAFE_FREE(content_info->media_meta.track_num);
2246         SAFE_FREE(content_info->media_meta.description);
2247         SAFE_FREE(content_info->media_meta.datetaken);
2248         SAFE_FREE(content_info->media_meta.exposure_time);
2249         SAFE_FREE(content_info->media_meta.model);
2250         SAFE_FREE(content_info->media_meta.weather);
2251         SAFE_FREE(content_info->media_meta.category);
2252         SAFE_FREE(content_info->media_meta.keyword);
2253         SAFE_FREE(content_info->media_meta.location_tag);
2254         SAFE_FREE(content_info->media_meta.content_name);
2255         SAFE_FREE(content_info->media_meta.age_rating);
2256         SAFE_FREE(content_info->media_meta.author);
2257         SAFE_FREE(content_info->media_meta.provider);
2258
2259         SAFE_FREE(content_info->media_meta.title_pinyin);
2260         SAFE_FREE(content_info->media_meta.album_pinyin);
2261         SAFE_FREE(content_info->media_meta.artist_pinyin);
2262         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
2263         SAFE_FREE(content_info->media_meta.genre_pinyin);
2264         SAFE_FREE(content_info->media_meta.composer_pinyin);
2265         SAFE_FREE(content_info->media_meta.copyright_pinyin);
2266         SAFE_FREE(content_info->media_meta.description_pinyin);
2267
2268         return;
2269 }
2270
2271 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
2272 {
2273         char result[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
2274         memset(result, 0x00, sizeof(result));
2275
2276         if (STRING_VALID(s) && STRING_VALID(olds) && STRING_VALID(news)) {
2277                 if (strncmp(s, olds, strlen(olds)) == 0) {
2278                         snprintf(result, sizeof(result), "%s%s", news, s + strlen(olds));
2279                 }
2280         }
2281
2282         if (STRING_VALID(result))
2283                 return g_strdup(result);
2284         else
2285                 return NULL;
2286
2287 }
2288
2289 bool _media_svc_is_drm_file(const char *path)
2290 {
2291         return FALSE;
2292 }
2293
2294 int _media_svc_request_thumbnail_with_origin_size(const char *path, char *thumb_path, int max_length, int *origin_width, int *origin_height, uid_t uid)
2295 {
2296         int ret = MS_MEDIA_ERR_NONE;
2297
2298         ret = thumbnail_request_from_db_with_size(path, thumb_path, max_length, origin_width, origin_height, uid);
2299
2300         if (ret != MS_MEDIA_ERR_NONE) {
2301                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
2302                 ret = MS_MEDIA_ERR_INTERNAL;
2303         } else {
2304                 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
2305         }
2306
2307         return ret;
2308 }
2309
2310 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
2311 {
2312         int ret = MS_MEDIA_ERR_NONE;
2313         int size = 0;
2314         pinyin_name_s *pinyinname = NULL;
2315
2316         *pinyin_str = NULL;
2317
2318         if (!STRING_VALID(src_str)) {
2319                 media_svc_debug("String is invalid");
2320                 return ret;
2321         }
2322
2323         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
2324         if (ret == MS_MEDIA_ERR_NONE) {
2325                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
2326                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
2327                 else
2328                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
2329         }
2330
2331         _media_svc_pinyin_free(pinyinname, size);
2332
2333         return ret;
2334 }
2335
2336 bool _media_svc_check_pinyin_support(void)
2337 {
2338         /*Check CSC*/
2339         return TRUE;
2340 }
2341
2342 char* _media_svc_get_title_from_path(const char *path)
2343 {
2344         char *filename = NULL;
2345         char *title = NULL;
2346         char    *ext = NULL;
2347         int filename_len = -1;
2348         int new_title_len = -1;
2349
2350         if (!path) {
2351                 media_svc_error("path is NULL");
2352                 return NULL;
2353         }
2354
2355         filename = g_path_get_basename(path);
2356         if (!STRING_VALID(filename)) {
2357                 media_svc_error("wrong file name");
2358                 SAFE_FREE(filename);
2359                 return NULL;
2360         }
2361
2362         filename_len = strlen(filename);
2363
2364         ext = g_strrstr(filename, ".");
2365         if (!ext) {
2366                 media_svc_error("there is no file extention");
2367                 return filename;
2368         }
2369
2370         new_title_len = filename_len - strlen(ext);
2371         if (new_title_len < 1) {
2372                 media_svc_error("title length is zero");
2373                 SAFE_FREE(filename);
2374                 return NULL;
2375         }
2376
2377         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
2378
2379         SAFE_FREE(filename);
2380
2381         media_svc_debug("extract title is [%s]", title);
2382
2383         return title;
2384 }
2385
2386 #define BUF_LENGHT 256
2387
2388 void _media_svc_print_stderror(void)
2389 {
2390         char buf[BUF_LENGHT] = {0,};
2391
2392         media_svc_error("STANDARD ERROR [%s]", strerror_r(errno, buf, BUF_LENGHT));
2393 }
2394
2395 int _media_svc_get_media_type(const char *path, int *mediatype)
2396 {
2397         int ret = MS_MEDIA_ERR_NONE;
2398         char mime_type[256] = {0};
2399         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
2400
2401         ret = __media_svc_get_mime_type(path, mime_type);
2402         if (ret == MS_MEDIA_ERR_NONE) {
2403                 __media_svc_get_media_type(path, mime_type, &media_type);
2404         } else {
2405                 media_svc_error("__media_svc_get_mime_type failed");
2406         }
2407
2408         *mediatype = media_type;
2409
2410         return ret;
2411 }
2412