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