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