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