]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
recycle the audio output gst element
[empathy.git] / src / empathy-call-window.c
1 /*
2  * empathy-call-window.c - Source for EmpathyCallWindow
3  * Copyright (C) 2008-2009 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 <telepathy-farsight/channel.h>
33
34 #include <gst/farsight/fs-element-added-notifier.h>
35
36 #include <libempathy/empathy-tp-contact-factory.h>
37 #include <libempathy/empathy-call-factory.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy-gtk/empathy-avatar-image.h>
40 #include <libempathy-gtk/empathy-video-widget.h>
41 #include <libempathy-gtk/empathy-audio-src.h>
42 #include <libempathy-gtk/empathy-audio-sink.h>
43 #include <libempathy-gtk/empathy-video-src.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
45 #include <libempathy-gtk/empathy-sound.h>
46 #include <libempathy-gtk/empathy-geometry.h>
47
48 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
49 #include <libempathy/empathy-debug.h>
50
51 #include "empathy-call-window.h"
52 #include "empathy-call-window-fullscreen.h"
53 #include "empathy-sidebar.h"
54
55 #define BUTTON_ID "empathy-call-dtmf-button-id"
56
57 #define CONTENT_HBOX_BORDER_WIDTH 6
58 #define CONTENT_HBOX_SPACING 3
59 #define CONTENT_HBOX_CHILDREN_PACKING_PADDING 3
60
61 #define SELF_VIDEO_SECTION_WIDTH 160
62 #define SELF_VIDEO_SECTION_HEIGTH 120
63
64 /* The avatar's default width and height are set to the same value because we
65    want a square icon. */
66 #define REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT
67 #define REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT \
68   EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT
69
70 /* If an video input error occurs, the error message will start with "v4l" */
71 #define VIDEO_INPUT_ERROR_PREFIX "v4l"
72
73 /* The time interval in milliseconds between 2 outgoing rings */
74 #define MS_BETWEEN_RING 500
75
76 G_DEFINE_TYPE(EmpathyCallWindow, empathy_call_window, GTK_TYPE_WINDOW)
77
78 /* signal enum */
79 #if 0
80 enum
81 {
82     LAST_SIGNAL
83 };
84
85 static guint signals[LAST_SIGNAL] = {0};
86 #endif
87
88 enum {
89   PROP_CALL_HANDLER = 1,
90 };
91
92 typedef enum {
93   CONNECTING,
94   CONNECTED,
95   DISCONNECTED,
96   REDIALING
97 } CallState;
98
99 typedef enum {
100   CAMERA_STATE_OFF = 0,
101   CAMERA_STATE_PREVIEW,
102   CAMERA_STATE_ON,
103 } CameraState;
104
105 /* private structure */
106 typedef struct _EmpathyCallWindowPriv EmpathyCallWindowPriv;
107
108 struct _EmpathyCallWindowPriv
109 {
110   gboolean dispose_has_run;
111   EmpathyCallHandler *handler;
112   EmpathyContact *contact;
113
114   guint call_state;
115   gboolean outgoing;
116
117   GtkUIManager *ui_manager;
118   GtkWidget *errors_vbox;
119   /* widget displays the video received from the remote user. This widget is
120    * alive only during call. */
121   GtkWidget *video_output;
122   GtkWidget *video_preview;
123   GtkWidget *remote_user_avatar_widget;
124   GtkWidget *self_user_avatar_widget;
125   GtkWidget *sidebar;
126   GtkWidget *sidebar_button;
127   GtkWidget *statusbar;
128   GtkWidget *volume_button;
129   GtkWidget *redial_button;
130   GtkWidget *mic_button;
131   GtkWidget *toolbar;
132   GtkWidget *pane;
133   GtkAction *redial;
134   GtkAction *menu_fullscreen;
135   GtkAction *action_camera;
136   GtkAction *action_camera_preview;
137   GtkWidget *tool_button_camera_off;
138   GtkWidget *tool_button_camera_preview;
139   GtkWidget *tool_button_camera_on;
140
141   /* The frames and boxes that contain self and remote avatar and video
142      input/output. When we redial, we destroy and re-create the boxes */
143   GtkWidget *remote_user_output_frame;
144   GtkWidget *self_user_output_frame;
145   GtkWidget *remote_user_output_hbox;
146   GtkWidget *self_user_output_hbox;
147
148   /* We keep a reference on the hbox which contains the main content so we can
149      easilly repack everything when toggling fullscreen */
150   GtkWidget *content_hbox;
151
152   /* This vbox is contained in the content_hbox and it contains the
153      self_user_output_frame and the sidebar button. When toggling fullscreen,
154      it needs to be repacked. We keep a reference on it for easier access. */
155   GtkWidget *vbox;
156
157   gulong video_output_motion_handler_id;
158   guint bus_message_source_id;
159
160   gdouble volume;
161   GtkWidget *volume_scale;
162   GtkWidget *volume_progress_bar;
163   GtkAdjustment *audio_input_adj;
164
165   GtkWidget *dtmf_panel;
166
167   GstElement *video_input;
168   GstElement *audio_input;
169   GstElement *audio_output;
170   GstElement *pipeline;
171   GstElement *video_tee;
172
173   GstElement *funnel;
174   GstElement *liveadder;
175
176   FsElementAddedNotifier *fsnotifier;
177
178   guint context_id;
179
180   GTimer *timer;
181   guint timer_id;
182
183   GtkWidget *video_contrast;
184   GtkWidget *video_brightness;
185   GtkWidget *video_gamma;
186
187   GMutex *lock;
188   gboolean call_started;
189   gboolean sending_video;
190   CameraState camera_state;
191
192   EmpathyCallWindowFullscreen *fullscreen;
193   gboolean is_fullscreen;
194
195   /* Those fields represent the state of the window before it actually was in
196      fullscreen mode. */
197   gboolean sidebar_was_visible_before_fs;
198   gint original_width_before_fs;
199   gint original_height_before_fs;
200 };
201
202 #define GET_PRIV(o) \
203   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CALL_WINDOW, \
204     EmpathyCallWindowPriv))
205
206 static void empathy_call_window_realized_cb (GtkWidget *widget,
207   EmpathyCallWindow *window);
208
209 static gboolean empathy_call_window_delete_cb (GtkWidget *widget,
210   GdkEvent *event, EmpathyCallWindow *window);
211
212 static gboolean empathy_call_window_state_event_cb (GtkWidget *widget,
213   GdkEventWindowState *event, EmpathyCallWindow *window);
214
215 static void empathy_call_window_sidebar_toggled_cb (GtkToggleButton *toggle,
216   EmpathyCallWindow *window);
217
218 static void empathy_call_window_set_send_video (EmpathyCallWindow *window,
219   gboolean send);
220
221 static void empathy_call_window_mic_toggled_cb (
222   GtkToggleToolButton *toggle, EmpathyCallWindow *window);
223
224 static void empathy_call_window_sidebar_hidden_cb (EmpathySidebar *sidebar,
225   EmpathyCallWindow *window);
226
227 static void empathy_call_window_sidebar_shown_cb (EmpathySidebar *sidebar,
228   EmpathyCallWindow *window);
229
230 static void empathy_call_window_hangup_cb (gpointer object,
231   EmpathyCallWindow *window);
232
233 static void empathy_call_window_fullscreen_cb (gpointer object,
234   EmpathyCallWindow *window);
235
236 static void empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window);
237
238 static gboolean empathy_call_window_video_button_press_cb (
239   GtkWidget *video_output, GdkEventButton *event, EmpathyCallWindow *window);
240
241 static gboolean empathy_call_window_key_press_cb (GtkWidget *video_output,
242   GdkEventKey *event, EmpathyCallWindow *window);
243
244 static gboolean empathy_call_window_video_output_motion_notify (
245   GtkWidget *widget, GdkEventMotion *event, EmpathyCallWindow *window);
246
247 static void empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
248   guint button);
249
250 static void empathy_call_window_redial_cb (gpointer object,
251   EmpathyCallWindow *window);
252
253 static void empathy_call_window_restart_call (EmpathyCallWindow *window);
254
255 static void empathy_call_window_status_message (EmpathyCallWindow *window,
256   gchar *message);
257
258 static void empathy_call_window_update_avatars_visibility (EmpathyTpCall *call,
259   EmpathyCallWindow *window);
260
261 static gboolean empathy_call_window_bus_message (GstBus *bus,
262   GstMessage *message, gpointer user_data);
263
264 static void
265 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
266   gdouble value, EmpathyCallWindow *window);
267
268 static void block_camera_control_signals (EmpathyCallWindow *self);
269 static void unblock_camera_control_signals (EmpathyCallWindow *self);
270
271 static void
272 empathy_call_window_setup_toolbar (EmpathyCallWindow *self)
273 {
274   EmpathyCallWindowPriv *priv = GET_PRIV (self);
275   GtkToolItem *tool_item;
276   GtkWidget *camera_off_icon;
277   GdkPixbuf *pixbuf, *modded_pixbuf;
278
279   /* set the icon of the 'camera off' button by greying off the webcam icon */
280   pixbuf = empathy_pixbuf_from_icon_name ("camera-web",
281       GTK_ICON_SIZE_SMALL_TOOLBAR);
282
283   modded_pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
284       gdk_pixbuf_get_width (pixbuf),
285       gdk_pixbuf_get_height (pixbuf));
286
287   gdk_pixbuf_saturate_and_pixelate (pixbuf, modded_pixbuf, 1.0, TRUE);
288   g_object_unref (pixbuf);
289
290   camera_off_icon = gtk_image_new_from_pixbuf (modded_pixbuf);
291   g_object_unref (modded_pixbuf);
292   gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (
293         priv->tool_button_camera_off), camera_off_icon);
294
295   /* Add an empty expanded GtkToolItem so the volume button is at the end of
296    * the toolbar. */
297   tool_item = gtk_tool_item_new ();
298   gtk_tool_item_set_expand (tool_item, TRUE);
299   gtk_widget_show (GTK_WIDGET (tool_item));
300   gtk_toolbar_insert (GTK_TOOLBAR (priv->toolbar), tool_item, -1);
301
302   priv->volume_button = gtk_volume_button_new ();
303   /* FIXME listen to the audiosinks signals and update the button according to
304    * that, for now starting out at 1.0 and assuming only the app changes the
305    * volume will do */
306   gtk_scale_button_set_value (GTK_SCALE_BUTTON (priv->volume_button), 1.0);
307   g_signal_connect (G_OBJECT (priv->volume_button), "value-changed",
308     G_CALLBACK (empathy_call_window_volume_changed_cb), self);
309
310   tool_item = gtk_tool_item_new ();
311   gtk_container_add (GTK_CONTAINER (tool_item), priv->volume_button);
312   gtk_widget_show_all (GTK_WIDGET (tool_item));
313   gtk_toolbar_insert (GTK_TOOLBAR (priv->toolbar), tool_item, -1);
314 }
315
316 static void
317 dtmf_button_pressed_cb (GtkButton *button, EmpathyCallWindow *window)
318 {
319   EmpathyCallWindowPriv *priv = GET_PRIV (window);
320   EmpathyTpCall *call;
321   GQuark button_quark;
322   TpDTMFEvent event;
323
324   g_object_get (priv->handler, "tp-call", &call, NULL);
325
326   button_quark = g_quark_from_static_string (BUTTON_ID);
327   event = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (button),
328     button_quark));
329
330   empathy_tp_call_start_tone (call, event);
331
332   g_object_unref (call);
333 }
334
335 static void
336 dtmf_button_released_cb (GtkButton *button, EmpathyCallWindow *window)
337 {
338   EmpathyCallWindowPriv *priv = GET_PRIV (window);
339   EmpathyTpCall *call;
340
341   g_object_get (priv->handler, "tp-call", &call, NULL);
342
343   empathy_tp_call_stop_tone (call);
344
345   g_object_unref (call);
346 }
347
348 static GtkWidget *
349 empathy_call_window_create_dtmf (EmpathyCallWindow *self)
350 {
351   GtkWidget *table;
352   int i;
353   GQuark button_quark;
354   struct {
355     gchar *label;
356     TpDTMFEvent event;
357   } dtmfbuttons[] = { { "1", TP_DTMF_EVENT_DIGIT_1 },
358                       { "2", TP_DTMF_EVENT_DIGIT_2 },
359                       { "3", TP_DTMF_EVENT_DIGIT_3 },
360                       { "4", TP_DTMF_EVENT_DIGIT_4 },
361                       { "5", TP_DTMF_EVENT_DIGIT_5 },
362                       { "6", TP_DTMF_EVENT_DIGIT_6 },
363                       { "7", TP_DTMF_EVENT_DIGIT_7 },
364                       { "8", TP_DTMF_EVENT_DIGIT_8 },
365                       { "9", TP_DTMF_EVENT_DIGIT_9 },
366                       { "#", TP_DTMF_EVENT_HASH },
367                       { "0", TP_DTMF_EVENT_DIGIT_0 },
368                       { "*", TP_DTMF_EVENT_ASTERISK },
369                       { NULL, } };
370
371   button_quark = g_quark_from_static_string (BUTTON_ID);
372
373   table = gtk_table_new (4, 3, TRUE);
374
375   for (i = 0; dtmfbuttons[i].label != NULL; i++)
376     {
377       GtkWidget *button = gtk_button_new_with_label (dtmfbuttons[i].label);
378       gtk_table_attach (GTK_TABLE (table), button, i % 3, i % 3 + 1,
379         i/3, i/3 + 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 1, 1);
380
381       g_object_set_qdata (G_OBJECT (button), button_quark,
382         GUINT_TO_POINTER (dtmfbuttons[i].event));
383
384       g_signal_connect (G_OBJECT (button), "pressed",
385         G_CALLBACK (dtmf_button_pressed_cb), self);
386       g_signal_connect (G_OBJECT (button), "released",
387         G_CALLBACK (dtmf_button_released_cb), self);
388     }
389
390   return table;
391 }
392
393 static GtkWidget *
394 empathy_call_window_create_video_input_add_slider (EmpathyCallWindow *self,
395   gchar *label_text, GtkWidget *bin)
396 {
397    GtkWidget *vbox = gtk_vbox_new (FALSE, 2);
398    GtkWidget *scale = gtk_vscale_new_with_range (0, 100, 10);
399    GtkWidget *label = gtk_label_new (label_text);
400
401    gtk_widget_set_sensitive (scale, FALSE);
402
403    gtk_container_add (GTK_CONTAINER (bin), vbox);
404
405    gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
406    gtk_box_pack_start (GTK_BOX (vbox), scale, TRUE, TRUE, 0);
407    gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
408
409    return scale;
410 }
411
412 static void
413 empathy_call_window_video_contrast_changed_cb (GtkAdjustment *adj,
414   EmpathyCallWindow *self)
415
416 {
417   EmpathyCallWindowPriv *priv = GET_PRIV (self);
418
419   empathy_video_src_set_channel (priv->video_input,
420     EMPATHY_GST_VIDEO_SRC_CHANNEL_CONTRAST, gtk_adjustment_get_value (adj));
421 }
422
423 static void
424 empathy_call_window_video_brightness_changed_cb (GtkAdjustment *adj,
425   EmpathyCallWindow *self)
426
427 {
428   EmpathyCallWindowPriv *priv = GET_PRIV (self);
429
430   empathy_video_src_set_channel (priv->video_input,
431     EMPATHY_GST_VIDEO_SRC_CHANNEL_BRIGHTNESS, gtk_adjustment_get_value (adj));
432 }
433
434 static void
435 empathy_call_window_video_gamma_changed_cb (GtkAdjustment *adj,
436   EmpathyCallWindow *self)
437
438 {
439   EmpathyCallWindowPriv *priv = GET_PRIV (self);
440
441   empathy_video_src_set_channel (priv->video_input,
442     EMPATHY_GST_VIDEO_SRC_CHANNEL_GAMMA, gtk_adjustment_get_value (adj));
443 }
444
445
446 static GtkWidget *
447 empathy_call_window_create_video_input (EmpathyCallWindow *self)
448 {
449   EmpathyCallWindowPriv *priv = GET_PRIV (self);
450   GtkWidget *hbox;
451
452   hbox = gtk_hbox_new (TRUE, 3);
453
454   priv->video_contrast = empathy_call_window_create_video_input_add_slider (
455     self,  _("Contrast"), hbox);
456
457   priv->video_brightness = empathy_call_window_create_video_input_add_slider (
458     self,  _("Brightness"), hbox);
459
460   priv->video_gamma = empathy_call_window_create_video_input_add_slider (
461     self,  _("Gamma"), hbox);
462
463   return hbox;
464 }
465
466 static void
467 empathy_call_window_setup_video_input (EmpathyCallWindow *self)
468 {
469   EmpathyCallWindowPriv *priv = GET_PRIV (self);
470   guint supported;
471   GtkAdjustment *adj;
472
473   supported = empathy_video_src_get_supported_channels (priv->video_input);
474
475   if (supported & EMPATHY_GST_VIDEO_SRC_SUPPORTS_CONTRAST)
476     {
477       adj = gtk_range_get_adjustment (GTK_RANGE (priv->video_contrast));
478
479       gtk_adjustment_set_value (adj,
480         empathy_video_src_get_channel (priv->video_input,
481           EMPATHY_GST_VIDEO_SRC_CHANNEL_CONTRAST));
482
483       g_signal_connect (G_OBJECT (adj), "value-changed",
484         G_CALLBACK (empathy_call_window_video_contrast_changed_cb), self);
485
486       gtk_widget_set_sensitive (priv->video_contrast, TRUE);
487     }
488
489   if (supported & EMPATHY_GST_VIDEO_SRC_SUPPORTS_BRIGHTNESS)
490     {
491       adj = gtk_range_get_adjustment (GTK_RANGE (priv->video_brightness));
492
493       gtk_adjustment_set_value (adj,
494         empathy_video_src_get_channel (priv->video_input,
495           EMPATHY_GST_VIDEO_SRC_CHANNEL_BRIGHTNESS));
496
497       g_signal_connect (G_OBJECT (adj), "value-changed",
498         G_CALLBACK (empathy_call_window_video_brightness_changed_cb), self);
499       gtk_widget_set_sensitive (priv->video_brightness, TRUE);
500     }
501
502   if (supported & EMPATHY_GST_VIDEO_SRC_SUPPORTS_GAMMA)
503     {
504       adj = gtk_range_get_adjustment (GTK_RANGE (priv->video_gamma));
505
506       gtk_adjustment_set_value (adj,
507         empathy_video_src_get_channel (priv->video_input,
508           EMPATHY_GST_VIDEO_SRC_CHANNEL_GAMMA));
509
510       g_signal_connect (G_OBJECT (adj), "value-changed",
511         G_CALLBACK (empathy_call_window_video_gamma_changed_cb), self);
512       gtk_widget_set_sensitive (priv->video_gamma, TRUE);
513     }
514 }
515
516 static void
517 empathy_call_window_mic_volume_changed_cb (GtkAdjustment *adj,
518   EmpathyCallWindow *self)
519 {
520   EmpathyCallWindowPriv *priv = GET_PRIV (self);
521   gdouble volume;
522
523   if (priv->audio_input == NULL)
524     return;
525
526   volume = gtk_adjustment_get_value (adj)/100.0;
527
528   /* Don't store the volume because of muting */
529   if (volume > 0 || gtk_toggle_tool_button_get_active (
530         GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
531     priv->volume = volume;
532
533   /* Ensure that the toggle button is active if the volume is > 0 and inactive
534    * if it's smaller than 0 */
535   if ((volume > 0) != gtk_toggle_tool_button_get_active (
536         GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
537     gtk_toggle_tool_button_set_active (
538       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), volume > 0);
539
540   empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (priv->audio_input),
541     volume);
542 }
543
544 static void
545 empathy_call_window_audio_input_level_changed_cb (EmpathyGstAudioSrc *src,
546   gdouble level, EmpathyCallWindow *window)
547 {
548   gdouble value;
549   EmpathyCallWindowPriv *priv = GET_PRIV (window);
550
551   value = CLAMP (pow (10, level / 20), 0.0, 1.0);
552   gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->volume_progress_bar),
553       value);
554 }
555
556 static GtkWidget *
557 empathy_call_window_create_audio_input (EmpathyCallWindow *self)
558 {
559   EmpathyCallWindowPriv *priv = GET_PRIV (self);
560   GtkWidget *hbox, *vbox, *label;
561
562   hbox = gtk_hbox_new (TRUE, 3);
563
564   vbox = gtk_vbox_new (FALSE, 3);
565   gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 3);
566
567   priv->volume_scale = gtk_vscale_new_with_range (0, 150, 100);
568   gtk_range_set_inverted (GTK_RANGE (priv->volume_scale), TRUE);
569   label = gtk_label_new (_("Volume"));
570
571   priv->audio_input_adj = gtk_range_get_adjustment (
572     GTK_RANGE (priv->volume_scale));
573   priv->volume =  empathy_audio_src_get_volume (EMPATHY_GST_AUDIO_SRC
574     (priv->audio_input));
575   gtk_adjustment_set_value (priv->audio_input_adj, priv->volume * 100);
576
577   g_signal_connect (G_OBJECT (priv->audio_input_adj), "value-changed",
578     G_CALLBACK (empathy_call_window_mic_volume_changed_cb), self);
579
580   gtk_box_pack_start (GTK_BOX (vbox), priv->volume_scale, TRUE, TRUE, 3);
581   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 3);
582
583   priv->volume_progress_bar = gtk_progress_bar_new ();
584   gtk_progress_bar_set_orientation (
585       GTK_PROGRESS_BAR (priv->volume_progress_bar),
586       GTK_PROGRESS_BOTTOM_TO_TOP);
587   gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (priv->volume_progress_bar),
588       0);
589
590   gtk_box_pack_start (GTK_BOX (hbox), priv->volume_progress_bar, FALSE, FALSE,
591       3);
592
593   return hbox;
594 }
595
596 static void
597 initialize_output_elements (GstBus *bus, EmpathyCallWindow *self)
598 {
599   EmpathyCallWindowPriv *priv = GET_PRIV (self);
600
601   g_assert (priv->video_output == NULL);
602   priv->video_output = empathy_video_widget_new (bus);
603
604   gtk_box_pack_start (GTK_BOX (priv->remote_user_output_hbox),
605       priv->video_output, TRUE, TRUE, 0);
606
607   gtk_widget_add_events (priv->video_output,
608       GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK);
609   g_signal_connect (G_OBJECT (priv->video_output), "button-press-event",
610       G_CALLBACK (empathy_call_window_video_button_press_cb), self);
611 }
612
613 static void
614 create_audio_output (EmpathyCallWindow *self)
615 {
616   EmpathyCallWindowPriv *priv = GET_PRIV (self);
617
618   g_assert (priv->audio_output == NULL);
619   priv->audio_output = empathy_audio_sink_new ();
620   gst_object_ref (priv->audio_output);
621   gst_object_sink (priv->audio_output);
622 }
623
624 static void
625 initialize_input_elements (GstBus *bus, EmpathyCallWindow *self)
626 {
627   EmpathyCallWindowPriv *priv = GET_PRIV (self);
628
629   priv->video_input = empathy_video_src_new ();
630   gst_object_ref (priv->video_input);
631   gst_object_sink (priv->video_input);
632
633   priv->audio_input = empathy_audio_src_new ();
634   gst_object_ref (priv->audio_input);
635   gst_object_sink (priv->audio_input);
636
637   /* The preview widget is initialized when the "video preview" button
638    * is activated */
639
640   empathy_signal_connect_weak (priv->audio_input, "peak-level-changed",
641     G_CALLBACK (empathy_call_window_audio_input_level_changed_cb),
642     G_OBJECT (self));
643 }
644
645 static void
646 empathy_call_window_setup_video_preview (EmpathyCallWindow *window)
647 {
648   EmpathyCallWindowPriv *priv = GET_PRIV (window);
649   GstElement *preview;
650   GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
651
652   if (priv->video_preview != NULL)
653     {
654       /* Since the video preview and the video tee are initialized and freed
655          at the same time, if one is initialized, then the other one should
656          be too. */
657       g_assert (priv->video_tee != NULL);
658       return;
659     }
660
661   DEBUG ("Create video preview");
662   g_assert (priv->video_tee == NULL);
663
664   priv->video_tee = gst_element_factory_make ("tee", NULL);
665   gst_object_ref (priv->video_tee);
666   gst_object_sink (priv->video_tee);
667
668   priv->video_preview = empathy_video_widget_new_with_size (bus,
669       SELF_VIDEO_SECTION_WIDTH, SELF_VIDEO_SECTION_HEIGTH);
670   g_object_set (priv->video_preview, "sync", FALSE, "async", TRUE, NULL);
671   gtk_box_pack_start (GTK_BOX (priv->self_user_output_hbox),
672       priv->video_preview, TRUE, TRUE, 0);
673
674   preview = empathy_video_widget_get_element (
675       EMPATHY_VIDEO_WIDGET (priv->video_preview));
676   gst_bin_add_many (GST_BIN (priv->pipeline), priv->video_input,
677       priv->video_tee, preview, NULL);
678   gst_element_link_many (priv->video_input, priv->video_tee,
679       preview, NULL);
680
681   g_object_unref (bus);
682
683   gst_element_set_state (preview, GST_STATE_PLAYING);
684   gst_element_set_state (priv->video_input, GST_STATE_PLAYING);
685   gst_element_set_state (priv->video_tee, GST_STATE_PLAYING);
686 }
687
688 static void
689 display_video_preview (EmpathyCallWindow *self,
690     gboolean display)
691 {
692   EmpathyCallWindowPriv *priv = GET_PRIV (self);
693
694   if (display)
695     {
696       /* Display the preview and hide the self avatar */
697       DEBUG ("Show video preview");
698
699       if (priv->video_preview == NULL)
700         empathy_call_window_setup_video_preview (self);
701       gtk_widget_show (priv->video_preview);
702       gtk_widget_hide (priv->self_user_avatar_widget);
703     }
704   else
705     {
706       /* Display the self avatar and hide the preview */
707       DEBUG ("Show self avatar");
708
709       if (priv->video_preview != NULL)
710         gtk_widget_hide (priv->video_preview);
711       gtk_widget_show (priv->self_user_avatar_widget);
712     }
713 }
714
715 static void
716 empathy_call_window_set_state_connecting (EmpathyCallWindow *window)
717 {
718   EmpathyCallWindowPriv *priv = GET_PRIV (window);
719
720   empathy_call_window_status_message (window, _("Connecting…"));
721   priv->call_state = CONNECTING;
722
723   if (priv->outgoing)
724     empathy_sound_start_playing (GTK_WIDGET (window),
725         EMPATHY_SOUND_PHONE_OUTGOING, MS_BETWEEN_RING);
726 }
727
728 static void
729 disable_camera (EmpathyCallWindow *self)
730 {
731   EmpathyCallWindowPriv *priv = GET_PRIV (self);
732
733   if (priv->camera_state == CAMERA_STATE_OFF)
734     return;
735
736   DEBUG ("Disable camera");
737
738   display_video_preview (self, FALSE);
739
740   if (priv->camera_state == CAMERA_STATE_ON)
741     empathy_call_window_set_send_video (self, FALSE);
742
743   block_camera_control_signals (self);
744   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
745         priv->tool_button_camera_on), FALSE);
746   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
747       priv->tool_button_camera_preview), FALSE);
748
749   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
750       priv->tool_button_camera_off), TRUE);
751   gtk_radio_action_set_current_value (GTK_RADIO_ACTION (priv->action_camera),
752       CAMERA_STATE_OFF);
753   unblock_camera_control_signals (self);
754
755   priv->camera_state = CAMERA_STATE_OFF;
756 }
757
758 static void
759 tool_button_camera_off_toggled_cb (GtkToggleToolButton *toggle,
760   EmpathyCallWindow *self)
761 {
762   EmpathyCallWindowPriv *priv = GET_PRIV (self);
763
764   if (!gtk_toggle_tool_button_get_active (toggle))
765     {
766       if (priv->camera_state == CAMERA_STATE_OFF)
767         {
768           /* We can't change the state by disabling the button */
769           block_camera_control_signals (self);
770           gtk_toggle_tool_button_set_active (toggle, TRUE);
771           unblock_camera_control_signals (self);
772         }
773
774       return;
775     }
776
777   disable_camera (self);
778 }
779
780 static void
781 enable_preview (EmpathyCallWindow *self)
782 {
783   EmpathyCallWindowPriv *priv = GET_PRIV (self);
784
785   if (priv->camera_state == CAMERA_STATE_PREVIEW)
786     return;
787
788   DEBUG ("Enable preview");
789
790   if (priv->camera_state == CAMERA_STATE_ON)
791     /* preview is already displayed so we just have to stop sending */
792     empathy_call_window_set_send_video (self, FALSE);
793
794   display_video_preview (self, TRUE);
795
796   block_camera_control_signals (self);
797   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
798       priv->tool_button_camera_off), FALSE);
799   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
800         priv->tool_button_camera_on), FALSE);
801
802   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
803         priv->tool_button_camera_preview), TRUE);
804   gtk_radio_action_set_current_value (GTK_RADIO_ACTION (priv->action_camera),
805       CAMERA_STATE_PREVIEW);
806   unblock_camera_control_signals (self);
807
808   priv->camera_state = CAMERA_STATE_PREVIEW;
809 }
810
811 static void
812 tool_button_camera_preview_toggled_cb (GtkToggleToolButton *toggle,
813   EmpathyCallWindow *self)
814 {
815   EmpathyCallWindowPriv *priv = GET_PRIV (self);
816
817   if (!gtk_toggle_tool_button_get_active (toggle))
818     {
819       if (priv->camera_state == CAMERA_STATE_PREVIEW)
820         {
821           /* We can't change the state by disabling the button */
822           block_camera_control_signals (self);
823           gtk_toggle_tool_button_set_active (toggle, TRUE);
824           unblock_camera_control_signals (self);
825         }
826
827       return;
828     }
829
830   enable_preview (self);
831 }
832
833 static void
834 enable_camera (EmpathyCallWindow *self)
835 {
836   EmpathyCallWindowPriv *priv = GET_PRIV (self);
837
838   if (priv->camera_state == CAMERA_STATE_ON)
839     return;
840
841   DEBUG ("Enable camera");
842
843   empathy_call_window_set_send_video (self, TRUE);
844
845   block_camera_control_signals (self);
846   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
847       priv->tool_button_camera_off), FALSE);
848   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
849         priv->tool_button_camera_preview), FALSE);
850
851   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (
852       priv->tool_button_camera_on), TRUE);
853   gtk_radio_action_set_current_value (GTK_RADIO_ACTION (priv->action_camera),
854       CAMERA_STATE_ON);
855   unblock_camera_control_signals (self);
856
857   priv->camera_state = CAMERA_STATE_ON;
858 }
859
860 static void
861 tool_button_camera_on_toggled_cb (GtkToggleToolButton *toggle,
862   EmpathyCallWindow *self)
863 {
864   EmpathyCallWindowPriv *priv = GET_PRIV (self);
865
866   if (!gtk_toggle_tool_button_get_active (toggle))
867     {
868       if (priv->camera_state == CAMERA_STATE_ON)
869         {
870           /* We can't change the state by disabling the button */
871           block_camera_control_signals (self);
872           gtk_toggle_tool_button_set_active (toggle, TRUE);
873           unblock_camera_control_signals (self);
874         }
875
876       return;
877     }
878
879   enable_camera (self);
880 }
881
882 static void
883 action_camera_change_cb (GtkRadioAction *action,
884     GtkRadioAction *current,
885     EmpathyCallWindow *self)
886 {
887   CameraState state;
888
889   state = gtk_radio_action_get_current_value (current);
890
891   switch (state)
892     {
893       case CAMERA_STATE_OFF:
894         disable_camera (self);
895         break;
896
897       case CAMERA_STATE_PREVIEW:
898         enable_preview (self);
899         break;
900
901       case CAMERA_STATE_ON:
902         enable_camera (self);
903         break;
904
905       default:
906         g_assert_not_reached ();
907     }
908 }
909
910 static void
911 create_pipeline (EmpathyCallWindow *self)
912 {
913   EmpathyCallWindowPriv *priv = GET_PRIV (self);
914   GstBus *bus;
915
916   g_assert (priv->pipeline == NULL);
917
918   priv->pipeline = gst_pipeline_new (NULL);
919   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
920   priv->bus_message_source_id = gst_bus_add_watch (bus,
921       empathy_call_window_bus_message, self);
922
923   initialize_output_elements (bus, self);
924   initialize_input_elements (bus, self);
925
926   g_object_unref (bus);
927 }
928
929
930 static void
931 empathy_call_window_init (EmpathyCallWindow *self)
932 {
933   EmpathyCallWindowPriv *priv = GET_PRIV (self);
934   GtkBuilder *gui;
935   GtkWidget *top_vbox;
936   GtkWidget *h;
937   GtkWidget *arrow;
938   GtkWidget *page;
939   gchar *filename;
940   GKeyFile *keyfile;
941   GError *error = NULL;
942
943   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
944   gui = empathy_builder_get_file (filename,
945     "call_window_vbox", &top_vbox,
946     "errors_vbox", &priv->errors_vbox,
947     "pane", &priv->pane,
948     "statusbar", &priv->statusbar,
949     "redial", &priv->redial_button,
950     "microphone", &priv->mic_button,
951     "toolbar", &priv->toolbar,
952     "menuredial", &priv->redial,
953     "ui_manager", &priv->ui_manager,
954     "menufullscreen", &priv->menu_fullscreen,
955     "camera_off", &priv->tool_button_camera_off,
956     "camera_preview", &priv->tool_button_camera_preview,
957     "camera_on", &priv->tool_button_camera_on,
958     "action_camera_off",  &priv->action_camera,
959     "action_camera_preview",  &priv->action_camera_preview,
960     NULL);
961   g_free (filename);
962
963   empathy_builder_connect (gui, self,
964     "menuhangup", "activate", empathy_call_window_hangup_cb,
965     "hangup", "clicked", empathy_call_window_hangup_cb,
966     "menuredial", "activate", empathy_call_window_redial_cb,
967     "redial", "clicked", empathy_call_window_redial_cb,
968     "microphone", "toggled", empathy_call_window_mic_toggled_cb,
969     "menufullscreen", "activate", empathy_call_window_fullscreen_cb,
970     "camera_off", "toggled", tool_button_camera_off_toggled_cb,
971     "camera_preview", "toggled", tool_button_camera_preview_toggled_cb,
972     "camera_on", "toggled", tool_button_camera_on_toggled_cb,
973     "action_camera_off", "changed", action_camera_change_cb,
974     NULL);
975
976   priv->lock = g_mutex_new ();
977
978   gtk_container_add (GTK_CONTAINER (self), top_vbox);
979
980   priv->content_hbox = gtk_hbox_new (FALSE, CONTENT_HBOX_SPACING);
981   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
982                                   CONTENT_HBOX_BORDER_WIDTH);
983   gtk_paned_pack1 (GTK_PANED (priv->pane), priv->content_hbox, TRUE, FALSE);
984
985   /* remote user output frame */
986   priv->remote_user_output_frame = gtk_frame_new (NULL);
987   gtk_widget_set_size_request (priv->remote_user_output_frame,
988       EMPATHY_VIDEO_WIDGET_DEFAULT_WIDTH, EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT);
989   gtk_box_pack_start (GTK_BOX (priv->content_hbox),
990       priv->remote_user_output_frame, TRUE, TRUE,
991       CONTENT_HBOX_CHILDREN_PACKING_PADDING);
992
993   priv->remote_user_output_hbox = gtk_hbox_new (FALSE, 0);
994
995   priv->remote_user_avatar_widget = gtk_image_new ();
996
997   gtk_box_pack_start (GTK_BOX (priv->remote_user_output_hbox),
998       priv->remote_user_avatar_widget, TRUE, TRUE, 0);
999
1000   gtk_container_add (GTK_CONTAINER (priv->remote_user_output_frame),
1001       priv->remote_user_output_hbox);
1002
1003   /* self user output frame */
1004   priv->self_user_output_frame = gtk_frame_new (NULL);
1005   gtk_widget_set_size_request (priv->self_user_output_frame,
1006       SELF_VIDEO_SECTION_WIDTH, SELF_VIDEO_SECTION_HEIGTH);
1007
1008   priv->self_user_output_hbox = gtk_hbox_new (FALSE, 0);
1009
1010   priv->self_user_avatar_widget = gtk_image_new ();
1011   gtk_box_pack_start (GTK_BOX (priv->self_user_output_hbox),
1012       priv->self_user_avatar_widget, TRUE, TRUE, 0);
1013
1014   gtk_container_add (GTK_CONTAINER (priv->self_user_output_frame),
1015       priv->self_user_output_hbox);
1016
1017   create_pipeline (self);
1018   create_audio_output (self);
1019
1020   priv->fsnotifier = fs_element_added_notifier_new ();
1021   fs_element_added_notifier_add (priv->fsnotifier, GST_BIN (priv->pipeline));
1022
1023   keyfile = g_key_file_new ();
1024   filename = empathy_file_lookup ("element-properties", "data");
1025   if (g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, &error))
1026     {
1027       fs_element_added_notifier_set_properties_from_keyfile (priv->fsnotifier,
1028           keyfile);
1029     }
1030   else
1031     {
1032       g_warning ("Could not load element-properties file: %s", error->message);
1033       g_key_file_free (keyfile);
1034       g_clear_error (&error);
1035     }
1036   g_free (filename);
1037
1038   priv->vbox = gtk_vbox_new (FALSE, 3);
1039   gtk_box_pack_start (GTK_BOX (priv->content_hbox), priv->vbox,
1040       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
1041   gtk_box_pack_start (GTK_BOX (priv->vbox), priv->self_user_output_frame,
1042       FALSE, FALSE, 0);
1043
1044   empathy_call_window_setup_toolbar (self);
1045
1046   priv->sidebar_button = gtk_toggle_button_new_with_mnemonic (_("_Sidebar"));
1047   arrow = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
1048   g_signal_connect (G_OBJECT (priv->sidebar_button), "toggled",
1049     G_CALLBACK (empathy_call_window_sidebar_toggled_cb), self);
1050
1051   gtk_button_set_image (GTK_BUTTON (priv->sidebar_button), arrow);
1052
1053   h = gtk_hbox_new (FALSE, 3);
1054   gtk_box_pack_end (GTK_BOX (priv->vbox), h, FALSE, FALSE, 3);
1055   gtk_box_pack_end (GTK_BOX (h), priv->sidebar_button, FALSE, FALSE, 3);
1056
1057   priv->sidebar = empathy_sidebar_new ();
1058   g_signal_connect (G_OBJECT (priv->sidebar),
1059     "hide", G_CALLBACK (empathy_call_window_sidebar_hidden_cb), self);
1060   g_signal_connect (G_OBJECT (priv->sidebar),
1061     "show", G_CALLBACK (empathy_call_window_sidebar_shown_cb), self);
1062   gtk_paned_pack2 (GTK_PANED (priv->pane), priv->sidebar, FALSE, FALSE);
1063
1064   page = empathy_call_window_create_audio_input (self);
1065   empathy_sidebar_add_page (EMPATHY_SIDEBAR (priv->sidebar), _("Audio input"),
1066     page);
1067
1068   page = empathy_call_window_create_video_input (self);
1069   empathy_sidebar_add_page (EMPATHY_SIDEBAR (priv->sidebar), _("Video input"),
1070     page);
1071
1072   priv->dtmf_panel = empathy_call_window_create_dtmf (self);
1073   empathy_sidebar_add_page (EMPATHY_SIDEBAR (priv->sidebar), _("Dialpad"),
1074     priv->dtmf_panel);
1075
1076   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
1077
1078
1079   gtk_widget_show_all (top_vbox);
1080
1081   gtk_widget_hide (priv->sidebar);
1082
1083   priv->fullscreen = empathy_call_window_fullscreen_new (self);
1084   empathy_call_window_fullscreen_set_video_widget (priv->fullscreen,
1085       priv->video_output);
1086   g_signal_connect (G_OBJECT (priv->fullscreen->leave_fullscreen_button),
1087       "clicked", G_CALLBACK (empathy_call_window_fullscreen_cb), self);
1088
1089   g_signal_connect (G_OBJECT (self), "realize",
1090     G_CALLBACK (empathy_call_window_realized_cb), self);
1091
1092   g_signal_connect (G_OBJECT (self), "delete-event",
1093     G_CALLBACK (empathy_call_window_delete_cb), self);
1094
1095   g_signal_connect (G_OBJECT (self), "window-state-event",
1096     G_CALLBACK (empathy_call_window_state_event_cb), self);
1097
1098   g_signal_connect (G_OBJECT (self), "key-press-event",
1099       G_CALLBACK (empathy_call_window_key_press_cb), self);
1100
1101   priv->timer = g_timer_new ();
1102
1103   g_object_ref (priv->ui_manager);
1104   g_object_unref (gui);
1105
1106   empathy_geometry_bind (GTK_WINDOW (self), "call-window");
1107 }
1108
1109 /* Instead of specifying a width and a height, we specify only one size. That's
1110    because we want a square avatar icon.  */
1111 static void
1112 init_contact_avatar_with_size (EmpathyContact *contact,
1113     GtkWidget *image_widget,
1114     gint size)
1115 {
1116   GdkPixbuf *pixbuf_avatar = NULL;
1117
1118   if (contact != NULL)
1119     {
1120       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
1121         size, size);
1122     }
1123
1124   if (pixbuf_avatar == NULL)
1125     {
1126       pixbuf_avatar = empathy_pixbuf_from_icon_name_sized ("stock_person",
1127           size);
1128     }
1129
1130   gtk_image_set_from_pixbuf (GTK_IMAGE (image_widget), pixbuf_avatar);
1131 }
1132
1133 static void
1134 set_window_title (EmpathyCallWindow *self)
1135 {
1136   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1137   gchar *tmp;
1138
1139   /* translators: Call is a noun and %s is the contact name. This string
1140    * is used in the window title */
1141   tmp = g_strdup_printf (_("Call with %s"),
1142       empathy_contact_get_name (priv->contact));
1143   gtk_window_set_title (GTK_WINDOW (self), tmp);
1144   g_free (tmp);
1145 }
1146
1147 static void
1148 contact_name_changed_cb (EmpathyContact *contact,
1149     GParamSpec *pspec, EmpathyCallWindow *self)
1150 {
1151   set_window_title (self);
1152 }
1153
1154 static void
1155 contact_avatar_changed_cb (EmpathyContact *contact,
1156     GParamSpec *pspec, GtkWidget *avatar_widget)
1157 {
1158   int size;
1159
1160   size = avatar_widget->allocation.height;
1161
1162   if (size == 0)
1163     {
1164       /* the widget is not allocated yet, set a default size */
1165       size = MIN (REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT,
1166           REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH);
1167     }
1168
1169   init_contact_avatar_with_size (contact, avatar_widget, size);
1170 }
1171
1172 static void
1173 empathy_call_window_got_self_contact_cb (EmpathyTpContactFactory *factory,
1174     EmpathyContact *contact, const GError *error, gpointer user_data,
1175     GObject *weak_object)
1176 {
1177   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1178   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1179
1180   init_contact_avatar_with_size (contact, priv->self_user_avatar_widget,
1181       MIN (SELF_VIDEO_SECTION_WIDTH, SELF_VIDEO_SECTION_HEIGTH));
1182
1183   g_signal_connect (contact, "notify::avatar",
1184       G_CALLBACK (contact_avatar_changed_cb), priv->self_user_avatar_widget);
1185 }
1186
1187 static void
1188 empathy_call_window_setup_avatars (EmpathyCallWindow *self,
1189     EmpathyCallHandler *handler)
1190 {
1191   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1192
1193   g_object_get (handler, "contact", &(priv->contact), NULL);
1194
1195   if (priv->contact != NULL)
1196     {
1197       TpConnection *connection;
1198       EmpathyTpContactFactory *factory;
1199
1200       set_window_title (self);
1201
1202       g_signal_connect (priv->contact, "notify::name",
1203           G_CALLBACK (contact_name_changed_cb), self);
1204       g_signal_connect (priv->contact, "notify::avatar",
1205           G_CALLBACK (contact_avatar_changed_cb),
1206           priv->remote_user_avatar_widget);
1207
1208       /* Retreiving the self avatar */
1209       connection = empathy_contact_get_connection (priv->contact);
1210       factory = empathy_tp_contact_factory_dup_singleton (connection);
1211       empathy_tp_contact_factory_get_from_handle (factory,
1212           tp_connection_get_self_handle (connection),
1213           empathy_call_window_got_self_contact_cb, self, NULL, G_OBJECT (self));
1214
1215       g_object_unref (factory);
1216     }
1217   else
1218     {
1219       g_warning ("call handler doesn't have a contact");
1220       /* translators: Call is a noun. This string is used in the window
1221        * title */
1222       gtk_window_set_title (GTK_WINDOW (self), _("Call"));
1223
1224       /* Since we can't access the remote contact, we can't get a connection
1225          to it and can't get the self contact (and its avatar). This means
1226          that we have to manually set the self avatar. */
1227       init_contact_avatar_with_size (NULL, priv->self_user_avatar_widget,
1228           MIN (SELF_VIDEO_SECTION_WIDTH, SELF_VIDEO_SECTION_HEIGTH));
1229     }
1230
1231   init_contact_avatar_with_size (priv->contact,
1232       priv->remote_user_avatar_widget,
1233       MIN (REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH,
1234           REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT));
1235
1236   /* The remote avatar is shown by default and will be hidden when we receive
1237      video from the remote side. */
1238   gtk_widget_hide (priv->video_output);
1239   gtk_widget_show (priv->remote_user_avatar_widget);
1240 }
1241
1242 static void
1243 empathy_call_window_constructed (GObject *object)
1244 {
1245   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1246   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1247   EmpathyTpCall *call;
1248
1249   g_assert (priv->handler != NULL);
1250
1251   g_object_get (priv->handler, "tp-call", &call, NULL);
1252   priv->outgoing = (call == NULL);
1253   if (call != NULL)
1254     g_object_unref (call);
1255
1256   empathy_call_window_setup_avatars (self, priv->handler);
1257   empathy_call_window_set_state_connecting (self);
1258
1259   if (!empathy_call_handler_has_initial_video (priv->handler))
1260     {
1261       gtk_toggle_tool_button_set_active (
1262           GTK_TOGGLE_TOOL_BUTTON (priv->tool_button_camera_off), TRUE);
1263     }
1264   /* If call has InitialVideo, the preview will be started once the call has
1265    * been started (start_call()). */
1266 }
1267
1268 static void empathy_call_window_dispose (GObject *object);
1269 static void empathy_call_window_finalize (GObject *object);
1270
1271 static void
1272 empathy_call_window_set_property (GObject *object,
1273   guint property_id, const GValue *value, GParamSpec *pspec)
1274 {
1275   EmpathyCallWindowPriv *priv = GET_PRIV (object);
1276
1277   switch (property_id)
1278     {
1279       case PROP_CALL_HANDLER:
1280         priv->handler = g_value_dup_object (value);
1281         break;
1282       default:
1283         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1284     }
1285 }
1286
1287 static void
1288 empathy_call_window_get_property (GObject *object,
1289   guint property_id, GValue *value, GParamSpec *pspec)
1290 {
1291   EmpathyCallWindowPriv *priv = GET_PRIV (object);
1292
1293   switch (property_id)
1294     {
1295       case PROP_CALL_HANDLER:
1296         g_value_set_object (value, priv->handler);
1297         break;
1298       default:
1299         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1300     }
1301 }
1302
1303 static void
1304 empathy_call_window_class_init (
1305   EmpathyCallWindowClass *empathy_call_window_class)
1306 {
1307   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
1308   GParamSpec *param_spec;
1309
1310   g_type_class_add_private (empathy_call_window_class,
1311     sizeof (EmpathyCallWindowPriv));
1312
1313   object_class->constructed = empathy_call_window_constructed;
1314   object_class->set_property = empathy_call_window_set_property;
1315   object_class->get_property = empathy_call_window_get_property;
1316
1317   object_class->dispose = empathy_call_window_dispose;
1318   object_class->finalize = empathy_call_window_finalize;
1319
1320   param_spec = g_param_spec_object ("handler",
1321     "handler", "The call handler",
1322     EMPATHY_TYPE_CALL_HANDLER,
1323     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1324   g_object_class_install_property (object_class,
1325     PROP_CALL_HANDLER, param_spec);
1326 }
1327
1328 static void
1329 empathy_call_window_video_stream_changed_cb (EmpathyTpCall *call,
1330     GParamSpec *property, EmpathyCallWindow *self)
1331 {
1332   DEBUG ("video stream changed");
1333   empathy_call_window_update_avatars_visibility (call, self);
1334 }
1335
1336 void
1337 empathy_call_window_dispose (GObject *object)
1338 {
1339   EmpathyTpCall *call;
1340   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1341   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1342
1343   if (priv->dispose_has_run)
1344     return;
1345
1346   priv->dispose_has_run = TRUE;
1347
1348   g_object_get (priv->handler, "tp-call", &call, NULL);
1349
1350   if (call != NULL)
1351     {
1352       g_signal_handlers_disconnect_by_func (call,
1353         empathy_call_window_video_stream_changed_cb, object);
1354       g_object_unref (call);
1355     }
1356
1357   if (priv->handler != NULL)
1358     {
1359       empathy_call_handler_stop_call (priv->handler);
1360       g_object_unref (priv->handler);
1361     }
1362   priv->handler = NULL;
1363
1364   if (priv->pipeline != NULL)
1365     g_object_unref (priv->pipeline);
1366   priv->pipeline = NULL;
1367
1368   if (priv->video_input != NULL)
1369     g_object_unref (priv->video_input);
1370   priv->video_input = NULL;
1371
1372   if (priv->audio_input != NULL)
1373     g_object_unref (priv->audio_input);
1374   priv->audio_input = NULL;
1375
1376   if (priv->audio_output != NULL)
1377     g_object_unref (priv->audio_output);
1378   priv->audio_output = NULL;
1379
1380   if (priv->video_tee != NULL)
1381     g_object_unref (priv->video_tee);
1382   priv->video_tee = NULL;
1383
1384   if (priv->fsnotifier != NULL)
1385     g_object_unref (priv->fsnotifier);
1386   priv->fsnotifier = NULL;
1387
1388   if (priv->timer_id != 0)
1389     g_source_remove (priv->timer_id);
1390   priv->timer_id = 0;
1391
1392   if (priv->ui_manager != NULL)
1393     g_object_unref (priv->ui_manager);
1394   priv->ui_manager = NULL;
1395
1396   if (priv->contact != NULL)
1397     {
1398       g_signal_handlers_disconnect_by_func (priv->contact,
1399           contact_name_changed_cb, self);
1400       g_object_unref (priv->contact);
1401       priv->contact = NULL;
1402     }
1403
1404   /* release any references held by the object here */
1405   if (G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose)
1406     G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
1407 }
1408
1409 void
1410 empathy_call_window_finalize (GObject *object)
1411 {
1412   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1413   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1414
1415   if (priv->video_output_motion_handler_id != 0)
1416     {
1417       g_signal_handler_disconnect (G_OBJECT (priv->video_output),
1418           priv->video_output_motion_handler_id);
1419       priv->video_output_motion_handler_id = 0;
1420     }
1421
1422   if (priv->bus_message_source_id != 0)
1423     {
1424       g_source_remove (priv->bus_message_source_id);
1425       priv->bus_message_source_id = 0;
1426     }
1427
1428   /* free any data held directly by the object here */
1429   g_mutex_free (priv->lock);
1430
1431   g_timer_destroy (priv->timer);
1432
1433   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
1434 }
1435
1436
1437 EmpathyCallWindow *
1438 empathy_call_window_new (EmpathyCallHandler *handler)
1439 {
1440   return EMPATHY_CALL_WINDOW (
1441     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
1442 }
1443
1444 static void
1445 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
1446   GstElement *conference, gpointer user_data)
1447 {
1448   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1449   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1450
1451   gst_bin_add (GST_BIN (priv->pipeline), conference);
1452
1453   gst_element_set_state (conference, GST_STATE_PLAYING);
1454 }
1455
1456 static gboolean
1457 empathy_call_window_request_resource_cb (EmpathyCallHandler *handler,
1458   FsMediaType type, FsStreamDirection direction, gpointer user_data)
1459 {
1460   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1461   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1462
1463   if (type != FS_MEDIA_TYPE_VIDEO)
1464     return TRUE;
1465
1466   if (direction == FS_DIRECTION_RECV)
1467     return TRUE;
1468
1469   /* video and direction is send */
1470   return priv->video_input != NULL;
1471 }
1472
1473 static gboolean
1474 empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
1475 {
1476   GstStateChangeReturn state_change_return;
1477   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1478
1479   if (priv->pipeline == NULL)
1480     return TRUE;
1481
1482   if (priv->bus_message_source_id != 0)
1483     {
1484       g_source_remove (priv->bus_message_source_id);
1485       priv->bus_message_source_id = 0;
1486     }
1487
1488   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1489
1490   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
1491         state_change_return == GST_STATE_CHANGE_NO_PREROLL)
1492     {
1493       if (priv->pipeline != NULL)
1494         g_object_unref (priv->pipeline);
1495       priv->pipeline = NULL;
1496
1497       if (priv->video_input != NULL)
1498         g_object_unref (priv->video_input);
1499       priv->video_input = NULL;
1500
1501       if (priv->audio_input != NULL)
1502         g_object_unref (priv->audio_input);
1503       priv->audio_input = NULL;
1504
1505       g_signal_handlers_disconnect_by_func (priv->audio_input_adj,
1506           empathy_call_window_mic_volume_changed_cb, self);
1507
1508       if (priv->video_tee != NULL)
1509         g_object_unref (priv->video_tee);
1510       priv->video_tee = NULL;
1511
1512       if (priv->video_preview != NULL)
1513         gtk_widget_destroy (priv->video_preview);
1514       priv->video_preview = NULL;
1515
1516       priv->liveadder = NULL;
1517       priv->funnel = NULL;
1518
1519       return TRUE;
1520     }
1521   else
1522     {
1523       g_message ("Error: could not destroy pipeline. Closing call window");
1524       gtk_widget_destroy (GTK_WIDGET (self));
1525
1526       return FALSE;
1527     }
1528 }
1529
1530 static gboolean
1531 empathy_call_window_disconnected (EmpathyCallWindow *self)
1532 {
1533   gboolean could_disconnect = FALSE;
1534   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1535   gboolean could_reset_pipeline;
1536
1537   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
1538
1539   if (priv->call_state == CONNECTING)
1540       empathy_sound_stop (EMPATHY_SOUND_PHONE_OUTGOING);
1541
1542   if (priv->call_state != REDIALING)
1543     priv->call_state = DISCONNECTED;
1544
1545   if (could_reset_pipeline)
1546     {
1547       g_mutex_lock (priv->lock);
1548
1549       g_timer_stop (priv->timer);
1550
1551       if (priv->timer_id != 0)
1552         g_source_remove (priv->timer_id);
1553       priv->timer_id = 0;
1554
1555       g_mutex_unlock (priv->lock);
1556
1557       empathy_call_window_status_message (self, _("Disconnected"));
1558
1559       gtk_action_set_sensitive (priv->redial, TRUE);
1560       gtk_widget_set_sensitive (priv->redial_button, TRUE);
1561
1562       /* Reseting the send_video, camera_buton and mic_button to their
1563          initial state */
1564       gtk_widget_set_sensitive (priv->tool_button_camera_on, FALSE);
1565       gtk_widget_set_sensitive (priv->mic_button, FALSE);
1566       gtk_toggle_tool_button_set_active (
1567           GTK_TOGGLE_TOOL_BUTTON (priv->tool_button_camera_off), TRUE);
1568       gtk_toggle_tool_button_set_active (
1569           GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
1570
1571       /* FIXME: This is to workaround the fact that the pipeline has been
1572        * destroyed and so we can't display preview until a new call (and so a
1573        * new pipeline) is created. We should fix this properly by refactoring
1574        * the code managing the pipeline. This is bug #602937 */
1575       gtk_widget_set_sensitive (priv->tool_button_camera_preview, FALSE);
1576       gtk_action_set_sensitive (priv->action_camera_preview, FALSE);
1577
1578       gtk_progress_bar_set_fraction (
1579           GTK_PROGRESS_BAR (priv->volume_progress_bar), 0);
1580
1581       /* destroy the video output; it will be recreated when we'll redial */
1582       gtk_widget_destroy (priv->video_output);
1583       priv->video_output = NULL;
1584
1585       gtk_widget_show (priv->remote_user_avatar_widget);
1586
1587       priv->sending_video = FALSE;
1588       priv->call_started = FALSE;
1589
1590       could_disconnect = TRUE;
1591
1592       /* TODO: display the self avatar of the preview (depends if the "Always
1593        * Show Video Preview" is enabled or not) */
1594     }
1595
1596   return could_disconnect;
1597 }
1598
1599
1600 static void
1601 empathy_call_window_channel_closed_cb (EmpathyCallHandler *handler,
1602     gpointer user_data)
1603 {
1604   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1605   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1606
1607   if (empathy_call_window_disconnected (self) && priv->call_state == REDIALING)
1608       empathy_call_window_restart_call (self);
1609 }
1610
1611
1612 static void
1613 empathy_call_window_channel_stream_closed_cb (EmpathyCallHandler *handler,
1614     TfStream *stream, gpointer user_data)
1615 {
1616   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1617   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1618   guint media_type;
1619
1620   g_object_get (stream, "media-type", &media_type, NULL);
1621
1622   /*
1623    * This assumes that there is only one video stream per channel...
1624    */
1625
1626   if (media_type == TP_MEDIA_STREAM_TYPE_VIDEO)
1627     {
1628       if (priv->funnel != NULL)
1629         {
1630           GstElement *output;
1631
1632           output = empathy_video_widget_get_element (EMPATHY_VIDEO_WIDGET
1633               (priv->video_output));
1634
1635           gst_element_set_state (output, GST_STATE_NULL);
1636           gst_element_set_state (priv->funnel, GST_STATE_NULL);
1637
1638           gst_bin_remove (GST_BIN (priv->pipeline), output);
1639           gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
1640           priv->funnel = NULL;
1641         }
1642     }
1643   else if (media_type == TP_MEDIA_STREAM_TYPE_AUDIO)
1644     {
1645       if (priv->liveadder != NULL)
1646         {
1647           gst_element_set_state (priv->audio_output, GST_STATE_NULL);
1648           gst_element_set_state (priv->liveadder, GST_STATE_NULL);
1649
1650           gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
1651           gst_bin_remove (GST_BIN (priv->pipeline), priv->liveadder);
1652           priv->liveadder = NULL;
1653         }
1654     }
1655 }
1656
1657 /* Called with global lock held */
1658 static GstPad *
1659 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
1660 {
1661   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1662   GstPad *pad;
1663   GstElement *output;
1664
1665   if (priv->funnel == NULL)
1666     {
1667       output = empathy_video_widget_get_element (EMPATHY_VIDEO_WIDGET
1668         (priv->video_output));
1669
1670       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
1671
1672       if (!priv->funnel)
1673         {
1674           g_warning ("Could not create fsfunnel");
1675           return NULL;
1676         }
1677
1678       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->funnel))
1679         {
1680           gst_object_unref (priv->funnel);
1681           priv->funnel = NULL;
1682           g_warning ("Could  not add funnel to pipeline");
1683           return NULL;
1684         }
1685
1686       if (!gst_bin_add (GST_BIN (priv->pipeline), output))
1687         {
1688           g_warning ("Could not add the video output widget to the pipeline");
1689           goto error;
1690         }
1691
1692       if (!gst_element_link (priv->funnel, output))
1693         {
1694           g_warning ("Could not link output sink to funnel");
1695           goto error_output_added;
1696         }
1697
1698       if (gst_element_set_state (output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1699         {
1700           g_warning ("Could not start video sink");
1701           goto error_output_added;
1702         }
1703
1704       if (gst_element_set_state (priv->funnel, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1705         {
1706           g_warning ("Could not start funnel");
1707           goto error_output_added;
1708         }
1709     }
1710
1711   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
1712
1713   if (!pad)
1714     g_warning ("Could not get request pad from funnel");
1715
1716   return pad;
1717
1718
1719  error_output_added:
1720
1721   gst_element_set_locked_state (priv->funnel, TRUE);
1722   gst_element_set_locked_state (output, TRUE);
1723
1724   gst_element_set_state (priv->funnel, GST_STATE_NULL);
1725   gst_element_set_state (output, GST_STATE_NULL);
1726
1727   gst_bin_remove (GST_BIN (priv->pipeline), output);
1728   gst_element_set_locked_state (output, FALSE);
1729
1730  error:
1731
1732   gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
1733   priv->funnel = NULL;
1734
1735   return NULL;
1736 }
1737
1738 /* Called with global lock held */
1739 static GstPad *
1740 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self)
1741 {
1742   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1743   GstPad *pad;
1744   GstElement *filter;
1745   GError *gerror = NULL;
1746
1747   if (priv->liveadder == NULL)
1748     {
1749       priv->liveadder = gst_element_factory_make ("liveadder", NULL);
1750
1751       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->liveadder))
1752         {
1753           g_warning ("Could not add liveadder to the pipeline");
1754           goto error_add_liveadder;
1755         }
1756       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output))
1757         {
1758           g_warning ("Could not add audio sink to pipeline");
1759           goto error_add_output;
1760         }
1761
1762       if (gst_element_set_state (priv->liveadder, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1763         {
1764           g_warning ("Could not start liveadder");
1765           goto error;
1766         }
1767
1768       if (gst_element_set_state (priv->audio_output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1769         {
1770           g_warning ("Could not start audio sink");
1771           goto error;
1772         }
1773
1774       if (GST_PAD_LINK_FAILED (
1775               gst_element_link (priv->liveadder, priv->audio_output)))
1776         {
1777           g_warning ("Could not link liveadder to audio output");
1778           goto error;
1779         }
1780     }
1781
1782   filter = gst_parse_bin_from_description (
1783       "audioconvert ! audioresample ! audioconvert", TRUE, &gerror);
1784   if (filter == NULL)
1785     {
1786       g_warning ("Could not make audio conversion filter: %s", gerror->message);
1787       g_clear_error (&gerror);
1788       goto error;
1789     }
1790
1791   if (!gst_bin_add (GST_BIN (priv->pipeline), filter))
1792     {
1793       g_warning ("Could not add audio conversion filter to pipeline");
1794       gst_object_unref (filter);
1795       goto error;
1796     }
1797
1798   if (gst_element_set_state (filter, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1799     {
1800       g_warning ("Could not start audio conversion filter");
1801       goto error_filter;
1802     }
1803
1804   if (!gst_element_link (filter, priv->liveadder))
1805     {
1806       g_warning ("Could not link audio conversion filter to liveadder");
1807       goto error_filter;
1808     }
1809
1810   pad = gst_element_get_static_pad (filter, "sink");
1811
1812   if (pad == NULL)
1813     {
1814       g_warning ("Could not get sink pad from filter");
1815       goto error_filter;
1816     }
1817
1818   return pad;
1819
1820  error_filter:
1821
1822   gst_element_set_locked_state (filter, TRUE);
1823   gst_element_set_state (filter, GST_STATE_NULL);
1824   gst_bin_remove (GST_BIN (priv->pipeline), filter);
1825
1826  error:
1827
1828   gst_element_set_locked_state (priv->liveadder, TRUE);
1829   gst_element_set_locked_state (priv->audio_output, TRUE);
1830
1831   gst_element_set_state (priv->liveadder, GST_STATE_NULL);
1832   gst_element_set_state (priv->audio_output, GST_STATE_NULL);
1833
1834   gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
1835
1836  error_add_output:
1837
1838   gst_bin_remove (GST_BIN (priv->pipeline), priv->liveadder);
1839
1840   gst_element_set_locked_state (priv->liveadder, FALSE);
1841   gst_element_set_locked_state (priv->audio_output, FALSE);
1842
1843  error_add_liveadder:
1844
1845   if (priv->liveadder != NULL)
1846     {
1847       gst_object_unref (priv->liveadder);
1848       priv->liveadder = NULL;
1849     }
1850
1851   return NULL;
1852 }
1853
1854 static gboolean
1855 empathy_call_window_update_timer (gpointer user_data)
1856 {
1857   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1858   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1859   gchar *str;
1860   gdouble time_;
1861
1862   time_ = g_timer_elapsed (priv->timer, NULL);
1863
1864   /* Translators: number of minutes:seconds the caller has been connected */
1865   str = g_strdup_printf (_("Connected â€” %d:%02dm"), (int) time_ / 60,
1866     (int) time_ % 60);
1867   empathy_call_window_status_message (self, str);
1868   g_free (str);
1869
1870   return TRUE;
1871 }
1872
1873 static void
1874 display_error (EmpathyCallWindow *self,
1875     EmpathyTpCall *call,
1876     const gchar *img,
1877     const gchar *title,
1878     const gchar *desc,
1879     const gchar *details)
1880 {
1881   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1882   GtkWidget *info_bar;
1883   GtkWidget *content_area;
1884   GtkWidget *hbox;
1885   GtkWidget *vbox;
1886   GtkWidget *image;
1887   GtkWidget *label;
1888   gchar *txt;
1889
1890   /* Create info bar */
1891   info_bar = gtk_info_bar_new_with_buttons (GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
1892       NULL);
1893
1894   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
1895
1896   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
1897
1898   /* hbox containing the image and the messages vbox */
1899   hbox = gtk_hbox_new (FALSE, 3);
1900   gtk_container_add (GTK_CONTAINER (content_area), hbox);
1901
1902   /* Add image */
1903   image = gtk_image_new_from_icon_name (img, GTK_ICON_SIZE_DIALOG);
1904   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
1905
1906   /* vbox containing the main message and the details expander */
1907   vbox = gtk_vbox_new (FALSE, 3);
1908   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
1909
1910   /* Add text */
1911   txt = g_strdup_printf ("<b>%s</b>\n%s", title, desc);
1912
1913   label = gtk_label_new (NULL);
1914   gtk_label_set_markup (GTK_LABEL (label), txt);
1915   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1916   gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1917   g_free (txt);
1918
1919   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
1920
1921   /* Add details */
1922   if (details != NULL)
1923     {
1924       GtkWidget *expander;
1925
1926       expander = gtk_expander_new (_("Technical Details"));
1927
1928       txt = g_strdup_printf ("<i>%s</i>", details);
1929
1930       label = gtk_label_new (NULL);
1931       gtk_label_set_markup (GTK_LABEL (label), txt);
1932       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1933       gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1934       g_free (txt);
1935
1936       gtk_container_add (GTK_CONTAINER (expander), label);
1937       gtk_box_pack_start (GTK_BOX (vbox), expander, TRUE, TRUE, 0);
1938     }
1939
1940   g_signal_connect (info_bar, "response",
1941       G_CALLBACK (gtk_widget_destroy), NULL);
1942
1943   gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar,
1944       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
1945   gtk_widget_show_all (info_bar);
1946 }
1947
1948 static gchar *
1949 media_stream_error_to_txt (EmpathyCallWindow *self,
1950     EmpathyTpCall *call,
1951     gboolean audio,
1952     TpMediaStreamError error)
1953 {
1954   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1955   const gchar *cm;
1956   gchar *url;
1957   gchar *result;
1958
1959   switch (error)
1960     {
1961       case TP_MEDIA_STREAM_ERROR_CODEC_NEGOTIATION_FAILED:
1962         if (audio)
1963           return g_strdup_printf (
1964               _("%s's software does not understand any of the audio formats "
1965                 "supported by your computer"),
1966             empathy_contact_get_name (priv->contact));
1967         else
1968           return g_strdup_printf (
1969               _("%s's software does not understand any of the video formats "
1970                 "supported by your computer"),
1971             empathy_contact_get_name (priv->contact));
1972
1973       case TP_MEDIA_STREAM_ERROR_CONNECTION_FAILED:
1974         return g_strdup_printf (
1975             _("Can't establish a connection to %s. "
1976               "One of you might be on a network that does not allow "
1977               "direct connections."),
1978           empathy_contact_get_name (priv->contact));
1979
1980       case TP_MEDIA_STREAM_ERROR_NETWORK_ERROR:
1981           return g_strdup (_("There was a failure on the network"));
1982
1983       case TP_MEDIA_STREAM_ERROR_NO_CODECS:
1984         if (audio)
1985           return g_strdup (_("The audio formats necessary for this call "
1986                 "are not installed on your computer"));
1987         else
1988           return g_strdup (_("The video formats necessary for this call "
1989                 "are not installed on your computer"));
1990
1991       case TP_MEDIA_STREAM_ERROR_INVALID_CM_BEHAVIOR:
1992         cm = empathy_tp_call_get_connection_manager (call);
1993
1994         url = g_strdup_printf ("http://bugs.freedesktop.org/enter_bug.cgi?"
1995             "product=Telepathy&amp;component=%s", cm);
1996
1997         result = g_strdup_printf (
1998             _("Something unexpected happened in a Telepathy component. "
1999               "Please <a href=\"%s\">report this bug</a> and attach "
2000               "logs gathered from the 'Debug' window in the Help menu."), url);
2001
2002         g_free (url);
2003         return result;
2004
2005       case TP_MEDIA_STREAM_ERROR_MEDIA_ERROR:
2006         return g_strdup (_("There was a failure in the call engine"));
2007
2008       default:
2009         return NULL;
2010     }
2011 }
2012
2013 static void
2014 empathy_call_window_stream_error (EmpathyCallWindow *self,
2015     EmpathyTpCall *call,
2016     gboolean audio,
2017     guint code,
2018     const gchar *msg,
2019     const gchar *icon,
2020     const gchar *title)
2021 {
2022   gchar *desc;
2023
2024   desc = media_stream_error_to_txt (self, call, audio, code);
2025   if (desc == NULL)
2026     {
2027       /* No description, use the error message. That's not great as it's not
2028        * localized but it's better than nothing. */
2029       display_error (self, call, icon, title, msg, NULL);
2030     }
2031   else
2032     {
2033       display_error (self, call, icon, title, desc, msg);
2034       g_free (desc);
2035     }
2036 }
2037
2038 static void
2039 empathy_call_window_audio_stream_error (EmpathyTpCall *call,
2040     guint code,
2041     const gchar *msg,
2042     EmpathyCallWindow *self)
2043 {
2044   empathy_call_window_stream_error (self, call, TRUE, code, msg,
2045       "gnome-stock-mic", _("Can't establish audio stream"));
2046 }
2047
2048 static void
2049 empathy_call_window_video_stream_error (EmpathyTpCall *call,
2050     guint code,
2051     const gchar *msg,
2052     EmpathyCallWindow *self)
2053 {
2054   empathy_call_window_stream_error (self, call, FALSE, code, msg,
2055       "camera-web", _("Can't establish video stream"));
2056 }
2057
2058 static gboolean
2059 empathy_call_window_connected (gpointer user_data)
2060 {
2061   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2062   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2063   EmpathyTpCall *call;
2064   gboolean can_send_video;
2065
2066   empathy_sound_stop (EMPATHY_SOUND_PHONE_OUTGOING);
2067
2068   can_send_video = priv->video_input != NULL && priv->contact != NULL &&
2069     empathy_contact_can_voip_video (priv->contact);
2070
2071   g_object_get (priv->handler, "tp-call", &call, NULL);
2072
2073   g_signal_connect (call, "notify::video-stream",
2074     G_CALLBACK (empathy_call_window_video_stream_changed_cb), self);
2075
2076   if (empathy_tp_call_has_dtmf (call))
2077     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
2078
2079   if (priv->video_input == NULL)
2080     empathy_call_window_set_send_video (self, FALSE);
2081
2082   priv->sending_video = can_send_video ?
2083     empathy_tp_call_is_sending_video (call) : FALSE;
2084
2085   gtk_toggle_tool_button_set_active (
2086       GTK_TOGGLE_TOOL_BUTTON (priv->tool_button_camera_on),
2087       priv->sending_video && priv->video_input != NULL);
2088   gtk_widget_set_sensitive (priv->tool_button_camera_on, can_send_video);
2089
2090   gtk_action_set_sensitive (priv->redial, FALSE);
2091   gtk_widget_set_sensitive (priv->redial_button, FALSE);
2092
2093   gtk_widget_set_sensitive (priv->mic_button, TRUE);
2094
2095   /* FIXME: this should won't be needed once bug #602937 is fixed
2096    * (see empathy_call_window_disconnected for details) */
2097   gtk_widget_set_sensitive (priv->tool_button_camera_preview, TRUE);
2098   gtk_action_set_sensitive (priv->action_camera_preview, TRUE);
2099
2100   empathy_call_window_update_avatars_visibility (call, self);
2101
2102   g_object_unref (call);
2103
2104   g_mutex_lock (priv->lock);
2105
2106   priv->timer_id = g_timeout_add_seconds (1,
2107     empathy_call_window_update_timer, self);
2108
2109   g_mutex_unlock (priv->lock);
2110
2111   empathy_call_window_update_timer (self);
2112
2113   return FALSE;
2114 }
2115
2116
2117 /* Called from the streaming thread */
2118 static gboolean
2119 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
2120   GstPad *src, guint media_type, gpointer user_data)
2121 {
2122   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2123   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2124   gboolean retval = FALSE;
2125
2126   GstPad *pad;
2127
2128   g_mutex_lock (priv->lock);
2129
2130   if (priv->call_state != CONNECTED)
2131     {
2132       g_timer_start (priv->timer);
2133       priv->timer_id = g_idle_add  (empathy_call_window_connected, self);
2134       priv->call_state = CONNECTED;
2135     }
2136
2137   switch (media_type)
2138     {
2139       case TP_MEDIA_STREAM_TYPE_AUDIO:
2140         pad = empathy_call_window_get_audio_sink_pad (self);
2141         break;
2142       case TP_MEDIA_STREAM_TYPE_VIDEO:
2143         gtk_widget_hide (priv->remote_user_avatar_widget);
2144         gtk_widget_show (priv->video_output);
2145         pad = empathy_call_window_get_video_sink_pad (self);
2146         break;
2147       default:
2148         g_assert_not_reached ();
2149     }
2150
2151   if (pad == NULL)
2152     goto out;
2153
2154   if (GST_PAD_LINK_FAILED (gst_pad_link (src, pad)))
2155       g_warning ("Could not link %s sink pad",
2156           media_type == TP_MEDIA_STREAM_TYPE_AUDIO ? "audio" : "video");
2157   else
2158       retval = TRUE;
2159
2160   gst_object_unref (pad);
2161
2162  out:
2163
2164   /* If no sink could be linked, try to add fakesink to prevent the whole call
2165    * aborting */
2166
2167   if (!retval)
2168     {
2169       GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
2170
2171       if (gst_bin_add (GST_BIN (priv->pipeline), fakesink))
2172         {
2173           GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
2174           if (gst_element_set_state (fakesink, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE ||
2175               GST_PAD_LINK_FAILED (gst_pad_link (src, sinkpad)))
2176             {
2177               gst_element_set_locked_state (fakesink, TRUE);
2178               gst_element_set_state (fakesink, GST_STATE_NULL);
2179               gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2180             }
2181           else
2182             {
2183               g_debug ("Could not link real sink, linked fakesink instead");
2184             }
2185           gst_object_unref (sinkpad);
2186         }
2187       else
2188         {
2189           gst_object_unref (fakesink);
2190         }
2191     }
2192
2193
2194   g_mutex_unlock (priv->lock);
2195
2196   return TRUE;
2197 }
2198
2199 static gboolean
2200 empathy_call_window_sink_added_cb (EmpathyCallHandler *handler,
2201   GstPad *sink, guint media_type, gpointer user_data)
2202 {
2203   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2204   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2205   GstPad *pad;
2206   gboolean retval = FALSE;
2207
2208   switch (media_type)
2209     {
2210       case TP_MEDIA_STREAM_TYPE_AUDIO:
2211         if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input))
2212           {
2213             g_warning ("Could not add audio source to pipeline");
2214             break;
2215           }
2216
2217         pad = gst_element_get_static_pad (priv->audio_input, "src");
2218         if (!pad)
2219           {
2220             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2221             g_warning ("Could not get source pad from audio source");
2222             break;
2223           }
2224
2225         if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
2226           {
2227             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2228             g_warning ("Could not link audio source to farsight");
2229             break;
2230           }
2231
2232         if (gst_element_set_state (priv->audio_input, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2233           {
2234             g_warning ("Could not start audio source");
2235             gst_element_set_state (priv->audio_input, GST_STATE_NULL);
2236             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2237             break;
2238           }
2239
2240         retval = TRUE;
2241         break;
2242       case TP_MEDIA_STREAM_TYPE_VIDEO:
2243         if (priv->video_input != NULL)
2244           {
2245             if (priv->video_tee != NULL)
2246               {
2247                 pad = gst_element_get_request_pad (priv->video_tee, "src%d");
2248                 if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
2249                   {
2250                     g_warning ("Could not link videp soure input pipeline");
2251                     break;
2252                   }
2253               }
2254
2255             retval = TRUE;
2256           }
2257         break;
2258       default:
2259         g_assert_not_reached ();
2260     }
2261
2262   return retval;
2263 }
2264
2265 static void
2266 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
2267 {
2268   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2269   GstElement *preview;
2270
2271   DEBUG ("remove video input");
2272   preview = empathy_video_widget_get_element (
2273     EMPATHY_VIDEO_WIDGET (priv->video_preview));
2274
2275   gst_element_set_state (priv->video_input, GST_STATE_NULL);
2276   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
2277   gst_element_set_state (preview, GST_STATE_NULL);
2278
2279   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
2280     priv->video_tee, preview, NULL);
2281
2282   g_object_unref (priv->video_input);
2283   priv->video_input = NULL;
2284   g_object_unref (priv->video_tee);
2285   priv->video_tee = NULL;
2286   gtk_widget_destroy (priv->video_preview);
2287   priv->video_preview = NULL;
2288
2289   gtk_toggle_tool_button_set_active (
2290       GTK_TOGGLE_TOOL_BUTTON (priv->tool_button_camera_on), FALSE);
2291   gtk_widget_set_sensitive (priv->tool_button_camera_on, FALSE);
2292
2293   gtk_widget_show (priv->self_user_avatar_widget);
2294 }
2295
2296 static void
2297 start_call (EmpathyCallWindow *self)
2298 {
2299   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2300
2301   priv->call_started = TRUE;
2302   empathy_call_handler_start_call (priv->handler);
2303   gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
2304
2305   if (empathy_call_handler_has_initial_video (priv->handler))
2306     {
2307       /* Enable 'send video' buttons and display the preview */
2308       gtk_toggle_tool_button_set_active (
2309           GTK_TOGGLE_TOOL_BUTTON (priv->tool_button_camera_on), TRUE);
2310     }
2311 }
2312
2313 static gboolean
2314 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
2315   gpointer user_data)
2316 {
2317   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2318   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2319   GstState newstate;
2320
2321   empathy_call_handler_bus_message (priv->handler, bus, message);
2322
2323   switch (GST_MESSAGE_TYPE (message))
2324     {
2325       case GST_MESSAGE_STATE_CHANGED:
2326         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
2327           {
2328             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
2329             if (newstate == GST_STATE_PAUSED)
2330                 empathy_call_window_setup_video_input (self);
2331           }
2332         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
2333             !priv->call_started)
2334           {
2335             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
2336             if (newstate == GST_STATE_PAUSED)
2337               {
2338                 start_call (self);
2339               }
2340           }
2341         break;
2342       case GST_MESSAGE_ERROR:
2343         {
2344           GError *error = NULL;
2345           GstElement *gst_error;
2346           gchar *debug;
2347
2348           gst_message_parse_error (message, &error, &debug);
2349           gst_error = GST_ELEMENT (GST_MESSAGE_SRC (message));
2350
2351           g_message ("Element error: %s -- %s\n", error->message, debug);
2352
2353           if (g_str_has_prefix (gst_element_get_name (gst_error),
2354                 VIDEO_INPUT_ERROR_PREFIX))
2355             {
2356               /* Remove the video input and continue */
2357               if (priv->video_input != NULL)
2358                 empathy_call_window_remove_video_input (self);
2359               gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
2360             }
2361           else
2362             {
2363               empathy_call_window_disconnected (self);
2364             }
2365           g_error_free (error);
2366           g_free (debug);
2367         }
2368       default:
2369         break;
2370     }
2371
2372   return TRUE;
2373 }
2374
2375 static void
2376 empathy_call_window_update_avatars_visibility (EmpathyTpCall *call,
2377     EmpathyCallWindow *window)
2378 {
2379   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2380
2381   if (empathy_tp_call_is_receiving_video (call))
2382     {
2383       gtk_widget_hide (priv->remote_user_avatar_widget);
2384       gtk_widget_show (priv->video_output);
2385     }
2386   else
2387     {
2388       gtk_widget_hide (priv->video_output);
2389       gtk_widget_show (priv->remote_user_avatar_widget);
2390     }
2391 }
2392
2393 static void
2394 call_handler_notify_tp_call_cb (EmpathyCallHandler *handler,
2395     GParamSpec *spec,
2396     EmpathyCallWindow *self)
2397 {
2398   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2399   EmpathyTpCall *call;
2400
2401   g_object_get (priv->handler, "tp-call", &call, NULL);
2402   if (call == NULL)
2403     return;
2404
2405   empathy_signal_connect_weak (call, "audio-stream-error",
2406       G_CALLBACK (empathy_call_window_audio_stream_error), G_OBJECT (self));
2407   empathy_signal_connect_weak (call, "video-stream-error",
2408       G_CALLBACK (empathy_call_window_video_stream_error), G_OBJECT (self));
2409
2410   g_object_unref (call);
2411 }
2412
2413 static void
2414 empathy_call_window_realized_cb (GtkWidget *widget, EmpathyCallWindow *window)
2415 {
2416   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2417   EmpathyTpCall *call;
2418
2419   g_signal_connect (priv->handler, "conference-added",
2420     G_CALLBACK (empathy_call_window_conference_added_cb), window);
2421   g_signal_connect (priv->handler, "request-resource",
2422     G_CALLBACK (empathy_call_window_request_resource_cb), window);
2423   g_signal_connect (priv->handler, "closed",
2424     G_CALLBACK (empathy_call_window_channel_closed_cb), window);
2425   g_signal_connect (priv->handler, "src-pad-added",
2426     G_CALLBACK (empathy_call_window_src_added_cb), window);
2427   g_signal_connect (priv->handler, "sink-pad-added",
2428     G_CALLBACK (empathy_call_window_sink_added_cb), window);
2429   g_signal_connect (priv->handler, "stream-closed",
2430     G_CALLBACK (empathy_call_window_channel_stream_closed_cb), window);
2431
2432   g_object_get (priv->handler, "tp-call", &call, NULL);
2433   if (call != NULL)
2434     {
2435       empathy_signal_connect_weak (call, "audio-stream-error",
2436         G_CALLBACK (empathy_call_window_audio_stream_error), G_OBJECT (window));
2437       empathy_signal_connect_weak (call, "video-stream-error",
2438         G_CALLBACK (empathy_call_window_video_stream_error), G_OBJECT (window));
2439
2440       g_object_unref (call);
2441     }
2442   else
2443     {
2444       /* tp-call doesn't exist yet, we'll connect signals once it has been
2445        * set */
2446       g_signal_connect (priv->handler, "notify::tp-call",
2447         G_CALLBACK (call_handler_notify_tp_call_cb), window);
2448     }
2449
2450   gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2451 }
2452
2453 static gboolean
2454 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
2455   EmpathyCallWindow *window)
2456 {
2457   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2458
2459   if (priv->pipeline != NULL)
2460     {
2461       if (priv->bus_message_source_id != 0)
2462         {
2463           g_source_remove (priv->bus_message_source_id);
2464           priv->bus_message_source_id = 0;
2465         }
2466
2467       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2468     }
2469
2470   if (priv->call_state == CONNECTING)
2471     empathy_sound_stop (EMPATHY_SOUND_PHONE_OUTGOING);
2472
2473   return FALSE;
2474 }
2475
2476 static void
2477 show_controls (EmpathyCallWindow *window, gboolean set_fullscreen)
2478 {
2479   GtkWidget *menu;
2480   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2481
2482   menu = gtk_ui_manager_get_widget (priv->ui_manager,
2483             "/menubar1");
2484
2485   if (set_fullscreen)
2486     {
2487       gtk_widget_hide (priv->sidebar);
2488       gtk_widget_hide (menu);
2489       gtk_widget_hide (priv->vbox);
2490       gtk_widget_hide (priv->statusbar);
2491       gtk_widget_hide (priv->toolbar);
2492     }
2493   else
2494     {
2495       if (priv->sidebar_was_visible_before_fs)
2496         gtk_widget_show (priv->sidebar);
2497
2498       gtk_widget_show (menu);
2499       gtk_widget_show (priv->vbox);
2500       gtk_widget_show (priv->statusbar);
2501       gtk_widget_show (priv->toolbar);
2502
2503       gtk_window_resize (GTK_WINDOW (window), priv->original_width_before_fs,
2504           priv->original_height_before_fs);
2505     }
2506 }
2507
2508 static void
2509 show_borders (EmpathyCallWindow *window, gboolean set_fullscreen)
2510 {
2511   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2512
2513   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
2514       set_fullscreen ? 0 : CONTENT_HBOX_BORDER_WIDTH);
2515   gtk_box_set_spacing (GTK_BOX (priv->content_hbox),
2516       set_fullscreen ? 0 : CONTENT_HBOX_SPACING);
2517   gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
2518       priv->video_output, TRUE, TRUE,
2519       set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
2520       GTK_PACK_START);
2521   gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
2522       priv->vbox, TRUE, TRUE,
2523       set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
2524       GTK_PACK_START);
2525 }
2526
2527 static gboolean
2528 empathy_call_window_state_event_cb (GtkWidget *widget,
2529   GdkEventWindowState *event, EmpathyCallWindow *window)
2530 {
2531   if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
2532     {
2533       EmpathyCallWindowPriv *priv = GET_PRIV (window);
2534       gboolean set_fullscreen = event->new_window_state &
2535         GDK_WINDOW_STATE_FULLSCREEN;
2536
2537       if (set_fullscreen)
2538         {
2539           gboolean sidebar_was_visible;
2540           GtkAllocation allocation;
2541           gint original_width, original_height;
2542
2543           gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
2544           original_width = allocation.width;
2545           original_height = allocation.height;
2546
2547           g_object_get (priv->sidebar, "visible", &sidebar_was_visible, NULL);
2548
2549           priv->sidebar_was_visible_before_fs = sidebar_was_visible;
2550           priv->original_width_before_fs = original_width;
2551           priv->original_height_before_fs = original_height;
2552
2553           if (priv->video_output_motion_handler_id == 0 &&
2554                 priv->video_output != NULL)
2555             {
2556               priv->video_output_motion_handler_id = g_signal_connect (
2557                   G_OBJECT (priv->video_output), "motion-notify-event",
2558                   G_CALLBACK (empathy_call_window_video_output_motion_notify),
2559                   window);
2560             }
2561         }
2562       else
2563         {
2564           if (priv->video_output_motion_handler_id != 0)
2565             {
2566               g_signal_handler_disconnect (G_OBJECT (priv->video_output),
2567                   priv->video_output_motion_handler_id);
2568               priv->video_output_motion_handler_id = 0;
2569             }
2570         }
2571
2572       empathy_call_window_fullscreen_set_fullscreen (priv->fullscreen,
2573           set_fullscreen);
2574       show_controls (window, set_fullscreen);
2575       show_borders (window, set_fullscreen);
2576       gtk_action_set_stock_id (priv->menu_fullscreen,
2577           (set_fullscreen ? "gtk-leave-fullscreen" : "gtk-fullscreen"));
2578       priv->is_fullscreen = set_fullscreen;
2579   }
2580
2581   return FALSE;
2582 }
2583
2584 static void
2585 empathy_call_window_sidebar_toggled_cb (GtkToggleButton *toggle,
2586   EmpathyCallWindow *window)
2587 {
2588   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2589   GtkWidget *arrow;
2590   int w, h, handle_size;
2591   GtkAllocation allocation, sidebar_allocation;
2592
2593   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
2594   w = allocation.width;
2595   h = allocation.height;
2596
2597   gtk_widget_style_get (priv->pane, "handle_size", &handle_size, NULL);
2598
2599   gtk_widget_get_allocation (priv->sidebar, &sidebar_allocation);
2600   if (gtk_toggle_button_get_active (toggle))
2601     {
2602       arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
2603       gtk_widget_show (priv->sidebar);
2604       w += sidebar_allocation.width + handle_size;
2605     }
2606   else
2607     {
2608       arrow = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
2609       w -= sidebar_allocation.width + handle_size;
2610       gtk_widget_hide (priv->sidebar);
2611     }
2612
2613   gtk_button_set_image (GTK_BUTTON (priv->sidebar_button), arrow);
2614
2615   if (w > 0 && h > 0)
2616     gtk_window_resize (GTK_WINDOW (window), w, h);
2617 }
2618
2619 static void
2620 empathy_call_window_set_send_video (EmpathyCallWindow *window,
2621   gboolean send)
2622 {
2623   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2624   EmpathyTpCall *call;
2625
2626   priv->sending_video = send;
2627
2628   /* When we start sending video, we want to show the video preview by
2629      default. */
2630   display_video_preview (window, send);
2631
2632   if (priv->call_state != CONNECTED)
2633     return;
2634
2635   g_object_get (priv->handler, "tp-call", &call, NULL);
2636   DEBUG ("%s sending video", send ? "start": "stop");
2637   empathy_tp_call_request_video_stream_direction (call, send);
2638   g_object_unref (call);
2639 }
2640
2641 static void
2642 empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
2643   EmpathyCallWindow *window)
2644 {
2645   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2646   gboolean active;
2647
2648   if (priv->audio_input == NULL)
2649     return;
2650
2651   active = (gtk_toggle_tool_button_get_active (toggle));
2652
2653   if (active)
2654     {
2655       empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (priv->audio_input),
2656         priv->volume);
2657       gtk_adjustment_set_value (priv->audio_input_adj, priv->volume * 100);
2658     }
2659   else
2660     {
2661       /* TODO, Instead of setting the input volume to 0 we should probably
2662        * stop sending but this would cause the audio call to drop if both
2663        * sides mute at the same time on certain CMs AFAIK. Need to revisit this
2664        * in the future. GNOME #574574
2665        */
2666       empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (priv->audio_input),
2667         0);
2668       gtk_adjustment_set_value (priv->audio_input_adj, 0);
2669     }
2670 }
2671
2672 static void
2673 empathy_call_window_sidebar_hidden_cb (EmpathySidebar *sidebar,
2674   EmpathyCallWindow *window)
2675 {
2676   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2677
2678   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->sidebar_button),
2679     FALSE);
2680 }
2681
2682 static void
2683 empathy_call_window_sidebar_shown_cb (EmpathySidebar *sidebar,
2684   EmpathyCallWindow *window)
2685 {
2686   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2687
2688   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->sidebar_button),
2689     TRUE);
2690 }
2691
2692 static void
2693 empathy_call_window_hangup_cb (gpointer object,
2694                                EmpathyCallWindow *window)
2695 {
2696   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2697
2698   empathy_call_handler_stop_call (priv->handler);
2699
2700   if (empathy_call_window_disconnected (window))
2701     gtk_widget_destroy (GTK_WIDGET (window));
2702 }
2703
2704 static void
2705 empathy_call_window_restart_call (EmpathyCallWindow *window)
2706 {
2707   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2708
2709   create_pipeline (window);
2710
2711   g_signal_connect (G_OBJECT (priv->audio_input_adj), "value-changed",
2712       G_CALLBACK (empathy_call_window_mic_volume_changed_cb), window);
2713
2714   /* While the call was disconnected, the input volume might have changed.
2715    * However, since the audio_input source was destroyed, its volume has not
2716    * been updated during that time. That's why we manually update it here */
2717   empathy_call_window_mic_volume_changed_cb (priv->audio_input_adj, window);
2718
2719   gtk_widget_show_all (priv->content_hbox);
2720
2721   priv->outgoing = TRUE;
2722   empathy_call_window_set_state_connecting (window);
2723
2724   start_call (window);
2725   empathy_call_window_setup_avatars (window, priv->handler);
2726
2727   gtk_action_set_sensitive (priv->redial, FALSE);
2728   gtk_widget_set_sensitive (priv->redial_button, FALSE);
2729 }
2730
2731 static void
2732 empathy_call_window_redial_cb (gpointer object,
2733     EmpathyCallWindow *window)
2734 {
2735   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2736
2737   if (priv->call_state == CONNECTED)
2738     priv->call_state = REDIALING;
2739
2740   empathy_call_handler_stop_call (priv->handler);
2741
2742   if (priv->call_state != CONNECTED)
2743     empathy_call_window_restart_call (window);
2744 }
2745
2746 static void
2747 empathy_call_window_fullscreen_cb (gpointer object,
2748                                    EmpathyCallWindow *window)
2749 {
2750   empathy_call_window_fullscreen_toggle (window);
2751 }
2752
2753 static void
2754 empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window)
2755 {
2756   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2757
2758   if (priv->is_fullscreen)
2759     gtk_window_unfullscreen (GTK_WINDOW (window));
2760   else
2761     gtk_window_fullscreen (GTK_WINDOW (window));
2762 }
2763
2764 static gboolean
2765 empathy_call_window_video_button_press_cb (GtkWidget *video_output,
2766   GdkEventButton *event, EmpathyCallWindow *window)
2767 {
2768   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
2769     {
2770       empathy_call_window_video_menu_popup (window, event->button);
2771       return TRUE;
2772     }
2773
2774   return FALSE;
2775 }
2776
2777 static gboolean
2778 empathy_call_window_key_press_cb (GtkWidget *video_output,
2779   GdkEventKey *event, EmpathyCallWindow *window)
2780 {
2781   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2782
2783   if (priv->is_fullscreen && event->keyval == GDK_Escape)
2784     {
2785       /* Since we are in fullscreen mode, toggling will bring us back to
2786          normal mode. */
2787       empathy_call_window_fullscreen_toggle (window);
2788       return TRUE;
2789     }
2790
2791   return FALSE;
2792 }
2793
2794 static gboolean
2795 empathy_call_window_video_output_motion_notify (GtkWidget *widget,
2796     GdkEventMotion *event, EmpathyCallWindow *window)
2797 {
2798   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2799
2800   if (priv->is_fullscreen)
2801     {
2802       empathy_call_window_fullscreen_show_popup (priv->fullscreen);
2803       return TRUE;
2804     }
2805   return FALSE;
2806 }
2807
2808 static void
2809 empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
2810   guint button)
2811 {
2812   GtkWidget *menu;
2813   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2814
2815   menu = gtk_ui_manager_get_widget (priv->ui_manager,
2816             "/video-popup");
2817   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
2818       button, gtk_get_current_event_time ());
2819   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
2820 }
2821
2822 static void
2823 empathy_call_window_status_message (EmpathyCallWindow *window,
2824   gchar *message)
2825 {
2826   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2827
2828   if (priv->context_id == 0)
2829     {
2830       priv->context_id = gtk_statusbar_get_context_id (
2831         GTK_STATUSBAR (priv->statusbar), "voip call status messages");
2832     }
2833   else
2834     {
2835       gtk_statusbar_pop (GTK_STATUSBAR (priv->statusbar), priv->context_id);
2836     }
2837
2838   gtk_statusbar_push (GTK_STATUSBAR (priv->statusbar), priv->context_id,
2839     message);
2840 }
2841
2842 static void
2843 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
2844   gdouble value, EmpathyCallWindow *window)
2845 {
2846   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2847
2848   if (priv->audio_output == NULL)
2849     return;
2850
2851   empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (priv->audio_output),
2852     value);
2853 }
2854
2855 /* block all the signals related to camera control widgets. This is useful
2856  * when we are manually updating the UI and so don't want to fire the
2857  * callbacks */
2858 static void
2859 block_camera_control_signals (EmpathyCallWindow *self)
2860 {
2861   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2862
2863   g_signal_handlers_block_by_func (priv->tool_button_camera_off,
2864       tool_button_camera_off_toggled_cb, self);
2865   g_signal_handlers_block_by_func (priv->tool_button_camera_preview,
2866       tool_button_camera_preview_toggled_cb, self);
2867   g_signal_handlers_block_by_func (priv->tool_button_camera_on,
2868       tool_button_camera_on_toggled_cb, self);
2869   g_signal_handlers_block_by_func (priv->action_camera,
2870       action_camera_change_cb, self);
2871 }
2872
2873 static void
2874 unblock_camera_control_signals (EmpathyCallWindow *self)
2875 {
2876   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2877
2878   g_signal_handlers_unblock_by_func (priv->tool_button_camera_off,
2879       tool_button_camera_off_toggled_cb, self);
2880   g_signal_handlers_unblock_by_func (priv->tool_button_camera_preview,
2881       tool_button_camera_preview_toggled_cb, self);
2882   g_signal_handlers_unblock_by_func (priv->tool_button_camera_on,
2883       tool_button_camera_on_toggled_cb, self);
2884   g_signal_handlers_unblock_by_func (priv->action_camera,
2885       action_camera_change_cb, self);
2886 }