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