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