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