2 * empathy-gst-audio-sink.c - Source for EmpathyGstAudioSink
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <gst/audio/audio.h>
26 #include <telepathy-glib/telepathy-glib.h>
28 #include <libempathy-gtk/empathy-call-utils.h>
30 #include "empathy-audio-sink.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
33 #include <libempathy/empathy-debug.h>
35 G_DEFINE_TYPE(EmpathyGstAudioSink, empathy_audio_sink, GST_TYPE_BIN)
44 static guint signals[LAST_SIGNAL] = {0};
47 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
51 GST_STATIC_CAPS ( GST_AUDIO_INT_PAD_TEMPLATE_CAPS " ; "
52 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
59 struct _EmpathyGstAudioSinkPrivate
64 #define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
65 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SINK, \
66 EmpathyGstAudioSinkPrivate))
69 empathy_audio_sink_init (EmpathyGstAudioSink *self)
71 self->priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
74 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
75 GstPadTemplate *templ,
78 static void empathy_audio_sink_release_pad (GstElement *self,
82 empathy_audio_sink_set_property (GObject *object,
83 guint property_id, const GValue *value, GParamSpec *pspec)
88 empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (object),
89 g_value_get_double (value));
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
97 empathy_audio_sink_get_property (GObject *object,
98 guint property_id, GValue *value, GParamSpec *pspec)
103 g_value_set_double (value,
104 empathy_audio_sink_get_volume (EMPATHY_GST_AUDIO_SINK (object)));
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
112 empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
113 *empathy_audio_sink_class)
115 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_sink_class);
116 GstElementClass *element_class =
117 GST_ELEMENT_CLASS (empathy_audio_sink_class);
118 GParamSpec *param_spec;
120 gst_element_class_add_pad_template (element_class,
121 gst_static_pad_template_get (&sink_template));
123 g_type_class_add_private (empathy_audio_sink_class,
124 sizeof (EmpathyGstAudioSinkPrivate));
126 object_class->set_property = empathy_audio_sink_set_property;
127 object_class->get_property = empathy_audio_sink_get_property;
129 element_class->request_new_pad = empathy_audio_sink_request_new_pad;
130 element_class->release_pad = empathy_audio_sink_release_pad;
132 param_spec = g_param_spec_double ("volume", "Volume", "volume control",
134 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
135 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
139 empathy_audio_sink_new (void)
141 static gboolean registered = FALSE;
144 if (!gst_element_register (NULL, "empathyaudiosink",
145 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SINK))
149 return gst_element_factory_make ("empathyaudiosink", NULL);
153 check_volume_support (EmpathyGstAudioSink *self)
157 if (g_object_class_find_property (G_OBJECT_GET_CLASS (self->priv->sink),
161 name = gst_element_get_name (self->priv->sink);
162 DEBUG ("Element %s doesn't support volume", name);
169 empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
171 EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
173 if (!check_volume_support (sink))
176 g_object_set (priv->sink, "volume", volume, NULL);
180 empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
182 EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
185 if (!check_volume_support (sink))
188 g_object_get (priv->sink, "volume", &volume, NULL);
196 const gchar *description;
198 description = g_getenv ("EMPATHY_AUDIO_SINK");
200 if (description != NULL)
202 GError *error = NULL;
204 sink = gst_parse_bin_from_description (description, TRUE, &error);
207 DEBUG ("Failed to create bin %s: %s", description, error->message);
208 g_error_free (error);
214 /* Use pulsesink as default */
215 sink = gst_element_factory_make ("pulsesink", NULL);
219 empathy_call_set_stream_properties (sink);
225 empathy_audio_sink_request_new_pad (GstElement *element,
226 GstPadTemplate *templ,
229 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (element);
230 GstElement *bin, *volume, *resample, *audioconvert0, *audioconvert1;
232 GstPad *subpad, *filterpad;
234 bin = gst_bin_new (NULL);
236 audioconvert0 = gst_element_factory_make ("audioconvert", NULL);
237 if (audioconvert0 == NULL)
240 gst_bin_add (GST_BIN (bin), audioconvert0);
242 resample = gst_element_factory_make ("audioresample", NULL);
243 if (resample == NULL)
246 gst_bin_add (GST_BIN (bin), resample);
248 audioconvert1 = gst_element_factory_make ("audioconvert", NULL);
249 if (audioconvert1 == NULL)
252 gst_bin_add (GST_BIN (bin), audioconvert1);
254 volume = gst_element_factory_make ("volume", NULL);
258 gst_bin_add (GST_BIN (bin), volume);
260 self->priv->sink = create_sink ();
261 if (self->priv->sink == NULL)
264 gst_bin_add (GST_BIN (bin), self->priv->sink);
266 if (!gst_element_link_many (audioconvert0, resample, audioconvert1,
267 volume, self->priv->sink, NULL))
270 filterpad = gst_element_get_static_pad (audioconvert0, "sink");
272 if (filterpad == NULL)
275 subpad = gst_ghost_pad_new ("sink", filterpad);
276 if (!gst_element_add_pad (GST_ELEMENT (bin), subpad))
279 gst_bin_add (GST_BIN (self), bin);
281 pad = gst_ghost_pad_new (name, subpad);
282 g_assert (pad != NULL);
284 if (!gst_element_sync_state_with_parent (bin))
287 if (!gst_pad_set_active (pad, TRUE))
290 if (!gst_element_add_pad (GST_ELEMENT (self), pad))
298 gst_object_unref (pad);
301 gst_object_unref (bin);
302 g_warning ("Failed to create output subpipeline");
307 empathy_audio_sink_release_pad (GstElement *element,
310 gst_pad_set_active (pad, FALSE);
311 gst_element_remove_pad (element, pad);