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