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