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