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