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