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