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