gstreamer/gst-libs/gst/audio/gstaudiofilter.c

236 lines
7.1 KiB
C
Raw Normal View History

/* GStreamer audio filter base class
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
* Copyright (C) <2003> David Schleef <ds@schleef.org>
* Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/**
* SECTION:gstaudiofilter
* @short_description: Base class for simple audio filters
*
* #GstAudioFilter is a #GstBaseTransform-derived base class for simple audio
* filters, ie. those that output the same format that they get as input.
*
* #GstAudioFilter will parse the input format for you (with error checking)
* before calling your setup function. Also, elements deriving from
* #GstAudioFilter may use gst_audio_filter_class_add_pad_templates() from
* their base_init function to easily configure the set of caps/formats that
* the element is able to handle.
*
* Derived classes should override the #GstAudioFilterClass.setup() and
* #GstBaseTransformClass.transform_ip() and/or
* #GstBaseTransformClass.transform()
* virtual functions in their class_init function.
*
* Last reviewed on 2007-02-03 (0.10.11.1)
*
* Since: 0.10.12
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstaudiofilter.h"
#include <string.h>
GST_DEBUG_CATEGORY_STATIC (audiofilter_dbg);
#define GST_CAT_DEFAULT audiofilter_dbg
static void gst_audio_filter_class_init (gpointer g_class, gpointer class_data);
static void gst_audio_filter_init (GTypeInstance * instance, gpointer g_class);
static GstStateChangeReturn gst_audio_filter_change_state (GstElement * element,
GstStateChange transition);
static gboolean gst_audio_filter_set_caps (GstBaseTransform * btrans,
GstCaps * incaps, GstCaps * outcaps);
static gboolean gst_audio_filter_get_unit_size (GstBaseTransform * btrans,
GstCaps * caps, guint * size);
static GstElementClass *parent_class = NULL;
GType
gst_audio_filter_get_type (void)
{
static GType audio_filter_type = 0;
if (!audio_filter_type) {
const GTypeInfo audio_filter_info = {
sizeof (GstAudioFilterClass),
NULL,
NULL,
gst_audio_filter_class_init,
NULL,
NULL,
sizeof (GstAudioFilter),
0,
gst_audio_filter_init,
};
GST_DEBUG_CATEGORY_INIT (audiofilter_dbg, "audiofilter", 0, "audiofilter");
audio_filter_type = g_type_register_static (GST_TYPE_BASE_TRANSFORM,
"GstAudioFilter", &audio_filter_info, G_TYPE_FLAG_ABSTRACT);
}
return audio_filter_type;
}
static void
gst_audio_filter_class_init (gpointer klass, gpointer class_data)
{
GstBaseTransformClass *basetrans_class;
GstElementClass *gstelement_class;
Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent) Original commit message from CVS: * ext/alsa/gstalsamixeroptions.c: (gst_alsa_mixer_options_class_init): * ext/alsa/gstalsamixertrack.c: (gst_alsa_mixer_track_class_init): * ext/ogg/gstoggdemux.c: (gst_ogg_pad_class_init): * ext/ogg/gstoggmux.c: (gst_ogg_mux_class_init): * ext/ogg/gstoggparse.c: (gst_ogg_parse_class_init): * gst-libs/gst/audio/gstaudioclock.c: (gst_audio_clock_class_init): * gst-libs/gst/audio/gstaudiofilter.c: (gst_audio_filter_class_init): * gst-libs/gst/audio/gstaudiosink.c: (gst_audioringbuffer_class_init): * gst-libs/gst/audio/gstaudiosrc.c: (gst_audioringbuffer_class_init): * gst-libs/gst/audio/gstringbuffer.c: (gst_ring_buffer_class_init): * gst-libs/gst/interfaces/colorbalancechannel.c: (gst_color_balance_channel_class_init): * gst-libs/gst/interfaces/mixeroptions.c: (gst_mixer_options_class_init): * gst-libs/gst/interfaces/mixertrack.c: (gst_mixer_track_class_init): * gst-libs/gst/interfaces/tunerchannel.c: (gst_tuner_channel_class_init): * gst-libs/gst/interfaces/tunernorm.c: (gst_tuner_norm_class_init): * gst-libs/gst/netbuffer/gstnetbuffer.c: (gst_netbuffer_class_init): * gst-libs/gst/rtp/gstbasertppayload.c: (gst_basertppayload_class_init): * gst/playback/gstdecodebin.c: (gst_decode_bin_class_init): * gst/playback/gstplaybasebin.c: (gst_play_base_bin_class_init): * gst/playback/gstplaybin.c: (gst_play_bin_class_init): * gst/playback/gststreaminfo.c: (gst_stream_info_class_init): * gst/playback/gststreamselector.c: (gst_stream_selector_class_init): * gst/subparse/gstsubparse.c: (gst_sub_parse_class_init): * gst/tcp/gsttcpclientsink.c: (gst_tcp_client_sink_class_init): * sys/v4l/gstv4lcolorbalance.c: (gst_v4l_color_balance_channel_class_init): * sys/v4l/gstv4ljpegsrc.c: (gst_v4ljpegsrc_class_init): * sys/v4l/gstv4lmjpegsink.c: (gst_v4lmjpegsink_class_init): * sys/v4l/gstv4lmjpegsrc.c: (gst_v4lmjpegsrc_class_init): * sys/v4l/gstv4ltuner.c: (gst_v4l_tuner_channel_class_init), (gst_v4l_tuner_norm_class_init): * sys/ximage/ximagesink.c: (gst_ximagesink_class_init): * sys/xvimage/xvimagesink.c: (gst_xvimagesink_class_init): * tests/old/testsuite/alsa/sinesrc.c: (sinesrc_class_init): Fix #337365 (g_type_class_ref <-> g_type_class_peek_parent)
2006-04-08 21:02:53 +00:00
parent_class = g_type_class_peek_parent (klass);
gstelement_class = (GstElementClass *) klass;
basetrans_class = (GstBaseTransformClass *) klass;
gstelement_class->change_state =
GST_DEBUG_FUNCPTR (gst_audio_filter_change_state);
basetrans_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_filter_set_caps);
basetrans_class->get_unit_size =
GST_DEBUG_FUNCPTR (gst_audio_filter_get_unit_size);
/* FIXME: Ref the GstRingerBuffer class to get it's debug category
* initialized. gst_ring_buffer_parse_caps () which we use later
* uses this debug category.
*/
g_type_class_ref (GST_TYPE_RING_BUFFER);
}
static void
gst_audio_filter_init (GTypeInstance * instance, gpointer g_class)
{
/* nothing to do here */
}
/* we override the state change vfunc here instead of GstBaseTransform's stop
* vfunc, so GstAudioFilter-derived elements can override ::stop() for their
* own purposes without having to worry about chaining up */
static GstStateChangeReturn
gst_audio_filter_change_state (GstElement * element, GstStateChange transition)
{
GstStateChangeReturn ret;
GstAudioFilter *filter;
filter = GST_AUDIO_FILTER (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
memset (&filter->format, 0, sizeof (GstRingBufferSpec));
/* to make gst_buffer_spec_parse_caps() happy */
filter->format.latency_time = GST_SECOND;
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
if (ret == GST_STATE_CHANGE_FAILURE)
return ret;
switch (transition) {
case GST_STATE_CHANGE_PAUSED_TO_READY:
case GST_STATE_CHANGE_READY_TO_NULL:
gst_caps_replace (&filter->format.caps, NULL);
break;
default:
break;
}
return ret;
}
static gboolean
gst_audio_filter_set_caps (GstBaseTransform * btrans, GstCaps * incaps,
GstCaps * outcaps)
{
GstAudioFilterClass *klass;
GstAudioFilter *filter;
gboolean ret = TRUE;
g_assert (gst_caps_is_equal (incaps, outcaps));
filter = GST_AUDIO_FILTER (btrans);
GST_LOG_OBJECT (filter, "caps: %" GST_PTR_FORMAT, incaps);
if (!gst_ring_buffer_parse_caps (&filter->format, incaps)) {
GST_WARNING_OBJECT (filter, "couldn't parse %" GST_PTR_FORMAT, incaps);
return FALSE;
}
klass = GST_AUDIO_FILTER_CLASS (G_OBJECT_GET_CLASS (filter));
if (klass->setup)
ret = klass->setup (filter, &filter->format);
return ret;
}
static gboolean
gst_audio_filter_get_unit_size (GstBaseTransform * btrans, GstCaps * caps,
guint * size)
{
GstStructure *structure;
gboolean ret = TRUE;
gint width, channels;
structure = gst_caps_get_structure (caps, 0);
ret &= gst_structure_get_int (structure, "width", &width);
ret &= gst_structure_get_int (structure, "channels", &channels);
if (ret)
*size = (width / 8) * channels;
return ret;
}
/**
* gst_audio_filter_class_add_pad_templates:
* @klass: an #GstAudioFilterClass
* @allowed_caps: what formats the filter can handle, as #GstCaps
*
* Convenience function to add pad templates to this element class, with
* @allowed_caps as the caps that can be handled.
*
* This function is usually used from within a GObject base_init function.
*
* Since: 0.10.12
*/
void
gst_audio_filter_class_add_pad_templates (GstAudioFilterClass * klass,
const GstCaps * allowed_caps)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
g_return_if_fail (GST_IS_AUDIO_FILTER_CLASS (klass));
g_return_if_fail (allowed_caps != NULL);
g_return_if_fail (GST_IS_CAPS (allowed_caps));
gst_element_class_add_pad_template (element_class,
gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
gst_caps_copy (allowed_caps)));
gst_element_class_add_pad_template (element_class,
gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
gst_caps_copy (allowed_caps)));
}