GStreamer-1.0的使用--基础教程(2):GStreamer的概念 C语言


前言

Basic tutorial 2: GStreamer concepts
基础教程2:GStreamer概念


提示:以下是本篇文章正文内容,下面案例可供参考

一、目标

前面的教程展示了如何自动构建管道。现在,我们将通过实例化每个元素并将它们连接在一起来手动构建一个管道。在这个过程中,我们会学到:

  • 什么是GStreamer的元件(element)以及如何创建它。
  • 如何将elements相互连接。
  • 如何自定义element的功能。
  • 如何监视总线中出现错误的情况并从GStreamer消息中提取信息。

二、手动编写Hello World!

将此代码复制到名为basic-tutorial-2.c的文本文件中(或者在您下载的GStreamer示例中找到它)。
basic-tutorial-2.c

#include <gst/gst.h>

int
main (int argc, char *argv[])
{
  GstElement *pipeline, *source, *sink;
  GstBus *bus;
  GstMessage *msg;
  GstStateChangeReturn ret;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  /* Create the elements */
  source = gst_element_factory_make ("videotestsrc", "source");
  sink = gst_element_factory_make ("autovideosink", "sink");

  /* Create the empty pipeline */
  pipeline = gst_pipeline_new ("test-pipeline");

  if (!pipeline || !source || !sink) {
    g_printerr ("Not all elements could be created.\n");
    return -1;
  }

  /* Build the pipeline */
  gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
  if (gst_element_link (source, sink) != TRUE) {
    g_printerr ("Elements could not be linked.\n");
    gst_object_unref (pipeline);
    return -1;
  }

  /* Modify the source's properties */
  g_object_set (source, "pattern", 0, NULL);

  /* Start playing */
  ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    g_printerr ("Unable to set the pipeline to the playing state.\n");
    gst_object_unref (pipeline);
    return -1;
  }

  /* Wait until error or EOS */
  bus = gst_element_get_bus (pipeline);
  msg =
      gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
      GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

  /* Parse message */
  if (msg != NULL) {
    GError *err;
    gchar *debug_info;

    switch (GST_MESSAGE_TYPE (msg)) {
      case GST_MESSAGE_ERROR:
        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);
        break;
      case GST_MESSAGE_EOS:
        g_print ("End-Of-Stream reached.\n");
        break;
      default:
        /* We should not reach here because we only asked for ERRORs and EOS */
        g_printerr ("Unexpected message received.\n");
        break;
    }
    gst_message_unref (msg);
  }

  /* Free resources */
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return 0;
}

Need help?
如果你需要帮助来编译这段代码,请参考为你的平台所Building the tutorials 部分:Linux,Mac OS XWindows,或者在Linux上使用这个特定的命令:

gcc basic-tutorial-2.c -o basic-tutorial-2 `pkg-config --cflags --libs gstreamer-1.0`
如果您需要帮助来运行这些代码,请参考您的平台对应的Running the tutorials部分:LinuxMac OS X[2]Windows
本教程将打开一个窗口并显示一个测试模式,但不包含音频。
必需的库:gstreamer - 1.0

三、示例通读

这些elements是GStreamer的基本构造块。它们从source elements(data producers)通过filter elements向sink elements(data consumers),传输数据。

源—滤波器—汇

图1:pipeline举例
在这里插入图片描述

1、Element创建

我们将跳过GStreamer初始化,因为它与前一教程相同:

  /* Create the elements */
  source = gst_element_factory_make ("videotestsrc", "source");
  sink = gst_element_factory_make ("autovideosink", "sink");

正如在这段代码中看到的,可以使用gst_element_factory_make()创建新elements。第一个参数是要创建的element的类型(基础教程14:灵活的elements展示了一些常见类型,而基础教程10:GStreamer工具展示了如何获取所有可用类型的列表)。第二个参数是我们想给这个特定实例的名称。如果没有保存指针,elements的命名便于以后检索它们(以及更有意义的调试输出)。但是,如果命名为NULL, GStreamer将自动为它提供一个惟一的名称。
对于本教程,我们创建了两个elements:videotestsrcautovideosink。没有filter元素。因此,管道看起来如下所示:
图2:本教程中构建的管道
在这里插入图片描述
Videotestsrc是一个source element(生成数据),它创建呃一个测试视频。这个元素对于调试(和教程)很有用,在实际应用程序中通常找不到。
Autovideosink是一个sink element(接收数据),它在一个窗口上显示它接收到的图像。根据操作系统的不同,存在一些具有不同功能的视频接收器。Autovideosink自动选择并实例化最优的音频源,因此您不必担心细节,而且您的代码更易跨平台使用。

2、Pipeline 创建

代码如下:

1.

  /* Create the empty pipeline/创建空管道 */
  pipeline = gst_pipeline_new ("test-pipeline");

GStreamer中的所有元素在使用之前必须包含在管道中,因为它负责一些时钟和信息传递功能。我们使用gst_pipeline_new()创建管道。

2.

  /* Build the pipeline */
  gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
  if (gst_element_link (source, sink) != TRUE) {
    g_printerr ("Elements could not be linked.\n");
    gst_object_unref (pipeline);
    return -1;
  }

管道pipeline)是一种特殊类型的容器(bin),它是用来包含其他elements的element。因此,适用于bin的所有方法也适用于管道。

在我们的例子中,我们调用gst_bin_add_many()将elements添加到管道中(mind the cast)。这个函数接收要添加的elements列表,以NULL结尾。可以使用gst_bin_add()添加单个element。

然而,这些元素还没有相互联系起来。为解决这个问题,我们需要使用gst_element_link()函数。它的第一个参数是源(source),第二个参数是目标(destination)。顺序很重要,因为必须按照数据流向(即从源element到汇element)建立链接。请记住,只有驻留在同一个容器中的元素才能链接在一起,所以在尝试链接它们之前,请记住将它们添加到管道中!

3、Properties

GStreamer中的elements都是一种特殊的GObject,它是提供某种功能属性的个体。

大多数GStreamer元素都有可定制的属性(properties):可以修改已命名的属性以改变element的功能(可写属性)或查询element的内部状态(可读属性)。使用g_object_get()函数读取element属性,使用g_object_set()函数为element写入属性。

g_object_set()函数接收以NULL结尾的property-name, property-value对儿,因此可以一次性更改多个属性。

这就是为什么属性处理函数前面有g_前缀。

回到上面的例子,

  /* Modify the source's properties/修改源element的属性 */
  g_object_set (source, "pattern", 0, NULL);

上面的代码更改了videotestsrc这个element的" pattern "属性,该属性控制element输出的测试视频的类型。尝试更改为不同值试试!

可以使用基础教程10:GStreamer工具中描述的gst-inspect-1.0工具或该element的文档(本示例使用的element是videotestsrc)找到elements公开的所有属性的名称和可以赋予的值。

4、报错核验


总结

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值