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