support rtsp playback
[platform/core/multimedia/libmm-player.git] / src / mm_player_utils.c
index eae43a3..4d273d3 100644 (file)
 #include <storage.h>
 #include <tzplatform_config.h>
 #include "mm_player_utils.h"
+#include "media_format.h"
 
-#define MEDIA_PATH_EXTERNAL tzplatform_getenv(TZ_SYS_STORAGE) /* external storage */
+#define MEDIA_PATH_EXTERNAL tzplatform_getenv(TZ_SYS_STORAGE) /* external storage, sd card, usb */
+#define FD_NUM_FOR_DEBUG 10
 
-int util_exist_file_path(const char *file_path)
+const gchar *
+_mmplayer_get_stream_type_name(int type)
+{
+       switch (type) {
+       case MM_PLAYER_STREAM_TYPE_AUDIO:
+               return "AUDIO";
+       case MM_PLAYER_STREAM_TYPE_VIDEO:
+               return "VIDEO";
+       case MM_PLAYER_STREAM_TYPE_TEXT:
+               return "TEXT";
+       case MM_PLAYER_STREAM_TYPE_DEFAULT: /* fall through */
+       case MM_PLAYER_STREAM_TYPE_MAX:     /* fall through */
+       default:
+               return "INVAID";
+       }
+}
+
+const gchar *
+_mmplayer_get_state_name(int state)
+{
+       switch (state) {
+       case MM_PLAYER_STATE_NULL:
+               return "NULL";
+       case MM_PLAYER_STATE_READY:
+               return "READY";
+       case MM_PLAYER_STATE_PAUSED:
+               return "PAUSED";
+       case MM_PLAYER_STATE_PLAYING:
+               return "PLAYING";
+       case MM_PLAYER_STATE_NONE:
+               return "NONE";
+       default:
+               return "INVAID";
+       }
+}
+
+gboolean
+_mmplayer_is_rtsp_streaming(mmplayer_t *player)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_RTSP) ? TRUE : FALSE;
+}
+
+gboolean
+_mmplayer_is_http_streaming(mmplayer_t *player)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       return (player->profile.uri_type == MM_PLAYER_URI_TYPE_URL_HTTP) ? TRUE : FALSE;
+}
+
+gboolean
+_mmplayer_is_streaming(mmplayer_t *player)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       return (_mmplayer_is_rtsp_streaming(player) || _mmplayer_is_http_streaming(player)
+               || _mmplayer_is_http_live_streaming(player) || _mmplayer_is_dash_streaming(player)
+               || _mmplayer_is_smooth_streaming(player)) ? TRUE : FALSE;
+}
+
+gboolean
+_mmplayer_is_live_streaming(mmplayer_t *player)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       return (_mmplayer_is_rtsp_streaming(player) && player->streaming_type == STREAMING_SERVICE_LIVE) ? TRUE : FALSE;
+}
+
+gboolean
+_mmplayer_is_http_live_streaming(mmplayer_t *player)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       return (player->profile.uri_type == MM_PLAYER_URI_TYPE_HLS) ? TRUE : FALSE;
+}
+
+gboolean
+_mmplayer_is_dash_streaming(mmplayer_t *player)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       return (player->profile.uri_type == MM_PLAYER_URI_TYPE_DASH) ? TRUE : FALSE;
+}
+
+gboolean
+_mmplayer_is_smooth_streaming(mmplayer_t *player)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       return (player->profile.uri_type == MM_PLAYER_URI_TYPE_SS) ? TRUE : FALSE;
+}
+
+gboolean
+_mmplayer_is_ms_buff_src(mmplayer_t *player)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       return (player->profile.uri_type == MM_PLAYER_URI_TYPE_MS_BUFF) ? TRUE : FALSE;
+}
+
+gboolean
+_mmplayer_has_suffix(mmplayer_t *player, const gchar *suffix)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+       MMPLAYER_RETURN_VAL_IF_FAIL(suffix, FALSE);
+
+       gboolean ret = FALSE;
+       gchar *t_url = g_ascii_strdown(player->profile.uri, -1);
+       gchar *t_suffix = g_ascii_strdown(suffix, -1);
+       gchar *opt = strchr(t_url, '?');
+
+       if (opt)
+               *opt = '\0';
+
+       ret = g_str_has_suffix(t_url, t_suffix);
+
+       MMPLAYER_FREEIF(t_url);
+       MMPLAYER_FREEIF(t_suffix);
+
+       return ret;
+}
+
+gboolean
+_mmplayer_is_videosink_ready(mmplayer_t *player, int surface_type)
+{
+       int curr_type = MM_DISPLAY_SURFACE_NUM;
+
+       if (!player ||
+               !player->pipeline ||
+               !player->pipeline->mainbin ||
+               !player->pipeline->videobin ||
+               !player->pipeline->videobin[MMPLAYER_V_BIN].gst ||
+               !player->pipeline->videobin[MMPLAYER_V_SINK].gst) {
+               LOGW("videosink is not ready yet");
+               return FALSE;
+       }
+
+       if (surface_type == MM_DISPLAY_SURFACE_NUM) /* don't care the surface type */
+               return TRUE;
+
+       mm_attrs_get_int_by_name(player->attrs, "display_surface_type", &curr_type);
+
+       if (curr_type != surface_type) {
+               LOGW("surface type is mismatched %d, %d", curr_type, surface_type);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+gboolean
+_mmplayer_post_message(mmplayer_t *player, enum MMMessageType msgtype, MMMessageParamType *param)
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       if (!player->msg_cb)
+               return FALSE;
+#ifdef __DEBUG__
+       LOGD("Message (type : %d)  will be posted using msg-cb(%p).", msgtype, player->msg_cb);
+#endif
+
+       player->msg_cb(msgtype, param, player->msg_cb_param);
+
+       return TRUE;
+}
+
+gboolean
+_mmplayer_dump_pipeline_state(mmplayer_t *player)
+{
+       GstIterator *iter = NULL;
+       gboolean done = FALSE;
+
+       GValue item = {0, };
+       GstElement *element = NULL;
+       GstElementFactory *factory = NULL;
+
+       GstState state = GST_STATE_VOID_PENDING;
+       GstState pending = GST_STATE_VOID_PENDING;
+       GstClockTime time = 200 * GST_MSECOND;
+
+       MMPLAYER_FENTER();
+
+       MMPLAYER_RETURN_VAL_IF_FAIL(player &&
+               player->pipeline &&
+               player->pipeline->mainbin,
+               FALSE);
+
+       MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-status-error-dump");
+
+       iter = gst_bin_iterate_recurse(GST_BIN(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst));
+
+       if (iter != NULL) {
+               while (!done) {
+                       switch (gst_iterator_next(iter, &item)) {
+                       case GST_ITERATOR_OK:
+                               element = g_value_get_object(&item);
+                               if (element) {
+                                       gst_element_get_state(element, &state, &pending, time);
+
+                                       factory = gst_element_get_factory(element) ;
+                                       if (factory)
+                                               LOGE("%s:%s : From:%s To:%s   refcount : %d", GST_OBJECT_NAME(factory) , GST_ELEMENT_NAME(element) ,
+                                                       gst_element_state_get_name(state), gst_element_state_get_name(pending) , GST_OBJECT_REFCOUNT_VALUE(element));
+                               }
+                               g_value_reset(&item);
+                               break;
+                       case GST_ITERATOR_RESYNC:
+                               gst_iterator_resync(iter);
+                               break;
+                       case GST_ITERATOR_ERROR:
+                               done = TRUE;
+                               break;
+                       case GST_ITERATOR_DONE:
+                               done = TRUE;
+                               break;
+                       }
+               }
+       }
+
+       element = GST_ELEMENT(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst);
+
+       gst_element_get_state(element, &state, &pending, time);
+
+       factory = gst_element_get_factory(element) ;
+
+       if (factory) {
+               LOGE("%s:%s : From:%s To:%s  refcount : %d",
+                       GST_OBJECT_NAME(factory),
+                       GST_ELEMENT_NAME(element),
+                       gst_element_state_get_name(state),
+                       gst_element_state_get_name(pending),
+                       GST_OBJECT_REFCOUNT_VALUE(element));
+       }
+
+       g_value_unset(&item);
+
+       if (iter)
+               gst_iterator_free(iter);
+
+       MMPLAYER_FLEAVE();
+
+       return FALSE;
+}
+
+int
+_mmplayer_exist_file_path(const char *file_path)
 {
        int fd = 0;
        struct stat stat_results = {0, };
@@ -61,18 +310,23 @@ int util_exist_file_path(const char *file_path)
                LOGE("failed to get file status");
        } else if (stat_results.st_size == 0) {
                LOGE("file size is zero");
+               if (fd < FD_NUM_FOR_DEBUG)
+                       LOGW("close fd %d", fd);
                close(fd);
                return MM_ERROR_PLAYER_FILE_NOT_FOUND;
-       } else
+       } else {
                LOGW("file size : %lld bytes", (long long)stat_results.st_size);
+       }
 
+       if (fd < FD_NUM_FOR_DEBUG)
+               LOGW("close fd %d", fd);
        close(fd);
 
        return MM_ERROR_NONE;
 }
 
-char**
-util_get_cookie_list(const char *cookies)
+char **
+_mmplayer_get_cookie_list(const char *cookies)
 {
        char **cookie_list = NULL;
        char *temp = NULL;
@@ -81,7 +335,7 @@ util_get_cookie_list(const char *cookies)
        if (!cookies || !strlen(cookies))
                return NULL;
 
-       SECURE_LOGD("cookies : %d[bytes] - %s \n", strlen(cookies), cookies);
+       SECURE_LOGD("cookies : %zu[bytes] - %s", strlen(cookies), cookies);
 
        temp = g_strdup(cookies);
 
@@ -91,45 +345,34 @@ util_get_cookie_list(const char *cookies)
        /* split */
        cookie_list = g_strsplit(temp, ";", 100);
 
+       if (!cookie_list) {
+               LOGE("failed to get cookie list");
+               goto EXIT;
+       }
+
        for (i = 0; i < g_strv_length(cookie_list); i++) {
                if (cookie_list[i]) {
                        if (strlen(cookie_list[i])) {
                                g_strstrip(cookie_list[i]);
-                               SECURE_LOGD("cookie_list[%d] : %d[bytes] - %s \n", i, strlen(cookie_list[i]), cookie_list[i]);
-                       } else
+                               SECURE_LOGD("cookie_list[%d] : %zu[bytes] - %s", i, strlen(cookie_list[i]), cookie_list[i]);
+                       } else {
                                cookie_list[i][0] = '\0';
+                       }
                }
        }
 
-       if (temp)
-               g_free(temp);
-       temp = NULL;
+EXIT:
+       MMPLAYER_FREEIF(temp);
 
        return cookie_list;
 }
 
-bool util_check_valid_url(const char *proxy)
-{
-       struct in_addr proxy_addr;
-       bool ret = TRUE;
-
-       MMPLAYER_RETURN_VAL_IF_FAIL(proxy, FALSE);
-       MMPLAYER_RETURN_VAL_IF_FAIL(strlen(proxy), FALSE);
-
-       if (inet_aton(proxy, &proxy_addr) != 0) {
-               LOGW("invalid proxy is set. \n");
-               ret = FALSE;
-       }
-
-       return ret;
-}
-
 /* check the given path is indicating sdp file */
 bool
-util_is_sdp_file(const char *path)
+_mmplayer_is_sdp_file(const char *path)
 {
        gboolean ret = FALSE;
-       gcharuri = NULL;
+       gchar *uri = NULL;
 
        MMPLAYER_FENTER();
 
@@ -145,8 +388,8 @@ util_is_sdp_file(const char *path)
 
        /* strlen(".sdp") == 4 */
        if (strlen(uri) <= 4) {
-               LOGW("path is too short.\n");
-               g_free(uri);
+               LOGW("path is too short.");
+               MMPLAYER_FREEIF(uri);
                return ret;
        }
 
@@ -158,41 +401,40 @@ util_is_sdp_file(const char *path)
                /* FIXIT : do it soon */
                LOGD("no suffix");
 
-       g_free(uri);
-       uri = NULL;
+       MMPLAYER_FREEIF(uri);
 
        return ret;
 }
 
-const char*
-util_get_charset(const char *file_path)
+const char *
+_mmplayer_get_charset(const char *file_path)
 {
-       UCharsetDetectorucsd;
-       const UCharsetMatchucm;
+       UCharsetDetector *ucsd;
+       const UCharsetMatch *ucm;
        UErrorCode status = U_ZERO_ERROR;
 
-       const charcharset = NULL;
+       const char *charset = NULL;
        char *buf = NULL;
-       FILEfin = 0;
+       FILE *fin = 0;
        size_t n_size = 0;
 
        fin = fopen(file_path, "r");
        if (!fin) {
-               SECURE_LOGE("fail to open file %s\n", file_path);
+               SECURE_LOGE("fail to open file %s", file_path);
                return NULL;
        }
 
        ucsd = ucsdet_open(&status);
        if (U_FAILURE(status)) {
-               LOGE("fail to ucsdet_open\n");
+               LOGE("fail to ucsdet_open");
                goto done;
        }
 
        ucsdet_enableInputFilter(ucsd, TRUE);
 
-       buf = g_malloc(1024*1024);
+       buf = g_try_malloc(1024 * 1024);
        if (!buf) {
-               LOGE("fail to alloc\n");
+               LOGE("fail to alloc");
                goto done;
        }
 
@@ -203,19 +445,19 @@ util_get_charset(const char *file_path)
 
        ucsdet_setText(ucsd, buf, strlen(buf), &status);
        if (U_FAILURE(status)) {
-               LOGE("fail to ucsdet_setText\n");
+               LOGE("fail to ucsdet_setText");
                goto done;
        }
 
        ucm = ucsdet_detect(ucsd, &status);
        if (U_FAILURE(status)) {
-               LOGE("fail to ucsdet_detect\n");
+               LOGE("fail to ucsdet_detect");
                goto done;
        }
 
        charset = ucsdet_getName(ucm, &status);
        if (U_FAILURE(status)) {
-               LOGE("fail to ucsdet_getName\n");
+               LOGE("fail to ucsdet_getName");
                goto done;
        }
 
@@ -230,22 +472,22 @@ done:
        if (ucsd)
                ucsdet_close(ucsd);
 
-       if (buf)
-               g_free(buf);
+       MMPLAYER_FREEIF(buf);
 
        return charset;
 }
 
-int util_get_pixtype(unsigned int fourcc)
+int
+_mmplayer_get_pixtype(unsigned int fourcc)
 {
        int pixtype = MM_PIXEL_FORMAT_INVALID;
 
-    /*
-       char *pfourcc = (char*)&fourcc;
+#ifdef __DEBUG__
+       char *pfourcc = (char *)&fourcc;
 
        LOGD("fourcc(%c%c%c%c)",
                pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]);
-    */
+#endif
 
        switch (fourcc) {
        case GST_MAKE_FOURCC('S', 'N', '1', '2'):
@@ -310,14 +552,15 @@ int util_get_pixtype(unsigned int fourcc)
        return pixtype;
 }
 
-static int _util_storage_supported_cb(int storage_id, storage_type_e type,
+static int
+__mmplayer_storage_supported_cb(int storage_id, storage_type_e type,
        storage_state_e state, const char *path, void *user_data)
 {
-       MMPlayerStorageInfo *storage_info = (MMPlayerStorageInfo *)user_data;
+       mmplayer_storage_info_t *storage_info = (mmplayer_storage_info_t *)user_data;
 
        MMPLAYER_RETURN_VAL_IF_FAIL(storage_info, FALSE);
 
-       if (type == storage_info->type) {
+       if (type == storage_info->type && strstr(storage_info->path, path)) {
                storage_info->id = storage_id;
                storage_info->state = state;
                return FALSE;
@@ -326,7 +569,8 @@ static int _util_storage_supported_cb(int storage_id, storage_type_e type,
        return TRUE;
 }
 
-bool util_get_storage_info(const char *path, MMPlayerStorageInfo *storage_info)
+bool
+_mmplayer_get_storage_info(const char *path, mmplayer_storage_info_t *storage_info)
 {
        int ret = 0;
        const char *file_path = path;
@@ -334,25 +578,83 @@ bool util_get_storage_info(const char *path, MMPlayerStorageInfo *storage_info)
        MMPLAYER_RETURN_VAL_IF_FAIL(file_path && storage_info, false);
 
        if (strncmp(file_path, "file://", strlen("file://")) == 0)
-               file_path = path+7; /* remove file prefix */
+               file_path = path + 7; /* remove file prefix */
 
-       if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0)
+       if (strncmp(file_path, MEDIA_PATH_EXTERNAL, strlen(MEDIA_PATH_EXTERNAL)) == 0) {
                storage_info->type = STORAGE_TYPE_EXTERNAL;
-       else
+
+               memset(storage_info->path, 0x00, MM_MAX_URL_LEN);
+               g_snprintf(storage_info->path, MM_MAX_URL_LEN, "%s", file_path);
+
+               ret = storage_foreach_device_supported((storage_device_supported_cb)__mmplayer_storage_supported_cb, storage_info);
+               if (ret != STORAGE_ERROR_NONE) {
+                       LOGE("failed to check supported storage 0x%x", ret);
+                       return false;
+               }
+
+               if (storage_info->state <= STORAGE_STATE_REMOVED) {
+                       LOGE("need to check the external storage state %d:%d", storage_info->id, storage_info->state);
+                       return false;
+               }
+       } else {
                storage_info->type = STORAGE_TYPE_INTERNAL;
+               storage_info->state = STORAGE_STATE_MOUNTED;
+       }
+
+       LOGD("storage info %d:%d:%d", storage_info->type, storage_info->id, storage_info->state);
+       return true;
+}
 
-       ret = storage_foreach_device_supported((storage_device_supported_cb)_util_storage_supported_cb, storage_info);
-       if (ret != STORAGE_ERROR_NONE) {
-               LOGE("storage_foreach_device_supported failed 0x%x", ret);
-               return false;
+media_format_mimetype_e _mmplayer_convert_audio_pcm_str_to_media_format_mime(const gchar *audio_pcm_str)
+{
+       if (!audio_pcm_str) {
+               LOGW("audio pcm str is NULL");
+               return MEDIA_FORMAT_MAX;
        }
 
-       if ((storage_info->type == STORAGE_TYPE_EXTERNAL) &&
-               (storage_info->state <= STORAGE_STATE_REMOVED)) {
-               LOGE("need to check the storage state : %d", storage_info->state);
-               return false;
+       if (strstr(audio_pcm_str, "S16LE"))
+               return MEDIA_FORMAT_PCM_S16LE;
+       else if (strstr(audio_pcm_str, "S24LE"))
+               return MEDIA_FORMAT_PCM_S24LE;
+       else if (strstr(audio_pcm_str, "S32LE"))
+               return MEDIA_FORMAT_PCM_S32LE;
+       else if (strstr(audio_pcm_str, "S16BE"))
+               return MEDIA_FORMAT_PCM_S16BE;
+       else if (strstr(audio_pcm_str, "S24BE"))
+               return MEDIA_FORMAT_PCM_S24BE;
+       else if (strstr(audio_pcm_str, "S32BE"))
+               return MEDIA_FORMAT_PCM_S32BE;
+       else if (strstr(audio_pcm_str, "F32LE"))
+               return MEDIA_FORMAT_PCM_F32LE;
+       else if (strstr(audio_pcm_str, "F32BE"))
+               return MEDIA_FORMAT_PCM_F32BE;
+       else if (strstr(audio_pcm_str, "U16LE"))
+               return MEDIA_FORMAT_PCM_U16LE;
+       else if (strstr(audio_pcm_str, "U24LE"))
+               return MEDIA_FORMAT_PCM_U24LE;
+       else if (strstr(audio_pcm_str, "U32LE"))
+               return MEDIA_FORMAT_PCM_U32LE;
+       else if (strstr(audio_pcm_str, "U16BE"))
+               return MEDIA_FORMAT_PCM_U16BE;
+       else if (strstr(audio_pcm_str, "U24BE"))
+               return MEDIA_FORMAT_PCM_U24BE;
+       else if (strstr(audio_pcm_str, "U32BE"))
+               return MEDIA_FORMAT_PCM_U32BE;
+       else {
+               LOGW("Not supported audio pcm format str : %s", audio_pcm_str);
+               return MEDIA_FORMAT_MAX;
        }
+}
 
-       LOGD("storage info: type %d, id %d", storage_info->type, storage_info->id);
-       return true;
+gboolean _mmplayer_use_uridecodebin3(mmplayer_t *player) /* MMPLAYER_USE_URIDECODEBIN3(player) */
+{
+       MMPLAYER_RETURN_VAL_IF_FAIL(player, FALSE);
+
+       if (MMPLAYER_IS_MS_BUFF_SRC(player))
+               return FALSE;
+
+       if (!player->ini.use_uridecodebin3)
+               return FALSE;
+
+       return TRUE;
 }