Chapter 7. GstPad

Table of Contents

Getting pads from an element
Useful pad functions
Dynamic pads
Request pads
Capabilities of a GstPad
What is a capability
What are properties
What capabilities are used for
Getting the capabilities of a pad
Creating capability structures

As we have seen in the previous chapter (GstElement), the pads are the element's links with the outside world.

The specific type of media that the element can handle will be exposed by the pads. The description of this media type is done with capabilities (GstCaps)

Once you have created an element, you can get one of its pads with:

 GstPad *srcpad;
    ...
 srcpad = gst_element_get_pad (element, "src");
    ...
    

This function will get the pad named "src" from the given element.

Alternatively, you can request a GList of pads from the element. The following code example will print the names of all the pads of an element.

 GList *pads;
    ...
 pads = gst_element_get_pad_list (element);
 while (pads) {
   GstPad *pad = GST_PAD (pads->data);

   g_print ("pad name %s\n", gst_pad_get_name (pad));
   
   pads = g_list_next (pads);
 }
    ...
    

Some elements might not have their pads when they are created. This can happen, for example, with an MPEG2 system demultiplexer. The demultiplexer will create its pads at runtime when it detects the different elementary streams in the MPEG2 system stream.

Running gst-inspect mpegdemux will show that the element has only one pad: a sink pad called 'sink'. The other pads are "dormant" as you can see in the padtemplates from the 'Exists: Sometimes' property. Depending on the type of MPEG2 file you play, the pads are created. We will see that this is very important when you are going to create dynamic pipelines later on in this manual.

You can attach a signal to an element to inform you when the element has created a new pad from one of its padtemplates. The following piece of code is an example of how to do this:

static void
pad_link_func (GstElement *parser, GstPad *pad, GstElement *pipeline)
{
  g_print("***** a new pad %s was created\n", gst_pad_get_name(pad));

  gst_element_set_state (pipeline, GST_STATE_PAUSED);

  if (strncmp (gst_pad_get_name (pad), "private_stream_1.0", 18) == 0) {
    // set up an AC3 decoder pipeline
    ...
    // link pad to the AC3 decoder pipeline
    ...
  }
  gst_element_set_state (GST_ELEMENT (audio_thread), GST_STATE_READY);
}

int 
main(int argc, char *argv[]) 
{
  GstElement *pipeline;
  GstElement *mpeg2parser;

  // create pipeline and do something useful
  ...
  
  mpeg2parser = gst_element_factory_make ("mpegdemux", "mpegdemux");
  g_signal_connect (G_OBJECT (mpeg2parser), "new_pad", pad_link_func, pipeline);  
  ...

  // start the pipeline
  gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
  ...
}