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