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