From c21e219ed425fdd9417e8c087f792955b242674f Mon Sep 17 00:00:00 2001 From: Nicolas Dufresne Date: Mon, 20 Mar 2017 15:50:49 -0400 Subject: [PATCH] structure: Add get/set_array/list using GValueArray This adds a binding friendly interface to get and set arrays and list into GstStructure. New API: - gst_structure_set_array - gst_structure_set_list - gst_structure_get_array - gst_structure_get_list https://bugzilla.gnome.org/show_bug.cgi?id=753754 --- docs/gst/gstreamer-sections.txt | 4 ++ gst/gststructure.c | 146 ++++++++++++++++++++++++++++++++++++++++ gst/gststructure.h | 16 +++++ win32/common/libgstreamer.def | 4 ++ 4 files changed, 170 insertions(+) diff --git a/docs/gst/gstreamer-sections.txt b/docs/gst/gstreamer-sections.txt index b0e6e6c..5acc42d 100644 --- a/docs/gst/gstreamer-sections.txt +++ b/docs/gst/gstreamer-sections.txt @@ -2771,6 +2771,8 @@ gst_structure_get gst_structure_get_valist gst_structure_get_value gst_structure_set_value +gst_structure_set_array +gst_structure_set_list gst_structure_take_value gst_structure_set gst_structure_set_valist @@ -2802,6 +2804,8 @@ gst_structure_get_date_time gst_structure_get_clock_time gst_structure_get_enum gst_structure_get_fraction +gst_structure_get_array +gst_structure_get_list gst_structure_foreach gst_structure_map_in_place gst_structure_filter_and_map_in_place diff --git a/gst/gststructure.c b/gst/gststructure.c index 81e5b67..5be496b 100644 --- a/gst/gststructure.c +++ b/gst/gststructure.c @@ -65,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" @@ -2989,3 +2993,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 and + * %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 an int, 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_ARRAY and + * %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 an int, 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_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_list (GstStructure * structure, const gchar * fieldname, + const GValueArray * array) +{ + _gst_structure_set_any_list (structure, GST_TYPE_LIST, fieldname, array); +} diff --git a/gst/gststructure.h b/gst/gststructure.h index e746ab0..6af9c83 100644 --- a/gst/gststructure.h +++ b/gst/gststructure.h @@ -145,6 +145,14 @@ void gst_structure_set_value (GstStructure * const gchar * fieldname, const GValue * value); +void gst_structure_set_array (GstStructure * structure, + const gchar * fieldname, + const GValueArray * array); + +void gst_structure_set_list (GstStructure * structure, + const gchar * fieldname, + const GValueArray * array); + void gst_structure_id_take_value (GstStructure * structure, GQuark field, GValue * value); @@ -293,6 +301,14 @@ gboolean gst_structure_get_flagset (const GstStructure * guint * value_flags, guint * value_mask); +gboolean gst_structure_get_array (GstStructure * structure, + const gchar * fieldname, + GValueArray ** array); + +gboolean gst_structure_get_list (GstStructure * structure, + const gchar * fieldname, + GValueArray ** array); + gchar * gst_structure_to_string (const GstStructure * structure) G_GNUC_MALLOC; GstStructure * gst_structure_from_string (const gchar * string, diff --git a/win32/common/libgstreamer.def b/win32/common/libgstreamer.def index 780d6c4..99a1541 100644 --- a/win32/common/libgstreamer.def +++ b/win32/common/libgstreamer.def @@ -1277,6 +1277,7 @@ EXPORTS gst_structure_free gst_structure_from_string gst_structure_get + gst_structure_get_array gst_structure_get_boolean gst_structure_get_clock_time gst_structure_get_date @@ -1288,6 +1289,7 @@ EXPORTS gst_structure_get_fraction gst_structure_get_int gst_structure_get_int64 + gst_structure_get_list gst_structure_get_name gst_structure_get_name_id gst_structure_get_string @@ -1325,6 +1327,8 @@ EXPORTS gst_structure_remove_fields gst_structure_remove_fields_valist gst_structure_set + gst_structure_set_array + gst_structure_set_list gst_structure_set_name gst_structure_set_parent_refcount gst_structure_set_valist -- 2.7.4