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