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