]> git.0d.be Git - empathy.git/blob - libempathy/empathy-call-handler.c
Also unref the tf channel
[empathy.git] / libempathy / empathy-call-handler.c
1 /*
2  * empathy-call-handler.c - Source for EmpathyCallHandler
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/util.h>
26
27 #include <telepathy-farsight/channel.h>
28 #include <telepathy-farsight/stream.h>
29
30 #include "empathy-call-handler.h"
31 #include "empathy-dispatcher.h"
32 #include "empathy-marshal.h"
33
34 G_DEFINE_TYPE(EmpathyCallHandler, empathy_call_handler, G_TYPE_OBJECT)
35
36 /* signal enum */
37 enum
38 {
39   CONFERENCE_ADDED,
40   SRC_PAD_ADDED,
41   SINK_PAD_ADDED,
42   LAST_SIGNAL
43 };
44
45 static guint signals[LAST_SIGNAL] = {0};
46
47 enum {
48   PROP_TP_CALL = 1,
49   PROP_GST_BUS,
50   PROP_CONTACT
51 };
52
53 /* private structure */
54 typedef struct _EmpathyCallHandlerPriv EmpathyCallHandlerPriv;
55
56 struct _EmpathyCallHandlerPriv
57 {
58   gboolean dispose_has_run;
59   EmpathyTpCall *call;
60   EmpathyContact *contact;
61   TfChannel *tfchannel;
62   GstBus *bus;
63 };
64
65 #define GET_PRIV(o) \
66   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_CALL_HANDLER,\
67     EmpathyCallHandlerPriv))
68
69 static void
70 empathy_call_handler_init (EmpathyCallHandler *obj)
71 {
72   //EmpathyCallHandlerPriv *priv = GET_PRIV (obj);
73
74   /* allocate any data required by the object here */
75 }
76
77 static void empathy_call_handler_dispose (GObject *object);
78 static void empathy_call_handler_finalize (GObject *object);
79
80 static void
81 empathy_call_handler_set_property (GObject *object,
82   guint property_id, const GValue *value, GParamSpec *pspec)
83 {
84   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
85
86   switch (property_id)
87     {
88       case PROP_CONTACT:
89         priv->contact = g_value_dup_object (value);
90         break;
91       case PROP_TP_CALL:
92         priv->call = g_value_dup_object (value);
93         break;
94       case PROP_GST_BUS:
95         priv->bus = g_value_dup_object (value);
96         break;
97       default:
98         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
99     }
100 }
101
102 static void
103 empathy_call_handler_get_property (GObject *object,
104   guint property_id, GValue *value, GParamSpec *pspec)
105 {
106   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
107
108   switch (property_id)
109     {
110       case PROP_CONTACT:
111         g_value_set_object (value, priv->contact);
112         break;
113       case PROP_TP_CALL:
114         g_value_set_object (value, priv->call);
115         break;
116       case PROP_GST_BUS:
117         g_value_set_object (value, priv->bus);
118         break;
119       default:
120         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
121     }
122 }
123
124
125 static void
126 empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
127 {
128   GObjectClass *object_class = G_OBJECT_CLASS (klass);
129   GParamSpec *param_spec;
130
131   g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
132
133   object_class->set_property = empathy_call_handler_set_property;
134   object_class->get_property = empathy_call_handler_get_property;
135   object_class->dispose = empathy_call_handler_dispose;
136   object_class->finalize = empathy_call_handler_finalize;
137
138   param_spec = g_param_spec_object ("contact",
139     "contact", "The remote contact",
140     EMPATHY_TYPE_CONTACT,
141     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
142   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
143
144   param_spec = g_param_spec_object ("gst-bus",
145     "gst-bus", "The gstreamer bus",
146     GST_TYPE_BUS,
147     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
148   g_object_class_install_property (object_class, PROP_GST_BUS, param_spec);
149
150   param_spec = g_param_spec_object ("tp-call",
151     "tp-call", "The calls channel wrapper",
152     EMPATHY_TYPE_TP_CALL,
153     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
154   g_object_class_install_property (object_class, PROP_TP_CALL, param_spec);
155
156   signals[CONFERENCE_ADDED] =
157     g_signal_new ("conference-added", G_TYPE_FROM_CLASS (klass),
158       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
159       g_cclosure_marshal_VOID__OBJECT,
160       G_TYPE_NONE,
161       1, FS_TYPE_CONFERENCE);
162
163   signals[SRC_PAD_ADDED] =
164     g_signal_new ("src-pad-added", G_TYPE_FROM_CLASS (klass),
165       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
166       _empathy_marshal_VOID__OBJECT_UINT,
167       G_TYPE_NONE,
168       2, GST_TYPE_PAD, G_TYPE_UINT);
169
170   signals[SINK_PAD_ADDED] =
171     g_signal_new ("sink-pad-added", G_TYPE_FROM_CLASS (klass),
172       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
173       _empathy_marshal_VOID__OBJECT_UINT,
174       G_TYPE_NONE,
175       2, GST_TYPE_PAD, G_TYPE_UINT);
176 }
177
178 void
179 empathy_call_handler_dispose (GObject *object)
180 {
181   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
182   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
183
184   if (priv->dispose_has_run)
185     return;
186
187   priv->dispose_has_run = TRUE;
188
189   if (priv->contact != NULL)
190     g_object_unref (priv->contact);
191
192   priv->contact = NULL;
193
194   if (priv->tfchannel != NULL)
195     g_object_unref (priv->tfchannel);
196
197   priv->tfchannel = NULL;
198
199   if (priv->call != NULL)
200     {
201       empathy_tp_call_close (priv->call);
202       g_object_unref (priv->call);
203     }
204
205   priv->call = NULL;
206
207   /* release any references held by the object here */
208   if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose)
209     G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
210 }
211
212 void
213 empathy_call_handler_finalize (GObject *object)
214 {
215   //EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
216   //EmpathyCallHandlerPriv *priv = GET_PRIV (self);
217
218   /* free any data held directly by the object here */
219
220   G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize (object);
221 }
222
223 EmpathyCallHandler *
224 empathy_call_handler_new_for_contact (EmpathyContact *contact)
225 {
226   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
227     "contact", contact, NULL));
228 }
229
230 EmpathyCallHandler *
231 empathy_call_handler_new_for_channel (EmpathyTpCall *call)
232 {
233   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
234     "tp-call", call, NULL));
235 }
236
237 static gboolean
238 empathy_call_handler_pipeline_bus_watch (GstBus *bus, GstMessage *message,
239   gpointer user_data)
240 {
241   EmpathyCallHandler *handler = EMPATHY_CALL_HANDLER (user_data);
242   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
243
244   g_assert (priv->tfchannel != NULL);
245
246   tf_channel_bus_message (priv->tfchannel, message);
247
248   return TRUE;
249 }
250
251 static void
252 empathy_call_handler_tf_channel_session_created_cb (TfChannel *tfchannel,
253   FsConference *conference, FsParticipant *participant,
254   EmpathyCallHandler *self)
255 {
256   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
257
258   gst_bus_add_watch (priv->bus, empathy_call_handler_pipeline_bus_watch, self);
259
260   g_signal_emit (G_OBJECT (self), signals[CONFERENCE_ADDED], 0,
261     GST_ELEMENT (conference));
262 }
263
264 static void
265 empathy_call_handler_tf_stream_src_pad_added_cb (TfStream *stream,
266   GstPad *pad, FsCodec *codec, EmpathyCallHandler  *handler)
267 {
268   guint media_type;
269
270   g_object_get (stream, "media-type", &media_type, NULL);
271
272   g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
273     pad, media_type);
274 }
275
276
277 static gboolean
278 empathy_call_handler_tf_stream_request_resource_cb (TfStream *stream,
279   guint direction, EmpathyTpCall *call)
280 {
281   return TRUE;
282 }
283
284 static void
285 empathy_call_handler_tf_channel_stream_created_cb (TfChannel *tfchannel,
286   TfStream *stream, EmpathyCallHandler *handler)
287 {
288   guint media_type;
289   GstPad *spad;
290
291   g_signal_connect (stream, "src-pad-added",
292       G_CALLBACK (empathy_call_handler_tf_stream_src_pad_added_cb), handler);
293   g_signal_connect (stream, "request-resource",
294       G_CALLBACK (empathy_call_handler_tf_stream_request_resource_cb),
295         handler);
296
297   g_object_get (stream, "media-type", &media_type,
298     "sink-pad", &spad, NULL);
299
300   g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
301     spad, media_type);
302
303   gst_object_unref (spad);
304 }
305
306 static void
307 empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
308 {
309   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
310   TpChannel *channel;
311
312   g_object_get (priv->call, "channel", &channel, NULL);
313
314   g_assert (channel != NULL);
315
316   priv->tfchannel = tf_channel_new (channel);
317
318   /* Set up the telepathy farsight channel */
319   g_signal_connect (priv->tfchannel, "session-created",
320       G_CALLBACK (empathy_call_handler_tf_channel_session_created_cb), self);
321   g_signal_connect (priv->tfchannel, "stream-created",
322       G_CALLBACK (empathy_call_handler_tf_channel_stream_created_cb), self);
323
324   g_object_unref (channel);
325 }
326
327 static void
328 empathy_call_handler_request_cb (EmpathyDispatchOperation *operation,
329   const GError *error, gpointer user_data)
330 {
331   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
332   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
333
334   if (error != NULL)
335     return;
336
337   priv->call = EMPATHY_TP_CALL (
338     empathy_dispatch_operation_get_channel_wrapper (operation));
339
340   g_object_ref (priv->call);
341
342   empathy_call_handler_start_tpfs (self);
343
344   empathy_tp_call_to (priv->call, priv->contact);
345
346   empathy_dispatch_operation_claim (operation);
347 }
348
349 static void
350 empathy_call_handler_contact_ready_cb (EmpathyContact *contact,
351   const GError *error, gpointer user_data, GObject *object)
352 {
353   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
354   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
355   EmpathyDispatcher *dispatcher;
356   McAccount *account;
357   GStrv allowed;
358   GValue *value;
359   GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
360       (GDestroyNotify) tp_g_value_slice_free);
361
362   g_assert (priv->contact != NULL);
363
364   dispatcher = empathy_dispatcher_dup_singleton ();
365   account = empathy_contact_get_account (priv->contact);
366   allowed = empathy_dispatcher_find_channel_class (dispatcher, account,
367     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT);
368
369   if (!tp_strv_contains ((const gchar * const *)allowed,
370       TP_IFACE_CHANNEL ".TargetHandle"))
371     g_assert_not_reached ();
372
373   /* org.freedesktop.Telepathy.Channel.ChannelType */
374   value = tp_g_value_slice_new (G_TYPE_STRING);
375   g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA);
376   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
377
378   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
379   value = tp_g_value_slice_new (G_TYPE_UINT);
380   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
381   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
382
383   /* org.freedesktop.Telepathy.Channel.TargetHandle*/
384   value = tp_g_value_slice_new (G_TYPE_UINT);
385   g_value_set_uint (value, empathy_contact_get_handle (priv->contact));
386   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
387
388   empathy_dispatcher_create_channel (dispatcher, account,
389     request, empathy_call_handler_request_cb, self);
390
391   g_object_unref (dispatcher);
392 }
393
394 void
395 empathy_call_handler_start_call (EmpathyCallHandler *handler)
396 {
397
398   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
399
400   if (priv->call == NULL)
401     {
402       empathy_contact_call_when_ready (priv->contact,
403         EMPATHY_CONTACT_READY_ID,
404         empathy_call_handler_contact_ready_cb, NULL, NULL, G_OBJECT (handler));
405     }
406   else
407     {
408       empathy_call_handler_start_tpfs (handler);
409       empathy_tp_call_accept_incoming_call (priv->call);
410     }
411 }
412
413 void
414 empathy_call_handler_set_bus (EmpathyCallHandler *handler, GstBus *bus)
415 {
416   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
417
418   g_assert (priv->bus == NULL);
419
420   priv->bus = g_object_ref (bus);
421 }
422