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