]> git.0d.be Git - empathy.git/blob - src/empathy-call-handler.c
Don't start the Call when the streams start
[empathy.git] / src / empathy-call-handler.c
1 /*
2  * empathy-call-handler.c - Source for EmpathyCallHandler
3  * Copyright (C) 2008-2009 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/util.h>
27 #include <telepathy-glib/interfaces.h>
28
29 #include <telepathy-yell/telepathy-yell.h>
30
31 #include <telepathy-farstream/telepathy-farstream.h>
32
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy/empathy-tp-contact-factory.h>
35
36 #include "empathy-call-handler.h"
37 #include "empathy-call-factory.h"
38 #include "src-marshal.h"
39
40 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
41 #include <libempathy/empathy-debug.h>
42
43 G_DEFINE_TYPE(EmpathyCallHandler, empathy_call_handler, G_TYPE_OBJECT)
44
45 /* signal enum */
46 enum {
47   CONFERENCE_ADDED,
48   CONFERENCE_REMOVED,
49   SRC_PAD_ADDED,
50   SINK_PAD_ADDED,
51   SINK_PAD_REMOVED,
52   CLOSED,
53   CANDIDATES_CHANGED,
54   STATE_CHANGED,
55   LAST_SIGNAL
56 };
57
58 static guint signals[LAST_SIGNAL] = {0};
59
60 enum {
61   PROP_CALL_CHANNEL = 1,
62   PROP_GST_BUS,
63   PROP_CONTACT,
64   PROP_MEMBERS,
65   PROP_INITIAL_AUDIO,
66   PROP_INITIAL_VIDEO,
67   PROP_SEND_AUDIO_CODEC,
68   PROP_SEND_VIDEO_CODEC,
69   PROP_RECV_AUDIO_CODECS,
70   PROP_RECV_VIDEO_CODECS,
71   PROP_AUDIO_REMOTE_CANDIDATE,
72   PROP_VIDEO_REMOTE_CANDIDATE,
73   PROP_AUDIO_LOCAL_CANDIDATE,
74   PROP_VIDEO_LOCAL_CANDIDATE,
75 };
76
77 /* private structure */
78
79 typedef struct {
80   TpyCallChannel *call;
81
82   EmpathyContact *contact;
83   /* GArray of TpContacts */
84   GArray *members;
85   TfChannel *tfchannel;
86   gboolean initial_audio;
87   gboolean initial_video;
88
89   FsCodec *send_audio_codec;
90   FsCodec *send_video_codec;
91   GList *recv_audio_codecs;
92   GList *recv_video_codecs;
93   FsCandidate *audio_remote_candidate;
94   FsCandidate *video_remote_candidate;
95   FsCandidate *audio_local_candidate;
96   FsCandidate *video_local_candidate;
97 } EmpathyCallHandlerPriv;
98
99 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
100
101 static void
102 empathy_call_handler_dispose (GObject *object)
103 {
104   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
105
106   tp_clear_object (&priv->tfchannel);
107   tp_clear_object (&priv->call);
108   tp_clear_object (&priv->contact);
109
110   tp_clear_pointer (&priv->members, g_array_unref);
111
112   G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
113 }
114
115 static void
116 empathy_call_handler_finalize (GObject *object)
117 {
118   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
119
120   fs_codec_destroy (priv->send_audio_codec);
121   fs_codec_destroy (priv->send_video_codec);
122   fs_codec_list_destroy (priv->recv_audio_codecs);
123   fs_codec_list_destroy (priv->recv_video_codecs);
124   fs_candidate_destroy (priv->audio_remote_candidate);
125   fs_candidate_destroy (priv->video_remote_candidate);
126   fs_candidate_destroy (priv->audio_local_candidate);
127   fs_candidate_destroy (priv->video_local_candidate);
128
129   G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize (object);
130 }
131
132 static void
133 empathy_call_handler_init (EmpathyCallHandler *obj)
134 {
135   EmpathyCallHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
136     EMPATHY_TYPE_CALL_HANDLER, EmpathyCallHandlerPriv);
137
138   obj->priv = priv;
139 }
140
141 static void
142 on_get_contacts_cb (TpConnection *connection,
143     guint n_contacts,
144     EmpathyContact * const * contacts,
145     guint n_failed,
146     const TpHandle *failed,
147     const GError *error,
148     gpointer user_data,
149     GObject *weak_object)
150 {
151   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (weak_object);
152   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
153   guint i;
154
155   if (n_failed > 0)
156     g_warning ("Failed to get %d EmpathyContacts: %s",
157         n_failed, error->message);
158
159   priv->members = g_array_sized_new (FALSE, TRUE,
160       sizeof (EmpathyContact *), n_contacts);
161
162   for (i = 0; i < n_contacts; i++)
163     g_object_ref (contacts[i]);
164
165   g_array_append_vals (priv->members, contacts, n_contacts);
166
167   g_object_notify (G_OBJECT (self), "members");
168 }
169
170 static void
171 on_call_state_changed_cb (TpyCallChannel *call,
172   TpyCallState state,
173   TpyCallFlags flags,
174    const GValueArray *call_state_reason,
175   GHashTable *call_state_details,
176   EmpathyCallHandler *handler)
177 {
178   if (state == TPY_CALL_STATE_ENDED)
179     tp_channel_close_async (TP_CHANNEL (call), NULL, NULL);
180
181   g_signal_emit (handler, signals[STATE_CHANGED], 0, state);
182 }
183
184 static void
185 on_members_changed_cb (TpyCallChannel *call,
186     GHashTable *members,
187     EmpathyCallHandler *self)
188 {
189   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
190   GHashTableIter iter;
191   gpointer key, value;
192   TpHandle *handles;
193   guint n_handles;
194   guint i = 0;
195
196   if (members == NULL)
197     return;
198
199   n_handles = g_hash_table_size (members);
200   if (n_handles == 0)
201     return;
202
203   handles = g_new0 (TpHandle, n_handles);
204
205   g_hash_table_iter_init (&iter, members);
206   while (g_hash_table_iter_next (&iter, &key, &value))
207     handles[i++] = GPOINTER_TO_UINT (key);
208
209   empathy_tp_contact_factory_get_from_handles (
210       tp_channel_borrow_connection (TP_CHANNEL (priv->call)),
211       n_handles, handles,
212       on_get_contacts_cb,
213       NULL, NULL, G_OBJECT (self));
214
215   g_free (handles);
216 }
217
218 static void
219 empathy_call_handler_constructed (GObject *object)
220 {
221   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
222   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
223 //  GHashTable *members;
224
225   g_signal_connect (priv->call, "members-changed",
226       G_CALLBACK (on_members_changed_cb), object);
227
228 /* FIXME
229   g_object_get (priv->call, "members", &members, NULL);
230
231   if (members)
232     on_members_changed_cb (priv->call, members, self);
233 */
234 }
235
236 static void
237 empathy_call_handler_set_property (GObject *object,
238   guint property_id, const GValue *value, GParamSpec *pspec)
239 {
240   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
241
242   switch (property_id)
243     {
244       case PROP_CONTACT:
245         priv->contact = g_value_dup_object (value);
246         break;
247       case PROP_MEMBERS:
248         priv->members = g_value_get_boxed (value);
249         break;
250       case PROP_CALL_CHANNEL:
251         g_return_if_fail (priv->call == NULL);
252
253         priv->call = g_value_dup_object (value);
254
255         tp_g_signal_connect_object (priv->call, "state-changed",
256           G_CALLBACK (on_call_state_changed_cb), object, 0);
257         break;
258       case PROP_INITIAL_AUDIO:
259         priv->initial_audio = g_value_get_boolean (value);
260         break;
261       case PROP_INITIAL_VIDEO:
262         priv->initial_video = g_value_get_boolean (value);
263         break;
264       default:
265         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
266     }
267 }
268
269 static void
270 empathy_call_handler_get_property (GObject *object,
271   guint property_id, GValue *value, GParamSpec *pspec)
272 {
273   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
274
275   switch (property_id)
276     {
277       case PROP_CONTACT:
278         g_value_set_object (value, priv->contact);
279         break;
280       case PROP_MEMBERS:
281         g_value_set_boxed (value, priv->members);
282         break;
283       case PROP_CALL_CHANNEL:
284         g_value_set_object (value, priv->call);
285         break;
286       case PROP_INITIAL_AUDIO:
287         g_value_set_boolean (value, priv->initial_audio);
288         break;
289       case PROP_INITIAL_VIDEO:
290         g_value_set_boolean (value, priv->initial_video);
291         break;
292       case PROP_SEND_AUDIO_CODEC:
293         g_value_set_boxed (value, priv->send_audio_codec);
294         break;
295       case PROP_SEND_VIDEO_CODEC:
296         g_value_set_boxed (value, priv->send_video_codec);
297         break;
298       case PROP_RECV_AUDIO_CODECS:
299         g_value_set_boxed (value, priv->recv_audio_codecs);
300         break;
301       case PROP_RECV_VIDEO_CODECS:
302         g_value_set_boxed (value, priv->recv_video_codecs);
303         break;
304       case PROP_AUDIO_REMOTE_CANDIDATE:
305         g_value_set_boxed (value, priv->audio_remote_candidate);
306         break;
307       case PROP_VIDEO_REMOTE_CANDIDATE:
308         g_value_set_boxed (value, priv->video_remote_candidate);
309         break;
310       case PROP_AUDIO_LOCAL_CANDIDATE:
311         g_value_set_boxed (value, priv->audio_local_candidate);
312         break;
313       case PROP_VIDEO_LOCAL_CANDIDATE:
314         g_value_set_boxed (value, priv->video_local_candidate);
315         break;
316       default:
317         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
318     }
319 }
320
321
322 static void
323 empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
324 {
325   GObjectClass *object_class = G_OBJECT_CLASS (klass);
326   GParamSpec *param_spec;
327
328   g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
329
330   object_class->constructed = empathy_call_handler_constructed;
331   object_class->set_property = empathy_call_handler_set_property;
332   object_class->get_property = empathy_call_handler_get_property;
333   object_class->dispose = empathy_call_handler_dispose;
334   object_class->finalize = empathy_call_handler_finalize;
335
336   param_spec = g_param_spec_object ("target-contact",
337     "TargetContact", "The contact",
338     EMPATHY_TYPE_CONTACT,
339     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
340   g_object_class_install_property (object_class, PROP_CONTACT, param_spec);
341
342   param_spec = g_param_spec_boxed ("members",
343     "call members", "The call participants",
344     G_TYPE_ARRAY,
345     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
346   g_object_class_install_property (object_class, PROP_MEMBERS, param_spec);
347
348   param_spec = g_param_spec_object ("call-channel",
349     "call channel", "The call channel",
350     TPY_TYPE_CALL_CHANNEL,
351     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
352   g_object_class_install_property (object_class, PROP_CALL_CHANNEL, param_spec);
353
354   param_spec = g_param_spec_boolean ("initial-audio",
355     "initial-audio", "Whether the call should start with audio",
356     TRUE,
357     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
358   g_object_class_install_property (object_class, PROP_INITIAL_AUDIO,
359       param_spec);
360
361   param_spec = g_param_spec_boolean ("initial-video",
362     "initial-video", "Whether the call should start with video",
363     FALSE,
364     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
365   g_object_class_install_property (object_class, PROP_INITIAL_VIDEO,
366     param_spec);
367
368   param_spec = g_param_spec_boxed ("send-audio-codec",
369     "send audio codec", "Codec used to encode the outgoing video stream",
370     FS_TYPE_CODEC,
371     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
372   g_object_class_install_property (object_class, PROP_SEND_AUDIO_CODEC,
373     param_spec);
374
375   param_spec = g_param_spec_boxed ("send-video-codec",
376     "send video codec", "Codec used to encode the outgoing video stream",
377     FS_TYPE_CODEC,
378     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
379   g_object_class_install_property (object_class, PROP_SEND_VIDEO_CODEC,
380     param_spec);
381
382   param_spec = g_param_spec_boxed ("recv-audio-codecs",
383     "recvs audio codec", "Codecs used to decode the incoming audio stream",
384     FS_TYPE_CODEC_LIST,
385     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
386   g_object_class_install_property (object_class, PROP_RECV_AUDIO_CODECS,
387     param_spec);
388
389   param_spec = g_param_spec_boxed ("recv-video-codecs",
390     "recvs video codec", "Codecs used to decode the incoming video stream",
391     FS_TYPE_CODEC_LIST,
392     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
393   g_object_class_install_property (object_class, PROP_RECV_VIDEO_CODECS,
394     param_spec);
395
396   param_spec = g_param_spec_boxed ("audio-remote-candidate",
397     "audio remote candidate",
398     "Remote candidate used for the audio stream",
399     FS_TYPE_CANDIDATE,
400     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
401   g_object_class_install_property (object_class,
402       PROP_AUDIO_REMOTE_CANDIDATE, param_spec);
403
404   param_spec = g_param_spec_boxed ("video-remote-candidate",
405     "video remote candidate",
406     "Remote candidate used for the video stream",
407     FS_TYPE_CANDIDATE,
408     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
409   g_object_class_install_property (object_class,
410       PROP_VIDEO_REMOTE_CANDIDATE, param_spec);
411
412   param_spec = g_param_spec_boxed ("audio-local-candidate",
413     "audio local candidate",
414     "Local candidate used for the audio stream",
415     FS_TYPE_CANDIDATE,
416     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
417   g_object_class_install_property (object_class,
418       PROP_AUDIO_REMOTE_CANDIDATE, param_spec);
419
420   param_spec = g_param_spec_boxed ("video-local-candidate",
421     "video local candidate",
422     "Local candidate used for the video stream",
423     FS_TYPE_CANDIDATE,
424     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
425   g_object_class_install_property (object_class,
426       PROP_VIDEO_REMOTE_CANDIDATE, param_spec);
427
428   signals[CONFERENCE_ADDED] =
429     g_signal_new ("conference-added", G_TYPE_FROM_CLASS (klass),
430       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
431       g_cclosure_marshal_VOID__OBJECT,
432       G_TYPE_NONE,
433       1, FS_TYPE_CONFERENCE);
434
435   signals[CONFERENCE_REMOVED] =
436     g_signal_new ("conference-removed", G_TYPE_FROM_CLASS (klass),
437       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
438       g_cclosure_marshal_VOID__OBJECT,
439       G_TYPE_NONE,
440       1, FS_TYPE_CONFERENCE);
441
442   signals[SRC_PAD_ADDED] =
443     g_signal_new ("src-pad-added", G_TYPE_FROM_CLASS (klass),
444       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
445       _src_marshal_BOOLEAN__OBJECT_UINT,
446       G_TYPE_BOOLEAN,
447       2, GST_TYPE_PAD, G_TYPE_UINT);
448
449   signals[SINK_PAD_ADDED] =
450     g_signal_new ("sink-pad-added", G_TYPE_FROM_CLASS (klass),
451       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
452       _src_marshal_BOOLEAN__OBJECT_UINT,
453       G_TYPE_BOOLEAN,
454       2, GST_TYPE_PAD, G_TYPE_UINT);
455
456   signals[SINK_PAD_REMOVED] =
457     g_signal_new ("sink-pad-removed", G_TYPE_FROM_CLASS (klass),
458       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
459       _src_marshal_BOOLEAN__OBJECT_UINT,
460       G_TYPE_BOOLEAN,
461       2, GST_TYPE_PAD, G_TYPE_UINT);
462
463   signals[CLOSED] =
464     g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
465       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
466       g_cclosure_marshal_VOID__VOID,
467       G_TYPE_NONE,
468       0);
469
470   signals[CANDIDATES_CHANGED] =
471     g_signal_new ("candidates-changed", G_TYPE_FROM_CLASS (klass),
472       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
473       g_cclosure_marshal_VOID__UINT,
474       G_TYPE_NONE, 1, G_TYPE_UINT);
475
476   signals[STATE_CHANGED] =
477     g_signal_new ("state-changed", G_TYPE_FROM_CLASS (klass),
478       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
479       g_cclosure_marshal_VOID__UINT,
480       G_TYPE_NONE, 1, G_TYPE_UINT);
481 }
482
483 EmpathyCallHandler *
484 empathy_call_handler_new_for_channel (TpyCallChannel *call,
485   EmpathyContact *contact)
486 {
487   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
488     "call-channel", call,
489     "initial-video", tpy_call_channel_has_initial_video (call),
490     "target-contact", contact,
491     NULL));
492 }
493
494 static void
495 update_sending_codec (EmpathyCallHandler *self,
496     FsCodec *codec,
497     FsSession *session)
498 {
499   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
500   FsMediaType type;
501
502   if (codec == NULL || session == NULL)
503     return;
504
505   g_object_get (session, "media-type", &type, NULL);
506
507   if (type == FS_MEDIA_TYPE_AUDIO)
508     {
509       priv->send_audio_codec = fs_codec_copy (codec);
510       g_object_notify (G_OBJECT (self), "send-audio-codec");
511     }
512   else if (type == FS_MEDIA_TYPE_VIDEO)
513     {
514       priv->send_video_codec = fs_codec_copy (codec);
515       g_object_notify (G_OBJECT (self), "send-video-codec");
516     }
517 }
518
519 static void
520 update_receiving_codec (EmpathyCallHandler *self,
521     GList *codecs,
522     FsStream *stream)
523 {
524   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
525   FsSession *session;
526   FsMediaType type;
527
528   if (codecs == NULL || stream == NULL)
529     return;
530
531   g_object_get (stream, "session", &session, NULL);
532   if (session == NULL)
533     return;
534
535   g_object_get (session, "media-type", &type, NULL);
536
537   if (type == FS_MEDIA_TYPE_AUDIO)
538     {
539       priv->recv_audio_codecs = fs_codec_list_copy (codecs);
540       g_object_notify (G_OBJECT (self), "recv-audio-codecs");
541     }
542   else if (type == FS_MEDIA_TYPE_VIDEO)
543     {
544       priv->recv_video_codecs = fs_codec_list_copy (codecs);
545       g_object_notify (G_OBJECT (self), "recv-video-codecs");
546     }
547
548   g_object_unref (session);
549 }
550
551 static void
552 update_candidates (EmpathyCallHandler *self,
553     FsCandidate *remote_candidate,
554     FsCandidate *local_candidate,
555     FsStream *stream)
556 {
557   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
558   FsSession *session;
559   FsMediaType type;
560
561   if (stream == NULL)
562     return;
563
564   g_object_get (stream, "session", &session, NULL);
565   if (session == NULL)
566     return;
567
568   g_object_get (session, "media-type", &type, NULL);
569
570   if (type == FS_MEDIA_TYPE_AUDIO)
571     {
572       if (remote_candidate != NULL)
573         {
574           fs_candidate_destroy (priv->audio_remote_candidate);
575           priv->audio_remote_candidate = fs_candidate_copy (remote_candidate);
576           g_object_notify (G_OBJECT (self), "audio-remote-candidate");
577         }
578
579       if (local_candidate != NULL)
580         {
581           fs_candidate_destroy (priv->audio_local_candidate);
582           priv->audio_local_candidate = fs_candidate_copy (local_candidate);
583           g_object_notify (G_OBJECT (self), "audio-local-candidate");
584         }
585
586       g_signal_emit (G_OBJECT (self), signals[CANDIDATES_CHANGED], 0,
587           FS_MEDIA_TYPE_AUDIO);
588     }
589   else if (type == FS_MEDIA_TYPE_VIDEO)
590     {
591       if (remote_candidate != NULL)
592         {
593           fs_candidate_destroy (priv->video_remote_candidate);
594           priv->video_remote_candidate = fs_candidate_copy (remote_candidate);
595           g_object_notify (G_OBJECT (self), "video-remote-candidate");
596         }
597
598       if (local_candidate != NULL)
599         {
600           fs_candidate_destroy (priv->video_local_candidate);
601           priv->video_local_candidate = fs_candidate_copy (local_candidate);
602           g_object_notify (G_OBJECT (self), "video-local-candidate");
603         }
604
605       g_signal_emit (G_OBJECT (self), signals[CANDIDATES_CHANGED], 0,
606           FS_MEDIA_TYPE_VIDEO);
607     }
608
609   g_object_unref (session);
610 }
611
612 void
613 empathy_call_handler_bus_message (EmpathyCallHandler *handler,
614   GstBus *bus, GstMessage *message)
615 {
616   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
617   const GstStructure *s = gst_message_get_structure (message);
618
619   if (priv->tfchannel == NULL)
620     return;
621
622   if (s != NULL &&
623       gst_structure_has_name (s, "farsight-send-codec-changed"))
624     {
625       const GValue *val;
626       FsCodec *codec;
627       FsSession *session;
628
629       g_print ("empathy_call_handler_bus_message: farsight-send-codec-changed\n");
630
631       val = gst_structure_get_value (s, "codec");
632       codec = g_value_get_boxed (val);
633
634       val = gst_structure_get_value (s, "session");
635       session = g_value_get_object (val);
636
637       update_sending_codec (handler, codec, session);
638     }
639   else if (s != NULL &&
640       gst_structure_has_name (s, "farsight-recv-codecs-changed"))
641     {
642       const GValue *val;
643       GList *codecs;
644       FsStream *stream;
645
646       g_print ("empathy_call_handler_bus_message: farsight-recv-codecs-changed\n");
647
648       val = gst_structure_get_value (s, "codecs");
649       codecs = g_value_get_boxed (val);
650
651       val = gst_structure_get_value (s, "stream");
652       stream = g_value_get_object (val);
653
654       update_receiving_codec (handler, codecs, stream);
655     }
656   else if (s != NULL &&
657       gst_structure_has_name (s, "farsight-new-active-candidate-pair"))
658     {
659       const GValue *val;
660       FsCandidate *remote_candidate, *local_candidate;
661       FsStream *stream;
662
663       g_print ("empathy_call_handler_bus_message: farsight-new-active-candidate-pair\n");
664
665       val = gst_structure_get_value (s, "remote-candidate");
666       remote_candidate = g_value_get_boxed (val);
667
668       val = gst_structure_get_value (s, "local-candidate");
669       local_candidate = g_value_get_boxed (val);
670
671       val = gst_structure_get_value (s, "stream");
672       stream = g_value_get_object (val);
673
674       update_candidates (handler, remote_candidate, local_candidate, stream);
675     }
676
677   tf_channel_bus_message (priv->tfchannel, message);
678 }
679
680 static void
681 on_tf_channel_conference_added_cb (TfChannel *tfchannel,
682   GstElement *conference,
683   EmpathyCallHandler *self)
684 {
685   g_signal_emit (G_OBJECT (self), signals[CONFERENCE_ADDED], 0,
686     conference);
687 }
688
689 static void
690 on_tf_channel_conference_removed_cb (TfChannel *tfchannel,
691   FsConference *conference,
692   EmpathyCallHandler *self)
693 {
694   g_signal_emit (G_OBJECT (self), signals[CONFERENCE_REMOVED], 0,
695     GST_ELEMENT (conference));
696 }
697
698 static gboolean
699 src_pad_added_error_idle (gpointer data)
700 {
701   TfContent *content = data;
702
703   tf_content_error (content, 0 /* FIXME */,
704       "Could not link sink", NULL);
705   g_object_unref (content);
706
707   return FALSE;
708 }
709
710 static void
711 on_tf_content_src_pad_added_cb (TfContent *content,
712   guint handle,
713   FsStream *stream,
714   GstPad *pad,
715   FsCodec *codec,
716   EmpathyCallHandler *handler)
717 {
718   guint media_type;
719   gboolean retval;
720
721   g_object_get (content, "media-type", &media_type, NULL);
722
723   g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
724       pad, media_type, &retval);
725
726   if (!retval)
727     g_idle_add (src_pad_added_error_idle, g_object_ref (content));
728 }
729
730 static void
731 on_tf_channel_content_added_cb (TfChannel *tfchannel,
732   TfContent *content,
733   EmpathyCallHandler *handler)
734 {
735   FsMediaType mtype;
736   GstPad *spad;
737   FsSession *session;
738 //  FsStream *fs_stream;
739   FsCodec *codec;
740 //  GList *codecs;
741   gboolean retval;
742
743   g_signal_connect (content, "src-pad-added",
744       G_CALLBACK (on_tf_content_src_pad_added_cb), handler);
745 #if 0
746   g_signal_connect (content, "start-sending",
747       G_CALLBACK (on_tf_content_start_sending_cb), handler);
748   g_signal_connect (content, "stop-sending",
749       G_CALLBACK (on_tf_content_stop_sending_cb), handler);
750 #endif
751
752   g_object_get (content, "media-type", &mtype,
753     "sink-pad", &spad, NULL);
754
755   g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
756       spad, mtype, &retval);
757
758  if (!retval)
759       tf_content_error (content, 0 /* FIXME */,
760           "Could not link source", NULL);
761
762  /* Get sending codec */
763  g_object_get (content, "fs-session", &session, NULL);
764  g_object_get (session, "current-send-codec", &codec, NULL);
765
766  update_sending_codec (handler, codec, session);
767
768  tp_clear_object (&session);
769  tp_clear_object (&codec);
770
771  /* Get receiving codec */
772 /* FIXME
773  g_object_get (content, "fs-stream", &fs_stream, NULL);
774  g_object_get (fs_stream, "current-recv-codecs", &codecs, NULL);
775
776  update_receiving_codec (handler, codecs, fs_stream);
777
778  fs_codec_list_destroy (codecs);
779  tp_clear_object (&fs_stream);
780 */
781
782  gst_object_unref (spad);
783 }
784
785 static void
786 on_tf_channel_content_removed_cb (TfChannel *tfchannel,
787   TfContent *content,
788   EmpathyCallHandler *handler)
789 {
790   FsMediaType mtype;
791   GstPad *spad;
792   gboolean retval;
793
794   g_print ("removing content\n");
795
796   g_object_get (content, "media-type", &mtype,
797     "sink-pad", &spad, NULL);
798
799   g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_REMOVED], 0,
800       spad, mtype, &retval);
801
802   if (!retval)
803     {
804       g_warning ("Could not remove content!");
805
806       tf_content_error (content, 0 /* FIXME */,
807           "Could not link source", NULL);
808     }
809 }
810
811 static void
812 on_tf_channel_closed_cb (TfChannel *tfchannel,
813     EmpathyCallHandler *handler)
814 {
815   g_signal_emit (G_OBJECT (handler), signals[CLOSED], 0);
816 }
817
818 static void
819 on_tf_channel_ready (GObject *source,
820     GAsyncResult *result,
821     gpointer user_data)
822 {
823   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
824   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
825   GError *error = NULL;
826
827   priv->tfchannel = TF_CHANNEL (g_async_initable_new_finish (
828       G_ASYNC_INITABLE (source), result, NULL));
829
830   if (priv->tfchannel == NULL)
831     {
832       g_warning ("Failed to create Farstream channel: %s", error->message);
833       g_error_free (error);
834       return;
835     }
836
837   /* Set up the telepathy farstream channel */
838   g_signal_connect (priv->tfchannel, "closed",
839       G_CALLBACK (on_tf_channel_closed_cb), self);
840   g_signal_connect (priv->tfchannel, "fs-conference-added",
841       G_CALLBACK (on_tf_channel_conference_added_cb), self);
842   g_signal_connect (priv->tfchannel, "fs-conference-removed",
843       G_CALLBACK (on_tf_channel_conference_removed_cb), self);
844   g_signal_connect (priv->tfchannel, "content-added",
845       G_CALLBACK (on_tf_channel_content_added_cb), self);
846   g_signal_connect (priv->tfchannel, "content-removed",
847       G_CALLBACK (on_tf_channel_content_removed_cb), self);
848 }
849
850 static void
851 empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
852 {
853   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
854
855   tf_channel_new_async (TP_CHANNEL (priv->call),
856       on_tf_channel_ready, self);
857 }
858
859 #if 0
860 static void
861 empathy_call_handler_request_cb (GObject *source,
862     GAsyncResult *result,
863     gpointer user_data)
864 {
865   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
866   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
867   TpChannel *channel;
868   GError *error = NULL;
869   TpAccountChannelRequest *req = TP_ACCOUNT_CHANNEL_REQUEST (source);
870
871   channel = tp_account_channel_request_create_and_handle_channel_finish (req,
872       result, NULL, &error);
873   if (channel == NULL)
874     {
875       DEBUG ("Failed to create the channel: %s", error->message);
876       g_error_free (error);
877       return;
878     }
879
880   if (!TPY_IS_CALL_CHANNEL (channel))
881     {
882       DEBUG ("The channel is not a Call channel!");
883       return;
884     }
885
886   priv->call = TPY_CALL_CHANNEL (channel);
887
888   g_object_notify (G_OBJECT (self), "call-channel");
889
890   empathy_call_handler_start_tpfs (self);
891 }
892 #endif
893
894 static void
895 on_call_accepted_cb (GObject *source_object,
896     GAsyncResult *res,
897     gpointer user_data)
898 {
899   TpyCallChannel *call = TPY_CALL_CHANNEL (source_object);
900   GError *error = NULL;
901
902   if (!tpy_call_channel_accept_finish (call, res, &error))
903     {
904       g_warning ("could not accept Call: %s", error->message);
905       g_error_free (error);
906     }
907 }
908
909 void
910 empathy_call_handler_start_call (EmpathyCallHandler *handler,
911     gint64 timestamp)
912 {
913   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
914 /*TpAccountChannelRequest *req;
915   TpAccount *account;
916   GHashTable *request;*/
917
918   if (priv->call != NULL)
919     {
920       empathy_call_handler_start_tpfs (handler);
921       tpy_call_channel_accept_async (priv->call, on_call_accepted_cb, NULL);
922       return;
923     }
924   else
925     {
926       g_warning ("No Call channel!");
927     }
928
929 #if 0
930   /* No TpyCallChannel (we are redialing). Request a new call channel */
931   g_assert (priv->contact != NULL);
932
933   account = empathy_contact_get_account (priv->contact);
934   request = empathy_call_create_call_request (priv->contact,
935       priv->initial_audio, priv->initial_video);
936
937   req = tp_account_channel_request_new (account, request, timestamp);
938
939   tp_account_channel_request_create_and_handle_channel_async (req, NULL,
940       empathy_call_handler_request_cb, handler);
941
942   g_object_unref (req);
943   g_hash_table_unref (request);
944 #endif
945 }
946
947 /**
948  * empathy_call_handler_stop_call:
949  * @handler: an #EmpathyCallHandler
950  *
951  * Closes the #EmpathyCallHandler's call and frees its resources.
952  */
953 void
954 empathy_call_handler_stop_call (EmpathyCallHandler *handler)
955 {
956   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
957
958   if (priv->call != NULL)
959     {
960       tpy_call_channel_hangup_async (priv->call,
961           TPY_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
962           "", "", NULL, NULL);
963       tp_channel_close_async (TP_CHANNEL (priv->call),
964         NULL, NULL);
965       tp_clear_object (&priv->call);
966       tp_clear_object (&priv->tfchannel);
967     }
968 }
969
970 /**
971  * empathy_call_handler_has_initial_video:
972  * @handler: an #EmpathyCallHandler
973  *
974  * Return %TRUE if the call managed by this #EmpathyCallHandler was
975  * created with video enabled
976  *
977  * Return value: %TRUE if the call was created as a video conversation.
978  */
979 gboolean
980 empathy_call_handler_has_initial_video (EmpathyCallHandler *handler)
981 {
982   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
983
984   return priv->initial_video;
985 }
986
987 FsCodec *
988 empathy_call_handler_get_send_audio_codec (EmpathyCallHandler *self)
989 {
990   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
991
992   return priv->send_audio_codec;
993 }
994
995 FsCodec *
996 empathy_call_handler_get_send_video_codec (EmpathyCallHandler *self)
997 {
998   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
999
1000   return priv->send_video_codec;
1001 }
1002
1003 GList *
1004 empathy_call_handler_get_recv_audio_codecs (EmpathyCallHandler *self)
1005 {
1006   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
1007
1008   return priv->recv_audio_codecs;
1009 }
1010
1011 GList *
1012 empathy_call_handler_get_recv_video_codecs (EmpathyCallHandler *self)
1013 {
1014   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
1015
1016   return priv->recv_video_codecs;
1017 }
1018
1019 FsCandidate *
1020 empathy_call_handler_get_audio_remote_candidate (
1021     EmpathyCallHandler *self)
1022 {
1023   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
1024
1025   return priv->audio_remote_candidate;
1026 }
1027
1028 FsCandidate *
1029 empathy_call_handler_get_audio_local_candidate (
1030     EmpathyCallHandler *self)
1031 {
1032   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
1033
1034   return priv->audio_local_candidate;
1035 }
1036
1037 FsCandidate *
1038 empathy_call_handler_get_video_remote_candidate (
1039     EmpathyCallHandler *self)
1040 {
1041   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
1042
1043   return priv->video_remote_candidate;
1044 }
1045
1046 FsCandidate *
1047 empathy_call_handler_get_video_local_candidate (
1048     EmpathyCallHandler *self)
1049 {
1050   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
1051
1052   return priv->video_local_candidate;
1053 }