]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Initial port from telepathy-stream-engine to telepathy-farsight
[empathy.git] / libempathy / empathy-tp-call.c
1 /*
2  * Copyright (C) 2007 Elliot Fairweather
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
20  *          Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <string.h>
24
25 #include <telepathy-glib/proxy-subclass.h>
26 #include <telepathy-glib/dbus.h>
27 #include <telepathy-glib/interfaces.h>
28 #include <telepathy-farsight/channel.h>
29 #include <telepathy-farsight/stream.h>
30
31 #include <gst/gst.h>
32
33 #include "empathy-tp-call.h"
34 #include "empathy-contact-factory.h"
35 #include "empathy-utils.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP
38 #include "empathy-debug.h"
39
40 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
41 typedef struct
42 {
43   gboolean dispose_has_run;
44   TpChannel *channel;
45   TfChannel *tfchannel;
46   EmpathyContact *contact;
47   gboolean is_incoming;
48   guint status;
49
50   GstElement *pipeline;
51
52   EmpathyTpCallStream *audio;
53   EmpathyTpCallStream *video;
54 } EmpathyTpCallPriv;
55
56 enum
57 {
58   PROP_0,
59   PROP_CHANNEL,
60   PROP_CONTACT,
61   PROP_IS_INCOMING,
62   PROP_STATUS,
63   PROP_AUDIO_STREAM,
64   PROP_VIDEO_STREAM
65 };
66
67 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
68
69 static void
70 tp_call_add_stream (EmpathyTpCall *call,
71                     guint stream_id,
72                     guint contact_handle,
73                     guint stream_type,
74                     guint stream_state,
75                     guint stream_direction)
76 {
77   EmpathyTpCallPriv *priv = GET_PRIV (call);
78
79   switch (stream_type)
80     {
81       case TP_MEDIA_STREAM_TYPE_AUDIO:
82         DEBUG ("Audio stream - id: %d, state: %d, direction: %d",
83             stream_id, stream_state, stream_direction);
84         priv->audio->exists = TRUE;
85         priv->audio->id = stream_id;
86         priv->audio->state = stream_state;
87         priv->audio->direction = stream_direction;
88         g_object_notify (G_OBJECT (call), "audio-stream");
89         break;
90       case TP_MEDIA_STREAM_TYPE_VIDEO:
91         DEBUG ("Video stream - id: %d, state: %d, direction: %d",
92             stream_id, stream_state, stream_direction);
93         priv->video->exists = TRUE;
94         priv->video->id = stream_id;
95         priv->video->state = stream_state;
96         priv->video->direction = stream_direction;
97         g_object_notify (G_OBJECT (call), "video-stream");
98         break;
99       default:
100         DEBUG ("Unknown stream type: %d", stream_type);
101     }
102 }
103
104 static void
105 tp_call_stream_added_cb (TpChannel *channel,
106                          guint stream_id,
107                          guint contact_handle,
108                          guint stream_type,
109                          gpointer user_data,
110                          GObject *call)
111 {
112   DEBUG ("Stream added - stream id: %d, contact handle: %d, stream type: %d",
113       stream_id, contact_handle, stream_type);
114
115   tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
116       stream_type, TP_MEDIA_STREAM_STATE_DISCONNECTED,
117       TP_MEDIA_STREAM_DIRECTION_NONE);
118 }
119
120 static void
121 tp_call_stream_removed_cb (TpChannel *channel,
122                            guint stream_id,
123                            gpointer user_data,
124                            GObject *call)
125 {
126   EmpathyTpCallPriv *priv = GET_PRIV (call);
127
128   DEBUG ("Stream removed - stream id: %d", stream_id);
129
130   if (stream_id == priv->audio->id)
131     {
132       priv->audio->exists = FALSE;
133       g_object_notify (call, "audio-stream");
134     }
135   else if (stream_id == priv->video->id)
136     {
137       priv->video->exists = FALSE;
138       g_object_notify (call, "video-stream");
139     }
140 }
141
142 static void
143 tp_call_stream_state_changed_cb (TpChannel *proxy,
144                                  guint stream_id,
145                                  guint stream_state,
146                                  gpointer user_data,
147                                  GObject *call)
148 {
149   EmpathyTpCallPriv *priv = GET_PRIV (call);
150
151   DEBUG ("Stream state changed - stream id: %d, state state: %d",
152       stream_id, stream_state);
153
154   if (stream_id == priv->audio->id)
155     {
156       priv->audio->state = stream_state;
157       g_object_notify (call, "audio-stream");
158     }
159   else if (stream_id == priv->video->id)
160     {
161       priv->video->state = stream_state;
162       g_object_notify (call, "video-stream");
163     }
164 }
165
166 static void
167 tp_call_stream_direction_changed_cb (TpChannel *channel,
168                                      guint stream_id,
169                                      guint stream_direction,
170                                      guint pending_flags,
171                                      gpointer user_data,
172                                      GObject *call)
173 {
174   EmpathyTpCallPriv *priv = GET_PRIV (call);
175
176   DEBUG ("Stream direction changed - stream: %d, direction: %d",
177       stream_id, stream_direction);
178
179   if (stream_id == priv->audio->id)
180     {
181       priv->audio->direction = stream_direction;
182       g_object_notify (call, "audio-stream");
183     }
184   else if (stream_id == priv->video->id)
185     {
186       priv->video->direction = stream_direction;
187       g_object_notify (call, "video-stream");
188     }
189 }
190
191 static void
192 tp_call_request_streams_cb (TpChannel *channel,
193                             const GPtrArray *streams,
194                             const GError *error,
195                             gpointer user_data,
196                             GObject *call)
197 {
198   guint i;
199
200   if (error)
201     {
202       DEBUG ("Error requesting streams: %s", error->message);
203       return;
204     }
205
206   for (i = 0; i < streams->len; i++)
207     {
208       GValueArray *values;
209       guint stream_id;
210       guint contact_handle;
211       guint stream_type;
212       guint stream_state;
213       guint stream_direction;
214
215       values = g_ptr_array_index (streams, i);
216       stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
217       contact_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
218       stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
219       stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
220       stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
221
222       tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
223           stream_type, stream_state, stream_direction);
224   }
225 }
226
227 static void
228 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
229                                           EmpathyCapabilities capabilities)
230 {
231   EmpathyTpCallPriv *priv = GET_PRIV (call);
232   GArray *stream_types;
233   guint handle;
234   guint stream_type;
235
236   if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
237       capabilities = EMPATHY_CAPABILITIES_AUDIO | EMPATHY_CAPABILITIES_VIDEO;
238
239   DEBUG ("Requesting new stream for capabilities %d",
240       capabilities);
241
242   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
243   handle = empathy_contact_get_handle (priv->contact);
244
245   if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
246     {
247       stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
248       g_array_append_val (stream_types, stream_type);
249     }
250   if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
251     {
252       stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
253       g_array_append_val (stream_types, stream_type);
254     }
255
256   tp_cli_channel_type_streamed_media_call_request_streams (priv->channel, -1,
257       handle, stream_types, tp_call_request_streams_cb, NULL, NULL,
258       G_OBJECT (call));
259
260   g_array_free (stream_types, TRUE);
261 }
262
263 static EmpathyContact *
264 tp_call_dup_contact_from_handle (EmpathyTpCall *call, TpHandle handle)
265 {
266   EmpathyTpCallPriv *priv = GET_PRIV (call);
267   EmpathyContactFactory *factory;
268   McAccount *account;
269   EmpathyContact *contact;
270
271   factory = empathy_contact_factory_dup_singleton ();
272   account = empathy_channel_get_account (priv->channel);
273   contact = empathy_contact_factory_get_from_handle (factory, account, handle);
274
275   g_object_unref (factory);
276   g_object_unref (account);
277
278   return contact;
279 }
280
281 static void
282 tp_call_update_status (EmpathyTpCall *call)
283 {
284   EmpathyTpCallPriv *priv = GET_PRIV (call);
285   TpHandle self_handle;
286   const TpIntSet *set;
287   TpIntSetIter iter;
288
289   g_object_ref (call);
290
291   self_handle = tp_channel_group_get_self_handle (priv->channel);
292   set = tp_channel_group_get_members (priv->channel);
293   tp_intset_iter_init (&iter, set);
294   while (tp_intset_iter_next (&iter))
295     {
296       if (priv->contact == NULL && iter.element != self_handle)
297         {
298           /* We found the remote contact */
299           priv->contact = tp_call_dup_contact_from_handle (call, iter.element);
300           priv->is_incoming = TRUE;
301           priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
302           g_object_notify (G_OBJECT (call), "is-incoming");
303           g_object_notify (G_OBJECT (call), "contact");
304           g_object_notify (G_OBJECT (call), "status");
305         }
306
307       if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
308           ((priv->is_incoming && iter.element == self_handle) ||
309            (!priv->is_incoming && iter.element != self_handle)))
310         {
311           priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
312           g_object_notify (G_OBJECT (call), "status");
313         }
314     }
315
316   g_object_unref (call);
317 }
318
319 static void
320 tp_call_members_changed_cb (TpChannel *channel,
321                             gchar *message,
322                             GArray *added,
323                             GArray *removed,
324                             GArray *local_pending,
325                             GArray *remote_pending,
326                             guint actor,
327                             guint reason,
328                             EmpathyTpCall *call)
329 {
330   tp_call_update_status (call);
331 }
332
333 void
334 empathy_tp_call_to (EmpathyTpCall *call, EmpathyContact *contact)
335 {
336   EmpathyTpCallPriv *priv = GET_PRIV (call);
337
338   priv->contact = g_object_ref (contact);
339   priv->is_incoming = FALSE;
340   priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
341   g_object_notify (G_OBJECT (call), "is-incoming");
342   g_object_notify (G_OBJECT (call), "contact");
343   g_object_notify (G_OBJECT (call), "status");
344   tp_call_request_streams_for_capabilities (call,
345      EMPATHY_CAPABILITIES_AUDIO);
346 }
347
348 static void
349 tp_call_channel_invalidated_cb (TpChannel     *channel,
350                                 GQuark         domain,
351                                 gint           code,
352                                 gchar         *message,
353                                 EmpathyTpCall *call)
354 {
355   EmpathyTpCallPriv *priv = GET_PRIV (call);
356
357   DEBUG ("Channel invalidated: %s", message);
358   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
359   g_object_notify (G_OBJECT (call), "status");
360 }
361
362 static void
363 tp_call_async_cb (TpProxy *proxy,
364                   const GError *error,
365                   gpointer user_data,
366                   GObject *call)
367 {
368   if (error)
369       DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
370 }
371
372 static void
373 tp_call_close_channel (EmpathyTpCall *call)
374 {
375   EmpathyTpCallPriv *priv = GET_PRIV (call);
376
377   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
378       return;
379
380   DEBUG ("Closing channel");
381
382   tp_cli_channel_call_close (priv->channel, -1,
383       NULL, NULL, NULL, NULL);
384
385   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
386   g_object_notify (G_OBJECT (call), "status");
387 }
388
389 static gboolean
390 tp_call_pipeline_bus_watch (GstBus *bus, GstMessage *message,
391   gpointer user_data)
392 {
393   EmpathyTpCall *call = EMPATHY_TP_CALL (user_data);
394   EmpathyTpCallPriv *priv = GET_PRIV (call);
395
396   g_assert (priv->tfchannel != NULL);
397
398   tf_channel_bus_message (priv->tfchannel, message);
399
400   return TRUE;
401 }
402
403 static void
404 tp_call_tf_channel_session_created_cb (TfChannel *tfchannel,
405   FsConference *conference, FsParticipant *participant, EmpathyTpCall *call)
406 {
407   EmpathyTpCallPriv *priv = GET_PRIV (call);
408   GstBus *bus;
409
410   g_assert (priv->pipeline == NULL);
411
412   priv->pipeline = gst_pipeline_new ("call-pipeline");
413
414   bus = gst_pipeline_get_bus (GST_PIPELINE (priv->pipeline));
415   gst_bus_add_watch (bus, tp_call_pipeline_bus_watch, call);
416   gst_object_unref (bus);
417
418   gst_bin_add ( GST_BIN (priv->pipeline), GST_ELEMENT (conference));
419   gst_element_set_state ( GST_ELEMENT(priv->pipeline), GST_STATE_PLAYING);
420 }
421
422 static void
423 tp_call_tf_stream_src_pad_added_cb (TfStream *stream, GstPad   *pad,
424   FsCodec  *codec, EmpathyTpCall  *call)
425 {
426   EmpathyTpCallPriv *priv = GET_PRIV (call);
427   guint media_type;
428   GstElement *sink;
429   GstPad *spad;
430
431   g_object_get (stream, "media-type", &media_type, NULL);
432
433   switch (media_type)
434     {
435       case TP_MEDIA_STREAM_TYPE_AUDIO:
436         sink = gst_element_factory_make ("gconfaudiosink", NULL);
437         break;
438       case TP_MEDIA_STREAM_TYPE_VIDEO:
439         sink = gst_element_factory_make ("gconfvideosink", NULL);
440         break;
441       default:
442         g_assert_not_reached();
443     }
444
445   gst_bin_add ( GST_BIN (priv->pipeline), sink);
446   gst_element_set_state (sink, GST_STATE_PLAYING);
447
448   spad = gst_element_get_static_pad (sink, "sink");
449   gst_pad_link (pad, spad);
450   gst_object_unref (spad);
451 }
452
453
454 static gboolean
455 tp_call_tf_stream_request_resource_cb (TfStream *stream,
456   guint direction, EmpathyTpCall *call)
457 {
458   return TRUE;
459 }
460
461 static void
462 tp_call_tf_channel_stream_created_cb (TfChannel *tfchannel, TfStream *stream,
463   EmpathyTpCall *call)
464 {
465   EmpathyTpCallPriv *priv = GET_PRIV (call);
466   guint media_type;
467   GstElement *src;
468   GstPad *pad, *spad;
469
470   g_signal_connect (stream, "src-pad-added",
471       G_CALLBACK (tp_call_tf_stream_src_pad_added_cb), call);
472   g_signal_connect (stream, "request-resource",
473       G_CALLBACK (tp_call_tf_stream_request_resource_cb), call);
474
475
476   g_object_get (stream, "media-type", &media_type,
477     "sink-pad", &spad, NULL);
478
479   switch (media_type)
480     {
481       case TP_MEDIA_STREAM_TYPE_AUDIO:
482         src = gst_element_factory_make ("gconfaudiosrc", NULL);
483         break;
484       case TP_MEDIA_STREAM_TYPE_VIDEO:
485         src = gst_element_factory_make ("gconfvideosrc", NULL);
486         break;
487       default:
488         g_assert_not_reached();
489     }
490
491   gst_bin_add (GST_BIN (priv->pipeline), src);
492
493   pad = gst_element_get_static_pad (src, "src");
494   gst_pad_link (pad, spad);
495   gst_object_unref (spad);
496
497   gst_element_set_state (src, GST_STATE_PLAYING);
498 }
499
500 static GObject *
501 tp_call_constructor (GType type,
502                      guint n_construct_params,
503                      GObjectConstructParam *construct_params)
504 {
505   GObject *object;
506   EmpathyTpCall *call;
507   EmpathyTpCallPriv *priv;
508
509   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
510       n_construct_params, construct_params);
511
512   call = EMPATHY_TP_CALL (object);
513   priv = GET_PRIV (call);
514
515   /* Setup streamed media channel */
516   g_signal_connect (priv->channel, "invalidated",
517       G_CALLBACK (tp_call_channel_invalidated_cb), call);
518   tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
519       tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
520   tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
521       tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
522   tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
523       tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
524   tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
525       tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
526   tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
527       tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
528
529   /* Update status when members changes */
530   tp_call_update_status (call);
531   g_signal_connect (priv->channel, "group-members-changed",
532       G_CALLBACK (tp_call_members_changed_cb), call);
533
534
535   /* Set up the telepathy farsight channel */
536   priv->tfchannel = tf_channel_new (priv->channel);
537   g_signal_connect (priv->tfchannel, "session-created",
538       G_CALLBACK (tp_call_tf_channel_session_created_cb), call);
539   g_signal_connect (priv->tfchannel, "stream-created",
540       G_CALLBACK (tp_call_tf_channel_stream_created_cb), call);
541
542   return object;
543 }
544 static void
545 tp_call_dispose (GObject *object)
546 {
547   EmpathyTpCallPriv *priv = GET_PRIV (object);
548
549   DEBUG ("Disposing: %p, %d", object, priv->dispose_has_run);
550
551   if (priv->dispose_has_run)
552     return;
553
554   priv->dispose_has_run = TRUE;
555
556   g_slice_free (EmpathyTpCallStream, priv->audio);
557   g_slice_free (EmpathyTpCallStream, priv->video);
558
559   if (priv->channel != NULL)
560     {
561       g_signal_handlers_disconnect_by_func (priv->channel,
562         tp_call_channel_invalidated_cb, object);
563
564       g_object_unref (priv->channel);
565       priv->channel = NULL;
566     }
567
568   if (priv->pipeline != NULL)
569     {
570       gst_element_set_state (priv->pipeline, GST_STATE_NULL);
571       gst_object_unref (priv->pipeline);
572       priv->pipeline = NULL;
573     }
574
575   if (priv->tfchannel != NULL)
576     {
577       g_object_unref (priv->tfchannel);
578       priv->tfchannel = NULL;
579     }
580
581   if (priv->contact != NULL)
582       g_object_unref (priv->contact);
583
584   if (G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose)
585     G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose (object);
586 }
587
588 static void
589 tp_call_finalize (GObject *object)
590 {
591   EmpathyTpCallPriv *priv = GET_PRIV (object);
592
593   DEBUG ("Finalizing: %p", object);
594
595   g_slice_free (EmpathyTpCallStream, priv->audio);
596   g_slice_free (EmpathyTpCallStream, priv->video);
597
598   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
599 }
600
601 static void
602 tp_call_set_property (GObject *object,
603                       guint prop_id,
604                       const GValue *value,
605                       GParamSpec *pspec)
606 {
607   EmpathyTpCallPriv *priv = GET_PRIV (object);
608
609   switch (prop_id)
610     {
611     case PROP_CHANNEL:
612       priv->channel = g_value_dup_object (value);
613       break;
614     default:
615       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
616       break;
617   }
618 }
619
620 static void
621 tp_call_get_property (GObject *object,
622                       guint prop_id,
623                       GValue *value,
624                       GParamSpec *pspec)
625 {
626   EmpathyTpCallPriv *priv = GET_PRIV (object);
627
628   switch (prop_id)
629     {
630     case PROP_CHANNEL:
631       g_value_set_object (value, priv->channel);
632       break;
633     case PROP_CONTACT:
634       g_value_set_object (value, priv->contact);
635       break;
636     case PROP_IS_INCOMING:
637       g_value_set_boolean (value, priv->is_incoming);
638       break;
639     case PROP_STATUS:
640       g_value_set_uint (value, priv->status);
641       break;
642     case PROP_AUDIO_STREAM:
643       g_value_set_pointer (value, priv->audio);
644       break;
645     case PROP_VIDEO_STREAM:
646       g_value_set_pointer (value, priv->video);
647       break;
648     default:
649       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
650       break;
651   }
652 }
653
654 static void
655 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
656 {
657   GObjectClass *object_class = G_OBJECT_CLASS (klass);
658
659   object_class->constructor = tp_call_constructor;
660   object_class->dispose = tp_call_dispose;
661   object_class->finalize = tp_call_finalize;
662   object_class->set_property = tp_call_set_property;
663   object_class->get_property = tp_call_get_property;
664
665   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
666
667   g_object_class_install_property (object_class, PROP_CHANNEL,
668       g_param_spec_object ("channel", "channel", "channel",
669       TP_TYPE_CHANNEL,
670       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
671       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
672   g_object_class_install_property (object_class, PROP_CONTACT,
673       g_param_spec_object ("contact", "Call contact", "Call contact",
674       EMPATHY_TYPE_CONTACT,
675       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
676   g_object_class_install_property (object_class, PROP_IS_INCOMING,
677       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
678       "Is media stream incoming", FALSE, G_PARAM_READABLE |
679       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
680   g_object_class_install_property (object_class, PROP_STATUS,
681       g_param_spec_uint ("status", "Call status",
682       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
683       G_PARAM_STATIC_BLURB));
684   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
685       g_param_spec_pointer ("audio-stream", "Audio stream data",
686       "Audio stream data",
687       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
688   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
689       g_param_spec_pointer ("video-stream", "Video stream data",
690       "Video stream data",
691       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
692 }
693
694 static void
695 empathy_tp_call_init (EmpathyTpCall *call)
696 {
697   EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
698     EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
699
700   call->priv = priv;
701   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
702   priv->contact = NULL;
703   priv->audio = g_slice_new0 (EmpathyTpCallStream);
704   priv->video = g_slice_new0 (EmpathyTpCallStream);
705   priv->audio->exists = FALSE;
706   priv->video->exists = FALSE;
707 }
708
709 EmpathyTpCall *
710 empathy_tp_call_new (TpChannel *channel)
711 {
712   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
713
714   return g_object_new (EMPATHY_TYPE_TP_CALL,
715       "channel", channel,
716       NULL);
717 }
718
719 void
720 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
721 {
722   EmpathyTpCallPriv *priv = GET_PRIV (call);
723   TpHandle self_handle;
724   GArray handles = {(gchar *) &self_handle, 1};
725
726   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
727   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
728   g_return_if_fail (priv->is_incoming);
729
730   DEBUG ("Accepting incoming call");
731
732   self_handle = tp_channel_group_get_self_handle (priv->channel);
733   tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
734       &handles, NULL, NULL, NULL, NULL, NULL);
735 }
736
737 void
738 empathy_tp_call_close (EmpathyTpCall *call)
739 {
740   EmpathyTpCallPriv *priv = GET_PRIV (call);
741
742   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
743
744   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
745       return;
746
747   DEBUG ("Closing channel");
748
749   tp_cli_channel_call_close (priv->channel, -1,
750       NULL, NULL, NULL, NULL);
751
752   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
753   g_object_notify (G_OBJECT (call), "status");
754 }
755
756 void
757 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
758                                                 gboolean is_sending)
759 {
760   EmpathyTpCallPriv *priv = GET_PRIV (call);
761   guint new_direction;
762
763   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
764   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
765
766   DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
767
768   if (!priv->video->exists)
769     {
770       if (is_sending)
771           tp_call_request_streams_for_capabilities (call,
772               EMPATHY_CAPABILITIES_VIDEO);
773       return;
774     }
775
776   if (is_sending)
777       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
778   else
779       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
780
781   tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
782       -1, priv->video->id, new_direction,
783       (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
784       tp_call_async_cb, NULL, NULL, G_OBJECT (call));
785 }
786
787 void
788 empathy_tp_call_add_preview_video (EmpathyTpCall *call,
789                                    guint preview_video_socket_id)
790 {
791   //EmpathyTpCallPriv *priv = GET_PRIV (call);
792
793   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
794
795   DEBUG ("Adding preview video");
796
797   /* FIXME add the preview window */
798 }
799
800 void
801 empathy_tp_call_remove_preview_video (EmpathyTpCall *call,
802                                       guint preview_video_socket_id)
803 {
804   //EmpathyTpCallPriv *priv = GET_PRIV (call);
805
806   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
807
808   DEBUG ("Removing preview video");
809
810   /* FIXME remove the preview window */
811 }
812
813 void
814 empathy_tp_call_add_output_video (EmpathyTpCall *call,
815                                   guint output_video_socket_id)
816 {
817   //EmpathyTpCallPriv *priv = GET_PRIV (call);
818
819   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
820
821   DEBUG ("Adding output video - socket: %d", output_video_socket_id);
822
823   /* FIXME add output window */
824 }
825
826 void
827 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
828                                    guint volume)
829 {
830   EmpathyTpCallPriv *priv = GET_PRIV (call);
831
832   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
833   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
834
835   DEBUG ("Setting output volume: %d", volume);
836
837   /* FIXME set volume */
838 }
839
840 void
841 empathy_tp_call_mute_output (EmpathyTpCall *call,
842                              gboolean is_muted)
843 {
844   //EmpathyTpCallPriv *priv = GET_PRIV (call);
845
846   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
847
848   DEBUG ("Setting output mute: %d", is_muted);
849   /* FIXME mute output */
850 }
851
852 void
853 empathy_tp_call_mute_input (EmpathyTpCall *call,
854                             gboolean is_muted)
855 {
856   //EmpathyTpCallPriv *priv = GET_PRIV (call);
857
858   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
859
860   DEBUG ("Setting input mute: %d", is_muted);
861
862   /* FIXME mute input */
863 }
864
865 void
866 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
867 {
868   EmpathyTpCallPriv *priv = GET_PRIV (call);
869
870   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
871   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
872
873   if (!priv->audio->exists)
874       return;
875
876   tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
877       priv->audio->id, event,
878       (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
879       "starting tone", NULL, G_OBJECT (call));
880 }
881
882 void
883 empathy_tp_call_stop_tone (EmpathyTpCall *call)
884 {
885   EmpathyTpCallPriv *priv = GET_PRIV (call);
886
887   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
888   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
889
890   if (!priv->audio->exists)
891       return;
892
893   tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
894       priv->audio->id,
895       (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
896       "stoping tone", NULL, G_OBJECT (call));
897 }
898
899 gboolean
900 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
901 {
902   EmpathyTpCallPriv *priv = GET_PRIV (call);
903
904   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
905
906   return tp_proxy_has_interface_by_id (priv->channel,
907       TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
908 }
909