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