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