gstreamer基础教程1-Hello world

索引:https://blog.csdn.net/knowledgebao/article/details/84621238

Goal

编写一个类似于hello world的视频播放程序,核心代码只有4句。初始化、设置参数、开始播放、监听消息,当然最后要释放资源。

Nothing better to get a first impression about a software library than to print “Hello World” on the screen!

But since we are dealing with multimedia frameworks, we are going to play a video instead.

Do not be scared by the amount of code below: there are only 4 lines which do real work. The rest is cleanup code, and, in C, this is always a bit verbose.

Without further ado, get ready for your first GStreamer application...

Hello world

下边代码运行正常的话,会弹出一个播放窗口,播放互联网上的视频和声音,当然你的网速。

Copy this code into a text file named basic-tutorial-1.c (or find it in your GStreamer installation).

basic-tutorial-1.c

#include <gst/gst.h>

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

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

  /* Build the pipeline */
  pipeline = gst_parse_launch ("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);

  /* Start playing */
  gst_element_set_state (pipeline, GST_STATE_PLAYING);

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

  /* Free resources */
  if (msg != NULL)
    gst_message_unref (msg);
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return 0;
}

If everything built fine, fire up the executable! You should see a window pop up, containing a video being played straight from the Internet, along with audio. Congratulations!

Walkthrough结构解析

Let's review these lines of code and see what they do:

初始化,所有gstream必须调用的接口。

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

This must always be your first GStreamer command. Among other things, gst_init():

  • Initializes all internal structures初始化内部结构

  • Checks what plug-ins are available检查可用的插件

  • Executes any command-line option intended for GStreamer命令行参数执行

If you always pass your command-line parameters argc and argv to gst_init() your application will automatically benefit from the GStreamer standard command-line options (more on this in Basic tutorial 10: GStreamer tools)

 /* Build the pipeline */
  pipeline = gst_parse_launch ("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm", NULL);

导入网络上的URI地址,这里涉及两部分gst_parse_launch和playbin。

This line is the heart of this tutorial, and exemplifies(例证) two key points: gst_parse_launch() and playbin.

gst_parse_launch

自动构建管道,适用于简单的,不包含高级功能的管道构建。有一个工具 gst-launch-1.0可以协助此函数的构建,具体详见Basic tutorial10

GStreamer is a framework designed to handle multimedia flows. Media travels from the “source” elements (the producers), down to the “sink” elements (the consumers), passing through a series of intermediate elements performing all kinds of tasks. The set of all the interconnected elements is called a “pipeline”.

In GStreamer you usually build the pipeline by manually assembling the individual elements, but, when the pipeline is easy enough, and you do not need any advanced features, you can take the shortcut:gst_parse_launch().

This function takes a textual representation of a pipeline and turns it into an actual pipeline, which is very handy. In fact, this function is so handy there is a tool built completely around it which you will get very acquainted with (see Basic tutorial 10: GStreamer tools to learn about gst-launch-1.0 and the gst-launch-1.0 syntax).

playbin

这是一个工具,会被gst_parse_launch调用,会根据用户传入参数,自动寻找合适的element进行组装,包括source和sink组件(element)。

So, what kind of pipeline are we asking gst_parse_launch()to build for us? Here enters the second key point: We are building a pipeline composed of a single element called playbin.

playbin is a special element which acts as a source and as a sink, and is a whole pipeline. Internally, it creates and connects all the necessary elements to play your media, so you do not have to worry about it.

It does not allow the control granularity that a manual pipeline does, but, still, it permits enough customization to suffice for a wide range of applications. Including this tutorial.

In this example, we are only passing one parameter to playbin, which is the URI of the media we want to play. Try changing it to something else! Whether it is an http:// or file:// URI, playbin will instantiate the appropriate GStreamer source transparently!

If you mistype the URI, or the file does not exist, or you are missing a plug-in, GStreamer provides several notification mechanisms, but the only thing we are doing in this example is exiting on error, so do not expect much feedback.

  /* Start playing */
  gst_element_set_state (pipeline, GST_STATE_PLAYING);

设置管道(pipeline)的状态,pipeline有4中状态,分别是:  GST_STATE_NULL ,GST_STATE_READY ,GST_STATE_PAUSED , GST_STATE_PLAYING

This line highlights another interesting concept: the state. Every GStreamer element has an associated state, which you can more or less think of as the Play/Pause button in your regular DVD player. For now, suffice to say that playback will not start unless you set the pipeline to the PLAYING state.

In this line, gst_element_set_state() is setting pipeline (our only element, remember) to the PLAYING state, thus initiating playback.

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

监听消息总线,直到文件播放完成或者出现致命错误。

These lines will wait until an error occurs or the end of the stream is found. gst_element_get_bus() retrieves the pipeline's bus, and gst_bus_timed_pop_filtered() will block until you receive either an ERROR or an EOS (End-Of-Stream) through that bus. Do not worry much about this line, the GStreamer bus is explained in Basic tutorial 2: GStreamer concepts.

And that's it! From this point onwards, GStreamer takes care of everything. Execution will end when the media reaches its end (EOS) or an error is encountered (try closing the video window, or unplugging the network cable). The application can always be stopped by pressing control-C in the console.

Cleanup

清空变量。

Before terminating the application, though, there is a couple of things we need to do to tidy up correctly after ourselves.

/* Free resources */
if (msg != NULL)
  gst_message_unref (msg);

gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);

Always read the documentation of the functions you use, to know if you should free the objects they return after using them.

In this case, gst_bus_timed_pop_filtered() returned a message which needs to be freed with gst_message_unref()(more about messages in Basic tutorial 2: GStreamer concepts).

gst_element_get_bus() added a reference to the bus that must be freed with gst_object_unref(). Setting the pipeline to the NULL state will make sure it frees any resources it has allocated (More about states in Basic tutorial 3: Dynamic pipelines). Finally, unreferencing the pipeline will destroy it, and all its contents.


Conclusion结论

And so ends your first tutorial with GStreamer. We hope its brevity serves as an example of how powerful this framework is!

Let's recap a bit. Today we have learned:

  • How to initialize GStreamer using gst_init().

  • How to quickly build a pipeline from a textual description using gst_parse_launch().

  • How to create an automatic playback pipeline using playbin.

  • How to signal GStreamer to start playback using gst_element_set_state().

  • How to sit back and relax, while GStreamer takes care of everything,  using  gst_element_get_bus()  and   gst_bus_timed_pop_filtered().  

The next tutorial will keep introducing more basic GStreamer elements, and show you how to build a pipeline manually.

It has been a pleasure having you here, and see you soon!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
这段代码是在Docker容器中执行的一系列命令,用于安装一些软件包和依赖项。具体来说,它执行以下操作: 1. `apt-get clean`:清理apt-get缓存,以释放磁盘空间。 2. `apt-get update`:更新apt-get软件包列表。 3. `apt-get install -y`:安装以下软件包和依赖项: - `python3`:Python 3的主要二进制文件。 - `python3-pip`:Python 3的包管理工具pip。 - `libopencv-dev`:OpenCV开发库的头文件和静态库。 - `python3-opencv`:Python 3的OpenCV绑定。 - `build-essential`:构建软件包所需的基本工具和编译器。 - `yasm`:视频编解码器的汇编器。 - `cmake`:跨平台的构建工具。 - `libtool`:通用库支持脚本工具。 - `libc6`、`libc6-dev`:C标准库的运行时库和开发文件。 - `unzip`:解压缩工具。 - `wget`:网络下载工具。 - `libnuma1`、`libnuma-dev`:NUMA(非统一内存访问)系统的库和开发文件。 - `libgstreamer1.0-0`:GStreamer多媒体框架的核心库。 - `gstreamer1.0-plugins-base`、`gstreamer1.0-plugins-good`、`gstreamer1.0-plugins-bad`、`gstreamer1.0-plugins-ugly`、`gstreamer1.0-libav`:GStreamer插件和解码器。 - `gstreamer1.0-doc`、`gstreamer1.0-tools`、`gstreamer1.0-x`、`gstreamer1.0-alsa`、`gstreamer1.0-gl`、`gstreamer1.0-gtk3`、`gstreamer1.0-qt5`、`gstreamer1.0-pulseaudio`:GStreamer的文档、工具和相关库。 - `libglib2.0-dev`:GLib开发库的头文件。 - `libgstrtspserver-1.0-dev`:GStreamer RTSP服务器库的开发文件。 - `gstreamer1.0-rtsp`:GStreamer的RTSP插件。 这些操作旨在为容器配置一个适合开发的环境,使其能够支持Python编程、OpenCV图像处理和GStreamer多媒体处理等任务。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值