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