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