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