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