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 <gst/interfaces/streamvolume.h>
28 #include <telepathy-glib/telepathy-glib.h>
30 #include <libempathy-gtk/empathy-call-utils.h>
32 #include "empathy-audio-sink.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
35 #include <libempathy/empathy-debug.h>
37 G_DEFINE_TYPE(EmpathyGstAudioSink, empathy_audio_sink, GST_TYPE_BIN)
46 static guint signals[LAST_SIGNAL] = {0};
49 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
53 GST_STATIC_CAPS ( GST_AUDIO_INT_PAD_TEMPLATE_CAPS " ; "
54 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
61 struct _EmpathyGstAudioSinkPrivate
67 GStaticMutex volume_mutex;
70 #define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
71 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SINK, \
72 EmpathyGstAudioSinkPrivate))
75 empathy_audio_sink_init (EmpathyGstAudioSink *self)
77 self->priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
78 self->priv->echo_cancel = TRUE;
79 g_static_mutex_init (&self->priv->volume_mutex);
82 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
83 GstPadTemplate *templ,
86 static void empathy_audio_sink_release_pad (GstElement *self,
90 empathy_audio_sink_set_property (GObject *object,
91 guint property_id, const GValue *value, GParamSpec *pspec)
93 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
97 g_static_mutex_lock (&self->priv->volume_mutex);
98 self->priv->volume = g_value_get_double (value);
99 g_static_mutex_unlock (&self->priv->volume_mutex);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
107 empathy_audio_sink_get_property (GObject *object,
108 guint property_id, GValue *value, GParamSpec *pspec)
110 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
114 g_value_set_double (value, self->priv->volume);
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
122 empathy_audio_sink_dispose (GObject *object)
124 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
125 EmpathyGstAudioSinkPrivate *priv = self->priv;
127 if (priv->volume_idle_id != 0)
128 g_source_remove (priv->volume_idle_id);
129 priv->volume_idle_id = 0;
131 g_static_mutex_free (&self->priv->volume_mutex);
133 /* release any references held by the object here */
134 if (G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose)
135 G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose (object);
139 empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
140 *empathy_audio_sink_class)
142 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_sink_class);
143 GstElementClass *element_class =
144 GST_ELEMENT_CLASS (empathy_audio_sink_class);
145 GParamSpec *param_spec;
147 gst_element_class_add_pad_template (element_class,
148 gst_static_pad_template_get (&sink_template));
150 g_type_class_add_private (empathy_audio_sink_class,
151 sizeof (EmpathyGstAudioSinkPrivate));
153 object_class->set_property = empathy_audio_sink_set_property;
154 object_class->get_property = empathy_audio_sink_get_property;
155 object_class->dispose = empathy_audio_sink_dispose;
157 element_class->request_new_pad = empathy_audio_sink_request_new_pad;
158 element_class->release_pad = empathy_audio_sink_release_pad;
160 param_spec = g_param_spec_double ("volume", "Volume", "volume control",
162 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
163 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
167 empathy_audio_sink_new (void)
169 static gboolean registered = FALSE;
172 if (!gst_element_register (NULL, "empathyaudiosink",
173 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SINK))
177 return gst_element_factory_make ("empathyaudiosink", NULL);
181 empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
183 g_object_set (sink, "volume", volume, NULL);
187 empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
189 EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
195 create_sink (EmpathyGstAudioSink *self)
198 const gchar *description;
200 description = g_getenv ("EMPATHY_AUDIO_SINK");
202 if (description != NULL)
204 GError *error = NULL;
206 sink = gst_parse_bin_from_description (description, TRUE, &error);
209 DEBUG ("Failed to create bin %s: %s", description, error->message);
210 g_error_free (error);
216 /* Use pulsesink as default */
217 sink = gst_element_factory_make ("pulsesink", NULL);
221 empathy_call_set_stream_properties (sink, self->priv->echo_cancel);
223 /* Set latency (buffering on the PulseAudio side) of 40ms and transfer data
225 g_object_set (sink, "buffer-time", 40000, "latency-time", 10000, NULL);
231 empathy_audio_sink_volume_idle_updated (gpointer user_data)
233 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
235 g_static_mutex_lock (&self->priv->volume_mutex);
236 self->priv->volume_idle_id = 0;
237 g_static_mutex_unlock (&self->priv->volume_mutex);
239 g_object_notify (G_OBJECT (self), "volume");
245 empathy_audio_sink_volume_updated (GObject *object,
249 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
252 g_static_mutex_lock (&self->priv->volume_mutex);
254 g_object_get (object, "volume", &volume, NULL);
255 if (self->priv->volume == volume)
258 self->priv->volume = volume;
259 if (self->priv->volume_idle_id == 0)
260 self->priv->volume_idle_id = g_idle_add (
261 empathy_audio_sink_volume_idle_updated, self);
264 g_static_mutex_unlock (&self->priv->volume_mutex);
268 empathy_audio_sink_volume_idle_setup (gpointer user_data)
270 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
273 g_static_mutex_lock (&self->priv->volume_mutex);
274 self->priv->volume_idle_id = 0;
275 g_static_mutex_unlock (&self->priv->volume_mutex);
277 /* We can't do a bidirection bind as the ::notify comes from another
278 * thread, for other bits of empathy it's most simpler if it comes from
280 g_object_bind_property (self, "volume", self->priv->sink, "volume",
283 /* sync and callback for bouncing */
284 g_object_get (self->priv->sink, "volume", &volume, NULL);
285 g_object_set (self, "volume", volume, NULL);
286 g_signal_connect (self->priv->sink, "notify::volume",
287 G_CALLBACK (empathy_audio_sink_volume_updated), self);
293 empathy_audio_sink_request_new_pad (GstElement *element,
294 GstPadTemplate *templ,
297 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (element);
298 GstElement *bin, *resample, *audioconvert0, *audioconvert1;
300 GstPad *subpad, *filterpad;
302 bin = gst_bin_new (NULL);
304 audioconvert0 = gst_element_factory_make ("audioconvert", NULL);
305 if (audioconvert0 == NULL)
308 gst_bin_add (GST_BIN (bin), audioconvert0);
310 resample = gst_element_factory_make ("audioresample", NULL);
311 if (resample == NULL)
314 gst_bin_add (GST_BIN (bin), resample);
316 audioconvert1 = gst_element_factory_make ("audioconvert", NULL);
317 if (audioconvert1 == NULL)
320 gst_bin_add (GST_BIN (bin), audioconvert1);
322 self->priv->sink = create_sink (self);
323 if (self->priv->sink == NULL)
326 if (GST_IS_STREAM_VOLUME (self->priv->sink))
328 g_static_mutex_lock (&self->priv->volume_mutex);
329 if (self->priv->volume_idle_id == 0)
330 self->priv->volume_idle_id = g_idle_add (
331 empathy_audio_sink_volume_idle_setup, self);
332 g_static_mutex_unlock (&self->priv->volume_mutex);
336 gchar *n = gst_element_get_name (self->priv->sink);
338 DEBUG ("Element %s doesn't support volume", n);
342 gst_bin_add (GST_BIN (bin), self->priv->sink);
344 if (!gst_element_link_many (audioconvert0, resample, audioconvert1,
345 self->priv->sink, NULL))
348 filterpad = gst_element_get_static_pad (audioconvert0, "sink");
350 if (filterpad == NULL)
353 subpad = gst_ghost_pad_new ("sink", filterpad);
354 if (!gst_element_add_pad (GST_ELEMENT (bin), subpad))
357 gst_bin_add (GST_BIN (self), bin);
359 pad = gst_ghost_pad_new (name, subpad);
360 g_assert (pad != NULL);
362 if (!gst_element_sync_state_with_parent (bin))
365 if (!gst_pad_set_active (pad, TRUE))
368 if (!gst_element_add_pad (GST_ELEMENT (self), pad))
376 gst_object_unref (pad);
379 gst_object_unref (bin);
380 g_warning ("Failed to create output subpipeline");
385 empathy_audio_sink_release_pad (GstElement *element,
388 gst_pad_set_active (pad, FALSE);
389 gst_element_remove_pad (element, pad);
393 empathy_audio_sink_set_echo_cancel (EmpathyGstAudioSink *sink,
394 gboolean echo_cancel)
396 DEBUG ("Sink echo cancellation setting: %s", echo_cancel ? "on" : "off");
397 sink->priv->echo_cancel = echo_cancel;
398 if (sink->priv->sink != NULL)
399 empathy_call_set_stream_properties (sink->priv->sink,
400 sink->priv->echo_cancel);