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