Gstreamer官方教程汇总基本教程5---GUI toolkit integration

目标

本教程介绍了如何整合 GStreamer 到一个图形用户界面(GUI)工具包像GStreamer的集成GTK +。基本上,当GUI与用户交互时,GStreamer需要关心媒体的播放。最有趣的部分是这两个库的互动:指示GStreamer输出视频到GTK +的窗口和转发用户操作到GStreamer。


特别是,您将了解到:

  • 如何告诉GStreamer将视频输出到一个特定的窗口(而不是创建自己的窗口)。

  • 如何根据由GStreamer发送的信息而不断刷新用户图形界面。

  • 如何从GStreamer的多个线程更新用户图形界面。

  • 有一种机制来只订阅您感兴趣的信息,而不是所有的信息都被告知,。


介绍

我们将使用GTK + 工具包建立一个媒体播放器,但这些概念也适用于其他工具包,如 QT,例如。基本的 GTK+   知识将有助于理解本教程。

主要的一点是告诉GStreamer输出视频到我们选择的窗口。具体的机制依赖于操作系统(或者更确切地说是窗口系统),但GStreamer中提供了一个抽象层,具有平台独立性。这种独立性是通过在 XOverlay  接口,它允许应用程序告诉视频接收器(video sink)一个应该得到渲染的窗口的句柄。


图形对象的接口

GObject的接口(其中GStreamer中使用)是一组一个元素可以实现的功能。如果是的话,那么它被认为支持该特定的接口。例如,视频接收器通常创建自己的窗口来显示视频,但是,如果他们还能够呈现一个对外窗口,他们可以选择实施XOverlay接口,并提供功能来指定这个对外窗口。从整个应用程序开发点,是否支持某个接口,你可以使用它,而忘记了哪一种元素正在实施它。此外,如果您使用的是playbin2,它会自动揭露一些由它的内部元件所支持的接口:您可以直接使用您的接口函数playbin2不知道谁在执行这些!

另一个问题是,GUI工具包通常只允许通过主(或应用程序)的线程操作的图形化的“小部件”,,而GStreamer的通常产生多个线程来采取不同的任务护理。调用GTK +  从内回调函数通常会失败,因为回调调用线程,这并不需要是主线程中执行。这个问题可以通过发布一条消息到GStreamer的总线上,然后由主线程的回调来响应该消息。

最后,到目前为止,我们已经注册了一个可以获取每次出现在总线上的消息的 方法 handle_message ,这迫使我们要分析每个消息,看它是否是我们感兴趣的。在本教程中不同的方法用于为各种消息注册一个回调,所以有较少的分析和整体更少的代码。


一个媒体播放器中的GTK +

让我们来编写一个基于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, &current)) {
    /* 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  ,其中detail 是触发信号发射的消息。例如,当总线接收到的EOS消息时,它发出一个以message::eos命名的信号

本教程使用的是Signals的细节,只注册我们关心的消息。如果我们已经注册 message  的信号,我们将通知所有单个消息,就像 gst_bus_add_watch()  会做的一样。

记住的是,为了使总线的观察机制工作(无论是gst_bus_add_watch()  或gst_bus_add_signal_watch() ),必须有GLib的主循环Main Loop   运行。在这种情况下,它被隐藏在GTK +  主循环内部

/* 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, &current)) {
  /* 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()  创建一个新的 APPLICATION  类型的消息。GStreamer的消息有不同的类型,并且该类型被保留到应用程序:它会通过总线而不受GStreamer的影响。类型列表可以在中找到 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_cbeos_cb  和state_changed_cb  就不具体向大家解释了,因为他们像在所有前面的教程中做的一样。

这就是它!代码本教程中的量似乎令人生畏,但所需要了解的概念却很少也容易理解。如果你已经阅读了以往教程,并有一定的GTK知识的话,你会很自然而然地理解本教程,并可以开始享受自己的媒体播放器了。

03141925_dc52.png


锻炼

如果这个媒体播放器对你来说不够好,尝试改变,显示有关的数据流信息到一个适当的列表视图(或树状视图)的文本组件。然后,当用户选择不同的数据流,使GStreamer切换流!要切换流,你将需要阅读 Playback tutorial 1: Playbin2 usage

结论

本教程显示:

这允许你建立一个有些完整的媒体播放器与一个适当的图形用户界面。

下面的基本教程继续专注于其他的GStreamer主题。

很高兴在此与你一起度过,并希望在以后的教程继续见到你!


转载于:https://my.oschina.net/u/735973/blog/204599

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值