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