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