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