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