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