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