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