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