*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
*/
/**
* SECTION:gststructure
+ * @title: GstStructure
* @short_description: Generic structure containing fields of names and values
* @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery
*
* as GQuarks and the values can be of any GType.
*
* In addition to the key/value pairs, a #GstStructure also has a name. The name
- * starts with a letter and can be folled by letters, numbers and any of "/-_.:".
+ * starts with a letter and can be filled by letters, numbers and any of "/-_.:".
*
* #GstStructure is used by various GStreamer subsystems to store information
* in a flexible and extensible way. A #GstStructure does not have a refcount
- * because it usually is part of a higher level object such as #GstCaps. It
- * provides a means to enforce mutability using the refcount of the parent
- * with the gst_structure_set_parent_refcount() method.
+ * because it usually is part of a higher level object such as #GstCaps,
+ * #GstMessage, #GstEvent, #GstQuery. It provides a means to enforce mutability
+ * using the refcount of the parent with the gst_structure_set_parent_refcount()
+ * method.
*
* A #GstStructure can be created with gst_structure_new_empty() or
* gst_structure_new(), which both take a name and an optional set of
* gst_structure_remove_fields().
*
* Strings in structures must be ASCII or UTF-8 encoded. Other encodings are
- * not allowed. Strings must not be empty either, but may be NULL.
+ * not allowed. Strings may be %NULL however.
*
- * Last reviewed on 2009-06-08 (0.10.23)
+ * 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
+/* FIXME 2.0: suppress warnings for deprecated API such as GValueArray
+ * with newer GLib versions (>= 2.31.0) */
+#define GLIB_DISABLE_DEPRECATION_WARNINGS
+
#include <string.h>
#include "gst_private.h"
#include <gst/gst.h>
#include <gobject/gvaluecollector.h>
+GST_DEBUG_CATEGORY_STATIC (gst_structure_debug);
+#define GST_CAT_DEFAULT gst_structure_debug
+
typedef struct _GstStructureField GstStructureField;
struct _GstStructureField
GValue * dest_value);
static GstStructure *gst_structure_copy_conditional (const GstStructure *
structure);
-static gboolean gst_structure_parse_value (gchar * str, gchar ** after,
- GValue * value, GType default_type);
-static gboolean gst_structure_parse_simple_string (gchar * s, gchar ** end);
GType _gst_structure_type = 0;
g_value_register_transform_func (_gst_structure_type, G_TYPE_STRING,
gst_structure_transform_to_string);
+
+ GST_DEBUG_CATEGORY_INIT (gst_structure_debug, "structure", 0,
+ "GstStructure debug");
}
static GstStructure *
* 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
*
*
* Free-function: gst_structure_free
*
- * Returns: (transfer none): a new #GstStructure.
+ * Returns: (transfer full): a new #GstStructure.
*/
GstStructure *
gst_structure_copy (const GstStructure * structure)
}
/**
+ * gst_clear_structure: (skip)
+ * @structure_ptr: a pointer to a #GstStructure reference
+ *
+ * Clears a reference to a #GstStructure.
+ *
+ * @structure_ptr must not be %NULL.
+ *
+ * If the reference is %NULL then this function does nothing.
+ * Otherwise, the structure is free'd using gst_structure_free() and the
+ * pointer is set to %NULL.
+ *
+ * A macro is also included that allows this function to be used without
+ * pointer casts.
+ *
+ * Since: 1.16
+ **/
+#undef gst_clear_structure
+void
+gst_clear_structure (GstStructure ** structure_ptr)
+{
+ g_clear_pointer (structure_ptr, gst_structure_free);
+}
+
+/**
* gst_structure_get_name:
* @structure: a #GstStructure
*
*
* 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)
* Sets the field with the given GQuark @field to @value. If the field
* does not exist, it is created. If the field exists, the previous
* value is replaced and freed.
- *
- * Since: 0.10.31
*/
void
gst_structure_id_take_value (GstStructure * structure, GQuark field,
* Sets the field with the given name @field to @value. If the field
* does not exist, it is created. If the field exists, the previous
* value is replaced and freed. The function will take ownership of @value.
- *
- * Since: 0.10.31
*/
void
gst_structure_take_value (GstStructure * structure, const gchar * fieldname,
G_VALUE_COLLECT_INIT (&field.value, type, varargs, 0, &err);
if (G_UNLIKELY (err)) {
g_critical ("%s", err);
+ g_free (err);
return;
}
gst_structure_set_field (structure, &field);
* @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, ...)
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);
+ g_free (err);
return;
}
gst_structure_set_field (structure, &field);
* 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.
- *
- * Since: 0.10.10
+ * The last variable argument must be %NULL.
*/
void
gst_structure_id_set (GstStructure * structure, GQuark field, ...)
* @varargs: variable arguments
*
* va_list form of gst_structure_id_set().
- *
- * Since: 0.10.10
*/
void
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
*
* Returns: (transfer full): a new #GstStructure
- *
- * Since: 0.10.24
*/
GstStructure *
gst_structure_new_id (GQuark name_quark, GQuark field_quark, ...)
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);
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);
*
* Get the value of the field with name @fieldname.
*
- * Returns: the #GValue corresponding to the field with the given name.
+ * Returns: (nullable): the #GValue corresponding to the field with the given
+ * name.
*/
const GValue *
gst_structure_get_value (const GstStructure * structure,
*
* Get the value of the field with GQuark @field.
*
- * Returns: the #GValue corresponding to the field with the given name
- * identifier.
+ * Returns: (nullable): the #GValue corresponding to the field with the given
+ * name identifier.
*/
const GValue *
gst_structure_id_get_value (const GstStructure * structure, GQuark field)
* 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.
* 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().
*/
* @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,
* 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,
}
/**
+ * 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
- *
- * Since: 0.10.26
+ * Returns: %TRUE if the structure contains a field with the given name
*/
gboolean
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,
*
* 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
- *
- * Since: 0.10.26
+ * Returns: %TRUE if the structure contains a field with the given name and type
*/
gboolean
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,
* 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,
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);
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);
* Returns: %TRUE if the value could be set correctly. If there was no field
* with @fieldname or the existing field did not contain a uint, this function
* returns %FALSE.
- *
- * Since: 0.10.15
*/
gboolean
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);
}
/**
+ * gst_structure_get_int64:
+ * @structure: a #GstStructure
+ * @fieldname: the name of a field
+ * @value: (out): a pointer to a #gint64 to set
+ *
+ * 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 a #gint64, this function
+ * returns %FALSE.
+ *
+ * Since: 1.4
+ */
+gboolean
+gst_structure_get_int64 (const GstStructure * structure,
+ const gchar * fieldname, gint64 * 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 || G_VALUE_TYPE (&field->value) != G_TYPE_INT64)
+ return FALSE;
+
+ *value = gst_g_value_get_int64_unchecked (&field->value);
+
+ return TRUE;
+}
+
+/**
+ * gst_structure_get_uint64:
+ * @structure: a #GstStructure
+ * @fieldname: the name of a field
+ * @value: (out): a pointer to a #guint64 to set
+ *
+ * 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 #guint64, this function
+ * returns %FALSE.
+ *
+ * Since: 1.4
+ */
+gboolean
+gst_structure_get_uint64 (const GstStructure * structure,
+ const gchar * fieldname, guint64 * 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 || G_VALUE_TYPE (&field->value) != G_TYPE_UINT64)
+ return FALSE;
+
+ *value = gst_g_value_get_uint64_unchecked (&field->value);
+
+ return TRUE;
+}
+
+/**
* gst_structure_get_date:
* @structure: a #GstStructure
* @fieldname: the name of a field
* 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,
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;
* (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.
- *
- * Since: 0.10.31
+ * returns %FALSE.
*/
gboolean
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;
* 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);
}
/**
* 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,
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);
* 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,
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);
* 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,
* 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,
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);
return TRUE;
}
-typedef struct _GstStructureAbbreviation
-{
- const gchar *type_name;
- GType type;
-}
-GstStructureAbbreviation;
-
-/* return a copy of an array of GstStructureAbbreviation containing all the
- * known type_string, GType maps, including abbreviations for common types */
-static GstStructureAbbreviation *
-gst_structure_get_abbrs (gint * n_abbrs)
-{
- static GstStructureAbbreviation *abbrs = NULL;
- static volatile gsize num = 0;
-
- if (g_once_init_enter (&num)) {
- /* dynamically generate the array */
- gsize _num;
- GstStructureAbbreviation dyn_abbrs[] = {
- {"int", G_TYPE_INT}
- ,
- {"i", G_TYPE_INT}
- ,
- {"uint", G_TYPE_UINT}
- ,
- {"u", G_TYPE_UINT}
- ,
- {"float", G_TYPE_FLOAT}
- ,
- {"f", G_TYPE_FLOAT}
- ,
- {"double", G_TYPE_DOUBLE}
- ,
- {"d", G_TYPE_DOUBLE}
- ,
- {"buffer", GST_TYPE_BUFFER}
- ,
- {"fraction", GST_TYPE_FRACTION}
- ,
- {"boolean", G_TYPE_BOOLEAN}
- ,
- {"bool", G_TYPE_BOOLEAN}
- ,
- {"b", G_TYPE_BOOLEAN}
- ,
- {"string", G_TYPE_STRING}
- ,
- {"str", G_TYPE_STRING}
- ,
- {"s", G_TYPE_STRING}
- ,
- {"structure", GST_TYPE_STRUCTURE}
- ,
- {"date", G_TYPE_DATE}
- ,
- {"datetime", GST_TYPE_DATE_TIME}
- ,
- {"bitmask", GST_TYPE_BITMASK}
- };
- _num = G_N_ELEMENTS (dyn_abbrs);
- /* permanently allocate and copy the array now */
- abbrs = g_new0 (GstStructureAbbreviation, _num);
- memcpy (abbrs, dyn_abbrs, sizeof (GstStructureAbbreviation) * _num);
- g_once_init_leave (&num, _num);
- }
- *n_abbrs = num;
-
- return abbrs;
-}
-
-/* given a type_name that could be a type abbreviation or a registered GType,
- * return a matching GType */
-static GType
-gst_structure_gtype_from_abbr (const char *type_name)
+/**
+ * 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)
{
- int i;
- GstStructureAbbreviation *abbrs;
- gint n_abbrs;
-
- g_return_val_if_fail (type_name != NULL, G_TYPE_INVALID);
-
- abbrs = gst_structure_get_abbrs (&n_abbrs);
-
- for (i = 0; i < n_abbrs; i++) {
- if (strcmp (type_name, abbrs[i].type_name) == 0) {
- return abbrs[i].type;
- }
- }
-
- /* this is the fallback */
- return g_type_from_name (type_name);
-}
+ GstStructureField *field;
-static const char *
-gst_structure_to_abbr (GType type)
-{
- int i;
- GstStructureAbbreviation *abbrs;
- gint n_abbrs;
+ g_return_val_if_fail (structure != NULL, FALSE);
+ g_return_val_if_fail (fieldname != NULL, FALSE);
- g_return_val_if_fail (type != G_TYPE_INVALID, NULL);
+ field = gst_structure_get_field (structure, fieldname);
- abbrs = gst_structure_get_abbrs (&n_abbrs);
+ if (field == NULL || !GST_VALUE_HOLDS_FLAG_SET (&field->value))
+ return FALSE;
- for (i = 0; i < n_abbrs; i++) {
- if (type == abbrs[i].type) {
- return abbrs[i].type_name;
- }
- }
+ 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 g_type_name (type);
+ return TRUE;
}
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) {
g_return_val_if_fail (s != NULL, FALSE);
- g_string_append (s, g_quark_to_string (structure->name));
len = GST_STRUCTURE_FIELDS (structure)->len;
for (i = 0; i < len; i++) {
char *t;
field = GST_STRUCTURE_FIELD (structure, i);
- t = gst_value_serialize (&field->value);
+ if (G_VALUE_TYPE (&field->value) == GST_TYPE_ARRAY) {
+ t = _priv_gst_value_serialize_any_list (&field->value, "< ", " >", FALSE);
+ } else if (G_VALUE_TYPE (&field->value) == GST_TYPE_LIST) {
+ t = _priv_gst_value_serialize_any_list (&field->value, "{ ", " }", FALSE);
+ } else {
+ t = gst_value_serialize (&field->value);
+ }
+
type = gst_structure_value_get_generic_type (&field->value);
g_string_append_len (s, ", ", 2);
/* FIXME: do we need to escape fieldnames? */
g_string_append (s, g_quark_to_string (field->name));
g_string_append_len (s, "=(", 2);
- g_string_append (s, gst_structure_to_abbr (type));
+ g_string_append (s, _priv_gst_value_gtype_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 if (G_TYPE_CHECK_VALUE_TYPE (&field->value, G_TYPE_POINTER)) {
+ gpointer ptr = g_value_get_pointer (&field->value);
+
+ if (!ptr)
+ g_string_append (s, "NULL");
+ else
+ g_string_append_printf (s, "%p", ptr);
+ } else {
+ if (!G_TYPE_CHECK_VALUE_TYPE (&field->value, G_TYPE_STRING))
+ GST_WARNING ("No value transform to serialize field '%s' of type '%s'",
+ g_quark_to_string (field->name),
+ _priv_gst_value_gtype_to_abbr (type));
+ /* TODO(ensonic): don't print NULL if field->value is not empty */
+ 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, _priv_gst_value_gtype_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
* 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 readble form.
+ * This prints the structure in human readable form.
+ *
+ * The current implementation of serialization will lead to unexpected results
+ * when there are nested #GstCaps / #GstStructure deeper than one level.
*
* Free-function: g_free
*
- * Returns: (transfer full)L a pointer to string allocated by g_malloc().
+ * Returns: (transfer full): a pointer to string allocated by g_malloc().
* g_free() after usage.
*/
gchar *
/* we estimate a minimum size based on the number of fields in order to
* avoid unnecessary reallocs within GString */
s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
+ g_string_append (s, g_quark_to_string (structure->name));
priv_gst_structure_append_to_gstring (structure, s);
return g_string_free (s, FALSE);
}
-/*
- * r will still point to the string. if end == next, the string will not be
- * null-terminated. In all other cases it will be.
- * end = pointer to char behind end of string, next = pointer to start of
- * unread data.
- * THIS FUNCTION MODIFIES THE STRING AND DETECTS INSIDE A NONTERMINATED STRING
- */
static gboolean
-gst_structure_parse_string (gchar * s, gchar ** end, gchar ** next,
- gboolean unescape)
+gst_structure_parse_field (gchar * str,
+ gchar ** after, GstStructureField * field)
{
- gchar *w;
-
- if (*s == 0)
- return FALSE;
-
- if (*s != '"') {
- int ret;
+ gchar *name;
+ gchar *name_end;
+ gchar *s;
+ gchar c;
- ret = gst_structure_parse_simple_string (s, end);
- *next = *end;
+ s = str;
- return ret;
+ while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
+ s++;
+ name = s;
+ if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &name_end))) {
+ GST_WARNING ("failed to parse simple string, str=%s", str);
+ return FALSE;
}
- if (unescape) {
- w = s;
- s++;
- while (*s != '"') {
- if (G_UNLIKELY (*s == 0))
- return FALSE;
- if (G_UNLIKELY (*s == '\\'))
- s++;
- *w = *s;
- w++;
- s++;
- }
- s++;
- } else {
- /* Find the closing quotes */
- s++;
- while (*s != '"') {
- if (G_UNLIKELY (*s == 0))
- return FALSE;
- if (G_UNLIKELY (*s == '\\'))
- s++;
- s++;
- }
+ s = name_end;
+ while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
s++;
- w = s;
+
+ if (G_UNLIKELY (*s != '=')) {
+ GST_WARNING ("missing assignment operator in the field, str=%s", str);
+ return FALSE;
}
+ s++;
- *end = w;
- *next = s;
+ c = *name_end;
+ *name_end = '\0';
+ field->name = g_quark_from_string (name);
+ GST_DEBUG ("trying field name '%s'", name);
+ *name_end = c;
+
+ if (G_UNLIKELY (!_priv_gst_value_parse_value (s, &s, &field->value,
+ G_TYPE_INVALID))) {
+ GST_WARNING ("failed to parse value %s", str);
+ return FALSE;
+ }
+ *after = s;
return TRUE;
}
-static gboolean
-gst_structure_parse_range (gchar * s, gchar ** after, GValue * value,
- GType type)
+gboolean
+priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end,
+ gchar ** next)
{
- GValue value1 = { 0 };
- GValue value2 = { 0 };
- GValue value3 = { 0 };
- GType range_type;
- gboolean ret, have_step = FALSE;
-
- if (*s != '[')
- return FALSE;
- s++;
+ char *w;
+ char *r;
- ret = gst_structure_parse_value (s, &s, &value1, type);
- if (ret == FALSE)
- return FALSE;
+ r = str;
- while (g_ascii_isspace (*s))
- s++;
-
- if (*s != ',')
- return FALSE;
- s++;
+ /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
+ while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
+ && g_ascii_isspace (r[1]))))
+ r++;
- while (g_ascii_isspace (*s))
- s++;
+ *start = r;
- ret = gst_structure_parse_value (s, &s, &value2, type);
- if (ret == FALSE)
+ if (G_UNLIKELY (!_priv_gst_value_parse_string (r, &w, &r, TRUE))) {
+ GST_WARNING ("Failed to parse structure string '%s'", str);
return FALSE;
-
- while (g_ascii_isspace (*s))
- s++;
-
- /* optional step for int and int64 */
- if (G_VALUE_TYPE (&value1) == G_TYPE_INT
- || G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
- if (*s == ',') {
- s++;
-
- while (g_ascii_isspace (*s))
- s++;
-
- ret = gst_structure_parse_value (s, &s, &value3, type);
- if (ret == FALSE)
- return FALSE;
-
- while (g_ascii_isspace (*s))
- s++;
-
- have_step = TRUE;
- }
}
- if (*s != ']')
- return FALSE;
- s++;
-
- if (G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value2))
- return FALSE;
- if (have_step && G_VALUE_TYPE (&value1) != G_VALUE_TYPE (&value3))
- return FALSE;
-
- if (G_VALUE_TYPE (&value1) == G_TYPE_DOUBLE) {
- range_type = GST_TYPE_DOUBLE_RANGE;
- g_value_init (value, range_type);
- gst_value_set_double_range (value,
- gst_g_value_get_double_unchecked (&value1),
- gst_g_value_get_double_unchecked (&value2));
- } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT) {
- range_type = GST_TYPE_INT_RANGE;
- g_value_init (value, range_type);
- if (have_step)
- gst_value_set_int_range_step (value,
- gst_g_value_get_int_unchecked (&value1),
- gst_g_value_get_int_unchecked (&value2),
- gst_g_value_get_int_unchecked (&value3));
- else
- gst_value_set_int_range (value, gst_g_value_get_int_unchecked (&value1),
- gst_g_value_get_int_unchecked (&value2));
- } else if (G_VALUE_TYPE (&value1) == G_TYPE_INT64) {
- range_type = GST_TYPE_INT64_RANGE;
- g_value_init (value, range_type);
- if (have_step)
- gst_value_set_int64_range_step (value,
- gst_g_value_get_int64_unchecked (&value1),
- gst_g_value_get_int64_unchecked (&value2),
- gst_g_value_get_int64_unchecked (&value3));
- else
- gst_value_set_int64_range (value,
- gst_g_value_get_int64_unchecked (&value1),
- gst_g_value_get_int64_unchecked (&value2));
- } else if (G_VALUE_TYPE (&value1) == GST_TYPE_FRACTION) {
- range_type = GST_TYPE_FRACTION_RANGE;
- g_value_init (value, range_type);
- gst_value_set_fraction_range (value, &value1, &value2);
- } else {
- return FALSE;
- }
+ *end = w;
+ *next = r;
- *after = s;
return TRUE;
}
-static gboolean
-gst_structure_parse_any_list (gchar * s, gchar ** after, GValue * value,
- GType type, GType list_type, char begin, char end)
+gboolean
+priv_gst_structure_parse_fields (gchar * str, gchar ** end,
+ GstStructure * structure)
{
- GValue list_value = { 0 };
- gboolean ret;
- GArray *array;
-
- g_value_init (value, list_type);
- array = g_value_peek_pointer (value);
-
- if (*s != begin)
- return FALSE;
- s++;
-
- while (g_ascii_isspace (*s))
- s++;
- if (*s == end) {
- s++;
- *after = s;
- return TRUE;
- }
-
- ret = gst_structure_parse_value (s, &s, &list_value, type);
- if (ret == FALSE)
- return FALSE;
-
- g_array_append_val (array, list_value);
+ gchar *r;
+ GstStructureField field;
- while (g_ascii_isspace (*s))
- s++;
+ r = str;
- while (*s != end) {
- if (*s != ',')
+ do {
+ while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
+ && g_ascii_isspace (r[1]))))
+ r++;
+ if (*r == ';') {
+ /* end of structure, get the next char and finish */
+ r++;
+ break;
+ }
+ if (*r == '\0') {
+ /* accept \0 as end delimiter */
+ break;
+ }
+ if (G_UNLIKELY (*r != ',')) {
+ GST_WARNING ("Failed to find delimiter, r=%s", r);
return FALSE;
- s++;
-
- while (g_ascii_isspace (*s))
- s++;
+ }
+ r++;
+ while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
+ && g_ascii_isspace (r[1]))))
+ r++;
- memset (&list_value, 0, sizeof (list_value));
- ret = gst_structure_parse_value (s, &s, &list_value, type);
- if (ret == FALSE)
+ memset (&field, 0, sizeof (field));
+ if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
+ GST_WARNING ("Failed to parse field, r=%s", r);
return FALSE;
+ }
+ gst_structure_set_field (structure, &field);
+ } while (TRUE);
- g_array_append_val (array, list_value);
- while (g_ascii_isspace (*s))
- s++;
- }
-
- s++;
-
- *after = s;
- return TRUE;
-}
-
-static gboolean
-gst_structure_parse_list (gchar * s, gchar ** after, GValue * value, GType type)
-{
- return gst_structure_parse_any_list (s, after, value, type, GST_TYPE_LIST,
- '{', '}');
-}
-
-static gboolean
-gst_structure_parse_array (gchar * s, gchar ** after, GValue * value,
- GType type)
-{
- return gst_structure_parse_any_list (s, after, value, type,
- GST_TYPE_ARRAY, '<', '>');
-}
-
-static gboolean
-gst_structure_parse_simple_string (gchar * str, gchar ** end)
-{
- char *s = str;
-
- while (G_LIKELY (GST_ASCII_IS_STRING (*s))) {
- s++;
- }
-
- *end = s;
-
- return (s != str);
-}
-
-static gboolean
-gst_structure_parse_field (gchar * str,
- gchar ** after, GstStructureField * field)
-{
- gchar *name;
- gchar *name_end;
- gchar *s;
- gchar c;
-
- s = str;
-
- while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
- s++;
- name = s;
- if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &name_end))) {
- GST_WARNING ("failed to parse simple string, str=%s", str);
- return FALSE;
- }
-
- s = name_end;
- while (g_ascii_isspace (*s) || (s[0] == '\\' && g_ascii_isspace (s[1])))
- s++;
-
- if (G_UNLIKELY (*s != '=')) {
- GST_WARNING ("missing assignment operator in the field, str=%s", str);
- return FALSE;
- }
- s++;
-
- c = *name_end;
- *name_end = '\0';
- field->name = g_quark_from_string (name);
- GST_DEBUG ("trying field name '%s'", name);
- *name_end = c;
-
- if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value,
- G_TYPE_INVALID))) {
- GST_WARNING ("failed to parse value %s", str);
- return FALSE;
- }
+ *end = r;
- *after = s;
return TRUE;
}
-static gboolean
-gst_structure_parse_value (gchar * str,
- gchar ** after, GValue * value, GType default_type)
+/**
+ * gst_structure_new_from_string:
+ * @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
+ * where parsing ended will be returned.
+ *
+ * The current implementation of serialization will lead to unexpected results
+ * when there are nested #GstCaps / #GstStructure deeper than one level.
+ *
+ * Free-function: gst_structure_free
+ *
+ * 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
+ */
+GstStructure *
+gst_structure_new_from_string (const gchar * string)
{
- gchar *type_name;
- gchar *type_end;
- gchar *value_s;
- gchar *value_end;
- gchar *s;
- gchar c;
- int ret = 0;
- GType type = default_type;
-
- s = str;
- while (g_ascii_isspace (*s))
- s++;
-
- /* check if there's a (type_name) 'cast' */
- type_name = NULL;
- if (*s == '(') {
- s++;
- while (g_ascii_isspace (*s))
- s++;
- type_name = s;
- if (G_UNLIKELY (!gst_structure_parse_simple_string (s, &type_end)))
- return FALSE;
- s = type_end;
- while (g_ascii_isspace (*s))
- s++;
- if (G_UNLIKELY (*s != ')'))
- return FALSE;
- s++;
- while (g_ascii_isspace (*s))
- s++;
-
- c = *type_end;
- *type_end = 0;
- type = gst_structure_gtype_from_abbr (type_name);
- GST_DEBUG ("trying type name '%s'", type_name);
- *type_end = c;
-
- if (G_UNLIKELY (type == G_TYPE_INVALID)) {
- GST_WARNING ("invalid type");
- return FALSE;
- }
- }
-
- while (g_ascii_isspace (*s))
- s++;
- if (*s == '[') {
- ret = gst_structure_parse_range (s, &s, value, type);
- } else if (*s == '{') {
- ret = gst_structure_parse_list (s, &s, value, type);
- } else if (*s == '<') {
- ret = gst_structure_parse_array (s, &s, value, type);
- } else {
- value_s = s;
-
- 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
- };
- int i;
-
- if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s, TRUE)))
- return FALSE;
- /* Set NULL terminator for deserialization */
- c = *value_end;
- *value_end = '\0';
-
- for (i = 0; i < G_N_ELEMENTS (try_types); i++) {
- g_value_init (value, try_types[i]);
- ret = gst_value_deserialize (value, value_s);
- if (ret)
- break;
- g_value_unset (value);
- }
- } else {
- g_value_init (value, type);
-
- if (G_UNLIKELY (!gst_structure_parse_string (s, &value_end, &s,
- (type != G_TYPE_STRING))))
- return FALSE;
- /* Set NULL terminator for deserialization */
- c = *value_end;
- *value_end = '\0';
-
- ret = gst_value_deserialize (value, value_s);
- if (G_UNLIKELY (!ret))
- g_value_unset (value);
- }
- *value_end = c;
- }
-
- *after = s;
-
- return ret;
+ return gst_structure_from_string (string, NULL);
}
/**
* gst_structure_from_string:
* @string: a string representation of a #GstStructure.
- * @end: (out) (allow-none) (transfer none): pointer to store the end of the string in.
+ * @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)
char *r;
char save;
GstStructure *structure = NULL;
- GstStructureField field;
g_return_val_if_fail (string != NULL, NULL);
copy = g_strdup (string);
r = copy;
- /* skip spaces (FIXME: _isspace treats tabs and newlines as space!) */
- while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
- && g_ascii_isspace (r[1]))))
- r++;
-
- name = r;
- if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) {
- GST_WARNING ("Failed to parse structure string '%s'", string);
+ if (!priv_gst_structure_parse_name (r, &name, &w, &r))
goto error;
- }
save = *w;
*w = '\0';
if (G_UNLIKELY (structure == NULL))
goto error;
- do {
- while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
- && g_ascii_isspace (r[1]))))
- r++;
- if (*r == ';') {
- /* end of structure, get the next char and finish */
- r++;
- break;
- }
- if (*r == '\0') {
- /* accept \0 as end delimiter */
- break;
- }
- if (G_UNLIKELY (*r != ',')) {
- GST_WARNING ("Failed to find delimiter, r=%s", r);
- goto error;
- }
- r++;
- while (*r && (g_ascii_isspace (*r) || (r[0] == '\\'
- && g_ascii_isspace (r[1]))))
- r++;
-
- memset (&field, 0, sizeof (field));
- if (G_UNLIKELY (!gst_structure_parse_field (r, &r, &field))) {
- GST_WARNING ("Failed to parse field, r=%s", r);
- goto error;
- }
- gst_structure_set_field (structure, &field);
- } while (TRUE);
+ if (!priv_gst_structure_parse_fields (r, &r, structure))
+ goto error;
if (end)
*end = (char *) string + (r - copy);
* 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,
/* already fixed */
return FALSE;
} else if (G_VALUE_TYPE (value) == GST_TYPE_INT_RANGE) {
- int x;
+ int min, max, step;
+
+ min = gst_value_get_int_range_min (value);
+ max = gst_value_get_int_range_max (value);
+ step = gst_value_get_int_range_step (value);
+
+ target = CLAMP (target, min, max);
+ if (G_UNLIKELY (step != 1)) {
+ gint rem = target % step;
+ target -= rem;
+ if (rem > step / 2)
+ target += step;
+ }
- x = gst_value_get_int_range_min (value);
- if (target < x)
- target = x;
- x = gst_value_get_int_range_max (value);
- if (target > x)
- target = x;
gst_structure_set (structure, field_name, G_TYPE_INT, target, NULL);
return TRUE;
} else if (G_VALUE_TYPE (value) == GST_TYPE_LIST) {
* 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,
* 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,
* 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
- *
- * Since: 0.10.30
+ * Returns: %TRUE if the structure could be fixated
*/
gboolean
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,
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);
GValue v = { 0 };
if (gst_value_fixate (&v, value)) {
- gst_structure_id_set_value (s, field_id, &v);
- g_value_unset (&v);
+ gst_structure_id_take_value (s, field_id, &v);
}
return TRUE;
}
*
* 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)
/* 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); \
guint _flags = (flags); \
GType _value_type = G_VALUE_TYPE (_value); \
GTypeValueTable *_vtable = g_type_value_table_peek (_value_type); \
- gchar *_lcopy_format = _vtable->lcopy_format; \
+ const gchar *_lcopy_format = _vtable->lcopy_format; \
GTypeCValue _cvalues[G_VALUE_COLLECT_FORMAT_MAX_LENGTH] = { { 0, }, }; \
guint _n_values = 0; \
\
* 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
- *
- * Since: 0.10.24
+ * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
*/
gboolean
gst_structure_get_valist (const GstStructure * structure,
/* ERRORS */
no_such_field:
{
- GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
+ GST_INFO ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
field_name, structure);
return FALSE;
}
wrong_type:
{
- GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
+ GST_INFO ("Expected field '%s' in structure to be of type '%s', but "
"field was of type '%s': %" GST_PTR_FORMAT, field_name,
GST_STR_NULL (g_type_name (expected_type)),
G_VALUE_TYPE_NAME (gst_structure_get_value (structure, field_name)),
* 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
- *
- * Since: 0.10.24
+ * Returns: %TRUE, or %FALSE if there was a problem reading any of the fields
*/
gboolean
gst_structure_id_get_valist (const GstStructure * structure,
/* ERRORS */
no_such_field:
{
- GST_WARNING ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
+ GST_DEBUG ("Expected field '%s' in structure: %" GST_PTR_FORMAT,
GST_STR_NULL (g_quark_to_string (field_id)), structure);
return FALSE;
}
wrong_type:
{
- GST_WARNING ("Expected field '%s' in structure to be of type '%s', but "
+ GST_DEBUG ("Expected field '%s' in structure to be of type '%s', but "
"field was of type '%s': %" GST_PTR_FORMAT,
g_quark_to_string (field_id),
GST_STR_NULL (g_type_name (expected_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.
- *
- * Since: 0.10.24
+ * than the type specified), otherwise %TRUE.
*/
gboolean
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
* 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.
- *
- * Since: 0.10.24
+ * than the type specified), otherwise %TRUE.
*/
gboolean
gst_structure_id_get (const GstStructure * structure, GQuark first_field_id,
*
* Tests if the two #GstStructure are equal.
*
- * Returns: TRUE if the two structures have the same name and field.
- *
- * Since: 0.10.36
+ * Returns: %TRUE if the two structures have the same name and field.
**/
gboolean
gst_structure_is_equal (const GstStructure * structure1,
} else {
GValue dest_value = { 0 };
if (gst_value_intersect (&dest_value, val1, val2)) {
- gst_structure_id_set_value (idata->dest, id, &dest_value);
- g_value_unset (&dest_value);
+ gst_structure_id_take_value (idata->dest, id, &dest_value);
} else {
return FALSE;
}
* @struct1: a #GstStructure
* @struct2: a #GstStructure
*
- * Interesects @struct1 and @struct2 and returns the intersection.
- *
- * Returns: Intersection of @struct1 and @struct2
+ * Intersects @struct1 and @struct2 and returns the intersection.
*
- * Since: 0.10.36
+ * Returns: (nullable): Intersection of @struct1 and @struct2
*/
GstStructure *
gst_structure_intersect (const GstStructure * struct1,
* would not be empty.
*
* Returns: %TRUE if intersection would not be empty
- *
- * Since: 0.10.36
*/
gboolean
gst_structure_can_intersect (const GstStructure * struct1,
}
static gboolean
-gst_caps_structure_is_subset_field (GQuark field_id, const GValue * value,
+gst_caps_structure_is_superset_field (GQuark field_id, const GValue * value,
gpointer user_data)
{
- GstStructure *superset = user_data;
+ GstStructure *subset = user_data;
const GValue *other;
int comparison;
- if (!(other = gst_structure_id_get_value (superset, field_id)))
- /* field is missing in the superset => is subset */
- return TRUE;
+ if (!(other = gst_structure_id_get_value (subset, field_id)))
+ /* field is missing in the subset => no subset */
+ return FALSE;
- comparison = gst_value_compare (other, value);
+ comparison = gst_value_compare (value, other);
/* equal values are subset */
if (comparison == GST_VALUE_EQUAL)
if (comparison != GST_VALUE_UNORDERED)
return FALSE;
- return gst_value_is_subset (value, other);
+ return gst_value_is_subset (other, value);
}
/**
* @subset has a value that is a subset of the value in @superset.
*
* Returns: %TRUE if @subset is a subset of @superset
- *
- * Since: 0.10.36
*/
gboolean
gst_structure_is_subset (const GstStructure * subset,
(gst_structure_n_fields (superset) > gst_structure_n_fields (subset)))
return FALSE;
- return gst_structure_foreach ((GstStructure *) subset,
- gst_caps_structure_is_subset_field, (gpointer) superset);
+ return gst_structure_foreach ((GstStructure *) superset,
+ gst_caps_structure_is_superset_field, (gpointer) subset);
}
gst_structure_foreach (structure, default_fixate, structure);
}
+
+static gboolean
+_gst_structure_get_any_list (GstStructure * structure, GType type,
+ const gchar * fieldname, GValueArray ** array)
+{
+ GstStructureField *field;
+ GValue val = G_VALUE_INIT;
+
+ g_return_val_if_fail (structure != NULL, FALSE);
+ g_return_val_if_fail (fieldname != NULL, FALSE);
+ g_return_val_if_fail (array != NULL, FALSE);
+
+ field = gst_structure_get_field (structure, fieldname);
+
+ if (field == NULL || G_VALUE_TYPE (&field->value) != type)
+ return FALSE;
+
+ g_value_init (&val, G_TYPE_VALUE_ARRAY);
+
+ if (g_value_transform (&field->value, &val)) {
+ *array = g_value_get_boxed (&val);
+ return TRUE;
+ }
+
+ g_value_unset (&val);
+ return FALSE;
+}
+
+/**
+ * gst_structure_get_array:
+ * @structure: a #GstStructure
+ * @fieldname: the name of a field
+ * @array: (out): a pointer to a #GValueArray
+ *
+ * This is useful in language bindings where unknown #GValue types are not
+ * supported. This function will convert the %GST_TYPE_ARRAY into a newly
+ * allocated #GValueArray and return it through @array. Be aware that this is
+ * slower then getting the #GValue directly.
+ *
+ * Returns: %TRUE if the value could be set correctly. If there was no field
+ * with @fieldname or the existing field did not contain a %GST_TYPE_ARRAY,
+ * this function returns %FALSE.
+ */
+gboolean
+gst_structure_get_array (GstStructure * structure, const gchar * fieldname,
+ GValueArray ** array)
+{
+ return _gst_structure_get_any_list (structure, GST_TYPE_ARRAY, fieldname,
+ array);
+}
+
+/**
+ * gst_structure_get_list:
+ * @structure: a #GstStructure
+ * @fieldname: the name of a field
+ * @array: (out): a pointer to a #GValueArray
+ *
+ * This is useful in language bindings where unknown #GValue types are not
+ * supported. This function will convert the %GST_TYPE_LIST into a newly
+ * allocated GValueArray and return it through @array. Be aware that this is
+ * slower then getting the #GValue directly.
+ *
+ * Returns: %TRUE if the value could be set correctly. If there was no field
+ * with @fieldname or the existing field did not contain a %GST_TYPE_LIST, this
+ * function returns %FALSE.
+ *
+ * Since 1.12
+ */
+gboolean
+gst_structure_get_list (GstStructure * structure, const gchar * fieldname,
+ GValueArray ** array)
+{
+ return _gst_structure_get_any_list (structure, GST_TYPE_LIST, fieldname,
+ array);
+}
+
+static void
+_gst_structure_set_any_list (GstStructure * structure, GType type,
+ const gchar * fieldname, const GValueArray * array)
+{
+ GValue arval = G_VALUE_INIT;
+ GValue value = G_VALUE_INIT;
+
+ g_return_if_fail (structure != NULL);
+ g_return_if_fail (fieldname != NULL);
+ g_return_if_fail (array != NULL);
+ g_return_if_fail (IS_MUTABLE (structure));
+
+ g_value_init (&value, type);
+ g_value_init (&arval, G_TYPE_VALUE_ARRAY);
+ g_value_set_static_boxed (&arval, array);
+
+ if (g_value_transform (&arval, &value)) {
+ gst_structure_id_set_value_internal (structure,
+ g_quark_from_string (fieldname), &value);
+ } else {
+ g_warning ("Failed to convert a GValueArray");
+ }
+
+ g_value_unset (&arval);
+ g_value_unset (&value);
+}
+
+/**
+ * gst_structure_set_array:
+ * @structure: a #GstStructure
+ * @fieldname: the name of a field
+ * @array: a pointer to a #GValueArray
+ *
+ * This is useful in language bindings where unknown GValue types are not
+ * supported. This function will convert a @array to %GST_TYPE_ARRAY and set
+ * the field specified by @fieldname. Be aware that this is slower then using
+ * %GST_TYPE_ARRAY in a #GValue directly.
+ *
+ * Since 1.12
+ */
+void
+gst_structure_set_array (GstStructure * structure, const gchar * fieldname,
+ const GValueArray * array)
+{
+ _gst_structure_set_any_list (structure, GST_TYPE_ARRAY, fieldname, array);
+}
+
+/**
+ * gst_structure_set_list:
+ * @structure: a #GstStructure
+ * @fieldname: the name of a field
+ * @array: a pointer to a #GValueArray
+ *
+ * This is useful in language bindings where unknown GValue types are not
+ * supported. This function will convert a @array to %GST_TYPE_LIST and set
+ * the field specified by @fieldname. Be aware that this is slower then using
+ * %GST_TYPE_LIST in a #GValue directly.
+ *
+ * Since 1.12
+ */
+void
+gst_structure_set_list (GstStructure * structure, const gchar * fieldname,
+ const GValueArray * array)
+{
+ _gst_structure_set_any_list (structure, GST_TYPE_LIST, fieldname, array);
+}