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