]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-call-utils.c
Merge branch 'gnome-3-4'
[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 "empathy-call-utils.h"
31
32 #include <libempathy/empathy-gsettings.h>
33 #include <libempathy/empathy-request-util.h>
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
36 #include <libempathy/empathy-debug.h>
37
38 static const gchar *
39 get_error_display_message (GError *error)
40 {
41   if (error->domain != TP_ERROR)
42     return _("There was an error starting the call");
43
44   switch (error->code)
45     {
46       case TP_ERROR_NETWORK_ERROR:
47         return _("Network error");
48       case TP_ERROR_NOT_CAPABLE:
49         return _("The specified contact doesn't support calls");
50       case TP_ERROR_OFFLINE:
51         return _("The specified contact is offline");
52       case TP_ERROR_INVALID_HANDLE:
53         return _("The specified contact is not valid");
54       case TP_ERROR_EMERGENCY_CALLS_NOT_SUPPORTED:
55         return _("Emergency calls are not supported on this protocol");
56       case TP_ERROR_INSUFFICIENT_BALANCE:
57         return _("You don't have enough credit in order to place this call");
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       TP_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     TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN,
92       initial_audio,
93     TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
94       initial_video,
95     NULL);
96 }
97
98 static void
99 create_call_channel_cb (GObject *source,
100     GAsyncResult *result,
101     gpointer user_data)
102 {
103   GError *error = NULL;
104
105   if (tp_account_channel_request_create_channel_finish (
106       TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
107     return;
108
109   DEBUG ("Failed to create Call channel: %s", error->message);
110
111   show_call_error (error);
112 }
113
114 /* Try to request a Call channel and fallback to StreamedMedia if that fails */
115 static void
116 call_new_with_streams (const gchar *contact,
117     TpAccount *account,
118     gboolean initial_audio,
119     gboolean initial_video,
120     gint64 timestamp)
121 {
122   GHashTable *call_request;
123   TpAccountChannelRequest *call_req;
124
125   /* Call */
126   call_request = empathy_call_create_call_request (contact,
127       initial_audio,
128       initial_video);
129
130   call_req = tp_account_channel_request_new (account, call_request, timestamp);
131
132   g_hash_table_unref (call_request);
133
134   tp_account_channel_request_create_channel_async (call_req,
135       EMPATHY_CALL_BUS_NAME, NULL, create_call_channel_cb, NULL);
136
137   g_object_unref (call_req);
138 }
139
140 void
141 empathy_call_new_with_streams (const gchar *contact,
142     TpAccount *account,
143     gboolean initial_audio,
144     gboolean initial_video,
145     gint64 timestamp)
146 {
147   call_new_with_streams (contact, account, initial_audio, initial_video,
148       timestamp);
149 }
150
151 void
152 empathy_call_set_stream_properties (GstElement *element,
153   gboolean echo_cancellation)
154 {
155   GstStructure *props;
156   GSettings *gsettings_call;
157   gboolean echo_cancellation_setting;
158
159   gsettings_call = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
160
161   echo_cancellation_setting = g_settings_get_boolean (gsettings_call,
162       EMPATHY_PREFS_CALL_ECHO_CANCELLATION);
163
164   DEBUG ("Echo cancellation: element allowed: %s, user enabled: %s",
165     echo_cancellation ? " yes" : "no",
166     echo_cancellation_setting ? " yes" : "no");
167
168
169   props = gst_structure_new ("props",
170       PA_PROP_MEDIA_ROLE, G_TYPE_STRING, "phone",
171       NULL);
172
173   if (echo_cancellation && echo_cancellation_setting)
174     {
175       gst_structure_set (props,
176           "filter.want", G_TYPE_STRING, "echo-cancel",
177           NULL);
178     }
179
180   g_object_set (element, "stream-properties", props, NULL);
181   gst_structure_free (props);
182
183   g_object_unref (gsettings_call);
184 }
185
186 /* Copied from telepathy-yell call-channel.c */
187 void
188 empathy_call_channel_send_video (TpCallChannel *self,
189     gboolean send)
190 {
191   GPtrArray *contents;
192   gboolean found = FALSE;
193   guint i;
194
195   g_return_if_fail (TP_IS_CALL_CHANNEL (self));
196
197   /* Loop over all the contents, if some of them a video set all their
198    * streams to sending, otherwise request a video channel in case we want to
199    * sent */
200   contents = tp_call_channel_get_contents (self);
201   for (i = 0 ; i < contents->len ; i++)
202     {
203       TpCallContent *content = g_ptr_array_index (contents, i);
204
205       if (tp_call_content_get_media_type (content) ==
206               TP_MEDIA_STREAM_TYPE_VIDEO)
207         {
208           GPtrArray *streams;
209           guint j;
210
211           found = TRUE;
212           streams = tp_call_content_get_streams (content);
213           for (j = 0; j < streams->len; j++)
214             {
215               TpCallStream *stream = g_ptr_array_index (streams, j);
216
217               tp_call_stream_set_sending_async (stream, send, NULL, NULL);
218             }
219         }
220     }
221
222   if (send && !found)
223     {
224       tp_call_channel_add_content_async (self, "video",
225           TP_MEDIA_STREAM_TYPE_VIDEO, TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL,
226           NULL, NULL);
227     }
228 }
229
230 /* Copied from telepathy-yell call-channel.c */
231 TpSendingState
232 empathy_call_channel_get_video_state (TpCallChannel *self)
233 {
234   TpSendingState result = TP_SENDING_STATE_NONE;
235   GPtrArray *contents;
236   guint i;
237
238   g_return_val_if_fail (TP_IS_CALL_CHANNEL (self), TP_SENDING_STATE_NONE);
239
240   contents = tp_call_channel_get_contents (self);
241   for (i = 0 ; i < contents->len ; i++)
242     {
243       TpCallContent *content = g_ptr_array_index (contents, i);
244
245       if (tp_call_content_get_media_type (content) ==
246               TP_MEDIA_STREAM_TYPE_VIDEO)
247         {
248           GPtrArray *streams;
249           guint j;
250
251           streams = tp_call_content_get_streams (content);
252           for (j = 0; j < streams->len; j++)
253             {
254               TpCallStream *stream = g_ptr_array_index (streams, j);
255               TpSendingState state;
256
257               state = tp_call_stream_get_local_sending_state (stream);
258               if (state != TP_SENDING_STATE_PENDING_STOP_SENDING &&
259                   state > result)
260                 result = state;
261             }
262         }
263     }
264
265   return result;
266 }