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