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