GStreamer 基础教程学习 1 - Hello World(GStreamer Basic tutorial 1 - Hello World)

https://gstreamer.freedesktop.org/documentation/tutorials/basic/hello-world.html?gi-language=c

重要的知识点: 

1. gst_init()

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

2. gst_parse_launch() and playbin

gst_parse_launch

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”.

多媒体流从称为”源“的元素(或者叫生产者)出发,一直到称为”SInk“的元素(或者叫消费者),中间会经历一系列中间处理的环节,这所有的相互连接的元素整体上叫做一个”管道“。

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().

在使用GStreamer时,通常需要手动的组装各个独立的元素去构成一个管道, 但是当管道本身很简单的话, 同时也不需要一些高级的特性的话, 就可以使用便捷的方式: 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

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.

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

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.

playbin是个特别的元素,它同时具有源和Sink的属性,作为一个整体的管道。 

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!

在这个例子中,仅仅传送了一个参数给playbin, 他是一个媒体的URI。

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.

3. gst_element_set_state

/* Start playing */
gst_element_set_state (pipeline, 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.

这行代码强调了另一个有趣的概念: 状态。每个GStreamer的元素都具有一个相关的状态,可以想象为DVD播放器的播放/停止按钮。这行代码设置了管道的状态为播放。

4. 等待反馈

/* 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.

这几行代码将会等待,直到已故障发生或者媒体流结束。gst_element_get_bus()获得管道的总线。

gst_bus_timed_pop_filtered(),这个函数会阻塞程序的执行,直到从总线接收到一个Error或者一个EOS(流的结束标志)。

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:

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

发布了11 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/gsymichael/article/details/104429111