GStreamer 基础教程二: 核心概念

Posted on Fri 19 January 2024 in Journal

Abstract GStreamer 基础教程二: 核心概念
Authors Walter Fan
 Category    learning note  
Status v1.0
Updated 2024-01-19
License CC-BY-NC-ND 4.0

-- 老范编译自 GStreamer 官方教程

目标

上一篇教程展示了如何自动构建管道。

这篇教程将通过实例化每个元素并将它们链接在一起来手动构建管道。

在此过程中,我们将学习:

  • 什么是 GStreamer 元素以及如何创建一个元件?

  • 如何将元件相互连接?

  • 如何自定义元件的行为?

  • 如何监视总线的错误情况并从 GStreamer 消息中提取信息。

程序流程

flow

@startuml

start
: 初始化 gst_init();
: 创建元件 gst_element_factory_make();
: 创建管道 gst_pipeline_new();
: 添加到管道 gst_bin_add_many();
: 将元件连接起来 gst_element_link;
: 设置元件属性 g_object_set();
: 设置管道状态 gst_element_set_state();
: 等待媒体流结束或出错 gst_bus_timed_pop_filtered();
stop

@enduml

源代码

#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
}
gcc basic-tutorial-2.c -o basic-tutorial-2 `pkg-config --cflags --libs gstreamer-1.0`

解析

管道是 GStreamer 的核心,基本结构就是从 source 经过 filter 到 sink

pipeline

这段小程序就构建了一个极简的管道, 从测试视频源中取出视频流到视频接收器中播放:

simple pipeline

基本流程如下:

  1. 创建管道
  2. 创建元件
  3. 设置元件
  4. 添加元件到管道中
  5. 连接这些元件
  6. 设置管道状态
  7. 解开元件之间的连接
  8. 从管道中删除元件
  9. 释放元件以及管道

答案


本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。