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