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