2 * Copyright (C) 2011 Collabora Ltd.
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.
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.
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
18 * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
23 #include <glib/gi18n.h>
26 #include <pulse/pulseaudio.h>
28 #include <telepathy-glib/telepathy-glib.h>
30 #include "empathy-call-utils.h"
32 #include <libempathy/empathy-gsettings.h>
33 #include <libempathy/empathy-request-util.h>
35 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
36 #include <libempathy/empathy-debug.h>
39 get_error_display_message (GError *error)
41 if (error->domain != TP_ERROR)
42 return _("There was an error starting the call");
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");
60 return _("There was an error starting the call");
64 show_call_error (GError *error)
68 dialog = gtk_message_dialog_new (NULL, 0,
69 GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
70 "%s", get_error_display_message (error));
72 g_signal_connect_swapped (dialog, "response",
73 G_CALLBACK (gtk_widget_destroy),
76 gtk_widget_show (dialog);
80 empathy_call_create_call_request (const gchar *contact,
81 gboolean initial_audio,
82 gboolean initial_video)
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,
91 TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN,
93 TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
99 empathy_call_create_streamed_media_request (const gchar *contact,
100 gboolean initial_audio,
101 gboolean initial_video)
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,
110 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN,
112 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
118 create_streamed_media_channel_cb (GObject *source,
119 GAsyncResult *result,
122 GError *error = NULL;
124 if (!tp_account_channel_request_create_channel_finish (
125 TP_ACCOUNT_CHANNEL_REQUEST (source),
129 DEBUG ("Failed to create StreamedMedia channel: %s", error->message);
130 show_call_error (error);
131 g_error_free (error);
137 create_call_channel_cb (GObject *source,
138 GAsyncResult *result,
141 TpAccountChannelRequest *streamed_media_req = user_data;
142 GError *error = NULL;
144 if (tp_account_channel_request_create_channel_finish (
145 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
147 g_object_unref (streamed_media_req);
151 DEBUG ("Failed to create Call channel: %s", error->message);
153 if (error->code != TP_ERROR_NOT_IMPLEMENTED)
155 show_call_error (error);
159 DEBUG ("Let's try with an StreamedMedia channel");
160 g_error_free (error);
161 tp_account_channel_request_create_channel_async (streamed_media_req,
162 EMPATHY_AV_BUS_NAME, NULL,
163 create_streamed_media_channel_cb,
167 /* Try to request a Call channel and fallback to StreamedMedia if that fails */
169 call_new_with_streams (const gchar *contact,
171 gboolean initial_audio,
172 gboolean initial_video,
175 GHashTable *call_request, *streamed_media_request;
176 TpAccountChannelRequest *call_req, *streamed_media_req;
179 call_request = empathy_call_create_call_request (contact,
183 call_req = tp_account_channel_request_new (account, call_request, timestamp);
185 g_hash_table_unref (call_request);
188 streamed_media_request = empathy_call_create_streamed_media_request (
189 contact, initial_audio, initial_video);
191 streamed_media_req = tp_account_channel_request_new (account,
192 streamed_media_request,
195 g_hash_table_unref (streamed_media_request);
197 tp_account_channel_request_create_channel_async (call_req,
198 EMPATHY_CALL_BUS_NAME, NULL,
199 create_call_channel_cb,
202 g_object_unref (call_req);
205 #else /* HAVE_CALL */
208 sm_new_with_streams (const gchar *contact,
210 gboolean initial_audio,
211 gboolean initial_video,
214 GHashTable *streamed_media_request;
215 TpAccountChannelRequest *streamed_media_req;
218 streamed_media_request = empathy_call_create_streamed_media_request (
219 contact, initial_audio, initial_video);
221 streamed_media_req = tp_account_channel_request_new (account,
222 streamed_media_request,
225 g_hash_table_unref (streamed_media_request);
227 tp_account_channel_request_create_channel_async (streamed_media_req,
228 EMPATHY_AV_BUS_NAME, NULL, create_streamed_media_channel_cb, NULL);
230 g_object_unref (streamed_media_req);
232 #endif /* HAVE_CALL */
235 empathy_call_new_with_streams (const gchar *contact,
237 gboolean initial_audio,
238 gboolean initial_video,
242 call_new_with_streams (contact, account, initial_audio, initial_video,
245 sm_new_with_streams (contact, account, initial_audio, initial_video,
251 empathy_call_set_stream_properties (GstElement *element,
252 gboolean echo_cancellation)
255 GSettings *gsettings_call;
256 gboolean echo_cancellation_setting;
258 gsettings_call = g_settings_new (EMPATHY_PREFS_CALL_SCHEMA);
260 echo_cancellation_setting = g_settings_get_boolean (gsettings_call,
261 EMPATHY_PREFS_CALL_ECHO_CANCELLATION);
263 DEBUG ("Echo cancellation: element allowed: %s, user enabled: %s",
264 echo_cancellation ? " yes" : "no",
265 echo_cancellation_setting ? " yes" : "no");
268 props = gst_structure_new ("props",
269 PA_PROP_MEDIA_ROLE, G_TYPE_STRING, "phone",
272 if (echo_cancellation && echo_cancellation_setting)
274 gst_structure_set (props,
275 "filter.want", G_TYPE_STRING, "echo-cancel",
279 g_object_set (element, "stream-properties", props, NULL);
280 gst_structure_free (props);
282 g_object_unref (gsettings_call);
285 /* Copied from telepathy-yell call-channel.c */
287 empathy_call_channel_send_video (TpCallChannel *self,
291 gboolean found = FALSE;
294 g_return_if_fail (TP_IS_CALL_CHANNEL (self));
296 /* Loop over all the contents, if some of them a video set all their
297 * streams to sending, otherwise request a video channel in case we want to
299 contents = tp_call_channel_get_contents (self);
300 for (i = 0 ; i < contents->len ; i++)
302 TpCallContent *content = g_ptr_array_index (contents, i);
304 if (tp_call_content_get_media_type (content) ==
305 TP_MEDIA_STREAM_TYPE_VIDEO)
311 streams = tp_call_content_get_streams (content);
312 for (j = 0; j < streams->len; j++)
314 TpCallStream *stream = g_ptr_array_index (streams, j);
316 tp_call_stream_set_sending_async (stream, send, NULL, NULL);
323 tp_call_channel_add_content_async (self, "video",
324 TP_MEDIA_STREAM_TYPE_VIDEO, TP_MEDIA_STREAM_DIRECTION_BIDIRECTIONAL,
329 /* Copied from telepathy-yell call-channel.c */
331 empathy_call_channel_get_video_state (TpCallChannel *self)
333 TpSendingState result = TP_SENDING_STATE_NONE;
337 g_return_val_if_fail (TP_IS_CALL_CHANNEL (self), TP_SENDING_STATE_NONE);
339 contents = tp_call_channel_get_contents (self);
340 for (i = 0 ; i < contents->len ; i++)
342 TpCallContent *content = g_ptr_array_index (contents, i);
344 if (tp_call_content_get_media_type (content) ==
345 TP_MEDIA_STREAM_TYPE_VIDEO)
350 streams = tp_call_content_get_streams (content);
351 for (j = 0; j < streams->len; j++)
353 TpCallStream *stream = g_ptr_array_index (streams, j);
354 TpSendingState state;
356 state = tp_call_stream_get_local_sending_state (stream);
357 if (state != TP_SENDING_STATE_PENDING_STOP_SENDING &&