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