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