]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Import tools from telepathy-glib 0.7.3 and build a static libemp-extensions.la.
[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       return;
133     }
134
135   for (i = 0; i < stream_infos->len; i++)
136     {
137       GValueArray *values;
138       guint stream_id;
139       guint stream_handle;
140       guint stream_type;
141       guint stream_state;
142       guint stream_direction;
143
144       values = g_ptr_array_index (stream_infos, i);
145       stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
146       stream_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
147       stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
148       stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
149       stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
150
151       switch (stream_type)
152         {
153         case TP_MEDIA_STREAM_TYPE_AUDIO:
154           empathy_debug (DEBUG_DOMAIN,
155               "Audio stream - id: %d, state: %d, direction: %d",
156               stream_id, stream_state, stream_direction);
157           priv->audio->exists = TRUE;
158           priv->audio->id = stream_id;
159           priv->audio->state = stream_state;
160           priv->audio->direction = stream_direction;
161           break;
162         case TP_MEDIA_STREAM_TYPE_VIDEO:
163           empathy_debug (DEBUG_DOMAIN,
164               "Video stream - id: %d, state: %d, direction: %d",
165               stream_id, stream_state, stream_direction);
166           priv->video->exists = TRUE;
167           priv->video->id = stream_id;
168           priv->video->state = stream_state;
169           priv->video->direction = stream_direction;
170           break;
171         default:
172           empathy_debug (DEBUG_DOMAIN, "Unknown stream type: %d",
173               stream_type);
174         }
175
176       g_value_array_free (values);
177     }
178 }
179
180 static void
181 tp_call_stream_added_cb (DBusGProxy *channel,
182                          guint stream_id,
183                          guint contact_handle,
184                          guint stream_type,
185                          EmpathyTpCall *call)
186 {
187   empathy_debug (DEBUG_DOMAIN,
188       "Stream added - stream id: %d, contact handle: %d, stream type: %d",
189       stream_id, contact_handle, stream_type);
190
191   tp_call_identify_streams (call);
192 }
193
194
195 static void
196 tp_call_stream_removed_cb (DBusGProxy *channel,
197                            guint stream_id,
198                            EmpathyTpCall *call)
199 {
200   EmpathyTpCallPriv *priv = GET_PRIV (call);
201
202   empathy_debug (DEBUG_DOMAIN, "Stream removed - stream id: %d", stream_id);
203
204   if (stream_id == priv->audio->id)
205     {
206       priv->audio->exists = FALSE;
207     }
208   else if (stream_id == priv->video->id)
209     {
210       priv->video->exists = FALSE;
211     }
212 }
213
214 static void
215 tp_call_channel_closed_cb (TpChan *channel,
216                            EmpathyTpCall *call)
217 {
218   EmpathyTpCallPriv *priv = GET_PRIV (call);
219   DBusGProxy *streamed_iface;
220   DBusGProxy *group_iface;
221
222   empathy_debug (DEBUG_DOMAIN, "Channel closed");
223
224   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
225   g_signal_emit_by_name (call, "status-changed");
226
227   streamed_iface = tp_chan_get_interface (priv->channel,
228       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
229   group_iface = tp_chan_get_interface (priv->channel,
230       TELEPATHY_CHAN_IFACE_GROUP_QUARK);
231
232   dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->channel), "Closed",
233       G_CALLBACK (tp_call_channel_closed_cb), (gpointer) call);
234   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamStateChanged",
235       G_CALLBACK (tp_call_stream_state_changed_cb), (gpointer) call);
236   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamAdded",
237       G_CALLBACK (tp_call_stream_added_cb), (gpointer) call);
238   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamRemoved",
239       G_CALLBACK (tp_call_stream_removed_cb), (gpointer) call);
240 }
241
242 static void
243 tp_call_stream_direction_changed_cb (DBusGProxy *channel,
244                                      guint stream_id,
245                                      guint stream_direction,
246                                      guint flags,
247                                      EmpathyTpCall *call)
248 {
249   EmpathyTpCallPriv *priv = GET_PRIV (call);
250
251   empathy_debug (DEBUG_DOMAIN,
252       "Stream direction changed - stream: %d, direction: %d",
253       stream_id, stream_direction);
254
255   if (stream_id == priv->audio->id)
256     {
257       priv->audio->direction = stream_direction;
258     }
259   else if (stream_id == priv->video->id)
260     {
261       priv->video->direction = stream_direction;
262
263       if (stream_direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE)
264         {
265           empathy_debug (DEBUG_DOMAIN, "RECEIVING");
266           g_signal_emit_by_name (call, "receiving-video", TRUE);
267         }
268       else
269         {
270           empathy_debug (DEBUG_DOMAIN, "NOT RECEIVING");
271           g_signal_emit_by_name (call, "receiving-video", FALSE);
272         }
273
274       if (stream_direction & TP_MEDIA_STREAM_DIRECTION_SEND)
275         {
276           empathy_debug (DEBUG_DOMAIN, "SENDING");
277           g_signal_emit_by_name (call, "sending-video", TRUE);
278         }
279       else
280         {
281           empathy_debug (DEBUG_DOMAIN, "NOT SENDING");
282           g_signal_emit_by_name (call, "sending-video", FALSE);
283         }
284     }
285 }
286
287 static void
288 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
289                                           EmpathyCapabilities capabilities)
290 {
291   EmpathyTpCallPriv *priv = GET_PRIV (call);
292   DBusGProxy *streamed_iface;
293   GArray *stream_types;
294   guint handle;
295   guint stream_type;
296   GError *error = NULL;
297
298   empathy_debug (DEBUG_DOMAIN, "Requesting new stream for capabilities %d",
299       capabilities);
300
301   streamed_iface = tp_chan_get_interface (priv->channel,
302       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
303   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
304   handle = empathy_contact_get_handle (priv->contact);
305
306   if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
307     {
308       stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
309       g_array_append_val (stream_types, stream_type);
310     }
311   if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
312     {
313       stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
314       g_array_append_val (stream_types, stream_type);
315     }
316
317   if (!tp_chan_type_streamed_media_request_streams (streamed_iface, handle,
318         stream_types, NULL, &error))
319     {
320       empathy_debug (DEBUG_DOMAIN, "Couldn't request new stream: %s",
321           error->message);
322       g_clear_error (&error);
323     }
324
325   g_array_free (stream_types, TRUE);
326 }
327
328 static void
329 tp_call_request_streams_capabilities_cb (EmpathyContact *contact,
330                                          GParamSpec *property,
331                                          gpointer user_data)
332 {
333   EmpathyTpCall *call = EMPATHY_TP_CALL (user_data);
334
335   g_signal_handlers_disconnect_by_func (contact,
336       tp_call_request_streams_capabilities_cb,
337       user_data);
338
339   tp_call_request_streams_for_capabilities (call,
340      empathy_contact_get_capabilities (contact));
341 }
342
343 static void
344 tp_call_request_streams (EmpathyTpCall *call)
345 {
346   EmpathyTpCallPriv *priv = GET_PRIV (call);
347   EmpathyCapabilities capabilities;
348   DBusGProxy *capabilities_iface;
349
350   empathy_debug (DEBUG_DOMAIN,
351       "Requesting appropriate audio/video streams from contact");
352
353
354   /* FIXME: SIP don't have capabilities interface but we know it supports
355    *        only audio and not video. */
356   capabilities_iface = tp_conn_get_interface (priv->connection,
357       TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES);
358   if (!capabilities_iface)
359     {
360       capabilities = EMPATHY_CAPABILITIES_AUDIO;
361     }
362   else
363     {
364       capabilities = empathy_contact_get_capabilities (priv->contact);
365       if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
366         {
367           g_signal_connect (priv->contact, "notify::capabilities",
368               G_CALLBACK (tp_call_request_streams_capabilities_cb), call);
369           return;
370         }
371     }
372
373   tp_call_request_streams_for_capabilities (call, capabilities);
374 }
375
376 static void
377 tp_call_is_ready (EmpathyTpCall *call)
378 {
379   EmpathyTpCallPriv *priv = GET_PRIV (call);
380   EmpathyContact *self_contact;
381   GList *members;
382   GList *local_pendings;
383   GList *remote_pendings;
384
385   if (priv->status > EMPATHY_TP_CALL_STATUS_READYING)
386     return;
387
388   members = empathy_tp_group_get_members (priv->group);
389   if (!members)
390     return;
391
392   self_contact = empathy_tp_group_get_self_contact (priv->group);
393   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
394   remote_pendings = empathy_tp_group_get_remote_pendings (priv->group);
395
396   if (local_pendings &&
397       empathy_contact_equal (EMPATHY_CONTACT (((EmpathyPendingInfo *)
398             local_pendings->data)->member), self_contact))
399     {
400       empathy_debug (DEBUG_DOMAIN,
401           "Incoming call is ready - %p",
402           ((EmpathyPendingInfo *) local_pendings->data)->member);
403       priv->is_incoming = TRUE;
404       priv->contact = g_object_ref (members->data);
405     }
406   else if (remote_pendings &&
407       empathy_contact_equal (EMPATHY_CONTACT (members->data), self_contact))
408     {
409       empathy_debug (DEBUG_DOMAIN,
410           "Outgoing call is ready - %p", remote_pendings->data);
411       priv->is_incoming = FALSE;
412       priv->contact = g_object_ref (remote_pendings->data);
413       tp_call_request_streams (call);
414     }
415
416   g_object_unref (self_contact);
417   g_list_foreach (members, (GFunc) g_object_unref, NULL);
418   g_list_free (members);
419   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
420   g_list_free (local_pendings);
421   g_list_foreach (remote_pendings, (GFunc) g_object_unref, NULL);
422   g_list_free (remote_pendings);
423
424   if (priv->contact)
425     {
426       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
427       g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
428     }
429 }
430
431 static void
432 tp_call_member_added_cb (EmpathyTpGroup *group,
433                          EmpathyContact *contact,
434                          EmpathyContact *actor,
435                          guint reason,
436                          const gchar *message,
437                          EmpathyTpCall *call)
438 {
439   EmpathyTpCallPriv *priv = GET_PRIV (call);
440
441   empathy_debug (DEBUG_DOMAIN, "New member added callback %p", contact);
442   tp_call_is_ready (call);
443
444   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING)
445     {
446       if ((priv->is_incoming &&
447             !empathy_contact_equal (contact, priv->contact))
448           || (!priv->is_incoming &&
449             empathy_contact_equal (contact, priv->contact)))
450         {
451           priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
452           g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
453         }
454     }
455 }
456
457 static void
458 tp_call_local_pending_cb (EmpathyTpGroup *group,
459                           EmpathyContact *contact,
460                           EmpathyContact *actor,
461                           guint reason,
462                           const gchar *message,
463                           EmpathyTpCall *call)
464 {
465   empathy_debug (DEBUG_DOMAIN, "New local pending added callback %p", contact);
466   tp_call_is_ready (call);
467 }
468
469 static void
470 tp_call_remote_pending_cb (EmpathyTpGroup *group,
471                            EmpathyContact *contact,
472                            EmpathyContact *actor,
473                            guint reason,
474                            const gchar *message,
475                            EmpathyTpCall *call)
476 {
477   empathy_debug (DEBUG_DOMAIN, "New remote pending added callback %p", contact);
478   tp_call_is_ready (call);
479 }
480
481 static void
482 tp_call_start_stream_engine (EmpathyTpCall *call)
483 {
484   EmpathyTpCallPriv *priv = GET_PRIV (call);
485   DBusGProxy *ch_proxy;
486   GError *error = NULL;
487
488   empathy_debug (DEBUG_DOMAIN, "Revving up the stream engine");
489
490   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
491       STREAM_ENGINE_OBJECT_PATH, CHANNEL_HANDLER_INTERFACE);
492
493   if (!org_freedesktop_Telepathy_ChannelHandler_handle_channel (ch_proxy,
494         dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->connection)),
495         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->connection)),
496         priv->channel->type,
497         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
498         priv->channel->handle_type, priv->channel->handle, &error))
499     {
500       empathy_debug (DEBUG_DOMAIN, "Couldn't start stream engine: %s",
501           error->message);
502       g_clear_error (&error);
503     }
504 }
505
506 static GObject *
507 tp_call_constructor (GType type,
508                      guint n_construct_params,
509                      GObjectConstructParam *construct_params)
510 {
511   GObject *object;
512   EmpathyTpCall *call;
513   EmpathyTpCallPriv *priv;
514   DBusGProxy *streamed_iface;
515   MissionControl *mc;
516   McAccount *account;
517
518   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
519       n_construct_params, construct_params);
520
521   call = EMPATHY_TP_CALL (object);
522   priv = GET_PRIV (call);
523
524   dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->channel), "Closed",
525      G_CALLBACK (tp_call_channel_closed_cb), (gpointer) call, NULL);
526
527   streamed_iface = tp_chan_get_interface (priv->channel,
528       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
529   dbus_g_proxy_connect_signal (streamed_iface, "StreamStateChanged",
530       G_CALLBACK (tp_call_stream_state_changed_cb),
531       (gpointer) call, NULL);
532   dbus_g_proxy_connect_signal (streamed_iface, "StreamDirectionChanged",
533       G_CALLBACK (tp_call_stream_direction_changed_cb),
534       (gpointer) call, NULL);
535   dbus_g_proxy_connect_signal (streamed_iface, "StreamAdded",
536       G_CALLBACK (tp_call_stream_added_cb), (gpointer) call, NULL);
537   dbus_g_proxy_connect_signal (streamed_iface, "StreamRemoved",
538       G_CALLBACK (tp_call_stream_removed_cb), (gpointer) call, NULL);
539
540   mc = empathy_mission_control_new ();
541   account = mission_control_get_account_for_connection (mc, priv->connection,
542       NULL);
543   priv->group = empathy_tp_group_new (account, priv->channel);
544
545   g_signal_connect (G_OBJECT (priv->group), "member-added",
546       G_CALLBACK (tp_call_member_added_cb), (gpointer) call);
547   g_signal_connect (G_OBJECT (priv->group), "local-pending",
548       G_CALLBACK (tp_call_local_pending_cb), (gpointer) call);
549   g_signal_connect (G_OBJECT (priv->group), "remote-pending",
550       G_CALLBACK (tp_call_remote_pending_cb), (gpointer) call);
551
552   tp_call_start_stream_engine (call);
553   /* FIXME: unnecessary for outgoing? */
554   tp_call_identify_streams (call);
555
556   return object;
557 }
558
559 static void 
560 tp_call_finalize (GObject *object)
561 {
562   EmpathyTpCallPriv *priv = GET_PRIV (object);
563
564   empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
565
566   g_slice_free (EmpathyTpCallStream, priv->audio);
567   g_slice_free (EmpathyTpCallStream, priv->video);
568   g_object_unref (priv->group);
569
570   if (priv->connection != NULL)
571     g_object_unref (priv->connection);
572
573   if (priv->channel != NULL)
574     g_object_unref (priv->channel);
575
576   if (priv->contact)
577     {
578       g_object_unref (priv->contact);
579     }
580
581   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
582 }
583
584 static void 
585 tp_call_set_property (GObject *object,
586                       guint prop_id,
587                       const GValue *value,
588                       GParamSpec *pspec)
589 {
590   EmpathyTpCallPriv *priv = GET_PRIV (object);
591
592   switch (prop_id)
593     {
594     case PROP_CONNECTION:
595       priv->connection = g_value_dup_object (value);
596       break;
597     case PROP_CHANNEL:
598       priv->channel = g_value_dup_object (value);
599       break;
600     case PROP_CONTACT:
601       /* FIXME should this one be writable in the first place ? */
602       g_assert (priv->contact == NULL);
603       priv->contact = g_value_dup_object (value);
604       break;
605     case PROP_IS_INCOMING:
606       priv->is_incoming = g_value_get_boolean (value);
607       break;
608     case PROP_STATUS:
609       priv->status = g_value_get_uint (value);
610       break;
611     case PROP_AUDIO_STREAM:
612       priv->audio = g_value_get_pointer (value);
613       break;
614     case PROP_VIDEO_STREAM:
615       priv->video = g_value_get_pointer (value);
616       break;
617     default:
618       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
619       break;
620   }
621 }
622
623
624 static void
625 tp_call_get_property (GObject *object,
626                       guint prop_id,
627                       GValue *value,
628                       GParamSpec *pspec)
629 {
630   EmpathyTpCallPriv *priv = GET_PRIV (object);
631
632   switch (prop_id)
633     {
634     case PROP_CONNECTION:
635       g_value_set_object (value, priv->connection);
636       break;
637     case PROP_CHANNEL:
638       g_value_set_object (value, priv->channel);
639       break;
640     case PROP_CONTACT:
641       g_value_set_object (value, priv->contact);
642       break;
643     case PROP_IS_INCOMING:
644       g_value_set_boolean (value, priv->is_incoming);
645       break;
646     case PROP_STATUS:
647       g_value_set_uint (value, priv->status);
648       break;
649     case PROP_AUDIO_STREAM:
650       g_value_set_pointer (value, priv->audio);
651       break;
652     case PROP_VIDEO_STREAM:
653       g_value_set_pointer (value, priv->video);
654       break;
655     default:
656       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
657       break;
658   }
659 }
660
661 static void
662 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
663 {
664   GObjectClass *object_class = G_OBJECT_CLASS (klass);
665
666   object_class->constructor = tp_call_constructor;
667   object_class->finalize = tp_call_finalize;
668   object_class->set_property = tp_call_set_property;
669   object_class->get_property = tp_call_get_property;
670
671   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
672
673   signals[STATUS_CHANGED_SIGNAL] =
674       g_signal_new ("status-changed", G_TYPE_FROM_CLASS (klass),
675       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID,
676       G_TYPE_NONE, 0);
677   signals[RECEIVING_VIDEO_SIGNAL] =
678       g_signal_new ("receiving-video", G_TYPE_FROM_CLASS (klass),
679       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
680       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
681   signals[SENDING_VIDEO_SIGNAL] =
682       g_signal_new ("sending-video", G_TYPE_FROM_CLASS (klass),
683       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
684       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
685
686   g_object_class_install_property (object_class, PROP_CONNECTION,
687       g_param_spec_object ("connection", "connection", "connection",
688       TELEPATHY_CONN_TYPE,
689       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
690       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
691   g_object_class_install_property (object_class, PROP_CHANNEL,
692       g_param_spec_object ("channel", "channel", "channel",
693       TELEPATHY_CHAN_TYPE,
694       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
695       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
696   g_object_class_install_property (object_class, PROP_CONTACT,
697       g_param_spec_object ("contact", "Call contact", "Call contact",
698       EMPATHY_TYPE_CONTACT,
699       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
700   g_object_class_install_property (object_class, PROP_IS_INCOMING,
701       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
702       "Is media stream incoming", FALSE, G_PARAM_READABLE |
703       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
704   g_object_class_install_property (object_class, PROP_STATUS,
705       g_param_spec_uint ("status", "Call status",
706       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
707       G_PARAM_STATIC_BLURB));
708   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
709       g_param_spec_pointer ("audio-stream", "Audio stream data",
710       "Audio stream data",
711       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
712   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
713       g_param_spec_pointer ("video-stream", "Video stream data",
714       "Video stream data",
715       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
716 }
717
718 static void
719 empathy_tp_call_init (EmpathyTpCall *call)
720 {
721   EmpathyTpCallPriv *priv = GET_PRIV (call);
722
723   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
724   priv->contact = NULL;
725   priv->audio = g_slice_new0 (EmpathyTpCallStream);
726   priv->video = g_slice_new0 (EmpathyTpCallStream);
727   priv->audio->exists = FALSE;
728   priv->video->exists = FALSE;
729 }
730
731 EmpathyTpCall *
732 empathy_tp_call_new (TpConn *connection, TpChan *channel)
733 {
734   return g_object_new (EMPATHY_TYPE_TP_CALL,
735       "connection", connection,
736       "channel", channel,
737       NULL);
738 }
739
740 void
741 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
742 {
743   EmpathyTpCallPriv *priv = GET_PRIV (call);
744   GList *local_pendings;
745
746   empathy_debug (DEBUG_DOMAIN, "Accepting incoming call");
747
748   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
749
750   empathy_tp_group_add_member (priv->group, EMPATHY_CONTACT
751       (((EmpathyPendingInfo *) local_pendings->data)->member), NULL);
752
753   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
754   g_list_free (local_pendings);
755 }
756
757 void
758 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
759                                                 gboolean is_sending)
760 {
761   EmpathyTpCallPriv *priv = GET_PRIV (call);
762   DBusGProxy *streamed_iface;
763   guint new_direction;
764   GError *error = NULL;
765
766   empathy_debug (DEBUG_DOMAIN,
767       "Requesting video stream direction - is_sending: %d", is_sending);
768
769   if (!priv->video->exists)
770     {
771       tp_call_request_streams_for_capabilities (call, EMPATHY_CAPABILITIES_VIDEO);
772       return;
773     }
774
775   streamed_iface = tp_chan_get_interface (priv->channel,
776       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
777
778   if (is_sending)
779     {
780       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
781     }
782   else
783     {
784       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
785     }
786
787   if (!tp_chan_type_streamed_media_request_stream_direction (streamed_iface,
788         priv->video->id, new_direction, &error))
789     {
790       empathy_debug (DEBUG_DOMAIN,
791           "Couldn't request video stream direction: %s", error->message);
792       g_clear_error (&error);
793     }
794 }
795
796 void
797 empathy_tp_call_close_channel (EmpathyTpCall *call)
798 {
799   EmpathyTpCallPriv *priv = GET_PRIV (call);
800   GError *error = NULL;
801
802   empathy_debug (DEBUG_DOMAIN, "Closing channel");
803
804   if (!tp_chan_close (DBUS_G_PROXY (priv->channel), &error))
805     {
806       empathy_debug (DEBUG_DOMAIN, "Error closing channel: %s",
807           error ? error->message : "No error given");
808       g_clear_error (&error);
809     }
810 }
811
812 void
813 empathy_tp_call_add_preview_video (guint preview_video_socket_id)
814 {
815   GError *error = NULL;
816   DBusGProxy *ch_proxy;
817
818   empathy_debug (DEBUG_DOMAIN, "Adding preview video");
819
820   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
821       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
822
823   if (!org_freedesktop_Telepathy_StreamEngine_add_preview_window (ch_proxy,
824         preview_video_socket_id, &error))
825     {
826       empathy_debug (DEBUG_DOMAIN, "Couldn't set video preview: %s",
827           error->message);
828       g_clear_error (&error);
829     }
830 }
831
832 void
833 empathy_tp_call_remove_preview_video (guint preview_video_socket_id)
834 {
835   GError *error = NULL;
836   DBusGProxy *ch_proxy;
837
838   empathy_debug (DEBUG_DOMAIN, "Removing preview video");
839
840   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
841       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
842
843   if (!org_freedesktop_Telepathy_StreamEngine_remove_preview_window (ch_proxy,
844         preview_video_socket_id, &error))
845     {
846       empathy_debug (DEBUG_DOMAIN, "Couldn't set video preview: %s",
847           error->message);
848       g_clear_error (&error);
849     }
850 }
851
852 void
853 empathy_tp_call_add_output_video (EmpathyTpCall *call,
854                                   guint output_video_socket_id)
855 {
856   EmpathyTpCallPriv *priv = GET_PRIV (call);
857   const gchar *object_path;
858   DBusGProxy *ch_proxy;
859   GError *error = NULL;
860
861   empathy_debug (DEBUG_DOMAIN, "Adding output video - socket: %d",
862       output_video_socket_id);
863
864   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
865   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
866       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
867
868   if (!org_freedesktop_Telepathy_StreamEngine_set_output_window (ch_proxy,
869         object_path, priv->video->id, output_video_socket_id, &error))
870     {
871       empathy_debug (DEBUG_DOMAIN, "Couldn't set video output: %s",
872           error->message);
873       g_clear_error (&error);
874     }
875 }
876
877 void
878 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
879                                    guint volume)
880 {
881   EmpathyTpCallPriv *priv = GET_PRIV (call);
882   const gchar *object_path;
883   DBusGProxy *ch_proxy;
884   GError *error = NULL;
885
886   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
887     return;
888
889   empathy_debug (DEBUG_DOMAIN, "Setting output volume: %d", volume);
890
891   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
892   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
893       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
894
895   if (!org_freedesktop_Telepathy_StreamEngine_set_output_volume (ch_proxy,
896         object_path, priv->audio->id, volume, &error))
897     {
898       empathy_debug (DEBUG_DOMAIN, "Couldn't set volume: %s", error->message);
899       g_clear_error (&error);
900     }
901 }
902
903
904 void
905 empathy_tp_call_mute_output (EmpathyTpCall *call,
906                              gboolean is_muted)
907 {
908   EmpathyTpCallPriv *priv = GET_PRIV (call);
909   const gchar *object_path;
910   DBusGProxy *ch_proxy;
911   GError *error = NULL;
912
913   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
914     return;
915
916   empathy_debug (DEBUG_DOMAIN, "Setting output mute: %d", is_muted);
917
918   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
919   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
920       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
921
922   if (!org_freedesktop_Telepathy_StreamEngine_mute_output (ch_proxy,
923         object_path, priv->audio->id, is_muted, &error))
924     {
925       empathy_debug (DEBUG_DOMAIN, "Couldn't mute output: %s", error->message);
926       g_clear_error (&error);
927     }
928 }
929
930 void
931 empathy_tp_call_mute_input (EmpathyTpCall *call,
932                             gboolean is_muted)
933 {
934   EmpathyTpCallPriv *priv = GET_PRIV (call);
935   const gchar *object_path;
936   DBusGProxy *ch_proxy;
937   GError *error = NULL;
938
939   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
940     return;
941
942   empathy_debug (DEBUG_DOMAIN, "Setting input mute: %d", is_muted);
943
944   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
945   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
946       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
947
948   if (!org_freedesktop_Telepathy_StreamEngine_mute_input (ch_proxy,
949         object_path, priv->audio->id, is_muted, &error))
950     {
951       empathy_debug (DEBUG_DOMAIN, "Couldn't mute input: %s", error->message);
952       g_clear_error (&error);
953     }
954 }
955