]> git.0d.be Git - empathy.git/blobdiff - src/empathy-call-window.c
Merge branch 'debug-window'
[empathy.git] / src / empathy-call-window.c
index 9946dd320b2dd24b37ca2fb8aae6c539259863ad..f7ba242326fadb68f4dd71202d124a59a6882308 100644 (file)
@@ -31,6 +31,8 @@
 
 #include <telepathy-farsight/channel.h>
 
+#include <gst/farsight/fs-element-added-notifier.h>
+
 #include <libempathy/empathy-tp-contact-factory.h>
 #include <libempathy/empathy-call-factory.h>
 #include <libempathy/empathy-utils.h>
@@ -40,9 +42,9 @@
 #include <libempathy-gtk/empathy-audio-sink.h>
 #include <libempathy-gtk/empathy-video-src.h>
 #include <libempathy-gtk/empathy-ui-utils.h>
+#include <libempathy-gtk/empathy-sound.h>
 
 #include "empathy-call-window.h"
-
 #include "empathy-call-window-fullscreen.h"
 #include "empathy-sidebar.h"
 
 #define REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT
 #define REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT
 
-#define CONNECTING_STATUS_TEXT _("Connecting...")
-
 /* If an video input error occurs, the error message will start with "v4l" */
 #define VIDEO_INPUT_ERROR_PREFIX "v4l"
 
+/* The time interval in milliseconds between 2 outgoing rings */
+#define MS_BETWEEN_RING 500
+
 G_DEFINE_TYPE(EmpathyCallWindow, empathy_call_window, GTK_TYPE_WINDOW)
 
 /* signal enum */
@@ -81,6 +84,13 @@ enum {
   PROP_CALL_HANDLER = 1,
 };
 
+typedef enum {
+  CONNECTING,
+  CONNECTED,
+  DISCONNECTED,
+  REDIALING
+} CallState;
+
 /* private structure */
 typedef struct _EmpathyCallWindowPriv EmpathyCallWindowPriv;
 
@@ -90,7 +100,8 @@ struct _EmpathyCallWindowPriv
   EmpathyCallHandler *handler;
   EmpathyContact *contact;
 
-  gboolean connected;
+  guint call_state;
+  gboolean outgoing;
 
   GtkUIManager *ui_manager;
   GtkWidget *video_output;
@@ -128,8 +139,10 @@ struct _EmpathyCallWindowPriv
   GtkWidget *vbox;
 
   gulong video_output_motion_handler_id;
+  guint bus_message_source_id;
 
   gdouble volume;
+  GtkWidget *volume_scale;
   GtkWidget *volume_progress_bar;
   GtkAdjustment *audio_input_adj;
 
@@ -144,6 +157,8 @@ struct _EmpathyCallWindowPriv
   GstElement *funnel;
   GstElement *liveadder;
 
+  FsElementAddedNotifier *fsnotifier;
+
   guint context_id;
 
   GTimer *timer;
@@ -165,10 +180,6 @@ struct _EmpathyCallWindowPriv
   gboolean sidebar_was_visible_before_fs;
   gint original_width_before_fs;
   gint original_height_before_fs;
-
-  /* Used to indicate if we are currently redialing. If we are, as soon as the
-     channel is closed, the call is automatically re-initiated.*/
-  gboolean redialing;
 };
 
 #define GET_PRIV(o) \
@@ -480,7 +491,10 @@ empathy_call_window_mic_volume_changed_cb (GtkAdjustment *adj,
   EmpathyCallWindowPriv *priv = GET_PRIV (self);
   gdouble volume;
 
-  volume =  gtk_adjustment_get_value (adj)/100.0;
+  if (priv->audio_input == NULL)
+    return;
+
+  volume = gtk_adjustment_get_value (adj)/100.0;
 
   /* Don't store the volume because of muting */
   if (volume > 0 || gtk_toggle_tool_button_get_active (
@@ -488,7 +502,7 @@ empathy_call_window_mic_volume_changed_cb (GtkAdjustment *adj,
     priv->volume = volume;
 
   /* Ensure that the toggle button is active if the volume is > 0 and inactive
-   * if it's smaller then 0 */
+   * if it's smaller than 0 */
   if ((volume > 0) != gtk_toggle_tool_button_get_active (
         GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
     gtk_toggle_tool_button_set_active (
@@ -513,27 +527,27 @@ static GtkWidget *
 empathy_call_window_create_audio_input (EmpathyCallWindow *self)
 {
   EmpathyCallWindowPriv *priv = GET_PRIV (self);
-  GtkWidget *hbox, *vbox, *scale, *label;
-  GtkAdjustment *adj;
+  GtkWidget *hbox, *vbox, *label;
 
   hbox = gtk_hbox_new (TRUE, 3);
 
   vbox = gtk_vbox_new (FALSE, 3);
   gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 3);
 
-  scale = gtk_vscale_new_with_range (0, 150, 100);
-  gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
+  priv->volume_scale = gtk_vscale_new_with_range (0, 150, 100);
+  gtk_range_set_inverted (GTK_RANGE (priv->volume_scale), TRUE);
   label = gtk_label_new (_("Volume"));
 
-  priv->audio_input_adj = adj = gtk_range_get_adjustment (GTK_RANGE (scale));
+  priv->audio_input_adj = gtk_range_get_adjustment (
+    GTK_RANGE (priv->volume_scale));
   priv->volume =  empathy_audio_src_get_volume (EMPATHY_GST_AUDIO_SRC
     (priv->audio_input));
-  gtk_adjustment_set_value (adj, priv->volume * 100);
+  gtk_adjustment_set_value (priv->audio_input_adj, priv->volume * 100);
 
-  g_signal_connect (G_OBJECT (adj), "value-changed",
+  g_signal_connect (G_OBJECT (priv->audio_input_adj), "value-changed",
     G_CALLBACK (empathy_call_window_mic_volume_changed_cb), self);
 
-  gtk_box_pack_start (GTK_BOX (vbox), scale, TRUE, TRUE, 3);
+  gtk_box_pack_start (GTK_BOX (vbox), priv->volume_scale, TRUE, TRUE, 3);
   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 3);
 
   priv->volume_progress_bar = gtk_progress_bar_new ();
@@ -649,6 +663,19 @@ empathy_call_window_setup_video_preview (EmpathyCallWindow *window)
   gst_element_set_state (priv->video_tee, GST_STATE_PLAYING);
 }
 
+static void
+empathy_call_window_set_state_connecting (EmpathyCallWindow *window)
+{
+  EmpathyCallWindowPriv *priv = GET_PRIV (window);
+
+  empathy_call_window_status_message (window, _("Connecting..."));
+  priv->call_state = CONNECTING;
+
+  if (priv->outgoing)
+    empathy_sound_start_playing (GTK_WIDGET (window),
+        EMPATHY_SOUND_PHONE_OUTGOING, MS_BETWEEN_RING);
+}
+
 static void
 empathy_call_window_init (EmpathyCallWindow *self)
 {
@@ -660,6 +687,8 @@ empathy_call_window_init (EmpathyCallWindow *self)
   GtkWidget *page;
   GstBus *bus;
   gchar *filename;
+  GKeyFile *keyfile;
+  GError *error = NULL;
 
   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
   gui = empathy_builder_get_file (filename,
@@ -676,6 +705,7 @@ empathy_call_window_init (EmpathyCallWindow *self)
     "ui_manager", &priv->ui_manager,
     "menufullscreen", &priv->menu_fullscreen,
     NULL);
+  g_free (filename);
 
   empathy_builder_connect (gui, self,
     "menuhangup", "activate", empathy_call_window_hangup_cb,
@@ -702,7 +732,27 @@ empathy_call_window_init (EmpathyCallWindow *self)
 
   priv->pipeline = gst_pipeline_new (NULL);
   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
-  gst_bus_add_watch (bus, empathy_call_window_bus_message, self);
+  priv->bus_message_source_id = gst_bus_add_watch (bus,
+      empathy_call_window_bus_message, self);
+
+  priv->fsnotifier = fs_element_added_notifier_new ();
+  fs_element_added_notifier_add (priv->fsnotifier, GST_BIN (priv->pipeline));
+
+  keyfile = g_key_file_new ();
+  filename = empathy_file_lookup ("element-properties", "data");
+  if (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, &error))
+    {
+      fs_element_added_notifier_set_properties_from_keyfile (priv->fsnotifier,
+          keyfile);
+    }
+  else
+    {
+      g_warning ("Could not load element-properties file: %s", error->message);
+      g_key_file_free (keyfile);
+      g_clear_error (&error);
+    }
+  g_free (filename);
+
 
   priv->remote_user_output_frame = gtk_frame_new (NULL);
   gtk_widget_set_size_request (priv->remote_user_output_frame,
@@ -778,13 +828,10 @@ empathy_call_window_init (EmpathyCallWindow *self)
   g_signal_connect (G_OBJECT (self), "key-press-event",
       G_CALLBACK (empathy_call_window_key_press_cb), self);
 
-  empathy_call_window_status_message (self, CONNECTING_STATUS_TEXT);
-
   priv->timer = g_timer_new ();
 
   g_object_ref (priv->ui_manager);
   g_object_unref (gui);
-  g_free (filename);
 }
 
 /* Instead of specifying a width and a height, we specify only one size. That's
@@ -793,7 +840,6 @@ static void
 init_contact_avatar_with_size (EmpathyContact *contact, GtkWidget *image_widget,
     gint size)
 {
-
   GdkPixbuf *pixbuf_avatar = NULL;
 
   if (contact != NULL)
@@ -817,6 +863,8 @@ set_window_title (EmpathyCallWindow *self)
   EmpathyCallWindowPriv *priv = GET_PRIV (self);
   gchar *tmp;
 
+  /* translators: Call is a noun and %s is the contact name. This string is used
+   * in the window title */
   tmp = g_strdup_printf (_("Call with %s"),
       empathy_contact_get_name (priv->contact));
   gtk_window_set_title (GTK_WINDOW (self), tmp);
@@ -886,6 +934,7 @@ empathy_call_window_setup_avatars (EmpathyCallWindow *self,
   else
     {
       g_warning ("call handler doesn't have a contact");
+      /* translators: Call is a noun. This string is used in the window title */
       gtk_window_set_title (GTK_WINDOW (self), _("Call"));
 
       /* Since we can't access the remote contact, we can't get a connection
@@ -921,10 +970,18 @@ empathy_call_window_constructed (GObject *object)
 {
   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
   EmpathyCallWindowPriv *priv = GET_PRIV (self);
+  EmpathyTpCall *call;
 
   g_assert (priv->handler != NULL);
+
+  g_object_get (priv->handler, "tp-call", &call, NULL);
+  priv->outgoing = (call == NULL);
+  if (call != NULL)
+    g_object_unref (call);
+
   empathy_call_window_setup_avatars (self, priv->handler);
   empathy_call_window_setup_video_preview_visibility (self, priv->handler);
+  empathy_call_window_set_state_connecting (self);
 }
 
 static void empathy_call_window_dispose (GObject *object);
@@ -985,7 +1042,6 @@ empathy_call_window_class_init (
     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
   g_object_class_install_property (object_class,
     PROP_CALL_HANDLER, param_spec);
-
 }
 
 static void
@@ -1019,7 +1075,6 @@ empathy_call_window_dispose (GObject *object)
 
   if (priv->handler != NULL)
     g_object_unref (priv->handler);
-
   priv->handler = NULL;
 
   if (priv->pipeline != NULL)
@@ -1042,6 +1097,10 @@ empathy_call_window_dispose (GObject *object)
     g_object_unref (priv->video_tee);
   priv->video_tee = NULL;
 
+  if (priv->fsnotifier != NULL)
+    g_object_unref (priv->fsnotifier);
+  priv->fsnotifier = NULL;
+
   if (priv->timer_id != 0)
     g_source_remove (priv->timer_id);
   priv->timer_id = 0;
@@ -1076,6 +1135,12 @@ empathy_call_window_finalize (GObject *object)
       priv->video_output_motion_handler_id = 0;
     }
 
+  if (priv->bus_message_source_id != 0)
+    {
+      g_source_remove (priv->bus_message_source_id);
+      priv->bus_message_source_id = 0;
+    }
+
   /* free any data held directly by the object here */
   g_mutex_free (priv->lock);
 
@@ -1130,6 +1195,12 @@ empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
   if (priv->pipeline == NULL)
     return TRUE;
 
+  if (priv->bus_message_source_id != 0)
+    {
+      g_source_remove (priv->bus_message_source_id);
+      priv->bus_message_source_id = 0;
+    }
+
   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
 
   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
@@ -1147,6 +1218,9 @@ empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
         g_object_unref (priv->audio_input);
       priv->audio_input = NULL;
 
+      g_signal_handlers_disconnect_by_func (priv->audio_input_adj,
+          empathy_call_window_mic_volume_changed_cb, self);
+
       if (priv->audio_output != NULL)
         g_object_unref (priv->audio_output);
       priv->audio_output = NULL;
@@ -1180,8 +1254,16 @@ empathy_call_window_disconnected (EmpathyCallWindow *self)
   EmpathyCallWindowPriv *priv = GET_PRIV (self);
   gboolean could_reset_pipeline = empathy_call_window_reset_pipeline (self);
 
+  if (priv->call_state == CONNECTING)
+      empathy_sound_stop (EMPATHY_SOUND_PHONE_OUTGOING);
+
+  if (priv->call_state != REDIALING)
+    priv->call_state = DISCONNECTED;
+
   if (could_reset_pipeline)
     {
+      gboolean initial_video = empathy_call_handler_has_initial_video (
+          priv->handler);
       g_mutex_lock (priv->lock);
 
       g_timer_stop (priv->timer);
@@ -1196,10 +1278,30 @@ empathy_call_window_disconnected (EmpathyCallWindow *self)
 
       gtk_action_set_sensitive (priv->redial, TRUE);
       gtk_widget_set_sensitive (priv->redial_button, TRUE);
+
+      /* Reseting the send_video, camera_buton and mic_button to their
+         initial state */
       gtk_widget_set_sensitive (priv->camera_button, FALSE);
+      gtk_widget_set_sensitive (priv->mic_button, FALSE);
       gtk_action_set_sensitive (priv->send_video, FALSE);
+      gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->send_video),
+          initial_video);
+      gtk_toggle_tool_button_set_active (
+          GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), initial_video);
+      gtk_toggle_tool_button_set_active (
+          GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
+
+      gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_preview),
+          FALSE);
+      gtk_action_set_sensitive (priv->show_preview, FALSE);
+
+      gtk_progress_bar_set_fraction (
+          GTK_PROGRESS_BAR (priv->volume_progress_bar), 0);
+
+      gtk_widget_hide (priv->video_output);
+      gtk_widget_show (priv->remote_user_avatar_widget);
+
       priv->sending_video = FALSE;
-      priv->connected = FALSE;
       priv->call_started = FALSE;
 
       could_disconnect = TRUE;
@@ -1215,11 +1317,8 @@ empathy_call_window_channel_closed_cb (TfChannel *channel, gpointer user_data)
   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
   EmpathyCallWindowPriv *priv = GET_PRIV (self);
 
-  if (empathy_call_window_disconnected (self) && priv->redialing)
-    {
+  if (empathy_call_window_disconnected (self) && priv->call_state == REDIALING)
       empathy_call_window_restart_call (self);
-      priv->redialing = FALSE;
-    }
 }
 
 /* Called with global lock held */
@@ -1302,6 +1401,10 @@ empathy_call_window_connected (gpointer user_data)
   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
   EmpathyCallWindowPriv *priv = GET_PRIV (self);
   EmpathyTpCall *call;
+  gboolean can_send_video;
+
+  can_send_video = priv->video_input != NULL && priv->contact != NULL &&
+    empathy_contact_can_voip_video (priv->contact);
 
   g_object_get (priv->handler, "tp-call", &call, NULL);
 
@@ -1312,10 +1415,12 @@ empathy_call_window_connected (gpointer user_data)
     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
 
   if (priv->video_input == NULL)
-      empathy_call_window_set_send_video (self, FALSE);
+    empathy_call_window_set_send_video (self, FALSE);
 
-  priv->sending_video = empathy_tp_call_is_sending_video (call);
+  priv->sending_video = can_send_video ?
+    empathy_tp_call_is_sending_video (call) : FALSE;
 
+  gtk_action_set_sensitive (priv->show_preview, TRUE);
   gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_preview),
       priv->sending_video
       || gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (priv->show_preview)));
@@ -1324,12 +1429,14 @@ empathy_call_window_connected (gpointer user_data)
   gtk_toggle_tool_button_set_active (
       GTK_TOGGLE_TOOL_BUTTON (priv->camera_button),
       priv->sending_video && priv->video_input != NULL);
-  gtk_widget_set_sensitive (priv->camera_button, priv->video_input != NULL);
-  gtk_action_set_sensitive (priv->send_video, priv->video_input != NULL);
+  gtk_widget_set_sensitive (priv->camera_button, can_send_video);
+  gtk_action_set_sensitive (priv->send_video, can_send_video);
 
   gtk_action_set_sensitive (priv->redial, FALSE);
   gtk_widget_set_sensitive (priv->redial_button, FALSE);
 
+  gtk_widget_set_sensitive (priv->mic_button, TRUE);
+
   empathy_call_window_update_avatars_visibility (call, self);
 
   g_object_unref (call);
@@ -1359,11 +1466,12 @@ empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
 
   g_mutex_lock (priv->lock);
 
-  if (priv->connected == FALSE)
+  if (priv->call_state != CONNECTED)
     {
       g_timer_start (priv->timer);
       priv->timer_id = g_idle_add  (empathy_call_window_connected, self);
-      priv->connected = TRUE;
+      priv->call_state = CONNECTED;
+      empathy_sound_stop (EMPATHY_SOUND_PHONE_OUTGOING);
     }
 
   switch (media_type)
@@ -1603,7 +1711,18 @@ empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
   EmpathyCallWindowPriv *priv = GET_PRIV (window);
 
   if (priv->pipeline != NULL)
-    gst_element_set_state (priv->pipeline, GST_STATE_NULL);
+    {
+      if (priv->bus_message_source_id != 0)
+        {
+          g_source_remove (priv->bus_message_source_id);
+          priv->bus_message_source_id = 0;
+        }
+
+      gst_element_set_state (priv->pipeline, GST_STATE_NULL);
+    }
+
+  if (priv->call_state == CONNECTING)
+    empathy_sound_stop (EMPATHY_SOUND_PHONE_OUTGOING);
 
   return FALSE;
 }
@@ -1772,6 +1891,9 @@ empathy_call_window_camera_toggled_cb (GtkToggleToolButton *toggle,
   EmpathyCallWindowPriv *priv = GET_PRIV (window);
   gboolean active;
 
+  if (priv->call_state != CONNECTED)
+    return;
+
   active = (gtk_toggle_tool_button_get_active (toggle));
 
   if (priv->sending_video == active)
@@ -1788,6 +1910,9 @@ empathy_call_window_send_video_toggled_cb (GtkToggleAction *toggle,
   EmpathyCallWindowPriv *priv = GET_PRIV (window);
   gboolean active;
 
+  if (priv->call_state != CONNECTED)
+    return;
+
   active = (gtk_toggle_action_get_active (toggle));
 
   if (priv->sending_video == active)
@@ -1826,6 +1951,9 @@ empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
   EmpathyCallWindowPriv *priv = GET_PRIV (window);
   gboolean active;
 
+  if (priv->audio_input == NULL)
+    return;
+
   active = (gtk_toggle_tool_button_get_active (toggle));
 
   if (active)
@@ -1886,21 +2014,33 @@ empathy_call_window_restart_call (EmpathyCallWindow *window)
 
   priv->pipeline = gst_pipeline_new (NULL);
   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
-  gst_bus_add_watch (bus, empathy_call_window_bus_message, window);
+  priv->bus_message_source_id = gst_bus_add_watch (bus,
+      empathy_call_window_bus_message, window);
 
   empathy_call_window_setup_remote_frame (bus, window);
   empathy_call_window_setup_self_frame (bus, window);
 
+  g_signal_connect (G_OBJECT (priv->audio_input_adj), "value-changed",
+      G_CALLBACK (empathy_call_window_mic_volume_changed_cb), window);
+
+  /* While the call was disconnected, the input volume might have changed.
+   * However, since the audio_input source was destroyed, its volume has not
+   * been updated during that time. That's why we manually update it here */
+  empathy_call_window_mic_volume_changed_cb (priv->audio_input_adj, window);
+
   g_object_unref (bus);
 
   gtk_widget_show_all (priv->content_hbox);
 
-  gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (priv->show_preview),
-      gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (priv->show_preview)));
+  if (!empathy_call_handler_has_initial_video (priv->handler))
+    gtk_widget_hide (priv->self_user_output_frame);
+
+  priv->outgoing = TRUE;
+  empathy_call_window_set_state_connecting (window);
 
-  empathy_call_window_status_message (window, CONNECTING_STATUS_TEXT);
   priv->call_started = TRUE;
   empathy_call_handler_start_call (priv->handler);
+  empathy_call_window_setup_avatars (window, priv->handler);
   gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
 
   gtk_action_set_sensitive (priv->redial, FALSE);
@@ -1913,12 +2053,12 @@ empathy_call_window_redial_cb (gpointer object,
 {
   EmpathyCallWindowPriv *priv = GET_PRIV (window);
 
-  if (priv->connected)
-    priv->redialing = TRUE;
+  if (priv->call_state == CONNECTED)
+    priv->call_state = REDIALING;
 
   empathy_call_handler_stop_call (priv->handler);
 
-  if (!priv->connected)
+  if (priv->call_state != CONNECTED)
     empathy_call_window_restart_call (window);
 }