在这一部分,我们将讨论 GStreamer 中的一些基本概念以及一些常用的对 象,像元件、衬垫和缓存等。我们给这些对象以一种形象化的描述,相信这样会对我们在后边学习到如何构建一条管道时大有帮助。首先你会对 GStreamer的 API有个粗略的认识,用这些 API来构建一个基于元件的应用程序已经绰绰有余。然后你 会学习到如何构建一个简单的基于命令行的应用程序。
注意:在这部分我们会了解一些底层(low-level)的 API 以及 GStreamer 的 一些概念。如果你立马想构建一个应用程序,你可能会使用一些高层(higher-level)的 API,它们会在这手册的后部分被提到。
1、 初始化 GStreamer
当你准备写一个GStreamer应用程序时,你仅需要通过包含头文件gst/gst.h 来访问库函数。除此之外,不要忘记初始化 GStreamer 库。
1.1.简易初始化
在 GStreamer 库被使用前,主应用程序中应该先调用函数 gst_init,这个函数将会对 GStreamer 库做一些必要的初始化工作,同时 也能够对 GStreamer 的命令行参数进行解析。
一个典型的初始化 GStreamer 库的代码如下所示:
例 1-1. 初始化 GStreamer
#include <gst/gst.h>
#include <stdio.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;
}
你可以使用 GST_VERSION_MAJOR, GST_VERSION_MINOR 以及 GST_VERSION_MICRO 三个宏得到你的GStreamer版本信息,或者使用函数gst_version得到当前你所调用的程序库的版本信息。目前 GStreamer 使用了一种 保证主要版本和次要版本中 API-/以及 ABI 兼容的策略。当命令行参数不需要被 GStreamer 解析的时候,你可以在调用函数 gst_init 时使用 2 个 NULL 参数。
我们编译这个这个程序看看输出结果:
编译:
gcc test-init.c -o test-init $(pkg-config --cflags --libs gstreamer-1.0)
nvidia@nvidia-desktop:~/GStreamer/examples$ gcc test-init.c -o test-init $(pkg-config --cflags --libs gstreamer-1.0)
nvidia@nvidia-desktop:~/GStreamer/examples$ ls
test-init test-init.c
nvidia@nvidia-desktop:~/GStreamer/examples$ ./test-init
This program is linked against GStreamer 1.14.5
1.2. 使用GOption 接口来初始化
你同样可以使用 GOption 表来初始化你的参数。例子如下:
#include <gst/gst.h>
#include <stdio.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_error_free (err);
return 1;
}
printf ("Run me with --help to see the Application options appended.\n");
return 0;
}
如例子中的代码所示,你可以通过 GOption 表来定义你的命令行选项。将表与由gst_init_get_option_group 函数返回的选项组一同传给 GLib 初始化函数。通过使用 GOption 表来初始化GSreamer,你的程序还可以解析除标准 GStreamer 选项以外的命令行选项。
编译:
gcc test-GOption.c -o test-GOption $(pkg-config --cflags --libs gstreamer-1.0)
nvidia@nvidia-desktop:~/GStreamer/examples$ gcc test-GOption.c -o test-GOption $(pkg-config --cflags --libs gstreamer-1.0)
nvidia@nvidia-desktop:~/GStreamer/examples$
nvidia@nvidia-desktop:~/GStreamer/examples$
nvidia@nvidia-desktop:~/GStreamer/examples$ ls
test-GOption test-GOption.c test-init test-init.c
nvidia@nvidia-desktop:~/GStreamer/examples$ ./test-GOption
Run me with --help to see the Application options appended.
nvidia@nvidia-desktop:~/GStreamer/examples$ ./test-GOption --help
Usage:
test-GOption [OPTION?] - Your application
Help Options:
-h, --help Show help options
--help-all Show all help options
--help-gst Show GStreamer Options
Application Options:
-s, --silent do not output status information
-o, --output=FILE save xml representation of pipeline to FILE and exit