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