Blame gst/gstparamspecs.c

Packit f546b1
/* GStreamer - GParamSpecs for some of our types
Packit f546b1
 * Copyright (C) 2007 Tim-Philipp Müller  <tim centricular net>
Packit f546b1
 *
Packit f546b1
 * This library is free software; you can redistribute it and/or
Packit f546b1
 * modify it under the terms of the GNU Library General Public
Packit f546b1
 * License as published by the Free Software Foundation; either
Packit f546b1
 * version 2 of the License, or (at your option) any later version.
Packit f546b1
 *
Packit f546b1
 * This library is distributed in the hope that it will be useful,
Packit f546b1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit f546b1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit f546b1
 * Library General Public License for more details.
Packit f546b1
 *
Packit f546b1
 * You should have received a copy of the GNU Library General Public
Packit f546b1
 * License along with this library; if not, write to the
Packit f546b1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit f546b1
 * Boston, MA 02110-1301, USA.
Packit f546b1
 */
Packit f546b1
/**
Packit f546b1
 * SECTION:gstparamspec
Packit f546b1
 * @title: GstParamSpec
Packit f546b1
 * @short_description: GParamSpec implementations specific
Packit f546b1
 * to GStreamer
Packit f546b1
 *
Packit f546b1
 * GParamSpec implementations specific to GStreamer.
Packit f546b1
 */
Packit f546b1
Packit f546b1
#ifdef HAVE_CONFIG_H
Packit f546b1
#include "config.h"
Packit f546b1
#endif
Packit f546b1
Packit f546b1
#include "gst_private.h"
Packit f546b1
#include "glib-compat-private.h"
Packit f546b1
#include "gstparamspecs.h"
Packit f546b1
Packit f546b1
/* --- GstParamSpecFraction --- */
Packit f546b1
Packit f546b1
static void
Packit f546b1
_gst_param_fraction_init (GParamSpec * pspec)
Packit f546b1
{
Packit f546b1
  GstParamSpecFraction *fspec = GST_PARAM_SPEC_FRACTION (pspec);
Packit f546b1
Packit f546b1
  fspec->min_num = 0;
Packit f546b1
  fspec->min_den = 1;
Packit f546b1
  fspec->max_num = G_MAXINT;
Packit f546b1
  fspec->max_den = 1;
Packit f546b1
  fspec->def_num = 1;
Packit f546b1
  fspec->def_den = 1;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
_gst_param_fraction_set_default (GParamSpec * pspec, GValue * value)
Packit f546b1
{
Packit f546b1
  value->data[0].v_int = GST_PARAM_SPEC_FRACTION (pspec)->def_num;
Packit f546b1
  value->data[1].v_int = GST_PARAM_SPEC_FRACTION (pspec)->def_den;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
_gst_param_fraction_validate (GParamSpec * pspec, GValue * value)
Packit f546b1
{
Packit f546b1
  GstParamSpecFraction *fspec = GST_PARAM_SPEC_FRACTION (pspec);
Packit f546b1
  gboolean within_range = FALSE;
Packit f546b1
  GValue f_this = { 0, };
Packit f546b1
  GValue f_min = { 0, };
Packit f546b1
  GValue f_max = { 0, };
Packit f546b1
  gint res;
Packit f546b1
Packit f546b1
  g_value_init (&f_this, GST_TYPE_FRACTION);
Packit f546b1
  gst_value_set_fraction (&f_this, value->data[0].v_int, value->data[1].v_int);
Packit f546b1
Packit f546b1
  g_value_init (&f_min, GST_TYPE_FRACTION);
Packit f546b1
  gst_value_set_fraction (&f_min, fspec->min_num, fspec->min_den);
Packit f546b1
Packit f546b1
  g_value_init (&f_max, GST_TYPE_FRACTION);
Packit f546b1
  gst_value_set_fraction (&f_max, fspec->max_num, fspec->max_den);
Packit f546b1
Packit f546b1
  res = gst_value_compare (&f_min, &f_this);
Packit f546b1
#ifndef GST_DISABLE_GST_DEBUG
Packit f546b1
  GST_LOG ("comparing %d/%d to %d/%d, result = %d", fspec->min_num,
Packit f546b1
      fspec->min_den, value->data[0].v_int, value->data[1].v_int, res);
Packit f546b1
#endif
Packit f546b1
  if (res != GST_VALUE_LESS_THAN && res != GST_VALUE_EQUAL)
Packit f546b1
    goto out;
Packit f546b1
Packit f546b1
#ifndef GST_DISABLE_GST_DEBUG
Packit f546b1
  GST_LOG ("comparing %d/%d to %d/%d, result = %d", value->data[0].v_int,
Packit f546b1
      value->data[1].v_int, fspec->max_num, fspec->max_den, res);
Packit f546b1
#endif
Packit f546b1
  res = gst_value_compare (&f_this, &f_max);
Packit f546b1
  if (res != GST_VALUE_LESS_THAN && res != GST_VALUE_EQUAL)
Packit f546b1
    goto out;
Packit f546b1
Packit f546b1
  within_range = TRUE;
Packit f546b1
Packit f546b1
out:
Packit f546b1
Packit f546b1
  g_value_unset (&f_min);
Packit f546b1
  g_value_unset (&f_max);
Packit f546b1
  g_value_unset (&f_this);
Packit f546b1
Packit f546b1
#ifndef GST_DISABLE_GST_DEBUG
Packit f546b1
  GST_LOG ("%swithin range", (within_range) ? "" : "not ");
Packit f546b1
#endif
Packit f546b1
Packit f546b1
  /* return FALSE if everything ok, otherwise TRUE */
Packit f546b1
  return !within_range;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gint
Packit f546b1
_gst_param_fraction_values_cmp (GParamSpec * pspec, const GValue * value1,
Packit f546b1
    const GValue * value2)
Packit f546b1
{
Packit f546b1
  gint res;
Packit f546b1
Packit f546b1
  res = gst_value_compare (value1, value2);
Packit f546b1
Packit f546b1
  g_assert (res != GST_VALUE_UNORDERED);
Packit f546b1
Packit f546b1
  /* GST_VALUE_LESS_THAN is -1, EQUAL is 0, and GREATER_THAN is 1 */
Packit f546b1
  return res;
Packit f546b1
}
Packit f546b1
Packit f546b1
GType
Packit f546b1
gst_param_spec_fraction_get_type (void)
Packit f546b1
{
Packit f546b1
  static volatile GType gst_faction_type = 0;
Packit f546b1
Packit f546b1
  /* register GST_TYPE_PARAM_FRACTION */
Packit f546b1
  if (g_once_init_enter (&gst_faction_type)) {
Packit f546b1
    GType type;
Packit f546b1
    static GParamSpecTypeInfo pspec_info = {
Packit f546b1
      sizeof (GstParamSpecFraction),    /* instance_size     */
Packit f546b1
      0,                        /* n_preallocs       */
Packit f546b1
      _gst_param_fraction_init, /* instance_init     */
Packit f546b1
      G_TYPE_INVALID,           /* value_type        */
Packit f546b1
      NULL,                     /* finalize          */
Packit f546b1
      _gst_param_fraction_set_default,  /* value_set_default */
Packit f546b1
      _gst_param_fraction_validate,     /* value_validate    */
Packit f546b1
      _gst_param_fraction_values_cmp,   /* values_cmp        */
Packit f546b1
    };
Packit f546b1
    pspec_info.value_type = gst_fraction_get_type ();
Packit f546b1
    type = g_param_type_register_static ("GstParamFraction", &pspec_info);
Packit f546b1
    g_once_init_leave (&gst_faction_type, type);
Packit f546b1
  }
Packit f546b1
Packit f546b1
  return gst_faction_type;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_param_spec_fraction:
Packit f546b1
 * @name: canonical name of the property specified
Packit f546b1
 * @nick: nick name for the property specified
Packit f546b1
 * @blurb: description of the property specified
Packit f546b1
 * @min_num: minimum value (fraction numerator)
Packit f546b1
 * @min_denom: minimum value (fraction denominator)
Packit f546b1
 * @max_num: maximum value (fraction numerator)
Packit f546b1
 * @max_denom: maximum value (fraction denominator)
Packit f546b1
 * @default_num: default value (fraction numerator)
Packit f546b1
 * @default_denom: default value (fraction denominator)
Packit f546b1
 * @flags: flags for the property specified
Packit f546b1
 *
Packit f546b1
 * This function creates a fraction GParamSpec for use by objects/elements
Packit f546b1
 * that want to expose properties of fraction type. This function is typically
Packit f546b1
 * used in connection with g_object_class_install_property() in a GObjects's
Packit f546b1
 * instance_init function.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full) (nullable): a newly created parameter specification
Packit f546b1
 */
Packit f546b1
GParamSpec *
Packit f546b1
gst_param_spec_fraction (const gchar * name, const gchar * nick,
Packit f546b1
    const gchar * blurb, gint min_num, gint min_denom, gint max_num,
Packit f546b1
    gint max_denom, gint default_num, gint default_denom, GParamFlags flags)
Packit f546b1
{
Packit f546b1
  GstParamSpecFraction *fspec;
Packit f546b1
  GParamSpec *pspec;
Packit f546b1
  GValue default_val = { 0, };
Packit f546b1
Packit f546b1
  fspec =
Packit f546b1
      g_param_spec_internal (GST_TYPE_PARAM_FRACTION, name, nick, blurb, flags);
Packit f546b1
Packit f546b1
  fspec->min_num = min_num;
Packit f546b1
  fspec->min_den = min_denom;
Packit f546b1
  fspec->max_num = max_num;
Packit f546b1
  fspec->max_den = max_denom;
Packit f546b1
  fspec->def_num = default_num;
Packit f546b1
  fspec->def_den = default_denom;
Packit f546b1
Packit f546b1
  pspec = G_PARAM_SPEC (fspec);
Packit f546b1
Packit f546b1
  /* check that min <= default <= max */
Packit f546b1
  g_value_init (&default_val, GST_TYPE_FRACTION);
Packit f546b1
  gst_value_set_fraction (&default_val, default_num, default_denom);
Packit f546b1
  /* validate returns TRUE if the validation fails */
Packit f546b1
  if (_gst_param_fraction_validate (pspec, &default_val)) {
Packit f546b1
    g_critical ("GstParamSpec of type 'fraction' for property '%s' has a "
Packit f546b1
        "default value of %d/%d, which is not within the allowed range of "
Packit f546b1
        "%d/%d to %d/%d", name, default_num, default_denom, min_num,
Packit f546b1
        min_denom, max_num, max_denom);
Packit f546b1
    g_param_spec_ref (pspec);
Packit f546b1
    g_param_spec_sink (pspec);
Packit f546b1
    g_param_spec_unref (pspec);
Packit f546b1
    pspec = NULL;
Packit f546b1
  }
Packit f546b1
  g_value_unset (&default_val);
Packit f546b1
Packit f546b1
  return pspec;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
_gst_param_array_init (GParamSpec * pspec)
Packit f546b1
{
Packit f546b1
  GstParamSpecArray *aspec = GST_PARAM_SPEC_ARRAY_LIST (pspec);
Packit f546b1
Packit f546b1
  aspec->element_spec = NULL;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
_gst_param_array_finalize (GParamSpec * pspec)
Packit f546b1
{
Packit f546b1
  GstParamSpecArray *aspec = GST_PARAM_SPEC_ARRAY_LIST (pspec);
Packit f546b1
  GParamSpecClass *parent_class =
Packit f546b1
      g_type_class_peek (g_type_parent (GST_TYPE_PARAM_ARRAY_LIST));
Packit f546b1
Packit f546b1
  if (aspec->element_spec) {
Packit f546b1
    g_param_spec_unref (aspec->element_spec);
Packit f546b1
    aspec->element_spec = NULL;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  parent_class->finalize (pspec);
Packit f546b1
}
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
_gst_param_array_validate (GParamSpec * pspec, GValue * value)
Packit f546b1
{
Packit f546b1
  GstParamSpecArray *aspec = GST_PARAM_SPEC_ARRAY_LIST (pspec);
Packit f546b1
  gboolean ret = FALSE;
Packit f546b1
Packit f546b1
  /* ensure array values validity against a present element spec */
Packit f546b1
  if (aspec->element_spec) {
Packit f546b1
    GParamSpec *element_spec = aspec->element_spec;
Packit f546b1
    guint i;
Packit f546b1
Packit f546b1
    for (i = 0; i < gst_value_array_get_size (value); i++) {
Packit f546b1
      GValue *element = (GValue *) gst_value_array_get_value (value, i);
Packit f546b1
Packit f546b1
      /* need to fixup value type, or ensure that the array value is initialized at all */
Packit f546b1
      if (!g_value_type_compatible (G_VALUE_TYPE (element),
Packit f546b1
              G_PARAM_SPEC_VALUE_TYPE (element_spec))) {
Packit f546b1
        if (G_VALUE_TYPE (element) != 0)
Packit f546b1
          g_value_unset (element);
Packit f546b1
        g_value_init (element, G_PARAM_SPEC_VALUE_TYPE (element_spec));
Packit f546b1
        g_param_value_set_default (element_spec, element);
Packit f546b1
        ret = TRUE;
Packit f546b1
      }
Packit f546b1
Packit f546b1
      /* validate array value against element_spec */
Packit f546b1
      if (g_param_value_validate (element_spec, element))
Packit f546b1
        ret = TRUE;
Packit f546b1
    }
Packit f546b1
  }
Packit f546b1
Packit f546b1
  return ret;
Packit f546b1
}
Packit f546b1
Packit f546b1
static gint
Packit f546b1
_gst_param_array_values_cmp (GParamSpec * pspec, const GValue * value1,
Packit f546b1
    const GValue * value2)
Packit f546b1
{
Packit f546b1
  GstParamSpecArray *aspec = GST_PARAM_SPEC_ARRAY_LIST (pspec);
Packit f546b1
  guint size1, size2;
Packit f546b1
Packit f546b1
  if (!value1 || !value2)
Packit f546b1
    return value2 ? -1 : value1 != value2;
Packit f546b1
Packit f546b1
  size1 = gst_value_array_get_size (value1);
Packit f546b1
  size2 = gst_value_array_get_size (value2);
Packit f546b1
Packit f546b1
  if (size1 != size2)
Packit f546b1
    return size1 < size2 ? -1 : 1;
Packit f546b1
  else if (!aspec->element_spec) {
Packit f546b1
    /* we need an element specification for comparisons, so there's not much
Packit f546b1
     * to compare here, try to at least provide stable lesser/greater result
Packit f546b1
     */
Packit f546b1
    return size1 < size2 ? -1 : size1 > size2;
Packit f546b1
  } else {                      /* size1 == size2 */
Packit f546b1
    guint i;
Packit f546b1
Packit f546b1
    for (i = 0; i < size1; i++) {
Packit f546b1
      const GValue *element1 = gst_value_array_get_value (value1, i);
Packit f546b1
      const GValue *element2 = gst_value_array_get_value (value2, i);
Packit f546b1
      gint cmp;
Packit f546b1
Packit f546b1
      /* need corresponding element types, provide stable result otherwise */
Packit f546b1
      if (G_VALUE_TYPE (element1) != G_VALUE_TYPE (element2))
Packit f546b1
        return G_VALUE_TYPE (element1) < G_VALUE_TYPE (element2) ? -1 : 1;
Packit f546b1
      cmp = g_param_values_cmp (aspec->element_spec, element1, element2);
Packit f546b1
      if (cmp)
Packit f546b1
        return cmp;
Packit f546b1
    }
Packit f546b1
    return 0;
Packit f546b1
  }
Packit f546b1
}
Packit f546b1
Packit f546b1
GType
Packit f546b1
gst_param_spec_array_get_type (void)
Packit f546b1
{
Packit f546b1
  static volatile GType gst_array_type = 0;
Packit f546b1
Packit f546b1
  /* register GST_TYPE_PARAM_FRACTION */
Packit f546b1
  if (g_once_init_enter (&gst_array_type)) {
Packit f546b1
    GType type;
Packit f546b1
    static GParamSpecTypeInfo pspec_info = {
Packit f546b1
      sizeof (GstParamSpecArray),       /* instance_size     */
Packit f546b1
      0,                        /* n_preallocs       */
Packit f546b1
      _gst_param_array_init,    /* instance_init     */
Packit f546b1
      G_TYPE_INVALID,           /* value_type        */
Packit f546b1
      _gst_param_array_finalize,        /* finalize          */
Packit f546b1
      NULL,                     /* value_set_default */
Packit f546b1
      _gst_param_array_validate,        /* value_validate    */
Packit f546b1
      _gst_param_array_values_cmp,      /* values_cmp        */
Packit f546b1
    };
Packit f546b1
    pspec_info.value_type = gst_value_array_get_type ();
Packit f546b1
    type = g_param_type_register_static ("GstParamArray", &pspec_info);
Packit f546b1
    g_once_init_leave (&gst_array_type, type);
Packit f546b1
  }
Packit f546b1
Packit f546b1
  return gst_array_type;
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_param_spec_array:
Packit f546b1
 * @name: canonical name of the property specified
Packit f546b1
 * @nick: nick name for the property specified
Packit f546b1
 * @blurb: description of the property specified
Packit f546b1
 * @element_spec: GParamSpec of the array
Packit f546b1
 * @flags: flags for the property specified
Packit f546b1
 *
Packit f546b1
 * This function creates a GstArray GParamSpec for use by objects/elements
Packit f546b1
 * that want to expose properties of GstArray type. This function is
Packit f546b1
 * typically * used in connection with g_object_class_install_property() in a
Packit f546b1
 * GObjects's instance_init function.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full): a newly created parameter specification
Packit f546b1
 *
Packit f546b1
 * Since: 1.14
Packit f546b1
 */
Packit f546b1
Packit f546b1
GParamSpec *
Packit f546b1
gst_param_spec_array (const gchar * name,
Packit f546b1
    const gchar * nick,
Packit f546b1
    const gchar * blurb, GParamSpec * element_spec, GParamFlags flags)
Packit f546b1
{
Packit f546b1
  GstParamSpecArray *aspec;
Packit f546b1
Packit f546b1
  g_return_val_if_fail (element_spec == NULL
Packit f546b1
      || G_IS_PARAM_SPEC (element_spec), NULL);
Packit f546b1
Packit f546b1
  aspec = g_param_spec_internal (GST_TYPE_PARAM_ARRAY_LIST,
Packit f546b1
      name, nick, blurb, flags);
Packit f546b1
  if (aspec == NULL)
Packit f546b1
    return NULL;
Packit f546b1
Packit f546b1
  if (element_spec) {
Packit f546b1
    aspec->element_spec = g_param_spec_ref (element_spec);
Packit f546b1
    g_param_spec_sink (element_spec);
Packit f546b1
  }
Packit f546b1
Packit f546b1
  return G_PARAM_SPEC (aspec);
Packit f546b1
}