aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-audio-sink.c
blob: fbffa4885900467759a12067d534599ea99bea43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * empathy-gst-audio-sink.c - Source for EmpathyGstAudioSink
 * Copyright (C) 2008 Collabora Ltd.
 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#include <gst/audio/audio.h>
#include <gst/interfaces/streamvolume.h>

#include <telepathy-glib/telepathy-glib.h>

#include <libempathy-gtk/empathy-call-utils.h>

#include "empathy-audio-sink.h"

#define DEBUG_FLAG EMPATHY_DEBUG_VOIP
#include <libempathy/empathy-debug.h>

G_DEFINE_TYPE(EmpathyGstAudioSink, empathy_audio_sink, GST_TYPE_BIN)

/* signal enum */
#if 0
enum
{
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = {0};
#endif

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
    "sink%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ( GST_AUDIO_INT_PAD_TEMPLATE_CAPS " ; "
        GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
);

enum {
  PROP_VOLUME = 1,
};

struct _EmpathyGstAudioSinkPrivate
{
  GstElement *sink;
  gboolean echo_cancel;
  gdouble volume;
  gint volume_idle_id;
  GStaticMutex volume_mutex;
};

#define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SINK, \
  EmpathyGstAudioSinkPrivate))

static void
empathy_audio_sink_init (EmpathyGstAudioSink *self)
{
  self->priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
  self->priv->echo_cancel = TRUE;
  g_static_mutex_init (&self->priv->volume_mutex);
}

static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
  GstPadTemplate *templ,
  const gchar* name);

static void empathy_audio_sink_release_pad (GstElement *self,
  GstPad *pad);

static void
empathy_audio_sink_set_property (GObject *object,
  guint property_id, const GValue *value, GParamSpec *pspec)
{
  EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
  switch (property_id)
    {
      case PROP_VOLUME:
        g_static_mutex_lock (&self->priv->volume_mutex);
        self->priv->volume = g_value_get_double (value);
        g_static_mutex_unlock (&self->priv->volume_mutex);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
empathy_audio_sink_get_property (GObject *object,
  guint property_id, GValue *value, GParamSpec *pspec)
{
  EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
  switch (property_id)
    {
      case PROP_VOLUME:
        g_value_set_double (value, self->priv->volume);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
empathy_audio_sink_dispose (GObject *object)
{
  EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
  EmpathyGstAudioSinkPrivate *priv = self->priv;

  if (priv->volume_idle_id != 0)
    g_source_remove (priv->volume_idle_id);
  priv->volume_idle_id = 0;

  g_static_mutex_free (&self->priv->volume_mutex);

  /* release any references held by the object here */
  if (G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose)
    G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose (object);
}

static void
empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
  *empathy_audio_sink_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_sink_class);
  GstElementClass *element_class =
    GST_ELEMENT_CLASS (empathy_audio_sink_class);
  GParamSpec *param_spec;

  gst_element_class_add_pad_template (element_class,
    gst_static_pad_template_get (&sink_template));

  g_type_class_add_private (empathy_audio_sink_class,
    sizeof (EmpathyGstAudioSinkPrivate));

  object_class->set_property = empathy_audio_sink_set_property;
  object_class->get_property = empathy_audio_sink_get_property;
  object_class->dispose = empathy_audio_sink_dispose;

  element_class->request_new_pad = empathy_audio_sink_request_new_pad;
  element_class->release_pad = empathy_audio_sink_release_pad;

  param_spec = g_param_spec_double ("volume", "Volume", "volume control",
    0.0, 5.0, 1.0,
    G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
}

GstElement *
empathy_audio_sink_new (void)
{
  static gboolean registered = FALSE;

  if (!registered) {
    if (!gst_element_register (NULL, "empathyaudiosink",
            GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SINK))
      return NULL;
    registered = TRUE;
  }
  return gst_element_factory_make ("empathyaudiosink", NULL);
}

void
empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
{
  g_object_set (sink, "volume", volume, NULL);
}

gdouble
empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
{
  EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);

  return priv->volume;
}

static GstElement *
create_sink (EmpathyGstAudioSink *self)
{
  GstElement *sink;
  const gchar *description;

  description = g_getenv ("EMPATHY_AUDIO_SINK");

  if (description != NULL)
    {
      GError *error = NULL;

      sink = gst_parse_bin_from_description (description, TRUE, &error);
      if (sink == NULL)
        {
          DEBUG ("Failed to create bin %s: %s", description, error->message);
          g_error_free (error);
        }

      return sink;
    }

  /* Use pulsesink as default */
  sink = gst_element_factory_make ("pulsesink", NULL);
  if (sink == NULL)
    return NULL;

  empathy_call_set_stream_properties (sink, self->priv->echo_cancel);

  /* Set latency (buffering on the PulseAudio side) of 40ms and transfer data
   * in 10ms chunks */
  g_object_set (sink,
      "buffer-time", (gint64) 40000,
      "latency-time", (gint64) 10000,
      NULL);

  return sink;
}

static gboolean
empathy_audio_sink_volume_idle_updated (gpointer user_data)
{
  EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);

  g_static_mutex_lock (&self->priv->volume_mutex);
  self->priv->volume_idle_id = 0;
  g_static_mutex_unlock (&self->priv->volume_mutex);

  g_object_notify (G_OBJECT (self), "volume");

  return FALSE;
}

static void
empathy_audio_sink_volume_updated (GObject *object,
  GParamSpec *pspec,
  gpointer user_data)
{
  EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
  gdouble volume;

  g_static_mutex_lock (&self->priv->volume_mutex);

  g_object_get (object, "volume", &volume, NULL);
  if (self->priv->volume == volume)
    goto out;

  self->priv->volume = volume;
  if (self->priv->volume_idle_id == 0)
    self->priv->volume_idle_id = g_idle_add (
      empathy_audio_sink_volume_idle_updated, self);

out:
  g_static_mutex_unlock (&self->priv->volume_mutex);
}

static gboolean
empathy_audio_sink_volume_idle_setup (gpointer user_data)
{
  EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (user_data);
  gdouble volume;

  g_static_mutex_lock (&self->priv->volume_mutex);
  self->priv->volume_idle_id = 0;
  g_static_mutex_unlock (&self->priv->volume_mutex);

  /* We can't do a bidirection bind as the ::notify comes from another
   * thread, for other bits of empathy it's most simpler if it comes from
   * the main thread */
  g_object_bind_property (self, "volume", self->priv->sink, "volume",
    G_BINDING_DEFAULT);

  /* sync and callback for bouncing */
  g_object_get (self->priv->sink, "volume", &volume, NULL);
  g_object_set (self, "volume", volume, NULL);
  g_signal_connect (self->priv->sink, "notify::volume",
    G_CALLBACK (empathy_audio_sink_volume_updated), self);

  return FALSE;
}

static GstPad *
empathy_audio_sink_request_new_pad (GstElement *element,
  GstPadTemplate *templ,
  const gchar* name)
{
  EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (element);
  GstElement *bin, *resample, *audioconvert0, *audioconvert1;
  GstPad *pad = NULL;
  GstPad *subpad, *filterpad;

  bin = gst_bin_new (NULL);

  audioconvert0 = gst_element_factory_make ("audioconvert", NULL);
  if (audioconvert0 == NULL)
    goto error;

  gst_bin_add (GST_BIN (bin), audioconvert0);

  resample = gst_element_factory_make ("audioresample", NULL);
  if (resample == NULL)
    goto error;

  gst_bin_add (GST_BIN (bin), resample);

  audioconvert1 = gst_element_factory_make ("audioconvert", NULL);
  if (audioconvert1 == NULL)
    goto error;

  gst_bin_add (GST_BIN (bin), audioconvert1);

  self->priv->sink = create_sink (self);
  if (self->priv->sink == NULL)
    goto error;

  if (GST_IS_STREAM_VOLUME (self->priv->sink))
    {
      g_static_mutex_lock (&self->priv->volume_mutex);
      if (self->priv->volume_idle_id == 0)
        self->priv->volume_idle_id = g_idle_add (
          empathy_audio_sink_volume_idle_setup, self);
      g_static_mutex_unlock (&self->priv->volume_mutex);
    }
  else
    {
      gchar *n = gst_element_get_name (self->priv->sink);

      DEBUG ("Element %s doesn't support volume", n);
      g_free (n);
    }

  gst_bin_add (GST_BIN (bin), self->priv->sink);

  if (!gst_element_link_many (audioconvert0, resample, audioconvert1,
      self->priv->sink, NULL))
    goto error;

  filterpad = gst_element_get_static_pad (audioconvert0, "sink");

  if (filterpad == NULL)
    goto error;

  subpad = gst_ghost_pad_new ("sink", filterpad);
  if (!gst_element_add_pad (GST_ELEMENT (bin), subpad))
    goto error;

  gst_bin_add (GST_BIN (self), bin);

  pad = gst_ghost_pad_new (name, subpad);
  g_assert (pad != NULL);

  if (!gst_element_sync_state_with_parent (bin))
    goto error;

  if (!gst_pad_set_active (pad, TRUE))
    goto error;

  if (!gst_element_add_pad (GST_ELEMENT (self), pad))
    goto error;

  return pad;

error:
  if (pad != NULL)
    {
      gst_object_unref (pad);
    }

  gst_object_unref (bin);
  g_warning ("Failed to create output subpipeline");
  return NULL;
}

static void
empathy_audio_sink_release_pad (GstElement *element,
  GstPad *pad)
{
  gst_pad_set_active (pad, FALSE);
  gst_element_remove_pad (element, pad);
}

void
empathy_audio_sink_set_echo_cancel (EmpathyGstAudioSink *sink,
  gboolean echo_cancel)
{
  DEBUG ("Sink echo cancellation setting: %s", echo_cancel ? "on" : "off");
  sink->priv->echo_cancel = echo_cancel;
  if (sink->priv->sink != NULL)
    empathy_call_set_stream_properties (sink->priv->sink,
      sink->priv->echo_cancel);
}