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