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