]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Use TP_ARRAY_TYPE_UCHAR_ARRAY_LIST
[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-glib/util.h>
29
30 #include "empathy-tp-call.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-utils.h"
33 #include "empathy-marshal.h"
34
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include "empathy-debug.h"
37
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
39 typedef struct
40 {
41   gboolean dispose_has_run;
42   TpAccount *account;
43   TpChannel *channel;
44   EmpathyContact *contact;
45   gboolean is_incoming;
46   guint status;
47
48   EmpathyTpCallStream *audio;
49   EmpathyTpCallStream *video;
50 } EmpathyTpCallPriv;
51
52 /* signal enum */
53 enum {
54   AUDIO_STREAM_ERROR,
55   VIDEO_STREAM_ERROR,
56   LAST_SIGNAL
57 };
58
59 static guint signals[LAST_SIGNAL] = {0};
60
61 enum
62 {
63   PROP_0,
64   PROP_ACCOUNT,
65   PROP_CHANNEL,
66   PROP_CONTACT,
67   PROP_STATUS,
68   PROP_AUDIO_STREAM,
69   PROP_VIDEO_STREAM
70 };
71
72 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
73
74 static void
75 tp_call_add_stream (EmpathyTpCall *call,
76                     guint stream_id,
77                     guint contact_handle,
78                     guint stream_type,
79                     guint stream_state,
80                     guint stream_direction)
81 {
82   EmpathyTpCallPriv *priv = GET_PRIV (call);
83
84   switch (stream_type)
85     {
86       case TP_MEDIA_STREAM_TYPE_AUDIO:
87         DEBUG ("Audio stream - id: %d, state: %d, direction: %d",
88             stream_id, stream_state, stream_direction);
89         priv->audio->exists = TRUE;
90         priv->audio->id = stream_id;
91         priv->audio->state = stream_state;
92         priv->audio->direction = stream_direction;
93         g_object_notify (G_OBJECT (call), "audio-stream");
94         break;
95       case TP_MEDIA_STREAM_TYPE_VIDEO:
96         DEBUG ("Video stream - id: %d, state: %d, direction: %d",
97             stream_id, stream_state, stream_direction);
98         priv->video->exists = TRUE;
99         priv->video->id = stream_id;
100         priv->video->state = stream_state;
101         priv->video->direction = stream_direction;
102         g_object_notify (G_OBJECT (call), "video-stream");
103         break;
104       default:
105         DEBUG ("Unknown stream type: %d", stream_type);
106     }
107 }
108
109 static void
110 tp_call_stream_added_cb (TpChannel *channel,
111                          guint stream_id,
112                          guint contact_handle,
113                          guint stream_type,
114                          gpointer user_data,
115                          GObject *call)
116 {
117   DEBUG ("Stream added - stream id: %d, contact handle: %d, stream type: %d",
118       stream_id, contact_handle, stream_type);
119
120   tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
121       stream_type, TP_MEDIA_STREAM_STATE_DISCONNECTED,
122       TP_MEDIA_STREAM_DIRECTION_NONE);
123 }
124
125 static void
126 tp_call_stream_removed_cb (TpChannel *channel,
127                            guint stream_id,
128                            gpointer user_data,
129                            GObject *call)
130 {
131   EmpathyTpCallPriv *priv = GET_PRIV (call);
132
133   DEBUG ("Stream removed - stream id: %d", stream_id);
134
135   if (stream_id == priv->audio->id)
136     {
137       priv->audio->exists = FALSE;
138       g_object_notify (call, "audio-stream");
139     }
140   else if (stream_id == priv->video->id)
141     {
142       priv->video->exists = FALSE;
143       g_object_notify (call, "video-stream");
144     }
145 }
146
147 static void
148 tp_call_stream_state_changed_cb (TpChannel *proxy,
149                                  guint stream_id,
150                                  guint stream_state,
151                                  gpointer user_data,
152                                  GObject *call)
153 {
154   EmpathyTpCallPriv *priv = GET_PRIV (call);
155
156   DEBUG ("Stream state changed - stream id: %d, state state: %d",
157       stream_id, stream_state);
158
159   if (stream_id == priv->audio->id)
160     {
161       priv->audio->state = stream_state;
162       g_object_notify (call, "audio-stream");
163     }
164   else if (stream_id == priv->video->id)
165     {
166       priv->video->state = stream_state;
167       g_object_notify (call, "video-stream");
168     }
169 }
170
171 static void
172 tp_call_stream_direction_changed_cb (TpChannel *channel,
173                                      guint stream_id,
174                                      guint stream_direction,
175                                      guint pending_flags,
176                                      gpointer user_data,
177                                      GObject *call)
178 {
179   EmpathyTpCallPriv *priv = GET_PRIV (call);
180
181   DEBUG ("Stream direction changed - stream: %d, direction: %d",
182       stream_id, stream_direction);
183
184   if (stream_id == priv->audio->id)
185     {
186       priv->audio->direction = stream_direction;
187       g_object_notify (call, "audio-stream");
188     }
189   else if (stream_id == priv->video->id)
190     {
191       priv->video->direction = stream_direction;
192       g_object_notify (call, "video-stream");
193     }
194 }
195
196 static void
197 tp_call_request_streams_cb (TpChannel *channel,
198                             const GPtrArray *streams,
199                             const GError *error,
200                             gpointer user_data,
201                             GObject *call)
202 {
203   guint i;
204
205   if (error)
206     {
207       DEBUG ("Error requesting streams: %s", error->message);
208       return;
209     }
210
211   for (i = 0; i < streams->len; i++)
212     {
213       GValueArray *values;
214       guint stream_id;
215       guint contact_handle;
216       guint stream_type;
217       guint stream_state;
218       guint stream_direction;
219
220       values = g_ptr_array_index (streams, i);
221       stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
222       contact_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
223       stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
224       stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
225       stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
226
227       tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
228           stream_type, stream_state, stream_direction);
229   }
230 }
231
232 static void
233 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
234                                           EmpathyCapabilities capabilities)
235 {
236   EmpathyTpCallPriv *priv = GET_PRIV (call);
237   GArray *stream_types;
238   guint handle;
239   guint stream_type;
240
241   if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
242       capabilities = EMPATHY_CAPABILITIES_AUDIO | EMPATHY_CAPABILITIES_VIDEO;
243
244   DEBUG ("Requesting new stream for capabilities %d",
245       capabilities);
246
247   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
248   handle = empathy_contact_get_handle (priv->contact);
249
250   if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
251     {
252       stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
253       g_array_append_val (stream_types, stream_type);
254     }
255   if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
256     {
257       stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
258       g_array_append_val (stream_types, stream_type);
259     }
260
261   tp_cli_channel_type_streamed_media_call_request_streams (priv->channel, -1,
262       handle, stream_types, tp_call_request_streams_cb, NULL, NULL,
263       G_OBJECT (call));
264
265   g_array_free (stream_types, TRUE);
266 }
267
268 static void
269 tp_call_got_contact_cb (TpConnection            *connection,
270                         EmpathyContact          *contact,
271                         const GError            *error,
272                         gpointer                 user_data,
273                         GObject                 *call)
274 {
275   EmpathyTpCallPriv *priv = GET_PRIV (call);
276
277   if (error)
278     {
279       DEBUG ("Error: %s", error->message);
280       return;
281     }
282
283   priv->contact = g_object_ref (contact);
284
285   if (priv->status < EMPATHY_TP_CALL_STATUS_PENDING)
286     {
287       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
288       g_object_notify (G_OBJECT (call), "status");
289     }
290
291   g_object_notify (G_OBJECT (call), "contact");
292 }
293
294 static void
295 tp_call_update_status (EmpathyTpCall *call)
296 {
297   EmpathyTpCallPriv *priv = GET_PRIV (call);
298   TpHandle self_handle;
299   const TpIntSet *set;
300   TpIntSetIter iter;
301
302   g_object_ref (call);
303
304   self_handle = tp_channel_group_get_self_handle (priv->channel);
305   set = tp_channel_group_get_members (priv->channel);
306   tp_intset_iter_init (&iter, set);
307   while (tp_intset_iter_next (&iter))
308     {
309       if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
310           ((priv->is_incoming && iter.element == self_handle) ||
311            (!priv->is_incoming && iter.element != self_handle)))
312         {
313           priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
314           g_object_notify (G_OBJECT (call), "status");
315         }
316     }
317
318   g_object_unref (call);
319 }
320
321 static void
322 tp_call_channel_invalidated_cb (TpChannel     *channel,
323                                 GQuark         domain,
324                                 gint           code,
325                                 gchar         *message,
326                                 EmpathyTpCall *call)
327 {
328   EmpathyTpCallPriv *priv = GET_PRIV (call);
329
330   DEBUG ("Channel invalidated: %s", message);
331   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
332   g_object_notify (G_OBJECT (call), "status");
333 }
334
335 static void
336 tp_call_async_cb (TpProxy *proxy,
337                   const GError *error,
338                   gpointer user_data,
339                   GObject *call)
340 {
341   if (error)
342       DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
343 }
344
345 static void
346 tp_call_stream_error_cb (TpChannel *channel,
347     guint stream_id,
348     guint error_code,
349     const gchar *msg,
350     gpointer user_data,
351     GObject *call)
352 {
353   EmpathyTpCall *self = EMPATHY_TP_CALL (call);
354   EmpathyTpCallPriv *priv = GET_PRIV (self);
355
356   DEBUG ("Stream error on stream %u: %s (code: %u)", stream_id, msg,
357       error_code);
358
359   if (priv->audio->id == stream_id)
360     {
361       g_signal_emit (call, signals[AUDIO_STREAM_ERROR], 0, error_code, msg);
362     }
363   else if (priv->video->id == stream_id)
364     {
365       g_signal_emit (call, signals[VIDEO_STREAM_ERROR], 0, error_code, msg);
366     }
367   else
368     {
369       DEBUG ("Unknown stream id: %u", stream_id);
370     }
371 }
372
373 static GObject *
374 tp_call_constructor (GType type,
375                      guint n_construct_params,
376                      GObjectConstructParam *construct_params)
377 {
378   GObject *object;
379   EmpathyTpCall *call;
380   EmpathyTpCallPriv *priv;
381   GHashTable *props;
382   gboolean requested;
383
384   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
385       n_construct_params, construct_params);
386
387   call = EMPATHY_TP_CALL (object);
388   priv = GET_PRIV (call);
389
390   /* Setup streamed media channel */
391   g_signal_connect (priv->channel, "invalidated",
392       G_CALLBACK (tp_call_channel_invalidated_cb), call);
393   tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
394       tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
395   tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
396       tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
397   tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
398       tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
399   tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
400       tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
401   tp_cli_channel_type_streamed_media_connect_to_stream_error (priv->channel,
402       tp_call_stream_error_cb, NULL, NULL, G_OBJECT (call), NULL);
403   tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
404       tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
405
406   /* Is the call incoming? */
407   props = tp_channel_borrow_immutable_properties (priv->channel);
408   requested = tp_asv_get_boolean (props, TP_PROP_CHANNEL_REQUESTED, NULL);
409
410   priv->is_incoming = !requested;
411
412   /* Get the remote contact */
413   empathy_tp_contact_factory_get_from_handle (
414       tp_channel_borrow_connection (priv->channel),
415       tp_channel_get_handle (priv->channel, NULL), tp_call_got_contact_cb,
416       NULL, NULL, object);
417
418   /* Update status when members changes */
419   tp_call_update_status (call);
420   tp_g_signal_connect_object (priv->channel, "group-members-changed",
421       G_CALLBACK (tp_call_update_status), call, G_CONNECT_SWAPPED);
422
423   return object;
424 }
425 static void
426 tp_call_dispose (GObject *object)
427 {
428   EmpathyTpCallPriv *priv = GET_PRIV (object);
429
430   DEBUG ("Disposing: %p, %d", object, priv->dispose_has_run);
431
432   if (priv->dispose_has_run)
433     return;
434
435   priv->dispose_has_run = TRUE;
436
437   if (priv->channel != NULL)
438     {
439       g_signal_handlers_disconnect_by_func (priv->channel,
440         tp_call_channel_invalidated_cb, object);
441
442       g_object_unref (priv->channel);
443       priv->channel = NULL;
444     }
445
446   if (priv->contact != NULL)
447       g_object_unref (priv->contact);
448
449   tp_clear_object (&priv->account);
450
451   if (G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose)
452     G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose (object);
453 }
454
455 static void
456 tp_call_finalize (GObject *object)
457 {
458   EmpathyTpCallPriv *priv = GET_PRIV (object);
459
460   DEBUG ("Finalizing: %p", object);
461
462   g_slice_free (EmpathyTpCallStream, priv->audio);
463   g_slice_free (EmpathyTpCallStream, priv->video);
464
465   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
466 }
467
468 static void
469 tp_call_set_property (GObject *object,
470                       guint prop_id,
471                       const GValue *value,
472                       GParamSpec *pspec)
473 {
474   EmpathyTpCallPriv *priv = GET_PRIV (object);
475
476   switch (prop_id)
477     {
478     case PROP_ACCOUNT:
479       priv->account = g_value_dup_object (value);
480       break;
481     case PROP_CHANNEL:
482       priv->channel = g_value_dup_object (value);
483       break;
484     default:
485       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486       break;
487   }
488 }
489
490 static void
491 tp_call_get_property (GObject *object,
492                       guint prop_id,
493                       GValue *value,
494                       GParamSpec *pspec)
495 {
496   EmpathyTpCallPriv *priv = GET_PRIV (object);
497
498   switch (prop_id)
499     {
500     case PROP_ACCOUNT:
501       g_value_set_object (value, priv->channel);
502       break;
503     case PROP_CHANNEL:
504       g_value_set_object (value, priv->channel);
505       break;
506     case PROP_CONTACT:
507       g_value_set_object (value, priv->contact);
508       break;
509     case PROP_STATUS:
510       g_value_set_uint (value, priv->status);
511       break;
512     case PROP_AUDIO_STREAM:
513       g_value_set_pointer (value, priv->audio);
514       break;
515     case PROP_VIDEO_STREAM:
516       g_value_set_pointer (value, priv->video);
517       break;
518     default:
519       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
520       break;
521   }
522 }
523
524 static void
525 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
526 {
527   GObjectClass *object_class = G_OBJECT_CLASS (klass);
528
529   object_class->constructor = tp_call_constructor;
530   object_class->dispose = tp_call_dispose;
531   object_class->finalize = tp_call_finalize;
532   object_class->set_property = tp_call_set_property;
533   object_class->get_property = tp_call_get_property;
534
535   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
536
537   g_object_class_install_property (object_class, PROP_ACCOUNT,
538       g_param_spec_object ("account", "TpAccount", "TpAccount",
539       TP_TYPE_ACCOUNT,
540       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
541       G_PARAM_STATIC_STRINGS));
542
543   g_object_class_install_property (object_class, PROP_CHANNEL,
544       g_param_spec_object ("channel", "channel", "channel",
545       TP_TYPE_CHANNEL,
546       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
547       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
548
549   g_object_class_install_property (object_class, PROP_CONTACT,
550       g_param_spec_object ("contact", "Call contact", "Call contact",
551       EMPATHY_TYPE_CONTACT,
552       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
553
554   g_object_class_install_property (object_class, PROP_STATUS,
555       g_param_spec_uint ("status", "Call status",
556       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
557       G_PARAM_STATIC_BLURB));
558
559   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
560       g_param_spec_pointer ("audio-stream", "Audio stream data",
561       "Audio stream data",
562       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
563
564   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
565       g_param_spec_pointer ("video-stream", "Video stream data",
566       "Video stream data",
567       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
568
569   signals[AUDIO_STREAM_ERROR] =
570     g_signal_new ("audio-stream-error",
571       G_TYPE_FROM_CLASS (klass),
572       G_SIGNAL_RUN_LAST, 0,
573       NULL, NULL,
574       _empathy_marshal_VOID__UINT_STRING,
575       G_TYPE_NONE,
576       2, G_TYPE_UINT, G_TYPE_STRING);
577
578   signals[VIDEO_STREAM_ERROR] =
579     g_signal_new ("video-stream-error",
580       G_TYPE_FROM_CLASS (klass),
581       G_SIGNAL_RUN_LAST, 0,
582       NULL, NULL,
583       _empathy_marshal_VOID__UINT_STRING,
584       G_TYPE_NONE,
585       2, G_TYPE_UINT, G_TYPE_STRING);
586 }
587
588 static void
589 empathy_tp_call_init (EmpathyTpCall *call)
590 {
591   EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
592     EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
593
594   call->priv = priv;
595   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
596   priv->contact = NULL;
597   priv->audio = g_slice_new0 (EmpathyTpCallStream);
598   priv->video = g_slice_new0 (EmpathyTpCallStream);
599   priv->audio->exists = FALSE;
600   priv->video->exists = FALSE;
601 }
602
603 EmpathyTpCall *
604 empathy_tp_call_new (TpAccount *account,
605     TpChannel *channel)
606 {
607   g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
608   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
609
610   return g_object_new (EMPATHY_TYPE_TP_CALL,
611       "account", account,
612       "channel", channel,
613       NULL);
614 }
615
616 void
617 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
618 {
619   EmpathyTpCallPriv *priv = GET_PRIV (call);
620   TpHandle self_handle;
621   GArray handles = {(gchar *) &self_handle, 1};
622
623   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
624   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
625
626   if (!priv->is_incoming)
627     return;
628
629   DEBUG ("Accepting incoming call");
630
631   self_handle = tp_channel_group_get_self_handle (priv->channel);
632   tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
633       &handles, NULL, NULL, NULL, NULL, NULL);
634 }
635
636 void
637 empathy_tp_call_close (EmpathyTpCall *call)
638 {
639   EmpathyTpCallPriv *priv = GET_PRIV (call);
640
641   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
642
643   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
644       return;
645
646   DEBUG ("Closing channel");
647
648   tp_cli_channel_call_close (priv->channel, -1,
649       NULL, NULL, NULL, NULL);
650
651   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
652   g_object_notify (G_OBJECT (call), "status");
653 }
654
655 void
656 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
657                                                 gboolean is_sending)
658 {
659   EmpathyTpCallPriv *priv = GET_PRIV (call);
660   guint new_direction;
661
662   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
663   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
664
665   DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
666
667   if (!priv->video->exists)
668     {
669       if (is_sending)
670           tp_call_request_streams_for_capabilities (call,
671               EMPATHY_CAPABILITIES_VIDEO);
672       return;
673     }
674
675   if (is_sending)
676       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
677   else
678       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
679
680   tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
681       -1, priv->video->id, new_direction,
682       (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
683       tp_call_async_cb, NULL, NULL, G_OBJECT (call));
684 }
685
686 void
687 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
688 {
689   EmpathyTpCallPriv *priv = GET_PRIV (call);
690
691   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
692   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
693
694   if (!priv->audio->exists)
695       return;
696
697   tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
698       priv->audio->id, event,
699       (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
700       "starting tone", NULL, G_OBJECT (call));
701 }
702
703 void
704 empathy_tp_call_stop_tone (EmpathyTpCall *call)
705 {
706   EmpathyTpCallPriv *priv = GET_PRIV (call);
707
708   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
709   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
710
711   if (!priv->audio->exists)
712       return;
713
714   tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
715       priv->audio->id,
716       (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
717       "stoping tone", NULL, G_OBJECT (call));
718 }
719
720 gboolean
721 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
722 {
723   EmpathyTpCallPriv *priv = GET_PRIV (call);
724
725   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
726
727   return tp_proxy_has_interface_by_id (priv->channel,
728       TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
729 }
730
731 /**
732  * empathy_tp_call_is_receiving_video:
733  * @call: the call
734  *
735  * Indicates if the call is receiving video or not.
736  *
737  * Returns: %TRUE if the call is currently receiving video, %FALSE otherwise.
738  */
739 gboolean
740 empathy_tp_call_is_receiving_video (EmpathyTpCall *call)
741 {
742   EmpathyTpCallPriv *priv = GET_PRIV (call);
743
744   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
745
746   if (!priv->video->exists)
747     return FALSE;
748
749   return priv->video->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE ?
750       TRUE : FALSE;
751 }
752
753 /**
754  * empathy_tp_call_is_sending_video:
755  * @call: the call
756  *
757  * Indicates if the call is sending video or not.
758  *
759  * Returns: %TRUE if the call is currently sending video, %FALSE otherwise.
760  */
761 gboolean
762 empathy_tp_call_is_sending_video (EmpathyTpCall *call)
763 {
764   EmpathyTpCallPriv *priv = GET_PRIV (call);
765
766   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
767
768   if (!priv->video->exists)
769     return FALSE;
770
771   return priv->video->direction & TP_MEDIA_STREAM_DIRECTION_SEND ?
772       TRUE : FALSE;
773 }
774
775 const gchar *
776 empathy_tp_call_get_connection_manager (EmpathyTpCall *self)
777 {
778   EmpathyTpCallPriv *priv = GET_PRIV (self);
779
780   return tp_account_get_connection_manager (priv->account);
781 }
782
783 gboolean
784 empathy_tp_call_has_initial_video (EmpathyTpCall *self)
785 {
786   EmpathyTpCallPriv *priv = GET_PRIV (self);
787   GHashTable *props;
788   gboolean initial_video;
789   gboolean valid;
790
791   if (priv->channel == NULL)
792     return FALSE;
793
794   g_object_get (priv->channel, "channel-properties", &props, NULL);
795
796   initial_video = tp_asv_get_boolean (props,
797     TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo", &valid);
798   if (!valid)
799     initial_video = FALSE;
800
801   g_hash_table_unref (props);
802   return initial_video;
803 }
804
805 static void
806 leave_remove_members_cb (TpChannel *proxy,
807     const GError *error,
808     gpointer user_data,
809     GObject *weak_object)
810 {
811   EmpathyTpCall *self = user_data;
812
813   if (error == NULL)
814     return;
815
816   DEBUG ("RemoveMembers failed (%s); closing the channel", error->message);
817   empathy_tp_call_close (self);
818 }
819
820 void
821 empathy_tp_call_leave (EmpathyTpCall *self)
822 {
823   EmpathyTpCallPriv *priv = GET_PRIV (self);
824   TpHandle self_handle;
825   GArray array = { (gchar *) &self_handle, 1 };
826
827   if (!tp_proxy_has_interface_by_id (priv->channel,
828         TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
829     {
830       empathy_tp_call_close (self);
831       return;
832     }
833
834   self_handle = tp_channel_group_get_self_handle (priv->channel);
835   if (self_handle == 0)
836     {
837       /* we are not member of the channel */
838       empathy_tp_call_close (self);
839       return;
840     }
841
842   tp_cli_channel_interface_group_call_remove_members (priv->channel, -1, &array,
843       "", leave_remove_members_cb, self, NULL, G_OBJECT (self));
844 }
845
846 EmpathyTpCallStatus
847 empathy_tp_call_get_status (EmpathyTpCall *self)
848 {
849   EmpathyTpCallPriv *priv = GET_PRIV (self);
850
851   return priv->status;
852 }
853
854 TpAccount *
855 empathy_tp_call_get_account (EmpathyTpCall *self)
856 {
857   EmpathyTpCallPriv *priv = GET_PRIV (self);
858
859   return priv->account;
860 }