exclude drm client APIs
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include <string.h>
26 #include <sys/vfs.h>
27 #include <glib/gstdio.h>
28 #include <sys/stat.h>
29 #include <string.h>
30 #include <dirent.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <aul/aul.h>
34 #include <mm_file.h>
35 #include <mm_error.h>
36 #include <libexif/exif-data.h>
37 #include <media-thumbnail.h>
38 #include <media-util.h>
39 #include "uuid.h"
40 #include "media-svc-util.h"
41 #include "media-svc-error.h"
42 #include "media-svc-debug.h"
43 #include "media-svc-env.h"
44 #include "media-svc-hash.h"
45 #include "media-svc-album.h"
46
47
48 #define MEDIA_SVC_FILE_EXT_LEN_MAX                              6                       /**<  Maximum file ext lenth*/
49
50 typedef enum {
51         MEDIA_SVC_EXTRACTED_FIELD_NONE                  = 0x00000001,
52         MEDIA_SVC_EXTRACTED_FIELD_TITLE                         = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
53         MEDIA_SVC_EXTRACTED_FIELD_DESC                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
54         MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT             = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
55         MEDIA_SVC_EXTRACTED_FIELD_AUTHOR                = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
56         MEDIA_SVC_EXTRACTED_FIELD_ARTIST                        = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
57         MEDIA_SVC_EXTRACTED_FIELD_GENRE                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
58         MEDIA_SVC_EXTRACTED_FIELD_ALBUM                 = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
59         MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
60         MEDIA_SVC_EXTRACTED_FIELD_YEAR                  = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
61         MEDIA_SVC_EXTRACTED_FIELD_CATEGORY              = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
62 } media_svc_extracted_field_e;
63
64 #if 0
65 static char *__year_2_str(int year);
66
67 static char *__year_2_str(int year)
68 {
69         static char ret[MEDIA_SVC_METADATA_LEN_MAX];
70
71         if (year == -1 || year == 0) {
72                 _strncpy_safe(ret, MEDIA_SVC_TAG_UNKNOWN, MEDIA_SVC_METADATA_LEN_MAX);
73         } else {
74                 snprintf(ret, MEDIA_SVC_METADATA_LEN_MAX - 1, "%d", year);
75         }
76         return ret;
77 }
78 #endif
79
80 char *_media_info_generate_uuid(void)
81 {
82         uuid_t uuid_value;
83         static char uuid_unparsed[50];  
84
85         uuid_generate(uuid_value);
86         uuid_unparse(uuid_value, uuid_unparsed);
87
88         //media_svc_debug("UUID : %s", uuid_unparsed);
89         return uuid_unparsed;
90 }
91
92 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
93 {
94         if (!x_src || strlen(x_src) == 0) {
95                 media_svc_error("x_src is NULL");
96                 return;
97         }
98
99         if (max_len < 1) {
100                 media_svc_error("length is Wrong");
101                 return;
102         }
103
104     strncpy(x_dst, x_src, max_len-1);
105         x_dst[max_len-1] = '\0';
106 }
107
108 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
109 {
110         int len = 0;
111
112         if (!STRING_VALID(src)) {
113                 media_svc_error("invalid src");
114                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
115         }
116
117         SAFE_FREE(*dst);
118
119         len = strlen(src) + 1;
120         *dst = malloc(len);
121
122         if (*dst == NULL) {
123                 media_svc_error("malloc failed");
124                 return MEDIA_INFO_ERROR_INTERNAL;
125         }
126
127         strncpy(*dst, src, len);
128         char *p = *dst;
129         p[len - 1] = '\0';
130
131         return MEDIA_INFO_ERROR_NONE;
132 }
133
134 static void __media_svc_split_to_double(char *input, double *arr, int *num)
135 {
136         char tmp_arr[255] = { 0, };
137         int len = strlen(input);
138         int i = 0, idx = 0, tmp_idx = 0;
139         int is_prev_space = 0;
140
141         for (;;) {
142                 if (input[len - 1] == ' ') {
143                         len--;
144                 } else {
145                         break;
146                 }
147         }
148
149         for (i = 0; i < len; i++) {
150                 if (idx > 2) {
151                         break;
152                 }
153
154                 if (input[i] == ' ') {
155                         if (is_prev_space == 1) {
156                                 continue;
157                         }
158                         if (idx <= 2) {
159                                 arr[idx++] = atof(tmp_arr);
160                         }
161                         tmp_idx = 0;
162                         is_prev_space = 1;
163                         continue;
164                 }
165
166                 tmp_arr[tmp_idx] = input[i];
167                 tmp_arr[++tmp_idx] = '\0';
168                 is_prev_space = 0;
169         }
170
171         if (i == len) {
172                 if (idx <= 2) {
173                         arr[idx++] = atof(tmp_arr);
174                 }
175                 *num = idx;
176                 return;
177         } else {
178                 *num = idx--;
179                 return;
180         }
181 }
182
183 static int __media_svc_get_exif_info(ExifData *ed,
184                                                                                 char *buf,
185                                                                                 int *i_value,
186                                                                                 double *d_value,
187                                                                                 int ifdtype,
188                                                                                 long tagtype)
189 {
190         ExifEntry *entry;
191         ExifIfd ifd;
192         ExifTag tag;
193
194         if (ed == NULL) {
195                 //media_svc_debug("ExifData is NULL");
196                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
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 MEDIA_INFO_ERROR_INVALID_PARAMETER;
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 || tag == EXIF_TAG_GPS_ALTITUDE) {
220
221                         if (d_value == NULL) {
222                                 media_svc_error("d_value is NULL");
223                                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
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 MEDIA_INFO_ERROR_INTERNAL;
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 MEDIA_INFO_ERROR_INVALID_PARAMETER;
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("Old : [%s] New : [%s] errno : [%s]", old_name, new_name, 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         if (!path) {
390                 media_svc_error("path is NULL");
391                 return NULL;
392         }
393
394         filename = g_path_get_basename(path);
395         if (!STRING_VALID(filename)) {
396                 media_svc_error("wrong file name");
397                 SAFE_FREE(filename);
398                 return NULL;
399         }
400
401         filename_len = strlen(filename);
402
403         ext = g_strrstr(filename, ".");
404         if (!ext) {
405                 media_svc_error("there is no file extention");
406                 return filename;
407         }
408
409         new_title_len = filename_len - strlen(ext);
410         if (new_title_len < 1) {
411                 media_svc_error("title length is zero");
412                 SAFE_FREE(filename);
413                 return NULL;
414         }
415
416         title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
417
418         SAFE_FREE(filename);
419
420         media_svc_debug("extract title is [%s]", title);
421
422         return title;
423 }
424
425 int _media_svc_save_image(void *image, int size, char *image_path)
426 {
427         media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
428
429         if (!image) {
430                 media_svc_error("invalid image..");
431                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
432         }
433
434         struct statfs fs;
435         if (-1 == statfs(MEDIA_SVC_THUMB_PATH_PREFIX, &fs)) {
436                 media_svc_error("error in statfs");
437                 return MEDIA_INFO_ERROR_INTERNAL;
438         }
439
440         long bsize_kbytes = fs.f_bsize >> 10;
441
442         if ((bsize_kbytes * fs.f_bavail) < 1024) {
443                 media_svc_error("not enought space...");
444                 return MEDIA_INFO_ERROR_INTERNAL;
445         }
446
447         FILE *fp = NULL;
448         int nwrite = -1;
449         if (image != NULL && size > 0) {
450                 fp = fopen(image_path, "w");
451
452                 if (fp == NULL) {
453                         media_svc_error("failed to open file");
454                         return MEDIA_INFO_ERROR_INTERNAL;
455                 }
456
457                 nwrite = fwrite(image, 1, size, fp);
458                 if (nwrite != size) {
459                         media_svc_error("failed to write thumbnail");
460                         fclose(fp);
461                         return MEDIA_INFO_ERROR_INTERNAL;
462                 }
463                 fclose(fp);
464         }
465
466         return MEDIA_INFO_ERROR_NONE;
467 }
468
469 bool _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format)
470 {
471         char savename[MEDIA_SVC_PATHNAME_SIZE] = {0};
472         char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0};
473         char *thumb_dir = NULL;
474         char hash[255 + 1];
475         char *thumbfile_ext = NULL;
476
477         thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? MEDIA_SVC_THUMB_INTERNAL_PATH : MEDIA_SVC_THUMB_EXTERNAL_PATH;
478
479         memset(file_ext, 0, sizeof(file_ext));
480         if (!_media_svc_get_file_ext(pathname, file_ext)) {
481                 media_svc_error("get file ext fail");
482         }
483
484         int err = -1;
485         err = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
486         if (err < 0) {
487                 media_svc_error("mb_svc_generate_hash_code failed : %d", err);
488                 return FALSE;
489         }
490
491         //media_svc_debug("img format is [%s]", img_format);
492
493         if((strstr(img_format, "jpeg") != NULL) ||(strstr(img_format, "jpg") != NULL) ||(strstr(img_format, "JPG") != NULL)) {
494                 thumbfile_ext = "jpg";
495         } else if((strstr(img_format, "png") != NULL) ||(strstr(img_format, "PNG") != NULL)) {
496                 thumbfile_ext = "png";
497         } else if((strstr(img_format, "gif") != NULL) ||(strstr(img_format, "GIF") != NULL)) {
498                 thumbfile_ext = "gif";
499         } else if((strstr(img_format, "bmp") != NULL) ||(strstr(img_format, "BMP") != NULL)) {
500                 thumbfile_ext = "bmp";
501         } else {
502                 media_svc_error("Not proper img format");
503                 return FALSE;
504         }
505
506         snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
507         _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
508         //media_svc_debug("thumb_path is [%s]", thumb_path);
509
510         return TRUE;
511 }
512
513 bool _media_svc_get_file_ext(const char *file_path, char *file_ext)
514 {
515         int i = 0;
516
517         for (i = strlen(file_path); i >= 0; i--) {
518                 if (file_path[i] == '.') {
519                         _strncpy_safe(file_ext, &file_path[i+1], MEDIA_SVC_FILE_EXT_LEN_MAX);
520                         return true;
521                 }
522
523                 if (file_path[i] == '/') {
524                         return false;
525                 }
526         }
527         return false;
528 }
529
530 int _media_svc_get_file_time(const char *full_path)
531 {
532         struct stat statbuf;
533         int fd = 0;
534
535         memset(&statbuf, 0, sizeof(struct stat));
536         fd = stat(full_path, &statbuf);
537         if (fd == -1) {
538                  media_svc_error("stat(%s) fails.", full_path);
539                  return MEDIA_INFO_ERROR_INTERNAL;
540          }
541
542          return statbuf.st_mtime;
543 }
544
545 int _media_svc_set_media_info(media_svc_content_info_s *content_info, media_svc_storage_type_e storage_type,
546                           const char *path, media_svc_media_type_e *media_type, bool refresh, drm_content_info_s **drm_contentInfo)
547 {
548         int ret = MEDIA_INFO_ERROR_NONE;
549         char * media_uuid = NULL;
550         char * file_name = NULL;
551         struct stat st;
552         drm_bool_type_e drm_type = DRM_FALSE;
553         char mime_type[256] = {0};
554
555         ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
556         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
557
558         memset(&st, 0, sizeof(struct stat));
559         if (stat(path, &st) == 0) {
560                 content_info->modified_time = st.st_mtime;
561                 content_info->size = st.st_size;
562                 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
563         } else {
564                 media_svc_error("stat failed : %s", strerror(errno));
565         }
566
567         /* Set default GPS value before extracting meta information */
568         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
569         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
570         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
571
572         /* Set default value before extracting meta information */
573         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
574         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
575
576         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
577         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
578
579         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
580         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
581
582         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
583         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
584
585         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
586         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
587
588         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
589         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
590
591         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
592         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
593
594         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
595         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
596
597         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
598         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
599
600         /* refresh is TRUE when file modified. so only modified_time and size are changed*/
601         if(refresh) {
602                 media_svc_debug("refresh");
603                 return MEDIA_INFO_ERROR_NONE;
604         }
605
606         content_info->storage_type = storage_type;
607         time(&content_info->added_time);
608
609         media_uuid = _media_info_generate_uuid();
610         media_svc_retvm_if(media_uuid == NULL, MEDIA_INFO_ERROR_INTERNAL, "Invalid UUID");
611
612         ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
613         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
614
615         file_name = g_path_get_basename(path);
616         ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
617         SAFE_FREE(file_name);
618         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
619
620         /* if the file is DRM file, drm_type value is DRM_TRUE(1).
621         if drm_contentinfo is not NULL, the file is OMA DRM.*/
622         ret = _media_svc_get_mime_type(path, mime_type, &drm_type, drm_contentInfo);
623         if (ret < 0) {
624                 media_svc_error("media_svc_get_mime_type failed : %d (%s)", ret, path);
625                 return MEDIA_INFO_ERROR_INVALID_PATH;
626         }
627
628         media_svc_error("mime [%s]", mime_type);
629         content_info->is_drm = drm_type;
630
631         ret = _media_svc_get_media_type(path, mime_type, media_type);
632         media_svc_retv_if(ret != MEDIA_INFO_ERROR_NONE, ret);
633         if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
634                 media_svc_error("invalid media_type condition[%d]", *media_type);
635                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
636         }
637
638         ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
639         media_svc_retv_del_if(ret != MEDIA_INFO_ERROR_NONE, ret, content_info);
640
641         media_svc_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
642
643         content_info->media_type = *media_type;
644
645         content_info->played_count = 0;
646         content_info->last_played_time= 0;
647         content_info->last_played_position= 0;
648         content_info->favourate= 0;
649         content_info->media_meta.rating = 0;
650
651         return MEDIA_INFO_ERROR_NONE;
652 }
653
654 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
655 {
656         if (content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
657                 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
658                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
659         }
660
661         char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
662         char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
663         memset(buf, 0x00, sizeof(buf));
664         memset(description_buf, 0x00, sizeof(description_buf));
665
666         int ret = MEDIA_INFO_ERROR_NONE;
667         double value = 0.0;
668         int orient_value = 0;
669         int exif_width = 0;
670         int exif_height = 0;
671         ExifData *ed = NULL;
672
673         char *path = content_info->path;
674         if (path == NULL) {
675                 media_svc_error("path is NULL");
676                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
677         }
678
679         /* Load an ExifData object from an EXIF file */
680         ed = exif_data_new_from_file(path);
681
682         if (!ed) {
683                 media_svc_debug("There is no exif data in [ %s ]", path);
684         }
685
686         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_GPS_LATITUDE_REF) == MEDIA_INFO_ERROR_NONE) {
687                 if (strlen(buf) != 0) {
688                         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE) == MEDIA_INFO_ERROR_NONE) {
689
690                                 if (strcmp(buf, "S") == 0) {
691                                         value = -1 * value;
692                                 }
693
694                                 content_info->media_meta.latitude = value;
695                         } else {
696                                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
697                         }
698                 } else {
699                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
700                 }
701         } else {
702                 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
703         }
704
705         memset(buf, 0x00, sizeof(buf));
706
707         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_GPS_LONGITUDE_REF) == MEDIA_INFO_ERROR_NONE) {
708                 if (strlen(buf) != 0) {
709                         if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE) == MEDIA_INFO_ERROR_NONE) {
710                                 if (strcmp(buf, "W") == 0) {
711                                         value = -1 * value;
712                                 }
713                                 content_info->media_meta.longitude = value;
714                         } else {
715                                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
716                         }
717                 } else {
718                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
719                 }
720         } else {
721                 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
722         }
723
724         memset(buf, 0x00, sizeof(buf));
725
726         if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION) == MEDIA_INFO_ERROR_NONE) {
727                 if (strlen(description_buf) == 0) {
728                         //media_svc_debug("Use 'No description'");
729                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
730                         if(ret != MEDIA_INFO_ERROR_NONE)
731                                 media_svc_error("strcpy error");
732                 } else {
733                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
734                         if(ret != MEDIA_INFO_ERROR_NONE)
735                                 media_svc_error("strcpy error");
736                 }
737         } else {
738                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
739                 if(ret != MEDIA_INFO_ERROR_NONE)
740                         media_svc_error("strcpy error");
741         }
742
743         memset(buf, 0x00, sizeof(buf));
744
745         if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME) == MEDIA_INFO_ERROR_NONE) {
746                 if (strlen(buf) == 0) {
747                         //media_svc_debug("time  is NULL");
748                 } else {
749                         //media_svc_debug("time  is %s", buf);
750                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, buf);
751                         if(ret != MEDIA_INFO_ERROR_NONE)
752                                 media_svc_error("strcpy error");
753                 }
754         }
755
756         /* Get orientation value from exif. */
757         if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_IFD_0, EXIF_TAG_ORIENTATION) == MEDIA_INFO_ERROR_NONE) {
758                 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
759                         content_info->media_meta.orientation = orient_value;
760                 } else {
761                         content_info->media_meta.orientation = 0;
762                 }
763         } else {
764                 content_info->media_meta.orientation = 0;
765         }
766
767         /* Get width value from exif. */
768         if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION) == MEDIA_INFO_ERROR_NONE) {
769                 if (exif_width > 0) {
770                         content_info->media_meta.width = exif_width;
771                 } else {
772                         content_info->media_meta.width = 0;
773                 }
774         } else {
775                 content_info->media_meta.width = 0;
776         }
777
778         /* Get height value from exif. */
779         if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION) == MEDIA_INFO_ERROR_NONE) {
780                 if (exif_height > 0) {
781                         content_info->media_meta.height = exif_height;
782                 } else {
783                         content_info->media_meta.height = 0;
784                 }
785         } else {
786                 content_info->media_meta.height = 0;
787         }
788
789         if (ed != NULL) exif_data_unref(ed);
790 #if 0
791         /* Extracting thumbnail */
792         char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
793         int width = 0;
794         int height = 0;
795
796         ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
797         if (ret < 0) {
798                 media_svc_error("thumbnail_request_from_db failed: %d", ret);
799         } else {
800                 //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
801         }
802
803         content_info->media_meta.width = width;
804         content_info->media_meta.height = height;
805
806         if (STRING_VALID(thumb_path))
807                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
808         else
809                 content_info->thumbnail_path = NULL;
810
811         if(ret != MEDIA_INFO_ERROR_NONE)
812                 media_svc_error("strcpy error");
813 #endif
814         return MEDIA_INFO_ERROR_NONE;
815 }
816
817 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type, drm_content_info_s *drm_contentInfo)
818 {
819         MMHandleType content = 0;
820         MMHandleType tag = 0;
821         char *p = NULL;
822         void *image = NULL;
823         int size = -1;
824         int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
825         int mmf_error = MM_ERROR_NONE;
826         bool thumb_extracted_from_drm = FALSE;
827         char *err_attr_name = NULL;
828         char *title = NULL;
829         bool extract_thumbnail = FALSE;
830         bool append_album = FALSE;
831         int album_id = 0;
832         double gps_value = 0.0;
833         int ret = MEDIA_INFO_ERROR_NONE;
834         char *path = content_info->path;
835
836 #ifdef __SUPPORT_DRM
837         /*To do - code for DRM content*/
838         if (content_info->is_drm) {
839                 drm_file_type_e drm_file_type;
840
841                 ret = drm_get_file_type(path, &drm_file_type);
842                 if (ret < 0) {
843                         media_svc_error("drm_get_file_type falied : %d", ret);
844                         drm_file_type = DRM_TYPE_UNDEFINED;
845                 }
846
847                 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
848                 if (drm_contentInfo != NULL) {
849                         if (drm_file_type == DRM_TYPE_OMA_V1) {
850                                 if (strlen(drm_contentInfo->title) > 0) {
851                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, drm_contentInfo->title);
852                                         if(ret != MEDIA_INFO_ERROR_NONE)
853                                                 media_svc_error("strcpy error");
854                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
855                                 }
856
857                                 if (strlen(drm_contentInfo->description) > 0) {
858                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, drm_contentInfo->description);
859                                         if(ret != MEDIA_INFO_ERROR_NONE)
860                                                 media_svc_error("strcpy error");
861                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
862                                 }
863                         } else if (drm_file_type == DRM_TYPE_OMA_V2) {
864                                 if (strlen(drm_contentInfo->title) > 0) {
865                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, drm_contentInfo->title);
866                                         if(ret != MEDIA_INFO_ERROR_NONE)
867                                                 media_svc_error("strcpy error");
868                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
869                                 }
870
871                                 if (strlen(drm_contentInfo->description) > 0) {
872                                         ret =  __media_svc_malloc_and_strncpy(&content_info->media_meta.description, drm_contentInfo->description);
873                                         if(ret != MEDIA_INFO_ERROR_NONE)
874                                                 media_svc_error("strcpy error");
875                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
876                                 }
877
878                                 if (strlen(drm_contentInfo->copyright) > 0) {
879                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, drm_contentInfo->copyright);
880                                         if(ret != MEDIA_INFO_ERROR_NONE)
881                                                 media_svc_error("strcpy error");
882                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT;
883                                 }
884
885                                 if (strlen(drm_contentInfo->author) > 0) {
886                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, drm_contentInfo->author);
887                                         if(ret != MEDIA_INFO_ERROR_NONE)
888                                                 media_svc_error("strcpy error");
889                                          ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, drm_contentInfo->author);
890                                         if(ret != MEDIA_INFO_ERROR_NONE)
891                                                 media_svc_error("strcpy error");
892
893                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
894                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_ARTIST;
895                                 }
896                         }
897
898                         if (!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) {
899                                 title = _media_svc_get_title_from_filepath(path);
900                                 if (title) {
901                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
902                                         SAFE_FREE(title);
903                                         if(ret != MEDIA_INFO_ERROR_NONE)
904                                                 media_svc_error("strcpy error");
905                                 } else {
906                                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
907                                 }
908                         }
909
910                         return MEDIA_INFO_ERROR_NONE;
911                 } else {
912                         media_svc_debug("Some Not OMA Content's metadata is not incrypted so fileinfo can extracted metadata");
913                 }
914         }
915 #endif
916 #if 0
917         if (drm_svc_is_drm_file(content_info->path)) {
918                 bool invalid_file = FALSE;
919
920                 DRM_FILE_TYPE type = drm_svc_get_drm_type(content_info->path);
921
922                 if (type == DRM_FILE_TYPE_OMA) {
923                         drm_dcf_header_t header_info;
924                         memset(&header_info, 0, sizeof(drm_dcf_header_t));
925                         media_svc_debug("drm type is OMA");
926
927                         if (drm_svc_get_dcf_header_info(content_info->path, &header_info) != DRM_RESULT_SUCCESS) {
928                                 media_svc_debug("cannot get dcf header info. just get the title");
929                                 title = _media_svc_get_title_from_filepath(content_info->path);
930                                 if (title) {
931                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
932                                         SAFE_FREE(title);
933                                         media_svc_retv_del_if(ret < 0, ret, content_info);
934                                         //_strncpy_safe(content_info->media_meta.title, title, sizeof(content_info->media_meta.title));
935                                 } else {
936                                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
937                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
938                                         media_svc_retv_del_if(ret < 0, ret, content_info);
939                                 }
940
941 /*
942                                 _strncpy_safe(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.album));
943                                 _strncpy_safe(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.artist));
944                                 _strncpy_safe(content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.genre));
945                                 _strncpy_safe(content_info->media_meta.author, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.author));
946                                 _strncpy_safe(content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.year));
947 */
948
949                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
950                                 media_svc_retv_del_if(ret < 0, ret, content_info);
951                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, 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.genre, 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.composer, 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.year, MEDIA_SVC_TAG_UNKNOWN);
958                                 media_svc_retv_del_if(ret < 0, ret, content_info);
959
960                                 return MEDIA_INFO_ERROR_NONE;
961                         }
962
963                         if (drm_svc_has_valid_ro(content_info->path, DRM_PERMISSION_PLAY) != DRM_RESULT_SUCCESS) {
964                                 media_svc_debug("no valid ro. can't extract meta data");
965                                 invalid_file = TRUE;
966                         }
967
968                         if (header_info.version == DRM_OMA_DRMV1_RIGHTS) {
969                                 media_svc_debug("DRM V1");
970                                 if (invalid_file) {
971
972                                         if (strlen(header_info.headerUnion.headerV1.contentName) > 0) {
973
974                                                 //_strncpy_safe(content_info->media_meta.title, header_info.headerUnion.headerV1.contentName, sizeof(content_info->media_meta.title));
975                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, header_info.headerUnion.headerV1.contentName);
976                                                 media_svc_retv_del_if(ret < 0, ret, content_info);
977
978                                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
979                                                 media_svc_debug("extract title from DCF");
980                                         }
981
982                                         if (strlen(header_info.headerUnion.headerV1.contentDescription) > 0) {
983                                                 //_strncpy_safe(content_info->media_meta.description, header_info.headerUnion.headerV1.contentDescription, sizeof(content_info->media_meta.description));
984                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, header_info.headerUnion.headerV1.contentDescription);
985                                                 media_svc_retv_del_if(ret < 0, ret, content_info);
986
987                                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
988                                                 media_svc_debug("extract description from DCF");
989                                         }
990                                 }
991                         } else if (header_info.version == DRM_OMA_DRMV2_RIGHTS) {
992                                 drm_user_data_common_t metadata;
993                                 int type_index = -1;
994
995                                 media_svc_debug("DRM V2");
996
997                                 if (drm_svc_get_user_data_box_info(content_info->path, DRM_UDTA_TITLE, &metadata) == DRM_RESULT_SUCCESS) {
998                                         //_strncpy_safe(content_info->media_meta.title, metadata.subBox.title.str, sizeof(content_info->media_meta.title));
999                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, metadata.subBox.title.str);
1000                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1001
1002                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1003                                         media_svc_debug("extract title from odf");
1004                                 }
1005
1006                                 if (drm_svc_get_user_data_box_info(content_info->path, DRM_UDTA_DESCRIPTION, &metadata) == DRM_RESULT_SUCCESS) {
1007                                         //_strncpy_safe(content_info->media_meta.description, metadata.subBox.desc.str, sizeof(content_info->media_meta.description));
1008                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, metadata.subBox.desc.str);
1009                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1010
1011                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
1012                                 }
1013
1014                                 if (drm_svc_get_user_data_box_info(content_info->path, DRM_UDTA_COPYRIGHT, &metadata) == DRM_RESULT_SUCCESS) {
1015                                         //_strncpy_safe(content_info->media_meta.copyright, metadata.subBox.copyright.str, sizeof(content_info->media_meta.copyright));
1016                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, metadata.subBox.copyright.str);
1017                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1018
1019                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT;
1020                                 }
1021
1022                                 if (drm_svc_get_user_data_box_info(content_info->path, DRM_UDTA_AUTHOR, &metadata) == DRM_RESULT_SUCCESS) {
1023                                         //_strncpy_safe(content_info->media_meta.composer, metadata.subBox.author.str, sizeof(content_info->media_meta.composer));
1024                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, metadata.subBox.author.str);
1025                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1026
1027                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1028                                 }
1029
1030                                 if (drm_svc_get_user_data_box_info(content_info->path, DRM_UDTA_PERFORMER, &metadata) == DRM_RESULT_SUCCESS) {
1031                                         //_strncpy_safe(content_info->media_meta.artist, metadata.subBox.performer.str, sizeof(content_info->media_meta.artist));
1032                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, metadata.subBox.performer.str);
1033                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1034
1035                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_ARTIST;
1036                                 }
1037
1038                                 if (drm_svc_get_user_data_box_info(content_info->path, DRM_UDTA_GENRE, &metadata) == DRM_RESULT_SUCCESS) {
1039                                         //_strncpy_safe(content_info->media_meta.genre, metadata.subBox.genre.str, sizeof(content_info->media_meta.genre));
1040                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, metadata.subBox.genre.str);
1041                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1042
1043                                         //media_svc_debug("genre : %s", content_info->media_meta.genre);
1044                                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1045                                         /*
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                                         */
1050                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_GENRE;
1051                                 }
1052
1053                                 if (drm_svc_get_user_data_box_info(content_info->path, DRM_UDTA_ALBUM, &metadata) == DRM_RESULT_SUCCESS) {
1054                                         //_strncpy_safe(content_info->media_meta.album, metadata.subBox.album.albumTitle, sizeof(content_info->media_meta.album));
1055                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, metadata.subBox.album.albumTitle);
1056                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1057
1058                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_ALBUM;
1059
1060                                         char track_num[MEDIA_SVC_METADATA_LEN_MAX] = {0,};
1061                                         snprintf(track_num, sizeof(track_num), "%d", metadata.subBox.album.trackNum);
1062
1063                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, track_num);
1064                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1065
1066                                         //snprintf(content_info->media_meta.track_num, MEDIA_SVC_METADATA_LEN_MAX, "%d", metadata.subBox.album.trackNum);
1067                                 }
1068
1069                                 if (drm_svc_get_user_data_box_info(content_info->path, DRM_UDTA_RECODINGYEAR, &metadata) == DRM_RESULT_SUCCESS) {
1070                                         //_strncpy_safe(content_info->media_meta.year, __year_2_str(metadata.subBox.recodingYear.recodingYear), sizeof(content_info->media_meta.year));
1071                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, __year_2_str(metadata.subBox.recodingYear.recodingYear));
1072                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1073
1074                                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_YEAR;
1075                                 }
1076
1077                                 if (drm_svc_get_index_of_relative_contents(content_info->path, DRM_CONTENTS_INDEX_ALBUMJACKET, &type_index) == DRM_RESULT_SUCCESS) {
1078                                         char thumb_path[MEDIA_SVC_PATHNAME_SIZE+1] = {0};
1079
1080                                         if (drm_svc_make_multipart_drm_full_path(content_info->path, type_index, MEDIA_SVC_PATHNAME_SIZE, thumb_path) == DRM_TRUE) {
1081
1082                                                 DRM_FILE_HANDLE hFile = DRM_HANDLE_NULL;
1083
1084                                                 media_svc_debug("drm image path : %s", thumb_path);
1085
1086                                                 if (drm_svc_open_file(thumb_path, DRM_PERMISSION_ANY, &hFile) == DRM_RESULT_SUCCESS) {
1087                                                         int thumb_size = 0;
1088
1089                                                         if (drm_svc_seek_file(hFile, 0, DRM_SEEK_END) != DRM_RESULT_SUCCESS) {
1090                                                                 goto DRM_SEEK_ERROR;
1091                                                         }
1092                                                         thumb_size = drm_svc_tell_file(hFile);
1093
1094                                                         if (drm_svc_seek_file(hFile, 0, DRM_SEEK_SET) != DRM_RESULT_SUCCESS) {
1095                                                                 goto DRM_SEEK_ERROR;
1096                                                         }
1097                                                         /* remove thumbnail extract routine in db creating time.
1098                                                         media_svc_debug("drm thumb size : %d", thumb_size);
1099                                                         if (thumb_size > 0) {
1100                                                                 unsigned int readSize = 0;
1101
1102                                                                 thumb_buffer = malloc(thumb_size);
1103                                                                 if (drm_svc_read_file(hFile, thumb_buffer,thumb_size, &readSize) != DRM_RESULT_SUCCESS) {
1104                                                                         SAFE_FREE(thumb_buffer);
1105                                                                         goto DRM_SEEK_ERROR;
1106                                                                 }
1107
1108                                                                 __save_thumbnail(thumb_buffer, readSize, 1, content_info);
1109                                                                 SAFE_FREE(thumb_buffer);
1110                                                                 thumb_extracted_from_drm = TRUE;
1111                                                         }
1112                                                         */
1113                                                         DRM_SEEK_ERROR:
1114                                                                 drm_svc_free_dcf_header_info(&header_info);
1115                                                                 drm_svc_close_file(hFile);
1116                                                 }
1117                                         }
1118                                 }
1119                         } else {
1120                                 media_svc_debug("unsupported drm format");
1121                                 drm_svc_free_dcf_header_info(&header_info);
1122                                 title = _media_svc_get_title_from_filepath(content_info->path);
1123                                 if (title) {
1124                                         //_strncpy_safe(content_info->media_meta.title, title, sizeof(content_info->media_meta.title));
1125                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1126                                         SAFE_FREE(title);
1127                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1128
1129                                 } else {
1130                                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1131                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1132                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1133                                 }
1134
1135                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, 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.artist, 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.genre, 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.composer, MEDIA_SVC_TAG_UNKNOWN);
1142                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1143                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1144                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1145 /*
1146                                 _strncpy_safe(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.album));
1147                                 _strncpy_safe(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.artist));
1148                                 _strncpy_safe(content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.genre));
1149                                 _strncpy_safe(content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.composer));
1150                                 _strncpy_safe(content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.year));
1151 */
1152                                 return MEDIA_INFO_ERROR_NONE;
1153                         }
1154
1155                         if (invalid_file == TRUE) {
1156                                 if (!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) {
1157                                         title = _media_svc_get_title_from_filepath(content_info->path);
1158                                         if (title) {
1159                                                 //_strncpy_safe(content_info->media_meta.title, title, sizeof(content_info->media_meta.title));
1160                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1161                                                 SAFE_FREE(title);
1162                                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1163
1164                                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1165                                         } else {
1166                                                 media_svc_error("Can't extract title from filepath");
1167                                                 drm_svc_free_dcf_header_info(&header_info);
1168                                                 return MEDIA_INFO_ERROR_INTERNAL;
1169                                         }
1170
1171                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, 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.artist, 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.genre, 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.composer, MEDIA_SVC_TAG_UNKNOWN);
1178                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1179                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1180                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1181 /*
1182                                         _strncpy_safe(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.album));
1183                                         _strncpy_safe(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.artist));
1184                                         _strncpy_safe(content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.genre));
1185                                         _strncpy_safe(content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.composer));
1186                                         _strncpy_safe(content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.year));
1187 */
1188                                 }
1189
1190                                 drm_svc_free_dcf_header_info(&header_info);
1191                                 return MEDIA_INFO_ERROR_NONE;
1192                         }
1193                 } else if (type == DRM_FILE_TYPE_PLAYREADY) {
1194                         media_svc_debug("drm type is PLAYREADY");
1195                         if (drm_svc_has_valid_ro(content_info->path, DRM_PERMISSION_PLAY) != DRM_RESULT_SUCCESS) {
1196                                 media_svc_debug("no valid ro. can't extract meta data");
1197                                 title = _media_svc_get_title_from_filepath(content_info->path);
1198                                 if (title) {
1199                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1200                                         SAFE_FREE(title);
1201                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1202                                         //_strncpy_safe(content_info->media_meta.title, title, sizeof(content_info->media_meta.title));
1203                                 } else {
1204                                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1205                                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1206                                         media_svc_retv_del_if(ret < 0, ret, content_info);
1207                                 }
1208
1209                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
1210                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1211                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
1212                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1213                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
1214                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1215                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1216                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1217                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1218                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1219 /*
1220                                 _strncpy_safe(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.album));
1221                                 _strncpy_safe(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.artist));
1222                                 _strncpy_safe(content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.genre));
1223                                 _strncpy_safe(content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.composer));
1224                                 _strncpy_safe(content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.year));
1225 */
1226
1227                                 return MEDIA_INFO_ERROR_NONE;
1228                         }
1229                 } else {
1230                         media_svc_error("Not supported DRM type");
1231                         title = _media_svc_get_title_from_filepath(content_info->path);
1232                         if (title) {
1233                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1234                                 SAFE_FREE(title);
1235                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1236                                 //_strncpy_safe(content_info->media_meta.title, title, sizeof(content_info->media_meta.title));
1237                         } else {
1238                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1239                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
1240                                 media_svc_retv_del_if(ret < 0, ret, content_info);
1241                         }
1242
1243                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, 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.artist, 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.genre, MEDIA_SVC_TAG_UNKNOWN);
1248                         media_svc_retv_del_if(ret < 0, ret, content_info);
1249                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
1250                         media_svc_retv_del_if(ret < 0, ret, content_info);
1251                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
1252                         media_svc_retv_del_if(ret < 0, ret, content_info);
1253 /*
1254                         _strncpy_safe(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.album));
1255                         _strncpy_safe(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.artist));
1256                         _strncpy_safe(content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.genre));
1257                         _strncpy_safe(content_info->media_meta.author, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.author));
1258                         _strncpy_safe(content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN, sizeof(content_info->media_meta.year));
1259 */
1260                         return MEDIA_INFO_ERROR_NONE;
1261                 }
1262         }
1263 #endif
1264         /*Get Content attribute ===========*/
1265         mmf_error = mm_file_create_content_attrs(&content, content_info->path);
1266         if (mmf_error == MM_ERROR_NONE) {
1267                 /*Common attribute*/
1268                 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1269                 if (mmf_error != MM_ERROR_NONE) {
1270                         SAFE_FREE(err_attr_name);
1271                         media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1272                 } else {
1273                         //media_svc_debug("duration : %d", content_info->media_meta.duration);
1274                 }
1275
1276                 /*Sound/Music attribute*/
1277                 if((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1278
1279                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1280                         if (mmf_error != MM_ERROR_NONE) {
1281                                 SAFE_FREE(err_attr_name);
1282                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1283                         } else {
1284                                 //media_svc_debug("bit rate : %d", content_info->media_meta.bitrate);
1285                         }
1286
1287                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1288                         if (mmf_error != MM_ERROR_NONE) {
1289                                 SAFE_FREE(err_attr_name);
1290                                 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1291                         } else {
1292                                 //media_svc_debug("sample rate : %d", content_info->media_meta.samplerate);
1293                         }
1294
1295                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1296                         if (mmf_error != MM_ERROR_NONE) {
1297                                 SAFE_FREE(err_attr_name);
1298                                 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1299                         } else {
1300                                 //media_svc_debug("channel : %d", content_info->media_meta.channel);
1301                         }
1302                 }else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO)      {       /*Video attribute*/
1303                         int audio_bitrate = 0;
1304                         int video_bitrate = 0;
1305
1306                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1307                         if (mmf_error != MM_ERROR_NONE) {
1308                                 SAFE_FREE(err_attr_name);
1309                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1310                         } else {
1311                                 //media_svc_debug("audio bit rate : %d", audio_bitrate);
1312                         }
1313
1314                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1315                         if (mmf_error != MM_ERROR_NONE) {
1316                                 SAFE_FREE(err_attr_name);
1317                                 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1318                         } else {
1319                                 //media_svc_debug("video bit rate : %d", video_bitrate);
1320                         }
1321
1322                         content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1323
1324                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1325                         if (mmf_error != MM_ERROR_NONE) {
1326                                 SAFE_FREE(err_attr_name);
1327                                 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1328                         } else {
1329                                 //media_svc_debug("width : %d", content_info->media_meta.width);
1330                         }
1331
1332                         mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1333                         if (mmf_error != MM_ERROR_NONE) {
1334                                 SAFE_FREE(err_attr_name);
1335                                 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1336                         } else {
1337                                 //media_svc_debug("height : %d", content_info->media_meta.height);
1338                         }
1339
1340                 } else {
1341                         media_svc_error("Not support type");
1342                         return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1343                 }
1344
1345                 mmf_error = mm_file_destroy_content_attrs(content);
1346                 if (mmf_error != MM_ERROR_NONE) {
1347                         media_svc_error("fail to free content attr - err(%x)", mmf_error);
1348                 }
1349         } else {
1350                 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1351         }
1352
1353         /*Get Content Tag attribute ===========*/
1354         mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1355
1356         if (mmf_error == MM_ERROR_NONE) {
1357                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1358                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1359                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1360                         if(ret != MEDIA_INFO_ERROR_NONE)
1361                                 media_svc_error("strcpy error");
1362
1363                         //media_svc_debug("album[%d] : %s", size, content_info->media_meta.album);
1364                 } else {
1365                         SAFE_FREE(err_attr_name);
1366                         //media_svc_debug("album - unknown");
1367                 }
1368
1369                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1370                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1371                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1372                         if(ret != MEDIA_INFO_ERROR_NONE)
1373                                 media_svc_error("strcpy error");
1374                         //media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist);
1375                 } else {
1376                         SAFE_FREE(err_attr_name);
1377                         //media_svc_debug("artist - unknown");
1378                 }
1379
1380                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1381                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1382                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1383                         if(ret != MEDIA_INFO_ERROR_NONE)
1384                                 media_svc_error("strcpy error");
1385
1386                         //media_svc_debug("genre : %s", content_info->media_meta.genre);
1387                         /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1388                         /*
1389                         if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1390                                 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1391                         }
1392                         */
1393                 } else {
1394                         SAFE_FREE(err_attr_name);
1395                         //media_svc_debug("genre - unknown");
1396                 }
1397
1398                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1399                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* &&   (!isspace(*p))*/) {
1400                         if(!isspace(*p)) {
1401                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1402                                 if(ret != MEDIA_INFO_ERROR_NONE)
1403                                         media_svc_error("strcpy error");
1404
1405                                 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1406                                 //media_svc_debug("extract title from content : %s", content_info->media_meta.title);
1407                                 //media_svc_debug("^^^^^^^^^^^^^^^ path = %s, title = %s, size = %d ^^^^^^^^^^^^^^", content_info->path, content_info->media_meta.title, size);
1408                         }
1409                         else {
1410                                 int idx = 0;
1411
1412                                 for(idx = 0; idx < size; idx++) {
1413                                         if(isspace(*p)) {
1414                                                 media_svc_debug("SPACE [%s]", p);
1415                                                 p++;
1416                                                 continue;
1417                                         } else {
1418                                                 media_svc_debug("Not SPACE [%s]", p);
1419                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1420                                                 if(ret != MEDIA_INFO_ERROR_NONE)
1421                                                         media_svc_error("strcpy error");
1422                                                 break;
1423                                         }
1424                                 }
1425
1426                                 if(idx == size)
1427                                 {
1428                                         media_svc_debug("Can't extract title. All string is space");
1429                                         title = _media_svc_get_title_from_filepath(content_info->path);
1430                                         if (title) {
1431                                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1432                                                 if(ret != MEDIA_INFO_ERROR_NONE)
1433                                                         media_svc_error("strcpy error");
1434                                                 SAFE_FREE(title);
1435                                         } else {
1436                                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1437                                         }
1438                                 }
1439                         }
1440                 } else {
1441                         SAFE_FREE(err_attr_name);
1442                         title = _media_svc_get_title_from_filepath(content_info->path);
1443                         if (title) {
1444                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1445                                 if(ret != MEDIA_INFO_ERROR_NONE)
1446                                         media_svc_error("strcpy error");
1447                                 SAFE_FREE(title);
1448                         } else {
1449                                 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1450                         }
1451                 }
1452
1453                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1454                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1455                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1456                         if(ret != MEDIA_INFO_ERROR_NONE)
1457                                 media_svc_error("strcpy error");
1458                         //media_svc_debug("desc : %s", content_info->media_meta.description);
1459                 } else {
1460                         SAFE_FREE(err_attr_name);
1461                 }
1462
1463                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1464                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1465                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1466                         if(ret != MEDIA_INFO_ERROR_NONE)
1467                                 media_svc_error("strcpy error");
1468                         //media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date);
1469                 } else {
1470                         SAFE_FREE(err_attr_name);
1471                 }
1472
1473                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1474                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1475                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1476                         if(ret != MEDIA_INFO_ERROR_NONE)
1477                                 media_svc_error("strcpy error");
1478                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1479                         //media_svc_debug("extract composer from content : %s", content_info->media_meta.composer);
1480                 } else {
1481                         //media_svc_debug("composer - unknown");
1482                         SAFE_FREE(err_attr_name);
1483                 }
1484
1485                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1486                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1487                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1488                         if(ret != MEDIA_INFO_ERROR_NONE)
1489                                 media_svc_error("strcpy error");
1490                         extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1491                         //media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright);
1492                 } else {
1493                         //media_svc_debug("copyright - unknown");
1494                         SAFE_FREE(err_attr_name);
1495                 }
1496
1497                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1498                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1499                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1500                         if(ret != MEDIA_INFO_ERROR_NONE)
1501                                 media_svc_error("strcpy error");
1502                 } else {
1503                         SAFE_FREE(err_attr_name);
1504                 }
1505
1506                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1507                 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == MM_ERROR_NONE) && (size == 4)) {
1508                         int year = 0;
1509                         if((p != NULL) && (sscanf( p, "%d", &year))) {
1510                                 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1511                                 if(ret != MEDIA_INFO_ERROR_NONE)
1512                                         media_svc_error("strcpy error");
1513                         } else {
1514                                 media_svc_debug("Wrong Year Information [%s]", p);
1515                         }
1516                 } else {
1517                         SAFE_FREE(err_attr_name);
1518                 }
1519
1520                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1521                 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1522                         content_info->media_meta.rating = atoi(p);
1523                 } else {
1524                         SAFE_FREE(err_attr_name);
1525                         content_info->media_meta.rating = 0;
1526                 }
1527
1528                 /*Initialize album_id to 0. below code will set the album_id*/
1529                 content_info->album_id = album_id;
1530 #if 0
1531                 /* extract thumbnail image */
1532                 if(strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) {
1533                         if(strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) {
1534
1535                                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1536
1537                                 if (ret != MEDIA_INFO_ERROR_NONE) {
1538                                         if (ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
1539                                                 media_svc_debug("album does not exist. So start to make album art");
1540                                                 extract_thumbnail = TRUE;
1541                                                 append_album = TRUE;
1542                                         } else {
1543                                                 extract_thumbnail = FALSE;
1544                                                 append_album = FALSE;
1545                                         }
1546                                 } else {
1547                                         media_svc_debug("album already exists. don't need to make album art");
1548                                         content_info->album_id = album_id;
1549                                         ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1550                                         media_svc_retv_del_if((ret != MEDIA_INFO_ERROR_NONE) && (ret != MEDIA_INFO_ERROR_DATABASE_NO_RECORD), ret, content_info);
1551                                         extract_thumbnail = FALSE;
1552                                         append_album = FALSE;
1553                                 }
1554                         } else {
1555                                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1556
1557                                 if (ret != MEDIA_INFO_ERROR_NONE) {
1558                                         if (ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
1559                                                 media_svc_debug("Unknown artist album does not exist.");
1560                                                 extract_thumbnail = TRUE;
1561                                                 append_album = TRUE;
1562                                         } else {
1563                                                 extract_thumbnail = FALSE;
1564                                                 append_album = FALSE;
1565                                         }
1566                                 } else {
1567                                         media_svc_debug("Unknown artist album already exists.");
1568
1569                                         content_info->album_id = album_id;
1570                                         extract_thumbnail = TRUE;
1571                                         append_album = FALSE;
1572                                 }
1573                         }
1574                 } else {
1575                         extract_thumbnail = TRUE;
1576                         append_album = FALSE;
1577                 }
1578 #else
1579                 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1580
1581                 if (ret != MEDIA_INFO_ERROR_NONE) {
1582                         if (ret == MEDIA_INFO_ERROR_DATABASE_NO_RECORD) {
1583                                 media_svc_debug("album does not exist. So start to make album art");
1584                                 extract_thumbnail = TRUE;
1585                                 append_album = TRUE;
1586                         } else {
1587                                 extract_thumbnail = FALSE;
1588                                 append_album = FALSE;
1589                         }
1590                 } else {
1591                         content_info->album_id = album_id;
1592                         append_album = FALSE;
1593
1594                         if((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1595                                 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1596
1597                                 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1598                                 extract_thumbnail = TRUE;
1599                         } else {
1600
1601                                 media_svc_debug("album already exists. don't need to make album art");
1602                                 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1603                                 media_svc_retv_del_if((ret != MEDIA_INFO_ERROR_NONE) && (ret != MEDIA_INFO_ERROR_DATABASE_NO_RECORD), ret, content_info);
1604                                 extract_thumbnail = FALSE;
1605                         }
1606                 }
1607 #endif
1608
1609                 if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE)) {
1610                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1611                         if (mmf_error != MM_ERROR_NONE) {
1612                                 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1613                                 SAFE_FREE(err_attr_name);
1614                         } else {
1615                                 //media_svc_debug("artwork size1 [%d]", size);
1616                         }
1617
1618                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1619                         if (mmf_error != MM_ERROR_NONE) {
1620                                 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1621                                 SAFE_FREE(err_attr_name);
1622                         } else {
1623                                 //media_svc_debug("artwork size2 [%d]", size);
1624                         }
1625                         if (image != NULL && size > 0) {
1626                                 bool result = FALSE;
1627                                 int ret = MEDIA_INFO_ERROR_NONE;
1628                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1629                                 int artwork_mime_size = -1;
1630
1631                                 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1632                                 if ((mmf_error == MM_ERROR_NONE) && (artwork_mime_size > 0)) {
1633                                         result = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p);
1634                                         if (result == FALSE) {
1635                                                 media_svc_error("Fail to Get Thumbnail Path");
1636                                         }
1637                                 } else {
1638                                         SAFE_FREE(err_attr_name);
1639                                 }
1640
1641                                 if(strlen(thumb_path) > 0)
1642                                 {
1643                                         ret = _media_svc_save_image(image, size, thumb_path);
1644                                         if (ret != MEDIA_INFO_ERROR_NONE) {
1645                                                 media_svc_error("Fail to Save Thumbnail Image");
1646                                         }
1647                                         else {
1648                                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1649                                                 if(ret != MEDIA_INFO_ERROR_NONE)
1650                                                         media_svc_error("strcpy error");
1651                                         }
1652                                 }
1653                         }
1654                 }
1655
1656                 if(append_album == TRUE) {
1657
1658                         if((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1659                                 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1660                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id);
1661                         else
1662                                 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id);
1663
1664                         content_info->album_id = album_id;
1665                 }
1666
1667                 if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1668                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
1669                         if (mmf_error == MM_ERROR_NONE) {
1670                                 if (gps_value == 0.0)
1671                                         content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1672                                 else
1673                                         content_info->media_meta.longitude = gps_value;
1674                         } else {
1675                                 SAFE_FREE(err_attr_name);
1676                         }
1677
1678                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
1679                         if (mmf_error == MM_ERROR_NONE) {
1680                                 if (gps_value == 0.0)
1681                                         content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1682                                 else
1683                                         content_info->media_meta.latitude = gps_value;
1684                         } else {
1685                                 SAFE_FREE(err_attr_name);
1686                         }
1687
1688                         mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
1689                         if (mmf_error == MM_ERROR_NONE) {
1690                                 if (gps_value == 0.0)
1691                                         content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1692                                 else
1693                                         content_info->media_meta.altitude = gps_value;
1694                         } else {
1695                                 SAFE_FREE(err_attr_name);
1696                         }
1697 #if 0
1698                         //if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE))
1699                         {
1700                                 /* Extracting thumbnail */
1701                                 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1702                                 int width = 0;
1703                                 int height = 0;
1704
1705                                 ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
1706                                 if (ret < 0) {
1707                                         media_svc_error("thumbnail_request_from_db failed: %d", ret);
1708                                 } else {
1709                                         //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
1710                                 }
1711
1712                                 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1713                                 if(ret != MEDIA_INFO_ERROR_NONE)
1714                                         media_svc_error("strcpy error");
1715
1716                                 if (content_info->media_meta.width <= 0) content_info->media_meta.width = width;
1717                                 if (content_info->media_meta.height <= 0) content_info->media_meta.height = height;
1718                         }
1719 #endif
1720                 }
1721
1722                 mmf_error = mm_file_destroy_tag_attrs(tag);
1723                 if (mmf_error != MM_ERROR_NONE) {
1724                         media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1725                 }
1726         }       else {
1727                 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1728                 char *title = NULL;
1729                 media_svc_error("no tag information");
1730
1731                 title = _media_svc_get_title_from_filepath(content_info->path);
1732                 if (title) {
1733                         ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1734                         if(ret != MEDIA_INFO_ERROR_NONE)
1735                                 media_svc_error("strcpy error");
1736                         SAFE_FREE(title);
1737                 } else {
1738                         media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1739                 }
1740
1741                 content_info->album_id = album_id;
1742         }
1743
1744         return MEDIA_INFO_ERROR_NONE;
1745 }
1746
1747 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1748 {
1749         media_svc_retm_if(content_info == NULL, "content info is NULL");
1750
1751         /* Delete media_svc_content_info_s */
1752         SAFE_FREE(content_info->media_uuid);
1753         SAFE_FREE(content_info->path);
1754         SAFE_FREE(content_info->file_name);
1755         SAFE_FREE(content_info->mime_type);
1756         SAFE_FREE(content_info->folder_uuid);
1757         SAFE_FREE(content_info->thumbnail_path);
1758
1759         /* Delete media_svc_content_meta_s */
1760         SAFE_FREE(content_info->media_meta.title);
1761         SAFE_FREE(content_info->media_meta.album);
1762         SAFE_FREE(content_info->media_meta.artist);
1763         SAFE_FREE(content_info->media_meta.genre);
1764         SAFE_FREE(content_info->media_meta.composer);
1765         SAFE_FREE(content_info->media_meta.year);
1766         SAFE_FREE(content_info->media_meta.recorded_date);
1767         SAFE_FREE(content_info->media_meta.copyright);
1768         SAFE_FREE(content_info->media_meta.track_num);
1769         SAFE_FREE(content_info->media_meta.description);
1770         SAFE_FREE(content_info->media_meta.datetaken);
1771
1772         return;
1773 }
1774
1775 int _media_svc_get_store_type_by_path(const char *path, media_svc_storage_type_e *storage_type)
1776 {
1777         if(STRING_VALID(path))
1778         {
1779                 if(strncmp(path, MEDIA_SVC_PATH_PHONE, strlen(MEDIA_SVC_PATH_PHONE)) == 0)
1780                 {
1781                         *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
1782                 }
1783                 else if(strncmp (path, MEDIA_SVC_PATH_MMC, strlen(MEDIA_SVC_PATH_MMC)) == 0)
1784                 {
1785                         *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
1786                 }
1787         }
1788         else
1789         {
1790                 media_svc_error("INVALID parameter");
1791                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1792         }
1793
1794         return MEDIA_INFO_ERROR_NONE;
1795 }
1796
1797 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
1798 {
1799   char *result, *sr;
1800   size_t i, count = 0;
1801   size_t oldlen = strlen(olds); if (oldlen < 1) return s;
1802   size_t newlen = strlen(news);
1803
1804   if (newlen != oldlen) {
1805     for (i = 0; s[i] != '\0';) {
1806       if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
1807       else i++;
1808     }   
1809   } else i = strlen(s);
1810
1811
1812   result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
1813   if (result == NULL) return NULL;
1814
1815   sr = result;
1816   while (*s) {
1817     if (memcmp(s, olds, oldlen) == 0) {
1818       memcpy(sr, news, newlen);
1819       sr += newlen;
1820       s  += oldlen;
1821     } else *sr++ = *s++;
1822   }
1823
1824   *sr = '\0';
1825
1826   return result;
1827 }
1828
1829 int _media_svc_error_convert(int error)
1830 {
1831         media_svc_debug("error : [%d]", error);
1832
1833         if(error == MS_MEDIA_ERR_NONE)                                                  /*Error None*/
1834                 return MEDIA_INFO_ERROR_NONE;
1835         else if(error == MS_MEDIA_ERR_DB_CONNECT_FAIL)                  /*DB Connect Fail*/
1836                 return MEDIA_INFO_ERROR_DATABASE_CONNECT;
1837         else if(error == MS_MEDIA_ERR_DB_DISCONNECT_FAIL)               /*DB Disconnect Fail*/
1838                 return MEDIA_INFO_ERROR_DATABASE_DISCONNECT;
1839         else if(error == MS_MEDIA_ERR_SOCKET_CONN)                              /*Socket Connect Fail*/
1840                 return MEDIA_INFO_ERROR_SOCKET_CONN;
1841         else if(error == MS_MEDIA_ERR_INVALID_PARAMETER || error == MS_MEDIA_ERR_INVALID_PATH)
1842                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1843
1844         return MEDIA_INFO_ERROR_INTERNAL;
1845 }
1846
1847
1848 bool _media_svc_is_drm_file(const char *path)
1849 {
1850 #ifdef __SUPPORT_DRM
1851         int ret;
1852         drm_bool_type_e is_drm_file = DRM_UNKNOWN;
1853
1854         ret = drm_is_drm_file(path,&is_drm_file);
1855         if(DRM_RETURN_SUCCESS == ret && DRM_TRUE == is_drm_file)
1856                 return TRUE;
1857 #endif
1858         return FALSE;
1859 }
1860
1861 int _media_svc_get_mime_in_drm_info(const char *path, char *mime, drm_content_info_s **drm_contentInfo)
1862 {
1863 #ifdef __SUPPORT_DRM
1864         int ret = MEDIA_INFO_ERROR_NONE;
1865         drm_file_type_e file_type = DRM_TYPE_UNDEFINED;
1866
1867         if (path == NULL || mime == NULL)
1868                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1869
1870         ret = drm_get_file_type(path, &file_type);
1871         if (ret != DRM_RETURN_SUCCESS) {
1872                 media_svc_error("drm_get_file_type() failed : %d", ret);
1873                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1874         } else {
1875                 if (file_type == DRM_TYPE_OMA_V1
1876                 || file_type == DRM_TYPE_OMA_V2
1877                 || file_type == DRM_TYPE_OMA_PD) {
1878                         *drm_contentInfo = malloc(sizeof(drm_content_info_s));
1879                         memset(*drm_contentInfo,0x0,sizeof(drm_content_info_s));
1880                         ret = drm_get_content_info(path, *drm_contentInfo);
1881                         if (ret != DRM_RETURN_SUCCESS) {
1882                                 media_svc_error("drm_svc_get_content_info() fails :%d ", ret);
1883                                 free(*drm_contentInfo);
1884                                 *drm_contentInfo = NULL;
1885                                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1886                         }
1887
1888                         if (STRING_VALID((*drm_contentInfo)->mime_type)) {
1889                                 strncpy(mime,(*drm_contentInfo)->mime_type, MEDIA_SVC_METADATA_LEN_MAX);
1890                                 media_svc_debug("DRM contentType : %s",(*drm_contentInfo)->mime_type);
1891                         } else {
1892                                 free(*drm_contentInfo);
1893                                 *drm_contentInfo = NULL;
1894                                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
1895                         }
1896                 } else {
1897                         media_svc_error("THIS IS DRM BUT YOU SHOULD USE API OF AUL LIBRARY");
1898                         *drm_contentInfo = NULL;
1899                         return MEDIA_INFO_ERROR_INVALID_MEDIA;
1900                 }
1901         }
1902
1903         return MEDIA_INFO_ERROR_NONE;
1904 #else
1905         *drm_contentInfo = NULL;
1906         return MEDIA_INFO_ERROR_INVALID_MEDIA;
1907 #endif
1908 }
1909
1910 int _media_svc_get_content_type_from_mime(const char * path, const char * mimetype, int * category)
1911 {
1912         int i = 0;
1913         int err = 0;
1914
1915         *category = MEDIA_SVC_CATEGORY_UNKNOWN;
1916
1917         //media_svc_debug("mime type : %s", mimetype);
1918
1919         /*categorize from mimetype */
1920         for (i = 0; i < CONTENT_TYPE_NUM; i++) {
1921                 if (strstr(mimetype, content_category[i].content_type) != NULL) {
1922                         *category = (*category | content_category[i].category_by_mime);
1923                         break;
1924                 }
1925         }
1926
1927         /*in application type, exitst sound file ex) x-smafs */
1928         if (*category & MEDIA_SVC_CATEGORY_ETC) {
1929                 int prefix_len = strlen(content_category[0].content_type);
1930
1931                 for (i = 0; i < SOUND_MIME_NUM; i++) {
1932                         if (strstr(mimetype + prefix_len, sound_mime_table[i]) != NULL) {
1933                                 *category ^= MEDIA_SVC_CATEGORY_ETC;
1934                                 *category |= MEDIA_SVC_CATEGORY_SOUND;
1935                                 break;
1936                         }
1937                 }
1938         }
1939
1940         /*check music file in soun files. */
1941         if (*category & MEDIA_SVC_CATEGORY_SOUND) {
1942                 int prefix_len = strlen(content_category[0].content_type) + 1;
1943
1944                 //MS_DBG("mime_type : %s", mimetype + prefix_len);
1945
1946                 for (i = 0; i < MUSIC_MIME_NUM; i++) {
1947                         if (strcmp(mimetype + prefix_len, music_mime_table[i]) == 0) {
1948                                 *category ^= MEDIA_SVC_CATEGORY_SOUND;
1949                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1950                                 break;
1951                         }
1952                 }
1953         } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
1954                 /*some video files don't have video stream. in this case it is categorize as music. */
1955                 char *ext;
1956                 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
1957                 ext = strrchr(path, '.');
1958                 if (ext != NULL) {
1959                         if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
1960                                 int audio = 0;
1961                                 int video = 0;
1962
1963                                 err = mm_file_get_stream_info(path, &audio, &video);
1964                                 if (err == 0) {
1965                                         if (audio > 0 && video == 0) {
1966                                                 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
1967                                                 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1968                                         }
1969                                 }
1970                         }
1971                 }
1972         }
1973
1974         //media_svc_debug("category_from_ext : %d", *category);
1975
1976         return err;
1977 }
1978
1979 /*
1980 drm_contentifo is not NULL, if the file is OMA DRM.
1981 If the file is not OMA DRM, drm_contentinfo must be NULL.
1982 */
1983 int _media_svc_get_mime_type(const char *path, char *mimetype, drm_bool_type_e *is_drm, drm_content_info_s **drm_contentInfo)
1984 {
1985         int ret = MEDIA_INFO_ERROR_NONE;
1986
1987         if (path == NULL)
1988                 return MEDIA_INFO_ERROR_INVALID_PARAMETER;
1989
1990 #ifdef __SUPPORT_DRM
1991         /* In case of drm file. */
1992         if (_media_svc_is_drm_file(path)) {
1993                 *is_drm = DRM_TRUE;
1994                 ret =  _media_svc_get_mime_in_drm_info(path, mimetype, drm_contentInfo);
1995                 if (ret == MEDIA_INFO_ERROR_NONE) {
1996                         return ret;
1997                 }
1998         }
1999 #else
2000         *is_drm = DRM_FALSE;
2001         *drm_contentInfo = NULL;
2002 #endif
2003
2004         /*in case of normal files or failure to get mime in drm */
2005         if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
2006                 media_svc_error("aul_get_mime_from_file fail");
2007                 return MEDIA_INFO_ERROR_INVALID_MEDIA;
2008         }
2009
2010         return MEDIA_INFO_ERROR_NONE;
2011 }
2012
2013 int _media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
2014 {
2015         int ret = MEDIA_INFO_ERROR_NONE;
2016         int category = 0;
2017
2018         media_svc_media_type_e type;
2019
2020         ret = _media_svc_get_content_type_from_mime(path, mime_type, &category);
2021         if (ret < 0) {
2022                 media_svc_error("_media_svc_get_content_type_from_mime failed : %d", ret);
2023         }
2024
2025         if (category & MEDIA_SVC_CATEGORY_SOUND)                type = MEDIA_SVC_MEDIA_TYPE_SOUND;
2026         else if (category & MEDIA_SVC_CATEGORY_MUSIC)   type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
2027         else if (category & MEDIA_SVC_CATEGORY_IMAGE)   type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
2028         else if (category & MEDIA_SVC_CATEGORY_VIDEO)   type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
2029         else    type = MEDIA_SVC_MEDIA_TYPE_OTHER;
2030
2031         *media_type = type;
2032
2033         return ret;
2034 }
2035