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