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
This commit is contained in:
Nicolas Dufresne 2017-03-20 15:50:49 -04:00
parent 84f826a937
commit c21e219ed4
4 changed files with 170 additions and 0 deletions

View file

@ -2771,6 +2771,8 @@ gst_structure_get
gst_structure_get_valist gst_structure_get_valist
gst_structure_get_value gst_structure_get_value
gst_structure_set_value gst_structure_set_value
gst_structure_set_array
gst_structure_set_list
gst_structure_take_value gst_structure_take_value
gst_structure_set gst_structure_set
gst_structure_set_valist gst_structure_set_valist
@ -2802,6 +2804,8 @@ gst_structure_get_date_time
gst_structure_get_clock_time gst_structure_get_clock_time
gst_structure_get_enum gst_structure_get_enum
gst_structure_get_fraction gst_structure_get_fraction
gst_structure_get_array
gst_structure_get_list
gst_structure_foreach gst_structure_foreach
gst_structure_map_in_place gst_structure_map_in_place
gst_structure_filter_and_map_in_place gst_structure_filter_and_map_in_place

View file

@ -65,6 +65,10 @@
#include "config.h" #include "config.h"
#endif #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 <string.h>
#include "gst_private.h" #include "gst_private.h"
@ -2989,3 +2993,145 @@ gst_structure_fixate (GstStructure * structure)
gst_structure_foreach (structure, default_fixate, 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);
}

View file

@ -145,6 +145,14 @@ void gst_structure_set_value (GstStructure *
const gchar * fieldname, const gchar * fieldname,
const GValue * value); 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, void gst_structure_id_take_value (GstStructure * structure,
GQuark field, GQuark field,
GValue * value); GValue * value);
@ -293,6 +301,14 @@ gboolean gst_structure_get_flagset (const GstStructure *
guint * value_flags, guint * value_flags,
guint * value_mask); 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; gchar * gst_structure_to_string (const GstStructure * structure) G_GNUC_MALLOC;
GstStructure * gst_structure_from_string (const gchar * string, GstStructure * gst_structure_from_string (const gchar * string,

View file

@ -1277,6 +1277,7 @@ EXPORTS
gst_structure_free gst_structure_free
gst_structure_from_string gst_structure_from_string
gst_structure_get gst_structure_get
gst_structure_get_array
gst_structure_get_boolean gst_structure_get_boolean
gst_structure_get_clock_time gst_structure_get_clock_time
gst_structure_get_date gst_structure_get_date
@ -1288,6 +1289,7 @@ EXPORTS
gst_structure_get_fraction gst_structure_get_fraction
gst_structure_get_int gst_structure_get_int
gst_structure_get_int64 gst_structure_get_int64
gst_structure_get_list
gst_structure_get_name gst_structure_get_name
gst_structure_get_name_id gst_structure_get_name_id
gst_structure_get_string gst_structure_get_string
@ -1325,6 +1327,8 @@ EXPORTS
gst_structure_remove_fields gst_structure_remove_fields
gst_structure_remove_fields_valist gst_structure_remove_fields_valist
gst_structure_set gst_structure_set
gst_structure_set_array
gst_structure_set_list
gst_structure_set_name gst_structure_set_name
gst_structure_set_parent_refcount gst_structure_set_parent_refcount
gst_structure_set_valist gst_structure_set_valist