]> git.0d.be Git - empathy.git/blob - src/empathy-call-handler.c
Temporarily disable some code
[empathy.git] / src / empathy-call-handler.c
1 /*
2  * empathy-call-handler.c - Source for EmpathyCallHandler
3  * Copyright (C) 2008-2009 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <telepathy-glib/account-channel-request.h>
26 #include <telepathy-glib/util.h>
27 #include <telepathy-glib/interfaces.h>
28
29 #include <telepathy-yell/telepathy-yell.h>
30
31 #include <telepathy-farstream/telepathy-farstream.h>
32
33 #include <libempathy/empathy-utils.h>
34 #include <libempathy/empathy-tp-contact-factory.h>
35
36 #include "empathy-call-handler.h"
37 #include "empathy-call-factory.h"
38 #include "src-marshal.h"
39
40 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
41 #include <libempathy/empathy-debug.h>
42
43 G_DEFINE_TYPE(EmpathyCallHandler, empathy_call_handler, G_TYPE_OBJECT)
44
45 /* signal enum */
46 enum {
47   CONFERENCE_ADDED,
48   CONFERENCE_REMOVED,
49   SRC_PAD_ADDED,
50   SINK_PAD_ADDED,
51   SINK_PAD_REMOVED,
52   CLOSED,
53   CANDIDATES_CHANGED,
54   LAST_SIGNAL
55 };
56
57 static guint signals[LAST_SIGNAL] = {0};
58
59 enum {
60   PROP_CALL_CHANNEL = 1,
61   PROP_GST_BUS,
62   PROP_MEMBERS,
63   PROP_INITIAL_AUDIO,
64   PROP_INITIAL_VIDEO,
65   PROP_SEND_AUDIO_CODEC,
66   PROP_SEND_VIDEO_CODEC,
67   PROP_RECV_AUDIO_CODECS,
68   PROP_RECV_VIDEO_CODECS,
69   PROP_AUDIO_REMOTE_CANDIDATE,
70   PROP_VIDEO_REMOTE_CANDIDATE,
71   PROP_AUDIO_LOCAL_CANDIDATE,
72   PROP_VIDEO_LOCAL_CANDIDATE,
73 };
74
75 /* private structure */
76
77 typedef struct {
78   TpyCallChannel *call;
79
80   /* GArray of TpContacts */
81   GArray *members;
82   TfChannel *tfchannel;
83   gboolean initial_audio;
84   gboolean initial_video;
85
86   FsCodec *send_audio_codec;
87   FsCodec *send_video_codec;
88   GList *recv_audio_codecs;
89   GList *recv_video_codecs;
90   FsCandidate *audio_remote_candidate;
91   FsCandidate *video_remote_candidate;
92   FsCandidate *audio_local_candidate;
93   FsCandidate *video_local_candidate;
94 } EmpathyCallHandlerPriv;
95
96 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallHandler)
97
98 static void
99 empathy_call_handler_dispose (GObject *object)
100 {
101   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
102
103   tp_clear_object (&priv->tfchannel);
104   tp_clear_object (&priv->call);
105
106   tp_clear_pointer (&priv->members, g_array_unref);
107
108   G_OBJECT_CLASS (empathy_call_handler_parent_class)->dispose (object);
109 }
110
111 static void
112 empathy_call_handler_finalize (GObject *object)
113 {
114   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
115
116   fs_codec_destroy (priv->send_audio_codec);
117   fs_codec_destroy (priv->send_video_codec);
118   fs_codec_list_destroy (priv->recv_audio_codecs);
119   fs_codec_list_destroy (priv->recv_video_codecs);
120   fs_candidate_destroy (priv->audio_remote_candidate);
121   fs_candidate_destroy (priv->video_remote_candidate);
122   fs_candidate_destroy (priv->audio_local_candidate);
123   fs_candidate_destroy (priv->video_local_candidate);
124
125   G_OBJECT_CLASS (empathy_call_handler_parent_class)->finalize (object);
126 }
127
128 static void
129 empathy_call_handler_init (EmpathyCallHandler *obj)
130 {
131   EmpathyCallHandlerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
132     EMPATHY_TYPE_CALL_HANDLER, EmpathyCallHandlerPriv);
133
134   obj->priv = priv;
135 }
136
137 static void
138 on_get_contacts_cb (TpConnection *connection,
139     guint n_contacts,
140     EmpathyContact * const * contacts,
141     guint n_failed,
142     const TpHandle *failed,
143     const GError *error,
144     gpointer user_data,
145     GObject *weak_object)
146 {
147   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (weak_object);
148   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
149   guint i;
150
151   g_print ("\nGot %d EmpathyContacts\n\n", n_contacts);
152
153   if (n_failed > 0)
154     g_warning ("Failed to get %d EmpathyContacts: %s",
155         n_failed, error->message);
156
157   priv->members = g_array_sized_new (FALSE, TRUE,
158       sizeof (EmpathyContact *), n_contacts);
159
160   for (i = 0; i < n_contacts; i++)
161     g_object_ref (contacts[i]);
162
163   g_array_append_vals (priv->members, contacts, n_contacts);
164
165   g_object_notify (G_OBJECT (self), "members");
166 }
167
168 static void
169 on_members_changed_cb (TpyCallChannel *call,
170     GHashTable *members,
171     EmpathyCallHandler *self)
172 {
173   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
174   GHashTableIter iter;
175   gpointer key, value;
176   TpHandle *handles;
177   guint n_handles;
178   guint i = 0;
179
180   g_print ("\non_members_changed!\n\n");
181
182   if (members == NULL)
183     {
184       g_print ("\nmembers is null!\n\n");
185       return;
186     }
187
188   n_handles = g_hash_table_size (members);
189   g_print ("\nn_handles: %d\n\n", n_handles);
190   if (n_handles == 0)
191     return;
192
193   handles = g_new0 (TpHandle, n_handles);
194
195   g_hash_table_iter_init (&iter, members);
196   while (g_hash_table_iter_next (&iter, &key, &value))
197     handles[i++] = GPOINTER_TO_UINT (key);
198
199   empathy_tp_contact_factory_get_from_handles (
200       tp_channel_borrow_connection (TP_CHANNEL (priv->call)),
201       n_handles, handles,
202       on_get_contacts_cb,
203       NULL, NULL, G_OBJECT (self));
204
205   g_free (handles);
206 }
207
208 static void
209 empathy_call_handler_constructed (GObject *object)
210 {
211   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (object);
212   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
213 //  GHashTable *members;
214
215   g_signal_connect (priv->call, "members-changed",
216       G_CALLBACK (on_members_changed_cb), object);
217
218 /* FIXME
219   g_object_get (priv->call, "members", &members, NULL);
220
221   if (members)
222     on_members_changed_cb (priv->call, members, self);
223 */
224 }
225
226 static void
227 empathy_call_handler_set_property (GObject *object,
228   guint property_id, const GValue *value, GParamSpec *pspec)
229 {
230   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
231
232   switch (property_id)
233     {
234       case PROP_MEMBERS:
235         priv->members = g_value_get_boxed (value);
236         break;
237       case PROP_CALL_CHANNEL:
238         priv->call = g_value_dup_object (value);
239         break;
240       case PROP_INITIAL_AUDIO:
241         priv->initial_audio = g_value_get_boolean (value);
242         break;
243       case PROP_INITIAL_VIDEO:
244         priv->initial_video = g_value_get_boolean (value);
245         break;
246       default:
247         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
248     }
249 }
250
251 static void
252 empathy_call_handler_get_property (GObject *object,
253   guint property_id, GValue *value, GParamSpec *pspec)
254 {
255   EmpathyCallHandlerPriv *priv = GET_PRIV (object);
256
257   switch (property_id)
258     {
259       case PROP_MEMBERS:
260         g_value_set_boxed (value, priv->members);
261         break;
262       case PROP_CALL_CHANNEL:
263         g_value_set_object (value, priv->call);
264         break;
265       case PROP_INITIAL_AUDIO:
266         g_value_set_boolean (value, priv->initial_audio);
267         break;
268       case PROP_INITIAL_VIDEO:
269         g_value_set_boolean (value, priv->initial_video);
270         break;
271       case PROP_SEND_AUDIO_CODEC:
272         g_value_set_boxed (value, priv->send_audio_codec);
273         break;
274       case PROP_SEND_VIDEO_CODEC:
275         g_value_set_boxed (value, priv->send_video_codec);
276         break;
277       case PROP_RECV_AUDIO_CODECS:
278         g_value_set_boxed (value, priv->recv_audio_codecs);
279         break;
280       case PROP_RECV_VIDEO_CODECS:
281         g_value_set_boxed (value, priv->recv_video_codecs);
282         break;
283       case PROP_AUDIO_REMOTE_CANDIDATE:
284         g_value_set_boxed (value, priv->audio_remote_candidate);
285         break;
286       case PROP_VIDEO_REMOTE_CANDIDATE:
287         g_value_set_boxed (value, priv->video_remote_candidate);
288         break;
289       case PROP_AUDIO_LOCAL_CANDIDATE:
290         g_value_set_boxed (value, priv->audio_local_candidate);
291         break;
292       case PROP_VIDEO_LOCAL_CANDIDATE:
293         g_value_set_boxed (value, priv->video_local_candidate);
294         break;
295       default:
296         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
297     }
298 }
299
300
301 static void
302 empathy_call_handler_class_init (EmpathyCallHandlerClass *klass)
303 {
304   GObjectClass *object_class = G_OBJECT_CLASS (klass);
305   GParamSpec *param_spec;
306
307   g_type_class_add_private (klass, sizeof (EmpathyCallHandlerPriv));
308
309   object_class->constructed = empathy_call_handler_constructed;
310   object_class->set_property = empathy_call_handler_set_property;
311   object_class->get_property = empathy_call_handler_get_property;
312   object_class->dispose = empathy_call_handler_dispose;
313   object_class->finalize = empathy_call_handler_finalize;
314
315   param_spec = g_param_spec_boxed ("members",
316     "call members", "The call participants",
317     G_TYPE_ARRAY,
318     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
319   g_object_class_install_property (object_class, PROP_MEMBERS, param_spec);
320
321   param_spec = g_param_spec_object ("call-channel",
322     "call channel", "The call channel",
323     TPY_TYPE_CALL_CHANNEL,
324     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
325   g_object_class_install_property (object_class, PROP_CALL_CHANNEL, param_spec);
326
327   param_spec = g_param_spec_boolean ("initial-audio",
328     "initial-audio", "Whether the call should start with audio",
329     TRUE,
330     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
331   g_object_class_install_property (object_class, PROP_INITIAL_AUDIO,
332       param_spec);
333
334   param_spec = g_param_spec_boolean ("initial-video",
335     "initial-video", "Whether the call should start with video",
336     FALSE,
337     G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
338   g_object_class_install_property (object_class, PROP_INITIAL_VIDEO,
339     param_spec);
340
341   param_spec = g_param_spec_boxed ("send-audio-codec",
342     "send audio codec", "Codec used to encode the outgoing video stream",
343     FS_TYPE_CODEC,
344     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
345   g_object_class_install_property (object_class, PROP_SEND_AUDIO_CODEC,
346     param_spec);
347
348   param_spec = g_param_spec_boxed ("send-video-codec",
349     "send video codec", "Codec used to encode the outgoing video stream",
350     FS_TYPE_CODEC,
351     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
352   g_object_class_install_property (object_class, PROP_SEND_VIDEO_CODEC,
353     param_spec);
354
355   param_spec = g_param_spec_boxed ("recv-audio-codecs",
356     "recvs audio codec", "Codecs used to decode the incoming audio stream",
357     FS_TYPE_CODEC_LIST,
358     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
359   g_object_class_install_property (object_class, PROP_RECV_AUDIO_CODECS,
360     param_spec);
361
362   param_spec = g_param_spec_boxed ("recv-video-codecs",
363     "recvs video codec", "Codecs used to decode the incoming video stream",
364     FS_TYPE_CODEC_LIST,
365     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
366   g_object_class_install_property (object_class, PROP_RECV_VIDEO_CODECS,
367     param_spec);
368
369   param_spec = g_param_spec_boxed ("audio-remote-candidate",
370     "audio remote candidate",
371     "Remote candidate used for the audio stream",
372     FS_TYPE_CANDIDATE,
373     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
374   g_object_class_install_property (object_class,
375       PROP_AUDIO_REMOTE_CANDIDATE, param_spec);
376
377   param_spec = g_param_spec_boxed ("video-remote-candidate",
378     "video remote candidate",
379     "Remote candidate used for the video stream",
380     FS_TYPE_CANDIDATE,
381     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
382   g_object_class_install_property (object_class,
383       PROP_VIDEO_REMOTE_CANDIDATE, param_spec);
384
385   param_spec = g_param_spec_boxed ("audio-local-candidate",
386     "audio local candidate",
387     "Local candidate used for the audio stream",
388     FS_TYPE_CANDIDATE,
389     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
390   g_object_class_install_property (object_class,
391       PROP_AUDIO_REMOTE_CANDIDATE, param_spec);
392
393   param_spec = g_param_spec_boxed ("video-local-candidate",
394     "video local candidate",
395     "Local candidate used for the video stream",
396     FS_TYPE_CANDIDATE,
397     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
398   g_object_class_install_property (object_class,
399       PROP_VIDEO_REMOTE_CANDIDATE, param_spec);
400
401   signals[CONFERENCE_ADDED] =
402     g_signal_new ("conference-added", G_TYPE_FROM_CLASS (klass),
403       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
404       g_cclosure_marshal_VOID__OBJECT,
405       G_TYPE_NONE,
406       1, FS_TYPE_CONFERENCE);
407
408   signals[CONFERENCE_REMOVED] =
409     g_signal_new ("conference-removed", G_TYPE_FROM_CLASS (klass),
410       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
411       g_cclosure_marshal_VOID__OBJECT,
412       G_TYPE_NONE,
413       1, FS_TYPE_CONFERENCE);
414
415   signals[SRC_PAD_ADDED] =
416     g_signal_new ("src-pad-added", G_TYPE_FROM_CLASS (klass),
417       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
418       _src_marshal_BOOLEAN__OBJECT_UINT,
419       G_TYPE_BOOLEAN,
420       2, GST_TYPE_PAD, G_TYPE_UINT);
421
422   signals[SINK_PAD_ADDED] =
423     g_signal_new ("sink-pad-added", G_TYPE_FROM_CLASS (klass),
424       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
425       _src_marshal_BOOLEAN__OBJECT_UINT,
426       G_TYPE_BOOLEAN,
427       2, GST_TYPE_PAD, G_TYPE_UINT);
428
429   signals[SINK_PAD_REMOVED] =
430     g_signal_new ("sink-pad-removed", G_TYPE_FROM_CLASS (klass),
431       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
432       _src_marshal_BOOLEAN__OBJECT_UINT,
433       G_TYPE_BOOLEAN,
434       2, GST_TYPE_PAD, G_TYPE_UINT);
435
436   signals[CLOSED] =
437     g_signal_new ("closed", G_TYPE_FROM_CLASS (klass),
438       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
439       g_cclosure_marshal_VOID__VOID,
440       G_TYPE_NONE,
441       0);
442
443   signals[CANDIDATES_CHANGED] =
444     g_signal_new ("candidates-changed", G_TYPE_FROM_CLASS (klass),
445       G_SIGNAL_RUN_LAST, 0, NULL, NULL,
446       g_cclosure_marshal_VOID__UINT,
447       G_TYPE_NONE, 1, G_TYPE_UINT);
448 }
449
450 EmpathyCallHandler *
451 empathy_call_handler_new_for_channel (TpyCallChannel *call)
452 {
453   return EMPATHY_CALL_HANDLER (g_object_new (EMPATHY_TYPE_CALL_HANDLER,
454     "call-channel", call,
455     "initial-video", tpy_call_channel_has_initial_video (call),
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       g_print ("empathy_call_handler_bus_message: farsight-send-codec-changed\n");
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       g_print ("empathy_call_handler_bus_message: farsight-recv-codecs-changed\n");
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       g_print ("empathy_call_handler_bus_message: farsight-new-active-candidate-pair\n");
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 (content, 0 /* FIXME */,
669       "Could not link sink", NULL);
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   guint media_type;
684   gboolean retval;
685
686   g_object_get (content, "media-type", &media_type, NULL);
687
688   g_signal_emit (G_OBJECT (handler), signals[SRC_PAD_ADDED], 0,
689       pad, media_type, &retval);
690
691   if (!retval)
692     g_idle_add (src_pad_added_error_idle, g_object_ref (content));
693 }
694
695 static void
696 on_tf_channel_content_added_cb (TfChannel *tfchannel,
697   TfContent *content,
698   EmpathyCallHandler *handler)
699 {
700   FsMediaType mtype;
701   GstPad *spad;
702   FsSession *session;
703 //  FsStream *fs_stream;
704   FsCodec *codec;
705 //  GList *codecs;
706   gboolean retval;
707
708   g_signal_connect (content, "src-pad-added",
709       G_CALLBACK (on_tf_content_src_pad_added_cb), handler);
710 #if 0
711   g_signal_connect (content, "start-sending",
712       G_CALLBACK (on_tf_content_start_sending_cb), handler);
713   g_signal_connect (content, "stop-sending",
714       G_CALLBACK (on_tf_content_stop_sending_cb), handler);
715 #endif
716
717   g_object_get (content, "media-type", &mtype,
718     "sink-pad", &spad, NULL);
719
720   g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_ADDED], 0,
721       spad, mtype, &retval);
722
723  if (!retval)
724       tf_content_error (content, 0 /* FIXME */,
725           "Could not link source", NULL);
726
727  /* Get sending codec */
728  g_object_get (content, "fs-session", &session, NULL);
729  g_object_get (session, "current-send-codec", &codec, NULL);
730
731  update_sending_codec (handler, codec, session);
732
733  tp_clear_object (&session);
734  tp_clear_object (&codec);
735
736  /* Get receiving codec */
737 /* FIXME
738  g_object_get (content, "fs-stream", &fs_stream, NULL);
739  g_object_get (fs_stream, "current-recv-codecs", &codecs, NULL);
740
741  update_receiving_codec (handler, codecs, fs_stream);
742
743  fs_codec_list_destroy (codecs);
744  tp_clear_object (&fs_stream);
745 */
746
747  gst_object_unref (spad);
748 }
749
750 static void
751 on_tf_channel_content_removed_cb (TfChannel *tfchannel,
752   TfContent *content,
753   EmpathyCallHandler *handler)
754 {
755   FsMediaType mtype;
756   GstPad *spad;
757   gboolean retval;
758
759   g_print ("removing content\n");
760
761   g_object_get (content, "media-type", &mtype,
762     "sink-pad", &spad, NULL);
763
764   g_signal_emit (G_OBJECT (handler), signals[SINK_PAD_REMOVED], 0,
765       spad, mtype, &retval);
766
767   if (!retval)
768     {
769       g_warning ("Could not remove content!");
770
771       tf_content_error (content, 0 /* FIXME */,
772           "Could not link source", NULL);
773     }
774 }
775
776 static void
777 on_tf_channel_closed_cb (TfChannel *tfchannel,
778     EmpathyCallHandler *handler)
779 {
780   g_signal_emit (G_OBJECT (handler), signals[CLOSED], 0);
781 }
782
783 static void
784 on_tf_channel_ready (GObject *source,
785     GAsyncResult *result,
786     gpointer user_data)
787 {
788   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
789   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
790   GError *error = NULL;
791
792   priv->tfchannel = TF_CHANNEL (g_async_initable_new_finish (
793       G_ASYNC_INITABLE (source), result, NULL));
794
795   if (priv->tfchannel == NULL)
796     {
797       g_warning ("Failed to create Farstream channel: %s", error->message);
798       g_error_free (error);
799       return;
800     }
801
802   /* Set up the telepathy farstream channel */
803   g_signal_connect (priv->tfchannel, "closed",
804       G_CALLBACK (on_tf_channel_closed_cb), self);
805   g_signal_connect (priv->tfchannel, "fs-conference-added",
806       G_CALLBACK (on_tf_channel_conference_added_cb), self);
807   g_signal_connect (priv->tfchannel, "fs-conference-removed",
808       G_CALLBACK (on_tf_channel_conference_removed_cb), self);
809   g_signal_connect (priv->tfchannel, "content-added",
810       G_CALLBACK (on_tf_channel_content_added_cb), self);
811   g_signal_connect (priv->tfchannel, "content-removed",
812       G_CALLBACK (on_tf_channel_content_removed_cb), self);
813 }
814
815 static void
816 empathy_call_handler_start_tpfs (EmpathyCallHandler *self)
817 {
818   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
819
820   tf_channel_new_async (TP_CHANNEL (priv->call),
821       on_tf_channel_ready, self);
822 }
823
824 #if 0
825 static void
826 empathy_call_handler_request_cb (GObject *source,
827     GAsyncResult *result,
828     gpointer user_data)
829 {
830   EmpathyCallHandler *self = EMPATHY_CALL_HANDLER (user_data);
831   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
832   TpChannel *channel;
833   GError *error = NULL;
834   TpAccountChannelRequest *req = TP_ACCOUNT_CHANNEL_REQUEST (source);
835
836   channel = tp_account_channel_request_create_and_handle_channel_finish (req,
837       result, NULL, &error);
838   if (channel == NULL)
839     {
840       DEBUG ("Failed to create the channel: %s", error->message);
841       g_error_free (error);
842       return;
843     }
844
845   if (!TPY_IS_CALL_CHANNEL (channel))
846     {
847       DEBUG ("The channel is not a Call channel!");
848       return;
849     }
850
851   priv->call = TPY_CALL_CHANNEL (channel);
852
853   g_object_notify (G_OBJECT (self), "call-channel");
854
855   empathy_call_handler_start_tpfs (self);
856 }
857 #endif
858
859 static void
860 on_call_accepted_cb (GObject *source_object,
861     GAsyncResult *res,
862     gpointer user_data)
863 {
864   TpyCallChannel *call = TPY_CALL_CHANNEL (source_object);
865   GError *error = NULL;
866
867   if (!tpy_call_channel_accept_finish (call, res, &error))
868     {
869       g_warning ("could not accept Call: %s", error->message);
870       g_error_free (error);
871     }
872 }
873
874 void
875 empathy_call_handler_start_call (EmpathyCallHandler *handler,
876     gint64 timestamp)
877 {
878   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
879 /*TpAccountChannelRequest *req;
880   TpAccount *account;
881   GHashTable *request;*/
882
883   if (priv->call != NULL)
884     {
885       empathy_call_handler_start_tpfs (handler);
886       tpy_call_channel_accept_async (priv->call, on_call_accepted_cb, NULL);
887       return;
888     }
889   else
890     {
891       g_warning ("No Call channel!");
892     }
893
894 #if 0
895   /* No TpyCallChannel (we are redialing). Request a new call channel */
896   g_assert (priv->contact != NULL);
897
898   account = empathy_contact_get_account (priv->contact);
899   request = empathy_call_create_call_request (priv->contact,
900       priv->initial_audio, priv->initial_video);
901
902   req = tp_account_channel_request_new (account, request, timestamp);
903
904   tp_account_channel_request_create_and_handle_channel_async (req, NULL,
905       empathy_call_handler_request_cb, handler);
906
907   g_object_unref (req);
908   g_hash_table_unref (request);
909 #endif
910 }
911
912 /**
913  * empathy_call_handler_stop_call:
914  * @handler: an #EmpathyCallHandler
915  *
916  * Closes the #EmpathyCallHandler's call and frees its resources.
917  */
918 void
919 empathy_call_handler_stop_call (EmpathyCallHandler *handler)
920 {
921   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
922
923   if (priv->call != NULL)
924     {
925       tpy_call_channel_hangup_async (priv->call,
926           TPY_CALL_STATE_CHANGE_REASON_USER_REQUESTED,
927           "", "", NULL, NULL);
928       tp_clear_object (&priv->call);
929     }
930 }
931
932 /**
933  * empathy_call_handler_has_initial_video:
934  * @handler: an #EmpathyCallHandler
935  *
936  * Return %TRUE if the call managed by this #EmpathyCallHandler was
937  * created with video enabled
938  *
939  * Return value: %TRUE if the call was created as a video conversation.
940  */
941 gboolean
942 empathy_call_handler_has_initial_video (EmpathyCallHandler *handler)
943 {
944   EmpathyCallHandlerPriv *priv = GET_PRIV (handler);
945
946   return priv->initial_video;
947 }
948
949 FsCodec *
950 empathy_call_handler_get_send_audio_codec (EmpathyCallHandler *self)
951 {
952   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
953
954   return priv->send_audio_codec;
955 }
956
957 FsCodec *
958 empathy_call_handler_get_send_video_codec (EmpathyCallHandler *self)
959 {
960   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
961
962   return priv->send_video_codec;
963 }
964
965 GList *
966 empathy_call_handler_get_recv_audio_codecs (EmpathyCallHandler *self)
967 {
968   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
969
970   return priv->recv_audio_codecs;
971 }
972
973 GList *
974 empathy_call_handler_get_recv_video_codecs (EmpathyCallHandler *self)
975 {
976   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
977
978   return priv->recv_video_codecs;
979 }
980
981 FsCandidate *
982 empathy_call_handler_get_audio_remote_candidate (
983     EmpathyCallHandler *self)
984 {
985   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
986
987   return priv->audio_remote_candidate;
988 }
989
990 FsCandidate *
991 empathy_call_handler_get_audio_local_candidate (
992     EmpathyCallHandler *self)
993 {
994   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
995
996   return priv->audio_local_candidate;
997 }
998
999 FsCandidate *
1000 empathy_call_handler_get_video_remote_candidate (
1001     EmpathyCallHandler *self)
1002 {
1003   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
1004
1005   return priv->video_remote_candidate;
1006 }
1007
1008 FsCandidate *
1009 empathy_call_handler_get_video_local_candidate (
1010     EmpathyCallHandler *self)
1011 {
1012   EmpathyCallHandlerPriv *priv = GET_PRIV (self);
1013
1014   return priv->video_local_candidate;
1015 }