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