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