]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Merge call branch from Elliot Fairweather with cleanups from Xavier Claessens.
[empathy.git] / libempathy / empathy-tp-call.c
1 /*
2  *  Copyright (C) 2007 Elliot Fairweather
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Lesser General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2.1 of the License, or (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public
15  *  License along with this library; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  *  Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
19  */
20
21 #include <string.h>
22 #include <dbus/dbus-glib.h>
23
24 #include <libtelepathy/tp-chan-type-streamed-media-gen.h>
25 #include <libtelepathy/tp-connmgr.h>
26 #include <libtelepathy/tp-helpers.h>
27
28 #include <libmissioncontrol/mc-account.h>
29
30 #include <libempathy/empathy-contact-factory.h>
31 #include <libempathy/empathy-debug.h>
32 #include <libempathy/empathy-tp-group.h>
33 #include <libempathy/empathy-utils.h>
34
35 #include "tp-stream-engine-gen.h"
36
37 #include "empathy-tp-call.h"
38
39 #define DEBUG_DOMAIN "TpCall"
40
41 #define GET_PRIV(object) (G_TYPE_INSTANCE_GET_PRIVATE \
42     ((object), EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv))
43
44 #define STREAM_ENGINE_BUS_NAME "org.freedesktop.Telepathy.StreamEngine"
45 #define STREAM_ENGINE_OBJECT_PATH "/org/freedesktop/Telepathy/StreamEngine"
46 #define STREAM_ENGINE_INTERFACE "org.freedesktop.Telepathy.StreamEngine"
47 #define CHANNEL_HANDLER_INTERFACE "org.freedesktop.Telepathy.ChannelHandler"
48
49 typedef struct _EmpathyTpCallPriv EmpathyTpCallPriv;
50
51 struct _EmpathyTpCallPriv
52 {
53   TpConn *connection;
54   TpChan *channel;
55   EmpathyTpGroup *group;
56   EmpathyContact *contact;
57   gboolean is_incoming;
58   guint status;
59
60   EmpathyTpCallStream *audio;
61   EmpathyTpCallStream *video;
62 };
63
64 enum
65 {
66   STATUS_CHANGED_SIGNAL,
67   RECEIVING_VIDEO_SIGNAL,
68   SENDING_VIDEO_SIGNAL,
69   LAST_SIGNAL
70 };
71
72 enum
73 {
74   PROP_0,
75   PROP_CONNECTION,
76   PROP_CHANNEL,
77   PROP_CONTACT,
78   PROP_IS_INCOMING,
79   PROP_STATUS,
80   PROP_AUDIO_STREAM,
81   PROP_VIDEO_STREAM
82 };
83
84 static guint signals[LAST_SIGNAL];
85
86 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
87
88 static void
89 tp_call_stream_state_changed_cb (DBusGProxy *channel,
90                                  guint stream_id,
91                                  guint stream_state,
92                                  EmpathyTpCall *call)
93 {
94   EmpathyTpCallPriv *priv = GET_PRIV (call);
95
96   empathy_debug (DEBUG_DOMAIN,
97       "Stream state changed - stream id: %d, state state: %d",
98       stream_id, stream_state);
99
100   if (stream_id == priv->audio->id)
101     {
102       priv->audio->state = stream_state;
103     }
104   else if (stream_id == priv->video->id)
105     {
106       priv->video->state = stream_state;
107     }
108
109   g_signal_emit_by_name (call, "status-changed");
110 }
111
112 static void
113 tp_call_identify_streams (EmpathyTpCall *call)
114 {
115   EmpathyTpCallPriv *priv = GET_PRIV (call);
116   GPtrArray *stream_infos;
117   DBusGProxy *streamed_iface;
118   GError *error = NULL;
119   guint i;
120
121   empathy_debug (DEBUG_DOMAIN, "Identifying audio/video streams");
122
123   streamed_iface = tp_chan_get_interface (priv->channel,
124       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
125
126   if (!tp_chan_type_streamed_media_list_streams (streamed_iface, &stream_infos,
127         &error))
128     {
129       empathy_debug (DEBUG_DOMAIN, "Couldn't list audio/video streams: %s",
130           error->message);
131       g_clear_error (&error);
132     }
133
134   for (i = 0; i < stream_infos->len; i++)
135     {
136       GValueArray *values;
137       guint stream_id;
138       guint stream_handle;
139       guint stream_type;
140       guint stream_state;
141       guint stream_direction;
142
143       values = g_ptr_array_index (stream_infos, i);
144       stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
145       stream_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
146       stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
147       stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
148       stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
149
150       switch (stream_type)
151         {
152         case TP_MEDIA_STREAM_TYPE_AUDIO:
153           empathy_debug (DEBUG_DOMAIN,
154               "Audio stream - id: %d, state: %d, direction: %d",
155               stream_id, stream_state, stream_direction);
156           priv->audio->exists = TRUE;
157           priv->audio->id = stream_id;
158           priv->audio->state = stream_state;
159           priv->audio->direction = stream_direction;
160           break;
161         case TP_MEDIA_STREAM_TYPE_VIDEO:
162           empathy_debug (DEBUG_DOMAIN,
163               "Video stream - id: %d, state: %d, direction: %d",
164               stream_id, stream_state, stream_direction);
165           priv->video->exists = TRUE;
166           priv->video->id = stream_id;
167           priv->video->state = stream_state;
168           priv->video->direction = stream_direction;
169           break;
170         default:
171           empathy_debug (DEBUG_DOMAIN, "Unknown stream type: %d",
172               stream_type);
173         }
174
175       g_value_array_free (values);
176     }
177 }
178
179 static void
180 tp_call_stream_added_cb (DBusGProxy *channel,
181                          guint stream_id,
182                          guint contact_handle,
183                          guint stream_type,
184                          EmpathyTpCall *call)
185 {
186   empathy_debug (DEBUG_DOMAIN,
187       "Stream added - stream id: %d, contact handle: %d, stream type: %d",
188       stream_id, contact_handle, stream_type);
189
190   tp_call_identify_streams (call);
191 }
192
193
194 static void
195 tp_call_stream_removed_cb (DBusGProxy *channel,
196                            guint stream_id,
197                            EmpathyTpCall *call)
198 {
199   EmpathyTpCallPriv *priv = GET_PRIV (call);
200
201   empathy_debug (DEBUG_DOMAIN, "Stream removed - stream id: %d", stream_id);
202
203   if (stream_id == priv->audio->id)
204     {
205       priv->audio->exists = FALSE;
206     }
207   else if (stream_id == priv->video->id)
208     {
209       priv->video->exists = FALSE;
210     }
211 }
212
213 static void
214 tp_call_channel_closed_cb (TpChan *channel,
215                            EmpathyTpCall *call)
216 {
217   EmpathyTpCallPriv *priv = GET_PRIV (call);
218   DBusGProxy *streamed_iface;
219   DBusGProxy *group_iface;
220
221   empathy_debug (DEBUG_DOMAIN, "Channel closed");
222
223   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
224   g_signal_emit_by_name (call, "status-changed");
225
226   streamed_iface = tp_chan_get_interface (priv->channel,
227       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
228   group_iface = tp_chan_get_interface (priv->channel,
229       TELEPATHY_CHAN_IFACE_GROUP_QUARK);
230
231   dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->channel), "Closed",
232       G_CALLBACK (tp_call_channel_closed_cb), (gpointer) call);
233   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamStateChanged",
234       G_CALLBACK (tp_call_stream_state_changed_cb), (gpointer) call);
235   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamAdded",
236       G_CALLBACK (tp_call_stream_added_cb), (gpointer) call);
237   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamRemoved",
238       G_CALLBACK (tp_call_stream_removed_cb), (gpointer) call);
239 }
240
241 static void
242 tp_call_stream_direction_changed_cb (DBusGProxy *channel,
243                                      guint stream_id,
244                                      guint stream_direction,
245                                      guint flags,
246                                      EmpathyTpCall *call)
247 {
248   EmpathyTpCallPriv *priv = GET_PRIV (call);
249
250   empathy_debug (DEBUG_DOMAIN,
251       "Stream direction changed - stream: %d, direction: %d",
252       stream_id, stream_direction);
253
254   if (stream_id == priv->audio->id)
255     {
256       priv->audio->direction = stream_direction;
257     }
258   else if (stream_id == priv->video->id)
259     {
260       priv->video->direction = stream_direction;
261
262       if (stream_direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE)
263         {
264           empathy_debug (DEBUG_DOMAIN, "RECEIVING");
265           g_signal_emit_by_name (call, "receiving-video", TRUE);
266         }
267       else
268         {
269           empathy_debug (DEBUG_DOMAIN, "NOT RECEIVING");
270           g_signal_emit_by_name (call, "receiving-video", FALSE);
271         }
272
273       if (stream_direction & TP_MEDIA_STREAM_DIRECTION_SEND)
274         {
275           empathy_debug (DEBUG_DOMAIN, "SENDING");
276           g_signal_emit_by_name (call, "sending-video", TRUE);
277         }
278       else
279         {
280           empathy_debug (DEBUG_DOMAIN, "NOT SENDING");
281           g_signal_emit_by_name (call, "sending-video", FALSE);
282         }
283     }
284 }
285
286 static void
287 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
288                                           EmpathyCapabilities capabilities)
289 {
290   EmpathyTpCallPriv *priv = GET_PRIV (call);
291   DBusGProxy *streamed_iface;
292   GArray *stream_types;
293   guint handle;
294   guint stream_type;
295   GError *error = NULL;
296
297   empathy_debug (DEBUG_DOMAIN, "Requesting new stream for capabilities %d",
298       capabilities);
299
300   streamed_iface = tp_chan_get_interface (priv->channel,
301       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
302   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
303   handle = empathy_contact_get_handle (priv->contact);
304
305   if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
306     {
307       stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
308       g_array_append_val (stream_types, stream_type);
309     }
310   if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
311     {
312       stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
313       g_array_append_val (stream_types, stream_type);
314     }
315
316   if (!tp_chan_type_streamed_media_request_streams (streamed_iface, handle,
317         stream_types, NULL, &error))
318     {
319       empathy_debug (DEBUG_DOMAIN, "Couldn't request new stream: %s",
320           error->message);
321       g_clear_error (&error);
322     }
323
324   g_array_free (stream_types, TRUE);
325 }
326
327 static void
328 tp_call_request_streams (EmpathyTpCall *call)
329 {
330   EmpathyTpCallPriv *priv = GET_PRIV (call);
331   EmpathyCapabilities capabilities;
332   DBusGProxy *capabilities_iface;
333
334   empathy_debug (DEBUG_DOMAIN,
335       "Requesting appropriate audio/video streams from contact");
336
337   capabilities = empathy_contact_get_capabilities (priv->contact);
338
339   /* FIXME: SIP don't have capabilities interface but we know it supports
340    *        only audio and not video. */
341   capabilities_iface = tp_conn_get_interface (priv->connection,
342       TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES);
343   if (!capabilities_iface)
344     {
345       capabilities = EMPATHY_CAPABILITIES_AUDIO;
346     }
347
348   tp_call_request_streams_for_capabilities (call, capabilities);
349 }
350
351 static void
352 tp_call_is_ready (EmpathyTpCall *call)
353 {
354   EmpathyTpCallPriv *priv = GET_PRIV (call);
355   EmpathyContact *self_contact;
356   GList *members;
357   GList *local_pendings;
358   GList *remote_pendings;
359
360   if (priv->status > EMPATHY_TP_CALL_STATUS_READYING)
361     return;
362
363   members = empathy_tp_group_get_members (priv->group);
364   if (!members)
365     return;
366
367   self_contact = empathy_tp_group_get_self_contact (priv->group);
368   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
369   remote_pendings = empathy_tp_group_get_remote_pendings (priv->group);
370
371   if (local_pendings &&
372       empathy_contact_equal (EMPATHY_CONTACT (((EmpathyPendingInfo *)
373             local_pendings->data)->member), self_contact))
374     {
375       empathy_debug (DEBUG_DOMAIN,
376           "Incoming call is ready - %p",
377           ((EmpathyPendingInfo *) local_pendings->data)->member);
378       priv->is_incoming = TRUE;
379       priv->contact = g_object_ref (members->data);
380     }
381   else if (remote_pendings &&
382       empathy_contact_equal (EMPATHY_CONTACT (members->data), self_contact))
383     {
384       empathy_debug (DEBUG_DOMAIN,
385           "Outgoing call is ready - %p", remote_pendings->data);
386       priv->is_incoming = FALSE;
387       priv->contact = g_object_ref (remote_pendings->data);
388       tp_call_request_streams (call);
389     }
390
391   g_object_unref (self_contact);
392   g_list_foreach (members, (GFunc) g_object_unref, NULL);
393   g_list_free (members);
394   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
395   g_list_free (local_pendings);
396   g_list_foreach (remote_pendings, (GFunc) g_object_unref, NULL);
397   g_list_free (remote_pendings);
398
399   if (priv->contact)
400     {
401       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
402       g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
403     }
404 }
405
406 static void
407 tp_call_member_added_cb (EmpathyTpGroup *group,
408                          EmpathyContact *contact,
409                          EmpathyContact *actor,
410                          guint reason,
411                          const gchar *message,
412                          EmpathyTpCall *call)
413 {
414   EmpathyTpCallPriv *priv = GET_PRIV (call);
415
416   empathy_debug (DEBUG_DOMAIN, "New member added callback %p", contact);
417   tp_call_is_ready (call);
418
419   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING)
420     {
421       if ((priv->is_incoming &&
422             !empathy_contact_equal (contact, priv->contact))
423           || (!priv->is_incoming &&
424             empathy_contact_equal (contact, priv->contact)))
425         {
426           priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
427           g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
428         }
429     }
430 }
431
432 static void
433 tp_call_local_pending_cb (EmpathyTpGroup *group,
434                           EmpathyContact *contact,
435                           EmpathyContact *actor,
436                           guint reason,
437                           const gchar *message,
438                           EmpathyTpCall *call)
439 {
440   empathy_debug (DEBUG_DOMAIN, "New local pending added callback %p", contact);
441   tp_call_is_ready (call);
442 }
443
444 static void
445 tp_call_remote_pending_cb (EmpathyTpGroup *group,
446                            EmpathyContact *contact,
447                            EmpathyContact *actor,
448                            guint reason,
449                            const gchar *message,
450                            EmpathyTpCall *call)
451 {
452   empathy_debug (DEBUG_DOMAIN, "New remote pending added callback %p", contact);
453   tp_call_is_ready (call);
454 }
455
456 static void
457 tp_call_start_stream_engine (EmpathyTpCall *call)
458 {
459   EmpathyTpCallPriv *priv = GET_PRIV (call);
460   DBusGProxy *ch_proxy;
461   GError *error = NULL;
462
463   empathy_debug (DEBUG_DOMAIN, "Revving up the stream engine");
464
465   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
466       STREAM_ENGINE_OBJECT_PATH, CHANNEL_HANDLER_INTERFACE);
467
468   if (!org_freedesktop_Telepathy_ChannelHandler_handle_channel (ch_proxy,
469         dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->connection)),
470         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->connection)),
471         priv->channel->type,
472         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
473         priv->channel->handle_type, priv->channel->handle, &error))
474     {
475       empathy_debug (DEBUG_DOMAIN, "Couldn't start stream engine: %s",
476           error->message);
477       g_clear_error (&error);
478     }
479 }
480
481 static GObject *
482 tp_call_constructor (GType type,
483                      guint n_construct_params,
484                      GObjectConstructParam *construct_params)
485 {
486   GObject *object;
487   EmpathyTpCall *call;
488   EmpathyTpCallPriv *priv;
489   DBusGProxy *streamed_iface;
490   MissionControl *mc;
491   McAccount *account;
492
493   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
494       n_construct_params, construct_params);
495
496   call = EMPATHY_TP_CALL (object);
497   priv = GET_PRIV (call);
498
499   dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->channel), "Closed",
500      G_CALLBACK (tp_call_channel_closed_cb), (gpointer) call, NULL);
501
502   streamed_iface = tp_chan_get_interface (priv->channel,
503       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
504   dbus_g_proxy_connect_signal (streamed_iface, "StreamStateChanged",
505       G_CALLBACK (tp_call_stream_state_changed_cb),
506       (gpointer) call, NULL);
507   dbus_g_proxy_connect_signal (streamed_iface, "StreamDirectionChanged",
508       G_CALLBACK (tp_call_stream_direction_changed_cb),
509       (gpointer) call, NULL);
510   dbus_g_proxy_connect_signal (streamed_iface, "StreamAdded",
511       G_CALLBACK (tp_call_stream_added_cb), (gpointer) call, NULL);
512   dbus_g_proxy_connect_signal (streamed_iface, "StreamRemoved",
513       G_CALLBACK (tp_call_stream_removed_cb), (gpointer) call, NULL);
514
515   mc = empathy_mission_control_new ();
516   account = mission_control_get_account_for_connection (mc, priv->connection,
517       NULL);
518   priv->group = empathy_tp_group_new (account, priv->channel);
519
520   g_signal_connect (G_OBJECT (priv->group), "member-added",
521       G_CALLBACK (tp_call_member_added_cb), (gpointer) call);
522   g_signal_connect (G_OBJECT (priv->group), "local-pending",
523       G_CALLBACK (tp_call_local_pending_cb), (gpointer) call);
524   g_signal_connect (G_OBJECT (priv->group), "remote-pending",
525       G_CALLBACK (tp_call_remote_pending_cb), (gpointer) call);
526
527   tp_call_start_stream_engine (call);
528   /* FIXME: unnecessary for outgoing? */
529   tp_call_identify_streams (call);
530
531   return object;
532 }
533
534 static void 
535 tp_call_finalize (GObject *object)
536 {
537   EmpathyTpCallPriv *priv = GET_PRIV (object);
538
539   empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
540
541   g_slice_free (EmpathyTpCallStream, priv->audio);
542   g_slice_free (EmpathyTpCallStream, priv->video);
543   g_object_unref (priv->group);
544   if (priv->contact)
545     {
546       g_object_unref (priv->contact);
547     }
548
549   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
550 }
551
552 static void 
553 tp_call_set_property (GObject *object,
554                       guint prop_id,
555                       const GValue *value,
556                       GParamSpec *pspec)
557 {
558   EmpathyTpCallPriv *priv = GET_PRIV (object);
559
560   switch (prop_id)
561     {
562     case PROP_CONNECTION:
563       priv->connection = g_value_get_object (value);
564       break;
565     case PROP_CHANNEL:
566       priv->channel = g_value_get_object (value);
567       break;
568     case PROP_CONTACT:
569       priv->contact = g_value_get_object (value);
570       break;
571     case PROP_IS_INCOMING:
572       priv->is_incoming = g_value_get_boolean (value);
573       break;
574     case PROP_STATUS:
575       priv->status = g_value_get_uint (value);
576       break;
577     case PROP_AUDIO_STREAM:
578       priv->audio = g_value_get_pointer (value);
579       break;
580     case PROP_VIDEO_STREAM:
581       priv->video = g_value_get_pointer (value);
582       break;
583     default:
584       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
585       break;
586   }
587 }
588
589
590 static void
591 tp_call_get_property (GObject *object,
592                       guint prop_id,
593                       GValue *value,
594                       GParamSpec *pspec)
595 {
596   EmpathyTpCallPriv *priv = GET_PRIV (object);
597
598   switch (prop_id)
599     {
600     case PROP_CONNECTION:
601       g_value_set_object (value, priv->connection);
602       break;
603     case PROP_CHANNEL:
604       g_value_set_object (value, priv->channel);
605       break;
606     case PROP_CONTACT:
607       g_value_set_object (value, priv->contact);
608       break;
609     case PROP_IS_INCOMING:
610       g_value_set_boolean (value, priv->is_incoming);
611       break;
612     case PROP_STATUS:
613       g_value_set_uint (value, priv->status);
614       break;
615     case PROP_AUDIO_STREAM:
616       g_value_set_pointer (value, priv->audio);
617       break;
618     case PROP_VIDEO_STREAM:
619       g_value_set_pointer (value, priv->video);
620       break;
621     default:
622       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
623       break;
624   }
625 }
626
627 static void
628 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
629 {
630   GObjectClass *object_class = G_OBJECT_CLASS (klass);
631
632   object_class->constructor = tp_call_constructor;
633   object_class->finalize = tp_call_finalize;
634   object_class->set_property = tp_call_set_property;
635   object_class->get_property = tp_call_get_property;
636
637   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
638
639   signals[STATUS_CHANGED_SIGNAL] =
640       g_signal_new ("status-changed", G_TYPE_FROM_CLASS (klass),
641       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID,
642       G_TYPE_NONE, 0);
643   signals[RECEIVING_VIDEO_SIGNAL] =
644       g_signal_new ("receiving-video", G_TYPE_FROM_CLASS (klass),
645       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
646       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
647   signals[SENDING_VIDEO_SIGNAL] =
648       g_signal_new ("sending-video", G_TYPE_FROM_CLASS (klass),
649       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
650       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
651
652   g_object_class_install_property (object_class, PROP_CONNECTION,
653       g_param_spec_object ("connection", "connection", "connection",
654       TELEPATHY_CONN_TYPE,
655       G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
656       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
657   g_object_class_install_property (object_class, PROP_CHANNEL,
658       g_param_spec_object ("channel", "channel", "channel",
659       TELEPATHY_CHAN_TYPE,
660       G_PARAM_CONSTRUCT | G_PARAM_READWRITE |
661       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
662   g_object_class_install_property (object_class, PROP_CONTACT,
663       g_param_spec_object ("contact", "Call contact", "Call contact",
664       EMPATHY_TYPE_CONTACT,
665       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
666   g_object_class_install_property (object_class, PROP_IS_INCOMING,
667       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
668       "Is media stream incoming", FALSE, G_PARAM_READABLE |
669       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
670   g_object_class_install_property (object_class, PROP_STATUS,
671       g_param_spec_uint ("status", "Call status",
672       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
673       G_PARAM_STATIC_BLURB));
674   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
675       g_param_spec_pointer ("audio-stream", "Audio stream data",
676       "Audio stream data",
677       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
678   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
679       g_param_spec_pointer ("video-stream", "Video stream data",
680       "Video stream data",
681       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
682 }
683
684 static void
685 empathy_tp_call_init (EmpathyTpCall *call)
686 {
687   EmpathyTpCallPriv *priv = GET_PRIV (call);
688
689   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
690   priv->contact = NULL;
691   priv->audio = g_slice_new0 (EmpathyTpCallStream);
692   priv->video = g_slice_new0 (EmpathyTpCallStream);
693   priv->audio->exists = FALSE;
694   priv->video->exists = FALSE;
695 }
696
697 EmpathyTpCall *
698 empathy_tp_call_new (TpConn *connection, TpChan *channel)
699 {
700   return g_object_new (EMPATHY_TYPE_TP_CALL,
701       "connection", connection,
702       "channel", channel,
703       NULL);
704 }
705
706 void
707 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
708 {
709   EmpathyTpCallPriv *priv = GET_PRIV (call);
710   GList *local_pendings;
711
712   empathy_debug (DEBUG_DOMAIN, "Accepting incoming call");
713
714   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
715
716   empathy_tp_group_add_member (priv->group, EMPATHY_CONTACT
717       (((EmpathyPendingInfo *) local_pendings->data)->member), NULL);
718
719   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
720   g_list_free (local_pendings);
721 }
722
723 void
724 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
725                                                 gboolean is_sending)
726 {
727   EmpathyTpCallPriv *priv = GET_PRIV (call);
728   DBusGProxy *streamed_iface;
729   guint new_direction;
730   GError *error = NULL;
731
732   empathy_debug (DEBUG_DOMAIN,
733       "Requesting video stream direction - is_sending: %d", is_sending);
734
735   if (!priv->video->exists)
736     {
737       tp_call_request_streams_for_capabilities (call, EMPATHY_CAPABILITIES_VIDEO);
738       return;
739     }
740
741   streamed_iface = tp_chan_get_interface (priv->channel,
742       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
743
744   if (is_sending)
745     {
746       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
747     }
748   else
749     {
750       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
751     }
752
753   if (!tp_chan_type_streamed_media_request_stream_direction (streamed_iface,
754         priv->video->id, new_direction, &error))
755     {
756       empathy_debug (DEBUG_DOMAIN,
757           "Couldn't request video stream direction: %s", error->message);
758       g_clear_error (&error);
759     }
760 }
761
762 void
763 empathy_tp_call_close_channel (EmpathyTpCall *call)
764 {
765   EmpathyTpCallPriv *priv = GET_PRIV (call);
766   GError *error = NULL;
767
768   empathy_debug (DEBUG_DOMAIN, "Closing channel");
769
770   if (!tp_chan_close (DBUS_G_PROXY (priv->channel), &error))
771     {
772       empathy_debug (DEBUG_DOMAIN, "Error closing channel: %s",
773           error ? error->message : "No error given");
774       g_clear_error (&error);
775     }
776 }
777
778 void
779 empathy_tp_call_add_preview_video (guint preview_video_socket_id)
780 {
781   GError *error = NULL;
782   DBusGProxy *ch_proxy;
783
784   empathy_debug (DEBUG_DOMAIN, "Adding preview video");
785
786   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
787       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
788
789   if (!org_freedesktop_Telepathy_StreamEngine_add_preview_window (ch_proxy,
790         preview_video_socket_id, &error))
791     {
792       empathy_debug (DEBUG_DOMAIN, "Couldn't set video preview: %s",
793           error->message);
794       g_clear_error (&error);
795     }
796 }
797
798 void
799 empathy_tp_call_remove_preview_video (guint preview_video_socket_id)
800 {
801   GError *error = NULL;
802   DBusGProxy *ch_proxy;
803
804   empathy_debug (DEBUG_DOMAIN, "Removing preview video");
805
806   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
807       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
808
809   if (!org_freedesktop_Telepathy_StreamEngine_remove_preview_window (ch_proxy,
810         preview_video_socket_id, &error))
811     {
812       empathy_debug (DEBUG_DOMAIN, "Couldn't set video preview: %s",
813           error->message);
814       g_clear_error (&error);
815     }
816 }
817
818 void
819 empathy_tp_call_add_output_video (EmpathyTpCall *call,
820                                   guint output_video_socket_id)
821 {
822   EmpathyTpCallPriv *priv = GET_PRIV (call);
823   const gchar *object_path;
824   DBusGProxy *ch_proxy;
825   GError *error = NULL;
826
827   empathy_debug (DEBUG_DOMAIN, "Adding output video - socket: %d",
828       output_video_socket_id);
829
830   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
831   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
832       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
833
834   if (!org_freedesktop_Telepathy_StreamEngine_set_output_window (ch_proxy,
835         object_path, priv->video->id, output_video_socket_id, &error))
836     {
837       empathy_debug (DEBUG_DOMAIN, "Couldn't set video output: %s",
838           error->message);
839       g_clear_error (&error);
840     }
841 }
842
843 void
844 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
845                                    guint volume)
846 {
847   EmpathyTpCallPriv *priv = GET_PRIV (call);
848   const gchar *object_path;
849   DBusGProxy *ch_proxy;
850   GError *error = NULL;
851
852   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
853     return;
854
855   empathy_debug (DEBUG_DOMAIN, "Setting output volume: %d", volume);
856
857   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
858   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
859       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
860
861   if (!org_freedesktop_Telepathy_StreamEngine_set_output_volume (ch_proxy,
862         object_path, priv->audio->id, volume, &error))
863     {
864       empathy_debug (DEBUG_DOMAIN, "Couldn't set volume: %s", error->message);
865       g_clear_error (&error);
866     }
867 }
868
869
870 void
871 empathy_tp_call_mute_output (EmpathyTpCall *call,
872                              gboolean is_muted)
873 {
874   EmpathyTpCallPriv *priv = GET_PRIV (call);
875   const gchar *object_path;
876   DBusGProxy *ch_proxy;
877   GError *error = NULL;
878
879   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
880     return;
881
882   empathy_debug (DEBUG_DOMAIN, "Setting output mute: %d", is_muted);
883
884   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
885   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
886       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
887
888   if (!org_freedesktop_Telepathy_StreamEngine_mute_output (ch_proxy,
889         object_path, priv->audio->id, is_muted, &error))
890     {
891       empathy_debug (DEBUG_DOMAIN, "Couldn't mute output: %s", error->message);
892       g_clear_error (&error);
893     }
894 }
895
896 void
897 empathy_tp_call_mute_input (EmpathyTpCall *call,
898                             gboolean is_muted)
899 {
900   EmpathyTpCallPriv *priv = GET_PRIV (call);
901   const gchar *object_path;
902   DBusGProxy *ch_proxy;
903   GError *error = NULL;
904
905   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
906     return;
907
908   empathy_debug (DEBUG_DOMAIN, "Setting input mute: %d", is_muted);
909
910   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
911   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
912       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
913
914   if (!org_freedesktop_Telepathy_StreamEngine_mute_input (ch_proxy,
915         object_path, priv->audio->id, is_muted, &error))
916     {
917       empathy_debug (DEBUG_DOMAIN, "Couldn't mute input: %s", error->message);
918       g_clear_error (&error);
919     }
920 }
921