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