Pads

As we have seen in the chapter called Elements, the pads are the element's interface to 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(see the Section called Capabilities of a pad in the chapter called Pads)

Pads are either source or sink pads. The terminology is defined from the view of the element itself: elements accept data on their sink pads, and send data out on their source pads. Sink pads are drawn on the left, while source pads are drawn on the right of an element. In general, data flows from left to right in the graph.[1]

Types of pads

Dynamic pads

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

Note

A pipeline cannot be changed in the PLAYING state.

Request pads

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 link another element to it.


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

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

The gst_element_get_request_pad 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 pad template. This is very useful if you want to link an element to a multiplexer element and you need to request a pad that is compatible. The gst_element_get_compatible_pad is used to request a compatible pad, as is shown in the next example.


    ...
  GstPadTemplate *templ;
  GstPad *pad;
    ...
  element = gst_element_factory_make ("tee", "element");
  mad = gst_element_factory_make ("mad", "mad");

  templ = gst_element_get_pad_template_by_name (mad, "sink");

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

Notes

[1]

In reality, there is no objection to data flowing from a source pad to the sink pad of an element upstream. Data will, however, always flow from a source pad of one element to the sink pad of another.