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