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