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