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