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