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