legacyresample: fix negotiation so that upstream can actually fixate to downstream...
authorTim-Philipp Müller <tim.muller@collabora.co.uk>
Wed, 1 Apr 2009 14:45:22 +0000 (15:45 +0100)
committerTim-Philipp Müller <tim.muller@collabora.co.uk>
Wed, 1 Apr 2009 14:45:22 +0000 (15:45 +0100)
If one side has a preference for a particular sample rate or set of sample rates, we
should honour this in the caps we advertise and transform to and from, so that elements
actually know about the other side's sample rate preference and can negotiate to it
if supported. Also add unit test for this.

gst/legacyresample/gstlegacyresample.c

index 908b6ad..cbcd6fd 100644 (file)
@@ -265,14 +265,34 @@ static GstCaps *
 legacyresample_transform_caps (GstBaseTransform * base,
     GstPadDirection direction, GstCaps * caps)
 {
+  const GValue *val;
+  GstStructure *s;
   GstCaps *res;
-  GstStructure *structure;
 
-  /* transform caps gives one single caps so we can just replace
-   * the rate property with our range. */
+  /* transform single caps into input_caps + input_caps with the rate
+   * field set to our supported range. This ensures that upstream knows
+   * about downstream's prefered rate(s) and can negotiate accordingly. */
   res = gst_caps_copy (caps);
-  structure = gst_caps_get_structure (res, 0);
-  gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
+
+  /* first, however, check if the caps contain a range for the rate field, in
+   * which case that side isn't going to care much about the exact sample rate
+   * chosen and we should just assume things will get fixated to something sane
+   * and we may just as well offer our full range instead of the range in the
+   * caps. If the rate is not an int range value, it's likely to express a
+   * real preference or limitation and we should maintain that structure as
+   * preference by putting it first into the transformed caps, and only add
+   * our full rate range as second option  */
+  s = gst_caps_get_structure (res, 0);
+  val = gst_structure_get_value (s, "rate");
+  if (val == NULL || GST_VALUE_HOLDS_INT_RANGE (val)) {
+    /* overwrite existing range, or add field if it doesn't exist yet */
+    gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
+  } else {
+    /* append caps with full range to existing caps with non-range rate field */
+    s = gst_structure_copy (s);
+    gst_structure_set (s, "rate", GST_TYPE_INT_RANGE, 1, G_MAXINT, NULL);
+    gst_caps_append_structure (res, s);
+  }
 
   return res;
 }