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