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