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