Revert "Modify error type, remove unused code, and replace uuid code to uuid library."
[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 #include <time.h>
25 #include <string.h>
26 #include <sys/vfs.h>
27 #include <glib/gstdio.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <dirent.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <aul/aul.h>
34 #include <mm_file.h>
35 #include <mm_error.h>
36 #include <libexif/exif-data.h>
37 #include <media-thumbnail.h>
38 #include <media-util.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include "uuid.h"
42 #include "media-svc-util.h"
43 #include "media-svc-error.h"
44 #include "media-svc-debug.h"
45 #include "media-svc-env.h"
46 #include "media-svc-hash.h"
47 #include "media-svc-album.h"
48 #include "media-svc-localize_ch.h"
49
50 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**<  Maximum file ext lenth*/
51 #define GLOBAL_USER    0 //#define     tzplatform_getenv(TZ_GLOBAL) //TODO
52
53 typedef enum {
54         MEDIA_SVC_EXTRACTED_FIELD_NONE                  = 0x00000001,
55         MEDIA_SVC_EXTRACTED_FIELD_TITLE                         = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
56         MEDIA_SVC_EXTRACTED_FIELD_DESC                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
57         MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT             = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
58         MEDIA_SVC_EXTRACTED_FIELD_AUTHOR                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
59         MEDIA_SVC_EXTRACTED_FIELD_ARTIST                        = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
60         MEDIA_SVC_EXTRACTED_FIELD_GENRE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
61         MEDIA_SVC_EXTRACTED_FIELD_ALBUM                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
62         MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
63         MEDIA_SVC_EXTRACTED_FIELD_YEAR                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
64         MEDIA_SVC_EXTRACTED_FIELD_CATEGORY              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
65 } media_svc_extracted_field_e;
66
67 #if 0
68 static char *__year_2_str(int year);
69
70 static char *__year_2_str(int year)
71 {
72         static char ret[MEDIA_SVC_METADATA_LEN_MAX];
73
74         if (year == -1 || year == 0) {
75                 _strncpy_safe(ret, MEDIA_SVC_TAG_UNKNOWN, MEDIA_SVC_METADATA_LEN_MAX);
76         } else {
77                 snprintf(ret, MEDIA_SVC_METADATA_LEN_MAX - 1, "%d", year);
78         }
79         return ret;
80 }
81 #endif
82
83 char *_media_info_generate_uuid(void)
84 {
85         uuid_t uuid_value;
86         static char uuid_unparsed[50];  
87
88         uuid_generate(uuid_value);
89         uuid_unparse(uuid_value, uuid_unparsed);
90
91         //media_svc_debug("UUID : %s", uuid_unparsed);
92         return uuid_unparsed;
93 }
94
95 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
96 {
97         if (!x_src || strlen(x_src) == 0) {
98                 media_svc_error("x_src is NULL");
99                 return;
100         }
101
102         if (max_len < 1) {
103                 media_svc_error("length is Wrong");
104                 return;
105         }
106
107     strncpy(x_dst, x_src, max_len-1);
108         x_dst[max_len-1] = '\0';
109 }
110
111 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
112 {
113         int len = 0;
114
115         if (!STRING_VALID(src)) {
116                 media_svc_error("invalid src");
117                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
118         }
119
120         SAFE_FREE(*dst);
121
122         len = strlen(src) + 1;
123         *dst = malloc(len);
124
125         if (*dst == NULL) {
126                 media_svc_error("malloc failed");
127                 return MEDIA_INFO_ERROR_INTERNAL;
128         }
129
130         strncpy(*dst, src, len);
131         char *p = *dst;
132         p[len - 1] = '\0';
133
134         return MEDIA_INFO_ERROR_NONE;
135 }
136
137 static void __media_svc_split_to_double(char *input, double *arr, int *num)
138 {
139         char tmp_arr[255] = { 0, };
140         int len = strlen(input);
141         int i = 0, idx = 0, tmp_idx = 0;
142         int is_prev_space = 0;
143
144         for (;;) {
145                 if (input[len - 1] == ' ') {
146                         len--;
147                 } else {
148                         break;
149                 }
150         }
151
152         for (i = 0; i < len; i++) {
153                 if (idx > 2) {
154                         break;
155                 }
156
157                 if (input[i] == ' ') {
158                         if (is_prev_space == 1) {
159                                 continue;
160                         }
161                         if (idx <= 2) {
162                                 arr[idx++] = atof(tmp_arr);
163                         }
164                         tmp_idx = 0;
165                         is_prev_space = 1;
166                         continue;
167                 }
168
169                 tmp_arr[tmp_idx] = input[i];
170                 tmp_arr[++tmp_idx] = '\0';
171                 is_prev_space = 0;
172         }
173
174         if (i == len) {
175                 if (idx <= 2) {
176                         arr[idx++] = atof(tmp_arr);
177                 }
178                 *num = idx;
179                 return;
180         } else {
181                 *num = idx--;
182                 return;
183         }
184 }
185
186 static int __media_svc_get_exif_info(ExifData *ed,
187                                                                                 char *buf,
188                                                                                 int *i_value,
189                                                                                 double *d_value,
190                                                                                 int ifdtype,
191                                                                                 long tagtype)
192 {
193         ExifEntry *entry;
194         ExifIfd ifd;
195         ExifTag tag;
196
197         if (ed == NULL) {
198                 //media_svc_debug("ExifData is NULL");
199                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
200         }
201
202         ifd = ifdtype;
203         tag = tagtype;
204
205         entry = exif_data_get_entry(ed, tag);
206         if (entry) {
207                 /* Get the contents of the tag in human-readable form */
208                 if (tag == EXIF_TAG_ORIENTATION ||
209                                 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
210                                 tag == EXIF_TAG_PIXEL_Y_DIMENSION) {
211
212                         if (i_value == NULL) {
213                                 media_svc_error("i_value is NULL");
214                                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
215                         }
216
217                         ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
218                         short exif_value = exif_get_short(entry->data, mByteOrder);
219                         //media_svc_debug("%s : %d", exif_tag_get_name_in_ifd(tag,ifd), exif_value);
220                         *i_value = (int)exif_value;
221
222                 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
223
224                         if (d_value == NULL) {
225                                 media_svc_error("d_value is NULL");
226                                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
227                         }
228
229                         /* Get the contents of the tag in human-readable form */
230                         char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
231                         exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
232                         gps_buf[strlen(gps_buf)] = '\0';
233
234                         //media_svc_debug("%s: %s\n", exif_tag_get_name_in_ifd(tag, ifd), gps_buf);
235
236                         double tmp_arr[3] = { 0.0, 0.0, 0.0 };
237                         int count = 0;
238
239                         __media_svc_split_to_double(gps_buf, tmp_arr, &count);
240                         if (count != 3) {
241                                 media_svc_error("Value is invalid");
242                                 return MEDIA_INFO_ERROR_INTERNAL;
243                         }
244
245                         *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
246                         //media_svc_debug("GPS value is %f", *d_value);
247                 } else {
248
249                         if (buf == NULL) {
250                                 media_svc_error("buf is NULL");
251                                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
252                         }
253
254                         exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
255                         buf[strlen(buf)] = '\0';
256
257                         if (*buf) {
258                                 media_svc_debug("%s: %s\n", exif_tag_get_name_in_ifd(tag, ifd), buf);
259                         }
260                 }
261         }
262
263         return MEDIA_INFO_ERROR_NONE;
264 }
265
266 unsigned int _media_svc_get_current_time(void)
267 {
268         struct timeval  t;
269         unsigned int tval = 0;
270         gettimeofday(&t, NULL);
271
272         tval = t.tv_sec*1000000L + t.tv_usec;
273
274         return tval/1000;
275 }
276
277 int _media_svc_check_escape_char(char ch)
278 {
279         int i;
280         char escape_char[3] = {'%', '_' ,'#'};
281
282         for (i = 0; i < 3; i++) {
283                 if (ch == escape_char[i]) {
284                         return 1;
285                 }
286         }
287
288         return 0;
289 }
290
291 char *_media_svc_escape_str(char *input, int len)
292 {
293         int i = 0;
294         int j = 0;
295         char *result = NULL;
296
297         result = (char*)malloc(len * 2 * sizeof(char) + 1);
298         if (result == NULL) {
299                 return NULL;
300         }
301
302         for (i = 0; i < len; i++, j++) {
303                 if (input[i] == '\0') break;
304
305                 if (_media_svc_check_escape_char(input[i])) {
306                         result[j] = '#';
307                         result[++j] = input[i];
308                 } else {
309                         result[j] = input[i];
310                 }
311         }
312
313         result[j] = '\0';
314
315         return result;
316 }
317
318 int _media_svc_rename_file( const char *old_name, const char *new_name)
319 {
320         if((old_name == NULL) || (new_name == NULL))
321         {
322                 media_svc_error("invalid file name");
323                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
324         }
325
326         if (rename(old_name, new_name) < 0) {
327                 media_svc_error("Old : [%s] New : [%s] errno : [%s]", old_name, new_name, strerror(errno));
328                 return MEDIA_INFO_ERROR_INTERNAL;
329         }
330
331         return MEDIA_INFO_ERROR_NONE;
332 }
333
334 bool _media_svc_remove_file(const char *path)
335 {
336         int result = -1;
337
338         result = remove(path);
339         if (result == 0) {
340                 media_svc_debug("success to remove file");
341                 return TRUE;
342         } else {
343                 media_svc_error("fail to remove file result errno = %s", strerror(errno));
344                 return FALSE;
345         }
346 }
347
348 int _media_svc_remove_all_files_in_dir(const char *dir_path)
349 {
350         struct dirent *entry = NULL;
351         struct stat st;
352         char filename[MEDIA_SVC_PATHNAME_SIZE] = {0};
353         DIR *dir = NULL;
354
355         dir = opendir(dir_path);
356         if (dir == NULL) {
357                 media_svc_error("%s is not exist", dir_path);
358                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
359         }
360
361         while ((entry = readdir(dir)) != NULL) {
362                 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
363                         continue;
364                 }
365                 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry->d_name);
366
367                 if (stat(filename, &st) != 0) {
368                         continue;
369                 }
370                 if (S_ISDIR(st.st_mode)) {
371                         continue;
372                 }
373                 if (unlink(filename) != 0) {
374                         media_svc_error("failed to remove : %s", filename);
375                         closedir(dir);
376                         return MEDIA_INFO_ERROR_INTERNAL;
377                 }
378         }
379
380         closedir(dir);
381         return MEDIA_INFO_ERROR_NONE;
382 }
383
384 char *_media_svc_get_title_from_filepath (const char *path)
385 {
386         char *filename = NULL;
387         char *title = NULL;
388         char    *ext = NULL;
389         int filename_len = -1;
390         int new_title_len = -1;
391
392         if (!path) {
393                 media_svc_error("path is NULL");
394                 return NULL;
395         }
396
397         filename = g_path_get_basename(path);
398         if (!STRING_VALID(filename)) {
399                 media_svc_error("wrong file name");
400                 SAFE_FREE(filename);
401                 return NULL;
402         }
403
404         filename_len = strlen(filename);
405
406         ext = g_strrstr(filename, ".");
407         if (!ext) {
408                 media_svc_error("there is no file extention");
409                 return filename;
410         }
411
412         new_title_len = filename_len - strlen(ext);
413         if (new_title_len < 1) {
414                 media_svc_error("title length is zero");
415                 SAFE_FREE(filename);
416                 return NULL;
417         }
418
419         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
420
421         SAFE_FREE(filename);
422
423         media_svc_debug("extract title is [%s]", title);
424
425         return title;
426 }
427
428 static int _mkdir(const char *dir, mode_t mode) {
429         char tmp[256];
430         char *p = NULL;
431         size_t len;
432
433         snprintf(tmp, sizeof(tmp),"%s",dir);
434         len = strlen(tmp);
435         if(tmp[len - 1] == '/')
436                 tmp[len - 1] = 0;
437         for(p = tmp + 1; *p; p++)
438                 if(*p == '/') {
439                         *p = 0;
440                         mkdir(tmp, mode);
441                         *p = '/';
442                 }
443         return mkdir(tmp, mode);
444 }
445
446 static char* _media_svc_get_thumb_path(uid_t uid)
447 {
448         char *result_psswd = NULL;
449         struct group *grpinfo = NULL;
450         if(uid == getuid())
451         {
452                 result_psswd = strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
453                 grpinfo = getgrnam("users");
454                 if(grpinfo == NULL) {
455                         media_svc_error("getgrnam(users) returns NULL !");
456                         return NULL;
457                 }
458         }
459         else
460         {
461                 struct passwd *userinfo = getpwuid(uid);
462                 if(userinfo == NULL) {
463                         media_svc_error("getpwuid(%d) returns NULL !", uid);
464                         return NULL;
465                 }
466                 grpinfo = getgrnam("users");
467                 if(grpinfo == NULL) {
468                         media_svc_error("getgrnam(users) returns NULL !");
469                         return NULL;
470                 }
471                 // Compare git_t type and not group name
472                 if (grpinfo->gr_gid != userinfo->pw_gid) {
473                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
474                         return NULL;
475                 }
476                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb", userinfo->pw_dir);
477         }
478
479         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
480
481         return result_psswd;
482 }
483
484 char* _media_svc_get_thumb_internal_path(uid_t uid)
485 {
486         char *result_psswd = NULL;
487         struct group *grpinfo = NULL;
488         if(uid == getuid())
489         {
490                 result_psswd = strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
491                 grpinfo = getgrnam("users");
492                 if(grpinfo == NULL) {
493                         media_svc_error("getgrnam(users) returns NULL !");
494                         return NULL;
495                 }
496         }
497         else
498         {
499                 struct passwd *userinfo = getpwuid(uid);
500                 if(userinfo == NULL) {
501                         media_svc_error("getpwuid(%d) returns NULL !", uid);
502                         return NULL;
503                 }
504                 grpinfo = getgrnam("users");
505                 if(grpinfo == NULL) {
506                         media_svc_error("getgrnam(users) returns NULL !");
507                         return NULL;
508                 }
509                 // Compare git_t type and not group name
510                 if (grpinfo->gr_gid != userinfo->pw_gid) {
511                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
512                         return NULL;
513                 }
514                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
515         }
516
517         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
518
519         return result_psswd;
520 }
521
522 char* _media_svc_get_thumb_external_path(uid_t uid)
523 {
524         char *result_psswd = NULL;
525         struct group *grpinfo = NULL;   
526         if(uid == getuid())
527         {
528                 result_psswd = strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
529                 grpinfo = getgrnam("users");
530                 if(grpinfo == NULL) {
531                         media_svc_error("getgrnam(users) returns NULL !");
532                         return NULL;
533                 }
534         }
535         else
536         {
537                 struct passwd *userinfo = getpwuid(uid);
538                 if(userinfo == NULL) {
539                         media_svc_error("getpwuid(%d) returns NULL !", uid);
540                         return NULL;
541                 }
542                 grpinfo = getgrnam("users");
543                 if(grpinfo == NULL) {
544                         media_svc_error("getgrnam(users) returns NULL !");
545                         return NULL;
546                 }
547                 // Compare git_t type and not group name
548                 if (grpinfo->gr_gid != userinfo->pw_gid) {
549                         media_svc_error("UID [%d] does not belong to 'users' group!", uid);
550                         return NULL;
551                 }
552                 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/mmc", userinfo->pw_dir);
553         }
554
555         _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
556
557         return result_psswd;
558 }
559
560 int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
561 {
562         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
563
564         if (!image) {
565                 media_svc_error("invalid image..");
566                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
567         }
568
569         struct statfs fs;
570         if (-1 == statfs(_media_svc_get_thumb_path(uid), &fs)) {
571                 media_svc_error("error in statfs");
572                 return MEDIA_INFO_ERROR_INTERNAL;
573         }
574
575         long bsize_kbytes = fs.f_bsize >> 10;
576
577         if ((bsize_kbytes * fs.f_bavail) < 1024) {
578                 media_svc_error("not enought space...");
579                 return MEDIA_INFO_ERROR_INTERNAL;
580         }
581
582         FILE *fp = NULL;
583         int nwrite = -1;
584         if (image != NULL && size > 0) {
585                 fp = fopen(image_path, "w");
586
587                 if (fp == NULL) {
588                         media_svc_error("failed to open file");
589                         return MEDIA_INFO_ERROR_INTERNAL;
590                 }
591
592                 nwrite = fwrite(image, 1, size, fp);
593                 if (nwrite != size) {
594                         media_svc_error("failed to write thumbnail");
595                         fclose(fp);
596                         return MEDIA_INFO_ERROR_INTERNAL;
597                 }
598                 fclose(fp);
599         }
600
601         return MEDIA_INFO_ERROR_NONE;
602 }
603
604
605 bool _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)
606 {
607         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0};
608         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0};
609         char *thumb_dir = NULL;
610         char hash[255 + 1];
611         char *thumbfile_ext = NULL;
612
613         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? _media_svc_get_thumb_internal_path(uid) : _media_svc_get_thumb_external_path(uid);
614
615         memset(file_ext, 0, sizeof(file_ext));
616         if (!_media_svc_get_file_ext(pathname, file_ext)) {
617                 media_svc_error("get file ext fail");
618         }
619
620         int err = -1;
621         err = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
622         if (err < 0) {
623                 media_svc_error("mb_svc_generate_hash_code failed : %d", err);
624                 return FALSE;
625         }
626
627         //media_svc_debug("img format is [%s]", img_format);
628
629         if((strstr(img_format, "jpeg") != NULL) ||(strstr(img_format, "jpg") != NULL) ||(strstr(img_format, "JPG") != NULL)) {
630                 thumbfile_ext = "jpg";
631         } else if((strstr(img_format, "png") != NULL) ||(strstr(img_format, "PNG") != NULL)) {
632                 thumbfile_ext = "png";
633         } else if((strstr(img_format, "gif") != NULL) ||(strstr(img_format, "GIF") != NULL)) {
634                 thumbfile_ext = "gif";
635         } else if((strstr(img_format, "bmp") != NULL) ||(strstr(img_format, "BMP") != NULL)) {
636                 thumbfile_ext = "bmp";
637         } else {
638                 media_svc_error("Not proper img format");
639                 return FALSE;
640         }
641
642         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
643         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
644         //media_svc_debug("thumb_path is [%s]", thumb_path);
645
646         return TRUE;
647 }
648
649 bool _media_svc_get_file_ext(const char *file_path, char *file_ext)
650 {
651         int i = 0;
652
653         for (i = strlen(file_path); i >= 0; i--) {
654                 if (file_path[i] == '.') {
655                         _strncpy_safe(file_ext, &file_path[i+1], MEDIA_SVC_FILE_EXT_LEN_MAX);
656                         return true;
657                 }
658
659                 if (file_path[i] == '/') {
660                         return false;
661                 }
662         }
663         return false;
664 }
665
666 int _media_svc_get_file_time(const char *full_path)
667 {
668         struct stat statbuf;
669         int fd = 0;
670
671         memset(&statbuf, 0, sizeof(struct stat));
672         fd = stat(full_path, &statbuf);
673         if (fd == -1) {
674                  media_svc_error("stat(%s) fails.", full_path);
675                  return MEDIA_INFO_ERROR_INTERNAL;
676          }
677
678          return statbuf.st_mtime;
679 }
680
681 int _media_svc_set_media_info(media_svc_content_info_s *content_info, media_svc_storage_type_e storage_type,
682                           const char *path, media_svc_media_type_e *media_type, bool refresh, drm_content_info_s **drm_contentInfo)
683 {
684         int ret = MEDIA_INFO_ERROR_NONE;
685         char * media_uuid = NULL;
686         char * file_name = NULL;
687         struct stat st;
688         drm_bool_type_e drm_type = DRM_FALSE;
689         char mime_type[256] = {0};
690
691         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
692         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
693
694         memset(&st, 0, sizeof(struct stat));
695         if (stat(path, &st) == 0) {
696                 content_info->modified_time = st.st_mtime;
697                 content_info->timeline = content_info->modified_time;
698                 content_info->size = st.st_size;
699                 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
700         } else {
701                 media_svc_error("stat failed : %s", strerror(errno));
702         }
703
704         /* Set default GPS value before extracting meta information */
705         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
706         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
707         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
708
709         /* Set filename to title for all media */
710         char *title = NULL;
711         title = _media_svc_get_title_from_filepath(content_info->path);
712         if (title) {
713                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
714                 if(ret != MEDIA_INFO_ERROR_NONE)
715                         media_svc_error("strcpy error");
716                 SAFE_FREE(title);
717         } else {
718                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
719                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
720                 media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
721         }
722
723         /* Set default value before extracting meta information */
724         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
725         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
726
727         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
728         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
729
730         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
731         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
732
733         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
734         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
735
736         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
737         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
738
739         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
740         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
741
742         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
743         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
744
745         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
746         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
747
748         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
749         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
750
751         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
752         if(refresh) {
753                 media_svc_debug("refresh");
754                 return MEDIA_INFO_ERROR_NONE;
755         }
756
757         content_info->storage_type = storage_type;
758         time(&content_info->added_time);
759
760         media_uuid = _media_info_generate_uuid();
761         media_svc_retvm_if(media_uuid == NULL, MEDIA_INFO_ERROR_INTERNAL, "Invalid UUID");
762
763         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
764         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
765
766         file_name = g_path_get_basename(path);
767         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
768         SAFE_FREE(file_name);
769         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
770
771         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
772         if drm_contentinfo is not NULL, the file is OMA DRM.*/
773         ret = _media_svc_get_mime_type(path, mime_type, &drm_type, drm_contentInfo);
774         if (ret < 0) {
775                 media_svc_error("media_svc_get_mime_type failed : %d (%s)", ret, path);
776                 return MEDIA_INFO_ERROR_INVALID_PATH;
777         }
778
779         media_svc_error("mime [%s]", mime_type);
780         content_info->is_drm = drm_type;
781
782         ret = _media_svc_get_media_type(path, mime_type, media_type);
783         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
784         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
785                 media_svc_error("invalid media_type condition[%d]", *media_type);
786                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
787         }
788
789         ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
790         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
791
792         media_svc_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
793
794         content_info->media_type = *media_type;
795
796         content_info->played_count = 0;
797         content_info->last_played_time= 0;
798         content_info->last_played_position= 0;
799         content_info->favourate= 0;
800         content_info->media_meta.rating = 0;
801
802         return MEDIA_INFO_ERROR_NONE;
803 }
804
805 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
806 {
807         if (content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
808                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
809                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
810         }
811
812         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
813         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
814         memset(buf, 0x00, sizeof(buf));
815         memset(description_buf, 0x00, sizeof(description_buf));
816
817         int ret = MEDIA_INFO_ERROR_NONE;
818         double value = 0.0;
819         int orient_value = 0;
820         int exif_width = 0;
821         int exif_height = 0;
822         ExifData *ed = NULL;
823
824         char *path = content_info->path;
825         if (path == NULL) {
826                 media_svc_error("path is NULL");
827                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
828         }
829
830         /* Load an ExifData object from an EXIF file */
831         ed = exif_data_new_from_file(path);
832
833         if (!ed) {
834                 media_svc_debug("There is no exif data in [ %s ]", path);
835         }
836
837         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_GPS_LATITUDE_REF) == MEDIA_INFO_ERROR_NONE) {
838                 if (strlen(buf) != 0) {
839                         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE) == MEDIA_INFO_ERROR_NONE) {
840
841                                 if (strcmp(buf, "S") == 0) {
842                                         value = -1 * value;
843                                 }
844
845                                 content_info->media_meta.latitude = value;
846                         } else {
847                                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
848                         }
849                 } else {
850                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
851                 }
852         } else {
853                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
854         }
855
856         memset(buf, 0x00, sizeof(buf));
857
858         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_GPS_LONGITUDE_REF) == MEDIA_INFO_ERROR_NONE) {
859                 if (strlen(buf) != 0) {
860                         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE) == MEDIA_INFO_ERROR_NONE) {
861                                 if (strcmp(buf, "W") == 0) {
862                                         value = -1 * value;
863                                 }
864                                 content_info->media_meta.longitude = value;
865                         } else {
866                                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
867                         }
868                 } else {
869                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
870                 }
871         } else {
872                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
873         }
874
875         memset(buf, 0x00, sizeof(buf));
876
877         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION) == MEDIA_INFO_ERROR_NONE) {
878                 if (strlen(description_buf) == 0) {
879                         //media_svc_debug("Use 'No description'");
880                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
881                         if(ret != MEDIA_INFO_ERROR_NONE)
882                                 media_svc_error("strcpy error");
883                 } else {
884                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
885                         if(ret != MEDIA_INFO_ERROR_NONE)
886                                 media_svc_error("strcpy error");
887                 }
888         } else {
889                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
890                 if(ret != MEDIA_INFO_ERROR_NONE)
891                         media_svc_error("strcpy error");
892         }
893
894         memset(buf, 0x00, sizeof(buf));
895
896         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME) == MEDIA_INFO_ERROR_NONE) {
897                 if (strlen(buf) == 0) {
898                         //media_svc_debug("time  is NULL");
899                 } else {
900                         //media_svc_debug("time  is %s", buf);
901                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, buf);
902                         if(ret != MEDIA_INFO_ERROR_NONE) {
903                                 media_svc_error("strcpy error");
904                         } else {
905                                 /* This is same as recorded_date */
906                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
907                                 if(ret != MEDIA_INFO_ERROR_NONE)
908                                         media_svc_error("strcpy error");
909                         }
910                 }
911         }
912
913         /* Get orientation value from exif. */
914         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_IFD_0, EXIF_TAG_ORIENTATION) == MEDIA_INFO_ERROR_NONE) {
915                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
916                         content_info->media_meta.orientation = orient_value;
917                 } else {
918                         content_info->media_meta.orientation = 0;
919                 }
920         } else {
921                 content_info->media_meta.orientation = 0;
922         }
923
924         /* Get width value from exif. */
925         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION) == MEDIA_INFO_ERROR_NONE) {
926                 if (exif_width > 0) {
927                         content_info->media_meta.width = exif_width;
928                 } else {
929                         content_info->media_meta.width = 0;
930                 }
931         } else {
932                 content_info->media_meta.width = 0;
933         }
934
935         /* Get height value from exif. */
936         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION) == MEDIA_INFO_ERROR_NONE) {
937                 if (exif_height > 0) {
938                         content_info->media_meta.height = exif_height;
939                 } else {
940                         content_info->media_meta.height = 0;
941                 }
942         } else {
943                 content_info->media_meta.height = 0;
944         }
945
946         content_info->media_meta.weather = NULL;
947
948         if (ed != NULL) exif_data_unref(ed);
949
950         /* Set filename to title for image media */
951         char *title = NULL;
952         title = _media_svc_get_title_from_filepath(content_info->path);
953         if (title) {
954                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
955                 if(ret != MEDIA_INFO_ERROR_NONE)
956                         media_svc_error("strcpy error");
957                 SAFE_FREE(title);
958         } else {
959                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
960         }
961
962 #if 0
963         /* Extracting thumbnail */
964         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
965         int width = 0;
966         int height = 0;
967
968         ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
969         if (ret < 0) {
970                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
971         } else {
972                 //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
973         }
974
975         content_info->media_meta.width = width;
976         content_info->media_meta.height = height;
977
978         if (STRING_VALID(thumb_path))
979                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
980         else
981                 content_info->thumbnail_path = NULL;
982
983         if(ret != MEDIA_INFO_ERROR_NONE)
984                 media_svc_error("strcpy error");
985 #endif
986         return MEDIA_INFO_ERROR_NONE;
987 }
988
989 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type, drm_content_info_s *drm_contentInfo, uid_t uid)
990 {
991         MMHandleType content = 0;
992         MMHandleType tag = 0;
993         char *p = NULL;
994         void *image = NULL;
995         int size = -1;
996         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
997         int mmf_error = MM_ERROR_NONE;
998         bool thumb_extracted_from_drm = FALSE;
999         char *err_attr_name = NULL;
1000         char *title = NULL;
1001         bool extract_thumbnail = FALSE;
1002         bool append_album = FALSE;
1003         int album_id = 0;
1004         double gps_value = 0.0;
1005         int ret = MEDIA_INFO_ERROR_NONE;
1006         char *path = content_info->path;
1007
1008 #ifdef __SUPPORT_DRM
1009         /*To do - code for DRM content*/
1010         if (content_info->is_drm) {
1011                 drm_file_type_e drm_file_type;
1012
1013                 ret = drm_get_file_type(path, &drm_file_type);
1014                 if (ret < 0) {
1015                         media_svc_error("drm_get_file_type falied : %d", ret);
1016                         drm_file_type = DRM_TYPE_UNDEFINED;
1017                 }
1018
1019                 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
1020                 if (drm_contentInfo != NULL) {
1021                         if (drm_file_type == DRM_TYPE_OMA_V1) {
1022                                 if (strlen(drm_contentInfo->title) > 0) {
1023                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, drm_contentInfo->title);
1024                                         if(ret != MEDIA_INFO_ERROR_NONE)
1025                                                 media_svc_error("strcpy error");
1026                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1027                                 }
1028
1029                                 if (strlen(drm_contentInfo->description) > 0) {
1030                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, drm_contentInfo->description);
1031                                         if(ret != MEDIA_INFO_ERROR_NONE)
1032                                                 media_svc_error("strcpy error");
1033                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
1034                                 }
1035                         } else if (drm_file_type == DRM_TYPE_OMA_V2) {
1036                                 if (strlen(drm_contentInfo->title) > 0) {
1037                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, drm_contentInfo->title);
1038                                         if(ret != MEDIA_INFO_ERROR_NONE)
1039                                                 media_svc_error("strcpy error");
1040                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1041                                 }
1042
1043                                 if (strlen(drm_contentInfo->description) > 0) {
1044                                         ret =  __media_svc_malloc_and_strncpy(&content_info->media_meta.description, drm_contentInfo->description);
1045                                         if(ret != MEDIA_INFO_ERROR_NONE)
1046                                                 media_svc_error("strcpy error");
1047                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
1048                                 }
1049
1050                                 if (strlen(drm_contentInfo->copyright) > 0) {
1051                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, drm_contentInfo->copyright);
1052                                         if(ret != MEDIA_INFO_ERROR_NONE)
1053                                                 media_svc_error("strcpy error");
1054                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT;
1055                                 }
1056
1057                                 if (strlen(drm_contentInfo->author) > 0) {
1058                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, drm_contentInfo->author);
1059                                         if(ret != MEDIA_INFO_ERROR_NONE)
1060                                                 media_svc_error("strcpy error");
1061                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, drm_contentInfo->author);
1062                                         if(ret != MEDIA_INFO_ERROR_NONE)
1063                                                 media_svc_error("strcpy error");
1064
1065                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1066                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_ARTIST;
1067                                 }
1068                         }
1069
1070                         if (!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) {
1071                                 title = _media_svc_get_title_from_filepath(path);
1072                                 if (title) {
1073                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1074                                         SAFE_FREE(title);
1075                                         if(ret != MEDIA_INFO_ERROR_NONE)
1076                                                 media_svc_error("strcpy error");
1077                                 } else {
1078                                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1079                                 }
1080                         }
1081
1082                         return MEDIA_INFO_ERROR_NONE;
1083                 } else {
1084                         media_svc_debug("Some Not OMA Content's metadata is not incrypted so fileinfo can extracted metadata");
1085                 }
1086         }
1087 #endif
1088
1089         /*Get Content attribute ===========*/
1090         mmf_error = mm_file_create_content_attrs(&content, content_info->path);
1091
1092         if (mmf_error == MM_ERROR_NONE) {
1093                 /*Common attribute*/
1094                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1095                 if (mmf_error != MM_ERROR_NONE) {
1096                         SAFE_FREE(err_attr_name);
1097                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1098                 } else {
1099                         //media_svc_debug("duration : %d", content_info->media_meta.duration);
1100                 }
1101
1102                 /*Sound/Music attribute*/
1103                 if((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1104
1105                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1106                         if (mmf_error != MM_ERROR_NONE) {
1107                                 SAFE_FREE(err_attr_name);
1108                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1109                         } else {
1110                                 //media_svc_debug("bit rate : %d", content_info->media_meta.bitrate);
1111                         }
1112
1113                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1114                         if (mmf_error != MM_ERROR_NONE) {
1115                                 SAFE_FREE(err_attr_name);
1116                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1117                         } else {
1118                                 //media_svc_debug("sample rate : %d", content_info->media_meta.samplerate);
1119                         }
1120
1121                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1122                         if (mmf_error != MM_ERROR_NONE) {
1123                                 SAFE_FREE(err_attr_name);
1124                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1125                         } else {
1126                                 //media_svc_debug("channel : %d", content_info->media_meta.channel);
1127                         }
1128
1129                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1130                         if (mmf_error != MM_ERROR_NONE) {
1131                                 SAFE_FREE(err_attr_name);
1132                                 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1133                         } else {
1134                                 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1135                         }
1136                 }else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1137                         int audio_bitrate = 0;
1138                         int video_bitrate = 0;
1139
1140                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1141                         if (mmf_error != MM_ERROR_NONE) {
1142                                 SAFE_FREE(err_attr_name);
1143                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1144                         } else {
1145                                 //media_svc_debug("audio bit rate : %d", audio_bitrate);
1146                         }
1147
1148                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1149                         if (mmf_error != MM_ERROR_NONE) {
1150                                 SAFE_FREE(err_attr_name);
1151                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1152                         } else {
1153                                 //media_svc_debug("video bit rate : %d", video_bitrate);
1154                         }
1155
1156                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1157
1158                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1159                         if (mmf_error != MM_ERROR_NONE) {
1160                                 SAFE_FREE(err_attr_name);
1161                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1162                         } else {
1163                                 //media_svc_debug("width : %d", content_info->media_meta.width);
1164                         }
1165
1166                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1167                         if (mmf_error != MM_ERROR_NONE) {
1168                                 SAFE_FREE(err_attr_name);
1169                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1170                         } else {
1171                                 //media_svc_debug("height : %d", content_info->media_meta.height);
1172                         }
1173
1174                 } else {
1175                         media_svc_error("Not support type");
1176                         return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1177                 }
1178
1179                 mmf_error = mm_file_destroy_content_attrs(content);
1180                 if (mmf_error != MM_ERROR_NONE) {
1181                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1182                 }
1183         } else {
1184                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1185         }
1186
1187         /*Get Content Tag attribute ===========*/
1188         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1189
1190         if (mmf_error == MM_ERROR_NONE) {
1191                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1192                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1193                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1194                         if(ret != MEDIA_INFO_ERROR_NONE)
1195                                 media_svc_error("strcpy error");
1196
1197                         //media_svc_debug("album[%d] : %s", size, content_info->media_meta.album);
1198                 } else {
1199                         SAFE_FREE(err_attr_name);
1200                         //media_svc_debug("album - unknown");
1201                 }
1202
1203                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1204                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1205                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1206                         if(ret != MEDIA_INFO_ERROR_NONE)
1207                                 media_svc_error("strcpy error");
1208                         //media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist);
1209                 } else {
1210                         SAFE_FREE(err_attr_name);
1211                         //media_svc_debug("artist - unknown");
1212                 }
1213
1214                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1215                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1216                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1217                         if(ret != MEDIA_INFO_ERROR_NONE)
1218                                 media_svc_error("strcpy error");
1219
1220                         //media_svc_debug("genre : %s", content_info->media_meta.genre);
1221                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1222                         /*
1223                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1224                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1225                         }
1226                         */
1227                 } else {
1228                         SAFE_FREE(err_attr_name);
1229                         //media_svc_debug("genre - unknown");
1230                 }
1231
1232                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1233                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* &&   (!isspace(*p))*/) {
1234                         if(!isspace(*p)) {
1235                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1236                                 if(ret != MEDIA_INFO_ERROR_NONE)
1237                                         media_svc_error("strcpy error");
1238
1239                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1240                                 //media_svc_debug("extract title from content : %s", content_info->media_meta.title);
1241                                 //media_svc_debug("^^^^^^^^^^^^^^^ path = %s, title = %s, size = %d ^^^^^^^^^^^^^^", content_info->path, content_info->media_meta.title, size);
1242                         }
1243                         else {
1244                                 int idx = 0;
1245
1246                                 for(idx = 0; idx < size; idx++) {
1247                                         if(isspace(*p)) {
1248                                                 media_svc_debug("SPACE [%s]", p);
1249                                                 p++;
1250                                                 continue;
1251                                         } else {
1252                                                 media_svc_debug("Not SPACE [%s]", p);
1253                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1254                                                 if(ret != MEDIA_INFO_ERROR_NONE)
1255                                                         media_svc_error("strcpy error");
1256                                                 break;
1257                                         }
1258                                 }
1259
1260                                 if(idx == size)
1261                                 {
1262                                         media_svc_debug("Can't extract title. All string is space");
1263                                         title = _media_svc_get_title_from_filepath(content_info->path);
1264                                         if (title) {
1265                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1266                                                 if(ret != MEDIA_INFO_ERROR_NONE)
1267                                                         media_svc_error("strcpy error");
1268                                                 SAFE_FREE(title);
1269                                         } else {
1270                                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1271                                         }
1272                                 }
1273                         }
1274                 } else {
1275                         SAFE_FREE(err_attr_name);
1276                         title = _media_svc_get_title_from_filepath(content_info->path);
1277                         if (title) {
1278                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1279                                 if(ret != MEDIA_INFO_ERROR_NONE)
1280                                         media_svc_error("strcpy error");
1281                                 SAFE_FREE(title);
1282                         } else {
1283                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1284                         }
1285                 }
1286
1287                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1288                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1289                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1290                         if(ret != MEDIA_INFO_ERROR_NONE)
1291                                 media_svc_error("strcpy error");
1292                         //media_svc_debug("desc : %s", content_info->media_meta.description);
1293                 } else {
1294                         SAFE_FREE(err_attr_name);
1295                 }
1296
1297                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1298                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1299                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1300                         if(ret != MEDIA_INFO_ERROR_NONE) {
1301                                 media_svc_error("strcpy error");
1302                         } else {
1303                                 /* This is same as datetaken */
1304                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, p);
1305                                 if(ret != MEDIA_INFO_ERROR_NONE)
1306                                         media_svc_error("strcpy error");
1307                         }
1308                         //media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date);
1309                 } else {
1310                         SAFE_FREE(err_attr_name);
1311                 }
1312
1313                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1314                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1315                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1316                         if(ret != MEDIA_INFO_ERROR_NONE)
1317                                 media_svc_error("strcpy error");
1318                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1319                         //media_svc_debug("extract composer from content : %s", content_info->media_meta.composer);
1320                 } else {
1321                         //media_svc_debug("composer - unknown");
1322                         SAFE_FREE(err_attr_name);
1323                 }
1324
1325                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1326                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1327                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1328                         if(ret != MEDIA_INFO_ERROR_NONE)
1329                                 media_svc_error("strcpy error");
1330                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1331                         //media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright);
1332                 } else {
1333                         //media_svc_debug("copyright - unknown");
1334                         SAFE_FREE(err_attr_name);
1335                 }
1336
1337                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1338                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1339                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1340                         if(ret != MEDIA_INFO_ERROR_NONE)
1341                                 media_svc_error("strcpy error");
1342                 } else {
1343                         SAFE_FREE(err_attr_name);
1344                 }
1345
1346                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1347                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == MM_ERROR_NONE) && (size == 4)) {
1348                         int year = 0;
1349                         if((p != NULL) && (sscanf( p, "%d", &year))) {
1350                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1351                                 if(ret != MEDIA_INFO_ERROR_NONE)
1352                                         media_svc_error("strcpy error");
1353                         } else {
1354                                 media_svc_debug("Wrong Year Information [%s]", p);
1355                         }
1356                 } else {
1357                         SAFE_FREE(err_attr_name);
1358                 }
1359
1360                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1361                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1362                         content_info->media_meta.rating = atoi(p);
1363                 } else {
1364                         SAFE_FREE(err_attr_name);
1365                         content_info->media_meta.rating = 0;
1366                 }
1367
1368                 if((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1369                         /*Initialize album_id to 0. below code will set the album_id*/
1370                         content_info->album_id = album_id;
1371                         ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1372         
1373                         if (ret != MEDIA_INFO_ERROR_NONE) {
1374                                 if (ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
1375                                         media_svc_debug("album does not exist. So start to make album art");
1376                                         extract_thumbnail = TRUE;
1377                                         append_album = TRUE;
1378                                 } else {
1379                                         extract_thumbnail = FALSE;
1380                                         append_album = FALSE;
1381                                 }
1382                         } else {
1383                                 content_info->album_id = album_id;
1384                                 append_album = FALSE;
1385         
1386                                 if((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1387                                         (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1388         
1389                                         media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1390                                         extract_thumbnail = TRUE;
1391                                 } else {
1392         
1393                                         media_svc_debug("album already exists. don't need to make album art");
1394                                         ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1395                                         media_svc_retv_del_if((ret != MEDIA_INFO_ERROR_NONE) && (ret != MEDIA_INFO_ERROR_DATABASE_NO_RECORD), ret, content_info);
1396                                         extract_thumbnail = FALSE;
1397                                 }
1398                         }
1399         
1400                         if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE)) {
1401                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1402                                 if (mmf_error != MM_ERROR_NONE) {
1403                                         media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1404                                         SAFE_FREE(err_attr_name);
1405                                 } else {
1406                                         //media_svc_debug("artwork size1 [%d]", size);
1407                                 }
1408         
1409                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1410                                 if (mmf_error != MM_ERROR_NONE) {
1411                                         media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1412                                         SAFE_FREE(err_attr_name);
1413                                 } else {
1414                                         //media_svc_debug("artwork size2 [%d]", size);
1415                                 }
1416                                 if (image != NULL && size > 0) {
1417                                         bool result = FALSE;
1418                                         int ret = MEDIA_INFO_ERROR_NONE;
1419                                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1420                                         int artwork_mime_size = -1;
1421         
1422                                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1423                                         if ((mmf_error == MM_ERROR_NONE) && (artwork_mime_size > 0)) {
1424                                                 result = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1425                                                 if (result == FALSE) {
1426                                                         media_svc_error("Fail to Get Thumbnail Path");
1427                                                 }
1428                                         } else {
1429                                                 SAFE_FREE(err_attr_name);
1430                                         }
1431         
1432                                         if(strlen(thumb_path) > 0)
1433                                         {
1434                                                 ret = _media_svc_save_image(image, size, thumb_path, uid);
1435                                                 if (ret != MEDIA_INFO_ERROR_NONE) {
1436                                                         media_svc_error("Fail to Save Thumbnail Image");
1437                                                 }
1438                                                 else {
1439                                                         ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1440                                                         if(ret != MEDIA_INFO_ERROR_NONE)
1441                                                                 media_svc_error("strcpy error");
1442                                                 }
1443                                         }
1444                                 }
1445                         }
1446         
1447                         if(append_album == TRUE) {
1448         
1449                                 if((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1450                                         (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1451                                         ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1452                                 else
1453                                         ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1454         
1455                                 content_info->album_id = album_id;
1456                         }
1457                 } else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1458                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
1459                         if (mmf_error == MM_ERROR_NONE) {
1460                                 if (gps_value == 0.0)
1461                                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1462                                 else
1463                                         content_info->media_meta.longitude = gps_value;
1464                         } else {
1465                                 SAFE_FREE(err_attr_name);
1466                         }
1467
1468                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
1469                         if (mmf_error == MM_ERROR_NONE) {
1470                                 if (gps_value == 0.0)
1471                                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1472                                 else
1473                                         content_info->media_meta.latitude = gps_value;
1474                         } else {
1475                                 SAFE_FREE(err_attr_name);
1476                         }
1477
1478                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
1479                         if (mmf_error == MM_ERROR_NONE) {
1480                                 if (gps_value == 0.0)
1481                                         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1482                                 else
1483                                         content_info->media_meta.altitude = gps_value;
1484                         } else {
1485                                 SAFE_FREE(err_attr_name);
1486                         }
1487 #if 0
1488                         //if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE))
1489                         {
1490                                 /* Extracting thumbnail */
1491                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1492                                 int width = 0;
1493                                 int height = 0;
1494
1495                                 ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
1496                                 if (ret < 0) {
1497                                         media_svc_error("thumbnail_request_from_db failed: %d", ret);
1498                                 } else {
1499                                         //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
1500                                 }
1501
1502                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1503                                 if(ret != MEDIA_INFO_ERROR_NONE)
1504                                         media_svc_error("strcpy error");
1505
1506                                 if (content_info->media_meta.width <= 0) content_info->media_meta.width = width;
1507                                 if (content_info->media_meta.height <= 0) content_info->media_meta.height = height;
1508                         }
1509 #endif
1510                 }
1511
1512                 mmf_error = mm_file_destroy_tag_attrs(tag);
1513                 if (mmf_error != MM_ERROR_NONE) {
1514                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1515                 }
1516         }       else {
1517                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1518                 char *title = NULL;
1519                 media_svc_error("no tag information");
1520
1521                 title = _media_svc_get_title_from_filepath(content_info->path);
1522                 if (title) {
1523                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1524                         if(ret != MEDIA_INFO_ERROR_NONE)
1525                                 media_svc_error("strcpy error");
1526                         SAFE_FREE(title);
1527                 } else {
1528                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1529                 }
1530
1531                 content_info->album_id = album_id;
1532         }
1533
1534         return MEDIA_INFO_ERROR_NONE;
1535 }
1536
1537 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1538 {
1539         media_svc_retm_if(content_info == NULL, "content info is NULL");
1540
1541         /* Delete media_svc_content_info_s */
1542         SAFE_FREE(content_info->media_uuid);
1543         SAFE_FREE(content_info->path);
1544         SAFE_FREE(content_info->file_name);
1545         SAFE_FREE(content_info->mime_type);
1546         SAFE_FREE(content_info->folder_uuid);
1547         SAFE_FREE(content_info->thumbnail_path);
1548
1549         /* Delete media_svc_content_meta_s */
1550         SAFE_FREE(content_info->media_meta.title);
1551         SAFE_FREE(content_info->media_meta.album);
1552         SAFE_FREE(content_info->media_meta.artist);
1553         SAFE_FREE(content_info->media_meta.album_artist);
1554         SAFE_FREE(content_info->media_meta.genre);
1555         SAFE_FREE(content_info->media_meta.composer);
1556         SAFE_FREE(content_info->media_meta.year);
1557         SAFE_FREE(content_info->media_meta.recorded_date);
1558         SAFE_FREE(content_info->media_meta.copyright);
1559         SAFE_FREE(content_info->media_meta.track_num);
1560         SAFE_FREE(content_info->media_meta.description);
1561         SAFE_FREE(content_info->media_meta.datetaken);
1562         SAFE_FREE(content_info->media_meta.weather);
1563
1564         SAFE_FREE(content_info->media_meta.title_pinyin);
1565         SAFE_FREE(content_info->media_meta.album_pinyin);
1566         SAFE_FREE(content_info->media_meta.artist_pinyin);
1567         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1568         SAFE_FREE(content_info->media_meta.genre_pinyin);
1569         SAFE_FREE(content_info->media_meta.composer_pinyin);
1570         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1571         SAFE_FREE(content_info->media_meta.description_pinyin);
1572
1573         return;
1574 }
1575
1576 int _media_svc_get_store_type_by_path(const char *path, media_svc_storage_type_e *storage_type)
1577 {
1578         if(STRING_VALID(path))
1579         {
1580                 if(strncmp(path, MEDIA_SVC_PATH_PHONE, strlen(MEDIA_SVC_PATH_PHONE)) == 0)
1581                 {
1582                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
1583                 }
1584                 else if(strncmp (path, MEDIA_SVC_PATH_MMC, strlen(MEDIA_SVC_PATH_MMC)) == 0)
1585                 {
1586                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
1587                 }
1588         }
1589         else
1590         {
1591                 media_svc_error("INVALID parameter");
1592                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1593         }
1594
1595         return MEDIA_INFO_ERROR_NONE;
1596 }
1597
1598 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
1599 {
1600   char *result, *sr;
1601   size_t i, count = 0;
1602   size_t oldlen = strlen(olds); if (oldlen < 1) return s;
1603   size_t newlen = strlen(news);
1604
1605   if (newlen != oldlen) {
1606     for (i = 0; s[i] != '\0';) {
1607       if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
1608       else i++;
1609     }   
1610   } else i = strlen(s);
1611
1612
1613   result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
1614   if (result == NULL) return NULL;
1615
1616   sr = result;
1617   while (*s) {
1618     if (memcmp(s, olds, oldlen) == 0) {
1619       memcpy(sr, news, newlen);
1620       sr += newlen;
1621       s  += oldlen;
1622     } else *sr++ = *s++;
1623   }
1624
1625   *sr = '\0';
1626
1627   return result;
1628 }
1629
1630 int _media_svc_error_convert(int error)
1631 {
1632         media_svc_debug("error : [%d]", error);
1633
1634         if(error == MS_MEDIA_ERR_NONE)                                                  /*Error None*/
1635                 return MEDIA_INFO_ERROR_NONE;
1636         else if(error == MS_MEDIA_ERR_DB_CONNECT_FAIL)                  /*DB Connect Fail*/
1637                 return MEDIA_INFO_ERROR_DATABASE_CONNECT;
1638         else if(error == MS_MEDIA_ERR_DB_DISCONNECT_FAIL)               /*DB Disconnect Fail*/
1639                 return MEDIA_INFO_ERROR_DATABASE_DISCONNECT;
1640         else if(error == MS_MEDIA_ERR_SOCKET_CONN)                              /*Socket Connect Fail*/
1641                 return MEDIA_INFO_ERROR_SOCKET_CONN;
1642         else if(error == MS_MEDIA_ERR_INVALID_PARAMETER || error == MS_MEDIA_ERR_INVALID_PATH)
1643                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1644
1645         return MEDIA_INFO_ERROR_INTERNAL;
1646 }
1647
1648
1649 bool _media_svc_is_drm_file(const char *path)
1650 {
1651 #ifdef __SUPPORT_DRM
1652         int ret;
1653         drm_bool_type_e is_drm_file = DRM_UNKNOWN;
1654
1655         ret = drm_is_drm_file(path,&is_drm_file);
1656         if(DRM_RETURN_SUCCESS == ret && DRM_TRUE == is_drm_file)
1657                 return TRUE;
1658 #endif
1659         return FALSE;
1660 }
1661
1662 int _media_svc_get_mime_in_drm_info(const char *path, char *mime, drm_content_info_s **drm_contentInfo)
1663 {
1664 #ifdef __SUPPORT_DRM
1665         int ret = MEDIA_INFO_ERROR_NONE;
1666         drm_file_type_e file_type = DRM_TYPE_UNDEFINED;
1667
1668         if (path == NULL || mime == NULL)
1669                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1670
1671         ret = drm_get_file_type(path, &file_type);
1672         if (ret != DRM_RETURN_SUCCESS) {
1673                 media_svc_error("drm_get_file_type() failed : %d", ret);
1674                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1675         } else {
1676                 if (file_type == DRM_TYPE_OMA_V1
1677                 || file_type == DRM_TYPE_OMA_V2
1678                 || file_type == DRM_TYPE_OMA_PD) {
1679                         *drm_contentInfo = malloc(sizeof(drm_content_info_s));
1680                         memset(*drm_contentInfo,0x0,sizeof(drm_content_info_s));
1681                         ret = drm_get_content_info(path, *drm_contentInfo);
1682                         if (ret != DRM_RETURN_SUCCESS) {
1683                                 media_svc_error("drm_svc_get_content_info() fails :%d ", ret);
1684                                 free(*drm_contentInfo);
1685                                 *drm_contentInfo = NULL;
1686                                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1687                         }
1688
1689                         if (STRING_VALID((*drm_contentInfo)->mime_type)) {
1690                                 strncpy(mime,(*drm_contentInfo)->mime_type, MEDIA_SVC_METADATA_LEN_MAX);
1691                                 media_svc_debug("DRM contentType : %s",(*drm_contentInfo)->mime_type);
1692                         } else {
1693                                 free(*drm_contentInfo);
1694                                 *drm_contentInfo = NULL;
1695                                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1696                         }
1697                 } else {
1698                         media_svc_error("THIS IS DRM BUT YOU SHOULD USE API OF AUL LIBRARY");
1699                         *drm_contentInfo = NULL;
1700                         return MEDIA_INFO_ERROR_INVALID_MEDIA;
1701                 }
1702         }
1703
1704         return MEDIA_INFO_ERROR_NONE;
1705 #else
1706         *drm_contentInfo = NULL;
1707         return MEDIA_INFO_ERROR_INVALID_MEDIA;
1708 #endif
1709 }
1710
1711 int _media_svc_get_content_type_from_mime(const char * path, const char * mimetype, int * category)
1712 {
1713         int i = 0;
1714         int err = 0;
1715
1716         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
1717
1718         //media_svc_debug("mime type : %s", mimetype);
1719
1720         /*categorize from mimetype */
1721         for (i = 0; i < CONTENT_TYPE_NUM; i++) {
1722                 if (strstr(mimetype, content_category[i].content_type) != NULL) {
1723                         *category = (*category | content_category[i].category_by_mime);
1724                         break;
1725                 }
1726         }
1727
1728         /*in application type, exitst sound file ex) x-smafs */
1729         if (*category & MEDIA_SVC_CATEGORY_ETC) {
1730                 int prefix_len = strlen(content_category[0].content_type);
1731
1732                 for (i = 0; i < SOUND_MIME_NUM; i++) {
1733                         if (strstr(mimetype + prefix_len, sound_mime_table[i]) != NULL) {
1734                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
1735                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
1736                                 break;
1737                         }
1738                 }
1739         }
1740
1741         /*check music file in soun files. */
1742         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
1743                 int prefix_len = strlen(content_category[0].content_type) + 1;
1744
1745                 //media_svc_error("mime_type : %s", mimetype + prefix_len);
1746
1747                 for (i = 0; i < MUSIC_MIME_NUM; i++) {
1748                         if (strcmp(mimetype + prefix_len, music_mime_table[i]) == 0) {
1749                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
1750                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1751                                 break;
1752                         }
1753                 }
1754
1755                 /*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*/
1756                 if(strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
1757                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
1758                         *category |= MEDIA_SVC_CATEGORY_ETC;
1759                 }
1760         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
1761                 /*some video files don't have video stream. in this case it is categorize as music. */
1762                 char *ext;
1763                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
1764                 ext = strrchr(path, '.');
1765                 if (ext != NULL) {
1766                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
1767                                 int audio = 0;
1768                                 int video = 0;
1769
1770                                 err = mm_file_get_stream_info(path, &audio, &video);
1771                                 if (err == 0) {
1772                                         if (audio > 0 && video == 0) {
1773                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
1774                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1775                                         }
1776                                 }
1777                         }
1778                 }
1779         }
1780
1781         //media_svc_debug("category_from_ext : %d", *category);
1782
1783         return err;
1784 }
1785
1786 /*
1787 drm_contentifo is not NULL, if the file is OMA DRM.
1788 If the file is not OMA DRM, drm_contentinfo must be NULL.
1789 */
1790 int _media_svc_get_mime_type(const char *path, char *mimetype, drm_bool_type_e *is_drm, drm_content_info_s **drm_contentInfo)
1791 {
1792         int ret = MEDIA_INFO_ERROR_NONE;
1793
1794         if (path == NULL)
1795                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1796
1797 #ifdef __SUPPORT_DRM
1798         /* In case of drm file. */
1799         if (_media_svc_is_drm_file(path)) {
1800                 *is_drm = DRM_TRUE;
1801                 ret =  _media_svc_get_mime_in_drm_info(path, mimetype, drm_contentInfo);
1802                 if (ret == MEDIA_INFO_ERROR_NONE) {
1803                         return ret;
1804                 }
1805         }
1806 #else
1807         *is_drm = DRM_FALSE;
1808         *drm_contentInfo = NULL;
1809 #endif
1810
1811         /*in case of normal files or failure to get mime in drm */
1812         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
1813                 media_svc_error("aul_get_mime_from_file fail");
1814                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1815         }
1816
1817         return MEDIA_INFO_ERROR_NONE;
1818 }
1819
1820 int _media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
1821 {
1822         int ret = MEDIA_INFO_ERROR_NONE;
1823         int category = 0;
1824
1825         media_svc_media_type_e type;
1826
1827         ret = _media_svc_get_content_type_from_mime(path, mime_type, &category);
1828         if (ret < 0) {
1829                 media_svc_error("_media_svc_get_content_type_from_mime failed : %d", ret);
1830         }
1831
1832         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1833         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
1834         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
1835         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
1836         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1837
1838         *media_type = type;
1839
1840         return ret;
1841 }
1842
1843 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1844 {
1845         int ret = MEDIA_INFO_ERROR_NONE;
1846         int size = 0;
1847         pinyin_name_s *pinyinname = NULL;
1848         *pinyin_str = NULL;
1849
1850         if(!STRING_VALID(src_str))
1851         {
1852                 media_svc_debug("String is invalid");
1853                 return ret;
1854         }
1855
1856         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1857         if (ret == MEDIA_INFO_ERROR_NONE)
1858         {
1859                 if(STRING_VALID(pinyinname[0].pinyin_name))
1860                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1861                 else
1862                         *pinyin_str = strdup(src_str);  //Return Original Non China Character
1863         }
1864
1865         _media_svc_pinyin_free(pinyinname, size);
1866
1867         return ret;
1868 }
1869
1870 bool _media_svc_check_pinyin_support(void)
1871 {
1872         /*Check CSC*/
1873         return TRUE;
1874 }