[0.6.242] Check MMPLAYER_BUS_WATCHER_WAIT_UNTIL() return value, when remove bus watcher
[platform/core/multimedia/libmm-player.git] / src / mm_player_priv.c
index 7107352..ec122c6 100644 (file)
@@ -196,6 +196,7 @@ static gboolean __mmplayer_swcodec_set_bo(mmplayer_t *player, mmplayer_video_dec
 
 static void __mmplayer_set_pause_state(mmplayer_t *player);
 static void __mmplayer_set_playing_state(mmplayer_t *player);
+static int __mmplayer_switch_stream(mmplayer_t *player, mmplayer_track_type_e type, int index);
 /*===========================================================================================
 |                                                                                                                                                                                      |
 |  FUNCTION DEFINITIONS                                                                                                                                                |
@@ -801,10 +802,14 @@ _mmplayer_bus_watcher_remove(MMHandleType hplayer)
                __mmplayer_remove_g_source_from_context(player->context.thread_default, player->bus_watcher);
                MMPLAYER_BUS_WATCHER_LOCK(player);
                end_time = g_get_monotonic_time () + 2 * G_TIME_SPAN_SECOND;
-               while (player->bus_watcher > 0)
-                       MMPLAYER_BUS_WATCHER_WAIT_UNTIL(player, end_time);
+               while (player->bus_watcher > 0) {
+                       if (!MMPLAYER_BUS_WATCHER_WAIT_UNTIL(player, end_time)) {
+                               LOGW("MMPLAYER_BUS_WATCHER_WAIT_UNTIL() timeout has passed - bus_watcher (%d)",
+                                               player->bus_watcher);
+                               break;
+                       }
+               }
                MMPLAYER_BUS_WATCHER_UNLOCK(player);
-
                g_mutex_clear(&player->bus_watcher_mutex);
                g_cond_clear(&player->bus_watcher_cond);
        }
@@ -1465,10 +1470,10 @@ __mmplayer_set_decode_track_info(mmplayer_t *player, mmplayer_track_type_e type)
 
        /* change track to active pad */
        active_index = player->track[type].active_track_index;
-       if ((active_index != DEFAULT_TRACK) &&
+       if ((active_index != DEFAULT_TRACK_INDEX) &&
                (__mmplayer_change_selector_pad(player, type, active_index) != MM_ERROR_NONE)) {
                LOGW("failed to change %d type track to %d", type, active_index);
-               player->track[type].active_track_index = DEFAULT_TRACK;
+               player->track[type].active_track_index = DEFAULT_TRACK_INDEX;
                return;
        }
 
@@ -1720,7 +1725,8 @@ __mmplayer_gst_create_sink_bin(GstElement *elem, GstPad *pad, GstCaps *ref_caps,
                MMPLAYER_GST_GET_CAPS_INFO(ref_caps, str, name, caps_ret);
                if (!caps_ret)
                        goto ERROR;
-
+               if (caps)
+                       gst_caps_unref(caps);
                caps = gst_caps_ref(ref_caps);
        }
 
@@ -2126,14 +2132,11 @@ _mmplayer_update_video_overlay_param(mmplayer_t *player, const char *param_name)
        return MM_ERROR_NONE;
 }
 
-int
-_mmplayer_set_audio_only(MMHandleType hplayer, bool audio_only)
+static int __mmplayer_set_disable_overlay_option(mmplayer_t *player, bool disable)
 {
        gboolean disable_overlay = FALSE;
-       mmplayer_t *player = (mmplayer_t *)hplayer;
 
        MMPLAYER_FENTER();
-       MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
        MMPLAYER_RETURN_VAL_IF_FAIL(player->pipeline->videobin &&
                                                                player->pipeline->videobin[MMPLAYER_V_SINK].gst,
                                                                MM_ERROR_PLAYER_NO_OP); /* invalid op */
@@ -2145,24 +2148,24 @@ _mmplayer_set_audio_only(MMHandleType hplayer, bool audio_only)
 
        g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "disable-overlay", &disable_overlay, NULL);
 
-       if (audio_only == (bool)disable_overlay) {
-               LOGE("It's the same with current setting: (%d)", audio_only);
+       if (disable == (bool)disable_overlay) {
+               LOGE("It's the same with current setting: (%d)", disable);
                return MM_ERROR_NONE;
        }
 
-       if (audio_only) {
+       if (disable) {
                LOGE("disable overlay");
                g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "disable-overlay", TRUE, NULL);
 
                /* release overlay resource */
                if (__mmplayer_release_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_OVERLAY) != MM_ERROR_NONE) {
                        LOGE("failed to release overlay resource");
-                       goto ERROR;
+                       return MM_ERROR_PLAYER_INTERNAL;
                }
        } else {
                if (_mmplayer_acquire_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_OVERLAY) != MM_ERROR_NONE) {
                        LOGE("failed to acquire video overlay resource");
-                       goto ERROR;
+                       return MM_ERROR_PLAYER_INTERNAL;
                }
                player->interrupted_by_resource = FALSE;
 
@@ -2171,39 +2174,46 @@ _mmplayer_set_audio_only(MMHandleType hplayer, bool audio_only)
                g_object_set(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "disable-overlay", FALSE, NULL);
        }
 
-ERROR:
        MMPLAYER_FLEAVE();
        return MM_ERROR_NONE;
 }
 
 int
-_mmplayer_get_audio_only(MMHandleType hplayer, bool *paudio_only)
+_mmplayer_set_audio_only(MMHandleType hplayer, bool audio_only)
 {
+       int ret = MM_ERROR_NONE;
        mmplayer_t *player = (mmplayer_t *)hplayer;
-       gboolean disable_overlay = FALSE;
 
        MMPLAYER_FENTER();
-
        MMPLAYER_RETURN_VAL_IF_FAIL(player && player->pipeline, MM_ERROR_PLAYER_NOT_INITIALIZED);
-       MMPLAYER_RETURN_VAL_IF_FAIL(paudio_only, MM_ERROR_INVALID_ARGUMENT);
-       MMPLAYER_RETURN_VAL_IF_FAIL(player->pipeline->videobin &&
-                                                               player->pipeline->videobin[MMPLAYER_V_SINK].gst,
-                                                               MM_ERROR_PLAYER_NO_OP); /* invalid op */
 
-       if (!g_object_class_find_property(G_OBJECT_GET_CLASS(player->pipeline->videobin[MMPLAYER_V_SINK].gst), "disable-overlay")) {
-               LOGW("Display control is not supported");
-               return MM_ERROR_PLAYER_INTERNAL;
+       if (MMPLAYER_USE_DECODEBIN(player)) {
+               ret = __mmplayer_set_disable_overlay_option(player, audio_only);
+               goto EXIT;
        }
 
-       g_object_get(player->pipeline->videobin[MMPLAYER_V_SINK].gst, "disable-overlay", &disable_overlay, NULL);
+       if (audio_only) {
+               MMPLAYER_RETURN_VAL_IF_FAIL(player->pipeline->videobin &&
+                                                                       player->pipeline->videobin[MMPLAYER_V_SINK].gst,
+                                                                       MM_ERROR_PLAYER_NO_OP); /* invalid op */
 
-       *paudio_only = (bool)disable_overlay;
+               __mmplayer_switch_stream(player, MM_PLAYER_TRACK_TYPE_VIDEO, INVALID_TRACK_INDEX);
+
+               /* release decoder resource */
+               if (__mmplayer_release_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_DECODER) != MM_ERROR_NONE) {
+                       LOGE("failed to release video decoder resources");
+                       return MM_ERROR_PLAYER_INTERNAL;
+               }
+               player->can_support_codec &= ~FOUND_PLUGIN_VIDEO;
+       } else {
+               __mmplayer_switch_stream(player, MM_PLAYER_TRACK_TYPE_VIDEO, DEFAULT_TRACK_INDEX);
+       }
 
-       LOGD("audio_only : %d", *paudio_only);
+EXIT:
+       mm_player_set_attribute(hplayer, NULL, MM_PLAYER_AUDIO_ONLY, (int)audio_only, (char *)NULL);
 
        MMPLAYER_FLEAVE();
-
-       return MM_ERROR_NONE;
+       return ret;
 }
 
 int
@@ -2309,10 +2319,6 @@ __mmplayer_gst_caps_notify_cb(GstPad *pad, GParamSpec *unused, gpointer data)
        MMPLAYER_RETURN_IF_FAIL(unused);
        MMPLAYER_RETURN_IF_FAIL(data);
 
-       caps = gst_pad_get_current_caps(pad);
-       if (!caps)
-               return;
-
        MMPLAYER_GST_GET_CAPS_INFO_FROM_PAD(pad, caps, str, name, caps_ret);
        if (!caps_ret)
                goto ERROR;
@@ -3775,6 +3781,7 @@ __mmplayer_gst_create_plain_text_elements(mmplayer_t *player)
        LOGD("adding created elements to bin");
        if (!_mmplayer_gst_element_add_bucket_to_bin(GST_BIN(textbin[MMPLAYER_T_BIN].gst), element_bucket)) {
                LOGE("failed to add elements");
+               g_list_free(element_bucket);
                goto ERROR;
        }
 
@@ -3786,6 +3793,7 @@ __mmplayer_gst_create_plain_text_elements(mmplayer_t *player)
        LOGD("Linking elements in the bucket by added order.");
        if (_mmplayer_gst_element_link_bucket(element_bucket) == -1) {
                LOGE("failed to link elements");
+               g_list_free(element_bucket);
                goto ERROR;
        }
 
@@ -3820,7 +3828,6 @@ __mmplayer_gst_create_plain_text_elements(mmplayer_t *player)
        return MM_ERROR_NONE;
 
 ERROR:
-       g_list_free(element_bucket);
 
        if (!player->play_subtitle && textbin[MMPLAYER_T_FAKE_SINK].gst) {
                LOGE("remove textbin sink from sink list");
@@ -6787,9 +6794,9 @@ _mmplayer_set_next_uri(MMHandleType hplayer, const char *uri, bool is_first_path
                        player->uri_info.uri_list = g_list_append(player->uri_info.uri_list, g_strdup(uri));
                        SECURE_LOGD("add original path : %s", uri);
                } else {
-                       player->uri_info.uri_list = g_list_delete_link(player->uri_info.uri_list, g_list_nth(player->uri_info.uri_list, 0));
-                       player->uri_info.uri_list = g_list_insert(player->uri_info.uri_list, g_strdup(uri), 0);
-
+                       g_free(g_list_nth_data(player->uri_info.uri_list, 0));
+                       player->uri_info.uri_list = g_list_prepend(
+                               g_list_delete_link(player->uri_info.uri_list, player->uri_info.uri_list), g_strdup(uri));
                        SECURE_LOGD("change original path : %s", uri);
                }
        } else {
@@ -7466,27 +7473,61 @@ DONE:
 }
 
 void
-_mmplayer_gst_decode_pad_removed(GstElement *elem, GstPad *new_pad,
+_mmplayer_gst_decode_pad_removed(GstElement *elem, GstPad *pad,
        gpointer data)
 {
-       //mmplayer_t *player = (mmplayer_t *)data;
-       GstCaps *caps = NULL;
+       int ret = MM_ERROR_NONE;
+       mmplayer_t *player = (mmplayer_t *)data;
+       mmplayer_gst_element_t *mainbin = player->pipeline->mainbin;
+       mmplayer_gst_element_t *videobin = player->pipeline->videobin;
+       gint timeout = MMPLAYER_STATE_CHANGE_TIMEOUT(player);
 
-       LOGD("[Decodebin2] pad-removed signal");
+       MMPLAYER_FENTER();
+       MMPLAYER_RETURN_IF_FAIL(player && player->pipeline && mainbin);
 
-       caps = gst_pad_query_caps(new_pad, NULL);
-       if (!caps) {
-               LOGW("query caps is NULL");
+       LOGD("decoded pad %s:%s removed", GST_DEBUG_PAD_NAME(pad));
+
+       if (MMPLAYER_USE_DECODEBIN(player))
+               return;
+
+       if (!videobin || !g_str_has_prefix (GST_PAD_NAME (pad), "video"))
+               return;
+
+       ret = _mmplayer_gst_set_state(player, mainbin[MMPLAYER_M_V_CONCAT].gst, GST_STATE_NULL, FALSE, timeout);
+       if (ret != MM_ERROR_NONE) {
+               LOGE("fail to change state to NULL");
                return;
        }
 
-       gchar *caps_str = NULL;
-       caps_str = gst_caps_to_string(caps);
+       ret = _mmplayer_gst_set_state(player, videobin[MMPLAYER_V_BIN].gst, GST_STATE_NULL, FALSE, timeout);
+       if (ret != MM_ERROR_NONE) {
+               LOGE("fail to change state to NULL");
+               return;
+       }
 
-       LOGD("pad removed caps : %s from %s", caps_str, GST_ELEMENT_NAME(elem));
+       if (!gst_bin_remove(GST_BIN_CAST(mainbin[MMPLAYER_M_PIPE].gst), mainbin[MMPLAYER_M_V_CONCAT].gst)) {
+               LOGE("failed to remove video concat");
+       }
 
-       MMPLAYER_FREEIF(caps_str);
-       gst_caps_unref(caps);
+       if (!gst_bin_remove(GST_BIN_CAST(mainbin[MMPLAYER_M_PIPE].gst), videobin[MMPLAYER_V_BIN].gst)) {
+               LOGE("failed to remove videobin");
+       }
+
+       gst_object_unref(GST_OBJECT(mainbin[MMPLAYER_M_V_CONCAT].gst));
+       mainbin[MMPLAYER_M_V_CONCAT].gst = NULL;
+       mainbin[MMPLAYER_M_V_CONCAT].id = 0;
+
+       gst_object_unref(GST_OBJECT(videobin[MMPLAYER_V_BIN].gst));
+       MMPLAYER_FREEIF(player->pipeline->videobin);
+
+       ret = __mmplayer_release_hw_resource(player, MMPLAYER_RESOURCE_TYPE_VIDEO_OVERLAY);
+       if (ret != MM_ERROR_NONE)
+               LOGE("failed to release overlay resources");
+
+       player->videodec_linked = 0;
+
+       MMPLAYER_GENERATE_DOT_IF_ENABLED(player, "pipeline-pad-removed");
+       MMPLAYER_FLEAVE();
 }
 
 void
@@ -7769,7 +7810,7 @@ __mmplayer_release_misc(mmplayer_t *player)
 static void
 __mmplayer_release_misc_post(mmplayer_t *player)
 {
-       char *original_uri = NULL;
+       gchar *original_uri = NULL;
        MMPLAYER_FENTER();
 
        /* player->pipeline is already released before. */
@@ -7786,7 +7827,8 @@ __mmplayer_release_misc_post(mmplayer_t *player)
        player->audio_stream_changed_cb = NULL;
        player->audio_stream_changed_cb_user_param = NULL;
 
-       mm_player_set_attribute((MMHandleType)player, NULL, "content_video_found", 0, NULL);
+       mm_player_set_attribute((MMHandleType)player, NULL,
+               "content_video_found", 0, MM_PLAYER_AUDIO_ONLY, 0, NULL);
 
        /* clean found audio decoders */
        if (player->audio_decoders) {
@@ -7806,7 +7848,7 @@ __mmplayer_release_misc_post(mmplayer_t *player)
 
                mm_player_set_attribute((MMHandleType)player, NULL, "profile_uri",
                                original_uri, (original_uri) ? strlen(original_uri) : (0), NULL);
-
+               MMPLAYER_FREEIF(original_uri);
        }
 
        /* clear the audio stream buffer list */
@@ -8346,16 +8388,19 @@ __mmplayer_switch_stream(mmplayer_t *player, mmplayer_track_type_e type, int ind
        guint active_idx = 0;
        GstStream *stream = NULL;
        GList *streams = NULL;
-       GstEvent *ev = NULL;
        GstCaps *caps = NULL;
 
+       MMPLAYER_FENTER();
        LOGD("Switching Streams... type: %d, index: %d", type, index);
 
        player->track[type].active_track_index = index;
 
        for (int i = 0; i < MM_PLAYER_TRACK_TYPE_MAX; i++) {
                /* FIXME: need to consider the non display type or audio only in case of MM_PLAYER_TRACK_TYPE_VIDEO */
-               if (player->track[i].total_track_num > 0) {
+               LOGD("track type:%d, total: %d, active: %d", i,
+                               player->track[i].total_track_num, player->track[i].active_track_index);
+               if (player->track[i].total_track_num > 0 &&
+                       player->track[i].active_track_index > INVALID_TRACK_INDEX) {
                        active_idx = player->track[i].active_track_index;
                        stream = g_ptr_array_index(player->track[i].streams, active_idx);
                        streams = g_list_append (streams, (gchar *)gst_stream_get_stream_id(stream));
@@ -8371,10 +8416,14 @@ __mmplayer_switch_stream(mmplayer_t *player, mmplayer_track_type_e type, int ind
                }
        }
 
-       ev = gst_event_new_select_streams(streams);
-       gst_element_send_event(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst, ev);
-       g_list_free(streams);
+       if (streams) {
+               LOGD("send select stream event");
+               gst_element_send_event(player->pipeline->mainbin[MMPLAYER_M_PIPE].gst,
+                               gst_event_new_select_streams(streams));
+               g_list_free(streams);
+       }
 
+       MMPLAYER_FLEAVE();
        return MM_ERROR_NONE;
 }