]> git.0d.be Git - empathy.git/blob - src/empathy-audio-sink.c
Keep the audio sinks volume property synchronized with the pulse volume
[empathy.git] / src / empathy-audio-sink.c
1 /*
2  * empathy-gst-audio-sink.c - Source for EmpathyGstAudioSink
3  * Copyright (C) 2008 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <gst/audio/audio.h>
26 #include <gst/interfaces/streamvolume.h>
27
28 #include <telepathy-glib/telepathy-glib.h>
29
30 #include <libempathy-gtk/empathy-call-utils.h>
31
32 #include "empathy-audio-sink.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
35 #include <libempathy/empathy-debug.h>
36
37 G_DEFINE_TYPE(EmpathyGstAudioSink, empathy_audio_sink, GST_TYPE_BIN)
38
39 /* signal enum */
40 #if 0
41 enum
42 {
43     LAST_SIGNAL
44 };
45
46 static guint signals[LAST_SIGNAL] = {0};
47 #endif
48
49 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
50     "sink%d",
51     GST_PAD_SINK,
52     GST_PAD_REQUEST,
53     GST_STATIC_CAPS ( GST_AUDIO_INT_PAD_TEMPLATE_CAPS " ; "
54         GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
55 );
56
57 enum {
58   PROP_VOLUME = 1,
59 };
60
61 struct _EmpathyGstAudioSinkPrivate
62 {
63   GstElement *sink;
64   gboolean echo_cancel;
65   gdouble volume;
66   gint volume_idle_id;
67   GStaticMutex volume_mutex;
68 };
69
70 #define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
71   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SINK, \
72   EmpathyGstAudioSinkPrivate))
73
74 static void
75 empathy_audio_sink_init (EmpathyGstAudioSink *self)
76 {
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);
80 }
81
82 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
83   GstPadTemplate *templ,
84   const gchar* name);
85
86 static void empathy_audio_sink_release_pad (GstElement *self,
87   GstPad *pad);
88
89 static void
90 empathy_audio_sink_set_property (GObject *object,
91   guint property_id, const GValue *value, GParamSpec *pspec)
92 {
93   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
94   switch (property_id)
95     {
96       case PROP_VOLUME:
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);
100         break;
101       default:
102         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
103     }
104 }
105
106 static void
107 empathy_audio_sink_get_property (GObject *object,
108   guint property_id, GValue *value, GParamSpec *pspec)
109 {
110   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
111   switch (property_id)
112     {
113       case PROP_VOLUME:
114         g_value_set_double (value, self->priv->volume);
115         break;
116       default:
117         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
118     }
119 }
120
121 static void
122 empathy_audio_sink_dispose (GObject *object)
123 {
124   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
125   EmpathyGstAudioSinkPrivate *priv = self->priv;
126
127   if (priv->volume_idle_id != 0)
128     g_source_remove (priv->volume_idle_id);
129   priv->volume_idle_id = 0;
130
131   g_static_mutex_free (&self->priv->volume_mutex);
132
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);
136 }
137
138 static void
139 empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
140   *empathy_audio_sink_class)
141 {
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;
146
147   gst_element_class_add_pad_template (element_class,
148     gst_static_pad_template_get (&sink_template));
149
150   g_type_class_add_private (empathy_audio_sink_class,
151     sizeof (EmpathyGstAudioSinkPrivate));
152
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;
156
157   element_class->request_new_pad = empathy_audio_sink_request_new_pad;
158   element_class->release_pad = empathy_audio_sink_release_pad;
159
160   param_spec = g_param_spec_double ("volume", "Volume", "volume control",
161     0.0, 5.0, 1.0,
162     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
163   g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
164 }
165
166 GstElement *
167 empathy_audio_sink_new (void)
168 {
169   static gboolean registered = FALSE;
170
171   if (!registered) {
172     if (!gst_element_register (NULL, "empathyaudiosink",
173             GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SINK))
174       return NULL;
175     registered = TRUE;
176   }
177   return gst_element_factory_make ("empathyaudiosink", NULL);
178 }
179
180 void
181 empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
182 {
183   g_object_set (sink, "volume", volume, NULL);
184 }
185
186 gdouble
187 empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
188 {
189   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
190
191   return priv->volume;
192 }
193
194 static GstElement *
195 create_sink (EmpathyGstAudioSink *self)
196 {
197   GstElement *sink;
198   const gchar *description;
199
200   description = g_getenv ("EMPATHY_AUDIO_SINK");
201
202   if (description != NULL)
203     {
204       GError *error = NULL;
205
206       sink = gst_parse_bin_from_description (description, TRUE, &error);
207       if (sink == NULL)
208         {
209           DEBUG ("Failed to create bin %s: %s", description, error->message);
210           g_error_free (error);
211         }
212
213       return sink;
214     }
215
216   /* Use pulsesink as default */
217   sink = gst_element_factory_make ("pulsesink", NULL);
218   if (sink == NULL)
219     return NULL;
220
221   empathy_call_set_stream_properties (sink, self->priv->echo_cancel);
222
223   return sink;
224 }
225
226 static gboolean
227 empathy_audio_sink_volume_idle_updated (gpointer user_data)
228 {
229   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
230
231   g_static_mutex_lock (&self->priv->volume_mutex);
232   self->priv->volume_idle_id = 0;
233   g_static_mutex_unlock (&self->priv->volume_mutex);
234
235   g_object_notify (G_OBJECT (self), "volume");
236
237   return FALSE;
238 }
239
240 static void
241 empathy_audio_sink_volume_updated (GObject *object,
242   GParamSpec *pspec,
243   gpointer user_data)
244 {
245   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
246   gdouble volume;
247
248   g_static_mutex_lock (&self->priv->volume_mutex);
249
250   g_object_get (object, "volume", &volume, NULL);
251   if (self->priv->volume == volume)
252     goto out;
253
254   self->priv->volume = volume;
255   if (self->priv->volume_idle_id == 0)
256     self->priv->volume_idle_id = g_idle_add (
257       empathy_audio_sink_volume_idle_updated, self);
258
259 out:
260   g_static_mutex_unlock (&self->priv->volume_mutex);
261 }
262
263 static GstPad *
264 empathy_audio_sink_request_new_pad (GstElement *element,
265   GstPadTemplate *templ,
266   const gchar* name)
267 {
268   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (element);
269   GstElement *bin, *resample, *audioconvert0, *audioconvert1;
270   GstPad *pad = NULL;
271   GstPad *subpad, *filterpad;
272
273   bin = gst_bin_new (NULL);
274
275   audioconvert0 = gst_element_factory_make ("audioconvert", NULL);
276   if (audioconvert0 == NULL)
277     goto error;
278
279   gst_bin_add (GST_BIN (bin), audioconvert0);
280
281   resample = gst_element_factory_make ("audioresample", NULL);
282   if (resample == NULL)
283     goto error;
284
285   gst_bin_add (GST_BIN (bin), resample);
286
287   audioconvert1 = gst_element_factory_make ("audioconvert", NULL);
288   if (audioconvert1 == NULL)
289     goto error;
290
291   gst_bin_add (GST_BIN (bin), audioconvert1);
292
293   self->priv->sink = create_sink (self);
294   if (self->priv->sink == NULL)
295     goto error;
296
297   if (GST_IS_STREAM_VOLUME (self->priv->sink))
298     {
299       gdouble volume;
300       /* We can't do a bidirection bind as the ::notify comes from another
301        * thread, for other bits of empathy it's most simpler if it comes from
302        * the main thread */
303       g_object_bind_property (self, "volume", self->priv->sink, "volume",
304         G_BINDING_DEFAULT);
305
306       /* sync and callback for bouncing */
307       g_object_get (self->priv->sink, "volume", &volume, NULL);
308       g_object_set (self, "volume", volume, NULL);
309       g_signal_connect (self->priv->sink, "notify::volume",
310         G_CALLBACK (empathy_audio_sink_volume_updated), self);
311     }
312   else
313     {
314       gchar *n = gst_element_get_name (self->priv->sink);
315
316       DEBUG ("Element %s doesn't support volume", n);
317       g_free (n);
318     }
319
320   gst_bin_add (GST_BIN (bin), self->priv->sink);
321
322   if (!gst_element_link_many (audioconvert0, resample, audioconvert1,
323       self->priv->sink, NULL))
324     goto error;
325
326   filterpad = gst_element_get_static_pad (audioconvert0, "sink");
327
328   if (filterpad == NULL)
329     goto error;
330
331   subpad = gst_ghost_pad_new ("sink", filterpad);
332   if (!gst_element_add_pad (GST_ELEMENT (bin), subpad))
333     goto error;
334
335   gst_bin_add (GST_BIN (self), bin);
336
337   pad = gst_ghost_pad_new (name, subpad);
338   g_assert (pad != NULL);
339
340   if (!gst_element_sync_state_with_parent (bin))
341     goto error;
342
343   if (!gst_pad_set_active (pad, TRUE))
344     goto error;
345
346   if (!gst_element_add_pad (GST_ELEMENT (self), pad))
347     goto error;
348
349   return pad;
350
351 error:
352   if (pad != NULL)
353     {
354       gst_object_unref (pad);
355     }
356
357   gst_object_unref (bin);
358   g_warning ("Failed to create output subpipeline");
359   return NULL;
360 }
361
362 static void
363 empathy_audio_sink_release_pad (GstElement *element,
364   GstPad *pad)
365 {
366   gst_pad_set_active (pad, FALSE);
367   gst_element_remove_pad (element, pad);
368 }
369
370 void
371 empathy_audio_sink_set_echo_cancel (EmpathyGstAudioSink *sink,
372   gboolean echo_cancel)
373 {
374   DEBUG ("Sink echo cancellation setting: %s", echo_cancel ? "on" : "off");
375   sink->priv->echo_cancel = echo_cancel;
376   if (sink->priv->sink != NULL)
377     empathy_call_set_stream_properties (sink->priv->sink,
378       sink->priv->echo_cancel);
379 }