]> git.0d.be Git - empathy.git/blobdiff - src/empathy-call-handler.c
Updated Swedish translation
[empathy.git] / src / empathy-call-handler.c
index 9fe89337d7f231c1bb023c85d774304746015ce0..2935c2a7ca2b1bd1382a06e302d05805b382a527 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <telepathy-glib/account-channel-request.h>
-#include <telepathy-glib/util.h>
-#include <telepathy-glib/interfaces.h>
-
-#include <telepathy-yell/telepathy-yell.h>
+#include "config.h"
+#include "empathy-call-handler.h"
 
 #include <telepathy-farstream/telepathy-farstream.h>
 
-#include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-tp-contact-factory.h>
-
-#include <libempathy-gtk/empathy-call-utils.h>
-
-#include "empathy-call-handler.h"
+#include "empathy-call-utils.h"
+#include "empathy-utils.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
-#include <libempathy/empathy-debug.h>
+#include "empathy-debug.h"
 
 G_DEFINE_TYPE(EmpathyCallHandler, empathy_call_handler, G_TYPE_OBJECT)
 
@@ -47,11 +36,13 @@ enum {
   CONFERENCE_ADDED,
   CONFERENCE_REMOVED,
   SRC_PAD_ADDED,
-  SINK_PAD_ADDED,
-  SINK_PAD_REMOVED,
+  CONTENT_ADDED,
+  CONTENT_REMOVED,
   CLOSED,
   CANDIDATES_CHANGED,
   STATE_CHANGED,
+  FRAMERATE_CHANGED,
+  RESOLUTION_CHANGED,
   LAST_SIGNAL
 };
 
@@ -61,8 +52,6 @@ enum {
   PROP_CALL_CHANNEL = 1,
   PROP_GST_BUS,
   PROP_CONTACT,
-  PROP_MEMBERS,
-  PROP_INITIAL_AUDIO,
   PROP_INITIAL_VIDEO,
   PROP_SEND_AUDIO_CODEC,
   PROP_SEND_VIDEO_CODEC,
@@ -77,13 +66,10 @@ enum {
 /* private structure */
 
 struct _EmpathyCallHandlerPriv {
-  TpyCallChannel *call;
+  TpCallChannel *call;
 
   EmpathyContact *contact;
-  /* GArray of TpContacts */
-  GArray *members;
   TfChannel *tfchannel;
-  gboolean initial_audio;
   gboolean initial_video;
 
   FsCodec *send_audio_codec;
@@ -94,6 +80,7 @@ struct _EmpathyCallHandlerPriv {
   FsCandidate *video_remote_candidate;
   FsCandidate *audio_local_candidate;
   FsCandidate *video_local_candidate;
+  gboolean accept_when_initialised;
 };
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
@@ -107,8 +94,6 @@ empathy_call_handler_dispose (GObject *object)
   tp_clear_object (&priv->call);
   tp_clear_object (&priv->contact);
 
-  tp_clear_pointer (&priv->members, g_array_unref);
-
   G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
 }
 
@@ -139,36 +124,22 @@ empathy_call_handler_init (EmpathyCallHandler *obj)
 }
 
 static void
-on_get_contacts_cb (TpConnection *connection,
-    guint n_contacts,
-    EmpathyContact * const * contacts,
-    guint n_failed,
-    const TpHandle *failed,
-    const GError *error,
-    gpointer user_data,
-    GObject *weak_object)
+on_call_accepted_cb (GObject *source_object,
+    GAsyncResult *res,
+    gpointer user_data)
 {
-  EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (weak_object);
-  EmpathyCallHandlerPriv *priv = GET_PRIV (self);
-  guint i;
-
-  if (n_failed > 0)
-    g_warning ("Failed to get %d EmpathyContacts: %s",
-        n_failed, error->message);
-
-  priv->members = g_array_sized_new (FALSE, TRUE,
-      sizeof (EmpathyContact *), n_contacts);
-
-  for (i = 0; i < n_contacts; i++)
-    g_object_ref (contacts[i]);
-
-  g_array_append_vals (priv->members, contacts, n_contacts);
+  TpCallChannel *call = TP_CALL_CHANNEL (source_object);
+  GError *error = NULL;
 
-  g_object_notify (G_OBJECT (self), "members");
+  if (!tp_call_channel_accept_finish (call, res, &error))
+    {
+      g_warning ("could not accept Call: %s", error->message);
+      g_error_free (error);
+    }
 }
 
 static void
-on_call_invalidated_cb (TpyCallChannel *call,
+on_call_invalidated_cb (TpCallChannel *call,
     guint domain,
     gint code,
     gchar *message,
@@ -180,83 +151,46 @@ on_call_invalidated_cb (TpyCallChannel *call,
     {
       /* Invalidated unexpectedly? Fake call ending */
       g_signal_emit (self, signals[STATE_CHANGED], 0,
-          TPY_CALL_STATE_ENDED, NULL);
+          TP_CALL_STATE_ENDED, NULL);
+      priv->accept_when_initialised = FALSE;
       tp_clear_object (&priv->call);
       tp_clear_object (&priv->tfchannel);
     }
 }
 
 static void
-on_call_state_changed_cb (TpyCallChannel *call,
-  TpyCallState state,
-  TpyCallFlags flags,
-   const GValueArray *call_state_reason,
-  GHashTable *call_state_details,
+on_call_state_changed_cb (TpCallChannel *call,
+  TpCallState state,
+  TpCallFlags flags,
+  TpCallStateReason *reason,
+  GHashTable *details,
   EmpathyCallHandler *handler)
 {
   EmpathyCallHandlerPriv *priv = handler->priv;
 
-  g_signal_emit (handler, signals[STATE_CHANGED], 0, state);
-
-  if (state == TPY_CALL_STATE_ENDED)
+  /* Clean up the TfChannel before bubbling the state-change signal
+   * further up. This ensures that the conference-removed signal is
+   * emitted before state-changed so that the client gets a chance
+   * to remove the conference from the pipeline before resetting the
+   * pipeline itself.
+   */
+  if (state == TP_CALL_STATE_ENDED)
     {
       tp_channel_close_async (TP_CHANNEL (call), NULL, NULL);
-
+      priv->accept_when_initialised = FALSE;
       tp_clear_object (&priv->call);
       tp_clear_object (&priv->tfchannel);
     }
-}
-
-static void
-on_members_changed_cb (TpyCallChannel *call,
-    GHashTable *members,
-    EmpathyCallHandler *self)
-{
-  EmpathyCallHandlerPriv *priv = GET_PRIV (self);
-  GHashTableIter iter;
-  gpointer key, value;
-  TpHandle *handles;
-  guint n_handles;
-  guint i = 0;
-
-  if (members == NULL)
-    return;
-
-  n_handles = g_hash_table_size (members);
-  if (n_handles == 0)
-    return;
-
-  handles = g_new0 (TpHandle, n_handles);
 
-  g_hash_table_iter_init (&iter, members);
-  while (g_hash_table_iter_next (&iter, &key, &value))
-    handles[i++] = GPOINTER_TO_UINT (key);
-
-  empathy_tp_contact_factory_get_from_handles (
-      tp_channel_borrow_connection (TP_CHANNEL (priv->call)),
-      n_handles, handles,
-      on_get_contacts_cb,
-      NULL, NULL, G_OBJECT (self));
-
-  g_free (handles);
-}
-
-static void
-empathy_call_handler_constructed (GObject *object)
-{
-  EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
-  EmpathyCallHandlerPriv *priv = GET_PRIV (self);
-//  GHashTable *members;
+  g_signal_emit (handler, signals[STATE_CHANGED], 0, state,
+      reason->dbus_reason);
 
-  g_signal_connect (priv->call, "members-changed",
-      G_CALLBACK (on_members_changed_cb), object);
-
-/* FIXME
-  g_object_get (priv->call, "members", &members, NULL);
-
-  if (members)
-    on_members_changed_cb (priv->call, members, self);
-*/
+  if (state == TP_CALL_STATE_INITIALISED &&
+      priv->accept_when_initialised)
+    {
+      tp_call_channel_accept_async (priv->call, on_call_accepted_cb, NULL);
+      priv->accept_when_initialised = FALSE;
+    }
 }
 
 static void
@@ -270,9 +204,6 @@ empathy_call_handler_set_property (GObject *object,
       case PROP_CONTACT:
         priv->contact = g_value_dup_object (value);
         break;
-      case PROP_MEMBERS:
-        priv->members = g_value_get_boxed (value);
-        break;
       case PROP_CALL_CHANNEL:
         g_return_if_fail (priv->call == NULL);
 
@@ -283,9 +214,6 @@ empathy_call_handler_set_property (GObject *object,
         tp_g_signal_connect_object (priv->call, "invalidated",
           G_CALLBACK (on_call_invalidated_cb), object, 0);
         break;
-      case PROP_INITIAL_AUDIO:
-        priv->initial_audio = g_value_get_boolean (value);
-        break;
       case PROP_INITIAL_VIDEO:
         priv->initial_video = g_value_get_boolean (value);
         break;
@@ -305,15 +233,9 @@ empathy_call_handler_get_property (GObject *object,
       case PROP_CONTACT:
         g_value_set_object (value, priv->contact);
         break;
-      case PROP_MEMBERS:
-        g_value_set_boxed (value, priv->members);
-        break;
       case PROP_CALL_CHANNEL:
         g_value_set_object (value, priv->call);
         break;
-      case PROP_INITIAL_AUDIO:
-        g_value_set_boolean (value, priv->initial_audio);
-        break;
       case PROP_INITIAL_VIDEO:
         g_value_set_boolean (value, priv->initial_video);
         break;
@@ -355,7 +277,6 @@ empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
 
   g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
 
-  object_class->constructed = empathy_call_handler_constructed;
   object_class->set_property = empathy_call_handler_set_property;
   object_class->get_property = empathy_call_handler_get_property;
   object_class->dispose = empathy_call_handler_dispose;
@@ -367,25 +288,12 @@ empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
 
-  param_spec = g_param_spec_boxed ("members",
-    "call members", "The call participants",
-    G_TYPE_ARRAY,
-    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
-  g_object_class_install_property (object_class, PROP_MEMBERS, param_spec);
-
   param_spec = g_param_spec_object ("call-channel",
     "call channel", "The call channel",
-    TPY_TYPE_CALL_CHANNEL,
+    TP_TYPE_CALL_CHANNEL,
     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (object_class, PROP_CALL_CHANNEL, param_spec);
 
-  param_spec = g_param_spec_boolean ("initial-audio",
-    "initial-audio", "Whether the call should start with audio",
-    TRUE,
-    G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
-  g_object_class_install_property (object_class, PROP_INITIAL_AUDIO,
-      param_spec);
-
   param_spec = g_param_spec_boolean ("initial-video",
     "initial-video", "Whether the call should start with video",
     FALSE,
@@ -472,21 +380,21 @@ empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
       g_cclosure_marshal_generic,
       G_TYPE_BOOLEAN,
-      2, GST_TYPE_PAD, G_TYPE_UINT);
+      2, TF_TYPE_CONTENT, GST_TYPE_PAD);
 
-  signals[SINK_PAD_ADDED] =
-    g_signal_new ("sink-pad-added", G_TYPE_FROM_CLASS (klass),
+  signals[CONTENT_ADDED] =
+    g_signal_new ("content-added", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
       g_cclosure_marshal_generic,
       G_TYPE_BOOLEAN,
-      2, GST_TYPE_PAD, G_TYPE_UINT);
+      1, TF_TYPE_CONTENT);
 
-  signals[SINK_PAD_REMOVED] =
-    g_signal_new ("sink-pad-removed", G_TYPE_FROM_CLASS (klass),
+  signals[CONTENT_REMOVED] =
+    g_signal_new ("content-removed", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
       g_cclosure_marshal_generic,
       G_TYPE_BOOLEAN,
-      2, GST_TYPE_PAD, G_TYPE_UINT);
+      1, TF_TYPE_CONTENT);
 
   signals[CLOSED] =
     g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
@@ -503,18 +411,31 @@ empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
 
   signals[STATE_CHANGED] =
     g_signal_new ("state-changed", G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, 0, NULL, NULL,
+      g_cclosure_marshal_generic,
+      G_TYPE_NONE, 2, G_TYPE_UINT, G_TYPE_STRING);
+
+  signals[FRAMERATE_CHANGED] =
+    g_signal_new ("framerate-changed", G_TYPE_FROM_CLASS (klass),
       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
       g_cclosure_marshal_generic,
       G_TYPE_NONE, 1, G_TYPE_UINT);
+
+  signals[RESOLUTION_CHANGED] =
+    g_signal_new ("resolution-changed", G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, 0, NULL, NULL,
+      g_cclosure_marshal_generic,
+      G_TYPE_NONE,
+      2, G_TYPE_UINT, G_TYPE_UINT);
 }
 
 EmpathyCallHandler *
-empathy_call_handler_new_for_channel (TpyCallChannel *call,
+empathy_call_handler_new_for_channel (TpCallChannel *call,
   EmpathyContact *contact)
 {
   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
     "call-channel", call,
-    "initial-video", tpy_call_channel_has_initial_video (call),
+    "initial-video", tp_call_channel_has_initial_video (call, NULL),
     "target-contact", contact,
     NULL));
 }
@@ -728,8 +649,7 @@ src_pad_added_error_idle (gpointer data)
 {
   TfContent *content = data;
 
-  tf_content_error (content, 0 /* FIXME */,
-      "Could not link sink", NULL);
+  tf_content_error_literal (content, "Could not link sink");
   g_object_unref (content);
 
   return FALSE;
@@ -743,25 +663,46 @@ on_tf_content_src_pad_added_cb (TfContent *content,
   FsCodec *codec,
   EmpathyCallHandler *handler)
 {
-  guint media_type;
   gboolean retval;
 
-  g_object_get (content, "media-type", &media_type, NULL);
-
   g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
-      pad, media_type, &retval);
+      content, pad, &retval);
 
   if (!retval)
     g_idle_add (src_pad_added_error_idle, g_object_ref (content));
 }
 
+static void
+on_tf_content_framerate_changed (TfContent *content,
+  GParamSpec *spec,
+  EmpathyCallHandler *handler)
+{
+  guint framerate;
+
+  g_object_get (content, "framerate", &framerate, NULL);
+
+  if (framerate != 0)
+    g_signal_emit (G_OBJECT (handler), signals[FRAMERATE_CHANGED], 0,
+        framerate);
+}
+
+static void
+on_tf_content_resolution_changed (TfContent *content,
+   guint width,
+   guint height,
+   EmpathyCallHandler *handler)
+{
+  if (width > 0 && height > 0)
+    g_signal_emit (G_OBJECT (handler), signals[RESOLUTION_CHANGED], 0,
+        width, height);
+}
+
 static void
 on_tf_channel_content_added_cb (TfChannel *tfchannel,
   TfContent *content,
   EmpathyCallHandler *handler)
 {
   FsMediaType mtype;
-  GstPad *spad;
   FsSession *session;
 //  FsStream *fs_stream;
   FsCodec *codec;
@@ -777,15 +718,11 @@ on_tf_channel_content_added_cb (TfChannel *tfchannel,
       G_CALLBACK (on_tf_content_stop_sending_cb), handler);
 #endif
 
-  g_object_get (content, "media-type", &mtype,
-    "sink-pad", &spad, NULL);
-
-  g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
-      spad, mtype, &retval);
+  g_signal_emit (G_OBJECT (handler), signals[CONTENT_ADDED], 0,
+    content, &retval);
 
  if (!retval)
-      tf_content_error (content, 0 /* FIXME */,
-          "Could not link source", NULL);
+      tf_content_error_literal (content, "Could not link source");
 
  /* Get sending codec */
  g_object_get (content, "fs-session", &session, NULL);
@@ -807,7 +744,34 @@ on_tf_channel_content_added_cb (TfChannel *tfchannel,
  tp_clear_object (&fs_stream);
 */
 
- gst_object_unref (spad);
+  g_object_get (content, "media-type", &mtype, NULL);
+
+ if (mtype == FS_MEDIA_TYPE_VIDEO)
+   {
+     guint framerate, width, height;
+
+     g_signal_connect (content, "notify::framerate",
+         G_CALLBACK (on_tf_content_framerate_changed),
+         handler);
+
+     g_signal_connect (content, "resolution-changed",
+         G_CALLBACK (on_tf_content_resolution_changed),
+         handler);
+
+     g_object_get (content,
+         "framerate", &framerate,
+         "width", &width,
+         "height", &height,
+         NULL);
+
+     if (framerate > 0)
+       g_signal_emit (G_OBJECT (handler), signals[FRAMERATE_CHANGED], 0,
+           framerate);
+
+     if (width > 0 && height > 0)
+       g_signal_emit (G_OBJECT (handler), signals[RESOLUTION_CHANGED], 0,
+           width, height);
+   }
 }
 
 static void
@@ -815,24 +779,18 @@ on_tf_channel_content_removed_cb (TfChannel *tfchannel,
   TfContent *content,
   EmpathyCallHandler *handler)
 {
-  FsMediaType mtype;
-  GstPad *spad;
   gboolean retval;
 
   DEBUG ("removing content");
 
-  g_object_get (content, "media-type", &mtype,
-    "sink-pad", &spad, NULL);
-
-  g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_REMOVED], 0,
-      spad, mtype, &retval);
+  g_signal_emit (G_OBJECT (handler), signals[CONTENT_REMOVED], 0,
+      content, &retval);
 
   if (!retval)
     {
       g_warning ("Could not remove content!");
 
-      tf_content_error (content, 0 /* FIXME */,
-          "Could not link source", NULL);
+      tf_content_error_literal (content, "Could not link source");
     }
 }
 
@@ -884,21 +842,6 @@ empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
       on_tf_channel_ready, self);
 }
 
-static void
-on_call_accepted_cb (GObject *source_object,
-    GAsyncResult *res,
-    gpointer user_data)
-{
-  TpyCallChannel *call = TPY_CALL_CHANNEL (source_object);
-  GError *error = NULL;
-
-  if (!tpy_call_channel_accept_finish (call, res, &error))
-    {
-      g_warning ("could not accept Call: %s", error->message);
-      g_error_free (error);
-    }
-}
-
 static void
 empathy_call_handler_request_cb (GObject *source,
     GAsyncResult *result,
@@ -919,13 +862,13 @@ empathy_call_handler_request_cb (GObject *source,
       return;
     }
 
-  if (!TPY_IS_CALL_CHANNEL (channel))
+  if (!TP_IS_CALL_CHANNEL (channel))
     {
       DEBUG ("The channel is not a Call channel!");
       return;
     }
 
-  priv->call = TPY_CALL_CHANNEL (channel);
+  priv->call = TP_CALL_CHANNEL (channel);
   tp_g_signal_connect_object (priv->call, "state-changed",
     G_CALLBACK (on_call_state_changed_cb), self, 0);
   tp_g_signal_connect_object (priv->call, "invalidated",
@@ -934,7 +877,7 @@ empathy_call_handler_request_cb (GObject *source,
   g_object_notify (G_OBJECT (self), "call-channel");
 
   empathy_call_handler_start_tpfs (self);
-  tpy_call_channel_accept_async (priv->call, on_call_accepted_cb, NULL);
+  tp_call_channel_accept_async (priv->call, on_call_accepted_cb, NULL);
 }
 
 void
@@ -944,30 +887,43 @@ empathy_call_handler_start_call (EmpathyCallHandler *handler,
   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
   TpAccountChannelRequest *req;
   TpAccount *account;
-  GHashTable *request;
 
   if (priv->call != NULL)
     {
       empathy_call_handler_start_tpfs (handler);
-      tpy_call_channel_accept_async (priv->call, on_call_accepted_cb, NULL);
+
+      if (tp_channel_get_requested (TP_CHANNEL (priv->call)))
+        {
+          /* accept outgoing channels immediately */
+          tp_call_channel_accept_async (priv->call,
+              on_call_accepted_cb, NULL);
+        }
+      else
+        {
+          /* accepting incoming channels when they are INITIALISED */
+          if (tp_call_channel_get_state (priv->call, NULL, NULL, NULL) ==
+              TP_CALL_STATE_INITIALISED)
+            tp_call_channel_accept_async (priv->call,
+                on_call_accepted_cb, NULL);
+          else
+            priv->accept_when_initialised = TRUE;
+        }
+
       return;
     }
 
-  /* No TpyCallChannel (we are redialing). Request a new call channel */
+  /* No TpCallChannel (we are redialing). Request a new call channel */
   g_assert (priv->contact != NULL);
 
   account = empathy_contact_get_account (priv->contact);
-  request = empathy_call_create_call_request (
-      empathy_contact_get_id (priv->contact),
-      priv->initial_audio, priv->initial_video);
 
-  req = tp_account_channel_request_new (account, request, timestamp);
+  req = empathy_call_create_call_request (account,
+      empathy_contact_get_id (priv->contact), priv->initial_video, timestamp);
 
   tp_account_channel_request_create_and_handle_channel_async (req, NULL,
       empathy_call_handler_request_cb, handler);
 
   g_object_unref (req);
-  g_hash_table_unref (request);
 }
 
 /**
@@ -983,13 +939,9 @@ empathy_call_handler_stop_call (EmpathyCallHandler *handler)
 
   if (priv->call != NULL)
     {
-      tpy_call_channel_hangup_async (priv->call,
-          TPY_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
+      tp_call_channel_hangup_async (priv->call,
+          TP_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
           "", "", NULL, NULL);
-      tp_channel_close_async (TP_CHANNEL (priv->call),
-        NULL, NULL);
-      tp_clear_object (&priv->call);
-      tp_clear_object (&priv->tfchannel);
     }
 }
 
@@ -1077,3 +1029,9 @@ empathy_call_handler_get_video_local_candidate (
 
   return priv->video_local_candidate;
 }
+
+EmpathyContact *
+empathy_call_handler_get_contact (EmpathyCallHandler *self)
+{
+  return self->priv->contact;
+}