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