]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-call-utils.c
individual_view_drag_end: remove the auto scroll
[empathy.git] / libempathy-gtk / empathy-call-utils.c
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
4  * The code contained in this file is free software; you can redistribute
5  * it and/or modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either version
7  * 2.1 of the License, or (at your option) any later version.
8  *
9  * This file is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this code; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
19  */
20
21 #include "config.h"
22
23 #include <glib/gi18n.h>
24
25 #include <gtk/gtk.h>
26 #include <pulse/pulseaudio.h>
27
28 #include <telepathy-glib/telepathy-glib.h>
29
30 #include <telepathy-yell/telepathy-yell.h>
31
32 #include "empathy-call-utils.h"
33
34 #include <libempathy/empathy-gsettings.h>
35 #include <libempathy/empathy-request-util.h>
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
38 #include <libempathy/empathy-debug.h>
39
40 static const gchar *
41 get_error_display_message (GError *error)
42 {
43   if (error->domain != TP_ERROR)
44     return _("There was an error starting the call");
45
46   switch (error->code)
47     {
48       case TP_ERROR_NETWORK_ERROR:
49         return _("Network error");
50       case TP_ERROR_NOT_CAPABLE:
51         return _("The specified contact doesn't support calls");
52       case TP_ERROR_OFFLINE:
53         return _("The specified contact is offline");
54       case TP_ERROR_INVALID_HANDLE:
55         return _("The specified contact is not valid");
56       case TP_ERROR_EMERGENCY_CALLS_NOT_SUPPORTED:
57         return _("Emergency calls are not supported on this protocol");
58       case TP_ERROR_INSUFFICIENT_BALANCE:
59         return _("You don't have enough credit in order to place this call");
60     }
61
62   return _("There was an error starting the call");
63 }
64
65 static void
66 show_call_error (GError *error)
67 {
68   GtkWidget *dialog;
69
70   dialog = gtk_message_dialog_new (NULL, 0,
71       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
72       "%s", get_error_display_message (error));
73
74   g_signal_connect_swapped (dialog, "response",
75       G_CALLBACK (gtk_widget_destroy),
76       dialog);
77
78   gtk_widget_show (dialog);
79 }
80
81 GHashTable *
82 empathy_call_create_call_request (const gchar *contact,
83     gboolean initial_audio,
84     gboolean initial_video)
85 {
86   return tp_asv_new (
87     TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
88       TPY_IFACE_CHANNEL_TYPE_CALL,
89     TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
90       TP_HANDLE_TYPE_CONTACT,
91     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
92       contact,
93     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN,
94       initial_audio,
95     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
96       initial_video,
97     NULL);
98 }
99
100 GHashTable *
101 empathy_call_create_streamed_media_request (const gchar *contact,
102     gboolean initial_audio,
103     gboolean initial_video)
104 {
105   return tp_asv_new (
106     TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
107       TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
108     TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
109       TP_HANDLE_TYPE_CONTACT,
110     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
111       contact,
112     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN,
113       initial_audio,
114     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
115       initial_video,
116     NULL);
117 }
118
119 static void
120 create_streamed_media_channel_cb (GObject *source,
121     GAsyncResult *result,
122     gpointer user_data)
123 {
124   GError *error = NULL;
125
126   if (!tp_account_channel_request_create_channel_finish (
127            TP_ACCOUNT_CHANNEL_REQUEST (source),
128            result,
129            &error))
130     {
131       DEBUG ("Failed to create StreamedMedia channel: %s", error->message);
132       show_call_error (error);
133       g_error_free (error);
134     }
135 }
136
137 #ifdef HAVE_CALL
138 static void
139 create_call_channel_cb (GObject *source,
140     GAsyncResult *result,
141     gpointer user_data)
142 {
143   TpAccountChannelRequest *streamed_media_req = user_data;
144   GError *error = NULL;
145
146   if (tp_account_channel_request_create_channel_finish (
147       TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
148     {
149       g_object_unref (streamed_media_req);
150       return;
151     }
152
153   DEBUG ("Failed to create Call channel: %s", error->message);
154
155   if (error->code != TP_ERROR_NOT_IMPLEMENTED)
156     {
157       show_call_error (error);
158       return;
159     }
160
161   DEBUG ("Let's try with an StreamedMedia channel");
162   g_error_free (error);
163   tp_account_channel_request_create_channel_async (streamed_media_req,
164       EMPATHY_AV_BUS_NAME, NULL,
165       create_streamed_media_channel_cb,
166       NULL);
167 }
168
169 /* Try to request a Call channel and fallback to StreamedMedia if that fails */
170 static void
171 call_new_with_streams (const gchar *contact,
172     TpAccount *account,
173     gboolean initial_audio,
174     gboolean initial_video,
175     gint64 timestamp)
176 {
177   GHashTable *call_request, *streamed_media_request;
178   TpAccountChannelRequest *call_req, *streamed_media_req;
179
180   /* Call */
181   call_request = empathy_call_create_call_request (contact,
182       initial_audio,
183       initial_video);
184
185   call_req = tp_account_channel_request_new (account, call_request, timestamp);
186
187   g_hash_table_unref (call_request);
188
189   /* StreamedMedia */
190   streamed_media_request = empathy_call_create_streamed_media_request (
191       contact, initial_audio, initial_video);
192
193   streamed_media_req = tp_account_channel_request_new (account,
194       streamed_media_request,
195       timestamp);
196
197   g_hash_table_unref (streamed_media_request);
198
199   tp_account_channel_request_create_channel_async (call_req,
200       EMPATHY_CALL_BUS_NAME, NULL,
201       create_call_channel_cb,
202       streamed_media_req);
203
204   g_object_unref (call_req);
205 }
206
207 #else /* HAVE_CALL */
208
209 static void
210 sm_new_with_streams (const gchar *contact,
211     TpAccount *account,
212     gboolean initial_audio,
213     gboolean initial_video,
214     gint64 timestamp)
215 {
216   GHashTable *streamed_media_request;
217   TpAccountChannelRequest *streamed_media_req;
218
219   /* StreamedMedia */
220   streamed_media_request = empathy_call_create_streamed_media_request (
221       contact, initial_audio, initial_video);
222
223   streamed_media_req = tp_account_channel_request_new (account,
224       streamed_media_request,
225       timestamp);
226
227   g_hash_table_unref (streamed_media_request);
228
229   tp_account_channel_request_create_channel_async (streamed_media_req,
230       EMPATHY_AV_BUS_NAME, NULL, create_streamed_media_channel_cb, NULL);
231
232   g_object_unref (streamed_media_req);
233 }
234 #endif /* HAVE_CALL */
235
236 void
237 empathy_call_new_with_streams (const gchar *contact,
238     TpAccount *account,
239     gboolean initial_audio,
240     gboolean initial_video,
241     gint64 timestamp)
242 {
243 #ifdef HAVE_CALL
244   call_new_with_streams (contact, account, initial_audio, initial_video,
245       timestamp);
246 #else
247   sm_new_with_streams (contact, account, initial_audio, initial_video,
248       timestamp);
249 #endif
250 }
251
252 void
253 empathy_call_set_stream_properties (GstElement *element,
254   gboolean echo_cancellation)
255 {
256   GstStructure *props;
257   GSettings *gsettings_call;
258   gboolean echo_cancellation_setting;
259
260   gsettings_call = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
261
262   echo_cancellation_setting = g_settings_get_boolean (gsettings_call,
263       EMPATHY_PREFS_CALL_ECHO_CANCELLATION);
264
265   DEBUG ("Echo cancellation: element allowed: %s, user enabled: %s",
266     echo_cancellation ? " yes" : "no",
267     echo_cancellation_setting ? " yes" : "no");
268
269
270   props = gst_structure_new ("props",
271       PA_PROP_MEDIA_ROLE, G_TYPE_STRING, "phone",
272       NULL);
273
274   if (echo_cancellation && echo_cancellation_setting)
275     {
276       gst_structure_set (props,
277           "filter.want", G_TYPE_STRING, "echo-cancel",
278           NULL);
279     }
280
281   g_object_set (element, "stream-properties", props, NULL);
282   gst_structure_free (props);
283
284   g_object_unref (gsettings_call);
285 }