原来的方案:
gboolean callback_bus_call (GstBus * bus, GstMessage * msg, gpointer data) { ...... gst_element_set_state(pipeline, GST_STATE_NULL); g_main_loop_quit(loop); return true; } int main_thread() { ...... GstBus* bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline)); guint bus_watch_id = gst_bus_add_watch(bus, callback_bus_call, pTask); gst_object_unref(bus); ...... g_main_loop_run (loop); }
试图在callbak_bus_call()中得到出错和结束消息,然后再退出.结果发现要么无法退出,要么直接崩溃.
以下是正确办法:
int main_thread() { ...... //阻塞,直到收到指定的消息 GstBus* bus = gst_element_get_bus(pipeline); GstMessage* msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS)); if (msg != NULL) { gst_message_unref(msg); } gst_object_unref (bus); gst_element_set_state(pipeline, GST_STATE_NULL); gst_object_unref(pipeline); ...... }