GstPad

As we have seen in the previous chapter (GstElement), the pads are the elements connections 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)

Getting pads from an element

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 also 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);
 }
    ...
    

Useful pad functions

You can get the name of a pad with gst_pad_get_name () and set its name with get_pad_set_name();

gst_pad_get_direction (GstPad *pad) can be used to query if the pad is a sink or a src pad. Remember a src pad is a pad that can output data and a sink pad is one that accepts data.

You can get the parent of the pad, this is the element that this pad belongs to, with get_pad_set_parent(GstPad *pad). This function will return a pointer to a GstObject.

Dynamic pads

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

Running gstreamer-inspect mpeg2parse 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_connect_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
    ...
    // connect 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 usefull
  ...
  
  mpeg2parser = gst_elementfactory_make ("mpeg2parse", "mpeg2parse");
  gtk_signal_connect (GTK_OBJECT (mpeg2parser), "new_pad", pad_connect_func, pipeline);  
  ...

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

Note

You need to set the pipeline to READY or NULL if you want to change it.

Request pads

An element can also have request pads. These pads are not created automatically but are only created on demand. This is very usefull for muxers, aggregators and tee elements.

The tee element, for example, has one input pad and a request padtemplate for the output pads. Whenever an element wants to get an output pad from the tee element, it has to request the pad.

The following piece of code can be used to get a pad from the tee element. After the pad has been requested, it can be used to connect another element to it.

    ...
  GstPad *pad;
    ...
  element = gst_elementfactory_make ("tee", "element");

  pad = gst_element_request_pad_by_name (element, "src%d");
  g_print ("new pad %s\n", gst_pad_get_name (pad));
    ...
      

The gst_element_request_pad_by_name method can be used to get a pad from the element based on the name_template of the padtemplate.

It is also possible to request a pad that is compatible with another padtemplate. This is very usefull if you want to connect an element to a muxer element and you need to request a pad that is compatible. The gst_element_request_compatible_pad is used to request a compatible pad, as is shown in the next example.

    ...
  GstPadTemplate *templ;
  GstPad *pad;
    ...
  element = gst_elementfactory_make ("tee", "element");
  mp3parse = gst_elementfactory_make ("mp3parse", "mp3parse");

  templ = gst_element_get_padtemplate_by_name (mp3parse, "sink");

  pad = gst_element_request_compatible_pad (element, templ);
  g_print ("new pad %s\n", gst_pad_get_name (pad));
  ...