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