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