GStreamer GstBusSyncHandler停止,暂停和播放状态

时间:2015-04-18 08:29:14

标签: wxwidgets gstreamer

我使用Gstreamer作为播放视频和音频的基础,到目前为止它的工作正常。现在我想知道媒体何时停止,暂停,加载或开始播放并通知GUI。现在消息不断重复(参见附图)。现在,在那种情况下,它将充斥我的GUI。我只想接收一次事件(例如媒体停止或暂停或加载时)。 我得到的唯一消息(一次)是End of Stream。

当它们发生时,有没有办法只获取一次所述消息? 这是我目前的代码

extern "C"
{
    static GstBusSyncReply
    bus_sync(GstBus* bus, GstMessage* message, gpointer user_data)
    {
        CustomData* data = reinterpret_cast<CustomData*>(user_data);

        GstState old_state, new_state, pending;             
        gst_message_parse_state_changed(message, &old_state, &new_state, &pending);
        g_print ("State set from %s to %s --- %s\n", gst_element_state_get_name (old_state), gst_element_state_get_name (new_state), gst_element_state_get_name (pending));

        switch(GST_MESSAGE_TYPE(message))
        {
            case GST_MESSAGE_STATE_CHANGED:
            {
                wxGStreamer* win = dynamic_cast<wxGStreamer*>(data->parent);
                if(win)
                {
                    win->SendMessage(wxMEDIASTATE_CHANGED); 
                }               

                if(data->state!=new_state)
                {
                    //state changed, save it
                    data->state = new_state; 

                    if(old_state==GST_STATE_NULL && new_state == GST_STATE_READY && pending== GST_STATE_PLAYING)
                    {
                        g_print ("***Loaded***\n");
                    } 
                    else if(old_state==GST_STATE_PLAYING && new_state == GST_STATE_PAUSED && pending== GST_STATE_VOID_PENDING)
                    {
                        g_print ("***Paused***\n");
                    }
                    else if(old_state==GST_STATE_READY && new_state == GST_STATE_NULL && pending== GST_STATE_VOID_PENDING)
                    {
                        g_print ("***Stopped***\n");
                    } 
                    else if(new_state == GST_STATE_PLAYING &&(old_state==GST_STATE_READY||old_state==GST_STATE_PAUSED) )
                    {
                        g_print ("***Playing***\n");
                    } 
                }
                break;
            }
        }  
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

简单回答:除了播放/停止状态之外,您从GstBus获得更多消息。 在代码中实现一个开关,只调用一次if / else语句:

switch (GST_MESSAGE_TYPE (msg)) {
  case GST_MESSAGE_ERROR:
    break;
  case GST_MESSAGE_WARNING:
    break;
  case GST_MESSAGE_INFO:
    break;
  case GST_MESSAGE_EOS:
    break;
  case GST_MESSAGE_BUFFERING:
    break;
  case GST_MESSAGE_STATE_CHANGED: {
    GstState old_state, new_state, pending_state;
    gst_message_parse_state_changed (msg, &old_state, 
        &new_state, &pending_state);

    if (GST_MESSAGE_SRC (msg) == GST_OBJECT (__YOUR_PIPELINE_POINTER__)) {
      // your code here
    }
  }
  break;
}
// DON'T FORGET TO RETURN
return GST_BUS_DROP;

希望,这有帮助; - )