Merge remote-tracking branch 'pochu/misc-fixes'
[empathy.git] / src / empathy-call-window.c
1 /*
2  * empathy-call-window.c - Source for EmpathyCallWindow
3  * Copyright (C) 2008-2011 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <math.h>
26
27 #include <gdk/gdkkeysyms.h>
28 #include <gst/gst.h>
29 #include <gtk/gtk.h>
30 #include <glib/gi18n.h>
31
32 #include <clutter/clutter.h>
33 #include <clutter-gtk/clutter-gtk.h>
34 #include <clutter-gst/clutter-gst.h>
35
36 #include <telepathy-glib/util.h>
37 #include <telepathy-farstream/telepathy-farstream.h>
38 #include <telepathy-glib/util.h>
39
40 #include <gst/farsight/fs-element-added-notifier.h>
41 #include <gst/farsight/fs-utils.h>
42
43 #include <libempathy/empathy-camera-monitor.h>
44 #include <libempathy/empathy-gsettings.h>
45 #include <libempathy/empathy-tp-contact-factory.h>
46 #include <libempathy/empathy-request-util.h>
47 #include <libempathy/empathy-utils.h>
48
49 #include <libempathy-gtk/empathy-avatar-image.h>
50 #include <libempathy-gtk/empathy-ui-utils.h>
51 #include <libempathy-gtk/empathy-sound-manager.h>
52 #include <libempathy-gtk/empathy-geometry.h>
53 #include <libempathy-gtk/empathy-images.h>
54
55 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
56 #include <libempathy/empathy-debug.h>
57
58 #include "empathy-call-window.h"
59 #include "empathy-call-window-fullscreen.h"
60 #include "empathy-call-factory.h"
61 #include "empathy-video-widget.h"
62 #include "empathy-about-dialog.h"
63 #include "empathy-audio-src.h"
64 #include "empathy-audio-sink.h"
65 #include "empathy-video-src.h"
66 #include "empathy-mic-menu.h"
67 #include "empathy-preferences.h"
68 #include "empathy-rounded-actor.h"
69 #include "empathy-rounded-rectangle.h"
70 #include "empathy-rounded-texture.h"
71 #include "empathy-camera-menu.h"
72
73 #define CONTENT_HBOX_BORDER_WIDTH 6
74 #define CONTENT_HBOX_SPACING 3
75 #define CONTENT_HBOX_CHILDREN_PACKING_PADDING 3
76
77 #define SELF_VIDEO_SECTION_WIDTH 120
78 #define SELF_VIDEO_SECTION_HEIGHT 90
79 #define SELF_VIDEO_SECTION_MARGIN 10
80
81 #define FLOATING_TOOLBAR_OPACITY 192
82 #define FLOATING_TOOLBAR_WIDTH 280
83 #define FLOATING_TOOLBAR_HEIGHT 36
84 #define FLOATING_TOOLBAR_SPACING 20
85
86 /* The avatar's default width and height are set to the same value because we
87    want a square icon. */
88 #define REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT
89 #define REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT \
90   EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT
91
92 #define SMALL_TOOLBAR_SIZE 36
93
94 /* If an video input error occurs, the error message will start with "v4l" */
95 #define VIDEO_INPUT_ERROR_PREFIX "v4l"
96
97 /* The time interval in milliseconds between 2 outgoing rings */
98 #define MS_BETWEEN_RING 500
99
100 G_DEFINE_TYPE(EmpathyCallWindow, empathy_call_window, GTK_TYPE_WINDOW)
101
102 enum {
103   PROP_CALL_HANDLER = 1,
104 };
105
106 typedef enum {
107   RINGING,       /* Incoming call */
108   CONNECTING,    /* Outgoing call */
109   CONNECTED,     /* Connected */
110   HELD,          /* Connected, but on hold */
111   DISCONNECTED,  /* Disconnected */
112   REDIALING      /* Redialing (special case of CONNECTING) */
113 } CallState;
114
115 typedef enum {
116   CAMERA_STATE_OFF = 0,
117   CAMERA_STATE_ON,
118 } CameraState;
119
120 typedef enum {
121   PREVIEW_POS_NONE,
122   PREVIEW_POS_TOP_LEFT,
123   PREVIEW_POS_TOP_RIGHT,
124   PREVIEW_POS_BOTTOM_LEFT,
125   PREVIEW_POS_BOTTOM_RIGHT,
126 } PreviewPosition;
127
128 struct _EmpathyCallWindowPriv
129 {
130   gboolean dispose_has_run;
131   EmpathyCallHandler *handler;
132
133   EmpathyContact *contact;
134
135   EmpathyCameraMonitor *camera_monitor;
136
137   guint call_state;
138   gboolean outgoing;
139
140   GtkUIManager *ui_manager;
141   GtkWidget *errors_vbox;
142   /* widget displays the video received from the remote user. This widget is
143    * alive only during call. */
144   ClutterActor *video_output;
145   ClutterActor *video_preview;
146   ClutterActor *drag_preview;
147   ClutterActor *preview_shown_button;
148   ClutterActor *preview_hidden_button;
149   ClutterActor *preview_rectangle1;
150   ClutterActor *preview_rectangle2;
151   ClutterActor *preview_rectangle3;
152   ClutterActor *preview_rectangle4;
153   ClutterActor *preview_rectangle_box1;
154   ClutterActor *preview_rectangle_box2;
155   ClutterActor *preview_rectangle_box3;
156   ClutterActor *preview_rectangle_box4;
157   ClutterActor *preview_spinner_actor;
158   GtkWidget *preview_spinner_widget;
159   GtkWidget *video_container;
160   GtkWidget *remote_user_avatar_widget;
161   GtkWidget *remote_user_avatar_toolbar;
162   GtkWidget *remote_user_name_toolbar;
163   GtkWidget *status_label;
164   GtkWidget *hangup_button;
165   GtkWidget *audio_call_button;
166   GtkWidget *video_call_button;
167   GtkWidget *mic_button;
168   GtkWidget *camera_button;
169   GtkWidget *dialpad_button;
170   GtkWidget *toolbar;
171   GtkWidget *bottom_toolbar;
172   ClutterActor *floating_toolbar;
173   GtkWidget *pane;
174   GtkAction *menu_fullscreen;
175   GtkAction *menu_swap_camera;
176
177   ClutterState *transitions;
178
179   /* The box that contains self and remote avatar and video
180      input/output. When we redial, we destroy and re-create the box */
181   ClutterActor *video_box;
182   ClutterLayoutManager *video_layout;
183
184   /* Coordinates of the preview drag event's start. */
185   PreviewPosition preview_pos;
186
187   /* We keep a reference on the hbox which contains the main content so we can
188      easilly repack everything when toggling fullscreen */
189   GtkWidget *content_hbox;
190
191   /* These are used to accept or reject an incoming call when the status
192      is RINGING. */
193   GtkWidget *incoming_call_dialog;
194   TpyCallChannel *pending_channel;
195   TpChannelDispatchOperation *pending_cdo;
196   TpAddDispatchOperationContext *pending_context;
197
198   gulong video_output_motion_handler_id;
199   guint bus_message_source_id;
200
201   gdouble volume;
202
203   /* String that contains the queued tones to send after the current ones
204      are sent */
205   GString *tones;
206   gboolean sending_tones;
207   GtkWidget *dtmf_panel;
208
209   /* Details vbox */
210   GtkWidget *details_vbox;
211   GtkWidget *vcodec_encoding_label;
212   GtkWidget *acodec_encoding_label;
213   GtkWidget *vcodec_decoding_label;
214   GtkWidget *acodec_decoding_label;
215
216   GtkWidget *audio_remote_candidate_label;
217   GtkWidget *audio_local_candidate_label;
218   GtkWidget *video_remote_candidate_label;
219   GtkWidget *video_local_candidate_label;
220   GtkWidget *video_remote_candidate_info_img;
221   GtkWidget *video_local_candidate_info_img;
222   GtkWidget *audio_remote_candidate_info_img;
223   GtkWidget *audio_local_candidate_info_img;
224
225   GstElement *video_input;
226   GstElement *video_preview_sink;
227   GstElement *video_output_sink;
228   GstElement *audio_input;
229   GstElement *audio_output;
230   GstElement *pipeline;
231   GstElement *video_tee;
232
233   GstElement *funnel;
234
235   GList *notifiers;
236
237   GTimer *timer;
238   guint timer_id;
239
240   GMutex *lock;
241   gboolean call_started;
242   gboolean sending_video;
243   CameraState camera_state;
244
245   EmpathyCallWindowFullscreen *fullscreen;
246   gboolean is_fullscreen;
247
248   gboolean got_video;
249   guint got_video_src;
250
251   guint inactivity_src;
252
253   /* Those fields represent the state of the window before it actually was in
254      fullscreen mode. */
255   gboolean dialpad_was_visible_before_fs;
256   gint original_width_before_fs;
257   gint original_height_before_fs;
258
259   gint x, y, w, h, dialpad_width;
260   gboolean maximized;
261
262   /* TRUE if the call should be started when the pipeline is playing */
263   gboolean start_call_when_playing;
264   /* TRUE if we requested to set the pipeline in the playing state */
265   gboolean pipeline_playing;
266
267   EmpathySoundManager *sound_mgr;
268
269   GSettings *settings;
270   EmpathyMicMenu *mic_menu;
271   EmpathyCameraMenu *camera_menu;
272 };
273
274 #define GET_PRIV(o) (EMPATHY_CALL_WINDOW (o)->priv)
275
276 static void empathy_call_window_realized_cb (GtkWidget *widget,
277   EmpathyCallWindow *window);
278
279 static gboolean empathy_call_window_delete_cb (GtkWidget *widget,
280   GdkEvent *event, EmpathyCallWindow *window);
281
282 static gboolean empathy_call_window_state_event_cb (GtkWidget *widget,
283   GdkEventWindowState *event, EmpathyCallWindow *window);
284
285 static void empathy_call_window_set_send_video (EmpathyCallWindow *window,
286   CameraState state);
287
288 static void empathy_call_window_mic_toggled_cb (
289   GtkToggleToolButton *toggle, EmpathyCallWindow *window);
290
291 static void empathy_call_window_hangup_cb (gpointer object,
292   EmpathyCallWindow *window);
293
294 static void empathy_call_window_fullscreen_cb (gpointer object,
295   EmpathyCallWindow *window);
296
297 static void empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window);
298
299 static gboolean empathy_call_window_video_button_press_cb (
300   GtkWidget *video_output, GdkEventButton *event, EmpathyCallWindow *window);
301
302 static gboolean empathy_call_window_key_press_cb (GtkWidget *video_output,
303   GdkEventKey *event, EmpathyCallWindow *window);
304
305 static gboolean empathy_call_window_video_output_motion_notify (
306   GtkWidget *widget, GdkEventMotion *event, EmpathyCallWindow *window);
307
308 static void empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
309   guint button);
310
311 static void empathy_call_window_connect_handler (EmpathyCallWindow *self);
312
313 static void empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
314   EmpathyCallWindow *window);
315
316 static void empathy_call_window_restart_call (EmpathyCallWindow *window);
317
318 static void empathy_call_window_status_message (EmpathyCallWindow *window,
319   gchar *message);
320
321 static gboolean empathy_call_window_bus_message (GstBus *bus,
322   GstMessage *message, gpointer user_data);
323
324 static void
325 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
326   gdouble value, EmpathyCallWindow *window);
327
328 static void
329 empathy_call_window_show_hangup_button (EmpathyCallWindow *self,
330     gboolean show)
331 {
332   gtk_widget_set_visible (self->priv->hangup_button, show);
333   gtk_widget_set_visible (self->priv->audio_call_button, !show);
334   gtk_widget_set_visible (self->priv->video_call_button, !show);
335 }
336
337 static void
338 empathy_call_window_audio_call_cb (GtkToggleToolButton *button,
339     EmpathyCallWindow *self)
340 {
341   g_object_set (self->priv->handler, "initial-video", FALSE, NULL);
342   empathy_call_window_restart_call (self);
343 }
344
345 static void
346 empathy_call_window_video_call_cb (GtkToggleToolButton *button,
347     EmpathyCallWindow *self)
348 {
349   empathy_call_window_set_send_video (self, CAMERA_STATE_ON);
350   g_object_set (self->priv->handler, "initial-video", TRUE, NULL);
351   empathy_call_window_restart_call (self);
352 }
353
354 static void
355 empathy_call_window_emit_tones (EmpathyCallWindow *self)
356 {
357   TpChannel *channel;
358
359   if (tp_str_empty (self->priv->tones->str))
360     return;
361
362   g_object_get (self->priv->handler, "call-channel", &channel, NULL);
363
364   DEBUG ("Emitting multiple tones: %s", self->priv->tones->str);
365
366   tp_cli_channel_interface_dtmf_call_multiple_tones (channel, -1,
367       self->priv->tones->str,
368       NULL, NULL, NULL, NULL);
369
370   self->priv->sending_tones = TRUE;
371
372   g_string_set_size (self->priv->tones, 0);
373
374   g_object_unref (channel);
375 }
376
377 static void
378 empathy_call_window_maybe_emit_tones (EmpathyCallWindow *self)
379 {
380   if (self->priv->sending_tones)
381     return;
382
383   empathy_call_window_emit_tones (self);
384 }
385
386 static void
387 empathy_call_window_tones_stopped_cb (TpChannel *proxy,
388     gboolean arg_cancelled,
389     gpointer user_data,
390     GObject *weak_object)
391 {
392   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
393
394   self->priv->sending_tones = FALSE;
395
396   empathy_call_window_emit_tones (self);
397 }
398
399 static void
400 dtmf_button_pressed_cb (GtkButton *button,
401     EmpathyCallWindow *self)
402 {
403   EmpathyCallWindowPriv *priv = GET_PRIV (self);
404   GQuark button_quark;
405   TpDTMFEvent event;
406
407   button_quark = g_quark_from_static_string (EMPATHY_DTMF_BUTTON_ID);
408   event = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (button),
409     button_quark));
410
411   g_string_append_c (priv->tones, tp_dtmf_event_to_char (event));
412
413   empathy_call_window_maybe_emit_tones (self);
414 }
415
416 /* empathy_create_dtmf_dialpad() requires a callback, even if empty */
417 static void
418 dtmf_button_released_cb (GtkButton *button,
419     EmpathyCallWindow *self)
420 {
421 }
422
423 static void
424 empathy_call_window_mic_volume_changed (EmpathyCallWindow *self)
425 {
426   EmpathyCallWindowPriv *priv = GET_PRIV (self);
427   gdouble volume;
428
429   volume = g_settings_get_double (priv->settings,
430       EMPATHY_PREFS_CALL_SOUND_VOLUME) / 100.0;
431
432   /* Don't store the volume because of muting */
433   if (volume > 0 || gtk_toggle_tool_button_get_active (
434         GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
435     priv->volume = volume;
436
437   /* Ensure that the toggle button is active if the volume is > 0 and inactive
438    * if it's smaller than 0 */
439   if ((volume > 0) != gtk_toggle_tool_button_get_active (
440         GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
441     gtk_toggle_tool_button_set_active (
442       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), volume > 0);
443
444   empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (priv->audio_input),
445     volume);
446 }
447
448 static void
449 empathy_call_window_prefs_volume_changed_cb (GSettings *settings,
450     gchar *key,
451     EmpathyCallWindow *self)
452 {
453   empathy_call_window_mic_volume_changed (self);
454 }
455
456 static void
457 empathy_call_window_raise_actors (EmpathyCallWindow *self)
458 {
459   clutter_actor_raise_top (self->priv->floating_toolbar);
460
461   clutter_actor_raise_top (self->priv->preview_rectangle_box1);
462   clutter_actor_raise_top (self->priv->preview_rectangle_box2);
463   clutter_actor_raise_top (self->priv->preview_rectangle_box3);
464   clutter_actor_raise_top (self->priv->preview_rectangle_box4);
465 }
466
467 static void
468 empathy_call_window_show_video_output (EmpathyCallWindow *self,
469     gboolean show)
470 {
471   if (self->priv->video_output != NULL)
472     g_object_set (self->priv->video_output, "visible", show, NULL);
473
474   gtk_widget_set_visible (self->priv->remote_user_avatar_widget, !show);
475
476   empathy_call_window_raise_actors (self);
477 }
478
479 static void
480 create_video_output_widget (EmpathyCallWindow *self)
481 {
482   EmpathyCallWindowPriv *priv = GET_PRIV (self);
483
484   g_assert (priv->video_output == NULL);
485   g_assert (priv->pipeline != NULL);
486
487   priv->video_output = clutter_texture_new ();
488
489   clutter_texture_set_keep_aspect_ratio (CLUTTER_TEXTURE (priv->video_output),
490       TRUE);
491
492   priv->video_output_sink = clutter_gst_video_sink_new (
493       CLUTTER_TEXTURE (priv->video_output));
494
495   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_box),
496       priv->video_output);
497
498   gtk_widget_add_events (priv->video_container,
499       GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK);
500   g_signal_connect (G_OBJECT (priv->video_container), "button-press-event",
501       G_CALLBACK (empathy_call_window_video_button_press_cb), self);
502 }
503
504 static void
505 create_video_input (EmpathyCallWindow *self)
506 {
507   EmpathyCallWindowPriv *priv = GET_PRIV (self);
508
509   g_assert (priv->video_input == NULL);
510   priv->video_input = empathy_video_src_new ();
511   gst_object_ref (priv->video_input);
512   gst_object_sink (priv->video_input);
513 }
514
515 static void
516 create_audio_input (EmpathyCallWindow *self)
517 {
518   EmpathyCallWindowPriv *priv = GET_PRIV (self);
519
520   g_assert (priv->audio_input == NULL);
521   priv->audio_input = empathy_audio_src_new ();
522   gst_object_ref (priv->audio_input);
523   gst_object_sink (priv->audio_input);
524 }
525
526 static void
527 add_video_preview_to_pipeline (EmpathyCallWindow *self)
528 {
529   EmpathyCallWindowPriv *priv = GET_PRIV (self);
530   GstElement *preview;
531
532   g_assert (priv->video_preview != NULL);
533   g_assert (priv->pipeline != NULL);
534   g_assert (priv->video_input != NULL);
535   g_assert (priv->video_tee != NULL);
536
537   preview = priv->video_preview_sink;
538
539   if (!gst_bin_add (GST_BIN (priv->pipeline), priv->video_input))
540     {
541       g_warning ("Could not add video input to pipeline");
542       return;
543     }
544
545   if (!gst_bin_add (GST_BIN (priv->pipeline), preview))
546     {
547       g_warning ("Could not add video preview to pipeline");
548       return;
549     }
550
551   if (!gst_element_link (priv->video_input, priv->video_tee))
552     {
553       g_warning ("Could not link video input to video tee");
554       return;
555     }
556
557   if (!gst_element_link (priv->video_tee, preview))
558     {
559       g_warning ("Could not link video tee to video preview");
560       return;
561     }
562 }
563
564 static void
565 empathy_call_window_disable_camera_cb (GtkAction *action,
566     EmpathyCallWindow *self)
567 {
568   clutter_actor_destroy (self->priv->preview_hidden_button);
569
570   gtk_toggle_tool_button_set_active (
571       GTK_TOGGLE_TOOL_BUTTON (self->priv->camera_button), FALSE);
572 }
573
574 static void
575 empathy_call_window_minimise_camera_cb (GtkAction *action,
576     EmpathyCallWindow *self)
577 {
578   clutter_actor_hide (self->priv->video_preview);
579   clutter_actor_show (self->priv->preview_hidden_button);
580 }
581
582 static void
583 empathy_call_window_maximise_camera_cb (GtkAction *action,
584     EmpathyCallWindow *self)
585 {
586   clutter_actor_show (self->priv->video_preview);
587   clutter_actor_hide (self->priv->preview_hidden_button);
588 }
589
590 static void
591 empathy_call_window_swap_camera_cb (GtkAction *action,
592     EmpathyCallWindow *self)
593 {
594   const GList *cameras, *l;
595   gchar *current_cam;
596
597   DEBUG ("Swapping the camera");
598
599   cameras = empathy_camera_monitor_get_cameras (self->priv->camera_monitor);
600   current_cam = empathy_video_src_dup_device (
601       EMPATHY_GST_VIDEO_SRC (self->priv->video_input));
602
603   for (l = cameras; l != NULL; l = l->next)
604     {
605       EmpathyCamera *camera = l->data;
606
607       if (!tp_strdiff (camera->device, current_cam))
608         {
609           EmpathyCamera *next;
610
611           if (l->next != NULL)
612             next = l->next->data;
613           else
614             next = cameras->data;
615
616           /* EmpathyCameraMenu will update itself and do the actual change
617            * for us */
618           g_settings_set_string (self->priv->settings,
619               EMPATHY_PREFS_CALL_CAMERA_DEVICE,
620               next->device);
621
622           break;
623         }
624     }
625
626   g_free (current_cam);
627 }
628
629 static void
630 empathy_call_window_camera_added_cb (EmpathyCameraMonitor *monitor,
631     EmpathyCamera *camera,
632     EmpathyCallWindow *self)
633 {
634   const GList *cameras = empathy_camera_monitor_get_cameras (monitor);
635
636   gtk_action_set_visible (self->priv->menu_swap_camera,
637       g_list_length ((GList *) cameras) >= 2);
638 }
639
640 static void
641 empathy_call_window_camera_removed_cb (EmpathyCameraMonitor *monitor,
642     EmpathyCamera *camera,
643     EmpathyCallWindow *self)
644 {
645   const GList *cameras = empathy_camera_monitor_get_cameras (monitor);
646
647   gtk_action_set_visible (self->priv->menu_swap_camera,
648       g_list_length ((GList *) cameras) >= 2);
649 }
650
651 static void
652 empathy_call_window_preview_button_clicked_cb (GtkButton *button,
653     EmpathyCallWindow *self)
654 {
655   GtkWidget *menu;
656
657   menu = gtk_ui_manager_get_widget (self->priv->ui_manager,
658       "/preview-menu");
659   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
660       0, gtk_get_current_event_time ());
661   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
662 }
663
664 static void
665 empathy_call_window_preview_hidden_button_clicked_cb (GtkButton *button,
666     EmpathyCallWindow *self)
667 {
668   GtkWidget *menu;
669
670   menu = gtk_ui_manager_get_widget (self->priv->ui_manager,
671       "/preview-hidden-menu");
672   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
673       0, gtk_get_current_event_time ());
674   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
675 }
676
677 static ClutterActor *
678 empathy_call_window_create_preview_rectangle (EmpathyCallWindow *self,
679     ClutterActor **box,
680     ClutterBinAlignment x,
681     ClutterBinAlignment y)
682 {
683   ClutterLayoutManager *layout1, *layout2;
684   ClutterActor *rectangle;
685   ClutterActor *box1, *box2;
686
687   layout1 = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
688       CLUTTER_BIN_ALIGNMENT_START);
689
690   box1 = clutter_box_new (layout1);
691
692   *box = box1;
693
694   rectangle = empathy_rounded_rectangle_new (
695       SELF_VIDEO_SECTION_WIDTH + 5,
696       SELF_VIDEO_SECTION_HEIGHT + 5);
697
698   clutter_actor_set_size (box1,
699       SELF_VIDEO_SECTION_WIDTH + 2 * SELF_VIDEO_SECTION_MARGIN,
700       SELF_VIDEO_SECTION_HEIGHT + 2 * SELF_VIDEO_SECTION_MARGIN +
701       FLOATING_TOOLBAR_HEIGHT + FLOATING_TOOLBAR_SPACING);
702
703   layout2 = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
704       CLUTTER_BIN_ALIGNMENT_CENTER);
705
706   /* We have a box with the margins and the video in the middle inside
707    * a bigger box with an extra bottom margin so we're not on top of
708    * the floating toolbar. */
709   box2 = clutter_box_new (layout2);
710
711   clutter_actor_set_size (box2,
712       SELF_VIDEO_SECTION_WIDTH + 2 * SELF_VIDEO_SECTION_MARGIN,
713       SELF_VIDEO_SECTION_HEIGHT + 2 * SELF_VIDEO_SECTION_MARGIN);
714
715   clutter_container_add_actor (CLUTTER_CONTAINER (box1), box2);
716   clutter_container_add_actor (CLUTTER_CONTAINER (box2), rectangle);
717
718   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (self->priv->video_layout),
719       box1, x, y);
720
721   clutter_actor_hide (rectangle);
722
723   return rectangle;
724 }
725
726 static void
727 empathy_call_window_create_preview_rectangles (EmpathyCallWindow *self)
728 {
729   self->priv->preview_rectangle1 =
730       empathy_call_window_create_preview_rectangle (self,
731           &self->priv->preview_rectangle_box1,
732           CLUTTER_BIN_ALIGNMENT_START, CLUTTER_BIN_ALIGNMENT_START);
733   self->priv->preview_rectangle2 =
734       empathy_call_window_create_preview_rectangle (self,
735           &self->priv->preview_rectangle_box2,
736           CLUTTER_BIN_ALIGNMENT_START, CLUTTER_BIN_ALIGNMENT_END);
737   self->priv->preview_rectangle3 =
738       empathy_call_window_create_preview_rectangle (self,
739           &self->priv->preview_rectangle_box3,
740           CLUTTER_BIN_ALIGNMENT_END, CLUTTER_BIN_ALIGNMENT_START);
741   self->priv->preview_rectangle4 =
742       empathy_call_window_create_preview_rectangle (self,
743           &self->priv->preview_rectangle_box4,
744           CLUTTER_BIN_ALIGNMENT_END, CLUTTER_BIN_ALIGNMENT_END);
745 }
746
747 static void
748 empathy_call_window_show_preview_rectangles (EmpathyCallWindow *self,
749     gboolean show)
750 {
751   g_object_set (self->priv->preview_rectangle1, "visible", show, NULL);
752   g_object_set (self->priv->preview_rectangle2, "visible", show, NULL);
753   g_object_set (self->priv->preview_rectangle3, "visible", show, NULL);
754   g_object_set (self->priv->preview_rectangle4, "visible", show, NULL);
755 }
756
757 static void
758 empathy_call_window_get_preview_coordinates (EmpathyCallWindow *self,
759     PreviewPosition pos,
760     guint *x,
761     guint *y)
762 {
763   guint ret_x = 0, ret_y = 0;
764   ClutterGeometry box;
765
766   if (!clutter_actor_has_allocation (self->priv->video_box))
767     goto out;
768
769   clutter_actor_get_geometry (self->priv->video_box, &box);
770
771   switch (pos)
772     {
773       case PREVIEW_POS_TOP_LEFT:
774         ret_x = ret_y = SELF_VIDEO_SECTION_MARGIN;
775         break;
776       case PREVIEW_POS_TOP_RIGHT:
777         ret_x = box.width - SELF_VIDEO_SECTION_MARGIN
778             - SELF_VIDEO_SECTION_WIDTH;
779         ret_y = SELF_VIDEO_SECTION_MARGIN;
780         break;
781       case PREVIEW_POS_BOTTOM_LEFT:
782         ret_x = SELF_VIDEO_SECTION_MARGIN;
783         ret_y = box.height - SELF_VIDEO_SECTION_MARGIN
784             - SELF_VIDEO_SECTION_HEIGHT
785             - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING;
786         break;
787       case PREVIEW_POS_BOTTOM_RIGHT:
788         ret_x = box.width - SELF_VIDEO_SECTION_MARGIN
789             - SELF_VIDEO_SECTION_WIDTH;
790         ret_y = box.height - SELF_VIDEO_SECTION_MARGIN
791             - SELF_VIDEO_SECTION_HEIGHT - FLOATING_TOOLBAR_HEIGHT
792             - FLOATING_TOOLBAR_SPACING;
793         break;
794       default:
795         g_warn_if_reached ();
796     }
797
798 out:
799   if (x != NULL)
800     *x = ret_x;
801
802   if (y != NULL)
803     *y = ret_y;
804 }
805
806 static PreviewPosition
807 empathy_call_window_get_preview_position (EmpathyCallWindow *self,
808     gfloat event_x,
809     gfloat event_y)
810 {
811   ClutterGeometry box;
812   PreviewPosition pos = PREVIEW_POS_NONE;
813
814   if (!clutter_actor_has_allocation (self->priv->video_box))
815     return pos;
816
817   clutter_actor_get_geometry (self->priv->video_box, &box);
818
819   if (0 + SELF_VIDEO_SECTION_MARGIN <= event_x &&
820       event_x <= (0 + SELF_VIDEO_SECTION_MARGIN + (gint) SELF_VIDEO_SECTION_WIDTH) &&
821       0 + SELF_VIDEO_SECTION_MARGIN <= event_y &&
822       event_y <= (0 + SELF_VIDEO_SECTION_MARGIN + (gint) SELF_VIDEO_SECTION_HEIGHT))
823     {
824       pos = PREVIEW_POS_TOP_LEFT;
825     }
826   else if (box.width - SELF_VIDEO_SECTION_MARGIN >= event_x &&
827       event_x >= (box.width - SELF_VIDEO_SECTION_MARGIN - (gint) SELF_VIDEO_SECTION_WIDTH) &&
828       0 + SELF_VIDEO_SECTION_MARGIN <= event_y &&
829       event_y <= (0 + SELF_VIDEO_SECTION_MARGIN + (gint) SELF_VIDEO_SECTION_HEIGHT))
830     {
831       pos = PREVIEW_POS_TOP_RIGHT;
832     }
833   else if (0 + SELF_VIDEO_SECTION_MARGIN <= event_x &&
834       event_x <= (0 + SELF_VIDEO_SECTION_MARGIN + (gint) SELF_VIDEO_SECTION_WIDTH) &&
835       box.height - SELF_VIDEO_SECTION_MARGIN - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING >= event_y &&
836       event_y >= (box.height - SELF_VIDEO_SECTION_MARGIN - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING - (gint) SELF_VIDEO_SECTION_HEIGHT))
837     {
838       pos = PREVIEW_POS_BOTTOM_LEFT;
839     }
840   else if (box.width - SELF_VIDEO_SECTION_MARGIN >= event_x &&
841       event_x >= (box.width - SELF_VIDEO_SECTION_MARGIN - (gint) SELF_VIDEO_SECTION_WIDTH) &&
842       box.height - SELF_VIDEO_SECTION_MARGIN - SELF_VIDEO_SECTION_MARGIN - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING >= event_y &&
843       event_y >= (box.height - SELF_VIDEO_SECTION_MARGIN - FLOATING_TOOLBAR_HEIGHT - FLOATING_TOOLBAR_SPACING - (gint) SELF_VIDEO_SECTION_HEIGHT))
844     {
845       pos = PREVIEW_POS_BOTTOM_RIGHT;
846     }
847
848   return pos;
849 }
850
851 static ClutterActor *
852 empathy_call_window_get_preview_rectangle (EmpathyCallWindow *self,
853     PreviewPosition pos)
854 {
855   ClutterActor *rectangle;
856
857   switch (pos)
858     {
859       case PREVIEW_POS_TOP_LEFT:
860         rectangle = self->priv->preview_rectangle1;
861         break;
862       case PREVIEW_POS_TOP_RIGHT:
863         rectangle = self->priv->preview_rectangle3;
864         break;
865       case PREVIEW_POS_BOTTOM_LEFT:
866         rectangle = self->priv->preview_rectangle2;
867         break;
868       case PREVIEW_POS_BOTTOM_RIGHT:
869         rectangle = self->priv->preview_rectangle4;
870         break;
871       default:
872         rectangle = NULL;
873     }
874
875   return rectangle;
876 }
877
878 static void
879 empathy_call_window_move_video_preview (EmpathyCallWindow *self,
880     PreviewPosition pos)
881 {
882   ClutterBinLayout *layout = CLUTTER_BIN_LAYOUT (self->priv->video_layout);
883
884   DEBUG ("moving the video preview to %d", pos);
885
886   self->priv->preview_pos = pos;
887
888   switch (pos)
889     {
890       case PREVIEW_POS_TOP_LEFT:
891         clutter_bin_layout_set_alignment (layout,
892             self->priv->video_preview,
893             CLUTTER_BIN_ALIGNMENT_START,
894             CLUTTER_BIN_ALIGNMENT_START);
895         break;
896       case PREVIEW_POS_TOP_RIGHT:
897         clutter_bin_layout_set_alignment (layout,
898             self->priv->video_preview,
899             CLUTTER_BIN_ALIGNMENT_END,
900             CLUTTER_BIN_ALIGNMENT_START);
901         break;
902       case PREVIEW_POS_BOTTOM_LEFT:
903         clutter_bin_layout_set_alignment (layout,
904             self->priv->video_preview,
905             CLUTTER_BIN_ALIGNMENT_START,
906             CLUTTER_BIN_ALIGNMENT_END);
907         break;
908       case PREVIEW_POS_BOTTOM_RIGHT:
909         clutter_bin_layout_set_alignment (layout,
910             self->priv->video_preview,
911             CLUTTER_BIN_ALIGNMENT_END,
912             CLUTTER_BIN_ALIGNMENT_END);
913         break;
914       default:
915         g_warn_if_reached ();
916     }
917
918   g_settings_set_enum (self->priv->settings, "camera-position", pos);
919 }
920
921 static void
922 empathy_call_window_highlight_preview_rectangle (EmpathyCallWindow *self,
923     PreviewPosition pos)
924 {
925   ClutterActor *rectangle;
926
927   rectangle = empathy_call_window_get_preview_rectangle (self, pos);
928
929   empathy_rounded_rectangle_set_border_width (
930       EMPATHY_ROUNDED_RECTANGLE (rectangle), 5);
931   empathy_rounded_rectangle_set_border_color (
932       EMPATHY_ROUNDED_RECTANGLE (rectangle), CLUTTER_COLOR_Red);
933 }
934
935 static void
936 empathy_call_window_darken_preview_rectangle (EmpathyCallWindow *self,
937     ClutterActor *rectangle)
938 {
939   empathy_rounded_rectangle_set_border_width (
940       EMPATHY_ROUNDED_RECTANGLE (rectangle), 1);
941   empathy_rounded_rectangle_set_border_color (
942       EMPATHY_ROUNDED_RECTANGLE (rectangle), CLUTTER_COLOR_Black);
943 }
944
945 static void
946 empathy_call_window_darken_preview_rectangles (EmpathyCallWindow *self)
947 {
948   ClutterActor *rectangle;
949
950   rectangle = empathy_call_window_get_preview_rectangle (self,
951       self->priv->preview_pos);
952
953   /* We don't want to darken the rectangle where the preview
954    * currently is. */
955
956   if (self->priv->preview_rectangle1 != rectangle)
957     empathy_call_window_darken_preview_rectangle (self,
958         self->priv->preview_rectangle1);
959
960   if (self->priv->preview_rectangle2 != rectangle)
961     empathy_call_window_darken_preview_rectangle (self,
962         self->priv->preview_rectangle2);
963
964   if (self->priv->preview_rectangle3 != rectangle)
965     empathy_call_window_darken_preview_rectangle (self,
966         self->priv->preview_rectangle3);
967
968   if (self->priv->preview_rectangle4 != rectangle)
969     empathy_call_window_darken_preview_rectangle (self,
970         self->priv->preview_rectangle4);
971 }
972
973 static void
974 empathy_call_window_preview_on_drag_begin_cb (ClutterDragAction *action,
975     ClutterActor *actor,
976     gfloat event_x,
977     gfloat event_y,
978     ClutterModifierType modifiers,
979     EmpathyCallWindow *self)
980 {
981   ClutterActor *stage = clutter_actor_get_stage (actor);
982   gfloat rel_x, rel_y;
983
984   self->priv->drag_preview = clutter_clone_new (actor);
985
986   clutter_container_add_actor (CLUTTER_CONTAINER (stage),
987       self->priv->drag_preview);
988
989   clutter_actor_transform_stage_point (actor, event_x, event_y,
990       &rel_x, &rel_y);
991
992   clutter_actor_set_position (self->priv->drag_preview,
993       event_x - rel_x, event_y - rel_y);
994
995   clutter_drag_action_set_drag_handle (action,
996       self->priv->drag_preview);
997
998   clutter_actor_set_opacity (actor, 0);
999   clutter_actor_hide (self->priv->preview_shown_button);
1000
1001   empathy_call_window_show_preview_rectangles (self, TRUE);
1002   empathy_call_window_darken_preview_rectangles (self);
1003 }
1004
1005 static void
1006 empathy_call_window_on_animation_completed_cb (ClutterAnimation *animation,
1007     ClutterActor *actor)
1008 {
1009   clutter_actor_set_opacity (actor, 255);
1010 }
1011
1012 static void
1013 empathy_call_window_preview_on_drag_end_cb (ClutterDragAction *action,
1014     ClutterActor *actor,
1015     gfloat event_x,
1016     gfloat event_y,
1017     ClutterModifierType modifiers,
1018     EmpathyCallWindow *self)
1019 {
1020   PreviewPosition pos;
1021   guint x, y;
1022
1023   /* Get the position before destroying the drag actor, otherwise the
1024    * preview_box allocation won't be valid and we won't be able to
1025    * calculate the position. */
1026   pos = empathy_call_window_get_preview_position (self, event_x, event_y);
1027
1028   empathy_call_window_get_preview_coordinates (self,
1029       pos != PREVIEW_POS_NONE ? pos : self->priv->preview_pos,
1030       &x, &y);
1031
1032   /* Move the preview to the destination and destroy it afterwards */
1033   clutter_actor_animate (self->priv->drag_preview, CLUTTER_LINEAR, 500,
1034       "x", (gfloat) x,
1035       "y", (gfloat) y,
1036       "signal-swapped-after::completed",
1037         clutter_actor_destroy, self->priv->drag_preview,
1038       "signal-swapped-after::completed",
1039         clutter_actor_show, self->priv->preview_shown_button,
1040       "signal::completed",
1041         empathy_call_window_on_animation_completed_cb, actor,
1042       NULL);
1043
1044   self->priv->drag_preview = NULL;
1045
1046   if (pos != PREVIEW_POS_NONE)
1047     empathy_call_window_move_video_preview (self, pos);
1048
1049   empathy_call_window_show_preview_rectangles (self, FALSE);
1050 }
1051
1052 static void
1053 empathy_call_window_preview_on_drag_motion_cb (ClutterDragAction *action,
1054     ClutterActor *actor,
1055     gfloat delta_x,
1056     gfloat delta_y,
1057     EmpathyCallWindow *self)
1058 {
1059   PreviewPosition pos;
1060   gfloat event_x, event_y;
1061
1062   clutter_drag_action_get_motion_coords (action, &event_x, &event_y);
1063
1064   pos = empathy_call_window_get_preview_position (self, event_x, event_y);
1065
1066   if (pos != PREVIEW_POS_NONE)
1067     empathy_call_window_highlight_preview_rectangle (self, pos);
1068   else
1069     empathy_call_window_darken_preview_rectangles (self);
1070 }
1071
1072 static gboolean
1073 empathy_call_window_preview_enter_event_cb (ClutterActor *actor,
1074     ClutterCrossingEvent *event,
1075     EmpathyCallWindow *self)
1076 {
1077   ClutterActor *rectangle;
1078
1079   rectangle = empathy_call_window_get_preview_rectangle (self,
1080       self->priv->preview_pos);
1081
1082   empathy_call_window_highlight_preview_rectangle (self,
1083       self->priv->preview_pos);
1084
1085   clutter_actor_show (rectangle);
1086
1087   return FALSE;
1088 }
1089
1090 static gboolean
1091 empathy_call_window_preview_leave_event_cb (ClutterActor *actor,
1092     ClutterCrossingEvent *event,
1093     EmpathyCallWindow *self)
1094 {
1095   ClutterActor *rectangle;
1096
1097   rectangle = empathy_call_window_get_preview_rectangle (self,
1098       self->priv->preview_pos);
1099
1100   empathy_call_window_darken_preview_rectangle (self, rectangle);
1101
1102   clutter_actor_hide (rectangle);
1103
1104   return FALSE;
1105 }
1106
1107 static void
1108 create_video_preview (EmpathyCallWindow *self)
1109 {
1110   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1111   ClutterLayoutManager *layout, *layout_center, *layout_end;
1112   ClutterActor *preview;
1113   ClutterActor *box;
1114   ClutterActor *b;
1115   ClutterAction *action;
1116   GtkWidget *button;
1117   PreviewPosition pos;
1118   GdkRGBA transparent = { 0., 0., 0., 0. };
1119
1120   g_assert (priv->video_preview == NULL);
1121
1122   pos = g_settings_get_enum (priv->settings, "camera-position");
1123
1124   preview = empathy_rounded_texture_new ();
1125   clutter_actor_set_size (preview,
1126       SELF_VIDEO_SECTION_WIDTH, SELF_VIDEO_SECTION_HEIGHT);
1127   priv->video_preview_sink = clutter_gst_video_sink_new (
1128       CLUTTER_TEXTURE (preview));
1129
1130   /* Add a little offset to the video preview */
1131   layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
1132       CLUTTER_BIN_ALIGNMENT_START);
1133   priv->video_preview = clutter_box_new (layout);
1134   clutter_actor_set_size (priv->video_preview,
1135       SELF_VIDEO_SECTION_WIDTH + 2 * SELF_VIDEO_SECTION_MARGIN,
1136       SELF_VIDEO_SECTION_HEIGHT + 2 * SELF_VIDEO_SECTION_MARGIN +
1137       FLOATING_TOOLBAR_HEIGHT + FLOATING_TOOLBAR_SPACING);
1138
1139   /* Spinner for when changing the camera device */
1140   priv->preview_spinner_widget = gtk_spinner_new ();
1141   priv->preview_spinner_actor = empathy_rounded_actor_new ();
1142   empathy_rounded_actor_set_round_factor (
1143       EMPATHY_ROUNDED_ACTOR (priv->preview_spinner_actor), 16);
1144
1145   g_object_set (priv->preview_spinner_widget, "expand", TRUE, NULL);
1146   gtk_widget_override_background_color (
1147       gtk_clutter_actor_get_widget (
1148           GTK_CLUTTER_ACTOR (priv->preview_spinner_actor)),
1149       GTK_STATE_FLAG_NORMAL, &transparent);
1150   gtk_widget_show (priv->preview_spinner_widget);
1151
1152   gtk_container_add (
1153       GTK_CONTAINER (gtk_clutter_actor_get_widget (
1154           GTK_CLUTTER_ACTOR (priv->preview_spinner_actor))),
1155       priv->preview_spinner_widget);
1156   clutter_actor_set_size (priv->preview_spinner_actor,
1157       SELF_VIDEO_SECTION_WIDTH, SELF_VIDEO_SECTION_HEIGHT);
1158   clutter_actor_set_opacity (priv->preview_spinner_actor, 128);
1159   clutter_actor_hide (priv->preview_spinner_actor);
1160
1161   /* We have a box with the margins and the video in the middle inside
1162    * a bigger box with an extra bottom margin so we're not on top of
1163    * the floating toolbar. */
1164   layout_center = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
1165       CLUTTER_BIN_ALIGNMENT_CENTER);
1166   box = clutter_box_new (layout_center);
1167   clutter_actor_set_size (box,
1168       SELF_VIDEO_SECTION_WIDTH + 2 * SELF_VIDEO_SECTION_MARGIN,
1169       SELF_VIDEO_SECTION_HEIGHT + 2 * SELF_VIDEO_SECTION_MARGIN);
1170
1171   clutter_container_add_actor (CLUTTER_CONTAINER (box), preview);
1172   clutter_container_add_actor (CLUTTER_CONTAINER (box),
1173       priv->preview_spinner_actor);
1174   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_preview), box);
1175
1176   g_object_set (priv->video_preview_sink,
1177       "sync", FALSE,
1178       "async", TRUE,
1179       NULL);
1180
1181   /* Translators: this is an "Info" label. It should be as short
1182    * as possible. */
1183   button = gtk_button_new_with_label (_("i"));
1184   priv->preview_shown_button = b = empathy_rounded_actor_new ();
1185   gtk_container_add (
1186       GTK_CONTAINER (gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (b))),
1187       button);
1188   clutter_actor_set_size (b, 24, 24);
1189
1190   layout_end = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_END,
1191       CLUTTER_BIN_ALIGNMENT_END);
1192   box = clutter_box_new (layout_end);
1193   clutter_actor_set_size (box,
1194       SELF_VIDEO_SECTION_WIDTH,
1195       SELF_VIDEO_SECTION_HEIGHT + SELF_VIDEO_SECTION_MARGIN);
1196
1197   clutter_container_add_actor (CLUTTER_CONTAINER (box), b);
1198   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_preview), box);
1199
1200   g_signal_connect (button, "clicked",
1201       G_CALLBACK (empathy_call_window_preview_button_clicked_cb),
1202       self);
1203
1204   /* Translators: this is an "Info" label. It should be as short
1205    * as possible. */
1206   button = gtk_button_new_with_label (_("i"));
1207   b = empathy_rounded_actor_new ();
1208   gtk_container_add (
1209       GTK_CONTAINER (gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (b))),
1210       button);
1211   clutter_actor_set_size (b, 24, 24);
1212   priv->preview_hidden_button = b;
1213
1214   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1215       priv->preview_hidden_button,
1216       CLUTTER_BIN_ALIGNMENT_START,
1217       CLUTTER_BIN_ALIGNMENT_END);
1218
1219   self->priv->preview_pos = PREVIEW_POS_BOTTOM_LEFT;
1220
1221   clutter_actor_hide (priv->preview_hidden_button);
1222
1223   g_signal_connect (button, "clicked",
1224       G_CALLBACK (empathy_call_window_preview_hidden_button_clicked_cb),
1225       self);
1226
1227   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1228       priv->video_preview,
1229       CLUTTER_BIN_ALIGNMENT_START,
1230       CLUTTER_BIN_ALIGNMENT_END);
1231
1232   empathy_call_window_move_video_preview (self, pos);
1233
1234   action = clutter_drag_action_new ();
1235   g_signal_connect (action, "drag-begin",
1236       G_CALLBACK (empathy_call_window_preview_on_drag_begin_cb), self);
1237   g_signal_connect (action, "drag-end",
1238       G_CALLBACK (empathy_call_window_preview_on_drag_end_cb), self);
1239   g_signal_connect (action, "drag-motion",
1240       G_CALLBACK (empathy_call_window_preview_on_drag_motion_cb), self);
1241
1242   g_signal_connect (preview, "enter-event",
1243       G_CALLBACK (empathy_call_window_preview_enter_event_cb), self);
1244   g_signal_connect (preview, "leave-event",
1245       G_CALLBACK (empathy_call_window_preview_leave_event_cb), self);
1246
1247   clutter_actor_add_action (preview, action);
1248   clutter_actor_set_reactive (preview, TRUE);
1249   clutter_actor_set_reactive (priv->preview_shown_button, TRUE);
1250 }
1251
1252 static void
1253 empathy_call_window_start_camera_spinning (EmpathyCallWindow *self)
1254 {
1255   clutter_actor_show (self->priv->preview_spinner_actor);
1256   gtk_spinner_start (GTK_SPINNER (self->priv->preview_spinner_widget));
1257 }
1258
1259 static void
1260 empathy_call_window_stop_camera_spinning (EmpathyCallWindow *self)
1261 {
1262   clutter_actor_hide (self->priv->preview_spinner_actor);
1263   gtk_spinner_stop (GTK_SPINNER (self->priv->preview_spinner_widget));
1264 }
1265
1266 void
1267 empathy_call_window_play_camera (EmpathyCallWindow *self,
1268     gboolean play)
1269 {
1270   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1271   GstElement *preview;
1272   GstState state;
1273
1274   if (priv->video_preview == NULL)
1275     {
1276       create_video_preview (self);
1277       add_video_preview_to_pipeline (self);
1278     }
1279
1280   if (play)
1281     {
1282       state = GST_STATE_PLAYING;
1283     }
1284   else
1285     {
1286       empathy_call_window_start_camera_spinning (self);
1287       state = GST_STATE_NULL;
1288     }
1289
1290   preview = priv->video_preview_sink;
1291
1292   gst_element_set_state (preview, state);
1293   gst_element_set_state (priv->video_input, state);
1294   gst_element_set_state (priv->video_tee, state);
1295 }
1296
1297 static void
1298 display_video_preview (EmpathyCallWindow *self,
1299     gboolean display)
1300 {
1301   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1302
1303   if (display)
1304     {
1305       /* Display the video preview */
1306       DEBUG ("Show video preview");
1307
1308       empathy_call_window_play_camera (self, TRUE);
1309       clutter_actor_show (priv->video_preview);
1310       clutter_actor_raise_top (priv->floating_toolbar);
1311     }
1312   else
1313     {
1314       /* Hide the video preview */
1315       DEBUG ("Hide video preview");
1316
1317       if (priv->video_preview != NULL)
1318         {
1319           clutter_actor_hide (priv->video_preview);
1320           empathy_call_window_play_camera (self, FALSE);
1321         }
1322     }
1323 }
1324
1325 static void
1326 empathy_call_window_set_state_connecting (EmpathyCallWindow *window)
1327 {
1328   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1329
1330   empathy_call_window_status_message (window, _("Connecting…"));
1331   priv->call_state = CONNECTING;
1332
1333   /* Show the toolbar */
1334   clutter_state_set_state (priv->transitions, "fade-in");
1335
1336   if (priv->outgoing)
1337     empathy_sound_manager_start_playing (priv->sound_mgr, GTK_WIDGET (window),
1338         EMPATHY_SOUND_PHONE_OUTGOING, MS_BETWEEN_RING);
1339 }
1340
1341 static void
1342 disable_camera (EmpathyCallWindow *self)
1343 {
1344   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1345
1346   if (priv->camera_state == CAMERA_STATE_OFF)
1347     return;
1348
1349   DEBUG ("Disable camera");
1350
1351   display_video_preview (self, FALSE);
1352
1353   if (priv->camera_state == CAMERA_STATE_ON)
1354     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
1355
1356   priv->camera_state = CAMERA_STATE_OFF;
1357 }
1358
1359 static void
1360 enable_camera (EmpathyCallWindow *self)
1361 {
1362   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1363
1364   if (priv->camera_state == CAMERA_STATE_ON)
1365     return;
1366
1367   if (priv->video_input == NULL)
1368     {
1369       DEBUG ("Can't enable camera, no input");
1370       return;
1371     }
1372
1373   DEBUG ("Enable camera");
1374
1375   empathy_call_window_set_send_video (self, CAMERA_STATE_ON);
1376
1377   priv->camera_state = CAMERA_STATE_ON;
1378 }
1379
1380 static void
1381 empathy_call_window_camera_toggled_cb (GtkToggleToolButton *toggle,
1382   EmpathyCallWindow *self)
1383 {
1384   if (gtk_toggle_tool_button_get_active (toggle))
1385     enable_camera (self);
1386   else
1387     disable_camera (self);
1388 }
1389
1390 static void
1391 create_pipeline (EmpathyCallWindow *self)
1392 {
1393   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1394   GstBus *bus;
1395
1396   g_assert (priv->pipeline == NULL);
1397
1398   priv->pipeline = gst_pipeline_new (NULL);
1399   priv->pipeline_playing = FALSE;
1400
1401   priv->video_tee = gst_element_factory_make ("tee", NULL);
1402   gst_object_ref (priv->video_tee);
1403   gst_object_sink (priv->video_tee);
1404
1405   gst_bin_add (GST_BIN (priv->pipeline), priv->video_tee);
1406
1407   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
1408   priv->bus_message_source_id = gst_bus_add_watch (bus,
1409       empathy_call_window_bus_message, self);
1410
1411   g_object_unref (bus);
1412 }
1413
1414 static void
1415 empathy_call_window_settings_cb (GtkAction *action,
1416     EmpathyCallWindow *self)
1417 {
1418   gchar *args = g_strdup_printf ("-p %s",
1419       empathy_preferences_tab_to_string (EMPATHY_PREFERENCES_TAB_CALLS));
1420
1421   empathy_launch_program (BIN_DIR, "empathy", args);
1422
1423   g_free (args);
1424 }
1425
1426 static void
1427 empathy_call_window_contents_cb (GtkAction *action,
1428     EmpathyCallWindow *self)
1429 {
1430   empathy_url_show (GTK_WIDGET (self), "ghelp:empathy?audio-video");
1431 }
1432
1433 static void
1434 empathy_call_window_debug_cb (GtkAction *action,
1435     EmpathyCallWindow *self)
1436 {
1437   empathy_launch_program (BIN_DIR, "empathy-debugger", "-s Empathy.Call");
1438 }
1439
1440 static void
1441 empathy_call_window_about_cb (GtkAction *action,
1442     EmpathyCallWindow *self)
1443 {
1444   empathy_about_dialog_new (GTK_WINDOW (self));
1445 }
1446
1447 static gboolean
1448 empathy_call_window_toolbar_timeout (gpointer data)
1449 {
1450   EmpathyCallWindow *self = data;
1451
1452   /* We don't want to hide the toolbar if we're not in a call, as
1453    * to show the call status all the time. */
1454   if (self->priv->call_state != CONNECTING &&
1455       self->priv->call_state != DISCONNECTED)
1456     clutter_state_set_state (self->priv->transitions, "fade-out");
1457
1458   return TRUE;
1459 }
1460
1461 static gboolean
1462 empathy_call_window_motion_notify_cb (GtkWidget *widget,
1463     GdkEvent *event,
1464     EmpathyCallWindow *self)
1465 {
1466   clutter_state_set_state (self->priv->transitions, "fade-in");
1467
1468   if (self->priv->inactivity_src > 0)
1469     g_source_remove (self->priv->inactivity_src);
1470
1471   self->priv->inactivity_src = g_timeout_add_seconds (3,
1472       empathy_call_window_toolbar_timeout, self);
1473
1474   return FALSE;
1475 }
1476
1477 static gboolean
1478 empathy_call_window_configure_event_cb (GtkWidget *widget,
1479     GdkEvent  *event,
1480     EmpathyCallWindow *self)
1481 {
1482   GdkWindow *gdk_window;
1483   GdkWindowState window_state;
1484
1485   gtk_window_get_position (GTK_WINDOW (self), &self->priv->x, &self->priv->y);
1486   gtk_window_get_size (GTK_WINDOW (self), &self->priv->w, &self->priv->h);
1487
1488   gtk_widget_get_preferred_width (self->priv->dtmf_panel,
1489       &self->priv->dialpad_width, NULL);
1490
1491   gdk_window = gtk_widget_get_window (widget);
1492   window_state = gdk_window_get_state (gdk_window);
1493   self->priv->maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED);
1494
1495   return FALSE;
1496 }
1497
1498 static void
1499 empathy_call_window_destroyed_cb (GtkWidget *object,
1500     EmpathyCallWindow *self)
1501 {
1502   if (gtk_widget_get_visible (self->priv->dtmf_panel))
1503     {
1504       /* Save the geometry as if the dialpad was hidden. */
1505       empathy_geometry_save_values (GTK_WINDOW (self),
1506           self->priv->x, self->priv->y,
1507           self->priv->w - self->priv->dialpad_width, self->priv->h,
1508           self->priv->maximized);
1509     }
1510 }
1511
1512 static void
1513 empathy_call_window_stage_allocation_changed_cb (ClutterActor *stage,
1514     GParamSpec *pspec,
1515     ClutterBindConstraint *constraint)
1516 {
1517   ClutterActorBox allocation;
1518
1519   clutter_actor_get_allocation_box (stage, &allocation);
1520
1521   clutter_bind_constraint_set_offset (constraint,
1522       allocation.y2 - allocation.y1 -
1523       FLOATING_TOOLBAR_SPACING - FLOATING_TOOLBAR_HEIGHT);
1524 }
1525
1526 static void
1527 empathy_call_window_incoming_call_response_cb (GtkDialog *dialog,
1528     gint response_id,
1529     EmpathyCallWindow *self)
1530 {
1531   switch (response_id)
1532     {
1533       case GTK_RESPONSE_ACCEPT:
1534         tp_channel_dispatch_operation_handle_with_async (
1535             self->priv->pending_cdo, EMPATHY_CALL_BUS_NAME, NULL, NULL);
1536
1537         tp_clear_object (&self->priv->pending_cdo);
1538         tp_clear_object (&self->priv->pending_channel);
1539         tp_clear_object (&self->priv->pending_context);
1540
1541         break;
1542       case GTK_RESPONSE_CANCEL:
1543         tp_channel_dispatch_operation_close_channels_async (
1544             self->priv->pending_cdo, NULL, NULL);
1545
1546         empathy_call_window_status_message (self, _("Disconnected"));
1547         self->priv->call_state = DISCONNECTED;
1548         break;
1549       default:
1550         g_warn_if_reached ();
1551     }
1552 }
1553
1554 static void
1555 empathy_call_window_set_state_ringing (EmpathyCallWindow *self)
1556 {
1557   gboolean video;
1558
1559   g_assert (self->priv->call_state != CONNECTED);
1560
1561   video = tpy_call_channel_has_initial_video (self->priv->pending_channel);
1562
1563   empathy_call_window_status_message (self, _("Incoming call"));
1564   self->priv->call_state = RINGING;
1565
1566   self->priv->incoming_call_dialog = gtk_message_dialog_new (
1567       GTK_WINDOW (self), GTK_DIALOG_MODAL,
1568       GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
1569       video ? _("Incoming video call from %s") : _("Incoming call from %s"),
1570       empathy_contact_get_alias (self->priv->contact));
1571
1572   gtk_dialog_add_buttons (GTK_DIALOG (self->priv->incoming_call_dialog),
1573       _("Reject"), GTK_RESPONSE_CANCEL,
1574       _("Answer"), GTK_RESPONSE_ACCEPT,
1575       NULL);
1576
1577   g_signal_connect (self->priv->incoming_call_dialog, "response",
1578       G_CALLBACK (empathy_call_window_incoming_call_response_cb), self);
1579   gtk_widget_show (self->priv->incoming_call_dialog);
1580 }
1581
1582 static void
1583 empathy_call_window_cdo_invalidated_cb (TpProxy *channel,
1584     guint domain,
1585     gint code,
1586     gchar *message,
1587     EmpathyCallWindow *self)
1588 {
1589   tp_clear_object (&self->priv->pending_cdo);
1590   tp_clear_object (&self->priv->pending_channel);
1591   tp_clear_object (&self->priv->pending_context);
1592
1593   /* We don't know if the incoming call has been accepted or not, so we
1594    * assume it hasn't and if it has, we'll set the proper status when
1595    * we get the new handler. */
1596   empathy_call_window_status_message (self, _("Disconnected"));
1597   self->priv->call_state = DISCONNECTED;
1598
1599   gtk_widget_destroy (self->priv->incoming_call_dialog);
1600   self->priv->incoming_call_dialog = NULL;
1601 }
1602
1603 void
1604 empathy_call_window_start_ringing (EmpathyCallWindow *self,
1605     TpyCallChannel *channel,
1606     TpChannelDispatchOperation *dispatch_operation,
1607     TpAddDispatchOperationContext *context)
1608 {
1609   g_assert (self->priv->pending_channel == NULL);
1610   g_assert (self->priv->pending_context == NULL);
1611   g_assert (self->priv->pending_cdo == NULL);
1612
1613   /* Start ringing and delay until the user answers or hangs. */
1614   self->priv->pending_channel = g_object_ref (channel);
1615   self->priv->pending_context = g_object_ref (context);
1616   self->priv->pending_cdo = g_object_ref (dispatch_operation);
1617
1618   g_signal_connect (self->priv->pending_cdo, "invalidated",
1619       G_CALLBACK (empathy_call_window_cdo_invalidated_cb), self);
1620
1621   empathy_call_window_set_state_ringing (self);
1622   tp_add_dispatch_operation_context_accept (context);
1623 }
1624
1625 static void
1626 empathy_call_window_init (EmpathyCallWindow *self)
1627 {
1628   EmpathyCallWindowPriv *priv;
1629   GtkBuilder *gui;
1630   GtkWidget *top_vbox;
1631   gchar *filename;
1632   ClutterConstraint *constraint;
1633   ClutterActor *remote_avatar;
1634   GtkStyleContext *context;
1635   GdkRGBA rgba;
1636   ClutterColor bg;
1637
1638   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1639     EMPATHY_TYPE_CALL_WINDOW, EmpathyCallWindowPriv);
1640
1641   priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
1642
1643   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
1644   gui = empathy_builder_get_file (filename,
1645     "call_window_vbox", &top_vbox,
1646     "errors_vbox", &priv->errors_vbox,
1647     "pane", &priv->pane,
1648     "remote_user_name_toolbar", &priv->remote_user_name_toolbar,
1649     "remote_user_avatar_toolbar", &priv->remote_user_avatar_toolbar,
1650     "status_label", &priv->status_label,
1651     "audiocall", &priv->audio_call_button,
1652     "videocall", &priv->video_call_button,
1653     "microphone", &priv->mic_button,
1654     "camera", &priv->camera_button,
1655     "hangup", &priv->hangup_button,
1656     "dialpad", &priv->dialpad_button,
1657     "toolbar", &priv->toolbar,
1658     "bottom_toolbar", &priv->bottom_toolbar,
1659     "ui_manager", &priv->ui_manager,
1660     "menufullscreen", &priv->menu_fullscreen,
1661     "menupreviewswap", &priv->menu_swap_camera,
1662     "details_vbox",  &priv->details_vbox,
1663     "vcodec_encoding_label", &priv->vcodec_encoding_label,
1664     "acodec_encoding_label", &priv->acodec_encoding_label,
1665     "acodec_decoding_label", &priv->acodec_decoding_label,
1666     "vcodec_decoding_label", &priv->vcodec_decoding_label,
1667     "audio_remote_candidate_label", &priv->audio_remote_candidate_label,
1668     "audio_local_candidate_label", &priv->audio_local_candidate_label,
1669     "video_remote_candidate_label", &priv->video_remote_candidate_label,
1670     "video_local_candidate_label", &priv->video_local_candidate_label,
1671     "video_remote_candidate_info_img", &priv->video_remote_candidate_info_img,
1672     "video_local_candidate_info_img", &priv->video_local_candidate_info_img,
1673     "audio_remote_candidate_info_img", &priv->audio_remote_candidate_info_img,
1674     "audio_local_candidate_info_img", &priv->audio_local_candidate_info_img,
1675     NULL);
1676   g_free (filename);
1677
1678   empathy_builder_connect (gui, self,
1679     "hangup", "clicked", empathy_call_window_hangup_cb,
1680     "audiocall", "clicked", empathy_call_window_audio_call_cb,
1681     "videocall", "clicked", empathy_call_window_video_call_cb,
1682     "volume", "value-changed", empathy_call_window_volume_changed_cb,
1683     "microphone", "toggled", empathy_call_window_mic_toggled_cb,
1684     "camera", "toggled", empathy_call_window_camera_toggled_cb,
1685     "dialpad", "toggled", empathy_call_window_dialpad_cb,
1686     "menufullscreen", "activate", empathy_call_window_fullscreen_cb,
1687     "menusettings", "activate", empathy_call_window_settings_cb,
1688     "menucontents", "activate", empathy_call_window_contents_cb,
1689     "menudebug", "activate", empathy_call_window_debug_cb,
1690     "menuabout", "activate", empathy_call_window_about_cb,
1691     "menupreviewdisable", "activate", empathy_call_window_disable_camera_cb,
1692     "menupreviewminimise", "activate", empathy_call_window_minimise_camera_cb,
1693     "menupreviewmaximise", "activate", empathy_call_window_maximise_camera_cb,
1694     "menupreviewswap", "activate", empathy_call_window_swap_camera_cb,
1695     NULL);
1696
1697   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
1698
1699   priv->camera_monitor = empathy_camera_monitor_dup_singleton ();
1700
1701   g_object_bind_property (priv->camera_monitor, "available",
1702       priv->camera_button, "sensitive",
1703       G_BINDING_SYNC_CREATE);
1704
1705   g_signal_connect (priv->camera_monitor, "added",
1706       G_CALLBACK (empathy_call_window_camera_added_cb), self);
1707   g_signal_connect (priv->camera_monitor, "removed",
1708       G_CALLBACK (empathy_call_window_camera_removed_cb), self);
1709
1710   priv->lock = g_mutex_new ();
1711
1712   gtk_container_add (GTK_CONTAINER (self), top_vbox);
1713
1714   priv->content_hbox = gtk_hbox_new (FALSE, CONTENT_HBOX_SPACING);
1715   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
1716                                   CONTENT_HBOX_BORDER_WIDTH);
1717   gtk_box_pack_start (GTK_BOX (priv->pane), priv->content_hbox,
1718       TRUE, TRUE, 0);
1719
1720   /* avatar/video box */
1721   priv->video_layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
1722       CLUTTER_BIN_ALIGNMENT_CENTER);
1723
1724   priv->video_box = clutter_box_new (priv->video_layout);
1725
1726   priv->video_container = gtk_clutter_embed_new ();
1727
1728   /* Set the background color to that of the rest of the window */
1729   context = gtk_widget_get_style_context (priv->content_hbox);
1730   gtk_style_context_get_background_color (context,
1731       GTK_STATE_FLAG_NORMAL, &rgba);
1732   bg.red = CLAMP (rgba.red * 255.0, 0, 255);
1733   bg.green = CLAMP (rgba.green * 255.0, 0, 255);
1734   bg.blue = CLAMP (rgba.blue * 255.0, 0, 255);
1735   bg.alpha = CLAMP (rgba.alpha * 255.0, 0, 255);
1736   clutter_stage_set_color (
1737       CLUTTER_STAGE (gtk_clutter_embed_get_stage (
1738           GTK_CLUTTER_EMBED (priv->video_container))),
1739       &bg);
1740
1741   clutter_container_add (
1742       CLUTTER_CONTAINER (gtk_clutter_embed_get_stage (
1743           GTK_CLUTTER_EMBED (priv->video_container))),
1744       priv->video_box,
1745       NULL);
1746
1747   constraint = clutter_bind_constraint_new (
1748       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1749       CLUTTER_BIND_SIZE, 0);
1750   clutter_actor_add_constraint (priv->video_box, constraint);
1751
1752   priv->remote_user_avatar_widget = gtk_image_new ();
1753   remote_avatar = gtk_clutter_actor_new_with_contents (
1754       priv->remote_user_avatar_widget);
1755
1756   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_box),
1757       remote_avatar);
1758
1759   empathy_call_window_create_preview_rectangles (self);
1760
1761   gtk_box_pack_start (GTK_BOX (priv->content_hbox),
1762       priv->video_container, TRUE, TRUE,
1763       CONTENT_HBOX_CHILDREN_PACKING_PADDING);
1764
1765   create_pipeline (self);
1766   create_video_output_widget (self);
1767   create_audio_input (self);
1768   create_video_input (self);
1769
1770   priv->floating_toolbar = empathy_rounded_actor_new ();
1771
1772   gtk_widget_reparent (priv->bottom_toolbar,
1773       gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (priv->floating_toolbar)));
1774
1775   constraint = clutter_bind_constraint_new (
1776       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1777       CLUTTER_BIND_Y, 0);
1778
1779   clutter_actor_add_constraint (priv->floating_toolbar, constraint);
1780
1781   g_signal_connect (
1782       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1783       "notify::allocation",
1784       G_CALLBACK (empathy_call_window_stage_allocation_changed_cb),
1785       constraint);
1786
1787   clutter_actor_set_size (priv->floating_toolbar,
1788       FLOATING_TOOLBAR_WIDTH, FLOATING_TOOLBAR_HEIGHT);
1789   clutter_actor_set_opacity (priv->floating_toolbar, FLOATING_TOOLBAR_OPACITY);
1790
1791   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1792       priv->floating_toolbar,
1793       CLUTTER_BIN_ALIGNMENT_CENTER,
1794       CLUTTER_BIN_ALIGNMENT_END);
1795
1796   clutter_actor_raise_top (priv->floating_toolbar);
1797
1798   /* Transitions for the floating toolbar */
1799   priv->transitions = clutter_state_new ();
1800
1801   /* all transitions last for 2s */
1802   clutter_state_set_duration (priv->transitions, NULL, NULL, 2000);
1803
1804   /* transition from any state to "fade-out" state */
1805   clutter_state_set (priv->transitions, NULL, "fade-out",
1806       priv->floating_toolbar,
1807       "opacity", CLUTTER_EASE_OUT_QUAD, 0,
1808       NULL);
1809
1810   /* transition from any state to "fade-in" state */
1811   clutter_state_set (priv->transitions, NULL, "fade-in",
1812       priv->floating_toolbar,
1813       "opacity", CLUTTER_EASE_OUT_QUAD, FLOATING_TOOLBAR_OPACITY,
1814       NULL);
1815
1816   /* put the actor into the "fade-in" state with no animation */
1817   clutter_state_warp_to_state (priv->transitions, "fade-in");
1818
1819   /* The call will be started as soon the pipeline is playing */
1820   priv->start_call_when_playing = TRUE;
1821
1822   priv->dtmf_panel = empathy_create_dtmf_dialpad (G_OBJECT (self),
1823       G_CALLBACK (dtmf_button_pressed_cb),
1824       G_CALLBACK (dtmf_button_released_cb));
1825
1826   priv->tones = g_string_new ("");
1827
1828   gtk_box_pack_start (GTK_BOX (priv->pane), priv->dtmf_panel,
1829       FALSE, FALSE, 6);
1830
1831   gtk_box_pack_start (GTK_BOX (priv->pane), priv->details_vbox,
1832       FALSE, FALSE, 0);
1833
1834   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
1835
1836   gtk_widget_show_all (top_vbox);
1837
1838   gtk_widget_hide (priv->dtmf_panel);
1839   gtk_widget_hide (priv->details_vbox);
1840
1841   priv->fullscreen = empathy_call_window_fullscreen_new (self);
1842
1843   empathy_call_window_fullscreen_set_video_widget (priv->fullscreen,
1844       priv->video_container);
1845
1846   /* We hide the bottom toolbar after 3s of inactivity and show it
1847    * again on mouse movement */
1848   priv->inactivity_src = g_timeout_add_seconds (3,
1849       empathy_call_window_toolbar_timeout, self);
1850
1851   g_signal_connect (G_OBJECT (priv->fullscreen->leave_fullscreen_button),
1852       "clicked", G_CALLBACK (empathy_call_window_fullscreen_cb), self);
1853
1854   g_signal_connect (G_OBJECT (self), "realize",
1855     G_CALLBACK (empathy_call_window_realized_cb), self);
1856
1857   g_signal_connect (G_OBJECT (self), "delete-event",
1858     G_CALLBACK (empathy_call_window_delete_cb), self);
1859
1860   g_signal_connect (G_OBJECT (self), "window-state-event",
1861     G_CALLBACK (empathy_call_window_state_event_cb), self);
1862
1863   g_signal_connect (G_OBJECT (self), "key-press-event",
1864       G_CALLBACK (empathy_call_window_key_press_cb), self);
1865
1866   g_signal_connect (self, "motion-notify-event",
1867       G_CALLBACK (empathy_call_window_motion_notify_cb), self);
1868
1869   priv->timer = g_timer_new ();
1870
1871   g_object_ref (priv->ui_manager);
1872   g_object_unref (gui);
1873
1874   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1875   priv->mic_menu = empathy_mic_menu_new (self);
1876   priv->camera_menu = empathy_camera_menu_new (self);
1877
1878   empathy_call_window_show_hangup_button (self, TRUE);
1879
1880   /* Retrieve initial volume */
1881   priv->volume = g_settings_get_double (priv->settings,
1882       EMPATHY_PREFS_CALL_SOUND_VOLUME) / 100.0;
1883
1884   g_signal_connect (priv->settings, "changed::"EMPATHY_PREFS_CALL_SOUND_VOLUME,
1885       G_CALLBACK (empathy_call_window_prefs_volume_changed_cb), self);
1886
1887   empathy_geometry_bind (GTK_WINDOW (self), "call-window");
1888   /* These signals are used to track the window position and save it
1889    * when the window is destroyed. We need to do this as we don't want
1890    * the window geometry to be saved with the dialpad taken into account. */
1891   g_signal_connect (self, "destroy",
1892       G_CALLBACK (empathy_call_window_destroyed_cb), self);
1893   g_signal_connect (self, "configure-event",
1894       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1895   g_signal_connect (self, "window-state-event",
1896       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1897
1898   /* Don't display labels in both toolbars */
1899   gtk_toolbar_set_style (GTK_TOOLBAR (priv->toolbar), GTK_TOOLBAR_ICONS);
1900 }
1901
1902 /* Instead of specifying a width and a height, we specify only one size. That's
1903    because we want a square avatar icon.  */
1904 static void
1905 init_contact_avatar_with_size (EmpathyContact *contact,
1906     GtkWidget *image_widget,
1907     gint size)
1908 {
1909   GdkPixbuf *pixbuf_avatar = NULL;
1910
1911   if (contact != NULL)
1912     {
1913       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
1914         size, size);
1915     }
1916
1917   if (pixbuf_avatar == NULL)
1918     {
1919       pixbuf_avatar = empathy_pixbuf_from_icon_name_sized (
1920           EMPATHY_IMAGE_AVATAR_DEFAULT, size);
1921     }
1922
1923   gtk_image_set_from_pixbuf (GTK_IMAGE (image_widget), pixbuf_avatar);
1924
1925   if (pixbuf_avatar != NULL)
1926     g_object_unref (pixbuf_avatar);
1927 }
1928
1929 static void
1930 set_window_title (EmpathyCallWindow *self)
1931 {
1932   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1933   gchar *tmp;
1934
1935   if (priv->contact != NULL)
1936     {
1937       /* translators: Call is a noun and %s is the contact name. This string
1938        * is used in the window title */
1939       tmp = g_strdup_printf (_("Call with %s"),
1940           empathy_contact_get_alias (priv->contact));
1941       gtk_window_set_title (GTK_WINDOW (self), tmp);
1942       g_free (tmp);
1943     }
1944   else
1945     {
1946       g_warning ("Unknown remote contact!");
1947     }
1948 }
1949
1950 static void
1951 set_remote_user_name (EmpathyCallWindow *self,
1952   EmpathyContact *contact)
1953 {
1954   const gchar *alias = empathy_contact_get_alias (contact);
1955   const gchar *status = empathy_contact_get_status (contact);
1956   gchar *label;
1957
1958   label = g_strdup_printf ("%s\n<small>%s</small>", alias, status);
1959   gtk_label_set_markup (GTK_LABEL (self->priv->remote_user_name_toolbar),
1960       label);
1961   g_free (label);
1962 }
1963
1964 static void
1965 contact_name_changed_cb (EmpathyContact *contact,
1966     GParamSpec *pspec,
1967     EmpathyCallWindow *self)
1968 {
1969   set_window_title (self);
1970   set_remote_user_name (self, contact);
1971 }
1972
1973 static void
1974 contact_presence_changed_cb (EmpathyContact *contact,
1975     GParamSpec *pspec,
1976     EmpathyCallWindow *self)
1977 {
1978   set_remote_user_name (self, contact);
1979 }
1980
1981 static void
1982 contact_avatar_changed_cb (EmpathyContact *contact,
1983     GParamSpec *pspec,
1984     EmpathyCallWindow *self)
1985 {
1986   int size;
1987   GtkAllocation allocation;
1988   GtkWidget *avatar_widget;
1989
1990   avatar_widget = self->priv->remote_user_avatar_widget;
1991
1992   gtk_widget_get_allocation (avatar_widget, &allocation);
1993   size = allocation.height;
1994
1995   if (size == 0)
1996     {
1997       /* the widget is not allocated yet, set a default size */
1998       size = MIN (REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT,
1999           REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH);
2000     }
2001
2002   init_contact_avatar_with_size (contact, avatar_widget, size);
2003
2004   avatar_widget = self->priv->remote_user_avatar_toolbar;
2005
2006   gtk_widget_get_allocation (avatar_widget, &allocation);
2007   size = allocation.height;
2008
2009   if (size == 0)
2010     {
2011       /* the widget is not allocated yet, set a default size */
2012       size = SMALL_TOOLBAR_SIZE;
2013     }
2014
2015   init_contact_avatar_with_size (contact, avatar_widget, size);
2016 }
2017
2018 static void
2019 empathy_call_window_setup_avatars (EmpathyCallWindow *self,
2020     EmpathyCallHandler *handler)
2021 {
2022   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2023
2024   tp_g_signal_connect_object (priv->contact, "notify::name",
2025       G_CALLBACK (contact_name_changed_cb), self, 0);
2026   tp_g_signal_connect_object (priv->contact, "notify::avatar",
2027     G_CALLBACK (contact_avatar_changed_cb), self, 0);
2028   tp_g_signal_connect_object (priv->contact, "notify::presence",
2029       G_CALLBACK (contact_presence_changed_cb), self, 0);
2030
2031   set_window_title (self);
2032   set_remote_user_name (self, priv->contact);
2033
2034   init_contact_avatar_with_size (priv->contact,
2035       priv->remote_user_avatar_widget,
2036       MIN (REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH,
2037           REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT));
2038
2039   init_contact_avatar_with_size (priv->contact,
2040       priv->remote_user_avatar_toolbar,
2041       SMALL_TOOLBAR_SIZE);
2042
2043   /* The remote avatar is shown by default and will be hidden when we receive
2044      video from the remote side. */
2045   clutter_actor_hide (priv->video_output);
2046   gtk_widget_show (priv->remote_user_avatar_widget);
2047 }
2048
2049 static void
2050 update_send_codec (EmpathyCallWindow *self,
2051     gboolean audio)
2052 {
2053   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2054   FsCodec *codec;
2055   GtkWidget *widget;
2056   gchar *tmp;
2057
2058   if (audio)
2059     {
2060       codec = empathy_call_handler_get_send_audio_codec (priv->handler);
2061       widget = priv->acodec_encoding_label;
2062     }
2063   else
2064     {
2065       codec = empathy_call_handler_get_send_video_codec (priv->handler);
2066       widget = priv->vcodec_encoding_label;
2067     }
2068
2069   if (codec == NULL)
2070     return;
2071
2072   tmp = g_strdup_printf ("%s/%u", codec->encoding_name, codec->clock_rate);
2073   gtk_label_set_text (GTK_LABEL (widget), tmp);
2074   g_free (tmp);
2075 }
2076
2077 static void
2078 send_audio_codec_notify_cb (GObject *object,
2079     GParamSpec *pspec,
2080     gpointer user_data)
2081 {
2082   EmpathyCallWindow *self = user_data;
2083
2084   update_send_codec (self, TRUE);
2085 }
2086
2087 static void
2088 send_video_codec_notify_cb (GObject *object,
2089     GParamSpec *pspec,
2090     gpointer user_data)
2091 {
2092   EmpathyCallWindow *self = user_data;
2093
2094   update_send_codec (self, FALSE);
2095 }
2096
2097 static void
2098 update_recv_codec (EmpathyCallWindow *self,
2099     gboolean audio)
2100 {
2101   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2102   GList *codecs, *l;
2103   GtkWidget *widget;
2104   GString *str = NULL;
2105
2106   if (audio)
2107     {
2108       codecs = empathy_call_handler_get_recv_audio_codecs (priv->handler);
2109       widget = priv->acodec_decoding_label;
2110     }
2111   else
2112     {
2113       codecs = empathy_call_handler_get_recv_video_codecs (priv->handler);
2114       widget = priv->vcodec_decoding_label;
2115     }
2116
2117   if (codecs == NULL)
2118     return;
2119
2120   for (l = codecs; l != NULL; l = g_list_next (l))
2121     {
2122       FsCodec *codec = l->data;
2123
2124       if (str == NULL)
2125         str = g_string_new (NULL);
2126       else
2127         g_string_append (str, ", ");
2128
2129       g_string_append_printf (str, "%s/%u", codec->encoding_name,
2130           codec->clock_rate);
2131     }
2132
2133   gtk_label_set_text (GTK_LABEL (widget), str->str);
2134   g_string_free (str, TRUE);
2135 }
2136
2137 static void
2138 recv_audio_codecs_notify_cb (GObject *object,
2139     GParamSpec *pspec,
2140     gpointer user_data)
2141 {
2142   EmpathyCallWindow *self = user_data;
2143
2144   update_recv_codec (self, TRUE);
2145 }
2146
2147 static void
2148 recv_video_codecs_notify_cb (GObject *object,
2149     GParamSpec *pspec,
2150     gpointer user_data)
2151 {
2152   EmpathyCallWindow *self = user_data;
2153
2154   update_recv_codec (self, FALSE);
2155 }
2156
2157 static const gchar *
2158 candidate_type_to_str (FsCandidate *candidate)
2159 {
2160   switch (candidate->type)
2161     {
2162       case FS_CANDIDATE_TYPE_HOST:
2163         return "host";
2164       case FS_CANDIDATE_TYPE_SRFLX:
2165         return "server reflexive";
2166       case FS_CANDIDATE_TYPE_PRFLX:
2167         return "peer reflexive";
2168       case FS_CANDIDATE_TYPE_RELAY:
2169         return "relay";
2170       case FS_CANDIDATE_TYPE_MULTICAST:
2171         return "multicast";
2172     }
2173
2174   return NULL;
2175 }
2176
2177 static const gchar *
2178 candidate_type_to_desc (FsCandidate *candidate)
2179 {
2180   switch (candidate->type)
2181     {
2182       case FS_CANDIDATE_TYPE_HOST:
2183         return _("The IP address as seen by the machine");
2184       case FS_CANDIDATE_TYPE_SRFLX:
2185         return _("The IP address as seen by a server on the Internet");
2186       case FS_CANDIDATE_TYPE_PRFLX:
2187         return _("The IP address of the peer as seen by the other side");
2188       case FS_CANDIDATE_TYPE_RELAY:
2189         return _("The IP address of a relay server");
2190       case FS_CANDIDATE_TYPE_MULTICAST:
2191         return _("The IP address of the multicast group");
2192     }
2193
2194   return NULL;
2195 }
2196
2197 static void
2198 update_candidat_widget (EmpathyCallWindow *self,
2199     GtkWidget *label,
2200     GtkWidget *img,
2201     FsCandidate *candidate)
2202 {
2203   gchar *str;
2204
2205   g_assert (candidate != NULL);
2206   str = g_strdup_printf ("%s %u (%s)", candidate->ip,
2207       candidate->port, candidate_type_to_str (candidate));
2208
2209   gtk_label_set_text (GTK_LABEL (label), str);
2210   gtk_widget_set_tooltip_text (img, candidate_type_to_desc (candidate));
2211
2212   g_free (str);
2213 }
2214
2215 static void
2216 candidates_changed_cb (GObject *object,
2217     FsMediaType type,
2218     EmpathyCallWindow *self)
2219 {
2220   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2221   FsCandidate *candidate = NULL;
2222
2223   if (type == FS_MEDIA_TYPE_VIDEO)
2224     {
2225       /* Update remote candidate */
2226       candidate = empathy_call_handler_get_video_remote_candidate (
2227           priv->handler);
2228
2229       update_candidat_widget (self, priv->video_remote_candidate_label,
2230           priv->video_remote_candidate_info_img, candidate);
2231
2232       /* Update local candidate */
2233       candidate = empathy_call_handler_get_video_local_candidate (
2234           priv->handler);
2235
2236       update_candidat_widget (self, priv->video_local_candidate_label,
2237           priv->video_local_candidate_info_img, candidate);
2238     }
2239   else
2240     {
2241       /* Update remote candidate */
2242       candidate = empathy_call_handler_get_audio_remote_candidate (
2243           priv->handler);
2244
2245       update_candidat_widget (self, priv->audio_remote_candidate_label,
2246           priv->audio_remote_candidate_info_img, candidate);
2247
2248       /* Update local candidate */
2249       candidate = empathy_call_handler_get_audio_local_candidate (
2250           priv->handler);
2251
2252       update_candidat_widget (self, priv->audio_local_candidate_label,
2253           priv->audio_local_candidate_info_img, candidate);
2254     }
2255 }
2256
2257 static void
2258 empathy_call_window_constructed (GObject *object)
2259 {
2260   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2261   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2262   TpyCallChannel *call;
2263   TpyCallState state;
2264
2265   g_assert (priv->handler != NULL);
2266
2267   g_object_get (priv->handler, "call-channel", &call, NULL);
2268   state = tpy_call_channel_get_state (call, NULL, NULL);
2269   priv->outgoing = (state == TPY_CALL_STATE_PENDING_INITIATOR);
2270   tp_clear_object (&call);
2271
2272   g_object_get (priv->handler, "target-contact", &priv->contact, NULL);
2273   g_assert (priv->contact != NULL);
2274
2275   if (!empathy_contact_can_voip_video (priv->contact))
2276     {
2277       gtk_widget_set_sensitive (priv->video_call_button, FALSE);
2278       gtk_widget_set_sensitive (priv->camera_button, FALSE);
2279     }
2280
2281   empathy_call_window_setup_avatars (self, priv->handler);
2282   empathy_call_window_set_state_connecting (self);
2283
2284   if (!empathy_call_handler_has_initial_video (priv->handler))
2285     {
2286       gtk_toggle_tool_button_set_active (
2287           GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
2288     }
2289   /* If call has InitialVideo, the preview will be started once the call has
2290    * been started (start_call()). */
2291
2292   update_send_codec (self, TRUE);
2293   update_send_codec (self, FALSE);
2294   update_recv_codec (self, TRUE);
2295   update_recv_codec (self, FALSE);
2296
2297   tp_g_signal_connect_object (priv->handler, "notify::send-audio-codec",
2298       G_CALLBACK (send_audio_codec_notify_cb), self, 0);
2299   tp_g_signal_connect_object (priv->handler, "notify::send-video-codec",
2300       G_CALLBACK (send_video_codec_notify_cb), self, 0);
2301   tp_g_signal_connect_object (priv->handler, "notify::recv-audio-codecs",
2302       G_CALLBACK (recv_audio_codecs_notify_cb), self, 0);
2303   tp_g_signal_connect_object (priv->handler, "notify::recv-video-codecs",
2304       G_CALLBACK (recv_video_codecs_notify_cb), self, 0);
2305
2306   tp_g_signal_connect_object (priv->handler, "candidates-changed",
2307       G_CALLBACK (candidates_changed_cb), self, 0);
2308 }
2309
2310 static void empathy_call_window_dispose (GObject *object);
2311 static void empathy_call_window_finalize (GObject *object);
2312
2313 static void
2314 empathy_call_window_set_property (GObject *object,
2315   guint property_id, const GValue *value, GParamSpec *pspec)
2316 {
2317   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2318
2319   switch (property_id)
2320     {
2321       case PROP_CALL_HANDLER:
2322         priv->handler = g_value_dup_object (value);
2323         break;
2324       default:
2325         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2326     }
2327 }
2328
2329 static void
2330 empathy_call_window_get_property (GObject *object,
2331   guint property_id, GValue *value, GParamSpec *pspec)
2332 {
2333   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2334
2335   switch (property_id)
2336     {
2337       case PROP_CALL_HANDLER:
2338         g_value_set_object (value, priv->handler);
2339         break;
2340       default:
2341         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2342     }
2343 }
2344
2345 static void
2346 empathy_call_window_class_init (
2347   EmpathyCallWindowClass *empathy_call_window_class)
2348 {
2349   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
2350   GParamSpec *param_spec;
2351
2352   g_type_class_add_private (empathy_call_window_class,
2353     sizeof (EmpathyCallWindowPriv));
2354
2355   object_class->constructed = empathy_call_window_constructed;
2356   object_class->set_property = empathy_call_window_set_property;
2357   object_class->get_property = empathy_call_window_get_property;
2358
2359   object_class->dispose = empathy_call_window_dispose;
2360   object_class->finalize = empathy_call_window_finalize;
2361
2362   param_spec = g_param_spec_object ("handler",
2363     "handler", "The call handler",
2364     EMPATHY_TYPE_CALL_HANDLER,
2365     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
2366   g_object_class_install_property (object_class,
2367     PROP_CALL_HANDLER, param_spec);
2368 }
2369
2370 void
2371 empathy_call_window_dispose (GObject *object)
2372 {
2373   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2374   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2375
2376   if (priv->dispose_has_run)
2377     return;
2378
2379   priv->dispose_has_run = TRUE;
2380
2381   if (priv->handler != NULL)
2382     {
2383       empathy_call_handler_stop_call (priv->handler);
2384       tp_clear_object (&priv->handler);
2385     }
2386
2387   if (priv->bus_message_source_id != 0)
2388     {
2389       g_source_remove (priv->bus_message_source_id);
2390       priv->bus_message_source_id = 0;
2391     }
2392
2393   if (priv->got_video_src > 0)
2394     {
2395       g_source_remove (priv->got_video_src);
2396       priv->got_video_src = 0;
2397     }
2398
2399   if (priv->inactivity_src > 0)
2400     {
2401       g_source_remove (priv->inactivity_src);
2402       priv->inactivity_src = 0;
2403     }
2404
2405   tp_clear_object (&priv->pipeline);
2406   tp_clear_object (&priv->video_input);
2407   tp_clear_object (&priv->audio_input);
2408   tp_clear_object (&priv->video_tee);
2409   tp_clear_object (&priv->ui_manager);
2410   tp_clear_object (&priv->fullscreen);
2411   tp_clear_object (&priv->camera_monitor);
2412   tp_clear_object (&priv->settings);
2413   tp_clear_object (&priv->sound_mgr);
2414   tp_clear_object (&priv->mic_menu);
2415   tp_clear_object (&priv->camera_menu);
2416
2417   g_list_free_full (priv->notifiers, g_object_unref);
2418
2419   if (priv->timer_id != 0)
2420     g_source_remove (priv->timer_id);
2421   priv->timer_id = 0;
2422
2423   if (priv->contact != NULL)
2424     {
2425       g_signal_handlers_disconnect_by_func (priv->contact,
2426           contact_name_changed_cb, self);
2427       priv->contact = NULL;
2428     }
2429
2430   G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
2431 }
2432
2433 static void
2434 disconnect_video_output_motion_handler (EmpathyCallWindow *self)
2435 {
2436   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2437
2438   if (priv->video_output_motion_handler_id != 0)
2439     {
2440       g_signal_handler_disconnect (G_OBJECT (priv->video_container),
2441           priv->video_output_motion_handler_id);
2442       priv->video_output_motion_handler_id = 0;
2443     }
2444 }
2445
2446 void
2447 empathy_call_window_finalize (GObject *object)
2448 {
2449   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2450   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2451
2452   disconnect_video_output_motion_handler (self);
2453
2454   /* free any data held directly by the object here */
2455   g_mutex_free (priv->lock);
2456
2457   g_timer_destroy (priv->timer);
2458
2459   g_string_free (priv->tones, TRUE);
2460
2461   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
2462 }
2463
2464
2465 EmpathyCallWindow *
2466 empathy_call_window_new (EmpathyCallHandler *handler)
2467 {
2468   return EMPATHY_CALL_WINDOW (
2469     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
2470 }
2471
2472 void
2473 empathy_call_window_present (EmpathyCallWindow *self,
2474     EmpathyCallHandler *handler)
2475 {
2476   g_return_if_fail (EMPATHY_IS_CALL_HANDLER (handler));
2477
2478   tp_clear_object (&self->priv->handler);
2479   self->priv->handler = g_object_ref (handler);
2480   empathy_call_window_connect_handler (self);
2481
2482   empathy_window_present (GTK_WINDOW (self));
2483   empathy_call_window_restart_call (self);
2484 }
2485
2486 static void
2487 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
2488   GstElement *conference, gpointer user_data)
2489 {
2490   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2491   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2492   FsElementAddedNotifier *notifier;
2493   GKeyFile *keyfile;
2494
2495   DEBUG ("Conference added");
2496
2497   /* Add notifier to set the various element properties as needed */
2498   notifier = fs_element_added_notifier_new ();
2499   keyfile = fs_utils_get_default_element_properties (conference);
2500
2501   if (keyfile != NULL)
2502     fs_element_added_notifier_set_properties_from_keyfile (notifier, keyfile);
2503
2504   fs_element_added_notifier_add (notifier, GST_BIN (priv->pipeline));
2505
2506   priv->notifiers = g_list_prepend (priv->notifiers, notifier);
2507
2508   gst_bin_add (GST_BIN (priv->pipeline), conference);
2509   gst_element_set_state (conference, GST_STATE_PLAYING);
2510 }
2511
2512 static void
2513 empathy_call_window_conference_removed_cb (EmpathyCallHandler *handler,
2514   GstElement *conference, gpointer user_data)
2515 {
2516   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2517   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2518
2519   gst_bin_remove (GST_BIN (priv->pipeline), conference);
2520   gst_element_set_state (conference, GST_STATE_NULL);
2521 }
2522
2523 static gboolean
2524 empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
2525 {
2526   GstStateChangeReturn state_change_return;
2527   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2528
2529   if (priv->pipeline == NULL)
2530     return TRUE;
2531
2532   if (priv->bus_message_source_id != 0)
2533     {
2534       g_source_remove (priv->bus_message_source_id);
2535       priv->bus_message_source_id = 0;
2536     }
2537
2538   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2539
2540   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
2541         state_change_return == GST_STATE_CHANGE_NO_PREROLL)
2542     {
2543       if (priv->pipeline != NULL)
2544         g_object_unref (priv->pipeline);
2545       priv->pipeline = NULL;
2546
2547       if (priv->audio_output != NULL)
2548         g_object_unref (priv->audio_output);
2549       priv->audio_output = NULL;
2550
2551       if (priv->video_tee != NULL)
2552         g_object_unref (priv->video_tee);
2553       priv->video_tee = NULL;
2554
2555       if (priv->video_preview != NULL)
2556         clutter_actor_destroy (priv->video_preview);
2557       priv->video_preview = NULL;
2558
2559       /* If we destroy the preview while it's being dragged, we won't
2560        * get the ::drag-end signal, so manually destroy the clone */
2561       if (priv->drag_preview != NULL)
2562         {
2563           clutter_actor_destroy (priv->drag_preview);
2564           empathy_call_window_show_preview_rectangles (self, FALSE);
2565           priv->drag_preview = NULL;
2566         }
2567
2568       priv->funnel = NULL;
2569
2570       create_pipeline (self);
2571       /* Call will be started when user will hit the 'redial' button */
2572       priv->start_call_when_playing = FALSE;
2573       gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2574
2575       return TRUE;
2576     }
2577   else
2578     {
2579       g_message ("Error: could not destroy pipeline. Closing call window");
2580       gtk_widget_destroy (GTK_WIDGET (self));
2581
2582       return FALSE;
2583     }
2584 }
2585
2586 static void
2587 reset_details_pane (EmpathyCallWindow *self)
2588 {
2589   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2590
2591   gtk_label_set_text (GTK_LABEL (priv->vcodec_encoding_label), _("Unknown"));
2592   gtk_label_set_text (GTK_LABEL (priv->acodec_encoding_label), _("Unknown"));
2593   gtk_label_set_text (GTK_LABEL (priv->vcodec_decoding_label), _("Unknown"));
2594   gtk_label_set_text (GTK_LABEL (priv->acodec_decoding_label), _("Unknown"));
2595 }
2596
2597 static gboolean
2598 empathy_call_window_disconnected (EmpathyCallWindow *self,
2599     gboolean restart)
2600 {
2601   gboolean could_disconnect = FALSE;
2602   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2603   gboolean could_reset_pipeline;
2604
2605   /* Leave full screen mode if needed */
2606   gtk_window_unfullscreen (GTK_WINDOW (self));
2607
2608   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
2609   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
2610
2611   priv->sending_tones = FALSE;
2612   g_string_set_size (priv->tones, 0);
2613
2614   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
2615
2616   if (priv->call_state == CONNECTING)
2617       empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2618
2619   if (priv->call_state != REDIALING)
2620     priv->call_state = DISCONNECTED;
2621
2622   /* Show the toolbar */
2623   clutter_state_set_state (priv->transitions, "fade-in");
2624
2625   if (could_reset_pipeline)
2626     {
2627       g_mutex_lock (priv->lock);
2628
2629       g_timer_stop (priv->timer);
2630
2631       if (priv->timer_id != 0)
2632         g_source_remove (priv->timer_id);
2633       priv->timer_id = 0;
2634
2635       g_mutex_unlock (priv->lock);
2636
2637       if (!restart)
2638         /* We are about to destroy the window, no need to update it or create
2639          * a video preview */
2640         return TRUE;
2641
2642       empathy_call_window_status_message (self, _("Disconnected"));
2643
2644       empathy_call_window_show_hangup_button (self, FALSE);
2645
2646       /* Unsensitive the camera and mic button */
2647       gtk_widget_set_sensitive (priv->camera_button, FALSE);
2648       gtk_widget_set_sensitive (priv->mic_button, FALSE);
2649
2650       /* Be sure that the mic button is enabled */
2651       gtk_toggle_tool_button_set_active (
2652           GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
2653
2654       if (priv->camera_state == CAMERA_STATE_ON)
2655         {
2656           /* Restart the preview with the new pipeline. */
2657           display_video_preview (self, TRUE);
2658         }
2659
2660       /* destroy the video output; it will be recreated when we'll redial */
2661       disconnect_video_output_motion_handler (self);
2662       if (priv->video_output != NULL)
2663         clutter_actor_destroy (priv->video_output);
2664       priv->video_output = NULL;
2665       if (priv->got_video_src > 0)
2666         {
2667           g_source_remove (priv->got_video_src);
2668           priv->got_video_src = 0;
2669         }
2670
2671       gtk_widget_show (priv->remote_user_avatar_widget);
2672
2673       reset_details_pane (self);
2674
2675       priv->sending_video = FALSE;
2676       priv->call_started = FALSE;
2677
2678       could_disconnect = TRUE;
2679
2680       /* TODO: display the self avatar of the preview (depends if the "Always
2681        * Show Video Preview" is enabled or not) */
2682     }
2683
2684   return could_disconnect;
2685 }
2686
2687
2688 static void
2689 empathy_call_window_channel_closed_cb (EmpathyCallHandler *handler,
2690     gpointer user_data)
2691 {
2692   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2693   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2694
2695   if (empathy_call_window_disconnected (self, TRUE) &&
2696       priv->call_state == REDIALING)
2697       empathy_call_window_restart_call (self);
2698 }
2699
2700 static gboolean
2701 empathy_call_window_sink_removed_cb (EmpathyCallHandler *handler,
2702     GstPad *sink,
2703     FsMediaType media_type,
2704     EmpathyCallWindow *self)
2705 {
2706   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2707
2708   DEBUG ("removing content");
2709
2710   /*
2711    * This assumes that there is only one video stream per channel...
2712    */
2713
2714   if ((guint) media_type == FS_MEDIA_TYPE_VIDEO)
2715     {
2716       if (priv->funnel != NULL)
2717         {
2718           GstElement *output;
2719
2720           output = priv->video_output_sink;
2721
2722           gst_element_set_state (output, GST_STATE_NULL);
2723           gst_element_set_state (priv->funnel, GST_STATE_NULL);
2724
2725           gst_bin_remove (GST_BIN (priv->pipeline), output);
2726           gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2727           priv->funnel = NULL;
2728           return TRUE;
2729         }
2730     }
2731   else if (media_type == FS_MEDIA_TYPE_AUDIO)
2732     {
2733       if (priv->audio_output != NULL)
2734         {
2735           gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2736
2737           gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2738           priv->audio_output = NULL;
2739           return TRUE;
2740         }
2741     }
2742
2743   return FALSE;
2744 }
2745
2746 /* Called with global lock held */
2747 static GstPad *
2748 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
2749 {
2750   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2751   GstPad *pad;
2752   GstElement *output;
2753
2754   if (priv->funnel == NULL)
2755     {
2756       output = priv->video_output_sink;
2757
2758       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
2759
2760       if (!priv->funnel)
2761         {
2762           g_warning ("Could not create fsfunnel");
2763           return NULL;
2764         }
2765
2766       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->funnel))
2767         {
2768           gst_object_unref (priv->funnel);
2769           priv->funnel = NULL;
2770           g_warning ("Could  not add funnel to pipeline");
2771           return NULL;
2772         }
2773
2774       if (!gst_bin_add (GST_BIN (priv->pipeline), output))
2775         {
2776           g_warning ("Could not add the video output widget to the pipeline");
2777           goto error;
2778         }
2779
2780       if (!gst_element_link (priv->funnel, output))
2781         {
2782           g_warning ("Could not link output sink to funnel");
2783           goto error_output_added;
2784         }
2785
2786       if (gst_element_set_state (output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2787         {
2788           g_warning ("Could not start video sink");
2789           goto error_output_added;
2790         }
2791
2792       if (gst_element_set_state (priv->funnel, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2793         {
2794           g_warning ("Could not start funnel");
2795           goto error_output_added;
2796         }
2797     }
2798
2799   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
2800
2801   if (!pad)
2802     g_warning ("Could not get request pad from funnel");
2803
2804   return pad;
2805
2806
2807  error_output_added:
2808
2809   gst_element_set_locked_state (priv->funnel, TRUE);
2810   gst_element_set_locked_state (output, TRUE);
2811
2812   gst_element_set_state (priv->funnel, GST_STATE_NULL);
2813   gst_element_set_state (output, GST_STATE_NULL);
2814
2815   gst_bin_remove (GST_BIN (priv->pipeline), output);
2816   gst_element_set_locked_state (output, FALSE);
2817
2818  error:
2819
2820   gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2821   priv->funnel = NULL;
2822
2823   return NULL;
2824 }
2825
2826 /* Called with global lock held */
2827 static GstPad *
2828 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self)
2829 {
2830   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2831   GstPad *pad;
2832   GstPadTemplate *template;
2833
2834   if (priv->audio_output == NULL)
2835     {
2836       priv->audio_output = empathy_audio_sink_new ();
2837       g_object_ref_sink (priv->audio_output);
2838
2839       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output))
2840         {
2841           g_warning ("Could not add audio sink to pipeline");
2842           g_object_unref (priv->audio_output);
2843           goto error_add_output;
2844         }
2845
2846       if (gst_element_set_state (priv->audio_output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2847         {
2848           g_warning ("Could not start audio sink");
2849           goto error;
2850         }
2851     }
2852
2853   template = gst_element_class_get_pad_template (
2854     GST_ELEMENT_GET_CLASS (priv->audio_output), "sink%d");
2855
2856   pad = gst_element_request_pad (priv->audio_output,
2857     template, NULL, NULL);
2858
2859   if (pad == NULL)
2860     {
2861       g_warning ("Could not get sink pad from sink");
2862       return NULL;
2863     }
2864
2865   return pad;
2866
2867 error:
2868   gst_element_set_locked_state (priv->audio_output, TRUE);
2869   gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2870   gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2871   priv->audio_output = NULL;
2872
2873 error_add_output:
2874
2875   return NULL;
2876 }
2877
2878 static gboolean
2879 empathy_call_window_update_timer (gpointer user_data)
2880 {
2881   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2882   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2883   const gchar *status;
2884   gchar *str;
2885   gdouble time_;
2886
2887   time_ = g_timer_elapsed (priv->timer, NULL);
2888
2889   if (priv->call_state == HELD)
2890     status = _("On hold");
2891   else if (!gtk_toggle_tool_button_get_active (
2892       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
2893     status = _("Mute");
2894   else
2895     status = _("Duration");
2896
2897   /* Translators: 'status - minutes:seconds' the caller has been connected */
2898   str = g_strdup_printf (_("%s â€” %d:%02dm"),
2899       status,
2900       (int) time_ / 60, (int) time_ % 60);
2901   empathy_call_window_status_message (self, str);
2902   g_free (str);
2903
2904   return TRUE;
2905 }
2906
2907 #if 0
2908 static void
2909 display_error (EmpathyCallWindow *self,
2910     TpyCallChannel *call,
2911     const gchar *img,
2912     const gchar *title,
2913     const gchar *desc,
2914     const gchar *details)
2915 {
2916   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2917   GtkWidget *info_bar;
2918   GtkWidget *content_area;
2919   GtkWidget *hbox;
2920   GtkWidget *vbox;
2921   GtkWidget *image;
2922   GtkWidget *label;
2923   gchar *txt;
2924
2925   /* Create info bar */
2926   info_bar = gtk_info_bar_new_with_buttons (GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2927       NULL);
2928
2929   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
2930
2931   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2932
2933   /* hbox containing the image and the messages vbox */
2934   hbox = gtk_hbox_new (FALSE, 3);
2935   gtk_container_add (GTK_CONTAINER (content_area), hbox);
2936
2937   /* Add image */
2938   image = gtk_image_new_from_icon_name (img, GTK_ICON_SIZE_DIALOG);
2939   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2940
2941   /* vbox containing the main message and the details expander */
2942   vbox = gtk_vbox_new (FALSE, 3);
2943   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
2944
2945   /* Add text */
2946   txt = g_strdup_printf ("<b>%s</b>\n%s", title, desc);
2947
2948   label = gtk_label_new (NULL);
2949   gtk_label_set_markup (GTK_LABEL (label), txt);
2950   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2951   gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2952   g_free (txt);
2953
2954   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
2955
2956   /* Add details */
2957   if (details != NULL)
2958     {
2959       GtkWidget *expander;
2960
2961       expander = gtk_expander_new (_("Technical Details"));
2962
2963       txt = g_strdup_printf ("<i>%s</i>", details);
2964
2965       label = gtk_label_new (NULL);
2966       gtk_label_set_markup (GTK_LABEL (label), txt);
2967       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2968       gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2969       g_free (txt);
2970
2971       gtk_container_add (GTK_CONTAINER (expander), label);
2972       gtk_box_pack_start (GTK_BOX (vbox), expander, TRUE, TRUE, 0);
2973     }
2974
2975   g_signal_connect (info_bar, "response",
2976       G_CALLBACK (gtk_widget_destroy), NULL);
2977
2978   gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar,
2979       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
2980   gtk_widget_show_all (info_bar);
2981 }
2982
2983 static gchar *
2984 media_stream_error_to_txt (EmpathyCallWindow *self,
2985     TpyCallChannel *call,
2986     gboolean audio,
2987     TpMediaStreamError error)
2988 {
2989   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2990   const gchar *cm = NULL;
2991   gchar *url;
2992   gchar *result;
2993
2994   switch (error)
2995     {
2996       case TP_MEDIA_STREAM_ERROR_CODEC_NEGOTIATION_FAILED:
2997         if (audio)
2998           return g_strdup_printf (
2999               _("%s's software does not understand any of the audio formats "
3000                 "supported by your computer"),
3001             empathy_contact_get_alias (priv->contact));
3002         else
3003           return g_strdup_printf (
3004               _("%s's software does not understand any of the video formats "
3005                 "supported by your computer"),
3006             empathy_contact_get_alias (priv->contact));
3007
3008       case TP_MEDIA_STREAM_ERROR_CONNECTION_FAILED:
3009         return g_strdup_printf (
3010             _("Can't establish a connection to %s. "
3011               "One of you might be on a network that does not allow "
3012               "direct connections."),
3013           empathy_contact_get_alias (priv->contact));
3014
3015       case TP_MEDIA_STREAM_ERROR_NETWORK_ERROR:
3016           return g_strdup (_("There was a failure on the network"));
3017
3018       case TP_MEDIA_STREAM_ERROR_NO_CODECS:
3019         if (audio)
3020           return g_strdup (_("The audio formats necessary for this call "
3021                 "are not installed on your computer"));
3022         else
3023           return g_strdup (_("The video formats necessary for this call "
3024                 "are not installed on your computer"));
3025
3026       case TP_MEDIA_STREAM_ERROR_INVALID_CM_BEHAVIOR:
3027         tp_connection_parse_object_path (
3028             tp_channel_borrow_connection (TP_CHANNEL (call)),
3029             NULL, &cm);
3030
3031         url = g_strdup_printf ("http://bugs.freedesktop.org/enter_bug.cgi?"
3032             "product=Telepathy&amp;component=%s", cm);
3033
3034         result = g_strdup_printf (
3035             _("Something unexpected happened in a Telepathy component. "
3036               "Please <a href=\"%s\">report this bug</a> and attach "
3037               "logs gathered from the 'Debug' window in the Help menu."), url);
3038
3039         g_free (url);
3040         g_free (cm);
3041         return result;
3042
3043       case TP_MEDIA_STREAM_ERROR_MEDIA_ERROR:
3044         return g_strdup (_("There was a failure in the call engine"));
3045
3046       case TP_MEDIA_STREAM_ERROR_EOS:
3047         return g_strdup (_("The end of the stream was reached"));
3048
3049       case TP_MEDIA_STREAM_ERROR_UNKNOWN:
3050       default:
3051         return NULL;
3052     }
3053 }
3054
3055 static void
3056 empathy_call_window_stream_error (EmpathyCallWindow *self,
3057     TpyCallChannel *call,
3058     gboolean audio,
3059     guint code,
3060     const gchar *msg,
3061     const gchar *icon,
3062     const gchar *title)
3063 {
3064   gchar *desc;
3065
3066   desc = media_stream_error_to_txt (self, call, audio, code);
3067   if (desc == NULL)
3068     {
3069       /* No description, use the error message. That's not great as it's not
3070        * localized but it's better than nothing. */
3071       display_error (self, call, icon, title, msg, NULL);
3072     }
3073   else
3074     {
3075       display_error (self, call, icon, title, desc, msg);
3076       g_free (desc);
3077     }
3078 }
3079
3080 static void
3081 empathy_call_window_audio_stream_error (TpyCallChannel *call,
3082     guint code,
3083     const gchar *msg,
3084     EmpathyCallWindow *self)
3085 {
3086   empathy_call_window_stream_error (self, call, TRUE, code, msg,
3087       "gnome-stock-mic", _("Can't establish audio stream"));
3088 }
3089
3090 static void
3091 empathy_call_window_video_stream_error (TpyCallChannel *call,
3092     guint code,
3093     const gchar *msg,
3094     EmpathyCallWindow *self)
3095 {
3096   empathy_call_window_stream_error (self, call, FALSE, code, msg,
3097       "camera-web", _("Can't establish video stream"));
3098 }
3099 #endif
3100
3101 static void
3102 empathy_call_window_state_changed_cb (EmpathyCallHandler *handler,
3103     TpyCallState state,
3104     EmpathyCallWindow *self)
3105 {
3106   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3107   TpyCallChannel *call;
3108   gboolean can_send_video;
3109
3110   if (state != TPY_CALL_STATE_ACCEPTED)
3111     return;
3112
3113   if (priv->call_state == CONNECTED)
3114     return;
3115
3116   g_timer_start (priv->timer);
3117   priv->call_state = CONNECTED;
3118
3119   empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
3120
3121   can_send_video = priv->video_input != NULL &&
3122     empathy_contact_can_voip_video (priv->contact) &&
3123     empathy_camera_monitor_get_available (priv->camera_monitor);
3124
3125   g_object_get (priv->handler, "call-channel", &call, NULL);
3126
3127   if (tpy_call_channel_has_dtmf (call))
3128     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
3129
3130   if (priv->video_input == NULL)
3131     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
3132
3133   gtk_widget_set_sensitive (priv->camera_button, can_send_video);
3134
3135   empathy_call_window_show_hangup_button (self, TRUE);
3136
3137   gtk_widget_set_sensitive (priv->mic_button, TRUE);
3138
3139   clutter_actor_hide (priv->video_output);
3140   gtk_widget_show (priv->remote_user_avatar_widget);
3141
3142   g_object_unref (call);
3143
3144   g_mutex_lock (priv->lock);
3145
3146   priv->timer_id = g_timeout_add_seconds (1,
3147     empathy_call_window_update_timer, self);
3148
3149   g_mutex_unlock (priv->lock);
3150
3151   empathy_call_window_update_timer (self);
3152
3153   gtk_action_set_sensitive (priv->menu_fullscreen, TRUE);
3154 }
3155
3156 static gboolean
3157 empathy_call_window_show_video_output_cb (gpointer user_data)
3158 {
3159   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3160
3161   if (self->priv->video_output != NULL)
3162     {
3163       gtk_widget_hide (self->priv->remote_user_avatar_widget);
3164       clutter_actor_show (self->priv->video_output);
3165       empathy_call_window_raise_actors (self);
3166     }
3167
3168   return FALSE;
3169 }
3170
3171 static gboolean
3172 empathy_call_window_check_video_cb (gpointer data)
3173 {
3174   EmpathyCallWindow *self = data;
3175
3176   if (self->priv->got_video)
3177     {
3178       self->priv->got_video = FALSE;
3179       return TRUE;
3180     }
3181
3182   /* No video in the last N seconds, display the remote avatar */
3183   empathy_call_window_show_video_output (self, FALSE);
3184
3185   return TRUE;
3186 }
3187
3188 /* Called from the streaming thread */
3189 static gboolean
3190 empathy_call_window_video_probe_cb (GstPad *pad,
3191     GstMiniObject *mini_obj,
3192     EmpathyCallWindow *self)
3193 {
3194   /* Ignore events */
3195   if (GST_IS_EVENT (mini_obj))
3196     return TRUE;
3197
3198   if (G_UNLIKELY (!self->priv->got_video))
3199     {
3200       /* show the remote video */
3201       g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
3202           empathy_call_window_show_video_output_cb,
3203           g_object_ref (self), g_object_unref);
3204
3205       self->priv->got_video = TRUE;
3206     }
3207
3208   return TRUE;
3209 }
3210
3211 /* Called from the streaming thread */
3212 static gboolean
3213 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
3214   GstPad *src, guint media_type, gpointer user_data)
3215 {
3216   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3217   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3218   gboolean retval = FALSE;
3219
3220   GstPad *pad;
3221
3222   g_mutex_lock (priv->lock);
3223
3224   switch (media_type)
3225     {
3226       case TP_MEDIA_STREAM_TYPE_AUDIO:
3227         pad = empathy_call_window_get_audio_sink_pad (self);
3228         break;
3229       case TP_MEDIA_STREAM_TYPE_VIDEO:
3230         g_idle_add (empathy_call_window_show_video_output_cb, self);
3231         pad = empathy_call_window_get_video_sink_pad (self);
3232
3233         gst_pad_add_data_probe (src,
3234             G_CALLBACK (empathy_call_window_video_probe_cb), self);
3235         if (priv->got_video_src > 0)
3236           g_source_remove (priv->got_video_src);
3237         priv->got_video_src = g_timeout_add_seconds (5,
3238             empathy_call_window_check_video_cb, self);
3239         break;
3240       default:
3241         g_assert_not_reached ();
3242     }
3243
3244   if (pad == NULL)
3245     goto out;
3246
3247   if (GST_PAD_LINK_FAILED (gst_pad_link (src, pad)))
3248       g_warning ("Could not link %s sink pad",
3249           media_type == TP_MEDIA_STREAM_TYPE_AUDIO ? "audio" : "video");
3250   else
3251       retval = TRUE;
3252
3253   gst_object_unref (pad);
3254
3255  out:
3256
3257   /* If no sink could be linked, try to add fakesink to prevent the whole call
3258    * aborting */
3259
3260   if (!retval)
3261     {
3262       GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
3263
3264       if (gst_bin_add (GST_BIN (priv->pipeline), fakesink))
3265         {
3266           GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
3267           if (gst_element_set_state (fakesink, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE ||
3268               GST_PAD_LINK_FAILED (gst_pad_link (src, sinkpad)))
3269             {
3270               gst_element_set_locked_state (fakesink, TRUE);
3271               gst_element_set_state (fakesink, GST_STATE_NULL);
3272               gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
3273             }
3274           else
3275             {
3276               DEBUG ("Could not link real sink, linked fakesink instead");
3277             }
3278           gst_object_unref (sinkpad);
3279         }
3280       else
3281         {
3282           gst_object_unref (fakesink);
3283         }
3284     }
3285
3286
3287   g_mutex_unlock (priv->lock);
3288
3289   return TRUE;
3290 }
3291
3292 static gboolean
3293 empathy_call_window_sink_added_cb (EmpathyCallHandler *handler,
3294   GstPad *sink, FsMediaType media_type, gpointer user_data)
3295 {
3296   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3297   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3298   GstPad *pad;
3299   gboolean retval = FALSE;
3300
3301   switch (media_type)
3302     {
3303       case FS_MEDIA_TYPE_AUDIO:
3304         if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input))
3305           {
3306             g_warning ("Could not add audio source to pipeline");
3307             break;
3308           }
3309
3310         pad = gst_element_get_static_pad (priv->audio_input, "src");
3311         if (!pad)
3312           {
3313             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3314             g_warning ("Could not get source pad from audio source");
3315             break;
3316           }
3317
3318         if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3319           {
3320             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3321             g_warning ("Could not link audio source to farsight");
3322             break;
3323           }
3324
3325         if (gst_element_set_state (priv->audio_input, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
3326           {
3327             g_warning ("Could not start audio source");
3328             gst_element_set_state (priv->audio_input, GST_STATE_NULL);
3329             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3330             break;
3331           }
3332
3333         retval = TRUE;
3334         break;
3335       case FS_MEDIA_TYPE_VIDEO:
3336         if (priv->video_tee != NULL)
3337           {
3338             pad = gst_element_get_request_pad (priv->video_tee, "src%d");
3339             if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3340               {
3341                 g_warning ("Could not link video source input pipeline");
3342                 break;
3343               }
3344             gst_object_unref (pad);
3345           }
3346
3347         retval = TRUE;
3348         break;
3349       default:
3350         g_assert_not_reached ();
3351     }
3352
3353   return retval;
3354 }
3355
3356 static void
3357 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
3358 {
3359   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3360   GstElement *preview;
3361
3362   disable_camera (self);
3363
3364   DEBUG ("remove video input");
3365   preview = priv->video_preview_sink;
3366
3367   gst_element_set_state (priv->video_input, GST_STATE_NULL);
3368   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
3369   gst_element_set_state (preview, GST_STATE_NULL);
3370
3371   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
3372     preview, NULL);
3373
3374   g_object_unref (priv->video_input);
3375   priv->video_input = NULL;
3376   g_object_unref (priv->video_tee);
3377   priv->video_tee = NULL;
3378   clutter_actor_destroy (priv->video_preview);
3379   priv->video_preview = NULL;
3380
3381   gtk_widget_set_sensitive (priv->camera_button, FALSE);
3382 }
3383
3384 static void
3385 start_call (EmpathyCallWindow *self)
3386 {
3387   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3388
3389   priv->call_started = TRUE;
3390   empathy_call_handler_start_call (priv->handler,
3391       gtk_get_current_event_time ());
3392
3393   if (empathy_call_handler_has_initial_video (priv->handler))
3394     {
3395       TpyCallChannel *call;
3396       TpySendingState s;
3397
3398       g_object_get (priv->handler, "call-channel", &call, NULL);
3399       s = tpy_call_channel_get_video_state (call);
3400
3401       if (s == TPY_SENDING_STATE_PENDING_SEND ||
3402           s == TPY_SENDING_STATE_SENDING)
3403         {
3404           /* Enable 'send video' buttons and display the preview */
3405           gtk_toggle_tool_button_set_active (
3406             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), TRUE);
3407         }
3408       else
3409         {
3410           gtk_toggle_tool_button_set_active (
3411             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
3412
3413           if (priv->video_preview == NULL)
3414             {
3415               create_video_preview (self);
3416               add_video_preview_to_pipeline (self);
3417             }
3418         }
3419
3420       g_object_unref (call);
3421     }
3422 }
3423
3424 static gboolean
3425 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
3426   gpointer user_data)
3427 {
3428   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3429   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3430   GstState newstate, pending;
3431
3432   empathy_call_handler_bus_message (priv->handler, bus, message);
3433
3434   switch (GST_MESSAGE_TYPE (message))
3435     {
3436       case GST_MESSAGE_STATE_CHANGED:
3437         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
3438           {
3439             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3440           }
3441         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
3442             !priv->call_started)
3443           {
3444             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3445             if (newstate == GST_STATE_PAUSED)
3446               {
3447                 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3448                 priv->pipeline_playing = TRUE;
3449
3450                 if (priv->start_call_when_playing)
3451                   start_call (self);
3452               }
3453           }
3454         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_preview_sink))
3455           {
3456             gst_message_parse_state_changed (message, NULL, &newstate,
3457                 &pending);
3458
3459             if (newstate == GST_STATE_PLAYING &&
3460                 pending == GST_STATE_VOID_PENDING)
3461               empathy_call_window_stop_camera_spinning (self);
3462           }
3463         break;
3464       case GST_MESSAGE_ERROR:
3465         {
3466           GError *error = NULL;
3467           GstElement *gst_error;
3468           gchar *debug;
3469
3470           gst_message_parse_error (message, &error, &debug);
3471           gst_error = GST_ELEMENT (GST_MESSAGE_SRC (message));
3472
3473           g_message ("Element error: %s -- %s\n", error->message, debug);
3474
3475           if (g_str_has_prefix (gst_element_get_name (gst_error),
3476                 VIDEO_INPUT_ERROR_PREFIX))
3477             {
3478               /* Remove the video input and continue */
3479               if (priv->video_input != NULL)
3480                 empathy_call_window_remove_video_input (self);
3481               gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3482             }
3483           else
3484             {
3485               empathy_call_window_disconnected (self, TRUE);
3486             }
3487           g_error_free (error);
3488           g_free (debug);
3489         }
3490       case GST_MESSAGE_UNKNOWN:
3491       case GST_MESSAGE_EOS:
3492       case GST_MESSAGE_WARNING:
3493       case GST_MESSAGE_INFO:
3494       case GST_MESSAGE_TAG:
3495       case GST_MESSAGE_BUFFERING:
3496       case GST_MESSAGE_STATE_DIRTY:
3497       case GST_MESSAGE_STEP_DONE:
3498       case GST_MESSAGE_CLOCK_PROVIDE:
3499       case GST_MESSAGE_CLOCK_LOST:
3500       case GST_MESSAGE_NEW_CLOCK:
3501       case GST_MESSAGE_STRUCTURE_CHANGE:
3502       case GST_MESSAGE_STREAM_STATUS:
3503       case GST_MESSAGE_APPLICATION:
3504       case GST_MESSAGE_ELEMENT:
3505       case GST_MESSAGE_SEGMENT_START:
3506       case GST_MESSAGE_SEGMENT_DONE:
3507       case GST_MESSAGE_DURATION:
3508       case GST_MESSAGE_ANY:
3509       default:
3510         break;
3511     }
3512
3513   return TRUE;
3514 }
3515
3516 static void
3517 empathy_call_window_members_changed_cb (TpyCallChannel *call,
3518     GHashTable *members,
3519     EmpathyCallWindow *self)
3520 {
3521   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3522   GHashTableIter iter;
3523   gpointer key, value;
3524   gboolean held = FALSE;
3525
3526   g_hash_table_iter_init (&iter, members);
3527   while (g_hash_table_iter_next (&iter, &key, &value))
3528     {
3529       if (GPOINTER_TO_INT (value) & TPY_CALL_MEMBER_FLAG_HELD)
3530         {
3531           /* This assumes this is a 1-1 call, otherwise one participant
3532            * putting the call on hold wouldn't mean the call is on hold
3533            * for everyone. */
3534           held = TRUE;
3535           break;
3536         }
3537     }
3538
3539   if (held)
3540     priv->call_state = HELD;
3541   else if (priv->call_state == HELD)
3542     priv->call_state = CONNECTED;
3543 }
3544
3545 static void
3546 call_handler_notify_call_cb (EmpathyCallHandler *handler,
3547     GParamSpec *spec,
3548     EmpathyCallWindow *self)
3549 {
3550   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3551   TpyCallChannel *call;
3552
3553   g_object_get (priv->handler, "call-channel", &call, NULL);
3554   if (call == NULL)
3555     return;
3556
3557 /* FIXME
3558   tp_g_signal_connect_object (call, "audio-stream-error",
3559       G_CALLBACK (empathy_call_window_audio_stream_error), self, 0);
3560   tp_g_signal_connect_object (call, "video-stream-error",
3561       G_CALLBACK (empathy_call_window_video_stream_error), self, 0);
3562 */
3563
3564   tp_g_signal_connect_object (call, "members-changed",
3565       G_CALLBACK (empathy_call_window_members_changed_cb), self, 0);
3566
3567   tp_cli_channel_interface_dtmf_connect_to_stopped_tones (TP_CHANNEL (call),
3568       empathy_call_window_tones_stopped_cb, self, NULL,
3569       G_OBJECT (call), NULL);
3570
3571   g_object_unref (call);
3572 }
3573
3574 static void
3575 empathy_call_window_connect_handler (EmpathyCallWindow *self)
3576 {
3577   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3578   TpyCallChannel *call;
3579
3580   g_signal_connect (priv->handler, "state-changed",
3581     G_CALLBACK (empathy_call_window_state_changed_cb), self);
3582   g_signal_connect (priv->handler, "conference-added",
3583     G_CALLBACK (empathy_call_window_conference_added_cb), self);
3584   g_signal_connect (priv->handler, "conference-removed",
3585     G_CALLBACK (empathy_call_window_conference_removed_cb), self);
3586   g_signal_connect (priv->handler, "closed",
3587     G_CALLBACK (empathy_call_window_channel_closed_cb), self);
3588   g_signal_connect (priv->handler, "src-pad-added",
3589     G_CALLBACK (empathy_call_window_src_added_cb), self);
3590   g_signal_connect (priv->handler, "sink-pad-added",
3591     G_CALLBACK (empathy_call_window_sink_added_cb), self);
3592   g_signal_connect (priv->handler, "sink-pad-removed",
3593     G_CALLBACK (empathy_call_window_sink_removed_cb), self);
3594
3595   /* We connect to ::call-channel unconditionally since we'll
3596    * get new channels if we hangup and redial or if we reuse the
3597    * call window. */
3598   g_signal_connect (priv->handler, "notify::call-channel",
3599     G_CALLBACK (call_handler_notify_call_cb), self);
3600
3601   g_object_get (priv->handler, "call-channel", &call, NULL);
3602   if (call != NULL)
3603     {
3604       /* We won't get notify::call-channel for this channel, so
3605        * directly call the callback. */
3606       call_handler_notify_call_cb (priv->handler, NULL, self);
3607       g_object_unref (call);
3608     }
3609 }
3610
3611 static void
3612 empathy_call_window_realized_cb (GtkWidget *widget,
3613     EmpathyCallWindow *self)
3614 {
3615   gint width;
3616
3617   /* Make the hangup button twice as wide */
3618   width = gtk_widget_get_allocated_width (self->priv->hangup_button);
3619   gtk_widget_set_size_request (self->priv->hangup_button, width * 2, -1);
3620
3621   empathy_call_window_connect_handler (self);
3622
3623   gst_element_set_state (self->priv->pipeline, GST_STATE_PAUSED);
3624 }
3625
3626 static gboolean
3627 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
3628   EmpathyCallWindow *window)
3629 {
3630   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3631
3632   if (priv->pipeline != NULL)
3633     {
3634       if (priv->bus_message_source_id != 0)
3635         {
3636           g_source_remove (priv->bus_message_source_id);
3637           priv->bus_message_source_id = 0;
3638         }
3639
3640       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
3641     }
3642
3643   if (priv->call_state == CONNECTING)
3644     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
3645
3646   return FALSE;
3647 }
3648
3649 static void
3650 show_controls (EmpathyCallWindow *window, gboolean set_fullscreen)
3651 {
3652   GtkWidget *menu;
3653   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3654
3655   menu = gtk_ui_manager_get_widget (priv->ui_manager,
3656             "/menubar1");
3657
3658   if (set_fullscreen)
3659     {
3660       gtk_widget_hide (priv->dtmf_panel);
3661       gtk_widget_hide (menu);
3662       gtk_widget_hide (priv->toolbar);
3663     }
3664   else
3665     {
3666       if (priv->dialpad_was_visible_before_fs)
3667         gtk_widget_show (priv->dtmf_panel);
3668
3669       gtk_widget_show (menu);
3670       gtk_widget_show (priv->toolbar);
3671
3672       gtk_window_resize (GTK_WINDOW (window), priv->original_width_before_fs,
3673           priv->original_height_before_fs);
3674     }
3675 }
3676
3677 static void
3678 show_borders (EmpathyCallWindow *window, gboolean set_fullscreen)
3679 {
3680   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3681
3682   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
3683       set_fullscreen ? 0 : CONTENT_HBOX_BORDER_WIDTH);
3684   gtk_box_set_spacing (GTK_BOX (priv->content_hbox),
3685       set_fullscreen ? 0 : CONTENT_HBOX_SPACING);
3686
3687   if (priv->video_output != NULL)
3688     {
3689 #if 0
3690       gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
3691           priv->video_output, TRUE, TRUE,
3692           set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
3693           GTK_PACK_START);
3694 #endif
3695     }
3696 }
3697
3698 static gboolean
3699 empathy_call_window_state_event_cb (GtkWidget *widget,
3700   GdkEventWindowState *event, EmpathyCallWindow *window)
3701 {
3702   if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
3703     {
3704       EmpathyCallWindowPriv *priv = GET_PRIV (window);
3705       gboolean set_fullscreen = event->new_window_state &
3706         GDK_WINDOW_STATE_FULLSCREEN;
3707
3708       if (set_fullscreen)
3709         {
3710           gboolean dialpad_was_visible;
3711           GtkAllocation allocation;
3712           gint original_width, original_height;
3713
3714           gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3715           original_width = allocation.width;
3716           original_height = allocation.height;
3717
3718           g_object_get (priv->dtmf_panel,
3719               "visible", &dialpad_was_visible,
3720               NULL);
3721
3722           priv->dialpad_was_visible_before_fs = dialpad_was_visible;
3723           priv->original_width_before_fs = original_width;
3724           priv->original_height_before_fs = original_height;
3725
3726           if (priv->video_output_motion_handler_id == 0 &&
3727                 priv->video_output != NULL)
3728             {
3729               priv->video_output_motion_handler_id = g_signal_connect (
3730                   G_OBJECT (priv->video_container), "motion-notify-event",
3731                   G_CALLBACK (empathy_call_window_video_output_motion_notify),
3732                   window);
3733             }
3734         }
3735       else
3736         {
3737           disconnect_video_output_motion_handler (window);
3738         }
3739
3740       empathy_call_window_fullscreen_set_fullscreen (priv->fullscreen,
3741           set_fullscreen);
3742       show_controls (window, set_fullscreen);
3743       show_borders (window, set_fullscreen);
3744       gtk_action_set_stock_id (priv->menu_fullscreen,
3745           (set_fullscreen ? "gtk-leave-fullscreen" : "gtk-fullscreen"));
3746       priv->is_fullscreen = set_fullscreen;
3747   }
3748
3749   return FALSE;
3750 }
3751
3752 static void
3753 empathy_call_window_show_dialpad (EmpathyCallWindow *window,
3754     gboolean active)
3755 {
3756   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3757   int w, h, dialpad_width;
3758   GtkAllocation allocation;
3759
3760   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3761   w = allocation.width;
3762   h = allocation.height;
3763
3764   gtk_widget_get_preferred_width (priv->dtmf_panel, &dialpad_width, NULL);
3765
3766   if (active)
3767     {
3768       gtk_widget_show (priv->dtmf_panel);
3769       w += dialpad_width;
3770     }
3771   else
3772     {
3773       w -= dialpad_width;
3774       gtk_widget_hide (priv->dtmf_panel);
3775     }
3776
3777   if (w > 0 && h > 0)
3778     gtk_window_resize (GTK_WINDOW (window), w, h);
3779 }
3780
3781 static void
3782 empathy_call_window_set_send_video (EmpathyCallWindow *window,
3783   CameraState state)
3784 {
3785   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3786   TpyCallChannel *call;
3787
3788   priv->sending_video = (state == CAMERA_STATE_ON);
3789
3790   if (state == CAMERA_STATE_ON)
3791     {
3792       /* When we start sending video, we want to show the video preview by
3793          default. */
3794       display_video_preview (window, TRUE);
3795     }
3796   else
3797     {
3798       display_video_preview (window, FALSE);
3799     }
3800
3801   if (priv->call_state != CONNECTED)
3802     return;
3803
3804   g_object_get (priv->handler, "call-channel", &call, NULL);
3805   DEBUG ("%s sending video", priv->sending_video ? "start": "stop");
3806   tpy_call_channel_send_video (call, priv->sending_video);
3807   g_object_unref (call);
3808 }
3809
3810 static void
3811 empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
3812   EmpathyCallWindow *self)
3813 {
3814   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3815   gboolean active;
3816
3817   active = (gtk_toggle_tool_button_get_active (toggle));
3818
3819   /* We don't want the settings callback to react to this change to avoid
3820    * a loop. */
3821   g_signal_handlers_block_by_func (priv->settings,
3822       empathy_call_window_prefs_volume_changed_cb, self);
3823
3824   if (active)
3825     {
3826       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
3827           priv->volume * 100);
3828     }
3829   else
3830     {
3831       /* TODO, Instead of setting the input volume to 0 we should probably
3832        * stop sending but this would cause the audio call to drop if both
3833        * sides mute at the same time on certain CMs AFAIK. Need to revisit this
3834        * in the future. GNOME #574574
3835        */
3836       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
3837           0);
3838     }
3839
3840     g_signal_handlers_unblock_by_func (priv->settings,
3841       empathy_call_window_prefs_volume_changed_cb, self);
3842 }
3843
3844 static void
3845 empathy_call_window_hangup_cb (gpointer object,
3846     EmpathyCallWindow *self)
3847 {
3848   empathy_call_handler_stop_call (self->priv->handler);
3849
3850   empathy_call_window_disconnected (self, TRUE);
3851 }
3852
3853 static void
3854 empathy_call_window_restart_call (EmpathyCallWindow *window)
3855 {
3856   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3857
3858   /* Remove error info bars */
3859   gtk_container_forall (GTK_CONTAINER (priv->errors_vbox),
3860       (GtkCallback) gtk_widget_destroy, NULL);
3861
3862   create_video_output_widget (window);
3863
3864   /* While the call was disconnected, the input volume might have changed.
3865    * However, since the audio_input source was destroyed, its volume has not
3866    * been updated during that time. That's why we manually update it here */
3867   empathy_call_window_mic_volume_changed (window);
3868
3869   priv->outgoing = TRUE;
3870   empathy_call_window_set_state_connecting (window);
3871
3872   if (priv->pipeline_playing)
3873     start_call (window);
3874   else
3875     /* call will be started when the pipeline is ready */
3876     priv->start_call_when_playing = TRUE;
3877
3878   empathy_call_window_setup_avatars (window, priv->handler);
3879
3880   empathy_call_window_show_hangup_button (window, TRUE);
3881 }
3882
3883 static void
3884 empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
3885     EmpathyCallWindow *window)
3886 {
3887   gboolean active;
3888
3889   active = gtk_toggle_tool_button_get_active (button);
3890
3891   empathy_call_window_show_dialpad (window, active);
3892 }
3893
3894 static void
3895 empathy_call_window_fullscreen_cb (gpointer object,
3896                                    EmpathyCallWindow *window)
3897 {
3898   empathy_call_window_fullscreen_toggle (window);
3899 }
3900
3901 static void
3902 empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window)
3903 {
3904   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3905
3906   if (priv->is_fullscreen)
3907     gtk_window_unfullscreen (GTK_WINDOW (window));
3908   else
3909     gtk_window_fullscreen (GTK_WINDOW (window));
3910 }
3911
3912 static gboolean
3913 empathy_call_window_video_button_press_cb (GtkWidget *video_preview,
3914   GdkEventButton *event, EmpathyCallWindow *window)
3915 {
3916   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3917     {
3918       empathy_call_window_video_menu_popup (window, event->button);
3919       return TRUE;
3920     }
3921
3922   return FALSE;
3923 }
3924
3925 static gboolean
3926 empathy_call_window_key_press_cb (GtkWidget *video_output,
3927   GdkEventKey *event, EmpathyCallWindow *window)
3928 {
3929   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3930
3931   if (priv->is_fullscreen && event->keyval == GDK_KEY_Escape)
3932     {
3933       /* Since we are in fullscreen mode, toggling will bring us back to
3934          normal mode. */
3935       empathy_call_window_fullscreen_toggle (window);