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