]> git.0d.be Git - empathy.git/blob - libempathy/empathy-call-factory.c
Merge branch 'sasl'
[empathy.git] / libempathy / empathy-call-factory.c
1 /*
2  * empathy-call-factory.c - Source for EmpathyCallFactory
3  * Copyright (C) 2008 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
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>
29
30 #include "empathy-dispatcher.h"
31 #include "empathy-marshal.h"
32 #include "empathy-call-factory.h"
33 #include "empathy-utils.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include <libempathy/empathy-debug.h>
37
38 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, G_TYPE_OBJECT)
39
40 static void handle_channels_cb (TpSimpleHandler *handler,
41     TpAccount *account,
42     TpConnection *connection,
43     GList *channels,
44     GList *requests_satisfied,
45     gint64 user_action_time,
46     TpHandleChannelsContext *context,
47     gpointer user_data);
48
49 /* signal enum */
50 enum
51 {
52     NEW_CALL_HANDLER,
53     LAST_SIGNAL
54 };
55
56 static guint signals[LAST_SIGNAL] = {0};
57
58 /* private structure */
59 typedef struct {
60   TpBaseClient *handler;
61   gboolean dispose_has_run;
62 } EmpathyCallFactoryPriv;
63
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallFactory)
65
66 static GObject *call_factory = NULL;
67
68 static void
69 empathy_call_factory_init (EmpathyCallFactory *obj)
70 {
71   EmpathyCallFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
72     EMPATHY_TYPE_CALL_FACTORY, EmpathyCallFactoryPriv);
73   TpDBusDaemon *dbus;
74   GError *error = NULL;
75
76   obj->priv = priv;
77
78   dbus = tp_dbus_daemon_dup (&error);
79   if (dbus == NULL)
80     {
81       g_warning ("Failed to get TpDBusDaemon: %s", error->message);
82       g_error_free (error);
83       return;
84     }
85
86   priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
87       "Empathy.AudioVideo", FALSE, handle_channels_cb, obj, NULL);
88
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,
93         NULL));
94
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,
100         NULL));
101
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,
107         NULL));
108
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",
113     NULL);
114
115   g_object_unref (dbus);
116 }
117
118 static GObject *
119 empathy_call_factory_constructor (GType type, guint n_construct_params,
120   GObjectConstructParam *construct_params)
121 {
122   g_return_val_if_fail (call_factory == NULL, NULL);
123
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);
127
128   return call_factory;
129 }
130
131 static void
132 empathy_call_factory_finalize (GObject *object)
133 {
134   /* free any data held directly by the object here */
135
136   if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize)
137     G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize (object);
138 }
139
140 static void
141 empathy_call_factory_dispose (GObject *object)
142 {
143   EmpathyCallFactoryPriv *priv = GET_PRIV (object);
144
145   if (priv->dispose_has_run)
146     return;
147
148   priv->dispose_has_run = TRUE;
149
150   tp_clear_object (&priv->handler);
151
152   if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose)
153     G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose (object);
154 }
155
156 static void
157 empathy_call_factory_class_init (
158   EmpathyCallFactoryClass *empathy_call_factory_class)
159 {
160   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_factory_class);
161
162   g_type_class_add_private (empathy_call_factory_class,
163     sizeof (EmpathyCallFactoryPriv));
164
165   object_class->constructor = empathy_call_factory_constructor;
166   object_class->dispose = empathy_call_factory_dispose;
167   object_class->finalize = empathy_call_factory_finalize;
168
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,
173       NULL, NULL,
174       _empathy_marshal_VOID__OBJECT_BOOLEAN,
175       G_TYPE_NONE,
176       2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
177 }
178
179 EmpathyCallFactory *
180 empathy_call_factory_initialise (void)
181 {
182   g_return_val_if_fail (call_factory == NULL, NULL);
183
184   return EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY, NULL));
185 }
186
187 EmpathyCallFactory *
188 empathy_call_factory_get (void)
189 {
190   g_return_val_if_fail (call_factory != NULL, NULL);
191
192   return EMPATHY_CALL_FACTORY (call_factory);
193 }
194
195 GHashTable *
196 empathy_call_factory_create_request (EmpathyContact *contact,
197     gboolean initial_audio,
198     gboolean initial_video)
199 {
200   return tp_asv_new (
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,
207         initial_audio,
208       TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
209         initial_video,
210       NULL);
211 }
212
213 static void
214 create_media_channel_cb (GObject *source,
215     GAsyncResult *result,
216     gpointer user_data)
217 {
218   GError *error = NULL;
219
220   if (!tp_account_channel_request_create_channel_finish (
221         TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
222     {
223       DEBUG ("Failed to create media channel: %s", error->message);
224       g_error_free (error);
225     }
226 }
227
228 /**
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
234  *
235  * Initiate a new call with @contact.
236  */
237 void
238 empathy_call_factory_new_call_with_streams (EmpathyContact *contact,
239     gboolean initial_audio,
240     gboolean initial_video,
241     gint64 timestamp,
242     gpointer user_data)
243 {
244   GHashTable *request;
245   TpAccount *account;
246   TpAccountChannelRequest *req;
247
248   request = empathy_call_factory_create_request (contact, initial_audio,
249       initial_video);
250
251   account = empathy_contact_get_account (contact);
252
253   req = tp_account_channel_request_new (account, request, timestamp);
254
255   tp_account_channel_request_create_channel_async (req, NULL, NULL,
256       create_media_channel_cb, NULL);
257
258   g_hash_table_unref (request);
259   g_object_unref (req);
260 }
261
262 static void
263 create_call_handler (EmpathyCallFactory *factory,
264   EmpathyTpCall *call)
265 {
266   EmpathyCallHandler *handler;
267
268   g_return_if_fail (factory != NULL);
269
270   handler = empathy_call_handler_new_for_channel (call);
271
272   g_signal_emit (factory, signals[NEW_CALL_HANDLER], 0,
273     handler, FALSE);
274
275   g_object_unref (handler);
276 }
277
278 static void
279 call_status_changed_cb (EmpathyTpCall *call,
280     GParamSpec *spec,
281     EmpathyCallFactory *self)
282 {
283   if (empathy_tp_call_get_status (call) <= EMPATHY_TP_CALL_STATUS_READYING)
284     return;
285
286   create_call_handler (self, call);
287
288   g_signal_handlers_disconnect_by_func (call, call_status_changed_cb, self);
289   g_object_unref (call);
290 }
291
292 static void
293 handle_channels_cb (TpSimpleHandler *handler,
294     TpAccount *account,
295     TpConnection *connection,
296     GList *channels,
297     GList *requests_satisfied,
298     gint64 user_action_time,
299     TpHandleChannelsContext *context,
300     gpointer user_data)
301 {
302   EmpathyCallFactory *self = user_data;
303   GList *l;
304
305   for (l = channels; l != NULL; l = g_list_next (l))
306     {
307       TpChannel *channel = l->data;
308       EmpathyTpCall *call;
309
310       if (tp_proxy_get_invalidated (channel) != NULL)
311         continue;
312
313       if (tp_channel_get_channel_type_id (channel) !=
314           TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
315         continue;
316
317       call = empathy_tp_call_new (account, channel);
318
319       if (empathy_tp_call_get_status (call) <= EMPATHY_TP_CALL_STATUS_READYING)
320         {
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);
325           continue;
326         }
327
328       create_call_handler (self, call);
329       g_object_unref (call);
330     }
331
332   tp_handle_channels_context_accept (context);
333 }
334
335 gboolean
336 empathy_call_factory_register (EmpathyCallFactory *self,
337     GError **error)
338 {
339   EmpathyCallFactoryPriv *priv = GET_PRIV (self);
340
341   return tp_base_client_register (priv->handler, error);
342 }