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