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
24 #include <gst/audio/streamvolume.h>
26 #include <gst/interfaces/streamvolume.h>
29 #include "empathy-audio-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(
52 GST_STATIC_CAPS ( "audio/x-raw" )
54 GST_STATIC_CAPS ( GST_AUDIO_INT_PAD_TEMPLATE_CAPS " ; "
55 GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
63 struct _EmpathyGstAudioSinkPrivate
72 #define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
73 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SINK, \
74 EmpathyGstAudioSinkPrivate))
77 empathy_audio_sink_init (EmpathyGstAudioSink *self)
79 self->priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
80 self->priv->echo_cancel = TRUE;
81 g_mutex_init (&self->priv->volume_mutex);
85 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
86 GstPadTemplate *templ,
90 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
91 GstPadTemplate *templ,
95 static void empathy_audio_sink_release_pad (GstElement *self,
99 empathy_audio_sink_set_property (GObject *object,
100 guint property_id, const GValue *value, GParamSpec *pspec)
102 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
106 g_mutex_lock (&self->priv->volume_mutex);
107 self->priv->volume = g_value_get_double (value);
108 g_mutex_unlock (&self->priv->volume_mutex);
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
116 empathy_audio_sink_get_property (GObject *object,
117 guint property_id, GValue *value, GParamSpec *pspec)
119 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
123 g_value_set_double (value, self->priv->volume);
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
131 empathy_audio_sink_dispose (GObject *object)
133 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
134 EmpathyGstAudioSinkPrivate *priv = self->priv;
136 if (priv->volume_idle_id != 0)
137 g_source_remove (priv->volume_idle_id);
138 priv->volume_idle_id = 0;
140 g_mutex_clear (&self->priv->volume_mutex);
142 /* release any references held by the object here */
143 if (G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose)
144 G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose (object);
148 empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
149 *empathy_audio_sink_class)
151 GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_sink_class);
152 GstElementClass *element_class =
153 GST_ELEMENT_CLASS (empathy_audio_sink_class);
154 GParamSpec *param_spec;
156 gst_element_class_add_pad_template (element_class,
157 gst_static_pad_template_get (&sink_template));
159 g_type_class_add_private (empathy_audio_sink_class,
160 sizeof (EmpathyGstAudioSinkPrivate));
162 object_class->set_property = empathy_audio_sink_set_property;
163 object_class->get_property = empathy_audio_sink_get_property;
164 object_class->dispose = empathy_audio_sink_dispose;
166 element_class->request_new_pad = empathy_audio_sink_request_new_pad;
167 element_class->release_pad = empathy_audio_sink_release_pad;
169 param_spec = g_param_spec_double ("volume", "Volume", "volume control",
171 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
172 g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
176 empathy_audio_sink_new (void)
178 static gboolean registered = FALSE;
181 if (!gst_element_register (NULL, "empathyaudiosink",
182 GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SINK))
186 return gst_element_factory_make ("empathyaudiosink", NULL);
190 empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
192 g_object_set (sink, "volume", volume, NULL);
196 empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
198 EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
204 create_sink (EmpathyGstAudioSink *self)
207 const gchar *description;
209 description = g_getenv ("EMPATHY_AUDIO_SINK");
211 if (description != NULL)
213 GError *error = NULL;
215 sink = gst_parse_bin_from_description (description, TRUE, &error);
218 DEBUG ("Failed to create bin %s: %s", description, error->message);
219 g_error_free (error);
225 /* Use pulsesink as default */
226 sink = gst_element_factory_make ("pulsesink", NULL);
230 empathy_audio_set_stream_properties (sink, self->priv->echo_cancel);
232 /* Set latency (buffering on the PulseAudio side) of 40ms and transfer data
235 "buffer-time", (gint64) 40000,
236 "latency-time", (gint64) 10000,
243 empathy_audio_sink_volume_idle_updated (gpointer user_data)
245 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
247 g_mutex_lock (&self->priv->volume_mutex);
248 self->priv->volume_idle_id = 0;
249 g_mutex_unlock (&self->priv->volume_mutex);
251 g_object_notify (G_OBJECT (self), "volume");
257 empathy_audio_sink_volume_updated (GObject *object,
261 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
264 g_mutex_lock (&self->priv->volume_mutex);
266 g_object_get (object, "volume", &volume, NULL);
267 if (self->priv->volume == volume)
270 self->priv->volume = volume;
271 if (self->priv->volume_idle_id == 0)
272 self->priv->volume_idle_id = g_idle_add (
273 empathy_audio_sink_volume_idle_updated, self);
276 g_mutex_unlock (&self->priv->volume_mutex);
280 empathy_audio_sink_volume_idle_setup (gpointer user_data)
282 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
285 g_mutex_lock (&self->priv->volume_mutex);
286 self->priv->volume_idle_id = 0;
287 g_mutex_unlock (&self->priv->volume_mutex);
289 /* We can't do a bidirection bind as the ::notify comes from another
290 * thread, for other bits of empathy it's most simpler if it comes from
292 g_object_bind_property (self, "volume", self->priv->sink, "volume",
295 /* sync and callback for bouncing */
296 g_object_get (self->priv->sink, "volume", &volume, NULL);
297 g_object_set (self, "volume", volume, NULL);
298 g_signal_connect (self->priv->sink, "notify::volume",
299 G_CALLBACK (empathy_audio_sink_volume_updated), self);
306 empathy_audio_sink_request_new_pad (GstElement *element,
307 GstPadTemplate *templ,
312 empathy_audio_sink_request_new_pad (GstElement *element,
313 GstPadTemplate *templ,
317 EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (element);
318 GstElement *bin, *resample, *audioconvert0, *audioconvert1;
320 GstPad *subpad, *filterpad;
322 bin = gst_bin_new (NULL);
324 audioconvert0 = gst_element_factory_make ("audioconvert", NULL);
325 if (audioconvert0 == NULL)
328 gst_bin_add (GST_BIN (bin), audioconvert0);
330 resample = gst_element_factory_make ("audioresample", NULL);
331 if (resample == NULL)
334 gst_bin_add (GST_BIN (bin), resample);
336 audioconvert1 = gst_element_factory_make ("audioconvert", NULL);
337 if (audioconvert1 == NULL)
340 gst_bin_add (GST_BIN (bin), audioconvert1);
342 self->priv->sink = create_sink (self);
343 if (self->priv->sink == NULL)
346 if (GST_IS_STREAM_VOLUME (self->priv->sink))
348 g_mutex_lock (&self->priv->volume_mutex);
349 if (self->priv->volume_idle_id == 0)
350 self->priv->volume_idle_id = g_idle_add (
351 empathy_audio_sink_volume_idle_setup, self);
352 g_mutex_unlock (&self->priv->volume_mutex);
356 gchar *n = gst_element_get_name (self->priv->sink);
358 DEBUG ("Element %s doesn't support volume", n);
362 gst_bin_add (GST_BIN (bin), self->priv->sink);
364 if (!gst_element_link_many (audioconvert0, resample, audioconvert1,
365 self->priv->sink, NULL))
368 filterpad = gst_element_get_static_pad (audioconvert0, "sink");
370 if (filterpad == NULL)
373 subpad = gst_ghost_pad_new ("sink", filterpad);
374 gst_object_unref (filterpad);
376 if (!gst_element_add_pad (GST_ELEMENT (bin), subpad))
379 gst_bin_add (GST_BIN (self), bin);
381 pad = gst_ghost_pad_new (name, subpad);
382 g_assert (pad != NULL);
384 if (!gst_element_sync_state_with_parent (bin))
387 if (!gst_pad_set_active (pad, TRUE))
390 if (!gst_element_add_pad (GST_ELEMENT (self), pad))
398 gst_object_unref (pad);
401 gst_object_unref (bin);
402 g_warning ("Failed to create output subpipeline");
407 empathy_audio_sink_release_pad (GstElement *element,
410 gst_pad_set_active (pad, FALSE);
411 gst_element_remove_pad (element, pad);
415 empathy_audio_sink_set_echo_cancel (EmpathyGstAudioSink *sink,
416 gboolean echo_cancel)
418 DEBUG ("Sink echo cancellation setting: %s", echo_cancel ? "on" : "off");
419 sink->priv->echo_cancel = echo_cancel;
420 if (sink->priv->sink != NULL)
421 empathy_audio_set_stream_properties (sink->priv->sink,
422 sink->priv->echo_cancel);