Merge "Fix playlist/tag bulk update" into tizen
[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                 }
871         } else {
872                 media_svc_error("%s is not exist", dir_path);
873                 return MS_MEDIA_ERR_INVALID_PARAMETER;
874         }
875
876         g_dir_close(dir);
877
878         return MS_MEDIA_ERR_NONE;
879 }
880
881 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)
882 {
883         int ret = MS_MEDIA_ERR_NONE;
884         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0, };
885         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0, };
886         char hash[255 + 1] = {0, };
887         char *thumbfile_ext = NULL;
888         char *thumb_dir = NULL;
889
890         ret = ms_user_get_thumb_store_path(uid, storage_type, &thumb_dir);
891         if (!STRING_VALID(thumb_dir)) {
892                 media_svc_error("ms_user_get_thumb_store_path failed");
893                 return MS_MEDIA_ERR_INTERNAL;
894         }
895
896         if (!g_file_test(thumb_dir, G_FILE_TEST_IS_DIR)) {
897                 media_svc_error("Wrong path[%s]", thumb_dir);
898                 SAFE_FREE(thumb_dir);
899                 return MS_MEDIA_ERR_INTERNAL;
900         }
901
902         memset(file_ext, 0, sizeof(file_ext));
903         if (!__media_svc_get_file_ext(pathname, file_ext))
904                 media_svc_error("get file ext fail");
905
906         ret = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
907         if (ret != MS_MEDIA_ERR_NONE) {
908                 media_svc_error("mb_svc_generate_hash_code failed : %d", ret);
909                 SAFE_FREE(thumb_dir);
910                 return MS_MEDIA_ERR_INTERNAL;
911         }
912
913         /*media_svc_debug("img format is [%s]", img_format); */
914
915         if ((strstr(img_format, "jpeg") != NULL) || (strstr(img_format, "jpg") != NULL) || (strstr(img_format, "JPG") != NULL)) {
916                 thumbfile_ext = (char *)"jpg";
917         } else if ((strstr(img_format, "png") != NULL) || (strstr(img_format, "PNG") != NULL)) {
918                 thumbfile_ext = (char *)"png";
919         } else if ((strstr(img_format, "gif") != NULL) || (strstr(img_format, "GIF") != NULL)) {
920                 thumbfile_ext = (char *)"gif";
921         } else if ((strstr(img_format, "bmp") != NULL) || (strstr(img_format, "BMP") != NULL)) {
922                 thumbfile_ext = (char *)"bmp";
923         } else {
924                 media_svc_error("Not proper img format");
925                 SAFE_FREE(thumb_dir);
926                 return MS_MEDIA_ERR_INTERNAL;
927         }
928
929         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
930         SAFE_STRLCPY(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
931         /*media_svc_debug("thumb_path is [%s]", thumb_path); */
932
933         SAFE_FREE(thumb_dir);
934
935         return MS_MEDIA_ERR_NONE;
936 }
937
938 int _media_svc_get_file_time(const char *full_path)
939 {
940         struct stat statbuf;
941         int fd = 0;
942
943         memset(&statbuf, 0, sizeof(struct stat));
944         fd = stat(full_path, &statbuf);
945         if (fd == -1) {
946                 media_svc_error("stat(%s) fails.", full_path);
947                 return MS_MEDIA_ERR_INTERNAL;
948         }
949
950         return statbuf.st_mtime;
951 }
952
953 int _media_svc_set_default_value(media_svc_content_info_s *content_info, bool refresh)
954 {
955         /* Set default GPS value before extracting meta information */
956         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
957         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
958         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
959
960         /* Set filename to title for all media */
961         char *title = NULL;
962         title = __media_svc_get_title_from_filepath(content_info->path);
963         if (title) {
964                 content_info->media_meta.title = g_strdup(title);
965                 SAFE_FREE(title);
966                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
967         } else {
968                 media_svc_error("Can't extract title");
969                 content_info->media_meta.title = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
970                 media_svc_retv_del_if(content_info->media_meta.title == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
971         }
972
973         /* Set default value before extracting meta information */
974         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
975         media_svc_retv_del_if(content_info->media_meta.description == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
976
977         content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
978         media_svc_retv_del_if(content_info->media_meta.copyright == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
979
980         content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
981         media_svc_retv_del_if(content_info->media_meta.track_num == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
982
983         content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
984         media_svc_retv_del_if(content_info->media_meta.album == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
985
986         content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
987         media_svc_retv_del_if(content_info->media_meta.artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
988
989         content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
990         media_svc_retv_del_if(content_info->media_meta.album_artist == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
991
992         content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
993         media_svc_retv_del_if(content_info->media_meta.genre == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
994
995         content_info->media_meta.composer = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
996         media_svc_retv_del_if(content_info->media_meta.composer == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
997
998         content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
999         media_svc_retv_del_if(content_info->media_meta.year == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1000
1001         if (refresh) {
1002                 media_svc_debug("refresh");
1003                 return MS_MEDIA_ERR_NONE;
1004         }
1005
1006         content_info->played_count = 0;
1007         content_info->last_played_time = 0;
1008         content_info->last_played_position = 0;
1009         content_info->favourate = 0;
1010         content_info->media_meta.rating = 0;
1011
1012         return MS_MEDIA_ERR_NONE;
1013 }
1014
1015 int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char *storage_id, media_svc_storage_type_e storage_type,
1016                         const char *path, media_svc_media_type_e *media_type, bool refresh)
1017 {
1018         int ret = MS_MEDIA_ERR_NONE;
1019         char * media_uuid = NULL;
1020         bool drm_type = false;
1021         char mime_type[256] = {0, };
1022
1023         content_info->path = g_strdup(path);
1024         media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1025
1026         struct stat st;
1027         memset(&st, 0, sizeof(struct stat));
1028         if (stat(path, &st) == 0) {
1029                 content_info->modified_time = st.st_mtime;
1030                 content_info->timeline = content_info->modified_time;
1031                 content_info->size = st.st_size;
1032         } else {
1033                 media_svc_stderror("stat failed");
1034         }
1035
1036         _media_svc_set_default_value(content_info, refresh);
1037
1038         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
1039         if (refresh) {
1040                 media_svc_debug("refresh");
1041                 return MS_MEDIA_ERR_NONE;
1042         }
1043
1044         content_info->storage_uuid = g_strdup(storage_id);
1045         media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1046
1047         content_info->storage_type = storage_type;
1048         time(&content_info->added_time);
1049
1050         media_uuid = _media_info_generate_uuid();
1051         if (media_uuid == NULL) {
1052                 _media_svc_destroy_content_info(content_info);
1053                 return MS_MEDIA_ERR_INTERNAL;
1054         }
1055
1056         content_info->media_uuid = g_strdup(media_uuid);
1057         media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1058
1059         content_info->file_name = g_path_get_basename(path);
1060         media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1061
1062         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
1063         if drm_contentinfo is not NULL, the file is OMA DRM.*/
1064         ret = __media_svc_get_mime_type(path, mime_type);
1065         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1066
1067         media_svc_debug("mime [%s]", mime_type);
1068         content_info->is_drm = drm_type;
1069
1070         ret = __media_svc_get_media_type(path, mime_type, media_type);
1071         media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
1072         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
1073                 media_svc_error("invalid media_type condition[%d]", *media_type);
1074                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1075         }
1076
1077         content_info->mime_type = g_strdup(mime_type);
1078         media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
1079
1080         media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
1081
1082         content_info->media_type = *media_type;
1083
1084         return MS_MEDIA_ERR_NONE;
1085 }
1086
1087 int image_360_check(char *path)
1088 {
1089         FILE *fp = NULL;
1090         long app1_size = 0;
1091         int size = 1;
1092         unsigned char exif_header[4];
1093         unsigned char exif_app1[2];
1094         unsigned char exif_app1_xmp[2];
1095         long exif_app1_xmp_size = 0;
1096         unsigned char exif_app1_xmp_t[2];
1097         char *xmp_data = NULL;
1098         int size1 = 0;
1099         int size2 = 0;
1100         int fdata = 0;
1101         int temp = 0;
1102
1103         fp = fopen(path, "rb");
1104         if (fp == NULL)
1105                 goto ERROR;
1106
1107         size = fread(exif_header, 1, sizeof(exif_header), fp);
1108         if (size <= 0)
1109                 goto ERROR;
1110
1111         if ((exif_header[0] == 0xff) && (exif_header[1] == 0xd8) && (exif_header[2] == 0xff) && (exif_header[3] == 0xe1)) {
1112                 size = fread(exif_app1, 1, sizeof(exif_app1), fp);
1113                 if (size <= 0)
1114                         goto ERROR;
1115
1116                 size1 = exif_app1[0];
1117                 size2 = exif_app1[1];
1118
1119                 app1_size = size1 * 256 + size2 - 2;
1120
1121                 if (fseek(fp, app1_size, SEEK_CUR) != 0)
1122                         goto ERROR;
1123
1124                 size = fread(exif_app1_xmp, 1, sizeof(exif_app1_xmp), fp);
1125                 if (size <= 0)
1126                         goto ERROR;
1127
1128                 if ((exif_app1_xmp[0] == 0xff) && (exif_app1_xmp[1] == 0xe1)) {
1129                         int result = 0;
1130                         char *ptr = NULL;
1131                         size = fread(exif_app1_xmp_t, 1, sizeof(exif_app1_xmp_t), fp);
1132                         if (size <= 0)
1133                                 goto ERROR;
1134
1135                         size1 = exif_app1_xmp_t[0];
1136                         size2 = exif_app1_xmp_t[1];
1137
1138                         exif_app1_xmp_size = size1 * 256 + size2 - 2;
1139
1140                         xmp_data = (char *)malloc(exif_app1_xmp_size);
1141                         if (xmp_data != NULL) {
1142                                 memset(xmp_data, 0x0, exif_app1_xmp_size);
1143                                 ptr = xmp_data;
1144
1145                                 while (exif_app1_xmp_size >= 0) {
1146                                         exif_app1_xmp_size--;
1147                                         fdata = fgetc(fp);
1148                                         if (fdata == EOF)
1149                                                 continue;
1150                                         if (fdata == '\0')
1151                                                 continue;
1152                                         *ptr = (char)fdata;
1153                                         ptr++;
1154                                         temp++;
1155                                 }
1156                                 ptr = ptr - temp;
1157
1158                                 if (strstr(ptr, "UsePanoramaViewer")
1159                                 && strstr(ptr, "True")
1160                                 && strstr(ptr, "ProjectionType")
1161                                 && strstr(ptr, "equirectangular"))
1162                                         result = 1;
1163
1164                                 SAFE_FREE(xmp_data);
1165                         } else {
1166                                 media_svc_error("malloc failed");
1167                         }
1168
1169                         if (fp) {
1170                                 fclose(fp);
1171                                 fp = NULL;
1172                         }
1173                         return result;
1174                 } else {
1175                         goto ERROR;
1176                 }
1177         } else {
1178                 goto ERROR;
1179         }
1180 ERROR:
1181         if (fp) {
1182                 fclose(fp);
1183                 fp = NULL;
1184         }
1185         return 0;
1186 }
1187
1188 int _media_svc_extract_image_metadata(sqlite3 *handle, media_svc_content_info_s *content_info)
1189 {
1190         double value = 0.0;
1191         int orient_value = 0;
1192         int exif_width = 0;
1193         int exif_height = 0;
1194         ExifData *ed = NULL;
1195         int has_datetaken = FALSE;
1196         double fnumber = 0.0;
1197         int iso = 0;
1198         char *path = NULL;
1199
1200         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
1201
1202         memset(buf, 0x00, sizeof(buf));
1203
1204         if (content_info == NULL || content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
1205                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
1206                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1207         }
1208
1209         path = content_info->path;
1210         if (!STRING_VALID(path)) {
1211                 media_svc_error("Invalid Path");
1212                 return MS_MEDIA_ERR_INVALID_PARAMETER;
1213         }
1214
1215         /* Load an ExifData object from an EXIF file */
1216         ed = exif_data_new_from_file(path);
1217
1218         if (!ed) {
1219                 media_svc_sec_debug("There is no exif data in [ %s ]", path);
1220                 goto GET_WIDTH_HEIGHT;
1221         }
1222
1223         content_info->media_meta.is_360 = image_360_check(path);
1224
1225         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
1226                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
1227                         if (strlen(buf) > 0) {
1228                                 if (strcmp(buf, "S") == 0)
1229                                         value = -1 * value;
1230                         }
1231                         content_info->media_meta.latitude = value;
1232                 } else {
1233                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1234                 }
1235         } else {
1236                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1237         }
1238
1239         memset(buf, 0x00, sizeof(buf));
1240
1241         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
1242                 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
1243                         if (strlen(buf) > 0) {
1244                                 if (strcmp(buf, "W") == 0)
1245                                         value = -1 * value;
1246                         }
1247                         content_info->media_meta.longitude = value;
1248                 } else {
1249                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1250                 }
1251         } else {
1252                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1253         }
1254
1255         memset(buf, 0x00, sizeof(buf));
1256
1257         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
1258                 if (strlen(buf) == 0)
1259                         content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1260                 else
1261                         content_info->media_meta.description = g_strdup(buf);
1262         } else {
1263                 content_info->media_meta.description = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
1264         }
1265
1266         memset(buf, 0x00, sizeof(buf));
1267
1268         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
1269                 if (strlen(buf) > 0) {
1270                         has_datetaken = TRUE;
1271                         content_info->media_meta.datetaken = g_strdup(buf);
1272
1273                         /* This is same as recorded_date */
1274                         content_info->media_meta.recorded_date = g_strdup(buf);
1275                 }
1276         }
1277
1278         memset(buf, 0x00, sizeof(buf));
1279
1280         if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
1281                 if (strlen(buf) > 0) {
1282                         has_datetaken = TRUE;
1283                         content_info->media_meta.datetaken = g_strdup(buf);
1284
1285                         /* This is same as recorded_date */
1286                         content_info->media_meta.recorded_date =  g_strdup(buf);
1287                 }
1288         }
1289
1290         if (has_datetaken) {
1291                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.datetaken);
1292                 if (content_info->timeline == 0)
1293                         content_info->timeline = content_info->modified_time;
1294                 else
1295                         media_svc_debug("Timeline : %ld", content_info->timeline);
1296         }
1297
1298         memset(buf, 0x00, sizeof(buf));
1299
1300         /* Get exposure_time value from exif. */
1301         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_EXPOSURE_TIME) == MS_MEDIA_ERR_NONE) {
1302                 if (strlen(buf) > 0)
1303                         content_info->media_meta.exposure_time = g_strdup(buf);
1304         }
1305
1306         /* Get fnumber value from exif. */
1307         if (__media_svc_get_exif_info(ed, NULL, NULL, &fnumber, EXIF_TAG_FNUMBER) == MS_MEDIA_ERR_NONE) {
1308                 if (fnumber > 0.0)
1309                         content_info->media_meta.fnumber = fnumber;
1310                 else
1311                         content_info->media_meta.fnumber = 0.0;
1312         } else {
1313                 content_info->media_meta.fnumber = 0.0;
1314         }
1315
1316         /* Get iso value from exif. */
1317         if (__media_svc_get_exif_info(ed, NULL, &iso, NULL, EXIF_TAG_ISO_SPEED_RATINGS) == MS_MEDIA_ERR_NONE) {
1318                 if (iso > 0)
1319                         content_info->media_meta.iso = iso;
1320                 else
1321                         content_info->media_meta.iso = 0;
1322         } else {
1323                 content_info->media_meta.iso = 0;
1324         }
1325
1326         memset(buf, 0x00, sizeof(buf));
1327
1328         /* Get model value from exif. */
1329         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_TAG_MODEL) == MS_MEDIA_ERR_NONE) {
1330                 if (strlen(buf) > 0)
1331                         content_info->media_meta.model = g_strdup(buf);
1332         }
1333
1334         /* Get orientation value from exif. */
1335         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
1336                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
1337                         content_info->media_meta.orientation = orient_value;
1338                 else
1339                         content_info->media_meta.orientation = 0;
1340         } else {
1341                 content_info->media_meta.orientation = 0;
1342         }
1343
1344         /* Get width value from exif. */
1345         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
1346                 if (exif_width > 0)
1347                         content_info->media_meta.width = exif_width;
1348                 else
1349                         content_info->media_meta.width = 0;
1350         } else {
1351                 content_info->media_meta.width = 0;
1352         }
1353
1354         /* Get height value from exif. */
1355         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
1356                 if (exif_height > 0)
1357                         content_info->media_meta.height = exif_height;
1358                 else
1359                         content_info->media_meta.height = 0;
1360         } else {
1361                 content_info->media_meta.height = 0;
1362         }
1363
1364         if (ed != NULL) exif_data_unref(ed);
1365
1366 GET_WIDTH_HEIGHT:
1367
1368         if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
1369                 /*Get image width, height*/
1370                 unsigned int img_width = 0;
1371                 unsigned int img_height = 0;
1372                 ImgCodecType img_type = IMG_CODEC_NONE;
1373
1374                 ImgGetImageInfo(path, &img_type, &img_width, &img_height);
1375                 if (content_info->media_meta.width == 0)
1376                         content_info->media_meta.width = img_width;
1377
1378                 if (content_info->media_meta.height == 0)
1379                         content_info->media_meta.height = img_height;
1380         }
1381
1382         return MS_MEDIA_ERR_NONE;
1383 }
1384
1385 int _media_svc_extract_music_metadata_for_update(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
1386 {
1387         MMHandleType tag = 0;
1388         char *p = NULL;
1389         int size = -1;
1390         int mmf_error = FILEINFO_ERROR_NONE;
1391         char *err_attr_name = NULL;
1392         int album_id = 0;
1393
1394         /*Get Content Tag attribute ===========*/
1395         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1396
1397         if (mmf_error == FILEINFO_ERROR_NONE) {
1398                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1399                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1400                         content_info->media_meta.album = g_strdup(p);
1401                 else
1402                         SAFE_FREE(err_attr_name);
1403
1404                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1405                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1406                         content_info->media_meta.artist = g_strdup(p);
1407                 else
1408                         SAFE_FREE(err_attr_name);
1409
1410                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1411                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1412                         content_info->media_meta.album_artist = g_strdup(p);
1413                 else
1414                         SAFE_FREE(err_attr_name);
1415
1416                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1417                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1418                         content_info->media_meta.genre = g_strdup(p);
1419
1420                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1421                         /*
1422                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1423                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1424                         }
1425                         */
1426                 } else {
1427                         SAFE_FREE(err_attr_name);
1428                 }
1429
1430                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1431                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1432                         if (!isspace(*p)) {
1433                                 content_info->media_meta.title = g_strdup(p);
1434                         } else {
1435                                 int idx = 0;
1436
1437                                 for (idx = 0; idx < size; idx++) {
1438                                         if (isspace(*p)) {
1439                                                 media_svc_debug("SPACE [%s]", p);
1440                                                 p++;
1441                                                 continue;
1442                                         } else {
1443                                                 media_svc_debug("Not SPACE [%s]", p);
1444                                                 content_info->media_meta.title = g_strdup(p);
1445                                                 break;
1446                                         }
1447                                 }
1448                         }
1449                 }
1450
1451                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1452                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1453                         content_info->media_meta.description = g_strdup(p);
1454                 else
1455                         SAFE_FREE(err_attr_name);
1456
1457                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1458                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1459                         content_info->media_meta.composer = g_strdup(p);
1460                 else
1461                         SAFE_FREE(err_attr_name);
1462
1463                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1464                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1465                         content_info->media_meta.copyright = g_strdup(p);
1466                 else
1467                         SAFE_FREE(err_attr_name);
1468
1469                 mmf_error = mm_file_destroy_tag_attrs(tag);
1470                 if (mmf_error != FILEINFO_ERROR_NONE)
1471                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1472         } else {
1473                 content_info->album_id = album_id;
1474         }
1475
1476         return MS_MEDIA_ERR_NONE;
1477 }
1478
1479 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, uid_t uid)
1480 {
1481         MMHandleType content = 0;
1482         MMHandleType tag = 0;
1483         char *p = NULL;
1484         unsigned char *image = NULL;
1485         unsigned int size = 0;
1486         int mmf_error = FILEINFO_ERROR_NONE;
1487         char *err_attr_name = NULL;
1488         bool extract_thumbnail = FALSE;
1489         bool append_album = FALSE;
1490         int album_id = 0;
1491         int ret = MS_MEDIA_ERR_NONE;
1492         int cdis_value = 0;
1493         unsigned int resize_size = 0;
1494         unsigned char *resize_image = NULL;
1495
1496         /*Get Content Tag attribute ===========*/
1497         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1498
1499         if (mmf_error == FILEINFO_ERROR_NONE) {
1500                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1501                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1502                         content_info->media_meta.album = g_strdup(p);
1503                 else
1504                         SAFE_FREE(err_attr_name);
1505
1506                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1507                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1508                         content_info->media_meta.artist = g_strdup(p);
1509                 else
1510                         SAFE_FREE(err_attr_name);
1511
1512                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM_ARTIST, &p, &size, NULL);
1513                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1514                         content_info->media_meta.album_artist = g_strdup(p);
1515                 else
1516                         SAFE_FREE(err_attr_name);
1517
1518                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1519                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1520                         content_info->media_meta.genre = g_strdup(p);
1521
1522                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1523                         /*
1524                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1525                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1526                         }
1527                         */
1528                 } else {
1529                         SAFE_FREE(err_attr_name);
1530                 }
1531
1532                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1533                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1534                         if (!isspace(*p)) {
1535                                 content_info->media_meta.title = g_strdup(p);
1536                         } else {
1537                                 int idx = 0;
1538
1539                                 for (idx = 0; idx < size; idx++) {
1540                                         if (isspace(*p)) {
1541                                                 media_svc_debug("SPACE [%s]", p);
1542                                                 p++;
1543                                                 continue;
1544                                         } else {
1545                                                 media_svc_debug("Not SPACE [%s]", p);
1546                                                 content_info->media_meta.title = g_strdup(p);
1547                                                 break;
1548                                         }
1549                                 }
1550                         }
1551                 }
1552
1553                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1554                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1555                         content_info->media_meta.description = g_strdup(p);
1556                 else
1557                         SAFE_FREE(err_attr_name);
1558
1559                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_SPHERICAL, &content_info->media_meta.is_360, NULL);
1560                 if (mmf_error != FILEINFO_ERROR_NONE)
1561                         SAFE_FREE(err_attr_name);
1562
1563                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1564                 if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1565                         if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1566                                 /*Creation time format is 2013-01-01 00:00:00. change it to 2013:01:01 00:00:00 like exif time format*/
1567                                 char *time_info = (char*)calloc(1, (size + 1));
1568                                 char *p_value = p;
1569                                 char *time_value = time_info;
1570                                 if (time_info != NULL) {
1571                                         while (*p_value != '\0') {
1572                                                 if (*p_value == '-')
1573                                                         *time_value = ':';
1574                                                 else
1575                                                         *time_value = *p_value;
1576                                                 time_value++;
1577                                                 p_value++;
1578                                         }
1579                                         *time_value = '\0';
1580                                         content_info->media_meta.recorded_date = g_strdup(time_info);
1581                                         SAFE_FREE(time_info);
1582                                 } else {
1583                                         media_svc_error("memory allocation error");
1584                                         ret = MS_MEDIA_ERR_OUT_OF_MEMORY;
1585                                 }
1586                         } else {
1587                                 content_info->media_meta.recorded_date = g_strdup(p);
1588                         }
1589
1590                         if (STRING_VALID(content_info->media_meta.recorded_date)) {
1591                                 /* This is same as datetaken */
1592                                 content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
1593
1594                                 content_info->timeline = __media_svc_get_timeline_from_str(content_info->media_meta.recorded_date);
1595                                 if (content_info->timeline == 0)
1596                                         content_info->timeline = content_info->modified_time;
1597                                 else
1598                                         media_svc_debug("Timeline : %ld", content_info->timeline);
1599                         }
1600                 } else {
1601                         SAFE_FREE(err_attr_name);
1602                 }
1603
1604                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1605                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1606                         content_info->media_meta.composer = g_strdup(p);
1607                 else
1608                         SAFE_FREE(err_attr_name);
1609
1610                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1611                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1612                         content_info->media_meta.copyright = g_strdup(p);
1613                 else
1614                         SAFE_FREE(err_attr_name);
1615
1616                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1617                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0)
1618                         content_info->media_meta.track_num = g_strdup(p);
1619                 else
1620                         SAFE_FREE(err_attr_name);
1621
1622                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1623                 if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
1624                         int year = 0;
1625                         if ((p != NULL) && (__media_svc_safe_atoi(p, &year) == MS_MEDIA_ERR_NONE))
1626                                 content_info->media_meta.year = g_strdup(p);
1627                         else
1628                                 media_svc_debug("Wrong Year Information [%s]", p);
1629                 } else {
1630                         SAFE_FREE(err_attr_name);
1631                 }
1632
1633                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1634                 if (mmf_error == FILEINFO_ERROR_NONE && size > 0) {
1635                         int rate = 0;
1636                         if ((p != NULL) && (__media_svc_safe_atoi(p, &rate) == MS_MEDIA_ERR_NONE))
1637                                 content_info->media_meta.rating = rate;
1638                 } else {
1639                         SAFE_FREE(err_attr_name);
1640                         content_info->media_meta.rating = 0;
1641                 }
1642
1643                 /*Initialize album_id to 0. below code will set the album_id*/
1644                 content_info->album_id = album_id;
1645                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1646
1647                 if (ret != MS_MEDIA_ERR_NONE) {
1648                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1649                                 media_svc_debug("album does not exist. So start to make album art");
1650                                 extract_thumbnail = TRUE;
1651                                 append_album = TRUE;
1652                         } else {
1653                                 extract_thumbnail = TRUE;
1654                                 append_album = FALSE;
1655                         }
1656                 } else {
1657                         content_info->album_id = album_id;
1658                         append_album = FALSE;
1659
1660                         if ((!g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) ||
1661                                 (!g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN))) {
1662                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1663                                 extract_thumbnail = TRUE;
1664                         } else {
1665                                 media_svc_debug("album already exists. don't need to make album art");
1666                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1667                                 extract_thumbnail = TRUE;
1668                         }
1669                 }
1670
1671                 /*Do not extract artwork for the USB Storage content*/
1672                 if (content_info->storage_type == MEDIA_SVC_STORAGE_EXTERNAL_USB)
1673                         extract_thumbnail = FALSE;
1674
1675                 if (extract_thumbnail == TRUE) {
1676                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1677                         if (mmf_error != FILEINFO_ERROR_NONE) {
1678                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1679                                 SAFE_FREE(err_attr_name);
1680                         } else {
1681                                 /*media_svc_debug("artwork size1 [%d]", size); */
1682                         }
1683
1684                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1685                         if (mmf_error != FILEINFO_ERROR_NONE) {
1686                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1687                                 SAFE_FREE(err_attr_name);
1688                         } else {
1689                                 /*media_svc_debug("artwork size2 [%d]", size); */
1690                         }
1691                         if (image != NULL && size > 0) {
1692                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1693                                 int artwork_mime_size = -1;
1694
1695                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1696                                 if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
1697                                         ret = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1698                                         if (ret != MS_MEDIA_ERR_NONE)
1699                                                 media_svc_error("Fail to Get Thumbnail Path");
1700                                         /* albumart resizing */
1701                                         __media_svc_resize_artwork(image, size, p, &resize_image, &resize_size);
1702                                 } else {
1703                                         SAFE_FREE(err_attr_name);
1704                                 }
1705
1706                                 if (strlen(thumb_path) > 0) {
1707                                         ret = __media_svc_save_image(resize_image, resize_size, thumb_path, uid);
1708                                         if (ret != MS_MEDIA_ERR_NONE)
1709                                                 media_svc_error("Fail to Save Thumbnail Image");
1710                                         else
1711                                                 content_info->thumbnail_path = g_strdup(thumb_path);
1712                                 }
1713
1714                                 if (size != resize_size) {
1715                                         media_svc_error("Albumart is resized");
1716                                         SAFE_FREE(resize_image);
1717                                 }
1718                         }
1719                 }
1720
1721                 if (append_album == TRUE) {
1722                         if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
1723                                 (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
1724                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1725                         else
1726                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1727
1728                         content_info->album_id = album_id;
1729                 }
1730
1731                 if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1732                         double longitude = 0.0;
1733                         double latitude = 0.0;
1734                         double altitude = 0.0;
1735
1736                         __media_svc_get_location_value(tag, &longitude, &latitude, &altitude);
1737                         content_info->media_meta.longitude = longitude;
1738                         content_info->media_meta.latitude = latitude;
1739                         content_info->media_meta.altitude = altitude;
1740
1741                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_CDIS, &cdis_value, NULL);
1742                         if (mmf_error != FILEINFO_ERROR_NONE) {
1743                                 cdis_value = 0;
1744                                 SAFE_FREE(err_attr_name);
1745                         }
1746
1747                         media_svc_debug("CDIS : %d", cdis_value);
1748
1749                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ROTATE, &p, &size, NULL);
1750                         if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
1751                                 content_info->media_meta.orientation = atoi(p);
1752                         } else {
1753                                 SAFE_FREE(err_attr_name);
1754                                 content_info->media_meta.orientation = 0;
1755                                 media_svc_debug("fail to get video orientation attr - err(%x)", mmf_error);
1756                         }
1757                 }
1758
1759                 mmf_error = mm_file_destroy_tag_attrs(tag);
1760                 if (mmf_error != FILEINFO_ERROR_NONE)
1761                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1762         }       else {
1763                 content_info->album_id = album_id;
1764         }
1765
1766         /*Get Content attribute ===========*/
1767         if (cdis_value == 1)
1768                 mmf_error = mm_file_create_content_attrs_safe(&content, content_info->path);
1769         else
1770                 mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
1771
1772         if (mmf_error == FILEINFO_ERROR_NONE) {
1773                 /*Common attribute*/
1774                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1775                 if (mmf_error != FILEINFO_ERROR_NONE) {
1776                         SAFE_FREE(err_attr_name);
1777                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1778                 } else {
1779                         /*media_svc_debug("duration : %d", content_info->media_meta.duration); */
1780                 }
1781
1782                 /*Sound/Music attribute*/
1783                 if ((content_info->media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1784
1785                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1786                         if (mmf_error != FILEINFO_ERROR_NONE) {
1787                                 SAFE_FREE(err_attr_name);
1788                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1789                         } else {
1790                                 /*media_svc_debug("bit rate : %d", content_info->media_meta.bitrate); */
1791                         }
1792
1793                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1794                         if (mmf_error != FILEINFO_ERROR_NONE) {
1795                                 SAFE_FREE(err_attr_name);
1796                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1797                         } else {
1798                                 /*media_svc_debug("sample rate : %d", content_info->media_meta.samplerate); */
1799                         }
1800
1801                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1802                         if (mmf_error != FILEINFO_ERROR_NONE) {
1803                                 SAFE_FREE(err_attr_name);
1804                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1805                         } else {
1806                                 /*media_svc_debug("channel : %d", content_info->media_meta.channel); */
1807                         }
1808
1809                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1810                         if (mmf_error != FILEINFO_ERROR_NONE) {
1811                                 SAFE_FREE(err_attr_name);
1812                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1813                         } else {
1814                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1815                         }
1816                 } else if (content_info->media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1817                         int audio_bitrate = 0;
1818                         int video_bitrate = 0;
1819
1820                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1821                         if (mmf_error != FILEINFO_ERROR_NONE) {
1822                                 SAFE_FREE(err_attr_name);
1823                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1824                         } else {
1825                                 /*media_svc_debug("audio bit rate : %d", audio_bitrate); */
1826                         }
1827
1828                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1829                         if (mmf_error != FILEINFO_ERROR_NONE) {
1830                                 SAFE_FREE(err_attr_name);
1831                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1832                         } else {
1833                                 /*media_svc_debug("video bit rate : %d", video_bitrate); */
1834                         }
1835
1836                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1837
1838                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1839                         if (mmf_error != FILEINFO_ERROR_NONE) {
1840                                 SAFE_FREE(err_attr_name);
1841                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1842                         } else {
1843                                 /*media_svc_debug("width : %d", content_info->media_meta.width); */
1844                         }
1845
1846                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1847                         if (mmf_error != FILEINFO_ERROR_NONE) {
1848                                 SAFE_FREE(err_attr_name);
1849                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1850                         } else {
1851                                 /*media_svc_debug("height : %d", content_info->media_meta.height); */
1852                         }
1853                 } else {
1854                         media_svc_error("Not support type");
1855                         mmf_error = mm_file_destroy_content_attrs(content);
1856                         if (mmf_error != FILEINFO_ERROR_NONE)
1857                                 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1858
1859                         return MS_MEDIA_ERR_INVALID_PARAMETER;
1860                 }
1861
1862                 mmf_error = mm_file_destroy_content_attrs(content);
1863                 if (mmf_error != FILEINFO_ERROR_NONE)
1864                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1865         } else {
1866                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1867         }
1868
1869         return MS_MEDIA_ERR_NONE;
1870 }
1871
1872 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1873 {
1874         media_svc_retm_if(content_info == NULL, "content info is NULL");
1875
1876         /* Delete media_svc_content_info_s */
1877         SAFE_FREE(content_info->media_uuid);
1878         SAFE_FREE(content_info->path);
1879         SAFE_FREE(content_info->file_name);
1880         SAFE_FREE(content_info->mime_type);
1881         SAFE_FREE(content_info->folder_uuid);
1882         SAFE_FREE(content_info->thumbnail_path);
1883         SAFE_FREE(content_info->storage_uuid);
1884
1885         /* Delete media_svc_content_meta_s */
1886         SAFE_FREE(content_info->media_meta.title);
1887         SAFE_FREE(content_info->media_meta.album);
1888         SAFE_FREE(content_info->media_meta.artist);
1889         SAFE_FREE(content_info->media_meta.album_artist);
1890         SAFE_FREE(content_info->media_meta.genre);
1891         SAFE_FREE(content_info->media_meta.composer);
1892         SAFE_FREE(content_info->media_meta.year);
1893         SAFE_FREE(content_info->media_meta.recorded_date);
1894         SAFE_FREE(content_info->media_meta.copyright);
1895         SAFE_FREE(content_info->media_meta.track_num);
1896         SAFE_FREE(content_info->media_meta.description);
1897         SAFE_FREE(content_info->media_meta.datetaken);
1898         SAFE_FREE(content_info->media_meta.exposure_time);
1899         SAFE_FREE(content_info->media_meta.model);
1900         SAFE_FREE(content_info->media_meta.weather);
1901         SAFE_FREE(content_info->media_meta.category);
1902         SAFE_FREE(content_info->media_meta.keyword);
1903         SAFE_FREE(content_info->media_meta.location_tag);
1904         SAFE_FREE(content_info->media_meta.content_name);
1905         SAFE_FREE(content_info->media_meta.age_rating);
1906         SAFE_FREE(content_info->media_meta.author);
1907         SAFE_FREE(content_info->media_meta.provider);
1908
1909         SAFE_FREE(content_info->media_meta.title_pinyin);
1910         SAFE_FREE(content_info->media_meta.album_pinyin);
1911         SAFE_FREE(content_info->media_meta.artist_pinyin);
1912         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1913         SAFE_FREE(content_info->media_meta.genre_pinyin);
1914         SAFE_FREE(content_info->media_meta.composer_pinyin);
1915         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1916         SAFE_FREE(content_info->media_meta.description_pinyin);
1917
1918         return;
1919 }
1920
1921 int _media_svc_request_thumbnail(const char *path, char *thumb_path, int max_length, uid_t uid)
1922 {
1923         int ret = MS_MEDIA_ERR_NONE;
1924
1925         ret = thumbnail_request_from_db(path, thumb_path, max_length, uid);
1926         if (ret != MS_MEDIA_ERR_NONE) {
1927                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
1928                 ret = MS_MEDIA_ERR_INTERNAL;
1929         } else {
1930                 media_svc_sec_debug("thumbnail_request_from_db success: thumbnail path[%s]", thumb_path);
1931         }
1932
1933         return ret;
1934 }
1935
1936 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1937 {
1938         int ret = MS_MEDIA_ERR_NONE;
1939         int size = 0;
1940         pinyin_name_s *pinyinname = NULL;
1941
1942         *pinyin_str = NULL;
1943
1944         if (!STRING_VALID(src_str)) {
1945                 media_svc_debug("String is invalid");
1946                 return ret;
1947         }
1948
1949         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1950         if (ret == MS_MEDIA_ERR_NONE) {
1951                 if (size > 0 && STRING_VALID(pinyinname[0].pinyin_name))
1952                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1953                 else
1954                         *pinyin_str = strdup(src_str);  /* Return Original Non China Character */
1955         }
1956
1957         _media_svc_pinyin_free(pinyinname, size);
1958
1959         return ret;
1960 }
1961
1962 bool _media_svc_check_pinyin_support(void)
1963 {
1964         /*Check CSC*/
1965         return TRUE;
1966 }
1967
1968 int _media_svc_get_media_type(const char *path, int *mediatype)
1969 {
1970         int ret = MS_MEDIA_ERR_NONE;
1971         char mime_type[256] = {0};
1972         media_svc_media_type_e media_type =  MEDIA_SVC_MEDIA_TYPE_OTHER;
1973
1974         ret = __media_svc_get_mime_type(path, mime_type);
1975         if (ret == MS_MEDIA_ERR_NONE)
1976                 __media_svc_get_media_type(path, mime_type, &media_type);
1977         else
1978                 media_svc_error("__media_svc_get_mime_type failed");
1979
1980         *mediatype = media_type;
1981
1982         return ret;
1983 }
1984