2 * empathy-call-handler.c - Source for EmpathyCallHandler
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
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.
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.
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
25 #include <telepathy-glib/util.h>
27 #include <telepathy-farsight/channel.h>
28 #include <telepathy-farsight/stream.h>
30 #include <gst/farsight/fs-element-added-notifier.h>
32 #include "empathy-call-handler.h"
33 #include "empathy-dispatcher.h"
34 #include "empathy-marshal.h"
35 #include "empathy-utils.h"
37 G_DEFINE_TYPE(EmpathyCallHandler, empathy_call_handler, G_TYPE_OBJECT)
49 static guint signals[LAST_SIGNAL] = {0};
59 /* private structure */
62 gboolean dispose_has_run;
64 EmpathyContact *contact;
66 gboolean initial_audio;
67 gboolean initial_video;
68 FsElementAddedNotifier *fsnotifier;
69 } EmpathyCallHandlerPriv;
71 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
74 empathy_call_handler_dispose (GObject *object)
76 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
78 if (priv->dispose_has_run)
81 priv->dispose_has_run = TRUE;
83 if (priv->contact != NULL)
84 g_object_unref (priv->contact);
88 if (priv->tfchannel != NULL)
89 g_object_unref (priv->tfchannel);
91 priv->tfchannel = NULL;
93 if (priv->call != NULL)
95 empathy_tp_call_close (priv->call);
96 g_object_unref (priv->call);
101 if (priv->fsnotifier != NULL)
103 g_object_unref (priv->fsnotifier);
105 priv->fsnotifier = NULL;
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);
113 empathy_call_handler_finalize (GObject *object)
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);
121 empathy_call_handler_init (EmpathyCallHandler *obj)
123 EmpathyCallHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
124 EMPATHY_TYPE_CALL_HANDLER, EmpathyCallHandlerPriv);
130 empathy_call_handler_constructed (GObject *object)
132 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
134 if (priv->contact == NULL)
136 g_object_get (priv->call, "contact", &(priv->contact), NULL);
141 empathy_call_handler_set_property (GObject *object,
142 guint property_id, const GValue *value, GParamSpec *pspec)
144 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
149 priv->contact = g_value_dup_object (value);
152 priv->call = g_value_dup_object (value);
154 case PROP_INITIAL_AUDIO:
155 priv->initial_audio = g_value_get_boolean (value);
157 case PROP_INITIAL_VIDEO:
158 priv->initial_video = g_value_get_boolean (value);
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
166 empathy_call_handler_get_property (GObject *object,
167 guint property_id, GValue *value, GParamSpec *pspec)
169 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
174 g_value_set_object (value, priv->contact);
177 g_value_set_object (value, priv->call);
179 case PROP_INITIAL_AUDIO:
180 g_value_set_boolean (value, priv->initial_audio);
182 case PROP_INITIAL_VIDEO:
183 g_value_set_boolean (value, priv->initial_video);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
192 empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
194 GObjectClass *object_class = G_OBJECT_CLASS (klass);
195 GParamSpec *param_spec;
197 g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
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;
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);
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);
217 param_spec = g_param_spec_boolean ("initial-audio",
218 "initial-audio", "Whether the call should start with audio",
220 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
221 g_object_class_install_property (object_class, PROP_INITIAL_AUDIO,
224 param_spec = g_param_spec_boolean ("initial-video",
225 "initial-video", "Whether the call should start with video",
227 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
228 g_object_class_install_property (object_class, PROP_INITIAL_VIDEO,
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,
236 1, FS_TYPE_CONFERENCE);
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,
243 2, GST_TYPE_PAD, G_TYPE_UINT);
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,
250 2, GST_TYPE_PAD, G_TYPE_UINT);
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);
260 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
261 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
262 g_cclosure_marshal_VOID__VOID,
268 * empathy_call_handler_new_for_contact:
269 * @contact: an #EmpathyContact
271 * Creates a new #EmpathyCallHandler with contact @contact.
273 * Return value: a new #EmpathyCallHandler
276 empathy_call_handler_new_for_contact (EmpathyContact *contact)
278 return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
279 "contact", contact, NULL));
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
288 * Creates a new #EmpathyCallHandler with contact @contact.
290 * Return value: a new #EmpathyCallHandler
293 empathy_call_handler_new_for_contact_with_streams (EmpathyContact *contact,
294 gboolean audio, gboolean video)
296 return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
298 "initial-audio", audio,
299 "initial-video", video,
304 empathy_call_handler_new_for_channel (EmpathyTpCall *call)
306 return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
307 "tp-call", call, NULL));
311 empathy_call_handler_bus_message (EmpathyCallHandler *handler,
312 GstBus *bus, GstMessage *message)
314 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
316 if (priv->tfchannel == NULL)
319 tf_channel_bus_message (priv->tfchannel, message);
323 conference_element_added (FsElementAddedNotifier *notifier,
328 GstElementFactory *factory;
331 factory = gst_element_get_factory (element);
332 name = gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory));
334 if (!tp_strdiff (name, "x264enc"))
336 /* Ensure that the encoder creates the baseline profile */
337 g_object_set (element,
345 else if (!tp_strdiff (name, "gstrtpbin"))
347 /* Lower the jitterbuffer latency to make it more suitable for video
349 g_object_set (element, "latency", 100, NULL);
354 empathy_call_handler_tf_channel_session_created_cb (TfChannel *tfchannel,
355 FsConference *conference, FsParticipant *participant,
356 EmpathyCallHandler *self)
358 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
360 priv->fsnotifier = fs_element_added_notifier_new ();
361 fs_element_added_notifier_add (priv->fsnotifier, GST_BIN (conference));
363 g_signal_connect (priv->fsnotifier, "element-added",
364 G_CALLBACK (conference_element_added), NULL);
366 g_signal_emit (G_OBJECT (self), signals[CONFERENCE_ADDED], 0,
367 GST_ELEMENT (conference));
371 empathy_call_handler_tf_stream_src_pad_added_cb (TfStream *stream,
372 GstPad *pad, FsCodec *codec, EmpathyCallHandler *handler)
376 g_object_get (stream, "media-type", &media_type, NULL);
378 g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
384 empathy_call_handler_tf_stream_request_resource_cb (TfStream *stream,
385 guint direction, EmpathyTpCall *call)
390 g_object_get (G_OBJECT (stream), "media-type", &media_type, NULL);
392 g_signal_emit (G_OBJECT (call),
393 signals[REQUEST_RESOURCE], 0, media_type, direction, &ret);
399 empathy_call_handler_tf_channel_stream_created_cb (TfChannel *tfchannel,
400 TfStream *stream, EmpathyCallHandler *handler)
405 g_signal_connect (stream, "src-pad-added",
406 G_CALLBACK (empathy_call_handler_tf_stream_src_pad_added_cb), handler);
407 g_signal_connect (stream, "request-resource",
408 G_CALLBACK (empathy_call_handler_tf_stream_request_resource_cb),
411 g_object_get (stream, "media-type", &media_type,
412 "sink-pad", &spad, NULL);
414 g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
417 gst_object_unref (spad);
421 empathy_call_handler_tf_channel_closed_cb (TfChannel *tfchannel,
422 EmpathyCallHandler *handler)
424 g_signal_emit (G_OBJECT (handler), signals[CLOSED], 0);
428 empathy_call_handler_tf_channel_codec_config_get_defaults (FsCodec *codecs)
433 for (i = 0; codecs[i].encoding_name != NULL; i++)
434 l = g_list_append (l, fs_codec_copy (codecs + i));
440 empathy_call_handler_tf_channel_codec_config_cb (TfChannel *channel,
441 guint stream_id, FsMediaType media_type, guint direction, gpointer user_data)
443 FsCodec audio_codecs[] = {
444 { FS_CODEC_ID_ANY, "SPEEX", FS_MEDIA_TYPE_AUDIO, 16000, },
445 { FS_CODEC_ID_ANY, "SPEEX", FS_MEDIA_TYPE_AUDIO, 8000, },
447 { FS_CODEC_ID_DISABLE, "DV", FS_MEDIA_TYPE_AUDIO, },
448 { FS_CODEC_ID_DISABLE, "MPA", FS_MEDIA_TYPE_AUDIO, },
449 { FS_CODEC_ID_DISABLE, "VORBIS", FS_MEDIA_TYPE_AUDIO, },
450 { FS_CODEC_ID_DISABLE, "MP3", FS_MEDIA_TYPE_AUDIO, },
453 FsCodec video_codecs[] = {
454 { FS_CODEC_ID_ANY, "H264", FS_MEDIA_TYPE_VIDEO, },
455 { FS_CODEC_ID_ANY, "THEORA", FS_MEDIA_TYPE_VIDEO, },
456 { FS_CODEC_ID_ANY, "H263", FS_MEDIA_TYPE_VIDEO, },
458 { FS_CODEC_ID_DISABLE, "DV", FS_MEDIA_TYPE_VIDEO, },
459 { FS_CODEC_ID_DISABLE, "JPEG", FS_MEDIA_TYPE_VIDEO, },
460 { FS_CODEC_ID_DISABLE, "MPV", FS_MEDIA_TYPE_VIDEO, },
466 case FS_MEDIA_TYPE_AUDIO:
467 return empathy_call_handler_tf_channel_codec_config_get_defaults
469 case FS_MEDIA_TYPE_VIDEO:
470 return empathy_call_handler_tf_channel_codec_config_get_defaults
478 empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
480 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
483 g_object_get (priv->call, "channel", &channel, NULL);
485 g_assert (channel != NULL);
487 priv->tfchannel = tf_channel_new (channel);
489 /* Set up the telepathy farsight channel */
490 g_signal_connect (priv->tfchannel, "session-created",
491 G_CALLBACK (empathy_call_handler_tf_channel_session_created_cb), self);
492 g_signal_connect (priv->tfchannel, "stream-created",
493 G_CALLBACK (empathy_call_handler_tf_channel_stream_created_cb), self);
494 g_signal_connect (priv->tfchannel, "closed",
495 G_CALLBACK (empathy_call_handler_tf_channel_closed_cb), self);
496 g_signal_connect (priv->tfchannel, "stream-get-codec-config",
497 G_CALLBACK (empathy_call_handler_tf_channel_codec_config_cb), self);
499 g_object_unref (channel);
503 empathy_call_handler_request_cb (EmpathyDispatchOperation *operation,
504 const GError *error, gpointer user_data)
506 EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
507 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
512 priv->call = EMPATHY_TP_CALL (
513 empathy_dispatch_operation_get_channel_wrapper (operation));
515 g_object_ref (priv->call);
517 empathy_call_handler_start_tpfs (self);
519 empathy_tp_call_to (priv->call, priv->contact,
520 priv->initial_audio, priv->initial_video);
522 empathy_dispatch_operation_claim (operation);
526 empathy_call_handler_start_call (EmpathyCallHandler *handler)
529 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
530 EmpathyDispatcher *dispatcher;
531 TpConnection *connection;
536 if (priv->call != NULL)
538 empathy_call_handler_start_tpfs (handler);
539 empathy_tp_call_accept_incoming_call (priv->call);
543 g_assert (priv->contact != NULL);
545 dispatcher = empathy_dispatcher_dup_singleton ();
546 connection = empathy_contact_get_connection (priv->contact);
547 allowed = empathy_dispatcher_find_channel_class (dispatcher, connection,
548 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT);
550 if (!tp_strv_contains ((const gchar * const *) allowed,
551 TP_IFACE_CHANNEL ".TargetHandle"))
554 request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
555 (GDestroyNotify) tp_g_value_slice_free);
557 /* org.freedesktop.Telepathy.Channel.ChannelType */
558 value = tp_g_value_slice_new (G_TYPE_STRING);
559 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA);
560 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
562 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
563 value = tp_g_value_slice_new (G_TYPE_UINT);
564 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
565 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
567 /* org.freedesktop.Telepathy.Channel.TargetHandle*/
568 value = tp_g_value_slice_new (G_TYPE_UINT);
569 g_value_set_uint (value, empathy_contact_get_handle (priv->contact));
570 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
572 empathy_dispatcher_create_channel (dispatcher, connection,
573 request, empathy_call_handler_request_cb, handler);
575 g_object_unref (dispatcher);