]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Remove usage of EmpathyTpGroup from EmpathyTpCall.
[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_close_channel (EmpathyTpCall *call)
374 {
375   EmpathyTpCallPriv *priv = GET_PRIV (call);
376
377   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
378       return;
379
380   DEBUG ("Closing channel");
381
382   tp_cli_channel_call_close (priv->channel, -1,
383       NULL, NULL, NULL, NULL);
384
385   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
386   g_object_notify (G_OBJECT (call), "status");
387 }
388
389 static void
390 tp_call_stream_engine_invalidated_cb (TpProxy       *stream_engine,
391                                       GQuark         domain,
392                                       gint           code,
393                                       gchar         *message,
394                                       EmpathyTpCall *call)
395 {
396   DEBUG ("Stream engine proxy invalidated: %s", message);
397   tp_call_close_channel (call);
398 }
399
400 static void
401 tp_call_stream_engine_watch_name_owner_cb (TpDBusDaemon *daemon,
402                                            const gchar *name,
403                                            const gchar *new_owner,
404                                            gpointer call)
405 {
406   EmpathyTpCallPriv *priv = GET_PRIV (call);
407
408   /* G_STR_EMPTY(new_owner) means either stream-engine has not started yet or
409    * has crashed. We want to close the channel if stream-engine has crashed.
410    * */
411   DEBUG ("Watch SE: name='%s' SE running='%s' new_owner='%s'",
412       name, priv->stream_engine_running ? "yes" : "no",
413       new_owner ? new_owner : "none");
414   if (priv->stream_engine_running && G_STR_EMPTY (new_owner))
415     {
416       DEBUG ("Stream engine falled off the bus");
417       tp_call_close_channel (call);
418       return;
419     }
420
421   priv->stream_engine_running = !G_STR_EMPTY (new_owner);
422 }
423
424 static void
425 tp_call_stream_engine_handle_channel (EmpathyTpCall *call)
426 {
427   EmpathyTpCallPriv *priv = GET_PRIV (call);
428   gchar *channel_type;
429   gchar *object_path;
430   guint handle_type;
431   guint handle;
432   TpProxy *connection;
433
434   DEBUG ("Revving up the stream engine");
435
436   priv->stream_engine = g_object_new (TP_TYPE_PROXY,
437       "bus-name", STREAM_ENGINE_BUS_NAME,
438       "dbus-connection", tp_get_bus (),
439       "object-path", STREAM_ENGINE_OBJECT_PATH,
440        NULL);
441   tp_proxy_add_interface_by_id (priv->stream_engine,
442       EMP_IFACE_QUARK_STREAM_ENGINE);
443   tp_proxy_add_interface_by_id (priv->stream_engine,
444       EMP_IFACE_QUARK_CHANNEL_HANDLER);
445
446   g_signal_connect (priv->stream_engine, "invalidated",
447       G_CALLBACK (tp_call_stream_engine_invalidated_cb),
448       call);
449   
450   /* FIXME: dbus daemon should be unique */
451   priv->dbus_daemon = tp_dbus_daemon_new (tp_get_bus ());
452   tp_dbus_daemon_watch_name_owner (priv->dbus_daemon, STREAM_ENGINE_BUS_NAME,
453       tp_call_stream_engine_watch_name_owner_cb,
454       call, NULL);
455
456   g_object_get (priv->channel,
457       "connection", &connection,
458       "channel-type", &channel_type,
459       "object-path", &object_path,
460       "handle_type", &handle_type,
461       "handle", &handle,
462       NULL);
463
464   emp_cli_channel_handler_call_handle_channel (priv->stream_engine, -1,
465         connection->bus_name,
466         connection->object_path,
467         channel_type, object_path, handle_type, handle,
468         tp_call_async_cb, "calling handle channel", NULL,
469         G_OBJECT (call));
470
471   g_object_unref (connection);
472   g_free (channel_type);
473   g_free (object_path);
474 }
475
476 static GObject *
477 tp_call_constructor (GType type,
478                      guint n_construct_params,
479                      GObjectConstructParam *construct_params)
480 {
481   GObject *object;
482   EmpathyTpCall *call;
483   EmpathyTpCallPriv *priv;
484
485   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
486       n_construct_params, construct_params);
487
488   call = EMPATHY_TP_CALL (object);
489   priv = GET_PRIV (call);
490
491   /* Setup streamed media channel */
492   g_signal_connect (priv->channel, "invalidated",
493       G_CALLBACK (tp_call_channel_invalidated_cb), call);
494   tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
495       tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
496   tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
497       tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
498   tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
499       tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
500   tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
501       tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
502   tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
503       tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
504
505   /* Update status when members changes */
506   tp_call_update_status (call);
507   g_signal_connect (priv->channel, "group-members-changed",
508       G_CALLBACK (tp_call_members_changed_cb), call);
509
510   /* Start stream engine */
511   tp_call_stream_engine_handle_channel (call);
512
513   return object;
514 }
515
516 static void
517 tp_call_finalize (GObject *object)
518 {
519   EmpathyTpCallPriv *priv = GET_PRIV (object);
520
521   DEBUG ("Finalizing: %p", object);
522
523   g_slice_free (EmpathyTpCallStream, priv->audio);
524   g_slice_free (EmpathyTpCallStream, priv->video);
525
526   if (priv->channel != NULL)
527     {
528       g_signal_handlers_disconnect_by_func (priv->channel,
529         tp_call_channel_invalidated_cb, object);
530
531       g_object_unref (priv->channel);
532       priv->channel = NULL;
533     }
534
535   if (priv->stream_engine != NULL)
536     {
537       g_signal_handlers_disconnect_by_func (priv->stream_engine,
538           tp_call_stream_engine_invalidated_cb, object);
539       g_object_unref (priv->stream_engine);
540     }
541
542   if (priv->contact != NULL)
543       g_object_unref (priv->contact);
544
545   if (priv->dbus_daemon != NULL)
546     {
547       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus_daemon,
548           STREAM_ENGINE_BUS_NAME,
549           tp_call_stream_engine_watch_name_owner_cb,
550           object);
551       g_object_unref (priv->dbus_daemon);
552     }
553
554   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
555 }
556
557 static void 
558 tp_call_set_property (GObject *object,
559                       guint prop_id,
560                       const GValue *value,
561                       GParamSpec *pspec)
562 {
563   EmpathyTpCallPriv *priv = GET_PRIV (object);
564
565   switch (prop_id)
566     {
567     case PROP_CHANNEL:
568       priv->channel = g_value_dup_object (value);
569       break;
570     default:
571       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
572       break;
573   }
574 }
575
576 static void
577 tp_call_get_property (GObject *object,
578                       guint prop_id,
579                       GValue *value,
580                       GParamSpec *pspec)
581 {
582   EmpathyTpCallPriv *priv = GET_PRIV (object);
583
584   switch (prop_id)
585     {
586     case PROP_CHANNEL:
587       g_value_set_object (value, priv->channel);
588       break;
589     case PROP_CONTACT:
590       g_value_set_object (value, priv->contact);
591       break;
592     case PROP_IS_INCOMING:
593       g_value_set_boolean (value, priv->is_incoming);
594       break;
595     case PROP_STATUS:
596       g_value_set_uint (value, priv->status);
597       break;
598     case PROP_AUDIO_STREAM:
599       g_value_set_pointer (value, priv->audio);
600       break;
601     case PROP_VIDEO_STREAM:
602       g_value_set_pointer (value, priv->video);
603       break;
604     default:
605       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
606       break;
607   }
608 }
609
610 static void
611 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
612 {
613   GObjectClass *object_class = G_OBJECT_CLASS (klass);
614
615   object_class->constructor = tp_call_constructor;
616   object_class->finalize = tp_call_finalize;
617   object_class->set_property = tp_call_set_property;
618   object_class->get_property = tp_call_get_property;
619
620   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
621
622   g_object_class_install_property (object_class, PROP_CHANNEL,
623       g_param_spec_object ("channel", "channel", "channel",
624       TP_TYPE_CHANNEL,
625       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
626       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
627   g_object_class_install_property (object_class, PROP_CONTACT,
628       g_param_spec_object ("contact", "Call contact", "Call contact",
629       EMPATHY_TYPE_CONTACT,
630       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
631   g_object_class_install_property (object_class, PROP_IS_INCOMING,
632       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
633       "Is media stream incoming", FALSE, G_PARAM_READABLE |
634       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
635   g_object_class_install_property (object_class, PROP_STATUS,
636       g_param_spec_uint ("status", "Call status",
637       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
638       G_PARAM_STATIC_BLURB));
639   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
640       g_param_spec_pointer ("audio-stream", "Audio stream data",
641       "Audio stream data",
642       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
643   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
644       g_param_spec_pointer ("video-stream", "Video stream data",
645       "Video stream data",
646       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
647 }
648
649 static void
650 empathy_tp_call_init (EmpathyTpCall *call)
651 {
652   EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
653     EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
654
655   call->priv = priv;
656   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
657   priv->contact = NULL;
658   priv->stream_engine_running = FALSE;
659   priv->audio = g_slice_new0 (EmpathyTpCallStream);
660   priv->video = g_slice_new0 (EmpathyTpCallStream);
661   priv->audio->exists = FALSE;
662   priv->video->exists = FALSE;
663 }
664
665 EmpathyTpCall *
666 empathy_tp_call_new (TpChannel *channel)
667 {
668   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
669
670   return g_object_new (EMPATHY_TYPE_TP_CALL,
671       "channel", channel,
672       NULL);
673 }
674
675 void
676 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
677 {
678   EmpathyTpCallPriv *priv = GET_PRIV (call);
679   TpHandle self_handle;
680   GArray handles = {(gchar *) &self_handle, 1};
681
682   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
683   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
684   g_return_if_fail (priv->is_incoming);
685
686   DEBUG ("Accepting incoming call");
687
688   self_handle = tp_channel_group_get_self_handle (priv->channel);
689   tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
690       &handles, NULL, NULL, NULL, NULL, NULL);
691 }
692
693 void
694 empathy_tp_call_close (EmpathyTpCall *call)
695 {
696   EmpathyTpCallPriv *priv = GET_PRIV (call);
697
698   g_signal_handlers_disconnect_by_func (priv->channel,
699     tp_call_channel_invalidated_cb, call);
700
701   tp_call_close_channel (call);
702
703   g_object_unref (priv->channel);
704   priv->channel = NULL;
705 }
706
707 void
708 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
709                                                 gboolean is_sending)
710 {
711   EmpathyTpCallPriv *priv = GET_PRIV (call);
712   guint new_direction;
713
714   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
715   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
716
717   DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
718
719   if (!priv->video->exists)
720     {
721       if (is_sending)
722           tp_call_request_streams_for_capabilities (call,
723               EMPATHY_CAPABILITIES_VIDEO);
724       return;
725     }
726
727   if (is_sending)
728       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
729   else
730       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
731
732   tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
733       -1, priv->video->id, new_direction,
734       (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
735       tp_call_async_cb, NULL, NULL, G_OBJECT (call));
736 }
737
738 void
739 empathy_tp_call_add_preview_video (EmpathyTpCall *call,
740                                    guint preview_video_socket_id)
741 {
742   EmpathyTpCallPriv *priv = GET_PRIV (call);
743
744   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
745
746   DEBUG ("Adding preview video");
747
748   emp_cli_stream_engine_call_add_preview_window (priv->stream_engine, -1,
749       preview_video_socket_id,
750       tp_call_async_cb,
751       "adding preview window", NULL,
752       G_OBJECT (call));
753 }
754
755 void
756 empathy_tp_call_remove_preview_video (EmpathyTpCall *call,
757                                       guint preview_video_socket_id)
758 {
759   EmpathyTpCallPriv *priv = GET_PRIV (call);
760
761   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
762
763   DEBUG ("Removing preview video");
764
765   emp_cli_stream_engine_call_remove_preview_window (priv->stream_engine, -1,
766       preview_video_socket_id,
767       tp_call_async_cb,
768       "removing preview window", NULL,
769       G_OBJECT (call));
770 }
771
772 void
773 empathy_tp_call_add_output_video (EmpathyTpCall *call,
774                                   guint output_video_socket_id)
775 {
776   EmpathyTpCallPriv *priv = GET_PRIV (call);
777
778   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
779
780   DEBUG ("Adding output video - socket: %d", output_video_socket_id);
781
782   emp_cli_stream_engine_call_set_output_window (priv->stream_engine, -1,
783       TP_PROXY (priv->channel)->object_path,
784       priv->video->id, output_video_socket_id,
785       tp_call_async_cb,
786       "setting output window", NULL,
787       G_OBJECT (call));
788 }
789
790 void
791 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
792                                    guint volume)
793 {
794   EmpathyTpCallPriv *priv = GET_PRIV (call);
795
796   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
797   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
798
799   DEBUG ("Setting output volume: %d", volume);
800
801   emp_cli_stream_engine_call_set_output_volume (priv->stream_engine, -1,
802       TP_PROXY (priv->channel)->object_path,
803       priv->audio->id, volume,
804       tp_call_async_cb,
805       "setting output volume", NULL,
806       G_OBJECT (call));
807 }
808
809 void
810 empathy_tp_call_mute_output (EmpathyTpCall *call,
811                              gboolean is_muted)
812 {
813   EmpathyTpCallPriv *priv = GET_PRIV (call);
814
815   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
816
817   DEBUG ("Setting output mute: %d", is_muted);
818
819   emp_cli_stream_engine_call_mute_output (priv->stream_engine, -1,
820       TP_PROXY (priv->channel)->object_path,
821       priv->audio->id, is_muted,
822       tp_call_async_cb,
823       "muting output", NULL,
824       G_OBJECT (call));
825 }
826
827 void
828 empathy_tp_call_mute_input (EmpathyTpCall *call,
829                             gboolean is_muted)
830 {
831   EmpathyTpCallPriv *priv = GET_PRIV (call);
832
833   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
834
835   DEBUG ("Setting input mute: %d", is_muted);
836
837   emp_cli_stream_engine_call_mute_input (priv->stream_engine, -1,
838       TP_PROXY (priv->channel)->object_path,
839       priv->audio->id, is_muted,
840       tp_call_async_cb,
841       "muting input", NULL,
842       G_OBJECT (call));
843 }
844
845 void
846 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
847 {
848   EmpathyTpCallPriv *priv = GET_PRIV (call);
849
850   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
851   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
852
853   if (!priv->audio->exists)
854       return;
855
856   tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
857       priv->audio->id, event,
858       (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
859       "starting tone", NULL, G_OBJECT (call));
860 }
861
862 void
863 empathy_tp_call_stop_tone (EmpathyTpCall *call)
864 {
865   EmpathyTpCallPriv *priv = GET_PRIV (call);
866
867   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
868   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
869
870   if (!priv->audio->exists)
871       return;
872
873   tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
874       priv->audio->id,
875       (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
876       "stoping tone", NULL, G_OBJECT (call));
877 }
878
879 gboolean
880 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
881 {
882   EmpathyTpCallPriv *priv = GET_PRIV (call);
883
884   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
885
886   return tp_proxy_has_interface_by_id (priv->channel,
887       TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
888 }
889