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