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