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