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