1 <chapter id="cha-pads">
4 As we have seen in the previous chapter (GstElement), the pads are the element's
5 links 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 request a GList of pads from the element. The
28 following code example will print the names of all the pads of an
34 pads = gst_element_get_pad_list (element);
36 GstPad *pad = GST_PAD (pads->data);
38 g_print ("pad name %s\n", gst_pad_get_name (pad));
40 pads = g_list_next (pads);
44 <sect2 id="sec-pads-functions">
45 <title>Useful pad functions</title>
47 You can get the name of a pad with gst_pad_get_name () and set its name with
51 gst_pad_get_direction (GstPad *pad) can be used to query if the pad
52 is a sink or a source pad. Remember that a source pad is a pad that
53 can output data and a sink pad is one that accepts data.
56 You can get the parent of the pad, this is the element that this pad belongs to,
57 with get_pad_get_parent(GstPad *pad). This function will return a pointer to a
61 <sect2 id="sec-pads-dynamic">
62 <title>Dynamic pads</title>
64 Some elements might not have their pads when they are created. This
65 can happen, for example, with an MPEG2 system demultiplexer. The
66 demultiplexer will create its pads at runtime when it detects the
67 different elementary streams in the MPEG2 system stream.
70 Running <application>gst-inspect mpegdemux</application> will show that
71 the element has only one pad: a sink pad called 'sink'. The other pads are
72 "dormant" as you can see in the padtemplates from the 'Exists: Sometimes'
73 property. Depending on the type of MPEG2 file you play, the pads are created. We
74 will see that this is very important when you are going to create dynamic
75 pipelines later on in this manual.
78 You can attach a signal to an element to inform you when the element has created
79 a new pad from one of its padtemplates. The following piece of code is an example
84 pad_link_func (GstElement *parser, GstPad *pad, GstElement *pipeline)
86 g_print("***** a new pad %s was created\n", gst_pad_get_name(pad));
88 gst_element_set_state (pipeline, GST_STATE_PAUSED);
90 if (strncmp (gst_pad_get_name (pad), "private_stream_1.0", 18) == 0) {
91 // set up an AC3 decoder pipeline
93 // link pad to the AC3 decoder pipeline
96 gst_element_set_state (GST_ELEMENT (audio_thread), GST_STATE_READY);
100 main(int argc, char *argv[])
102 GstElement *pipeline;
103 GstElement *mpeg2parser;
105 // create pipeline and do something useful
108 mpeg2parser = gst_element_factory_make ("mpegdemux", "mpegdemux");
109 g_signal_connect (G_OBJECT (mpeg2parser), "new_pad", pad_link_func, pipeline);
112 // start the pipeline
113 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
119 You need to set the pipeline to READY or NULL if you want to change it.
123 <sect2 id="sec-pads-request">
124 <title>Request pads</title>
126 An element can also have request pads. These pads are not created
127 automatically but are only created on demand. This is very useful
128 for multiplexers, aggregators and tee elements.
131 The tee element, for example, has one input pad and a request padtemplate for the
132 output pads. Whenever an element wants to get an output pad from the tee element, it
133 has to request the pad.
136 The following piece of code can be used to get a pad from the tee element. After
137 the pad has been requested, it can be used to link another element to it.
143 element = gst_element_factory_make ("tee", "element");
145 pad = gst_element_get_request_pad (element, "src%d");
146 g_print ("new pad %s\n", gst_pad_get_name (pad));
150 The gst_element_get_request_pad method can be used to get a pad
151 from the element based on the name_template of the padtemplate.
154 It is also possible to request a pad that is compatible with another
155 pad template. This is very useful if you want to link an element
156 to a multiplexer element and you need to request a pad that is
157 compatible. The gst_element_get_compatible_pad is used to request
158 a compatible pad, as is shown in the next example.
162 GstPadTemplate *templ;
165 element = gst_element_factory_make ("tee", "element");
166 mad = gst_element_factory_make ("mad", "mad");
168 templ = gst_element_get_pad_template_by_name (mad, "sink");
170 pad = gst_element_get_compatible_pad (element, templ);
171 g_print ("new pad %s\n", gst_pad_get_name (pad));
176 <sect1 id="sec-pads-description">
177 <title>Capabilities of a GstPad</title>
179 Since the pads play a very important role in how the element is viewed by the
180 outside world, a mechanism is implemented to describe the pad by using capabilities.
183 We will briefly describe what capabilities are, enough for you to get a basic understanding
184 of the concepts. You will find more information on how to create capabilities in the
185 Plugin Writer's Guide.
188 <sect2 id="sec-pads-caps">
189 <title>What is a capability</title>
191 A capability is attached to a pad in order to describe what type of media the pad
195 A capability is named and consists of a MIME type and a set of properties. Its data
200 gchar *name; /* the name of this caps */
201 guint16 id; /* type id (major type) */
203 guint refcount; /* caps are refcounted */
205 GstProps *properties; /* properties for this capability */
207 GstCaps *next; /* caps can be chained together */
211 Below is a dump of the capabilities of the element mad, as shown by
212 <command>gst-inspect</command>.
213 You can see two pads: sink and src. Both pads have capability information attached to them.
216 The sink pad (input pad) is called 'sink' and takes data of MIME type 'audio/mp3'. It also has
217 three properties: layer, bitrate and framed.
220 The source pad (output pad) is called 'src' and outputs data of
221 MIME type 'audio/raw'. It also has four properties: format, depth,
226 SINK template: 'sink'
230 MIME type: 'audio/mp3':
236 MIME type: 'audio/raw':
238 endianness: Integer: 1234
241 channels: Integer range: 1 - 2
243 signed: Boolean: TRUE
244 rate: Integer range: 11025 - 48000
247 <sect2 id="sec-pads-props">
248 <title>What are properties</title>
250 Properties are used to describe extra information for
251 capabilities. The properties basically consist of a key (a string) and
252 a value. There are different possibile value types that can be used:
258 An integer value: the property has this exact value.
263 An integer range value. The property denotes a range of possible
264 values. In the case of the mad element, the source pad has a
265 property rate that can go from 11025 to 48000.
275 a fourcc value: this is a value that is commonly used to describe an encoding for video,
276 as used by the AVI specification.
281 A list value: the property can take any value from a list.
286 A float value: the property has this exact floating point value.
291 A float range value: denotes a range of possible floating point values.
302 <sect2 id="sec-pads-caps-use">
303 <title>What capabilities are used for</title>
305 Capabilities describe in great detail the type of media that is handled by the pads.
306 They are mostly used for:
311 Autoplugging: automatically finding plugins for a set of capabilities
316 Compatibility detection: when two pads are linked, <application>GStreamer</application>
317 can verify if the two pads are talking about the same media types.
322 <sect2 id="sec-pads-caps-get">
323 <title>Getting the capabilities of a pad</title>
325 A pad can have a chain of capabilities attached to it. You can get the capabilities chain
331 caps = gst_pad_get_caps (pad);
333 g_print ("pad name %s\n", gst_pad_get_name (pad));
336 g_print (" Capability name %s, MIME type %s\n",
337 gst_caps_get_name (cap),
338 gst_caps_get_mime (cap));
340 caps = caps->next;
345 <sect2 id="sec-pads-caps-create">
346 <title>Creating capability structures</title>
348 While capabilities are mainly used inside a plugin to describe the
349 media type of the pads, the application programmer also has to have
350 basic understanding of capabilities in order to interface with the
351 plugins, specially when using the autopluggers.
354 As we said, a capability has a name, a mime-type and some
355 properties. The signature of the function to create a new
356 <classname>GstCaps</classname> structure is:
359 GstCaps* gst_caps_new (const gchar *name, const gchar *mime, GstProps *props);
363 You can therefore create a new capability with no properties like this:
367 newcaps = gst_caps_new ("my_caps", "audio/wav", NULL);
371 <classname>GstProps</classname> basically consist of a set of key-value pairs
372 and are created with a function with this signature:
374 GstProps* gst_props_new (const gchar *firstname, ...);
378 The keys are given as strings and the values are given with a set of macros:
382 GST_PROPS_INT(a): An integer value
387 GST_PROPS_FLOAT(a): A floating point value
392 GST_PROPS_FOURCC(a): A fourcc value
397 GST_PROPS_BOOLEAN(a): A boolean value
402 GST_PROPS_STRING(a): A string value
406 The values can also be specified as ranges with:
410 GST_PROPS_INT_RANGE(a,b): An integer ragne from a to b
415 GST_PROPS_FLOAT_RANGE(a,b): A float ragne from a to b
419 All of the above values can be given with a list too, using:
423 GST_PROPS_LIST(a,...): A list of property values.
429 A more complex capability with properties is created like this:
433 newcaps = gst_caps_new ("my_caps",
436 "bitrate", GST_PROPS_INT_RANGE (11025,22050),
437 "depth", GST_PROPS_INT (16),
438 "signed", GST_PROPS_LIST (
439 GST_PROPS_BOOLEAN (TRUE),
440 GST_PROPS_BOOLEAN (FALSE)
445 Optionally, the convenient shortcut macro can be used. The above complex
446 capability can be created with:
450 newcaps = GST_CAPS_NEW ("my_caps",
452 "bitrate", GST_PROPS_INT_RANGE (11025,22050),
453 "depth", GST_PROPS_INT (16),
454 "signed", GST_PROPS_LIST (
455 GST_PROPS_BOOLEAN (TRUE),
456 GST_PROPS_BOOLEAN (FALSE)