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