2 * empathy-call-handler.c - Source for EmpathyCallHandler
3 * Copyright (C) 2008-2009 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 "empathy-call-handler.h"
31 #include "empathy-dispatcher.h"
32 #include "empathy-marshal.h"
33 #include "empathy-utils.h"
35 G_DEFINE_TYPE(EmpathyCallHandler, empathy_call_handler, G_TYPE_OBJECT)
47 static guint signals[LAST_SIGNAL] = {0};
57 /* private structure */
60 gboolean dispose_has_run;
62 EmpathyContact *contact;
64 gboolean initial_audio;
65 gboolean initial_video;
66 } EmpathyCallHandlerPriv;
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
71 empathy_call_handler_dispose (GObject *object)
73 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
75 if (priv->dispose_has_run)
78 priv->dispose_has_run = TRUE;
80 if (priv->contact != NULL)
81 g_object_unref (priv->contact);
85 if (priv->tfchannel != NULL)
86 g_object_unref (priv->tfchannel);
88 priv->tfchannel = NULL;
90 if (priv->call != NULL)
92 empathy_tp_call_close (priv->call);
93 g_object_unref (priv->call);
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);
104 empathy_call_handler_finalize (GObject *object)
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);
112 empathy_call_handler_init (EmpathyCallHandler *obj)
114 EmpathyCallHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
115 EMPATHY_TYPE_CALL_HANDLER, EmpathyCallHandlerPriv);
121 empathy_call_handler_constructed (GObject *object)
123 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
125 if (priv->contact == NULL)
127 g_object_get (priv->call, "contact", &(priv->contact), NULL);
132 empathy_call_handler_set_property (GObject *object,
133 guint property_id, const GValue *value, GParamSpec *pspec)
135 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
140 priv->contact = g_value_dup_object (value);
143 priv->call = g_value_dup_object (value);
145 case PROP_INITIAL_AUDIO:
146 priv->initial_audio = g_value_get_boolean (value);
148 case PROP_INITIAL_VIDEO:
149 priv->initial_video = g_value_get_boolean (value);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
157 empathy_call_handler_get_property (GObject *object,
158 guint property_id, GValue *value, GParamSpec *pspec)
160 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
165 g_value_set_object (value, priv->contact);
168 g_value_set_object (value, priv->call);
170 case PROP_INITIAL_AUDIO:
171 g_value_set_boolean (value, priv->initial_audio);
173 case PROP_INITIAL_VIDEO:
174 g_value_set_boolean (value, priv->initial_video);
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
183 empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
185 GObjectClass *object_class = G_OBJECT_CLASS (klass);
186 GParamSpec *param_spec;
188 g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
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;
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);
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);
208 param_spec = g_param_spec_boolean ("initial-audio",
209 "initial-audio", "Whether the call should start with audio",
211 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
212 g_object_class_install_property (object_class, PROP_INITIAL_AUDIO,
215 param_spec = g_param_spec_boolean ("initial-video",
216 "initial-video", "Whether the call should start with video",
218 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
219 g_object_class_install_property (object_class, PROP_INITIAL_VIDEO,
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,
227 1, FS_TYPE_CONFERENCE);
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,
234 2, GST_TYPE_PAD, G_TYPE_UINT);
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,
241 2, GST_TYPE_PAD, G_TYPE_UINT);
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);
251 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
252 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
253 g_cclosure_marshal_VOID__VOID,
259 * empathy_call_handler_new_for_contact:
260 * @contact: an #EmpathyContact
262 * Creates a new #EmpathyCallHandler with contact @contact.
264 * Return value: a new #EmpathyCallHandler
267 empathy_call_handler_new_for_contact (EmpathyContact *contact)
269 return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
270 "contact", contact, NULL));
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
279 * Creates a new #EmpathyCallHandler with contact @contact.
281 * Return value: a new #EmpathyCallHandler
284 empathy_call_handler_new_for_contact_with_streams (EmpathyContact *contact,
285 gboolean audio, gboolean video)
287 return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
289 "initial-audio", audio,
290 "initial-video", video,
295 empathy_call_handler_new_for_channel (EmpathyTpCall *call)
297 return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
299 "initial-video", empathy_tp_call_is_receiving_video (call),
304 empathy_call_handler_bus_message (EmpathyCallHandler *handler,
305 GstBus *bus, GstMessage *message)
307 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
309 if (priv->tfchannel == NULL)
312 tf_channel_bus_message (priv->tfchannel, message);
316 empathy_call_handler_tf_channel_session_created_cb (TfChannel *tfchannel,
317 FsConference *conference, FsParticipant *participant,
318 EmpathyCallHandler *self)
320 g_signal_emit (G_OBJECT (self), signals[CONFERENCE_ADDED], 0,
321 GST_ELEMENT (conference));
325 empathy_call_handler_tf_stream_src_pad_added_cb (TfStream *stream,
326 GstPad *pad, FsCodec *codec, EmpathyCallHandler *handler)
330 g_object_get (stream, "media-type", &media_type, NULL);
332 g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
338 empathy_call_handler_tf_stream_request_resource_cb (TfStream *stream,
339 guint direction, EmpathyTpCall *call)
344 g_object_get (G_OBJECT (stream), "media-type", &media_type, NULL);
346 g_signal_emit (G_OBJECT (call),
347 signals[REQUEST_RESOURCE], 0, media_type, direction, &ret);
353 empathy_call_handler_tf_channel_stream_created_cb (TfChannel *tfchannel,
354 TfStream *stream, EmpathyCallHandler *handler)
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),
365 g_object_get (stream, "media-type", &media_type,
366 "sink-pad", &spad, NULL);
368 g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
371 gst_object_unref (spad);
375 empathy_call_handler_tf_channel_closed_cb (TfChannel *tfchannel,
376 EmpathyCallHandler *handler)
378 g_signal_emit (G_OBJECT (handler), signals[CLOSED], 0);
382 empathy_call_handler_tf_channel_codec_config_cb (TfChannel *channel,
383 guint stream_id, FsMediaType media_type, guint direction, gpointer user_data)
385 gchar *filename = empathy_file_lookup ("codec-preferences", "data");
387 GError *error = NULL;
389 codecs = fs_codec_list_from_keyfile (filename, &error);
394 g_warning ("No codec-preferences file: %s",
395 error ? error->message : "No error message");
397 g_clear_error (&error);
403 empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
405 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
408 g_object_get (priv->call, "channel", &channel, NULL);
410 g_assert (channel != NULL);
412 priv->tfchannel = tf_channel_new (channel);
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);
424 g_object_unref (channel);
428 empathy_call_handler_request_cb (EmpathyDispatchOperation *operation,
429 const GError *error, gpointer user_data)
431 EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
432 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
437 priv->call = EMPATHY_TP_CALL (
438 empathy_dispatch_operation_get_channel_wrapper (operation));
440 g_object_ref (priv->call);
442 empathy_call_handler_start_tpfs (self);
444 empathy_tp_call_to (priv->call, priv->contact,
445 priv->initial_audio, priv->initial_video);
447 empathy_dispatch_operation_claim (operation);
451 empathy_call_handler_start_call (EmpathyCallHandler *handler)
454 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
455 EmpathyDispatcher *dispatcher;
456 TpConnection *connection;
461 if (priv->call != NULL)
463 empathy_call_handler_start_tpfs (handler);
464 empathy_tp_call_accept_incoming_call (priv->call);
468 g_assert (priv->contact != NULL);
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);
479 g_list_free (classes);
481 request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
482 (GDestroyNotify) tp_g_value_slice_free);
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);
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);
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);
499 empathy_dispatcher_create_channel (dispatcher, connection,
500 request, empathy_call_handler_request_cb, handler);
502 g_object_unref (dispatcher);
506 * empathy_call_handler_stop_call:
507 * @handler: an #EmpathyCallHandler
509 * Closes the #EmpathyCallHandler's call and frees its resources.
512 empathy_call_handler_stop_call (EmpathyCallHandler *handler)
514 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
516 if (priv->call != NULL)
518 empathy_tp_call_close (priv->call);
519 g_object_unref (priv->call);
526 * empathy_call_handler_has_initial_video:
527 * @handler: an #EmpathyCallHandler
529 * Return %TRUE if the call managed by this #EmpathyCallHandler was
530 * created with video enabled
532 * Return value: %TRUE if the call was created as a video conversation.
535 empathy_call_handler_has_initial_video (EmpathyCallHandler *handler)
537 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
539 return priv->initial_video;