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