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