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