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