]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-call-handler.c
Merge branch 'master' into tp-tube
[empathy.git] / libempathy / empathy-call-handler.c
index 1fba9317a939617d08bbac48b02a1590332e1ccf..820de6de59592cd6f4ff0a62ccc218ae7ea80110 100644 (file)
 #include "empathy-call-handler.h"
 #include "empathy-dispatcher.h"
 #include "empathy-marshal.h"
+#include "empathy-utils.h"
 
 G_DEFINE_TYPE(EmpathyCallHandler, empathy_call_handler, G_TYPE_OBJECT)
 
 /* signal enum */
-enum
-{
+enum {
   CONFERENCE_ADDED,
   SRC_PAD_ADDED,
   SINK_PAD_ADDED,
+  REQUEST_RESOURCE,
+  CLOSED,
   LAST_SIGNAL
 };
 
@@ -51,31 +53,65 @@ enum {
 };
 
 /* private structure */
-typedef struct _EmpathyCallHandlerPriv EmpathyCallHandlerPriv;
 
-struct _EmpathyCallHandlerPriv
-{
+typedef struct {
   gboolean dispose_has_run;
   EmpathyTpCall *call;
   EmpathyContact *contact;
   TfChannel *tfchannel;
-  GstBus *bus;
-};
+} EmpathyCallHandlerPriv;
 
-#define GET_PRIV(o) \
-  (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CALL_HANDLER,\
-    EmpathyCallHandlerPriv))
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
 
 static void
-empathy_call_handler_init (EmpathyCallHandler *obj)
+empathy_call_handler_dispose (GObject *object)
 {
-  //EmpathyCallHandlerPriv *priv = GET_PRIV (obj);
+  EmpathyCallHandlerPriv *priv = GET_PRIV (object);
+
+  if (priv->dispose_has_run)
+    return;
 
-  /* allocate any data required by the object here */
+  priv->dispose_has_run = TRUE;
+
+  if (priv->contact != NULL)
+    g_object_unref (priv->contact);
+
+  priv->contact = NULL;
+
+  if (priv->tfchannel != NULL)
+    g_object_unref (priv->tfchannel);
+
+  priv->tfchannel = NULL;
+
+  if (priv->call != NULL)
+    {
+      empathy_tp_call_close (priv->call);
+      g_object_unref (priv->call);
+    }
+
+  priv->call = NULL;
+
+  /* release any references held by the object here */
+  if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose)
+    G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
 }
 
-static void empathy_call_handler_dispose (GObject *object);
-static void empathy_call_handler_finalize (GObject *object);
+static void
+empathy_call_handler_finalize (GObject *object)
+{
+  /* free any data held directly by the object here */
+  if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize)
+    G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize (object);
+}
+
+static void
+empathy_call_handler_init (EmpathyCallHandler *obj)
+{
+  EmpathyCallHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
+    EMPATHY_TYPE_CALL_HANDLER, EmpathyCallHandlerPriv);
+
+  obj->priv = priv;
+}
 
 static void
 empathy_call_handler_set_property (GObject *object,
@@ -91,9 +127,6 @@ empathy_call_handler_set_property (GObject *object,
       case PROP_TP_CALL:
         priv->call = g_value_dup_object (value);
         break;
-      case PROP_GST_BUS:
-        priv->bus = g_value_dup_object (value);
-        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -113,9 +146,6 @@ empathy_call_handler_get_property (GObject *object,
       case PROP_TP_CALL:
         g_value_set_object (value, priv->call);
         break;
-      case PROP_GST_BUS:
-        g_value_set_object (value, priv->bus);
-        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -141,15 +171,9 @@ empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
 
-  param_spec = g_param_spec_object ("gst-bus",
-    "gst-bus", "The gstreamer bus",
-    GST_TYPE_BUS,
-    G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
-  g_object_class_install_property (object_class, PROP_GST_BUS, param_spec);
-
   param_spec = g_param_spec_object ("tp-call",
     "tp-call", "The calls channel wrapper",
-    EMPATHY_TYPE_CONTACT,
+    EMPATHY_TYPE_TP_CALL,
     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (object_class, PROP_TP_CALL, param_spec);
 
@@ -173,43 +197,20 @@ empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
       _empathy_marshal_VOID__OBJECT_UINT,
       G_TYPE_NONE,
       2, GST_TYPE_PAD, G_TYPE_UINT);
-}
-
-void
-empathy_call_handler_dispose (GObject *object)
-{
-  EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
-  EmpathyCallHandlerPriv *priv = GET_PRIV (self);
-
-  if (priv->dispose_has_run)
-    return;
-
-  priv->dispose_has_run = TRUE;
-
-  if (priv->contact != NULL)
-    g_object_unref (priv->contact);
-
-  /* FIXME close the call ? */
-  if (priv->call != NULL)
-    g_object_unref (priv->call);
-
-  priv->call = NULL;
 
+  signals[REQUEST_RESOURCE] =
+    g_signal_new ("request-resource", G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, 0,
+      g_signal_accumulator_true_handled, NULL,
+      _empathy_marshal_BOOLEAN__UINT_UINT,
+      G_TYPE_BOOLEAN, 2, G_TYPE_UINT, G_TYPE_UINT);
 
-  /* release any references held by the object here */
-  if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose)
-    G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
-}
-
-void
-empathy_call_handler_finalize (GObject *object)
-{
-  //EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
-  //EmpathyCallHandlerPriv *priv = GET_PRIV (self);
-
-  /* free any data held directly by the object here */
-
-  G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize (object);
+  signals[CLOSED] =
+    g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
+      G_SIGNAL_RUN_LAST, 0, NULL, NULL,
+      g_cclosure_marshal_VOID__VOID,
+      G_TYPE_NONE,
+      0);
 }
 
 EmpathyCallHandler *
@@ -226,18 +227,16 @@ empathy_call_handler_new_for_channel (EmpathyTpCall *call)
     "tp-call", call, NULL));
 }
 
-static gboolean
-empathy_call_handler_pipeline_bus_watch (GstBus *bus, GstMessage *message,
-  gpointer user_data)
+void
+empathy_call_handler_bus_message (EmpathyCallHandler *handler,
+  GstBus *bus, GstMessage *message)
 {
-  EmpathyCallHandler *handler = EMPATHY_CALL_HANDLER (user_data);
   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
 
-  g_assert (priv->tfchannel != NULL);
+  if (priv->tfchannel == NULL)
+    return;
 
   tf_channel_bus_message (priv->tfchannel, message);
-
-  return TRUE;
 }
 
 static void
@@ -245,12 +244,6 @@ empathy_call_handler_tf_channel_session_created_cb (TfChannel *tfchannel,
   FsConference *conference, FsParticipant *participant,
   EmpathyCallHandler *self)
 {
-  EmpathyCallHandlerPriv *priv = GET_PRIV (self);
-
-  //gst_bus_enable_essage_emission (priv->bus);
-
-  gst_bus_add_watch (priv->bus, empathy_call_handler_pipeline_bus_watch, self);
-
   g_signal_emit (G_OBJECT (self), signals[CONFERENCE_ADDED], 0,
     GST_ELEMENT (conference));
 }
@@ -272,7 +265,15 @@ static gboolean
 empathy_call_handler_tf_stream_request_resource_cb (TfStream *stream,
   guint direction, EmpathyTpCall *call)
 {
-  return TRUE;
+  gboolean ret;
+  guint media_type;
+
+  g_object_get (G_OBJECT (stream), "media-type", &media_type, NULL);
+
+  g_signal_emit (G_OBJECT (call),
+    signals[REQUEST_RESOURCE], 0, media_type, direction, &ret);
+
+  return ret;
 }
 
 static void
@@ -297,6 +298,87 @@ empathy_call_handler_tf_channel_stream_created_cb (TfChannel *tfchannel,
   gst_object_unref (spad);
 }
 
+static void
+empathy_call_handler_tf_channel_closed_cb (TfChannel *tfchannel,
+  EmpathyCallHandler *handler)
+{
+  g_signal_emit (G_OBJECT (handler), signals[CLOSED], 0);
+}
+
+static GList *
+empathy_call_handler_tf_channel_codec_config_get_defaults (FsCodec *codecs)
+{
+  GList *l = NULL;
+  int i;
+
+  for (i = 0; codecs[i].encoding_name != NULL; i++)
+      l = g_list_append (l, fs_codec_copy (codecs + i));
+
+  return l;
+}
+
+static GList *
+empathy_call_handler_tf_channel_codec_config_cb (TfChannel *channel,
+  guint stream_id, FsMediaType media_type, guint direction, gpointer user_data)
+{
+  FsCodec audio_codecs[] = {
+    { FS_CODEC_ID_ANY, "SPEEX", FS_MEDIA_TYPE_AUDIO, 16000, },
+    { FS_CODEC_ID_ANY, "SPEEX", FS_MEDIA_TYPE_AUDIO, 8000, },
+
+    { FS_CODEC_ID_DISABLE, "DV",     FS_MEDIA_TYPE_AUDIO, },
+    { FS_CODEC_ID_DISABLE, "MPA",    FS_MEDIA_TYPE_AUDIO, },
+    { FS_CODEC_ID_DISABLE, "VORBIS", FS_MEDIA_TYPE_AUDIO, },
+    { FS_CODEC_ID_DISABLE, "MP3",    FS_MEDIA_TYPE_AUDIO, },
+    { 0, NULL, 0,}
+  };
+  FsCodec video_codecs[] = {
+    { FS_CODEC_ID_ANY, "H264",   FS_MEDIA_TYPE_VIDEO, },
+    { FS_CODEC_ID_ANY, "THEORA", FS_MEDIA_TYPE_VIDEO, },
+    { FS_CODEC_ID_ANY, "H263",   FS_MEDIA_TYPE_VIDEO, },
+
+    { FS_CODEC_ID_DISABLE, "DV",   FS_MEDIA_TYPE_VIDEO, },
+    { FS_CODEC_ID_DISABLE, "JPEG", FS_MEDIA_TYPE_VIDEO, },
+    { FS_CODEC_ID_DISABLE, "MPV",  FS_MEDIA_TYPE_VIDEO, },
+    { 0, NULL, 0}
+  };
+
+  switch (media_type)
+    {
+      case FS_MEDIA_TYPE_AUDIO:
+        return empathy_call_handler_tf_channel_codec_config_get_defaults
+          (audio_codecs);
+      case FS_MEDIA_TYPE_VIDEO:
+        return empathy_call_handler_tf_channel_codec_config_get_defaults
+          (video_codecs);
+    }
+
+  return NULL;
+}
+
+static void
+empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
+{
+  EmpathyCallHandlerPriv *priv = GET_PRIV (self);
+  TpChannel *channel;
+
+  g_object_get (priv->call, "channel", &channel, NULL);
+
+  g_assert (channel != NULL);
+
+  priv->tfchannel = tf_channel_new (channel);
+
+  /* Set up the telepathy farsight channel */
+  g_signal_connect (priv->tfchannel, "session-created",
+      G_CALLBACK (empathy_call_handler_tf_channel_session_created_cb), self);
+  g_signal_connect (priv->tfchannel, "stream-created",
+      G_CALLBACK (empathy_call_handler_tf_channel_stream_created_cb), self);
+  g_signal_connect (priv->tfchannel, "closed",
+      G_CALLBACK (empathy_call_handler_tf_channel_closed_cb), self);
+  g_signal_connect (priv->tfchannel, "stream-get-codec-config",
+      G_CALLBACK (empathy_call_handler_tf_channel_codec_config_cb), self);
+
+  g_object_unref (channel);
+}
 
 static void
 empathy_call_handler_request_cb (EmpathyDispatchOperation *operation,
@@ -310,45 +392,47 @@ empathy_call_handler_request_cb (EmpathyDispatchOperation *operation,
 
   priv->call = EMPATHY_TP_CALL (
     empathy_dispatch_operation_get_channel_wrapper (operation));
-  g_object_ref (priv->call);
 
-  priv->tfchannel = tf_channel_new (
-    empathy_dispatch_operation_get_channel (operation));
+  g_object_ref (priv->call);
 
-  /* Set up the telepathy farsight channel */
-  g_signal_connect (priv->tfchannel, "session-created",
-      G_CALLBACK (empathy_call_handler_tf_channel_session_created_cb), self);
-  g_signal_connect (priv->tfchannel, "stream-created",
-      G_CALLBACK (empathy_call_handler_tf_channel_stream_created_cb), self);
+  empathy_call_handler_start_tpfs (self);
 
   empathy_tp_call_to (priv->call, priv->contact);
 
   empathy_dispatch_operation_claim (operation);
 }
 
-static void
-empathy_call_handler_contact_ready_cb (EmpathyContact *contact,
-  const GError *error, gpointer user_data, GObject *object)
+void
+empathy_call_handler_start_call (EmpathyCallHandler *handler)
 {
-  EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
-  EmpathyCallHandlerPriv *priv = GET_PRIV (self);
+
+  EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
   EmpathyDispatcher *dispatcher;
-  McAccount *account;
+  TpConnection *connection;
   GStrv allowed;
   GValue *value;
-  GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
-      (GDestroyNotify) tp_g_value_slice_free);
+  GHashTable *request;
+
+  if (priv->call != NULL)
+    {
+      empathy_call_handler_start_tpfs (handler);
+      empathy_tp_call_accept_incoming_call (priv->call);
+      return;
+    }
 
   g_assert (priv->contact != NULL);
 
   dispatcher = empathy_dispatcher_dup_singleton ();
-  account = empathy_contact_get_account (priv->contact);
-  allowed = empathy_dispatcher_find_channel_class (dispatcher, account,
+  connection = empathy_contact_get_connection (priv->contact);
+  allowed = empathy_dispatcher_find_channel_class (dispatcher, connection,
     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT);
 
-  if (!tp_strv_contains ((const gchar * const *)allowed,
+  if (!tp_strv_contains ((const gchar * const *) allowed,
       TP_IFACE_CHANNEL ".TargetHandle"))
-    g_assert_not_reached ();
+    return;
+
+  request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+      (GDestroyNotify) tp_g_value_slice_free);
 
   /* org.freedesktop.Telepathy.Channel.ChannelType */
   value = tp_g_value_slice_new (G_TYPE_STRING);
@@ -365,32 +449,9 @@ empathy_call_handler_contact_ready_cb (EmpathyContact *contact,
   g_value_set_uint (value, empathy_contact_get_handle (priv->contact));
   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
 
-  empathy_dispatcher_create_channel (dispatcher, account,
-    request, empathy_call_handler_request_cb, self);
+  empathy_dispatcher_create_channel (dispatcher, connection,
+    request, empathy_call_handler_request_cb, handler);
 
   g_object_unref (dispatcher);
 }
 
-void
-empathy_call_handler_start_call (EmpathyCallHandler *handler)
-{
-
-  EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
-
-  g_assert (priv->contact != NULL);
-
-  empathy_contact_call_when_ready (priv->contact,
-    EMPATHY_CONTACT_READY_ID,
-    empathy_call_handler_contact_ready_cb, NULL, NULL, G_OBJECT (handler));
-}
-
-void
-empathy_call_handler_set_bus (EmpathyCallHandler *handler, GstBus *bus)
-{
-  EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
-
-  g_assert (priv->bus == NULL);
-
-  priv->bus = g_object_ref (bus);
-}
-