]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
CallWindow: add a Settings menu item
[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_settings_cb (GtkAction *action,
735     EmpathyCallWindow *self)
736 {
737   GdkDisplay *display;
738   GError *error = NULL;
739   gchar *path, *cmd;
740   GAppInfo *app_info;
741   GdkAppLaunchContext *context = NULL;
742
743   /* Try to run from source directory if possible */
744   path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
745       "empathy", NULL);
746
747   if (!g_file_test (path, G_FILE_TEST_EXISTS))
748     {
749       g_free (path);
750       path = g_build_filename (BIN_DIR, "empathy", NULL);
751     }
752
753   cmd = g_strconcat (path, " -p", NULL);
754
755   app_info = g_app_info_create_from_commandline (cmd, NULL, 0, &error);
756   if (app_info == NULL)
757     {
758       DEBUG ("Failed to create app info: %s", error->message);
759       g_error_free (error);
760       goto out;
761     }
762
763   display = gdk_display_get_default ();
764   context = gdk_display_get_app_launch_context (display);
765
766   if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
767       &error))
768     {
769       g_warning ("Failed to open debug window: %s", error->message);
770       g_error_free (error);
771       goto out;
772     }
773
774 out:
775   tp_clear_object (&app_info);
776   tp_clear_object (&context);
777   g_free (path);
778   g_free (cmd);
779 }
780
781 static void
782 empathy_call_window_contents_cb (GtkAction *action,
783     EmpathyCallWindow *self)
784 {
785   empathy_url_show (GTK_WIDGET (self), "ghelp:empathy?audio-video");
786 }
787
788 static void
789 empathy_call_window_debug_cb (GtkAction *action,
790     EmpathyCallWindow *self)
791 {
792   GdkDisplay *display;
793   GError *error = NULL;
794   gchar *path;
795   GAppInfo *app_info;
796   GdkAppLaunchContext *context = NULL;
797
798   /* Try to run from source directory if possible */
799   path = g_build_filename (g_getenv ("EMPATHY_SRCDIR"), "src",
800       "empathy-debugger", NULL);
801
802   if (!g_file_test (path, G_FILE_TEST_EXISTS))
803     {
804       g_free (path);
805       path = g_build_filename (BIN_DIR, "empathy-debugger", NULL);
806     }
807
808   app_info = g_app_info_create_from_commandline (path, NULL, 0, &error);
809   if (app_info == NULL)
810     {
811       DEBUG ("Failed to create app info: %s", error->message);
812       g_error_free (error);
813       goto out;
814     }
815
816   display = gdk_display_get_default ();
817   context = gdk_display_get_app_launch_context (display);
818
819   if (!g_app_info_launch (app_info, NULL, (GAppLaunchContext *) context,
820       &error))
821     {
822       g_warning ("Failed to open debug window: %s", error->message);
823       g_error_free (error);
824       goto out;
825     }
826
827 out:
828   tp_clear_object (&app_info);
829   tp_clear_object (&context);
830   g_free (path);
831 }
832
833 static void
834 empathy_call_window_about_cb (GtkAction *action,
835     EmpathyCallWindow *self)
836 {
837   empathy_about_dialog_new (GTK_WINDOW (self));
838 }
839
840 static gboolean
841 empathy_call_window_configure_event_cb (GtkWidget *widget,
842     GdkEvent  *event,
843     EmpathyCallWindow *self)
844 {
845   GdkWindow *gdk_window;
846   GdkWindowState window_state;
847
848   gtk_window_get_position (GTK_WINDOW (self), &self->priv->x, &self->priv->y);
849   gtk_window_get_size (GTK_WINDOW (self), &self->priv->w, &self->priv->h);
850
851   gtk_widget_get_preferred_width (self->priv->dtmf_panel,
852       &self->priv->dialpad_width, NULL);
853
854   gdk_window = gtk_widget_get_window (widget);
855   window_state = gdk_window_get_state (gdk_window);
856   self->priv->maximized = (window_state & GDK_WINDOW_STATE_MAXIMIZED);
857
858   return FALSE;
859 }
860
861 static void
862 empathy_call_window_destroyed_cb (GtkWidget *object,
863     EmpathyCallWindow *self)
864 {
865   if (gtk_widget_get_visible (self->priv->dtmf_panel))
866     {
867       /* Save the geometry as if the dialpad was hidden. */
868       empathy_geometry_save_values (GTK_WINDOW (self),
869           self->priv->x, self->priv->y,
870           self->priv->w - self->priv->dialpad_width, self->priv->h,
871           self->priv->maximized);
872     }
873 }
874
875 static void
876 empathy_call_window_init (EmpathyCallWindow *self)
877 {
878   EmpathyCallWindowPriv *priv;
879   GtkBuilder *gui;
880   GtkWidget *top_vbox;
881   gchar *filename;
882   ClutterConstraint *size_constraint;
883   ClutterActor *remote_avatar;
884   GtkStyleContext *context;
885   GdkRGBA rgba;
886   ClutterColor bg;
887
888   priv = self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
889     EMPATHY_TYPE_CALL_WINDOW, EmpathyCallWindowPriv);
890
891   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
892   gui = empathy_builder_get_file (filename,
893     "call_window_vbox", &top_vbox,
894     "errors_vbox", &priv->errors_vbox,
895     "pane", &priv->pane,
896     "remote_user_name_toolbar", &priv->remote_user_name_toolbar,
897     "remote_user_avatar_toolbar", &priv->remote_user_avatar_toolbar,
898     "status_label", &priv->status_label,
899     "audiocall", &priv->audio_call_button,
900     "videocall", &priv->video_call_button,
901     "microphone", &priv->mic_button,
902     "camera", &priv->camera_button,
903     "hangup", &priv->hangup_button,
904     "dialpad", &priv->dialpad_button,
905     "toolbar", &priv->toolbar,
906     "bottom_toolbar", &priv->bottom_toolbar,
907     "ui_manager", &priv->ui_manager,
908     "menufullscreen", &priv->menu_fullscreen,
909     "details_vbox",  &priv->details_vbox,
910     "vcodec_encoding_label", &priv->vcodec_encoding_label,
911     "acodec_encoding_label", &priv->acodec_encoding_label,
912     "acodec_decoding_label", &priv->acodec_decoding_label,
913     "vcodec_decoding_label", &priv->vcodec_decoding_label,
914     "audio_remote_candidate_label", &priv->audio_remote_candidate_label,
915     "audio_local_candidate_label", &priv->audio_local_candidate_label,
916     "video_remote_candidate_label", &priv->video_remote_candidate_label,
917     "video_local_candidate_label", &priv->video_local_candidate_label,
918     "video_remote_candidate_info_img", &priv->video_remote_candidate_info_img,
919     "video_local_candidate_info_img", &priv->video_local_candidate_info_img,
920     "audio_remote_candidate_info_img", &priv->audio_remote_candidate_info_img,
921     "audio_local_candidate_info_img", &priv->audio_local_candidate_info_img,
922     NULL);
923   g_free (filename);
924
925   empathy_builder_connect (gui, self,
926     "hangup", "clicked", empathy_call_window_hangup_cb,
927     "audiocall", "clicked", empathy_call_window_audio_call_cb,
928     "videocall", "clicked", empathy_call_window_video_call_cb,
929     "volume", "value-changed", empathy_call_window_volume_changed_cb,
930     "microphone", "toggled", empathy_call_window_mic_toggled_cb,
931     "camera", "toggled", empathy_call_window_camera_toggled_cb,
932     "dialpad", "toggled", empathy_call_window_dialpad_cb,
933     "menufullscreen", "activate", empathy_call_window_fullscreen_cb,
934     "menusettings", "activate", empathy_call_window_settings_cb,
935     "menucontents", "activate", empathy_call_window_contents_cb,
936     "menudebug", "activate", empathy_call_window_debug_cb,
937     "menuabout", "activate", empathy_call_window_about_cb,
938     "menupreviewdisable", "activate", empathy_call_window_disable_camera_cb,
939     "menupreviewminimise", "activate", empathy_call_window_minimise_camera_cb,
940     "menupreviewmaximise", "activate", empathy_call_window_maximise_camera_cb,
941     NULL);
942
943   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
944
945   priv->camera_monitor = empathy_camera_monitor_dup_singleton ();
946
947   g_object_bind_property (priv->camera_monitor, "available",
948       priv->camera_button, "sensitive",
949       G_BINDING_SYNC_CREATE);
950
951   priv->lock = g_mutex_new ();
952
953   gtk_container_add (GTK_CONTAINER (self), top_vbox);
954
955   priv->content_hbox = gtk_hbox_new (FALSE, CONTENT_HBOX_SPACING);
956   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
957                                   CONTENT_HBOX_BORDER_WIDTH);
958   gtk_box_pack_start (GTK_BOX (priv->pane), priv->content_hbox,
959       TRUE, TRUE, 0);
960
961   /* avatar/video box */
962   priv->video_layout = clutter_bin_layout_new (CLUTTER_BIN_ALIGNMENT_CENTER,
963       CLUTTER_BIN_ALIGNMENT_CENTER);
964
965   priv->video_box = clutter_box_new (priv->video_layout);
966
967   priv->video_container = gtk_clutter_embed_new ();
968
969   /* Set the background color to that of the rest of the window */
970   context = gtk_widget_get_style_context (priv->content_hbox);
971   gtk_style_context_get_background_color (context,
972       GTK_STATE_FLAG_NORMAL, &rgba);
973   bg.red = CLAMP (rgba.red * 255.0, 0, 255);
974   bg.green = CLAMP (rgba.green * 255.0, 0, 255);
975   bg.blue = CLAMP (rgba.blue * 255.0, 0, 255);
976   bg.alpha = CLAMP (rgba.alpha * 255.0, 0, 255);
977   clutter_stage_set_color (
978       CLUTTER_STAGE (gtk_clutter_embed_get_stage (
979           GTK_CLUTTER_EMBED (priv->video_container))),
980       &bg);
981
982   clutter_container_add (
983       CLUTTER_CONTAINER (gtk_clutter_embed_get_stage (
984           GTK_CLUTTER_EMBED (priv->video_container))),
985       priv->video_box,
986       NULL);
987
988   size_constraint = clutter_bind_constraint_new (
989       gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->video_container)),
990       CLUTTER_BIND_SIZE, 0);
991   clutter_actor_add_constraint (priv->video_box, size_constraint);
992
993   priv->remote_user_avatar_widget = gtk_image_new ();
994   remote_avatar = gtk_clutter_actor_new_with_contents (
995       priv->remote_user_avatar_widget);
996
997   clutter_container_add_actor (CLUTTER_CONTAINER (priv->video_box),
998       remote_avatar);
999
1000   gtk_box_pack_start (GTK_BOX (priv->content_hbox),
1001       priv->video_container, TRUE, TRUE,
1002       CONTENT_HBOX_CHILDREN_PACKING_PADDING);
1003
1004   create_pipeline (self);
1005   create_video_output_widget (self);
1006   create_audio_input (self);
1007   create_video_input (self);
1008
1009   /* The call will be started as soon the pipeline is playing */
1010   priv->start_call_when_playing = TRUE;
1011
1012   priv->dtmf_panel = empathy_create_dtmf_dialpad (G_OBJECT (self),
1013       G_CALLBACK (dtmf_button_pressed_cb),
1014       G_CALLBACK (dtmf_button_released_cb));
1015
1016   gtk_box_pack_start (GTK_BOX (priv->pane), priv->dtmf_panel,
1017       FALSE, FALSE, 0);
1018
1019   gtk_box_pack_start (GTK_BOX (priv->pane), priv->details_vbox,
1020       FALSE, FALSE, 0);
1021
1022   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
1023
1024   gtk_widget_show_all (top_vbox);
1025
1026   gtk_widget_hide (priv->dtmf_panel);
1027   gtk_widget_hide (priv->details_vbox);
1028
1029   priv->fullscreen = empathy_call_window_fullscreen_new (self);
1030
1031   empathy_call_window_fullscreen_set_video_widget (priv->fullscreen,
1032       priv->video_container);
1033
1034   g_signal_connect (G_OBJECT (priv->fullscreen->leave_fullscreen_button),
1035       "clicked", G_CALLBACK (empathy_call_window_fullscreen_cb), self);
1036
1037   g_signal_connect (G_OBJECT (self), "realize",
1038     G_CALLBACK (empathy_call_window_realized_cb), self);
1039
1040   g_signal_connect (G_OBJECT (self), "delete-event",
1041     G_CALLBACK (empathy_call_window_delete_cb), self);
1042
1043   g_signal_connect (G_OBJECT (self), "window-state-event",
1044     G_CALLBACK (empathy_call_window_state_event_cb), self);
1045
1046   g_signal_connect (G_OBJECT (self), "key-press-event",
1047       G_CALLBACK (empathy_call_window_key_press_cb), self);
1048
1049   priv->timer = g_timer_new ();
1050
1051   g_object_ref (priv->ui_manager);
1052   g_object_unref (gui);
1053
1054   priv->sound_mgr = empathy_sound_manager_dup_singleton ();
1055   priv->mic_menu = empathy_mic_menu_new (self);
1056
1057   empathy_call_window_show_hangup_button (self, TRUE);
1058
1059   priv->settings = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
1060   g_signal_connect (priv->settings, "changed::"EMPATHY_PREFS_CALL_SOUND_VOLUME,
1061       G_CALLBACK (empathy_call_window_prefs_volume_changed_cb), self);
1062
1063   empathy_geometry_bind (GTK_WINDOW (self), "call-window");
1064   /* These signals are used to track the window position and save it
1065    * when the window is destroyed. We need to do this as we don't want
1066    * the window geometry to be saved with the dialpad taken into account. */
1067   g_signal_connect (self, "destroy",
1068       G_CALLBACK (empathy_call_window_destroyed_cb), self);
1069   g_signal_connect (self, "configure-event",
1070       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1071   g_signal_connect (self, "window-state-event",
1072       G_CALLBACK (empathy_call_window_configure_event_cb), self);
1073
1074   /* Don't display labels in both toolbars */
1075   gtk_toolbar_set_style (GTK_TOOLBAR (priv->toolbar), GTK_TOOLBAR_ICONS);
1076   gtk_toolbar_set_style (GTK_TOOLBAR (priv->bottom_toolbar), GTK_TOOLBAR_ICONS);
1077 }
1078
1079 /* Instead of specifying a width and a height, we specify only one size. That's
1080    because we want a square avatar icon.  */
1081 static void
1082 init_contact_avatar_with_size (EmpathyContact *contact,
1083     GtkWidget *image_widget,
1084     gint size)
1085 {
1086   GdkPixbuf *pixbuf_avatar = NULL;
1087
1088   if (contact != NULL)
1089     {
1090       pixbuf_avatar = empathy_pixbuf_avatar_from_contact_scaled (contact,
1091         size, size);
1092     }
1093
1094   if (pixbuf_avatar == NULL)
1095     {
1096       pixbuf_avatar = empathy_pixbuf_from_icon_name_sized (
1097           EMPATHY_IMAGE_AVATAR_DEFAULT, size);
1098     }
1099
1100   gtk_image_set_from_pixbuf (GTK_IMAGE (image_widget), pixbuf_avatar);
1101
1102   if (pixbuf_avatar != NULL)
1103     g_object_unref (pixbuf_avatar);
1104 }
1105
1106 static void
1107 set_window_title (EmpathyCallWindow *self)
1108 {
1109   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1110   gchar *tmp;
1111
1112   if (priv->contact != NULL)
1113     {
1114       /* translators: Call is a noun and %s is the contact name. This string
1115        * is used in the window title */
1116       tmp = g_strdup_printf (_("Call with %s"),
1117           empathy_contact_get_alias (priv->contact));
1118       gtk_window_set_title (GTK_WINDOW (self), tmp);
1119       g_free (tmp);
1120     }
1121   else
1122     {
1123       gtk_window_set_title (GTK_WINDOW (self), _("Call with %d participants"));
1124     }
1125 }
1126
1127 static void
1128 set_remote_user_name (EmpathyCallWindow *self,
1129   EmpathyContact *contact)
1130 {
1131   const gchar *alias = empathy_contact_get_alias (contact);
1132   const gchar *status = empathy_contact_get_status (contact);
1133   gchar *label;
1134
1135   label = g_strdup_printf ("%s\n<small>%s</small>", alias, status);
1136   gtk_label_set_markup (GTK_LABEL (self->priv->remote_user_name_toolbar),
1137       label);
1138   g_free (label);
1139 }
1140
1141 static void
1142 contact_name_changed_cb (EmpathyContact *contact,
1143     GParamSpec *pspec,
1144     EmpathyCallWindow *self)
1145 {
1146   set_window_title (self);
1147   set_remote_user_name (self, contact);
1148 }
1149
1150 static void
1151 contact_presence_changed_cb (EmpathyContact *contact,
1152     GParamSpec *pspec,
1153     EmpathyCallWindow *self)
1154 {
1155   set_remote_user_name (self, contact);
1156 }
1157
1158 static void
1159 contact_avatar_changed_cb (EmpathyContact *contact,
1160     GParamSpec *pspec,
1161     EmpathyCallWindow *self)
1162 {
1163   int size;
1164   GtkAllocation allocation;
1165   GtkWidget *avatar_widget;
1166
1167   avatar_widget = self->priv->remote_user_avatar_widget;
1168
1169   gtk_widget_get_allocation (avatar_widget, &allocation);
1170   size = allocation.height;
1171
1172   if (size == 0)
1173     {
1174       /* the widget is not allocated yet, set a default size */
1175       size = MIN (REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT,
1176           REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH);
1177     }
1178
1179   init_contact_avatar_with_size (contact, avatar_widget, size);
1180
1181   avatar_widget = self->priv->remote_user_avatar_toolbar;
1182
1183   gtk_widget_get_allocation (avatar_widget, &allocation);
1184   size = allocation.height;
1185
1186   if (size == 0)
1187     {
1188       /* the widget is not allocated yet, set a default size */
1189       size = SMALL_TOOLBAR_SIZE;
1190     }
1191
1192   init_contact_avatar_with_size (contact, avatar_widget, size);
1193 }
1194
1195 static void
1196 empathy_call_window_setup_avatars (EmpathyCallWindow *self,
1197     EmpathyCallHandler *handler)
1198 {
1199   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1200
1201   tp_g_signal_connect_object (priv->contact, "notify::name",
1202       G_CALLBACK (contact_name_changed_cb), self, 0);
1203   tp_g_signal_connect_object (priv->contact, "notify::avatar",
1204     G_CALLBACK (contact_avatar_changed_cb), self, 0);
1205   tp_g_signal_connect_object (priv->contact, "notify::presence",
1206       G_CALLBACK (contact_presence_changed_cb), self, 0);
1207
1208   set_window_title (self);
1209   set_remote_user_name (self, priv->contact);
1210
1211   init_contact_avatar_with_size (priv->contact,
1212       priv->remote_user_avatar_widget,
1213       MIN (REMOTE_CONTACT_AVATAR_DEFAULT_WIDTH,
1214           REMOTE_CONTACT_AVATAR_DEFAULT_HEIGHT));
1215
1216   init_contact_avatar_with_size (priv->contact,
1217       priv->remote_user_avatar_toolbar,
1218       SMALL_TOOLBAR_SIZE);
1219
1220   /* The remote avatar is shown by default and will be hidden when we receive
1221      video from the remote side. */
1222   clutter_actor_hide (priv->video_output);
1223   gtk_widget_show (priv->remote_user_avatar_widget);
1224 }
1225
1226 static void
1227 update_send_codec (EmpathyCallWindow *self,
1228     gboolean audio)
1229 {
1230   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1231   FsCodec *codec;
1232   GtkWidget *widget;
1233   gchar *tmp;
1234
1235   if (audio)
1236     {
1237       codec = empathy_call_handler_get_send_audio_codec (priv->handler);
1238       widget = priv->acodec_encoding_label;
1239     }
1240   else
1241     {
1242       codec = empathy_call_handler_get_send_video_codec (priv->handler);
1243       widget = priv->vcodec_encoding_label;
1244     }
1245
1246   if (codec == NULL)
1247     return;
1248
1249   tmp = g_strdup_printf ("%s/%u", codec->encoding_name, codec->clock_rate);
1250   gtk_label_set_text (GTK_LABEL (widget), tmp);
1251   g_free (tmp);
1252 }
1253
1254 static void
1255 send_audio_codec_notify_cb (GObject *object,
1256     GParamSpec *pspec,
1257     gpointer user_data)
1258 {
1259   EmpathyCallWindow *self = user_data;
1260
1261   update_send_codec (self, TRUE);
1262 }
1263
1264 static void
1265 send_video_codec_notify_cb (GObject *object,
1266     GParamSpec *pspec,
1267     gpointer user_data)
1268 {
1269   EmpathyCallWindow *self = user_data;
1270
1271   update_send_codec (self, FALSE);
1272 }
1273
1274 static void
1275 update_recv_codec (EmpathyCallWindow *self,
1276     gboolean audio)
1277 {
1278   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1279   GList *codecs, *l;
1280   GtkWidget *widget;
1281   GString *str = NULL;
1282
1283   if (audio)
1284     {
1285       codecs = empathy_call_handler_get_recv_audio_codecs (priv->handler);
1286       widget = priv->acodec_decoding_label;
1287     }
1288   else
1289     {
1290       codecs = empathy_call_handler_get_recv_video_codecs (priv->handler);
1291       widget = priv->vcodec_decoding_label;
1292     }
1293
1294   if (codecs == NULL)
1295     return;
1296
1297   for (l = codecs; l != NULL; l = g_list_next (l))
1298     {
1299       FsCodec *codec = l->data;
1300
1301       if (str == NULL)
1302         str = g_string_new (NULL);
1303       else
1304         g_string_append (str, ", ");
1305
1306       g_string_append_printf (str, "%s/%u", codec->encoding_name,
1307           codec->clock_rate);
1308     }
1309
1310   gtk_label_set_text (GTK_LABEL (widget), str->str);
1311   g_string_free (str, TRUE);
1312 }
1313
1314 static void
1315 recv_audio_codecs_notify_cb (GObject *object,
1316     GParamSpec *pspec,
1317     gpointer user_data)
1318 {
1319   EmpathyCallWindow *self = user_data;
1320
1321   update_recv_codec (self, TRUE);
1322 }
1323
1324 static void
1325 recv_video_codecs_notify_cb (GObject *object,
1326     GParamSpec *pspec,
1327     gpointer user_data)
1328 {
1329   EmpathyCallWindow *self = user_data;
1330
1331   update_recv_codec (self, FALSE);
1332 }
1333
1334 static const gchar *
1335 candidate_type_to_str (FsCandidate *candidate)
1336 {
1337   switch (candidate->type)
1338     {
1339       case FS_CANDIDATE_TYPE_HOST:
1340         return "host";
1341       case FS_CANDIDATE_TYPE_SRFLX:
1342         return "server reflexive";
1343       case FS_CANDIDATE_TYPE_PRFLX:
1344         return "peer reflexive";
1345       case FS_CANDIDATE_TYPE_RELAY:
1346         return "relay";
1347       case FS_CANDIDATE_TYPE_MULTICAST:
1348         return "multicast";
1349     }
1350
1351   return NULL;
1352 }
1353
1354 static const gchar *
1355 candidate_type_to_desc (FsCandidate *candidate)
1356 {
1357   switch (candidate->type)
1358     {
1359       case FS_CANDIDATE_TYPE_HOST:
1360         return _("The IP address as seen by the machine");
1361       case FS_CANDIDATE_TYPE_SRFLX:
1362         return _("The IP address as seen by a server on the Internet");
1363       case FS_CANDIDATE_TYPE_PRFLX:
1364         return _("The IP address of the peer as seen by the other side");
1365       case FS_CANDIDATE_TYPE_RELAY:
1366         return _("The IP address of a relay server");
1367       case FS_CANDIDATE_TYPE_MULTICAST:
1368         return _("The IP address of the multicast group");
1369     }
1370
1371   return NULL;
1372 }
1373
1374 static void
1375 update_candidat_widget (EmpathyCallWindow *self,
1376     GtkWidget *label,
1377     GtkWidget *img,
1378     FsCandidate *candidate)
1379 {
1380   gchar *str;
1381
1382   g_assert (candidate != NULL);
1383   str = g_strdup_printf ("%s %u (%s)", candidate->ip,
1384       candidate->port, candidate_type_to_str (candidate));
1385
1386   gtk_label_set_text (GTK_LABEL (label), str);
1387   gtk_widget_set_tooltip_text (img, candidate_type_to_desc (candidate));
1388
1389   g_free (str);
1390 }
1391
1392 static void
1393 candidates_changed_cb (GObject *object,
1394     FsMediaType type,
1395     EmpathyCallWindow *self)
1396 {
1397   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1398   FsCandidate *candidate = NULL;
1399
1400   if (type == FS_MEDIA_TYPE_VIDEO)
1401     {
1402       /* Update remote candidate */
1403       candidate = empathy_call_handler_get_video_remote_candidate (
1404           priv->handler);
1405
1406       update_candidat_widget (self, priv->video_remote_candidate_label,
1407           priv->video_remote_candidate_info_img, candidate);
1408
1409       /* Update local candidate */
1410       candidate = empathy_call_handler_get_video_local_candidate (
1411           priv->handler);
1412
1413       update_candidat_widget (self, priv->video_local_candidate_label,
1414           priv->video_local_candidate_info_img, candidate);
1415     }
1416   else
1417     {
1418       /* Update remote candidate */
1419       candidate = empathy_call_handler_get_audio_remote_candidate (
1420           priv->handler);
1421
1422       update_candidat_widget (self, priv->audio_remote_candidate_label,
1423           priv->audio_remote_candidate_info_img, candidate);
1424
1425       /* Update local candidate */
1426       candidate = empathy_call_handler_get_audio_local_candidate (
1427           priv->handler);
1428
1429       update_candidat_widget (self, priv->audio_local_candidate_label,
1430           priv->audio_local_candidate_info_img, candidate);
1431     }
1432 }
1433
1434 static void
1435 empathy_call_window_constructed (GObject *object)
1436 {
1437   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1438   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1439   TpyCallChannel *call;
1440
1441   g_assert (priv->handler != NULL);
1442
1443   g_object_get (priv->handler, "call-channel", &call, NULL);
1444   priv->outgoing = (call == NULL);
1445   if (call != NULL)
1446     g_object_unref (call);
1447
1448   g_object_get (priv->handler, "target-contact", &priv->contact, NULL);
1449   g_assert (priv->contact != NULL);
1450
1451   empathy_call_window_setup_avatars (self, priv->handler);
1452   empathy_call_window_set_state_connecting (self);
1453
1454   if (!empathy_call_handler_has_initial_video (priv->handler))
1455     {
1456       gtk_toggle_tool_button_set_active (
1457           GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
1458     }
1459   /* If call has InitialVideo, the preview will be started once the call has
1460    * been started (start_call()). */
1461
1462   update_send_codec (self, TRUE);
1463   update_send_codec (self, FALSE);
1464   update_recv_codec (self, TRUE);
1465   update_recv_codec (self, FALSE);
1466
1467   tp_g_signal_connect_object (priv->handler, "notify::send-audio-codec",
1468       G_CALLBACK (send_audio_codec_notify_cb), self, 0);
1469   tp_g_signal_connect_object (priv->handler, "notify::send-video-codec",
1470       G_CALLBACK (send_video_codec_notify_cb), self, 0);
1471   tp_g_signal_connect_object (priv->handler, "notify::recv-audio-codecs",
1472       G_CALLBACK (recv_audio_codecs_notify_cb), self, 0);
1473   tp_g_signal_connect_object (priv->handler, "notify::recv-video-codecs",
1474       G_CALLBACK (recv_video_codecs_notify_cb), self, 0);
1475
1476   tp_g_signal_connect_object (priv->handler, "candidates-changed",
1477       G_CALLBACK (candidates_changed_cb), self, 0);
1478 }
1479
1480 static void empathy_call_window_dispose (GObject *object);
1481 static void empathy_call_window_finalize (GObject *object);
1482
1483 static void
1484 empathy_call_window_set_property (GObject *object,
1485   guint property_id, const GValue *value, GParamSpec *pspec)
1486 {
1487   EmpathyCallWindowPriv *priv = GET_PRIV (object);
1488
1489   switch (property_id)
1490     {
1491       case PROP_CALL_HANDLER:
1492         priv->handler = g_value_dup_object (value);
1493         break;
1494       default:
1495         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1496     }
1497 }
1498
1499 static void
1500 empathy_call_window_get_property (GObject *object,
1501   guint property_id, GValue *value, GParamSpec *pspec)
1502 {
1503   EmpathyCallWindowPriv *priv = GET_PRIV (object);
1504
1505   switch (property_id)
1506     {
1507       case PROP_CALL_HANDLER:
1508         g_value_set_object (value, priv->handler);
1509         break;
1510       default:
1511         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
1512     }
1513 }
1514
1515 static void
1516 empathy_call_window_class_init (
1517   EmpathyCallWindowClass *empathy_call_window_class)
1518 {
1519   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
1520   GParamSpec *param_spec;
1521
1522   g_type_class_add_private (empathy_call_window_class,
1523     sizeof (EmpathyCallWindowPriv));
1524
1525   object_class->constructed = empathy_call_window_constructed;
1526   object_class->set_property = empathy_call_window_set_property;
1527   object_class->get_property = empathy_call_window_get_property;
1528
1529   object_class->dispose = empathy_call_window_dispose;
1530   object_class->finalize = empathy_call_window_finalize;
1531
1532   param_spec = g_param_spec_object ("handler",
1533     "handler", "The call handler",
1534     EMPATHY_TYPE_CALL_HANDLER,
1535     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
1536   g_object_class_install_property (object_class,
1537     PROP_CALL_HANDLER, param_spec);
1538 }
1539
1540 void
1541 empathy_call_window_dispose (GObject *object)
1542 {
1543   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1544   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1545
1546   if (priv->dispose_has_run)
1547     return;
1548
1549   priv->dispose_has_run = TRUE;
1550
1551   if (priv->handler != NULL)
1552     {
1553       empathy_call_handler_stop_call (priv->handler);
1554       tp_clear_object (&priv->handler);
1555     }
1556
1557   if (priv->bus_message_source_id != 0)
1558     {
1559       g_source_remove (priv->bus_message_source_id);
1560       priv->bus_message_source_id = 0;
1561     }
1562
1563   if (priv->got_video_src > 0)
1564     {
1565       g_source_remove (priv->got_video_src);
1566       priv->got_video_src = 0;
1567     }
1568
1569   tp_clear_object (&priv->pipeline);
1570   tp_clear_object (&priv->video_input);
1571   tp_clear_object (&priv->audio_input);
1572   tp_clear_object (&priv->video_tee);
1573   tp_clear_object (&priv->ui_manager);
1574   tp_clear_object (&priv->fullscreen);
1575   tp_clear_object (&priv->camera_monitor);
1576   tp_clear_object (&priv->settings);
1577
1578   g_list_free_full (priv->notifiers, g_object_unref);
1579
1580   if (priv->timer_id != 0)
1581     g_source_remove (priv->timer_id);
1582   priv->timer_id = 0;
1583
1584   if (priv->contact != NULL)
1585     {
1586       g_signal_handlers_disconnect_by_func (priv->contact,
1587           contact_name_changed_cb, self);
1588       priv->contact = NULL;
1589     }
1590
1591
1592   tp_clear_object (&priv->sound_mgr);
1593
1594   tp_clear_object (&priv->mic_menu);
1595
1596   G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
1597 }
1598
1599 static void
1600 disconnect_video_output_motion_handler (EmpathyCallWindow *self)
1601 {
1602   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1603
1604   if (priv->video_output_motion_handler_id != 0)
1605     {
1606       g_signal_handler_disconnect (G_OBJECT (priv->video_container),
1607           priv->video_output_motion_handler_id);
1608       priv->video_output_motion_handler_id = 0;
1609     }
1610 }
1611
1612 void
1613 empathy_call_window_finalize (GObject *object)
1614 {
1615   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
1616   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1617
1618   disconnect_video_output_motion_handler (self);
1619
1620   /* free any data held directly by the object here */
1621   g_mutex_free (priv->lock);
1622
1623   g_timer_destroy (priv->timer);
1624
1625   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
1626 }
1627
1628
1629 EmpathyCallWindow *
1630 empathy_call_window_new (EmpathyCallHandler *handler)
1631 {
1632   return EMPATHY_CALL_WINDOW (
1633     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
1634 }
1635
1636 static void
1637 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
1638   GstElement *conference, gpointer user_data)
1639 {
1640   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1641   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1642   FsElementAddedNotifier *notifier;
1643   GKeyFile *keyfile;
1644
1645   DEBUG ("Conference added");
1646
1647   /* Add notifier to set the various element properties as needed */
1648   notifier = fs_element_added_notifier_new ();
1649   keyfile = fs_utils_get_default_element_properties (conference);
1650
1651   if (keyfile != NULL)
1652     fs_element_added_notifier_set_properties_from_keyfile (notifier, keyfile);
1653
1654   fs_element_added_notifier_add (notifier, GST_BIN (priv->pipeline));
1655
1656   priv->notifiers = g_list_prepend (priv->notifiers, notifier);
1657
1658   gst_bin_add (GST_BIN (priv->pipeline), conference);
1659   gst_element_set_state (conference, GST_STATE_PLAYING);
1660 }
1661
1662 static void
1663 empathy_call_window_conference_removed_cb (EmpathyCallHandler *handler,
1664   GstElement *conference, gpointer user_data)
1665 {
1666   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1667   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1668
1669   gst_bin_remove (GST_BIN (priv->pipeline), conference);
1670   gst_element_set_state (conference, GST_STATE_NULL);
1671 }
1672
1673 static gboolean
1674 empathy_call_window_reset_pipeline (EmpathyCallWindow *self)
1675 {
1676   GstStateChangeReturn state_change_return;
1677   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1678
1679   if (priv->pipeline == NULL)
1680     return TRUE;
1681
1682   if (priv->bus_message_source_id != 0)
1683     {
1684       g_source_remove (priv->bus_message_source_id);
1685       priv->bus_message_source_id = 0;
1686     }
1687
1688   state_change_return = gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1689
1690   if (state_change_return == GST_STATE_CHANGE_SUCCESS ||
1691         state_change_return == GST_STATE_CHANGE_NO_PREROLL)
1692     {
1693       if (priv->pipeline != NULL)
1694         g_object_unref (priv->pipeline);
1695       priv->pipeline = NULL;
1696
1697       if (priv->audio_output != NULL)
1698         g_object_unref (priv->audio_output);
1699       priv->audio_output = NULL;
1700
1701       if (priv->video_tee != NULL)
1702         g_object_unref (priv->video_tee);
1703       priv->video_tee = NULL;
1704
1705       if (priv->video_preview != NULL)
1706         clutter_actor_destroy (priv->video_preview);
1707       priv->video_preview = NULL;
1708
1709       priv->funnel = NULL;
1710
1711       create_pipeline (self);
1712       /* Call will be started when user will hit the 'redial' button */
1713       priv->start_call_when_playing = FALSE;
1714       gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1715
1716       return TRUE;
1717     }
1718   else
1719     {
1720       g_message ("Error: could not destroy pipeline. Closing call window");
1721       gtk_widget_destroy (GTK_WIDGET (self));
1722
1723       return FALSE;
1724     }
1725 }
1726
1727 static void
1728 reset_details_pane (EmpathyCallWindow *self)
1729 {
1730   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1731
1732   gtk_label_set_text (GTK_LABEL (priv->vcodec_encoding_label), _("Unknown"));
1733   gtk_label_set_text (GTK_LABEL (priv->acodec_encoding_label), _("Unknown"));
1734   gtk_label_set_text (GTK_LABEL (priv->vcodec_decoding_label), _("Unknown"));
1735   gtk_label_set_text (GTK_LABEL (priv->acodec_decoding_label), _("Unknown"));
1736 }
1737
1738 static gboolean
1739 empathy_call_window_disconnected (EmpathyCallWindow *self,
1740     gboolean restart)
1741 {
1742   gboolean could_disconnect = FALSE;
1743   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1744   gboolean could_reset_pipeline;
1745
1746   /* Leave full screen mode if needed */
1747   gtk_window_unfullscreen (GTK_WINDOW (self));
1748
1749   gtk_action_set_sensitive (priv->menu_fullscreen, FALSE);
1750   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
1751
1752   could_reset_pipeline = empathy_call_window_reset_pipeline (self);
1753
1754   if (priv->call_state == CONNECTING)
1755       empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
1756
1757   if (priv->call_state != REDIALING)
1758     priv->call_state = DISCONNECTED;
1759
1760   if (could_reset_pipeline)
1761     {
1762       g_mutex_lock (priv->lock);
1763
1764       g_timer_stop (priv->timer);
1765
1766       if (priv->timer_id != 0)
1767         g_source_remove (priv->timer_id);
1768       priv->timer_id = 0;
1769
1770       g_mutex_unlock (priv->lock);
1771
1772       if (!restart)
1773         /* We are about to destroy the window, no need to update it or create
1774          * a video preview */
1775         return TRUE;
1776
1777       empathy_call_window_status_message (self, _("Disconnected"));
1778
1779       empathy_call_window_show_hangup_button (self, FALSE);
1780
1781       /* Unsensitive the camera and mic button */
1782       gtk_widget_set_sensitive (priv->camera_button, FALSE);
1783       gtk_widget_set_sensitive (priv->mic_button, FALSE);
1784
1785       /* Be sure that the mic button is enabled */
1786       gtk_toggle_tool_button_set_active (
1787           GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), TRUE);
1788
1789       if (priv->camera_state == CAMERA_STATE_ON)
1790         {
1791           /* Restart the preview with the new pipeline. */
1792           display_video_preview (self, TRUE);
1793         }
1794
1795       /* destroy the video output; it will be recreated when we'll redial */
1796       disconnect_video_output_motion_handler (self);
1797       if (priv->video_output != NULL)
1798         clutter_actor_destroy (priv->video_output);
1799       priv->video_output = NULL;
1800       if (priv->got_video_src > 0)
1801         {
1802           g_source_remove (priv->got_video_src);
1803           priv->got_video_src = 0;
1804         }
1805
1806       gtk_widget_show (priv->remote_user_avatar_widget);
1807
1808       reset_details_pane (self);
1809
1810       priv->sending_video = FALSE;
1811       priv->call_started = FALSE;
1812
1813       could_disconnect = TRUE;
1814
1815       /* TODO: display the self avatar of the preview (depends if the "Always
1816        * Show Video Preview" is enabled or not) */
1817     }
1818
1819   return could_disconnect;
1820 }
1821
1822
1823 static void
1824 empathy_call_window_channel_closed_cb (EmpathyCallHandler *handler,
1825     gpointer user_data)
1826 {
1827   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1828   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1829
1830   if (empathy_call_window_disconnected (self, TRUE) &&
1831       priv->call_state == REDIALING)
1832       empathy_call_window_restart_call (self);
1833 }
1834
1835 static gboolean
1836 empathy_call_window_sink_removed_cb (EmpathyCallHandler *handler,
1837     GstPad *sink,
1838     FsMediaType media_type,
1839     EmpathyCallWindow *self)
1840 {
1841   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1842
1843   DEBUG ("removing content");
1844
1845   /*
1846    * This assumes that there is only one video stream per channel...
1847    */
1848
1849   if ((guint) media_type == FS_MEDIA_TYPE_VIDEO)
1850     {
1851       if (priv->funnel != NULL)
1852         {
1853           GstElement *output;
1854
1855           output = priv->video_output_sink;
1856
1857           gst_element_set_state (output, GST_STATE_NULL);
1858           gst_element_set_state (priv->funnel, GST_STATE_NULL);
1859
1860           gst_bin_remove (GST_BIN (priv->pipeline), output);
1861           gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
1862           priv->funnel = NULL;
1863           return TRUE;
1864         }
1865     }
1866   else if (media_type == FS_MEDIA_TYPE_AUDIO)
1867     {
1868       if (priv->audio_output != NULL)
1869         {
1870           gst_element_set_state (priv->audio_output, GST_STATE_NULL);
1871
1872           gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
1873           priv->audio_output = NULL;
1874           return TRUE;
1875         }
1876     }
1877
1878   return FALSE;
1879 }
1880
1881 /* Called with global lock held */
1882 static GstPad *
1883 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
1884 {
1885   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1886   GstPad *pad;
1887   GstElement *output;
1888
1889   if (priv->funnel == NULL)
1890     {
1891       output = priv->video_output_sink;
1892
1893       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
1894
1895       if (!priv->funnel)
1896         {
1897           g_warning ("Could not create fsfunnel");
1898           return NULL;
1899         }
1900
1901       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->funnel))
1902         {
1903           gst_object_unref (priv->funnel);
1904           priv->funnel = NULL;
1905           g_warning ("Could  not add funnel to pipeline");
1906           return NULL;
1907         }
1908
1909       if (!gst_bin_add (GST_BIN (priv->pipeline), output))
1910         {
1911           g_warning ("Could not add the video output widget to the pipeline");
1912           goto error;
1913         }
1914
1915       if (!gst_element_link (priv->funnel, output))
1916         {
1917           g_warning ("Could not link output sink to funnel");
1918           goto error_output_added;
1919         }
1920
1921       if (gst_element_set_state (output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1922         {
1923           g_warning ("Could not start video sink");
1924           goto error_output_added;
1925         }
1926
1927       if (gst_element_set_state (priv->funnel, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1928         {
1929           g_warning ("Could not start funnel");
1930           goto error_output_added;
1931         }
1932     }
1933
1934   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
1935
1936   if (!pad)
1937     g_warning ("Could not get request pad from funnel");
1938
1939   return pad;
1940
1941
1942  error_output_added:
1943
1944   gst_element_set_locked_state (priv->funnel, TRUE);
1945   gst_element_set_locked_state (output, TRUE);
1946
1947   gst_element_set_state (priv->funnel, GST_STATE_NULL);
1948   gst_element_set_state (output, GST_STATE_NULL);
1949
1950   gst_bin_remove (GST_BIN (priv->pipeline), output);
1951   gst_element_set_locked_state (output, FALSE);
1952
1953  error:
1954
1955   gst_bin_remove (GST_BIN (priv->pipeline), priv->funnel);
1956   priv->funnel = NULL;
1957
1958   return NULL;
1959 }
1960
1961 /* Called with global lock held */
1962 static GstPad *
1963 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self)
1964 {
1965   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1966   GstPad *pad;
1967   GstPadTemplate *template;
1968
1969   if (priv->audio_output == NULL)
1970     {
1971       priv->audio_output = empathy_audio_sink_new ();
1972       g_object_ref_sink (priv->audio_output);
1973
1974       if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output))
1975         {
1976           g_warning ("Could not add audio sink to pipeline");
1977           g_object_unref (priv->audio_output);
1978           goto error_add_output;
1979         }
1980
1981       if (gst_element_set_state (priv->audio_output, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
1982         {
1983           g_warning ("Could not start audio sink");
1984           goto error;
1985         }
1986     }
1987
1988   template = gst_element_class_get_pad_template (
1989     GST_ELEMENT_GET_CLASS (priv->audio_output), "sink%d");
1990
1991   pad = gst_element_request_pad (priv->audio_output,
1992     template, NULL, NULL);
1993
1994   if (pad == NULL)
1995     {
1996       g_warning ("Could not get sink pad from sink");
1997       return NULL;
1998     }
1999
2000   return pad;
2001
2002 error:
2003   gst_element_set_locked_state (priv->audio_output, TRUE);
2004   gst_element_set_state (priv->audio_output, GST_STATE_NULL);
2005   gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_output);
2006   priv->audio_output = NULL;
2007
2008 error_add_output:
2009
2010   return NULL;
2011 }
2012
2013 static gboolean
2014 empathy_call_window_update_timer (gpointer user_data)
2015 {
2016   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2017   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2018   const gchar *status;
2019   gchar *str;
2020   gdouble time_;
2021
2022   time_ = g_timer_elapsed (priv->timer, NULL);
2023
2024   if (priv->call_state == HELD)
2025     status = _("On hold");
2026   else if (!gtk_toggle_tool_button_get_active (
2027       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
2028     status = _("Mute");
2029   else
2030     status = _("Duration");
2031
2032   /* Translators: 'status - minutes:seconds' the caller has been connected */
2033   str = g_strdup_printf (_("%s â€” %d:%02dm"),
2034       status,
2035       (int) time_ / 60, (int) time_ % 60);
2036   empathy_call_window_status_message (self, str);
2037   g_free (str);
2038
2039   return TRUE;
2040 }
2041
2042 #if 0
2043 static void
2044 display_error (EmpathyCallWindow *self,
2045     TpyCallChannel *call,
2046     const gchar *img,
2047     const gchar *title,
2048     const gchar *desc,
2049     const gchar *details)
2050 {
2051   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2052   GtkWidget *info_bar;
2053   GtkWidget *content_area;
2054   GtkWidget *hbox;
2055   GtkWidget *vbox;
2056   GtkWidget *image;
2057   GtkWidget *label;
2058   gchar *txt;
2059
2060   /* Create info bar */
2061   info_bar = gtk_info_bar_new_with_buttons (GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
2062       NULL);
2063
2064   gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar), GTK_MESSAGE_WARNING);
2065
2066   content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));
2067
2068   /* hbox containing the image and the messages vbox */
2069   hbox = gtk_hbox_new (FALSE, 3);
2070   gtk_container_add (GTK_CONTAINER (content_area), hbox);
2071
2072   /* Add image */
2073   image = gtk_image_new_from_icon_name (img, GTK_ICON_SIZE_DIALOG);
2074   gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
2075
2076   /* vbox containing the main message and the details expander */
2077   vbox = gtk_vbox_new (FALSE, 3);
2078   gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
2079
2080   /* Add text */
2081   txt = g_strdup_printf ("<b>%s</b>\n%s", title, desc);
2082
2083   label = gtk_label_new (NULL);
2084   gtk_label_set_markup (GTK_LABEL (label), txt);
2085   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2086   gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2087   g_free (txt);
2088
2089   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
2090
2091   /* Add details */
2092   if (details != NULL)
2093     {
2094       GtkWidget *expander;
2095
2096       expander = gtk_expander_new (_("Technical Details"));
2097
2098       txt = g_strdup_printf ("<i>%s</i>", details);
2099
2100       label = gtk_label_new (NULL);
2101       gtk_label_set_markup (GTK_LABEL (label), txt);
2102       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
2103       gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
2104       g_free (txt);
2105
2106       gtk_container_add (GTK_CONTAINER (expander), label);
2107       gtk_box_pack_start (GTK_BOX (vbox), expander, TRUE, TRUE, 0);
2108     }
2109
2110   g_signal_connect (info_bar, "response",
2111       G_CALLBACK (gtk_widget_destroy), NULL);
2112
2113   gtk_box_pack_start (GTK_BOX (priv->errors_vbox), info_bar,
2114       FALSE, FALSE, CONTENT_HBOX_CHILDREN_PACKING_PADDING);
2115   gtk_widget_show_all (info_bar);
2116 }
2117
2118 static gchar *
2119 media_stream_error_to_txt (EmpathyCallWindow *self,
2120     TpyCallChannel *call,
2121     gboolean audio,
2122     TpMediaStreamError error)
2123 {
2124   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2125   const gchar *cm = NULL;
2126   gchar *url;
2127   gchar *result;
2128
2129   switch (error)
2130     {
2131       case TP_MEDIA_STREAM_ERROR_CODEC_NEGOTIATION_FAILED:
2132         if (audio)
2133           return g_strdup_printf (
2134               _("%s's software does not understand any of the audio formats "
2135                 "supported by your computer"),
2136             empathy_contact_get_alias (priv->contact));
2137         else
2138           return g_strdup_printf (
2139               _("%s's software does not understand any of the video formats "
2140                 "supported by your computer"),
2141             empathy_contact_get_alias (priv->contact));
2142
2143       case TP_MEDIA_STREAM_ERROR_CONNECTION_FAILED:
2144         return g_strdup_printf (
2145             _("Can't establish a connection to %s. "
2146               "One of you might be on a network that does not allow "
2147               "direct connections."),
2148           empathy_contact_get_alias (priv->contact));
2149
2150       case TP_MEDIA_STREAM_ERROR_NETWORK_ERROR:
2151           return g_strdup (_("There was a failure on the network"));
2152
2153       case TP_MEDIA_STREAM_ERROR_NO_CODECS:
2154         if (audio)
2155           return g_strdup (_("The audio formats necessary for this call "
2156                 "are not installed on your computer"));
2157         else
2158           return g_strdup (_("The video formats necessary for this call "
2159                 "are not installed on your computer"));
2160
2161       case TP_MEDIA_STREAM_ERROR_INVALID_CM_BEHAVIOR:
2162         tp_connection_parse_object_path (
2163             tp_channel_borrow_connection (TP_CHANNEL (call)),
2164             NULL, &cm);
2165
2166         url = g_strdup_printf ("http://bugs.freedesktop.org/enter_bug.cgi?"
2167             "product=Telepathy&amp;component=%s", cm);
2168
2169         result = g_strdup_printf (
2170             _("Something unexpected happened in a Telepathy component. "
2171               "Please <a href=\"%s\">report this bug</a> and attach "
2172               "logs gathered from the 'Debug' window in the Help menu."), url);
2173
2174         g_free (url);
2175         g_free (cm);
2176         return result;
2177
2178       case TP_MEDIA_STREAM_ERROR_MEDIA_ERROR:
2179         return g_strdup (_("There was a failure in the call engine"));
2180
2181       case TP_MEDIA_STREAM_ERROR_EOS:
2182         return g_strdup (_("The end of the stream was reached"));
2183
2184       case TP_MEDIA_STREAM_ERROR_UNKNOWN:
2185       default:
2186         return NULL;
2187     }
2188 }
2189
2190 static void
2191 empathy_call_window_stream_error (EmpathyCallWindow *self,
2192     TpyCallChannel *call,
2193     gboolean audio,
2194     guint code,
2195     const gchar *msg,
2196     const gchar *icon,
2197     const gchar *title)
2198 {
2199   gchar *desc;
2200
2201   desc = media_stream_error_to_txt (self, call, audio, code);
2202   if (desc == NULL)
2203     {
2204       /* No description, use the error message. That's not great as it's not
2205        * localized but it's better than nothing. */
2206       display_error (self, call, icon, title, msg, NULL);
2207     }
2208   else
2209     {
2210       display_error (self, call, icon, title, desc, msg);
2211       g_free (desc);
2212     }
2213 }
2214
2215 static void
2216 empathy_call_window_audio_stream_error (TpyCallChannel *call,
2217     guint code,
2218     const gchar *msg,
2219     EmpathyCallWindow *self)
2220 {
2221   empathy_call_window_stream_error (self, call, TRUE, code, msg,
2222       "gnome-stock-mic", _("Can't establish audio stream"));
2223 }
2224
2225 static void
2226 empathy_call_window_video_stream_error (TpyCallChannel *call,
2227     guint code,
2228     const gchar *msg,
2229     EmpathyCallWindow *self)
2230 {
2231   empathy_call_window_stream_error (self, call, FALSE, code, msg,
2232       "camera-web", _("Can't establish video stream"));
2233 }
2234 #endif
2235
2236 static void
2237 empathy_call_window_state_changed_cb (EmpathyCallHandler *handler,
2238     TpyCallState state,
2239     EmpathyCallWindow *self)
2240 {
2241   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2242   TpyCallChannel *call;
2243   gboolean can_send_video;
2244
2245   if (state != TPY_CALL_STATE_ACCEPTED)
2246     return;
2247
2248   if (priv->call_state == CONNECTED)
2249     return;
2250
2251   g_timer_start (priv->timer);
2252   priv->call_state = CONNECTED;
2253
2254   empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2255
2256   can_send_video = priv->video_input != NULL &&
2257     empathy_contact_can_voip_video (priv->contact) &&
2258     empathy_camera_monitor_get_available (priv->camera_monitor);
2259
2260   g_object_get (priv->handler, "call-channel", &call, NULL);
2261
2262   if (tpy_call_channel_has_dtmf (call))
2263     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
2264
2265   if (priv->video_input == NULL)
2266     empathy_call_window_set_send_video (self, CAMERA_STATE_OFF);
2267
2268   gtk_widget_set_sensitive (priv->camera_button, can_send_video);
2269
2270   empathy_call_window_show_hangup_button (self, TRUE);
2271
2272   gtk_widget_set_sensitive (priv->mic_button, TRUE);
2273
2274   clutter_actor_hide (priv->video_output);
2275   gtk_widget_show (priv->remote_user_avatar_widget);
2276
2277   g_object_unref (call);
2278
2279   g_mutex_lock (priv->lock);
2280
2281   priv->timer_id = g_timeout_add_seconds (1,
2282     empathy_call_window_update_timer, self);
2283
2284   g_mutex_unlock (priv->lock);
2285
2286   empathy_call_window_update_timer (self);
2287
2288   gtk_action_set_sensitive (priv->menu_fullscreen, TRUE);
2289 }
2290
2291 static gboolean
2292 empathy_call_window_show_video_output_cb (gpointer user_data)
2293 {
2294   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2295
2296   if (self->priv->video_output != NULL)
2297     {
2298       gtk_widget_hide (self->priv->remote_user_avatar_widget);
2299       clutter_actor_show (self->priv->video_output);
2300     }
2301
2302   return FALSE;
2303 }
2304
2305 static gboolean
2306 empathy_call_window_check_video_cb (gpointer data)
2307 {
2308   EmpathyCallWindow *self = data;
2309
2310   if (self->priv->got_video)
2311     {
2312       self->priv->got_video = FALSE;
2313       return TRUE;
2314     }
2315
2316   /* No video in the last N seconds, display the remote avatar */
2317   empathy_call_window_show_video_output (self, FALSE);
2318
2319   return TRUE;
2320 }
2321
2322 /* Called from the streaming thread */
2323 static gboolean
2324 empathy_call_window_video_probe_cb (GstPad *pad,
2325     GstMiniObject *mini_obj,
2326     EmpathyCallWindow *self)
2327 {
2328   /* Ignore events */
2329   if (GST_IS_EVENT (mini_obj))
2330     return TRUE;
2331
2332   if (G_UNLIKELY (!self->priv->got_video))
2333     {
2334       /* show the remote video */
2335       g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
2336           empathy_call_window_show_video_output_cb,
2337           g_object_ref (self), g_object_unref);
2338
2339       self->priv->got_video = TRUE;
2340     }
2341
2342   return TRUE;
2343 }
2344
2345 /* Called from the streaming thread */
2346 static gboolean
2347 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
2348   GstPad *src, guint media_type, gpointer user_data)
2349 {
2350   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2351   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2352   gboolean retval = FALSE;
2353
2354   GstPad *pad;
2355
2356   g_mutex_lock (priv->lock);
2357
2358   switch (media_type)
2359     {
2360       case TP_MEDIA_STREAM_TYPE_AUDIO:
2361         pad = empathy_call_window_get_audio_sink_pad (self);
2362         break;
2363       case TP_MEDIA_STREAM_TYPE_VIDEO:
2364         g_idle_add (empathy_call_window_show_video_output_cb, self);
2365         pad = empathy_call_window_get_video_sink_pad (self);
2366
2367         gst_pad_add_data_probe (src,
2368             G_CALLBACK (empathy_call_window_video_probe_cb), self);
2369         if (priv->got_video_src > 0)
2370           g_source_remove (priv->got_video_src);
2371         priv->got_video_src = g_timeout_add_seconds (5,
2372             empathy_call_window_check_video_cb, self);
2373         break;
2374       default:
2375         g_assert_not_reached ();
2376     }
2377
2378   if (pad == NULL)
2379     goto out;
2380
2381   if (GST_PAD_LINK_FAILED (gst_pad_link (src, pad)))
2382       g_warning ("Could not link %s sink pad",
2383           media_type == TP_MEDIA_STREAM_TYPE_AUDIO ? "audio" : "video");
2384   else
2385       retval = TRUE;
2386
2387   gst_object_unref (pad);
2388
2389  out:
2390
2391   /* If no sink could be linked, try to add fakesink to prevent the whole call
2392    * aborting */
2393
2394   if (!retval)
2395     {
2396       GstElement *fakesink = gst_element_factory_make ("fakesink", NULL);
2397
2398       if (gst_bin_add (GST_BIN (priv->pipeline), fakesink))
2399         {
2400           GstPad *sinkpad = gst_element_get_static_pad (fakesink, "sink");
2401           if (gst_element_set_state (fakesink, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE ||
2402               GST_PAD_LINK_FAILED (gst_pad_link (src, sinkpad)))
2403             {
2404               gst_element_set_locked_state (fakesink, TRUE);
2405               gst_element_set_state (fakesink, GST_STATE_NULL);
2406               gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2407             }
2408           else
2409             {
2410               DEBUG ("Could not link real sink, linked fakesink instead");
2411             }
2412           gst_object_unref (sinkpad);
2413         }
2414       else
2415         {
2416           gst_object_unref (fakesink);
2417         }
2418     }
2419
2420
2421   g_mutex_unlock (priv->lock);
2422
2423   return TRUE;
2424 }
2425
2426 static gboolean
2427 empathy_call_window_sink_added_cb (EmpathyCallHandler *handler,
2428   GstPad *sink, FsMediaType media_type, gpointer user_data)
2429 {
2430   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2431   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2432   GstPad *pad;
2433   gboolean retval = FALSE;
2434
2435   switch (media_type)
2436     {
2437       case FS_MEDIA_TYPE_AUDIO:
2438         if (!gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input))
2439           {
2440             g_warning ("Could not add audio source to pipeline");
2441             break;
2442           }
2443
2444         pad = gst_element_get_static_pad (priv->audio_input, "src");
2445         if (!pad)
2446           {
2447             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2448             g_warning ("Could not get source pad from audio source");
2449             break;
2450           }
2451
2452         if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
2453           {
2454             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2455             g_warning ("Could not link audio source to farsight");
2456             break;
2457           }
2458
2459         if (gst_element_set_state (priv->audio_input, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
2460           {
2461             g_warning ("Could not start audio source");
2462             gst_element_set_state (priv->audio_input, GST_STATE_NULL);
2463             gst_bin_remove (GST_BIN (priv->pipeline), priv->audio_input);
2464             break;
2465           }
2466
2467         retval = TRUE;
2468         break;
2469       case FS_MEDIA_TYPE_VIDEO:
2470         if (priv->video_tee != NULL)
2471           {
2472             pad = gst_element_get_request_pad (priv->video_tee, "src%d");
2473             if (GST_PAD_LINK_FAILED (gst_pad_link (pad, sink)))
2474               {
2475                 g_warning ("Could not link video source input pipeline");
2476                 break;
2477               }
2478             gst_object_unref (pad);
2479           }
2480
2481         retval = TRUE;
2482         break;
2483       default:
2484         g_assert_not_reached ();
2485     }
2486
2487   return retval;
2488 }
2489
2490 static void
2491 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
2492 {
2493   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2494   GstElement *preview;
2495
2496   disable_camera (self);
2497
2498   DEBUG ("remove video input");
2499   preview = priv->video_preview_sink;
2500
2501   gst_element_set_state (priv->video_input, GST_STATE_NULL);
2502   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
2503   gst_element_set_state (preview, GST_STATE_NULL);
2504
2505   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
2506     preview, NULL);
2507
2508   g_object_unref (priv->video_input);
2509   priv->video_input = NULL;
2510   g_object_unref (priv->video_tee);
2511   priv->video_tee = NULL;
2512   clutter_actor_destroy (priv->video_preview);
2513   priv->video_preview = NULL;
2514
2515   gtk_widget_set_sensitive (priv->camera_button, FALSE);
2516 }
2517
2518 static void
2519 start_call (EmpathyCallWindow *self)
2520 {
2521   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2522
2523   priv->call_started = TRUE;
2524   empathy_call_handler_start_call (priv->handler,
2525       gtk_get_current_event_time ());
2526
2527   if (empathy_call_handler_has_initial_video (priv->handler))
2528     {
2529       TpyCallChannel *call;
2530       TpySendingState s;
2531
2532       g_object_get (priv->handler, "call-channel", &call, NULL);
2533       s = tpy_call_channel_get_video_state (call);
2534
2535       if (s == TPY_SENDING_STATE_PENDING_SEND ||
2536           s == TPY_SENDING_STATE_SENDING)
2537         {
2538           /* Enable 'send video' buttons and display the preview */
2539           gtk_toggle_tool_button_set_active (
2540             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), TRUE);
2541         }
2542       else
2543         {
2544           gtk_toggle_tool_button_set_active (
2545             GTK_TOGGLE_TOOL_BUTTON (priv->camera_button), FALSE);
2546
2547           if (priv->video_preview == NULL)
2548             {
2549               create_video_preview (self);
2550               add_video_preview_to_pipeline (self);
2551             }
2552         }
2553
2554       g_object_unref (call);
2555     }
2556 }
2557
2558 static gboolean
2559 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
2560   gpointer user_data)
2561 {
2562   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
2563   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2564   GstState newstate;
2565
2566   empathy_call_handler_bus_message (priv->handler, bus, message);
2567
2568   switch (GST_MESSAGE_TYPE (message))
2569     {
2570       case GST_MESSAGE_STATE_CHANGED:
2571         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
2572           {
2573             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
2574           }
2575         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
2576             !priv->call_started)
2577           {
2578             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
2579             if (newstate == GST_STATE_PAUSED)
2580               {
2581                 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
2582                 priv->pipeline_playing = TRUE;
2583
2584                 if (priv->start_call_when_playing)
2585                   start_call (self);
2586               }
2587           }
2588         break;
2589       case GST_MESSAGE_ERROR:
2590         {
2591           GError *error = NULL;
2592           GstElement *gst_error;
2593           gchar *debug;
2594
2595           gst_message_parse_error (message, &error, &debug);
2596           gst_error = GST_ELEMENT (GST_MESSAGE_SRC (message));
2597
2598           g_message ("Element error: %s -- %s\n", error->message, debug);
2599
2600           if (g_str_has_prefix (gst_element_get_name (gst_error),
2601                 VIDEO_INPUT_ERROR_PREFIX))
2602             {
2603               /* Remove the video input and continue */
2604               if (priv->video_input != NULL)
2605                 empathy_call_window_remove_video_input (self);
2606               gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
2607             }
2608           else
2609             {
2610               empathy_call_window_disconnected (self, TRUE);
2611             }
2612           g_error_free (error);
2613           g_free (debug);
2614         }
2615       case GST_MESSAGE_UNKNOWN:
2616       case GST_MESSAGE_EOS:
2617       case GST_MESSAGE_WARNING:
2618       case GST_MESSAGE_INFO:
2619       case GST_MESSAGE_TAG:
2620       case GST_MESSAGE_BUFFERING:
2621       case GST_MESSAGE_STATE_DIRTY:
2622       case GST_MESSAGE_STEP_DONE:
2623       case GST_MESSAGE_CLOCK_PROVIDE:
2624       case GST_MESSAGE_CLOCK_LOST:
2625       case GST_MESSAGE_NEW_CLOCK:
2626       case GST_MESSAGE_STRUCTURE_CHANGE:
2627       case GST_MESSAGE_STREAM_STATUS:
2628       case GST_MESSAGE_APPLICATION:
2629       case GST_MESSAGE_ELEMENT:
2630       case GST_MESSAGE_SEGMENT_START:
2631       case GST_MESSAGE_SEGMENT_DONE:
2632       case GST_MESSAGE_DURATION:
2633       case GST_MESSAGE_ANY:
2634       default:
2635         break;
2636     }
2637
2638   return TRUE;
2639 }
2640
2641 static void
2642 empathy_call_window_members_changed_cb (TpyCallChannel *call,
2643     GHashTable *members,
2644     EmpathyCallWindow *self)
2645 {
2646   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2647   GHashTableIter iter;
2648   gpointer key, value;
2649   gboolean held = FALSE;
2650
2651   g_hash_table_iter_init (&iter, members);
2652   while (g_hash_table_iter_next (&iter, &key, &value))
2653     {
2654       if (GPOINTER_TO_INT (value) & TPY_CALL_MEMBER_FLAG_HELD)
2655         {
2656           /* This assumes this is a 1-1 call, otherwise one participant
2657            * putting the call on hold wouldn't mean the call is on hold
2658            * for everyone. */
2659           held = TRUE;
2660           break;
2661         }
2662     }
2663
2664   if (held)
2665     priv->call_state = HELD;
2666   else if (priv->call_state == HELD)
2667     priv->call_state = CONNECTED;
2668 }
2669
2670 static void
2671 call_handler_notify_call_cb (EmpathyCallHandler *handler,
2672     GParamSpec *spec,
2673     EmpathyCallWindow *self)
2674 {
2675   EmpathyCallWindowPriv *priv = GET_PRIV (self);
2676   TpyCallChannel *call;
2677
2678   g_object_get (priv->handler, "call-channel", &call, NULL);
2679   if (call == NULL)
2680     return;
2681
2682 /* FIXME
2683   tp_g_signal_connect_object (call, "audio-stream-error",
2684       G_CALLBACK (empathy_call_window_audio_stream_error), self, 0);
2685   tp_g_signal_connect_object (call, "video-stream-error",
2686       G_CALLBACK (empathy_call_window_video_stream_error), self, 0);
2687 */
2688
2689   tp_g_signal_connect_object (call, "members-changed",
2690       G_CALLBACK (empathy_call_window_members_changed_cb), self, 0);
2691
2692   g_object_unref (call);
2693 }
2694
2695 static void
2696 empathy_call_window_realized_cb (GtkWidget *widget, EmpathyCallWindow *window)
2697 {
2698   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2699   TpyCallChannel *call;
2700   gint width;
2701
2702   /* Make the hangup button twice as wide */
2703   width = gtk_widget_get_allocated_width (priv->hangup_button);
2704   gtk_widget_set_size_request (priv->hangup_button, width * 2, -1);
2705
2706   g_signal_connect (priv->handler, "state-changed",
2707     G_CALLBACK (empathy_call_window_state_changed_cb), window);
2708   g_signal_connect (priv->handler, "conference-added",
2709     G_CALLBACK (empathy_call_window_conference_added_cb), window);
2710   g_signal_connect (priv->handler, "conference-removed",
2711     G_CALLBACK (empathy_call_window_conference_removed_cb), window);
2712   g_signal_connect (priv->handler, "closed",
2713     G_CALLBACK (empathy_call_window_channel_closed_cb), window);
2714   g_signal_connect (priv->handler, "src-pad-added",
2715     G_CALLBACK (empathy_call_window_src_added_cb), window);
2716   g_signal_connect (priv->handler, "sink-pad-added",
2717     G_CALLBACK (empathy_call_window_sink_added_cb), window);
2718   g_signal_connect (priv->handler, "sink-pad-removed",
2719     G_CALLBACK (empathy_call_window_sink_removed_cb), window);
2720
2721   g_object_get (priv->handler, "call-channel", &call, NULL);
2722   if (call != NULL)
2723     {
2724       call_handler_notify_call_cb (priv->handler, NULL, window);
2725       g_object_unref (call);
2726     }
2727   else
2728     {
2729       /* call-channel doesn't exist yet, we'll connect signals once it has been
2730        * set */
2731       g_signal_connect (priv->handler, "notify::call-channel",
2732         G_CALLBACK (call_handler_notify_call_cb), window);
2733     }
2734
2735   gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
2736 }
2737
2738 static gboolean
2739 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
2740   EmpathyCallWindow *window)
2741 {
2742   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2743
2744   if (priv->pipeline != NULL)
2745     {
2746       if (priv->bus_message_source_id != 0)
2747         {
2748           g_source_remove (priv->bus_message_source_id);
2749           priv->bus_message_source_id = 0;
2750         }
2751
2752       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
2753     }
2754
2755   if (priv->call_state == CONNECTING)
2756     empathy_sound_manager_stop (priv->sound_mgr, EMPATHY_SOUND_PHONE_OUTGOING);
2757
2758   return FALSE;
2759 }
2760
2761 static void
2762 show_controls (EmpathyCallWindow *window, gboolean set_fullscreen)
2763 {
2764   GtkWidget *menu;
2765   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2766
2767   menu = gtk_ui_manager_get_widget (priv->ui_manager,
2768             "/menubar1");
2769
2770   if (set_fullscreen)
2771     {
2772       gtk_widget_hide (priv->dtmf_panel);
2773       gtk_widget_hide (menu);
2774       gtk_widget_hide (priv->toolbar);
2775     }
2776   else
2777     {
2778       if (priv->dialpad_was_visible_before_fs)
2779         gtk_widget_show (priv->dtmf_panel);
2780
2781       gtk_widget_show (menu);
2782       gtk_widget_show (priv->toolbar);
2783
2784       gtk_window_resize (GTK_WINDOW (window), priv->original_width_before_fs,
2785           priv->original_height_before_fs);
2786     }
2787 }
2788
2789 static void
2790 show_borders (EmpathyCallWindow *window, gboolean set_fullscreen)
2791 {
2792   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2793
2794   gtk_container_set_border_width (GTK_CONTAINER (priv->content_hbox),
2795       set_fullscreen ? 0 : CONTENT_HBOX_BORDER_WIDTH);
2796   gtk_box_set_spacing (GTK_BOX (priv->content_hbox),
2797       set_fullscreen ? 0 : CONTENT_HBOX_SPACING);
2798
2799   if (priv->video_output != NULL)
2800     {
2801 #if 0
2802       gtk_box_set_child_packing (GTK_BOX (priv->content_hbox),
2803           priv->video_output, TRUE, TRUE,
2804           set_fullscreen ? 0 : CONTENT_HBOX_CHILDREN_PACKING_PADDING,
2805           GTK_PACK_START);
2806 #endif
2807     }
2808 }
2809
2810 static gboolean
2811 empathy_call_window_state_event_cb (GtkWidget *widget,
2812   GdkEventWindowState *event, EmpathyCallWindow *window)
2813 {
2814   if (event->changed_mask & GDK_WINDOW_STATE_FULLSCREEN)
2815     {
2816       EmpathyCallWindowPriv *priv = GET_PRIV (window);
2817       gboolean set_fullscreen = event->new_window_state &
2818         GDK_WINDOW_STATE_FULLSCREEN;
2819
2820       if (set_fullscreen)
2821         {
2822           gboolean dialpad_was_visible;
2823           GtkAllocation allocation;
2824           gint original_width, original_height;
2825
2826           gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
2827           original_width = allocation.width;
2828           original_height = allocation.height;
2829
2830           g_object_get (priv->dtmf_panel,
2831               "visible", &dialpad_was_visible,
2832               NULL);
2833
2834           priv->dialpad_was_visible_before_fs = dialpad_was_visible;
2835           priv->original_width_before_fs = original_width;
2836           priv->original_height_before_fs = original_height;
2837
2838           if (priv->video_output_motion_handler_id == 0 &&
2839                 priv->video_output != NULL)
2840             {
2841               priv->video_output_motion_handler_id = g_signal_connect (
2842                   G_OBJECT (priv->video_container), "motion-notify-event",
2843                   G_CALLBACK (empathy_call_window_video_output_motion_notify),
2844                   window);
2845             }
2846         }
2847       else
2848         {
2849           disconnect_video_output_motion_handler (window);
2850         }
2851
2852       empathy_call_window_fullscreen_set_fullscreen (priv->fullscreen,
2853           set_fullscreen);
2854       show_controls (window, set_fullscreen);
2855       show_borders (window, set_fullscreen);
2856       gtk_action_set_stock_id (priv->menu_fullscreen,
2857           (set_fullscreen ? "gtk-leave-fullscreen" : "gtk-fullscreen"));
2858       priv->is_fullscreen = set_fullscreen;
2859   }
2860
2861   return FALSE;
2862 }
2863
2864 static void
2865 empathy_call_window_show_dialpad (EmpathyCallWindow *window,
2866     gboolean active)
2867 {
2868   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2869   int w, h, dialpad_width;
2870   GtkAllocation allocation;
2871
2872   gtk_widget_get_allocation (GTK_WIDGET (window), &allocation);
2873   w = allocation.width;
2874   h = allocation.height;
2875
2876   gtk_widget_get_preferred_width (priv->dtmf_panel, &dialpad_width, NULL);
2877
2878   if (active)
2879     {
2880       gtk_widget_show (priv->dtmf_panel);
2881       w += dialpad_width;
2882     }
2883   else
2884     {
2885       w -= dialpad_width;
2886       gtk_widget_hide (priv->dtmf_panel);
2887     }
2888
2889   if (w > 0 && h > 0)
2890     gtk_window_resize (GTK_WINDOW (window), w, h);
2891 }
2892
2893 static void
2894 empathy_call_window_set_send_video (EmpathyCallWindow *window,
2895   CameraState state)
2896 {
2897   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2898   TpyCallChannel *call;
2899
2900   priv->sending_video = (state == CAMERA_STATE_ON);
2901
2902   if (state == CAMERA_STATE_ON)
2903     {
2904       /* When we start sending video, we want to show the video preview by
2905          default. */
2906       display_video_preview (window, TRUE);
2907     }
2908   else
2909     {
2910       display_video_preview (window, FALSE);
2911     }
2912
2913   if (priv->call_state != CONNECTED)
2914     return;
2915
2916   g_object_get (priv->handler, "call-channel", &call, NULL);
2917   DEBUG ("%s sending video", priv->sending_video ? "start": "stop");
2918   tpy_call_channel_send_video (call, priv->sending_video);
2919   g_object_unref (call);
2920 }
2921
2922 static void
2923 empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
2924   EmpathyCallWindow *window)
2925 {
2926   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2927   gboolean active;
2928
2929   active = (gtk_toggle_tool_button_get_active (toggle));
2930
2931   if (active)
2932     {
2933       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
2934           priv->volume * 100);
2935     }
2936   else
2937     {
2938       /* TODO, Instead of setting the input volume to 0 we should probably
2939        * stop sending but this would cause the audio call to drop if both
2940        * sides mute at the same time on certain CMs AFAIK. Need to revisit this
2941        * in the future. GNOME #574574
2942        */
2943       g_settings_set_double (priv->settings, EMPATHY_PREFS_CALL_SOUND_VOLUME,
2944           0);
2945     }
2946 }
2947
2948 static void
2949 empathy_call_window_hangup_cb (gpointer object,
2950     EmpathyCallWindow *self)
2951 {
2952   empathy_call_handler_stop_call (self->priv->handler);
2953
2954   empathy_call_window_disconnected (self, TRUE);
2955 }
2956
2957 static void
2958 empathy_call_window_restart_call (EmpathyCallWindow *window)
2959 {
2960   EmpathyCallWindowPriv *priv = GET_PRIV (window);
2961
2962   /* Remove error info bars */
2963   gtk_container_forall (GTK_CONTAINER (priv->errors_vbox),
2964       (GtkCallback) gtk_widget_destroy, NULL);
2965
2966   create_video_output_widget (window);
2967
2968   /* While the call was disconnected, the input volume might have changed.
2969    * However, since the audio_input source was destroyed, its volume has not
2970    * been updated during that time. That's why we manually update it here */
2971   empathy_call_window_mic_volume_changed (window);
2972
2973   priv->outgoing = TRUE;
2974   empathy_call_window_set_state_connecting (window);
2975
2976   if (priv->pipeline_playing)
2977     start_call (window);
2978   else
2979     /* call will be started when the pipeline is ready */
2980     priv->start_call_when_playing = TRUE;
2981
2982   empathy_call_window_setup_avatars (window, priv->handler);
2983
2984   empathy_call_window_show_hangup_button (window, TRUE);
2985 }
2986
2987 static void
2988 empathy_call_window_dialpad_cb (GtkToggleToolButton *button,
2989     EmpathyCallWindow *window)
2990 {
2991   gboolean active;
2992
2993   active = gtk_toggle_tool_button_get_active (button);
2994
2995   empathy_call_window_show_dialpad (window, active);
2996 }
2997
2998 static void
2999 empathy_call_window_fullscreen_cb (gpointer object,
3000                                    EmpathyCallWindow *window)
3001 {
3002   empathy_call_window_fullscreen_toggle (window);
3003 }
3004
3005 static void
3006 empathy_call_window_fullscreen_toggle (EmpathyCallWindow *window)
3007 {
3008   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3009
3010   if (priv->is_fullscreen)
3011     gtk_window_unfullscreen (GTK_WINDOW (window));
3012   else
3013     gtk_window_fullscreen (GTK_WINDOW (window));
3014 }
3015
3016 static gboolean
3017 empathy_call_window_video_button_press_cb (GtkWidget *video_preview,
3018   GdkEventButton *event, EmpathyCallWindow *window)
3019 {
3020   if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3021     {
3022       empathy_call_window_video_menu_popup (window, event->button);
3023       return TRUE;
3024     }
3025
3026   return FALSE;
3027 }
3028
3029 static gboolean
3030 empathy_call_window_key_press_cb (GtkWidget *video_output,
3031   GdkEventKey *event, EmpathyCallWindow *window)
3032 {
3033   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3034
3035   if (priv->is_fullscreen && event->keyval == GDK_KEY_Escape)
3036     {
3037       /* Since we are in fullscreen mode, toggling will bring us back to
3038          normal mode. */
3039       empathy_call_window_fullscreen_toggle (window);
3040       return TRUE;
3041     }
3042
3043   return FALSE;
3044 }
3045
3046 static gboolean
3047 empathy_call_window_video_output_motion_notify (GtkWidget *widget,
3048     GdkEventMotion *event, EmpathyCallWindow *window)
3049 {
3050   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3051
3052   if (priv->is_fullscreen)
3053     {
3054       empathy_call_window_fullscreen_show_popup (priv->fullscreen);
3055       return TRUE;
3056     }
3057   return FALSE;
3058 }
3059
3060 static void
3061 empathy_call_window_video_menu_popup (EmpathyCallWindow *window,
3062   guint button)
3063 {
3064   GtkWidget *menu;
3065   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3066
3067   menu = gtk_ui_manager_get_widget (priv->ui_manager,
3068             "/video-popup");
3069   gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
3070       button, gtk_get_current_event_time ());
3071   gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
3072 }
3073
3074 static void
3075 empathy_call_window_status_message (EmpathyCallWindow *self,
3076   gchar *message)
3077 {
3078   gtk_label_set_label (GTK_LABEL (self->priv->status_label), message);
3079 }
3080
3081 static void
3082 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
3083   gdouble value, EmpathyCallWindow *window)
3084 {
3085   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3086
3087   if (priv->audio_output == NULL)
3088     return;
3089
3090   empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (priv->audio_output),
3091     value);
3092 }
3093
3094 GtkUIManager *
3095 empathy_call_window_get_ui_manager (EmpathyCallWindow *window)
3096 {
3097   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3098
3099   return priv->ui_manager;
3100 }
3101
3102 EmpathyGstAudioSrc *
3103 empathy_call_window_get_audio_src (EmpathyCallWindow *window)
3104 {
3105   EmpathyCallWindowPriv *priv = GET_PRIV (window);
3106
3107   return (EmpathyGstAudioSrc *) priv->audio_input;
3108 }