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