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