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