]> git.0d.be Git - empathy.git/blobdiff - src/empathy-audio-sink.c
Use a flat namespace for internal includes
[empathy.git] / src / empathy-audio-sink.c
index c9d7203107a3523fb01b15de888b811d751586ce..0147b870679d56645be19566e0391440724cd399 100644 (file)
  * 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 <telepathy-glib/telepathy-glib.h>
-
-#include <libempathy-gtk/empathy-call-utils.h>
+#ifdef HAVE_GST1
+#include <gst/audio/streamvolume.h>
+#else
+#include <gst/interfaces/streamvolume.h>
+#endif
 
+#include "empathy-audio-utils.h"
 #include "empathy-audio-sink.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 G_DEFINE_TYPE(EmpathyGstAudioSink, empathy_audio_sink, GST_TYPE_BIN)
 
@@ -48,8 +48,12 @@ static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
     "sink%d",
     GST_PAD_SINK,
     GST_PAD_REQUEST,
+#ifdef HAVE_GST1
+    GST_STATIC_CAPS ( "audio/x-raw" )
+#else
     GST_STATIC_CAPS ( GST_AUDIO_INT_PAD_TEMPLATE_CAPS " ; "
         GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
+#endif
 );
 
 enum {
@@ -59,6 +63,10 @@ enum {
 struct _EmpathyGstAudioSinkPrivate
 {
   GstElement *sink;
+  gboolean echo_cancel;
+  gdouble volume;
+  gint volume_idle_id;
+  GMutex volume_mutex;
 };
 
 #define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
@@ -69,11 +77,20 @@ static void
 empathy_audio_sink_init (EmpathyGstAudioSink *self)
 {
   self->priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
+  self->priv->echo_cancel = TRUE;
+  g_mutex_init (&self->priv->volume_mutex);
 }
 
+#ifdef HAVE_GST1
+static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
+  GstPadTemplate *templ,
+  const gchar* name,
+  const GstCaps *caps);
+#else
 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
   GstPadTemplate *templ,
   const gchar* name);
+#endif
 
 static void empathy_audio_sink_release_pad (GstElement *self,
   GstPad *pad);
@@ -82,11 +99,13 @@ 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:
-        empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (object),
-          g_value_get_double (value));
+        g_mutex_lock (&self->priv->volume_mutex);
+        self->priv->volume = g_value_get_double (value);
+        g_mutex_unlock (&self->priv->volume_mutex);
         break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -97,17 +116,34 @@ 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,
-          empathy_audio_sink_get_volume (EMPATHY_GST_AUDIO_SINK (object)));
+        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_mutex_clear (&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)
@@ -125,6 +161,7 @@ empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
 
   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;
@@ -149,50 +186,27 @@ empathy_audio_sink_new (void)
   return gst_element_factory_make ("empathyaudiosink", NULL);
 }
 
-static gboolean
-check_volume_support (EmpathyGstAudioSink *self)
-{
-  gchar *name;
-
-  if (g_object_class_find_property (G_OBJECT_GET_CLASS (self->priv->sink),
-        "volume"))
-    return TRUE;
-
-  name = gst_element_get_name (self->priv->sink);
-  DEBUG ("Element %s doesn't support volume", name);
-
-  g_free (name);
-  return FALSE;
-}
-
 void
 empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
 {
-  EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
-
-  if (!check_volume_support (sink))
-      return;
-
-  g_object_set (priv->sink, "volume", volume, NULL);
+  g_object_set (sink, "volume", volume, NULL);
 }
 
 gdouble
 empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
 {
   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
-  gdouble volume;
-
-  if (!check_volume_support (sink))
-      return 1.0;
 
-  g_object_get (priv->sink, "volume", &volume, NULL);
-  return volume;
+  return priv->volume;
 }
 
 static GstElement *
-create_sink (const gchar *description)
+create_sink (EmpathyGstAudioSink *self)
 {
   GstElement *sink;
+  const gchar *description;
+
+  description = g_getenv ("EMPATHY_AUDIO_SINK");
 
   if (description != NULL)
     {
@@ -213,18 +227,95 @@ create_sink (const gchar *description)
   if (sink == NULL)
     return NULL;
 
-  empathy_call_set_stream_properties (sink);
+  empathy_audio_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_mutex_lock (&self->priv->volume_mutex);
+  self->priv->volume_idle_id = 0;
+  g_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_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_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_mutex_lock (&self->priv->volume_mutex);
+  self->priv->volume_idle_id = 0;
+  g_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;
+}
+
+#ifdef HAVE_GST1
+static GstPad *
+empathy_audio_sink_request_new_pad (GstElement *element,
+  GstPadTemplate *templ,
+  const gchar* name,
+  const GstCaps *caps)
+#else
 static GstPad *
 empathy_audio_sink_request_new_pad (GstElement *element,
   GstPadTemplate *templ,
   const gchar* name)
+#endif
 {
   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (element);
-  GstElement *bin, *volume, *resample, *audioconvert0, *audioconvert1;
+  GstElement *bin, *resample, *audioconvert0, *audioconvert1;
   GstPad *pad = NULL;
   GstPad *subpad, *filterpad;
 
@@ -248,20 +339,30 @@ empathy_audio_sink_request_new_pad (GstElement *element,
 
   gst_bin_add (GST_BIN (bin), audioconvert1);
 
-  volume = gst_element_factory_make ("volume", NULL);
-  if (volume == NULL)
+  self->priv->sink = create_sink (self);
+  if (self->priv->sink == NULL)
     goto error;
 
-  gst_bin_add (GST_BIN (bin), volume);
+  if (GST_IS_STREAM_VOLUME (self->priv->sink))
+    {
+      g_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_mutex_unlock (&self->priv->volume_mutex);
+    }
+  else
+    {
+      gchar *n = gst_element_get_name (self->priv->sink);
 
-  self->priv->sink = create_sink (g_getenv ("EMPATHY_AUDIO_SINK"));
-  if (self->priv->sink == NULL)
-    goto error;
+      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,
-      volume, self->priv->sink, NULL))
+      self->priv->sink, NULL))
     goto error;
 
   filterpad = gst_element_get_static_pad (audioconvert0, "sink");
@@ -270,6 +371,8 @@ empathy_audio_sink_request_new_pad (GstElement *element,
     goto error;
 
   subpad = gst_ghost_pad_new ("sink", filterpad);
+  gst_object_unref (filterpad);
+
   if (!gst_element_add_pad (GST_ELEMENT (bin), subpad))
     goto error;
 
@@ -307,3 +410,14 @@ empathy_audio_sink_release_pad (GstElement *element,
   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_audio_set_stream_properties (sink->priv->sink,
+      sink->priv->echo_cancel);
+}