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