]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Merge commit 'sjoerd/master'
[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 (EmpathyTpCall *call)
330 {
331   EmpathyTpCallPriv *priv = GET_PRIV (call);
332   EmpathyCapabilities capabilities;
333   DBusGProxy *capabilities_iface;
334
335   empathy_debug (DEBUG_DOMAIN,
336       "Requesting appropriate audio/video streams from contact");
337
338   capabilities = empathy_contact_get_capabilities (priv->contact);
339
340   /* FIXME: SIP don't have capabilities interface but we know it supports
341    *        only audio and not video. */
342   capabilities_iface = tp_conn_get_interface (priv->connection,
343       TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES);
344   if (!capabilities_iface)
345     {
346       capabilities = EMPATHY_CAPABILITIES_AUDIO;
347     }
348
349   tp_call_request_streams_for_capabilities (call, capabilities);
350 }
351
352 static void
353 tp_call_is_ready (EmpathyTpCall *call)
354 {
355   EmpathyTpCallPriv *priv = GET_PRIV (call);
356   EmpathyContact *self_contact;
357   GList *members;
358   GList *local_pendings;
359   GList *remote_pendings;
360
361   if (priv->status > EMPATHY_TP_CALL_STATUS_READYING)
362     return;
363
364   members = empathy_tp_group_get_members (priv->group);
365   if (!members)
366     return;
367
368   self_contact = empathy_tp_group_get_self_contact (priv->group);
369   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
370   remote_pendings = empathy_tp_group_get_remote_pendings (priv->group);
371
372   if (local_pendings &&
373       empathy_contact_equal (EMPATHY_CONTACT (((EmpathyPendingInfo *)
374             local_pendings->data)->member), self_contact))
375     {
376       empathy_debug (DEBUG_DOMAIN,
377           "Incoming call is ready - %p",
378           ((EmpathyPendingInfo *) local_pendings->data)->member);
379       priv->is_incoming = TRUE;
380       priv->contact = g_object_ref (members->data);
381     }
382   else if (remote_pendings &&
383       empathy_contact_equal (EMPATHY_CONTACT (members->data), self_contact))
384     {
385       empathy_debug (DEBUG_DOMAIN,
386           "Outgoing call is ready - %p", remote_pendings->data);
387       priv->is_incoming = FALSE;
388       priv->contact = g_object_ref (remote_pendings->data);
389       tp_call_request_streams (call);
390     }
391
392   g_object_unref (self_contact);
393   g_list_foreach (members, (GFunc) g_object_unref, NULL);
394   g_list_free (members);
395   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
396   g_list_free (local_pendings);
397   g_list_foreach (remote_pendings, (GFunc) g_object_unref, NULL);
398   g_list_free (remote_pendings);
399
400   if (priv->contact)
401     {
402       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
403       g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
404     }
405 }
406
407 static void
408 tp_call_member_added_cb (EmpathyTpGroup *group,
409                          EmpathyContact *contact,
410                          EmpathyContact *actor,
411                          guint reason,
412                          const gchar *message,
413                          EmpathyTpCall *call)
414 {
415   EmpathyTpCallPriv *priv = GET_PRIV (call);
416
417   empathy_debug (DEBUG_DOMAIN, "New member added callback %p", contact);
418   tp_call_is_ready (call);
419
420   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING)
421     {
422       if ((priv->is_incoming &&
423             !empathy_contact_equal (contact, priv->contact))
424           || (!priv->is_incoming &&
425             empathy_contact_equal (contact, priv->contact)))
426         {
427           priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
428           g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
429         }
430     }
431 }
432
433 static void
434 tp_call_local_pending_cb (EmpathyTpGroup *group,
435                           EmpathyContact *contact,
436                           EmpathyContact *actor,
437                           guint reason,
438                           const gchar *message,
439                           EmpathyTpCall *call)
440 {
441   empathy_debug (DEBUG_DOMAIN, "New local pending added callback %p", contact);
442   tp_call_is_ready (call);
443 }
444
445 static void
446 tp_call_remote_pending_cb (EmpathyTpGroup *group,
447                            EmpathyContact *contact,
448                            EmpathyContact *actor,
449                            guint reason,
450                            const gchar *message,
451                            EmpathyTpCall *call)
452 {
453   empathy_debug (DEBUG_DOMAIN, "New remote pending added callback %p", contact);
454   tp_call_is_ready (call);
455 }
456
457 static void
458 tp_call_start_stream_engine (EmpathyTpCall *call)
459 {
460   EmpathyTpCallPriv *priv = GET_PRIV (call);
461   DBusGProxy *ch_proxy;
462   GError *error = NULL;
463
464   empathy_debug (DEBUG_DOMAIN, "Revving up the stream engine");
465
466   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
467       STREAM_ENGINE_OBJECT_PATH, CHANNEL_HANDLER_INTERFACE);
468
469   if (!org_freedesktop_Telepathy_ChannelHandler_handle_channel (ch_proxy,
470         dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->connection)),
471         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->connection)),
472         priv->channel->type,
473         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
474         priv->channel->handle_type, priv->channel->handle, &error))
475     {
476       empathy_debug (DEBUG_DOMAIN, "Couldn't start stream engine: %s",
477           error->message);
478       g_clear_error (&error);
479     }
480 }
481
482 static GObject *
483 tp_call_constructor (GType type,
484                      guint n_construct_params,
485                      GObjectConstructParam *construct_params)
486 {
487   GObject *object;
488   EmpathyTpCall *call;
489   EmpathyTpCallPriv *priv;
490   DBusGProxy *streamed_iface;
491   MissionControl *mc;
492   McAccount *account;
493
494   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
495       n_construct_params, construct_params);
496
497   call = EMPATHY_TP_CALL (object);
498   priv = GET_PRIV (call);
499
500   dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->channel), "Closed",
501      G_CALLBACK (tp_call_channel_closed_cb), (gpointer) call, NULL);
502
503   streamed_iface = tp_chan_get_interface (priv->channel,
504       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
505   dbus_g_proxy_connect_signal (streamed_iface, "StreamStateChanged",
506       G_CALLBACK (tp_call_stream_state_changed_cb),
507       (gpointer) call, NULL);
508   dbus_g_proxy_connect_signal (streamed_iface, "StreamDirectionChanged",
509       G_CALLBACK (tp_call_stream_direction_changed_cb),
510       (gpointer) call, NULL);
511   dbus_g_proxy_connect_signal (streamed_iface, "StreamAdded",
512       G_CALLBACK (tp_call_stream_added_cb), (gpointer) call, NULL);
513   dbus_g_proxy_connect_signal (streamed_iface, "StreamRemoved",
514       G_CALLBACK (tp_call_stream_removed_cb), (gpointer) call, NULL);
515
516   mc = empathy_mission_control_new ();
517   account = mission_control_get_account_for_connection (mc, priv->connection,
518       NULL);
519   priv->group = empathy_tp_group_new (account, priv->channel);
520
521   g_signal_connect (G_OBJECT (priv->group), "member-added",
522       G_CALLBACK (tp_call_member_added_cb), (gpointer) call);
523   g_signal_connect (G_OBJECT (priv->group), "local-pending",
524       G_CALLBACK (tp_call_local_pending_cb), (gpointer) call);
525   g_signal_connect (G_OBJECT (priv->group), "remote-pending",
526       G_CALLBACK (tp_call_remote_pending_cb), (gpointer) call);
527
528   tp_call_start_stream_engine (call);
529   /* FIXME: unnecessary for outgoing? */
530   tp_call_identify_streams (call);
531
532   return object;
533 }
534
535 static void 
536 tp_call_finalize (GObject *object)
537 {
538   EmpathyTpCallPriv *priv = GET_PRIV (object);
539
540   empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
541
542   g_slice_free (EmpathyTpCallStream, priv->audio);
543   g_slice_free (EmpathyTpCallStream, priv->video);
544   g_object_unref (priv->group);
545
546   if (priv->connection != NULL)
547     g_object_unref (priv->connection);
548
549   if (priv->channel != NULL)
550     g_object_unref (priv->channel);
551
552   if (priv->contact)
553     {
554       g_object_unref (priv->contact);
555     }
556
557   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
558 }
559
560 static void 
561 tp_call_set_property (GObject *object,
562                       guint prop_id,
563                       const GValue *value,
564                       GParamSpec *pspec)
565 {
566   EmpathyTpCallPriv *priv = GET_PRIV (object);
567
568   switch (prop_id)
569     {
570     case PROP_CONNECTION:
571       priv->connection = g_value_dup_object (value);
572       break;
573     case PROP_CHANNEL:
574       priv->channel = g_value_dup_object (value);
575       break;
576     case PROP_CONTACT:
577       /* FIXME should this one be writable in the first place ? */
578       g_assert (priv->contact == NULL);
579       priv->contact = g_value_dup_object (value);
580       break;
581     case PROP_IS_INCOMING:
582       priv->is_incoming = g_value_get_boolean (value);
583       break;
584     case PROP_STATUS:
585       priv->status = g_value_get_uint (value);
586       break;
587     case PROP_AUDIO_STREAM:
588       priv->audio = g_value_get_pointer (value);
589       break;
590     case PROP_VIDEO_STREAM:
591       priv->video = g_value_get_pointer (value);
592       break;
593     default:
594       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
595       break;
596   }
597 }
598
599
600 static void
601 tp_call_get_property (GObject *object,
602                       guint prop_id,
603                       GValue *value,
604                       GParamSpec *pspec)
605 {
606   EmpathyTpCallPriv *priv = GET_PRIV (object);
607
608   switch (prop_id)
609     {
610     case PROP_CONNECTION:
611       g_value_set_object (value, priv->connection);
612       break;
613     case PROP_CHANNEL:
614       g_value_set_object (value, priv->channel);
615       break;
616     case PROP_CONTACT:
617       g_value_set_object (value, priv->contact);
618       break;
619     case PROP_IS_INCOMING:
620       g_value_set_boolean (value, priv->is_incoming);
621       break;
622     case PROP_STATUS:
623       g_value_set_uint (value, priv->status);
624       break;
625     case PROP_AUDIO_STREAM:
626       g_value_set_pointer (value, priv->audio);
627       break;
628     case PROP_VIDEO_STREAM:
629       g_value_set_pointer (value, priv->video);
630       break;
631     default:
632       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
633       break;
634   }
635 }
636
637 static void
638 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
639 {
640   GObjectClass *object_class = G_OBJECT_CLASS (klass);
641
642   object_class->constructor = tp_call_constructor;
643   object_class->finalize = tp_call_finalize;
644   object_class->set_property = tp_call_set_property;
645   object_class->get_property = tp_call_get_property;
646
647   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
648
649   signals[STATUS_CHANGED_SIGNAL] =
650       g_signal_new ("status-changed", G_TYPE_FROM_CLASS (klass),
651       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID,
652       G_TYPE_NONE, 0);
653   signals[RECEIVING_VIDEO_SIGNAL] =
654       g_signal_new ("receiving-video", G_TYPE_FROM_CLASS (klass),
655       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
656       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
657   signals[SENDING_VIDEO_SIGNAL] =
658       g_signal_new ("sending-video", G_TYPE_FROM_CLASS (klass),
659       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
660       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
661
662   g_object_class_install_property (object_class, PROP_CONNECTION,
663       g_param_spec_object ("connection", "connection", "connection",
664       TELEPATHY_CONN_TYPE,
665       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
666       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
667   g_object_class_install_property (object_class, PROP_CHANNEL,
668       g_param_spec_object ("channel", "channel", "channel",
669       TELEPATHY_CHAN_TYPE,
670       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
671       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
672   g_object_class_install_property (object_class, PROP_CONTACT,
673       g_param_spec_object ("contact", "Call contact", "Call contact",
674       EMPATHY_TYPE_CONTACT,
675       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
676   g_object_class_install_property (object_class, PROP_IS_INCOMING,
677       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
678       "Is media stream incoming", FALSE, G_PARAM_READABLE |
679       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
680   g_object_class_install_property (object_class, PROP_STATUS,
681       g_param_spec_uint ("status", "Call status",
682       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
683       G_PARAM_STATIC_BLURB));
684   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
685       g_param_spec_pointer ("audio-stream", "Audio stream data",
686       "Audio stream data",
687       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
688   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
689       g_param_spec_pointer ("video-stream", "Video stream data",
690       "Video stream data",
691       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
692 }
693
694 static void
695 empathy_tp_call_init (EmpathyTpCall *call)
696 {
697   EmpathyTpCallPriv *priv = GET_PRIV (call);
698
699   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
700   priv->contact = NULL;
701   priv->audio = g_slice_new0 (EmpathyTpCallStream);
702   priv->video = g_slice_new0 (EmpathyTpCallStream);
703   priv->audio->exists = FALSE;
704   priv->video->exists = FALSE;
705 }
706
707 EmpathyTpCall *
708 empathy_tp_call_new (TpConn *connection, TpChan *channel)
709 {
710   return g_object_new (EMPATHY_TYPE_TP_CALL,
711       "connection", connection,
712       "channel", channel,
713       NULL);
714 }
715
716 void
717 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
718 {
719   EmpathyTpCallPriv *priv = GET_PRIV (call);
720   GList *local_pendings;
721
722   empathy_debug (DEBUG_DOMAIN, "Accepting incoming call");
723
724   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
725
726   empathy_tp_group_add_member (priv->group, EMPATHY_CONTACT
727       (((EmpathyPendingInfo *) local_pendings->data)->member), NULL);
728
729   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
730   g_list_free (local_pendings);
731 }
732
733 void
734 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
735                                                 gboolean is_sending)
736 {
737   EmpathyTpCallPriv *priv = GET_PRIV (call);
738   DBusGProxy *streamed_iface;
739   guint new_direction;
740   GError *error = NULL;
741
742   empathy_debug (DEBUG_DOMAIN,
743       "Requesting video stream direction - is_sending: %d", is_sending);
744
745   if (!priv->video->exists)
746     {
747       tp_call_request_streams_for_capabilities (call, EMPATHY_CAPABILITIES_VIDEO);
748       return;
749     }
750
751   streamed_iface = tp_chan_get_interface (priv->channel,
752       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
753
754   if (is_sending)
755     {
756       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
757     }
758   else
759     {
760       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
761     }
762
763   if (!tp_chan_type_streamed_media_request_stream_direction (streamed_iface,
764         priv->video->id, new_direction, &error))
765     {
766       empathy_debug (DEBUG_DOMAIN,
767           "Couldn't request video stream direction: %s", error->message);
768       g_clear_error (&error);
769     }
770 }
771
772 void
773 empathy_tp_call_close_channel (EmpathyTpCall *call)
774 {
775   EmpathyTpCallPriv *priv = GET_PRIV (call);
776   GError *error = NULL;
777
778   empathy_debug (DEBUG_DOMAIN, "Closing channel");
779
780   if (!tp_chan_close (DBUS_G_PROXY (priv->channel), &error))
781     {
782       empathy_debug (DEBUG_DOMAIN, "Error closing channel: %s",
783           error ? error->message : "No error given");
784       g_clear_error (&error);
785     }
786 }
787
788 void
789 empathy_tp_call_add_preview_video (guint preview_video_socket_id)
790 {
791   GError *error = NULL;
792   DBusGProxy *ch_proxy;
793
794   empathy_debug (DEBUG_DOMAIN, "Adding preview video");
795
796   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
797       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
798
799   if (!org_freedesktop_Telepathy_StreamEngine_add_preview_window (ch_proxy,
800         preview_video_socket_id, &error))
801     {
802       empathy_debug (DEBUG_DOMAIN, "Couldn't set video preview: %s",
803           error->message);
804       g_clear_error (&error);
805     }
806 }
807
808 void
809 empathy_tp_call_remove_preview_video (guint preview_video_socket_id)
810 {
811   GError *error = NULL;
812   DBusGProxy *ch_proxy;
813
814   empathy_debug (DEBUG_DOMAIN, "Removing preview video");
815
816   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
817       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
818
819   if (!org_freedesktop_Telepathy_StreamEngine_remove_preview_window (ch_proxy,
820         preview_video_socket_id, &error))
821     {
822       empathy_debug (DEBUG_DOMAIN, "Couldn't set video preview: %s",
823           error->message);
824       g_clear_error (&error);
825     }
826 }
827
828 void
829 empathy_tp_call_add_output_video (EmpathyTpCall *call,
830                                   guint output_video_socket_id)
831 {
832   EmpathyTpCallPriv *priv = GET_PRIV (call);
833   const gchar *object_path;
834   DBusGProxy *ch_proxy;
835   GError *error = NULL;
836
837   empathy_debug (DEBUG_DOMAIN, "Adding output video - socket: %d",
838       output_video_socket_id);
839
840   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
841   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
842       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
843
844   if (!org_freedesktop_Telepathy_StreamEngine_set_output_window (ch_proxy,
845         object_path, priv->video->id, output_video_socket_id, &error))
846     {
847       empathy_debug (DEBUG_DOMAIN, "Couldn't set video output: %s",
848           error->message);
849       g_clear_error (&error);
850     }
851 }
852
853 void
854 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
855                                    guint volume)
856 {
857   EmpathyTpCallPriv *priv = GET_PRIV (call);
858   const gchar *object_path;
859   DBusGProxy *ch_proxy;
860   GError *error = NULL;
861
862   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
863     return;
864
865   empathy_debug (DEBUG_DOMAIN, "Setting output volume: %d", volume);
866
867   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
868   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
869       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
870
871   if (!org_freedesktop_Telepathy_StreamEngine_set_output_volume (ch_proxy,
872         object_path, priv->audio->id, volume, &error))
873     {
874       empathy_debug (DEBUG_DOMAIN, "Couldn't set volume: %s", error->message);
875       g_clear_error (&error);
876     }
877 }
878
879
880 void
881 empathy_tp_call_mute_output (EmpathyTpCall *call,
882                              gboolean is_muted)
883 {
884   EmpathyTpCallPriv *priv = GET_PRIV (call);
885   const gchar *object_path;
886   DBusGProxy *ch_proxy;
887   GError *error = NULL;
888
889   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
890     return;
891
892   empathy_debug (DEBUG_DOMAIN, "Setting output mute: %d", is_muted);
893
894   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
895   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
896       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
897
898   if (!org_freedesktop_Telepathy_StreamEngine_mute_output (ch_proxy,
899         object_path, priv->audio->id, is_muted, &error))
900     {
901       empathy_debug (DEBUG_DOMAIN, "Couldn't mute output: %s", error->message);
902       g_clear_error (&error);
903     }
904 }
905
906 void
907 empathy_tp_call_mute_input (EmpathyTpCall *call,
908                             gboolean is_muted)
909 {
910   EmpathyTpCallPriv *priv = GET_PRIV (call);
911   const gchar *object_path;
912   DBusGProxy *ch_proxy;
913   GError *error = NULL;
914
915   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
916     return;
917
918   empathy_debug (DEBUG_DOMAIN, "Setting input mute: %d", is_muted);
919
920   object_path = dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel));
921   ch_proxy = dbus_g_proxy_new_for_name (tp_get_bus (), STREAM_ENGINE_BUS_NAME,
922       STREAM_ENGINE_OBJECT_PATH, STREAM_ENGINE_INTERFACE);
923
924   if (!org_freedesktop_Telepathy_StreamEngine_mute_input (ch_proxy,
925         object_path, priv->audio->id, is_muted, &error))
926     {
927       empathy_debug (DEBUG_DOMAIN, "Couldn't mute input: %s", error->message);
928       g_clear_error (&error);
929     }
930 }
931