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