]> git.0d.be Git - empathy.git/blob - src/empathy-streamed-media-factory.c
Un-pausing incoming messages should update All's active buffer too
[empathy.git] / src / empathy-streamed-media-factory.c
1 /*
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>
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/account-channel-request.h>
26 #include <telepathy-glib/simple-handler.h>
27 #include <telepathy-glib/interfaces.h>
28 #include <telepathy-glib/util.h>
29
30 #include <libempathy/empathy-request-util.h>
31 #include <libempathy/empathy-utils.h>
32
33 #include "empathy-streamed-media-factory.h"
34 #include "empathy-streamed-media-handler.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
37 #include <libempathy/empathy-debug.h>
38
39 G_DEFINE_TYPE(EmpathyStreamedMediaFactory, empathy_streamed_media_factory, G_TYPE_OBJECT)
40
41 static void handle_channels_cb (TpSimpleHandler *handler,
42     TpAccount *account,
43     TpConnection *connection,
44     GList *channels,
45     GList *requests_satisfied,
46     gint64 user_action_time,
47     TpHandleChannelsContext *context,
48     gpointer user_data);
49
50 /* signal enum */
51 enum
52 {
53     NEW_STREAMED_MEDIA_HANDLER,
54     LAST_SIGNAL
55 };
56
57 static guint signals[LAST_SIGNAL] = {0};
58
59 /* private structure */
60 typedef struct {
61   TpBaseClient *handler;
62   gboolean dispose_has_run;
63 } EmpathyStreamedMediaFactoryPriv;
64
65 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyStreamedMediaFactory)
66
67 static GObject *call_factory = NULL;
68
69 static void
70 empathy_streamed_media_factory_init (EmpathyStreamedMediaFactory *obj)
71 {
72   EmpathyStreamedMediaFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
73     EMPATHY_TYPE_STREAMED_MEDIA_FACTORY, EmpathyStreamedMediaFactoryPriv);
74   TpAccountManager *am;
75
76   obj->priv = priv;
77
78   am = tp_account_manager_dup ();
79
80   priv->handler = tp_simple_handler_new_with_am (am, FALSE, FALSE,
81       EMPATHY_AV_BUS_NAME_SUFFIX, FALSE, handle_channels_cb, obj, NULL);
82
83   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
84         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
85           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
86         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
87         NULL));
88
89   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
90         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
91           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
92         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
93         TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
94         NULL));
95
96   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
97         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
98           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
99         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
100         TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
101         NULL));
102
103   tp_base_client_add_handler_capabilities_varargs (priv->handler,
104     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
105     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
106     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/video/h264",
107     NULL);
108
109   g_object_unref (am);
110 }
111
112 static GObject *
113 empathy_streamed_media_factory_constructor (GType type, guint n_construct_params,
114   GObjectConstructParam *construct_params)
115 {
116   g_return_val_if_fail (call_factory == NULL, NULL);
117
118   call_factory = G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->constructor
119           (type, n_construct_params, construct_params);
120   g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
121
122   return call_factory;
123 }
124
125 static void
126 empathy_streamed_media_factory_finalize (GObject *object)
127 {
128   /* free any data held directly by the object here */
129
130   if (G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->finalize)
131     G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->finalize (object);
132 }
133
134 static void
135 empathy_streamed_media_factory_dispose (GObject *object)
136 {
137   EmpathyStreamedMediaFactoryPriv *priv = GET_PRIV (object);
138
139   if (priv->dispose_has_run)
140     return;
141
142   priv->dispose_has_run = TRUE;
143
144   tp_clear_object (&priv->handler);
145
146   if (G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->dispose)
147     G_OBJECT_CLASS (empathy_streamed_media_factory_parent_class)->dispose (object);
148 }
149
150 static void
151 empathy_streamed_media_factory_class_init (
152   EmpathyStreamedMediaFactoryClass *empathy_streamed_media_factory_class)
153 {
154   GObjectClass *object_class = G_OBJECT_CLASS (empathy_streamed_media_factory_class);
155
156   g_type_class_add_private (empathy_streamed_media_factory_class,
157     sizeof (EmpathyStreamedMediaFactoryPriv));
158
159   object_class->constructor = empathy_streamed_media_factory_constructor;
160   object_class->dispose = empathy_streamed_media_factory_dispose;
161   object_class->finalize = empathy_streamed_media_factory_finalize;
162
163   signals[NEW_STREAMED_MEDIA_HANDLER] =
164     g_signal_new ("new-streamed-media-handler",
165       G_TYPE_FROM_CLASS (empathy_streamed_media_factory_class),
166       G_SIGNAL_RUN_LAST, 0,
167       NULL, NULL,
168       g_cclosure_marshal_generic,
169       G_TYPE_NONE,
170       2, EMPATHY_TYPE_STREAMED_MEDIA_HANDLER, G_TYPE_BOOLEAN);
171 }
172
173 EmpathyStreamedMediaFactory *
174 empathy_streamed_media_factory_initialise (void)
175 {
176   g_return_val_if_fail (call_factory == NULL, NULL);
177
178   return EMPATHY_STREAMED_MEDIA_FACTORY (g_object_new (EMPATHY_TYPE_STREAMED_MEDIA_FACTORY, NULL));
179 }
180
181 EmpathyStreamedMediaFactory *
182 empathy_streamed_media_factory_get (void)
183 {
184   g_return_val_if_fail (call_factory != NULL, NULL);
185
186   return EMPATHY_STREAMED_MEDIA_FACTORY (call_factory);
187 }
188
189 static void
190 create_streamed_media_handler (EmpathyStreamedMediaFactory *factory,
191   EmpathyTpStreamedMedia *call)
192 {
193   EmpathyStreamedMediaHandler *handler;
194
195   g_return_if_fail (factory != NULL);
196
197   handler = empathy_streamed_media_handler_new_for_channel (call);
198
199   g_signal_emit (factory, signals[NEW_STREAMED_MEDIA_HANDLER], 0,
200     handler, FALSE);
201
202   g_object_unref (handler);
203 }
204
205 static void
206 call_status_changed_cb (EmpathyTpStreamedMedia *call,
207     GParamSpec *spec,
208     EmpathyStreamedMediaFactory *self)
209 {
210   if (empathy_tp_streamed_media_get_status (call) <= EMPATHY_TP_STREAMED_MEDIA_STATUS_READYING)
211     return;
212
213   create_streamed_media_handler (self, call);
214
215   g_signal_handlers_disconnect_by_func (call, call_status_changed_cb, self);
216   g_object_unref (call);
217 }
218
219 static void
220 handle_channels_cb (TpSimpleHandler *handler,
221     TpAccount *account,
222     TpConnection *connection,
223     GList *channels,
224     GList *requests_satisfied,
225     gint64 user_action_time,
226     TpHandleChannelsContext *context,
227     gpointer user_data)
228 {
229   EmpathyStreamedMediaFactory *self = user_data;
230   GList *l;
231
232   for (l = channels; l != NULL; l = g_list_next (l))
233     {
234       TpChannel *channel = l->data;
235       EmpathyTpStreamedMedia *call;
236
237       if (tp_proxy_get_invalidated (channel) != NULL)
238         continue;
239
240       if (tp_channel_get_channel_type_id (channel) !=
241           TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
242         continue;
243
244       call = empathy_tp_streamed_media_new (account, channel);
245
246       if (empathy_tp_streamed_media_get_status (call) <= EMPATHY_TP_STREAMED_MEDIA_STATUS_READYING)
247         {
248           /* We have to wait that the TpStreamedMedia is ready as the
249            * call-handler rely on it. */
250           tp_g_signal_connect_object (call, "notify::status",
251               G_CALLBACK (call_status_changed_cb), self, 0);
252           continue;
253         }
254
255       create_streamed_media_handler (self, call);
256       g_object_unref (call);
257     }
258
259   tp_handle_channels_context_accept (context);
260 }
261
262 gboolean
263 empathy_streamed_media_factory_register (EmpathyStreamedMediaFactory *self,
264     GError **error)
265 {
266   EmpathyStreamedMediaFactoryPriv *priv = GET_PRIV (self);
267
268   return tp_base_client_register (priv->handler, error);
269 }