gstreamer_core/gst/gstparamspecs.c
changeset 0 0e761a78d257
child 7 567bb019e3e3
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer - GParamSpecs for some of our types
       
     2  * Copyright (C) 2007 Tim-Philipp Müller  <tim centricular net>
       
     3  *
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Library General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Library General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Library General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 
       
    20 #ifdef HAVE_CONFIG_H
       
    21 #include "config.h"
       
    22 #endif
       
    23 
       
    24 #include "gst_private.h"
       
    25 #include "glib-compat-private.h"
       
    26 #include "gstparamspecs.h"
       
    27 
       
    28 /* --- GstParamSpecFraction --- */
       
    29 
       
    30 static void
       
    31 _gst_param_fraction_init (GParamSpec * pspec)
       
    32 {
       
    33   GstParamSpecFraction *fspec = GST_PARAM_SPEC_FRACTION (pspec);
       
    34 
       
    35   fspec->min_num = 0;
       
    36   fspec->min_den = 1;
       
    37   fspec->max_num = G_MAXINT;
       
    38   fspec->max_den = 1;
       
    39   fspec->def_num = 1;
       
    40   fspec->def_den = 1;
       
    41 }
       
    42 
       
    43 static void
       
    44 _gst_param_fraction_set_default (GParamSpec * pspec, GValue * value)
       
    45 {
       
    46   value->data[0].v_int = GST_PARAM_SPEC_FRACTION (pspec)->def_num;
       
    47   value->data[1].v_int = GST_PARAM_SPEC_FRACTION (pspec)->def_den;
       
    48 }
       
    49 
       
    50 static gboolean
       
    51 _gst_param_fraction_validate (GParamSpec * pspec, GValue * value)
       
    52 {
       
    53   GstParamSpecFraction *fspec = GST_PARAM_SPEC_FRACTION (pspec);
       
    54   gboolean within_range = FALSE;
       
    55   GValue f_this = { 0, };
       
    56   GValue f_min = { 0, };
       
    57   GValue f_max = { 0, };
       
    58   gint res;
       
    59 
       
    60   g_value_init (&f_this, GST_TYPE_FRACTION);
       
    61   gst_value_set_fraction (&f_this, value->data[0].v_int, value->data[1].v_int);
       
    62 
       
    63   g_value_init (&f_min, GST_TYPE_FRACTION);
       
    64   gst_value_set_fraction (&f_min, fspec->min_num, fspec->min_den);
       
    65 
       
    66   g_value_init (&f_max, GST_TYPE_FRACTION);
       
    67   gst_value_set_fraction (&f_max, fspec->max_num, fspec->max_den);
       
    68 
       
    69   res = gst_value_compare (&f_min, &f_this);
       
    70 #ifndef GST_DISABLE_GST_DEBUG
       
    71   GST_LOG ("comparing %d/%d to %d/%d, result = %d", fspec->min_num,
       
    72       fspec->min_den, value->data[0].v_int, value->data[1].v_int, res);
       
    73 #endif
       
    74   if (res != GST_VALUE_LESS_THAN && res != GST_VALUE_EQUAL)
       
    75     goto out;
       
    76 
       
    77 #ifndef GST_DISABLE_GST_DEBUG
       
    78   GST_LOG ("comparing %d/%d to %d/%d, result = %d", value->data[0].v_int,
       
    79       value->data[1].v_int, fspec->max_num, fspec->max_den, res);
       
    80 #endif
       
    81   res = gst_value_compare (&f_this, &f_max);
       
    82   if (res != GST_VALUE_LESS_THAN && res != GST_VALUE_EQUAL)
       
    83     goto out;
       
    84 
       
    85   within_range = TRUE;
       
    86 
       
    87 out:
       
    88 
       
    89   g_value_unset (&f_min);
       
    90   g_value_unset (&f_max);
       
    91   g_value_unset (&f_this);
       
    92 
       
    93 #ifndef GST_DISABLE_GST_DEBUG
       
    94   GST_LOG ("%swithin range", (within_range) ? "" : "not ");
       
    95 #endif
       
    96 
       
    97   /* return FALSE if everything ok, otherwise TRUE */
       
    98   return !within_range;
       
    99 }
       
   100 
       
   101 static gint
       
   102 _gst_param_fraction_values_cmp (GParamSpec * pspec, const GValue * value1,
       
   103     const GValue * value2)
       
   104 {
       
   105   gint res;
       
   106 
       
   107   res = gst_value_compare (value1, value2);
       
   108 
       
   109   g_assert (res != GST_VALUE_UNORDERED);
       
   110 
       
   111   /* GST_VALUE_LESS_THAN is -1, EQUAL is 0, and GREATER_THAN is 1 */
       
   112   return res;
       
   113 }
       
   114 #ifdef __SYMBIAN32__
       
   115 EXPORT_C
       
   116 #endif
       
   117 
       
   118 
       
   119 GType
       
   120 gst_param_spec_fraction_get_type (void)
       
   121 {
       
   122   static GType type;            /* 0 */
       
   123 
       
   124   /* register GST_TYPE_PARAM_FRACTION */
       
   125   if (type == 0) {
       
   126     static GParamSpecTypeInfo pspec_info = {
       
   127       sizeof (GstParamSpecFraction),    /* instance_size     */
       
   128       0,                        /* n_preallocs       */
       
   129       _gst_param_fraction_init, /* instance_init     */
       
   130       G_TYPE_INVALID,           /* value_type        */
       
   131       NULL,                     /* finalize          */
       
   132       _gst_param_fraction_set_default,  /* value_set_default */
       
   133       _gst_param_fraction_validate,     /* value_validate    */
       
   134       _gst_param_fraction_values_cmp,   /* values_cmp        */
       
   135     };
       
   136     pspec_info.value_type = GST_TYPE_FRACTION;
       
   137     type = g_param_type_register_static ("GstParamFraction", &pspec_info);
       
   138   }
       
   139   return type;
       
   140 }
       
   141 
       
   142 /**
       
   143  * gst_param_spec_fraction:
       
   144  * @name: canonical name of the property specified
       
   145  * @nick: nick name for the property specified
       
   146  * @blurb: description of the property specified
       
   147  * @min_num: minimum value (fraction numerator)
       
   148  * @min_denom: minimum value (fraction denominator)
       
   149  * @max_num: maximum value (fraction numerator)
       
   150  * @max_denom: maximum value (fraction denominator)
       
   151  * @default_num: default value (fraction numerator)
       
   152  * @default_denom: default value (fraction denominator)
       
   153  * @flags: flags for the property specified
       
   154  *
       
   155  * This function creates a fraction GParamSpec for use by objects/elements
       
   156  * that want to expose properties of fraction type. This function is typically
       
   157  * used in connection with g_object_class_install_property() in a GObjects's
       
   158  * instance_init function.
       
   159  *
       
   160  * Returns: a newly created parameter specification
       
   161  *
       
   162  * Since: 0.10.14
       
   163  */
       
   164 #ifdef __SYMBIAN32__
       
   165 EXPORT_C
       
   166 #endif
       
   167 
       
   168 GParamSpec *
       
   169 gst_param_spec_fraction (const gchar * name, const gchar * nick,
       
   170     const gchar * blurb, gint min_num, gint min_denom, gint max_num,
       
   171     gint max_denom, gint default_num, gint default_denom, GParamFlags flags)
       
   172 {
       
   173   GstParamSpecFraction *fspec;
       
   174   GParamSpec *pspec;
       
   175   GValue default_val = { 0, };
       
   176 
       
   177   fspec =
       
   178       g_param_spec_internal (GST_TYPE_PARAM_FRACTION, name, nick, blurb, flags);
       
   179 
       
   180   fspec->min_num = min_num;
       
   181   fspec->min_den = min_denom;
       
   182   fspec->max_num = max_num;
       
   183   fspec->max_den = max_denom;
       
   184   fspec->def_num = default_num;
       
   185   fspec->def_den = default_denom;
       
   186 
       
   187   pspec = G_PARAM_SPEC (fspec);
       
   188 
       
   189   /* check that min <= default <= max */
       
   190   g_value_init (&default_val, GST_TYPE_FRACTION);
       
   191   gst_value_set_fraction (&default_val, default_num, default_denom);
       
   192   /* validate returns TRUE if the validation fails */
       
   193   if (_gst_param_fraction_validate (pspec, &default_val)) {
       
   194     g_critical ("GstParamSpec of type 'fraction' for property '%s' has a "
       
   195         "default value of %d/%d, which is not within the allowed range of "
       
   196         "%d/%d to %d/%d", name, default_num, default_denom, min_num,
       
   197         min_denom, max_num, max_denom);
       
   198     g_param_spec_ref (pspec);
       
   199     g_param_spec_sink (pspec);
       
   200     g_param_spec_unref (pspec);
       
   201     pspec = NULL;
       
   202   }
       
   203   g_value_unset (&default_val);
       
   204 
       
   205   return pspec;
       
   206 }