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