gstreamer基础教程14-Handy elements

官网:https://gstreamer.freedesktop.org/

Demo基础教程:https://gstreamer.freedesktop.org/documentation/tutorials/basic/concepts.html

Demo下载地址:git://anongit.freedesktop.org/gstreamer/gst-docs

Goal

这篇教程主要是介绍gstreamer提供的各种element工具,从复杂的playbin到简单的帮助调试element,非常有必要学会这些工具。为了方便我们使用gst-launch-1.0来演示这些工具。如果你想看pad谈判过程,可以使用-v参数查看。

This tutorial gives a list of handy GStreamer elements that are worth knowing. They range from powerful all-in-one elements that allow you to build complex pipelines easily (like playbin), to little helper elements which are extremely非常 useful when debugging.

For simplicity简单起见, the following examples are given using the gst-launch-1.0 tool (Learn about it in Basic tutorial 10: GStreamer tools). Use the -v command line parameter if you want to see the Pad Caps that are being negotiated谈判.

Bins

这些是Bin元素,可以将其视为单个元素,它们负责实例化所有必要的内部管道以完成其任务。

These are Bin elements which you treat as a single element and they take care of instantiating all the necessary internal pipeline to accomplish完成 their task.

playbin

整个教程中广泛使用了这个元素。它管理媒体播放的各个方面,通过解复用和解码,从源到显示。它非常灵活,有很多选项,所以使用一整套教程来讲解它(播放教程)。有关更多详细信息,请参阅播放教程

This element has been extensively大量的 used throughout the tutorials. It manages all aspects of各个方面 media playback, from source to display, passing through demuxing and decoding. It is so flexible and has so many options that a whole set of tutorials are devoted to献身于,依赖于 it. See the Playback tutorials for more details.

uridecodebin

此元素将URI中的数据解码为原始媒体。它选择一个src element。这个src element可以处理给定原始数据类型并且将其连接到decodebin元素。它就像一个多路分离器,所以它提供了与媒体中的流一样多的源垫。

扫描二维码关注公众号,回复: 3734292 查看本文章

Pad Templates:
  SRC template: 'src_%u'
    Availability: Sometimes
    Capabilities:
      ANY

This element decodes data from a URI into raw media. It selects a source element that can handle the given URI scheme方案 and connects it to a decodebin element. It acts like a demuxer多路复用, so it offers as many source pads as streams are found in the media.

gst-launch-1.0 uridecodebin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm ! videoconvert ! autovideosink
gst-launch-1.0 uridecodebin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm ! audioconvert ! autoaudiosink

decodebin

该元素通过自动插入可用的解码器和解复用器自动构建解码流水线,直到获得原始媒体。因为它可以创建合适的源元素,所以它在内部被uridecodebin使用。它取代了旧decodebin元素。它就像一个多路分离器,所以它提供了与媒体中的流一样多的源垫。

Pad Templates:
  SINK template: 'sink'
    Availability: Always
    Capabilities:
      ANY

  SRC template: 'src_%u'
    Availability: Sometimes
    Capabilities:
      ANY

This element automatically constructs a decoding pipeline using available decoders and demuxers via auto-plugging until raw media is obtained. It is used internally by uridecodebin which is often more convenient方便 to use, as it creates a suitable source element as well. It replaces the old decodebin element. It acts like a demuxer, so it offers as many source pads as streams are found in the media.

gst-launch-1.0 souphttpsrc location=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm ! decodebin ! autovideosink

File input/output

filesrc

此元素读取本地文件并生成带ANY Caps的媒体。如果要为媒体获取正确的Caps,请使用typefind元素或通过给filesrc属性typefind设置为TRUE

Pad Templates:
  SRC template: 'src'
    Availability: Always
    Capabilities:
      ANY

This element reads a local file and produces media with ANY Caps. If you want to obtain the correct Caps for the media, explore the stream by using a typefind element or by setting the typefind property of filesrc to TRUE.

gst-launch-1.0 filesrc location=f:\\media\\sintel\\sintel_trailer-480p.webm ! decodebin ! autovideosink

filesink

此元素将收到的所有媒体写入文件。使用location属性指定文件名。

This element writes to a file all the media it receives. Use the location property to specify the file name.

gst-launch-1.0 audiotestsrc ! vorbisenc ! oggmux ! filesink location=test.ogg

Network

souphttpsrc

此元素使用libsoup库接收网络上HTTP流,类似于一个客户端。通过属性location设置待检索的URL 。

This element receives data as a client over the network via HTTP using the libsoup library. Set the URL to retrieve through the location property.

gst-launch-1.0 souphttpsrc location=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm ! decodebin ! autovideosink

Test media generation

这些源肯定没有问题,所以可以利用这些源替代目标源来测试其它部分。这些元素对于检查管道的其他部分是否正常工作非常有用。

These elements are very useful to check if other parts of the pipeline are working, by replacing the source by one of these test sources which are “guaranteed保证的” to work.

videotestsrc

此元素生成视频模式(通过设置pattern属性,可以得到很多选项)。用它来测试视频管道。

This element produces a video pattern (selectable among many different options with the pattern property). Use it to test video pipelines.

gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink

audiotestsrc

此元素产生音频波(通过设置wave属性,可以得到很多选项)。用它来测试音频管道。

This element produces an audio wave (selectable among many different options with the wave property). Use it to test video pipelines.

gst-launch-1.0 audiotestsrc ! audioconvert ! autoaudiosink

Video adapters

videoconvert

该元素从一个颜色空间(例如RGB)转换为另一个颜色空间(例如YUV)。它还可以在不同的YUV格式(例如I420,NV12,YUY2 ......)或RGB格式排列(例如RGBA,ARGB,BGRA ......)之间进行转换。在解决谈判问题时,这通常是您的首选。当不需要时,因为它的上游和下游元素已经可以相互理解,所以它以直通模式运行,对性能的影响最小。根据经验,当你使用的element的caps不确定时(像autovideosink),或者依赖外部因素(比如解码用户文件),应该使用videoconvert

This element converts from one color space (e.g. RGB) to another one (e.g. YUV). It can also convert between different YUV formats (e.g. I420, NV12, YUY2 …) or RGB format arrangements (e.g. RGBA, ARGB, BGRA…).

This is normally your first choice when solving negotiation problems. When not needed, because its upstream and downstream elements can already understand each other, it acts in pass-through mode having minimal impact on the performance.

As a rule of thumb根据经验, always use videoconvert whenever you use elements whose Caps are unknown at design time, like autovideosink, or that can vary depending on external factors外部因素, like decoding a user-provided file.

gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink

videorate

此元素接收带时间戳的视频帧,生成与设置帧速率匹配的流。通过丢弃和复制帧来执行校正,没有使用花哨的算法来内插帧。这对于允许需要不同帧速率的元素进行链接非常有用。与其他适配器一样,如果不需要(因为两个Pads都可以同意帧速率),它将以直通模式运行,不会影响性能。因此,为了以防万一,在设计时不知道实际帧速率时应该使用它。

This element takes an incoming stream of time-stamped video frames and produces a stream that matches the source pad's frame rate. The correction is performed by dropping丢弃 and duplicating插入 frames, no fancy花哨的 algorithm is used to interpolate篡改 frames.

This is useful to allow elements requiring different frame rates to link. As with the other adapters, if it is not needed (because there is a frame rate on which both Pads can agree), it acts in pass-through mode and does not impact performance.

It is therefore a good idea to always use it whenever the actual frame rate is unknown at design time, just in case.

gst-launch-1.0 videotestsrc ! video/x-raw,framerate=30/1 ! videorate ! video/x-raw,framerate=1/1 ! videoconvert ! autovideosink

videoscale

此元素调整视频帧的大小。默认情况下,元素尝试在源和接收Pads上协商相同的大小,以便不需要缩放。因此,如果不需要缩放,则可以安全地将此元素插入管道中以获得更强大的行为,而无需任何成本。该元素支持各种颜色空间,包括各种YUV和RGB格式,因此通常能够在管道中的任何位置操作。如果要将视频输出到其大小由用户控制的窗口,则最好使用videoscale元素,因为并非所有视频接收器都能够执行缩放操作。

This element resizes video frames. By default the element tries to negotiate谈判 to the same size on the source and sink Pads so that no scaling is needed. It is therefore safe to insert this element in a pipeline to get more robust简装的 behavior without any cost if no scaling is needed.

This element supports a wide range of color spaces including various YUV and RGB formats and is therefore generally able to operate anywhere in a pipeline.

If the video is to be output to a window whose size is controlled by the user, it is a good idea to use a videoscale element, since not all video sinks are capable of performing scaling operations.

gst-launch-1.0 uridecodebin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm ! videoscale ! video/x-raw,width=178,height=100 ! videoconvert ! autovideosink

Audio adapters

audioconvert

此元素对原始音频数据在各种可能格式之间转换。它支持整数到浮点转换,宽度/深度转换,符号和字节顺序转换以及通道转换。

就像videoconvert视频一样,你使用它来解决音频的协商问题,并且通常可以放心地使用它,因为如果不需要它,这个元素什么都不做。

This element converts raw audio buffers between various possible formats. It supports integer to float conversion, width/depth conversion, signedness and endianness conversion and channel transformations.

Like videoconvert does for video, you use this to solve negotiation problems with audio, and it is generally safe to use it liberally大方的, since this element does nothing if it is not needed.

gst-launch-1.0 audiotestsrc ! audioconvert ! autoaudiosink

audioresample

此元素使用可配置的窗口函数将原始音频数据重新采样到不同的采样率,以提高质量。再次重申,用它来解决有关采样率的谈判问题,无需害怕,不使用时对效率没有影响。

This element resamples raw audio buffers to different sampling rates using a configurable windowing function to enhance提高 quality

Again, use it to solve negotiation problems regarding sampling rates and do not fear to use it generously丰富的.

gst-launch-1.0 uridecodebin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm ! audioresample ! audio/x-raw-float,rate=4000 ! audioconvert ! autoaudiosink

audiorate

此元素接收带时间戳的原始音频数据,根据参数插入或删除样本,从而生成需要的流。它不允许像videorate这样更改采样率,只是填充间隙并移除重叠的样本,因此输出流是连续的并且“干净”。

在时间戳丢失的情况下(例如,当存储到某些文件格式中)并且接收器将要求存在所有样本时,它是有用的。举例说明这很麻烦,因此没有给出任何例子。大多数时候,audiorate不常用

This element takes an incoming stream of time-stamped raw audio frames and produces a perfect完美的 stream by inserting or dropping samples as needed. It does not allow the sample rate to be changed as videorate does, it just fills gaps and removes overlapped samples so the output stream is continuous and “clean”.

It is useful in situations情况 where the timestamps are going to be lost (when storing into certain file formats, for example) and the receiver will require all samples to be present. It is cumbersome笨拙的 to exemplify示例 this, so no example is given.

WarningMost of the time, audiorate is not what you want.

Multithreading

queue

队列已在基础教程7:多线程和填充可用性中进行了解释。基本上,队列执行两个任务:

  • queue中数据不够时,需要的等待queue中有足够的数据才可以对其进行操作。当queue中数据过多时,所有给queue插入数据的线程会被阻塞,直到有足够空间可用。
  • 为了src和sink之间的减耦合,在src和sink之间插入一个queue线程。

此外,queue会在满或者为空时触发信号(根据配置的空或满的阈值),根据型号可以在缓冲区满时丢弃缓冲区而不是阻塞缓冲区。

根据经验,如果您不关心网络缓冲,queue元素比queue2更简单好用。有关示例,请参阅基本教程7:多线程和填充可用性 。

Queues have been explained in Basic tutorial 7: Multithreading and Pad Availability. Basically, a queue performs two tasks:

  • Data is queued until a selected limit is reached. Any attempt to push more buffers into the queue blocks the pushing thread until more space becomes available.
  • The queue creates a new thread on the source Pad to decouple解耦合 the processing on sink and source Pads.

Additionally, queue triggers signals when it is about to become empty or full (according to some configurable thresholds阀值), and can be instructed指示 to drop buffers instead of blocking when it is full.

As a rule of thumb, prefer the simpler queue element over queue2 whenever network buffering is not a concern to you. See Basic tutorial 7: Multithreading and Pad Availability for an example.

queue2

这个元素不是queue的演变。它和queue具有相同的设计目标,但使用不同的实现方法,所以产生了不同的功能。不幸的是,通常不容易判断哪个队列是最佳选择。

queue2可以实现上面queue实现的两个任务,并且,能够将接收的数据(或其一部分)存储在磁盘文件,以便以后检索。它还可以使用更通用和方便的缓冲消息替代基本教程12:Streaming中描述的信号 。

根据经验,queue2一般用于关心网络缓存的应用中。有关示例,请参阅基本教程12:Streamingqueue2隐藏在playbin中)。

This element is not an evolution演变 of queue. It has the same design goals but follows a different implementation approach实现方法, which results in different features. Unfortunately不幸的是, it is often not easy to tell which queue is the best choice.

queue2 performs the two tasks listed above for queue, and, additionally, is able to store the received data (or part of it) on a disk file, for later retrieval检索. It also replaces代替 the signals with the more general and convenient buffering messages described in Basic tutorial 12: Streaming.

As a rule of thumb, prefer queue2 over queue whenever network buffering is a concern to you. See Basic tutorial 12: Streaming for an example (queue2 is hidden inside playbin).

multiqueue

此元素同时为多个流同时提供队列,当一些queue的数据没有到来时,允许其他队列queue的长度变大,或者允许某些队列在没有目标sink的情况下丢掉数据(而不是像queue一样返回错误)。此外,它同步不同的流,确保它们都不会远远超过其他流。

这是一个高级元素。它位于decodebin内部,很少需要在正常播放应用程序中使用。

This element provides queues for multiple streams simultaneously同时, and eases their management, by allowing some queues to grow if no data is being received on other streams, or by allowing some queues to drop data if they are not connected to anything (instead of returning an error, as a simpler queue would do). Additionally, it synchronizes the different streams, ensuring that none of them goes too far ahead of the others.

This is an advanced element. It is found inside decodebin, but you will rarely need to instantiate it yourself in a normal playback application.

tee

基础教程7:多线程和Pad可用性已经展示了如何使用一个tee元素,它将数据分成多个pad。拆分数据流非常有用,例如,捕获的视频需要在屏幕上显示同时需要编码后写入文件。另一个例子是播放音乐同时连接可视化模块。使用tee的同时,在每一个分支中尽量使用queue为每个分支提供单独的线程。否则,一个分支中数据的阻塞会影响其他分支。

Basic tutorial 7: Multithreading and Pad Availability already showed how to use a tee element, which splits data to multiple pads. Splitting the data flow is useful, for example, when capturing a video where the video is shown on the screen and also encoded and written to a file. Another example is playing music and hooking up a visualization module.

One needs to use separate queue elements in each branch to provide separate threads for each branch. Otherwise a blocked dataflow in one branch would stall阻塞 the other branches.

gst-launch-1.0 audiotestsrc ! tee name=t ! queue ! audioconvert ! autoaudiosink t. ! queue ! wavescope ! videoconvert ! autovideosink

Capabilities

capsfilter

基础教程10:GStreamer工具已经解释了如何使用Caps过滤器gst-launch-1.0。以编程方式构建管道时,使用该capsfilter元素实现Caps过滤器。此元素不会修改数据,但会强制限制数据格式。

Basic tutorial 10: GStreamer tools already explained how to use Caps filters with gst-launch-1.0. When building a pipeline programmatically, Caps filters are implemented with the capsfilter element. This element does not modify data as such, but enforces limitations on the data format.

gst-launch-1.0 videotestsrc ! video/x-raw, format=GRAY8 ! videoconvert ! autovideosink

typefind

此元素确定流包含的媒体类型。它按照排名的顺序调用typefind函数。一旦检测到类型,它将其源Pad Caps设置为找到的媒体类型并发出have-type信号。它在内部实例化decodebin,您也可以使用它来查找媒体类型,尽管您通常可以使用 GstDiscoverer来获取更多信息(如基本教程9:媒体信息收集中所示)。

This element determines the type of media a stream contains. It applies typefind functions in the order of their rank. Once the type has been detected it sets its source Pad Caps to the found media type and emits发射 the have-type signal.

It is instantiated internally by decodebin, and you can use it too to find the media type, although you can normally use the GstDiscoverer which provides more information (as seen in Basic tutorial 9: Media information gathering).

Debugging

fakesink

这个宿元素只是吞下任何数据。在调试时,它非常有用,可以替换正常的接收器并将其排除在正常队列之外。gst-launch-1.0配合-v开关组合时,它可能显得非常冗长,因此使用silent属性可以消除任何不需要的噪音。

This sink element simply swallows吞掉 any data fed to it. It is useful when debugging, to replace your normal sinks and rule them out of the equation排除在外. It can be very verbose冗余 when combined with the -v switch of gst-launch-1.0, so use the silent property to remove any unwanted noise.

gst-launch-1.0 audiotestsrc num-buffers=1000 ! fakesink sync=false

identity

这是一个检测元素,对输入的数据不进行修改。它有几个有用的诊断功能,例如偏移和时间戳检查,或数据丢失率等。阅读其文档,了解这个看似无害的元素可以做的所有事情。

This is a dummy element that passes incoming data through unmodified未修改. It has several useful diagnostic诊断 functions, such as offset偏移 and timestamp checking, or buffer dropping. Read its documentation to learn all the things this seemingly harmless element can do.

gst-launch-1.0 audiotestsrc ! identity drop-probability=0.1 ! audioconvert ! autoaudiosink

Conclusion

This tutorial has listed a few elements which are worth knowing, due to their usefulness in the day-to-day work with GStreamer. Some are valuable for production pipelines, whereas others are only needed for debugging purposes.

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

猜你喜欢

转载自blog.csdn.net/knowledgebao/article/details/82864974
今日推荐