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