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>
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.
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.
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.
23 * SECTION:gstaudiofilter
24 * @short_description: Base class for simple audio filters
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.
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.
35 * Derived classes should override the #GstAudioFilterClass.setup() and
36 * #GstBaseTransformClass.transform_ip() and/or
37 * #GstBaseTransformClass.transform()
38 * virtual functions in their class_init function.
40 * Last reviewed on 2007-02-03 (0.10.11.1)
49 #include "gstaudiofilter.h"
53 GST_DEBUG_CATEGORY_STATIC (audiofilter_dbg);
54 #define GST_CAT_DEFAULT audiofilter_dbg
56 static GstStateChangeReturn gst_audio_filter_change_state (GstElement * element,
57 GstStateChange transition);
58 static gboolean gst_audio_filter_set_caps (GstBaseTransform * btrans,
59 GstCaps * incaps, GstCaps * outcaps);
60 static gboolean gst_audio_filter_get_unit_size (GstBaseTransform * btrans,
61 GstCaps * caps, guint * size);
63 #define do_init G_STMT_START { \
64 GST_DEBUG_CATEGORY_INIT (audiofilter_dbg, "audiofilter", 0, "audiofilter"); \
67 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstAudioFilter, gst_audio_filter,
68 GST_TYPE_BASE_TRANSFORM, do_init);
71 gst_audio_filter_class_init (GstAudioFilterClass * klass)
73 GstBaseTransformClass *basetrans_class = (GstBaseTransformClass *) klass;
74 GstElementClass *gstelement_class = (GstElementClass *) klass;
76 gstelement_class->change_state =
77 GST_DEBUG_FUNCPTR (gst_audio_filter_change_state);
78 basetrans_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_filter_set_caps);
79 basetrans_class->get_unit_size =
80 GST_DEBUG_FUNCPTR (gst_audio_filter_get_unit_size);
82 /* FIXME: Ref the GstRingerBuffer class to get it's debug category
83 * initialized. gst_ring_buffer_parse_caps () which we use later
84 * uses this debug category.
86 g_type_class_ref (GST_TYPE_RING_BUFFER);
90 gst_audio_filter_init (GstAudioFilter * self)
92 /* nothing to do here */
95 /* we override the state change vfunc here instead of GstBaseTransform's stop
96 * vfunc, so GstAudioFilter-derived elements can override ::stop() for their
97 * own purposes without having to worry about chaining up */
98 static GstStateChangeReturn
99 gst_audio_filter_change_state (GstElement * element, GstStateChange transition)
101 GstStateChangeReturn ret;
102 GstAudioFilter *filter = GST_AUDIO_FILTER (element);
104 switch (transition) {
105 case GST_STATE_CHANGE_NULL_TO_READY:
106 memset (&filter->format, 0, sizeof (GstRingBufferSpec));
107 /* to make gst_buffer_spec_parse_caps() happy */
108 filter->format.latency_time = GST_SECOND;
115 GST_ELEMENT_CLASS (gst_audio_filter_parent_class)->change_state (element,
117 if (ret == GST_STATE_CHANGE_FAILURE)
120 switch (transition) {
121 case GST_STATE_CHANGE_PAUSED_TO_READY:
122 case GST_STATE_CHANGE_READY_TO_NULL:
123 gst_caps_replace (&filter->format.caps, NULL);
133 gst_audio_filter_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
136 GstAudioFilterClass *klass;
137 GstAudioFilter *filter = GST_AUDIO_FILTER (btrans);
140 GST_LOG_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
142 if (!gst_ring_buffer_parse_caps (&filter->format, incaps)) {
143 GST_WARNING_OBJECT (filter, "couldn't parse %" GST_PTR_FORMAT, incaps);
147 klass = GST_AUDIO_FILTER_CLASS_CAST (G_OBJECT_GET_CLASS (filter));
150 ret = klass->setup (filter, &filter->format);
156 gst_audio_filter_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
159 GstStructure *structure;
161 gint width, channels;
163 structure = gst_caps_get_structure (caps, 0);
165 ret &= gst_structure_get_int (structure, "width", &width);
166 ret &= gst_structure_get_int (structure, "channels", &channels);
169 *size = (width / 8) * channels;
175 * gst_audio_filter_class_add_pad_templates:
176 * @klass: an #GstAudioFilterClass
177 * @allowed_caps: what formats the filter can handle, as #GstCaps
179 * Convenience function to add pad templates to this element class, with
180 * @allowed_caps as the caps that can be handled.
182 * This function is usually used from within a GObject base_init function.
187 gst_audio_filter_class_add_pad_templates (GstAudioFilterClass * klass,
188 const GstCaps * allowed_caps)
190 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
191 GstPadTemplate *pad_template;
193 g_return_if_fail (GST_IS_AUDIO_FILTER_CLASS (klass));
194 g_return_if_fail (GST_IS_CAPS (allowed_caps));
196 pad_template = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
197 gst_caps_copy (allowed_caps));
198 gst_element_class_add_pad_template (element_class, pad_template);
199 gst_object_unref (pad_template);
201 pad_template = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
202 gst_caps_copy (allowed_caps));
203 gst_element_class_add_pad_template (element_class, pad_template);
204 gst_object_unref (pad_template);