4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include <glib/gstdio.h>
36 #include <libexif/exif-data.h>
37 #include <media-thumbnail.h>
38 #include <media-util.h>
42 #include "media-util-err.h"
43 #include "media-svc-util.h"
44 #include "media-svc-db-utils.h"
45 #include "media-svc-debug.h"
46 #include "media-svc-env.h"
47 #include "media-svc-hash.h"
48 #include "media-svc-album.h"
49 #include "media-svc-localize_ch.h"
51 #define MEDIA_SVC_FILE_EXT_LEN_MAX 6 /**< Maximum file ext lenth*/
52 #define GLOBAL_USER 0 //#define tzplatform_getenv(TZ_GLOBAL) //TODO
55 MEDIA_SVC_EXTRACTED_FIELD_NONE = 0x00000001,
56 MEDIA_SVC_EXTRACTED_FIELD_TITLE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 1,
57 MEDIA_SVC_EXTRACTED_FIELD_DESC = MEDIA_SVC_EXTRACTED_FIELD_NONE << 2,
58 MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT = MEDIA_SVC_EXTRACTED_FIELD_NONE << 3,
59 MEDIA_SVC_EXTRACTED_FIELD_AUTHOR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 4,
60 MEDIA_SVC_EXTRACTED_FIELD_ARTIST = MEDIA_SVC_EXTRACTED_FIELD_NONE << 5,
61 MEDIA_SVC_EXTRACTED_FIELD_GENRE = MEDIA_SVC_EXTRACTED_FIELD_NONE << 6,
62 MEDIA_SVC_EXTRACTED_FIELD_ALBUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 7,
63 MEDIA_SVC_EXTRACTED_FIELD_TRACKNUM = MEDIA_SVC_EXTRACTED_FIELD_NONE << 8,
64 MEDIA_SVC_EXTRACTED_FIELD_YEAR = MEDIA_SVC_EXTRACTED_FIELD_NONE << 9,
65 MEDIA_SVC_EXTRACTED_FIELD_CATEGORY = MEDIA_SVC_EXTRACTED_FIELD_NONE << 10,
66 } media_svc_extracted_field_e;
69 char *_media_info_generate_uuid(void)
72 static char uuid_unparsed[50];
74 uuid_generate(uuid_value);
75 uuid_unparse(uuid_value, uuid_unparsed);
77 //media_svc_debug("UUID : %s", uuid_unparsed);
81 void _strncpy_safe(char *x_dst, const char *x_src, int max_len)
83 if (!x_src || strlen(x_src) == 0) {
84 media_svc_error("x_src is NULL");
89 media_svc_error("length is Wrong");
93 strncpy(x_dst, x_src, max_len-1);
94 x_dst[max_len-1] = '\0';
97 int __media_svc_malloc_and_strncpy(char **dst, const char *src)
101 if (!STRING_VALID(src)) {
102 media_svc_error("invalid src");
103 return MS_MEDIA_ERR_INVALID_PARAMETER;
108 len = strlen(src) + 1;
112 media_svc_error("malloc failed");
113 return MS_MEDIA_ERR_INTERNAL;
116 strncpy(*dst, src, len);
120 return MS_MEDIA_ERR_NONE;
123 static void __media_svc_split_to_double(char *input, double *arr, int *num)
125 char tmp_arr[255] = { 0, };
126 int len = strlen(input);
127 int i = 0, idx = 0, tmp_idx = 0;
128 int is_prev_space = 0;
131 if (input[len - 1] == ' ') {
138 for (i = 0; i < len; i++) {
143 if (input[i] == ' ') {
144 if (is_prev_space == 1) {
148 arr[idx++] = atof(tmp_arr);
155 tmp_arr[tmp_idx] = input[i];
156 tmp_arr[++tmp_idx] = '\0';
162 arr[idx++] = atof(tmp_arr);
172 static int __media_svc_get_exif_info(ExifData *ed,
184 //media_svc_debug("ExifData is NULL");
185 return MS_MEDIA_ERR_INVALID_PARAMETER;
191 entry = exif_data_get_entry(ed, tag);
193 /* Get the contents of the tag in human-readable form */
194 if (tag == EXIF_TAG_ORIENTATION ||
195 tag == EXIF_TAG_PIXEL_X_DIMENSION ||
196 tag == EXIF_TAG_PIXEL_Y_DIMENSION) {
198 if (i_value == NULL) {
199 media_svc_error("i_value is NULL");
200 return MS_MEDIA_ERR_INVALID_PARAMETER;
203 ExifByteOrder mByteOrder = exif_data_get_byte_order(ed);
204 short exif_value = exif_get_short(entry->data, mByteOrder);
205 //media_svc_debug("%s : %d", exif_tag_get_name_in_ifd(tag,ifd), exif_value);
206 *i_value = (int)exif_value;
208 } else if (tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE || tag == EXIF_TAG_GPS_ALTITUDE) {
210 if (d_value == NULL) {
211 media_svc_error("d_value is NULL");
212 return MS_MEDIA_ERR_INVALID_PARAMETER;
215 /* Get the contents of the tag in human-readable form */
216 char gps_buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
217 exif_entry_get_value(entry, gps_buf, sizeof(gps_buf));
218 gps_buf[strlen(gps_buf)] = '\0';
220 //media_svc_debug("%s: %s\n", exif_tag_get_name_in_ifd(tag, ifd), gps_buf);
222 double tmp_arr[3] = { 0.0, 0.0, 0.0 };
225 __media_svc_split_to_double(gps_buf, tmp_arr, &count);
227 media_svc_error("Value is invalid");
228 return MS_MEDIA_ERR_INTERNAL;
231 *d_value = tmp_arr[0] + tmp_arr[1] / 60 + tmp_arr[2] / 3600;
232 //media_svc_debug("GPS value is %f", *d_value);
236 media_svc_error("buf is NULL");
237 return MS_MEDIA_ERR_INVALID_PARAMETER;
240 exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
241 buf[strlen(buf)] = '\0';
244 media_svc_debug("%s: %s\n", exif_tag_get_name_in_ifd(tag, ifd), buf);
249 return MS_MEDIA_ERR_NONE;
252 unsigned int _media_svc_get_current_time(void)
255 unsigned int tval = 0;
256 gettimeofday(&t, NULL);
258 tval = t.tv_sec*1000000L + t.tv_usec;
263 int _media_svc_check_escape_char(char ch)
266 char escape_char[3] = {'%', '_' ,'#'};
268 for (i = 0; i < 3; i++) {
269 if (ch == escape_char[i]) {
277 char *_media_svc_escape_str(char *input, int len)
283 result = (char*)malloc(len * 2 * sizeof(char) + 1);
284 if (result == NULL) {
288 for (i = 0; i < len; i++, j++) {
289 if (input[i] == '\0') break;
291 if (_media_svc_check_escape_char(input[i])) {
293 result[++j] = input[i];
295 result[j] = input[i];
304 int _media_svc_rename_file( const char *old_name, const char *new_name)
306 if((old_name == NULL) || (new_name == NULL))
308 media_svc_error("invalid file name");
309 return MS_MEDIA_ERR_INVALID_PARAMETER;
312 if (rename(old_name, new_name) < 0) {
313 media_svc_error("Old : [%s] New : [%s] errno : [%s]", old_name, new_name, strerror(errno));
314 return MS_MEDIA_ERR_INTERNAL;
317 return MS_MEDIA_ERR_NONE;
320 bool _media_svc_remove_file(const char *path)
324 result = remove(path);
326 media_svc_debug("success to remove file");
329 media_svc_error("fail to remove file result errno = %s", strerror(errno));
334 int _media_svc_remove_all_files_in_dir(const char *dir_path)
336 struct dirent *entry = NULL;
338 char filename[MEDIA_SVC_PATHNAME_SIZE] = {0};
341 dir = opendir(dir_path);
343 media_svc_error("%s is not exist", dir_path);
344 return MS_MEDIA_ERR_INVALID_PARAMETER;
347 while ((entry = readdir(dir)) != NULL) {
348 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
351 snprintf(filename, sizeof(filename), "%s/%s", dir_path, entry->d_name);
353 if (stat(filename, &st) != 0) {
356 if (S_ISDIR(st.st_mode)) {
359 if (unlink(filename) != 0) {
360 media_svc_error("failed to remove : %s", filename);
362 return MS_MEDIA_ERR_INTERNAL;
367 return MS_MEDIA_ERR_NONE;
370 char *_media_svc_get_title_from_filepath (const char *path)
372 char *filename = NULL;
375 int filename_len = -1;
376 int new_title_len = -1;
379 media_svc_error("path is NULL");
383 filename = g_path_get_basename(path);
384 if (!STRING_VALID(filename)) {
385 media_svc_error("wrong file name");
390 filename_len = strlen(filename);
392 ext = g_strrstr(filename, ".");
394 media_svc_error("there is no file extention");
398 new_title_len = filename_len - strlen(ext);
399 if (new_title_len < 1) {
400 media_svc_error("title length is zero");
405 title = g_strndup(filename, new_title_len < MEDIA_SVC_PATHNAME_SIZE ? new_title_len : MEDIA_SVC_PATHNAME_SIZE-1);
409 media_svc_debug("extract title is [%s]", title);
414 static int _mkdir(const char *dir, mode_t mode) {
419 snprintf(tmp, sizeof(tmp),"%s",dir);
421 if(tmp[len - 1] == '/')
423 for(p = tmp + 1; *p; p++)
429 return mkdir(tmp, mode);
432 static char* _media_svc_get_thumb_path(uid_t uid)
434 char *result_psswd = NULL;
435 struct group *grpinfo = NULL;
438 result_psswd = strdup(MEDIA_SVC_THUMB_PATH_PREFIX);
439 grpinfo = getgrnam("users");
440 if(grpinfo == NULL) {
441 media_svc_error("getgrnam(users) returns NULL !");
447 struct passwd *userinfo = getpwuid(uid);
448 if(userinfo == NULL) {
449 media_svc_error("getpwuid(%d) returns NULL !", uid);
452 grpinfo = getgrnam("users");
453 if(grpinfo == NULL) {
454 media_svc_error("getgrnam(users) returns NULL !");
457 // Compare git_t type and not group name
458 if (grpinfo->gr_gid != userinfo->pw_gid) {
459 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
462 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb", userinfo->pw_dir);
465 _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
470 char* _media_svc_get_thumb_internal_path(uid_t uid)
472 char *result_psswd = NULL;
473 struct group *grpinfo = NULL;
476 result_psswd = strdup(MEDIA_SVC_THUMB_INTERNAL_PATH);
477 grpinfo = getgrnam("users");
478 if(grpinfo == NULL) {
479 media_svc_error("getgrnam(users) returns NULL !");
485 struct passwd *userinfo = getpwuid(uid);
486 if(userinfo == NULL) {
487 media_svc_error("getpwuid(%d) returns NULL !", uid);
490 grpinfo = getgrnam("users");
491 if(grpinfo == NULL) {
492 media_svc_error("getgrnam(users) returns NULL !");
495 // Compare git_t type and not group name
496 if (grpinfo->gr_gid != userinfo->pw_gid) {
497 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
500 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/phone", userinfo->pw_dir);
503 _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
508 char* _media_svc_get_thumb_external_path(uid_t uid)
510 char *result_psswd = NULL;
511 struct group *grpinfo = NULL;
514 result_psswd = strdup(MEDIA_SVC_THUMB_EXTERNAL_PATH);
515 grpinfo = getgrnam("users");
516 if(grpinfo == NULL) {
517 media_svc_error("getgrnam(users) returns NULL !");
523 struct passwd *userinfo = getpwuid(uid);
524 if(userinfo == NULL) {
525 media_svc_error("getpwuid(%d) returns NULL !", uid);
528 grpinfo = getgrnam("users");
529 if(grpinfo == NULL) {
530 media_svc_error("getgrnam(users) returns NULL !");
533 // Compare git_t type and not group name
534 if (grpinfo->gr_gid != userinfo->pw_gid) {
535 media_svc_error("UID [%d] does not belong to 'users' group!", uid);
538 asprintf(&result_psswd, "%s/data/file-manager-service/.thumb/mmc", userinfo->pw_dir);
541 _mkdir(result_psswd,S_IRWXU | S_IRWXG | S_IRWXO);
546 int _media_svc_save_image(void *image, int size, char *image_path, uid_t uid)
548 media_svc_debug("start save image, path [%s] image size [%d]", image_path, size);
551 media_svc_error("invalid image..");
552 return MS_MEDIA_ERR_INVALID_PARAMETER;
556 if (-1 == statfs(_media_svc_get_thumb_path(uid), &fs)) {
557 media_svc_error("error in statfs");
558 return MS_MEDIA_ERR_INTERNAL;
561 long bsize_kbytes = fs.f_bsize >> 10;
563 if ((bsize_kbytes * fs.f_bavail) < 1024) {
564 media_svc_error("not enought space...");
565 return MS_MEDIA_ERR_INTERNAL;
570 if (image != NULL && size > 0) {
571 fp = fopen(image_path, "w");
574 media_svc_error("failed to open file");
575 return MS_MEDIA_ERR_INTERNAL;
578 nwrite = fwrite(image, 1, size, fp);
579 if (nwrite != size) {
580 media_svc_error("failed to write thumbnail");
582 return MS_MEDIA_ERR_INTERNAL;
587 return MS_MEDIA_ERR_NONE;
591 bool _media_svc_get_thumbnail_path(media_svc_storage_type_e storage_type, char *thumb_path, const char *pathname, const char *img_format, uid_t uid)
593 char savename[MEDIA_SVC_PATHNAME_SIZE] = {0};
594 char file_ext[MEDIA_SVC_FILE_EXT_LEN_MAX + 1] = {0};
595 char *thumb_dir = NULL;
597 char *thumbfile_ext = NULL;
599 thumb_dir = (storage_type == MEDIA_SVC_STORAGE_INTERNAL) ? _media_svc_get_thumb_internal_path(uid) : _media_svc_get_thumb_external_path(uid);
601 memset(file_ext, 0, sizeof(file_ext));
602 if (!_media_svc_get_file_ext(pathname, file_ext)) {
603 media_svc_error("get file ext fail");
607 err = mb_svc_generate_hash_code(pathname, hash, sizeof(hash));
609 media_svc_error("mb_svc_generate_hash_code failed : %d", err);
613 //media_svc_debug("img format is [%s]", img_format);
615 if((strstr(img_format, "jpeg") != NULL) ||(strstr(img_format, "jpg") != NULL) ||(strstr(img_format, "JPG") != NULL)) {
616 thumbfile_ext = "jpg";
617 } else if((strstr(img_format, "png") != NULL) ||(strstr(img_format, "PNG") != NULL)) {
618 thumbfile_ext = "png";
619 } else if((strstr(img_format, "gif") != NULL) ||(strstr(img_format, "GIF") != NULL)) {
620 thumbfile_ext = "gif";
621 } else if((strstr(img_format, "bmp") != NULL) ||(strstr(img_format, "BMP") != NULL)) {
622 thumbfile_ext = "bmp";
624 media_svc_error("Not proper img format");
628 snprintf(savename, sizeof(savename), "%s/.%s-%s.%s", thumb_dir, file_ext, hash, thumbfile_ext);
629 _strncpy_safe(thumb_path, savename, MEDIA_SVC_PATHNAME_SIZE);
630 //media_svc_debug("thumb_path is [%s]", thumb_path);
635 bool _media_svc_get_file_ext(const char *file_path, char *file_ext)
639 for (i = strlen(file_path); i >= 0; i--) {
640 if (file_path[i] == '.') {
641 _strncpy_safe(file_ext, &file_path[i+1], MEDIA_SVC_FILE_EXT_LEN_MAX);
645 if (file_path[i] == '/') {
652 int _media_svc_get_file_time(const char *full_path)
657 memset(&statbuf, 0, sizeof(struct stat));
658 fd = stat(full_path, &statbuf);
660 media_svc_error("stat(%s) fails.", full_path);
661 return MS_MEDIA_ERR_INTERNAL;
664 return statbuf.st_mtime;
667 int _media_svc_set_media_info(media_svc_content_info_s *content_info, media_svc_storage_type_e storage_type,
668 const char *path, media_svc_media_type_e *media_type, bool refresh, drm_content_info_s **drm_contentInfo)
670 int ret = MS_MEDIA_ERR_NONE;
671 char * media_uuid = NULL;
672 char * file_name = NULL;
674 drm_bool_type_e drm_type = DRM_FALSE;
675 char mime_type[256] = {0};
677 ret = __media_svc_malloc_and_strncpy(&content_info->path, path);
678 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
680 memset(&st, 0, sizeof(struct stat));
681 if (stat(path, &st) == 0) {
682 content_info->modified_time = st.st_mtime;
683 content_info->timeline = content_info->modified_time;
684 content_info->size = st.st_size;
685 //media_svc_debug("Modified time : [%d] Size : [%lld]", content_info->modified_time, content_info->size);
687 media_svc_error("stat failed : %s", strerror(errno));
690 /* Set default GPS value before extracting meta information */
691 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
692 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
693 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
695 /* Set filename to title for all media */
697 title = _media_svc_get_title_from_filepath(content_info->path);
699 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
700 if(ret != MS_MEDIA_ERR_NONE)
701 media_svc_error("strcpy error");
704 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
705 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, MEDIA_SVC_TAG_UNKNOWN);
706 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
709 /* Set default value before extracting meta information */
710 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
711 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
713 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, MEDIA_SVC_TAG_UNKNOWN);
714 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
716 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, MEDIA_SVC_TAG_UNKNOWN);
717 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
719 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN);
720 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
722 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN);
723 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
725 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album_artist, MEDIA_SVC_TAG_UNKNOWN);
726 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
728 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, MEDIA_SVC_TAG_UNKNOWN);
729 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
731 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, MEDIA_SVC_TAG_UNKNOWN);
732 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
734 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, MEDIA_SVC_TAG_UNKNOWN);
735 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
737 /* refresh is TRUE when file modified. so only modified_time and size are changed*/
739 media_svc_debug("refresh");
740 return MS_MEDIA_ERR_NONE;
743 content_info->storage_type = storage_type;
744 time(&content_info->added_time);
746 media_uuid = _media_info_generate_uuid();
747 media_svc_retvm_if(media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, "Invalid UUID");
749 ret = __media_svc_malloc_and_strncpy(&content_info->media_uuid, media_uuid);
750 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
752 file_name = g_path_get_basename(path);
753 ret = __media_svc_malloc_and_strncpy(&content_info->file_name, file_name);
754 SAFE_FREE(file_name);
755 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
757 /* if the file is DRM file, drm_type value is DRM_TRUE(1).
758 if drm_contentinfo is not NULL, the file is OMA DRM.*/
759 ret = _media_svc_get_mime_type(path, mime_type, &drm_type, drm_contentInfo);
761 media_svc_error("media_svc_get_mime_type failed : %d (%s)", ret, path);
762 return MS_MEDIA_ERR_INVALID_PARAMETER;
765 media_svc_error("mime [%s]", mime_type);
766 content_info->is_drm = drm_type;
768 ret = _media_svc_get_media_type(path, mime_type, media_type);
769 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
770 if ((*media_type < MEDIA_SVC_MEDIA_TYPE_IMAGE) || (*media_type > MEDIA_SVC_MEDIA_TYPE_OTHER)) {
771 media_svc_error("invalid media_type condition[%d]", *media_type);
772 return MS_MEDIA_ERR_INVALID_PARAMETER;
775 ret = __media_svc_malloc_and_strncpy(&content_info->mime_type, mime_type);
776 media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
778 media_svc_sec_debug("storage[%d], path[%s], media_type[%d]", storage_type, path, *media_type);
780 content_info->media_type = *media_type;
782 content_info->played_count = 0;
783 content_info->last_played_time= 0;
784 content_info->last_played_position= 0;
785 content_info->favourate= 0;
786 content_info->media_meta.rating = 0;
788 return MS_MEDIA_ERR_NONE;
791 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info, media_svc_media_type_e media_type)
793 if (content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE) {
794 media_svc_error("content_info == NULL || media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE");
795 return MS_MEDIA_ERR_INVALID_PARAMETER;
798 char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
799 char description_buf[MEDIA_SVC_METADATA_DESCRIPTION_MAX + 1] = { '\0' };
800 memset(buf, 0x00, sizeof(buf));
801 memset(description_buf, 0x00, sizeof(description_buf));
803 int ret = MS_MEDIA_ERR_NONE;
805 int orient_value = 0;
810 char *path = content_info->path;
812 media_svc_error("path is NULL");
813 return MS_MEDIA_ERR_INVALID_PARAMETER;
816 /* Load an ExifData object from an EXIF file */
817 ed = exif_data_new_from_file(path);
820 media_svc_debug("There is no exif data in [ %s ]", path);
823 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_GPS_LATITUDE_REF) == MS_MEDIA_ERR_NONE) {
824 if (strlen(buf) != 0) {
825 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LATITUDE) == MS_MEDIA_ERR_NONE) {
827 if (strcmp(buf, "S") == 0) {
831 content_info->media_meta.latitude = value;
833 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
836 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
839 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
842 memset(buf, 0x00, sizeof(buf));
844 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_GPS_LONGITUDE_REF) == MS_MEDIA_ERR_NONE) {
845 if (strlen(buf) != 0) {
846 if (__media_svc_get_exif_info(ed, NULL, NULL, &value, EXIF_IFD_GPS, EXIF_TAG_GPS_LONGITUDE) == MS_MEDIA_ERR_NONE) {
847 if (strcmp(buf, "W") == 0) {
850 content_info->media_meta.longitude = value;
852 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
855 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
858 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
861 memset(buf, 0x00, sizeof(buf));
863 if (__media_svc_get_exif_info(ed, description_buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_IMAGE_DESCRIPTION) == MS_MEDIA_ERR_NONE) {
864 if (strlen(description_buf) == 0) {
865 //media_svc_debug("Use 'No description'");
866 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
867 if(ret != MS_MEDIA_ERR_NONE)
868 media_svc_error("strcpy error");
870 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, description_buf);
871 if(ret != MS_MEDIA_ERR_NONE)
872 media_svc_error("strcpy error");
875 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, MEDIA_SVC_TAG_UNKNOWN);
876 if(ret != MS_MEDIA_ERR_NONE)
877 media_svc_error("strcpy error");
880 memset(buf, 0x00, sizeof(buf));
882 if (__media_svc_get_exif_info(ed, buf, NULL, NULL, EXIF_IFD_0, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
883 if (strlen(buf) == 0) {
884 //media_svc_debug("time is NULL");
886 //media_svc_debug("time is %s", buf);
887 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, buf);
888 if(ret != MS_MEDIA_ERR_NONE) {
889 media_svc_error("strcpy error");
891 /* This is same as recorded_date */
892 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, buf);
893 if(ret != MS_MEDIA_ERR_NONE)
894 media_svc_error("strcpy error");
899 /* Get orientation value from exif. */
900 if (__media_svc_get_exif_info(ed, NULL, &orient_value, NULL, EXIF_IFD_0, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
901 if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270) {
902 content_info->media_meta.orientation = orient_value;
904 content_info->media_meta.orientation = 0;
907 content_info->media_meta.orientation = 0;
910 /* Get width value from exif. */
911 if (__media_svc_get_exif_info(ed, NULL, &exif_width, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
912 if (exif_width > 0) {
913 content_info->media_meta.width = exif_width;
915 content_info->media_meta.width = 0;
918 content_info->media_meta.width = 0;
921 /* Get height value from exif. */
922 if (__media_svc_get_exif_info(ed, NULL, &exif_height, NULL, EXIF_IFD_EXIF, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
923 if (exif_height > 0) {
924 content_info->media_meta.height = exif_height;
926 content_info->media_meta.height = 0;
929 content_info->media_meta.height = 0;
932 content_info->media_meta.weather = NULL;
934 if (ed != NULL) exif_data_unref(ed);
936 /* Set filename to title for image media */
938 title = _media_svc_get_title_from_filepath(content_info->path);
940 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
941 if(ret != MS_MEDIA_ERR_NONE)
942 media_svc_error("strcpy error");
945 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
949 /* Extracting thumbnail */
950 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
954 ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
956 media_svc_error("thumbnail_request_from_db failed: %d", ret);
958 //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
961 content_info->media_meta.width = width;
962 content_info->media_meta.height = height;
964 if (STRING_VALID(thumb_path))
965 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
967 content_info->thumbnail_path = NULL;
969 if(ret != MS_MEDIA_ERR_NONE)
970 media_svc_error("strcpy error");
972 return MS_MEDIA_ERR_NONE;
975 int _media_svc_extract_media_metadata(sqlite3 *handle, media_svc_content_info_s *content_info, media_svc_media_type_e media_type, drm_content_info_s *drm_contentInfo, uid_t uid)
977 MMHandleType content = 0;
978 MMHandleType tag = 0;
982 int extracted_field = MEDIA_SVC_EXTRACTED_FIELD_NONE;
983 int mmf_error = MM_ERROR_NONE;
984 bool thumb_extracted_from_drm = FALSE;
985 char *err_attr_name = NULL;
987 bool extract_thumbnail = FALSE;
988 bool append_album = FALSE;
990 double gps_value = 0.0;
991 int ret = MS_MEDIA_ERR_NONE;
992 char *path = content_info->path;
995 /*To do - code for DRM content*/
996 if (content_info->is_drm) {
997 drm_file_type_e drm_file_type;
999 ret = drm_get_file_type(path, &drm_file_type);
1001 media_svc_error("drm_get_file_type falied : %d", ret);
1002 drm_file_type = DRM_TYPE_UNDEFINED;
1005 /* if drm_contentinfo is not NULL, the file is OMA DRM.*/
1006 if (drm_contentInfo != NULL) {
1007 if (drm_file_type == DRM_TYPE_OMA_V1) {
1008 if (strlen(drm_contentInfo->title) > 0) {
1009 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, drm_contentInfo->title);
1010 if(ret != MS_MEDIA_ERR_NONE)
1011 media_svc_error("strcpy error");
1012 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1015 if (strlen(drm_contentInfo->description) > 0) {
1016 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, drm_contentInfo->description);
1017 if(ret != MS_MEDIA_ERR_NONE)
1018 media_svc_error("strcpy error");
1019 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
1021 } else if (drm_file_type == DRM_TYPE_OMA_V2) {
1022 if (strlen(drm_contentInfo->title) > 0) {
1023 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, drm_contentInfo->title);
1024 if(ret != MS_MEDIA_ERR_NONE)
1025 media_svc_error("strcpy error");
1026 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1029 if (strlen(drm_contentInfo->description) > 0) {
1030 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, drm_contentInfo->description);
1031 if(ret != MS_MEDIA_ERR_NONE)
1032 media_svc_error("strcpy error");
1033 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_DESC;
1036 if (strlen(drm_contentInfo->copyright) > 0) {
1037 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, drm_contentInfo->copyright);
1038 if(ret != MS_MEDIA_ERR_NONE)
1039 media_svc_error("strcpy error");
1040 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT;
1043 if (strlen(drm_contentInfo->author) > 0) {
1044 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, drm_contentInfo->author);
1045 if(ret != MS_MEDIA_ERR_NONE)
1046 media_svc_error("strcpy error");
1047 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, drm_contentInfo->author);
1048 if(ret != MS_MEDIA_ERR_NONE)
1049 media_svc_error("strcpy error");
1051 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1052 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_ARTIST;
1056 if (!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) {
1057 title = _media_svc_get_title_from_filepath(path);
1059 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1061 if(ret != MS_MEDIA_ERR_NONE)
1062 media_svc_error("strcpy error");
1064 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1068 return MS_MEDIA_ERR_NONE;
1070 media_svc_debug("Some Not OMA Content's metadata is not incrypted so fileinfo can extracted metadata");
1075 /*Get Content attribute ===========*/
1076 mmf_error = mm_file_create_content_attrs(&content, content_info->path);
1078 if (mmf_error == MM_ERROR_NONE) {
1079 /*Common attribute*/
1080 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_DURATION, &content_info->media_meta.duration, NULL);
1081 if (mmf_error != MM_ERROR_NONE) {
1082 SAFE_FREE(err_attr_name);
1083 media_svc_debug("fail to get duration attr - err(%x)", mmf_error);
1085 //media_svc_debug("duration : %d", content_info->media_meta.duration);
1088 /*Sound/Music attribute*/
1089 if((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1091 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &content_info->media_meta.bitrate, NULL);
1092 if (mmf_error != MM_ERROR_NONE) {
1093 SAFE_FREE(err_attr_name);
1094 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1096 //media_svc_debug("bit rate : %d", content_info->media_meta.bitrate);
1099 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_SAMPLERATE, &content_info->media_meta.samplerate, NULL);
1100 if (mmf_error != MM_ERROR_NONE) {
1101 SAFE_FREE(err_attr_name);
1102 media_svc_debug("fail to get sample rate attr - err(%x)", mmf_error);
1104 //media_svc_debug("sample rate : %d", content_info->media_meta.samplerate);
1107 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_CHANNELS, &content_info->media_meta.channel, NULL);
1108 if (mmf_error != MM_ERROR_NONE) {
1109 SAFE_FREE(err_attr_name);
1110 media_svc_debug("fail to get audio channels attr - err(%x)", mmf_error);
1112 //media_svc_debug("channel : %d", content_info->media_meta.channel);
1115 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITPERSAMPLE, &content_info->media_meta.bitpersample, NULL);
1116 if (mmf_error != MM_ERROR_NONE) {
1117 SAFE_FREE(err_attr_name);
1118 media_svc_debug("fail to get audio bit per sample attr - err(%x)", mmf_error);
1120 media_svc_debug("bitpersample : %d", content_info->media_meta.bitpersample);
1122 }else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) { /*Video attribute*/
1123 int audio_bitrate = 0;
1124 int video_bitrate = 0;
1126 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_AUDIO_BITRATE, &audio_bitrate, NULL);
1127 if (mmf_error != MM_ERROR_NONE) {
1128 SAFE_FREE(err_attr_name);
1129 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1131 //media_svc_debug("audio bit rate : %d", audio_bitrate);
1134 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_BITRATE, &video_bitrate, NULL);
1135 if (mmf_error != MM_ERROR_NONE) {
1136 SAFE_FREE(err_attr_name);
1137 media_svc_debug("fail to get audio bitrate attr - err(%x)", mmf_error);
1139 //media_svc_debug("video bit rate : %d", video_bitrate);
1142 content_info->media_meta.bitrate = audio_bitrate + video_bitrate;
1144 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width, NULL);
1145 if (mmf_error != MM_ERROR_NONE) {
1146 SAFE_FREE(err_attr_name);
1147 media_svc_debug("fail to get video width attr - err(%x)", mmf_error);
1149 //media_svc_debug("width : %d", content_info->media_meta.width);
1152 mmf_error = mm_file_get_attrs(content, &err_attr_name, MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height, NULL);
1153 if (mmf_error != MM_ERROR_NONE) {
1154 SAFE_FREE(err_attr_name);
1155 media_svc_debug("fail to get video height attr - err(%x)", mmf_error);
1157 //media_svc_debug("height : %d", content_info->media_meta.height);
1161 media_svc_error("Not support type");
1162 return MS_MEDIA_ERR_INVALID_PARAMETER;
1165 mmf_error = mm_file_destroy_content_attrs(content);
1166 if (mmf_error != MM_ERROR_NONE) {
1167 media_svc_error("fail to free content attr - err(%x)", mmf_error);
1170 media_svc_error("error in mm_file_create_content_attrs [%d]", mmf_error);
1173 /*Get Content Tag attribute ===========*/
1174 mmf_error = mm_file_create_tag_attrs(&tag, content_info->path);
1176 if (mmf_error == MM_ERROR_NONE) {
1177 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALBUM, &p, &size, NULL);
1178 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ALBUM)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1179 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.album, p);
1180 if(ret != MS_MEDIA_ERR_NONE)
1181 media_svc_error("strcpy error");
1183 //media_svc_debug("album[%d] : %s", size, content_info->media_meta.album);
1185 SAFE_FREE(err_attr_name);
1186 //media_svc_debug("album - unknown");
1189 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTIST, &p, &size, NULL);
1190 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_ARTIST)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1191 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.artist, p);
1192 if(ret != MS_MEDIA_ERR_NONE)
1193 media_svc_error("strcpy error");
1194 //media_svc_debug("artist[%d] : %s", size, content_info->media_meta.artist);
1196 SAFE_FREE(err_attr_name);
1197 //media_svc_debug("artist - unknown");
1200 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_GENRE, &p, &size, NULL);
1201 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_GENRE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1202 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.genre, p);
1203 if(ret != MS_MEDIA_ERR_NONE)
1204 media_svc_error("strcpy error");
1206 //media_svc_debug("genre : %s", content_info->media_meta.genre);
1207 /* If genre is Ringtone, it's categorized as sound. But this logic is commented */
1209 if ((strcasecmp("Ringtone", p) == 0) | (strcasecmp("Alert tone", p) == 0)) {
1210 content_info->media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1214 SAFE_FREE(err_attr_name);
1215 //media_svc_debug("genre - unknown");
1218 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TITLE, &p, &size, NULL);
1219 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_TITLE)) && (mmf_error == MM_ERROR_NONE) && (size > 0)/* && (!isspace(*p))*/) {
1221 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1222 if(ret != MS_MEDIA_ERR_NONE)
1223 media_svc_error("strcpy error");
1225 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_TITLE;
1230 for(idx = 0; idx < size; idx++) {
1232 media_svc_debug("SPACE [%s]", p);
1236 media_svc_debug("Not SPACE [%s]", p);
1237 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, p);
1238 if(ret != MS_MEDIA_ERR_NONE)
1239 media_svc_error("strcpy error");
1246 media_svc_debug("Can't extract title. All string is space");
1247 title = _media_svc_get_title_from_filepath(content_info->path);
1249 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1250 if(ret != MS_MEDIA_ERR_NONE)
1251 media_svc_error("strcpy error");
1254 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1259 SAFE_FREE(err_attr_name);
1260 title = _media_svc_get_title_from_filepath(content_info->path);
1262 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1263 if(ret != MS_MEDIA_ERR_NONE)
1264 media_svc_error("strcpy error");
1267 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1271 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DESCRIPTION, &p, &size, NULL);
1272 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1273 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.description, p);
1274 if(ret != MS_MEDIA_ERR_NONE)
1275 media_svc_error("strcpy error");
1276 //media_svc_debug("desc : %s", content_info->media_meta.description);
1278 SAFE_FREE(err_attr_name);
1281 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RECDATE, &p, &size, NULL);
1282 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_DESC)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1283 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.recorded_date, p);
1284 if(ret != MS_MEDIA_ERR_NONE) {
1285 media_svc_error("strcpy error");
1287 /* This is same as datetaken */
1288 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.datetaken, p);
1289 if(ret != MS_MEDIA_ERR_NONE)
1290 media_svc_error("strcpy error");
1292 //media_svc_debug("Recorded date : %s", content_info->media_meta.recorded_date);
1294 SAFE_FREE(err_attr_name);
1297 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_AUTHOR, &p, &size, NULL);
1298 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_AUTHOR)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1299 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.composer, p);
1300 if(ret != MS_MEDIA_ERR_NONE)
1301 media_svc_error("strcpy error");
1302 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1303 //media_svc_debug("extract composer from content : %s", content_info->media_meta.composer);
1305 //media_svc_debug("composer - unknown");
1306 SAFE_FREE(err_attr_name);
1309 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_COPYRIGHT, &p, &size, NULL);
1310 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_COPYRIGHT)) && (mmf_error == MM_ERROR_NONE) && (size > 0)) {
1311 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.copyright, p);
1312 if(ret != MS_MEDIA_ERR_NONE)
1313 media_svc_error("strcpy error");
1314 extracted_field |= MEDIA_SVC_EXTRACTED_FIELD_AUTHOR;
1315 //media_svc_debug("extract copyright from content : %s", content_info->media_meta.copyright);
1317 //media_svc_debug("copyright - unknown");
1318 SAFE_FREE(err_attr_name);
1321 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_TRACK_NUM, &p, &size, NULL);
1322 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1323 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.track_num, p);
1324 if(ret != MS_MEDIA_ERR_NONE)
1325 media_svc_error("strcpy error");
1327 SAFE_FREE(err_attr_name);
1330 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_DATE, &p, &size, NULL);
1331 if ((!(extracted_field & MEDIA_SVC_EXTRACTED_FIELD_YEAR)) && (mmf_error == MM_ERROR_NONE) && (size == 4)) {
1333 if((p != NULL) && (sscanf( p, "%d", &year))) {
1334 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.year, p);
1335 if(ret != MS_MEDIA_ERR_NONE)
1336 media_svc_error("strcpy error");
1338 media_svc_debug("Wrong Year Information [%s]", p);
1341 SAFE_FREE(err_attr_name);
1344 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_RATING, &p, &size, NULL);
1345 if ((mmf_error == MM_ERROR_NONE) && (size > 0)) {
1346 content_info->media_meta.rating = atoi(p);
1348 SAFE_FREE(err_attr_name);
1349 content_info->media_meta.rating = 0;
1352 if((media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) || (media_type == MEDIA_SVC_MEDIA_TYPE_MUSIC)) {
1353 /*Initialize album_id to 0. below code will set the album_id*/
1354 content_info->album_id = album_id;
1355 ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
1357 if (ret != MS_MEDIA_ERR_NONE) {
1358 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
1359 media_svc_debug("album does not exist. So start to make album art");
1360 extract_thumbnail = TRUE;
1361 append_album = TRUE;
1363 extract_thumbnail = FALSE;
1364 append_album = FALSE;
1367 content_info->album_id = album_id;
1368 append_album = FALSE;
1370 if((!strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) ||
1371 (!strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN)))) {
1373 media_svc_debug("Unknown album or artist already exists. Extract thumbnail for Unknown.");
1374 extract_thumbnail = TRUE;
1377 media_svc_debug("album already exists. don't need to make album art");
1378 ret = _media_svc_get_album_art_by_album_id(handle, album_id, &content_info->thumbnail_path);
1379 media_svc_retv_del_if((ret != MS_MEDIA_ERR_NONE) && (ret != MS_MEDIA_ERR_DB_NO_RECORD), ret, content_info);
1380 extract_thumbnail = FALSE;
1384 if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE)) {
1385 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
1386 if (mmf_error != MM_ERROR_NONE) {
1387 media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
1388 SAFE_FREE(err_attr_name);
1390 //media_svc_debug("artwork size1 [%d]", size);
1393 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
1394 if (mmf_error != MM_ERROR_NONE) {
1395 media_svc_error("fail to get artwork size - err(%x)", mmf_error);
1396 SAFE_FREE(err_attr_name);
1398 //media_svc_debug("artwork size2 [%d]", size);
1400 if (image != NULL && size > 0) {
1401 bool result = FALSE;
1402 int ret = MS_MEDIA_ERR_NONE;
1403 char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
1404 int artwork_mime_size = -1;
1406 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
1407 if ((mmf_error == MM_ERROR_NONE) && (artwork_mime_size > 0)) {
1408 result = _media_svc_get_thumbnail_path(content_info->storage_type, thumb_path, content_info->path, p, uid);
1409 if (result == FALSE) {
1410 media_svc_error("Fail to Get Thumbnail Path");
1413 SAFE_FREE(err_attr_name);
1416 if(strlen(thumb_path) > 0)
1418 ret = _media_svc_save_image(image, size, thumb_path, uid);
1419 if (ret != MS_MEDIA_ERR_NONE) {
1420 media_svc_error("Fail to Save Thumbnail Image");
1423 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1424 if(ret != MS_MEDIA_ERR_NONE)
1425 media_svc_error("strcpy error");
1431 if(append_album == TRUE) {
1433 if((strncmp(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))) &&
1434 (strncmp(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN, strlen(MEDIA_SVC_TAG_UNKNOWN))))
1435 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
1437 ret = _media_svc_append_album(handle, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
1439 content_info->album_id = album_id;
1441 } else if(media_type == MEDIA_SVC_MEDIA_TYPE_VIDEO) {
1442 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LONGITUDE, &gps_value, NULL);
1443 if (mmf_error == MM_ERROR_NONE) {
1444 if (gps_value == 0.0)
1445 content_info->media_meta.longitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1447 content_info->media_meta.longitude = gps_value;
1449 SAFE_FREE(err_attr_name);
1452 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_LATIDUE, &gps_value, NULL);
1453 if (mmf_error == MM_ERROR_NONE) {
1454 if (gps_value == 0.0)
1455 content_info->media_meta.latitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1457 content_info->media_meta.latitude = gps_value;
1459 SAFE_FREE(err_attr_name);
1462 mmf_error = mm_file_get_attrs(tag, &err_attr_name, MM_FILE_TAG_ALTIDUE, &gps_value, NULL);
1463 if (mmf_error == MM_ERROR_NONE) {
1464 if (gps_value == 0.0)
1465 content_info->media_meta.altitude = MEDIA_SVC_DEFAULT_GPS_VALUE;
1467 content_info->media_meta.altitude = gps_value;
1469 SAFE_FREE(err_attr_name);
1472 //if ((!thumb_extracted_from_drm) && (extract_thumbnail == TRUE))
1474 /* Extracting thumbnail */
1475 char thumb_path[MEDIA_SVC_PATHNAME_SIZE + 1] = {0, };
1479 ret = thumbnail_request_from_db_with_size(content_info->path, thumb_path, sizeof(thumb_path), &width, &height);
1481 media_svc_error("thumbnail_request_from_db failed: %d", ret);
1483 //media_svc_debug("thumbnail_request_from_db success: %s", thumb_path);
1486 ret = __media_svc_malloc_and_strncpy(&content_info->thumbnail_path, thumb_path);
1487 if(ret != MS_MEDIA_ERR_NONE)
1488 media_svc_error("strcpy error");
1490 if (content_info->media_meta.width <= 0) content_info->media_meta.width = width;
1491 if (content_info->media_meta.height <= 0) content_info->media_meta.height = height;
1496 mmf_error = mm_file_destroy_tag_attrs(tag);
1497 if (mmf_error != MM_ERROR_NONE) {
1498 media_svc_error("fail to free tag attr - err(%x)", mmf_error);
1501 /* in case of file size 0, MMFW Can't parsting tag info but add it to Music DB. */
1503 media_svc_error("no tag information");
1505 title = _media_svc_get_title_from_filepath(content_info->path);
1507 ret = __media_svc_malloc_and_strncpy(&content_info->media_meta.title, title);
1508 if(ret != MS_MEDIA_ERR_NONE)
1509 media_svc_error("strcpy error");
1512 media_svc_error("Can't extract title from filepath [%s]", content_info->path);
1515 content_info->album_id = album_id;
1518 return MS_MEDIA_ERR_NONE;
1521 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
1523 media_svc_retm_if(content_info == NULL, "content info is NULL");
1525 /* Delete media_svc_content_info_s */
1526 SAFE_FREE(content_info->media_uuid);
1527 SAFE_FREE(content_info->path);
1528 SAFE_FREE(content_info->file_name);
1529 SAFE_FREE(content_info->mime_type);
1530 SAFE_FREE(content_info->folder_uuid);
1531 SAFE_FREE(content_info->thumbnail_path);
1533 /* Delete media_svc_content_meta_s */
1534 SAFE_FREE(content_info->media_meta.title);
1535 SAFE_FREE(content_info->media_meta.album);
1536 SAFE_FREE(content_info->media_meta.artist);
1537 SAFE_FREE(content_info->media_meta.album_artist);
1538 SAFE_FREE(content_info->media_meta.genre);
1539 SAFE_FREE(content_info->media_meta.composer);
1540 SAFE_FREE(content_info->media_meta.year);
1541 SAFE_FREE(content_info->media_meta.recorded_date);
1542 SAFE_FREE(content_info->media_meta.copyright);
1543 SAFE_FREE(content_info->media_meta.track_num);
1544 SAFE_FREE(content_info->media_meta.description);
1545 SAFE_FREE(content_info->media_meta.datetaken);
1546 SAFE_FREE(content_info->media_meta.weather);
1548 SAFE_FREE(content_info->media_meta.title_pinyin);
1549 SAFE_FREE(content_info->media_meta.album_pinyin);
1550 SAFE_FREE(content_info->media_meta.artist_pinyin);
1551 SAFE_FREE(content_info->media_meta.album_artist_pinyin);
1552 SAFE_FREE(content_info->media_meta.genre_pinyin);
1553 SAFE_FREE(content_info->media_meta.composer_pinyin);
1554 SAFE_FREE(content_info->media_meta.copyright_pinyin);
1555 SAFE_FREE(content_info->media_meta.description_pinyin);
1560 int _media_svc_get_store_type_by_path(const char *path, media_svc_storage_type_e *storage_type)
1562 if(STRING_VALID(path))
1564 if(strncmp(path, MEDIA_SVC_PATH_PHONE, strlen(MEDIA_SVC_PATH_PHONE)) == 0)
1566 *storage_type = MEDIA_SVC_STORAGE_INTERNAL;
1568 else if(strncmp (path, MEDIA_SVC_PATH_MMC, strlen(MEDIA_SVC_PATH_MMC)) == 0)
1570 *storage_type = MEDIA_SVC_STORAGE_EXTERNAL;
1575 media_svc_error("INVALID parameter");
1576 return MS_MEDIA_ERR_INVALID_PARAMETER;
1579 return MS_MEDIA_ERR_NONE;
1582 char *_media_svc_replace_path(char *s, const char *olds, const char *news)
1585 size_t i, count = 0;
1586 size_t oldlen = strlen(olds); if (oldlen < 1) return s;
1587 size_t newlen = strlen(news);
1589 if (newlen != oldlen) {
1590 for (i = 0; s[i] != '\0';) {
1591 if (memcmp(&s[i], olds, oldlen) == 0) count++, i += oldlen;
1594 } else i = strlen(s);
1597 result = (char *) calloc(1, i + 1 + count * (newlen - oldlen));
1598 if (result == NULL) return NULL;
1602 if (memcmp(s, olds, oldlen) == 0) {
1603 memcpy(sr, news, newlen);
1606 } else *sr++ = *s++;
1614 bool _media_svc_is_drm_file(const char *path)
1616 #ifdef __SUPPORT_DRM
1618 drm_bool_type_e is_drm_file = DRM_UNKNOWN;
1620 ret = drm_is_drm_file(path,&is_drm_file);
1621 if(DRM_RETURN_SUCCESS == ret && DRM_TRUE == is_drm_file)
1627 int _media_svc_get_mime_in_drm_info(const char *path, char *mime, drm_content_info_s **drm_contentInfo)
1629 #ifdef __SUPPORT_DRM
1630 int ret = MS_MEDIA_ERR_NONE;
1631 drm_file_type_e file_type = DRM_TYPE_UNDEFINED;
1633 if (path == NULL || mime == NULL)
1634 return MS_MEDIA_ERR_INVALID_PARAMETER;
1636 ret = drm_get_file_type(path, &file_type);
1637 if (ret != DRM_RETURN_SUCCESS) {
1638 media_svc_error("drm_get_file_type() failed : %d", ret);
1639 return MS_MEDIA_ERR_INTERNAL;
1641 if (file_type == DRM_TYPE_OMA_V1
1642 || file_type == DRM_TYPE_OMA_V2
1643 || file_type == DRM_TYPE_OMA_PD) {
1644 *drm_contentInfo = malloc(sizeof(drm_content_info_s));
1645 memset(*drm_contentInfo,0x0,sizeof(drm_content_info_s));
1646 ret = drm_get_content_info(path, *drm_contentInfo);
1647 if (ret != DRM_RETURN_SUCCESS) {
1648 media_svc_error("drm_svc_get_content_info() fails :%d ", ret);
1649 free(*drm_contentInfo);
1650 *drm_contentInfo = NULL;
1651 return MS_MEDIA_ERR_INTERNAL;
1654 if (STRING_VALID((*drm_contentInfo)->mime_type)) {
1655 strncpy(mime,(*drm_contentInfo)->mime_type, MEDIA_SVC_METADATA_LEN_MAX);
1656 media_svc_debug("DRM contentType : %s",(*drm_contentInfo)->mime_type);
1658 free(*drm_contentInfo);
1659 *drm_contentInfo = NULL;
1660 return MS_MEDIA_ERR_INTERNAL;
1663 media_svc_error("THIS IS DRM BUT YOU SHOULD USE API OF AUL LIBRARY");
1664 *drm_contentInfo = NULL;
1665 return MS_MEDIA_ERR_INTERNAL;
1669 return MS_MEDIA_ERR_NONE;
1671 *drm_contentInfo = NULL;
1672 return MS_MEDIA_ERR_INTERNAL;
1676 int _media_svc_get_content_type_from_mime(const char * path, const char * mimetype, int * category)
1681 *category = MEDIA_SVC_CATEGORY_UNKNOWN;
1683 //media_svc_debug("mime type : %s", mimetype);
1685 /*categorize from mimetype */
1686 for (i = 0; i < CONTENT_TYPE_NUM; i++) {
1687 if (strstr(mimetype, content_category[i].content_type) != NULL) {
1688 *category = (*category | content_category[i].category_by_mime);
1693 /*in application type, exitst sound file ex) x-smafs */
1694 if (*category & MEDIA_SVC_CATEGORY_ETC) {
1695 int prefix_len = strlen(content_category[0].content_type);
1697 for (i = 0; i < SOUND_MIME_NUM; i++) {
1698 if (strstr(mimetype + prefix_len, sound_mime_table[i]) != NULL) {
1699 *category ^= MEDIA_SVC_CATEGORY_ETC;
1700 *category |= MEDIA_SVC_CATEGORY_SOUND;
1706 /*check music file in soun files. */
1707 if (*category & MEDIA_SVC_CATEGORY_SOUND) {
1708 int prefix_len = strlen(content_category[0].content_type) + 1;
1710 //media_svc_error("mime_type : %s", mimetype + prefix_len);
1712 for (i = 0; i < MUSIC_MIME_NUM; i++) {
1713 if (strcmp(mimetype + prefix_len, music_mime_table[i]) == 0) {
1714 *category ^= MEDIA_SVC_CATEGORY_SOUND;
1715 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1720 /*m3u file is playlist but mime type is "audio/x-mpegurl". but It has to be classified into MS_CATEGORY_ETC since playlist is not a sound track*/
1721 if(strncasecmp(mimetype, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0) {
1722 *category ^= MEDIA_SVC_CATEGORY_SOUND;
1723 *category |= MEDIA_SVC_CATEGORY_ETC;
1725 } else if (*category & MEDIA_SVC_CATEGORY_VIDEO) {
1726 /*some video files don't have video stream. in this case it is categorize as music. */
1728 /*"3gp" and "mp4" must check video stream and then categorize in directly. */
1729 ext = strrchr(path, '.');
1731 if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
1735 err = mm_file_get_stream_info(path, &audio, &video);
1737 if (audio > 0 && video == 0) {
1738 *category ^= MEDIA_SVC_CATEGORY_VIDEO;
1739 *category |= MEDIA_SVC_CATEGORY_MUSIC;
1746 //media_svc_debug("category_from_ext : %d", *category);
1752 drm_contentifo is not NULL, if the file is OMA DRM.
1753 If the file is not OMA DRM, drm_contentinfo must be NULL.
1755 int _media_svc_get_mime_type(const char *path, char *mimetype, drm_bool_type_e *is_drm, drm_content_info_s **drm_contentInfo)
1757 int ret = MS_MEDIA_ERR_NONE;
1760 return MS_MEDIA_ERR_INVALID_PARAMETER;
1762 #ifdef __SUPPORT_DRM
1763 /* In case of drm file. */
1764 if (_media_svc_is_drm_file(path)) {
1766 ret = _media_svc_get_mime_in_drm_info(path, mimetype, drm_contentInfo);
1767 if (ret == MS_MEDIA_ERR_NONE) {
1772 *is_drm = DRM_FALSE;
1773 *drm_contentInfo = NULL;
1776 /*in case of normal files or failure to get mime in drm */
1777 if (aul_get_mime_from_file(path, mimetype, 255) < 0) {
1778 media_svc_error("aul_get_mime_from_file fail");
1779 return MS_MEDIA_ERR_INTERNAL;
1782 return MS_MEDIA_ERR_NONE;
1785 int _media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
1787 int ret = MS_MEDIA_ERR_NONE;
1790 media_svc_media_type_e type;
1792 ret = _media_svc_get_content_type_from_mime(path, mime_type, &category);
1794 media_svc_error("_media_svc_get_content_type_from_mime failed : %d", ret);
1797 if (category & MEDIA_SVC_CATEGORY_SOUND) type = MEDIA_SVC_MEDIA_TYPE_SOUND;
1798 else if (category & MEDIA_SVC_CATEGORY_MUSIC) type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
1799 else if (category & MEDIA_SVC_CATEGORY_IMAGE) type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
1800 else if (category & MEDIA_SVC_CATEGORY_VIDEO) type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
1801 else type = MEDIA_SVC_MEDIA_TYPE_OTHER;
1808 int _media_svc_get_pinyin_str(const char *src_str, char **pinyin_str)
1810 int ret = MS_MEDIA_ERR_NONE;
1812 pinyin_name_s *pinyinname = NULL;
1815 if(!STRING_VALID(src_str))
1817 media_svc_debug("String is invalid");
1821 ret = _media_svc_convert_chinese_to_pinyin(src_str, &pinyinname, &size);
1822 if (ret == MS_MEDIA_ERR_NONE)
1824 if(STRING_VALID(pinyinname[0].pinyin_name))
1825 *pinyin_str = strdup(pinyinname[0].pinyin_name);
1827 *pinyin_str = strdup(src_str); //Return Original Non China Character
1830 _media_svc_pinyin_free(pinyinname, size);
1835 bool _media_svc_check_pinyin_support(void)