gstpad: only warn on performance penalty if not using the template caps
[platform/upstream/gstreamer.git] / gst / gststructure.c
index 6edc359..c8d37e0 100644 (file)
  * gst_structure_remove_fields().
  *
  * Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
- * not allowed. Strings may be NULL however.
+ * not allowed. Strings may be %NULL however.
  *
  * Be aware that the current #GstCaps / #GstStructure serialization into string
  * has limited support for nested #GstCaps / #GstStructure fields. It can only
  * support one level of nesting. Using more levels will lead to unexpected
  * behavior when using serialization features, such as gst_caps_to_string() or
  * gst_value_serialize() and their counterparts.
- *
- * Last reviewed on 2012-03-29 (0.11.3)
  */
 
 #ifdef HAVE_CONFIG_H
@@ -239,7 +237,7 @@ gst_structure_new_empty (const gchar * name)
  * Creates a new #GstStructure with the given name.  Parses the
  * list of variable arguments and sets fields to the values listed.
  * Variable arguments should be passed as field name, field type,
- * and value.  Last variable argument should be NULL.
+ * and value.  Last variable argument should be %NULL.
  *
  * Free-function: gst_structure_free
  *
@@ -417,7 +415,7 @@ gst_structure_get_name (const GstStructure * structure)
  *
  * Checks if the structure has the given name
  *
- * Returns: TRUE if @name matches the name of the structure.
+ * Returns: %TRUE if @name matches the name of the structure.
  */
 gboolean
 gst_structure_has_name (const GstStructure * structure, const gchar * name)
@@ -621,9 +619,10 @@ gst_structure_set_valist_internal (GstStructure * structure,
  * @fieldname: the name of the field to set
  * @...: variable arguments
  *
- * Parses the variable arguments and sets fields accordingly.
+ * Parses the variable arguments and sets fields accordingly. Fields that
+ * weren't already part of the structure are added as needed.
  * Variable arguments should be in the form field name, field type
- * (as a GType), value(s).  The last variable argument should be NULL.
+ * (as a GType), value(s).  The last variable argument should be %NULL.
  */
 void
 gst_structure_set (GstStructure * structure, const gchar * field, ...)
@@ -667,15 +666,9 @@ gst_structure_id_set_valist_internal (GstStructure * structure,
     GstStructureField field = { 0 };
 
     field.name = fieldname;
-
     type = va_arg (varargs, GType);
 
-#ifndef G_VALUE_COLLECT_INIT
-    g_value_init (&field.value, type);
-    G_VALUE_COLLECT (&field.value, varargs, 0, &err);
-#else
     G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
-#endif
     if (G_UNLIKELY (err)) {
       g_critical ("%s", err);
       return;
@@ -696,7 +689,7 @@ gst_structure_id_set_valist_internal (GstStructure * structure,
  * passed using the GQuark for the field name. This allows more efficient
  * setting of the structure if the caller already knows the associated
  * quark values.
- * The last variable argument must be NULL.
+ * The last variable argument must be %NULL.
  */
 void
 gst_structure_id_set (GstStructure * structure, GQuark field, ...)
@@ -739,7 +732,7 @@ gst_structure_id_set_valist (GstStructure * structure,
  * gst_structure_id_set(). Basically a convenience wrapper around
  * gst_structure_new_id_empty() and gst_structure_id_set().
  *
- * The last variable argument must be NULL (or 0).
+ * The last variable argument must be %NULL (or 0).
  *
  * Free-function: gst_structure_free
  *
@@ -777,9 +770,13 @@ static void
 gst_structure_set_field (GstStructure * structure, GstStructureField * field)
 {
   GstStructureField *f;
-  guint i, len = GST_STRUCTURE_FIELDS (structure)->len;
+  GType field_value_type;
+  guint i, len;
+
+  len = GST_STRUCTURE_FIELDS (structure)->len;
 
-  if (G_UNLIKELY (G_VALUE_HOLDS_STRING (&field->value))) {
+  field_value_type = G_VALUE_TYPE (&field->value);
+  if (field_value_type == G_TYPE_STRING) {
     const gchar *s;
 
     s = g_value_get_string (&field->value);
@@ -806,7 +803,7 @@ gst_structure_set_field (GstStructure * structure, GstStructureField * field)
       g_value_unset (&field->value);
       return;
     }
-  } else if (G_UNLIKELY (G_VALUE_HOLDS (&field->value, G_TYPE_DATE))) {
+  } else if (G_UNLIKELY (field_value_type == G_TYPE_DATE)) {
     const GDate *d;
 
     d = g_value_get_boxed (&field->value);
@@ -962,7 +959,7 @@ gst_structure_remove_field (GstStructure * structure, const gchar * fieldname)
  * gst_structure_remove_fields:
  * @structure: a #GstStructure
  * @fieldname: the name of the field to remove
- * @...: NULL-terminated list of more fieldnames to remove
+ * @...: %NULL-terminated list of more fieldnames to remove
  *
  * Removes the fields with the given names. If a field does not exist, the
  * argument is ignored.
@@ -986,7 +983,7 @@ gst_structure_remove_fields (GstStructure * structure,
  * gst_structure_remove_fields_valist:
  * @structure: a #GstStructure
  * @fieldname: the name of the field to remove
- * @varargs: NULL-terminated list of more fieldnames to remove
+ * @varargs: %NULL-terminated list of more fieldnames to remove
  *
  * va_list form of gst_structure_remove_fields().
  */
@@ -1104,10 +1101,11 @@ gst_structure_nth_field_name (const GstStructure * structure, guint index)
  * @user_data: (closure): private data
  *
  * Calls the provided function once for each field in the #GstStructure. The
- * function must not modify the fields. Also see gst_structure_map_in_place().
+ * function must not modify the fields. Also see gst_structure_map_in_place()
+ * and gst_structure_filter_and_map_in_place().
  *
- * Returns: TRUE if the supplied function returns TRUE For each of the fields,
- * FALSE otherwise.
+ * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
+ * %FALSE otherwise.
  */
 gboolean
 gst_structure_foreach (const GstStructure * structure,
@@ -1143,8 +1141,8 @@ gst_structure_foreach (const GstStructure * structure,
  * contrast to gst_structure_foreach(), the function may modify but not delete the
  * fields. The structure must be mutable.
  *
- * Returns: TRUE if the supplied function returns TRUE For each of the fields,
- * FALSE otherwise.
+ * Returns: %TRUE if the supplied function returns %TRUE For each of the fields,
+ * %FALSE otherwise.
  */
 gboolean
 gst_structure_map_in_place (GstStructure * structure,
@@ -1171,13 +1169,58 @@ gst_structure_map_in_place (GstStructure * structure,
 }
 
 /**
+ * gst_structure_filter_and_map_in_place:
+ * @structure: a #GstStructure
+ * @func: (scope call): a function to call for each field
+ * @user_data: (closure): private data
+ *
+ * Calls the provided function once for each field in the #GstStructure. In
+ * contrast to gst_structure_foreach(), the function may modify the fields.
+ * In contrast to gst_structure_map_in_place(), the field is removed from
+ * the structure if %FALSE is returned from the function.
+ * The structure must be mutable.
+ *
+ * Since: 1.6
+ */
+void
+gst_structure_filter_and_map_in_place (GstStructure * structure,
+    GstStructureFilterMapFunc func, gpointer user_data)
+{
+  guint i, len;
+  GstStructureField *field;
+  gboolean ret;
+
+  g_return_if_fail (structure != NULL);
+  g_return_if_fail (IS_MUTABLE (structure));
+  g_return_if_fail (func != NULL);
+  len = GST_STRUCTURE_FIELDS (structure)->len;
+
+  for (i = 0; i < len;) {
+    field = GST_STRUCTURE_FIELD (structure, i);
+
+    ret = func (field->name, &field->value, user_data);
+
+    if (!ret) {
+      if (G_IS_VALUE (&field->value)) {
+        g_value_unset (&field->value);
+      }
+      GST_STRUCTURE_FIELDS (structure) =
+          g_array_remove_index (GST_STRUCTURE_FIELDS (structure), i);
+      len = GST_STRUCTURE_FIELDS (structure)->len;
+    } else {
+      i++;
+    }
+  }
+}
+
+/**
  * gst_structure_id_has_field:
  * @structure: a #GstStructure
  * @field: #GQuark of the field name
  *
  * Check if @structure contains a field named @field.
  *
- * Returns: TRUE if the structure contains a field with the given name
+ * Returns: %TRUE if the structure contains a field with the given name
  */
 gboolean
 gst_structure_id_has_field (const GstStructure * structure, GQuark field)
@@ -1199,7 +1242,7 @@ gst_structure_id_has_field (const GstStructure * structure, GQuark field)
  *
  * Check if @structure contains a field named @fieldname.
  *
- * Returns: TRUE if the structure contains a field with the given name
+ * Returns: %TRUE if the structure contains a field with the given name
  */
 gboolean
 gst_structure_has_field (const GstStructure * structure,
@@ -1220,7 +1263,7 @@ gst_structure_has_field (const GstStructure * structure,
  *
  * Check if @structure contains a field named @field and with GType @type.
  *
- * Returns: TRUE if the structure contains a field with the given name and type
+ * Returns: %TRUE if the structure contains a field with the given name and type
  */
 gboolean
 gst_structure_id_has_field_typed (const GstStructure * structure,
@@ -1246,7 +1289,7 @@ gst_structure_id_has_field_typed (const GstStructure * structure,
  *
  * Check if @structure contains a field named @fieldname and with GType @type.
  *
- * Returns: TRUE if the structure contains a field with the given name and type
+ * Returns: %TRUE if the structure contains a field with the given name and type
  */
 gboolean
 gst_structure_has_field_typed (const GstStructure * structure,
@@ -1271,9 +1314,9 @@ gst_structure_has_field_typed (const GstStructure * structure,
  * given field.  Caller is responsible for making sure the field exists
  * and has the correct type.
  *
- * Returns: TRUE if the value could be set correctly. If there was no field
+ * Returns: %TRUE if the value could be set correctly. If there was no field
  * with @fieldname or the existing field did not contain a boolean, this
- * function returns FALSE.
+ * function returns %FALSE.
  */
 gboolean
 gst_structure_get_boolean (const GstStructure * structure,
@@ -1286,9 +1329,7 @@ gst_structure_get_boolean (const GstStructure * structure,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return FALSE;
-  if (!G_VALUE_HOLDS_BOOLEAN (&field->value))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_BOOLEAN)
     return FALSE;
 
   *value = gst_g_value_get_boolean_unchecked (&field->value);
@@ -1322,9 +1363,7 @@ gst_structure_get_int (const GstStructure * structure,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return FALSE;
-  if (!G_VALUE_HOLDS_INT (&field->value))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT)
     return FALSE;
 
   *value = gst_g_value_get_int_unchecked (&field->value);
@@ -1358,9 +1397,7 @@ gst_structure_get_uint (const GstStructure * structure,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return FALSE;
-  if (!G_VALUE_HOLDS_UINT (&field->value))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT)
     return FALSE;
 
   *value = gst_g_value_get_uint_unchecked (&field->value);
@@ -1372,14 +1409,14 @@ gst_structure_get_uint (const GstStructure * structure,
  * gst_structure_get_int64:
  * @structure: a #GstStructure
  * @fieldname: the name of a field
- * @value: (out): a pointer to aint64 to set
+ * @value: (out): a pointer to a #gint64 to set
  *
- * Sets the int64 pointed to by @value corresponding to the value of the
+ * Sets the #gint64 pointed to by @value corresponding to the value of the
  * given field. Caller is responsible for making sure the field exists
  * and has the correct type.
  *
  * Returns: %TRUE if the value could be set correctly. If there was no field
- * with @fieldname or the existing field did not contain aint64, this function
+ * with @fieldname or the existing field did not contain a #gint64, this function
  * returns %FALSE.
  *
  * Since: 1.4
@@ -1396,9 +1433,7 @@ gst_structure_get_int64 (const GstStructure * structure,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return FALSE;
-  if (!G_VALUE_HOLDS_INT64 (&field->value))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_INT64)
     return FALSE;
 
   *value = gst_g_value_get_int64_unchecked (&field->value);
@@ -1410,14 +1445,14 @@ gst_structure_get_int64 (const GstStructure * structure,
  * gst_structure_get_uint64:
  * @structure: a #GstStructure
  * @fieldname: the name of a field
- * @value: (out): a pointer to a uint64 to set
+ * @value: (out): a pointer to a #guint64 to set
  *
- * Sets the uint64 pointed to by @value corresponding to the value of the
+ * Sets the #guint64 pointed to by @value corresponding to the value of the
  * given field. Caller is responsible for making sure the field exists
  * and has the correct type.
  *
  * Returns: %TRUE if the value could be set correctly. If there was no field
- * with @fieldname or the existing field did not contain a uint64, this function
+ * with @fieldname or the existing field did not contain a #guint64, this function
  * returns %FALSE.
  *
  * Since: 1.4
@@ -1434,9 +1469,7 @@ gst_structure_get_uint64 (const GstStructure * structure,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return FALSE;
-  if (!G_VALUE_HOLDS_UINT64 (&field->value))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_UINT64)
     return FALSE;
 
   *value = gst_g_value_get_uint64_unchecked (&field->value);
@@ -1459,9 +1492,9 @@ gst_structure_get_uint64 (const GstStructure * structure,
  * inconsistent with e.g. gst_structure_get_string() which doesn't return a
  * copy of the string).
  *
- * Returns: TRUE if the value could be set correctly. If there was no field
+ * Returns: %TRUE if the value could be set correctly. If there was no field
  * with @fieldname or the existing field did not contain a data, this function
- * returns FALSE.
+ * returns %FALSE.
  */
 gboolean
 gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
@@ -1475,12 +1508,10 @@ gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return FALSE;
-  if (!G_VALUE_HOLDS (&field->value, G_TYPE_DATE))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DATE)
     return FALSE;
 
-  /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
+  /* FIXME: 2.0 g_value_dup_boxed() -> g_value_get_boxed() */
   *value = g_value_dup_boxed (&field->value);
 
   return TRUE;
@@ -1501,9 +1532,9 @@ gst_structure_get_date (const GstStructure * structure, const gchar * fieldname,
  * (note: this is inconsistent with e.g. gst_structure_get_string()
  * which doesn't return a copy of the string).
  *
- * Returns: TRUE if the value could be set correctly. If there was no field
+ * Returns: %TRUE if the value could be set correctly. If there was no field
  * with @fieldname or the existing field did not contain a data, this function
- * returns FALSE.
+ * returns %FALSE.
  */
 gboolean
 gst_structure_get_date_time (const GstStructure * structure,
@@ -1522,7 +1553,7 @@ gst_structure_get_date_time (const GstStructure * structure,
   if (!GST_VALUE_HOLDS_DATE_TIME (&field->value))
     return FALSE;
 
-  /* FIXME: 0.11 g_value_dup_boxed() -> g_value_get_boxed() */
+  /* FIXME 2.0: g_value_dup_boxed() -> g_value_get_boxed() */
   *value = g_value_dup_boxed (&field->value);
 
   return TRUE;
@@ -1538,30 +1569,15 @@ gst_structure_get_date_time (const GstStructure * structure,
  * of the given field.  Caller is responsible for making sure the field exists
  * and has the correct type.
  *
- * Returns: TRUE if the value could be set correctly. If there was no field
+ * Returns: %TRUE if the value could be set correctly. If there was no field
  * with @fieldname or the existing field did not contain a #GstClockTime, this
- * function returns FALSE.
+ * function returns %FALSE.
  */
 gboolean
 gst_structure_get_clock_time (const GstStructure * structure,
     const gchar * fieldname, GstClockTime * value)
 {
-  GstStructureField *field;
-
-  g_return_val_if_fail (structure != NULL, FALSE);
-  g_return_val_if_fail (fieldname != NULL, FALSE);
-  g_return_val_if_fail (value != NULL, FALSE);
-
-  field = gst_structure_get_field (structure, fieldname);
-
-  if (field == NULL)
-    return FALSE;
-  if (!G_VALUE_HOLDS_UINT64 (&field->value))
-    return FALSE;
-
-  *value = gst_g_value_get_uint64_unchecked (&field->value);
-
-  return TRUE;
+  return gst_structure_get_uint64 (structure, fieldname, value);
 }
 
 /**
@@ -1574,9 +1590,9 @@ gst_structure_get_clock_time (const GstStructure * structure,
  * given field.  Caller is responsible for making sure the field exists
  * and has the correct type.
  *
- * Returns: TRUE if the value could be set correctly. If there was no field
+ * Returns: %TRUE if the value could be set correctly. If there was no field
  * with @fieldname or the existing field did not contain a double, this
- * function returns FALSE.
+ * function returns %FALSE.
  */
 gboolean
 gst_structure_get_double (const GstStructure * structure,
@@ -1590,9 +1606,7 @@ gst_structure_get_double (const GstStructure * structure,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return FALSE;
-  if (!G_VALUE_HOLDS_DOUBLE (&field->value))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_DOUBLE)
     return FALSE;
 
   *value = gst_g_value_get_double_unchecked (&field->value);
@@ -1612,8 +1626,8 @@ gst_structure_get_double (const GstStructure * structure,
  * The string should not be modified, and remains valid until the next
  * call to a gst_structure_*() function with the given structure.
  *
- * Returns: a pointer to the string or NULL when the field did not exist
- * or did not contain a string.
+ * Returns: (nullable): a pointer to the string or %NULL when the
+ * field did not exist or did not contain a string.
  */
 const gchar *
 gst_structure_get_string (const GstStructure * structure,
@@ -1626,9 +1640,7 @@ gst_structure_get_string (const GstStructure * structure,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return NULL;
-  if (!G_VALUE_HOLDS_STRING (&field->value))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != G_TYPE_STRING)
     return NULL;
 
   return gst_g_value_get_string_unchecked (&field->value);
@@ -1645,9 +1657,9 @@ gst_structure_get_string (const GstStructure * structure,
  * given field.  Caller is responsible for making sure the field exists,
  * has the correct type and that the enumtype is correct.
  *
- * Returns: TRUE if the value could be set correctly. If there was no field
+ * Returns: %TRUE if the value could be set correctly. If there was no field
  * with @fieldname or the existing field did not contain an enum of the given
- * type, this function returns FALSE.
+ * type, this function returns %FALSE.
  */
 gboolean
 gst_structure_get_enum (const GstStructure * structure,
@@ -1683,9 +1695,9 @@ gst_structure_get_enum (const GstStructure * structure,
  * corresponding to the value of the given field.  Caller is responsible
  * for making sure the field exists and has the correct type.
  *
- * Returns: TRUE if the values could be set correctly. If there was no field
+ * Returns: %TRUE if the values could be set correctly. If there was no field
  * with @fieldname or the existing field did not contain a GstFraction, this
- * function returns FALSE.
+ * function returns %FALSE.
  */
 gboolean
 gst_structure_get_fraction (const GstStructure * structure,
@@ -1700,9 +1712,7 @@ gst_structure_get_fraction (const GstStructure * structure,
 
   field = gst_structure_get_field (structure, fieldname);
 
-  if (field == NULL)
-    return FALSE;
-  if (!GST_VALUE_HOLDS_FRACTION (&field->value))
+  if (field == NULL || G_VALUE_TYPE (&field->value) != GST_TYPE_FRACTION)
     return FALSE;
 
   *value_numerator = gst_value_get_fraction_numerator (&field->value);
@@ -1711,6 +1721,44 @@ gst_structure_get_fraction (const GstStructure * structure,
   return TRUE;
 }
 
+/**
+ * gst_structure_get_flagset:
+ * @structure: a #GstStructure
+ * @fieldname: the name of a field
+ * @value_flags: (out) (allow-none): a pointer to a guint for the flags field
+ * @value_mask: (out) (allow-none): a pointer to a guint for the mask field
+ *
+ * Read the GstFlagSet flags and mask out of the structure into the
+ * provided pointers.
+ *
+ * Returns: %TRUE if the values could be set correctly. If there was no field
+ * with @fieldname or the existing field did not contain a GstFlagSet, this
+ * function returns %FALSE.
+ *
+ * Since: 1.6
+ */
+gboolean
+gst_structure_get_flagset (const GstStructure * structure,
+    const gchar * fieldname, guint * value_flags, guint * value_mask)
+{
+  GstStructureField *field;
+
+  g_return_val_if_fail (structure != NULL, FALSE);
+  g_return_val_if_fail (fieldname != NULL, FALSE);
+
+  field = gst_structure_get_field (structure, fieldname);
+
+  if (field == NULL || !GST_VALUE_HOLDS_FLAG_SET (&field->value))
+    return FALSE;
+
+  if (value_flags)
+    *value_flags = gst_value_get_flagset_flags (&field->value);
+  if (value_mask)
+    *value_mask = gst_value_get_flagset_mask (&field->value);
+
+  return TRUE;
+}
+
 typedef struct _GstStructureAbbreviation
 {
   const gchar *type_name;
@@ -1773,6 +1821,8 @@ gst_structure_get_abbrs (gint * n_abbrs)
       {"sample", GST_TYPE_SAMPLE}
       ,
       {"taglist", GST_TYPE_TAG_LIST}
+      ,
+      {"type", G_TYPE_GTYPE}
     };
     _num = G_N_ELEMENTS (dyn_abbrs);
     /* permanently allocate and copy the array now */
@@ -1793,6 +1843,7 @@ gst_structure_gtype_from_abbr (const char *type_name)
   int i;
   GstStructureAbbreviation *abbrs;
   gint n_abbrs;
+  GType ret;
 
   g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
 
@@ -1805,7 +1856,11 @@ gst_structure_gtype_from_abbr (const char *type_name)
   }
 
   /* this is the fallback */
-  return g_type_from_name (type_name);
+  ret = g_type_from_name (type_name);
+  /* If not found, try it as a dynamic type */
+  if (G_UNLIKELY (ret == 0))
+    ret = gst_dynamic_type_factory_load (type_name);
+  return ret;
 }
 
 static const char *
@@ -1829,7 +1884,7 @@ gst_structure_to_abbr (GType type)
 }
 
 static GType
-gst_structure_value_get_generic_type (GValue * val)
+gst_structure_value_get_generic_type (const GValue * val)
 {
   if (G_VALUE_TYPE (val) == GST_TYPE_LIST
       || G_VALUE_TYPE (val) == GST_TYPE_ARRAY) {
@@ -1879,14 +1934,69 @@ priv_gst_structure_append_to_gstring (const GstStructure * structure,
     g_string_append_len (s, "=(", 2);
     g_string_append (s, gst_structure_to_abbr (type));
     g_string_append_c (s, ')');
-    g_string_append (s, t == NULL ? "NULL" : t);
-    g_free (t);
+    if (t) {
+      g_string_append (s, t);
+      g_free (t);
+    } else {
+      GST_WARNING ("No value transform to serialize field '%s' of type '%s'",
+          g_quark_to_string (field->name), gst_structure_to_abbr (type));
+      g_string_append (s, "NULL");
+    }
   }
 
   g_string_append_c (s, ';');
   return TRUE;
 }
 
+gboolean
+priv__gst_structure_append_template_to_gstring (GQuark field_id,
+    const GValue * value, gpointer user_data)
+{
+  GType type = gst_structure_value_get_generic_type (value);
+  GString *s = (GString *) user_data;
+
+  g_string_append_len (s, ", ", 2);
+  /* FIXME: do we need to escape fieldnames? */
+  g_string_append (s, g_quark_to_string (field_id));
+  g_string_append_len (s, "=(", 2);
+  g_string_append (s, gst_structure_to_abbr (type));
+  g_string_append_c (s, ')');
+
+  //TODO(ensonic): table like GstStructureAbbreviation (or extend it)
+  if (type == G_TYPE_INT) {
+    g_string_append_len (s, "%i", 2);
+  } else if (type == G_TYPE_UINT) {
+    g_string_append_len (s, "%u", 2);
+  } else if (type == G_TYPE_FLOAT) {
+    g_string_append_len (s, "%f", 2);
+  } else if (type == G_TYPE_DOUBLE) {
+    g_string_append_len (s, "%lf", 3);
+  } else if (type == G_TYPE_STRING) {
+    g_string_append_len (s, "%s", 2);
+  } else if (type == G_TYPE_BOOLEAN) {
+    /* we normally store this as a string, but can parse it also from an int */
+    g_string_append_len (s, "%i", 2);
+  } else if (type == G_TYPE_INT64) {
+    g_string_append (s, "%" G_GINT64_FORMAT);
+  } else if (type == G_TYPE_UINT64) {
+    g_string_append (s, "%" G_GUINT64_FORMAT);
+  } else if (type == GST_TYPE_STRUCTURE) {
+    g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
+  } else if (g_type_is_a (type, G_TYPE_ENUM)
+      || g_type_is_a (type, G_TYPE_FLAGS)) {
+    g_string_append_len (s, "%i", 2);
+  } else if (type == G_TYPE_GTYPE) {
+    g_string_append_len (s, "%s", 2);
+  } else if (type == G_TYPE_POINTER) {
+    g_string_append_len (s, "%p", 2);
+  } else {
+    GST_WARNING ("unhandled type: %s", g_type_name (type));
+    g_string_append (s, "%" GST_WRAPPED_PTR_FORMAT);
+  }
+
+  return TRUE;
+}
+
 /**
  * gst_structure_to_string:
  * @structure: a #GstStructure
@@ -1894,7 +2004,7 @@ priv_gst_structure_append_to_gstring (const GstStructure * structure,
  * Converts @structure to a human-readable string representation.
  *
  * For debugging purposes its easier to do something like this:
- * |[
+ * |[<!-- language="C" -->
  * GST_LOG ("structure is %" GST_PTR_FORMAT, structure);
  * ]|
  * This prints the structure in human readable form.
@@ -1959,8 +2069,11 @@ gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
     while (*s != '"') {
       if (G_UNLIKELY (*s == 0))
         return FALSE;
-      if (G_UNLIKELY (*s == '\\'))
+      if (G_UNLIKELY (*s == '\\')) {
         s++;
+        if (G_UNLIKELY (*s == 0))
+          return FALSE;
+      }
       *w = *s;
       w++;
       s++;
@@ -1972,8 +2085,11 @@ gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
     while (*s != '"') {
       if (G_UNLIKELY (*s == 0))
         return FALSE;
-      if (G_UNLIKELY (*s == '\\'))
+      if (G_UNLIKELY (*s == '\\')) {
         s++;
+        if (G_UNLIKELY (*s == 0))
+          return FALSE;
+      }
       s++;
     }
     s++;
@@ -2001,7 +2117,7 @@ gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
   s++;
 
   ret = gst_structure_parse_value (s, &s, &value1, type);
-  if (ret == FALSE)
+  if (!ret)
     return FALSE;
 
   while (g_ascii_isspace (*s))
@@ -2015,7 +2131,7 @@ gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
     s++;
 
   ret = gst_structure_parse_value (s, &s, &value2, type);
-  if (ret == FALSE)
+  if (!ret)
     return FALSE;
 
   while (g_ascii_isspace (*s))
@@ -2031,7 +2147,7 @@ gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
         s++;
 
       ret = gst_structure_parse_value (s, &s, &value3, type);
-      if (ret == FALSE)
+      if (!ret)
         return FALSE;
 
       while (g_ascii_isspace (*s))
@@ -2115,7 +2231,7 @@ gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
   }
 
   ret = gst_structure_parse_value (s, &s, &list_value, type);
-  if (ret == FALSE)
+  if (!ret)
     return FALSE;
 
   g_array_append_val (array, list_value);
@@ -2133,7 +2249,7 @@ gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
 
     memset (&list_value, 0, sizeof (list_value));
     ret = gst_structure_parse_value (s, &s, &list_value, type);
-    if (ret == FALSE)
+    if (!ret)
       return FALSE;
 
     g_array_append_val (array, list_value);
@@ -2281,8 +2397,8 @@ gst_structure_parse_value (gchar * str,
 
     if (G_UNLIKELY (type == G_TYPE_INVALID)) {
       GType try_types[] =
-          { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, G_TYPE_BOOLEAN,
-        G_TYPE_STRING
+          { G_TYPE_INT, G_TYPE_DOUBLE, GST_TYPE_FRACTION, GST_TYPE_FLAG_SET,
+        G_TYPE_BOOLEAN, G_TYPE_STRING
       };
       int i;
 
@@ -2397,7 +2513,7 @@ priv_gst_structure_parse_fields (gchar * str, gchar ** end,
  * @string: a string representation of a #GstStructure
  *
  * Creates a #GstStructure from a string representation.
- * If end is not NULL, a pointer to the place inside the given string
+ * If end is not %NULL, a pointer to the place inside the given string
  * where parsing ended will be returned.
  *
  * The current implementation of serialization will lead to unexpected results
@@ -2405,8 +2521,9 @@ priv_gst_structure_parse_fields (gchar * str, gchar ** end,
  *
  * Free-function: gst_structure_free
  *
- * Returns: (transfer full): a new #GstStructure or NULL when the string could
- *     not be parsed. Free with gst_structure_free() after use.
+ * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
+ *     when the string could not be parsed. Free with
+ *     gst_structure_free() after use.
  *
  * Since: 1.2
  */
@@ -2422,13 +2539,14 @@ gst_structure_new_from_string (const gchar * string)
  * @end: (out) (allow-none) (transfer none) (skip): pointer to store the end of the string in.
  *
  * Creates a #GstStructure from a string representation.
- * If end is not NULL, a pointer to the place inside the given string
+ * If end is not %NULL, a pointer to the place inside the given string
  * where parsing ended will be returned.
  *
  * Free-function: gst_structure_free
  *
- * Returns: (transfer full): a new #GstStructure or NULL when the string could
- *     not be parsed. Free with gst_structure_free() after use.
+ * Returns: (transfer full) (nullable): a new #GstStructure or %NULL
+ *     when the string could not be parsed. Free with
+ *     gst_structure_free() after use.
  */
 GstStructure *
 gst_structure_from_string (const gchar * string, gchar ** end)
@@ -2505,7 +2623,7 @@ gst_structure_copy_conditional (const GstStructure * structure)
  * Fixates a #GstStructure by changing the given field to the nearest
  * integer to @target that is a subset of the existing field.
  *
- * Returns: TRUE if the structure could be fixated
+ * Returns: %TRUE if the structure could be fixated
  */
 gboolean
 gst_structure_fixate_field_nearest_int (GstStructure * structure,
@@ -2569,7 +2687,7 @@ gst_structure_fixate_field_nearest_int (GstStructure * structure,
  * Fixates a #GstStructure by changing the given field to the nearest
  * double to @target that is a subset of the existing field.
  *
- * Returns: TRUE if the structure could be fixated
+ * Returns: %TRUE if the structure could be fixated
  */
 gboolean
 gst_structure_fixate_field_nearest_double (GstStructure * structure,
@@ -2634,7 +2752,7 @@ gst_structure_fixate_field_nearest_double (GstStructure * structure,
  * Fixates a #GstStructure by changing the given @field_name field to the given
  * @target boolean if that field is not fixed yet.
  *
- * Returns: TRUE if the structure could be fixated
+ * Returns: %TRUE if the structure could be fixated
  */
 gboolean
 gst_structure_fixate_field_boolean (GstStructure * structure,
@@ -2687,7 +2805,7 @@ gst_structure_fixate_field_boolean (GstStructure * structure,
  * Fixates a #GstStructure by changing the given @field_name field to the given
  * @target string if that field is not fixed yet.
  *
- * Returns: TRUE if the structure could be fixated
+ * Returns: %TRUE if the structure could be fixated
  */
 gboolean
 gst_structure_fixate_field_string (GstStructure * structure,
@@ -2742,7 +2860,7 @@ gst_structure_fixate_field_string (GstStructure * structure,
  * fraction to @target_numerator/@target_denominator that is a subset
  * of the existing field.
  *
- * Returns: TRUE if the structure could be fixated
+ * Returns: %TRUE if the structure could be fixated
  */
 gboolean
 gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
@@ -2753,6 +2871,7 @@ gst_structure_fixate_field_nearest_fraction (GstStructure * structure,
 
   g_return_val_if_fail (gst_structure_has_field (structure, field_name), FALSE);
   g_return_val_if_fail (IS_MUTABLE (structure), FALSE);
+  g_return_val_if_fail (target_denominator != 0, FALSE);
 
   value = gst_structure_get_value (structure, field_name);
 
@@ -2843,7 +2962,7 @@ default_fixate (GQuark field_id, const GValue * value, gpointer data)
  *
  * Fixates a #GstStructure by changing the given field with its fixated value.
  *
- * Returns: TRUE if the structure field could be fixated
+ * Returns: %TRUE if the structure field could be fixated
  */
 gboolean
 gst_structure_fixate_field (GstStructure * structure, const char *field_name)
@@ -2861,7 +2980,7 @@ gst_structure_fixate_field (GstStructure * structure, const char *field_name)
 
 /* our very own version of G_VALUE_LCOPY that allows NULL return locations
  * (useful for message parsing functions where the return location is user
- * supplied and the user may pass NULL if the value isn't of interest) */
+ * supplied and the user may pass %NULL if the value isn't of interest) */
 #define GST_VALUE_LCOPY(value, var_args, flags, __error, fieldname)           \
 G_STMT_START {                                                                \
   const GValue *_value = (value);                                             \
@@ -2895,7 +3014,7 @@ G_STMT_START {                                                                \
  * valist-variant of gst_structure_get(). Look at the documentation of
  * gst_structure_get() for more details.
  *
- * Returns: TRUE, or FALSE if there was a problem reading any of the fields
+ * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
  */
 gboolean
 gst_structure_get_valist (const GstStructure * structure,
@@ -2962,7 +3081,7 @@ wrong_type:
  * valist-variant of gst_structure_id_get(). Look at the documentation of
  * gst_structure_id_get() for more details.
  *
- * Returns: TRUE, or FALSE if there was a problem reading any of the fields
+ * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
  */
 gboolean
 gst_structure_id_get_valist (const GstStructure * structure,
@@ -3029,16 +3148,16 @@ wrong_type:
  * Parses the variable arguments and reads fields from @structure accordingly.
  * Variable arguments should be in the form field name, field type
  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
- * The last variable argument should be NULL.
+ * The last variable argument should be %NULL.
  *
  * For refcounted (mini)objects you will receive a new reference which
  * you must release with a suitable _unref() when no longer needed. For
  * strings and boxed types you will receive a copy which you will need to
  * release with either g_free() or the suitable function for the boxed type.
  *
- * Returns: FALSE if there was a problem reading any of the fields (e.g.
+ * Returns: %FALSE if there was a problem reading any of the fields (e.g.
  *     because the field requested did not exist, or was of a type other
- *     than the type specified), otherwise TRUE.
+ *     than the type specified), otherwise %TRUE.
  */
 gboolean
 gst_structure_get (const GstStructure * structure, const char *first_fieldname,
@@ -3066,9 +3185,9 @@ gst_structure_get (const GstStructure * structure, const char *first_fieldname,
  * Parses the variable arguments and reads fields from @structure accordingly.
  * Variable arguments should be in the form field id quark, field type
  * (as a GType), pointer(s) to a variable(s) to hold the return value(s).
- * The last variable argument should be NULL (technically it should be a
- * 0 quark, but we require NULL so compilers that support it can check for
- * the NULL terminator and warn if it's not there).
+ * The last variable argument should be %NULL (technically it should be a
+ * 0 quark, but we require %NULL so compilers that support it can check for
+ * the %NULL terminator and warn if it's not there).
  *
  * This function is just like gst_structure_get() only that it is slightly
  * more efficient since it saves the string-to-quark lookup in the global
@@ -3079,9 +3198,9 @@ gst_structure_get (const GstStructure * structure, const char *first_fieldname,
  * strings and boxed types you will receive a copy which you will need to
  * release with either g_free() or the suitable function for the boxed type.
  *
- * Returns: FALSE if there was a problem reading any of the fields (e.g.
+ * Returns: %FALSE if there was a problem reading any of the fields (e.g.
  *     because the field requested did not exist, or was of a type other
- *     than the type specified), otherwise TRUE.
+ *     than the type specified), otherwise %TRUE.
  */
 gboolean
 gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
@@ -3123,7 +3242,7 @@ gst_structure_is_equal_foreach (GQuark field_id, const GValue * val2,
  *
  * Tests if the two #GstStructure are equal.
  *
- * Returns: TRUE if the two structures have the same name and field.
+ * Returns: %TRUE if the two structures have the same name and field.
  **/
 gboolean
 gst_structure_is_equal (const GstStructure * structure1,