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