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