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