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