1 <chapter id="cha-pads">
4 As we have seen in the previous chapter (GstElement), the pads are the elements
5 connections with the outside world.
8 The specific type of media that the element can handle will be exposed by the pads.
9 The description of this media type is done with capabilities (<classname>GstCaps</classname>)
12 <sect1 id="sec-pads-get">
13 <title>Getting pads from an element</title>
15 Once you have created an element, you can get one of its pads with:
20 srcpad = gst_element_get_pad (element, "src");
24 This function will get the pad named "src" from the given element.
27 Alternatively, you can also request a GList of pads from the element. The following
28 code example will print the names of all the pads of an element.
33 pads = gst_element_get_pad_list (element);
35 GstPad *pad = GST_PAD (pads->data);
37 g_print ("pad name %s\n", gst_pad_get_name (pad));
39 pads = g_list_next (pads);
43 <sect2 id="sec-pads-functions">
44 <title>Useful pad functions</title>
46 You can get the name of a pad with gst_pad_get_name () and set its name with
50 gst_pad_get_direction (GstPad *pad) can be used to query if the pad is a sink
51 or a src pad. Remember a src pad is a pad that can output data and a sink pad is
52 one that accepts data.
55 You can get the parent of the pad, this is the element that this pad belongs to,
56 with get_pad_set_parent(GstPad *pad). This function will return a pointer to a
60 <sect2 id="sec-pads-dynamic">
61 <title>Dynamic pads</title>
63 Some elements might not have their pads when they are created. This can, for
64 example, happen with an MPEG2 system demuxer. The demuxer will create its
65 pads at runtime when it detects the different elementary streams in the MPEG2
69 Running <application>gstreamer-inspect mpeg2parse</application> will show that
70 the element has only one pad: a sink pad called 'sink'. The other pads are
71 "dormant" as you can see in the padtemplates from the 'Exists: Sometimes'
72 property. Depending on the type of MPEG2 file you play, the pads are created. We
73 will see that this is very important when you are going to create dynamic
74 pipelines later on in this manual.
77 You can attach a signal to an element to inform you when the element has created
78 a new pad from one of its padtemplates. The following piece of code is an example
83 pad_connect_func (GstElement *parser, GstPad *pad, GstElement *pipeline)
85 g_print("***** a new pad %s was created\n", gst_pad_get_name(pad));
87 gst_element_set_state (pipeline, GST_STATE_PAUSED);
89 if (strncmp (gst_pad_get_name (pad), "private_stream_1.0", 18) == 0) {
90 // set up an AC3 decoder pipeline
92 // connect pad to the AC3 decoder pipeline
95 gst_element_set_state (GST_ELEMENT (audio_thread), GST_STATE_READY);
99 main(int argc, char *argv[])
101 GstElement *pipeline;
102 GstElement *mpeg2parser;
104 // create pipeline and do something usefull
107 mpeg2parser = gst_elementfactory_make ("mpeg2parse", "mpeg2parse");
108 g_signal_connect (G_OBJECT (mpeg2parser), "new_pad", pad_connect_func, pipeline);
111 // start the pipeline
112 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
118 You need to set the pipeline to READY or NULL if you want to change it.
122 <sect2 id="sec-pads-request">
123 <title>Request pads</title>
125 An element can also have request pads. These pads are not created automatically
126 but are only created on demand. This is very usefull for muxers, aggregators
130 The tee element, for example, has one input pad and a request padtemplate for the
131 output pads. Whenever an element wants to get an output pad from the tee element, it
132 has to request the pad.
135 The following piece of code can be used to get a pad from the tee element. After
136 the pad has been requested, it can be used to connect another element to it.
142 element = gst_elementfactory_make ("tee", "element");
144 pad = gst_element_request_pad_by_name (element, "src%d");
145 g_print ("new pad %s\n", gst_pad_get_name (pad));
149 The gst_element_request_pad_by_name method can be used to get a pad
150 from the element based on the name_template of the padtemplate.
153 It is also possible to request a pad that is compatible with another
154 padtemplate. This is very usefull if you want to connect an element to
155 a muxer element and you need to request a pad that is compatible. The
156 gst_element_request_compatible_pad is used to request a compatible pad, as
157 is shown in the next example.
161 GstPadTemplate *templ;
164 element = gst_elementfactory_make ("tee", "element");
165 mp3parse = gst_elementfactory_make ("mp3parse", "mp3parse");
167 templ = gst_element_get_padtemplate_by_name (mp3parse, "sink");
169 pad = gst_element_request_compatible_pad (element, templ);
170 g_print ("new pad %s\n", gst_pad_get_name (pad));
175 <sect1 id="sec-pads-description">
176 <title>Capabilities of a GstPad</title>
178 Since the pads play a very important role in how the element is viewed by the
179 outside world, a mechanism is implemented to describe the pad by using capabilities.
182 We will briefly describe what capabilities are, enough for you to get a basic understanding
183 of the concepts. You will find more information on how to create capabilities in the
187 <sect2 id="sec-pads-caps">
188 <title>What is a capability</title>
190 A capability is attached to a pad in order to describe what type of media the pad
194 A capability is named and consists of a MIME type and a set of properties. Its data
199 gchar *name; /* the name of this caps */
200 guint16 id; /* type id (major type) */
202 guint refcount; /* caps are refcounted */
204 GstProps *properties; /* properties for this capability */
206 GstCaps *next; /* caps can be chained together */
210 Below is a dump of the capabilities of the element mpg123, as shown by
211 <command>gstreamer-inspect</command>.
212 You can see two pads: sink and src. Both pads have capability information attached to them.
215 The sink pad (input pad) is called 'sink' and takes data of MIME type 'audio/mp3'. It also has
216 three properties: layer, bitrate and framed.
219 The src pad (output pad) is called 'src' and outputs data of MIME type 'audio/raw'. It also has
220 four properties: format, depth, rate and channels.
228 MIME type: 'audio/mp3':
229 layer: Integer range: 1 - 3
230 bitrate: Integer range: 8 - 320
231 framed: Boolean: TRUE
237 MIME type: 'audio/raw':
240 rate: Integer range: 11025 - 48000
246 <sect2 id="sec-pads-props">
247 <title>What are properties</title>
249 Properties are used to describe extra information for the capabilities. The properties
250 basically exist of a key (a string) and a value. There are different possibile value types
257 An integer value: the property has this exact value.
262 An integer range value. The property denotes a range of possible values. In the case
263 of the mpg123 element: the src pad has a property rate that can go from 11025 to 48000.
273 a fourcc value: this is a value that is commonly used to describe an encoding for video,
274 as used be the AVI specification.
279 A list value: the property can take any value from a list.
284 A float value: the property has this exact floating point value.
289 A float range value: denotes a range of possible floating point values.
300 <sect2 id="sec-pads-caps-use">
301 <title>What are the capabilities used for?</title>
303 Capabilities describe in great detail the type of media that is handled by the pads.
304 They are mostly used for:
309 Autoplugging: automatically finding plugins for a set of capabilities
314 Compatibility detection: when two pads are connected, <application>GStreamer</application>
315 can verify if the two pads are talking about the same media types.
320 <sect2 id="sec-pads-caps-get">
321 <title>Getting the capabilities of a pad</title>
323 A pad can have a chain of capabilities attached to it. You can get the capabilities chain
329 caps = gst_pad_get_caps (pad);
331 g_print ("pad name %s\n", gst_pad_get_name (pad));
334 g_print (" Capability name %s, MIME type %s\n",
335 gst_caps_get_name (cap),
336 gst_caps_get_mime (cap));
338 caps = caps->next;
343 <sect2 id="sec-pads-caps-create">
344 <title>Creating capabilities structures</title>
346 While the capabilities are mainly used inside the plugin to describe the media type of the
347 pads, the application programmer also has to have basic understanding of caps in order to
348 interface with the plugins, specially when using the autopluggers.
351 As we said, a capability has a name, a mime-type and some properties. The signature of the
352 function to create a new <classname>GstCaps *</classname> structure is like:
354 GstCaps* gst_caps_new (const gchar *name, const gchar *mime, GstProps *props);
358 You can therefore create a new capability with no properties like this:
362 newcaps = gst_caps_new ("my_caps", "audio/wav", NULL);
366 <classname>GstProps</classname> basically consist of a set of key-value pairs
367 and are created with a function with this signature:
369 GstProps* gst_props_new (const gchar *firstname, ...);
373 The keys are given as strings and the values are given with a set of macros:
377 GST_PROPS_INT(a): An integer value
382 GST_PROPS_FLOAT(a): A floating point value
387 GST_PROPS_FOURCC(a): A fourcc value
392 GST_PROPS_BOOLEAN(a): A boolean value
397 GST_PROPS_STRING(a): A string value
401 The values can also be specified as ranges with:
405 GST_PROPS_INT_RANGE(a,b): An integer ragne from a to b
410 GST_PROPS_FLOAT_RANGE(a,b): A float ragne from a to b
414 All of the above values can be given with a list too, using:
418 GST_PROPS_LIST(a,...): A list of property values.
424 A more complex capability with properties is created like this:
428 newcaps = gst_caps_new ("my_caps",
431 "bitrate", GST_PROPS_INT_RANGE (11025,22050),
432 "depth", GST_PROPS_INT (16),
433 "signed", GST_PROPS_LIST (
434 GST_PROPS_BOOLEAN (TRUE),
435 GST_PROPS_BOOLEAN (FALSE)
440 Optionally the convenient shortcut macro can be used. The above complex
441 capability can be created with:
445 newcaps = GST_CAPS_NEW ("my_caps",
447 "bitrate", GST_PROPS_INT_RANGE (11025,22050),
448 "depth", GST_PROPS_INT (16),
449 "signed", GST_PROPS_LIST (
450 GST_PROPS_BOOLEAN (TRUE),
451 GST_PROPS_BOOLEAN (FALSE)