gstreamer_core/gst/gstformat.c
changeset 0 0e761a78d257
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
       
     3  *                    2000 Wim Taymans <wim.taymans@chello.be>
       
     4  *                    2005 Wim Taymans <wim@fluendo.com>
       
     5  *
       
     6  * gstformat.c: GstFormat registration
       
     7  *
       
     8  * This library is free software; you can redistribute it and/or
       
     9  * modify it under the terms of the GNU Library General Public
       
    10  * License as published by the Free Software Foundation; either
       
    11  * version 2 of the License, or (at your option) any later version.
       
    12  *
       
    13  * This library is distributed in the hope that it will be useful,
       
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16  * Library General Public License for more details.
       
    17  *
       
    18  * You should have received a copy of the GNU Library General Public
       
    19  * License along with this library; if not, write to the
       
    20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    21  * Boston, MA 02111-1307, USA.
       
    22  */
       
    23 
       
    24 /**
       
    25  * SECTION:gstformat
       
    26  * @short_description: Dynamically register new data formats
       
    27  * @see_also: #GstPad, #GstElement
       
    28  *
       
    29  * GstFormats functions are used to register a new format to the gstreamer
       
    30  * core.  Formats can be used to perform seeking or conversions/query
       
    31  * operations.
       
    32  */
       
    33 
       
    34 
       
    35 #include "gst_private.h"
       
    36 #include <string.h>
       
    37 #include "gstformat.h"
       
    38 #include "gstenumtypes.h"
       
    39 
       
    40 #ifdef __SYMBIAN32__
       
    41 #include <glib_global.h>
       
    42 #endif
       
    43 
       
    44 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
       
    45 static GList *_gst_formats = NULL;
       
    46 static GHashTable *_nick_to_format = NULL;
       
    47 static GHashTable *_format_to_nick = NULL;
       
    48 static guint32 _n_values = 1;   /* we start from 1 because 0 reserved for UNDEFINED */
       
    49 
       
    50 static GstFormatDefinition standard_definitions[] = {
       
    51   {GST_FORMAT_DEFAULT, "default", "Default format for the media type", 0},
       
    52   {GST_FORMAT_BYTES, "bytes", "Bytes", 0},
       
    53   {GST_FORMAT_TIME, "time", "Time", 0},
       
    54   {GST_FORMAT_BUFFERS, "buffers", "Buffers", 0},
       
    55   {GST_FORMAT_PERCENT, "percent", "Percent", 0},
       
    56   {0, NULL, NULL, 0}
       
    57 };
       
    58 #ifdef __SYMBIAN32__
       
    59 EXPORT_C
       
    60 #endif
       
    61 
       
    62 
       
    63 void
       
    64 _gst_format_initialize (void)
       
    65 {
       
    66   GstFormatDefinition *standards = standard_definitions;
       
    67 
       
    68   g_static_mutex_lock (&mutex);
       
    69   if (_nick_to_format == NULL) {
       
    70     _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
       
    71     _format_to_nick = g_hash_table_new (NULL, NULL);
       
    72   }
       
    73 
       
    74   while (standards->nick) {
       
    75     standards->quark = g_quark_from_static_string (standards->nick);
       
    76     g_hash_table_insert (_nick_to_format, standards->nick, standards);
       
    77     g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
       
    78         standards);
       
    79 
       
    80     _gst_formats = g_list_append (_gst_formats, standards);
       
    81     standards++;
       
    82     _n_values++;
       
    83   }
       
    84   /* getting the type registers the enum */
       
    85   g_type_class_ref (gst_format_get_type ());
       
    86   g_static_mutex_unlock (&mutex);
       
    87 }
       
    88 
       
    89 /**
       
    90  * gst_format_get_name:
       
    91  * @format: a #GstFormat
       
    92  *
       
    93  * Get a printable name for the given format. Do not modify or free.
       
    94  *
       
    95  * Returns: a reference to the static name of the format or NULL if
       
    96  * the format is unknown.
       
    97  */
       
    98 #ifdef __SYMBIAN32__
       
    99 EXPORT_C
       
   100 #endif
       
   101 
       
   102 const gchar *
       
   103 gst_format_get_name (GstFormat format)
       
   104 {
       
   105   const GstFormatDefinition *def;
       
   106   const gchar *result;
       
   107 
       
   108   if ((def = gst_format_get_details (format)) != NULL)
       
   109     result = def->nick;
       
   110   else
       
   111     result = NULL;
       
   112 
       
   113   return result;
       
   114 }
       
   115 
       
   116 /**
       
   117  * gst_format_to_quark:
       
   118  * @format: a #GstFormat
       
   119  *
       
   120  * Get the unique quark for the given format.
       
   121  *
       
   122  * Returns: the quark associated with the format or 0 if the format
       
   123  * is unknown.
       
   124  */
       
   125 #ifdef __SYMBIAN32__
       
   126 EXPORT_C
       
   127 #endif
       
   128 
       
   129 GQuark
       
   130 gst_format_to_quark (GstFormat format)
       
   131 {
       
   132   const GstFormatDefinition *def;
       
   133   GQuark result;
       
   134 
       
   135   if ((def = gst_format_get_details (format)) != NULL)
       
   136     result = def->quark;
       
   137   else
       
   138     result = 0;
       
   139 
       
   140   return result;
       
   141 }
       
   142 
       
   143 /**
       
   144  * gst_format_register:
       
   145  * @nick: The nick of the new format
       
   146  * @description: The description of the new format
       
   147  *
       
   148  * Create a new GstFormat based on the nick or return an
       
   149  * already registered format with that nick.
       
   150  *
       
   151  * Returns: A new GstFormat or an already registered format
       
   152  * with the same nick.
       
   153  *
       
   154  * MT safe.
       
   155  */
       
   156 #ifdef __SYMBIAN32__
       
   157 EXPORT_C
       
   158 #endif
       
   159 
       
   160 GstFormat
       
   161 gst_format_register (const gchar * nick, const gchar * description)
       
   162 {
       
   163   GstFormatDefinition *format;
       
   164   GstFormat query;
       
   165 
       
   166   g_return_val_if_fail (nick != NULL, 0);
       
   167   g_return_val_if_fail (description != NULL, 0);
       
   168 
       
   169   query = gst_format_get_by_nick (nick);
       
   170   if (query != GST_FORMAT_UNDEFINED)
       
   171     return query;
       
   172 
       
   173   g_static_mutex_lock (&mutex);
       
   174   format = g_new0 (GstFormatDefinition, 1);
       
   175   format->value = _n_values;
       
   176   format->nick = g_strdup (nick);
       
   177   format->description = g_strdup (description);
       
   178   format->quark = g_quark_from_static_string (format->nick);
       
   179 
       
   180   g_hash_table_insert (_nick_to_format, format->nick, format);
       
   181   g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
       
   182       format);
       
   183   _gst_formats = g_list_append (_gst_formats, format);
       
   184   _n_values++;
       
   185   g_static_mutex_unlock (&mutex);
       
   186 
       
   187   return format->value;
       
   188 }
       
   189 
       
   190 /**
       
   191  * gst_format_get_by_nick:
       
   192  * @nick: The nick of the format
       
   193  *
       
   194  * Return the format registered with the given nick.
       
   195  *
       
   196  * Returns: The format with @nick or GST_FORMAT_UNDEFINED
       
   197  * if the format was not registered.
       
   198  */
       
   199 #ifdef __SYMBIAN32__
       
   200 EXPORT_C
       
   201 #endif
       
   202 
       
   203 GstFormat
       
   204 gst_format_get_by_nick (const gchar * nick)
       
   205 {
       
   206   GstFormatDefinition *format;
       
   207 
       
   208   g_return_val_if_fail (nick != NULL, 0);
       
   209 
       
   210   g_static_mutex_lock (&mutex);
       
   211   format = g_hash_table_lookup (_nick_to_format, nick);
       
   212   g_static_mutex_unlock (&mutex);
       
   213 
       
   214   if (format != NULL)
       
   215     return format->value;
       
   216   else
       
   217     return GST_FORMAT_UNDEFINED;
       
   218 }
       
   219 
       
   220 /**
       
   221  * gst_formats_contains:
       
   222  * @formats: The format array to search
       
   223  * @format: the format to find
       
   224  *
       
   225  * See if the given format is inside the format array.
       
   226  *
       
   227  * Returns: TRUE if the format is found inside the array
       
   228  */
       
   229 #ifdef __SYMBIAN32__
       
   230 EXPORT_C
       
   231 #endif
       
   232 
       
   233 gboolean
       
   234 gst_formats_contains (const GstFormat * formats, GstFormat format)
       
   235 {
       
   236   if (!formats)
       
   237     return FALSE;
       
   238 
       
   239   while (*formats) {
       
   240     if (*formats == format)
       
   241       return TRUE;
       
   242 
       
   243     formats++;
       
   244   }
       
   245   return FALSE;
       
   246 }
       
   247 
       
   248 
       
   249 /**
       
   250  * gst_format_get_details:
       
   251  * @format: The format to get details of
       
   252  *
       
   253  * Get details about the given format.
       
   254  *
       
   255  * Returns: The #GstFormatDefinition for @format or NULL on failure.
       
   256  *
       
   257  * MT safe.
       
   258  */
       
   259 #ifdef __SYMBIAN32__
       
   260 EXPORT_C
       
   261 #endif
       
   262 
       
   263 const GstFormatDefinition *
       
   264 gst_format_get_details (GstFormat format)
       
   265 {
       
   266   const GstFormatDefinition *result;
       
   267 
       
   268   g_static_mutex_lock (&mutex);
       
   269   result = g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
       
   270   g_static_mutex_unlock (&mutex);
       
   271 
       
   272   return result;
       
   273 }
       
   274 
       
   275 /**
       
   276  * gst_format_iterate_definitions:
       
   277  *
       
   278  * Iterate all the registered formats. The format definition is read
       
   279  * only.
       
   280  *
       
   281  * Returns: A GstIterator of #GstFormatDefinition.
       
   282  */
       
   283 #ifdef __SYMBIAN32__
       
   284 EXPORT_C
       
   285 #endif
       
   286 
       
   287 GstIterator *
       
   288 gst_format_iterate_definitions (void)
       
   289 {
       
   290   GstIterator *result;
       
   291 
       
   292   g_static_mutex_lock (&mutex);
       
   293   /* FIXME: register a boxed type for GstFormatDefinition */
       
   294   result = gst_iterator_new_list (G_TYPE_POINTER,
       
   295       g_static_mutex_get_mutex (&mutex), &_n_values, &_gst_formats,
       
   296       NULL, NULL, NULL);
       
   297   g_static_mutex_unlock (&mutex);
       
   298 
       
   299   return result;
       
   300 }