Improve __media_svc_get_media_type() 36/261436/4 submit/tizen/20210722.043131
authorMinje Ahn <minje.ahn@samsung.com>
Mon, 19 Jul 2021 03:16:25 +0000 (12:16 +0900)
committerMinje Ahn <minje.ahn@samsung.com>
Wed, 21 Jul 2021 07:57:54 +0000 (16:57 +0900)
Change-Id: I2005aa361acab47ce7a9e69bef8a2e362de24319
Signed-off-by: Minje Ahn <minje.ahn@samsung.com>
src/common/media-svc-util.c

index e458a03..b4c4522 100644 (file)
 
 #define MEDIA_SVC_FILE_EXT_LEN_MAX                             6                       /**< Maximum file ext lenth*/
 
-#define CONTENT_TYPE_NUM 5
 #define MUSIC_MIME_NUM 29
-#define SOUND_MIME_NUM 1
+#define SOUND_MIME_NUM 2
 #define MIME_TYPE_LENGTH 255
 #define THUMB_HASH_LEN 256
 #define MIME_LENGTH 50
-#define _3GP_FILE ".3gp"
-#define _MP4_FILE ".mp4"
-#define _ASF_FILE ".asf"
 #define MEDIA_SVC_ARTWORK_SIZE 2000
 #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 MEDIA_SVC_DEFAULT_GPS_VALUE                    -200                    /**< Default GPS Value*/
 
 #define MEDIA_SVC_PDF_TAG_TAIL_LEN 12
 #define MEDIA_SVC_PDF_BUF_SIZE 256
 
-typedef struct {
-       char content_type[15];
-       media_svc_media_type_e media_type;
-} _media_svc_content_table_s;
-
 enum Exif_Orientation {
        NOT_AVAILABLE = 0,
        NORMAL = 1,
@@ -95,14 +90,6 @@ enum Exif_Orientation {
        ROT_270 = 8
 };
 
-static const _media_svc_content_table_s content_category[CONTENT_TYPE_NUM] = {
-       {"audio", MEDIA_SVC_MEDIA_TYPE_SOUND},
-       {"image", MEDIA_SVC_MEDIA_TYPE_IMAGE},
-       {"video", MEDIA_SVC_MEDIA_TYPE_VIDEO},
-       {"application", MEDIA_SVC_MEDIA_TYPE_OTHER},
-       {"text", MEDIA_SVC_MEDIA_TYPE_OTHER},
-};
-
 static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
        /*known mime types of normal files*/
        "mpeg",
@@ -140,7 +127,8 @@ static const char music_mime_table[MUSIC_MIME_NUM][MIME_LENGTH] = {
 };
 
 static const char sound_mime_table[SOUND_MIME_NUM][MIME_LENGTH] = {
-       "x-smaf",
+       "application/x-smaf",
+       "text/x-iMelody"
 };
 
 char *_media_info_generate_uuid(void)
@@ -318,94 +306,78 @@ static time_t __media_svc_get_timeline_from_str(const char *timstr)
 static int __media_svc_get_media_type(const char *path, const char *mime_type, media_svc_media_type_e *media_type)
 {
        int idx = 0;
+       int audio = 0;
+       int video = 0;
 
        media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is null");
        media_svc_retvm_if(!mime_type, MS_MEDIA_ERR_INVALID_PARAMETER, "mime_type is null");
        media_svc_retvm_if(!media_type, MS_MEDIA_ERR_INVALID_PARAMETER, "media_type is null");
 
-       *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
-
-       /*categorize from mime_type */
-       for (idx = 0; idx < CONTENT_TYPE_NUM; idx++) {
-               if (strncmp(mime_type, content_category[idx].content_type, strlen(content_category[idx].content_type)) == 0) {
-                       *media_type = content_category[idx].media_type;
-                       break;
-               }
+       /* Image */
+       if (strncmp(mime_type, IMAGE_PREFIX, IMAGE_PREFIX_LEN) == 0) {
+               *media_type = MEDIA_SVC_MEDIA_TYPE_IMAGE;
+               return MS_MEDIA_ERR_NONE;
        }
 
-       /*in application type, exitst sound file ex) x-smafs, asf */
-       if (*media_type == MEDIA_SVC_MEDIA_TYPE_OTHER) {
-               int prefix_len = strlen(content_category[3].content_type) + 1;
-               char *ext = NULL;
+       /* Audio */
+       if (strncmp(mime_type, AUDIO_PREFIX, AUDIO_PREFIX_LEN) == 0) {
+               *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
 
-               for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
-                       if (strstr(mime_type + prefix_len, sound_mime_table[idx]) != NULL) {
-                               *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) {
+                               *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
                                break;
                        }
                }
 
-               if (strncasecmp(mime_type, "text/x-iMelody", strlen("text/x-iMelody")) == 0)
-                       *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
+               /* audio/x-mpegurl : .m3u file (playlist file) */
+               if (strcmp(mime_type + AUDIO_PREFIX_LEN, "x-mpegurl") == 0)
+                       *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
 
-               /*"asf" must check video stream and then categorize in directly. */
-               ext = strrchr(path, '.');
-               if (ext != NULL) {
-                       if (strncasecmp(ext, _ASF_FILE, 5) == 0) {
-                               int audio = 0;
-                               int video = 0;
-                               int err = 0;
-
-                               err = mm_file_get_stream_info(path, &audio, &video);
-                               if (err == 0) {
-                                       if (audio > 0 && video == 0)
-                                               *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
-                                       else
-                                               *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
-                               }
+               return MS_MEDIA_ERR_NONE;
+       }
+
+       /* Video */
+       if (strncmp(mime_type, VIDEO_PREFIX, VIDEO_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 (mm_file_get_stream_info(path, &audio, &video) == FILEINFO_ERROR_NONE) {
+                               if (audio > 0 && video == 0)
+                                       *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
                        }
                }
 
-               if (g_str_has_suffix(mime_type, "epub+zip") || g_str_has_suffix(mime_type, "pdf"))
-                       *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
+               return MS_MEDIA_ERR_NONE;
        }
 
-       /*check music file in sound files. */
-       if (*media_type == MEDIA_SVC_MEDIA_TYPE_SOUND) {
-               int prefix_len = strlen(content_category[0].content_type) + 1;
+       /* ETC */
+       *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
 
-               for (idx = 0; idx < MUSIC_MIME_NUM; idx++) {
-                       if (strcmp(mime_type + prefix_len, music_mime_table[idx]) == 0) {
-                               *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
-                               break;
-                       }
+       for (idx = 0; idx < SOUND_MIME_NUM; idx++) {
+               if (strcmp(mime_type, sound_mime_table[idx]) == 0) {
+                       *media_type = MEDIA_SVC_MEDIA_TYPE_SOUND;
+                       return MS_MEDIA_ERR_NONE;
                }
+       }
 
-               /*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*/
-               if (strncasecmp(mime_type, "audio/x-mpegurl", strlen("audio/x-mpegurl")) == 0)
-                       *media_type = MEDIA_SVC_MEDIA_TYPE_OTHER;
-
-       } else if (*media_type == MEDIA_SVC_VIDEO_VIDEO) {
-               /*some video files don't have video stream. in this case it is categorize as music. */
-               char *ext = NULL;
-               /*"3gp" and "mp4" must check video stream and then categorize in directly. */
-               ext = strrchr(path, '.');
-               if (ext != NULL) {
-                       if ((strncasecmp(ext, _3GP_FILE, 4) == 0) || (strncasecmp(ext, _MP4_FILE, 5) == 0)) {
-                               int audio = 0;
-                               int video = 0;
-                               int err = 0;
-
-                               err = mm_file_get_stream_info(path, &audio, &video);
-                               if (err == 0) {
-                                       if (audio > 0 && video == 0) {
-                                               *media_type = MEDIA_SVC_MEDIA_TYPE_MUSIC;
-                                       }
-                               }
-                       }
+       /*"asf" must check video stream and then categorize in directly. */
+       if (strcmp(mime_type, "application/vnd.ms-asf") == 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;
+                       else
+                               *media_type = MEDIA_SVC_MEDIA_TYPE_VIDEO;
                }
+
+               return MS_MEDIA_ERR_NONE;
        }
 
+       if (strcmp(mime_type, "application/epub+zip") == 0 || strcmp(mime_type, "application/pdf") == 0)
+               *media_type = MEDIA_SVC_MEDIA_TYPE_BOOK;
+
        return MS_MEDIA_ERR_NONE;
 }