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