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