]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Make sure we don't expect contact id and handle to be directly ready. Add some _run_u...
[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   gboolean stream_engine_started;
61
62   EmpathyTpCallStream *audio;
63   EmpathyTpCallStream *video;
64 };
65
66 enum
67 {
68   STATUS_CHANGED_SIGNAL,
69   RECEIVING_VIDEO_SIGNAL,
70   SENDING_VIDEO_SIGNAL,
71   LAST_SIGNAL
72 };
73
74 enum
75 {
76   PROP_0,
77   PROP_CONNECTION,
78   PROP_CHANNEL,
79   PROP_CONTACT,
80   PROP_IS_INCOMING,
81   PROP_STATUS,
82   PROP_AUDIO_STREAM,
83   PROP_VIDEO_STREAM
84 };
85
86 static guint signals[LAST_SIGNAL];
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   GList *members;
396   GList *local_pendings;
397   GList *remote_pendings;
398
399   if (priv->status > EMPATHY_TP_CALL_STATUS_READYING)
400     return;
401
402   members = empathy_tp_group_get_members (priv->group);
403   if (!members)
404     return;
405
406   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
407   remote_pendings = empathy_tp_group_get_remote_pendings (priv->group);
408
409   if (local_pendings &&
410       empathy_contact_is_user (((EmpathyPendingInfo *) local_pendings->data)->member))
411     {
412       empathy_debug (DEBUG_DOMAIN,
413           "Incoming call is ready - %p",
414           ((EmpathyPendingInfo *) local_pendings->data)->member);
415       priv->is_incoming = TRUE;
416       priv->contact = g_object_ref (members->data);
417     }
418   else if (remote_pendings && empathy_contact_is_user (members->data))
419     {
420       empathy_debug (DEBUG_DOMAIN,
421           "Outgoing call is ready - %p", remote_pendings->data);
422       priv->is_incoming = FALSE;
423       priv->contact = g_object_ref (remote_pendings->data);
424       tp_call_request_streams (call);
425     }
426
427   g_list_foreach (members, (GFunc) g_object_unref, NULL);
428   g_list_free (members);
429   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
430   g_list_free (local_pendings);
431   g_list_foreach (remote_pendings, (GFunc) g_object_unref, NULL);
432   g_list_free (remote_pendings);
433
434   if (priv->contact)
435     {
436       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
437       g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
438     }
439 }
440
441 static void
442 tp_call_member_added_cb (EmpathyTpGroup *group,
443                          EmpathyContact *contact,
444                          EmpathyContact *actor,
445                          guint reason,
446                          const gchar *message,
447                          EmpathyTpCall *call)
448 {
449   EmpathyTpCallPriv *priv = GET_PRIV (call);
450
451   empathy_debug (DEBUG_DOMAIN, "New member added callback %p", contact);
452   tp_call_is_ready (call);
453
454   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING)
455     {
456       if ((priv->is_incoming && contact != priv->contact) ||
457           (!priv->is_incoming && contact == priv->contact))
458         {
459           priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
460           g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
461         }
462     }
463 }
464
465 static void
466 tp_call_local_pending_cb (EmpathyTpGroup *group,
467                           EmpathyContact *contact,
468                           EmpathyContact *actor,
469                           guint reason,
470                           const gchar *message,
471                           EmpathyTpCall *call)
472 {
473   empathy_debug (DEBUG_DOMAIN, "New local pending added callback %p", contact);
474   tp_call_is_ready (call);
475 }
476
477 static void
478 tp_call_remote_pending_cb (EmpathyTpGroup *group,
479                            EmpathyContact *contact,
480                            EmpathyContact *actor,
481                            guint reason,
482                            const gchar *message,
483                            EmpathyTpCall *call)
484 {
485   empathy_debug (DEBUG_DOMAIN, "New remote pending added callback %p", contact);
486   tp_call_is_ready (call);
487 }
488
489 static void
490 tp_call_async_cb (TpProxy *proxy,
491                   const GError *error,
492                   gpointer user_data,
493                   GObject *call)
494 {
495   if (error)
496     {
497       empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
498           user_data, error->message);
499     }
500 }
501
502 static void
503 tp_call_invalidated_cb (TpProxy       *stream_engine,
504                         GQuark         domain,
505                         gint           code,
506                         gchar         *message,
507                         EmpathyTpCall *call)
508 {
509   empathy_debug (DEBUG_DOMAIN, "Stream engine proxy invalidated: %s",
510       message);
511   empathy_tp_call_close_channel (call);
512 }
513
514 static void
515 tp_call_watch_name_owner_cb (TpDBusDaemon *daemon,
516                              const gchar *name,
517                              const gchar *new_owner,
518                              gpointer call)
519 {
520   EmpathyTpCallPriv *priv = GET_PRIV (call);
521
522   /* G_STR_EMPTY(new_owner) means either stream-engine has not started yet or
523    * has crashed. We want to close the channel if stream-engine has crashed.
524    * */
525   empathy_debug (DEBUG_DOMAIN,
526                  "Watch SE: name='%s' SE started='%s' new_owner='%s'",
527                  name, priv->stream_engine_started ? "yes" : "no",
528                  new_owner ? new_owner : "none");
529   if (priv->stream_engine_started && G_STR_EMPTY (new_owner))
530     {
531       empathy_debug (DEBUG_DOMAIN, "Stream engine falled off the bus");
532       empathy_tp_call_close_channel (call);
533     }
534   priv->stream_engine_started = ! G_STR_EMPTY (new_owner);
535 }
536
537 static void
538 tp_call_start_stream_engine (EmpathyTpCall *call)
539 {
540   EmpathyTpCallPriv *priv = GET_PRIV (call);
541
542   empathy_debug (DEBUG_DOMAIN, "Revving up the stream engine");
543
544   priv->stream_engine = g_object_new (TP_TYPE_PROXY,
545       "bus-name", STREAM_ENGINE_BUS_NAME,
546       "dbus-connection", tp_get_bus (),
547       "object-path", STREAM_ENGINE_OBJECT_PATH,
548        NULL);
549   tp_proxy_add_interface_by_id (priv->stream_engine,
550       EMP_IFACE_QUARK_STREAM_ENGINE);
551   tp_proxy_add_interface_by_id (priv->stream_engine,
552       EMP_IFACE_QUARK_CHANNEL_HANDLER);
553
554   g_signal_connect (priv->stream_engine, "invalidated",
555       G_CALLBACK (tp_call_invalidated_cb),
556       call);
557   
558   /* FIXME: dbus daemon should be unique */
559   priv->dbus_daemon = tp_dbus_daemon_new (tp_get_bus ());
560   tp_dbus_daemon_watch_name_owner (priv->dbus_daemon, STREAM_ENGINE_BUS_NAME,
561       tp_call_watch_name_owner_cb,
562       call, NULL);
563
564   emp_cli_channel_handler_call_handle_channel (priv->stream_engine, -1,
565         dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->connection)),
566         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->connection)),
567         priv->channel->type,
568         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
569         priv->channel->handle_type, priv->channel->handle,
570         tp_call_async_cb,
571         "calling handle channel", NULL,
572         G_OBJECT (call));
573 }
574
575 static GObject *
576 tp_call_constructor (GType type,
577                      guint n_construct_params,
578                      GObjectConstructParam *construct_params)
579 {
580   GObject *object;
581   EmpathyTpCall *call;
582   EmpathyTpCallPriv *priv;
583   DBusGProxy *streamed_iface;
584   MissionControl *mc;
585   McAccount *account;
586
587   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
588       n_construct_params, construct_params);
589
590   call = EMPATHY_TP_CALL (object);
591   priv = GET_PRIV (call);
592
593   dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->channel), "Closed",
594      G_CALLBACK (tp_call_channel_closed_cb), (gpointer) call, NULL);
595
596   streamed_iface = tp_chan_get_interface (priv->channel,
597       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
598   dbus_g_proxy_connect_signal (streamed_iface, "StreamStateChanged",
599       G_CALLBACK (tp_call_stream_state_changed_cb),
600       (gpointer) call, NULL);
601   dbus_g_proxy_connect_signal (streamed_iface, "StreamDirectionChanged",
602       G_CALLBACK (tp_call_stream_direction_changed_cb),
603       (gpointer) call, NULL);
604   dbus_g_proxy_connect_signal (streamed_iface, "StreamAdded",
605       G_CALLBACK (tp_call_stream_added_cb), (gpointer) call, NULL);
606   dbus_g_proxy_connect_signal (streamed_iface, "StreamRemoved",
607       G_CALLBACK (tp_call_stream_removed_cb), (gpointer) call, NULL);
608
609   mc = empathy_mission_control_new ();
610   account = mission_control_get_account_for_connection (mc, priv->connection,
611       NULL);
612   priv->group = empathy_tp_group_new (account, priv->channel);
613   g_object_unref (mc);
614
615   g_signal_connect (G_OBJECT (priv->group), "member-added",
616       G_CALLBACK (tp_call_member_added_cb), (gpointer) call);
617   g_signal_connect (G_OBJECT (priv->group), "local-pending",
618       G_CALLBACK (tp_call_local_pending_cb), (gpointer) call);
619   g_signal_connect (G_OBJECT (priv->group), "remote-pending",
620       G_CALLBACK (tp_call_remote_pending_cb), (gpointer) call);
621
622   tp_call_start_stream_engine (call);
623   /* FIXME: unnecessary for outgoing? */
624   tp_call_identify_streams (call);
625
626   return object;
627 }
628
629 static void 
630 tp_call_finalize (GObject *object)
631 {
632   EmpathyTpCallPriv *priv = GET_PRIV (object);
633
634   empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
635
636   g_slice_free (EmpathyTpCallStream, priv->audio);
637   g_slice_free (EmpathyTpCallStream, priv->video);
638   g_object_unref (priv->group);
639
640   if (priv->connection != NULL)
641     g_object_unref (priv->connection);
642
643   if (priv->channel != NULL)
644     g_object_unref (priv->channel);
645
646   if (priv->stream_engine != NULL)
647     g_object_unref (priv->stream_engine);
648
649   if (priv->contact != NULL)
650       g_object_unref (priv->contact);
651
652   if (priv->dbus_daemon != NULL)
653     {
654       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus_daemon,
655           STREAM_ENGINE_BUS_NAME,
656           tp_call_watch_name_owner_cb,
657           object);
658       g_object_unref (priv->dbus_daemon);
659     }
660
661   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
662 }
663
664 static void 
665 tp_call_set_property (GObject *object,
666                       guint prop_id,
667                       const GValue *value,
668                       GParamSpec *pspec)
669 {
670   EmpathyTpCallPriv *priv = GET_PRIV (object);
671
672   switch (prop_id)
673     {
674     case PROP_CONNECTION:
675       priv->connection = g_value_dup_object (value);
676       break;
677     case PROP_CHANNEL:
678       priv->channel = g_value_dup_object (value);
679       break;
680     case PROP_CONTACT:
681       /* FIXME should this one be writable in the first place ? */
682       g_assert (priv->contact == NULL);
683       priv->contact = g_value_dup_object (value);
684       break;
685     case PROP_IS_INCOMING:
686       priv->is_incoming = g_value_get_boolean (value);
687       break;
688     case PROP_STATUS:
689       priv->status = g_value_get_uint (value);
690       break;
691     case PROP_AUDIO_STREAM:
692       priv->audio = g_value_get_pointer (value);
693       break;
694     case PROP_VIDEO_STREAM:
695       priv->video = g_value_get_pointer (value);
696       break;
697     default:
698       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
699       break;
700   }
701 }
702
703
704 static void
705 tp_call_get_property (GObject *object,
706                       guint prop_id,
707                       GValue *value,
708                       GParamSpec *pspec)
709 {
710   EmpathyTpCallPriv *priv = GET_PRIV (object);
711
712   switch (prop_id)
713     {
714     case PROP_CONNECTION:
715       g_value_set_object (value, priv->connection);
716       break;
717     case PROP_CHANNEL:
718       g_value_set_object (value, priv->channel);
719       break;
720     case PROP_CONTACT:
721       g_value_set_object (value, priv->contact);
722       break;
723     case PROP_IS_INCOMING:
724       g_value_set_boolean (value, priv->is_incoming);
725       break;
726     case PROP_STATUS:
727       g_value_set_uint (value, priv->status);
728       break;
729     case PROP_AUDIO_STREAM:
730       g_value_set_pointer (value, priv->audio);
731       break;
732     case PROP_VIDEO_STREAM:
733       g_value_set_pointer (value, priv->video);
734       break;
735     default:
736       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
737       break;
738   }
739 }
740
741 static void
742 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
743 {
744   GObjectClass *object_class = G_OBJECT_CLASS (klass);
745
746   emp_cli_init ();
747
748   object_class->constructor = tp_call_constructor;
749   object_class->finalize = tp_call_finalize;
750   object_class->set_property = tp_call_set_property;
751   object_class->get_property = tp_call_get_property;
752
753   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
754
755   signals[STATUS_CHANGED_SIGNAL] =
756       g_signal_new ("status-changed", G_TYPE_FROM_CLASS (klass),
757       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID,
758       G_TYPE_NONE, 0);
759   signals[RECEIVING_VIDEO_SIGNAL] =
760       g_signal_new ("receiving-video", G_TYPE_FROM_CLASS (klass),
761       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
762       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
763   signals[SENDING_VIDEO_SIGNAL] =
764       g_signal_new ("sending-video", G_TYPE_FROM_CLASS (klass),
765       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
766       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
767
768   g_object_class_install_property (object_class, PROP_CONNECTION,
769       g_param_spec_object ("connection", "connection", "connection",
770       TELEPATHY_CONN_TYPE,
771       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
772       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
773   g_object_class_install_property (object_class, PROP_CHANNEL,
774       g_param_spec_object ("channel", "channel", "channel",
775       TELEPATHY_CHAN_TYPE,
776       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
777       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
778   g_object_class_install_property (object_class, PROP_CONTACT,
779       g_param_spec_object ("contact", "Call contact", "Call contact",
780       EMPATHY_TYPE_CONTACT,
781       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
782   g_object_class_install_property (object_class, PROP_IS_INCOMING,
783       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
784       "Is media stream incoming", FALSE, G_PARAM_READABLE |
785       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
786   g_object_class_install_property (object_class, PROP_STATUS,
787       g_param_spec_uint ("status", "Call status",
788       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
789       G_PARAM_STATIC_BLURB));
790   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
791       g_param_spec_pointer ("audio-stream", "Audio stream data",
792       "Audio stream data",
793       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
794   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
795       g_param_spec_pointer ("video-stream", "Video stream data",
796       "Video stream data",
797       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
798 }
799
800 static void
801 empathy_tp_call_init (EmpathyTpCall *call)
802 {
803   EmpathyTpCallPriv *priv = GET_PRIV (call);
804
805   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
806   priv->contact = NULL;
807   priv->stream_engine_started = FALSE;
808   priv->audio = g_slice_new0 (EmpathyTpCallStream);
809   priv->video = g_slice_new0 (EmpathyTpCallStream);
810   priv->audio->exists = FALSE;
811   priv->video->exists = FALSE;
812 }
813
814 EmpathyTpCall *
815 empathy_tp_call_new (TpConn *connection, TpChan *channel)
816 {
817   return g_object_new (EMPATHY_TYPE_TP_CALL,
818       "connection", connection,
819       "channel", channel,
820       NULL);
821 }
822
823 void
824 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
825 {
826   EmpathyTpCallPriv *priv = GET_PRIV (call);
827   EmpathyContact *self_contact;
828
829   empathy_debug (DEBUG_DOMAIN, "Accepting incoming call");
830
831   self_contact = empathy_tp_group_get_self_contact (priv->group);
832   empathy_tp_group_add_member (priv->group, self_contact, NULL);
833   g_object_unref (self_contact);
834 }
835
836 void
837 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
838                                                 gboolean is_sending)
839 {
840   EmpathyTpCallPriv *priv = GET_PRIV (call);
841   DBusGProxy *streamed_iface;
842   guint new_direction;
843   GError *error = NULL;
844
845   empathy_debug (DEBUG_DOMAIN,
846       "Requesting video stream direction - is_sending: %d", is_sending);
847
848   if (!priv->video->exists)
849     {
850       tp_call_request_streams_for_capabilities (call, EMPATHY_CAPABILITIES_VIDEO);
851       return;
852     }
853
854   streamed_iface = tp_chan_get_interface (priv->channel,
855       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
856
857   if (is_sending)
858     {
859       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
860     }
861   else
862     {
863       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
864     }
865
866   if (!tp_chan_type_streamed_media_request_stream_direction (streamed_iface,
867         priv->video->id, new_direction, &error))
868     {
869       empathy_debug (DEBUG_DOMAIN,
870           "Couldn't request video stream direction: %s", error->message);
871       g_clear_error (&error);
872     }
873 }
874
875 void
876 empathy_tp_call_close_channel (EmpathyTpCall *call)
877 {
878   EmpathyTpCallPriv *priv = GET_PRIV (call);
879   GError *error = NULL;
880
881   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
882       return;
883
884   empathy_debug (DEBUG_DOMAIN, "Closing channel");
885
886   if (!tp_chan_close (DBUS_G_PROXY (priv->channel), &error))
887     {
888       empathy_debug (DEBUG_DOMAIN, "Error closing channel: %s",
889           error ? error->message : "No error given");
890       g_clear_error (&error);
891     }
892   else
893         priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
894 }
895
896 void
897 empathy_tp_call_add_preview_video (EmpathyTpCall *call,
898                                    guint preview_video_socket_id)
899 {
900   EmpathyTpCallPriv *priv = GET_PRIV (call);
901
902   empathy_debug (DEBUG_DOMAIN, "Adding preview video");
903
904   emp_cli_stream_engine_call_add_preview_window (priv->stream_engine, -1,
905       preview_video_socket_id,
906       tp_call_async_cb,
907       "adding preview window", NULL,
908       G_OBJECT (call));
909 }
910
911 void
912 empathy_tp_call_remove_preview_video (EmpathyTpCall *call,
913                                       guint preview_video_socket_id)
914 {
915   EmpathyTpCallPriv *priv = GET_PRIV (call);
916
917   empathy_debug (DEBUG_DOMAIN, "Removing preview video");
918
919   emp_cli_stream_engine_call_remove_preview_window (priv->stream_engine, -1,
920       preview_video_socket_id,
921       tp_call_async_cb,
922       "removing preview window", NULL,
923       G_OBJECT (call));
924 }
925
926 void
927 empathy_tp_call_add_output_video (EmpathyTpCall *call,
928                                   guint output_video_socket_id)
929 {
930   EmpathyTpCallPriv *priv = GET_PRIV (call);
931
932   empathy_debug (DEBUG_DOMAIN, "Adding output video - socket: %d",
933       output_video_socket_id);
934
935   emp_cli_stream_engine_call_set_output_window (priv->stream_engine, -1,
936       dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
937       priv->video->id, output_video_socket_id,
938       tp_call_async_cb,
939       "setting output window", NULL,
940       G_OBJECT (call));
941 }
942
943 void
944 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
945                                    guint volume)
946 {
947   EmpathyTpCallPriv *priv = GET_PRIV (call);
948
949   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
950     return;
951
952   empathy_debug (DEBUG_DOMAIN, "Setting output volume: %d", volume);
953
954   emp_cli_stream_engine_call_set_output_volume (priv->stream_engine, -1,
955       dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
956       priv->audio->id, volume,
957       tp_call_async_cb,
958       "setting output volume", NULL,
959       G_OBJECT (call));
960 }
961
962 void
963 empathy_tp_call_mute_output (EmpathyTpCall *call,
964                              gboolean is_muted)
965 {
966   EmpathyTpCallPriv *priv = GET_PRIV (call);
967
968   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
969     return;
970
971   empathy_debug (DEBUG_DOMAIN, "Setting output mute: %d", is_muted);
972
973   emp_cli_stream_engine_call_mute_output (priv->stream_engine, -1,
974       dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
975       priv->audio->id, is_muted,
976       tp_call_async_cb,
977       "muting output", NULL,
978       G_OBJECT (call));
979 }
980
981 void
982 empathy_tp_call_mute_input (EmpathyTpCall *call,
983                             gboolean is_muted)
984 {
985   EmpathyTpCallPriv *priv = GET_PRIV (call);
986
987   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
988     return;
989
990   empathy_debug (DEBUG_DOMAIN, "Setting input mute: %d", is_muted);
991
992   emp_cli_stream_engine_call_mute_input (priv->stream_engine, -1,
993       dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
994       priv->audio->id, is_muted,
995       tp_call_async_cb,
996       "muting input", NULL,
997       G_OBJECT (call));
998 }
999