X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=gst%2Fgststructure.c;h=734b2d27f613ef8a811335a38e585c4f20bb662a;hb=ce4698487e2650ee9b365d6cf41a254483349bc0;hp=581b00e09edd9ee437e38cd83f20d663165b3707;hpb=aa336008b378ebe24d56896f96240fc3bd96d5d4;p=platform%2Fupstream%2Fgstreamer.git diff --git a/gst/gststructure.c b/gst/gststructure.c index 581b00e..734b2d2 100644 --- a/gst/gststructure.c +++ b/gst/gststructure.c @@ -21,6 +21,7 @@ /** * SECTION:gststructure + * @title: GstStructure * @short_description: Generic structure containing fields of names and values * @see_also: #GstCaps, #GstMessage, #GstEvent, #GstQuery * @@ -64,6 +65,10 @@ #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 #include "gst_private.h" @@ -115,9 +120,6 @@ static void gst_structure_transform_to_string (const GValue * src_value, 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; @@ -877,7 +879,8 @@ gst_structure_get_field (const GstStructure * structure, * * 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, @@ -902,8 +905,8 @@ 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) @@ -1759,123 +1762,6 @@ gst_structure_get_flagset (const GstStructure * structure, 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} - , - {"sample", GST_TYPE_SAMPLE} - , - {"taglist", GST_TYPE_TAG_LIST} - }; - _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) -{ - 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); -} - -static const char * -gst_structure_to_abbr (GType type) -{ - int i; - GstStructureAbbreviation *abbrs; - gint n_abbrs; - - g_return_val_if_fail (type != G_TYPE_INVALID, NULL); - - abbrs = gst_structure_get_abbrs (&n_abbrs); - - for (i = 0; i < n_abbrs; i++) { - if (type == abbrs[i].type) { - return abbrs[i].type_name; - } - } - - return g_type_name (type); -} - static GType gst_structure_value_get_generic_type (const GValue * val) { @@ -1918,21 +1804,38 @@ priv_gst_structure_append_to_gstring (const GstStructure * structure, 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, ')'); 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 { - GST_WARNING ("No value transform to serialize field '%s' of type '%s'", - g_quark_to_string (field->name), gst_structure_to_abbr (type)); + 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"); } } @@ -1952,7 +1855,7 @@ priv__gst_structure_append_template_to_gstring (GQuark field_id, /* 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 (s, _priv_gst_value_gtype_to_abbr (type)); g_string_append_c (s, ')'); //TODO(ensonic): table like GstStructureAbbreviation (or extend it) @@ -1977,7 +1880,9 @@ priv__gst_structure_append_template_to_gstring (GQuark field_id, 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 (s, "%i"); + 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 { @@ -2029,260 +1934,6 @@ gst_structure_to_string (const GstStructure * structure) 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) -{ - gchar *w; - - if (*s == 0) - return FALSE; - - if (*s != '"') { - int ret; - - ret = gst_structure_parse_simple_string (s, end); - *next = *end; - - return ret; - } - - if (unescape) { - w = s; - s++; - while (*s != '"') { - if (G_UNLIKELY (*s == 0)) - return FALSE; - if (G_UNLIKELY (*s == '\\')) { - s++; - if (G_UNLIKELY (*s == 0)) - return FALSE; - } - *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++; - if (G_UNLIKELY (*s == 0)) - return FALSE; - } - s++; - } - s++; - w = s; - } - - *end = w; - *next = s; - - return TRUE; -} - -static gboolean -gst_structure_parse_range (gchar * s, gchar ** after, GValue * value, - GType type) -{ - GValue value1 = { 0 }; - GValue value2 = { 0 }; - GValue value3 = { 0 }; - GType range_type; - gboolean ret, have_step = FALSE; - - if (*s != '[') - return FALSE; - s++; - - ret = gst_structure_parse_value (s, &s, &value1, type); - if (!ret) - return FALSE; - - while (g_ascii_isspace (*s)) - s++; - - if (*s != ',') - return FALSE; - s++; - - while (g_ascii_isspace (*s)) - s++; - - ret = gst_structure_parse_value (s, &s, &value2, type); - if (!ret) - 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) - 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; - } - - *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) -{ - 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) - return FALSE; - - g_array_append_val (array, list_value); - - while (g_ascii_isspace (*s)) - s++; - - while (*s != end) { - if (*s != ',') - return FALSE; - s++; - - while (g_ascii_isspace (*s)) - s++; - - memset (&list_value, 0, sizeof (list_value)); - ret = gst_structure_parse_value (s, &s, &list_value, type); - if (!ret) - return FALSE; - - 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) @@ -2297,7 +1948,7 @@ gst_structure_parse_field (gchar * 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))) { + if (G_UNLIKELY (!_priv_gst_value_parse_simple_string (s, &name_end))) { GST_WARNING ("failed to parse simple string, str=%s", str); return FALSE; } @@ -2318,7 +1969,7 @@ gst_structure_parse_field (gchar * str, GST_DEBUG ("trying field name '%s'", name); *name_end = c; - if (G_UNLIKELY (!gst_structure_parse_value (s, &s, &field->value, + 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; @@ -2328,106 +1979,6 @@ gst_structure_parse_field (gchar * str, return TRUE; } -static gboolean -gst_structure_parse_value (gchar * str, - gchar ** after, GValue * value, GType default_type) -{ - 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, GST_TYPE_FLAG_SET, - 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; -} - gboolean priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end, gchar ** next) @@ -2444,7 +1995,7 @@ priv_gst_structure_parse_name (gchar * str, gchar ** start, gchar ** end, *start = r; - if (G_UNLIKELY (!gst_structure_parse_string (r, &w, &r, TRUE))) { + if (G_UNLIKELY (!_priv_gst_value_parse_string (r, &w, &r, TRUE))) { GST_WARNING ("Failed to parse structure string '%s'", str); return FALSE; } @@ -3303,7 +2854,7 @@ gst_structure_intersect_field2 (GQuark id, const GValue * val1, gpointer data) * * Intersects @struct1 and @struct2 and returns the intersection. * - * Returns: Intersection of @struct1 and @struct2 + * Returns: (nullable): Intersection of @struct1 and @struct2 */ GstStructure * gst_structure_intersect (const GstStructure * struct1, @@ -3452,3 +3003,145 @@ gst_structure_fixate (GstStructure * structure) 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); +}