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 "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)
46 static guint signals[LAST_SIGNAL] = {0};
54 /* private structure */
57 gboolean dispose_has_run;
59 EmpathyContact *contact;
62 } EmpathyCallHandlerPriv;
64 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
67 empathy_call_handler_dispose (GObject *object)
69 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
71 if (priv->dispose_has_run)
74 priv->dispose_has_run = TRUE;
76 if (priv->contact != NULL)
77 g_object_unref (priv->contact);
81 if (priv->tfchannel != NULL)
82 g_object_unref (priv->tfchannel);
84 priv->tfchannel = NULL;
86 if (priv->call != NULL)
88 empathy_tp_call_close (priv->call);
89 g_object_unref (priv->call);
94 /* release any references held by the object here */
95 if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose)
96 G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
100 empathy_call_handler_finalize (GObject *object)
102 /* free any data held directly by the object here */
103 if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize)
104 G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize (object);
108 empathy_call_handler_init (EmpathyCallHandler *obj)
110 EmpathyCallHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
111 EMPATHY_TYPE_CALL_HANDLER, EmpathyCallHandlerPriv);
117 empathy_call_handler_set_property (GObject *object,
118 guint property_id, const GValue *value, GParamSpec *pspec)
120 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
125 priv->contact = g_value_dup_object (value);
128 priv->call = g_value_dup_object (value);
131 priv->bus = g_value_dup_object (value);
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
139 empathy_call_handler_get_property (GObject *object,
140 guint property_id, GValue *value, GParamSpec *pspec)
142 EmpathyCallHandlerPriv *priv = GET_PRIV (object);
147 g_value_set_object (value, priv->contact);
150 g_value_set_object (value, priv->call);
153 g_value_set_object (value, priv->bus);
156 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
162 empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
164 GObjectClass *object_class = G_OBJECT_CLASS (klass);
165 GParamSpec *param_spec;
167 g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
169 object_class->set_property = empathy_call_handler_set_property;
170 object_class->get_property = empathy_call_handler_get_property;
171 object_class->dispose = empathy_call_handler_dispose;
172 object_class->finalize = empathy_call_handler_finalize;
174 param_spec = g_param_spec_object ("contact",
175 "contact", "The remote contact",
176 EMPATHY_TYPE_CONTACT,
177 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
178 g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
180 param_spec = g_param_spec_object ("gst-bus",
181 "gst-bus", "The gstreamer bus",
183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
184 g_object_class_install_property (object_class, PROP_GST_BUS, param_spec);
186 param_spec = g_param_spec_object ("tp-call",
187 "tp-call", "The calls channel wrapper",
188 EMPATHY_TYPE_TP_CALL,
189 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
190 g_object_class_install_property (object_class, PROP_TP_CALL, param_spec);
192 signals[CONFERENCE_ADDED] =
193 g_signal_new ("conference-added", G_TYPE_FROM_CLASS (klass),
194 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
195 g_cclosure_marshal_VOID__OBJECT,
197 1, FS_TYPE_CONFERENCE);
199 signals[SRC_PAD_ADDED] =
200 g_signal_new ("src-pad-added", G_TYPE_FROM_CLASS (klass),
201 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
202 _empathy_marshal_VOID__OBJECT_UINT,
204 2, GST_TYPE_PAD, G_TYPE_UINT);
206 signals[SINK_PAD_ADDED] =
207 g_signal_new ("sink-pad-added", G_TYPE_FROM_CLASS (klass),
208 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
209 _empathy_marshal_VOID__OBJECT_UINT,
211 2, GST_TYPE_PAD, G_TYPE_UINT);
214 g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
215 G_SIGNAL_RUN_LAST, 0, NULL, NULL,
216 g_cclosure_marshal_VOID__VOID,
222 empathy_call_handler_new_for_contact (EmpathyContact *contact)
224 return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
225 "contact", contact, NULL));
229 empathy_call_handler_new_for_channel (EmpathyTpCall *call)
231 return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
232 "tp-call", call, NULL));
236 empathy_call_handler_pipeline_bus_watch (GstBus *bus, GstMessage *message,
239 EmpathyCallHandler *handler = EMPATHY_CALL_HANDLER (user_data);
240 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
242 g_assert (priv->tfchannel != NULL);
244 tf_channel_bus_message (priv->tfchannel, message);
250 empathy_call_handler_tf_channel_session_created_cb (TfChannel *tfchannel,
251 FsConference *conference, FsParticipant *participant,
252 EmpathyCallHandler *self)
254 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
256 gst_bus_add_watch (priv->bus, empathy_call_handler_pipeline_bus_watch, self);
258 g_signal_emit (G_OBJECT (self), signals[CONFERENCE_ADDED], 0,
259 GST_ELEMENT (conference));
263 empathy_call_handler_tf_stream_src_pad_added_cb (TfStream *stream,
264 GstPad *pad, FsCodec *codec, EmpathyCallHandler *handler)
268 g_object_get (stream, "media-type", &media_type, NULL);
270 g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
276 empathy_call_handler_tf_stream_request_resource_cb (TfStream *stream,
277 guint direction, EmpathyTpCall *call)
283 empathy_call_handler_tf_channel_stream_created_cb (TfChannel *tfchannel,
284 TfStream *stream, EmpathyCallHandler *handler)
289 g_signal_connect (stream, "src-pad-added",
290 G_CALLBACK (empathy_call_handler_tf_stream_src_pad_added_cb), handler);
291 g_signal_connect (stream, "request-resource",
292 G_CALLBACK (empathy_call_handler_tf_stream_request_resource_cb),
295 g_object_get (stream, "media-type", &media_type,
296 "sink-pad", &spad, NULL);
298 g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
301 gst_object_unref (spad);
305 empathy_call_handler_tf_channel_closed_cb (TfChannel *tfchannel,
306 EmpathyCallHandler *handler)
308 g_signal_emit (G_OBJECT (handler), signals[CLOSED], 0);
312 empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
314 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
317 g_object_get (priv->call, "channel", &channel, NULL);
319 g_assert (channel != NULL);
321 priv->tfchannel = tf_channel_new (channel);
323 /* Set up the telepathy farsight channel */
324 g_signal_connect (priv->tfchannel, "session-created",
325 G_CALLBACK (empathy_call_handler_tf_channel_session_created_cb), self);
326 g_signal_connect (priv->tfchannel, "stream-created",
327 G_CALLBACK (empathy_call_handler_tf_channel_stream_created_cb), self);
328 g_signal_connect (priv->tfchannel, "closed",
329 G_CALLBACK (empathy_call_handler_tf_channel_closed_cb), self);
331 g_object_unref (channel);
335 empathy_call_handler_request_cb (EmpathyDispatchOperation *operation,
336 const GError *error, gpointer user_data)
338 EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
339 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
344 priv->call = EMPATHY_TP_CALL (
345 empathy_dispatch_operation_get_channel_wrapper (operation));
347 g_object_ref (priv->call);
349 empathy_call_handler_start_tpfs (self);
351 empathy_tp_call_to (priv->call, priv->contact);
353 empathy_dispatch_operation_claim (operation);
357 empathy_call_handler_contact_ready_cb (EmpathyContact *contact,
358 const GError *error, gpointer user_data, GObject *object)
360 EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
361 EmpathyCallHandlerPriv *priv = GET_PRIV (self);
362 EmpathyDispatcher *dispatcher;
366 GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
367 (GDestroyNotify) tp_g_value_slice_free);
369 g_assert (priv->contact != NULL);
371 dispatcher = empathy_dispatcher_dup_singleton ();
372 account = empathy_contact_get_account (priv->contact);
373 allowed = empathy_dispatcher_find_channel_class (dispatcher, account,
374 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT);
376 if (!tp_strv_contains ((const gchar * const *)allowed,
377 TP_IFACE_CHANNEL ".TargetHandle"))
378 g_assert_not_reached ();
380 /* org.freedesktop.Telepathy.Channel.ChannelType */
381 value = tp_g_value_slice_new (G_TYPE_STRING);
382 g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA);
383 g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
385 /* org.freedesktop.Telepathy.Channel.TargetHandleType */
386 value = tp_g_value_slice_new (G_TYPE_UINT);
387 g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
388 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
390 /* org.freedesktop.Telepathy.Channel.TargetHandle*/
391 value = tp_g_value_slice_new (G_TYPE_UINT);
392 g_value_set_uint (value, empathy_contact_get_handle (priv->contact));
393 g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
395 empathy_dispatcher_create_channel (dispatcher, account,
396 request, empathy_call_handler_request_cb, self);
398 g_object_unref (dispatcher);
402 empathy_call_handler_start_call (EmpathyCallHandler *handler)
405 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
407 if (priv->call == NULL)
409 empathy_contact_call_when_ready (priv->contact,
410 EMPATHY_CONTACT_READY_HANDLE,
411 empathy_call_handler_contact_ready_cb, NULL, NULL, G_OBJECT (handler));
415 empathy_call_handler_start_tpfs (handler);
416 empathy_tp_call_accept_incoming_call (priv->call);
421 empathy_call_handler_set_bus (EmpathyCallHandler *handler, GstBus *bus)
423 EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
425 g_assert (priv->bus == NULL);
427 priv->bus = g_object_ref (bus);