]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
CallWindow: round the video preview corners
[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 =
1044       gtk_clutter_actor_new_with_contents (button);
1045
1046   layout_end = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_END,
1047       CLUTTER_BIN_ALIGNMENT_END);
1048   box = clutter_box_new (layout_end);
1049   clutter_actor_set_size (box,
1050       SELF_VIDEO_SECTION_WIDTH,
1051       SELF_VIDEO_SECTION_HEIGHT + SELF_VIDEO_SECTION_MARGIN);
1052
1053   clutter_container_add_actor (CLUTTER_CONTAINER (box), b);
1054   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_preview), box);
1055
1056   g_signal_connect (button, "clicked",
1057       G_CALLBACK (empathy_call_window_preview_button_clicked_cb),
1058       self);
1059
1060   /* Translators: this is an "Info" label. It should be as short
1061    * as possible. */
1062   button = gtk_button_new_with_label (_("i"));
1063   priv->preview_hidden_button =
1064       gtk_clutter_actor_new_with_contents (button);
1065
1066   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1067       priv->preview_hidden_button,
1068       CLUTTER_BIN_ALIGNMENT_START,
1069       CLUTTER_BIN_ALIGNMENT_END);
1070
1071   self->priv->preview_pos = PREVIEW_POS_BOTTOM_LEFT;
1072
1073   clutter_actor_hide (priv->preview_hidden_button);
1074
1075   g_signal_connect (button, "clicked",
1076       G_CALLBACK (empathy_call_window_preview_hidden_button_clicked_cb),
1077       self);
1078
1079   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1080       priv->video_preview,
1081       CLUTTER_BIN_ALIGNMENT_START,
1082       CLUTTER_BIN_ALIGNMENT_END);
1083
1084   action = clutter_drag_action_new ();
1085   g_signal_connect (action, "drag-begin",
1086       G_CALLBACK (empathy_call_window_preview_on_drag_begin_cb), self);
1087   g_signal_connect (action, "drag-end",
1088       G_CALLBACK (empathy_call_window_preview_on_drag_end_cb), self);
1089   g_signal_connect (action, "drag-motion",
1090       G_CALLBACK (empathy_call_window_preview_on_drag_motion_cb), self);
1091
1092   g_signal_connect (preview, "enter-event",
1093       G_CALLBACK (empathy_call_window_preview_enter_event_cb), self);
1094   g_signal_connect (preview, "leave-event",
1095       G_CALLBACK (empathy_call_window_preview_leave_event_cb), self);
1096
1097   clutter_actor_add_action (preview, action);
1098   clutter_actor_set_reactive (preview, TRUE);
1099 }
1100
1101 static void
1102 play_camera (EmpathyCallWindow *window,
1103     gboolean play)
1104 {
1105   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1106   GstElement *preview;
1107   GstState state;
1108
1109   if (priv->video_preview == NULL)
1110     {
1111       create_video_preview (window);
1112       add_video_preview_to_pipeline (window);
1113     }
1114
1115   if (play)
1116     state = GST_STATE_PLAYING;
1117   else
1118     state = GST_STATE_NULL;
1119
1120   preview = priv->video_preview_sink;
1121
1122   gst_element_set_state (preview, state);
1123   gst_element_set_state (priv->video_input, state);
1124   gst_element_set_state (priv->video_tee, state);
1125 }
1126
1127 static void
1128 display_video_preview (EmpathyCallWindow *self,
1129     gboolean display)
1130 {
1131   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1132
1133   if (display)
1134     {
1135       /* Display the video preview */
1136       DEBUG ("Show video preview");
1137
1138       play_camera (self, TRUE);
1139       clutter_actor_show (priv->video_preview);
1140       clutter_actor_raise_top (priv->floating_toolbar);
1141     }
1142   else
1143     {
1144       /* Hide the video preview */
1145       DEBUG ("Hide video preview");
1146
1147       if (priv->video_preview != NULL)
1148         {
1149           clutter_actor_hide (priv->video_preview);
1150           play_camera (self, FALSE);
1151         }
1152     }
1153 }
1154
1155 static void
1156 empathy_call_window_set_state_connecting (EmpathyCallWindow *window)
1157 {
1158   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1159
1160   empathy_call_window_status_message (window, _("Connecting…"));
1161   priv->call_state = CONNECTING;
1162
1163   /* Show the toolbar */
1164   clutter_state_set_state (priv->transitions, "fade-in");
1165
1166   if (priv->outgoing)
1167     empathy_sound_manager_start_playing (priv->sound_mgr, GTK_WIDGET (window),
1168         EMPATHY_SOUND_PHONE_OUTGOING, MS_BETWEEN_RING);
1169 }
1170
1171 static void
1172 disable_camera (EmpathyCallWindow *self)
1173 {
1174   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1175
1176   if (priv->camera_state == CAMERA_STATE_OFF)
1177     return;
1178
1179   DEBUG ("Disable camera");
1180
1181   display_video_preview (self, FALSE);
1182
1183   if (priv->camera_state == CAMERA_STATE_ON)
1184     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
1185
1186   priv->camera_state = CAMERA_STATE_OFF;
1187 }
1188
1189 static void
1190 enable_camera (EmpathyCallWindow *self)
1191 {
1192   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1193
1194   if (priv->camera_state == CAMERA_STATE_ON)
1195     return;
1196
1197   if (priv->video_input == NULL)
1198     {
1199       DEBUG ("Can't enable camera, no input");
1200       return;
1201     }
1202
1203   DEBUG ("Enable camera");
1204
1205   empathy_call_window_set_send_video (self, CAMERA_STATE_ON);
1206
1207   priv->camera_state = CAMERA_STATE_ON;
1208 }
1209
1210 static void
1211 empathy_call_window_camera_toggled_cb (GtkToggleToolButton *toggle,
1212   EmpathyCallWindow *self)
1213 {
1214   if (gtk_toggle_tool_button_get_active (toggle))
1215     enable_camera (self);
1216   else
1217     disable_camera (self);
1218 }
1219
1220 static void
1221 create_pipeline (EmpathyCallWindow *self)
1222 {
1223   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1224   GstBus *bus;
1225
1226   g_assert (priv->pipeline == NULL);
1227
1228   priv->pipeline = gst_pipeline_new (NULL);
1229   priv->pipeline_playing = FALSE;
1230
1231   priv->video_tee = gst_element_factory_make ("tee", NULL);
1232   gst_object_ref (priv->video_tee);
1233   gst_object_sink (priv->video_tee);
1234
1235   gst_bin_add (GST_BIN (priv->pipeline), priv->video_tee);
1236
1237   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
1238   priv->bus_message_source_id = gst_bus_add_watch (bus,
1239       empathy_call_window_bus_message, self);
1240
1241   g_object_unref (bus);
1242 }
1243
1244 static void
1245 empathy_call_window_settings_cb (GtkAction *action,
1246     EmpathyCallWindow *self)
1247 {
1248   gchar *args = g_strdup_printf ("-p %s",
1249       empathy_preferences_tab_to_string (EMPATHY_PREFERENCES_TAB_CALLS));
1250
1251   empathy_launch_program (BIN_DIR, "empathy", args);
1252
1253   g_free (args);
1254 }
1255
1256 static void
1257 empathy_call_window_contents_cb (GtkAction *action,
1258     EmpathyCallWindow *self)
1259 {
1260   empathy_url_show (GTK_WIDGET (self), "ghelp:empathy?audio-video");
1261 }
1262
1263 static void
1264 empathy_call_window_debug_cb (GtkAction *action,
1265     EmpathyCallWindow *self)
1266 {
1267   empathy_launch_program (BIN_DIR, "empathy-debugger", "-s Empathy.Call");
1268 }
1269
1270 static void
1271 empathy_call_window_about_cb (GtkAction *action,
1272     EmpathyCallWindow *self)
1273 {
1274   empathy_about_dialog_new (GTK_WINDOW (self));
1275 }
1276
1277 static gboolean
1278 empathy_call_window_toolbar_timeout (gpointer data)
1279 {
1280   EmpathyCallWindow *self = data;
1281
1282   /* We don't want to hide the toolbar if we're not in a call, as
1283    * to show the call status all the time. */
1284   if (self->priv->call_state != CONNECTING &&
1285       self->priv->call_state != DISCONNECTED)
1286     clutter_state_set_state (self->priv->transitions, "fade-out");
1287
1288   return TRUE;
1289 }
1290
1291 static gboolean
1292 empathy_call_window_motion_notify_cb (GtkWidget *widget,
1293     GdkEvent *event,
1294     EmpathyCallWindow *self)
1295 {
1296   clutter_state_set_state (self->priv->transitions, "fade-in");
1297
1298   if (self->priv->inactivity_src > 0)
1299     g_source_remove (self->priv->inactivity_src);
1300
1301   self->priv->inactivity_src = g_timeout_add_seconds (3,
1302       empathy_call_window_toolbar_timeout, self);
1303
1304   return FALSE;
1305 }
1306
1307 static gboolean
1308 empathy_call_window_configure_event_cb (GtkWidget *widget,
1309     GdkEvent  *event,
1310     EmpathyCallWindow *self)
1311 {
1312   GdkWindow *gdk_window;
1313   GdkWindowState window_state;
1314
1315   gtk_window_get_position (GTK_WINDOW (self), &self->priv->x, &self->priv->y);
1316   gtk_window_get_size (GTK_WINDOW (self), &self->priv->w, &self->priv->h);
1317
1318   gtk_widget_get_preferred_width (self->priv->dtmf_panel,
1319       &self->priv->dialpad_width, NULL);
1320
1321   gdk_window = gtk_widget_get_window (widget);
1322   window_state = gdk_window_get_state (gdk_window);
1323   self->priv->maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED);
1324
1325   return FALSE;
1326 }
1327
1328 static void
1329 empathy_call_window_destroyed_cb (GtkWidget *object,
1330     EmpathyCallWindow *self)
1331 {
1332   if (gtk_widget_get_visible (self->priv->dtmf_panel))
1333     {
1334       /* Save the geometry as if the dialpad was hidden. */
1335       empathy_geometry_save_values (GTK_WINDOW (self),
1336           self->priv->x, self->priv->y,
1337           self->priv->w - self->priv->dialpad_width, self->priv->h,
1338           self->priv->maximized);
1339     }
1340 }
1341
1342 static void
1343 empathy_call_window_stage_allocation_changed_cb (ClutterActor *stage,
1344     GParamSpec *pspec,
1345     ClutterBindConstraint *constraint)
1346 {
1347   ClutterActorBox allocation;
1348
1349   clutter_actor_get_allocation_box (stage, &allocation);
1350
1351   clutter_bind_constraint_set_offset (constraint,
1352       allocation.y2 - allocation.y1 -
1353       FLOATING_TOOLBAR_SPACING - FLOATING_TOOLBAR_HEIGHT);
1354 }
1355
1356 static void
1357 empathy_call_window_init (EmpathyCallWindow *self)
1358 {
1359   EmpathyCallWindowPriv *priv;
1360   GtkBuilder *gui;
1361   GtkWidget *top_vbox;
1362   gchar *filename;
1363   ClutterConstraint *constraint;
1364   ClutterActor *remote_avatar;
1365   GtkStyleContext *context;
1366   GdkRGBA rgba;
1367   ClutterColor bg;
1368
1369   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
1370     EMPATHY_TYPE_CALL_WINDOW, EmpathyCallWindowPriv);
1371
1372   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
1373   gui = empathy_builder_get_file (filename,
1374     "call_window_vbox", &top_vbox,
1375     "errors_vbox", &priv->errors_vbox,
1376     "pane", &priv->pane,
1377     "remote_user_name_toolbar", &priv->remote_user_name_toolbar,
1378     "remote_user_avatar_toolbar", &priv->remote_user_avatar_toolbar,
1379     "status_label", &priv->status_label,
1380     "audiocall", &priv->audio_call_button,
1381     "videocall", &priv->video_call_button,
1382     "microphone", &priv->mic_button,
1383     "camera", &priv->camera_button,
1384     "hangup", &priv->hangup_button,
1385     "dialpad", &priv->dialpad_button,
1386     "toolbar", &priv->toolbar,
1387     "bottom_toolbar", &priv->bottom_toolbar,
1388     "ui_manager", &priv->ui_manager,
1389     "menufullscreen", &priv->menu_fullscreen,
1390     "menupreviewswap", &priv->menu_swap_camera,
1391     "details_vbox",  &priv->details_vbox,
1392     "vcodec_encoding_label", &priv->vcodec_encoding_label,
1393     "acodec_encoding_label", &priv->acodec_encoding_label,
1394     "acodec_decoding_label", &priv->acodec_decoding_label,
1395     "vcodec_decoding_label", &priv->vcodec_decoding_label,
1396     "audio_remote_candidate_label", &priv->audio_remote_candidate_label,
1397     "audio_local_candidate_label", &priv->audio_local_candidate_label,
1398     "video_remote_candidate_label", &priv->video_remote_candidate_label,
1399     "video_local_candidate_label", &priv->video_local_candidate_label,
1400     "video_remote_candidate_info_img", &priv->video_remote_candidate_info_img,
1401     "video_local_candidate_info_img", &priv->video_local_candidate_info_img,
1402     "audio_remote_candidate_info_img", &priv->audio_remote_candidate_info_img,
1403     "audio_local_candidate_info_img", &priv->audio_local_candidate_info_img,
1404     NULL);
1405   g_free (filename);
1406
1407   empathy_builder_connect (gui, self,
1408     "hangup", "clicked", empathy_call_window_hangup_cb,
1409     "audiocall", "clicked", empathy_call_window_audio_call_cb,
1410     "videocall", "clicked", empathy_call_window_video_call_cb,
1411     "volume", "value-changed", empathy_call_window_volume_changed_cb,
1412     "microphone", "toggled", empathy_call_window_mic_toggled_cb,
1413     "camera", "toggled", empathy_call_window_camera_toggled_cb,
1414     "dialpad", "toggled", empathy_call_window_dialpad_cb,
1415     "menufullscreen", "activate", empathy_call_window_fullscreen_cb,
1416     "menusettings", "activate", empathy_call_window_settings_cb,
1417     "menucontents", "activate", empathy_call_window_contents_cb,
1418     "menudebug", "activate", empathy_call_window_debug_cb,
1419     "menuabout", "activate", empathy_call_window_about_cb,
1420     "menupreviewdisable", "activate", empathy_call_window_disable_camera_cb,
1421     "menupreviewminimise", "activate", empathy_call_window_minimise_camera_cb,
1422     "menupreviewmaximise", "activate", empathy_call_window_maximise_camera_cb,
1423     "menupreviewswap", "activate", empathy_call_window_swap_camera_cb,
1424     NULL);
1425
1426   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
1427
1428   priv->camera_monitor = empathy_camera_monitor_dup_singleton ();
1429
1430   g_object_bind_property (priv->camera_monitor, "available",
1431       priv->camera_button, "sensitive",
1432       G_BINDING_SYNC_CREATE);
1433
1434   g_signal_connect (priv->camera_monitor, "added",
1435       G_CALLBACK (empathy_call_window_camera_added_cb), self);
1436   g_signal_connect (priv->camera_monitor, "removed",
1437       G_CALLBACK (empathy_call_window_camera_removed_cb), self);
1438
1439   priv->lock = g_mutex_new ();
1440
1441   gtk_container_add (GTK_CONTAINER (self), top_vbox);
1442
1443   priv->content_hbox = gtk_hbox_new (FALSE, CONTENT_HBOX_SPACING);
1444   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
1445                                   CONTENT_HBOX_BORDER_WIDTH);
1446   gtk_box_pack_start (GTK_BOX (priv->pane), priv->content_hbox,
1447       TRUE, TRUE, 0);
1448
1449   /* avatar/video box */
1450   priv->video_layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
1451       CLUTTER_BIN_ALIGNMENT_CENTER);
1452
1453   priv->video_box = clutter_box_new (priv->video_layout);
1454
1455   priv->video_container = gtk_clutter_embed_new ();
1456
1457   /* Set the background color to that of the rest of the window */
1458   context = gtk_widget_get_style_context (priv->content_hbox);
1459   gtk_style_context_get_background_color (context,
1460       GTK_STATE_FLAG_NORMAL, &rgba);
1461   bg.red = CLAMP (rgba.red * 255.0, 0, 255);
1462   bg.green = CLAMP (rgba.green * 255.0, 0, 255);
1463   bg.blue = CLAMP (rgba.blue * 255.0, 0, 255);
1464   bg.alpha = CLAMP (rgba.alpha * 255.0, 0, 255);
1465   clutter_stage_set_color (
1466       CLUTTER_STAGE (gtk_clutter_embed_get_stage (
1467           GTK_CLUTTER_EMBED (priv->video_container))),
1468       &bg);
1469
1470   clutter_container_add (
1471       CLUTTER_CONTAINER (gtk_clutter_embed_get_stage (
1472           GTK_CLUTTER_EMBED (priv->video_container))),
1473       priv->video_box,
1474       NULL);
1475
1476   constraint = clutter_bind_constraint_new (
1477       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1478       CLUTTER_BIND_SIZE, 0);
1479   clutter_actor_add_constraint (priv->video_box, constraint);
1480
1481   priv->remote_user_avatar_widget = gtk_image_new ();
1482   remote_avatar = gtk_clutter_actor_new_with_contents (
1483       priv->remote_user_avatar_widget);
1484
1485   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_box),
1486       remote_avatar);
1487
1488   empathy_call_window_create_preview_rectangles (self);
1489
1490   gtk_box_pack_start (GTK_BOX (priv->content_hbox),
1491       priv->video_container, TRUE, TRUE,
1492       CONTENT_HBOX_CHILDREN_PACKING_PADDING);
1493
1494   create_pipeline (self);
1495   create_video_output_widget (self);
1496   create_audio_input (self);
1497   create_video_input (self);
1498
1499   priv->floating_toolbar = empathy_rounded_actor_new ();
1500
1501   gtk_widget_reparent (priv->bottom_toolbar,
1502       gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (priv->floating_toolbar)));
1503
1504   constraint = clutter_bind_constraint_new (
1505       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1506       CLUTTER_BIND_Y, 0);
1507
1508   clutter_actor_add_constraint (priv->floating_toolbar, constraint);
1509
1510   g_signal_connect (
1511       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
1512       "notify::allocation",
1513       G_CALLBACK (empathy_call_window_stage_allocation_changed_cb),
1514       constraint);
1515
1516   clutter_actor_set_size (priv->floating_toolbar,
1517       FLOATING_TOOLBAR_WIDTH, FLOATING_TOOLBAR_HEIGHT);
1518   clutter_actor_set_opacity (priv->floating_toolbar, FLOATING_TOOLBAR_OPACITY);
1519
1520   clutter_bin_layout_add (CLUTTER_BIN_LAYOUT (priv->video_layout),
1521       priv->floating_toolbar,
1522       CLUTTER_BIN_ALIGNMENT_CENTER,
1523       CLUTTER_BIN_ALIGNMENT_END);
1524
1525   clutter_actor_raise_top (priv->floating_toolbar);
1526
1527   /* Transitions for the floating toolbar */
1528   priv->transitions = clutter_state_new ();
1529
1530   /* all transitions last for 2s */
1531   clutter_state_set_duration (priv->transitions, NULL, NULL, 2000);
1532
1533   /* transition from any state to "fade-out" state */
1534   clutter_state_set (priv->transitions, NULL, "fade-out",
1535       priv->floating_toolbar,
1536       "opacity", CLUTTER_EASE_OUT_QUAD, 0,
1537       NULL);
1538
1539   /* transition from any state to "fade-in" state */
1540   clutter_state_set (priv->transitions, NULL, "fade-in",
1541       priv->floating_toolbar,
1542       "opacity", CLUTTER_EASE_OUT_QUAD, FLOATING_TOOLBAR_OPACITY,
1543       NULL);
1544
1545   /* put the actor into the "fade-in" state with no animation */
1546   clutter_state_warp_to_state (priv->transitions, "fade-in");
1547
1548   /* The call will be started as soon the pipeline is playing */
1549   priv->start_call_when_playing = TRUE;
1550
1551   priv->dtmf_panel = empathy_create_dtmf_dialpad (G_OBJECT (self),
1552       G_CALLBACK (dtmf_button_pressed_cb),
1553       G_CALLBACK (dtmf_button_released_cb));
1554
1555   gtk_box_pack_start (GTK_BOX (priv->pane), priv->dtmf_panel,
1556       FALSE, FALSE, 6);
1557
1558   gtk_box_pack_start (GTK_BOX (priv->pane), priv->details_vbox,
1559       FALSE, FALSE, 0);
1560
1561   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
1562
1563   gtk_widget_show_all (top_vbox);
1564
1565   gtk_widget_hide (priv->dtmf_panel);
1566   gtk_widget_hide (priv->details_vbox);
1567
1568   priv->fullscreen = empathy_call_window_fullscreen_new (self);
1569
1570   empathy_call_window_fullscreen_set_video_widget (priv->fullscreen,
1571       priv->video_container);
1572
1573   /* We hide the bottom toolbar after 3s of inactivity and show it
1574    * again on mouse movement */
1575   priv->inactivity_src = g_timeout_add_seconds (3,
1576       empathy_call_window_toolbar_timeout, self);
1577
1578   g_signal_connect (G_OBJECT (priv->fullscreen->leave_fullscreen_button),
1579       "clicked", G_CALLBACK (empathy_call_window_fullscreen_cb), self);
1580
1581   g_signal_connect (G_OBJECT (self), "realize",
1582     G_CALLBACK (empathy_call_window_realized_cb), self);
1583
1584   g_signal_connect (G_OBJECT (self), "delete-event",
1585     G_CALLBACK (empathy_call_window_delete_cb), self);
1586
1587   g_signal_connect (G_OBJECT (self), "window-state-event",
1588     G_CALLBACK (empathy_call_window_state_event_cb), self);
1589
1590   g_signal_connect (G_OBJECT (self), "key-press-event",
1591       G_CALLBACK (empathy_call_window_key_press_cb), self);
1592
1593   g_signal_connect (self, "motion-notify-event",
1594       G_CALLBACK (empathy_call_window_motion_notify_cb), self);
1595
1596   priv->timer = g_timer_new ();
1597
1598   g_object_ref (priv->ui_manager);
1599   g_object_unref (gui);
1600
1601   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1602   priv->mic_menu = empathy_mic_menu_new (self);
1603   priv->camera_menu = empathy_camera_menu_new (self);
1604
1605   empathy_call_window_show_hangup_button (self, TRUE);
1606
1607   priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
1608
1609   /* Retrieve initial volume */
1610   priv->volume = g_settings_get_double (priv->settings,
1611       EMPATHY_PREFS_CALL_SOUND_VOLUME) / 100.0;
1612
1613   g_signal_connect (priv->settings, "changed::"EMPATHY_PREFS_CALL_SOUND_VOLUME,
1614       G_CALLBACK (empathy_call_window_prefs_volume_changed_cb), self);
1615
1616   empathy_geometry_bind (GTK_WINDOW (self), "call-window");
1617   /* These signals are used to track the window position and save it
1618    * when the window is destroyed. We need to do this as we don't want
1619    * the window geometry to be saved with the dialpad taken into account. */
1620   g_signal_connect (self, "destroy",
1621       G_CALLBACK (empathy_call_window_destroyed_cb), self);
1622   g_signal_connect (self, "configure-event",
1623       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1624   g_signal_connect (self, "window-state-event",
1625       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1626
1627   /* Don't display labels in both toolbars */
1628   gtk_toolbar_set_style (GTK_TOOLBAR (priv->toolbar), GTK_TOOLBAR_ICONS);
1629 }
1630
1631 /* Instead of specifying a width and a height, we specify only one size. That's
1632    because we want a square avatar icon.  */
1633 static void
1634 init_contact_avatar_with_size (EmpathyContact *contact,
1635     GtkWidget *image_widget,
1636     gint size)
1637 {
1638   GdkPixbuf *pixbuf_avatar = NULL;
1639
1640   if (contact != NULL)
1641     {
1642       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
1643         size, size);
1644     }
1645
1646   if (pixbuf_avatar == NULL)
1647     {
1648       pixbuf_avatar = empathy_pixbuf_from_icon_name_sized (
1649           EMPATHY_IMAGE_AVATAR_DEFAULT, size);
1650     }
1651
1652   gtk_image_set_from_pixbuf (GTK_IMAGE (image_widget), pixbuf_avatar);
1653
1654   if (pixbuf_avatar != NULL)
1655     g_object_unref (pixbuf_avatar);
1656 }
1657
1658 static void
1659 set_window_title (EmpathyCallWindow *self)
1660 {
1661   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1662   gchar *tmp;
1663
1664   if (priv->contact != NULL)
1665     {
1666       /* translators: Call is a noun and %s is the contact name. This string
1667        * is used in the window title */
1668       tmp = g_strdup_printf (_("Call with %s"),
1669           empathy_contact_get_alias (priv->contact));
1670       gtk_window_set_title (GTK_WINDOW (self), tmp);
1671       g_free (tmp);
1672     }
1673   else
1674     {
1675       g_warning ("Unknown remote contact!");
1676     }
1677 }
1678
1679 static void
1680 set_remote_user_name (EmpathyCallWindow *self,
1681   EmpathyContact *contact)
1682 {
1683   const gchar *alias = empathy_contact_get_alias (contact);
1684   const gchar *status = empathy_contact_get_status (contact);
1685   gchar *label;
1686
1687   label = g_strdup_printf ("%s\n<small>%s</small>", alias, status);
1688   gtk_label_set_markup (GTK_LABEL (self->priv->remote_user_name_toolbar),
1689       label);
1690   g_free (label);
1691 }
1692
1693 static void
1694 contact_name_changed_cb (EmpathyContact *contact,
1695     GParamSpec *pspec,
1696     EmpathyCallWindow *self)
1697 {
1698   set_window_title (self);
1699   set_remote_user_name (self, contact);
1700 }
1701
1702 static void
1703 contact_presence_changed_cb (EmpathyContact *contact,
1704     GParamSpec *pspec,
1705     EmpathyCallWindow *self)
1706 {
1707   set_remote_user_name (self, contact);
1708 }
1709
1710 static void
1711 contact_avatar_changed_cb (EmpathyContact *contact,
1712     GParamSpec *pspec,
1713     EmpathyCallWindow *self)
1714 {
1715   int size;
1716   GtkAllocation allocation;
1717   GtkWidget *avatar_widget;
1718
1719   avatar_widget = self->priv->remote_user_avatar_widget;
1720
1721   gtk_widget_get_allocation (avatar_widget, &allocation);
1722   size = allocation.height;
1723
1724   if (size == 0)
1725     {
1726       /* the widget is not allocated yet, set a default size */
1727       size = MIN (REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT,
1728           REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH);
1729     }
1730
1731   init_contact_avatar_with_size (contact, avatar_widget, size);
1732
1733   avatar_widget = self->priv->remote_user_avatar_toolbar;
1734
1735   gtk_widget_get_allocation (avatar_widget, &allocation);
1736   size = allocation.height;
1737
1738   if (size == 0)
1739     {
1740       /* the widget is not allocated yet, set a default size */
1741       size = SMALL_TOOLBAR_SIZE;
1742     }
1743
1744   init_contact_avatar_with_size (contact, avatar_widget, size);
1745 }
1746
1747 static void
1748 empathy_call_window_setup_avatars (EmpathyCallWindow *self,
1749     EmpathyCallHandler *handler)
1750 {
1751   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1752
1753   tp_g_signal_connect_object (priv->contact, "notify::name",
1754       G_CALLBACK (contact_name_changed_cb), self, 0);
1755   tp_g_signal_connect_object (priv->contact, "notify::avatar",
1756     G_CALLBACK (contact_avatar_changed_cb), self, 0);
1757   tp_g_signal_connect_object (priv->contact, "notify::presence",
1758       G_CALLBACK (contact_presence_changed_cb), self, 0);
1759
1760   set_window_title (self);
1761   set_remote_user_name (self, priv->contact);
1762
1763   init_contact_avatar_with_size (priv->contact,
1764       priv->remote_user_avatar_widget,
1765       MIN (REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH,
1766           REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT));
1767
1768   init_contact_avatar_with_size (priv->contact,
1769       priv->remote_user_avatar_toolbar,
1770       SMALL_TOOLBAR_SIZE);
1771
1772   /* The remote avatar is shown by default and will be hidden when we receive
1773      video from the remote side. */
1774   clutter_actor_hide (priv->video_output);
1775   gtk_widget_show (priv->remote_user_avatar_widget);
1776 }
1777
1778 static void
1779 update_send_codec (EmpathyCallWindow *self,
1780     gboolean audio)
1781 {
1782   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1783   FsCodec *codec;
1784   GtkWidget *widget;
1785   gchar *tmp;
1786
1787   if (audio)
1788     {
1789       codec = empathy_call_handler_get_send_audio_codec (priv->handler);
1790       widget = priv->acodec_encoding_label;
1791     }
1792   else
1793     {
1794       codec = empathy_call_handler_get_send_video_codec (priv->handler);
1795       widget = priv->vcodec_encoding_label;
1796     }
1797
1798   if (codec == NULL)
1799     return;
1800
1801   tmp = g_strdup_printf ("%s/%u", codec->encoding_name, codec->clock_rate);
1802   gtk_label_set_text (GTK_LABEL (widget), tmp);
1803   g_free (tmp);
1804 }
1805
1806 static void
1807 send_audio_codec_notify_cb (GObject *object,
1808     GParamSpec *pspec,
1809     gpointer user_data)
1810 {
1811   EmpathyCallWindow *self = user_data;
1812
1813   update_send_codec (self, TRUE);
1814 }
1815
1816 static void
1817 send_video_codec_notify_cb (GObject *object,
1818     GParamSpec *pspec,
1819     gpointer user_data)
1820 {
1821   EmpathyCallWindow *self = user_data;
1822
1823   update_send_codec (self, FALSE);
1824 }
1825
1826 static void
1827 update_recv_codec (EmpathyCallWindow *self,
1828     gboolean audio)
1829 {
1830   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1831   GList *codecs, *l;
1832   GtkWidget *widget;
1833   GString *str = NULL;
1834
1835   if (audio)
1836     {
1837       codecs = empathy_call_handler_get_recv_audio_codecs (priv->handler);
1838       widget = priv->acodec_decoding_label;
1839     }
1840   else
1841     {
1842       codecs = empathy_call_handler_get_recv_video_codecs (priv->handler);
1843       widget = priv->vcodec_decoding_label;
1844     }
1845
1846   if (codecs == NULL)
1847     return;
1848
1849   for (l = codecs; l != NULL; l = g_list_next (l))
1850     {
1851       FsCodec *codec = l->data;
1852
1853       if (str == NULL)
1854         str = g_string_new (NULL);
1855       else
1856         g_string_append (str, ", ");
1857
1858       g_string_append_printf (str, "%s/%u", codec->encoding_name,
1859           codec->clock_rate);
1860     }
1861
1862   gtk_label_set_text (GTK_LABEL (widget), str->str);
1863   g_string_free (str, TRUE);
1864 }
1865
1866 static void
1867 recv_audio_codecs_notify_cb (GObject *object,
1868     GParamSpec *pspec,
1869     gpointer user_data)
1870 {
1871   EmpathyCallWindow *self = user_data;
1872
1873   update_recv_codec (self, TRUE);
1874 }
1875
1876 static void
1877 recv_video_codecs_notify_cb (GObject *object,
1878     GParamSpec *pspec,
1879     gpointer user_data)
1880 {
1881   EmpathyCallWindow *self = user_data;
1882
1883   update_recv_codec (self, FALSE);
1884 }
1885
1886 static const gchar *
1887 candidate_type_to_str (FsCandidate *candidate)
1888 {
1889   switch (candidate->type)
1890     {
1891       case FS_CANDIDATE_TYPE_HOST:
1892         return "host";
1893       case FS_CANDIDATE_TYPE_SRFLX:
1894         return "server reflexive";
1895       case FS_CANDIDATE_TYPE_PRFLX:
1896         return "peer reflexive";
1897       case FS_CANDIDATE_TYPE_RELAY:
1898         return "relay";
1899       case FS_CANDIDATE_TYPE_MULTICAST:
1900         return "multicast";
1901     }
1902
1903   return NULL;
1904 }
1905
1906 static const gchar *
1907 candidate_type_to_desc (FsCandidate *candidate)
1908 {
1909   switch (candidate->type)
1910     {
1911       case FS_CANDIDATE_TYPE_HOST:
1912         return _("The IP address as seen by the machine");
1913       case FS_CANDIDATE_TYPE_SRFLX:
1914         return _("The IP address as seen by a server on the Internet");
1915       case FS_CANDIDATE_TYPE_PRFLX:
1916         return _("The IP address of the peer as seen by the other side");
1917       case FS_CANDIDATE_TYPE_RELAY:
1918         return _("The IP address of a relay server");
1919       case FS_CANDIDATE_TYPE_MULTICAST:
1920         return _("The IP address of the multicast group");
1921     }
1922
1923   return NULL;
1924 }
1925
1926 static void
1927 update_candidat_widget (EmpathyCallWindow *self,
1928     GtkWidget *label,
1929     GtkWidget *img,
1930     FsCandidate *candidate)
1931 {
1932   gchar *str;
1933
1934   g_assert (candidate != NULL);
1935   str = g_strdup_printf ("%s %u (%s)", candidate->ip,
1936       candidate->port, candidate_type_to_str (candidate));
1937
1938   gtk_label_set_text (GTK_LABEL (label), str);
1939   gtk_widget_set_tooltip_text (img, candidate_type_to_desc (candidate));
1940
1941   g_free (str);
1942 }
1943
1944 static void
1945 candidates_changed_cb (GObject *object,
1946     FsMediaType type,
1947     EmpathyCallWindow *self)
1948 {
1949   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1950   FsCandidate *candidate = NULL;
1951
1952   if (type == FS_MEDIA_TYPE_VIDEO)
1953     {
1954       /* Update remote candidate */
1955       candidate = empathy_call_handler_get_video_remote_candidate (
1956           priv->handler);
1957
1958       update_candidat_widget (self, priv->video_remote_candidate_label,
1959           priv->video_remote_candidate_info_img, candidate);
1960
1961       /* Update local candidate */
1962       candidate = empathy_call_handler_get_video_local_candidate (
1963           priv->handler);
1964
1965       update_candidat_widget (self, priv->video_local_candidate_label,
1966           priv->video_local_candidate_info_img, candidate);
1967     }
1968   else
1969     {
1970       /* Update remote candidate */
1971       candidate = empathy_call_handler_get_audio_remote_candidate (
1972           priv->handler);
1973
1974       update_candidat_widget (self, priv->audio_remote_candidate_label,
1975           priv->audio_remote_candidate_info_img, candidate);
1976
1977       /* Update local candidate */
1978       candidate = empathy_call_handler_get_audio_local_candidate (
1979           priv->handler);
1980
1981       update_candidat_widget (self, priv->audio_local_candidate_label,
1982           priv->audio_local_candidate_info_img, candidate);
1983     }
1984 }
1985
1986 static void
1987 empathy_call_window_constructed (GObject *object)
1988 {
1989   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1990   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1991   TpyCallChannel *call;
1992
1993   g_assert (priv->handler != NULL);
1994
1995   g_object_get (priv->handler, "call-channel", &call, NULL);
1996   priv->outgoing = (call == NULL);
1997   if (call != NULL)
1998     g_object_unref (call);
1999
2000   g_object_get (priv->handler, "target-contact", &priv->contact, NULL);
2001   g_assert (priv->contact != NULL);
2002
2003   empathy_call_window_setup_avatars (self, priv->handler);
2004   empathy_call_window_set_state_connecting (self);
2005
2006   if (!empathy_call_handler_has_initial_video (priv->handler))
2007     {
2008       gtk_toggle_tool_button_set_active (
2009           GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
2010     }
2011   /* If call has InitialVideo, the preview will be started once the call has
2012    * been started (start_call()). */
2013
2014   update_send_codec (self, TRUE);
2015   update_send_codec (self, FALSE);
2016   update_recv_codec (self, TRUE);
2017   update_recv_codec (self, FALSE);
2018
2019   tp_g_signal_connect_object (priv->handler, "notify::send-audio-codec",
2020       G_CALLBACK (send_audio_codec_notify_cb), self, 0);
2021   tp_g_signal_connect_object (priv->handler, "notify::send-video-codec",
2022       G_CALLBACK (send_video_codec_notify_cb), self, 0);
2023   tp_g_signal_connect_object (priv->handler, "notify::recv-audio-codecs",
2024       G_CALLBACK (recv_audio_codecs_notify_cb), self, 0);
2025   tp_g_signal_connect_object (priv->handler, "notify::recv-video-codecs",
2026       G_CALLBACK (recv_video_codecs_notify_cb), self, 0);
2027
2028   tp_g_signal_connect_object (priv->handler, "candidates-changed",
2029       G_CALLBACK (candidates_changed_cb), self, 0);
2030 }
2031
2032 static void empathy_call_window_dispose (GObject *object);
2033 static void empathy_call_window_finalize (GObject *object);
2034
2035 static void
2036 empathy_call_window_set_property (GObject *object,
2037   guint property_id, const GValue *value, GParamSpec *pspec)
2038 {
2039   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2040
2041   switch (property_id)
2042     {
2043       case PROP_CALL_HANDLER:
2044         priv->handler = g_value_dup_object (value);
2045         break;
2046       default:
2047         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2048     }
2049 }
2050
2051 static void
2052 empathy_call_window_get_property (GObject *object,
2053   guint property_id, GValue *value, GParamSpec *pspec)
2054 {
2055   EmpathyCallWindowPriv *priv = GET_PRIV (object);
2056
2057   switch (property_id)
2058     {
2059       case PROP_CALL_HANDLER:
2060         g_value_set_object (value, priv->handler);
2061         break;
2062       default:
2063         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
2064     }
2065 }
2066
2067 static void
2068 empathy_call_window_class_init (
2069   EmpathyCallWindowClass *empathy_call_window_class)
2070 {
2071   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
2072   GParamSpec *param_spec;
2073
2074   g_type_class_add_private (empathy_call_window_class,
2075     sizeof (EmpathyCallWindowPriv));
2076
2077   object_class->constructed = empathy_call_window_constructed;
2078   object_class->set_property = empathy_call_window_set_property;
2079   object_class->get_property = empathy_call_window_get_property;
2080
2081   object_class->dispose = empathy_call_window_dispose;
2082   object_class->finalize = empathy_call_window_finalize;
2083
2084   param_spec = g_param_spec_object ("handler",
2085     "handler", "The call handler",
2086     EMPATHY_TYPE_CALL_HANDLER,
2087     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
2088   g_object_class_install_property (object_class,
2089     PROP_CALL_HANDLER, param_spec);
2090 }
2091
2092 void
2093 empathy_call_window_dispose (GObject *object)
2094 {
2095   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2096   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2097
2098   if (priv->dispose_has_run)
2099     return;
2100
2101   priv->dispose_has_run = TRUE;
2102
2103   if (priv->handler != NULL)
2104     {
2105       empathy_call_handler_stop_call (priv->handler);
2106       tp_clear_object (&priv->handler);
2107     }
2108
2109   if (priv->bus_message_source_id != 0)
2110     {
2111       g_source_remove (priv->bus_message_source_id);
2112       priv->bus_message_source_id = 0;
2113     }
2114
2115   if (priv->got_video_src > 0)
2116     {
2117       g_source_remove (priv->got_video_src);
2118       priv->got_video_src = 0;
2119     }
2120
2121   if (priv->inactivity_src > 0)
2122     {
2123       g_source_remove (priv->inactivity_src);
2124       priv->inactivity_src = 0;
2125     }
2126
2127   tp_clear_object (&priv->pipeline);
2128   tp_clear_object (&priv->video_input);
2129   tp_clear_object (&priv->audio_input);
2130   tp_clear_object (&priv->video_tee);
2131   tp_clear_object (&priv->ui_manager);
2132   tp_clear_object (&priv->fullscreen);
2133   tp_clear_object (&priv->camera_monitor);
2134   tp_clear_object (&priv->settings);
2135   tp_clear_object (&priv->sound_mgr);
2136   tp_clear_object (&priv->mic_menu);
2137   tp_clear_object (&priv->camera_menu);
2138
2139   g_list_free_full (priv->notifiers, g_object_unref);
2140
2141   if (priv->timer_id != 0)
2142     g_source_remove (priv->timer_id);
2143   priv->timer_id = 0;
2144
2145   if (priv->contact != NULL)
2146     {
2147       g_signal_handlers_disconnect_by_func (priv->contact,
2148           contact_name_changed_cb, self);
2149       priv->contact = NULL;
2150     }
2151
2152   G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
2153 }
2154
2155 static void
2156 disconnect_video_output_motion_handler (EmpathyCallWindow *self)
2157 {
2158   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2159
2160   if (priv->video_output_motion_handler_id != 0)
2161     {
2162       g_signal_handler_disconnect (G_OBJECT (priv->video_container),
2163           priv->video_output_motion_handler_id);
2164       priv->video_output_motion_handler_id = 0;
2165     }
2166 }
2167
2168 void
2169 empathy_call_window_finalize (GObject *object)
2170 {
2171   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
2172   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2173
2174   disconnect_video_output_motion_handler (self);
2175
2176   /* free any data held directly by the object here */
2177   g_mutex_free (priv->lock);
2178
2179   g_timer_destroy (priv->timer);
2180
2181   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
2182 }
2183
2184
2185 EmpathyCallWindow *
2186 empathy_call_window_new (EmpathyCallHandler *handler)
2187 {
2188   return EMPATHY_CALL_WINDOW (
2189     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
2190 }
2191
2192 static void
2193 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
2194   GstElement *conference, gpointer user_data)
2195 {
2196   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2197   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2198   FsElementAddedNotifier *notifier;
2199   GKeyFile *keyfile;
2200
2201   DEBUG ("Conference added");
2202
2203   /* Add notifier to set the various element properties as needed */
2204   notifier = fs_element_added_notifier_new ();
2205   keyfile = fs_utils_get_default_element_properties (conference);
2206
2207   if (keyfile != NULL)
2208     fs_element_added_notifier_set_properties_from_keyfile (notifier, keyfile);
2209
2210   fs_element_added_notifier_add (notifier, GST_BIN (priv->pipeline));
2211
2212   priv->notifiers = g_list_prepend (priv->notifiers, notifier);
2213
2214   gst_bin_add (GST_BIN (priv->pipeline), conference);
2215   gst_element_set_state (conference, GST_STATE_PLAYING);
2216 }
2217
2218 static void
2219 empathy_call_window_conference_removed_cb (EmpathyCallHandler *handler,
2220   GstElement *conference, gpointer user_data)
2221 {
2222   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2223   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2224
2225   gst_bin_remove (GST_BIN (priv->pipeline), conference);
2226   gst_element_set_state (conference, GST_STATE_NULL);
2227 }
2228
2229 static gboolean
2230 empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
2231 {
2232   GstStateChangeReturn state_change_return;
2233   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2234
2235   if (priv->pipeline == NULL)
2236     return TRUE;
2237
2238   if (priv->bus_message_source_id != 0)
2239     {
2240       g_source_remove (priv->bus_message_source_id);
2241       priv->bus_message_source_id = 0;
2242     }
2243
2244   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2245
2246   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
2247         state_change_return == GST_STATE_CHANGE_NO_PREROLL)
2248     {
2249       if (priv->pipeline != NULL)
2250         g_object_unref (priv->pipeline);
2251       priv->pipeline = NULL;
2252
2253       if (priv->audio_output != NULL)
2254         g_object_unref (priv->audio_output);
2255       priv->audio_output = NULL;
2256
2257       if (priv->video_tee != NULL)
2258         g_object_unref (priv->video_tee);
2259       priv->video_tee = NULL;
2260
2261       if (priv->video_preview != NULL)
2262         clutter_actor_destroy (priv->video_preview);
2263       priv->video_preview = NULL;
2264
2265       priv->funnel = NULL;
2266
2267       create_pipeline (self);
2268       /* Call will be started when user will hit the 'redial' button */
2269       priv->start_call_when_playing = FALSE;
2270       gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2271
2272       return TRUE;
2273     }
2274   else
2275     {
2276       g_message ("Error: could not destroy pipeline. Closing call window");
2277       gtk_widget_destroy (GTK_WIDGET (self));
2278
2279       return FALSE;
2280     }
2281 }
2282
2283 static void
2284 reset_details_pane (EmpathyCallWindow *self)
2285 {
2286   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2287
2288   gtk_label_set_text (GTK_LABEL (priv->vcodec_encoding_label), _("Unknown"));
2289   gtk_label_set_text (GTK_LABEL (priv->acodec_encoding_label), _("Unknown"));
2290   gtk_label_set_text (GTK_LABEL (priv->vcodec_decoding_label), _("Unknown"));
2291   gtk_label_set_text (GTK_LABEL (priv->acodec_decoding_label), _("Unknown"));
2292 }
2293
2294 static gboolean
2295 empathy_call_window_disconnected (EmpathyCallWindow *self,
2296     gboolean restart)
2297 {
2298   gboolean could_disconnect = FALSE;
2299   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2300   gboolean could_reset_pipeline;
2301
2302   /* Leave full screen mode if needed */
2303   gtk_window_unfullscreen (GTK_WINDOW (self));
2304
2305   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
2306   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
2307
2308   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
2309
2310   if (priv->call_state == CONNECTING)
2311       empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2312
2313   if (priv->call_state != REDIALING)
2314     priv->call_state = DISCONNECTED;
2315
2316   /* Show the toolbar */
2317   clutter_state_set_state (priv->transitions, "fade-in");
2318
2319   if (could_reset_pipeline)
2320     {
2321       g_mutex_lock (priv->lock);
2322
2323       g_timer_stop (priv->timer);
2324
2325       if (priv->timer_id != 0)
2326         g_source_remove (priv->timer_id);
2327       priv->timer_id = 0;
2328
2329       g_mutex_unlock (priv->lock);
2330
2331       if (!restart)
2332         /* We are about to destroy the window, no need to update it or create
2333          * a video preview */
2334         return TRUE;
2335
2336       empathy_call_window_status_message (self, _("Disconnected"));
2337
2338       empathy_call_window_show_hangup_button (self, FALSE);
2339
2340       /* Unsensitive the camera and mic button */
2341       gtk_widget_set_sensitive (priv->camera_button, FALSE);
2342       gtk_widget_set_sensitive (priv->mic_button, FALSE);
2343
2344       /* Be sure that the mic button is enabled */
2345       gtk_toggle_tool_button_set_active (
2346           GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
2347
2348       if (priv->camera_state == CAMERA_STATE_ON)
2349         {
2350           /* Restart the preview with the new pipeline. */
2351           display_video_preview (self, TRUE);
2352         }
2353
2354       /* destroy the video output; it will be recreated when we'll redial */
2355       disconnect_video_output_motion_handler (self);
2356       if (priv->video_output != NULL)
2357         clutter_actor_destroy (priv->video_output);
2358       priv->video_output = NULL;
2359       if (priv->got_video_src > 0)
2360         {
2361           g_source_remove (priv->got_video_src);
2362           priv->got_video_src = 0;
2363         }
2364
2365       gtk_widget_show (priv->remote_user_avatar_widget);
2366
2367       reset_details_pane (self);
2368
2369       priv->sending_video = FALSE;
2370       priv->call_started = FALSE;
2371
2372       could_disconnect = TRUE;
2373
2374       /* TODO: display the self avatar of the preview (depends if the "Always
2375        * Show Video Preview" is enabled or not) */
2376     }
2377
2378   return could_disconnect;
2379 }
2380
2381
2382 static void
2383 empathy_call_window_channel_closed_cb (EmpathyCallHandler *handler,
2384     gpointer user_data)
2385 {
2386   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2387   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2388
2389   if (empathy_call_window_disconnected (self, TRUE) &&
2390       priv->call_state == REDIALING)
2391       empathy_call_window_restart_call (self);
2392 }
2393
2394 static gboolean
2395 empathy_call_window_sink_removed_cb (EmpathyCallHandler *handler,
2396     GstPad *sink,
2397     FsMediaType media_type,
2398     EmpathyCallWindow *self)
2399 {
2400   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2401
2402   DEBUG ("removing content");
2403
2404   /*
2405    * This assumes that there is only one video stream per channel...
2406    */
2407
2408   if ((guint) media_type == FS_MEDIA_TYPE_VIDEO)
2409     {
2410       if (priv->funnel != NULL)
2411         {
2412           GstElement *output;
2413
2414           output = priv->video_output_sink;
2415
2416           gst_element_set_state (output, GST_STATE_NULL);
2417           gst_element_set_state (priv->funnel, GST_STATE_NULL);
2418
2419           gst_bin_remove (GST_BIN (priv->pipeline), output);
2420           gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2421           priv->funnel = NULL;
2422           return TRUE;
2423         }
2424     }
2425   else if (media_type == FS_MEDIA_TYPE_AUDIO)
2426     {
2427       if (priv->audio_output != NULL)
2428         {
2429           gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2430
2431           gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2432           priv->audio_output = NULL;
2433           return TRUE;
2434         }
2435     }
2436
2437   return FALSE;
2438 }
2439
2440 /* Called with global lock held */
2441 static GstPad *
2442 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
2443 {
2444   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2445   GstPad *pad;
2446   GstElement *output;
2447
2448   if (priv->funnel == NULL)
2449     {
2450       output = priv->video_output_sink;
2451
2452       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
2453
2454       if (!priv->funnel)
2455         {
2456           g_warning ("Could not create fsfunnel");
2457           return NULL;
2458         }
2459
2460       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->funnel))
2461         {
2462           gst_object_unref (priv->funnel);
2463           priv->funnel = NULL;
2464           g_warning ("Could  not add funnel to pipeline");
2465           return NULL;
2466         }
2467
2468       if (!gst_bin_add (GST_BIN (priv->pipeline), output))
2469         {
2470           g_warning ("Could not add the video output widget to the pipeline");
2471           goto error;
2472         }
2473
2474       if (!gst_element_link (priv->funnel, output))
2475         {
2476           g_warning ("Could not link output sink to funnel");
2477           goto error_output_added;
2478         }
2479
2480       if (gst_element_set_state (output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2481         {
2482           g_warning ("Could not start video sink");
2483           goto error_output_added;
2484         }
2485
2486       if (gst_element_set_state (priv->funnel, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2487         {
2488           g_warning ("Could not start funnel");
2489           goto error_output_added;
2490         }
2491     }
2492
2493   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
2494
2495   if (!pad)
2496     g_warning ("Could not get request pad from funnel");
2497
2498   return pad;
2499
2500
2501  error_output_added:
2502
2503   gst_element_set_locked_state (priv->funnel, TRUE);
2504   gst_element_set_locked_state (output, TRUE);
2505
2506   gst_element_set_state (priv->funnel, GST_STATE_NULL);
2507   gst_element_set_state (output, GST_STATE_NULL);
2508
2509   gst_bin_remove (GST_BIN (priv->pipeline), output);
2510   gst_element_set_locked_state (output, FALSE);
2511
2512  error:
2513
2514   gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
2515   priv->funnel = NULL;
2516
2517   return NULL;
2518 }
2519
2520 /* Called with global lock held */
2521 static GstPad *
2522 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self)
2523 {
2524   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2525   GstPad *pad;
2526   GstPadTemplate *template;
2527
2528   if (priv->audio_output == NULL)
2529     {
2530       priv->audio_output = empathy_audio_sink_new ();
2531       g_object_ref_sink (priv->audio_output);
2532
2533       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output))
2534         {
2535           g_warning ("Could not add audio sink to pipeline");
2536           g_object_unref (priv->audio_output);
2537           goto error_add_output;
2538         }
2539
2540       if (gst_element_set_state (priv->audio_output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2541         {
2542           g_warning ("Could not start audio sink");
2543           goto error;
2544         }
2545     }
2546
2547   template = gst_element_class_get_pad_template (
2548     GST_ELEMENT_GET_CLASS (priv->audio_output), "sink%d");
2549
2550   pad = gst_element_request_pad (priv->audio_output,
2551     template, NULL, NULL);
2552
2553   if (pad == NULL)
2554     {
2555       g_warning ("Could not get sink pad from sink");
2556       return NULL;
2557     }
2558
2559   return pad;
2560
2561 error:
2562   gst_element_set_locked_state (priv->audio_output, TRUE);
2563   gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2564   gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2565   priv->audio_output = NULL;
2566
2567 error_add_output:
2568
2569   return NULL;
2570 }
2571
2572 static gboolean
2573 empathy_call_window_update_timer (gpointer user_data)
2574 {
2575   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2576   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2577   const gchar *status;
2578   gchar *str;
2579   gdouble time_;
2580
2581   time_ = g_timer_elapsed (priv->timer, NULL);
2582
2583   if (priv->call_state == HELD)
2584     status = _("On hold");
2585   else if (!gtk_toggle_tool_button_get_active (
2586       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
2587     status = _("Mute");
2588   else
2589     status = _("Duration");
2590
2591   /* Translators: 'status - minutes:seconds' the caller has been connected */
2592   str = g_strdup_printf (_("%s â€” %d:%02dm"),
2593       status,
2594       (int) time_ / 60, (int) time_ % 60);
2595   empathy_call_window_status_message (self, str);
2596   g_free (str);
2597
2598   return TRUE;
2599 }
2600
2601 #if 0
2602 static void
2603 display_error (EmpathyCallWindow *self,
2604     TpyCallChannel *call,
2605     const gchar *img,
2606     const gchar *title,
2607     const gchar *desc,
2608     const gchar *details)
2609 {
2610   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2611   GtkWidget *info_bar;
2612   GtkWidget *content_area;
2613   GtkWidget *hbox;
2614   GtkWidget *vbox;
2615   GtkWidget *image;
2616   GtkWidget *label;
2617   gchar *txt;
2618
2619   /* Create info bar */
2620   info_bar = gtk_info_bar_new_with_buttons (GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2621       NULL);
2622
2623   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
2624
2625   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2626
2627   /* hbox containing the image and the messages vbox */
2628   hbox = gtk_hbox_new (FALSE, 3);
2629   gtk_container_add (GTK_CONTAINER (content_area), hbox);
2630
2631   /* Add image */
2632   image = gtk_image_new_from_icon_name (img, GTK_ICON_SIZE_DIALOG);
2633   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2634
2635   /* vbox containing the main message and the details expander */
2636   vbox = gtk_vbox_new (FALSE, 3);
2637   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
2638
2639   /* Add text */
2640   txt = g_strdup_printf ("<b>%s</b>\n%s", title, desc);
2641
2642   label = gtk_label_new (NULL);
2643   gtk_label_set_markup (GTK_LABEL (label), txt);
2644   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2645   gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2646   g_free (txt);
2647
2648   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
2649
2650   /* Add details */
2651   if (details != NULL)
2652     {
2653       GtkWidget *expander;
2654
2655       expander = gtk_expander_new (_("Technical Details"));
2656
2657       txt = g_strdup_printf ("<i>%s</i>", details);
2658
2659       label = gtk_label_new (NULL);
2660       gtk_label_set_markup (GTK_LABEL (label), txt);
2661       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2662       gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2663       g_free (txt);
2664
2665       gtk_container_add (GTK_CONTAINER (expander), label);
2666       gtk_box_pack_start (GTK_BOX (vbox), expander, TRUE, TRUE, 0);
2667     }
2668
2669   g_signal_connect (info_bar, "response",
2670       G_CALLBACK (gtk_widget_destroy), NULL);
2671
2672   gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar,
2673       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
2674   gtk_widget_show_all (info_bar);
2675 }
2676
2677 static gchar *
2678 media_stream_error_to_txt (EmpathyCallWindow *self,
2679     TpyCallChannel *call,
2680     gboolean audio,
2681     TpMediaStreamError error)
2682 {
2683   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2684   const gchar *cm = NULL;
2685   gchar *url;
2686   gchar *result;
2687
2688   switch (error)
2689     {
2690       case TP_MEDIA_STREAM_ERROR_CODEC_NEGOTIATION_FAILED:
2691         if (audio)
2692           return g_strdup_printf (
2693               _("%s's software does not understand any of the audio formats "
2694                 "supported by your computer"),
2695             empathy_contact_get_alias (priv->contact));
2696         else
2697           return g_strdup_printf (
2698               _("%s's software does not understand any of the video formats "
2699                 "supported by your computer"),
2700             empathy_contact_get_alias (priv->contact));
2701
2702       case TP_MEDIA_STREAM_ERROR_CONNECTION_FAILED:
2703         return g_strdup_printf (
2704             _("Can't establish a connection to %s. "
2705               "One of you might be on a network that does not allow "
2706               "direct connections."),
2707           empathy_contact_get_alias (priv->contact));
2708
2709       case TP_MEDIA_STREAM_ERROR_NETWORK_ERROR:
2710           return g_strdup (_("There was a failure on the network"));
2711
2712       case TP_MEDIA_STREAM_ERROR_NO_CODECS:
2713         if (audio)
2714           return g_strdup (_("The audio formats necessary for this call "
2715                 "are not installed on your computer"));
2716         else
2717           return g_strdup (_("The video formats necessary for this call "
2718                 "are not installed on your computer"));
2719
2720       case TP_MEDIA_STREAM_ERROR_INVALID_CM_BEHAVIOR:
2721         tp_connection_parse_object_path (
2722             tp_channel_borrow_connection (TP_CHANNEL (call)),
2723             NULL, &cm);
2724
2725         url = g_strdup_printf ("http://bugs.freedesktop.org/enter_bug.cgi?"
2726             "product=Telepathy&amp;component=%s", cm);
2727
2728         result = g_strdup_printf (
2729             _("Something unexpected happened in a Telepathy component. "
2730               "Please <a href=\"%s\">report this bug</a> and attach "
2731               "logs gathered from the 'Debug' window in the Help menu."), url);
2732
2733         g_free (url);
2734         g_free (cm);
2735         return result;
2736
2737       case TP_MEDIA_STREAM_ERROR_MEDIA_ERROR:
2738         return g_strdup (_("There was a failure in the call engine"));
2739
2740       case TP_MEDIA_STREAM_ERROR_EOS:
2741         return g_strdup (_("The end of the stream was reached"));
2742
2743       case TP_MEDIA_STREAM_ERROR_UNKNOWN:
2744       default:
2745         return NULL;
2746     }
2747 }
2748
2749 static void
2750 empathy_call_window_stream_error (EmpathyCallWindow *self,
2751     TpyCallChannel *call,
2752     gboolean audio,
2753     guint code,
2754     const gchar *msg,
2755     const gchar *icon,
2756     const gchar *title)
2757 {
2758   gchar *desc;
2759
2760   desc = media_stream_error_to_txt (self, call, audio, code);
2761   if (desc == NULL)
2762     {
2763       /* No description, use the error message. That's not great as it's not
2764        * localized but it's better than nothing. */
2765       display_error (self, call, icon, title, msg, NULL);
2766     }
2767   else
2768     {
2769       display_error (self, call, icon, title, desc, msg);
2770       g_free (desc);
2771     }
2772 }
2773
2774 static void
2775 empathy_call_window_audio_stream_error (TpyCallChannel *call,
2776     guint code,
2777     const gchar *msg,
2778     EmpathyCallWindow *self)
2779 {
2780   empathy_call_window_stream_error (self, call, TRUE, code, msg,
2781       "gnome-stock-mic", _("Can't establish audio stream"));
2782 }
2783
2784 static void
2785 empathy_call_window_video_stream_error (TpyCallChannel *call,
2786     guint code,
2787     const gchar *msg,
2788     EmpathyCallWindow *self)
2789 {
2790   empathy_call_window_stream_error (self, call, FALSE, code, msg,
2791       "camera-web", _("Can't establish video stream"));
2792 }
2793 #endif
2794
2795 static void
2796 empathy_call_window_state_changed_cb (EmpathyCallHandler *handler,
2797     TpyCallState state,
2798     EmpathyCallWindow *self)
2799 {
2800   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2801   TpyCallChannel *call;
2802   gboolean can_send_video;
2803
2804   if (state != TPY_CALL_STATE_ACCEPTED)
2805     return;
2806
2807   if (priv->call_state == CONNECTED)
2808     return;
2809
2810   g_timer_start (priv->timer);
2811   priv->call_state = CONNECTED;
2812
2813   empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2814
2815   can_send_video = priv->video_input != NULL &&
2816     empathy_contact_can_voip_video (priv->contact) &&
2817     empathy_camera_monitor_get_available (priv->camera_monitor);
2818
2819   g_object_get (priv->handler, "call-channel", &call, NULL);
2820
2821   if (tpy_call_channel_has_dtmf (call))
2822     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
2823
2824   if (priv->video_input == NULL)
2825     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
2826
2827   gtk_widget_set_sensitive (priv->camera_button, can_send_video);
2828
2829   empathy_call_window_show_hangup_button (self, TRUE);
2830
2831   gtk_widget_set_sensitive (priv->mic_button, TRUE);
2832
2833   clutter_actor_hide (priv->video_output);
2834   gtk_widget_show (priv->remote_user_avatar_widget);
2835
2836   g_object_unref (call);
2837
2838   g_mutex_lock (priv->lock);
2839
2840   priv->timer_id = g_timeout_add_seconds (1,
2841     empathy_call_window_update_timer, self);
2842
2843   g_mutex_unlock (priv->lock);
2844
2845   empathy_call_window_update_timer (self);
2846
2847   gtk_action_set_sensitive (priv->menu_fullscreen, TRUE);
2848 }
2849
2850 static gboolean
2851 empathy_call_window_show_video_output_cb (gpointer user_data)
2852 {
2853   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2854
2855   if (self->priv->video_output != NULL)
2856     {
2857       gtk_widget_hide (self->priv->remote_user_avatar_widget);
2858       clutter_actor_show (self->priv->video_output);
2859       empathy_call_window_raise_actors (self);
2860     }
2861
2862   return FALSE;
2863 }
2864
2865 static gboolean
2866 empathy_call_window_check_video_cb (gpointer data)
2867 {
2868   EmpathyCallWindow *self = data;
2869
2870   if (self->priv->got_video)
2871     {
2872       self->priv->got_video = FALSE;
2873       return TRUE;
2874     }
2875
2876   /* No video in the last N seconds, display the remote avatar */
2877   empathy_call_window_show_video_output (self, FALSE);
2878
2879   return TRUE;
2880 }
2881
2882 /* Called from the streaming thread */
2883 static gboolean
2884 empathy_call_window_video_probe_cb (GstPad *pad,
2885     GstMiniObject *mini_obj,
2886     EmpathyCallWindow *self)
2887 {
2888   /* Ignore events */
2889   if (GST_IS_EVENT (mini_obj))
2890     return TRUE;
2891
2892   if (G_UNLIKELY (!self->priv->got_video))
2893     {
2894       /* show the remote video */
2895       g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
2896           empathy_call_window_show_video_output_cb,
2897           g_object_ref (self), g_object_unref);
2898
2899       self->priv->got_video = TRUE;
2900     }
2901
2902   return TRUE;
2903 }
2904
2905 /* Called from the streaming thread */
2906 static gboolean
2907 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
2908   GstPad *src, guint media_type, gpointer user_data)
2909 {
2910   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2911   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2912   gboolean retval = FALSE;
2913
2914   GstPad *pad;
2915
2916   g_mutex_lock (priv->lock);
2917
2918   switch (media_type)
2919     {
2920       case TP_MEDIA_STREAM_TYPE_AUDIO:
2921         pad = empathy_call_window_get_audio_sink_pad (self);
2922         break;
2923       case TP_MEDIA_STREAM_TYPE_VIDEO:
2924         g_idle_add (empathy_call_window_show_video_output_cb, self);
2925         pad = empathy_call_window_get_video_sink_pad (self);
2926
2927         gst_pad_add_data_probe (src,
2928             G_CALLBACK (empathy_call_window_video_probe_cb), self);
2929         if (priv->got_video_src > 0)
2930           g_source_remove (priv->got_video_src);
2931         priv->got_video_src = g_timeout_add_seconds (5,
2932             empathy_call_window_check_video_cb, self);
2933         break;
2934       default:
2935         g_assert_not_reached ();
2936     }
2937
2938   if (pad == NULL)
2939     goto out;
2940
2941   if (GST_PAD_LINK_FAILED (gst_pad_link (src, pad)))
2942       g_warning ("Could not link %s sink pad",
2943           media_type == TP_MEDIA_STREAM_TYPE_AUDIO ? "audio" : "video");
2944   else
2945       retval = TRUE;
2946
2947   gst_object_unref (pad);
2948
2949  out:
2950
2951   /* If no sink could be linked, try to add fakesink to prevent the whole call
2952    * aborting */
2953
2954   if (!retval)
2955     {
2956       GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
2957
2958       if (gst_bin_add (GST_BIN (priv->pipeline), fakesink))
2959         {
2960           GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
2961           if (gst_element_set_state (fakesink, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE ||
2962               GST_PAD_LINK_FAILED (gst_pad_link (src, sinkpad)))
2963             {
2964               gst_element_set_locked_state (fakesink, TRUE);
2965               gst_element_set_state (fakesink, GST_STATE_NULL);
2966               gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2967             }
2968           else
2969             {
2970               DEBUG ("Could not link real sink, linked fakesink instead");
2971             }
2972           gst_object_unref (sinkpad);
2973         }
2974       else
2975         {
2976           gst_object_unref (fakesink);
2977         }
2978     }
2979
2980
2981   g_mutex_unlock (priv->lock);
2982
2983   return TRUE;
2984 }
2985
2986 static gboolean
2987 empathy_call_window_sink_added_cb (EmpathyCallHandler *handler,
2988   GstPad *sink, FsMediaType media_type, gpointer user_data)
2989 {
2990   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2991   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2992   GstPad *pad;
2993   gboolean retval = FALSE;
2994
2995   switch (media_type)
2996     {
2997       case FS_MEDIA_TYPE_AUDIO:
2998         if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input))
2999           {
3000             g_warning ("Could not add audio source to pipeline");
3001             break;
3002           }
3003
3004         pad = gst_element_get_static_pad (priv->audio_input, "src");
3005         if (!pad)
3006           {
3007             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3008             g_warning ("Could not get source pad from audio source");
3009             break;
3010           }
3011
3012         if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3013           {
3014             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3015             g_warning ("Could not link audio source to farsight");
3016             break;
3017           }
3018
3019         if (gst_element_set_state (priv->audio_input, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
3020           {
3021             g_warning ("Could not start audio source");
3022             gst_element_set_state (priv->audio_input, GST_STATE_NULL);
3023             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
3024             break;
3025           }
3026
3027         retval = TRUE;
3028         break;
3029       case FS_MEDIA_TYPE_VIDEO:
3030         if (priv->video_tee != NULL)
3031           {
3032             pad = gst_element_get_request_pad (priv->video_tee, "src%d");
3033             if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
3034               {
3035                 g_warning ("Could not link video source input pipeline");
3036                 break;
3037               }
3038             gst_object_unref (pad);
3039           }
3040
3041         retval = TRUE;
3042         break;
3043       default:
3044         g_assert_not_reached ();
3045     }
3046
3047   return retval;
3048 }
3049
3050 static void
3051 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
3052 {
3053   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3054   GstElement *preview;
3055
3056   disable_camera (self);
3057
3058   DEBUG ("remove video input");
3059   preview = priv->video_preview_sink;
3060
3061   gst_element_set_state (priv->video_input, GST_STATE_NULL);
3062   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
3063   gst_element_set_state (preview, GST_STATE_NULL);
3064
3065   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
3066     preview, NULL);
3067
3068   g_object_unref (priv->video_input);
3069   priv->video_input = NULL;
3070   g_object_unref (priv->video_tee);
3071   priv->video_tee = NULL;
3072   clutter_actor_destroy (priv->video_preview);
3073   priv->video_preview = NULL;
3074
3075   gtk_widget_set_sensitive (priv->camera_button, FALSE);
3076 }
3077
3078 static void
3079 start_call (EmpathyCallWindow *self)
3080 {
3081   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3082
3083   priv->call_started = TRUE;
3084   empathy_call_handler_start_call (priv->handler,
3085       gtk_get_current_event_time ());
3086
3087   if (empathy_call_handler_has_initial_video (priv->handler))
3088     {
3089       TpyCallChannel *call;
3090       TpySendingState s;
3091
3092       g_object_get (priv->handler, "call-channel", &call, NULL);
3093       s = tpy_call_channel_get_video_state (call);
3094
3095       if (s == TPY_SENDING_STATE_PENDING_SEND ||
3096           s == TPY_SENDING_STATE_SENDING)
3097         {
3098           /* Enable 'send video' buttons and display the preview */
3099           gtk_toggle_tool_button_set_active (
3100             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), TRUE);
3101         }
3102       else
3103         {
3104           gtk_toggle_tool_button_set_active (
3105             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
3106
3107           if (priv->video_preview == NULL)
3108             {
3109               create_video_preview (self);
3110               add_video_preview_to_pipeline (self);
3111             }
3112         }
3113
3114       g_object_unref (call);
3115     }
3116 }
3117
3118 static gboolean
3119 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
3120   gpointer user_data)
3121 {
3122   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
3123   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3124   GstState newstate;
3125
3126   empathy_call_handler_bus_message (priv->handler, bus, message);
3127
3128   switch (GST_MESSAGE_TYPE (message))
3129     {
3130       case GST_MESSAGE_STATE_CHANGED:
3131         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
3132           {
3133             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3134           }
3135         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
3136             !priv->call_started)
3137           {
3138             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
3139             if (newstate == GST_STATE_PAUSED)
3140               {
3141                 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3142                 priv->pipeline_playing = TRUE;
3143
3144                 if (priv->start_call_when_playing)
3145                   start_call (self);
3146               }
3147           }
3148         break;
3149       case GST_MESSAGE_ERROR:
3150         {
3151           GError *error = NULL;
3152           GstElement *gst_error;
3153           gchar *debug;
3154
3155           gst_message_parse_error (message, &error, &debug);
3156           gst_error = GST_ELEMENT (GST_MESSAGE_SRC (message));
3157
3158           g_message ("Element error: %s -- %s\n", error->message, debug);
3159
3160           if (g_str_has_prefix (gst_element_get_name (gst_error),
3161                 VIDEO_INPUT_ERROR_PREFIX))
3162             {
3163               /* Remove the video input and continue */
3164               if (priv->video_input != NULL)
3165                 empathy_call_window_remove_video_input (self);
3166               gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
3167             }
3168           else
3169             {
3170               empathy_call_window_disconnected (self, TRUE);
3171             }
3172           g_error_free (error);
3173           g_free (debug);
3174         }
3175       case GST_MESSAGE_UNKNOWN:
3176       case GST_MESSAGE_EOS:
3177       case GST_MESSAGE_WARNING:
3178       case GST_MESSAGE_INFO:
3179       case GST_MESSAGE_TAG:
3180       case GST_MESSAGE_BUFFERING:
3181       case GST_MESSAGE_STATE_DIRTY:
3182       case GST_MESSAGE_STEP_DONE:
3183       case GST_MESSAGE_CLOCK_PROVIDE:
3184       case GST_MESSAGE_CLOCK_LOST:
3185       case GST_MESSAGE_NEW_CLOCK:
3186       case GST_MESSAGE_STRUCTURE_CHANGE:
3187       case GST_MESSAGE_STREAM_STATUS:
3188       case GST_MESSAGE_APPLICATION:
3189       case GST_MESSAGE_ELEMENT:
3190       case GST_MESSAGE_SEGMENT_START:
3191       case GST_MESSAGE_SEGMENT_DONE:
3192       case GST_MESSAGE_DURATION:
3193       case GST_MESSAGE_ANY:
3194       default:
3195         break;
3196     }
3197
3198   return TRUE;
3199 }
3200
3201 static void
3202 empathy_call_window_members_changed_cb (TpyCallChannel *call,
3203     GHashTable *members,
3204     EmpathyCallWindow *self)
3205 {
3206   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3207   GHashTableIter iter;
3208   gpointer key, value;
3209   gboolean held = FALSE;
3210
3211   g_hash_table_iter_init (&iter, members);
3212   while (g_hash_table_iter_next (&iter, &key, &value))
3213     {
3214       if (GPOINTER_TO_INT (value) & TPY_CALL_MEMBER_FLAG_HELD)
3215         {
3216           /* This assumes this is a 1-1 call, otherwise one participant
3217            * putting the call on hold wouldn't mean the call is on hold
3218            * for everyone. */
3219           held = TRUE;
3220           break;
3221         }
3222     }
3223
3224   if (held)
3225     priv->call_state = HELD;
3226   else if (priv->call_state == HELD)
3227     priv->call_state = CONNECTED;
3228 }
3229
3230 static void
3231 call_handler_notify_call_cb (EmpathyCallHandler *handler,
3232     GParamSpec *spec,
3233     EmpathyCallWindow *self)
3234 {
3235   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3236   TpyCallChannel *call;
3237
3238   g_object_get (priv->handler, "call-channel", &call, NULL);
3239   if (call == NULL)
3240     return;
3241
3242 /* FIXME
3243   tp_g_signal_connect_object (call, "audio-stream-error",
3244       G_CALLBACK (empathy_call_window_audio_stream_error), self, 0);
3245   tp_g_signal_connect_object (call, "video-stream-error",
3246       G_CALLBACK (empathy_call_window_video_stream_error), self, 0);
3247 */
3248
3249   tp_g_signal_connect_object (call, "members-changed",
3250       G_CALLBACK (empathy_call_window_members_changed_cb), self, 0);
3251
3252   g_object_unref (call);
3253 }
3254
3255 static void
3256 empathy_call_window_realized_cb (GtkWidget *widget, EmpathyCallWindow *window)
3257 {
3258   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3259   TpyCallChannel *call;
3260   gint width;
3261
3262   /* Make the hangup button twice as wide */
3263   width = gtk_widget_get_allocated_width (priv->hangup_button);
3264   gtk_widget_set_size_request (priv->hangup_button, width * 2, -1);
3265
3266   g_signal_connect (priv->handler, "state-changed",
3267     G_CALLBACK (empathy_call_window_state_changed_cb), window);
3268   g_signal_connect (priv->handler, "conference-added",
3269     G_CALLBACK (empathy_call_window_conference_added_cb), window);
3270   g_signal_connect (priv->handler, "conference-removed",
3271     G_CALLBACK (empathy_call_window_conference_removed_cb), window);
3272   g_signal_connect (priv->handler, "closed",
3273     G_CALLBACK (empathy_call_window_channel_closed_cb), window);
3274   g_signal_connect (priv->handler, "src-pad-added",
3275     G_CALLBACK (empathy_call_window_src_added_cb), window);
3276   g_signal_connect (priv->handler, "sink-pad-added",
3277     G_CALLBACK (empathy_call_window_sink_added_cb), window);
3278   g_signal_connect (priv->handler, "sink-pad-removed",
3279     G_CALLBACK (empathy_call_window_sink_removed_cb), window);
3280
3281   g_object_get (priv->handler, "call-channel", &call, NULL);
3282   if (call != NULL)
3283     {
3284       call_handler_notify_call_cb (priv->handler, NULL, window);
3285       g_object_unref (call);
3286     }
3287   else
3288     {
3289       /* call-channel doesn't exist yet, we'll connect signals once it has been
3290        * set */
3291       g_signal_connect (priv->handler, "notify::call-channel",
3292         G_CALLBACK (call_handler_notify_call_cb), window);
3293     }
3294
3295   gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
3296 }
3297
3298 static gboolean
3299 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
3300   EmpathyCallWindow *window)
3301 {
3302   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3303
3304   if (priv->pipeline != NULL)
3305     {
3306       if (priv->bus_message_source_id != 0)
3307         {
3308           g_source_remove (priv->bus_message_source_id);
3309           priv->bus_message_source_id = 0;
3310         }
3311
3312       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
3313     }
3314
3315   if (priv->call_state == CONNECTING)
3316     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
3317
3318   return FALSE;
3319 }
3320
3321 static void
3322 show_controls (EmpathyCallWindow *window, gboolean set_fullscreen)
3323 {
3324   GtkWidget *menu;
3325   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3326
3327   menu = gtk_ui_manager_get_widget (priv->ui_manager,
3328             "/menubar1");
3329
3330   if (set_fullscreen)
3331     {
3332       gtk_widget_hide (priv->dtmf_panel);
3333       gtk_widget_hide (menu);
3334       gtk_widget_hide (priv->toolbar);
3335     }
3336   else
3337     {
3338       if (priv->dialpad_was_visible_before_fs)
3339         gtk_widget_show (priv->dtmf_panel);
3340
3341       gtk_widget_show (menu);
3342       gtk_widget_show (priv->toolbar);
3343
3344       gtk_window_resize (GTK_WINDOW (window), priv->original_width_before_fs,
3345           priv->original_height_before_fs);
3346     }
3347 }
3348
3349 static void
3350 show_borders (EmpathyCallWindow *window, gboolean set_fullscreen)
3351 {
3352   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3353
3354   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
3355       set_fullscreen ? 0 : CONTENT_HBOX_BORDER_WIDTH);
3356   gtk_box_set_spacing (GTK_BOX (priv->content_hbox),
3357       set_fullscreen ? 0 : CONTENT_HBOX_SPACING);
3358
3359   if (priv->video_output != NULL)
3360     {
3361 #if 0
3362       gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
3363           priv->video_output, TRUE, TRUE,
3364           set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
3365           GTK_PACK_START);
3366 #endif
3367     }
3368 }
3369
3370 static gboolean
3371 empathy_call_window_state_event_cb (GtkWidget *widget,
3372   GdkEventWindowState *event, EmpathyCallWindow *window)
3373 {
3374   if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
3375     {
3376       EmpathyCallWindowPriv *priv = GET_PRIV (window);
3377       gboolean set_fullscreen = event->new_window_state &
3378         GDK_WINDOW_STATE_FULLSCREEN;
3379
3380       if (set_fullscreen)
3381         {
3382           gboolean dialpad_was_visible;
3383           GtkAllocation allocation;
3384           gint original_width, original_height;
3385
3386           gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3387           original_width = allocation.width;
3388           original_height = allocation.height;
3389
3390           g_object_get (priv->dtmf_panel,
3391               "visible", &dialpad_was_visible,
3392               NULL);
3393
3394           priv->dialpad_was_visible_before_fs = dialpad_was_visible;
3395           priv->original_width_before_fs = original_width;
3396           priv->original_height_before_fs = original_height;
3397
3398           if (priv->video_output_motion_handler_id == 0 &&
3399                 priv->video_output != NULL)
3400             {
3401               priv->video_output_motion_handler_id = g_signal_connect (
3402                   G_OBJECT (priv->video_container), "motion-notify-event",
3403                   G_CALLBACK (empathy_call_window_video_output_motion_notify),
3404                   window);
3405             }
3406         }
3407       else
3408         {
3409           disconnect_video_output_motion_handler (window);
3410         }
3411
3412       empathy_call_window_fullscreen_set_fullscreen (priv->fullscreen,
3413           set_fullscreen);
3414       show_controls (window, set_fullscreen);
3415       show_borders (window, set_fullscreen);
3416       gtk_action_set_stock_id (priv->menu_fullscreen,
3417           (set_fullscreen ? "gtk-leave-fullscreen" : "gtk-fullscreen"));
3418       priv->is_fullscreen = set_fullscreen;
3419   }
3420
3421   return FALSE;
3422 }
3423
3424 static void
3425 empathy_call_window_show_dialpad (EmpathyCallWindow *window,
3426     gboolean active)
3427 {
3428   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3429   int w, h, dialpad_width;
3430   GtkAllocation allocation;
3431
3432   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
3433   w = allocation.width;
3434   h = allocation.height;
3435
3436   gtk_widget_get_preferred_width (priv->dtmf_panel, &dialpad_width, NULL);
3437
3438   if (active)
3439     {
3440       gtk_widget_show (priv->dtmf_panel);
3441       w += dialpad_width;
3442     }
3443   else
3444     {
3445       w -= dialpad_width;
3446       gtk_widget_hide (priv->dtmf_panel);
3447     }
3448
3449   if (w > 0 && h > 0)
3450     gtk_window_resize (GTK_WINDOW (window), w, h);
3451 }
3452
3453 static void
3454 empathy_call_window_set_send_video (EmpathyCallWindow *window,
3455   CameraState state)
3456 {
3457   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3458   TpyCallChannel *call;
3459
3460   priv->sending_video = (state == CAMERA_STATE_ON);
3461
3462   if (state == CAMERA_STATE_ON)
3463     {
3464       /* When we start sending video, we want to show the video preview by
3465          default. */
3466       display_video_preview (window, TRUE);
3467     }
3468   else
3469     {
3470       display_video_preview (window, FALSE);
3471     }
3472
3473   if (priv->call_state != CONNECTED)
3474     return;
3475
3476   g_object_get (priv->handler, "call-channel", &call, NULL);
3477   DEBUG ("%s sending video", priv->sending_video ? "start": "stop");
3478   tpy_call_channel_send_video (call, priv->sending_video);
3479   g_object_unref (call);
3480 }
3481
3482 static void
3483 empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
3484   EmpathyCallWindow *self)
3485 {
3486   EmpathyCallWindowPriv *priv = GET_PRIV (self);
3487   gboolean active;
3488
3489   active = (gtk_toggle_tool_button_get_active (toggle));
3490
3491   /* We don't want the settings callback to react to this change to avoid
3492    * a loop. */
3493   g_signal_handlers_block_by_func (priv->settings,
3494       empathy_call_window_prefs_volume_changed_cb, self);
3495
3496   if (active)
3497     {
3498       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
3499           priv->volume * 100);
3500     }
3501   else
3502     {
3503       /* TODO, Instead of setting the input volume to 0 we should probably
3504        * stop sending but this would cause the audio call to drop if both
3505        * sides mute at the same time on certain CMs AFAIK. Need to revisit this
3506        * in the future. GNOME #574574
3507        */
3508       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
3509           0);
3510     }
3511
3512     g_signal_handlers_unblock_by_func (priv->settings,
3513       empathy_call_window_prefs_volume_changed_cb, self);
3514 }
3515
3516 static void
3517 empathy_call_window_hangup_cb (gpointer object,
3518     EmpathyCallWindow *self)
3519 {
3520   empathy_call_handler_stop_call (self->priv->handler);
3521
3522   empathy_call_window_disconnected (self, TRUE);
3523 }
3524
3525 static void
3526 empathy_call_window_restart_call (EmpathyCallWindow *window)
3527 {
3528   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3529
3530   /* Remove error info bars */
3531   gtk_container_forall (GTK_CONTAINER (priv->errors_vbox),
3532       (GtkCallback) gtk_widget_destroy, NULL);
3533
3534   create_video_output_widget (window);
3535
3536   /* While the call was disconnected, the input volume might have changed.
3537    * However, since the audio_input source was destroyed, its volume has not
3538    * been updated during that time. That's why we manually update it here */
3539   empathy_call_window_mic_volume_changed (window);
3540
3541   priv->outgoing = TRUE;
3542   empathy_call_window_set_state_connecting (window);
3543
3544   if (priv->pipeline_playing)
3545     start_call (window);
3546   else
3547     /* call will be started when the pipeline is ready */
3548     priv->start_call_when_playing = TRUE;
3549
3550   empathy_call_window_setup_avatars (window, priv->handler);
3551
3552   empathy_call_window_show_hangup_button (window, TRUE);
3553 }
3554
3555 static void
3556 empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
3557     EmpathyCallWindow *window)
3558 {
3559   gboolean active;
3560
3561   active = gtk_toggle_tool_button_get_active (button);
3562
3563   empathy_call_window_show_dialpad (window, active);
3564 }
3565
3566 static void
3567 empathy_call_window_fullscreen_cb (gpointer object,
3568                                    EmpathyCallWindow *window)
3569 {
3570   empathy_call_window_fullscreen_toggle (window);
3571 }
3572
3573 static void
3574 empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window)
3575 {
3576   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3577
3578   if (priv->is_fullscreen)
3579     gtk_window_unfullscreen (GTK_WINDOW (window));
3580   else
3581     gtk_window_fullscreen (GTK_WINDOW (window));
3582 }
3583
3584 static gboolean
3585 empathy_call_window_video_button_press_cb (GtkWidget *video_preview,
3586   GdkEventButton *event, EmpathyCallWindow *window)
3587 {
3588   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3589     {
3590       empathy_call_window_video_menu_popup (window, event->button);
3591       return TRUE;
3592     }
3593
3594   return FALSE;
3595 }
3596
3597 static gboolean
3598 empathy_call_window_key_press_cb (GtkWidget *video_output,
3599   GdkEventKey *event, EmpathyCallWindow *window)
3600 {
3601   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3602
3603   if (priv->is_fullscreen && event->keyval == GDK_KEY_Escape)
3604     {
3605       /* Since we are in fullscreen mode, toggling will bring us back to
3606          normal mode. */
3607       empathy_call_window_fullscreen_toggle (window);
3608       return TRUE;
3609     }
3610
3611   return FALSE;
3612 }
3613
3614 static gboolean
3615 empathy_call_window_video_output_motion_notify (GtkWidget *widget,
3616     GdkEventMotion *event, EmpathyCallWindow *window)
3617 {
3618   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3619
3620   if (priv->is_fullscreen)
3621     {
3622       empathy_call_window_fullscreen_show_popup (priv->fullscreen);
3623
3624       /* Show the bottom toolbar */
3625       empathy_call_window_motion_notify_cb (NULL, NULL, window);
3626       return TRUE;
3627     }
3628   return FALSE;
3629 }
3630
3631 static void
3632 empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
3633   guint button)
3634 {
3635   GtkWidget *menu;
3636   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3637
3638   menu = gtk_ui_manager_get_widget (priv->ui_manager,
3639             "/video-popup");
3640   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
3641       button, gtk_get_current_event_time ());
3642   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
3643 }
3644
3645 static void
3646 empathy_call_window_status_message (EmpathyCallWindow *self,
3647   gchar *message)
3648 {
3649   gtk_label_set_label (GTK_LABEL (self->priv->status_label), message);
3650 }
3651
3652 static void
3653 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
3654   gdouble value, EmpathyCallWindow *window)
3655 {
3656   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3657
3658   if (priv->audio_output == NULL)
3659     return;
3660
3661   empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (priv->audio_output),
3662     value);
3663 }
3664
3665 GtkUIManager *
3666 empathy_call_window_get_ui_manager (EmpathyCallWindow *window)
3667 {
3668   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3669
3670   return priv->ui_manager;
3671 }
3672
3673 EmpathyGstAudioSrc *
3674 empathy_call_window_get_audio_src (EmpathyCallWindow *window)
3675 {
3676   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3677
3678   return (EmpathyGstAudioSrc *) priv->audio_input;
3679 }
3680
3681 EmpathyGstVideoSrc *
3682 empathy_call_window_get_video_src (EmpathyCallWindow *self)
3683 {
3684   return EMPATHY_GST_VIDEO_SRC (self->priv->video_input);
3685 }