4.2. The getcaps function

The _getcaps () funtion is used to request the list of supported formats and properties from the element. In some cases, this will be equal to the formats provided by the pad template, in which case this function can be omitted. In some cases, too, it will not depend on anything inside this element, but it will rather depend on the input from another element linked to this element's sink or source pads. In that case, you can use gst_pad_proxy_getcaps as implementation, it provides getcaps forwarding in the core. However, in many cases, the format supported by this element cannot be defined externally, but is more specific than those provided by the pad template. In this case, you should use a _getcaps () function. In the case as specified below, we assume that our filter is able to resample sound, so it would be able to provide any samplerate (indifferent from the samplerate specified on the other pad) on both pads. It explains how a _getcaps () can be used to do this.


static GstCaps *
gst_my_filter_getcaps (GstPad *pad)
{
  GstMyFilter *filter = GST_MY_FILTER (gst_pad_get_parent (pad));
  GstPad *otherpad = (pad == filter->srcpad) ? filter->sinkpad :
					       filter->srcpad;
  GstCaps *othercaps = gst_pad_get_allowed_caps (otherpad), *caps;
  gint n;

  if (gst_caps_is_empty (othercaps))
    return othercaps;

  /* We support *any* samplerate, indifferent from the samplerate
   * supported by the linked elements on both sides. */
  for (i = 0; i < gst_caps_get_size (othercaps); i++) {
    GstStructure *structure = gst_caps_get_structure (othercaps, i);

    gst_structure_remove_field (structure, "rate");
  }
  caps = gst_caps_intersect (othercaps, gst_pad_get_pad_template_caps (pad));
  gst_caps_free (othercaps);

  return caps;
}