]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Add a function to explicitly set the remote candidate on outgoing calls instead of...
[empathy.git] / libempathy / empathy-tp-call.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Elliot Fairweather
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include <string.h>
25
26 #include <telepathy-glib/proxy-subclass.h>
27 #include <telepathy-glib/dbus.h>
28 #include <telepathy-glib/interfaces.h>
29
30 #include <extensions/extensions.h>
31
32 #include "empathy-tp-call.h"
33 #include "empathy-contact-factory.h"
34 #include "empathy-tp-group.h"
35 #include "empathy-utils.h"
36
37 #define DEBUG_FLAG EMPATHY_DEBUG_TP
38 #include "empathy-debug.h"
39
40 #define STREAM_ENGINE_BUS_NAME "org.freedesktop.Telepathy.StreamEngine"
41 #define STREAM_ENGINE_OBJECT_PATH "/org/freedesktop/Telepathy/StreamEngine"
42
43 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
44 typedef struct
45 {
46   TpChannel *channel;
47   TpProxy *stream_engine;
48   TpDBusDaemon *dbus_daemon;
49   EmpathyTpGroup *group;
50   EmpathyContact *contact;
51   gboolean is_incoming;
52   guint status;
53   gboolean stream_engine_running;
54
55   EmpathyTpCallStream *audio;
56   EmpathyTpCallStream *video;
57 } EmpathyTpCallPriv;
58
59 enum
60 {
61   PROP_0,
62   PROP_CHANNEL,
63   PROP_CONTACT,
64   PROP_IS_INCOMING,
65   PROP_STATUS,
66   PROP_AUDIO_STREAM,
67   PROP_VIDEO_STREAM
68 };
69
70 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
71
72 static void
73 tp_call_add_stream (EmpathyTpCall *call,
74                     guint stream_id,
75                     guint contact_handle,
76                     guint stream_type,
77                     guint stream_state,
78                     guint stream_direction)
79 {
80   EmpathyTpCallPriv *priv = GET_PRIV (call);
81
82   switch (stream_type)
83     {
84       case TP_MEDIA_STREAM_TYPE_AUDIO:
85         DEBUG ("Audio stream - id: %d, state: %d, direction: %d",
86             stream_id, stream_state, stream_direction);
87         priv->audio->exists = TRUE;
88         priv->audio->id = stream_id;
89         priv->audio->state = stream_state;
90         priv->audio->direction = stream_direction;
91         g_object_notify (G_OBJECT (call), "audio-stream");
92         break;
93       case TP_MEDIA_STREAM_TYPE_VIDEO:
94         DEBUG ("Video stream - id: %d, state: %d, direction: %d",
95             stream_id, stream_state, stream_direction);
96         priv->video->exists = TRUE;
97         priv->video->id = stream_id;
98         priv->video->state = stream_state;
99         priv->video->direction = stream_direction;
100         g_object_notify (G_OBJECT (call), "video-stream");
101         break;
102       default:
103         DEBUG ("Unknown stream type: %d", stream_type);
104     }
105 }
106
107 static void
108 tp_call_stream_added_cb (TpChannel *channel,
109                          guint stream_id,
110                          guint contact_handle,
111                          guint stream_type,
112                          gpointer user_data,
113                          GObject *call)
114 {
115   DEBUG ("Stream added - stream id: %d, contact handle: %d, stream type: %d",
116       stream_id, contact_handle, stream_type);
117
118   tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
119       stream_type, TP_MEDIA_STREAM_STATE_DISCONNECTED,
120       TP_MEDIA_STREAM_DIRECTION_NONE);
121 }
122
123 static void
124 tp_call_stream_removed_cb (TpChannel *channel,
125                            guint stream_id,
126                            gpointer user_data,
127                            GObject *call)
128 {
129   EmpathyTpCallPriv *priv = GET_PRIV (call);
130
131   DEBUG ("Stream removed - stream id: %d", stream_id);
132
133   if (stream_id == priv->audio->id)
134     {
135       priv->audio->exists = FALSE;
136       g_object_notify (call, "audio-stream");
137     }
138   else if (stream_id == priv->video->id)
139     {
140       priv->video->exists = FALSE;
141       g_object_notify (call, "video-stream");
142     }
143 }
144
145 static void
146 tp_call_stream_state_changed_cb (TpChannel *proxy,
147                                  guint stream_id,
148                                  guint stream_state,
149                                  gpointer user_data,
150                                  GObject *call)
151 {
152   EmpathyTpCallPriv *priv = GET_PRIV (call);
153
154   DEBUG ("Stream state changed - stream id: %d, state state: %d",
155       stream_id, stream_state);
156
157   if (stream_id == priv->audio->id)
158     {
159       priv->audio->state = stream_state;
160       g_object_notify (call, "audio-stream");
161     }
162   else if (stream_id == priv->video->id)
163     {
164       priv->video->state = stream_state;
165       g_object_notify (call, "video-stream");
166     }
167 }
168
169 static void
170 tp_call_stream_direction_changed_cb (TpChannel *channel,
171                                      guint stream_id,
172                                      guint stream_direction,
173                                      guint pending_flags,
174                                      gpointer user_data,
175                                      GObject *call)
176 {
177   EmpathyTpCallPriv *priv = GET_PRIV (call);
178
179   DEBUG ("Stream direction changed - stream: %d, direction: %d",
180       stream_id, stream_direction);
181
182   if (stream_id == priv->audio->id)
183     {
184       priv->audio->direction = stream_direction;
185       g_object_notify (call, "audio-stream");
186     }
187   else if (stream_id == priv->video->id)
188     {
189       priv->video->direction = stream_direction;
190       g_object_notify (call, "video-stream");
191     }
192 }
193
194 static void
195 tp_call_request_streams_cb (TpChannel *channel,
196                             const GPtrArray *streams,
197                             const GError *error,
198                             gpointer user_data,
199                             GObject *call)
200 {
201   guint i;
202
203   if (error)
204     {
205       DEBUG ("Error requesting streams: %s", error->message);
206       return;
207     }
208
209   for (i = 0; i < streams->len; i++)
210     {
211       GValueArray *values;
212       guint stream_id;
213       guint contact_handle;
214       guint stream_type;
215       guint stream_state;
216       guint stream_direction;
217
218       values = g_ptr_array_index (streams, i);
219       stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
220       contact_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
221       stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
222       stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
223       stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
224
225       tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
226           stream_type, stream_state, stream_direction);
227   }
228 }
229
230 static void
231 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
232                                           EmpathyCapabilities capabilities)
233 {
234   EmpathyTpCallPriv *priv = GET_PRIV (call);
235   GArray *stream_types;
236   guint handle;
237   guint stream_type;
238
239   if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
240       capabilities = EMPATHY_CAPABILITIES_AUDIO | EMPATHY_CAPABILITIES_VIDEO;
241
242   DEBUG ("Requesting new stream for capabilities %d",
243       capabilities);
244
245   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
246   handle = empathy_contact_get_handle (priv->contact);
247
248   if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
249     {
250       stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
251       g_array_append_val (stream_types, stream_type);
252     }
253   if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
254     {
255       stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
256       g_array_append_val (stream_types, stream_type);
257     }
258
259   tp_cli_channel_type_streamed_media_call_request_streams (priv->channel, -1,
260       handle, stream_types, tp_call_request_streams_cb, NULL, NULL,
261       G_OBJECT (call));
262
263   g_array_free (stream_types, TRUE);
264 }
265
266 static void
267 tp_call_member_added_cb (EmpathyTpGroup *group,
268                          EmpathyContact *contact,
269                          EmpathyContact *actor,
270                          guint reason,
271                          const gchar *message,
272                          EmpathyTpCall *call)
273 {
274   EmpathyTpCallPriv *priv = GET_PRIV (call);
275
276   g_object_ref (call);
277   if (!priv->contact && !empathy_contact_is_user (contact))
278     {
279       priv->contact = g_object_ref (contact);
280       priv->is_incoming = TRUE;
281       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
282       g_object_notify (G_OBJECT (call), "is-incoming");
283       g_object_notify (G_OBJECT (call), "contact");
284       g_object_notify (G_OBJECT (call), "status");
285     }
286
287   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
288       ((priv->is_incoming && contact != priv->contact) ||
289        (!priv->is_incoming && contact == priv->contact)))
290     {
291       priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
292       g_object_notify (G_OBJECT (call), "status");
293     }
294   g_object_unref (call);
295 }
296
297 void
298 empathy_tp_call_to (EmpathyTpCall *call, EmpathyContact *contact)
299 {
300   EmpathyTpCallPriv *priv = GET_PRIV (call);
301
302   priv->contact = g_object_ref (contact);
303   priv->is_incoming = FALSE;
304   priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
305   g_object_notify (G_OBJECT (call), "is-incoming");
306   g_object_notify (G_OBJECT (call), "contact");
307   g_object_notify (G_OBJECT (call), "status");
308   tp_call_request_streams_for_capabilities (call,
309      EMPATHY_CAPABILITIES_AUDIO);
310 }
311
312 static void
313 tp_call_channel_invalidated_cb (TpChannel     *channel,
314                                 GQuark         domain,
315                                 gint           code,
316                                 gchar         *message,
317                                 EmpathyTpCall *call)
318 {
319   EmpathyTpCallPriv *priv = GET_PRIV (call);
320
321   DEBUG ("Channel invalidated: %s", message);
322   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
323   g_object_notify (G_OBJECT (call), "status");
324 }
325
326 static void
327 tp_call_async_cb (TpProxy *proxy,
328                   const GError *error,
329                   gpointer user_data,
330                   GObject *call)
331 {
332   if (error)
333       DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
334 }
335
336 static void
337 tp_call_close_channel (EmpathyTpCall *call)
338 {
339   EmpathyTpCallPriv *priv = GET_PRIV (call);
340
341   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
342       return;
343
344   DEBUG ("Closing channel");
345
346   tp_cli_channel_call_close (priv->channel, -1,
347       NULL, NULL, NULL, NULL);
348
349   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
350   g_object_notify (G_OBJECT (call), "status");
351 }
352
353 static void
354 tp_call_stream_engine_invalidated_cb (TpProxy       *stream_engine,
355                                       GQuark         domain,
356                                       gint           code,
357                                       gchar         *message,
358                                       EmpathyTpCall *call)
359 {
360   DEBUG ("Stream engine proxy invalidated: %s", message);
361   tp_call_close_channel (call);
362 }
363
364 static void
365 tp_call_stream_engine_watch_name_owner_cb (TpDBusDaemon *daemon,
366                                            const gchar *name,
367                                            const gchar *new_owner,
368                                            gpointer call)
369 {
370   EmpathyTpCallPriv *priv = GET_PRIV (call);
371
372   /* G_STR_EMPTY(new_owner) means either stream-engine has not started yet or
373    * has crashed. We want to close the channel if stream-engine has crashed.
374    * */
375   DEBUG ("Watch SE: name='%s' SE running='%s' new_owner='%s'",
376       name, priv->stream_engine_running ? "yes" : "no",
377       new_owner ? new_owner : "none");
378   if (priv->stream_engine_running && G_STR_EMPTY (new_owner))
379     {
380       DEBUG ("Stream engine falled off the bus");
381       tp_call_close_channel (call);
382       return;
383     }
384
385   priv->stream_engine_running = !G_STR_EMPTY (new_owner);
386 }
387
388 static void
389 tp_call_stream_engine_handle_channel (EmpathyTpCall *call)
390 {
391   EmpathyTpCallPriv *priv = GET_PRIV (call);
392   gchar *channel_type;
393   gchar *object_path;
394   guint handle_type;
395   guint handle;
396   TpProxy *connection;
397
398   DEBUG ("Revving up the stream engine");
399
400   priv->stream_engine = g_object_new (TP_TYPE_PROXY,
401       "bus-name", STREAM_ENGINE_BUS_NAME,
402       "dbus-connection", tp_get_bus (),
403       "object-path", STREAM_ENGINE_OBJECT_PATH,
404        NULL);
405   tp_proxy_add_interface_by_id (priv->stream_engine,
406       EMP_IFACE_QUARK_STREAM_ENGINE);
407   tp_proxy_add_interface_by_id (priv->stream_engine,
408       EMP_IFACE_QUARK_CHANNEL_HANDLER);
409
410   g_signal_connect (priv->stream_engine, "invalidated",
411       G_CALLBACK (tp_call_stream_engine_invalidated_cb),
412       call);
413   
414   /* FIXME: dbus daemon should be unique */
415   priv->dbus_daemon = tp_dbus_daemon_new (tp_get_bus ());
416   tp_dbus_daemon_watch_name_owner (priv->dbus_daemon, STREAM_ENGINE_BUS_NAME,
417       tp_call_stream_engine_watch_name_owner_cb,
418       call, NULL);
419
420   g_object_get (priv->channel,
421       "connection", &connection,
422       "channel-type", &channel_type,
423       "object-path", &object_path,
424       "handle_type", &handle_type,
425       "handle", &handle,
426       NULL);
427
428   emp_cli_channel_handler_call_handle_channel (priv->stream_engine, -1,
429         connection->bus_name,
430         connection->object_path,
431         channel_type, object_path, handle_type, handle,
432         tp_call_async_cb, "calling handle channel", NULL,
433         G_OBJECT (call));
434
435   g_object_unref (connection);
436   g_free (channel_type);
437   g_free (object_path);
438 }
439
440 static GObject *
441 tp_call_constructor (GType type,
442                      guint n_construct_params,
443                      GObjectConstructParam *construct_params)
444 {
445   GObject *object;
446   EmpathyTpCall *call;
447   EmpathyTpCallPriv *priv;
448
449   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
450       n_construct_params, construct_params);
451
452   call = EMPATHY_TP_CALL (object);
453   priv = GET_PRIV (call);
454
455   /* Setup streamed media channel */
456   g_signal_connect (priv->channel, "invalidated",
457       G_CALLBACK (tp_call_channel_invalidated_cb), call);
458   tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
459       tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
460   tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
461       tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
462   tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
463       tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
464   tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
465       tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
466   tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
467       tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
468
469   /* Setup group interface */
470   priv->group = empathy_tp_group_new (priv->channel);
471
472   g_signal_connect (priv->group, "member-added",
473       G_CALLBACK (tp_call_member_added_cb), call);
474
475   /* Start stream engine */
476   tp_call_stream_engine_handle_channel (call);
477
478   return object;
479 }
480
481 static void 
482 tp_call_finalize (GObject *object)
483 {
484   EmpathyTpCallPriv *priv = GET_PRIV (object);
485
486   DEBUG ("Finalizing: %p", object);
487
488   g_slice_free (EmpathyTpCallStream, priv->audio);
489   g_slice_free (EmpathyTpCallStream, priv->video);
490   g_object_unref (priv->group);
491
492   if (priv->channel != NULL)
493     {
494       empathy_tp_call_close (EMPATHY_TP_CALL (object));
495     }
496
497   if (priv->stream_engine != NULL)
498     {
499       g_signal_handlers_disconnect_by_func (priv->stream_engine,
500           tp_call_stream_engine_invalidated_cb, object);
501       g_object_unref (priv->stream_engine);
502     }
503
504   if (priv->contact != NULL)
505       g_object_unref (priv->contact);
506
507   if (priv->dbus_daemon != NULL)
508     {
509       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus_daemon,
510           STREAM_ENGINE_BUS_NAME,
511           tp_call_stream_engine_watch_name_owner_cb,
512           object);
513       g_object_unref (priv->dbus_daemon);
514     }
515
516   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
517 }
518
519 static void 
520 tp_call_set_property (GObject *object,
521                       guint prop_id,
522                       const GValue *value,
523                       GParamSpec *pspec)
524 {
525   EmpathyTpCallPriv *priv = GET_PRIV (object);
526
527   switch (prop_id)
528     {
529     case PROP_CHANNEL:
530       priv->channel = g_value_dup_object (value);
531       break;
532     default:
533       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
534       break;
535   }
536 }
537
538 static void
539 tp_call_get_property (GObject *object,
540                       guint prop_id,
541                       GValue *value,
542                       GParamSpec *pspec)
543 {
544   EmpathyTpCallPriv *priv = GET_PRIV (object);
545
546   switch (prop_id)
547     {
548     case PROP_CHANNEL:
549       g_value_set_object (value, priv->channel);
550       break;
551     case PROP_CONTACT:
552       g_value_set_object (value, priv->contact);
553       break;
554     case PROP_IS_INCOMING:
555       g_value_set_boolean (value, priv->is_incoming);
556       break;
557     case PROP_STATUS:
558       g_value_set_uint (value, priv->status);
559       break;
560     case PROP_AUDIO_STREAM:
561       g_value_set_pointer (value, priv->audio);
562       break;
563     case PROP_VIDEO_STREAM:
564       g_value_set_pointer (value, priv->video);
565       break;
566     default:
567       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
568       break;
569   }
570 }
571
572 static void
573 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
574 {
575   GObjectClass *object_class = G_OBJECT_CLASS (klass);
576
577   object_class->constructor = tp_call_constructor;
578   object_class->finalize = tp_call_finalize;
579   object_class->set_property = tp_call_set_property;
580   object_class->get_property = tp_call_get_property;
581
582   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
583
584   g_object_class_install_property (object_class, PROP_CHANNEL,
585       g_param_spec_object ("channel", "channel", "channel",
586       TP_TYPE_CHANNEL,
587       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
588       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
589   g_object_class_install_property (object_class, PROP_CONTACT,
590       g_param_spec_object ("contact", "Call contact", "Call contact",
591       EMPATHY_TYPE_CONTACT,
592       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
593   g_object_class_install_property (object_class, PROP_IS_INCOMING,
594       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
595       "Is media stream incoming", FALSE, G_PARAM_READABLE |
596       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
597   g_object_class_install_property (object_class, PROP_STATUS,
598       g_param_spec_uint ("status", "Call status",
599       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
600       G_PARAM_STATIC_BLURB));
601   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
602       g_param_spec_pointer ("audio-stream", "Audio stream data",
603       "Audio stream data",
604       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
605   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
606       g_param_spec_pointer ("video-stream", "Video stream data",
607       "Video stream data",
608       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
609 }
610
611 static void
612 empathy_tp_call_init (EmpathyTpCall *call)
613 {
614   EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
615     EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
616
617   call->priv = priv;
618   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
619   priv->contact = NULL;
620   priv->stream_engine_running = FALSE;
621   priv->audio = g_slice_new0 (EmpathyTpCallStream);
622   priv->video = g_slice_new0 (EmpathyTpCallStream);
623   priv->audio->exists = FALSE;
624   priv->video->exists = FALSE;
625 }
626
627 EmpathyTpCall *
628 empathy_tp_call_new (TpChannel *channel)
629 {
630   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
631
632   return g_object_new (EMPATHY_TYPE_TP_CALL,
633       "channel", channel,
634       NULL);
635 }
636
637 void
638 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
639 {
640   EmpathyTpCallPriv *priv = GET_PRIV (call);
641   EmpathyContact *self_contact;
642
643   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
644   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
645
646   DEBUG ("Accepting incoming call");
647
648   self_contact = empathy_tp_group_get_self_contact (priv->group);
649   empathy_tp_group_add_member (priv->group, self_contact, NULL);
650   g_object_unref (self_contact);
651 }
652
653 void
654 empathy_tp_call_close (EmpathyTpCall *call)
655 {
656   EmpathyTpCallPriv *priv = GET_PRIV (call);
657
658   g_signal_handlers_disconnect_by_func (priv->channel,
659     tp_call_channel_invalidated_cb, call);
660
661   tp_call_close_channel (call);
662
663   g_object_unref (priv->channel);
664   priv->channel = NULL;
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
839 gboolean
840 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
841 {
842   EmpathyTpCallPriv *priv = GET_PRIV (call);
843
844   g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
845
846   return tp_proxy_has_interface_by_id (priv->channel,
847       TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
848 }
849