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