]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
CallWindow: destroy the preview clone when restarting the pipeline
[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
1997   g_assert (priv->handler != NULL);
1998
1999   g_object_get (priv->handler, "call-channel", &call, NULL);
2000   priv->outgoing = (call == NULL);
2001   if (call != NULL)
2002     g_object_unref (call);
2003
2004   g_object_get (priv->handler, "target-contact", &priv->contact, NULL);
2005   g_assert (priv->contact != NULL);
2006
2007   empathy_call_window_setup_avatars (self, priv->handler);
2008   empathy_call_window_set_state_connecting (self);
2009
2010   if (!empathy_call_handler_has_initial_video (priv->handler))
2011     {
2012       gtk_toggle_tool_button_set_active (
2013           GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
2014     }
2015   /* If call has InitialVideo, the preview will be started once the call has
2016    * been started (start_call()). */
2017
2018   update_send_codec (self, TRUE);
2019   update_send_codec (self, FALSE);
2020   update_recv_codec (self, TRUE);
2021   update_recv_codec (self, FALSE);
2022
2023   tp_g_signal_connect_object (priv->handler, "notify::send-audio-codec",
2024       G_CALLBACK (send_audio_codec_notify_cb), self, 0);
2025   tp_g_signal_connect_object (priv->handler, "notify::send-video-codec",
2026       G_CALLBACK (send_video_codec_notify_cb), self, 0);
2027   tp_g_signal_connect_object (priv->handler, "notify::recv-audio-codecs",
2028       G_CALLBACK (recv_audio_codecs_notify_cb), self, 0);
2029   tp_g_signal_connect_object (priv->handler, "notify::recv-video-codecs",
2030       G_CALLBACK (recv_video_codecs_notify_cb), self, 0);
2031
2032   tp_g_signal_connect_object (priv->handler, "candidates-changed",
2033       G_CALLBACK (candidates_changed_cb), self, 0);
2034 }
2035
2036 static void empathy_call_window_dispose (GObject *object);
2037 static void empathy_call_window_finalize (GObject *object);
2038
2039 static void
2040 empathy_call_window_set_property (GObject *object,
2041   guint property_id, const GValue *value, GParamSpec *pspec)
2042 {
2043   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2044
2045   switch (property_id)
2046     {
2047       case PROP_CALL_HANDLER:
2048         priv->handler = g_value_dup_object (value);
2049         break;
2050       default:
2051         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2052     }
2053 }
2054
2055 static void
2056 empathy_call_window_get_property (GObject *object,
2057   guint property_id, GValue *value, GParamSpec *pspec)
2058 {
2059   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2060
2061   switch (property_id)
2062     {
2063       case PROP_CALL_HANDLER:
2064         g_value_set_object (value, priv->handler);
2065         break;
2066       default:
2067         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2068     }
2069 }
2070
2071 static void
2072 empathy_call_window_class_init (
2073   EmpathyCallWindowClass *empathy_call_window_class)
2074 {
2075   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
2076   GParamSpec *param_spec;
2077
2078   g_type_class_add_private (empathy_call_window_class,
2079     sizeof (EmpathyCallWindowPriv));
2080
2081   object_class->constructed = empathy_call_window_constructed;
2082   object_class->set_property = empathy_call_window_set_property;
2083   object_class->get_property = empathy_call_window_get_property;
2084
2085   object_class->dispose = empathy_call_window_dispose;
2086   object_class->finalize = empathy_call_window_finalize;
2087
2088   param_spec = g_param_spec_object ("handler",
2089     "handler", "The call handler",
2090     EMPATHY_TYPE_CALL_HANDLER,
2091     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
2092   g_object_class_install_property (object_class,
2093     PROP_CALL_HANDLER, param_spec);
2094 }
2095
2096 void
2097 empathy_call_window_dispose (GObject *object)
2098 {
2099   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2100   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2101
2102   if (priv->dispose_has_run)
2103     return;
2104
2105   priv->dispose_has_run = TRUE;
2106
2107   if (priv->handler != NULL)
2108     {
2109       empathy_call_handler_stop_call (priv->handler);
2110       tp_clear_object (&priv->handler);
2111     }
2112
2113   if (priv->bus_message_source_id != 0)
2114     {
2115       g_source_remove (priv->bus_message_source_id);
2116       priv->bus_message_source_id = 0;
2117     }
2118
2119   if (priv->got_video_src > 0)
2120     {
2121       g_source_remove (priv->got_video_src);
2122       priv->got_video_src = 0;
2123     }
2124
2125   if (priv->inactivity_src > 0)
2126     {
2127       g_source_remove (priv->inactivity_src);
2128       priv->inactivity_src = 0;
2129     }
2130
2131   tp_clear_object (&priv->pipeline);
2132   tp_clear_object (&priv->video_input);
2133   tp_clear_object (&priv->audio_input);
2134   tp_clear_object (&priv->video_tee);
2135   tp_clear_object (&priv->ui_manager);
2136   tp_clear_object (&priv->fullscreen);
2137   tp_clear_object (&priv->camera_monitor);
2138   tp_clear_object (&priv->settings);
2139   tp_clear_object (&priv->sound_mgr);
2140   tp_clear_object (&priv->mic_menu);
2141   tp_clear_object (&priv->camera_menu);
2142
2143   g_list_free_full (priv->notifiers, g_object_unref);
2144
2145   if (priv->timer_id != 0)
2146     g_source_remove (priv->timer_id);
2147   priv->timer_id = 0;
2148
2149   if (priv->contact != NULL)
2150     {
2151       g_signal_handlers_disconnect_by_func (priv->contact,
2152           contact_name_changed_cb, self);
2153       priv->contact = NULL;
2154     }
2155
2156   G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
2157 }
2158
2159 static void
2160 disconnect_video_output_motion_handler (EmpathyCallWindow *self)
2161 {
2162   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2163
2164   if (priv->video_output_motion_handler_id != 0)
2165     {
2166       g_signal_handler_disconnect (G_OBJECT (priv->video_container),
2167           priv->video_output_motion_handler_id);
2168       priv->video_output_motion_handler_id = 0;
2169     }
2170 }
2171
2172 void
2173 empathy_call_window_finalize (GObject *object)
2174 {
2175   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2176   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2177
2178   disconnect_video_output_motion_handler (self);
2179
2180   /* free any data held directly by the object here */
2181   g_mutex_free (priv->lock);
2182
2183   g_timer_destroy (priv->timer);
2184
2185   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
2186 }
2187
2188
2189 EmpathyCallWindow *
2190 empathy_call_window_new (EmpathyCallHandler *handler)
2191 {
2192   return EMPATHY_CALL_WINDOW (
2193     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
2194 }
2195
2196 static void
2197 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
2198   GstElement *conference, gpointer user_data)
2199 {
2200   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2201   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2202   FsElementAddedNotifier *notifier;
2203   GKeyFile *keyfile;
2204
2205   DEBUG ("Conference added");
2206
2207   /* Add notifier to set the various element properties as needed */
2208   notifier = fs_element_added_notifier_new ();
2209   keyfile = fs_utils_get_default_element_properties (conference);
2210
2211   if (keyfile != NULL)
2212     fs_element_added_notifier_set_properties_from_keyfile (notifier, keyfile);
2213
2214   fs_element_added_notifier_add (notifier, GST_BIN (priv->pipeline));
2215
2216   priv->notifiers = g_list_prepend (priv->notifiers, notifier);
2217
2218   gst_bin_add (GST_BIN (priv->pipeline), conference);
2219   gst_element_set_state (conference, GST_STATE_PLAYING);
2220 }
2221
2222 static void
2223 empathy_call_window_conference_removed_cb (EmpathyCallHandler *handler,
2224   GstElement *conference, gpointer user_data)
2225 {
2226   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2227   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2228
2229   gst_bin_remove (GST_BIN (priv->pipeline), conference);
2230   gst_element_set_state (conference, GST_STATE_NULL);
2231 }
2232
2233 static gboolean
2234 empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
2235 {
2236   GstStateChangeReturn state_change_return;
2237   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2238
2239   if (priv->pipeline == NULL)
2240     return TRUE;
2241
2242   if (priv->bus_message_source_id != 0)
2243     {
2244       g_source_remove (priv->bus_message_source_id);
2245       priv->bus_message_source_id = 0;
2246     }
2247
2248   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2249
2250   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
2251         state_change_return == GST_STATE_CHANGE_NO_PREROLL)
2252     {
2253       if (priv->pipeline != NULL)
2254         g_object_unref (priv->pipeline);
2255       priv->pipeline = NULL;
2256
2257       if (priv->audio_output != NULL)
2258         g_object_unref (priv->audio_output);
2259       priv->audio_output = NULL;
2260
2261       if (priv->video_tee != NULL)
2262         g_object_unref (priv->video_tee);
2263       priv->video_tee = NULL;
2264
2265       if (priv->video_preview != NULL)
2266         clutter_actor_destroy (priv->video_preview);
2267       priv->video_preview = NULL;
2268
2269       /* If we destroy the preview while it's being dragged, we won't
2270        * get the ::drag-end signal, so manually destroy the clone */
2271       if (priv->drag_preview != NULL)
2272         {
2273           clutter_actor_destroy (priv->drag_preview);
2274           empathy_call_window_show_preview_rectangles (self, FALSE);
2275           priv->drag_preview = NULL;
2276         }
2277
2278       priv->funnel = NULL;
2279
2280       create_pipeline (self);
2281       /* Call will be started when user will hit the 'redial' button */
2282       priv->start_call_when_playing = FALSE;
2283       gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2284
2285       return TRUE;
2286     }
2287   else
2288     {
2289       g_message ("Error: could not destroy pipeline. Closing call window");
2290       gtk_widget_destroy (GTK_WIDGET (self));
2291
2292       return FALSE;
2293     }
2294 }
2295
2296 static void
2297 reset_details_pane (EmpathyCallWindow *self)
2298 {
2299   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2300
2301   gtk_label_set_text (GTK_LABEL (priv->vcodec_encoding_label), _("Unknown"));
2302   gtk_label_set_text (GTK_LABEL (priv->acodec_encoding_label), _("Unknown"));
2303   gtk_label_set_text (GTK_LABEL (priv->vcodec_decoding_label), _("Unknown"));
2304   gtk_label_set_text (GTK_LABEL (priv->acodec_decoding_label), _("Unknown"));
2305 }
2306
2307 static gboolean
2308 empathy_call_window_disconnected (EmpathyCallWindow *self,
2309     gboolean restart)
2310 {
2311   gboolean could_disconnect = FALSE;
2312   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2313   gboolean could_reset_pipeline;
2314
2315   /* Leave full screen mode if needed */
2316   gtk_window_unfullscreen (GTK_WINDOW (self));
2317
2318   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
2319   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
2320
2321   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
2322
2323   if (priv->call_state == CONNECTING)
2324       empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2325
2326   if (priv->call_state != REDIALING)
2327     priv->call_state = DISCONNECTED;
2328
2329   /* Show the toolbar */
2330   clutter_state_set_state (priv->transitions, "fade-in");
2331
2332   if (could_reset_pipeline)
2333     {
2334       g_mutex_lock (priv->lock);
2335
2336       g_timer_stop (priv->timer);
2337
2338       if (priv->timer_id != 0)
2339         g_source_remove (priv->timer_id);
2340       priv->timer_id = 0;
2341
2342       g_mutex_unlock (priv->lock);
2343
2344       if (!restart)
2345         /* We are about to destroy the window, no need to update it or create
2346          * a video preview */
2347         return TRUE;
2348
2349       empathy_call_window_status_message (self, _("Disconnected"));
2350
2351       empathy_call_window_show_hangup_button (self, FALSE);
2352
2353       /* Unsensitive the camera and mic button */
2354       gtk_widget_set_sensitive (priv->camera_button, FALSE);
2355       gtk_widget_set_sensitive (priv->mic_button, FALSE);
2356
2357       /* Be sure that the mic button is enabled */
2358       gtk_toggle_tool_button_set_active (
2359           GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
2360
2361       if (priv->camera_state == CAMERA_STATE_ON)
2362         {
2363           /* Restart the preview with the new pipeline. */
2364           display_video_preview (self, TRUE);
2365         }
2366
2367       /* destroy the video output; it will be recreated when we'll redial */
2368       disconnect_video_output_motion_handler (self);
2369       if (priv->video_output != NULL)
2370         clutter_actor_destroy (priv->video_output);
2371       priv->video_output = NULL;
2372       if (priv->got_video_src > 0)
2373         {
2374           g_source_remove (priv->got_video_src);
2375           priv->got_video_src = 0;
2376         }
2377
2378       gtk_widget_show (priv->remote_user_avatar_widget);
2379
2380       reset_details_pane (self);
2381
2382       priv->sending_video = FALSE;
2383       priv->call_started = FALSE;
2384
2385       could_disconnect = TRUE;
2386
2387       /* TODO: display the self avatar of the preview (depends if the "Always
2388        * Show Video Preview" is enabled or not) */
2389     }
2390
2391   return could_disconnect;
2392 }
2393
2394
2395 static void
2396 empathy_call_window_channel_closed_cb (EmpathyCallHandler *handler,
2397     gpointer user_data)
2398 {
2399   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2400   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2401
2402   if (empathy_call_window_disconnected (self, TRUE) &&
2403       priv->call_state == REDIALING)
2404       empathy_call_window_restart_call (self);
2405 }
2406
2407 static gboolean
2408 empathy_call_window_sink_removed_cb (EmpathyCallHandler *handler,
2409     GstPad *sink,
2410     FsMediaType media_type,
2411     EmpathyCallWindow *self)
2412 {
2413   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2414
2415   DEBUG ("removing content");
2416
2417   /*
2418    * This assumes that there is only one video stream per channel...
2419    */
2420
2421   if ((guint) media_type == FS_MEDIA_TYPE_VIDEO)
2422     {
2423       if (priv->funnel != NULL)
2424         {
2425           GstElement *output;
2426
2427           output = priv->video_output_sink;
2428
2429           gst_element_set_state (output, GST_STATE_NULL);
2430           gst_element_set_state (priv->funnel, GST_STATE_NULL);
2431
2432           gst_bin_remove (GST_BIN (priv->pipeline), output);
2433           gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2434           priv->funnel = NULL;
2435           return TRUE;
2436         }
2437     }
2438   else if (media_type == FS_MEDIA_TYPE_AUDIO)
2439     {
2440       if (priv->audio_output != NULL)
2441         {
2442           gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2443
2444           gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2445           priv->audio_output = NULL;
2446           return TRUE;
2447         }
2448     }
2449
2450   return FALSE;
2451 }
2452
2453 /* Called with global lock held */
2454 static GstPad *
2455 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
2456 {
2457   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2458   GstPad *pad;
2459   GstElement *output;
2460
2461   if (priv->funnel == NULL)
2462     {
2463       output = priv->video_output_sink;
2464
2465       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
2466
2467       if (!priv->funnel)
2468         {
2469           g_warning ("Could not create fsfunnel");
2470           return NULL;
2471         }
2472
2473       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->funnel))
2474         {
2475           gst_object_unref (priv->funnel);
2476           priv->funnel = NULL;
2477           g_warning ("Could  not add funnel to pipeline");
2478           return NULL;
2479         }
2480
2481       if (!gst_bin_add (GST_BIN (priv->pipeline), output))
2482         {
2483           g_warning ("Could not add the video output widget to the pipeline");
2484           goto error;
2485         }
2486
2487       if (!gst_element_link (priv->funnel, output))
2488         {
2489           g_warning ("Could not link output sink to funnel");
2490           goto error_output_added;
2491         }
2492
2493       if (gst_element_set_state (output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2494         {
2495           g_warning ("Could not start video sink");
2496           goto error_output_added;
2497         }
2498
2499       if (gst_element_set_state (priv->funnel, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2500         {
2501           g_warning ("Could not start funnel");
2502           goto error_output_added;
2503         }
2504     }
2505
2506   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
2507
2508   if (!pad)
2509     g_warning ("Could not get request pad from funnel");
2510
2511   return pad;
2512
2513
2514  error_output_added:
2515
2516   gst_element_set_locked_state (priv->funnel, TRUE);
2517   gst_element_set_locked_state (output, TRUE);
2518
2519   gst_element_set_state (priv->funnel, GST_STATE_NULL);
2520   gst_element_set_state (output, GST_STATE_NULL);
2521
2522   gst_bin_remove (GST_BIN (priv->pipeline), output);
2523   gst_element_set_locked_state (output, FALSE);
2524
2525  error:
2526
2527   gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2528   priv->funnel = NULL;
2529
2530   return NULL;
2531 }
2532
2533 /* Called with global lock held */
2534 static GstPad *
2535 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self)
2536 {
2537   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2538   GstPad *pad;
2539   GstPadTemplate *template;
2540
2541   if (priv->audio_output == NULL)
2542     {
2543       priv->audio_output = empathy_audio_sink_new ();
2544       g_object_ref_sink (priv->audio_output);
2545
2546       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output))
2547         {
2548           g_warning ("Could not add audio sink to pipeline");
2549           g_object_unref (priv->audio_output);
2550           goto error_add_output;
2551         }
2552
2553       if (gst_element_set_state (priv->audio_output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2554         {
2555           g_warning ("Could not start audio sink");
2556           goto error;
2557         }
2558     }
2559
2560   template = gst_element_class_get_pad_template (
2561     GST_ELEMENT_GET_CLASS (priv->audio_output), "sink%d");
2562
2563   pad = gst_element_request_pad (priv->audio_output,
2564     template, NULL, NULL);
2565
2566   if (pad == NULL)
2567     {
2568       g_warning ("Could not get sink pad from sink");
2569       return NULL;
2570     }
2571
2572   return pad;
2573
2574 error:
2575   gst_element_set_locked_state (priv->audio_output, TRUE);
2576   gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2577   gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2578   priv->audio_output = NULL;
2579
2580 error_add_output:
2581
2582   return NULL;
2583 }
2584
2585 static gboolean
2586 empathy_call_window_update_timer (gpointer user_data)
2587 {
2588   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2589   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2590   const gchar *status;
2591   gchar *str;
2592   gdouble time_;
2593
2594   time_ = g_timer_elapsed (priv->timer, NULL);
2595
2596   if (priv->call_state == HELD)
2597     status = _("On hold");
2598   else if (!gtk_toggle_tool_button_get_active (
2599       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
2600     status = _("Mute");
2601   else
2602     status = _("Duration");
2603
2604   /* Translators: 'status - minutes:seconds' the caller has been connected */
2605   str = g_strdup_printf (_("%s â€” %d:%02dm"),
2606       status,
2607       (int) time_ / 60, (int) time_ % 60);
2608   empathy_call_window_status_message (self, str);
2609   g_free (str);
2610
2611   return TRUE;
2612 }
2613
2614 #if 0
2615 static void
2616 display_error (EmpathyCallWindow *self,
2617     TpyCallChannel *call,
2618     const gchar *img,
2619     const gchar *title,
2620     const gchar *desc,
2621     const gchar *details)
2622 {
2623   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2624   GtkWidget *info_bar;
2625   GtkWidget *content_area;
2626   GtkWidget *hbox;
2627   GtkWidget *vbox;
2628   GtkWidget *image;
2629   GtkWidget *label;
2630   gchar *txt;
2631
2632   /* Create info bar */
2633   info_bar = gtk_info_bar_new_with_buttons (GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2634       NULL);
2635
2636   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
2637
2638   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2639
2640   /* hbox containing the image and the messages vbox */
2641   hbox = gtk_hbox_new (FALSE, 3);
2642   gtk_container_add (GTK_CONTAINER (content_area), hbox);
2643
2644   /* Add image */
2645   image = gtk_image_new_from_icon_name (img, GTK_ICON_SIZE_DIALOG);
2646   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2647
2648   /* vbox containing the main message and the details expander */
2649   vbox = gtk_vbox_new (FALSE, 3);
2650   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
2651
2652   /* Add text */
2653   txt = g_strdup_printf ("<b>%s</b>\n%s", title, desc);
2654
2655   label = gtk_label_new (NULL);
2656   gtk_label_set_markup (GTK_LABEL (label), txt);
2657   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2658   gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2659   g_free (txt);
2660
2661   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
2662
2663   /* Add details */
2664   if (details != NULL)
2665     {
2666       GtkWidget *expander;
2667
2668       expander = gtk_expander_new (_("Technical Details"));
2669
2670       txt = g_strdup_printf ("<i>%s</i>", details);
2671
2672       label = gtk_label_new (NULL);
2673       gtk_label_set_markup (GTK_LABEL (label), txt);
2674       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2675       gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2676       g_free (txt);
2677
2678       gtk_container_add (GTK_CONTAINER (expander), label);
2679       gtk_box_pack_start (GTK_BOX (vbox), expander, TRUE, TRUE, 0);
2680     }
2681
2682   g_signal_connect (info_bar, "response",
2683       G_CALLBACK (gtk_widget_destroy), NULL);
2684
2685   gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar,
2686       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
2687   gtk_widget_show_all (info_bar);
2688 }
2689
2690 static gchar *
2691 media_stream_error_to_txt (EmpathyCallWindow *self,
2692     TpyCallChannel *call,
2693     gboolean audio,
2694     TpMediaStreamError error)
2695 {
2696   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2697   const gchar *cm = NULL;
2698   gchar *url;
2699   gchar *result;
2700
2701   switch (error)
2702     {
2703       case TP_MEDIA_STREAM_ERROR_CODEC_NEGOTIATION_FAILED:
2704         if (audio)
2705           return g_strdup_printf (
2706               _("%s's software does not understand any of the audio formats "
2707                 "supported by your computer"),
2708             empathy_contact_get_alias (priv->contact));
2709         else
2710           return g_strdup_printf (
2711               _("%s's software does not understand any of the video formats "
2712                 "supported by your computer"),
2713             empathy_contact_get_alias (priv->contact));
2714
2715       case TP_MEDIA_STREAM_ERROR_CONNECTION_FAILED:
2716         return g_strdup_printf (
2717             _("Can't establish a connection to %s. "
2718               "One of you might be on a network that does not allow "
2719               "direct connections."),
2720           empathy_contact_get_alias (priv->contact));
2721
2722       case TP_MEDIA_STREAM_ERROR_NETWORK_ERROR:
2723           return g_strdup (_("There was a failure on the network"));
2724
2725       case TP_MEDIA_STREAM_ERROR_NO_CODECS:
2726         if (audio)
2727           return g_strdup (_("The audio formats necessary for this call "
2728                 "are not installed on your computer"));
2729         else
2730           return g_strdup (_("The video formats necessary for this call "
2731                 "are not installed on your computer"));
2732
2733       case TP_MEDIA_STREAM_ERROR_INVALID_CM_BEHAVIOR:
2734         tp_connection_parse_object_path (
2735             tp_channel_borrow_connection (TP_CHANNEL (call)),
2736             NULL, &cm);
2737
2738         url = g_strdup_printf ("http://bugs.freedesktop.org/enter_bug.cgi?"
2739             "product=Telepathy&amp;component=%s", cm);
2740
2741         result = g_strdup_printf (
2742             _("Something unexpected happened in a Telepathy component. "
2743               "Please <a href=\"%s\">report this bug</a> and attach "
2744               "logs gathered from the 'Debug' window in the Help menu."), url);
2745
2746         g_free (url);
2747         g_free (cm);
2748         return result;
2749
2750       case TP_MEDIA_STREAM_ERROR_MEDIA_ERROR:
2751         return g_strdup (_("There was a failure in the call engine"));
2752
2753       case TP_MEDIA_STREAM_ERROR_EOS:
2754         return g_strdup (_("The end of the stream was reached"));
2755
2756       case TP_MEDIA_STREAM_ERROR_UNKNOWN:
2757       default:
2758         return NULL;
2759     }
2760 }
2761
2762 static void
2763 empathy_call_window_stream_error (EmpathyCallWindow *self,
2764     TpyCallChannel *call,
2765     gboolean audio,
2766     guint code,
2767     const gchar *msg,
2768     const gchar *icon,
2769     const gchar *title)
2770 {
2771   gchar *desc;
2772
2773   desc = media_stream_error_to_txt (self, call, audio, code);
2774   if (desc == NULL)
2775     {
2776       /* No description, use the error message. That's not great as it's not
2777        * localized but it's better than nothing. */
2778       display_error (self, call, icon, title, msg, NULL);
2779     }
2780   else
2781     {
2782       display_error (self, call, icon, title, desc, msg);
2783       g_free (desc);
2784     }
2785 }
2786
2787 static void
2788 empathy_call_window_audio_stream_error (TpyCallChannel *call,
2789     guint code,
2790     const gchar *msg,
2791     EmpathyCallWindow *self)
2792 {
2793   empathy_call_window_stream_error (self, call, TRUE, code, msg,
2794       "gnome-stock-mic", _("Can't establish audio stream"));
2795 }
2796
2797 static void
2798 empathy_call_window_video_stream_error (TpyCallChannel *call,
2799     guint code,
2800     const gchar *msg,
2801     EmpathyCallWindow *self)
2802 {
2803   empathy_call_window_stream_error (self, call, FALSE, code, msg,
2804       "camera-web", _("Can't establish video stream"));
2805 }
2806 #endif
2807
2808 static void
2809 empathy_call_window_state_changed_cb (EmpathyCallHandler *handler,
2810     TpyCallState state,
2811     EmpathyCallWindow *self)
2812 {
2813   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2814   TpyCallChannel *call;
2815   gboolean can_send_video;
2816
2817   if (state != TPY_CALL_STATE_ACCEPTED)
2818     return;
2819
2820   if (priv->call_state == CONNECTED)
2821     return;
2822
2823   g_timer_start (priv->timer);
2824   priv->call_state = CONNECTED;
2825
2826   empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2827
2828   can_send_video = priv->video_input != NULL &&
2829     empathy_contact_can_voip_video (priv->contact) &&
2830     empathy_camera_monitor_get_available (priv->camera_monitor);
2831
2832   g_object_get (priv->handler, "call-channel", &call, NULL);
2833
2834   if (tpy_call_channel_has_dtmf (call))
2835     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
2836
2837   if (priv->video_input == NULL)
2838     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
2839
2840   gtk_widget_set_sensitive (priv->camera_button, can_send_video);
2841
2842   empathy_call_window_show_hangup_button (self, TRUE);
2843
2844   gtk_widget_set_sensitive (priv->mic_button, TRUE);
2845
2846   clutter_actor_hide (priv->video_output);
2847   gtk_widget_show (priv->remote_user_avatar_widget);
2848
2849   g_object_unref (call);
2850
2851   g_mutex_lock (priv->lock);
2852
2853   priv->timer_id = g_timeout_add_seconds (1,
2854     empathy_call_window_update_timer, self);
2855
2856   g_mutex_unlock (priv->lock);
2857
2858   empathy_call_window_update_timer (self);
2859
2860   gtk_action_set_sensitive (priv->menu_fullscreen, TRUE);
2861 }
2862
2863 static gboolean
2864 empathy_call_window_show_video_output_cb (gpointer user_data)
2865 {
2866   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2867
2868   if (self->priv->video_output != NULL)
2869     {
2870       gtk_widget_hide (self->priv->remote_user_avatar_widget);
2871       clutter_actor_show (self->priv->video_output);
2872       empathy_call_window_raise_actors (self);
2873     }
2874
2875   return FALSE;
2876 }
2877
2878 static gboolean
2879 empathy_call_window_check_video_cb (gpointer data)
2880 {
2881   EmpathyCallWindow *self = data;
2882
2883   if (self->priv->got_video)
2884     {
2885       self->priv->got_video = FALSE;
2886       return TRUE;
2887     }
2888
2889   /* No video in the last N seconds, display the remote avatar */
2890   empathy_call_window_show_video_output (self, FALSE);
2891
2892   return TRUE;
2893 }
2894
2895 /* Called from the streaming thread */
2896 static gboolean
2897 empathy_call_window_video_probe_cb (GstPad *pad,
2898     GstMiniObject *mini_obj,
2899     EmpathyCallWindow *self)
2900 {
2901   /* Ignore events */
2902   if (GST_IS_EVENT (mini_obj))
2903     return TRUE;
2904
2905   if (G_UNLIKELY (!self->priv->got_video))
2906     {
2907       /* show the remote video */
2908       g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
2909           empathy_call_window_show_video_output_cb,
2910           g_object_ref (self), g_object_unref);
2911
2912       self->priv->got_video = TRUE;
2913     }
2914
2915   return TRUE;
2916 }
2917
2918 /* Called from the streaming thread */
2919 static gboolean
2920 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
2921   GstPad *src, guint media_type, gpointer user_data)
2922 {
2923   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2924   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2925   gboolean retval = FALSE;
2926
2927   GstPad *pad;
2928
2929   g_mutex_lock (priv->lock);
2930
2931   switch (media_type)
2932     {
2933       case TP_MEDIA_STREAM_TYPE_AUDIO:
2934         pad = empathy_call_window_get_audio_sink_pad (self);
2935         break;
2936       case TP_MEDIA_STREAM_TYPE_VIDEO:
2937         g_idle_add (empathy_call_window_show_video_output_cb, self);
2938         pad = empathy_call_window_get_video_sink_pad (self);
2939
2940         gst_pad_add_data_probe (src,
2941             G_CALLBACK (empathy_call_window_video_probe_cb), self);
2942         if (priv->got_video_src > 0)
2943           g_source_remove (priv->got_video_src);
2944         priv->got_video_src = g_timeout_add_seconds (5,
2945             empathy_call_window_check_video_cb, self);
2946         break;
2947       default:
2948         g_assert_not_reached ();
2949     }
2950
2951   if (pad == NULL)
2952     goto out;
2953
2954   if (GST_PAD_LINK_FAILED (gst_pad_link (src, pad)))
2955       g_warning ("Could not link %s sink pad",
2956           media_type == TP_MEDIA_STREAM_TYPE_AUDIO ? "audio" : "video");
2957   else
2958       retval = TRUE;
2959
2960   gst_object_unref (pad);
2961
2962  out:
2963
2964   /* If no sink could be linked, try to add fakesink to prevent the whole call
2965    * aborting */
2966
2967   if (!retval)
2968     {
2969       GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
2970
2971       if (gst_bin_add (GST_BIN (priv->pipeline), fakesink))
2972         {
2973           GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
2974           if (gst_element_set_state (fakesink, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE ||
2975               GST_PAD_LINK_FAILED (gst_pad_link (src, sinkpad)))
2976             {
2977               gst_element_set_locked_state (fakesink, TRUE);
2978               gst_element_set_state (fakesink, GST_STATE_NULL);
2979               gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2980             }
2981           else
2982             {
2983               DEBUG ("Could not link real sink, linked fakesink instead");
2984             }
2985           gst_object_unref (sinkpad);
2986         }
2987       else
2988         {
2989           gst_object_unref (fakesink);
2990         }
2991     }
2992
2993
2994   g_mutex_unlock (priv->lock);
2995
2996   return TRUE;
2997 }
2998
2999 static gboolean
3000 empathy_call_window_sink_added_cb (EmpathyCallHandler *handler,
3001   GstPad *sink, FsMediaType media_type, gpointer user_data)
3002 {
3003   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3004   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3005   GstPad *pad;
3006   gboolean retval = FALSE;
3007
3008   switch (media_type)
3009     {
3010       case FS_MEDIA_TYPE_AUDIO:
3011         if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input))
3012           {
3013             g_warning ("Could not add audio source to pipeline");
3014             break;
3015           }
3016
3017         pad = gst_element_get_static_pad (priv->audio_input, "src");
3018         if (!pad)
3019           {
3020             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3021             g_warning ("Could not get source pad from audio source");
3022             break;
3023           }
3024
3025         if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3026           {
3027             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3028             g_warning ("Could not link audio source to farsight");
3029             break;
3030           }
3031
3032         if (gst_element_set_state (priv->audio_input, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
3033           {
3034             g_warning ("Could not start audio source");
3035             gst_element_set_state (priv->audio_input, GST_STATE_NULL);
3036             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3037             break;
3038           }
3039
3040         retval = TRUE;
3041         break;
3042       case FS_MEDIA_TYPE_VIDEO:
3043         if (priv->video_tee != NULL)
3044           {
3045             pad = gst_element_get_request_pad (priv->video_tee, "src%d");
3046             if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3047               {
3048                 g_warning ("Could not link video source input pipeline");
3049                 break;
3050               }
3051             gst_object_unref (pad);
3052           }
3053
3054         retval = TRUE;
3055         break;
3056       default:
3057         g_assert_not_reached ();
3058     }
3059
3060   return retval;
3061 }
3062
3063 static void
3064 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
3065 {
3066   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3067   GstElement *preview;
3068
3069   disable_camera (self);
3070
3071   DEBUG ("remove video input");
3072   preview = priv->video_preview_sink;
3073
3074   gst_element_set_state (priv->video_input, GST_STATE_NULL);
3075   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
3076   gst_element_set_state (preview, GST_STATE_NULL);
3077
3078   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
3079     preview, NULL);
3080
3081   g_object_unref (priv->video_input);
3082   priv->video_input = NULL;
3083   g_object_unref (priv->video_tee);
3084   priv->video_tee = NULL;
3085   clutter_actor_destroy (priv->video_preview);
3086   priv->video_preview = NULL;
3087
3088   gtk_widget_set_sensitive (priv->camera_button, FALSE);
3089 }
3090
3091 static void
3092 start_call (EmpathyCallWindow *self)
3093 {
3094   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3095
3096   priv->call_started = TRUE;
3097   empathy_call_handler_start_call (priv->handler,
3098       gtk_get_current_event_time ());
3099
3100   if (empathy_call_handler_has_initial_video (priv->handler))
3101     {
3102       TpyCallChannel *call;
3103       TpySendingState s;
3104
3105       g_object_get (priv->handler, "call-channel", &call, NULL);
3106       s = tpy_call_channel_get_video_state (call);
3107
3108       if (s == TPY_SENDING_STATE_PENDING_SEND ||
3109           s == TPY_SENDING_STATE_SENDING)
3110         {
3111           /* Enable 'send video' buttons and display the preview */
3112           gtk_toggle_tool_button_set_active (
3113             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), TRUE);
3114         }
3115       else
3116         {
3117           gtk_toggle_tool_button_set_active (
3118             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
3119
3120           if (priv->video_preview == NULL)
3121             {
3122               create_video_preview (self);
3123               add_video_preview_to_pipeline (self);
3124             }
3125         }
3126
3127       g_object_unref (call);
3128     }
3129 }
3130
3131 static gboolean
3132 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
3133   gpointer user_data)
3134 {
3135   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3136   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3137   GstState newstate;
3138
3139   empathy_call_handler_bus_message (priv->handler, bus, message);
3140
3141   switch (GST_MESSAGE_TYPE (message))
3142     {
3143       case GST_MESSAGE_STATE_CHANGED:
3144         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
3145           {
3146             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3147           }
3148         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
3149             !priv->call_started)
3150           {
3151             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3152             if (newstate == GST_STATE_PAUSED)
3153               {
3154                 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3155                 priv->pipeline_playing = TRUE;
3156
3157                 if (priv->start_call_when_playing)
3158                   start_call (self);
3159               }
3160           }
3161         break;
3162       case GST_MESSAGE_ERROR:
3163         {
3164           GError *error = NULL;
3165           GstElement *gst_error;
3166           gchar *debug;
3167
3168           gst_message_parse_error (message, &error, &debug);
3169           gst_error = GST_ELEMENT (GST_MESSAGE_SRC (message));
3170
3171           g_message ("Element error: %s -- %s\n", error->message, debug);
3172
3173           if (g_str_has_prefix (gst_element_get_name (gst_error),
3174                 VIDEO_INPUT_ERROR_PREFIX))
3175             {
3176               /* Remove the video input and continue */
3177               if (priv->video_input != NULL)
3178                 empathy_call_window_remove_video_input (self);
3179               gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3180             }
3181           else
3182             {
3183               empathy_call_window_disconnected (self, TRUE);
3184             }
3185           g_error_free (error);
3186           g_free (debug);
3187         }
3188       case GST_MESSAGE_UNKNOWN:
3189       case GST_MESSAGE_EOS:
3190       case GST_MESSAGE_WARNING:
3191       case GST_MESSAGE_INFO:
3192       case GST_MESSAGE_TAG:
3193       case GST_MESSAGE_BUFFERING:
3194       case GST_MESSAGE_STATE_DIRTY:
3195       case GST_MESSAGE_STEP_DONE:
3196       case GST_MESSAGE_CLOCK_PROVIDE:
3197       case GST_MESSAGE_CLOCK_LOST:
3198       case GST_MESSAGE_NEW_CLOCK:
3199       case GST_MESSAGE_STRUCTURE_CHANGE:
3200       case GST_MESSAGE_STREAM_STATUS:
3201       case GST_MESSAGE_APPLICATION:
3202       case GST_MESSAGE_ELEMENT:
3203       case GST_MESSAGE_SEGMENT_START:
3204       case GST_MESSAGE_SEGMENT_DONE:
3205       case GST_MESSAGE_DURATION:
3206       case GST_MESSAGE_ANY:
3207       default:
3208         break;
3209     }
3210
3211   return TRUE;
3212 }
3213
3214 static void
3215 empathy_call_window_members_changed_cb (TpyCallChannel *call,
3216     GHashTable *members,
3217     EmpathyCallWindow *self)
3218 {
3219   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3220   GHashTableIter iter;
3221   gpointer key, value;
3222   gboolean held = FALSE;
3223
3224   g_hash_table_iter_init (&iter, members);
3225   while (g_hash_table_iter_next (&iter, &key, &value))
3226     {
3227       if (GPOINTER_TO_INT (value) & TPY_CALL_MEMBER_FLAG_HELD)
3228         {
3229           /* This assumes this is a 1-1 call, otherwise one participant
3230            * putting the call on hold wouldn't mean the call is on hold
3231            * for everyone. */
3232           held = TRUE;
3233           break;
3234         }
3235     }
3236
3237   if (held)
3238     priv->call_state = HELD;
3239   else if (priv->call_state == HELD)
3240     priv->call_state = CONNECTED;
3241 }
3242
3243 static void
3244 call_handler_notify_call_cb (EmpathyCallHandler *handler,
3245     GParamSpec *spec,
3246     EmpathyCallWindow *self)
3247 {
3248   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3249   TpyCallChannel *call;
3250
3251   g_object_get (priv->handler, "call-channel", &call, NULL);
3252   if (call == NULL)
3253     return;
3254
3255 /* FIXME
3256   tp_g_signal_connect_object (call, "audio-stream-error",
3257       G_CALLBACK (empathy_call_window_audio_stream_error), self, 0);
3258   tp_g_signal_connect_object (call, "video-stream-error",
3259       G_CALLBACK (empathy_call_window_video_stream_error), self, 0);
3260 */
3261
3262   tp_g_signal_connect_object (call, "members-changed",
3263       G_CALLBACK (empathy_call_window_members_changed_cb), self, 0);
3264
3265   g_object_unref (call);
3266 }
3267
3268 static void
3269 empathy_call_window_realized_cb (GtkWidget *widget, EmpathyCallWindow *window)
3270 {
3271   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3272   TpyCallChannel *call;
3273   gint width;
3274
3275   /* Make the hangup button twice as wide */
3276   width = gtk_widget_get_allocated_width (priv->hangup_button);
3277   gtk_widget_set_size_request (priv->hangup_button, width * 2, -1);
3278
3279   g_signal_connect (priv->handler, "state-changed",
3280     G_CALLBACK (empathy_call_window_state_changed_cb), window);
3281   g_signal_connect (priv->handler, "conference-added",
3282     G_CALLBACK (empathy_call_window_conference_added_cb), window);
3283   g_signal_connect (priv->handler, "conference-removed",
3284     G_CALLBACK (empathy_call_window_conference_removed_cb), window);
3285   g_signal_connect (priv->handler, "closed",
3286     G_CALLBACK (empathy_call_window_channel_closed_cb), window);
3287   g_signal_connect (priv->handler, "src-pad-added",
3288     G_CALLBACK (empathy_call_window_src_added_cb), window);
3289   g_signal_connect (priv->handler, "sink-pad-added",
3290     G_CALLBACK (empathy_call_window_sink_added_cb), window);
3291   g_signal_connect (priv->handler, "sink-pad-removed",
3292     G_CALLBACK (empathy_call_window_sink_removed_cb), window);
3293
3294   g_object_get (priv->handler, "call-channel", &call, NULL);
3295   if (call != NULL)
3296     {
3297       call_handler_notify_call_cb (priv->handler, NULL, window);
3298       g_object_unref (call);
3299     }
3300   else
3301     {
3302       /* call-channel doesn't exist yet, we'll connect signals once it has been
3303        * set */
3304       g_signal_connect (priv->handler, "notify::call-channel",
3305         G_CALLBACK (call_handler_notify_call_cb), window);
3306     }
3307
3308   gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
3309 }
3310
3311 static gboolean
3312 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
3313   EmpathyCallWindow *window)
3314 {
3315   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3316
3317   if (priv->pipeline != NULL)
3318     {
3319       if (priv->bus_message_source_id != 0)
3320         {
3321           g_source_remove (priv->bus_message_source_id);
3322           priv->bus_message_source_id = 0;
3323         }
3324
3325       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
3326     }
3327
3328   if (priv->call_state == CONNECTING)
3329     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
3330
3331   return FALSE;
3332 }
3333
3334 static void
3335 show_controls (EmpathyCallWindow *window, gboolean set_fullscreen)
3336 {
3337   GtkWidget *menu;
3338   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3339
3340   menu = gtk_ui_manager_get_widget (priv->ui_manager,
3341             "/menubar1");
3342
3343   if (set_fullscreen)
3344     {
3345       gtk_widget_hide (priv->dtmf_panel);
3346       gtk_widget_hide (menu);
3347       gtk_widget_hide (priv->toolbar);
3348     }
3349   else
3350     {
3351       if (priv->dialpad_was_visible_before_fs)
3352         gtk_widget_show (priv->dtmf_panel);
3353
3354       gtk_widget_show (menu);
3355       gtk_widget_show (priv->toolbar);
3356
3357       gtk_window_resize (GTK_WINDOW (window), priv->original_width_before_fs,
3358           priv->original_height_before_fs);
3359     }
3360 }
3361
3362 static void
3363 show_borders (EmpathyCallWindow *window, gboolean set_fullscreen)
3364 {
3365   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3366
3367   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
3368       set_fullscreen ? 0 : CONTENT_HBOX_BORDER_WIDTH);
3369   gtk_box_set_spacing (GTK_BOX (priv->content_hbox),
3370       set_fullscreen ? 0 : CONTENT_HBOX_SPACING);
3371
3372   if (priv->video_output != NULL)
3373     {
3374 #if 0
3375       gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
3376           priv->video_output, TRUE, TRUE,
3377           set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
3378           GTK_PACK_START);
3379 #endif
3380     }
3381 }
3382
3383 static gboolean
3384 empathy_call_window_state_event_cb (GtkWidget *widget,
3385   GdkEventWindowState *event, EmpathyCallWindow *window)
3386 {
3387   if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
3388     {
3389       EmpathyCallWindowPriv *priv = GET_PRIV (window);
3390       gboolean set_fullscreen = event->new_window_state &
3391         GDK_WINDOW_STATE_FULLSCREEN;
3392
3393       if (set_fullscreen)
3394         {
3395           gboolean dialpad_was_visible;
3396           GtkAllocation allocation;
3397           gint original_width, original_height;
3398
3399           gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3400           original_width = allocation.width;
3401           original_height = allocation.height;
3402
3403           g_object_get (priv->dtmf_panel,
3404               "visible", &dialpad_was_visible,
3405               NULL);
3406
3407           priv->dialpad_was_visible_before_fs = dialpad_was_visible;
3408           priv->original_width_before_fs = original_width;
3409           priv->original_height_before_fs = original_height;
3410
3411           if (priv->video_output_motion_handler_id == 0 &&
3412                 priv->video_output != NULL)
3413             {
3414               priv->video_output_motion_handler_id = g_signal_connect (
3415                   G_OBJECT (priv->video_container), "motion-notify-event",
3416                   G_CALLBACK (empathy_call_window_video_output_motion_notify),
3417                   window);
3418             }
3419         }
3420       else
3421         {
3422           disconnect_video_output_motion_handler (window);
3423         }
3424
3425       empathy_call_window_fullscreen_set_fullscreen (priv->fullscreen,
3426           set_fullscreen);
3427       show_controls (window, set_fullscreen);
3428       show_borders (window, set_fullscreen);
3429       gtk_action_set_stock_id (priv->menu_fullscreen,
3430           (set_fullscreen ? "gtk-leave-fullscreen" : "gtk-fullscreen"));
3431       priv->is_fullscreen = set_fullscreen;
3432   }
3433
3434   return FALSE;
3435 }
3436
3437 static void
3438 empathy_call_window_show_dialpad (EmpathyCallWindow *window,
3439     gboolean active)
3440 {
3441   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3442   int w, h, dialpad_width;
3443   GtkAllocation allocation;
3444
3445   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3446   w = allocation.width;
3447   h = allocation.height;
3448
3449   gtk_widget_get_preferred_width (priv->dtmf_panel, &dialpad_width, NULL);
3450
3451   if (active)
3452     {
3453       gtk_widget_show (priv->dtmf_panel);
3454       w += dialpad_width;
3455     }
3456   else
3457     {
3458       w -= dialpad_width;
3459       gtk_widget_hide (priv->dtmf_panel);
3460     }
3461
3462   if (w > 0 && h > 0)
3463     gtk_window_resize (GTK_WINDOW (window), w, h);
3464 }
3465
3466 static void
3467 empathy_call_window_set_send_video (EmpathyCallWindow *window,
3468   CameraState state)
3469 {
3470   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3471   TpyCallChannel *call;
3472
3473   priv->sending_video = (state == CAMERA_STATE_ON);
3474
3475   if (state == CAMERA_STATE_ON)
3476     {
3477       /* When we start sending video, we want to show the video preview by
3478          default. */
3479       display_video_preview (window, TRUE);
3480     }
3481   else
3482     {
3483       display_video_preview (window, FALSE);
3484     }
3485
3486   if (priv->call_state != CONNECTED)
3487     return;
3488
3489   g_object_get (priv->handler, "call-channel", &call, NULL);
3490   DEBUG ("%s sending video", priv->sending_video ? "start": "stop");
3491   tpy_call_channel_send_video (call, priv->sending_video);
3492   g_object_unref (call);
3493 }
3494
3495 static void
3496 empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
3497   EmpathyCallWindow *self)
3498 {
3499   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3500   gboolean active;
3501
3502   active = (gtk_toggle_tool_button_get_active (toggle));
3503
3504   /* We don't want the settings callback to react to this change to avoid
3505    * a loop. */
3506   g_signal_handlers_block_by_func (priv->settings,
3507       empathy_call_window_prefs_volume_changed_cb, self);
3508
3509   if (active)
3510     {
3511       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
3512           priv->volume * 100);
3513     }
3514   else
3515     {
3516       /* TODO, Instead of setting the input volume to 0 we should probably
3517        * stop sending but this would cause the audio call to drop if both
3518        * sides mute at the same time on certain CMs AFAIK. Need to revisit this
3519        * in the future. GNOME #574574
3520        */
3521       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
3522           0);
3523     }
3524
3525     g_signal_handlers_unblock_by_func (priv->settings,
3526       empathy_call_window_prefs_volume_changed_cb, self);
3527 }
3528
3529 static void
3530 empathy_call_window_hangup_cb (gpointer object,
3531     EmpathyCallWindow *self)
3532 {
3533   empathy_call_handler_stop_call (self->priv->handler);
3534
3535   empathy_call_window_disconnected (self, TRUE);
3536 }
3537
3538 static void
3539 empathy_call_window_restart_call (EmpathyCallWindow *window)
3540 {
3541   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3542
3543   /* Remove error info bars */
3544   gtk_container_forall (GTK_CONTAINER (priv->errors_vbox),
3545       (GtkCallback) gtk_widget_destroy, NULL);
3546
3547   create_video_output_widget (window);
3548
3549   /* While the call was disconnected, the input volume might have changed.
3550    * However, since the audio_input source was destroyed, its volume has not
3551    * been updated during that time. That's why we manually update it here */
3552   empathy_call_window_mic_volume_changed (window);
3553
3554   priv->outgoing = TRUE;
3555   empathy_call_window_set_state_connecting (window);
3556
3557   if (priv->pipeline_playing)
3558     start_call (window);
3559   else
3560     /* call will be started when the pipeline is ready */
3561     priv->start_call_when_playing = TRUE;
3562
3563   empathy_call_window_setup_avatars (window, priv->handler);
3564
3565   empathy_call_window_show_hangup_button (window, TRUE);
3566 }
3567
3568 static void
3569 empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
3570     EmpathyCallWindow *window)
3571 {
3572   gboolean active;
3573
3574   active = gtk_toggle_tool_button_get_active (button);
3575
3576   empathy_call_window_show_dialpad (window, active);
3577 }
3578
3579 static void
3580 empathy_call_window_fullscreen_cb (gpointer object,
3581                                    EmpathyCallWindow *window)
3582 {
3583   empathy_call_window_fullscreen_toggle (window);
3584 }
3585
3586 static void
3587 empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window)
3588 {
3589   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3590
3591   if (priv->is_fullscreen)
3592     gtk_window_unfullscreen (GTK_WINDOW (window));
3593   else
3594     gtk_window_fullscreen (GTK_WINDOW (window));
3595 }
3596
3597 static gboolean
3598 empathy_call_window_video_button_press_cb (GtkWidget *video_preview,
3599   GdkEventButton *event, EmpathyCallWindow *window)
3600 {
3601   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3602     {
3603       empathy_call_window_video_menu_popup (window, event->button);
3604       return TRUE;
3605     }
3606
3607   return FALSE;
3608 }
3609
3610 static gboolean
3611 empathy_call_window_key_press_cb (GtkWidget *video_output,
3612   GdkEventKey *event, EmpathyCallWindow *window)
3613 {
3614   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3615
3616   if (priv->is_fullscreen && event->keyval == GDK_KEY_Escape)
3617     {
3618       /* Since we are in fullscreen mode, toggling will bring us back to
3619          normal mode. */
3620       empathy_call_window_fullscreen_toggle (window);
3621       return TRUE;
3622     }
3623
3624   return FALSE;
3625 }
3626
3627 static gboolean
3628 empathy_call_window_video_output_motion_notify (GtkWidget *widget,
3629     GdkEventMotion *event, EmpathyCallWindow *window)
3630 {
3631   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3632
3633   if (priv->is_fullscreen)
3634     {
3635       empathy_call_window_fullscreen_show_popup (priv->fullscreen);
3636
3637       /* Show the bottom toolbar */
3638       empathy_call_window_motion_notify_cb (NULL, NULL, window);
3639       return TRUE;
3640     }
3641   return FALSE;
3642 }
3643
3644 static void
3645 empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
3646   guint button)
3647 {
3648   GtkWidget *menu;
3649   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3650
3651   menu = gtk_ui_manager_get_widget (priv->ui_manager,
3652             "/video-popup");
3653   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
3654       button, gtk_get_current_event_time ());
3655   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
3656 }
3657
3658 static void
3659 empathy_call_window_status_message (EmpathyCallWindow *self,
3660   gchar *message)
3661 {
3662   gtk_label_set_label (GTK_LABEL (self->priv->status_label), message);
3663 }
3664
3665 static void
3666 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
3667   gdouble value, EmpathyCallWindow *window)
3668 {
3669   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3670
3671   if (priv->audio_output == NULL)
3672     return;
3673
3674   empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (priv->audio_output),
3675     value);
3676 }
3677
3678 GtkUIManager *
3679 empathy_call_window_get_ui_manager (EmpathyCallWindow *window)
3680 {
3681   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3682
3683   return priv->ui_manager;
3684 }
3685
3686 EmpathyGstAudioSrc *
3687 empathy_call_window_get_audio_src (EmpathyCallWindow *window)
3688 {
3689   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3690
3691   return (EmpathyGstAudioSrc *) priv->audio_input;
3692 }
3693
3694 EmpathyGstVideoSrc *
3695 empathy_call_window_get_video_src (EmpathyCallWindow *self)
3696 {
3697   return EMPATHY_GST_VIDEO_SRC (self->priv->video_input);
3698 }