]> git.0d.be Git - empathy.git/blob - src/empathy-call-window.c
Port EmpathyMainWindow to new API
[empathy.git] / src / empathy-call-window.c
1 /*
2  * empathy-call-window.c - Source for EmpathyCallWindow
3  * Copyright (C) 2008 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 <gst/gst.h>
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30
31 #include <telepathy-farsight/channel.h>
32
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy-gtk/empathy-video-widget.h>
35 #include <libempathy-gtk/empathy-audio-src.h>
36 #include <libempathy-gtk/empathy-audio-sink.h>
37 #include <libempathy-gtk/empathy-video-src.h>
38 #include <libempathy-gtk/empathy-ui-utils.h>
39
40 #include "empathy-call-window.h"
41
42 #include "empathy-sidebar.h"
43
44 #define BUTTON_ID "empathy-call-dtmf-button-id"
45
46 G_DEFINE_TYPE(EmpathyCallWindow, empathy_call_window, GTK_TYPE_WINDOW)
47
48 /* signal enum */
49 #if 0
50 enum
51 {
52     LAST_SIGNAL
53 };
54
55 static guint signals[LAST_SIGNAL] = {0};
56 #endif
57
58 enum {
59   PROP_CALL_HANDLER = 1,
60 };
61
62 /* private structure */
63 typedef struct _EmpathyCallWindowPriv EmpathyCallWindowPriv;
64
65 struct _EmpathyCallWindowPriv
66 {
67   gboolean dispose_has_run;
68   EmpathyCallHandler *handler;
69
70   gboolean connected;
71
72   GtkWidget *video_output;
73   GtkWidget *video_preview;
74   GtkWidget *sidebar;
75   GtkWidget *sidebar_button;
76   GtkWidget *statusbar;
77   GtkWidget *volume_button;
78   GtkWidget *mic_button;
79   GtkWidget *camera_button;
80   GtkWidget *toolbar;
81   GtkWidget *hangup;
82   GtkWidget *pane;
83
84   gdouble volume;
85   GtkAdjustment *audio_input_adj;
86
87   GtkWidget *dtmf_panel;
88
89   GstElement *video_input;
90   GstElement *audio_input;
91   GstElement *audio_output;
92   GstElement *pipeline;
93   GstElement *video_tee;
94
95   GstElement *funnel;
96   GstElement *liveadder;
97
98   guint context_id;
99
100   GTimer *timer;
101   guint timer_id;
102
103   GtkWidget *video_contrast;
104   GtkWidget *video_brightness;
105   GtkWidget *video_gamma;
106
107   GMutex *lock;
108   gboolean call_started;
109 };
110
111 #define GET_PRIV(o) \
112   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CALL_WINDOW, \
113     EmpathyCallWindowPriv))
114
115 static void empathy_call_window_realized_cb (GtkWidget *widget,
116   EmpathyCallWindow *window);
117
118 static gboolean empathy_call_window_delete_cb (GtkWidget *widget,
119   GdkEvent*event, EmpathyCallWindow *window);
120
121 static void empathy_call_window_sidebar_toggled_cb (GtkToggleButton *toggle,
122   EmpathyCallWindow *window);
123
124 static void empathy_call_window_camera_toggled_cb (GtkToggleToolButton *toggle,
125   EmpathyCallWindow *window);
126
127 static void empathy_call_window_mic_toggled_cb (
128   GtkToggleToolButton *toggle, EmpathyCallWindow *window);
129
130 static void empathy_call_window_sidebar_hidden_cb (EmpathySidebar *sidebar,
131   EmpathyCallWindow *window);
132
133 static void empathy_call_window_hangup (EmpathyCallWindow *window);
134
135 static void empathy_call_window_status_message (EmpathyCallWindow *window,
136   gchar *message);
137
138 static gboolean empathy_call_window_bus_message (GstBus *bus,
139   GstMessage *message, gpointer user_data);
140
141 static void
142 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
143   gdouble value, EmpathyCallWindow *window);
144
145 static void
146 empathy_call_window_setup_menubar (EmpathyCallWindow *self)
147 {
148   EmpathyCallWindowPriv *priv = GET_PRIV (self);
149
150   g_signal_connect_swapped (priv->hangup, "activate",
151     G_CALLBACK (empathy_call_window_hangup), self);
152 }
153
154 static void
155 empathy_call_window_setup_toolbar (EmpathyCallWindow *self)
156 {
157   EmpathyCallWindowPriv *priv = GET_PRIV (self);
158   GtkToolItem *tool_item;
159
160   g_signal_connect_swapped (priv->hangup, "clicked",
161     G_CALLBACK (empathy_call_window_hangup), self);
162
163   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (priv->mic_button),
164       TRUE);
165
166   g_signal_connect (G_OBJECT (priv->mic_button), "toggled",
167     G_CALLBACK (empathy_call_window_mic_toggled_cb), self);
168
169   /* Add an empty expanded GtkToolItem so the volume button is at the end of
170    * the toolbar. */
171   tool_item = gtk_tool_item_new ();
172   gtk_tool_item_set_expand (tool_item, TRUE);
173   gtk_widget_show (GTK_WIDGET (tool_item));
174   gtk_toolbar_insert (GTK_TOOLBAR (priv->toolbar), tool_item, -1);
175
176   priv->volume_button = gtk_volume_button_new ();
177   /* FIXME listen to the audiosinks signals and update the button according to
178    * that, for now starting out at 1.0 and assuming only the app changes the
179    * volume will do */
180   gtk_scale_button_set_value (GTK_SCALE_BUTTON (priv->volume_button), 1.0);
181   g_signal_connect (G_OBJECT (priv->volume_button), "value-changed",
182     G_CALLBACK (empathy_call_window_volume_changed_cb), self);
183
184   tool_item = gtk_tool_item_new ();
185   gtk_container_add (GTK_CONTAINER (tool_item), priv->volume_button);
186   gtk_widget_show_all (GTK_WIDGET (tool_item));
187   gtk_toolbar_insert (GTK_TOOLBAR (priv->toolbar), tool_item, -1);
188
189   gtk_toggle_tool_button_set_active (GTK_TOGGLE_TOOL_BUTTON (priv->camera_button),
190       FALSE);
191   gtk_widget_set_sensitive (priv->camera_button, FALSE);
192
193   g_signal_connect (priv->camera_button, "toggled",
194     G_CALLBACK (empathy_call_window_camera_toggled_cb), self);
195 }
196
197 static void
198 dtmf_button_pressed_cb (GtkButton *button, EmpathyCallWindow *window)
199 {
200   EmpathyCallWindowPriv *priv = GET_PRIV (window);
201   EmpathyTpCall *call;
202   GQuark button_quark;
203   TpDTMFEvent event;
204
205   g_object_get (priv->handler, "tp-call", &call, NULL);
206
207   button_quark = g_quark_from_static_string (BUTTON_ID);
208   event = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (button),
209     button_quark));
210
211   empathy_tp_call_start_tone (call, event);
212
213   g_object_unref (call);
214 }
215
216 static void
217 dtmf_button_released_cb (GtkButton *button, EmpathyCallWindow *window)
218 {
219   EmpathyCallWindowPriv *priv = GET_PRIV (window);
220   EmpathyTpCall *call;
221
222   g_object_get (priv->handler, "tp-call", &call, NULL);
223
224   empathy_tp_call_stop_tone (call);
225
226   g_object_unref (call);
227 }
228
229 static GtkWidget *
230 empathy_call_window_create_dtmf (EmpathyCallWindow *self)
231 {
232   GtkWidget *table;
233   int i;
234   GQuark button_quark;
235   struct {
236     gchar *label;
237     TpDTMFEvent event;
238   } dtmfbuttons[] = { { "1", TP_DTMF_EVENT_DIGIT_1 },
239                       { "2", TP_DTMF_EVENT_DIGIT_2 },
240                       { "3", TP_DTMF_EVENT_DIGIT_3 },
241                       { "4", TP_DTMF_EVENT_DIGIT_4 },
242                       { "5", TP_DTMF_EVENT_DIGIT_5 },
243                       { "6", TP_DTMF_EVENT_DIGIT_6 },
244                       { "7", TP_DTMF_EVENT_DIGIT_7 },
245                       { "8", TP_DTMF_EVENT_DIGIT_8 },
246                       { "9", TP_DTMF_EVENT_DIGIT_9 },
247                       { "#", TP_DTMF_EVENT_HASH },
248                       { "0", TP_DTMF_EVENT_DIGIT_0 },
249                       { "*", TP_DTMF_EVENT_ASTERISK },
250                       { NULL, } };
251
252   button_quark = g_quark_from_static_string (BUTTON_ID);
253
254   table = gtk_table_new (4, 3, TRUE);
255
256   for (i = 0; dtmfbuttons[i].label != NULL; i++)
257     {
258       GtkWidget *button = gtk_button_new_with_label (dtmfbuttons[i].label);
259       gtk_table_attach (GTK_TABLE (table), button, i % 3, i % 3 + 1,
260         i/3, i/3 + 1, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 1, 1);
261
262       g_object_set_qdata (G_OBJECT (button), button_quark,
263         GUINT_TO_POINTER (dtmfbuttons[i].event));
264
265       g_signal_connect (G_OBJECT (button), "pressed",
266         G_CALLBACK (dtmf_button_pressed_cb), self);
267       g_signal_connect (G_OBJECT (button), "released",
268         G_CALLBACK (dtmf_button_released_cb), self);
269     }
270
271   return table;
272 }
273
274 static GtkWidget *
275 empathy_call_window_create_video_input_add_slider (EmpathyCallWindow *self,
276   gchar *label_text, GtkWidget *bin)
277 {
278    GtkWidget *vbox = gtk_vbox_new (FALSE, 2);
279    GtkWidget *scale = gtk_vscale_new_with_range (0, 100, 10);
280    GtkWidget *label = gtk_label_new (label_text);
281
282    gtk_widget_set_sensitive (scale, FALSE);
283
284    gtk_container_add (GTK_CONTAINER (bin), vbox);
285
286    gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
287    gtk_box_pack_start (GTK_BOX (vbox), scale, TRUE, TRUE, 0);
288    gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
289
290    return scale;
291 }
292
293 static void
294 empathy_call_window_video_contrast_changed_cb (GtkAdjustment *adj,
295   EmpathyCallWindow *self)
296
297 {
298   EmpathyCallWindowPriv *priv = GET_PRIV (self);
299
300   empathy_video_src_set_channel (priv->video_input,
301     EMPATHY_GST_VIDEO_SRC_CHANNEL_CONTRAST, gtk_adjustment_get_value (adj));
302 }
303
304 static void
305 empathy_call_window_video_brightness_changed_cb (GtkAdjustment *adj,
306   EmpathyCallWindow *self)
307
308 {
309   EmpathyCallWindowPriv *priv = GET_PRIV (self);
310
311   empathy_video_src_set_channel (priv->video_input,
312     EMPATHY_GST_VIDEO_SRC_CHANNEL_BRIGHTNESS, gtk_adjustment_get_value (adj));
313 }
314
315 static void
316 empathy_call_window_video_gamma_changed_cb (GtkAdjustment *adj,
317   EmpathyCallWindow *self)
318
319 {
320   EmpathyCallWindowPriv *priv = GET_PRIV (self);
321
322   empathy_video_src_set_channel (priv->video_input,
323     EMPATHY_GST_VIDEO_SRC_CHANNEL_GAMMA, gtk_adjustment_get_value (adj));
324 }
325
326
327 static GtkWidget *
328 empathy_call_window_create_video_input (EmpathyCallWindow *self)
329 {
330   EmpathyCallWindowPriv *priv = GET_PRIV (self);
331   GtkWidget *hbox;
332
333   hbox = gtk_hbox_new (TRUE, 3);
334
335   priv->video_contrast = empathy_call_window_create_video_input_add_slider (
336     self,  _("Contrast"), hbox);
337
338   priv->video_brightness = empathy_call_window_create_video_input_add_slider (
339     self,  _("Brightness"), hbox);
340
341   priv->video_gamma = empathy_call_window_create_video_input_add_slider (
342     self,  _("Gamma"), hbox);
343
344   return hbox;
345 }
346
347 static void
348 empathy_call_window_setup_video_input (EmpathyCallWindow *self)
349 {
350   EmpathyCallWindowPriv *priv = GET_PRIV (self);
351   guint supported;
352   GtkAdjustment *adj;
353
354   supported = empathy_video_src_get_supported_channels (priv->video_input);
355
356   if (supported & EMPATHY_GST_VIDEO_SRC_SUPPORTS_CONTRAST)
357     {
358       adj = gtk_range_get_adjustment (GTK_RANGE (priv->video_contrast));
359
360       gtk_adjustment_set_value (adj,
361         empathy_video_src_get_channel (priv->video_input,
362           EMPATHY_GST_VIDEO_SRC_CHANNEL_CONTRAST));
363
364       g_signal_connect (G_OBJECT (adj), "value-changed",
365         G_CALLBACK (empathy_call_window_video_contrast_changed_cb), self);
366
367       gtk_widget_set_sensitive (priv->video_contrast, TRUE);
368     }
369
370   if (supported & EMPATHY_GST_VIDEO_SRC_SUPPORTS_BRIGHTNESS)
371     {
372       adj = gtk_range_get_adjustment (GTK_RANGE (priv->video_brightness));
373
374       gtk_adjustment_set_value (adj,
375         empathy_video_src_get_channel (priv->video_input,
376           EMPATHY_GST_VIDEO_SRC_CHANNEL_BRIGHTNESS));
377
378       g_signal_connect (G_OBJECT (adj), "value-changed",
379         G_CALLBACK (empathy_call_window_video_brightness_changed_cb), self);
380       gtk_widget_set_sensitive (priv->video_brightness, TRUE);
381     }
382
383   if (supported & EMPATHY_GST_VIDEO_SRC_SUPPORTS_GAMMA)
384     {
385       adj = gtk_range_get_adjustment (GTK_RANGE (priv->video_gamma));
386
387       gtk_adjustment_set_value (adj,
388         empathy_video_src_get_channel (priv->video_input,
389           EMPATHY_GST_VIDEO_SRC_CHANNEL_GAMMA));
390
391       g_signal_connect (G_OBJECT (adj), "value-changed",
392         G_CALLBACK (empathy_call_window_video_gamma_changed_cb), self);
393       gtk_widget_set_sensitive (priv->video_gamma, TRUE);
394     }
395 }
396
397 static void
398 empathy_call_window_mic_volume_changed_cb (GtkAdjustment *adj,
399   EmpathyCallWindow *self)
400 {
401   EmpathyCallWindowPriv *priv = GET_PRIV (self);
402   gdouble volume;
403
404   volume =  gtk_adjustment_get_value (adj)/100.0;
405
406   /* Don't store the volume because of muting */
407   if (volume > 0 || gtk_toggle_tool_button_get_active (
408         GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
409     priv->volume = volume;
410
411   /* Ensure that the toggle button is active if the volume is > 0 and inactive
412    * if it's smaller then 0 */
413   if ((volume > 0) != gtk_toggle_tool_button_get_active (
414         GTK_TOGGLE_TOOL_BUTTON (priv->mic_button)))
415     gtk_toggle_tool_button_set_active (
416       GTK_TOGGLE_TOOL_BUTTON (priv->mic_button), volume > 0);
417
418   empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (priv->audio_input),
419     volume);
420 }
421
422 static void
423 empathy_call_window_audio_input_level_changed_cb (EmpathyGstAudioSrc *src,
424   gdouble level, GtkProgressBar *bar)
425 {
426   gdouble value;
427
428   value = CLAMP (pow (10, level / 20), 0.0, 1.0);
429   gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), value);
430 }
431
432 static GtkWidget *
433 empathy_call_window_create_audio_input (EmpathyCallWindow *self)
434 {
435   EmpathyCallWindowPriv *priv = GET_PRIV (self);
436   GtkWidget *hbox, *vbox, *scale, *progress, *label;
437   GtkAdjustment *adj;
438
439   hbox = gtk_hbox_new (TRUE, 3);
440
441   vbox = gtk_vbox_new (FALSE, 3);
442   gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 3);
443
444   scale = gtk_vscale_new_with_range (0, 150, 100);
445   gtk_range_set_inverted (GTK_RANGE (scale), TRUE);
446   label = gtk_label_new (_("Volume"));
447
448   priv->audio_input_adj = adj = gtk_range_get_adjustment (GTK_RANGE (scale));
449   priv->volume =  empathy_audio_src_get_volume (EMPATHY_GST_AUDIO_SRC
450     (priv->audio_input));
451   gtk_adjustment_set_value (adj, priv->volume * 100);
452
453   g_signal_connect (G_OBJECT (adj), "value-changed",
454     G_CALLBACK (empathy_call_window_mic_volume_changed_cb), self);
455
456   gtk_box_pack_start (GTK_BOX (vbox), scale, TRUE, TRUE, 3);
457   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 3);
458
459   progress = gtk_progress_bar_new ();
460   gtk_progress_bar_set_orientation (GTK_PROGRESS_BAR (progress),
461     GTK_PROGRESS_BOTTOM_TO_TOP);
462   gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress), 0);
463
464   g_signal_connect (priv->audio_input, "peak-level-changed",
465     G_CALLBACK (empathy_call_window_audio_input_level_changed_cb), progress);
466
467   gtk_box_pack_start (GTK_BOX (hbox), progress, FALSE, FALSE, 3);
468
469   return hbox;
470 }
471
472 static void
473 empathy_call_window_init (EmpathyCallWindow *self)
474 {
475   EmpathyCallWindowPriv *priv = GET_PRIV (self);
476   GtkBuilder *gui;
477   GtkWidget *vbox, *top_vbox;
478   GtkWidget *hbox, *h;
479   GtkWidget *arrow;
480   GtkWidget *page;
481   GstBus *bus;
482   gchar *filename;
483
484   filename = empathy_file_lookup ("empathy-call-window.ui", "src");
485   gui = empathy_builder_get_file (filename,
486     "call_window_vbox", &top_vbox,
487     "pane", &priv->pane,
488     "statusbar", &priv->statusbar,
489     "microphone", &priv->mic_button,
490     "camera", &priv->camera_button,
491     "toolbar", &priv->toolbar,
492     "hangup", &priv->hangup,
493     NULL);
494
495   priv->lock = g_mutex_new ();
496
497   gtk_container_add (GTK_CONTAINER (self), top_vbox);
498
499   empathy_call_window_setup_menubar (self);
500   empathy_call_window_setup_toolbar (self);
501
502   priv->pipeline = gst_pipeline_new (NULL);
503
504   hbox = gtk_hbox_new (FALSE, 3);
505   gtk_container_set_border_width (GTK_CONTAINER (hbox), 6);
506   gtk_paned_pack1 (GTK_PANED (priv->pane), hbox, TRUE, FALSE);
507
508   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
509
510   gst_bus_add_watch (bus, empathy_call_window_bus_message, self);
511
512   priv->video_output = empathy_video_widget_new (bus);
513   gtk_box_pack_start (GTK_BOX (hbox), priv->video_output, TRUE, TRUE, 3);
514
515   priv->video_tee = gst_element_factory_make ("tee", NULL);
516   gst_object_ref (priv->video_tee);
517   gst_object_sink (priv->video_tee);
518
519   vbox = gtk_vbox_new (FALSE, 3);
520   gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 3);
521
522   priv->video_preview = empathy_video_widget_new_with_size (bus, 160, 120);
523   g_object_set (priv->video_preview, "sync", FALSE, "async", TRUE, NULL);
524   gtk_box_pack_start (GTK_BOX (vbox), priv->video_preview, FALSE, FALSE, 0);
525
526   priv->video_input = empathy_video_src_new ();
527   gst_object_ref (priv->video_input);
528   gst_object_sink (priv->video_input);
529
530   priv->audio_input = empathy_audio_src_new ();
531   gst_object_ref (priv->audio_input);
532   gst_object_sink (priv->audio_input);
533
534   priv->audio_output = empathy_audio_sink_new ();
535   gst_object_ref (priv->audio_output);
536   gst_object_sink (priv->audio_output);
537
538   g_object_unref (bus);
539
540   priv->sidebar_button = gtk_toggle_button_new_with_mnemonic (_("_Sidebar"));
541   arrow = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
542   g_signal_connect (G_OBJECT (priv->sidebar_button), "toggled",
543     G_CALLBACK (empathy_call_window_sidebar_toggled_cb), self);
544
545   gtk_button_set_image (GTK_BUTTON (priv->sidebar_button), arrow);
546
547   h = gtk_hbox_new (FALSE, 3);
548   gtk_box_pack_end (GTK_BOX (vbox), h, FALSE, FALSE, 3);
549   gtk_box_pack_end (GTK_BOX (h), priv->sidebar_button, FALSE, FALSE, 3);
550
551   priv->sidebar = empathy_sidebar_new ();
552   g_signal_connect (G_OBJECT (priv->sidebar),
553     "hide", G_CALLBACK (empathy_call_window_sidebar_hidden_cb),
554     self);
555   gtk_paned_pack2 (GTK_PANED (priv->pane), priv->sidebar, FALSE, FALSE);
556
557   priv->dtmf_panel = empathy_call_window_create_dtmf (self);
558   empathy_sidebar_add_page (EMPATHY_SIDEBAR (priv->sidebar), _("Dialpad"),
559     priv->dtmf_panel);
560
561   gtk_widget_set_sensitive (priv->dtmf_panel, FALSE);
562
563   page = empathy_call_window_create_audio_input (self);
564   empathy_sidebar_add_page (EMPATHY_SIDEBAR (priv->sidebar), _("Audio input"),
565     page);
566
567   page = empathy_call_window_create_video_input (self);
568   empathy_sidebar_add_page (EMPATHY_SIDEBAR (priv->sidebar), _("Video input"),
569     page);
570
571   gtk_widget_show_all (top_vbox);
572
573   gtk_widget_hide (priv->sidebar);
574
575   g_signal_connect (G_OBJECT (self), "realize",
576     G_CALLBACK (empathy_call_window_realized_cb), self);
577
578   g_signal_connect (G_OBJECT (self), "delete-event",
579     G_CALLBACK (empathy_call_window_delete_cb), self);
580
581   empathy_call_window_status_message (self, _("Connecting..."));
582
583   priv->timer = g_timer_new ();
584
585   g_object_unref (gui);
586 }
587
588 static void empathy_call_window_dispose (GObject *object);
589 static void empathy_call_window_finalize (GObject *object);
590
591 static void
592 empathy_call_window_set_property (GObject *object,
593   guint property_id, const GValue *value, GParamSpec *pspec)
594 {
595   EmpathyCallWindowPriv *priv = GET_PRIV (object);
596
597   switch (property_id)
598     {
599       case PROP_CALL_HANDLER:
600         priv->handler = g_value_dup_object (value);
601         break;
602       default:
603         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
604     }
605 }
606
607 static void
608 empathy_call_window_get_property (GObject *object,
609   guint property_id, GValue *value, GParamSpec *pspec)
610 {
611   EmpathyCallWindowPriv *priv = GET_PRIV (object);
612
613   switch (property_id)
614     {
615       case PROP_CALL_HANDLER:
616         g_value_set_object (value, priv->handler);
617         break;
618       default:
619         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
620     }
621 }
622
623 static void
624 empathy_call_window_class_init (
625   EmpathyCallWindowClass *empathy_call_window_class)
626 {
627   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_window_class);
628   GParamSpec *param_spec;
629
630   g_type_class_add_private (empathy_call_window_class,
631     sizeof (EmpathyCallWindowPriv));
632
633   object_class->set_property = empathy_call_window_set_property;
634   object_class->get_property = empathy_call_window_get_property;
635
636   object_class->dispose = empathy_call_window_dispose;
637   object_class->finalize = empathy_call_window_finalize;
638
639   param_spec = g_param_spec_object ("handler",
640     "handler", "The call handler",
641     EMPATHY_TYPE_CALL_HANDLER,
642     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
643   g_object_class_install_property (object_class,
644     PROP_CALL_HANDLER, param_spec);
645
646 }
647
648 void
649 empathy_call_window_dispose (GObject *object)
650 {
651   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
652   EmpathyCallWindowPriv *priv = GET_PRIV (self);
653
654   if (priv->dispose_has_run)
655     return;
656
657   priv->dispose_has_run = TRUE;
658
659   if (priv->handler != NULL)
660     g_object_unref (priv->handler);
661
662   priv->handler = NULL;
663
664   if (priv->pipeline != NULL)
665     g_object_unref (priv->pipeline);
666   priv->pipeline = NULL;
667
668   if (priv->video_input != NULL)
669     g_object_unref (priv->video_input);
670   priv->video_input = NULL;
671
672   if (priv->audio_input != NULL)
673     g_object_unref (priv->audio_input);
674   priv->audio_input = NULL;
675
676   if (priv->audio_output != NULL)
677     g_object_unref (priv->audio_output);
678   priv->audio_output = NULL;
679
680   if (priv->video_tee != NULL)
681     g_object_unref (priv->video_tee);
682   priv->video_tee = NULL;
683
684   if (priv->timer_id != 0)
685     g_source_remove (priv->timer_id);
686   priv->timer_id = 0;
687
688   /* release any references held by the object here */
689   if (G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose)
690     G_OBJECT_CLASS (empathy_call_window_parent_class)->dispose (object);
691 }
692
693 void
694 empathy_call_window_finalize (GObject *object)
695 {
696   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (object);
697   EmpathyCallWindowPriv *priv = GET_PRIV (self);
698
699   /* free any data held directly by the object here */
700   g_mutex_free (priv->lock);
701
702   g_timer_destroy (priv->timer);
703
704   G_OBJECT_CLASS (empathy_call_window_parent_class)->finalize (object);
705 }
706
707
708 EmpathyCallWindow *
709 empathy_call_window_new (EmpathyCallHandler *handler)
710 {
711   return EMPATHY_CALL_WINDOW (
712     g_object_new (EMPATHY_TYPE_CALL_WINDOW, "handler", handler, NULL));
713 }
714
715 static void
716 empathy_call_window_conference_added_cb (EmpathyCallHandler *handler,
717   GstElement *conference, gpointer user_data)
718 {
719   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
720   EmpathyCallWindowPriv *priv = GET_PRIV (self);
721
722   gst_bin_add (GST_BIN (priv->pipeline), conference);
723
724   gst_element_set_state (conference, GST_STATE_PLAYING);
725 }
726
727 static gboolean
728 empathy_call_window_request_resource_cb (EmpathyCallHandler *handler,
729   FsMediaType type, FsStreamDirection direction, gpointer user_data)
730 {
731   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
732   EmpathyCallWindowPriv *priv = GET_PRIV (self);
733
734   if (type != TP_MEDIA_STREAM_TYPE_VIDEO)
735     return TRUE;
736
737   if (direction == FS_DIRECTION_RECV)
738     return TRUE;
739
740   /* video and direction is send */
741   return priv->video_input != NULL;
742 }
743
744 static void
745 empathy_call_window_disconnected (EmpathyCallWindow *self)
746 {
747   EmpathyCallWindowPriv *priv = GET_PRIV (self);
748
749   g_mutex_lock (priv->lock);
750
751   g_timer_stop (priv->timer);
752
753   if (priv->timer_id != 0)
754     g_source_remove (priv->timer_id);
755   priv->timer_id = 0;
756
757   g_mutex_unlock (priv->lock);
758
759   empathy_call_window_status_message (self, _("Disconnected"));
760
761   gtk_widget_set_sensitive (priv->camera_button, FALSE);
762 }
763
764
765 static void
766 empathy_call_window_channel_closed_cb (TfChannel *channel, gpointer user_data)
767 {
768   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
769
770   empathy_call_window_disconnected (self);
771 }
772
773 /* Called with global lock held */
774 static GstPad *
775 empathy_call_window_get_video_sink_pad (EmpathyCallWindow *self)
776 {
777   EmpathyCallWindowPriv *priv = GET_PRIV (self);
778   GstPad *pad;
779
780   if (priv->funnel == NULL)
781     {
782       GstElement *output;
783
784       output = empathy_video_widget_get_element (EMPATHY_VIDEO_WIDGET
785         (priv->video_output));
786
787       priv->funnel = gst_element_factory_make ("fsfunnel", NULL);
788
789       gst_bin_add (GST_BIN (priv->pipeline), priv->funnel);
790       gst_bin_add (GST_BIN (priv->pipeline), output);
791
792       gst_element_link (priv->funnel, output);
793
794       gst_element_set_state (priv->funnel, GST_STATE_PLAYING);
795       gst_element_set_state (output, GST_STATE_PLAYING);
796     }
797
798   pad = gst_element_get_request_pad (priv->funnel, "sink%d");
799
800   return pad;
801 }
802
803 /* Called with global lock held */
804 static GstPad *
805 empathy_call_window_get_audio_sink_pad (EmpathyCallWindow *self)
806 {
807   EmpathyCallWindowPriv *priv = GET_PRIV (self);
808   GstPad *pad;
809
810   if (priv->liveadder == NULL)
811     {
812       priv->liveadder = gst_element_factory_make ("liveadder", NULL);
813
814       gst_bin_add (GST_BIN (priv->pipeline), priv->liveadder);
815       gst_bin_add (GST_BIN (priv->pipeline), priv->audio_output);
816
817       gst_element_link (priv->liveadder, priv->audio_output);
818
819       gst_element_set_state (priv->liveadder, GST_STATE_PLAYING);
820       gst_element_set_state (priv->audio_output, GST_STATE_PLAYING);
821     }
822
823   pad = gst_element_get_request_pad (priv->liveadder, "sink%d");
824
825   return pad;
826 }
827
828 static gboolean
829 empathy_call_window_update_timer (gpointer user_data)
830 {
831   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
832   EmpathyCallWindowPriv *priv = GET_PRIV (self);
833   gchar *str;
834   gdouble time;
835
836   time = g_timer_elapsed (priv->timer, NULL);
837
838   /* Translators: number of minutes:seconds the caller has been connected */
839   str = g_strdup_printf (_("Connected â€” %d:%02dm"), (int) time / 60,
840     (int) time % 60);
841   empathy_call_window_status_message (self, str);
842   g_free (str);
843
844   return TRUE;
845 }
846
847 static gboolean
848 empathy_call_window_connected (gpointer user_data)
849 {
850   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
851   EmpathyCallWindowPriv *priv = GET_PRIV (self);
852   EmpathyTpCall *call;
853
854   g_object_get (priv->handler, "tp-call", &call, NULL);
855
856   if (empathy_tp_call_has_dtmf (call))
857     gtk_widget_set_sensitive (priv->dtmf_panel, TRUE);
858
859   if (priv->video_input != NULL)
860     gtk_widget_set_sensitive (priv->camera_button, TRUE);
861
862   g_object_unref (call);
863
864   g_mutex_lock (priv->lock);
865
866   priv->timer_id = g_timeout_add_seconds (1,
867     empathy_call_window_update_timer, self);
868
869   g_mutex_unlock (priv->lock);
870
871   empathy_call_window_update_timer (self);
872
873   return FALSE;
874 }
875
876
877 /* Called from the streaming thread */
878 static void
879 empathy_call_window_src_added_cb (EmpathyCallHandler *handler,
880   GstPad *src, guint media_type, gpointer user_data)
881 {
882   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
883   EmpathyCallWindowPriv *priv = GET_PRIV (self);
884
885   GstPad *pad;
886
887   g_mutex_lock (priv->lock);
888
889   if (priv->connected == FALSE)
890     {
891       g_timer_start (priv->timer);
892       priv->timer_id = g_idle_add  (empathy_call_window_connected, self);
893       priv->connected = TRUE;
894     }
895
896   switch (media_type)
897     {
898       case TP_MEDIA_STREAM_TYPE_AUDIO:
899         pad = empathy_call_window_get_audio_sink_pad (self);
900         break;
901       case TP_MEDIA_STREAM_TYPE_VIDEO:
902         pad = empathy_call_window_get_video_sink_pad (self);
903         break;
904       default:
905         g_assert_not_reached ();
906     }
907
908   gst_pad_link (src, pad);
909   gst_object_unref (pad);
910
911   g_mutex_unlock (priv->lock);
912 }
913
914 /* Called from the streaming thread */
915 static void
916 empathy_call_window_sink_added_cb (EmpathyCallHandler *handler,
917   GstPad *sink, guint media_type, gpointer user_data)
918 {
919   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
920   EmpathyCallWindowPriv *priv = GET_PRIV (self);
921   GstPad *pad;
922
923   switch (media_type)
924     {
925       case TP_MEDIA_STREAM_TYPE_AUDIO:
926         gst_bin_add (GST_BIN (priv->pipeline), priv->audio_input);
927
928         pad = gst_element_get_static_pad (priv->audio_input, "src");
929         gst_pad_link (pad, sink);
930
931         gst_element_set_state (priv->audio_input, GST_STATE_PLAYING);
932         break;
933       case TP_MEDIA_STREAM_TYPE_VIDEO:
934         if (priv->video_input != NULL)
935           {
936             pad =  gst_element_get_request_pad (priv->video_tee, "src%d");
937             gst_pad_link (pad, sink);
938           }
939         break;
940       default:
941         g_assert_not_reached ();
942     }
943
944 }
945
946 static gboolean
947 empathy_gst_bin_has_child (GstBin *bin, GstElement *element)
948 {
949   GstIterator *it;
950   gboolean ret = FALSE;
951   GstElement *item;
952
953   it = gst_bin_iterate_recurse (bin);
954
955   for (;;)
956     {
957       switch (gst_iterator_next (it, (gpointer *)&item))
958        {
959          case GST_ITERATOR_OK:
960            if (item == element)
961             {
962               gst_object_unref (GST_OBJECT (item));
963               ret = TRUE;
964               goto out;
965             }
966            gst_object_unref (GST_OBJECT (item));
967            break;
968          case GST_ITERATOR_RESYNC:
969            gst_iterator_resync (it);
970            break;
971         case GST_ITERATOR_ERROR:
972            g_assert_not_reached ();
973            /* fallthrough */
974         case GST_ITERATOR_DONE:
975            goto out;
976            break;
977       }
978     }
979     gst_iterator_free (it);
980
981 out:
982   return ret;
983 }
984
985 static void
986 empathy_call_window_remove_video_input (EmpathyCallWindow *self)
987 {
988   EmpathyCallWindowPriv *priv = GET_PRIV (self);
989   GstElement *preview;
990
991   preview = empathy_video_widget_get_element (
992     EMPATHY_VIDEO_WIDGET (priv->video_preview));
993
994   gst_element_set_state (priv->video_input, GST_STATE_NULL);
995   gst_element_set_state (priv->video_tee, GST_STATE_NULL);
996   gst_element_set_state (preview, GST_STATE_NULL);
997
998   gst_bin_remove_many (GST_BIN (priv->pipeline), priv->video_input,
999     priv->video_tee, preview, NULL);
1000
1001   g_object_unref (priv->video_input);
1002   priv->video_input = NULL;
1003   g_object_unref (priv->video_tee);
1004   priv->video_tee = NULL;
1005 }
1006
1007
1008 static gboolean
1009 empathy_call_window_bus_message (GstBus *bus, GstMessage *message,
1010   gpointer user_data)
1011 {
1012   EmpathyCallWindow *self = EMPATHY_CALL_WINDOW (user_data);
1013   EmpathyCallWindowPriv *priv = GET_PRIV (self);
1014   GstState newstate;
1015
1016   empathy_call_handler_bus_message (priv->handler, bus, message);
1017
1018   switch (GST_MESSAGE_TYPE (message))
1019     {
1020       case GST_MESSAGE_STATE_CHANGED:
1021         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->video_input))
1022           {
1023             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
1024             if (newstate == GST_STATE_PAUSED)
1025                 empathy_call_window_setup_video_input (self);
1026           }
1027         if (GST_MESSAGE_SRC (message) == GST_OBJECT (priv->pipeline) &&
1028             !priv->call_started)
1029           {
1030             gst_message_parse_state_changed (message, NULL, &newstate, NULL);
1031             if (newstate == GST_STATE_PAUSED)
1032               {
1033                 priv->call_started = TRUE;
1034                 empathy_call_handler_start_call (priv->handler);
1035                 gst_element_set_state (priv->pipeline, GST_STATE_PLAYING);
1036               }
1037           }
1038         break;
1039       case GST_MESSAGE_ERROR:
1040         {
1041           GError *error;
1042           gchar *debug;
1043
1044           gst_message_parse_error (message, &error, &debug);
1045
1046           g_message ("Element error: %s -- %s\n", error->message, debug);
1047
1048           if (priv->video_input != NULL &&
1049               empathy_gst_bin_has_child (GST_BIN (priv->video_input),
1050                 GST_ELEMENT (GST_MESSAGE_SRC (message))))
1051             {
1052               /* Remove the video input and continue */
1053               empathy_call_window_remove_video_input (self);
1054               gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1055             }
1056           else
1057             {
1058               gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1059               empathy_call_window_disconnected (self);
1060             }
1061           g_error_free (error);
1062           g_free (debug);
1063         }
1064       default:
1065         break;
1066     }
1067
1068   return TRUE;
1069 }
1070
1071 static void
1072 empathy_call_window_realized_cb (GtkWidget *widget, EmpathyCallWindow *window)
1073 {
1074   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1075   GstElement *preview;
1076
1077   g_signal_connect (priv->handler, "conference-added",
1078     G_CALLBACK (empathy_call_window_conference_added_cb), window);
1079   g_signal_connect (priv->handler, "request-resource",
1080     G_CALLBACK (empathy_call_window_request_resource_cb), window);
1081   g_signal_connect (priv->handler, "closed",
1082     G_CALLBACK (empathy_call_window_channel_closed_cb), window);
1083   g_signal_connect (priv->handler, "src-pad-added",
1084     G_CALLBACK (empathy_call_window_src_added_cb), window);
1085   g_signal_connect (priv->handler, "sink-pad-added",
1086     G_CALLBACK (empathy_call_window_sink_added_cb), window);
1087
1088
1089   preview = empathy_video_widget_get_element (
1090     EMPATHY_VIDEO_WIDGET (priv->video_preview));
1091
1092   gst_bin_add_many (GST_BIN (priv->pipeline), priv->video_input,
1093     priv->video_tee, preview, NULL);
1094   gst_element_link_many (priv->video_input, priv->video_tee,
1095     preview, NULL);
1096
1097   gst_element_set_state (priv->pipeline, GST_STATE_PAUSED);
1098 }
1099
1100 static gboolean
1101 empathy_call_window_delete_cb (GtkWidget *widget, GdkEvent*event,
1102   EmpathyCallWindow *window)
1103 {
1104   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1105
1106   gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1107
1108   return FALSE;
1109 }
1110
1111 static void
1112 empathy_call_window_sidebar_toggled_cb (GtkToggleButton *toggle,
1113   EmpathyCallWindow *window)
1114 {
1115   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1116   GtkWidget *arrow;
1117   int w,h, handle_size;
1118
1119   w = GTK_WIDGET (window)->allocation.width;
1120   h = GTK_WIDGET (window)->allocation.height;
1121
1122   gtk_widget_style_get (priv->pane, "handle_size", &handle_size, NULL);
1123
1124   if (gtk_toggle_button_get_active (toggle))
1125     {
1126       arrow = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_NONE);
1127       gtk_widget_show (priv->sidebar);
1128       w += priv->sidebar->allocation.width + handle_size;
1129     }
1130   else
1131     {
1132       arrow = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
1133       w -= priv->sidebar->allocation.width + handle_size;
1134       gtk_widget_hide (priv->sidebar);
1135     }
1136
1137   gtk_button_set_image (GTK_BUTTON (priv->sidebar_button), arrow);
1138
1139   if (w > 0 && h > 0)
1140     gtk_window_resize (GTK_WINDOW (window), w, h);
1141 }
1142
1143 static void
1144 empathy_call_window_camera_toggled_cb (GtkToggleToolButton *toggle,
1145   EmpathyCallWindow *window)
1146 {
1147   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1148   gboolean active;
1149   EmpathyTpCall *call;
1150
1151   active = (gtk_toggle_tool_button_get_active (toggle));
1152
1153   g_object_get (priv->handler, "tp-call", &call, NULL);
1154
1155   empathy_tp_call_request_video_stream_direction (call, active);
1156
1157   g_object_unref (call);
1158 }
1159
1160 static void
1161 empathy_call_window_mic_toggled_cb (GtkToggleToolButton *toggle,
1162   EmpathyCallWindow *window)
1163 {
1164   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1165   gboolean active;
1166
1167   active = (gtk_toggle_tool_button_get_active (toggle));
1168
1169   if (active)
1170     {
1171       empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (priv->audio_input),
1172         priv->volume);
1173       gtk_adjustment_set_value (priv->audio_input_adj, priv->volume * 100);
1174     }
1175   else
1176     {
1177       /* TODO, Instead of setting the input volume to 0 we should probably
1178        * stop sending but this would cause the audio call to drop if both
1179        * sides mute at the same time on certain CMs AFAIK. Need to revisit this
1180        * in the future. GNOME #574574
1181        */
1182       empathy_audio_src_set_volume (EMPATHY_GST_AUDIO_SRC (priv->audio_input),
1183         0);
1184       gtk_adjustment_set_value (priv->audio_input_adj, 0);
1185     }
1186 }
1187
1188 static void
1189 empathy_call_window_sidebar_hidden_cb (EmpathySidebar *sidebar,
1190   EmpathyCallWindow *window)
1191 {
1192   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1193
1194   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->sidebar_button),
1195     FALSE);
1196 }
1197
1198 static void
1199 empathy_call_window_hangup (EmpathyCallWindow *window)
1200 {
1201   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1202
1203   gst_element_set_state (priv->pipeline, GST_STATE_NULL);
1204   gtk_widget_destroy (GTK_WIDGET (window));
1205 }
1206
1207 static void
1208 empathy_call_window_status_message (EmpathyCallWindow *window,
1209   gchar *message)
1210 {
1211   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1212
1213   if (priv->context_id == 0)
1214     {
1215       priv->context_id = gtk_statusbar_get_context_id (
1216         GTK_STATUSBAR (priv->statusbar), "voip call status messages");
1217     }
1218   else
1219     {
1220       gtk_statusbar_pop (GTK_STATUSBAR (priv->statusbar), priv->context_id);
1221     }
1222
1223   gtk_statusbar_push (GTK_STATUSBAR (priv->statusbar), priv->context_id,
1224     message);
1225 }
1226
1227 static void
1228 empathy_call_window_volume_changed_cb (GtkScaleButton *button,
1229   gdouble value, EmpathyCallWindow *window)
1230 {
1231   EmpathyCallWindowPriv *priv = GET_PRIV (window);
1232
1233   empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (priv->audio_output),
1234     value);
1235 }