2 * empathy-streamed-media-factory.c - Source for EmpathyStreamedMediaFactory
3 * Copyright (C) 2008-2011 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/account-channel-request.h>
26 #include <telepathy-glib/simple-handler.h>
27 #include <telepathy-glib/interfaces.h>
28 #include <telepathy-glib/util.h>
30 #include <libempathy/empathy-request-util.h>
31 #include <libempathy/empathy-utils.h>
33 #include "empathy-streamed-media-factory.h"
34 #include "empathy-streamed-media-handler.h"
35 #include "src-marshal.h"
37 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
38 #include <libempathy/empathy-debug.h>
40 G_DEFINE_TYPE(EmpathyStreamedMediaFactory, empathy_streamed_media_factory, G_TYPE_OBJECT)
42 static void handle_channels_cb (TpSimpleHandler *handler,
44 TpConnection *connection,
46 GList *requests_satisfied,
47 gint64 user_action_time,
48 TpHandleChannelsContext *context,
54 NEW_STREAMED_MEDIA_HANDLER,
58 static guint signals[LAST_SIGNAL] = {0};
60 /* private structure */
62 TpBaseClient *handler;
63 gboolean dispose_has_run;
64 } EmpathyStreamedMediaFactoryPriv;
66 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStreamedMediaFactory)
68 static GObject *call_factory = NULL;
71 empathy_streamed_media_factory_init (EmpathyStreamedMediaFactory *obj)
73 EmpathyStreamedMediaFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
74 EMPATHY_TYPE_STREAMED_MEDIA_FACTORY, EmpathyStreamedMediaFactoryPriv);
80 dbus = tp_dbus_daemon_dup (&error);
83 g_warning ("Failed to get TpDBusDaemon: %s", error->message);
88 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
89 EMPATHY_AV_BUS_NAME_SUFFIX, FALSE, handle_channels_cb, obj, NULL);
91 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
92 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
93 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
94 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
97 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
98 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
99 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
100 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
101 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
104 tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
105 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
106 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
107 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
108 TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
111 tp_base_client_add_handler_capabilities_varargs (priv->handler,
112 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
113 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
114 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/video/h264",
117 g_object_unref (dbus);
121 empathy_streamed_media_factory_constructor (GType type, guint n_construct_params,
122 GObjectConstructParam *construct_params)
124 g_return_val_if_fail (call_factory == NULL, NULL);
126 call_factory = G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->constructor
127 (type, n_construct_params, construct_params);
128 g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
134 empathy_streamed_media_factory_finalize (GObject *object)
136 /* free any data held directly by the object here */
138 if (G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->finalize)
139 G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->finalize (object);
143 empathy_streamed_media_factory_dispose (GObject *object)
145 EmpathyStreamedMediaFactoryPriv *priv = GET_PRIV (object);
147 if (priv->dispose_has_run)
150 priv->dispose_has_run = TRUE;
152 tp_clear_object (&priv->handler);
154 if (G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->dispose)
155 G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->dispose (object);
159 empathy_streamed_media_factory_class_init (
160 EmpathyStreamedMediaFactoryClass *empathy_streamed_media_factory_class)
162 GObjectClass *object_class = G_OBJECT_CLASS (empathy_streamed_media_factory_class);
164 g_type_class_add_private (empathy_streamed_media_factory_class,
165 sizeof (EmpathyStreamedMediaFactoryPriv));
167 object_class->constructor = empathy_streamed_media_factory_constructor;
168 object_class->dispose = empathy_streamed_media_factory_dispose;
169 object_class->finalize = empathy_streamed_media_factory_finalize;
171 signals[NEW_STREAMED_MEDIA_HANDLER] =
172 g_signal_new ("new-streamed-media-handler",
173 G_TYPE_FROM_CLASS (empathy_streamed_media_factory_class),
174 G_SIGNAL_RUN_LAST, 0,
176 _src_marshal_VOID__OBJECT_BOOLEAN,
178 2, EMPATHY_TYPE_STREAMED_MEDIA_HANDLER, G_TYPE_BOOLEAN);
181 EmpathyStreamedMediaFactory *
182 empathy_streamed_media_factory_initialise (void)
184 g_return_val_if_fail (call_factory == NULL, NULL);
186 return EMPATHY_STREAMED_MEDIA_FACTORY (g_object_new (EMPATHY_TYPE_STREAMED_MEDIA_FACTORY, NULL));
189 EmpathyStreamedMediaFactory *
190 empathy_streamed_media_factory_get (void)
192 g_return_val_if_fail (call_factory != NULL, NULL);
194 return EMPATHY_STREAMED_MEDIA_FACTORY (call_factory);
198 create_streamed_media_handler (EmpathyStreamedMediaFactory *factory,
199 EmpathyTpStreamedMedia *call)
201 EmpathyStreamedMediaHandler *handler;
203 g_return_if_fail (factory != NULL);
205 handler = empathy_streamed_media_handler_new_for_channel (call);
207 g_signal_emit (factory, signals[NEW_STREAMED_MEDIA_HANDLER], 0,
210 g_object_unref (handler);
214 call_status_changed_cb (EmpathyTpStreamedMedia *call,
216 EmpathyStreamedMediaFactory *self)
218 if (empathy_tp_streamed_media_get_status (call) <= EMPATHY_TP_STREAMED_MEDIA_STATUS_READYING)
221 create_streamed_media_handler (self, call);
223 g_signal_handlers_disconnect_by_func (call, call_status_changed_cb, self);
224 g_object_unref (call);
228 handle_channels_cb (TpSimpleHandler *handler,
230 TpConnection *connection,
232 GList *requests_satisfied,
233 gint64 user_action_time,
234 TpHandleChannelsContext *context,
237 EmpathyStreamedMediaFactory *self = user_data;
240 for (l = channels; l != NULL; l = g_list_next (l))
242 TpChannel *channel = l->data;
243 EmpathyTpStreamedMedia *call;
245 if (tp_proxy_get_invalidated (channel) != NULL)
248 if (tp_channel_get_channel_type_id (channel) !=
249 TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
252 call = empathy_tp_streamed_media_new (account, channel);
254 if (empathy_tp_streamed_media_get_status (call) <= EMPATHY_TP_STREAMED_MEDIA_STATUS_READYING)
256 /* We have to wait that the TpStreamedMedia is ready as the
257 * call-handler rely on it. */
258 tp_g_signal_connect_object (call, "notify::status",
259 G_CALLBACK (call_status_changed_cb), self, 0);
263 create_streamed_media_handler (self, call);
264 g_object_unref (call);
267 tp_handle_channels_context_accept (context);
271 empathy_streamed_media_factory_register (EmpathyStreamedMediaFactory *self,
274 EmpathyStreamedMediaFactoryPriv *priv = GET_PRIV (self);
276 return tp_base_client_register (priv->handler, error);