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