]> git.0d.be Git - empathy.git/blob - libempathy/empathy-call-handler.c
Hardcode some a default codec preferences
[empathy.git] / libempathy / empathy-call-handler.c
1 /*
2  * empathy-call-handler.c - Source for EmpathyCallHandler
3  * Copyright (C) 2008 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/util.h>
26
27 #include <telepathy-farsight/channel.h>
28 #include <telepathy-farsight/stream.h>
29
30 #include "empathy-call-handler.h"
31 #include "empathy-dispatcher.h"
32 #include "empathy-marshal.h"
33 #include "empathy-utils.h"
34
35 G_DEFINE_TYPE(EmpathyCallHandler, empathy_call_handler, G_TYPE_OBJECT)
36
37 /* signal enum */
38 enum {
39   CONFERENCE_ADDED,
40   SRC_PAD_ADDED,
41   SINK_PAD_ADDED,
42   CLOSED,
43   LAST_SIGNAL
44 };
45
46 static guint signals[LAST_SIGNAL] = {0};
47
48 enum {
49   PROP_TP_CALL = 1,
50   PROP_GST_BUS,
51   PROP_CONTACT
52 };
53
54 /* private structure */
55
56 typedef struct {
57   gboolean dispose_has_run;
58   EmpathyTpCall *call;
59   EmpathyContact *contact;
60   TfChannel *tfchannel;
61 } EmpathyCallHandlerPriv;
62
63 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
64
65 static void
66 empathy_call_handler_dispose (GObject *object)
67 {
68   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
69
70   if (priv->dispose_has_run)
71     return;
72
73   priv->dispose_has_run = TRUE;
74
75   if (priv->contact != NULL)
76     g_object_unref (priv->contact);
77
78   priv->contact = NULL;
79
80   if (priv->tfchannel != NULL)
81     g_object_unref (priv->tfchannel);
82
83   priv->tfchannel = NULL;
84
85   if (priv->call != NULL)
86     {
87       empathy_tp_call_close (priv->call);
88       g_object_unref (priv->call);
89     }
90
91   priv->call = NULL;
92
93   /* release any references held by the object here */
94   if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose)
95     G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
96 }
97
98 static void
99 empathy_call_handler_finalize (GObject *object)
100 {
101   /* free any data held directly by the object here */
102   if (G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize)
103     G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize (object);
104 }
105
106 static void
107 empathy_call_handler_init (EmpathyCallHandler *obj)
108 {
109   EmpathyCallHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
110     EMPATHY_TYPE_CALL_HANDLER, EmpathyCallHandlerPriv);
111
112   obj->priv = priv;
113 }
114
115 static void
116 empathy_call_handler_set_property (GObject *object,
117   guint property_id, const GValue *value, GParamSpec *pspec)
118 {
119   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
120
121   switch (property_id)
122     {
123       case PROP_CONTACT:
124         priv->contact = g_value_dup_object (value);
125         break;
126       case PROP_TP_CALL:
127         priv->call = g_value_dup_object (value);
128         break;
129       default:
130         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
131     }
132 }
133
134 static void
135 empathy_call_handler_get_property (GObject *object,
136   guint property_id, GValue *value, GParamSpec *pspec)
137 {
138   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
139
140   switch (property_id)
141     {
142       case PROP_CONTACT:
143         g_value_set_object (value, priv->contact);
144         break;
145       case PROP_TP_CALL:
146         g_value_set_object (value, priv->call);
147         break;
148       default:
149         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
150     }
151 }
152
153
154 static void
155 empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
156 {
157   GObjectClass *object_class = G_OBJECT_CLASS (klass);
158   GParamSpec *param_spec;
159
160   g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
161
162   object_class->set_property = empathy_call_handler_set_property;
163   object_class->get_property = empathy_call_handler_get_property;
164   object_class->dispose = empathy_call_handler_dispose;
165   object_class->finalize = empathy_call_handler_finalize;
166
167   param_spec = g_param_spec_object ("contact",
168     "contact", "The remote contact",
169     EMPATHY_TYPE_CONTACT,
170     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
171   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
172
173   param_spec = g_param_spec_object ("tp-call",
174     "tp-call", "The calls channel wrapper",
175     EMPATHY_TYPE_TP_CALL,
176     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
177   g_object_class_install_property (object_class, PROP_TP_CALL, param_spec);
178
179   signals[CONFERENCE_ADDED] =
180     g_signal_new ("conference-added", G_TYPE_FROM_CLASS (klass),
181       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
182       g_cclosure_marshal_VOID__OBJECT,
183       G_TYPE_NONE,
184       1, FS_TYPE_CONFERENCE);
185
186   signals[SRC_PAD_ADDED] =
187     g_signal_new ("src-pad-added", G_TYPE_FROM_CLASS (klass),
188       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
189       _empathy_marshal_VOID__OBJECT_UINT,
190       G_TYPE_NONE,
191       2, GST_TYPE_PAD, G_TYPE_UINT);
192
193   signals[SINK_PAD_ADDED] =
194     g_signal_new ("sink-pad-added", G_TYPE_FROM_CLASS (klass),
195       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
196       _empathy_marshal_VOID__OBJECT_UINT,
197       G_TYPE_NONE,
198       2, GST_TYPE_PAD, G_TYPE_UINT);
199
200   signals[CLOSED] =
201     g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
202       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
203       g_cclosure_marshal_VOID__VOID,
204       G_TYPE_NONE,
205       0);
206 }
207
208 EmpathyCallHandler *
209 empathy_call_handler_new_for_contact (EmpathyContact *contact)
210 {
211   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
212     "contact", contact, NULL));
213 }
214
215 EmpathyCallHandler *
216 empathy_call_handler_new_for_channel (EmpathyTpCall *call)
217 {
218   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
219     "tp-call", call, NULL));
220 }
221
222 void
223 empathy_call_handler_bus_message (EmpathyCallHandler *handler,
224   GstBus *bus, GstMessage *message)
225 {
226   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
227
228   if (priv->tfchannel == NULL)
229     return;
230
231   tf_channel_bus_message (priv->tfchannel, message);
232 }
233
234 static void
235 empathy_call_handler_tf_channel_session_created_cb (TfChannel *tfchannel,
236   FsConference *conference, FsParticipant *participant,
237   EmpathyCallHandler *self)
238 {
239   g_signal_emit (G_OBJECT (self), signals[CONFERENCE_ADDED], 0,
240     GST_ELEMENT (conference));
241 }
242
243 static void
244 empathy_call_handler_tf_stream_src_pad_added_cb (TfStream *stream,
245   GstPad *pad, FsCodec *codec, EmpathyCallHandler  *handler)
246 {
247   guint media_type;
248
249   g_object_get (stream, "media-type", &media_type, NULL);
250
251   g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
252     pad, media_type);
253 }
254
255
256 static gboolean
257 empathy_call_handler_tf_stream_request_resource_cb (TfStream *stream,
258   guint direction, EmpathyTpCall *call)
259 {
260   return TRUE;
261 }
262
263 static void
264 empathy_call_handler_tf_channel_stream_created_cb (TfChannel *tfchannel,
265   TfStream *stream, EmpathyCallHandler *handler)
266 {
267   guint media_type;
268   GstPad *spad;
269
270   g_signal_connect (stream, "src-pad-added",
271       G_CALLBACK (empathy_call_handler_tf_stream_src_pad_added_cb), handler);
272   g_signal_connect (stream, "request-resource",
273       G_CALLBACK (empathy_call_handler_tf_stream_request_resource_cb),
274         handler);
275
276   g_object_get (stream, "media-type", &media_type,
277     "sink-pad", &spad, NULL);
278
279   g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
280     spad, media_type);
281
282   gst_object_unref (spad);
283 }
284
285 static void
286 empathy_call_handler_tf_channel_closed_cb (TfChannel *tfchannel,
287   EmpathyCallHandler *handler)
288 {
289   g_signal_emit (G_OBJECT (handler), signals[CLOSED], 0);
290 }
291
292 static GList *
293 empathy_call_handler_tf_channel_codec_config_get_defaults (FsCodec *codecs)
294 {
295   GList *l = NULL;
296   int i;
297
298   for (i = 0; codecs[i].encoding_name != NULL; i++)
299       l = g_list_append (l, fs_codec_copy (codecs + i));
300
301   return l;
302 }
303
304 static GList *
305 empathy_call_handler_tf_channel_codec_config_cb (TfChannel *channel,
306   guint stream_id, FsMediaType media_type, guint direction, gpointer user_data)
307 {
308   FsCodec audio_codecs[] = {
309     { FS_CODEC_ID_ANY, "SPEEX", FS_MEDIA_TYPE_AUDIO, 16000, },
310     { FS_CODEC_ID_ANY, "SPEEX", FS_MEDIA_TYPE_AUDIO, 8000, },
311
312     { FS_CODEC_ID_DISABLE, "DV",     FS_MEDIA_TYPE_AUDIO, },
313     { FS_CODEC_ID_DISABLE, "MPA",    FS_MEDIA_TYPE_AUDIO, },
314     { FS_CODEC_ID_DISABLE, "VORBIS", FS_MEDIA_TYPE_AUDIO, },
315     { FS_CODEC_ID_DISABLE, "MP3",    FS_MEDIA_TYPE_AUDIO, },
316     { 0, NULL, 0,}
317   };
318   FsCodec video_codecs[] = {
319     { FS_CODEC_ID_ANY, "H264",   FS_MEDIA_TYPE_VIDEO, },
320     { FS_CODEC_ID_ANY, "THEORA", FS_MEDIA_TYPE_VIDEO, },
321     { FS_CODEC_ID_ANY, "H263",   FS_MEDIA_TYPE_VIDEO, },
322
323     { FS_CODEC_ID_DISABLE, "DV",   FS_MEDIA_TYPE_VIDEO, },
324     { FS_CODEC_ID_DISABLE, "JPEG", FS_MEDIA_TYPE_VIDEO, },
325     { FS_CODEC_ID_DISABLE, "MPV",  FS_MEDIA_TYPE_VIDEO, },
326     { 0, NULL, 0}
327   };
328
329   switch (media_type)
330     {
331       case FS_MEDIA_TYPE_AUDIO:
332         return empathy_call_handler_tf_channel_codec_config_get_defaults
333           (audio_codecs);
334       case FS_MEDIA_TYPE_VIDEO:
335         return empathy_call_handler_tf_channel_codec_config_get_defaults
336           (video_codecs);
337     }
338
339   return NULL;
340 }
341
342 static void
343 empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
344 {
345   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
346   TpChannel *channel;
347
348   g_object_get (priv->call, "channel", &channel, NULL);
349
350   g_assert (channel != NULL);
351
352   priv->tfchannel = tf_channel_new (channel);
353
354   /* Set up the telepathy farsight channel */
355   g_signal_connect (priv->tfchannel, "session-created",
356       G_CALLBACK (empathy_call_handler_tf_channel_session_created_cb), self);
357   g_signal_connect (priv->tfchannel, "stream-created",
358       G_CALLBACK (empathy_call_handler_tf_channel_stream_created_cb), self);
359   g_signal_connect (priv->tfchannel, "closed",
360       G_CALLBACK (empathy_call_handler_tf_channel_closed_cb), self);
361   g_signal_connect (priv->tfchannel, "stream-get-codec-config",
362       G_CALLBACK (empathy_call_handler_tf_channel_codec_config_cb), self);
363
364   g_object_unref (channel);
365 }
366
367 static void
368 empathy_call_handler_request_cb (EmpathyDispatchOperation *operation,
369   const GError *error, gpointer user_data)
370 {
371   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
372   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
373
374   if (error != NULL)
375     return;
376
377   priv->call = EMPATHY_TP_CALL (
378     empathy_dispatch_operation_get_channel_wrapper (operation));
379
380   g_object_ref (priv->call);
381
382   empathy_call_handler_start_tpfs (self);
383
384   empathy_tp_call_to (priv->call, priv->contact);
385
386   empathy_dispatch_operation_claim (operation);
387 }
388
389 static void
390 empathy_call_handler_contact_ready_cb (EmpathyContact *contact,
391   const GError *error, gpointer user_data, GObject *object)
392 {
393   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
394   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
395   EmpathyDispatcher *dispatcher;
396   McAccount *account;
397   GStrv allowed;
398   GValue *value;
399   GHashTable *request = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
400       (GDestroyNotify) tp_g_value_slice_free);
401
402   g_assert (priv->contact != NULL);
403
404   dispatcher = empathy_dispatcher_dup_singleton ();
405   account = empathy_contact_get_account (priv->contact);
406   allowed = empathy_dispatcher_find_channel_class (dispatcher, account,
407     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA, TP_HANDLE_TYPE_CONTACT);
408
409   if (!tp_strv_contains ((const gchar * const *)allowed,
410       TP_IFACE_CHANNEL ".TargetHandle"))
411     g_assert_not_reached ();
412
413   /* org.freedesktop.Telepathy.Channel.ChannelType */
414   value = tp_g_value_slice_new (G_TYPE_STRING);
415   g_value_set_string (value, TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA);
416   g_hash_table_insert (request, TP_IFACE_CHANNEL ".ChannelType", value);
417
418   /* org.freedesktop.Telepathy.Channel.TargetHandleType */
419   value = tp_g_value_slice_new (G_TYPE_UINT);
420   g_value_set_uint (value, TP_HANDLE_TYPE_CONTACT);
421   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandleType", value);
422
423   /* org.freedesktop.Telepathy.Channel.TargetHandle*/
424   value = tp_g_value_slice_new (G_TYPE_UINT);
425   g_value_set_uint (value, empathy_contact_get_handle (priv->contact));
426   g_hash_table_insert (request, TP_IFACE_CHANNEL ".TargetHandle", value);
427
428   empathy_dispatcher_create_channel (dispatcher, account,
429     request, empathy_call_handler_request_cb, self);
430
431   g_object_unref (dispatcher);
432 }
433
434 void
435 empathy_call_handler_start_call (EmpathyCallHandler *handler)
436 {
437
438   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
439
440   if (priv->call == NULL)
441     {
442       empathy_contact_call_when_ready (priv->contact,
443         EMPATHY_CONTACT_READY_HANDLE,
444         empathy_call_handler_contact_ready_cb, NULL, NULL, G_OBJECT (handler));
445     }
446   else
447     {
448       empathy_call_handler_start_tpfs (handler);
449       empathy_tp_call_accept_incoming_call (priv->call);
450     }
451 }