2 * empathy-call-factory.c - Source for EmpathyCallFactory
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <telepathy-glib/account-channel-request.h>
26 #include <telepathy-glib/simple-handler.h>
27 #include <telepathy-glib/interfaces.h>
28 #include <telepathy-glib/util.h>
30 #include "empathy-dispatcher.h"
31 #include "empathy-marshal.h"
32 #include "empathy-call-factory.h"
33 #include "empathy-utils.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include <libempathy/empathy-debug.h>
38 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, G_TYPE_OBJECT)
40 static void handle_channels_cb (TpSimpleHandler *handler,
42 TpConnection *connection,
44 GList *requests_satisfied,
45 gint64 user_action_time,
46 TpHandleChannelsContext *context,
56 static guint signals[LAST_SIGNAL] = {0};
58 /* private structure */
60 TpBaseClient *handler;
61 gboolean dispose_has_run;
62 } EmpathyCallFactoryPriv;
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallFactory)
66 static GObject *call_factory = NULL;
69 empathy_call_factory_init (EmpathyCallFactory *obj)
71 EmpathyCallFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
72 EMPATHY_TYPE_CALL_FACTORY, EmpathyCallFactoryPriv);
78 dbus = tp_dbus_daemon_dup (&error);
81 g_warning ("Failed to get TpDBusDaemon: %s", error->message);
86 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
87 "Empathy.AudioVideo", FALSE, handle_channels_cb, obj, NULL);
89 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
90 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
91 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
92 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
95 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
96 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
97 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
98 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
99 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
102 tp_base_client_take_handler_filter (priv->handler, 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, TP_HANDLE_TYPE_CONTACT,
106 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
109 tp_base_client_add_handler_capabilities_varargs (priv->handler,
110 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
111 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
112 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/video/h264",
115 g_object_unref (dbus);
119 empathy_call_factory_constructor (GType type, guint n_construct_params,
120 GObjectConstructParam *construct_params)
122 g_return_val_if_fail (call_factory == NULL, NULL);
124 call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
125 (type, n_construct_params, construct_params);
126 g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
132 empathy_call_factory_finalize (GObject *object)
134 /* free any data held directly by the object here */
136 if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize)
137 G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize (object);
141 empathy_call_factory_dispose (GObject *object)
143 EmpathyCallFactoryPriv *priv = GET_PRIV (object);
145 if (priv->dispose_has_run)
148 priv->dispose_has_run = TRUE;
150 tp_clear_object (&priv->handler);
152 if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose)
153 G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose (object);
157 empathy_call_factory_class_init (
158 EmpathyCallFactoryClass *empathy_call_factory_class)
160 GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_factory_class);
162 g_type_class_add_private (empathy_call_factory_class,
163 sizeof (EmpathyCallFactoryPriv));
165 object_class->constructor = empathy_call_factory_constructor;
166 object_class->dispose = empathy_call_factory_dispose;
167 object_class->finalize = empathy_call_factory_finalize;
169 signals[NEW_CALL_HANDLER] =
170 g_signal_new ("new-call-handler",
171 G_TYPE_FROM_CLASS (empathy_call_factory_class),
172 G_SIGNAL_RUN_LAST, 0,
174 _empathy_marshal_VOID__OBJECT_BOOLEAN,
176 2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
180 empathy_call_factory_initialise (void)
182 g_return_val_if_fail (call_factory == NULL, NULL);
184 return EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY, NULL));
188 empathy_call_factory_get (void)
190 g_return_val_if_fail (call_factory != NULL, NULL);
192 return EMPATHY_CALL_FACTORY (call_factory);
196 empathy_call_factory_create_request (EmpathyContact *contact,
197 gboolean initial_audio,
198 gboolean initial_video)
201 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
202 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
203 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
204 TP_PROP_CHANNEL_TARGET_HANDLE, G_TYPE_UINT,
205 empathy_contact_get_handle (contact),
206 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN,
208 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
214 create_media_channel_cb (GObject *source,
215 GAsyncResult *result,
218 GError *error = NULL;
220 if (!tp_account_channel_request_create_channel_finish (
221 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
223 DEBUG ("Failed to create media channel: %s", error->message);
224 g_error_free (error);
229 * empathy_call_factory_new_call_with_streams:
230 * @factory: an #EmpathyCallFactory
231 * @contact: an #EmpathyContact
232 * @initial_audio: if %TRUE the call will be started with audio
233 * @initial_video: if %TRUE the call will be started with video
235 * Initiate a new call with @contact.
238 empathy_call_factory_new_call_with_streams (EmpathyContact *contact,
239 gboolean initial_audio,
240 gboolean initial_video,
246 TpAccountChannelRequest *req;
248 request = empathy_call_factory_create_request (contact, initial_audio,
251 account = empathy_contact_get_account (contact);
253 req = tp_account_channel_request_new (account, request, timestamp);
255 tp_account_channel_request_create_channel_async (req, NULL, NULL,
256 create_media_channel_cb, NULL);
258 g_hash_table_unref (request);
259 g_object_unref (req);
263 create_call_handler (EmpathyCallFactory *factory,
266 EmpathyCallHandler *handler;
268 g_return_if_fail (factory != NULL);
270 handler = empathy_call_handler_new_for_channel (call);
272 g_signal_emit (factory, signals[NEW_CALL_HANDLER], 0,
275 g_object_unref (handler);
279 call_status_changed_cb (EmpathyTpCall *call,
281 EmpathyCallFactory *self)
283 if (empathy_tp_call_get_status (call) <= EMPATHY_TP_CALL_STATUS_READYING)
286 create_call_handler (self, call);
288 g_signal_handlers_disconnect_by_func (call, call_status_changed_cb, self);
289 g_object_unref (call);
293 handle_channels_cb (TpSimpleHandler *handler,
295 TpConnection *connection,
297 GList *requests_satisfied,
298 gint64 user_action_time,
299 TpHandleChannelsContext *context,
302 EmpathyCallFactory *self = user_data;
305 for (l = channels; l != NULL; l = g_list_next (l))
307 TpChannel *channel = l->data;
310 if (tp_proxy_get_invalidated (channel) != NULL)
313 if (tp_channel_get_channel_type_id (channel) !=
314 TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
317 call = empathy_tp_call_new (account, channel);
319 if (empathy_tp_call_get_status (call) <= EMPATHY_TP_CALL_STATUS_READYING)
321 /* We have to wait that the TpCall is ready as the
322 * call-handler rely on it. */
323 tp_g_signal_connect_object (call, "notify::status",
324 G_CALLBACK (call_status_changed_cb), self, 0);
328 create_call_handler (self, call);
329 g_object_unref (call);
332 tp_handle_channels_context_accept (context);
336 empathy_call_factory_register (EmpathyCallFactory *self,
339 EmpathyCallFactoryPriv *priv = GET_PRIV (self);
341 return tp_base_client_register (priv->handler, error);