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
26 #include <gst/audio/audio.h>
27 #include <gst/interfaces/streamvolume.h>
29 #include <telepathy-glib/telepathy-glib.h>
31 #include "empathy-audio-utils.h"
33 #include "empathy-audio-sink.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include <libempathy/empathy-debug.h>
38 G_DEFINE_TYPE(EmpathyGstAudioSink, empathy_audio_sink, GST_TYPE_BIN)
47 static guint signals[LAST_SIGNAL] = {0};
50 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
55 GST_STATIC_CAPS ( "audio/x-raw" )
57 GST_STATIC_CAPS ( GST_AUDIO_INT_PAD_TEMPLATE_CAPS " ; "
58 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
66 struct _EmpathyGstAudioSinkPrivate
75 #define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
76 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SINK, \
77 EmpathyGstAudioSinkPrivate))
80 empathy_audio_sink_init (EmpathyGstAudioSink *self)
82 self->priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
83 self->priv->echo_cancel = TRUE;
84 g_mutex_init (&self->priv->volume_mutex);
88 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
89 GstPadTemplate *templ,
93 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
94 GstPadTemplate *templ,
98 static void empathy_audio_sink_release_pad (GstElement *self,
102 empathy_audio_sink_set_property (GObject *object,
103 guint property_id, const GValue *value, GParamSpec *pspec)
105 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
109 g_mutex_lock (&self->priv->volume_mutex);
110 self->priv->volume = g_value_get_double (value);
111 g_mutex_unlock (&self->priv->volume_mutex);
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
119 empathy_audio_sink_get_property (GObject *object,
120 guint property_id, GValue *value, GParamSpec *pspec)
122 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
126 g_value_set_double (value, self->priv->volume);
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
134 empathy_audio_sink_dispose (GObject *object)
136 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
137 EmpathyGstAudioSinkPrivate *priv = self->priv;
139 if (priv->volume_idle_id != 0)
140 g_source_remove (priv->volume_idle_id);
141 priv->volume_idle_id = 0;
143 g_mutex_clear (&self->priv->volume_mutex);
145 /* release any references held by the object here */
146 if (G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose)
147 G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose (object);
151 empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
152 *empathy_audio_sink_class)
154 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_sink_class);
155 GstElementClass *element_class =
156 GST_ELEMENT_CLASS (empathy_audio_sink_class);
157 GParamSpec *param_spec;
159 gst_element_class_add_pad_template (element_class,
160 gst_static_pad_template_get (&sink_template));
162 g_type_class_add_private (empathy_audio_sink_class,
163 sizeof (EmpathyGstAudioSinkPrivate));
165 object_class->set_property = empathy_audio_sink_set_property;
166 object_class->get_property = empathy_audio_sink_get_property;
167 object_class->dispose = empathy_audio_sink_dispose;
169 element_class->request_new_pad = empathy_audio_sink_request_new_pad;
170 element_class->release_pad = empathy_audio_sink_release_pad;
172 param_spec = g_param_spec_double ("volume", "Volume", "volume control",
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
175 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
179 empathy_audio_sink_new (void)
181 static gboolean registered = FALSE;
184 if (!gst_element_register (NULL, "empathyaudiosink",
185 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SINK))
189 return gst_element_factory_make ("empathyaudiosink", NULL);
193 empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
195 g_object_set (sink, "volume", volume, NULL);
199 empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
201 EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
207 create_sink (EmpathyGstAudioSink *self)
210 const gchar *description;
212 description = g_getenv ("EMPATHY_AUDIO_SINK");
214 if (description != NULL)
216 GError *error = NULL;
218 sink = gst_parse_bin_from_description (description, TRUE, &error);
221 DEBUG ("Failed to create bin %s: %s", description, error->message);
222 g_error_free (error);
228 /* Use pulsesink as default */
229 sink = gst_element_factory_make ("pulsesink", NULL);
233 empathy_audio_set_stream_properties (sink, self->priv->echo_cancel);
235 /* Set latency (buffering on the PulseAudio side) of 40ms and transfer data
238 "buffer-time", (gint64) 40000,
239 "latency-time", (gint64) 10000,
246 empathy_audio_sink_volume_idle_updated (gpointer user_data)
248 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
250 g_mutex_lock (&self->priv->volume_mutex);
251 self->priv->volume_idle_id = 0;
252 g_mutex_unlock (&self->priv->volume_mutex);
254 g_object_notify (G_OBJECT (self), "volume");
260 empathy_audio_sink_volume_updated (GObject *object,
264 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
267 g_mutex_lock (&self->priv->volume_mutex);
269 g_object_get (object, "volume", &volume, NULL);
270 if (self->priv->volume == volume)
273 self->priv->volume = volume;
274 if (self->priv->volume_idle_id == 0)
275 self->priv->volume_idle_id = g_idle_add (
276 empathy_audio_sink_volume_idle_updated, self);
279 g_mutex_unlock (&self->priv->volume_mutex);
283 empathy_audio_sink_volume_idle_setup (gpointer user_data)
285 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
288 g_mutex_lock (&self->priv->volume_mutex);
289 self->priv->volume_idle_id = 0;
290 g_mutex_unlock (&self->priv->volume_mutex);
292 /* We can't do a bidirection bind as the ::notify comes from another
293 * thread, for other bits of empathy it's most simpler if it comes from
295 g_object_bind_property (self, "volume", self->priv->sink, "volume",
298 /* sync and callback for bouncing */
299 g_object_get (self->priv->sink, "volume", &volume, NULL);
300 g_object_set (self, "volume", volume, NULL);
301 g_signal_connect (self->priv->sink, "notify::volume",
302 G_CALLBACK (empathy_audio_sink_volume_updated), self);
309 empathy_audio_sink_request_new_pad (GstElement *element,
310 GstPadTemplate *templ,
315 empathy_audio_sink_request_new_pad (GstElement *element,
316 GstPadTemplate *templ,
320 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (element);
321 GstElement *bin, *resample, *audioconvert0, *audioconvert1;
323 GstPad *subpad, *filterpad;
325 bin = gst_bin_new (NULL);
327 audioconvert0 = gst_element_factory_make ("audioconvert", NULL);
328 if (audioconvert0 == NULL)
331 gst_bin_add (GST_BIN (bin), audioconvert0);
333 resample = gst_element_factory_make ("audioresample", NULL);
334 if (resample == NULL)
337 gst_bin_add (GST_BIN (bin), resample);
339 audioconvert1 = gst_element_factory_make ("audioconvert", NULL);
340 if (audioconvert1 == NULL)
343 gst_bin_add (GST_BIN (bin), audioconvert1);
345 self->priv->sink = create_sink (self);
346 if (self->priv->sink == NULL)
349 if (GST_IS_STREAM_VOLUME (self->priv->sink))
351 g_mutex_lock (&self->priv->volume_mutex);
352 if (self->priv->volume_idle_id == 0)
353 self->priv->volume_idle_id = g_idle_add (
354 empathy_audio_sink_volume_idle_setup, self);
355 g_mutex_unlock (&self->priv->volume_mutex);
359 gchar *n = gst_element_get_name (self->priv->sink);
361 DEBUG ("Element %s doesn't support volume", n);
365 gst_bin_add (GST_BIN (bin), self->priv->sink);
367 if (!gst_element_link_many (audioconvert0, resample, audioconvert1,
368 self->priv->sink, NULL))
371 filterpad = gst_element_get_static_pad (audioconvert0, "sink");
373 if (filterpad == NULL)
376 subpad = gst_ghost_pad_new ("sink", filterpad);
377 gst_object_unref (filterpad);
379 if (!gst_element_add_pad (GST_ELEMENT (bin), subpad))
382 gst_bin_add (GST_BIN (self), bin);
384 pad = gst_ghost_pad_new (name, subpad);
385 g_assert (pad != NULL);
387 if (!gst_element_sync_state_with_parent (bin))
390 if (!gst_pad_set_active (pad, TRUE))
393 if (!gst_element_add_pad (GST_ELEMENT (self), pad))
401 gst_object_unref (pad);
404 gst_object_unref (bin);
405 g_warning ("Failed to create output subpipeline");
410 empathy_audio_sink_release_pad (GstElement *element,
413 gst_pad_set_active (pad, FALSE);
414 gst_element_remove_pad (element, pad);
418 empathy_audio_sink_set_echo_cancel (EmpathyGstAudioSink *sink,
419 gboolean echo_cancel)
421 DEBUG ("Sink echo cancellation setting: %s", echo_cancel ? "on" : "off");
422 sink->priv->echo_cancel = echo_cancel;
423 if (sink->priv->sink != NULL)
424 empathy_audio_set_stream_properties (sink->priv->sink,
425 sink->priv->echo_cancel);