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