gstreamer教程(4)——构建应用之gstreamer的初始化
学习内容:
通过构建应用学习,我们将讨论 GStreamer 的基本概念和最常用的对象,例如element、pad和buffer。我们将使用这些对象的可视化表示形式,以便我们可以可视化您稍后将学习构建的更复杂的pipeline。您将初步了解 GStreamer API,它应该足以构建基本应用程序。在本部分的后面部分,您还将学习如何构建基本的命令行应用程序。
这部分将介绍 GStreamer 底层的 API 和概念。一旦要构建应用程序,您可能希望使用更高级的 API。这些内容将在本手册的后面讨论。
测试环境:
系统:ubuntu 18.04
安装:gstreamer,命令如下
apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio gstreamer1.0-plugins-base-apps
初始化 GStreamer:
在编写 GStreamer 应用程序时,您只需包含 gst/gst.h 即可访问库函数。除此之外,您还需要初始化 GStreamer 库。
简单初始化:
在使用 GStreamer 之前,必须从主应用程序调用 gst_init。此调用将执行库的必要初始化,并解析特定于 GStreamer 的命令行选项。
一个最基本的初始化gstreamer的代码,如下所示:basic-example-1.c
#include <stdio.h>
#include <gst/gst.h>
int main (int argc, char *argv[])
{
const gchar *nano_str;
guint major, minor, micro, nano;
gst_init (&argc, &argv);
gst_version (&major, &minor, µ, &nano);
if (nano == 1)
nano_str = "(CVS)";
else if (nano == 2)
nano_str = "(Prerelease)";
else
nano_str = "";
printf ("This program is linked against GStreamer %d.%d.%d %s\n", major, minor, micro, nano_str);
return 0;
}
编译文件:
gcc basic-example-1.c -o basic-example-1 `pkg-config --cflags --libs gstreamer-1.0`
运行结果:
/opt/gstreamer# ./basic-example-1 This program is linked against GStreamer 1.14.5
使用 GST_VERSION_MAJOR、GST_VERSION_MINOR 和 GST_VERSION_MICRO 宏获取您正在构建的 GStreamer 版本,或使用函数 gst_version 获取应用程序所链接的版本。GStreamer 目前使用一种方案,其中具有相同主要版本和次要版本的版本与 API 和 ABI 兼容。
也可以使用两个 NULL 参数调用 gst_init 函数,像这样“gst_init(NULL,NULL);”。在这种情况下,GStreamer 不会解析任何命令行选项。
GOption接口:
您还可以使用 GOption 表来初始化您自己的参数,如下例所示:basic-example-2.c
#include <stdio.h>
#include <gst/gst.h>
int main (int argc, char *argv[])
{
gboolean silent = FALSE;
gchar *savefile = NULL;
GOptionContext *ctx;
GError *err = NULL;
GOptionEntry entries[] = {
{ "silent", 's', 0, G_OPTION_ARG_NONE, &silent,
"do not output status information", NULL },
{ "output", 'o', 0, G_OPTION_ARG_STRING, &savefile,
"save xml representation of pipeline to FILE and exit", "FILE" },
{ NULL }
};
ctx = g_option_context_new ("- Your application");
g_option_context_add_main_entries (ctx, entries, NULL);
g_option_context_add_group (ctx, gst_init_get_option_group ());
if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
g_print ("Failed to initialize: %s\n", err->message);
g_clear_error (&err);
g_option_context_free (ctx);
return 1;
}
g_option_context_free (ctx);
printf ("Run me with --help to see the Application options appended.\n");
return 0;
}
编译文件:
gcc basic-example-2.c -o basic-example-2 `pkg-config --cflags --libs gstreamer-1.0`
运行结果:
/opt/gstreamer# ./basic-example-2 Run me with --help to see the Application options appended.
如此片段所示,您可以使用 GOption 表来定义特定于应用程序的命令行选项,并将此表与从函数 gst_init_get_option_group()返回的选项组一起传递给 GLib 初始化函数。除了标准 GStreamer 选项之外,您的应用程序选项也将被解析。

浙公网安备 33010602011771号