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