How to confige the threads internel of a pipeline.
GStreamer的设计原生支持多线程,并完全保证线程安全。大多数情况下,多线程实现细节对基于GStreamer开发的应用程序隐藏,因为这会让应用程序开发更便利。而在某些场景下,应用程序可能会介入Gstreamer的多线程机制。此时,Gstreamer允许应用程序指定在流水线内的某些部分使用多线程。具体请参考《When would you want to force a thread?》一节。
Gstreamer还支持开发人员在线程被创建时获取通知。从而,开发人员可以配置线程的优先级,设置线程池的相关行为等。具体请参考《Configuring Threads in GStreamer》一节。
static GMainLoop* loop;staticvoidon_stream_status (GstBus *bus, GstMessage *message, gpointer user_data){ GstStreamStatusType type; GstElement *owner;const GValue *val; GstTask *task =NULL;gst_message_parse_stream_status (message,&type,&owner); val =gst_message_get_stream_status_object (message); /* see if we know how to deal with this object */if (G_VALUE_TYPE (val)== GST_TYPE_TASK) { task =g_value_get_object (val); }switch (type) {case GST_STREAM_STATUS_TYPE_CREATE:if (task) { GstTaskPool *pool; pool =test_rt_pool_new();gst_task_set_pool (task, pool); }break;default:break; }}staticvoidon_error (GstBus *bus, GstMessage *message, gpointer user_data){g_message ("received ERROR");g_main_loop_quit (loop);}staticvoidon_eos (GstBus *bus, GstMessage *message, gpointer user_data){g_main_loop_quit (loop);}intmain (int argc,char*argv[]){ GstElement *bin,*fakesrc,*fakesink; GstBus *bus; GstStateChangeReturn ret;gst_init (&argc,&argv); /* create a new bin to hold the elements */ bin =gst_pipeline_new ("pipeline");g_assert (bin); /* create a source */ fakesrc =gst_element_factory_make ("fakesrc","fakesrc");g_assert (fakesrc);g_object_set (fakesrc,"num-buffers",50,NULL); /* and a sink */ fakesink =gst_element_factory_make ("fakesink","fakesink");g_assert (fakesink); /* add objects to the main pipeline */gst_bin_add_many (GST_BIN (bin), fakesrc, fakesink,NULL); /* link the elements */gst_element_link (fakesrc, fakesink); loop =g_main_loop_new (NULL,FALSE); /* get the bus, we need to install a sync handler */ bus =gst_pipeline_get_bus (GST_PIPELINE (bin));gst_bus_enable_sync_message_emission (bus);gst_bus_add_signal_watch (bus);g_signal_connect (bus,"sync-message::stream-status", (GCallback) on_stream_status,NULL);g_signal_connect (bus,"message::error", (GCallback) on_error,NULL);g_signal_connect (bus,"message::eos", (GCallback) on_eos,NULL); /* start playing */ ret =gst_element_set_state (bin, GST_STATE_PLAYING);if (ret != GST_STATE_CHANGE_SUCCESS) {g_message ("failed to change state");return-1; } /* Run event loop listening for bus messages until EOS or ERROR */g_main_loop_run (loop); /* stop the bin */gst_element_set_state (bin, GST_STATE_NULL);gst_object_unref (bus);g_main_loop_unref (loop);return0;}