]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-tp-call.c
On incoming calls don't request a new audio stream. This makes streams collide and...
[empathy.git] / libempathy / empathy-tp-call.c
index 1d7ec2c4e8309601efe76a0c7d7e8ad60e2020f5..6c90f49d890a967fba8feac6b387f5627df86c92 100644 (file)
@@ -1,47 +1,47 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
- *  Copyright (C) 2007 Elliot Fairweather
+ * Copyright (C) 2007 Elliot Fairweather
+ * Copyright (C) 2007-2008 Collabora Ltd.
  *
- *  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 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.
+ * 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
+ * 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
  *
- *  Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
+ * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
+ *          Xavier Claessens <xclaesse@gmail.com>
  */
 
 #include <string.h>
 
 #include <telepathy-glib/proxy-subclass.h>
 #include <telepathy-glib/dbus.h>
+#include <telepathy-glib/interfaces.h>
 
 #include <extensions/extensions.h>
-#include <libempathy/empathy-contact-factory.h>
-#include <libempathy/empathy-debug.h>
-#include <libempathy/empathy-tp-group.h>
-#include <libempathy/empathy-utils.h>
 
 #include "empathy-tp-call.h"
+#include "empathy-contact-factory.h"
+#include "empathy-tp-group.h"
+#include "empathy-utils.h"
 
-#define DEBUG_DOMAIN "TpCall"
-
-#define GET_PRIV(object) (G_TYPE_INSTANCE_GET_PRIVATE \
-    ((object), EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv))
+#define DEBUG_FLAG EMPATHY_DEBUG_TP
+#include "empathy-debug.h"
 
 #define STREAM_ENGINE_BUS_NAME "org.freedesktop.Telepathy.StreamEngine"
 #define STREAM_ENGINE_OBJECT_PATH "/org/freedesktop/Telepathy/StreamEngine"
 
-typedef struct _EmpathyTpCallPriv EmpathyTpCallPriv;
-
-struct _EmpathyTpCallPriv
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
+typedef struct
 {
   TpChannel *channel;
   TpProxy *stream_engine;
@@ -54,7 +54,7 @@ struct _EmpathyTpCallPriv
 
   EmpathyTpCallStream *audio;
   EmpathyTpCallStream *video;
-};
+} EmpathyTpCallPriv;
 
 enum
 {
@@ -82,8 +82,7 @@ tp_call_add_stream (EmpathyTpCall *call,
   switch (stream_type)
     {
       case TP_MEDIA_STREAM_TYPE_AUDIO:
-        empathy_debug (DEBUG_DOMAIN,
-            "Audio stream - id: %d, state: %d, direction: %d",
+        DEBUG ("Audio stream - id: %d, state: %d, direction: %d",
             stream_id, stream_state, stream_direction);
         priv->audio->exists = TRUE;
         priv->audio->id = stream_id;
@@ -92,8 +91,7 @@ tp_call_add_stream (EmpathyTpCall *call,
         g_object_notify (G_OBJECT (call), "audio-stream");
         break;
       case TP_MEDIA_STREAM_TYPE_VIDEO:
-        empathy_debug (DEBUG_DOMAIN,
-            "Video stream - id: %d, state: %d, direction: %d",
+        DEBUG ("Video stream - id: %d, state: %d, direction: %d",
             stream_id, stream_state, stream_direction);
         priv->video->exists = TRUE;
         priv->video->id = stream_id;
@@ -102,8 +100,7 @@ tp_call_add_stream (EmpathyTpCall *call,
         g_object_notify (G_OBJECT (call), "video-stream");
         break;
       default:
-        empathy_debug (DEBUG_DOMAIN, "Unknown stream type: %d",
-            stream_type);
+        DEBUG ("Unknown stream type: %d", stream_type);
     }
 }
 
@@ -115,8 +112,7 @@ tp_call_stream_added_cb (TpChannel *channel,
                          gpointer user_data,
                          GObject *call)
 {
-  empathy_debug (DEBUG_DOMAIN,
-      "Stream added - stream id: %d, contact handle: %d, stream type: %d",
+  DEBUG ("Stream added - stream id: %d, contact handle: %d, stream type: %d",
       stream_id, contact_handle, stream_type);
 
   tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
@@ -132,7 +128,7 @@ tp_call_stream_removed_cb (TpChannel *channel,
 {
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
-  empathy_debug (DEBUG_DOMAIN, "Stream removed - stream id: %d", stream_id);
+  DEBUG ("Stream removed - stream id: %d", stream_id);
 
   if (stream_id == priv->audio->id)
     {
@@ -155,8 +151,7 @@ tp_call_stream_state_changed_cb (TpChannel *proxy,
 {
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
-  empathy_debug (DEBUG_DOMAIN,
-      "Stream state changed - stream id: %d, state state: %d",
+  DEBUG ("Stream state changed - stream id: %d, state state: %d",
       stream_id, stream_state);
 
   if (stream_id == priv->audio->id)
@@ -181,8 +176,7 @@ tp_call_stream_direction_changed_cb (TpChannel *channel,
 {
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
-  empathy_debug (DEBUG_DOMAIN,
-      "Stream direction changed - stream: %d, direction: %d",
+  DEBUG ("Stream direction changed - stream: %d, direction: %d",
       stream_id, stream_direction);
 
   if (stream_id == priv->audio->id)
@@ -208,7 +202,7 @@ tp_call_request_streams_cb (TpChannel *channel,
 
   if (error)
     {
-      empathy_debug (DEBUG_DOMAIN, "Error requesting streams: %s", error->message);
+      DEBUG ("Error requesting streams: %s", error->message);
       return;
     }
 
@@ -245,7 +239,7 @@ tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
   if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
       capabilities = EMPATHY_CAPABILITIES_AUDIO | EMPATHY_CAPABILITIES_VIDEO;
 
-  empathy_debug (DEBUG_DOMAIN, "Requesting new stream for capabilities %d",
+  DEBUG ("Requesting new stream for capabilities %d",
       capabilities);
 
   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
@@ -269,33 +263,6 @@ tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
   g_array_free (stream_types, TRUE);
 }
 
-static void
-tp_call_request_streams (EmpathyTpCall *call)
-{
-  EmpathyTpCallPriv *priv = GET_PRIV (call);
-
-  tp_call_request_streams_for_capabilities (call,
-      empathy_contact_get_capabilities (priv->contact));
-}
-
-static void
-tp_call_group_ready_cb (EmpathyTpCall *call)
-{
-  EmpathyTpCallPriv  *priv = GET_PRIV (call);
-  EmpathyPendingInfo *invitation;
-
-  invitation = empathy_tp_group_get_invitation (priv->group, &priv->contact);
-  priv->is_incoming = (invitation != NULL);
-  priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
-
-  if (!priv->is_incoming)
-      tp_call_request_streams (call);
-
-  g_object_notify (G_OBJECT (call), "is-incoming");
-  g_object_notify (G_OBJECT (call), "contact");
-  g_object_notify (G_OBJECT (call), "status");
-}
-
 static void
 tp_call_member_added_cb (EmpathyTpGroup *group,
                          EmpathyContact *contact,
@@ -306,6 +273,17 @@ tp_call_member_added_cb (EmpathyTpGroup *group,
 {
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
+  g_object_ref (call);
+  if (!priv->contact && !empathy_contact_is_user (contact))
+    {
+      priv->contact = g_object_ref (contact);
+      priv->is_incoming = TRUE;
+      priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
+      g_object_notify (G_OBJECT (call), "is-incoming");
+      g_object_notify (G_OBJECT (call), "contact");
+      g_object_notify (G_OBJECT (call), "status");
+    }
+
   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
       ((priv->is_incoming && contact != priv->contact) ||
        (!priv->is_incoming && contact == priv->contact)))
@@ -313,6 +291,32 @@ tp_call_member_added_cb (EmpathyTpGroup *group,
       priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
       g_object_notify (G_OBJECT (call), "status");
     }
+  g_object_unref (call);
+}
+
+static void
+tp_call_remote_pending_cb (EmpathyTpGroup *group,
+                           EmpathyContact *contact,
+                           EmpathyContact *actor,
+                           guint reason,
+                           const gchar *message,
+                           EmpathyTpCall *call)
+{
+  EmpathyTpCallPriv *priv = GET_PRIV (call);
+
+  g_object_ref (call);
+  if (!priv->contact && !empathy_contact_is_user (contact))
+    {
+      priv->contact = g_object_ref (contact);
+      priv->is_incoming = FALSE;
+      priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
+      g_object_notify (G_OBJECT (call), "is-incoming");
+      g_object_notify (G_OBJECT (call), "contact"); 
+      g_object_notify (G_OBJECT (call), "status");     
+      tp_call_request_streams_for_capabilities (call,
+          EMPATHY_CAPABILITIES_AUDIO);
+    }
+  g_object_unref (call);
 }
 
 static void
@@ -324,7 +328,7 @@ tp_call_channel_invalidated_cb (TpChannel     *channel,
 {
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
-  empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
+  DEBUG ("Channel invalidated: %s", message);
   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
   g_object_notify (G_OBJECT (call), "status");
 }
@@ -336,10 +340,7 @@ tp_call_async_cb (TpProxy *proxy,
                   GObject *call)
 {
   if (error)
-    {
-      empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
-          user_data, error->message);
-    }
+      DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
 }
 
 static void
@@ -350,11 +351,10 @@ tp_call_close_channel (EmpathyTpCall *call)
   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
       return;
 
-  empathy_debug (DEBUG_DOMAIN, "Closing channel");
+  DEBUG ("Closing channel");
 
   tp_cli_channel_call_close (priv->channel, -1,
-      (tp_cli_channel_callback_for_close) tp_call_async_cb,
-      "closing channel", NULL, G_OBJECT (call));
+      NULL, NULL, NULL, NULL);
 
   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
   g_object_notify (G_OBJECT (call), "status");
@@ -367,8 +367,7 @@ tp_call_stream_engine_invalidated_cb (TpProxy       *stream_engine,
                                      gchar         *message,
                                      EmpathyTpCall *call)
 {
-  empathy_debug (DEBUG_DOMAIN, "Stream engine proxy invalidated: %s",
-      message);
+  DEBUG ("Stream engine proxy invalidated: %s", message);
   tp_call_close_channel (call);
 }
 
@@ -383,13 +382,12 @@ tp_call_stream_engine_watch_name_owner_cb (TpDBusDaemon *daemon,
   /* G_STR_EMPTY(new_owner) means either stream-engine has not started yet or
    * has crashed. We want to close the channel if stream-engine has crashed.
    * */
-  empathy_debug (DEBUG_DOMAIN,
-                 "Watch SE: name='%s' SE running='%s' new_owner='%s'",
-                 name, priv->stream_engine_running ? "yes" : "no",
-                 new_owner ? new_owner : "none");
+  DEBUG ("Watch SE: name='%s' SE running='%s' new_owner='%s'",
+      name, priv->stream_engine_running ? "yes" : "no",
+      new_owner ? new_owner : "none");
   if (priv->stream_engine_running && G_STR_EMPTY (new_owner))
     {
-      empathy_debug (DEBUG_DOMAIN, "Stream engine falled off the bus");
+      DEBUG ("Stream engine falled off the bus");
       tp_call_close_channel (call);
       return;
     }
@@ -407,7 +405,7 @@ tp_call_stream_engine_handle_channel (EmpathyTpCall *call)
   guint handle;
   TpProxy *connection;
 
-  empathy_debug (DEBUG_DOMAIN, "Revving up the stream engine");
+  DEBUG ("Revving up the stream engine");
 
   priv->stream_engine = g_object_new (TP_TYPE_PROXY,
       "bus-name", STREAM_ENGINE_BUS_NAME,
@@ -481,14 +479,10 @@ tp_call_constructor (GType type,
   /* Setup group interface */
   priv->group = empathy_tp_group_new (priv->channel);
 
-  g_signal_connect (G_OBJECT (priv->group), "member-added",
+  g_signal_connect (priv->group, "member-added",
       G_CALLBACK (tp_call_member_added_cb), call);
-
-  if (empathy_tp_group_is_ready (priv->group))
-      tp_call_group_ready_cb (call);
-  else
-      g_signal_connect_swapped (priv->group, "notify::ready",
-          G_CALLBACK (tp_call_group_ready_cb), call);
+  g_signal_connect (priv->group, "remote-pending",
+      G_CALLBACK (tp_call_remote_pending_cb), call);
 
   /* Start stream engine */
   tp_call_stream_engine_handle_channel (call);
@@ -501,7 +495,7 @@ tp_call_finalize (GObject *object)
 {
   EmpathyTpCallPriv *priv = GET_PRIV (object);
 
-  empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
+  DEBUG ("Finalizing: %p", object);
 
   g_slice_free (EmpathyTpCallStream, priv->audio);
   g_slice_free (EmpathyTpCallStream, priv->video);
@@ -511,6 +505,7 @@ tp_call_finalize (GObject *object)
     {
       g_signal_handlers_disconnect_by_func (priv->channel,
           tp_call_channel_invalidated_cb, object);
+      tp_call_close_channel (EMPATHY_TP_CALL (object));
       g_object_unref (priv->channel);
     }
 
@@ -633,8 +628,10 @@ empathy_tp_call_class_init (EmpathyTpCallClass *klass)
 static void
 empathy_tp_call_init (EmpathyTpCall *call)
 {
-  EmpathyTpCallPriv *priv = GET_PRIV (call);
+  EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
+               EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
 
+  call->priv = priv;
   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
   priv->contact = NULL;
   priv->stream_engine_running = FALSE;
@@ -663,7 +660,7 @@ empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
 
-  empathy_debug (DEBUG_DOMAIN, "Accepting incoming call");
+  DEBUG ("Accepting incoming call");
 
   self_contact = empathy_tp_group_get_self_contact (priv->group);
   empathy_tp_group_add_member (priv->group, self_contact, NULL);
@@ -680,8 +677,7 @@ empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
 
-  empathy_debug (DEBUG_DOMAIN,
-      "Requesting video stream direction - is_sending: %d", is_sending);
+  DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
 
   if (!priv->video->exists)
     {
@@ -709,9 +705,8 @@ empathy_tp_call_add_preview_video (EmpathyTpCall *call,
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
-  g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
 
-  empathy_debug (DEBUG_DOMAIN, "Adding preview video");
+  DEBUG ("Adding preview video");
 
   emp_cli_stream_engine_call_add_preview_window (priv->stream_engine, -1,
       preview_video_socket_id,
@@ -727,9 +722,8 @@ empathy_tp_call_remove_preview_video (EmpathyTpCall *call,
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
-  g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
 
-  empathy_debug (DEBUG_DOMAIN, "Removing preview video");
+  DEBUG ("Removing preview video");
 
   emp_cli_stream_engine_call_remove_preview_window (priv->stream_engine, -1,
       preview_video_socket_id,
@@ -745,10 +739,8 @@ empathy_tp_call_add_output_video (EmpathyTpCall *call,
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
-  g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
 
-  empathy_debug (DEBUG_DOMAIN, "Adding output video - socket: %d",
-      output_video_socket_id);
+  DEBUG ("Adding output video - socket: %d", output_video_socket_id);
 
   emp_cli_stream_engine_call_set_output_window (priv->stream_engine, -1,
       TP_PROXY (priv->channel)->object_path,
@@ -767,7 +759,7 @@ empathy_tp_call_set_output_volume (EmpathyTpCall *call,
   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
 
-  empathy_debug (DEBUG_DOMAIN, "Setting output volume: %d", volume);
+  DEBUG ("Setting output volume: %d", volume);
 
   emp_cli_stream_engine_call_set_output_volume (priv->stream_engine, -1,
       TP_PROXY (priv->channel)->object_path,
@@ -784,9 +776,8 @@ empathy_tp_call_mute_output (EmpathyTpCall *call,
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
-  g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
 
-  empathy_debug (DEBUG_DOMAIN, "Setting output mute: %d", is_muted);
+  DEBUG ("Setting output mute: %d", is_muted);
 
   emp_cli_stream_engine_call_mute_output (priv->stream_engine, -1,
       TP_PROXY (priv->channel)->object_path,
@@ -803,9 +794,8 @@ empathy_tp_call_mute_input (EmpathyTpCall *call,
   EmpathyTpCallPriv *priv = GET_PRIV (call);
 
   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
-  g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
 
-  empathy_debug (DEBUG_DOMAIN, "Setting input mute: %d", is_muted);
+  DEBUG ("Setting input mute: %d", is_muted);
 
   emp_cli_stream_engine_call_mute_input (priv->stream_engine, -1,
       TP_PROXY (priv->channel)->object_path,
@@ -815,3 +805,48 @@ empathy_tp_call_mute_input (EmpathyTpCall *call,
       G_OBJECT (call));
 }
 
+void
+empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
+{
+  EmpathyTpCallPriv *priv = GET_PRIV (call);
+
+  g_return_if_fail (EMPATHY_IS_TP_CALL (call));
+  g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
+
+  if (!priv->audio->exists)
+      return;
+
+  tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
+      priv->audio->id, event,
+      (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
+      "starting tone", NULL, G_OBJECT (call));
+}
+
+void
+empathy_tp_call_stop_tone (EmpathyTpCall *call)
+{
+  EmpathyTpCallPriv *priv = GET_PRIV (call);
+
+  g_return_if_fail (EMPATHY_IS_TP_CALL (call));
+  g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
+
+  if (!priv->audio->exists)
+      return;
+
+  tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
+      priv->audio->id,
+      (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
+      "stoping tone", NULL, G_OBJECT (call));
+}
+
+gboolean
+empathy_tp_call_has_dtmf (EmpathyTpCall *call)
+{
+  EmpathyTpCallPriv *priv = GET_PRIV (call);
+
+  g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
+
+  return tp_proxy_has_interface_by_id (priv->channel,
+      TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
+}
+