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