]> git.0d.be Git - empathy.git/blob - libempathy/empathy-call-handler.c
Update Czech translation
[empathy.git] / libempathy / empathy-call-handler.c
1 /*
2  * empathy-call-handler.c - Source for EmpathyCallHandler
3  * Copyright (C) 2008-2009 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   REQUEST_RESOURCE,
43   CLOSED,
44   LAST_SIGNAL
45 };
46
47 static guint signals[LAST_SIGNAL] = {0};
48
49 enum {
50   PROP_TP_CALL = 1,
51   PROP_GST_BUS,
52   PROP_CONTACT,
53   PROP_INITIAL_AUDIO,
54   PROP_INITIAL_VIDEO
55 };
56
57 /* private structure */
58
59 typedef struct {
60   gboolean dispose_has_run;
61   EmpathyTpCall *call;
62   EmpathyContact *contact;
63   TfChannel *tfchannel;
64   gboolean initial_audio;
65   gboolean initial_video;
66 } EmpathyCallHandlerPriv;
67
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
69
70 static void
71 empathy_call_handler_dispose (GObject *object)
72 {
73   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
74
75   if (priv->dispose_has_run)
76     return;
77
78   priv->dispose_has_run = TRUE;
79
80   if (priv->contact != NULL)
81     g_object_unref (priv->contact);
82
83   priv->contact = NULL;
84
85   if (priv->tfchannel != NULL)
86     g_object_unref (priv->tfchannel);
87
88   priv->tfchannel = NULL;
89
90   if (priv->call != NULL)
91     {
92       empathy_tp_call_close (priv->call);
93       g_object_unref (priv->call);
94     }
95
96   priv->call = NULL;
97
98   /* release any references held by the object here */
99   if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose)
100     G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
101 }
102
103 static void
104 empathy_call_handler_finalize (GObject *object)
105 {
106   /* free any data held directly by the object here */
107   if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize)
108     G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize (object);
109 }
110
111 static void
112 empathy_call_handler_init (EmpathyCallHandler *obj)
113 {
114   EmpathyCallHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
115     EMPATHY_TYPE_CALL_HANDLER, EmpathyCallHandlerPriv);
116
117   obj->priv = priv;
118 }
119
120 static void
121 empathy_call_handler_constructed (GObject *object)
122 {
123   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
124
125   if (priv->contact == NULL)
126     {
127       g_object_get (priv->call, "contact", &(priv->contact), NULL);
128     }
129 }
130
131 static void
132 empathy_call_handler_set_property (GObject *object,
133   guint property_id, const GValue *value, GParamSpec *pspec)
134 {
135   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
136
137   switch (property_id)
138     {
139       case PROP_CONTACT:
140         priv->contact = g_value_dup_object (value);
141         break;
142       case PROP_TP_CALL:
143         priv->call = g_value_dup_object (value);
144         break;
145       case PROP_INITIAL_AUDIO:
146         priv->initial_audio = g_value_get_boolean (value);
147         break;
148       case PROP_INITIAL_VIDEO:
149         priv->initial_video = g_value_get_boolean (value);
150         break;
151       default:
152         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
153     }
154 }
155
156 static void
157 empathy_call_handler_get_property (GObject *object,
158   guint property_id, GValue *value, GParamSpec *pspec)
159 {
160   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
161
162   switch (property_id)
163     {
164       case PROP_CONTACT:
165         g_value_set_object (value, priv->contact);
166         break;
167       case PROP_TP_CALL:
168         g_value_set_object (value, priv->call);
169         break;
170       case PROP_INITIAL_AUDIO:
171         g_value_set_boolean (value, priv->initial_audio);
172         break;
173       case PROP_INITIAL_VIDEO:
174         g_value_set_boolean (value, priv->initial_video);
175         break;
176       default:
177         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
178     }
179 }
180
181
182 static void
183 empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
184 {
185   GObjectClass *object_class = G_OBJECT_CLASS (klass);
186   GParamSpec *param_spec;
187
188   g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
189
190   object_class->constructed = empathy_call_handler_constructed;
191   object_class->set_property = empathy_call_handler_set_property;
192   object_class->get_property = empathy_call_handler_get_property;
193   object_class->dispose = empathy_call_handler_dispose;
194   object_class->finalize = empathy_call_handler_finalize;
195
196   param_spec = g_param_spec_object ("contact",
197     "contact", "The remote contact",
198     EMPATHY_TYPE_CONTACT,
199     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
200   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
201
202   param_spec = g_param_spec_object ("tp-call",
203     "tp-call", "The calls channel wrapper",
204     EMPATHY_TYPE_TP_CALL,
205     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
206   g_object_class_install_property (object_class, PROP_TP_CALL, param_spec);
207
208   param_spec = g_param_spec_boolean ("initial-audio",
209     "initial-audio", "Whether the call should start with audio",
210     TRUE,
211     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
212   g_object_class_install_property (object_class, PROP_INITIAL_AUDIO,
213       param_spec);
214
215   param_spec = g_param_spec_boolean ("initial-video",
216     "initial-video", "Whether the call should start with video",
217     FALSE,
218     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
219   g_object_class_install_property (object_class, PROP_INITIAL_VIDEO,
220     param_spec);
221
222   signals[CONFERENCE_ADDED] =
223     g_signal_new ("conference-added", G_TYPE_FROM_CLASS (klass),
224       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
225       g_cclosure_marshal_VOID__OBJECT,
226       G_TYPE_NONE,
227       1, FS_TYPE_CONFERENCE);
228
229   signals[SRC_PAD_ADDED] =
230     g_signal_new ("src-pad-added", G_TYPE_FROM_CLASS (klass),
231       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
232       _empathy_marshal_VOID__OBJECT_UINT,
233       G_TYPE_NONE,
234       2, GST_TYPE_PAD, G_TYPE_UINT);
235
236   signals[SINK_PAD_ADDED] =
237     g_signal_new ("sink-pad-added", G_TYPE_FROM_CLASS (klass),
238       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
239       _empathy_marshal_VOID__OBJECT_UINT,
240       G_TYPE_NONE,
241       2, GST_TYPE_PAD, G_TYPE_UINT);
242
243   signals[REQUEST_RESOURCE] =
244     g_signal_new ("request-resource", G_TYPE_FROM_CLASS (klass),
245       G_SIGNAL_RUN_LAST, 0,
246       g_signal_accumulator_true_handled, NULL,
247       _empathy_marshal_BOOLEAN__UINT_UINT,
248       G_TYPE_BOOLEAN, 2, G_TYPE_UINT, G_TYPE_UINT);
249
250   signals[CLOSED] =
251     g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
252       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
253       g_cclosure_marshal_VOID__VOID,
254       G_TYPE_NONE,
255       0);
256 }
257
258 /**
259  * empathy_call_handler_new_for_contact:
260  * @contact: an #EmpathyContact
261  *
262  * Creates a new #EmpathyCallHandler with contact @contact.
263  *
264  * Return value: a new #EmpathyCallHandler
265  */
266 EmpathyCallHandler *
267 empathy_call_handler_new_for_contact (EmpathyContact *contact)
268 {
269   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
270     "contact", contact, NULL));
271 }
272
273 /**
274  * empathy_call_handler_new_for_contact_with_streams:
275  * @contact: an #EmpathyContact
276  * @audio: if %TRUE the call will be started with audio
277  * @video: if %TRUE the call will be started with video
278  *
279  * Creates a new #EmpathyCallHandler with contact @contact.
280  *
281  * Return value: a new #EmpathyCallHandler
282  */
283 EmpathyCallHandler *
284 empathy_call_handler_new_for_contact_with_streams (EmpathyContact *contact,
285     gboolean audio, gboolean video)
286 {
287   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
288     "contact", contact,
289     "initial-audio", audio,
290     "initial-video", video,
291     NULL));
292 }
293
294 EmpathyCallHandler *
295 empathy_call_handler_new_for_channel (EmpathyTpCall *call)
296 {
297   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
298     "tp-call", call,
299     "initial-video", empathy_tp_call_is_receiving_video (call),
300     NULL));
301 }
302
303 void
304 empathy_call_handler_bus_message (EmpathyCallHandler *handler,
305   GstBus *bus, GstMessage *message)
306 {
307   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
308
309   if (priv->tfchannel == NULL)
310     return;
311
312   tf_channel_bus_message (priv->tfchannel, message);
313 }
314
315 static void
316 empathy_call_handler_tf_channel_session_created_cb (TfChannel *tfchannel,
317   FsConference *conference, FsParticipant *participant,
318   EmpathyCallHandler *self)
319 {
320   g_signal_emit (G_OBJECT (self), signals[CONFERENCE_ADDED], 0,
321     GST_ELEMENT (conference));
322 }
323
324 static void
325 empathy_call_handler_tf_stream_src_pad_added_cb (TfStream *stream,
326   GstPad *pad, FsCodec *codec, EmpathyCallHandler  *handler)
327 {
328   guint media_type;
329
330   g_object_get (stream, "media-type", &media_type, NULL);
331
332   g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
333     pad, media_type);
334 }
335
336
337 static gboolean
338 empathy_call_handler_tf_stream_request_resource_cb (TfStream *stream,
339   guint direction, EmpathyTpCall *call)
340 {
341   gboolean ret;
342   guint media_type;
343
344   g_object_get (G_OBJECT (stream), "media-type", &media_type, NULL);
345
346   g_signal_emit (G_OBJECT (call),
347     signals[REQUEST_RESOURCE], 0, media_type, direction, &ret);
348
349   return ret;
350 }
351
352 static void
353 empathy_call_handler_tf_channel_stream_created_cb (TfChannel *tfchannel,
354   TfStream *stream, EmpathyCallHandler *handler)
355 {
356   guint media_type;
357   GstPad *spad;
358
359   g_signal_connect (stream, "src-pad-added",
360       G_CALLBACK (empathy_call_handler_tf_stream_src_pad_added_cb), handler);
361   g_signal_connect (stream, "request-resource",
362       G_CALLBACK (empathy_call_handler_tf_stream_request_resource_cb),
363         handler);
364
365   g_object_get (stream, "media-type", &media_type,
366     "sink-pad", &spad, NULL);
367
368   g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
369     spad, media_type);
370
371   gst_object_unref (spad);
372 }
373
374 static void
375 empathy_call_handler_tf_channel_closed_cb (TfChannel *tfchannel,
376   EmpathyCallHandler *handler)
377 {
378   g_signal_emit (G_OBJECT (handler), signals[CLOSED], 0);
379 }
380
381 static GList *
382 empathy_call_handler_tf_channel_codec_config_cb (TfChannel *channel,
383   guint stream_id, FsMediaType media_type, guint direction, gpointer user_data)
384 {
385   gchar *filename = empathy_file_lookup ("codec-preferences", "data");
386   GList *codecs;
387   GError *error = NULL;
388
389   codecs = fs_codec_list_from_keyfile (filename, &error);
390   g_free (filename);
391
392   if (!codecs)
393     {
394       g_warning ("No codec-preferences file: %s",
395           error ? error->message : "No error message");
396     }
397   g_clear_error (&error);
398
399   return codecs;
400 }
401
402 static void
403 empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
404 {
405   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
406   TpChannel *channel;
407
408   g_object_get (priv->call, "channel", &channel, NULL);
409
410   g_assert (channel != NULL);
411
412   priv->tfchannel = tf_channel_new (channel);
413
414   /* Set up the telepathy farsight channel */
415   g_signal_connect (priv->tfchannel, "session-created",
416       G_CALLBACK (empathy_call_handler_tf_channel_session_created_cb), self);
417   g_signal_connect (priv->tfchannel, "stream-created",
418       G_CALLBACK (empathy_call_handler_tf_channel_stream_created_cb), self);
419   g_signal_connect (priv->tfchannel, "closed",
420       G_CALLBACK (empathy_call_handler_tf_channel_closed_cb), self);
421   g_signal_connect (priv->tfchannel, "stream-get-codec-config",
422       G_CALLBACK (empathy_call_handler_tf_channel_codec_config_cb), self);
423
424   g_object_unref (channel);
425 }
426
427 static void
428 empathy_call_handler_request_cb (EmpathyDispatchOperation *operation,
429   const GError *error, gpointer user_data)
430 {
431   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
432   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
433
434   if (error != NULL)
435     return;
436
437   priv->call = EMPATHY_TP_CALL (
438     empathy_dispatch_operation_get_channel_wrapper (operation));
439
440   g_object_ref (priv->call);
441
442   empathy_call_handler_start_tpfs (self);
443
444   empathy_tp_call_to (priv->call, priv->contact,
445     priv->initial_audio, priv->initial_video);
446
447   empathy_dispatch_operation_claim (operation);
448 }
449
450 void
451 empathy_call_handler_start_call (EmpathyCallHandler *handler)
452 {
453
454   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
455   EmpathyDispatcher *dispatcher;
456   TpConnection *connection;
457   GList *classes;
458   GValue *value;
459   GHashTable *request;
460
461   if (priv->call != NULL)
462     {
463       empathy_call_handler_start_tpfs (handler);
464       empathy_tp_call_accept_incoming_call (priv->call);
465       return;
466     }
467
468   g_assert (priv->contact != NULL);
469
470   dispatcher = empathy_dispatcher_dup_singleton ();
471   connection = empathy_contact_get_connection (priv->contact);
472   classes = empathy_dispatcher_find_requestable_channel_classes
473     (dispatcher, connection, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
474      TP_HANDLE_TYPE_CONTACT, NULL);
475
476   if (classes == NULL)
477     return;
478
479   g_list_free (classes);
480
481   request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
482       (GDestroyNotify) tp_g_value_slice_free);
483
484   /* org.freedesktop.Telepathy.Channel.ChannelType */
485   value = tp_g_value_slice_new (G_TYPE_STRING);
486   g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA);
487   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
488
489   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
490   value = tp_g_value_slice_new (G_TYPE_UINT);
491   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
492   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
493
494   /* org.freedesktop.Telepathy.Channel.TargetHandle*/
495   value = tp_g_value_slice_new (G_TYPE_UINT);
496   g_value_set_uint (value, empathy_contact_get_handle (priv->contact));
497   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
498
499   empathy_dispatcher_create_channel (dispatcher, connection,
500     request, empathy_call_handler_request_cb, handler);
501
502   g_object_unref (dispatcher);
503 }
504
505 /**
506  * empathy_call_handler_stop_call:
507  * @handler: an #EmpathyCallHandler
508  *
509  * Closes the #EmpathyCallHandler's call and frees its resources.
510  */
511 void
512 empathy_call_handler_stop_call (EmpathyCallHandler *handler)
513 {
514   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
515
516   if (priv->call != NULL)
517     {
518       empathy_tp_call_close (priv->call);
519       g_object_unref (priv->call);
520     }
521
522   priv->call = NULL;
523 }
524
525 /**
526  * empathy_call_handler_has_initial_video:
527  * @handler: an #EmpathyCallHandler
528  *
529  * Return %TRUE if the call managed by this #EmpathyCallHandler was
530  * created with video enabled
531  *
532  * Return value: %TRUE if the call was created as a video conversation.
533  */
534 gboolean
535 empathy_call_handler_has_initial_video (EmpathyCallHandler *handler)
536 {
537   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
538
539   return priv->initial_video;
540 }
541