gst_plugins_base/gst-libs/gst/audio/gstaudiofilter.c
changeset 0 0e761a78d257
child 8 4a7fac7dd34a
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer audio filter base class
       
     2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
       
     3  * Copyright (C) <2003> David Schleef <ds@schleef.org>
       
     4  * Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
       
     5  *
       
     6  * This library is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU Library General Public
       
     8  * License as published by the Free Software Foundation; either
       
     9  * version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This library is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  * Library General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU Library General Public
       
    17  * License along with this library; if not, write to the
       
    18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    19  * Boston, MA 02111-1307, USA.
       
    20  */
       
    21 
       
    22 /**
       
    23  * SECTION:gstaudiofilter
       
    24  * @short_description: Base class for simple audio filters
       
    25  *
       
    26  * #GstAudioFilter is a #GstBaseTransform-derived base class for simple audio
       
    27  * filters, ie. those that output the same format that they get as input.
       
    28  *
       
    29  * #GstAudioFilter will parse the input format for you (with error checking)
       
    30  * before calling your setup function. Also, elements deriving from
       
    31  * #GstAudioFilter may use gst_audio_filter_class_add_pad_templates() from
       
    32  * their base_init function to easily configure the set of caps/formats that
       
    33  * the element is able to handle.
       
    34  *
       
    35  * Derived classes should override the GstAudioFilter::setup() and
       
    36  * GstBaseTransform::transform_ip() and/or GstBaseTransform::transform()
       
    37  * virtual functions in their class_init function.
       
    38  *
       
    39  * Since: 0.10.12
       
    40  *
       
    41  * Last reviewed on 2007-02-03 (0.10.11.1)
       
    42  */
       
    43 
       
    44 #ifdef HAVE_CONFIG_H
       
    45 #include "config.h"
       
    46 #endif
       
    47 
       
    48 #include "gstaudiofilter.h"
       
    49 
       
    50 #include <string.h>
       
    51 
       
    52 GST_DEBUG_CATEGORY_STATIC (audiofilter_dbg);
       
    53 #define GST_CAT_DEFAULT audiofilter_dbg
       
    54 
       
    55 static void gst_audio_filter_class_init (gpointer g_class, gpointer class_data);
       
    56 static void gst_audio_filter_init (GTypeInstance * instance, gpointer g_class);
       
    57 static GstStateChangeReturn gst_audio_filter_change_state (GstElement * element,
       
    58     GstStateChange transition);
       
    59 static gboolean gst_audio_filter_set_caps (GstBaseTransform * btrans,
       
    60     GstCaps * incaps, GstCaps * outcaps);
       
    61 static gboolean gst_audio_filter_get_unit_size (GstBaseTransform * btrans,
       
    62     GstCaps * caps, guint * size);
       
    63 
       
    64 static GstElementClass *parent_class = NULL;
       
    65 #ifdef __SYMBIAN32__
       
    66 EXPORT_C
       
    67 #endif
       
    68 
       
    69 
       
    70 GType
       
    71 gst_audio_filter_get_type (void)
       
    72 {
       
    73   static GType audio_filter_type = 0;
       
    74 
       
    75   if (!audio_filter_type) {
       
    76     const GTypeInfo audio_filter_info = {
       
    77       sizeof (GstAudioFilterClass),
       
    78       NULL,
       
    79       NULL,
       
    80       gst_audio_filter_class_init,
       
    81       NULL,
       
    82       NULL,
       
    83       sizeof (GstAudioFilter),
       
    84       0,
       
    85       gst_audio_filter_init,
       
    86     };
       
    87 
       
    88     GST_DEBUG_CATEGORY_INIT (audiofilter_dbg, "audiofilter", 0, "audiofilter");
       
    89 
       
    90     audio_filter_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
       
    91         "GstAudioFilter", &audio_filter_info, G_TYPE_FLAG_ABSTRACT);
       
    92   }
       
    93   return audio_filter_type;
       
    94 }
       
    95 
       
    96 static void
       
    97 gst_audio_filter_class_init (gpointer klass, gpointer class_data)
       
    98 {
       
    99   GstBaseTransformClass *basetrans_class;
       
   100   GstElementClass *gstelement_class;
       
   101 
       
   102   parent_class = g_type_class_peek_parent (klass);
       
   103 
       
   104   gstelement_class = (GstElementClass *) klass;
       
   105   basetrans_class = (GstBaseTransformClass *) klass;
       
   106 
       
   107   gstelement_class->change_state =
       
   108       GST_DEBUG_FUNCPTR (gst_audio_filter_change_state);
       
   109   basetrans_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_filter_set_caps);
       
   110   basetrans_class->get_unit_size =
       
   111       GST_DEBUG_FUNCPTR (gst_audio_filter_get_unit_size);
       
   112 
       
   113   /* FIXME: Ref the GstRingerBuffer class to get it's debug category
       
   114    * initialized. gst_ring_buffer_parse_caps () which we use later
       
   115    * uses this debug category.
       
   116    */
       
   117   g_type_class_ref (GST_TYPE_RING_BUFFER);
       
   118 }
       
   119 
       
   120 static void
       
   121 gst_audio_filter_init (GTypeInstance * instance, gpointer g_class)
       
   122 {
       
   123   /* nothing to do here */
       
   124 }
       
   125 
       
   126 /* we override the state change vfunc here instead of GstBaseTransform's stop
       
   127  * vfunc, so GstAudioFilter-derived elements can override ::stop() for their
       
   128  * own purposes without having to worry about chaining up */
       
   129 static GstStateChangeReturn
       
   130 gst_audio_filter_change_state (GstElement * element, GstStateChange transition)
       
   131 {
       
   132   GstStateChangeReturn ret;
       
   133   GstAudioFilter *filter;
       
   134 
       
   135   filter = GST_AUDIO_FILTER (element);
       
   136 
       
   137   switch (transition) {
       
   138     case GST_STATE_CHANGE_NULL_TO_READY:
       
   139       memset (&filter->format, 0, sizeof (GstRingBufferSpec));
       
   140       /* to make gst_buffer_spec_parse_caps() happy */
       
   141       filter->format.latency_time = GST_SECOND;
       
   142       break;
       
   143     default:
       
   144       break;
       
   145   }
       
   146 
       
   147   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
       
   148   if (ret == GST_STATE_CHANGE_FAILURE)
       
   149     return ret;
       
   150 
       
   151   switch (transition) {
       
   152     case GST_STATE_CHANGE_PAUSED_TO_READY:
       
   153     case GST_STATE_CHANGE_READY_TO_NULL:
       
   154       gst_caps_replace (&filter->format.caps, NULL);
       
   155       break;
       
   156     default:
       
   157       break;
       
   158   }
       
   159 
       
   160   return ret;
       
   161 }
       
   162 
       
   163 static gboolean
       
   164 gst_audio_filter_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
       
   165     GstCaps * outcaps)
       
   166 {
       
   167   GstAudioFilterClass *klass;
       
   168   GstAudioFilter *filter;
       
   169   gboolean ret = TRUE;
       
   170 
       
   171   g_assert (gst_caps_is_equal (incaps, outcaps));
       
   172 
       
   173   filter = GST_AUDIO_FILTER (btrans);
       
   174 
       
   175   GST_LOG_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
       
   176 
       
   177   if (!gst_ring_buffer_parse_caps (&filter->format, incaps)) {
       
   178     GST_WARNING_OBJECT (filter, "couldn't parse %" GST_PTR_FORMAT, incaps);
       
   179     return FALSE;
       
   180   }
       
   181 
       
   182   klass = GST_AUDIO_FILTER_CLASS (G_OBJECT_GET_CLASS (filter));
       
   183 
       
   184   if (klass->setup)
       
   185     ret = klass->setup (filter, &filter->format);
       
   186 
       
   187   return ret;
       
   188 }
       
   189 
       
   190 static gboolean
       
   191 gst_audio_filter_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
       
   192     guint * size)
       
   193 {
       
   194   GstStructure *structure;
       
   195   gboolean ret = TRUE;
       
   196   gint width, channels;
       
   197 
       
   198   structure = gst_caps_get_structure (caps, 0);
       
   199 
       
   200   ret &= gst_structure_get_int (structure, "width", &width);
       
   201   ret &= gst_structure_get_int (structure, "channels", &channels);
       
   202 
       
   203   if (ret)
       
   204     *size = (width / 8) * channels;
       
   205 
       
   206   return ret;
       
   207 }
       
   208 
       
   209 /**
       
   210  * gst_audio_filter_class_add_pad_templates:
       
   211  * @klass: an #GstAudioFilterClass
       
   212  * @allowed_caps: what formats the filter can handle, as #GstCaps
       
   213  *
       
   214  * Convenience function to add pad templates to this element class, with
       
   215  * @allowed_caps as the caps that can be handled.
       
   216  *
       
   217  * This function is usually used from within a GObject base_init function.
       
   218  *
       
   219  * Since: 0.10.12
       
   220  */
       
   221 #ifdef __SYMBIAN32__
       
   222 EXPORT_C
       
   223 #endif
       
   224 
       
   225 void
       
   226 gst_audio_filter_class_add_pad_templates (GstAudioFilterClass * klass,
       
   227     const GstCaps * allowed_caps)
       
   228 {
       
   229   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
       
   230 
       
   231   g_return_if_fail (GST_IS_AUDIO_FILTER_CLASS (klass));
       
   232   g_return_if_fail (allowed_caps != NULL);
       
   233   g_return_if_fail (GST_IS_CAPS (allowed_caps));
       
   234 
       
   235   gst_element_class_add_pad_template (element_class,
       
   236       gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
       
   237           gst_caps_copy (allowed_caps)));
       
   238 
       
   239   gst_element_class_add_pad_template (element_class,
       
   240       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
       
   241           gst_caps_copy (allowed_caps)));
       
   242 }