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