]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-call-utils.c
Enable echo cancellation if needed
[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
27 #include <telepathy-glib/telepathy-glib.h>
28
29 #include <telepathy-yell/telepathy-yell.h>
30
31 #include "empathy-call-utils.h"
32
33 #include <libempathy/empathy-gsettings.h>
34 #include <libempathy/empathy-request-util.h>
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
37 #include <libempathy/empathy-debug.h>
38
39 static const gchar *
40 get_error_display_message (GError *error)
41 {
42   if (error->domain != TP_ERROR)
43     return _("There was an error starting the call");
44
45   switch (error->code)
46     {
47       case TP_ERROR_NETWORK_ERROR:
48         return _("Network error");
49       case TP_ERROR_NOT_CAPABLE:
50         return _("The specified contact doesn't support calls");
51       case TP_ERROR_OFFLINE:
52         return _("The specified contact is offline");
53       case TP_ERROR_INVALID_HANDLE:
54         return _("The specified contact is not valid");
55       case TP_ERROR_EMERGENCY_CALLS_NOT_SUPPORTED:
56         return _("Emergency calls are not supported on this protocol");
57     }
58
59   return _("There was an error starting the call");
60 }
61
62 static void
63 show_call_error (GError *error)
64 {
65   GtkWidget *dialog;
66
67   dialog = gtk_message_dialog_new (NULL, 0,
68       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
69       "%s", get_error_display_message (error));
70
71   g_signal_connect_swapped (dialog, "response",
72       G_CALLBACK (gtk_widget_destroy),
73       dialog);
74
75   gtk_widget_show (dialog);
76 }
77
78 GHashTable *
79 empathy_call_create_call_request (const gchar *contact,
80     gboolean initial_audio,
81     gboolean initial_video)
82 {
83   return tp_asv_new (
84     TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
85       TPY_IFACE_CHANNEL_TYPE_CALL,
86     TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
87       TP_HANDLE_TYPE_CONTACT,
88     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
89       contact,
90     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN,
91       initial_audio,
92     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
93       initial_video,
94     NULL);
95 }
96
97 GHashTable *
98 empathy_call_create_streamed_media_request (const gchar *contact,
99     gboolean initial_audio,
100     gboolean initial_video)
101 {
102   return tp_asv_new (
103     TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
104       TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
105     TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
106       TP_HANDLE_TYPE_CONTACT,
107     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
108       contact,
109     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN,
110       initial_audio,
111     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
112       initial_video,
113     NULL);
114 }
115
116 static void
117 create_streamed_media_channel_cb (GObject *source,
118     GAsyncResult *result,
119     gpointer user_data)
120 {
121   GError *error = NULL;
122
123   if (!tp_account_channel_request_create_channel_finish (
124            TP_ACCOUNT_CHANNEL_REQUEST (source),
125            result,
126            &error))
127     {
128       DEBUG ("Failed to create StreamedMedia channel: %s", error->message);
129       show_call_error (error);
130       g_error_free (error);
131     }
132 }
133
134 static void
135 create_call_channel_cb (GObject *source,
136     GAsyncResult *result,
137     gpointer user_data)
138 {
139   TpAccountChannelRequest *streamed_media_req = user_data;
140   GError *error = NULL;
141
142   if (tp_account_channel_request_create_channel_finish (
143       TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
144     {
145       g_object_unref (streamed_media_req);
146       return;
147     }
148
149   DEBUG ("Failed to create Call channel: %s", error->message);
150
151   if (error->code != TP_ERROR_NOT_IMPLEMENTED)
152     {
153       show_call_error (error);
154       return;
155     }
156
157   DEBUG ("Let's try with an StreamedMedia channel");
158   g_error_free (error);
159   tp_account_channel_request_create_channel_async (streamed_media_req,
160       EMPATHY_AV_BUS_NAME, NULL,
161       create_streamed_media_channel_cb,
162       NULL);
163 }
164
165 void
166 empathy_call_new_with_streams (const gchar *contact,
167     TpAccount *account,
168     gboolean initial_audio,
169     gboolean initial_video,
170     gint64 timestamp)
171 {
172   GHashTable *call_request, *streamed_media_request;
173   TpAccountChannelRequest *call_req, *streamed_media_req;
174
175   call_request = empathy_call_create_call_request (contact,
176       initial_audio,
177       initial_video);
178
179   streamed_media_request = empathy_call_create_streamed_media_request (
180       contact, initial_audio, initial_video);
181
182   call_req = tp_account_channel_request_new (account, call_request, timestamp);
183   streamed_media_req = tp_account_channel_request_new (account,
184       streamed_media_request,
185       timestamp);
186
187   tp_account_channel_request_create_channel_async (call_req,
188       EMPATHY_CALL_BUS_NAME, NULL,
189       create_call_channel_cb,
190       streamed_media_req);
191
192   g_hash_table_unref (call_request);
193   g_hash_table_unref (streamed_media_request);
194   g_object_unref (call_req);
195 }
196
197 void
198 empathy_call_set_stream_properties (GstElement *element)
199 {
200   GstStructure *props;
201   GSettings *gsettings_call;
202   gboolean echo_cancellation;
203   gchar *tmp;
204
205   gsettings_call = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
206
207   echo_cancellation = g_settings_get_boolean (gsettings_call,
208       EMPATHY_PREFS_CALL_ECHO_CANCELLATION);
209
210   tmp = g_strdup_printf ("props,media.role=phone%s", echo_cancellation ?
211       ",filter.want=echo-cancel": "");
212
213   props = gst_structure_from_string (tmp, NULL);
214   g_object_set (element, "stream-properties", props, NULL);
215   gst_structure_free (props);
216
217   g_free (tmp);
218   g_object_unref (gsettings_call);
219 }