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