]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-call-utils.c
Merge branch 'blockade'
[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     }
59
60   return _("There was an error starting the call");
61 }
62
63 static void
64 show_call_error (GError *error)
65 {
66   GtkWidget *dialog;
67
68   dialog = gtk_message_dialog_new (NULL, 0,
69       GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
70       "%s", get_error_display_message (error));
71
72   g_signal_connect_swapped (dialog, "response",
73       G_CALLBACK (gtk_widget_destroy),
74       dialog);
75
76   gtk_widget_show (dialog);
77 }
78
79 GHashTable *
80 empathy_call_create_call_request (const gchar *contact,
81     gboolean initial_audio,
82     gboolean initial_video)
83 {
84   return tp_asv_new (
85     TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
86       TPY_IFACE_CHANNEL_TYPE_CALL,
87     TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
88       TP_HANDLE_TYPE_CONTACT,
89     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
90       contact,
91     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN,
92       initial_audio,
93     TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
94       initial_video,
95     NULL);
96 }
97
98 GHashTable *
99 empathy_call_create_streamed_media_request (const gchar *contact,
100     gboolean initial_audio,
101     gboolean initial_video)
102 {
103   return tp_asv_new (
104     TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
105       TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
106     TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
107       TP_HANDLE_TYPE_CONTACT,
108     TP_PROP_CHANNEL_TARGET_ID, G_TYPE_STRING,
109       contact,
110     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN,
111       initial_audio,
112     TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
113       initial_video,
114     NULL);
115 }
116
117 static void
118 create_streamed_media_channel_cb (GObject *source,
119     GAsyncResult *result,
120     gpointer user_data)
121 {
122   GError *error = NULL;
123
124   if (!tp_account_channel_request_create_channel_finish (
125            TP_ACCOUNT_CHANNEL_REQUEST (source),
126            result,
127            &error))
128     {
129       DEBUG ("Failed to create StreamedMedia channel: %s", error->message);
130       show_call_error (error);
131       g_error_free (error);
132     }
133 }
134
135 static void
136 create_call_channel_cb (GObject *source,
137     GAsyncResult *result,
138     gpointer user_data)
139 {
140   TpAccountChannelRequest *streamed_media_req = user_data;
141   GError *error = NULL;
142
143   if (tp_account_channel_request_create_channel_finish (
144       TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
145     {
146       g_object_unref (streamed_media_req);
147       return;
148     }
149
150   DEBUG ("Failed to create Call channel: %s", error->message);
151
152   if (error->code != TP_ERROR_NOT_IMPLEMENTED)
153     {
154       show_call_error (error);
155       return;
156     }
157
158   DEBUG ("Let's try with an StreamedMedia channel");
159   g_error_free (error);
160   tp_account_channel_request_create_channel_async (streamed_media_req,
161       EMPATHY_AV_BUS_NAME, NULL,
162       create_streamed_media_channel_cb,
163       NULL);
164 }
165
166 void
167 empathy_call_new_with_streams (const gchar *contact,
168     TpAccount *account,
169     gboolean initial_audio,
170     gboolean initial_video,
171     gint64 timestamp)
172 {
173   GHashTable *call_request, *streamed_media_request;
174   TpAccountChannelRequest *call_req, *streamed_media_req;
175
176   call_request = empathy_call_create_call_request (contact,
177       initial_audio,
178       initial_video);
179
180   streamed_media_request = empathy_call_create_streamed_media_request (
181       contact, initial_audio, initial_video);
182
183   call_req = tp_account_channel_request_new (account, call_request, timestamp);
184   streamed_media_req = tp_account_channel_request_new (account,
185       streamed_media_request,
186       timestamp);
187
188   tp_account_channel_request_create_channel_async (call_req,
189       EMPATHY_CALL_BUS_NAME, NULL,
190       create_call_channel_cb,
191       streamed_media_req);
192
193   g_hash_table_unref (call_request);
194   g_hash_table_unref (streamed_media_request);
195   g_object_unref (call_req);
196 }
197
198 void
199 empathy_call_set_stream_properties (GstElement *element)
200 {
201   GstStructure *props;
202   GSettings *gsettings_call;
203   gboolean echo_cancellation;
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   props = gst_structure_new ("props",
211       PA_PROP_MEDIA_ROLE, G_TYPE_STRING, "phone",
212       NULL);
213
214   if (echo_cancellation)
215     {
216       gst_structure_set (props,
217           "filter.want", G_TYPE_STRING, "echo-cancel",
218           NULL);
219     }
220
221   g_object_set (element, "stream-properties", props, NULL);
222   gst_structure_free (props);
223
224   g_object_unref (gsettings_call);
225 }