gstreamer学习(2)——手动创建一个pipeline并运行

此博客是在gstreamer官网学习并总结的学习概要,具体参考gstreamer官网教程基础教程 2:GStreamer 概念

实例

依然先运行官网给的实例,代码在下面(也可以在gstreamer代码中找到):

示例代码

#include <gst/gst.h>

#ifdef __APPLE__
#include <TargetConditionals.h>
#endif

int
tutorial_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;
}

int
main (int argc, char *argv[])
{
#if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE
  return gst_macos_main (tutorial_main, argc, argv, NULL);
#else
  return tutorial_main (argc, argv);
#endif
}

运行结果

在这里插入图片描述

概念

元素(elements)

元素(elements)是gstreamer里面的基础模块,一个最简单的pipeline如下:
在这里插入图片描述
source元素解析视频数据,通过filter元素,发送给sink模块播放

元素类型可参考链接: 简易元素

GstBin

bin是一个可以包含其他元素的元素(它的类型就是GstElement),
如何派生出来的:

GObject
    ╰──GInitiallyUnowned
        ╰──GstObject
            ╰──GstElement
                ╰──GstBin
                    ╰──GstPipeline

从上面可以看出来,pipeline是bin的一种,同时它们也都是特殊的element。它们本质上都是由GObject派生出来的。

BUS

它是由元素生成的,用来向应用程序传递message的对象,它传递的message会依次发送给应用程序。应用程序需要一直关注bus,来获取对应的消息,可以通过函数gst_bus_timed_pop_filtered()来获取,也可以使用信号来获取。

代码解析

元素创建

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

通过调用函数gst_element_factory_make()可以创建一个元素,第一个入参是元素类型,第二个参数是元素名,如果元素名是NULL,系统会自动指定一个唯一的名字。

创建pipeline

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

调用函数gst_pipeline_new()创建一个新的pipeline

  /* 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,所以适用于bin的接口也适用于pipeline。
函数gst_bin_add_many()用来给bin加入多个元素,如果只加入一个元素,则可以调用函数gst_bin_add()
函数 gst_element_link()用来关联两个元素,需要注意的是:要用来关联的两个元素必须在同一个bin里面,所以要先调用gst_bin_add()再关联两个元素。

配置元素

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

配置source元素的属性,属性名为pattern,属性值为0。
原生代码配置的属性结果已经放在上面的图里,修改这个属性值为1,则可以得到另一种结果:

在这里插入图片描述
如图所示,修改属性值之后,显示的图案变成了纯雪花。
元素公开的所有属性的名称和可能值可以使用 Basic tutorial 10: GStreamer tools 中描述的 gst-inspect-1.0 工具找到。

错误检查

 /* 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;
  }

这条语句开启播放并检查返回值,如果pipeline返回失败则退出。

  /* 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);
  }

通过gst_bus_timed_pop_filtered()获取对应的事件,如果是ERROR,可以调用gst_message_parse_error()函数获取具体错误。

课后练习

添加VertigoTV模块,这个模块具有旋转和缩放功能,此处,还加入了videoconvert模块,防止出现错误。
在这里插入图片描述
添加上述两个模块之后,显示的视频有了动态模糊效果。通过修改VertigoTV模块的speed参数,还可以修改动态模糊的速度。代码如下:

#include <gst/gst.h>

#ifdef __APPLE__
#include <TargetConditionals.h>
#endif

int
tutorial_main (int argc, char *argv[])
{
  GstElement *pipeline, *source, *sink, *vto, *vct;
  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");

  /* my elements */
  vto = gst_element_factory_make ("vertigotv", "myvto");
  vct = gst_element_factory_make ("videoconvert", "myvct");

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

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

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

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

  /* test: modify vto's speed */
  g_object_set (vto, "speed", 0.1, 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;
}

int
main (int argc, char *argv[])
{
#if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE
  return gst_macos_main (tutorial_main, argc, argv, NULL);
#else
  return tutorial_main (argc, argv);
#endif
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
GStreamer是一个流媒体处理的框架,用于在嵌入式系统和桌面环境中处理音频和视频数据。学习GStreamer可以帮助我们理解音视频流的处理、数据编码和解码、多媒体的播放和编辑等方面的知识。 要学习GStreamer,我们可以从以下几个方面入手: 1. 学习框架和架构:了解GStreamer的基本术语、组件和概念,例如Pipeline(管道)、Element(元素)、Pad(端口)等等。熟悉框架的架构可以帮助我们理解数据流的处理和转换。 2. 安装和配置:不同平台和操作系统的安装和配置方法可能略有不同。我们需要根据自己的需求选择合适的版本,然后按照相关文档进行安装和配置。 3. 编写和运行代码:GStreamer提供了C语言、Python等多种编程语言的接口和库。我们可以根据自己的熟悉程度选择合适的编程语言,并学习如何使用GStreamerAPI来编写音视频处理的代码。可以通过构建和运行一些简单的示例程序来加深理解。 4. 测试和调试:GStreamer提供了一些工具和命令行选项来进行测试和调试。我们可以使用这些工具来验证和评估我们的代码的性能和正确性,以及识别和解决可能出现的问题。 5. 学习案例和文档:在学习的过程中,我们可以参考一些实际的案例和文档来了解GStreamer的使用方法和最佳实践。可以通过在线文档、论坛、博客等渠道找到一些有价值的资源。 总结来说,学习GStreamer需要一定的编程基础和对音视频处理的兴趣。通过深入学习和实践,我们可以逐步掌握GStreamer的核心概念和应用技巧,为开发和调试音视频处理的应用程序提供有力的支持。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值