]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
4c3779f285e8bf66267485aa304eba604c8e2e99
[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_request_streams (EmpathyTpCall *call)
271 {
272   EmpathyTpCallPriv *priv = GET_PRIV (call);
273
274   tp_call_request_streams_for_capabilities (call,
275       empathy_contact_get_capabilities (priv->contact));
276 }
277
278 static void
279 tp_call_member_added_cb (EmpathyTpGroup *group,
280                          EmpathyContact *contact,
281                          EmpathyContact *actor,
282                          guint reason,
283                          const gchar *message,
284                          EmpathyTpCall *call)
285 {
286   EmpathyTpCallPriv *priv = GET_PRIV (call);
287
288   if (!priv->contact && !empathy_contact_is_user (contact))
289     {
290       priv->contact = g_object_ref (contact);
291       priv->is_incoming = TRUE;
292       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
293       tp_call_request_streams (call);
294       g_object_notify (G_OBJECT (call), "is-incoming");
295       g_object_notify (G_OBJECT (call), "contact"); 
296       g_object_notify (G_OBJECT (call), "status");      
297     }
298
299   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
300       ((priv->is_incoming && contact != priv->contact) ||
301        (!priv->is_incoming && contact == priv->contact)))
302     {
303       priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
304       g_object_notify (G_OBJECT (call), "status");
305     }
306 }
307
308 static void
309 tp_call_remote_pending_cb (EmpathyTpGroup *group,
310                            EmpathyContact *contact,
311                            EmpathyContact *actor,
312                            guint reason,
313                            const gchar *message,
314                            EmpathyTpCall *call)
315 {
316   EmpathyTpCallPriv *priv = GET_PRIV (call);
317
318   if (!priv->contact && !empathy_contact_is_user (contact))
319     {
320       priv->contact = g_object_ref (contact);
321       priv->is_incoming = FALSE;
322       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
323       tp_call_request_streams (call);
324       g_object_notify (G_OBJECT (call), "is-incoming");
325       g_object_notify (G_OBJECT (call), "contact"); 
326       g_object_notify (G_OBJECT (call), "status");      
327     }
328 }
329
330 static void
331 tp_call_channel_invalidated_cb (TpChannel     *channel,
332                                 GQuark         domain,
333                                 gint           code,
334                                 gchar         *message,
335                                 EmpathyTpCall *call)
336 {
337   EmpathyTpCallPriv *priv = GET_PRIV (call);
338
339   DEBUG ("Channel invalidated: %s", message);
340   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
341   g_object_notify (G_OBJECT (call), "status");
342 }
343
344 static void
345 tp_call_async_cb (TpProxy *proxy,
346                   const GError *error,
347                   gpointer user_data,
348                   GObject *call)
349 {
350   if (error)
351     {
352       DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
353     }
354 }
355
356 static void
357 tp_call_close_channel (EmpathyTpCall *call)
358 {
359   EmpathyTpCallPriv *priv = GET_PRIV (call);
360
361   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
362       return;
363
364   DEBUG ("Closing channel");
365
366   tp_cli_channel_call_close (priv->channel, -1,
367       NULL, NULL, NULL, NULL);
368
369   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
370   g_object_notify (G_OBJECT (call), "status");
371 }
372
373 static void
374 tp_call_stream_engine_invalidated_cb (TpProxy       *stream_engine,
375                                       GQuark         domain,
376                                       gint           code,
377                                       gchar         *message,
378                                       EmpathyTpCall *call)
379 {
380   DEBUG ("Stream engine proxy invalidated: %s", message);
381   tp_call_close_channel (call);
382 }
383
384 static void
385 tp_call_stream_engine_watch_name_owner_cb (TpDBusDaemon *daemon,
386                                            const gchar *name,
387                                            const gchar *new_owner,
388                                            gpointer call)
389 {
390   EmpathyTpCallPriv *priv = GET_PRIV (call);
391
392   /* G_STR_EMPTY(new_owner) means either stream-engine has not started yet or
393    * has crashed. We want to close the channel if stream-engine has crashed.
394    * */
395   DEBUG ("Watch SE: name='%s' SE running='%s' new_owner='%s'",
396       name, priv->stream_engine_running ? "yes" : "no",
397       new_owner ? new_owner : "none");
398   if (priv->stream_engine_running && G_STR_EMPTY (new_owner))
399     {
400       DEBUG ("Stream engine falled off the bus");
401       tp_call_close_channel (call);
402       return;
403     }
404
405   priv->stream_engine_running = !G_STR_EMPTY (new_owner);
406 }
407
408 static void
409 tp_call_stream_engine_handle_channel (EmpathyTpCall *call)
410 {
411   EmpathyTpCallPriv *priv = GET_PRIV (call);
412   gchar *channel_type;
413   gchar *object_path;
414   guint handle_type;
415   guint handle;
416   TpProxy *connection;
417
418   DEBUG ("Revving up the stream engine");
419
420   priv->stream_engine = g_object_new (TP_TYPE_PROXY,
421       "bus-name", STREAM_ENGINE_BUS_NAME,
422       "dbus-connection", tp_get_bus (),
423       "object-path", STREAM_ENGINE_OBJECT_PATH,
424        NULL);
425   tp_proxy_add_interface_by_id (priv->stream_engine,
426       EMP_IFACE_QUARK_STREAM_ENGINE);
427   tp_proxy_add_interface_by_id (priv->stream_engine,
428       EMP_IFACE_QUARK_CHANNEL_HANDLER);
429
430   g_signal_connect (priv->stream_engine, "invalidated",
431       G_CALLBACK (tp_call_stream_engine_invalidated_cb),
432       call);
433   
434   /* FIXME: dbus daemon should be unique */
435   priv->dbus_daemon = tp_dbus_daemon_new (tp_get_bus ());
436   tp_dbus_daemon_watch_name_owner (priv->dbus_daemon, STREAM_ENGINE_BUS_NAME,
437       tp_call_stream_engine_watch_name_owner_cb,
438       call, NULL);
439
440   g_object_get (priv->channel,
441       "connection", &connection,
442       "channel-type", &channel_type,
443       "object-path", &object_path,
444       "handle_type", &handle_type,
445       "handle", &handle,
446       NULL);
447
448   emp_cli_channel_handler_call_handle_channel (priv->stream_engine, -1,
449         connection->bus_name,
450         connection->object_path,
451         channel_type, object_path, handle_type, handle,
452         tp_call_async_cb, "calling handle channel", NULL,
453         G_OBJECT (call));
454
455   g_object_unref (connection);
456   g_free (channel_type);
457   g_free (object_path);
458 }
459
460 static GObject *
461 tp_call_constructor (GType type,
462                      guint n_construct_params,
463                      GObjectConstructParam *construct_params)
464 {
465   GObject *object;
466   EmpathyTpCall *call;
467   EmpathyTpCallPriv *priv;
468
469   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
470       n_construct_params, construct_params);
471
472   call = EMPATHY_TP_CALL (object);
473   priv = GET_PRIV (call);
474
475   /* Setup streamed media channel */
476   g_signal_connect (priv->channel, "invalidated",
477       G_CALLBACK (tp_call_channel_invalidated_cb), call);
478   tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
479       tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
480   tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
481       tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
482   tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
483       tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
484   tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
485       tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
486   tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
487       tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
488
489   /* Setup group interface */
490   priv->group = empathy_tp_group_new (priv->channel);
491
492   g_signal_connect (priv->group, "member-added",
493       G_CALLBACK (tp_call_member_added_cb), call);
494   g_signal_connect (priv->group, "remote-pending",
495       G_CALLBACK (tp_call_remote_pending_cb), call);
496
497   /* Start stream engine */
498   tp_call_stream_engine_handle_channel (call);
499
500   return object;
501 }
502
503 static void 
504 tp_call_finalize (GObject *object)
505 {
506   EmpathyTpCallPriv *priv = GET_PRIV (object);
507
508   DEBUG ("Finalizing: %p", object);
509
510   g_slice_free (EmpathyTpCallStream, priv->audio);
511   g_slice_free (EmpathyTpCallStream, priv->video);
512   g_object_unref (priv->group);
513
514   if (priv->channel != NULL)
515     {
516       g_signal_handlers_disconnect_by_func (priv->channel,
517           tp_call_channel_invalidated_cb, object);
518       tp_call_close_channel (EMPATHY_TP_CALL (object));
519       g_object_unref (priv->channel);
520     }
521
522   if (priv->stream_engine != NULL)
523     {
524       g_signal_handlers_disconnect_by_func (priv->stream_engine,
525           tp_call_stream_engine_invalidated_cb, object);
526       g_object_unref (priv->stream_engine);
527     }
528
529   if (priv->contact != NULL)
530       g_object_unref (priv->contact);
531
532   if (priv->dbus_daemon != NULL)
533     {
534       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus_daemon,
535           STREAM_ENGINE_BUS_NAME,
536           tp_call_stream_engine_watch_name_owner_cb,
537           object);
538       g_object_unref (priv->dbus_daemon);
539     }
540
541   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
542 }
543
544 static void 
545 tp_call_set_property (GObject *object,
546                       guint prop_id,
547                       const GValue *value,
548                       GParamSpec *pspec)
549 {
550   EmpathyTpCallPriv *priv = GET_PRIV (object);
551
552   switch (prop_id)
553     {
554     case PROP_CHANNEL:
555       priv->channel = g_value_dup_object (value);
556       break;
557     default:
558       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
559       break;
560   }
561 }
562
563 static void
564 tp_call_get_property (GObject *object,
565                       guint prop_id,
566                       GValue *value,
567                       GParamSpec *pspec)
568 {
569   EmpathyTpCallPriv *priv = GET_PRIV (object);
570
571   switch (prop_id)
572     {
573     case PROP_CHANNEL:
574       g_value_set_object (value, priv->channel);
575       break;
576     case PROP_CONTACT:
577       g_value_set_object (value, priv->contact);
578       break;
579     case PROP_IS_INCOMING:
580       g_value_set_boolean (value, priv->is_incoming);
581       break;
582     case PROP_STATUS:
583       g_value_set_uint (value, priv->status);
584       break;
585     case PROP_AUDIO_STREAM:
586       g_value_set_pointer (value, priv->audio);
587       break;
588     case PROP_VIDEO_STREAM:
589       g_value_set_pointer (value, priv->video);
590       break;
591     default:
592       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
593       break;
594   }
595 }
596
597 static void
598 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
599 {
600   GObjectClass *object_class = G_OBJECT_CLASS (klass);
601
602   emp_cli_init ();
603
604   object_class->constructor = tp_call_constructor;
605   object_class->finalize = tp_call_finalize;
606   object_class->set_property = tp_call_set_property;
607   object_class->get_property = tp_call_get_property;
608
609   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
610
611   g_object_class_install_property (object_class, PROP_CHANNEL,
612       g_param_spec_object ("channel", "channel", "channel",
613       TP_TYPE_CHANNEL,
614       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
615       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
616   g_object_class_install_property (object_class, PROP_CONTACT,
617       g_param_spec_object ("contact", "Call contact", "Call contact",
618       EMPATHY_TYPE_CONTACT,
619       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
620   g_object_class_install_property (object_class, PROP_IS_INCOMING,
621       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
622       "Is media stream incoming", FALSE, G_PARAM_READABLE |
623       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
624   g_object_class_install_property (object_class, PROP_STATUS,
625       g_param_spec_uint ("status", "Call status",
626       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
627       G_PARAM_STATIC_BLURB));
628   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
629       g_param_spec_pointer ("audio-stream", "Audio stream data",
630       "Audio stream data",
631       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
632   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
633       g_param_spec_pointer ("video-stream", "Video stream data",
634       "Video stream data",
635       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
636 }
637
638 static void
639 empathy_tp_call_init (EmpathyTpCall *call)
640 {
641   EmpathyTpCallPriv *priv = GET_PRIV (call);
642
643   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
644   priv->contact = NULL;
645   priv->stream_engine_running = FALSE;
646   priv->audio = g_slice_new0 (EmpathyTpCallStream);
647   priv->video = g_slice_new0 (EmpathyTpCallStream);
648   priv->audio->exists = FALSE;
649   priv->video->exists = FALSE;
650 }
651
652 EmpathyTpCall *
653 empathy_tp_call_new (TpChannel *channel)
654 {
655   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
656
657   return g_object_new (EMPATHY_TYPE_TP_CALL,
658       "channel", channel,
659       NULL);
660 }
661
662 void
663 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
664 {
665   EmpathyTpCallPriv *priv = GET_PRIV (call);
666   EmpathyContact *self_contact;
667
668   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
669   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
670
671   DEBUG ("Accepting incoming call");
672
673   self_contact = empathy_tp_group_get_self_contact (priv->group);
674   empathy_tp_group_add_member (priv->group, self_contact, NULL);
675   g_object_unref (self_contact);
676 }
677
678 void
679 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
680                                                 gboolean is_sending)
681 {
682   EmpathyTpCallPriv *priv = GET_PRIV (call);
683   guint new_direction;
684
685   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
686   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
687
688   DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
689
690   if (!priv->video->exists)
691     {
692       if (is_sending)
693           tp_call_request_streams_for_capabilities (call,
694               EMPATHY_CAPABILITIES_VIDEO);
695       return;
696     }
697
698   if (is_sending)
699       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
700   else
701       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
702
703   tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
704       -1, priv->video->id, new_direction,
705       (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
706       tp_call_async_cb, NULL, NULL, G_OBJECT (call));
707 }
708
709 void
710 empathy_tp_call_add_preview_video (EmpathyTpCall *call,
711                                    guint preview_video_socket_id)
712 {
713   EmpathyTpCallPriv *priv = GET_PRIV (call);
714
715   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
716
717   DEBUG ("Adding preview video");
718
719   emp_cli_stream_engine_call_add_preview_window (priv->stream_engine, -1,
720       preview_video_socket_id,
721       tp_call_async_cb,
722       "adding preview window", NULL,
723       G_OBJECT (call));
724 }
725
726 void
727 empathy_tp_call_remove_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 ("Removing preview video");
735
736   emp_cli_stream_engine_call_remove_preview_window (priv->stream_engine, -1,
737       preview_video_socket_id,
738       tp_call_async_cb,
739       "removing preview window", NULL,
740       G_OBJECT (call));
741 }
742
743 void
744 empathy_tp_call_add_output_video (EmpathyTpCall *call,
745                                   guint output_video_socket_id)
746 {
747   EmpathyTpCallPriv *priv = GET_PRIV (call);
748
749   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
750
751   DEBUG ("Adding output video - socket: %d", output_video_socket_id);
752
753   emp_cli_stream_engine_call_set_output_window (priv->stream_engine, -1,
754       TP_PROXY (priv->channel)->object_path,
755       priv->video->id, output_video_socket_id,
756       tp_call_async_cb,
757       "setting output window", NULL,
758       G_OBJECT (call));
759 }
760
761 void
762 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
763                                    guint volume)
764 {
765   EmpathyTpCallPriv *priv = GET_PRIV (call);
766
767   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
768   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
769
770   DEBUG ("Setting output volume: %d", volume);
771
772   emp_cli_stream_engine_call_set_output_volume (priv->stream_engine, -1,
773       TP_PROXY (priv->channel)->object_path,
774       priv->audio->id, volume,
775       tp_call_async_cb,
776       "setting output volume", NULL,
777       G_OBJECT (call));
778 }
779
780 void
781 empathy_tp_call_mute_output (EmpathyTpCall *call,
782                              gboolean is_muted)
783 {
784   EmpathyTpCallPriv *priv = GET_PRIV (call);
785
786   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
787
788   DEBUG ("Setting output mute: %d", is_muted);
789
790   emp_cli_stream_engine_call_mute_output (priv->stream_engine, -1,
791       TP_PROXY (priv->channel)->object_path,
792       priv->audio->id, is_muted,
793       tp_call_async_cb,
794       "muting output", NULL,
795       G_OBJECT (call));
796 }
797
798 void
799 empathy_tp_call_mute_input (EmpathyTpCall *call,
800                             gboolean is_muted)
801 {
802   EmpathyTpCallPriv *priv = GET_PRIV (call);
803
804   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
805
806   DEBUG ("Setting input mute: %d", is_muted);
807
808   emp_cli_stream_engine_call_mute_input (priv->stream_engine, -1,
809       TP_PROXY (priv->channel)->object_path,
810       priv->audio->id, is_muted,
811       tp_call_async_cb,
812       "muting input", NULL,
813       G_OBJECT (call));
814 }
815