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