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