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