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