1 简介
两个元素直接连接是通过Pad,数据流从一个元件的src到另一元件的sink。
2 可用性Availability
2.1 永久性 Always
一直存在。
sinkpad = gst_element_get_static_pad (sinkelem, "sink");
/*用完之后要释放pad*/
gst_object_unref (sinkpad);
2.2 请求型 request
2.2.1 直接通过name获得pad
需要用户请求去创建pad。
pad = gst_element_get_request_pad (tee, "src%d");
name = gst_pad_get_name (pad);
g_print ("A new pad %s was created\n", name);
g_free (name);
/*用完之后要释放pad*/
gst_object_unref (pad);
2.2.2 通过Template获得pad
padtemplate =
gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (tee), "src_%u");
tee_src_pad = gst_element_request_pad (tee, padtemplate, NULL, NULL);
/* 最后要释放pad */
gst_object_unref (tee_src_pad);
2.3 随机型 sometimes
当元素有数据时候,会产生信号,一般通过连接信号的回调函数接受出来pad。
static void
cb_new_pad (GstElement *element,
GstPad *pad,
gpointer data)
{
gchar *name;
name = gst_pad_get_name (pad);
g_print ("A new pad %s was created\n", name);
g_free (name);
/* here, you would setup a new pad link for the newly created pad */
[..]
}
g_signal_connect (oggdemux, "pad-added", G_CALLBACK(cb_new_pad), NULL);
3 精灵衬垫(Ghost pads)
精灵衬垫来自于箱柜中某些元件,它同样可以在该箱柜中被直接访问。精灵衬垫与 UNIX 文件系统中的符号链接很类似。使用箱柜,你可以在你的代码中将箱柜当作一个普通元件来使用。
上图显示了一个精灵衬垫。最左边元件的接收衬垫同样也是整个箱柜的精灵衬垫。由于精灵衬垫看起来与其它衬垫没什么区别,而且与其它衬垫有着类似的功能。所以它 们可以加到任何一种元件上,而不仅仅是 GstBin。通过函数 gst_ghost_pad_new ()可以创建一个 ghostpad :
#include <gst/gst.h>
int
main (int argc,
char *argv[])
{
GstElement *bin, *sink;
GstPad *pad;
/* init */
gst_init (&argc, &argv);
/* create element, add to bin */
sink = gst_element_factory_make ("fakesink", "sink");
bin = gst_bin_new ("mybin");
gst_bin_add (GST_BIN (bin), sink);
/* add ghostpad */
pad = gst_element_get_static_pad (sink, "sink");
gst_element_add_pad (bin, gst_ghost_pad_new ("sink", pad));
gst_object_unref (GST_OBJECT (pad));
[..]
}
4 能力Capabilities
以来并非继承关系,只能说明是包含关系。
GstElement /*插件*/
╰───GstPad /*插件接口*/
╰───GstCap /*接口的性质*/
╰───GstStructure /*具体性质信息*/
元素会有sink pad和src pad,每个sink pad或src pad会有不同
Pad的功能通过GstCaps对象来进行描述。一个GstCaps对象会包含一个或多个GstStructure。其中audioconvert元素sin pad中GstStructure的name是audio/x-raw,成员包含则是format、rate、channels、layout信息。
5 GstPad连接
/* 1.连接在同一个bin下的2个element */
gboolean gst_element_link (GstElement *src,
GstElement *dest);
/* 2.连接同一bin下的多个element */
gboolean gst_element_link_many (GstElement *element_1,
GstElement *element_2, ...);
/* 3.连接2个element的pad,padname可以为空 */
gboolean gst_element_link_pads (GstElement *src,
const gchar *srcpadname,
GstElement *dest,
const gchar *destpadname);
/* 4.连接2个Pad */
GstPadLinkReturn gst_pad_link(GstPad *srcpad, GstPad *sinkpad);
/* 5.指定Caps格式,连接element */
GstCaps * gst_caps_new_simple (const char *media_type,
const char *fieldname,
...);
4.1 指定Caps格式,连接element举例
static gboolean
link_elements_with_filter (GstElement *element1, GstElement *element2)
{
gboolean link_ok;
GstCaps *caps;
caps = gst_caps_new_simple ("video/x-raw",
"format", G_TYPE_STRING, "I420",
"width", G_TYPE_INT, 384,
"height", G_TYPE_INT, 288,
"framerate", GST_TYPE_FRACTION, 25, 1,
NULL);
link_ok = gst_element_link_filtered (element1, element2, caps);
gst_caps_unref (caps);
if (!link_ok) {
g_warning ("Failed to link element1 and element2!");
}
return link_ok;
}
参考1: Gstreamer-GstPad关于Link总结
参考2:官网GstPad
参考3:Gstreamer应用开发手册4:衬垫、缓冲区、事件