本教程介绍了如何整合 GStreamer 到一个图形用户界面(GUI)工具包像GStreamer的集成GTK +。基本上,当GUI与用户交互时,GStreamer须要关心媒体的播放。最有趣的部分是这两个库的互动:指示GStreamer输出视频到GTK +的窗口和转发用户的操做到GStreamer。html
特别是,您将了解到:web
如何告诉GStreamer将视频输出到一个特定的窗口(而不是建立本身的窗口)。windows
如何根据由GStreamer发送的信息而不断刷新用户图形界面。数组
如何从GStreamer的多个线程中更新用户图形界面。app
有一种机制来只订阅您感兴趣的信息,而不是全部的信息都被告知,。less
咱们将使用GTK + 工具包创建一个媒体播放器,但这些概念也适用于其余工具包,如 QT,例如。基本的 GTK+ 知识将有助于理解本教程。ide
主要的一点是告诉GStreamer输出视频到咱们选择的窗口。具体的机制依赖于操做系统(或者更确切地说是窗口系统),但GStreamer中提供了一个抽象层,具备平台独立性。这种独立性是经过在
XOverlay
接口,它容许应用程序告诉视频接收器(video sink)一个应该获得渲染的窗口的句柄。函数
![]() |
图形对象的接口 GObject的 |
另外一个问题是,GUI工具包一般只容许经过主(或应用程序)的线程操做的图形化的“小部件”,,而GStreamer的一般产生多个线程来采起不一样的任务护理。调用GTK + 从内回调函数一般会失败,由于回调调用线程,这并不须要是主线程中执行。这个问题能够经过发布一条消息到GStreamer的总线上,而后由主线程的回调来响应该消息。oop
最后,到目前为止,咱们已经注册了一个能够获取每次出如今总线上的消息的 方法 handle_message
,这迫使咱们要分析每一个消息,看它是不是咱们感兴趣的。在本教程中不一样的方法用于为各类消息注册一个回调,因此有较少的分析和总体更少的代码。
让咱们来编写一个基于playbin2,具备图形用户界面、很是简单的媒体播放器!
将此代码复制到名为的文本文件
。basic-tutorial-5.c
#include <string.h> #include <gtk/gtk.h> #include <gst/gst.h> #include <gst/interfaces/xoverlay.h> #include <gdk/gdk.h> #if defined (GDK_WINDOWING_X11) #include <gdk/gdkx.h> #elif defined (GDK_WINDOWING_WIN32) #include <gdk/gdkwin32.h> #elif defined (GDK_WINDOWING_QUARTZ) #include <gdk/gdkquartz.h> #endif /* Structure to contain all our information, so we can pass it around */ typedef struct _CustomData { GstElement *playbin2; /* Our one and only pipeline */ GtkWidget *slider; /* Slider widget to keep track of current position */ GtkWidget *streams_list; /* Text widget to display info about the streams */ gulong slider_update_signal_id; /* Signal ID for the slider update signal */ GstState state; /* Current state of the pipeline */ gint64 duration; /* Duration of the clip, in nanoseconds */ } CustomData; /* This function is called when the GUI toolkit creates the physical window that will hold the video. * At this point we can retrieve its handler (which has a different meaning depending on the windowing system) * and pass it to GStreamer through the XOverlay interface. */ static void realize_cb (GtkWidget *widget, CustomData *data) { GdkWindow *window = gtk_widget_get_window (widget); guintptr window_handle; if (!gdk_window_ensure_native (window)) g_error ("Couldn't create native window needed for GstXOverlay!"); /* Retrieve window handler from GDK */ #if defined (GDK_WINDOWING_WIN32) window_handle = (guintptr)GDK_WINDOW_HWND (window); #elif defined (GDK_WINDOWING_QUARTZ) window_handle = gdk_quartz_window_get_nsview (window); #elif defined (GDK_WINDOWING_X11) window_handle = GDK_WINDOW_XID (window); #endif /* Pass it to playbin2, which implements XOverlay and will forward it to the video sink */ gst_x_overlay_set_window_handle (GST_X_OVERLAY (data->playbin2), window_handle); } /* This function is called when the PLAY button is clicked */ static void play_cb (GtkButton *button, CustomData *data) { gst_element_set_state (data->playbin2, GST_STATE_PLAYING); } /* This function is called when the PAUSE button is clicked */ static void pause_cb (GtkButton *button, CustomData *data) { gst_element_set_state (data->playbin2, GST_STATE_PAUSED); } /* This function is called when the STOP button is clicked */ static void stop_cb (GtkButton *button, CustomData *data) { gst_element_set_state (data->playbin2, GST_STATE_READY); } /* This function is called when the main window is closed */ static void delete_event_cb (GtkWidget *widget, GdkEvent *event, CustomData *data) { stop_cb (NULL, data); gtk_main_quit (); } /* This function is called everytime the video window needs to be redrawn (due to damage/exposure, * rescaling, etc). GStreamer takes care of this in the PAUSED and PLAYING states, otherwise, * we simply draw a black rectangle to avoid garbage showing up. */ static gboolean expose_cb (GtkWidget *widget, GdkEventExpose *event, CustomData *data) { if (data->state < GST_STATE_PAUSED) { GtkAllocation allocation; GdkWindow *window = gtk_widget_get_window (widget); cairo_t *cr; /* Cairo is a 2D graphics library which we use here to clean the video window. * It is used by GStreamer for other reasons, so it will always be available to us. */ gtk_widget_get_allocation (widget, &allocation); cr = gdk_cairo_create (window); cairo_set_source_rgb (cr, 0, 0, 0); cairo_rectangle (cr, 0, 0, allocation.width, allocation.height); cairo_fill (cr); cairo_destroy (cr); } return FALSE; } /* This function is called when the slider changes its position. We perform a seek to the * new position here. */ static void slider_cb (GtkRange *range, CustomData *data) { gdouble value = gtk_range_get_value (GTK_RANGE (data->slider)); gst_element_seek_simple (data->playbin2, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, (gint64)(value * GST_SECOND)); } /* This creates all the GTK+ widgets that compose our application, and registers the callbacks */ static void create_ui (CustomData *data) { GtkWidget *main_window; /* The uppermost window, containing all other windows */ GtkWidget *video_window; /* The drawing area where the video will be shown */ GtkWidget *main_box; /* VBox to hold main_hbox and the controls */ GtkWidget *main_hbox; /* HBox to hold the video_window and the stream info text widget */ GtkWidget *controls; /* HBox to hold the buttons and the slider */ GtkWidget *play_button, *pause_button, *stop_button; /* Buttons */ main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL); g_signal_connect (G_OBJECT (main_window), "delete-event", G_CALLBACK (delete_event_cb), data); video_window = gtk_drawing_area_new (); gtk_widget_set_double_buffered (video_window, FALSE); g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), data); g_signal_connect (video_window, "expose_event", G_CALLBACK (expose_cb), data); play_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PLAY); g_signal_connect (G_OBJECT (play_button), "clicked", G_CALLBACK (play_cb), data); pause_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_PAUSE); g_signal_connect (G_OBJECT (pause_button), "clicked", G_CALLBACK (pause_cb), data); stop_button = gtk_button_new_from_stock (GTK_STOCK_MEDIA_STOP); g_signal_connect (G_OBJECT (stop_button), "clicked", G_CALLBACK (stop_cb), data); data->slider = gtk_hscale_new_with_range (0, 100, 1); gtk_scale_set_draw_value (GTK_SCALE (data->slider), 0); data->slider_update_signal_id = g_signal_connect (G_OBJECT (data->slider), "value-changed", G_CALLBACK (slider_cb), data); data->streams_list = gtk_text_view_new (); gtk_text_view_set_editable (GTK_TEXT_VIEW (data->streams_list), FALSE); controls = gtk_hbox_new (FALSE, 0); gtk_box_pack_start (GTK_BOX (controls), play_button, FALSE, FALSE, 2); gtk_box_pack_start (GTK_BOX (controls), pause_button, FALSE, FALSE, 2); gtk_box_pack_start (GTK_BOX (controls), stop_button, FALSE, FALSE, 2); gtk_box_pack_start (GTK_BOX (controls), data->slider, TRUE, TRUE, 2); main_hbox = gtk_hbox_new (FALSE, 0); gtk_box_pack_start (GTK_BOX (main_hbox), video_window, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (main_hbox), data->streams_list, FALSE, FALSE, 2); main_box = gtk_vbox_new (FALSE, 0); gtk_box_pack_start (GTK_BOX (main_box), main_hbox, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (main_box), controls, FALSE, FALSE, 0); gtk_container_add (GTK_CONTAINER (main_window), main_box); gtk_window_set_default_size (GTK_WINDOW (main_window), 640, 480); gtk_widget_show_all (main_window); } /* This function is called periodically to refresh the GUI */ static gboolean refresh_ui (CustomData *data) { GstFormat fmt = GST_FORMAT_TIME; gint64 current = -1; /* We do not want to update anything unless we are in the PAUSED or PLAYING states */ if (data->state < GST_STATE_PAUSED) return TRUE; /* If we didn't know it yet, query the stream duration */ if (!GST_CLOCK_TIME_IS_VALID (data->duration)) { if (!gst_element_query_duration (data->playbin2, &fmt, &data->duration)) { g_printerr ("Could not query current duration.\n"); } else { /* Set the range of the slider to the clip duration, in SECONDS */ gtk_range_set_range (GTK_RANGE (data->slider), 0, (gdouble)data->duration / GST_SECOND); } } if (gst_element_query_position (data->playbin2, &fmt, ¤t)) { /* Block the "value-changed" signal, so the slider_cb function is not called * (which would trigger a seek the user has not requested) */ g_signal_handler_block (data->slider, data->slider_update_signal_id); /* Set the position of the slider to the current pipeline positoin, in SECONDS */ gtk_range_set_value (GTK_RANGE (data->slider), (gdouble)current / GST_SECOND); /* Re-enable the signal */ g_signal_handler_unblock (data->slider, data->slider_update_signal_id); } return TRUE; } /* This function is called when new metadata is discovered in the stream */ static void tags_cb (GstElement *playbin2, gint stream, CustomData *data) { /* We are possibly in a GStreamer working thread, so we notify the main * thread of this event through a message in the bus */ gst_element_post_message (playbin2, gst_message_new_application (GST_OBJECT (playbin2), gst_structure_new ("tags-changed", NULL))); } /* This function is called when an error message is posted on the bus */ static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) { GError *err; gchar *debug_info; /* Print error details on the screen */ gst_message_parse_error (msg, &err, &debug_info); g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message); g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none"); g_clear_error (&err); g_free (debug_info); /* Set the pipeline to READY (which stops playback) */ gst_element_set_state (data->playbin2, GST_STATE_READY); } /* This function is called when an End-Of-Stream message is posted on the bus. * We just set the pipeline to READY (which stops playback) */ static void eos_cb (GstBus *bus, GstMessage *msg, CustomData *data) { g_print ("End-Of-Stream reached.\n"); gst_element_set_state (data->playbin2, GST_STATE_READY); } /* This function is called when the pipeline changes states. We use it to * keep track of the current state. */ static void state_changed_cb (GstBus *bus, GstMessage *msg, CustomData *data) { 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 (data->playbin2)) { data->state = new_state; g_print ("State set to %s\n", gst_element_state_get_name (new_state)); if (old_state == GST_STATE_READY && new_state == GST_STATE_PAUSED) { /* For extra responsiveness, we refresh the GUI as soon as we reach the PAUSED state */ refresh_ui (data); } } } /* Extract metadata from all the streams and write it to the text widget in the GUI */ static void analyze_streams (CustomData *data) { gint i; GstTagList *tags; gchar *str, *total_str; guint rate; gint n_video, n_audio, n_text; GtkTextBuffer *text; /* Clean current contents of the widget */ text = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->streams_list)); gtk_text_buffer_set_text (text, "", -1); /* Read some properties */ g_object_get (data->playbin2, "n-video", &n_video, NULL); g_object_get (data->playbin2, "n-audio", &n_audio, NULL); g_object_get (data->playbin2, "n-text", &n_text, NULL); for (i = 0; i < n_video; i++) { tags = NULL; /* Retrieve the stream's video tags */ g_signal_emit_by_name (data->playbin2, "get-video-tags", i, &tags); if (tags) { total_str = g_strdup_printf ("video stream %d:\n", i); gtk_text_buffer_insert_at_cursor (text, total_str, -1); g_free (total_str); gst_tag_list_get_string (tags, GST_TAG_VIDEO_CODEC, &str); total_str = g_strdup_printf (" codec: %s\n", str ? str : "unknown"); gtk_text_buffer_insert_at_cursor (text, total_str, -1); g_free (total_str); g_free (str); gst_tag_list_free (tags); } } for (i = 0; i < n_audio; i++) { tags = NULL; /* Retrieve the stream's audio tags */ g_signal_emit_by_name (data->playbin2, "get-audio-tags", i, &tags); if (tags) { total_str = g_strdup_printf ("\naudio stream %d:\n", i); gtk_text_buffer_insert_at_cursor (text, total_str, -1); g_free (total_str); if (gst_tag_list_get_string (tags, GST_TAG_AUDIO_CODEC, &str)) { total_str = g_strdup_printf (" codec: %s\n", str); gtk_text_buffer_insert_at_cursor (text, total_str, -1); g_free (total_str); g_free (str); } if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_CODE, &str)) { total_str = g_strdup_printf (" language: %s\n", str); gtk_text_buffer_insert_at_cursor (text, total_str, -1); g_free (total_str); g_free (str); } if (gst_tag_list_get_uint (tags, GST_TAG_BITRATE, &rate)) { total_str = g_strdup_printf (" bitrate: %d\n", rate); gtk_text_buffer_insert_at_cursor (text, total_str, -1); g_free (total_str); } gst_tag_list_free (tags); } } for (i = 0; i < n_text; i++) { tags = NULL; /* Retrieve the stream's subtitle tags */ g_signal_emit_by_name (data->playbin2, "get-text-tags", i, &tags); if (tags) { total_str = g_strdup_printf ("\nsubtitle stream %d:\n", i); gtk_text_buffer_insert_at_cursor (text, total_str, -1); g_free (total_str); if (gst_tag_list_get_string (tags, GST_TAG_LANGUAGE_CODE, &str)) { total_str = g_strdup_printf (" language: %s\n", str); gtk_text_buffer_insert_at_cursor (text, total_str, -1); g_free (total_str); g_free (str); } gst_tag_list_free (tags); } } } /* This function is called when an "application" message is posted on the bus. * Here we retrieve the message posted by the tags_cb callback */ static void application_cb (GstBus *bus, GstMessage *msg, CustomData *data) { if (g_strcmp0 (gst_structure_get_name (msg->structure), "tags-changed") == 0) { /* If the message is the "tags-changed" (only one we are currently issuing), update * the stream info GUI */ analyze_streams (data); } } int main(int argc, char *argv[]) { CustomData data; GstStateChangeReturn ret; GstBus *bus; /* Initialize GTK */ gtk_init (&argc, &argv); /* Initialize GStreamer */ gst_init (&argc, &argv); /* Initialize our data structure */ memset (&data, 0, sizeof (data)); data.duration = GST_CLOCK_TIME_NONE; /* Create the elements */ data.playbin2 = gst_element_factory_make ("playbin2", "playbin2"); if (!data.playbin2) { g_printerr ("Not all elements could be created.\n"); return -1; } /* Set the URI to play */ g_object_set (data.playbin2, "uri", "http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL); /* Connect to interesting signals in playbin2 */ g_signal_connect (G_OBJECT (data.playbin2), "video-tags-changed", (GCallback) tags_cb, &data); g_signal_connect (G_OBJECT (data.playbin2), "audio-tags-changed", (GCallback) tags_cb, &data); g_signal_connect (G_OBJECT (data.playbin2), "text-tags-changed", (GCallback) tags_cb, &data); /* Create the GUI */ create_ui (&data); /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ bus = gst_element_get_bus (data.playbin2); gst_bus_add_signal_watch (bus); g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data); g_signal_connect (G_OBJECT (bus), "message::eos", (GCallback)eos_cb, &data); g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, &data); g_signal_connect (G_OBJECT (bus), "message::application", (GCallback)application_cb, &data); gst_object_unref (bus); /* Start playing */ ret = gst_element_set_state (data.playbin2, GST_STATE_PLAYING); if (ret == GST_STATE_CHANGE_FAILURE) { g_printerr ("Unable to set the pipeline to the playing state.\n"); gst_object_unref (data.playbin2); return -1; } /* Register a function that GLib will call every second */ g_timeout_add_seconds (1, (GSourceFunc)refresh_ui, &data); /* Start the GTK main loop. We will not regain control until gtk_main_quit is called. */ gtk_main (); /* Free resources */ gst_element_set_state (data.playbin2, GST_STATE_NULL); gst_object_unref (data.playbin2); return 0; }
关于本教程的结构,咱们不打算使用提早的函数定义了:函数将被定义在使用以前。此外,为了解释清楚,其中的代码片断显示的顺序不会永远与程序顺序相匹配。使用行号来在完整代码中定位片断。
#include <gdk/gdk.h> #if defined (GDK_WINDOWING_X11) #include <gdk/gdkx.h> #elif defined (GDK_WINDOWING_WIN32) #include <gdk/gdkwin32.h> #elif defined (GDK_WINDOWING_QUARTZ) #include <gdk/gdkquartzwindow.h> #endif
值得关注的第一件事是,咱们再也不是彻底独立于平台的。咱们须要包含适当的GDK标题的窗口系统。幸运的是,没有那么多的可支持视窗系统,因此这三行每每就够了:X11适用于Linux,Win32适用于Windows和Quartz适用于Mac OSX系统。
本教程是由回调函数组成,这些回调将是从GStreamer的或GTK +调用的,因此让咱们来回顾一下主函数,注册全部这些回调。
int main(int argc, char *argv[]) { CustomData data; GstStateChangeReturn ret; GstBus *bus; /* Initialize GTK */ gtk_init (&argc, &argv); /* Initialize GStreamer */ gst_init (&argc, &argv); /* Initialize our data structure */ memset (&data, 0, sizeof (data)); data.duration = GST_CLOCK_TIME_NONE; /* Create the elements */ data.playbin2 = gst_element_factory_make ("playbin2", "playbin2"); if (!data.playbin2) { g_printerr ("Not all elements could be created.\n"); return -1; } /* Set the URI to play */ g_object_set (data.playbin2, "uri", " "http://docs.gstreamer.com/media/sintel_trailer-480p.webm", NULL);
标准的GStreamer的初始化和playbin2管道的建立,以及GTK +的初始化。没有太多新意。
/* Connect to interesting signals in playbin2 */ g_signal_connect (G_OBJECT (data.playbin2), "video-tags-changed", (GCallback) tags_cb, &data); g_signal_connect (G_OBJECT (data.playbin2), "audio-tags-changed", (GCallback) tags_cb, &data); g_signal_connect (G_OBJECT (data.playbin2), "text-tags-changed", (GCallback) tags_cb, &data);
当新的标签(元数据)出如今流中,咱们感兴趣的事件被通知。为简单起见,咱们将用同一个回调 tags_cb 处理各类标记(视频,音频和文本)。
/* Create the GUI */ create_ui (&data);
全部的GTK +控件的建立和信号登记发生在这个函数。它仅包含GTK相关的函数调用,所以咱们将跳过它的定义。其注册的信号传达用户命令,以下方所示。
/* Instruct the bus to emit signals for each received message, and connect to the interesting signals */ bus = gst_element_get_bus (data.playbin2); gst_bus_add_signal_watch (bus); g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data); g_signal_connect (G_OBJECT (bus), "message::eos", (GCallback)eos_cb, &data); g_signal_connect (G_OBJECT (bus), "message::state-changed", (GCallback)state_changed_cb, &data); g_signal_connect (G_OBJECT (bus), "message::application", (GCallback)application_cb, &data); gst_object_unref (bus);
在Playback tutorial 1: Playbin2 usage , gst_bus_add_watch() 用于注册从GStreamer的总线接收每一个消息的函数。咱们能够达到更精细的粒度经过使用信号来代替,这使咱们可以只注册咱们感兴趣的消息。
gst_bus_add_signal_watch()
咱们指示总线每收到一条消息时发出一个信号。这个信号的名称为 message::detail
,其中的
是触发信号发射的消息。例如,当总线接收到的EOS消息时,它发出一个以message::eos命名的信号。detail
本教程使用的是Signals
的细节,只注册咱们关心的消息。若是咱们已经注册 message
的信号,咱们将通知全部单个消息,就像
会作的同样。gst_bus_add_watch()
记住的是,为了使总线的观察机制工做(不管是gst_bus_add_watch()
或gst_bus_add_signal_watch()
),必须有GLib的主循环(
运行。在这种状况下,它被隐藏在GTK + 主循环的内部。Main Loop
)
/* Register a function that GLib will call every second */ g_timeout_add_seconds (1, (GSourceFunc)refresh_ui, &data);
为了动态控制GTK+,咱们使用
g_timeout_add_seconds ()
来注册另外一个回调,每次超时,它将呗调用一次。咱们将从 refresh_ui 函数中调用它来刷新GUI。
在此以后,咱们已经完成了安装并能启动GTK +主循环。咱们会从咱们的回调从新得到控制时,有趣的事情发生。让咱们回顾一下回调。每次回调都有一个不一样的签名,这取决于谁将会调用它。你能够看看签名(参数的意义和返回值),在信号的文档中。
/* This function is called when the GUI toolkit creates the physical window that will hold the video. * At this point we can retrieve its handler (which has a different meaning depending on the windowing system) * and pass it to GStreamer through the XOverlay interface. */ static void realize_cb (GtkWidget *widget, CustomData *data) { GdkWindow *window = gtk_widget_get_window (widget); guintptr window_handle; if (!gdk_window_ensure_native (window)) g_error ("Couldn't create native window needed for GstXOverlay!"); /* Retrieve window handler from GDK */ #if defined (GDK_WINDOWING_WIN32) window_handle = (guintptr)GDK_WINDOW_HWND (window); #elif defined (GDK_WINDOWING_QUARTZ) window_handle = gdk_quartz_window_get_nsview (window); #elif defined (GDK_WINDOWING_X11) window_handle = GDK_WINDOW_XID (window); #endif /* Pass it to playbin2, which implements XOverlay and will forward it to the video sink */ gst_x_overlay_set_window_handle (GST_X_OVERLAY (data->playbin2), window_handle); }
代码自己会完成会话。此时在应用程序的生命周期中,咱们知道即将表达视频的窗口句柄(不管是一个X11的XID
,一个Window's HWND
或Quartz's NSView
)。咱们只是从窗口系统进行检索,并经过 XOverlay 接口使用
。gst_x_overlay_set_window_handle() 把它传递给
playbin2
将查找视频接收段并将窗口句柄传递给本身,因此它不会建立本身的窗口,并使用这一个。playbin2
这里不用看太多; playbin2
和
XOverlay
真正大大地简化这个过程了!
/* This function is called when the PLAY button is clicked */ static void play_cb (GtkButton *button, CustomData *data) { gst_element_set_state (data->playbin2, GST_STATE_PLAYING); } /* This function is called when the PAUSE button is clicked */ static void pause_cb (GtkButton *button, CustomData *data) { gst_element_set_state (data->playbin2, GST_STATE_PAUSED); } /* This function is called when the STOP button is clicked */ static void stop_cb (GtkButton *button, CustomData *data) { gst_element_set_state (data->playbin2, GST_STATE_READY); }
这三个小回调与GUI的播放,暂停和中止按钮相关联。他们简单地将管道输送到相应的状态。请注意,在中止状态下,咱们设置管道的状态为就绪(READY)。咱们也能够把管道的全部向下的状态置空,但这样过渡会慢一些,由于有些资源(如音频设备)将须要被释放并从新获取。
/* This function is called when the main window is closed */ static void delete_event_cb (GtkWidget *widget, GdkEvent *event, CustomData *data) { stop_cb (NULL, data); gtk_main_quit (); }
gtk_main_quit()将最终再调用到主函数中的 gtk_main_run()以终止程序。在这里,咱们在当主窗口被关闭、管道被中止(只是为了整洁)以后调用它。
/* This function is called everytime the video window needs to be redrawn (due to damage/exposure, * rescaling, etc). GStreamer takes care of this in the PAUSED and PLAYING states, otherwise, * we simply draw a black rectangle to avoid garbage showing up. */ static gboolean expose_cb (GtkWidget *widget, GdkEventExpose *event, CustomData *data) { if (data->state < GST_STATE_PAUSED) { GtkAllocation allocation; GdkWindow *window = gtk_widget_get_window (widget); cairo_t *cr; /* Cairo is a 2D graphics library which we use here to clean the video window. * It is used by GStreamer for other reasons, so it will always be available to us. */ gtk_widget_get_allocation (widget, &allocation); cr = gdk_cairo_create (window); cairo_set_source_rgb (cr, 0, 0, 0); cairo_rectangle (cr, 0, 0, allocation.width, allocation.height); cairo_fill (cr); cairo_destroy (cr); } return FALSE; }
当有数据流(在PAUSED
和PLAYING
状态)的视频接收器采用清爽的视频窗口的内容负责。在其余的状况下,可是,它不会,因此咱们必须这样作。在这个例子中,咱们只是填补了窗口,一个黑色矩形。
当有数据流(在PAUSED 和 PLAYING 状态),视频接收器刷新视频窗口的内容。在其余状况下,它则不刷新。在这个例子中,咱们只是用一个黑色矩形填补了窗口。
/* This function is called when the slider changes its position. We perform a seek to the * new position here. */ static void slider_cb (GtkRange *range, CustomData *data) { gdouble value = gtk_range_get_value (GTK_RANGE (data->slider)); gst_element_seek_simple (data->playbin2, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, (gint64)(value * GST_SECOND)); }
这是一个经过与GStreamer 和 GTK+合做很容易地完成具备一个滑动条的复杂的GUI的播放器的例子。若是滑块一直拖动到新的位置,告诉GStreamer经过 gst_element_seek_simple()
寻求到该位置(如在 Basic tutorial 4: Time management看到的同样)。滑块已设置它如今的秒一级的值。
值得一提的是,一些性能(和响应)能够经过作一些限制来得到,这就是不回应每个用户的请求。因为寻道操做势必会须要必定的时间,它每每是须要等待半秒钟(例如),在容许后 一个请求前。不然在响应一个请求前,若是用户拖动滑块疯狂,应用程序看起来会没有反应。
/* This function is called periodically to refresh the GUI */ static gboolean refresh_ui (CustomData *data) { GstFormat fmt = GST_FORMAT_TIME; gint64 current = -1; /* We do not want to update anything unless we are in the PAUSED or PLAYING states */ if (data->state < GST_STATE_PAUSED) return TRUE;
此功能将移动滑块到与媒体文件对应的位置。首先,若是咱们不是在PLAYING
状态,咱们什么都没有作这里(plus,位置和持续时间的查询,一般会失败)。
/* If we didn't know it yet, query the stream duration */ if (!GST_CLOCK_TIME_IS_VALID (data->duration)) { if (!gst_element_query_duration (data->playbin2, &fmt, &data->duration)) { g_printerr ("Could not query current duration.\n"); } else { /* Set the range of the slider to the clip duration, in SECONDS */ gtk_range_set_range (GTK_RANGE (data->slider), 0, (gdouble)data->duration / GST_SECOND); } }
若是咱们不知道媒体的持续时间,咱们能够从新获取它,这样咱们能够设定滑块的范围。
if (gst_element_query_position (data->playbin2, &fmt, ¤t)) { /* Block the "value-changed" signal, so the slider_cb function is not called * (which would trigger a seek the user has not requested) */ g_signal_handler_block (data->slider, data->slider_update_signal_id); /* Set the position of the slider to the current pipeline positoin, in SECONDS */ gtk_range_set_value (GTK_RANGE (data->slider), (gdouble)current / GST_SECOND); /* Re-enable the signal */ g_signal_handler_unblock (data->slider, data->slider_update_signal_id); } return TRUE;
咱们查询当前管道的位置,并相应地设置滑块的位置。这将触发一个 value-changed
的信号,当用户拖动滑块。因为咱们不但愿发生seek,除非用户要求,咱们禁用 value-changed 这一信号的发射使用
和g_signal_handler_block()
。g_signal_handler_unblock() 函数
从这个函数返回TRUE,事件会继续传递。若是返回FALSE,定时器将被删除。
/* This function is called when new metadata is discovered in the stream */ static void tags_cb (GstElement *playbin2, gint stream, CustomData *data) { /* We are possibly in a GStreamer working thread, so we notify the main * thread of this event through a message in the bus */ gst_element_post_message (playbin2, gst_message_new_application (GST_OBJECT (playbin2), gst_structure_new ("tags-changed", NULL))); }
这是本教程的重点之一。这个函数会被调用时,新的标签被发如今媒体上,从流的线程,这是另外一个线程而不是应用程序(或主)的线程。咱们要在这里作的是更新一个GTK +控件以反映这个新的信息,但GTK +不容许从主线程之外的线程进行次操做。
解决的办法是让
playbin2
发布消息总线上,并返回给调用线程。在适当的时候,主线程会拿起这个消息,并更新GTK。
gst_element_post_message() 使得 GStreamer 元件将给定的消息发送到总线。
gst_message_new_application()
建立一个新的
类型的消息。GStreamer的消息有不一样的类型,而且该类型被保留到应用程序:它会经过总线而不受GStreamer的影响。类型列表能够在中找到APPLICATION
GstMessageType
文档。
信息能够经过其内嵌的 GstStructure
提供更多信息,这是一个很是灵活的数据容器。在这里,咱们建立了一个新的结构经过函数 gst_structure_new
,并将其命名为
tags-changed
,以免在咱们想送其余应用程序的消息的状况下发生混淆。
然后,在主线程,总线将收到此消息,并放出与函数application_cb相关联的
的信号:message::application
/* This function is called when an "application" message is posted on the bus. * Here we retrieve the message posted by the tags_cb callback */ static void application_cb (GstBus *bus, GstMessage *msg, CustomData *data) { if (g_strcmp0 (gst_structure_get_name (msg->structure), "tags-changed") == 0) { /* If the message is the "tags-changed" (only one we are currently issuing), update * the stream info GUI */ analyze_streams (data); } }
一旦我确信它是 tags-changed 的
消息,咱们称之为 analyze_streams 函数,也用于 Playback tutorial 1: Playbin2 usage ,更详细的在那里讲述。它基本上完成了从流中恢复标签并将它们写在GUI中的一个文本组件上。
该error_cb
,eos_cb
和state_changed_cb
就不具体向你们解释了,由于他们像在全部前面的教程中作的同样。
这就是它!代码本教程中的量彷佛使人生畏,但所须要了解的概念却不多也容易理解。若是你已经阅读了以往教程,并有必定的GTK知识的话,你会很天然而然地理解本教程,并能够开始享受本身的媒体播放器了。
若是这个媒体播放器对你来讲不够好,尝试改变,显示有关的数据流信息到一个适当的列表视图(或树状视图)的文本组件。而后,当用户选择不一样的数据流,使GStreamer切换流!要切换流,你将须要阅读 Playback tutorial 1: Playbin2 usage。
本教程显示:
如何使用 gst_x_overlay_set_window_handle()
输出的视频到一个特定的窗口句柄 。
如何按期刷新GUI经过使用 g_timeout_add_seconds ()
注册一个超时回调。
如何经过应用程序消息的手段,经过总线使用 gst_element_post_message()
将信息传递给主线程。
如何使用 gst_bus_add_signal_watch() 在只在从总线发出的消息为咱们感兴趣的消息时被通知和使用信号细节判别全部消息类型。
这容许你创建一个有些完整的媒体播放器与一个适当的图形用户界面。
下面的基本教程继续专一于其余的GStreamer主题。
很高兴在此与你一块儿度过,并但愿在之后的教程继续见到你!