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