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