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