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