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