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