2021 gstreamer插件编写指南中英对照 (二) 基础知识

Foundations 基础

This chapter of the guide introduces the basic concepts of GStreamer. Understanding these concepts will help you grok the issues involved in extending GStreamer. Many of these concepts are explained in greater detail in the GStreamer Application Development Manual; the basic concepts presented here serve mainly to refresh your memory.
本章介绍GStreamer的基本概念。理解这些概念将有助于您理解扩展GStreamer所涉及的问题。在《GStreamer应用程序开发手册》中对这些概念进行了更详细的解释;这里介绍的基本概念主要是为了简单的回顾。

Elements and Plugins 元素和插件

Elements are at the core of GStreamer. In the context of plugin development, an element is an object derived from the GstElement class. Elements provide some sort of functionality when linked with other elements: For example, a source element provides data to a stream, and a filter element acts on the data in a stream. Without elements, GStreamer is just a bunch of conceptual pipe fittings with nothing to link. A large number of elements ship with GStreamer, but extra elements can also be written.
元素是GStreamer的核心。在插件开发的上下文中,元素是从GstElement类派生出来的对象。元素在与其他元素链接时提供某种功能:例如,源source 元素向数据流提供数据,而筛选器filter元素作用于流中的数据。如果没有元素,GStreamer只是一堆概念性的管道配件,没有什么可以把他们关联起来。GStreamer本身就附带了大量的元素,但是也可以编写其它新的元素。
Just writing a new element is not entirely enough, however: You will need to encapsulate your element in a plugin to enable GStreamer to use it. A plugin is essentially a loadable block of code, usually called a shared object file or a dynamically linked library. A single plugin may contain the implementation of several elements, or just a single one. For simplicity, this guide concentrates primarily on plugins containing one element.
然而,仅仅编写一个新元素是不够的:您需要将您的元素封装在一个插件中,以便GStreamer能够使用它。插件本质上是一个可加载的代码块,通常称为共享目标文件或动态链接库。一个插件可以包含一个或多个元素的实现。为简单起见,本指南主要关注包含一个元素的插件。
A filter is an important type of element that processes a stream of data. Producers and consumers of data are called source and sink elements, respectively. Bin elements contain other elements. One type of bin is responsible for synchronization of the elements that they contain so that data flows smoothly. Another type of bin, called autoplugger elements, automatically add other elements to the bin and links them together so that they act as a filter between two arbitrary stream types.
过滤器是处理数据流的一种重要元素类型。数据的生产者和消费者分别称为source 源元素和sink输出元素。Bin元素是把其他元素打包形成一个元素。一种类型的bin负责它们所包含的元素的同步,以便数据平稳流动。另一种类型的容器,称为自动插入元素,自动将其他元素添加到容器中,并将它们链接在一起,以便它们充当两种任意流类型之间的过滤器。
The plugin mechanism is used everywhere in GStreamer, even if only the standard packages are being used. A few very basic functions reside in the core library, and all others are implemented in plugins. A plugin registry is used to store the details of the plugins in a binary registry file. This way, a program using GStreamer does not have to load all plugins to determine which are needed. Plugins are only loaded when their provided elements are requested.
插件机制在GStreamer中非常常见,即使是标准包也一样。只有一些非常基本的函数位于核心库中,其他所有函数都在插件中实现。插件注册表用于在二进制注册表文件中存储插件的详细信息。这样,使用GStreamer的程序不必加载所有插件来确定需要哪些插件。插件只有在其提供的元素被请求时才会被加载。
See the GStreamer Library Reference for the current implementation details of GstElement and GstPlugin.
有关GstElement和GstPlugin的当前实现细节,请参阅《GStreamer库参考》文档。

Pads 衬垫

Pads are used to negotiate links and data flow between elements in GStreamer. A pad can be viewed as a “place” or “port” on an element where links may be made with other elements, and through which data can flow to or from those elements. Pads have specific data handling capabilities: A pad can restrict the type of data that flows through it. Links are only allowed between two pads when the allowed data types of the two pads are compatible.
pad用于协商GStreamer中元素之间的链接和数据流。pad可以被看作是元素上的一个“位置”或“端口”,在这里可以与其他元素建立链接,数据可以通过它流入或流出。pad具有特定的数据处理功能:pad可以限制流经它的数据类型。只有当两个Pad允许的数据类型兼容时,才允许两个Pad之间有链接。
An analogy may be helpful here. A pad is similar to a plug or jack on a physical device. Consider, for example, a home theater system consisting of an amplifier, a DVD player, and a (silent) video projector. Linking the DVD player to the amplifier is allowed because both devices have audio jacks, and linking the projector to the DVD player is allowed because both devices have compatible video jacks. Links between the projector and the amplifier may not be made because the projector and amplifier have different types of jacks. Pads in GStreamer serve the same purpose as the jacks in the home theater system.
这里可以打个比方。pad类似于物理设备上的插头或插孔。例如,考虑一个由功放、DVD播放器和无声的视频投影机,组成的家庭影院系统。允许将DVD播放器连接到放大器,因为两个设备都有音频插孔;允许将投影机连接到DVD播放器,因为两个设备都有兼容的视频插孔。投影机和放大器之间不能连接,因为投影机和放大器有不同类型的插孔。GStreamer中的Pad相当于家庭影院系统中的插孔。
For the most part, all data in GStreamer flows one way through a link between elements. Data flows out of one element through one or more source pads, and elements accept incoming data through one or more sink pads. Source and sink elements have only source and sink pads, respectively.
在大多数情况下,GStreamer中的所有数据都通过元素之间的链接单向流动。数据通过一个或多个源衬垫(source pads)流出,通过一个或多个接收衬垫( sink pads)流入。源元素Source 和接受元素sink则分别只有源衬垫和接受衬垫。
See the GStreamer Library Reference for the current implementation details of a GstPad.
有关GstPad的当前实现细节,请参阅《GStreamer库参考》文档。

GstMiniObject, Buffers and Events 缓冲和事件

All streams of data in GStreamer are chopped up into chunks that are passed from a source pad on one element to a sink pad on another element. GstMiniObject is the structure used to hold these chunks of data.
GstMiniObject contains the following important types:
GStreamer中的所有数据流都被切成块,这些块从一个元素上的源衬垫传递到另一个元素上的接收聚垫。GstMiniObject是用来保存这些数据块的结构,它包含了以下重要的类型:

  • An exact type indicating what type of data (event, buffer, …) this GstMiniObject is.
    一种精确的类型,表明该GstMiniObject是什么类型的数据,比如事件,缓冲区等。

  • A reference count indicating the number of elements currently holding a reference to the miniobject. When the reference count falls to zero, the miniobject will be disposed, and its memory will be freed in some sense (see below for more details).
    一个引用计数,表示当前持有对该迷你对象的引用的元素数量。当引用计数降为0时,miniobject将被销毁,其内存将在某种意义上被释放(参见下面的详细信息)。
    For data transport, there are two types of GstMiniObject defined: events (control) and buffers (content).
    对于数据传输,定义了两种类型的GstMiniObject:事件(控制)和缓冲区(内容)。

Buffers may contain any sort of data that the two linked pads know how to handle. Normally, a buffer contains a chunk of some sort of audio or video data that flows from one element to another.
缓冲区可以包含任何类型的数据,这两个连接的pad知道如何处理。通常,缓冲区包含从一个元素流到另一个元素的某种音频或视频数据块。
Buffers also contain metadata describing the buffer’s contents. Some of the important types of metadata are:
缓冲区还包含描述缓冲区内容的元数据。重要的元数据类型有:

  • Pointers to one or more GstMemory objects. GstMemory objects are refcounted objects that encapsulate a region of memory.
    指向一个或多个GstMemory对象的指针。GstMemory对象是封装内存区域的重计数对象。
  • A timestamp indicating the preferred display timestamp of the content in the buffer.
    一个时间戳,指示缓冲区中内容的首选显示时间戳。
    Events contain information on the state of the stream flowing between the two linked pads. Events will only be sent if the element explicitly supports them, else the core will (try to) handle the events automatically. Events are used to indicate, for example, a media type, the end of a media stream or that the cache should be flushed.
    Events may contain several of the following items:
    事件包含有关在两个链接的衬垫之间的流状态的信息。事件只有在元素显式支持时才会发送,否则核心将(尝试)自动处理事件。事件用于指示(例如)媒体类型、媒体流的结束或应该刷新缓存。它包含以下条目:
  • A subtype indicating the type of the contained event.
    事件的子类型
  • The other contents of the event depend on the specific event type.各种特定事件的其它内容。

Events will be discussed extensively in Events: Seeking, Navigation and More. Until then, the only event that will be used is the EOS event, which is used to indicate the end-of-stream (usually end-of-file).
事件将被广泛讨论在事件章节,包括: 寻找,导航和其它操作。在此之前,将使用的唯一事件是EOS事件,它表示流结束符(通常是文件结束符)。
See the GStreamer Library Reference for the current implementation details of a GstMiniObject, GstBuffer and GstEvent.
有关GstMiniObject、GstBuffer和GstEvent的细节,请参阅《GStreamer库参考》。

Buffer Allocation 缓存的分配

Buffers are able to store chunks of memory of several different types. The most generic type of buffer contains memory allocated by malloc(). Such buffers, although convenient, are not always very fast, since data often needs to be specifically copied into the buffer.
缓冲区能够存储几种不同类型的内存块。最通用的缓冲区类型包含由malloc()分配的内存。这样的缓冲区虽然方便,但一般不够快,因为数据需要经常被拷贝(因为大量数据拷贝会花费很多时间,占用CPU资源——译者注)。

Many specialized elements create buffers that point to special memory. For example, the filesrc element usually maps a file into the address space of the application (using mmap()), and creates buffers that point into that address range. These buffers created by filesrc act exactly like generic buffers, except that they are read-only. The buffer freeing code automatically determines the correct method of freeing the underlying memory. Downstream elements that receive these kinds of buffers do not need to do anything special to handle or unreference it.
许多元素专门创建指向特殊内存的缓冲区。例如,filesrc元素通常将文件映射到应用程序的地址空间(使用mmap()),并创建指向该地址范围的缓冲区。由filesrc创建的这些缓冲区的行为与通用缓冲区完全相同,只是它们是只读的。缓冲区释放代码自动确定释放底层内存的正确方式。接收这类缓冲区的下游元素,不需要做任何特殊操作来处理它们,也不需要对其取消引用。

Another way an element might get specialized buffers is to request them from a downstream peer through a GstBufferPool or GstAllocator. Elements can ask a GstBufferPool or GstAllocator from the downstream peer element. If downstream is able to provide these objects, upstream can use them to allocate buffers. See more in Memory allocation.
元素获得专用缓冲区的另一种方式,通过GstBufferPool或GstAllocator,从下游的流来获取。元素可以从下游对等元素请求GstBufferPool或GstAllocator。如果下游能够提供这些对象,上游就可以使用它们来分配缓冲区。更多信息请参见内存分配章节。

Many sink elements have accelerated methods for copying data to hardware, or have direct access to hardware. It is common for these elements to be able to create a GstBufferPool or GstAllocator for their upstream peers. One such example is ximagesink. It creates buffers that contain XImages. Thus, when an upstream peer copies data into the buffer, it is copying directly into the XImage, enabling ximagesink to draw the image directly to the screen instead of having to copy data into an XImage first.
许多接收器元素都有将数据复制到硬件的加速方法,或者直接访问硬件。这些元素通常能够为它们的上游点创建GstBufferPool或GstAllocator。例如ximagesink就是这样,它创建包含ximage的缓冲区。因此,当上游端将数据复制到缓冲区时,实际上上游端就已经复制到了XImage。所以ximagesink能够将图像直接绘制到屏幕上,不需要在显示之前把数据再次复制到XImage。
Filter elements often have the opportunity to either work on a buffer in-place, or work while copying from a source buffer to a destination buffer. It is optimal to implement both algorithms, since the GStreamer framework can choose the fastest algorithm as appropriate. Naturally, this only makes sense for strict filters – elements that have exactly the same format on source and sink pads.
对于过滤器元素,要么在缓冲区上直接运行,要么在从源缓冲区复制到目标缓冲区再运行。最好把这两种算法全都进行实现,因为GStreamer框架可以根据需要选择最快的算法。当然,这只适用于严格的过滤器——在源和接收衬垫上具有完全相同格式的元素。

Media types and Properties 媒体类型和属性

GStreamer uses a type system to ensure that the data passed between elements is in a recognized format. The type system is also important for ensuring that the parameters required to fully specify a format match up correctly when linking pads between elements. Each link that is made between elements has a specified type and optionally a set of properties. See more about caps negotiation in Caps negotiation.
GStreamer使用类型系统,来确保元素之间传递的数据是公认的格式。当链接元素之间的衬垫时,类型系统也非常重要,它来完全保证格式正确匹配。元素之间的每个链接都有一个指定的类型和一组可选的属性。有关衬垫协商的更多信息请参见《Caps协商》章节。

The Basic Types 基础类型

GStreamer already supports many basic media types. Following is a table of a few of the basic types used for buffers in GStreamer. The table contains the name (“media type”) and a description of the type, the properties associated with the type, and the meaning of each property. A full list of supported types is included in List of Defined Types.
GStreamer已经支持许多基本的媒体类型。下面是GStreamer中用于缓冲区的基本类型表。该表包含名称(“媒体类型”)和类型的描述,类型相关的属性,以及每个属性的含义。支持类型的完整列表包含在《已定义类型列表》中。

媒体类型描述属性属性类型属性值属性描述
audio/*所有音频类型Rate 比率整形数大于0数据的采样率,单位是每秒采样(单通道)。
channels 通道数整形数大于0音频数据通道的数目
audio/x-raw非结构化和未压缩的原始整数音频数据。format 格式string 字符串S8 U8 S16LE S16BE U16LE U16BE S24_32LE S24_32BE U24_32LE U24_32BE S32LE S32BE U32LE U32BE S24LE S24BE U24LE U24BE S20LE S20BE U20LE U20BE S18LE S18BE U18LE U18BE F32LE F32BE F64LE F64BE数据格式
audio/mpeg使用mpeg压缩的音频数据mpegversion mpeg版本整数1, 2 or 4用于编码数据的mpeg版本。取值为1时,表示MPEG-1、-2、-2.5层1、2、3。 2和4是MPEG-AAC音频编码方案。
framed 按帧存储boolean0 or 1true值表示每个缓冲区恰好包含一帧。false值表示帧和缓冲区不一定匹配。
layer整数1, 2, or 3数据的压缩方案层(仅当mpegversion=1时)。
bitrate整数大于0比特率,单位为比特/秒。对于VBR(可变比特率)MPEG数据,这是平均比特率。
audio/x-vorbisVorbis音频数据目前还没有为这种类型定义特定的属性。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值