modify DB schema.
[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                 }else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1129                         int audio_bitrate = 0;
1130                         int video_bitrate = 0;
1131
1132                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1133                         if (mmf_error != MM_ERROR_NONE) {
1134                                 SAFE_FREE(err_attr_name);
1135                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1136                         } else {
1137                                 //media_svc_debug("audio bit rate : %d", audio_bitrate);
1138                         }
1139
1140                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_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("video bit rate : %d", video_bitrate);
1146                         }
1147
1148                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1149
1150                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1151                         if (mmf_error != MM_ERROR_NONE) {
1152                                 SAFE_FREE(err_attr_name);
1153                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1154                         } else {
1155                                 //media_svc_debug("width : %d", content_info->media_meta.width);
1156                         }
1157
1158                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1159                         if (mmf_error != MM_ERROR_NONE) {
1160                                 SAFE_FREE(err_attr_name);
1161                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1162                         } else {
1163                                 //media_svc_debug("height : %d", content_info->media_meta.height);
1164                         }
1165
1166                 } else {
1167                         media_svc_error("Not support type");
1168                         return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1169                 }
1170
1171                 mmf_error = mm_file_destroy_content_attrs(content);
1172                 if (mmf_error != MM_ERROR_NONE) {
1173                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1174                 }
1175         } else {
1176                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1177         }
1178
1179         /*Get Content Tag attribute ===========*/
1180         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1181
1182         if (mmf_error == MM_ERROR_NONE) {
1183                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1184                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1185                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1186                         if(ret != MEDIA_INFO_ERROR_NONE)
1187                                 media_svc_error("strcpy error");
1188
1189                         //media_svc_debug("album[%d] : %s", size, content_info->media_meta.album);
1190                 } else {
1191                         SAFE_FREE(err_attr_name);
1192                         //media_svc_debug("album - unknown");
1193                 }
1194
1195                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1196                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1197                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1198                         if(ret != MEDIA_INFO_ERROR_NONE)
1199                                 media_svc_error("strcpy error");
1200                         //media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist);
1201                 } else {
1202                         SAFE_FREE(err_attr_name);
1203                         //media_svc_debug("artist - unknown");
1204                 }
1205
1206                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1207                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1208                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1209                         if(ret != MEDIA_INFO_ERROR_NONE)
1210                                 media_svc_error("strcpy error");
1211
1212                         //media_svc_debug("genre : %s", content_info->media_meta.genre);
1213                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1214                         /*
1215                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1216                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1217                         }
1218                         */
1219                 } else {
1220                         SAFE_FREE(err_attr_name);
1221                         //media_svc_debug("genre - unknown");
1222                 }
1223
1224                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1225                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* &&   (!isspace(*p))*/) {
1226                         if(!isspace(*p)) {
1227                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1228                                 if(ret != MEDIA_INFO_ERROR_NONE)
1229                                         media_svc_error("strcpy error");
1230
1231                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1232                                 //media_svc_debug("extract title from content : %s", content_info->media_meta.title);
1233                                 //media_svc_debug("^^^^^^^^^^^^^^^ path = %s, title = %s, size = %d ^^^^^^^^^^^^^^", content_info->path, content_info->media_meta.title, size);
1234                         }
1235                         else {
1236                                 int idx = 0;
1237
1238                                 for(idx = 0; idx < size; idx++) {
1239                                         if(isspace(*p)) {
1240                                                 media_svc_debug("SPACE [%s]", p);
1241                                                 p++;
1242                                                 continue;
1243                                         } else {
1244                                                 media_svc_debug("Not SPACE [%s]", p);
1245                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1246                                                 if(ret != MEDIA_INFO_ERROR_NONE)
1247                                                         media_svc_error("strcpy error");
1248                                                 break;
1249                                         }
1250                                 }
1251
1252                                 if(idx == size)
1253                                 {
1254                                         media_svc_debug("Can't extract title. All string is space");
1255                                         title = _media_svc_get_title_from_filepath(content_info->path);
1256                                         if (title) {
1257                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1258                                                 if(ret != MEDIA_INFO_ERROR_NONE)
1259                                                         media_svc_error("strcpy error");
1260                                                 SAFE_FREE(title);
1261                                         } else {
1262                                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1263                                         }
1264                                 }
1265                         }
1266                 } else {
1267                         SAFE_FREE(err_attr_name);
1268                         title = _media_svc_get_title_from_filepath(content_info->path);
1269                         if (title) {
1270                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1271                                 if(ret != MEDIA_INFO_ERROR_NONE)
1272                                         media_svc_error("strcpy error");
1273                                 SAFE_FREE(title);
1274                         } else {
1275                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1276                         }
1277                 }
1278
1279                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1280                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1281                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1282                         if(ret != MEDIA_INFO_ERROR_NONE)
1283                                 media_svc_error("strcpy error");
1284                         //media_svc_debug("desc : %s", content_info->media_meta.description);
1285                 } else {
1286                         SAFE_FREE(err_attr_name);
1287                 }
1288
1289                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1290                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1291                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1292                         if(ret != MEDIA_INFO_ERROR_NONE) {
1293                                 media_svc_error("strcpy error");
1294                         } else {
1295                                 /* This is same as datetaken */
1296                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, p);
1297                                 if(ret != MEDIA_INFO_ERROR_NONE)
1298                                         media_svc_error("strcpy error");
1299                         }
1300                         //media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date);
1301                 } else {
1302                         SAFE_FREE(err_attr_name);
1303                 }
1304
1305                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1306                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1307                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1308                         if(ret != MEDIA_INFO_ERROR_NONE)
1309                                 media_svc_error("strcpy error");
1310                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1311                         //media_svc_debug("extract composer from content : %s", content_info->media_meta.composer);
1312                 } else {
1313                         //media_svc_debug("composer - unknown");
1314                         SAFE_FREE(err_attr_name);
1315                 }
1316
1317                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1318                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1319                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1320                         if(ret != MEDIA_INFO_ERROR_NONE)
1321                                 media_svc_error("strcpy error");
1322                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1323                         //media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright);
1324                 } else {
1325                         //media_svc_debug("copyright - unknown");
1326                         SAFE_FREE(err_attr_name);
1327                 }
1328
1329                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1330                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1331                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1332                         if(ret != MEDIA_INFO_ERROR_NONE)
1333                                 media_svc_error("strcpy error");
1334                 } else {
1335                         SAFE_FREE(err_attr_name);
1336                 }
1337
1338                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1339                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == MM_ERROR_NONE) && (size == 4)) {
1340                         int year = 0;
1341                         if((p != NULL) && (sscanf( p, "%d", &year))) {
1342                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1343                                 if(ret != MEDIA_INFO_ERROR_NONE)
1344                                         media_svc_error("strcpy error");
1345                         } else {
1346                                 media_svc_debug("Wrong Year Information [%s]", p);
1347                         }
1348                 } else {
1349                         SAFE_FREE(err_attr_name);
1350                 }
1351
1352                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1353                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1354                         content_info->media_meta.rating = atoi(p);
1355                 } else {
1356                         SAFE_FREE(err_attr_name);
1357                         content_info->media_meta.rating = 0;
1358                 }
1359
1360                 if((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1361                         /*Initialize album_id to 0. below code will set the album_id*/
1362                         content_info->album_id = album_id;
1363                         ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1364         
1365                         if (ret != MEDIA_INFO_ERROR_NONE) {
1366                                 if (ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
1367                                         media_svc_debug("album does not exist. So start to make album art");
1368                                         extract_thumbnail = TRUE;
1369                                         append_album = TRUE;
1370                                 } else {
1371                                         extract_thumbnail = FALSE;
1372                                         append_album = FALSE;
1373                                 }
1374                         } else {
1375                                 content_info->album_id = album_id;
1376                                 append_album = FALSE;
1377         
1378                                 if((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1379                                         (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1380         
1381                                         media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1382                                         extract_thumbnail = TRUE;
1383                                 } else {
1384         
1385                                         media_svc_debug("album already exists. don't need to make album art");
1386                                         ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1387                                         media_svc_retv_del_if((ret != MEDIA_INFO_ERROR_NONE) && (ret != MEDIA_INFO_ERROR_DATABASE_NO_RECORD), ret, content_info);
1388                                         extract_thumbnail = FALSE;
1389                                 }
1390                         }
1391         
1392                         if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE)) {
1393                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1394                                 if (mmf_error != MM_ERROR_NONE) {
1395                                         media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1396                                         SAFE_FREE(err_attr_name);
1397                                 } else {
1398                                         //media_svc_debug("artwork size1 [%d]", size);
1399                                 }
1400         
1401                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1402                                 if (mmf_error != MM_ERROR_NONE) {
1403                                         media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1404                                         SAFE_FREE(err_attr_name);
1405                                 } else {
1406                                         //media_svc_debug("artwork size2 [%d]", size);
1407                                 }
1408                                 if (image != NULL && size > 0) {
1409                                         bool result = FALSE;
1410                                         int ret = MEDIA_INFO_ERROR_NONE;
1411                                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1412                                         int artwork_mime_size = -1;
1413         
1414                                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1415                                         if ((mmf_error == MM_ERROR_NONE) && (artwork_mime_size > 0)) {
1416                                                 result = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1417                                                 if (result == FALSE) {
1418                                                         media_svc_error("Fail to Get Thumbnail Path");
1419                                                 }
1420                                         } else {
1421                                                 SAFE_FREE(err_attr_name);
1422                                         }
1423         
1424                                         if(strlen(thumb_path) > 0)
1425                                         {
1426                                                 ret = _media_svc_save_image(image, size, thumb_path, uid);
1427                                                 if (ret != MEDIA_INFO_ERROR_NONE) {
1428                                                         media_svc_error("Fail to Save Thumbnail Image");
1429                                                 }
1430                                                 else {
1431                                                         ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1432                                                         if(ret != MEDIA_INFO_ERROR_NONE)
1433                                                                 media_svc_error("strcpy error");
1434                                                 }
1435                                         }
1436                                 }
1437                         }
1438         
1439                         if(append_album == TRUE) {
1440         
1441                                 if((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1442                                         (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1443                                         ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1444                                 else
1445                                         ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1446         
1447                                 content_info->album_id = album_id;
1448                         }
1449                 } else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1450                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
1451                         if (mmf_error == MM_ERROR_NONE) {
1452                                 if (gps_value == 0.0)
1453                                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1454                                 else
1455                                         content_info->media_meta.longitude = gps_value;
1456                         } else {
1457                                 SAFE_FREE(err_attr_name);
1458                         }
1459
1460                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
1461                         if (mmf_error == MM_ERROR_NONE) {
1462                                 if (gps_value == 0.0)
1463                                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1464                                 else
1465                                         content_info->media_meta.latitude = gps_value;
1466                         } else {
1467                                 SAFE_FREE(err_attr_name);
1468                         }
1469
1470                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
1471                         if (mmf_error == MM_ERROR_NONE) {
1472                                 if (gps_value == 0.0)
1473                                         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1474                                 else
1475                                         content_info->media_meta.altitude = gps_value;
1476                         } else {
1477                                 SAFE_FREE(err_attr_name);
1478                         }
1479 #if 0
1480                         //if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE))
1481                         {
1482                                 /* Extracting thumbnail */
1483                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1484                                 int width = 0;
1485                                 int height = 0;
1486
1487                                 ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
1488                                 if (ret < 0) {
1489                                         media_svc_error("thumbnail_request_from_db failed: %d", ret);
1490                                 } else {
1491                                         //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
1492                                 }
1493
1494                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1495                                 if(ret != MEDIA_INFO_ERROR_NONE)
1496                                         media_svc_error("strcpy error");
1497
1498                                 if (content_info->media_meta.width <= 0) content_info->media_meta.width = width;
1499                                 if (content_info->media_meta.height <= 0) content_info->media_meta.height = height;
1500                         }
1501 #endif
1502                 }
1503
1504                 mmf_error = mm_file_destroy_tag_attrs(tag);
1505                 if (mmf_error != MM_ERROR_NONE) {
1506                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1507                 }
1508         }       else {
1509                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1510                 char *title = NULL;
1511                 media_svc_error("no tag information");
1512
1513                 title = _media_svc_get_title_from_filepath(content_info->path);
1514                 if (title) {
1515                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1516                         if(ret != MEDIA_INFO_ERROR_NONE)
1517                                 media_svc_error("strcpy error");
1518                         SAFE_FREE(title);
1519                 } else {
1520                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1521                 }
1522
1523                 content_info->album_id = album_id;
1524         }
1525
1526         return MEDIA_INFO_ERROR_NONE;
1527 }
1528
1529 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1530 {
1531         media_svc_retm_if(content_info == NULL, "content info is NULL");
1532
1533         /* Delete media_svc_content_info_s */
1534         SAFE_FREE(content_info->media_uuid);
1535         SAFE_FREE(content_info->path);
1536         SAFE_FREE(content_info->file_name);
1537         SAFE_FREE(content_info->mime_type);
1538         SAFE_FREE(content_info->folder_uuid);
1539         SAFE_FREE(content_info->thumbnail_path);
1540
1541         /* Delete media_svc_content_meta_s */
1542         SAFE_FREE(content_info->media_meta.title);
1543         SAFE_FREE(content_info->media_meta.album);
1544         SAFE_FREE(content_info->media_meta.artist);
1545         SAFE_FREE(content_info->media_meta.album_artist);
1546         SAFE_FREE(content_info->media_meta.genre);
1547         SAFE_FREE(content_info->media_meta.composer);
1548         SAFE_FREE(content_info->media_meta.year);
1549         SAFE_FREE(content_info->media_meta.recorded_date);
1550         SAFE_FREE(content_info->media_meta.copyright);
1551         SAFE_FREE(content_info->media_meta.track_num);
1552         SAFE_FREE(content_info->media_meta.description);
1553         SAFE_FREE(content_info->media_meta.datetaken);
1554         SAFE_FREE(content_info->media_meta.weather);
1555
1556         SAFE_FREE(content_info->media_meta.title_pinyin);
1557         SAFE_FREE(content_info->media_meta.album_pinyin);
1558         SAFE_FREE(content_info->media_meta.artist_pinyin);
1559         SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1560         SAFE_FREE(content_info->media_meta.genre_pinyin);
1561         SAFE_FREE(content_info->media_meta.composer_pinyin);
1562         SAFE_FREE(content_info->media_meta.copyright_pinyin);
1563         SAFE_FREE(content_info->media_meta.description_pinyin);
1564
1565         return;
1566 }
1567
1568 int _media_svc_get_store_type_by_path(const char *path, media_svc_storage_type_e *storage_type)
1569 {
1570         if(STRING_VALID(path))
1571         {
1572                 if(strncmp(path, MEDIA_SVC_PATH_PHONE, strlen(MEDIA_SVC_PATH_PHONE)) == 0)
1573                 {
1574                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
1575                 }
1576                 else if(strncmp (path, MEDIA_SVC_PATH_MMC, strlen(MEDIA_SVC_PATH_MMC)) == 0)
1577                 {
1578                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
1579                 }
1580         }
1581         else
1582         {
1583                 media_svc_error("INVALID parameter");
1584                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1585         }
1586
1587         return MEDIA_INFO_ERROR_NONE;
1588 }
1589
1590 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
1591 {
1592   char *result, *sr;
1593   size_t i, count = 0;
1594   size_t oldlen = strlen(olds); if (oldlen < 1) return s;
1595   size_t newlen = strlen(news);
1596
1597   if (newlen != oldlen) {
1598     for (i = 0; s[i] != '\0';) {
1599       if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
1600       else i++;
1601     }   
1602   } else i = strlen(s);
1603
1604
1605   result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
1606   if (result == NULL) return NULL;
1607
1608   sr = result;
1609   while (*s) {
1610     if (memcmp(s, olds, oldlen) == 0) {
1611       memcpy(sr, news, newlen);
1612       sr += newlen;
1613       s  += oldlen;
1614     } else *sr++ = *s++;
1615   }
1616
1617   *sr = '\0';
1618
1619   return result;
1620 }
1621
1622 int _media_svc_error_convert(int error)
1623 {
1624         media_svc_debug("error : [%d]", error);
1625
1626         if(error == MS_MEDIA_ERR_NONE)                                                  /*Error None*/
1627                 return MEDIA_INFO_ERROR_NONE;
1628         else if(error == MS_MEDIA_ERR_DB_CONNECT_FAIL)                  /*DB Connect Fail*/
1629                 return MEDIA_INFO_ERROR_DATABASE_CONNECT;
1630         else if(error == MS_MEDIA_ERR_DB_DISCONNECT_FAIL)               /*DB Disconnect Fail*/
1631                 return MEDIA_INFO_ERROR_DATABASE_DISCONNECT;
1632         else if(error == MS_MEDIA_ERR_SOCKET_CONN)                              /*Socket Connect Fail*/
1633                 return MEDIA_INFO_ERROR_SOCKET_CONN;
1634         else if(error == MS_MEDIA_ERR_INVALID_PARAMETER || error == MS_MEDIA_ERR_INVALID_PATH)
1635                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1636
1637         return MEDIA_INFO_ERROR_INTERNAL;
1638 }
1639
1640
1641 bool _media_svc_is_drm_file(const char *path)
1642 {
1643 #ifdef __SUPPORT_DRM
1644         int ret;
1645         drm_bool_type_e is_drm_file = DRM_UNKNOWN;
1646
1647         ret = drm_is_drm_file(path,&is_drm_file);
1648         if(DRM_RETURN_SUCCESS == ret && DRM_TRUE == is_drm_file)
1649                 return TRUE;
1650 #endif
1651         return FALSE;
1652 }
1653
1654 int _media_svc_get_mime_in_drm_info(const char *path, char *mime, drm_content_info_s **drm_contentInfo)
1655 {
1656 #ifdef __SUPPORT_DRM
1657         int ret = MEDIA_INFO_ERROR_NONE;
1658         drm_file_type_e file_type = DRM_TYPE_UNDEFINED;
1659
1660         if (path == NULL || mime == NULL)
1661                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1662
1663         ret = drm_get_file_type(path, &file_type);
1664         if (ret != DRM_RETURN_SUCCESS) {
1665                 media_svc_error("drm_get_file_type() failed : %d", ret);
1666                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1667         } else {
1668                 if (file_type == DRM_TYPE_OMA_V1
1669                 || file_type == DRM_TYPE_OMA_V2
1670                 || file_type == DRM_TYPE_OMA_PD) {
1671                         *drm_contentInfo = malloc(sizeof(drm_content_info_s));
1672                         memset(*drm_contentInfo,0x0,sizeof(drm_content_info_s));
1673                         ret = drm_get_content_info(path, *drm_contentInfo);
1674                         if (ret != DRM_RETURN_SUCCESS) {
1675                                 media_svc_error("drm_svc_get_content_info() fails :%d ", ret);
1676                                 free(*drm_contentInfo);
1677                                 *drm_contentInfo = NULL;
1678                                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1679                         }
1680
1681                         if (STRING_VALID((*drm_contentInfo)->mime_type)) {
1682                                 strncpy(mime,(*drm_contentInfo)->mime_type, MEDIA_SVC_METADATA_LEN_MAX);
1683                                 media_svc_debug("DRM contentType : %s",(*drm_contentInfo)->mime_type);
1684                         } else {
1685                                 free(*drm_contentInfo);
1686                                 *drm_contentInfo = NULL;
1687                                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1688                         }
1689                 } else {
1690                         media_svc_error("THIS IS DRM BUT YOU SHOULD USE API OF AUL LIBRARY");
1691                         *drm_contentInfo = NULL;
1692                         return MEDIA_INFO_ERROR_INVALID_MEDIA;
1693                 }
1694         }
1695
1696         return MEDIA_INFO_ERROR_NONE;
1697 #else
1698         *drm_contentInfo = NULL;
1699         return MEDIA_INFO_ERROR_INVALID_MEDIA;
1700 #endif
1701 }
1702
1703 int _media_svc_get_content_type_from_mime(const char * path, const char * mimetype, int * category)
1704 {
1705         int i = 0;
1706         int err = 0;
1707
1708         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
1709
1710         //media_svc_debug("mime type : %s", mimetype);
1711
1712         /*categorize from mimetype */
1713         for (i = 0; i < CONTENT_TYPE_NUM; i++) {
1714                 if (strstr(mimetype, content_category[i].content_type) != NULL) {
1715                         *category = (*category | content_category[i].category_by_mime);
1716                         break;
1717                 }
1718         }
1719
1720         /*in application type, exitst sound file ex) x-smafs */
1721         if (*category & MEDIA_SVC_CATEGORY_ETC) {
1722                 int prefix_len = strlen(content_category[0].content_type);
1723
1724                 for (i = 0; i < SOUND_MIME_NUM; i++) {
1725                         if (strstr(mimetype + prefix_len, sound_mime_table[i]) != NULL) {
1726                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
1727                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
1728                                 break;
1729                         }
1730                 }
1731         }
1732
1733         /*check music file in soun files. */
1734         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
1735                 int prefix_len = strlen(content_category[0].content_type) + 1;
1736
1737                 //media_svc_error("mime_type : %s", mimetype + prefix_len);
1738
1739                 for (i = 0; i < MUSIC_MIME_NUM; i++) {
1740                         if (strcmp(mimetype + prefix_len, music_mime_table[i]) == 0) {
1741                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
1742                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1743                                 break;
1744                         }
1745                 }
1746
1747                 /*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*/
1748                 if(strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
1749                         *category ^= MEDIA_SVC_CATEGORY_SOUND;
1750                         *category |= MEDIA_SVC_CATEGORY_ETC;
1751                 }
1752         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
1753                 /*some video files don't have video stream. in this case it is categorize as music. */
1754                 char *ext;
1755                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
1756                 ext = strrchr(path, '.');
1757                 if (ext != NULL) {
1758                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
1759                                 int audio = 0;
1760                                 int video = 0;
1761
1762                                 err = mm_file_get_stream_info(path, &audio, &video);
1763                                 if (err == 0) {
1764                                         if (audio > 0 && video == 0) {
1765                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
1766                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1767                                         }
1768                                 }
1769                         }
1770                 }
1771         }
1772
1773         //media_svc_debug("category_from_ext : %d", *category);
1774
1775         return err;
1776 }
1777
1778 /*
1779 drm_contentifo is not NULL, if the file is OMA DRM.
1780 If the file is not OMA DRM, drm_contentinfo must be NULL.
1781 */
1782 int _media_svc_get_mime_type(const char *path, char *mimetype, drm_bool_type_e *is_drm, drm_content_info_s **drm_contentInfo)
1783 {
1784         int ret = MEDIA_INFO_ERROR_NONE;
1785
1786         if (path == NULL)
1787                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1788
1789 #ifdef __SUPPORT_DRM
1790         /* In case of drm file. */
1791         if (_media_svc_is_drm_file(path)) {
1792                 *is_drm = DRM_TRUE;
1793                 ret =  _media_svc_get_mime_in_drm_info(path, mimetype, drm_contentInfo);
1794                 if (ret == MEDIA_INFO_ERROR_NONE) {
1795                         return ret;
1796                 }
1797         }
1798 #else
1799         *is_drm = DRM_FALSE;
1800         *drm_contentInfo = NULL;
1801 #endif
1802
1803         /*in case of normal files or failure to get mime in drm */
1804         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
1805                 media_svc_error("aul_get_mime_from_file fail");
1806                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1807         }
1808
1809         return MEDIA_INFO_ERROR_NONE;
1810 }
1811
1812 int _media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
1813 {
1814         int ret = MEDIA_INFO_ERROR_NONE;
1815         int category = 0;
1816
1817         media_svc_media_type_e type;
1818
1819         ret = _media_svc_get_content_type_from_mime(path, mime_type, &category);
1820         if (ret < 0) {
1821                 media_svc_error("_media_svc_get_content_type_from_mime failed : %d", ret);
1822         }
1823
1824         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1825         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
1826         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
1827         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
1828         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1829
1830         *media_type = type;
1831
1832         return ret;
1833 }
1834
1835 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1836 {
1837         int ret = MEDIA_INFO_ERROR_NONE;
1838         int size = 0;
1839         pinyin_name_s *pinyinname = NULL;
1840         *pinyin_str = NULL;
1841
1842         if(!STRING_VALID(src_str))
1843         {
1844                 media_svc_debug("String is invalid");
1845                 return ret;
1846         }
1847
1848         ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1849         if (ret == MEDIA_INFO_ERROR_NONE)
1850         {
1851                 if(STRING_VALID(pinyinname[0].pinyin_name))
1852                         *pinyin_str = strdup(pinyinname[0].pinyin_name);
1853                 else
1854                         *pinyin_str = strdup(src_str);  //Return Original Non China Character
1855         }
1856
1857         _media_svc_pinyin_free(pinyinname, size);
1858
1859         return ret;
1860 }
1861
1862 bool _media_svc_check_pinyin_support(void)
1863 {
1864         /*Check CSC*/
1865         return TRUE;
1866 }