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