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