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