Correct the type
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-util.c
index f4997a6..a4bed31 100644 (file)
 #include <aul/aul.h>
 #include <mm_file.h>
 #include <libexif/exif-data.h>
-#include <media-util.h>
 #include <uuid/uuid.h>
-#include <mm_util_magick.h>
 #include <media-thumbnail.h>
+#include <media-util-user.h>
 #include "media-svc-util.h"
 #include "media-svc-db-utils.h"
 #include "media-svc-debug.h"
 #include <libxml/HTMLparser.h>
 #include <dlfcn.h>
 
-#define MEDIA_SVC_FILE_EXT_LEN_MAX                             6                       /**< Maximum file ext lenth*/
+#define MEDIA_SVC_FILE_EXT_LEN_MAX 6
 
 #define MUSIC_MIME_NUM 29
 #define SOUND_MIME_NUM 2
 #define MIME_LENGTH 50
-#define MEDIA_SVC_DEFAULT_FORMAT_LEN 19
 #define IMAGE_PREFIX "image/"
-#define IMAGE_PREFIX_LEN 6
 #define AUDIO_PREFIX "audio/"
-#define AUDIO_PREFIX_LEN 6
 #define VIDEO_PREFIX "video/"
-#define VIDEO_PREFIX_LEN 6
+#define PREFIX_LEN 6
 
 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
 #define MEDIA_SVC_PDF_BUF_SIZE 256
@@ -101,7 +97,6 @@ static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
        "x-ape",
        "x-ms-asx",
        "vnd.rn-realaudio",
-
        "x-vorbis", /*alias of audio/x-vorbis+ogg*/
        "vorbis", /*alias of audio/x-vorbis+ogg*/
        "x-oggflac",
@@ -133,34 +128,42 @@ RETRY_GEN:
        return g_strdup(uuid_unparsed);
 }
 
-static int __media_svc_get_exif_info(ExifData *ed, char *buf, int *i_value, ExifTag tagtype)
+static char * __media_svc_get_exif_datetaken(ExifData *ed)
 {
        ExifEntry *entry;
-       ExifByteOrder mByteOrder;
+       char tmp[MEDIA_SVC_METADATA_LEN_MAX + 1] = { 0, };
 
-       media_svc_retv_if(!ed, MS_MEDIA_ERR_INVALID_PARAMETER);
+       media_svc_retv_if(!ed, NULL);
 
-       entry = exif_data_get_entry(ed, tagtype);
-       media_svc_retv_if(!entry, MS_MEDIA_ERR_NONE);
-
-       switch (tagtype) {
-       case EXIF_TAG_ORIENTATION:
-       case EXIF_TAG_PIXEL_X_DIMENSION:
-       case EXIF_TAG_PIXEL_Y_DIMENSION:
-               media_svc_retvm_if(!i_value, MS_MEDIA_ERR_INVALID_PARAMETER, "i_value is NULL");
-
-               mByteOrder = exif_data_get_byte_order(ed);
-               short exif_value = exif_get_short(entry->data, mByteOrder);
-               *i_value = (int)exif_value;
-               break;
-       default:
-               media_svc_retvm_if(!buf, MS_MEDIA_ERR_INVALID_PARAMETER, "buf is NULL");
-
-               exif_entry_get_value(entry, buf, MEDIA_SVC_METADATA_LEN_MAX);
-               buf[strlen(buf)] = '\0';
+       entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME_ORIGINAL);
+       if (entry) {
+               exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
+               if (strlen(tmp) > 0)
+                       return g_strdup(tmp);
        }
 
-       return MS_MEDIA_ERR_NONE;
+       entry = exif_data_get_entry(ed, EXIF_TAG_DATE_TIME);
+       if (entry) {
+               exif_entry_get_value(entry, tmp, MEDIA_SVC_METADATA_LEN_MAX);
+               if (strlen(tmp) > 0)
+                       return g_strdup(tmp);
+       }
+
+       return NULL;
+}
+
+static bool __media_svc_get_exif_short(ExifData *ed, ExifTag tagtype, unsigned short *value)
+{
+       ExifEntry *entry;
+
+       media_svc_retv_if(!ed, false);
+       media_svc_retvm_if(!value, false, "value is NULL");
+
+       entry = exif_data_get_entry(ed, tagtype);
+       media_svc_retv_if(!entry, false);
+       *value = exif_get_short(entry->data, exif_data_get_byte_order(ed));
+
+       return true;
 }
 
 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
@@ -174,36 +177,36 @@ static int __media_svc_get_media_type(const char *path, const char *mime_type, m
        media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
 
        /* Image */
-       if (strncmp(mime_type, IMAGE_PREFIX, IMAGE_PREFIX_LEN) == 0) {
+       if (strncmp(mime_type, IMAGE_PREFIX, PREFIX_LEN) == 0) {
                *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
                return MS_MEDIA_ERR_NONE;
        }
 
        /* Audio */
-       if (strncmp(mime_type, AUDIO_PREFIX, AUDIO_PREFIX_LEN) == 0) {
+       if (strncmp(mime_type, AUDIO_PREFIX, PREFIX_LEN) == 0) {
                *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
 
                for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
-                       if (strcmp(mime_type + AUDIO_PREFIX_LEN, music_mime_table[idx]) == 0) {
+                       if (strcmp(mime_type + PREFIX_LEN, music_mime_table[idx]) == 0) {
                                *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
                                break;
                        }
                }
 
                /* audio/x-mpegurl : .m3u file (playlist file) */
-               if (strcmp(mime_type + AUDIO_PREFIX_LEN, "x-mpegurl") == 0)
+               if (strcmp(mime_type + PREFIX_LEN, "x-mpegurl") == 0)
                        *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
 
                return MS_MEDIA_ERR_NONE;
        }
 
        /* Video */
-       if (strncmp(mime_type, VIDEO_PREFIX, VIDEO_PREFIX_LEN) == 0) {
+       if (strncmp(mime_type, VIDEO_PREFIX, PREFIX_LEN) == 0) {
                *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
 
                /*some video files don't have video stream. in this case it is categorize as music. */
-               if (strcmp(mime_type + VIDEO_PREFIX_LEN, "3gpp") == 0 ||
-                       strcmp(mime_type + VIDEO_PREFIX_LEN, "mp4") == 0) {
+               if (strcmp(mime_type + PREFIX_LEN, "3gpp") == 0 ||
+                       strcmp(mime_type + PREFIX_LEN, "mp4") == 0) {
                        if (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
                                if (audio > 0 && video == 0)
                                        *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
@@ -259,7 +262,7 @@ static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
 
        for (i = strlen(file_path); i >= 0; i--) {
                if (file_path[i] == '.') {
-                       SAFE_STRLCPY(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
+                       g_strlcpy(file_ext, &file_path[i + 1], MEDIA_SVC_FILE_EXT_LEN_MAX);
                        return true;
                }
 
@@ -269,25 +272,6 @@ static bool __media_svc_get_file_ext(const char *file_path, char *file_ext)
        return false;
 }
 
-static int __media_svc_safe_atoi(char *buffer, int *si)
-{
-       char *end = NULL;
-       errno = 0;
-       media_svc_retvm_if(buffer == NULL || si == NULL, MS_MEDIA_ERR_INTERNAL, "invalid parameter");
-
-       const long sl = strtol(buffer, &end, 10);
-
-       media_svc_retvm_if(end == buffer, MS_MEDIA_ERR_INTERNAL, "not a decimal number");
-       media_svc_retvm_if('\0' != *end, MS_MEDIA_ERR_INTERNAL, "extra characters at end of input: %s", end);
-       media_svc_retvm_if((LONG_MIN == sl || LONG_MAX == sl) && (ERANGE == errno), MS_MEDIA_ERR_INTERNAL, "out of range of type long");
-       media_svc_retvm_if(sl > INT_MAX, MS_MEDIA_ERR_INTERNAL, "greater than INT_MAX");
-       media_svc_retvm_if(sl < INT_MIN, MS_MEDIA_ERR_INTERNAL, "less than INT_MIN");
-
-       *si = (int)sl;
-
-       return MS_MEDIA_ERR_NONE;
-}
-
 static int __media_svc_save_image(unsigned char *image, unsigned int size, char *image_path, uid_t uid)
 {
        int ret = MS_MEDIA_ERR_NONE;
@@ -318,22 +302,18 @@ static int __media_svc_save_image(unsigned char *image, unsigned int size, char
        return MS_MEDIA_ERR_NONE;
 }
 
-static char *__media_svc_get_title_from_filepath(const char *path)
+static char *__media_svc_get_title_from_filename(const char *filename)
 {
-       char *filename = NULL;
        char *title = NULL;
        char *last_dot = NULL;
 
-       media_svc_retvm_if(!STRING_VALID(path), NULL, "Invalid path");
-
-       filename = g_path_get_basename(path);
+       media_svc_retvm_if(!STRING_VALID(filename), g_strdup(MEDIA_SVC_TAG_UNKNOWN), "Invalid path");
 
        last_dot = strrchr(filename, '.');
        if (last_dot) {
                title = g_strndup(filename, last_dot - filename);
-               g_free(filename);
        } else {
-               title = filename;
+               title = g_strdup(filename);
        }
 
        media_svc_debug("extract title is [%s]", title);
@@ -370,12 +350,12 @@ static int __media_svc_get_thumbnail_path(char *thumb_path, const char *pathname
 
        if (img_format) {
                /* 'img_format' is mime-type */
-               if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == IMAGE_PREFIX_LEN) {
+               if (!g_str_has_prefix(img_format, IMAGE_PREFIX) || strlen(img_format) == PREFIX_LEN) {
                        media_svc_error("Not proper img format");
                        return MS_MEDIA_ERR_INTERNAL;
                }
 
-               snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + IMAGE_PREFIX_LEN);
+               snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.%s", thumb_dir, file_ext, hash, img_format + PREFIX_LEN);
        } else {
                if (strcasecmp(file_ext, "PNG") == 0)
                        snprintf(thumb_path, MEDIA_SVC_PATHNAME_SIZE, "%s/.%s-%s.png", thumb_dir, file_ext, hash);
@@ -403,17 +383,15 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
        int ret = MS_MEDIA_ERR_NONE;
        char mime_type[256] = {0, };
        media_svc_media_type_e media_type;
+       struct stat st = { 0, };
 
        media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
 
        content_info->path = g_strdup(path);
-       media_svc_retv_del_if(content_info->path == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
+       content_info->file_name = g_path_get_basename(path);
 
-       struct stat st;
-       memset(&st, 0, sizeof(struct stat));
        if (stat(path, &st) == 0) {
                content_info->modified_time = st.st_mtime;
-               content_info->timeline = content_info->modified_time;
                content_info->size = st.st_size;
        } else {
                media_svc_stderror("stat failed");
@@ -428,14 +406,9 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
        content_info->storage_uuid = g_strdup(storage_id);
        media_svc_retv_del_if(content_info->storage_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       time(&content_info->added_time);
-
        content_info->media_uuid = __media_info_generate_uuid();
        media_svc_retv_del_if(content_info->media_uuid == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       content_info->file_name = g_path_get_basename(path);
-       media_svc_retv_del_if(content_info->file_name == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
-
        ret = __media_svc_get_mime_type(path, mime_type);
        media_svc_retv_del_if(ret != MS_MEDIA_ERR_NONE, ret, content_info);
 
@@ -447,19 +420,18 @@ int _media_svc_set_media_info(media_svc_content_info_s *content_info, const char
        content_info->mime_type = g_strdup(mime_type);
        media_svc_retv_del_if(content_info->mime_type == NULL, MS_MEDIA_ERR_INTERNAL, content_info);
 
-       media_svc_sec_debug("path[%s], media_type[%d]", path, media_type);
+       media_svc_sec_debug("path[%s], media_type[%d]", content_info->path, media_type);
 
        content_info->media_type = media_type;
 
        return MS_MEDIA_ERR_NONE;
 }
 
-static char * __media_svc_get_title(MMHandleType tag, const char *path)
+static char * __media_svc_get_title(MMHandleType tag, const char *filename)
 {
        int ret = FILEINFO_ERROR_NONE;
        char *p = NULL;
        int size = 0;
-       char *title = NULL;
 
        if (tag) {
                ret = mm_file_get_attrs(tag, MM_FILE_TAG_TITLE, &p, &size, NULL);
@@ -471,109 +443,58 @@ static char * __media_svc_get_title(MMHandleType tag, const char *path)
                }
        }
 
-       title = __media_svc_get_title_from_filepath(path);
-       if (title)
-               return title;
-
-       media_svc_error("Can't extract title");
-
-       return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       return __media_svc_get_title_from_filename(filename);
 }
 
-char * _media_svc_get_title_by_path(const char *path)
+char * _media_svc_get_title_from_filename(const char *filename)
 {
-       /* No MMHandleType in media-svc.c */
-       return __media_svc_get_title(NULL, path);
+       return __media_svc_get_title_from_filename(filename);
 }
 
 int _media_svc_extract_image_metadata(media_svc_content_info_s *content_info)
 {
-       int orient_value = 0;
-       int exif_width = 0;
-       int exif_height = 0;
+       unsigned short orient_value = 0;
+       unsigned short exif_width = 0;
+       unsigned short exif_height = 0;
        ExifData *ed = NULL;
-       bool has_datetaken = false;
-       char *path = NULL;
-
-       char buf[MEDIA_SVC_METADATA_LEN_MAX + 1] = { '\0' };
 
        media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid content_info");
-       media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type [%d]", content_info->media_type);
+       media_svc_retvm_if(content_info->media_type != MEDIA_SVC_MEDIA_TYPE_IMAGE, MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid media_type");
        media_svc_retvm_if(!STRING_VALID(content_info->path), MS_MEDIA_ERR_INVALID_PARAMETER, "Invalid path");
 
-       path = content_info->path;
-       content_info->media_meta.title = __media_svc_get_title(NULL, path);
-
-       /* Not used. But to preserved the behavior, set MEDIA_SVC_TAG_UNKNOWN. */
-       content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-       content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-       content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-       content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-       content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-       content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
 
        /* Load an ExifData object from an EXIF file */
-       ed = exif_data_new_from_file(path);
+       ed = exif_data_new_from_file(content_info->path);
        if (!ed) {
-               media_svc_sec_debug("There is no exif data in [ %s ]", path);
+               media_svc_sec_debug("There is no exif data in [ %s ]", content_info->path);
                goto GET_WIDTH_HEIGHT;
        }
 
-       memset(buf, 0x00, sizeof(buf));
-       if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, EXIF_TAG_DATE_TIME_ORIGINAL) == MS_MEDIA_ERR_NONE) {
-               if (strlen(buf) > 0) {
-                       has_datetaken = true;
-                       content_info->media_meta.datetaken = g_strdup(buf);
-
-                       /* This is same as recorded_date */
-                       content_info->media_meta.recorded_date = g_strdup(buf);
-               }
-       }
+       content_info->media_meta.datetaken = __media_svc_get_exif_datetaken(ed);
 
-       memset(buf, 0x00, sizeof(buf));
-       if (!has_datetaken && __media_svc_get_exif_info(ed, buf, NULL, EXIF_TAG_DATE_TIME) == MS_MEDIA_ERR_NONE) {
-               if (strlen(buf) > 0) {
-                       has_datetaken = true;
-                       content_info->media_meta.datetaken = g_strdup(buf);
-
-                       /* This is same as recorded_date */
-                       content_info->media_meta.recorded_date = g_strdup(buf);
-               }
-       }
-
-       if (content_info->media_meta.recorded_date == NULL)
-               content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-
-       /* Get orientation value from exif. */
-       if (__media_svc_get_exif_info(ed, NULL, &orient_value, EXIF_TAG_ORIENTATION) == MS_MEDIA_ERR_NONE) {
-               if (orient_value >= NOT_AVAILABLE && orient_value <= ROT_270)
+       if (__media_svc_get_exif_short(ed, EXIF_TAG_ORIENTATION, &orient_value)) {
+               if (orient_value <= ROT_270)
                        content_info->media_meta.orientation = orient_value;
        }
 
-       /* Get width value from exif. */
-       if (__media_svc_get_exif_info(ed, NULL, &exif_width, EXIF_TAG_PIXEL_X_DIMENSION) == MS_MEDIA_ERR_NONE) {
-               if (exif_width > 0)
-                       content_info->media_meta.width = exif_width;
-       }
+       if (__media_svc_get_exif_short(ed, EXIF_TAG_PIXEL_X_DIMENSION, &exif_width))
+               content_info->media_meta.width = (unsigned int)exif_width;
 
-       /* Get height value from exif. */
-       if (__media_svc_get_exif_info(ed, NULL, &exif_height, EXIF_TAG_PIXEL_Y_DIMENSION) == MS_MEDIA_ERR_NONE) {
-               if (exif_height > 0)
-                       content_info->media_meta.height = exif_height;
-       }
+       if (__media_svc_get_exif_short(ed, EXIF_TAG_PIXEL_Y_DIMENSION, &exif_height))
+               content_info->media_meta.height = (unsigned int)exif_height;
 
-       if (ed)
-               exif_data_unref(ed);
+       exif_data_unref(ed);
 
 GET_WIDTH_HEIGHT:
-
        if (content_info->media_meta.width == 0 || content_info->media_meta.height == 0) {
                /*Get image width, height*/
                unsigned int img_width = 0;
                unsigned int img_height = 0;
-               mm_util_img_codec_type img_type = IMG_CODEC_UNKNOWN_TYPE;
 
-               mm_util_extract_image_info(path, &img_type, &img_width, &img_height);
+               if (get_image_info(content_info->path, &img_width, &img_height) != THUMB_OK)
+                       return MS_MEDIA_ERR_NONE;
+
                if (content_info->media_meta.width == 0)
                        content_info->media_meta.width = img_width;
 
@@ -597,16 +518,40 @@ static char * __media_svc_get_tag_str_value(MMHandleType tag, const char *tag_na
        return g_strdup(MEDIA_SVC_TAG_UNKNOWN);
 }
 
-int _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
+static char * __media_svc_extract_albumart(MMHandleType tag, const char *path, uid_t uid)
+{
+       int ret = FILEINFO_ERROR_NONE;
+       unsigned char *image = NULL;
+       unsigned int size = 0;
+       char *mimetype = NULL;
+       char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = { 0, };
+       unsigned int mime_size = 0;
+
+       ret = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
+       media_svc_retvm_if(ret != FILEINFO_ERROR_NONE, NULL, "Failed to get tag artwork[%d]", ret);
+       media_svc_retvm_if(!image || size == 0, NULL, "Invalid artwork");
+
+       ret = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &mimetype, &mime_size, NULL);
+       media_svc_retvm_if(ret != FILEINFO_ERROR_NONE, NULL, "Failed to get tag mime[%d]", ret);
+       media_svc_retvm_if(mime_size == 0, NULL, "Invalid mimetype");
+
+       ret = __media_svc_get_thumbnail_path(thumb_path, path, mimetype, uid);
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, NULL, "Failed to get thumbnail path");
+
+       ret = __media_svc_save_image(image, size, thumb_path, uid);
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, NULL, "Fail to save thumbnail");
+
+       return g_strdup(thumb_path);
+}
+
+void _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc_content_info_s *content_info, uid_t uid)
 {
        MMHandleType tag = 0;
        char *p = NULL;
-       unsigned char *image = NULL;
        unsigned int size = 0;
        int mmf_error = FILEINFO_ERROR_NONE;
        int album_id = 0;
        int ret = MS_MEDIA_ERR_NONE;
-       int convert_value = 0;
        bool support_albumart = ms_user_thumb_support(uid, content_info->path);
 
        /*Get Content Tag attribute ===========*/
@@ -615,149 +560,54 @@ int _media_svc_extract_audio_metadata(sqlite3 *handle, bool is_direct, media_svc
        else
                mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
 
-       if (mmf_error == FILEINFO_ERROR_NONE) {
-               content_info->media_meta.title = __media_svc_get_title(tag, content_info->path);
-               content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
-               content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
-               content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
-               content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
-               content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
-               content_info->media_meta.copyright = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_COPYRIGHT);
-
-               mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
-               if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
-                               /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
-                               char *p_value = g_strdelimit(g_strdup(p), "-", ':');
-                               content_info->media_meta.recorded_date = g_strdup_printf("%s +0000", p_value);
-                               g_free(p_value);
-                       } else {
-                               content_info->media_meta.recorded_date = g_strdup(p);
-                       }
-               }
-
-               if (content_info->media_meta.recorded_date == NULL)
-                       content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
+       if (mmf_error != FILEINFO_ERROR_NONE) {
+               content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
+               content_info->album_id = 0;
+               return;
+       }
 
-               mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
-               if (mmf_error == FILEINFO_ERROR_NONE && size == 4) {
-                       if (__media_svc_safe_atoi(p, &convert_value) == MS_MEDIA_ERR_NONE)
-                               content_info->media_meta.year = g_strdup(p);
-               }
+       content_info->media_meta.title = __media_svc_get_title(tag, content_info->file_name);
+       content_info->media_meta.album = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM);
+       content_info->media_meta.artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ARTIST);
+       content_info->media_meta.album_artist = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_ALBUM_ARTIST);
+       content_info->media_meta.genre = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_GENRE);
+       content_info->media_meta.track_num = __media_svc_get_tag_str_value(tag, MM_FILE_TAG_TRACK_NUM);
 
-               if (!content_info->media_meta.year)
-                               content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-
-               /*Do not extract artwork for the USB Storage content*/
-               if (support_albumart) {
-                       mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK, &image, &size, NULL);
-                       if (mmf_error != FILEINFO_ERROR_NONE)
-                               media_svc_error("fail to get tag artwork - err(%x)", mmf_error);
-
-                       mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_SIZE, &size, NULL);
-                       if (mmf_error != FILEINFO_ERROR_NONE)
-                               media_svc_error("fail to get artwork size - err(%x)", mmf_error);
-
-                       if (image != NULL && size > 0) {
-                               char thumb_path[MEDIA_SVC_PATHNAME_SIZE] = "\0";
-                               int artwork_mime_size = -1;
-
-                               mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_ARTWORK_MIME, &p, &artwork_mime_size, NULL);
-                               if ((mmf_error == FILEINFO_ERROR_NONE) && (artwork_mime_size > 0)) {
-                                       ret = __media_svc_get_thumbnail_path(thumb_path, content_info->path, p, uid);
-                                       if (ret != MS_MEDIA_ERR_NONE) {
-                                               media_svc_error("Fail to Get Thumbnail Path");
-                                       } else {
-                                               ret = __media_svc_save_image(image, size, thumb_path, uid);
-                                               if (ret != MS_MEDIA_ERR_NONE) {
-                                                       media_svc_error("Fail to Save Image");
-                                               } else {
-                                                       content_info->thumbnail_path = g_strdup(thumb_path);
-                                               }
-                                       }
-                               }
-                       }
-               }
+       mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_DATE, &p, &size, NULL);
+       if (mmf_error == FILEINFO_ERROR_NONE && size == 4)
+               content_info->media_meta.year = g_strdup(p);
+       else
+               content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
 
-               /*Initialize album_id to 0. below code will set the album_id*/
-               content_info->album_id = album_id;
-               ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
-               if (ret != MS_MEDIA_ERR_NONE) {
-                       if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
-                               media_svc_debug("album does not exist. So start to make album art");
-                               if ((g_strcmp0(content_info->media_meta.album, MEDIA_SVC_TAG_UNKNOWN)) &&
-                                       (g_strcmp0(content_info->media_meta.artist, MEDIA_SVC_TAG_UNKNOWN)))
-                                       ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
-                               else
-                                       ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
-
-                               content_info->album_id = album_id;
-                       }
-               } else {
-                       content_info->album_id = album_id;
-               }
+       /*Do not extract artwork for the USB Storage content*/
+       if (support_albumart)
+               content_info->thumbnail_path = __media_svc_extract_albumart(tag, content_info->path, uid);
 
-               mmf_error = mm_file_destroy_tag_attrs(tag);
-               if (mmf_error != FILEINFO_ERROR_NONE)
-                       media_svc_error("fail to free tag attr - err(%x)", mmf_error);
-       }       else {
-               content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
-               content_info->album_id = album_id;
+       ret = _media_svc_get_album_id(handle, content_info->media_meta.album, content_info->media_meta.artist, &album_id);
+       if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
+               media_svc_debug("album does not exist. So start to make album art");
+               if (strlen(content_info->media_meta.album) > 0 && strlen(content_info->media_meta.artist) > 0)
+                       ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, content_info->thumbnail_path, &album_id, uid);
+               else
+                       ret = _media_svc_append_album(handle, is_direct, content_info->media_meta.album, content_info->media_meta.artist, NULL, &album_id, uid);
        }
+       content_info->album_id = album_id;
 
-       return MS_MEDIA_ERR_NONE;
+       if (mm_file_destroy_tag_attrs(tag) != FILEINFO_ERROR_NONE)
+               media_svc_error("destroy failed");
 }
 
-int _media_svc_extract_video_metadata(media_svc_content_info_s *content_info)
+void _media_svc_extract_video_metadata(media_svc_content_info_s *content_info)
 {
-       int mmf_error = FILEINFO_ERROR_NONE;
-       MMHandleType tag = 0;
-       MMHandleType content = 0;
-       char *p = NULL;
-       unsigned int size = 0;
-
-       mmf_error = mm_file_create_tag_attrs_no_albumart(&tag, content_info->path);
-
-       if (mmf_error == FILEINFO_ERROR_NONE) {
-               mmf_error = mm_file_get_attrs(tag, MM_FILE_TAG_RECDATE, &p, &size, NULL);
-               if ((mmf_error == FILEINFO_ERROR_NONE) && (size > 0)) {
-                       if (g_str_has_suffix(content_info->mime_type, "mp4") || g_str_has_suffix(content_info->mime_type, "3gpp")) {
-                               /*Creation time format is 20130101 00:00:00 +0000. change it to 2013:01:01 00:00:00  +0000 like exif time format*/
-                               content_info->media_meta.recorded_date = g_strdelimit(g_strdup(p), "-", ':');
-                       } else {
-                               content_info->media_meta.recorded_date = g_strdup(p);
-                       }
-               }
-
-               if (content_info->media_meta.recorded_date == NULL)
-                       content_info->media_meta.recorded_date = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-               content_info->media_meta.datetaken = g_strdup(content_info->media_meta.recorded_date);
-
-               mmf_error = mm_file_destroy_tag_attrs(tag);
-               if (mmf_error != FILEINFO_ERROR_NONE)
-                       media_svc_error("fail to free tag attr - err(%x)", mmf_error);
-       }
-       /*Get Content attribute ===========*/
-       mmf_error = mm_file_create_content_attrs_simple(&content, content_info->path);
-       media_svc_retvm_if(mmf_error != FILEINFO_ERROR_NONE, MS_MEDIA_ERR_NONE, "mm_file_create_content_attrs failed");
-
-       mm_file_get_attrs(content, MM_FILE_CONTENT_VIDEO_WIDTH, &content_info->media_meta.width,
-               MM_FILE_CONTENT_VIDEO_HEIGHT, &content_info->media_meta.height,
-               NULL);
-
-       mm_file_destroy_content_attrs(content);
-
-       content_info->media_meta.title = __media_svc_get_title(NULL, content_info->path);
+       /* All metadata fields must be empty strings until media_video is deleted */
+       content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
        content_info->media_meta.album = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.album_artist = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.genre = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.track_num = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->media_meta.year = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
-       content_info->media_meta.copyright = g_strdup(MEDIA_SVC_TAG_UNKNOWN);
        content_info->album_id = 0;
-
-       return MS_MEDIA_ERR_NONE;
 }
 
 static gchar * __media_svc_get_zipfile_string(zip_t *z, const char *fname)
@@ -914,6 +764,7 @@ static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_
        }
 
        content_info->media_meta.title = __media_svc_find_and_get_value(root, "title");
+       /* In the case of PDF, if there is no title, it may not be a metadata block. Search for the next xml block*/
        if (is_pdf && !content_info->media_meta.title) {
                xmlFreeDoc(doc);
                return FALSE;
@@ -923,8 +774,6 @@ static gboolean __media_svc_get_xml_metadata(const xmlChar *buffer, gboolean is_
        if (!content_info->media_meta.artist)
                content_info->media_meta.artist = __media_svc_find_and_get_value(root, "author");
        content_info->media_meta.genre = __media_svc_find_and_get_value(root, "subject");
-       content_info->media_meta.copyright = __media_svc_find_and_get_value(root, "publisher");
-       content_info->media_meta.recorded_date = __media_svc_find_and_get_value(root, "date");
 
        xmlFreeDoc(doc);
 
@@ -1040,12 +889,22 @@ static int __media_svc_get_pdf_metadata(media_svc_content_info_s *content_info)
 
 int _media_svc_extract_book_metadata(media_svc_content_info_s *content_info)
 {
+       int ret = MS_MEDIA_ERR_NONE;
+
        media_svc_retvm_if(!content_info, MS_MEDIA_ERR_INVALID_PARAMETER, "content info is NULL");
 
        if (g_str_has_suffix(content_info->mime_type, "epub+zip"))
-               return __media_svc_get_epub_metadata(content_info);
+               ret = __media_svc_get_epub_metadata(content_info);
        else
-               return __media_svc_get_pdf_metadata(content_info);
+               ret = __media_svc_get_pdf_metadata(content_info);
+
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "failed to extract metadata");
+       if (!content_info->media_meta.title || strlen(content_info->media_meta.title) == 0) {
+               g_free(content_info->media_meta.title);
+               content_info->media_meta.title = __media_svc_get_title_from_filename(content_info->file_name);
+       }
+
+       return MS_MEDIA_ERR_NONE;
 }
 
 void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
@@ -1067,8 +926,6 @@ void _media_svc_destroy_content_info(media_svc_content_info_s *content_info)
        g_free(content_info->media_meta.album_artist);
        g_free(content_info->media_meta.genre);
        g_free(content_info->media_meta.year);
-       g_free(content_info->media_meta.recorded_date);
-       g_free(content_info->media_meta.copyright);
        g_free(content_info->media_meta.track_num);
        g_free(content_info->media_meta.datetaken);
 }
@@ -1090,11 +947,7 @@ int _media_svc_create_thumbnail(const char *path, char *thumb_path, media_svc_me
 
        //1. make thumb path
        ret = __media_svc_get_thumbnail_path(thumb_path, path, NULL, uid);
-       if (ret != MS_MEDIA_ERR_NONE) {
-               media_svc_error("Failed to create thumbnail path[%d]", ret);
-               SAFE_STRLCPY(thumb_path, "", MAX_FILEPATH_LEN);
-               return ret;
-       }
+       media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Failed to create thumbnail path[%d]", ret);
 
        //2. save thumbnail
        if (media_type == MEDIA_SVC_MEDIA_TYPE_IMAGE)