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