Improve caps setters API
authorBenjamin Otte <otte@gnome.org>
Wed, 7 Oct 2009 13:32:18 +0000 (15:32 +0200)
committerBenjamin Otte <otte@gnome.org>
Thu, 15 Oct 2009 17:10:50 +0000 (19:10 +0200)
This patch adds gst_caps_set_value() and allows gst_caps_set_simple() to
work on non-simple caps. See the API documentation for the functions
about what they do.

The intention of these changes is to ease working with caps in caps
transform functions. An example for this would be ffmpegcolorspace,
where the caps transform function could be changed to look roughly like
this (pseudocode ahead):
  result = gst_caps_copy (template_caps);
  value = gst_structure_get_value (gst_caps_get_structure (caps, 0),
    "widh");
  gst_caps_set_value (result, value);
  /* same for height, framerate and par */
  return caps;
which is much cleaner and easier to understand than the current code.

https://bugzilla.gnome.org/show_bug.cgi?id=597690

docs/gst/gstreamer-sections.txt
gst/gstcaps.c
gst/gstcaps.h

index c7b3d87..59a3de2 100644 (file)
@@ -291,6 +291,7 @@ gst_caps_remove_structure
 gst_caps_merge_structure
 gst_caps_get_size
 gst_caps_get_structure
+gst_caps_set_value
 gst_caps_set_simple
 gst_caps_set_simple_valist
 gst_caps_is_any
index b7e5ebc..8b69c8e 100644 (file)
@@ -71,6 +71,7 @@
 
 #include "gst_private.h"
 #include <gst/gst.h>
+#include <gobject/gvaluecollector.h>
 
 #define DEBUG_REFCOUNT
 
@@ -869,54 +870,105 @@ gst_caps_truncate (GstCaps * caps)
 }
 
 /**
- * gst_caps_set_simple:
+ * gst_caps_set_value:
+ * @caps: a writable caps
+ * @field: name of the field to set
+ * @value: value to set the field to
+ *
+ * Sets the given @field on all structures of @caps to the given @value.
+ * This is a convenience function for calling gst_structure_set_value() on
+ * all structures of @caps.
+ *
+ * Since: 0.10.26
+ **/
+void
+gst_caps_set_value (GstCaps * caps, const char *field, const GValue * value)
+{
+  guint i, len;
+
+  g_return_if_fail (GST_IS_CAPS (caps));
+  g_return_if_fail (IS_WRITABLE (caps));
+  g_return_if_fail (field != NULL);
+  g_return_if_fail (G_IS_VALUE (value));
+
+  len = caps->structs->len;
+  for (i = 0; i < len; i++) {
+    GstStructure *structure = gst_caps_get_structure_unchecked (caps, i);
+    gst_structure_set_value (structure, field, value);
+  }
+}
+
+/**
+ * gst_caps_set_simple_valist:
  * @caps: the #GstCaps to set
  * @field: first field to set
- * @...: additional parameters
+ * @varargs: additional parameters
  *
- * Sets fields in a simple #GstCaps.  A simple #GstCaps is one that
- * only has one structure.  The arguments must be passed in the same
+ * Sets fields in a #GstCaps.  The arguments must be passed in the same
  * manner as gst_structure_set(), and be NULL-terminated.
+ * <note>Prior to GStreamer version 0.10.26, this function failed when
+ * @caps was simple. If your code needs to work with those versions of
+ * GStreamer, you amy only call this function when GST_CAPS_IS_SIMPLE()
+ * returns %TRUE for @caps.</note>
  */
 void
-gst_caps_set_simple (GstCaps * caps, const char *field, ...)
+gst_caps_set_simple_valist (GstCaps * caps, const char *field, va_list varargs)
 {
-  GstStructure *structure;
-  va_list var_args;
+  GValue value = { 0, };
 
   g_return_if_fail (GST_IS_CAPS (caps));
-  g_return_if_fail (caps->structs->len == 1);
   g_return_if_fail (IS_WRITABLE (caps));
 
-  structure = gst_caps_get_structure_unchecked (caps, 0);
+  while (field) {
+    GType type;
+    char *err;
 
-  va_start (var_args, field);
-  gst_structure_set_valist (structure, field, var_args);
-  va_end (var_args);
+    type = va_arg (varargs, GType);
+
+    if (G_UNLIKELY (type == G_TYPE_DATE)) {
+      g_warning ("Don't use G_TYPE_DATE, use GST_TYPE_DATE instead\n");
+      type = GST_TYPE_DATE;
+    }
+
+    g_value_init (&value, type);
+    G_VALUE_COLLECT (&value, varargs, 0, &err);
+    if (G_UNLIKELY (err)) {
+      g_critical ("%s", err);
+      return;
+    }
+
+    gst_caps_set_value (caps, field, &value);
+
+    g_value_unset (&value);
+
+    field = va_arg (varargs, const gchar *);
+  }
 }
 
 /**
- * gst_caps_set_simple_valist:
- * @caps: the #GstCaps to copy
+ * gst_caps_set_simple:
+ * @caps: the #GstCaps to set
  * @field: first field to set
- * @varargs: additional parameters
+ * @...: additional parameters
  *
- * Sets fields in a simple #GstCaps.  A simple #GstCaps is one that
- * only has one structure.  The arguments must be passed in the same
+ * Sets fields in a #GstCaps.  The arguments must be passed in the same
  * manner as gst_structure_set(), and be NULL-terminated.
+ * <note>Prior to GStreamer version 0.10.26, this function failed when
+ * @caps was simple. If your code needs to work with those versions of
+ * GStreamer, you amy only call this function when GST_CAPS_IS_SIMPLE()
+ * returns %TRUE for @caps.</note>
  */
 void
-gst_caps_set_simple_valist (GstCaps * caps, const char *field, va_list varargs)
+gst_caps_set_simple (GstCaps * caps, const char *field, ...)
 {
-  GstStructure *structure;
+  va_list var_args;
 
   g_return_if_fail (GST_IS_CAPS (caps));
-  g_return_if_fail (caps->structs->len == 1);
   g_return_if_fail (IS_WRITABLE (caps));
 
-  structure = gst_caps_get_structure_unchecked (caps, 0);
-
-  gst_structure_set_valist (structure, field, varargs);
+  va_start (var_args, field);
+  gst_caps_set_simple_valist (caps, field, var_args);
+  va_end (var_args);
 }
 
 /* tests */
index e535881..e0a2aae 100644 (file)
@@ -207,6 +207,9 @@ GstStructure *    gst_caps_get_structure           (const GstCaps *caps,
                                                     guint          index);
 GstCaps *         gst_caps_copy_nth                (const GstCaps *caps, guint nth);
 void              gst_caps_truncate                (GstCaps       *caps);
+void              gst_caps_set_value               (GstCaps       *caps,
+                                                    const char    *field,
+                                                    const GValue  *value);
 void              gst_caps_set_simple              (GstCaps       *caps,
                                                     const char    *field, ...) G_GNUC_NULL_TERMINATED;
 void              gst_caps_set_simple_valist       (GstCaps       *caps,