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