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