]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
call-window: don't display labels in both toolbars
[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 gboolean
733 empathy_call_window_configure_event_cb (GtkWidget *widget,
734     GdkEvent  *event,
735     EmpathyCallWindow *self)
736 {
737   GdkWindow *gdk_window;
738   GdkWindowState window_state;
739
740   gtk_window_get_position (GTK_WINDOW (self), &self->priv->x, &self->priv->y);
741   gtk_window_get_size (GTK_WINDOW (self), &self->priv->w, &self->priv->h);
742
743   gtk_widget_get_preferred_width (self->priv->dtmf_panel,
744       &self->priv->dialpad_width, NULL);
745
746   gdk_window = gtk_widget_get_window (widget);
747   window_state = gdk_window_get_state (gdk_window);
748   self->priv->maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED);
749
750   return FALSE;
751 }
752
753 static void
754 empathy_call_window_destroyed_cb (GtkWidget *object,
755     EmpathyCallWindow *self)
756 {
757   if (gtk_widget_get_visible (self->priv->dtmf_panel))
758     {
759       /* Save the geometry as if the dialpad was hidden. */
760       empathy_geometry_save_values (GTK_WINDOW (self),
761           self->priv->x, self->priv->y,
762           self->priv->w - self->priv->dialpad_width, self->priv->h,
763           self->priv->maximized);
764     }
765 }
766
767 static void
768 empathy_call_window_init (EmpathyCallWindow *self)
769 {
770   EmpathyCallWindowPriv *priv;
771   GtkBuilder *gui;
772   GtkWidget *top_vbox;
773   gchar *filename;
774   ClutterConstraint *size_constraint;
775   ClutterActor *remote_avatar;
776   GtkStyleContext *context;
777   GdkRGBA rgba;
778   ClutterColor bg;
779
780   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
781     EMPATHY_TYPE_CALL_WINDOW, EmpathyCallWindowPriv);
782
783   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
784   gui = empathy_builder_get_file (filename,
785     "call_window_vbox", &top_vbox,
786     "errors_vbox", &priv->errors_vbox,
787     "pane", &priv->pane,
788     "remote_user_name_toolbar", &priv->remote_user_name_toolbar,
789     "remote_user_avatar_toolbar", &priv->remote_user_avatar_toolbar,
790     "status_label", &priv->status_label,
791     "audiocall", &priv->audio_call_button,
792     "videocall", &priv->video_call_button,
793     "microphone", &priv->mic_button,
794     "camera", &priv->camera_button,
795     "hangup", &priv->hangup_button,
796     "dialpad", &priv->dialpad_button,
797     "toolbar", &priv->toolbar,
798     "bottom_toolbar", &priv->bottom_toolbar,
799     "ui_manager", &priv->ui_manager,
800     "menufullscreen", &priv->menu_fullscreen,
801     "details_vbox",  &priv->details_vbox,
802     "vcodec_encoding_label", &priv->vcodec_encoding_label,
803     "acodec_encoding_label", &priv->acodec_encoding_label,
804     "acodec_decoding_label", &priv->acodec_decoding_label,
805     "vcodec_decoding_label", &priv->vcodec_decoding_label,
806     "audio_remote_candidate_label", &priv->audio_remote_candidate_label,
807     "audio_local_candidate_label", &priv->audio_local_candidate_label,
808     "video_remote_candidate_label", &priv->video_remote_candidate_label,
809     "video_local_candidate_label", &priv->video_local_candidate_label,
810     "video_remote_candidate_info_img", &priv->video_remote_candidate_info_img,
811     "video_local_candidate_info_img", &priv->video_local_candidate_info_img,
812     "audio_remote_candidate_info_img", &priv->audio_remote_candidate_info_img,
813     "audio_local_candidate_info_img", &priv->audio_local_candidate_info_img,
814     NULL);
815   g_free (filename);
816
817   empathy_builder_connect (gui, self,
818     "menuhangup", "activate", empathy_call_window_hangup_cb,
819     "hangup", "clicked", empathy_call_window_hangup_cb,
820     "audiocall", "clicked", empathy_call_window_audio_call_cb,
821     "videocall", "clicked", empathy_call_window_video_call_cb,
822     "volume", "value-changed", empathy_call_window_volume_changed_cb,
823     "microphone", "toggled", empathy_call_window_mic_toggled_cb,
824     "camera", "toggled", empathy_call_window_camera_toggled_cb,
825     "dialpad", "toggled", empathy_call_window_dialpad_cb,
826     "menufullscreen", "activate", empathy_call_window_fullscreen_cb,
827     "menupreviewdisable", "activate", empathy_call_window_disable_camera_cb,
828     "menupreviewminimise", "activate", empathy_call_window_minimise_camera_cb,
829     "menupreviewmaximise", "activate", empathy_call_window_maximise_camera_cb,
830     NULL);
831
832   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
833
834   priv->camera_monitor = empathy_camera_monitor_dup_singleton ();
835
836   g_object_bind_property (priv->camera_monitor, "available",
837       priv->camera_button, "sensitive",
838       G_BINDING_SYNC_CREATE);
839
840   priv->lock = g_mutex_new ();
841
842   gtk_container_add (GTK_CONTAINER (self), top_vbox);
843
844   priv->content_hbox = gtk_hbox_new (FALSE, CONTENT_HBOX_SPACING);
845   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
846                                   CONTENT_HBOX_BORDER_WIDTH);
847   gtk_box_pack_start (GTK_BOX (priv->pane), priv->content_hbox,
848       TRUE, TRUE, 0);
849
850   /* avatar/video box */
851   priv->video_layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
852       CLUTTER_BIN_ALIGNMENT_CENTER);
853
854   priv->video_box = clutter_box_new (priv->video_layout);
855
856   priv->video_container = gtk_clutter_embed_new ();
857
858   /* Set the background color to that of the rest of the window */
859   context = gtk_widget_get_style_context (priv->content_hbox);
860   gtk_style_context_get_background_color (context,
861       GTK_STATE_FLAG_NORMAL, &rgba);
862   bg.red = CLAMP (rgba.red * 255.0, 0, 255);
863   bg.green = CLAMP (rgba.green * 255.0, 0, 255);
864   bg.blue = CLAMP (rgba.blue * 255.0, 0, 255);
865   bg.alpha = CLAMP (rgba.alpha * 255.0, 0, 255);
866   clutter_stage_set_color (
867       CLUTTER_STAGE (gtk_clutter_embed_get_stage (
868           GTK_CLUTTER_EMBED (priv->video_container))),
869       &bg);
870
871   clutter_container_add (
872       CLUTTER_CONTAINER (gtk_clutter_embed_get_stage (
873           GTK_CLUTTER_EMBED (priv->video_container))),
874       priv->video_box,
875       NULL);
876
877   size_constraint = clutter_bind_constraint_new (
878       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
879       CLUTTER_BIND_SIZE, 0);
880   clutter_actor_add_constraint (priv->video_box, size_constraint);
881
882   priv->remote_user_avatar_widget = gtk_image_new ();
883   remote_avatar = gtk_clutter_actor_new_with_contents (
884       priv->remote_user_avatar_widget);
885
886   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_box),
887       remote_avatar);
888
889   gtk_box_pack_start (GTK_BOX (priv->content_hbox),
890       priv->video_container, TRUE, TRUE,
891       CONTENT_HBOX_CHILDREN_PACKING_PADDING);
892
893   create_pipeline (self);
894   create_video_output_widget (self);
895   create_audio_input (self);
896   create_video_input (self);
897
898   /* The call will be started as soon the pipeline is playing */
899   priv->start_call_when_playing = TRUE;
900
901   priv->dtmf_panel = empathy_create_dtmf_dialpad (G_OBJECT (self),
902       G_CALLBACK (dtmf_button_pressed_cb),
903       G_CALLBACK (dtmf_button_released_cb));
904
905   gtk_box_pack_start (GTK_BOX (priv->pane), priv->dtmf_panel,
906       FALSE, FALSE, 0);
907
908   gtk_box_pack_start (GTK_BOX (priv->pane), priv->details_vbox,
909       FALSE, FALSE, 0);
910
911   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
912
913   gtk_widget_show_all (top_vbox);
914
915   gtk_widget_hide (priv->dtmf_panel);
916   gtk_widget_hide (priv->details_vbox);
917
918   priv->fullscreen = empathy_call_window_fullscreen_new (self);
919
920   empathy_call_window_fullscreen_set_video_widget (priv->fullscreen,
921       priv->video_container);
922
923   g_signal_connect (G_OBJECT (priv->fullscreen->leave_fullscreen_button),
924       "clicked", G_CALLBACK (empathy_call_window_fullscreen_cb), self);
925
926   g_signal_connect (G_OBJECT (self), "realize",
927     G_CALLBACK (empathy_call_window_realized_cb), self);
928
929   g_signal_connect (G_OBJECT (self), "delete-event",
930     G_CALLBACK (empathy_call_window_delete_cb), self);
931
932   g_signal_connect (G_OBJECT (self), "window-state-event",
933     G_CALLBACK (empathy_call_window_state_event_cb), self);
934
935   g_signal_connect (G_OBJECT (self), "key-press-event",
936       G_CALLBACK (empathy_call_window_key_press_cb), self);
937
938   priv->timer = g_timer_new ();
939
940   g_object_ref (priv->ui_manager);
941   g_object_unref (gui);
942
943   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
944   priv->mic_menu = empathy_mic_menu_new (self);
945
946   empathy_call_window_show_hangup_button (self, TRUE);
947
948   priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
949   g_signal_connect (priv->settings, "changed::"EMPATHY_PREFS_CALL_SOUND_VOLUME,
950       G_CALLBACK (empathy_call_window_prefs_volume_changed_cb), self);
951
952   empathy_geometry_bind (GTK_WINDOW (self), "call-window");
953   /* These signals are used to track the window position and save it
954    * when the window is destroyed. We need to do this as we don't want
955    * the window geometry to be saved with the dialpad taken into account. */
956   g_signal_connect (self, "destroy",
957       G_CALLBACK (empathy_call_window_destroyed_cb), self);
958   g_signal_connect (self, "configure-event",
959       G_CALLBACK (empathy_call_window_configure_event_cb), self);
960   g_signal_connect (self, "window-state-event",
961       G_CALLBACK (empathy_call_window_configure_event_cb), self);
962
963   /* Don't display labels in both toolbars */
964   gtk_toolbar_set_style (GTK_TOOLBAR (priv->toolbar), GTK_TOOLBAR_ICONS);
965   gtk_toolbar_set_style (GTK_TOOLBAR (priv->bottom_toolbar), GTK_TOOLBAR_ICONS);
966 }
967
968 /* Instead of specifying a width and a height, we specify only one size. That's
969    because we want a square avatar icon.  */
970 static void
971 init_contact_avatar_with_size (EmpathyContact *contact,
972     GtkWidget *image_widget,
973     gint size)
974 {
975   GdkPixbuf *pixbuf_avatar = NULL;
976
977   if (contact != NULL)
978     {
979       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
980         size, size);
981     }
982
983   if (pixbuf_avatar == NULL)
984     {
985       pixbuf_avatar = empathy_pixbuf_from_icon_name_sized (
986           EMPATHY_IMAGE_AVATAR_DEFAULT, size);
987     }
988
989   gtk_image_set_from_pixbuf (GTK_IMAGE (image_widget), pixbuf_avatar);
990
991   if (pixbuf_avatar != NULL)
992     g_object_unref (pixbuf_avatar);
993 }
994
995 static void
996 set_window_title (EmpathyCallWindow *self)
997 {
998   EmpathyCallWindowPriv *priv = GET_PRIV (self);
999   gchar *tmp;
1000
1001   if (priv->contact != NULL)
1002     {
1003       /* translators: Call is a noun and %s is the contact name. This string
1004        * is used in the window title */
1005       tmp = g_strdup_printf (_("Call with %s"),
1006           empathy_contact_get_alias (priv->contact));
1007       gtk_window_set_title (GTK_WINDOW (self), tmp);
1008       g_free (tmp);
1009     }
1010   else
1011     {
1012       gtk_window_set_title (GTK_WINDOW (self), _("Call with %d participants"));
1013     }
1014 }
1015
1016 static void
1017 set_remote_user_name (EmpathyCallWindow *self,
1018   EmpathyContact *contact)
1019 {
1020   const gchar *alias = empathy_contact_get_alias (contact);
1021   const gchar *status = empathy_contact_get_status (contact);
1022   gchar *label;
1023
1024   label = g_strdup_printf ("%s\n<small>%s</small>", alias, status);
1025   gtk_label_set_markup (GTK_LABEL (self->priv->remote_user_name_toolbar),
1026       label);
1027   g_free (label);
1028 }
1029
1030 static void
1031 contact_name_changed_cb (EmpathyContact *contact,
1032     GParamSpec *pspec,
1033     EmpathyCallWindow *self)
1034 {
1035   set_window_title (self);
1036   set_remote_user_name (self, contact);
1037 }
1038
1039 static void
1040 contact_presence_changed_cb (EmpathyContact *contact,
1041     GParamSpec *pspec,
1042     EmpathyCallWindow *self)
1043 {
1044   set_remote_user_name (self, contact);
1045 }
1046
1047 static void
1048 contact_avatar_changed_cb (EmpathyContact *contact,
1049     GParamSpec *pspec,
1050     EmpathyCallWindow *self)
1051 {
1052   int size;
1053   GtkAllocation allocation;
1054   GtkWidget *avatar_widget;
1055
1056   avatar_widget = self->priv->remote_user_avatar_widget;
1057
1058   gtk_widget_get_allocation (avatar_widget, &allocation);
1059   size = allocation.height;
1060
1061   if (size == 0)
1062     {
1063       /* the widget is not allocated yet, set a default size */
1064       size = MIN (REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT,
1065           REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH);
1066     }
1067
1068   init_contact_avatar_with_size (contact, avatar_widget, size);
1069
1070   avatar_widget = self->priv->remote_user_avatar_toolbar;
1071
1072   gtk_widget_get_allocation (avatar_widget, &allocation);
1073   size = allocation.height;
1074
1075   if (size == 0)
1076     {
1077       /* the widget is not allocated yet, set a default size */
1078       size = SMALL_TOOLBAR_SIZE;
1079     }
1080
1081   init_contact_avatar_with_size (contact, avatar_widget, size);
1082 }
1083
1084 static void
1085 empathy_call_window_setup_avatars (EmpathyCallWindow *self,
1086     EmpathyCallHandler *handler)
1087 {
1088   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1089
1090   tp_g_signal_connect_object (priv->contact, "notify::name",
1091       G_CALLBACK (contact_name_changed_cb), self, 0);
1092   tp_g_signal_connect_object (priv->contact, "notify::avatar",
1093     G_CALLBACK (contact_avatar_changed_cb), self, 0);
1094   tp_g_signal_connect_object (priv->contact, "notify::presence",
1095       G_CALLBACK (contact_presence_changed_cb), self, 0);
1096
1097   set_window_title (self);
1098   set_remote_user_name (self, priv->contact);
1099
1100   init_contact_avatar_with_size (priv->contact,
1101       priv->remote_user_avatar_widget,
1102       MIN (REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH,
1103           REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT));
1104
1105   init_contact_avatar_with_size (priv->contact,
1106       priv->remote_user_avatar_toolbar,
1107       SMALL_TOOLBAR_SIZE);
1108
1109   /* The remote avatar is shown by default and will be hidden when we receive
1110      video from the remote side. */
1111   clutter_actor_hide (priv->video_output);
1112   gtk_widget_show (priv->remote_user_avatar_widget);
1113 }
1114
1115 static void
1116 update_send_codec (EmpathyCallWindow *self,
1117     gboolean audio)
1118 {
1119   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1120   FsCodec *codec;
1121   GtkWidget *widget;
1122   gchar *tmp;
1123
1124   if (audio)
1125     {
1126       codec = empathy_call_handler_get_send_audio_codec (priv->handler);
1127       widget = priv->acodec_encoding_label;
1128     }
1129   else
1130     {
1131       codec = empathy_call_handler_get_send_video_codec (priv->handler);
1132       widget = priv->vcodec_encoding_label;
1133     }
1134
1135   if (codec == NULL)
1136     return;
1137
1138   tmp = g_strdup_printf ("%s/%u", codec->encoding_name, codec->clock_rate);
1139   gtk_label_set_text (GTK_LABEL (widget), tmp);
1140   g_free (tmp);
1141 }
1142
1143 static void
1144 send_audio_codec_notify_cb (GObject *object,
1145     GParamSpec *pspec,
1146     gpointer user_data)
1147 {
1148   EmpathyCallWindow *self = user_data;
1149
1150   update_send_codec (self, TRUE);
1151 }
1152
1153 static void
1154 send_video_codec_notify_cb (GObject *object,
1155     GParamSpec *pspec,
1156     gpointer user_data)
1157 {
1158   EmpathyCallWindow *self = user_data;
1159
1160   update_send_codec (self, FALSE);
1161 }
1162
1163 static void
1164 update_recv_codec (EmpathyCallWindow *self,
1165     gboolean audio)
1166 {
1167   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1168   GList *codecs, *l;
1169   GtkWidget *widget;
1170   GString *str = NULL;
1171
1172   if (audio)
1173     {
1174       codecs = empathy_call_handler_get_recv_audio_codecs (priv->handler);
1175       widget = priv->acodec_decoding_label;
1176     }
1177   else
1178     {
1179       codecs = empathy_call_handler_get_recv_video_codecs (priv->handler);
1180       widget = priv->vcodec_decoding_label;
1181     }
1182
1183   if (codecs == NULL)
1184     return;
1185
1186   for (l = codecs; l != NULL; l = g_list_next (l))
1187     {
1188       FsCodec *codec = l->data;
1189
1190       if (str == NULL)
1191         str = g_string_new (NULL);
1192       else
1193         g_string_append (str, ", ");
1194
1195       g_string_append_printf (str, "%s/%u", codec->encoding_name,
1196           codec->clock_rate);
1197     }
1198
1199   gtk_label_set_text (GTK_LABEL (widget), str->str);
1200   g_string_free (str, TRUE);
1201 }
1202
1203 static void
1204 recv_audio_codecs_notify_cb (GObject *object,
1205     GParamSpec *pspec,
1206     gpointer user_data)
1207 {
1208   EmpathyCallWindow *self = user_data;
1209
1210   update_recv_codec (self, TRUE);
1211 }
1212
1213 static void
1214 recv_video_codecs_notify_cb (GObject *object,
1215     GParamSpec *pspec,
1216     gpointer user_data)
1217 {
1218   EmpathyCallWindow *self = user_data;
1219
1220   update_recv_codec (self, FALSE);
1221 }
1222
1223 static const gchar *
1224 candidate_type_to_str (FsCandidate *candidate)
1225 {
1226   switch (candidate->type)
1227     {
1228       case FS_CANDIDATE_TYPE_HOST:
1229         return "host";
1230       case FS_CANDIDATE_TYPE_SRFLX:
1231         return "server reflexive";
1232       case FS_CANDIDATE_TYPE_PRFLX:
1233         return "peer reflexive";
1234       case FS_CANDIDATE_TYPE_RELAY:
1235         return "relay";
1236       case FS_CANDIDATE_TYPE_MULTICAST:
1237         return "multicast";
1238     }
1239
1240   return NULL;
1241 }
1242
1243 static const gchar *
1244 candidate_type_to_desc (FsCandidate *candidate)
1245 {
1246   switch (candidate->type)
1247     {
1248       case FS_CANDIDATE_TYPE_HOST:
1249         return _("The IP address as seen by the machine");
1250       case FS_CANDIDATE_TYPE_SRFLX:
1251         return _("The IP address as seen by a server on the Internet");
1252       case FS_CANDIDATE_TYPE_PRFLX:
1253         return _("The IP address of the peer as seen by the other side");
1254       case FS_CANDIDATE_TYPE_RELAY:
1255         return _("The IP address of a relay server");
1256       case FS_CANDIDATE_TYPE_MULTICAST:
1257         return _("The IP address of the multicast group");
1258     }
1259
1260   return NULL;
1261 }
1262
1263 static void
1264 update_candidat_widget (EmpathyCallWindow *self,
1265     GtkWidget *label,
1266     GtkWidget *img,
1267     FsCandidate *candidate)
1268 {
1269   gchar *str;
1270
1271   g_assert (candidate != NULL);
1272   str = g_strdup_printf ("%s %u (%s)", candidate->ip,
1273       candidate->port, candidate_type_to_str (candidate));
1274
1275   gtk_label_set_text (GTK_LABEL (label), str);
1276   gtk_widget_set_tooltip_text (img, candidate_type_to_desc (candidate));
1277
1278   g_free (str);
1279 }
1280
1281 static void
1282 candidates_changed_cb (GObject *object,
1283     FsMediaType type,
1284     EmpathyCallWindow *self)
1285 {
1286   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1287   FsCandidate *candidate = NULL;
1288
1289   if (type == FS_MEDIA_TYPE_VIDEO)
1290     {
1291       /* Update remote candidate */
1292       candidate = empathy_call_handler_get_video_remote_candidate (
1293           priv->handler);
1294
1295       update_candidat_widget (self, priv->video_remote_candidate_label,
1296           priv->video_remote_candidate_info_img, candidate);
1297
1298       /* Update local candidate */
1299       candidate = empathy_call_handler_get_video_local_candidate (
1300           priv->handler);
1301
1302       update_candidat_widget (self, priv->video_local_candidate_label,
1303           priv->video_local_candidate_info_img, candidate);
1304     }
1305   else
1306     {
1307       /* Update remote candidate */
1308       candidate = empathy_call_handler_get_audio_remote_candidate (
1309           priv->handler);
1310
1311       update_candidat_widget (self, priv->audio_remote_candidate_label,
1312           priv->audio_remote_candidate_info_img, candidate);
1313
1314       /* Update local candidate */
1315       candidate = empathy_call_handler_get_audio_local_candidate (
1316           priv->handler);
1317
1318       update_candidat_widget (self, priv->audio_local_candidate_label,
1319           priv->audio_local_candidate_info_img, candidate);
1320     }
1321 }
1322
1323 static void
1324 empathy_call_window_constructed (GObject *object)
1325 {
1326   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1327   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1328   TpyCallChannel *call;
1329
1330   g_assert (priv->handler != NULL);
1331
1332   g_object_get (priv->handler, "call-channel", &call, NULL);
1333   priv->outgoing = (call == NULL);
1334   if (call != NULL)
1335     g_object_unref (call);
1336
1337   g_object_get (priv->handler, "target-contact", &priv->contact, NULL);
1338   g_assert (priv->contact != NULL);
1339
1340   empathy_call_window_setup_avatars (self, priv->handler);
1341   empathy_call_window_set_state_connecting (self);
1342
1343   if (!empathy_call_handler_has_initial_video (priv->handler))
1344     {
1345       gtk_toggle_tool_button_set_active (
1346           GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
1347     }
1348   /* If call has InitialVideo, the preview will be started once the call has
1349    * been started (start_call()). */
1350
1351   update_send_codec (self, TRUE);
1352   update_send_codec (self, FALSE);
1353   update_recv_codec (self, TRUE);
1354   update_recv_codec (self, FALSE);
1355
1356   tp_g_signal_connect_object (priv->handler, "notify::send-audio-codec",
1357       G_CALLBACK (send_audio_codec_notify_cb), self, 0);
1358   tp_g_signal_connect_object (priv->handler, "notify::send-video-codec",
1359       G_CALLBACK (send_video_codec_notify_cb), self, 0);
1360   tp_g_signal_connect_object (priv->handler, "notify::recv-audio-codecs",
1361       G_CALLBACK (recv_audio_codecs_notify_cb), self, 0);
1362   tp_g_signal_connect_object (priv->handler, "notify::recv-video-codecs",
1363       G_CALLBACK (recv_video_codecs_notify_cb), self, 0);
1364
1365   tp_g_signal_connect_object (priv->handler, "candidates-changed",
1366       G_CALLBACK (candidates_changed_cb), self, 0);
1367 }
1368
1369 static void empathy_call_window_dispose (GObject *object);
1370 static void empathy_call_window_finalize (GObject *object);
1371
1372 static void
1373 empathy_call_window_set_property (GObject *object,
1374   guint property_id, const GValue *value, GParamSpec *pspec)
1375 {
1376   EmpathyCallWindowPriv *priv = GET_PRIV (object);
1377
1378   switch (property_id)
1379     {
1380       case PROP_CALL_HANDLER:
1381         priv->handler = g_value_dup_object (value);
1382         break;
1383       default:
1384         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1385     }
1386 }
1387
1388 static void
1389 empathy_call_window_get_property (GObject *object,
1390   guint property_id, GValue *value, GParamSpec *pspec)
1391 {
1392   EmpathyCallWindowPriv *priv = GET_PRIV (object);
1393
1394   switch (property_id)
1395     {
1396       case PROP_CALL_HANDLER:
1397         g_value_set_object (value, priv->handler);
1398         break;
1399       default:
1400         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1401     }
1402 }
1403
1404 static void
1405 empathy_call_window_class_init (
1406   EmpathyCallWindowClass *empathy_call_window_class)
1407 {
1408   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
1409   GParamSpec *param_spec;
1410
1411   g_type_class_add_private (empathy_call_window_class,
1412     sizeof (EmpathyCallWindowPriv));
1413
1414   object_class->constructed = empathy_call_window_constructed;
1415   object_class->set_property = empathy_call_window_set_property;
1416   object_class->get_property = empathy_call_window_get_property;
1417
1418   object_class->dispose = empathy_call_window_dispose;
1419   object_class->finalize = empathy_call_window_finalize;
1420
1421   param_spec = g_param_spec_object ("handler",
1422     "handler", "The call handler",
1423     EMPATHY_TYPE_CALL_HANDLER,
1424     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1425   g_object_class_install_property (object_class,
1426     PROP_CALL_HANDLER, param_spec);
1427 }
1428
1429 void
1430 empathy_call_window_dispose (GObject *object)
1431 {
1432   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1433   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1434
1435   if (priv->dispose_has_run)
1436     return;
1437
1438   priv->dispose_has_run = TRUE;
1439
1440   if (priv->handler != NULL)
1441     {
1442       empathy_call_handler_stop_call (priv->handler);
1443       tp_clear_object (&priv->handler);
1444     }
1445
1446   if (priv->bus_message_source_id != 0)
1447     {
1448       g_source_remove (priv->bus_message_source_id);
1449       priv->bus_message_source_id = 0;
1450     }
1451
1452   if (priv->got_video_src > 0)
1453     {
1454       g_source_remove (priv->got_video_src);
1455       priv->got_video_src = 0;
1456     }
1457
1458   tp_clear_object (&priv->pipeline);
1459   tp_clear_object (&priv->video_input);
1460   tp_clear_object (&priv->audio_input);
1461   tp_clear_object (&priv->video_tee);
1462   tp_clear_object (&priv->ui_manager);
1463   tp_clear_object (&priv->fullscreen);
1464   tp_clear_object (&priv->camera_monitor);
1465   tp_clear_object (&priv->settings);
1466
1467   g_list_free_full (priv->notifiers, g_object_unref);
1468
1469   if (priv->timer_id != 0)
1470     g_source_remove (priv->timer_id);
1471   priv->timer_id = 0;
1472
1473   if (priv->contact != NULL)
1474     {
1475       g_signal_handlers_disconnect_by_func (priv->contact,
1476           contact_name_changed_cb, self);
1477       priv->contact = NULL;
1478     }
1479
1480
1481   tp_clear_object (&priv->sound_mgr);
1482
1483   tp_clear_object (&priv->mic_menu);
1484
1485   G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
1486 }
1487
1488 static void
1489 disconnect_video_output_motion_handler (EmpathyCallWindow *self)
1490 {
1491   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1492
1493   if (priv->video_output_motion_handler_id != 0)
1494     {
1495       g_signal_handler_disconnect (G_OBJECT (priv->video_container),
1496           priv->video_output_motion_handler_id);
1497       priv->video_output_motion_handler_id = 0;
1498     }
1499 }
1500
1501 void
1502 empathy_call_window_finalize (GObject *object)
1503 {
1504   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1505   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1506
1507   disconnect_video_output_motion_handler (self);
1508
1509   /* free any data held directly by the object here */
1510   g_mutex_free (priv->lock);
1511
1512   g_timer_destroy (priv->timer);
1513
1514   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
1515 }
1516
1517
1518 EmpathyCallWindow *
1519 empathy_call_window_new (EmpathyCallHandler *handler)
1520 {
1521   return EMPATHY_CALL_WINDOW (
1522     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
1523 }
1524
1525 static void
1526 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
1527   GstElement *conference, gpointer user_data)
1528 {
1529   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1530   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1531   FsElementAddedNotifier *notifier;
1532   GKeyFile *keyfile;
1533
1534   DEBUG ("Conference added");
1535
1536   /* Add notifier to set the various element properties as needed */
1537   notifier = fs_element_added_notifier_new ();
1538   keyfile = fs_utils_get_default_element_properties (conference);
1539
1540   if (keyfile != NULL)
1541     fs_element_added_notifier_set_properties_from_keyfile (notifier, keyfile);
1542
1543   fs_element_added_notifier_add (notifier, GST_BIN (priv->pipeline));
1544
1545   priv->notifiers = g_list_prepend (priv->notifiers, notifier);
1546
1547   gst_bin_add (GST_BIN (priv->pipeline), conference);
1548   gst_element_set_state (conference, GST_STATE_PLAYING);
1549 }
1550
1551 static void
1552 empathy_call_window_conference_removed_cb (EmpathyCallHandler *handler,
1553   GstElement *conference, gpointer user_data)
1554 {
1555   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1556   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1557
1558   gst_bin_remove (GST_BIN (priv->pipeline), conference);
1559   gst_element_set_state (conference, GST_STATE_NULL);
1560 }
1561
1562 static gboolean
1563 empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
1564 {
1565   GstStateChangeReturn state_change_return;
1566   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1567
1568   if (priv->pipeline == NULL)
1569     return TRUE;
1570
1571   if (priv->bus_message_source_id != 0)
1572     {
1573       g_source_remove (priv->bus_message_source_id);
1574       priv->bus_message_source_id = 0;
1575     }
1576
1577   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1578
1579   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
1580         state_change_return == GST_STATE_CHANGE_NO_PREROLL)
1581     {
1582       if (priv->pipeline != NULL)
1583         g_object_unref (priv->pipeline);
1584       priv->pipeline = NULL;
1585
1586       if (priv->audio_output != NULL)
1587         g_object_unref (priv->audio_output);
1588       priv->audio_output = NULL;
1589
1590       if (priv->video_tee != NULL)
1591         g_object_unref (priv->video_tee);
1592       priv->video_tee = NULL;
1593
1594       if (priv->video_preview != NULL)
1595         clutter_actor_destroy (priv->video_preview);
1596       priv->video_preview = NULL;
1597
1598       priv->funnel = NULL;
1599
1600       create_pipeline (self);
1601       /* Call will be started when user will hit the 'redial' button */
1602       priv->start_call_when_playing = FALSE;
1603       gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1604
1605       return TRUE;
1606     }
1607   else
1608     {
1609       g_message ("Error: could not destroy pipeline. Closing call window");
1610       gtk_widget_destroy (GTK_WIDGET (self));
1611
1612       return FALSE;
1613     }
1614 }
1615
1616 static void
1617 reset_details_pane (EmpathyCallWindow *self)
1618 {
1619   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1620
1621   gtk_label_set_text (GTK_LABEL (priv->vcodec_encoding_label), _("Unknown"));
1622   gtk_label_set_text (GTK_LABEL (priv->acodec_encoding_label), _("Unknown"));
1623   gtk_label_set_text (GTK_LABEL (priv->vcodec_decoding_label), _("Unknown"));
1624   gtk_label_set_text (GTK_LABEL (priv->acodec_decoding_label), _("Unknown"));
1625 }
1626
1627 static gboolean
1628 empathy_call_window_disconnected (EmpathyCallWindow *self,
1629     gboolean restart)
1630 {
1631   gboolean could_disconnect = FALSE;
1632   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1633   gboolean could_reset_pipeline;
1634
1635   /* Leave full screen mode if needed */
1636   gtk_window_unfullscreen (GTK_WINDOW (self));
1637
1638   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
1639   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
1640
1641   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
1642
1643   if (priv->call_state == CONNECTING)
1644       empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
1645
1646   if (priv->call_state != REDIALING)
1647     priv->call_state = DISCONNECTED;
1648
1649   if (could_reset_pipeline)
1650     {
1651       g_mutex_lock (priv->lock);
1652
1653       g_timer_stop (priv->timer);
1654
1655       if (priv->timer_id != 0)
1656         g_source_remove (priv->timer_id);
1657       priv->timer_id = 0;
1658
1659       g_mutex_unlock (priv->lock);
1660
1661       if (!restart)
1662         /* We are about to destroy the window, no need to update it or create
1663          * a video preview */
1664         return TRUE;
1665
1666       empathy_call_window_status_message (self, _("Disconnected"));
1667
1668       empathy_call_window_show_hangup_button (self, FALSE);
1669
1670       /* Unsensitive the camera and mic button */
1671       gtk_widget_set_sensitive (priv->camera_button, FALSE);
1672       gtk_widget_set_sensitive (priv->mic_button, FALSE);
1673
1674       /* Be sure that the mic button is enabled */
1675       gtk_toggle_tool_button_set_active (
1676           GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
1677
1678       if (priv->camera_state == CAMERA_STATE_ON)
1679         {
1680           /* Restart the preview with the new pipeline. */
1681           display_video_preview (self, TRUE);
1682         }
1683
1684       /* destroy the video output; it will be recreated when we'll redial */
1685       disconnect_video_output_motion_handler (self);
1686       if (priv->video_output != NULL)
1687         clutter_actor_destroy (priv->video_output);
1688       priv->video_output = NULL;
1689       if (priv->got_video_src > 0)
1690         {
1691           g_source_remove (priv->got_video_src);
1692           priv->got_video_src = 0;
1693         }
1694
1695       gtk_widget_show (priv->remote_user_avatar_widget);
1696
1697       reset_details_pane (self);
1698
1699       priv->sending_video = FALSE;
1700       priv->call_started = FALSE;
1701
1702       could_disconnect = TRUE;
1703
1704       /* TODO: display the self avatar of the preview (depends if the "Always
1705        * Show Video Preview" is enabled or not) */
1706     }
1707
1708   return could_disconnect;
1709 }
1710
1711
1712 static void
1713 empathy_call_window_channel_closed_cb (EmpathyCallHandler *handler,
1714     gpointer user_data)
1715 {
1716   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1717   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1718
1719   if (empathy_call_window_disconnected (self, TRUE) &&
1720       priv->call_state == REDIALING)
1721       empathy_call_window_restart_call (self);
1722 }
1723
1724 static gboolean
1725 empathy_call_window_sink_removed_cb (EmpathyCallHandler *handler,
1726     GstPad *sink,
1727     FsMediaType media_type,
1728     EmpathyCallWindow *self)
1729 {
1730   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1731
1732   DEBUG ("removing content");
1733
1734   /*
1735    * This assumes that there is only one video stream per channel...
1736    */
1737
1738   if ((guint) media_type == FS_MEDIA_TYPE_VIDEO)
1739     {
1740       if (priv->funnel != NULL)
1741         {
1742           GstElement *output;
1743
1744           output = priv->video_output_sink;
1745
1746           gst_element_set_state (output, GST_STATE_NULL);
1747           gst_element_set_state (priv->funnel, GST_STATE_NULL);
1748
1749           gst_bin_remove (GST_BIN (priv->pipeline), output);
1750           gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
1751           priv->funnel = NULL;
1752           return TRUE;
1753         }
1754     }
1755   else if (media_type == FS_MEDIA_TYPE_AUDIO)
1756     {
1757       if (priv->audio_output != NULL)
1758         {
1759           gst_element_set_state (priv->audio_output, GST_STATE_NULL);
1760
1761           gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
1762           priv->audio_output = NULL;
1763           return TRUE;
1764         }
1765     }
1766
1767   return FALSE;
1768 }
1769
1770 /* Called with global lock held */
1771 static GstPad *
1772 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
1773 {
1774   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1775   GstPad *pad;
1776   GstElement *output;
1777
1778   if (priv->funnel == NULL)
1779     {
1780       output = priv->video_output_sink;
1781
1782       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
1783
1784       if (!priv->funnel)
1785         {
1786           g_warning ("Could not create fsfunnel");
1787           return NULL;
1788         }
1789
1790       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->funnel))
1791         {
1792           gst_object_unref (priv->funnel);
1793           priv->funnel = NULL;
1794           g_warning ("Could  not add funnel to pipeline");
1795           return NULL;
1796         }
1797
1798       if (!gst_bin_add (GST_BIN (priv->pipeline), output))
1799         {
1800           g_warning ("Could not add the video output widget to the pipeline");
1801           goto error;
1802         }
1803
1804       if (!gst_element_link (priv->funnel, output))
1805         {
1806           g_warning ("Could not link output sink to funnel");
1807           goto error_output_added;
1808         }
1809
1810       if (gst_element_set_state (output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1811         {
1812           g_warning ("Could not start video sink");
1813           goto error_output_added;
1814         }
1815
1816       if (gst_element_set_state (priv->funnel, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1817         {
1818           g_warning ("Could not start funnel");
1819           goto error_output_added;
1820         }
1821     }
1822
1823   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
1824
1825   if (!pad)
1826     g_warning ("Could not get request pad from funnel");
1827
1828   return pad;
1829
1830
1831  error_output_added:
1832
1833   gst_element_set_locked_state (priv->funnel, TRUE);
1834   gst_element_set_locked_state (output, TRUE);
1835
1836   gst_element_set_state (priv->funnel, GST_STATE_NULL);
1837   gst_element_set_state (output, GST_STATE_NULL);
1838
1839   gst_bin_remove (GST_BIN (priv->pipeline), output);
1840   gst_element_set_locked_state (output, FALSE);
1841
1842  error:
1843
1844   gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
1845   priv->funnel = NULL;
1846
1847   return NULL;
1848 }
1849
1850 /* Called with global lock held */
1851 static GstPad *
1852 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self)
1853 {
1854   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1855   GstPad *pad;
1856   GstPadTemplate *template;
1857
1858   if (priv->audio_output == NULL)
1859     {
1860       priv->audio_output = empathy_audio_sink_new ();
1861       g_object_ref_sink (priv->audio_output);
1862
1863       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output))
1864         {
1865           g_warning ("Could not add audio sink to pipeline");
1866           g_object_unref (priv->audio_output);
1867           goto error_add_output;
1868         }
1869
1870       if (gst_element_set_state (priv->audio_output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1871         {
1872           g_warning ("Could not start audio sink");
1873           goto error;
1874         }
1875     }
1876
1877   template = gst_element_class_get_pad_template (
1878     GST_ELEMENT_GET_CLASS (priv->audio_output), "sink%d");
1879
1880   pad = gst_element_request_pad (priv->audio_output,
1881     template, NULL, NULL);
1882
1883   if (pad == NULL)
1884     {
1885       g_warning ("Could not get sink pad from sink");
1886       return NULL;
1887     }
1888
1889   return pad;
1890
1891 error:
1892   gst_element_set_locked_state (priv->audio_output, TRUE);
1893   gst_element_set_state (priv->audio_output, GST_STATE_NULL);
1894   gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
1895   priv->audio_output = NULL;
1896
1897 error_add_output:
1898
1899   return NULL;
1900 }
1901
1902 static gboolean
1903 empathy_call_window_update_timer (gpointer user_data)
1904 {
1905   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1906   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1907   gchar *str;
1908   gdouble time_;
1909
1910   time_ = g_timer_elapsed (priv->timer, NULL);
1911
1912   /* Translators: 'status - minutes:seconds' the caller has been connected */
1913   str = g_strdup_printf (_("%s â€” %d:%02dm"),
1914       priv->call_state == HELD ? _("On hold") : _("Connected"),
1915       (int) time_ / 60, (int) time_ % 60);
1916   empathy_call_window_status_message (self, str);
1917   g_free (str);
1918
1919   return TRUE;
1920 }
1921
1922 #if 0
1923 static void
1924 display_error (EmpathyCallWindow *self,
1925     TpyCallChannel *call,
1926     const gchar *img,
1927     const gchar *title,
1928     const gchar *desc,
1929     const gchar *details)
1930 {
1931   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1932   GtkWidget *info_bar;
1933   GtkWidget *content_area;
1934   GtkWidget *hbox;
1935   GtkWidget *vbox;
1936   GtkWidget *image;
1937   GtkWidget *label;
1938   gchar *txt;
1939
1940   /* Create info bar */
1941   info_bar = gtk_info_bar_new_with_buttons (GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
1942       NULL);
1943
1944   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
1945
1946   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
1947
1948   /* hbox containing the image and the messages vbox */
1949   hbox = gtk_hbox_new (FALSE, 3);
1950   gtk_container_add (GTK_CONTAINER (content_area), hbox);
1951
1952   /* Add image */
1953   image = gtk_image_new_from_icon_name (img, GTK_ICON_SIZE_DIALOG);
1954   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
1955
1956   /* vbox containing the main message and the details expander */
1957   vbox = gtk_vbox_new (FALSE, 3);
1958   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
1959
1960   /* Add text */
1961   txt = g_strdup_printf ("<b>%s</b>\n%s", title, desc);
1962
1963   label = gtk_label_new (NULL);
1964   gtk_label_set_markup (GTK_LABEL (label), txt);
1965   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1966   gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1967   g_free (txt);
1968
1969   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
1970
1971   /* Add details */
1972   if (details != NULL)
1973     {
1974       GtkWidget *expander;
1975
1976       expander = gtk_expander_new (_("Technical Details"));
1977
1978       txt = g_strdup_printf ("<i>%s</i>", details);
1979
1980       label = gtk_label_new (NULL);
1981       gtk_label_set_markup (GTK_LABEL (label), txt);
1982       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
1983       gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1984       g_free (txt);
1985
1986       gtk_container_add (GTK_CONTAINER (expander), label);
1987       gtk_box_pack_start (GTK_BOX (vbox), expander, TRUE, TRUE, 0);
1988     }
1989
1990   g_signal_connect (info_bar, "response",
1991       G_CALLBACK (gtk_widget_destroy), NULL);
1992
1993   gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar,
1994       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
1995   gtk_widget_show_all (info_bar);
1996 }
1997
1998 static gchar *
1999 media_stream_error_to_txt (EmpathyCallWindow *self,
2000     TpyCallChannel *call,
2001     gboolean audio,
2002     TpMediaStreamError error)
2003 {
2004   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2005   const gchar *cm = NULL;
2006   gchar *url;
2007   gchar *result;
2008
2009   switch (error)
2010     {
2011       case TP_MEDIA_STREAM_ERROR_CODEC_NEGOTIATION_FAILED:
2012         if (audio)
2013           return g_strdup_printf (
2014               _("%s's software does not understand any of the audio formats "
2015                 "supported by your computer"),
2016             empathy_contact_get_alias (priv->contact));
2017         else
2018           return g_strdup_printf (
2019               _("%s's software does not understand any of the video formats "
2020                 "supported by your computer"),
2021             empathy_contact_get_alias (priv->contact));
2022
2023       case TP_MEDIA_STREAM_ERROR_CONNECTION_FAILED:
2024         return g_strdup_printf (
2025             _("Can't establish a connection to %s. "
2026               "One of you might be on a network that does not allow "
2027               "direct connections."),
2028           empathy_contact_get_alias (priv->contact));
2029
2030       case TP_MEDIA_STREAM_ERROR_NETWORK_ERROR:
2031           return g_strdup (_("There was a failure on the network"));
2032
2033       case TP_MEDIA_STREAM_ERROR_NO_CODECS:
2034         if (audio)
2035           return g_strdup (_("The audio formats necessary for this call "
2036                 "are not installed on your computer"));
2037         else
2038           return g_strdup (_("The video formats necessary for this call "
2039                 "are not installed on your computer"));
2040
2041       case TP_MEDIA_STREAM_ERROR_INVALID_CM_BEHAVIOR:
2042         tp_connection_parse_object_path (
2043             tp_channel_borrow_connection (TP_CHANNEL (call)),
2044             NULL, &cm);
2045
2046         url = g_strdup_printf ("http://bugs.freedesktop.org/enter_bug.cgi?"
2047             "product=Telepathy&amp;component=%s", cm);
2048
2049         result = g_strdup_printf (
2050             _("Something unexpected happened in a Telepathy component. "
2051               "Please <a href=\"%s\">report this bug</a> and attach "
2052               "logs gathered from the 'Debug' window in the Help menu."), url);
2053
2054         g_free (url);
2055         g_free (cm);
2056         return result;
2057
2058       case TP_MEDIA_STREAM_ERROR_MEDIA_ERROR:
2059         return g_strdup (_("There was a failure in the call engine"));
2060
2061       case TP_MEDIA_STREAM_ERROR_EOS:
2062         return g_strdup (_("The end of the stream was reached"));
2063
2064       case TP_MEDIA_STREAM_ERROR_UNKNOWN:
2065       default:
2066         return NULL;
2067     }
2068 }
2069
2070 static void
2071 empathy_call_window_stream_error (EmpathyCallWindow *self,
2072     TpyCallChannel *call,
2073     gboolean audio,
2074     guint code,
2075     const gchar *msg,
2076     const gchar *icon,
2077     const gchar *title)
2078 {
2079   gchar *desc;
2080
2081   desc = media_stream_error_to_txt (self, call, audio, code);
2082   if (desc == NULL)
2083     {
2084       /* No description, use the error message. That's not great as it's not
2085        * localized but it's better than nothing. */
2086       display_error (self, call, icon, title, msg, NULL);
2087     }
2088   else
2089     {
2090       display_error (self, call, icon, title, desc, msg);
2091       g_free (desc);
2092     }
2093 }
2094
2095 static void
2096 empathy_call_window_audio_stream_error (TpyCallChannel *call,
2097     guint code,
2098     const gchar *msg,
2099     EmpathyCallWindow *self)
2100 {
2101   empathy_call_window_stream_error (self, call, TRUE, code, msg,
2102       "gnome-stock-mic", _("Can't establish audio stream"));
2103 }
2104
2105 static void
2106 empathy_call_window_video_stream_error (TpyCallChannel *call,
2107     guint code,
2108     const gchar *msg,
2109     EmpathyCallWindow *self)
2110 {
2111   empathy_call_window_stream_error (self, call, FALSE, code, msg,
2112       "camera-web", _("Can't establish video stream"));
2113 }
2114 #endif
2115
2116 static void
2117 empathy_call_window_state_changed_cb (EmpathyCallHandler *handler,
2118     TpyCallState state,
2119     EmpathyCallWindow *self)
2120 {
2121   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2122   TpyCallChannel *call;
2123   gboolean can_send_video;
2124
2125   if (state != TPY_CALL_STATE_ACCEPTED)
2126     return;
2127
2128   if (priv->call_state == CONNECTED)
2129     return;
2130
2131   g_timer_start (priv->timer);
2132   priv->call_state = CONNECTED;
2133
2134   empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2135
2136   can_send_video = priv->video_input != NULL &&
2137     empathy_contact_can_voip_video (priv->contact) &&
2138     empathy_camera_monitor_get_available (priv->camera_monitor);
2139
2140   g_object_get (priv->handler, "call-channel", &call, NULL);
2141
2142   if (tpy_call_channel_has_dtmf (call))
2143     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
2144
2145   if (priv->video_input == NULL)
2146     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
2147
2148   gtk_widget_set_sensitive (priv->camera_button, can_send_video);
2149
2150   empathy_call_window_show_hangup_button (self, TRUE);
2151
2152   gtk_widget_set_sensitive (priv->mic_button, TRUE);
2153
2154   clutter_actor_hide (priv->video_output);
2155   gtk_widget_show (priv->remote_user_avatar_widget);
2156
2157   g_object_unref (call);
2158
2159   g_mutex_lock (priv->lock);
2160
2161   priv->timer_id = g_timeout_add_seconds (1,
2162     empathy_call_window_update_timer, self);
2163
2164   g_mutex_unlock (priv->lock);
2165
2166   empathy_call_window_update_timer (self);
2167
2168   gtk_action_set_sensitive (priv->menu_fullscreen, TRUE);
2169 }
2170
2171 static gboolean
2172 empathy_call_window_show_video_output_cb (gpointer user_data)
2173 {
2174   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2175
2176   if (self->priv->video_output != NULL)
2177     {
2178       gtk_widget_hide (self->priv->remote_user_avatar_widget);
2179       clutter_actor_show (self->priv->video_output);
2180     }
2181
2182   return FALSE;
2183 }
2184
2185 static gboolean
2186 empathy_call_window_check_video_cb (gpointer data)
2187 {
2188   EmpathyCallWindow *self = data;
2189
2190   if (self->priv->got_video)
2191     {
2192       self->priv->got_video = FALSE;
2193       return TRUE;
2194     }
2195
2196   /* No video in the last N seconds, display the remote avatar */
2197   empathy_call_window_show_video_output (self, FALSE);
2198
2199   return TRUE;
2200 }
2201
2202 /* Called from the streaming thread */
2203 static gboolean
2204 empathy_call_window_video_probe_cb (GstPad *pad,
2205     GstMiniObject *mini_obj,
2206     EmpathyCallWindow *self)
2207 {
2208   /* Ignore events */
2209   if (GST_IS_EVENT (mini_obj))
2210     return TRUE;
2211
2212   if (G_UNLIKELY (!self->priv->got_video))
2213     {
2214       /* show the remote video */
2215       g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
2216           empathy_call_window_show_video_output_cb,
2217           g_object_ref (self), g_object_unref);
2218
2219       self->priv->got_video = TRUE;
2220     }
2221
2222   return TRUE;
2223 }
2224
2225 /* Called from the streaming thread */
2226 static gboolean
2227 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
2228   GstPad *src, guint media_type, gpointer user_data)
2229 {
2230   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2231   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2232   gboolean retval = FALSE;
2233
2234   GstPad *pad;
2235
2236   g_mutex_lock (priv->lock);
2237
2238   switch (media_type)
2239     {
2240       case TP_MEDIA_STREAM_TYPE_AUDIO:
2241         pad = empathy_call_window_get_audio_sink_pad (self);
2242         break;
2243       case TP_MEDIA_STREAM_TYPE_VIDEO:
2244         g_idle_add (empathy_call_window_show_video_output_cb, self);
2245         pad = empathy_call_window_get_video_sink_pad (self);
2246
2247         gst_pad_add_data_probe (src,
2248             G_CALLBACK (empathy_call_window_video_probe_cb), self);
2249         if (priv->got_video_src > 0)
2250           g_source_remove (priv->got_video_src);
2251         priv->got_video_src = g_timeout_add_seconds (5,
2252             empathy_call_window_check_video_cb, self);
2253         break;
2254       default:
2255         g_assert_not_reached ();
2256     }
2257
2258   if (pad == NULL)
2259     goto out;
2260
2261   if (GST_PAD_LINK_FAILED (gst_pad_link (src, pad)))
2262       g_warning ("Could not link %s sink pad",
2263           media_type == TP_MEDIA_STREAM_TYPE_AUDIO ? "audio" : "video");
2264   else
2265       retval = TRUE;
2266
2267   gst_object_unref (pad);
2268
2269  out:
2270
2271   /* If no sink could be linked, try to add fakesink to prevent the whole call
2272    * aborting */
2273
2274   if (!retval)
2275     {
2276       GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
2277
2278       if (gst_bin_add (GST_BIN (priv->pipeline), fakesink))
2279         {
2280           GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
2281           if (gst_element_set_state (fakesink, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE ||
2282               GST_PAD_LINK_FAILED (gst_pad_link (src, sinkpad)))
2283             {
2284               gst_element_set_locked_state (fakesink, TRUE);
2285               gst_element_set_state (fakesink, GST_STATE_NULL);
2286               gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2287             }
2288           else
2289             {
2290               DEBUG ("Could not link real sink, linked fakesink instead");
2291             }
2292           gst_object_unref (sinkpad);
2293         }
2294       else
2295         {
2296           gst_object_unref (fakesink);
2297         }
2298     }
2299
2300
2301   g_mutex_unlock (priv->lock);
2302
2303   return TRUE;
2304 }
2305
2306 static gboolean
2307 empathy_call_window_sink_added_cb (EmpathyCallHandler *handler,
2308   GstPad *sink, FsMediaType media_type, gpointer user_data)
2309 {
2310   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2311   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2312   GstPad *pad;
2313   gboolean retval = FALSE;
2314
2315   switch (media_type)
2316     {
2317       case FS_MEDIA_TYPE_AUDIO:
2318         if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input))
2319           {
2320             g_warning ("Could not add audio source to pipeline");
2321             break;
2322           }
2323
2324         pad = gst_element_get_static_pad (priv->audio_input, "src");
2325         if (!pad)
2326           {
2327             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2328             g_warning ("Could not get source pad from audio source");
2329             break;
2330           }
2331
2332         if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
2333           {
2334             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2335             g_warning ("Could not link audio source to farsight");
2336             break;
2337           }
2338
2339         if (gst_element_set_state (priv->audio_input, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2340           {
2341             g_warning ("Could not start audio source");
2342             gst_element_set_state (priv->audio_input, GST_STATE_NULL);
2343             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2344             break;
2345           }
2346
2347         retval = TRUE;
2348         break;
2349       case FS_MEDIA_TYPE_VIDEO:
2350         if (priv->video_tee != NULL)
2351           {
2352             pad = gst_element_get_request_pad (priv->video_tee, "src%d");
2353             if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
2354               {
2355                 g_warning ("Could not link video source input pipeline");
2356                 break;
2357               }
2358             gst_object_unref (pad);
2359           }
2360
2361         retval = TRUE;
2362         break;
2363       default:
2364         g_assert_not_reached ();
2365     }
2366
2367   return retval;
2368 }
2369
2370 static void
2371 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
2372 {
2373   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2374   GstElement *preview;
2375
2376   disable_camera (self);
2377
2378   DEBUG ("remove video input");
2379   preview = priv->video_preview_sink;
2380
2381   gst_element_set_state (priv->video_input, GST_STATE_NULL);
2382   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
2383   gst_element_set_state (preview, GST_STATE_NULL);
2384
2385   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
2386     preview, NULL);
2387
2388   g_object_unref (priv->video_input);
2389   priv->video_input = NULL;
2390   g_object_unref (priv->video_tee);
2391   priv->video_tee = NULL;
2392   clutter_actor_destroy (priv->video_preview);
2393   priv->video_preview = NULL;
2394
2395   gtk_widget_set_sensitive (priv->camera_button, FALSE);
2396 }
2397
2398 static void
2399 start_call (EmpathyCallWindow *self)
2400 {
2401   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2402
2403   priv->call_started = TRUE;
2404   empathy_call_handler_start_call (priv->handler,
2405       gtk_get_current_event_time ());
2406
2407   if (empathy_call_handler_has_initial_video (priv->handler))
2408     {
2409       TpyCallChannel *call;
2410       TpySendingState s;
2411
2412       g_object_get (priv->handler, "call-channel", &call, NULL);
2413       s = tpy_call_channel_get_video_state (call);
2414
2415       if (s == TPY_SENDING_STATE_PENDING_SEND ||
2416           s == TPY_SENDING_STATE_SENDING)
2417         {
2418           /* Enable 'send video' buttons and display the preview */
2419           gtk_toggle_tool_button_set_active (
2420             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), TRUE);
2421         }
2422       else
2423         {
2424           gtk_toggle_tool_button_set_active (
2425             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
2426
2427           if (priv->video_preview == NULL)
2428             {
2429               create_video_preview (self);
2430               add_video_preview_to_pipeline (self);
2431             }
2432         }
2433
2434       g_object_unref (call);
2435     }
2436 }
2437
2438 static gboolean
2439 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
2440   gpointer user_data)
2441 {
2442   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2443   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2444   GstState newstate;
2445
2446   empathy_call_handler_bus_message (priv->handler, bus, message);
2447
2448   switch (GST_MESSAGE_TYPE (message))
2449     {
2450       case GST_MESSAGE_STATE_CHANGED:
2451         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
2452           {
2453             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
2454           }
2455         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
2456             !priv->call_started)
2457           {
2458             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
2459             if (newstate == GST_STATE_PAUSED)
2460               {
2461                 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
2462                 priv->pipeline_playing = TRUE;
2463
2464                 if (priv->start_call_when_playing)
2465                   start_call (self);
2466               }
2467           }
2468         break;
2469       case GST_MESSAGE_ERROR:
2470         {
2471           GError *error = NULL;
2472           GstElement *gst_error;
2473           gchar *debug;
2474
2475           gst_message_parse_error (message, &error, &debug);
2476           gst_error = GST_ELEMENT (GST_MESSAGE_SRC (message));
2477
2478           g_message ("Element error: %s -- %s\n", error->message, debug);
2479
2480           if (g_str_has_prefix (gst_element_get_name (gst_error),
2481                 VIDEO_INPUT_ERROR_PREFIX))
2482             {
2483               /* Remove the video input and continue */
2484               if (priv->video_input != NULL)
2485                 empathy_call_window_remove_video_input (self);
2486               gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
2487             }
2488           else
2489             {
2490               empathy_call_window_disconnected (self, TRUE);
2491             }
2492           g_error_free (error);
2493           g_free (debug);
2494         }
2495       case GST_MESSAGE_UNKNOWN:
2496       case GST_MESSAGE_EOS:
2497       case GST_MESSAGE_WARNING:
2498       case GST_MESSAGE_INFO:
2499       case GST_MESSAGE_TAG:
2500       case GST_MESSAGE_BUFFERING:
2501       case GST_MESSAGE_STATE_DIRTY:
2502       case GST_MESSAGE_STEP_DONE:
2503       case GST_MESSAGE_CLOCK_PROVIDE:
2504       case GST_MESSAGE_CLOCK_LOST:
2505       case GST_MESSAGE_NEW_CLOCK:
2506       case GST_MESSAGE_STRUCTURE_CHANGE:
2507       case GST_MESSAGE_STREAM_STATUS:
2508       case GST_MESSAGE_APPLICATION:
2509       case GST_MESSAGE_ELEMENT:
2510       case GST_MESSAGE_SEGMENT_START:
2511       case GST_MESSAGE_SEGMENT_DONE:
2512       case GST_MESSAGE_DURATION:
2513       case GST_MESSAGE_ANY:
2514       default:
2515         break;
2516     }
2517
2518   return TRUE;
2519 }
2520
2521 static void
2522 empathy_call_window_members_changed_cb (TpyCallChannel *call,
2523     GHashTable *members,
2524     EmpathyCallWindow *self)
2525 {
2526   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2527   GHashTableIter iter;
2528   gpointer key, value;
2529   gboolean held = FALSE;
2530
2531   g_hash_table_iter_init (&iter, members);
2532   while (g_hash_table_iter_next (&iter, &key, &value))
2533     {
2534       if (GPOINTER_TO_INT (value) & TPY_CALL_MEMBER_FLAG_HELD)
2535         {
2536           /* This assumes this is a 1-1 call, otherwise one participant
2537            * putting the call on hold wouldn't mean the call is on hold
2538            * for everyone. */
2539           held = TRUE;
2540           break;
2541         }
2542     }
2543
2544   if (held)
2545     priv->call_state = HELD;
2546   else if (priv->call_state == HELD)
2547     priv->call_state = CONNECTED;
2548 }
2549
2550 static void
2551 call_handler_notify_call_cb (EmpathyCallHandler *handler,
2552     GParamSpec *spec,
2553     EmpathyCallWindow *self)
2554 {
2555   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2556   TpyCallChannel *call;
2557
2558   g_object_get (priv->handler, "call-channel", &call, NULL);
2559   if (call == NULL)
2560     return;
2561
2562 /* FIXME
2563   tp_g_signal_connect_object (call, "audio-stream-error",
2564       G_CALLBACK (empathy_call_window_audio_stream_error), self, 0);
2565   tp_g_signal_connect_object (call, "video-stream-error",
2566       G_CALLBACK (empathy_call_window_video_stream_error), self, 0);
2567 */
2568
2569   tp_g_signal_connect_object (call, "members-changed",
2570       G_CALLBACK (empathy_call_window_members_changed_cb), self, 0);
2571
2572   g_object_unref (call);
2573 }
2574
2575 static void
2576 empathy_call_window_realized_cb (GtkWidget *widget, EmpathyCallWindow *window)
2577 {
2578   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2579   TpyCallChannel *call;
2580   gint width;
2581
2582   /* Make the hangup button twice as wide */
2583   width = gtk_widget_get_allocated_width (priv->hangup_button);
2584   gtk_widget_set_size_request (priv->hangup_button, width * 2, -1);
2585
2586   g_signal_connect (priv->handler, "state-changed",
2587     G_CALLBACK (empathy_call_window_state_changed_cb), window);
2588   g_signal_connect (priv->handler, "conference-added",
2589     G_CALLBACK (empathy_call_window_conference_added_cb), window);
2590   g_signal_connect (priv->handler, "conference-removed",
2591     G_CALLBACK (empathy_call_window_conference_removed_cb), window);
2592   g_signal_connect (priv->handler, "closed",
2593     G_CALLBACK (empathy_call_window_channel_closed_cb), window);
2594   g_signal_connect (priv->handler, "src-pad-added",
2595     G_CALLBACK (empathy_call_window_src_added_cb), window);
2596   g_signal_connect (priv->handler, "sink-pad-added",
2597     G_CALLBACK (empathy_call_window_sink_added_cb), window);
2598   g_signal_connect (priv->handler, "sink-pad-removed",
2599     G_CALLBACK (empathy_call_window_sink_removed_cb), window);
2600
2601   g_object_get (priv->handler, "call-channel", &call, NULL);
2602   if (call != NULL)
2603     {
2604       call_handler_notify_call_cb (priv->handler, NULL, window);
2605       g_object_unref (call);
2606     }
2607   else
2608     {
2609       /* call-channel doesn't exist yet, we'll connect signals once it has been
2610        * set */
2611       g_signal_connect (priv->handler, "notify::call-channel",
2612         G_CALLBACK (call_handler_notify_call_cb), window);
2613     }
2614
2615   gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2616 }
2617
2618 static gboolean
2619 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
2620   EmpathyCallWindow *window)
2621 {
2622   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2623
2624   if (priv->pipeline != NULL)
2625     {
2626       if (priv->bus_message_source_id != 0)
2627         {
2628           g_source_remove (priv->bus_message_source_id);
2629           priv->bus_message_source_id = 0;
2630         }
2631
2632       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2633     }
2634
2635   if (priv->call_state == CONNECTING)
2636     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2637
2638   return FALSE;
2639 }
2640
2641 static void
2642 show_controls (EmpathyCallWindow *window, gboolean set_fullscreen)
2643 {
2644   GtkWidget *menu;
2645   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2646
2647   menu = gtk_ui_manager_get_widget (priv->ui_manager,
2648             "/menubar1");
2649
2650   if (set_fullscreen)
2651     {
2652       gtk_widget_hide (priv->dtmf_panel);
2653       gtk_widget_hide (menu);
2654       gtk_widget_hide (priv->toolbar);
2655     }
2656   else
2657     {
2658       if (priv->dialpad_was_visible_before_fs)
2659         gtk_widget_show (priv->dtmf_panel);
2660
2661       gtk_widget_show (menu);
2662       gtk_widget_show (priv->toolbar);
2663
2664       gtk_window_resize (GTK_WINDOW (window), priv->original_width_before_fs,
2665           priv->original_height_before_fs);
2666     }
2667 }
2668
2669 static void
2670 show_borders (EmpathyCallWindow *window, gboolean set_fullscreen)
2671 {
2672   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2673
2674   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
2675       set_fullscreen ? 0 : CONTENT_HBOX_BORDER_WIDTH);
2676   gtk_box_set_spacing (GTK_BOX (priv->content_hbox),
2677       set_fullscreen ? 0 : CONTENT_HBOX_SPACING);
2678
2679   if (priv->video_output != NULL)
2680     {
2681 #if 0
2682       gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
2683           priv->video_output, TRUE, TRUE,
2684           set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
2685           GTK_PACK_START);
2686 #endif
2687     }
2688 }
2689
2690 static gboolean
2691 empathy_call_window_state_event_cb (GtkWidget *widget,
2692   GdkEventWindowState *event, EmpathyCallWindow *window)
2693 {
2694   if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
2695     {
2696       EmpathyCallWindowPriv *priv = GET_PRIV (window);
2697       gboolean set_fullscreen = event->new_window_state &
2698         GDK_WINDOW_STATE_FULLSCREEN;
2699
2700       if (set_fullscreen)
2701         {
2702           gboolean dialpad_was_visible;
2703           GtkAllocation allocation;
2704           gint original_width, original_height;
2705
2706           gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
2707           original_width = allocation.width;
2708           original_height = allocation.height;
2709
2710           g_object_get (priv->dtmf_panel,
2711               "visible", &dialpad_was_visible,
2712               NULL);
2713
2714           priv->dialpad_was_visible_before_fs = dialpad_was_visible;
2715           priv->original_width_before_fs = original_width;
2716           priv->original_height_before_fs = original_height;
2717
2718           if (priv->video_output_motion_handler_id == 0 &&
2719                 priv->video_output != NULL)
2720             {
2721               priv->video_output_motion_handler_id = g_signal_connect (
2722                   G_OBJECT (priv->video_container), "motion-notify-event",
2723                   G_CALLBACK (empathy_call_window_video_output_motion_notify),
2724                   window);
2725             }
2726         }
2727       else
2728         {
2729           disconnect_video_output_motion_handler (window);
2730         }
2731
2732       empathy_call_window_fullscreen_set_fullscreen (priv->fullscreen,
2733           set_fullscreen);
2734       show_controls (window, set_fullscreen);
2735       show_borders (window, set_fullscreen);
2736       gtk_action_set_stock_id (priv->menu_fullscreen,
2737           (set_fullscreen ? "gtk-leave-fullscreen" : "gtk-fullscreen"));
2738       priv->is_fullscreen = set_fullscreen;
2739   }
2740
2741   return FALSE;
2742 }
2743
2744 static void
2745 empathy_call_window_show_dialpad (EmpathyCallWindow *window,
2746     gboolean active)
2747 {
2748   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2749   int w, h, dialpad_width;
2750   GtkAllocation allocation;
2751
2752   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
2753   w = allocation.width;
2754   h = allocation.height;
2755
2756   gtk_widget_get_preferred_width (priv->dtmf_panel, &dialpad_width, NULL);
2757
2758   if (active)
2759     {
2760       gtk_widget_show (priv->dtmf_panel);
2761       w += dialpad_width;
2762     }
2763   else
2764     {
2765       w -= dialpad_width;
2766       gtk_widget_hide (priv->dtmf_panel);
2767     }
2768
2769   if (w > 0 && h > 0)
2770     gtk_window_resize (GTK_WINDOW (window), w, h);
2771 }
2772
2773 static void
2774 empathy_call_window_set_send_video (EmpathyCallWindow *window,
2775   CameraState state)
2776 {
2777   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2778   TpyCallChannel *call;
2779
2780   priv->sending_video = (state == CAMERA_STATE_ON);
2781
2782   if (state == CAMERA_STATE_ON)
2783     {
2784       /* When we start sending video, we want to show the video preview by
2785          default. */
2786       display_video_preview (window, TRUE);
2787     }
2788   else
2789     {
2790       display_video_preview (window, FALSE);
2791     }
2792
2793   if (priv->call_state != CONNECTED)
2794     return;
2795
2796   g_object_get (priv->handler, "call-channel", &call, NULL);
2797   DEBUG ("%s sending video", priv->sending_video ? "start": "stop");
2798   tpy_call_channel_send_video (call, priv->sending_video);
2799   g_object_unref (call);
2800 }
2801
2802 static void
2803 empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
2804   EmpathyCallWindow *window)
2805 {
2806   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2807   gboolean active;
2808
2809   active = (gtk_toggle_tool_button_get_active (toggle));
2810
2811   if (active)
2812     {
2813       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
2814           priv->volume * 100);
2815     }
2816   else
2817     {
2818       /* TODO, Instead of setting the input volume to 0 we should probably
2819        * stop sending but this would cause the audio call to drop if both
2820        * sides mute at the same time on certain CMs AFAIK. Need to revisit this
2821        * in the future. GNOME #574574
2822        */
2823       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
2824           0);
2825     }
2826 }
2827
2828 static void
2829 empathy_call_window_hangup_cb (gpointer object,
2830                                EmpathyCallWindow *window)
2831 {
2832   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2833
2834   empathy_call_handler_stop_call (priv->handler);
2835
2836   if (empathy_call_window_disconnected (window, FALSE))
2837     gtk_widget_destroy (GTK_WIDGET (window));
2838 }
2839
2840 static void
2841 empathy_call_window_restart_call (EmpathyCallWindow *window)
2842 {
2843   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2844
2845   /* Remove error info bars */
2846   gtk_container_forall (GTK_CONTAINER (priv->errors_vbox),
2847       (GtkCallback) gtk_widget_destroy, NULL);
2848
2849   create_video_output_widget (window);
2850
2851   /* While the call was disconnected, the input volume might have changed.
2852    * However, since the audio_input source was destroyed, its volume has not
2853    * been updated during that time. That's why we manually update it here */
2854   empathy_call_window_mic_volume_changed (window);
2855
2856   priv->outgoing = TRUE;
2857   empathy_call_window_set_state_connecting (window);
2858
2859   if (priv->pipeline_playing)
2860     start_call (window);
2861   else
2862     /* call will be started when the pipeline is ready */
2863     priv->start_call_when_playing = TRUE;
2864
2865   empathy_call_window_setup_avatars (window, priv->handler);
2866
2867   empathy_call_window_show_hangup_button (window, TRUE);
2868 }
2869
2870 static void
2871 empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
2872     EmpathyCallWindow *window)
2873 {
2874   gboolean active;
2875
2876   active = gtk_toggle_tool_button_get_active (button);
2877
2878   empathy_call_window_show_dialpad (window, active);
2879 }
2880
2881 static void
2882 empathy_call_window_fullscreen_cb (gpointer object,
2883                                    EmpathyCallWindow *window)
2884 {
2885   empathy_call_window_fullscreen_toggle (window);
2886 }
2887
2888 static void
2889 empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window)
2890 {
2891   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2892
2893   if (priv->is_fullscreen)
2894     gtk_window_unfullscreen (GTK_WINDOW (window));
2895   else
2896     gtk_window_fullscreen (GTK_WINDOW (window));
2897 }
2898
2899 static gboolean
2900 empathy_call_window_video_button_press_cb (GtkWidget *video_preview,
2901   GdkEventButton *event, EmpathyCallWindow *window)
2902 {
2903   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
2904     {
2905       empathy_call_window_video_menu_popup (window, event->button);
2906       return TRUE;
2907     }
2908
2909   return FALSE;
2910 }
2911
2912 static gboolean
2913 empathy_call_window_key_press_cb (GtkWidget *video_output,
2914   GdkEventKey *event, EmpathyCallWindow *window)
2915 {
2916   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2917
2918   if (priv->is_fullscreen && event->keyval == GDK_KEY_Escape)
2919     {
2920       /* Since we are in fullscreen mode, toggling will bring us back to
2921          normal mode. */
2922       empathy_call_window_fullscreen_toggle (window);
2923       return TRUE;
2924     }
2925
2926   return FALSE;
2927 }
2928
2929 static gboolean
2930 empathy_call_window_video_output_motion_notify (GtkWidget *widget,
2931     GdkEventMotion *event, EmpathyCallWindow *window)
2932 {
2933   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2934
2935   if (priv->is_fullscreen)
2936     {
2937       empathy_call_window_fullscreen_show_popup (priv->fullscreen);
2938       return TRUE;
2939     }
2940   return FALSE;
2941 }
2942
2943 static void
2944 empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
2945   guint button)
2946 {
2947   GtkWidget *menu;
2948   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2949
2950   menu = gtk_ui_manager_get_widget (priv->ui_manager,
2951             "/video-popup");
2952   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
2953       button, gtk_get_current_event_time ());
2954   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
2955 }
2956
2957 static void
2958 empathy_call_window_status_message (EmpathyCallWindow *self,
2959   gchar *message)
2960 {
2961   gtk_label_set_label (GTK_LABEL (self->priv->status_label), message);
2962 }
2963
2964 static void
2965 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
2966   gdouble value, EmpathyCallWindow *window)
2967 {
2968   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2969
2970   if (priv->audio_output == NULL)
2971     return;
2972
2973   empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (priv->audio_output),
2974     value);
2975 }
2976
2977 GtkUIManager *
2978 empathy_call_window_get_ui_manager (EmpathyCallWindow *window)
2979 {
2980   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2981
2982   return priv->ui_manager;
2983 }
2984
2985 EmpathyGstAudioSrc *
2986 empathy_call_window_get_audio_src (EmpathyCallWindow *window)
2987 {
2988   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2989
2990   return (EmpathyGstAudioSrc *) priv->audio_input;
2991 }